aboutsummaryrefslogtreecommitdiff
path: root/src/domains/setup.ts
blob: 2ddd7398d2c2bfbb9e6ab28837841ceea60da5b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
 * browser_setup tool — agent-callable browser initialization.
 *
 * The agent can call this directly when browser tools fail with
 * "not initialized" instead of asking the user. Idempotent: safe
 * to call even when already connected.
 */

import { Type } from "typebox";
import { Text } from "@mariozechner/pi-tui";
import type { Result } from "../util/result";
import { err, ok } from "../util/result";
import { defineBrowserTool, type ToolErr, type ToolOk } from "../util/tool";
import { performSetup } from "../setup";

const SetupArgs = Type.Object({});

export const setupTool = defineBrowserTool({
  name: "browser_setup",
  label: "Browser Setup",
  description:
    "Initialize browser control. Spawns the daemon, connects to Chrome, opens a test tab. " +
    "Call when browser tools fail with 'Browser harness not initialized'. Idempotent.",
  promptSnippet: "Initialize browser connection",
  promptGuidelines: [
    "Call browser_setup when you get a 'Browser harness not initialized' error from any browser tool.",
    "This tool is idempotent — calling it when already connected is harmless.",
    "After browser_setup succeeds, retry the browser tool that failed.",
  ],
  parameters: SetupArgs,
  // ensureAlive:false — this tool IS the setup; it must bypass the socket guard
  ensureAlive: false,
  renderCall: () => new Text("🔧 Initializing browser...", 0, 0),
  async handler(_args, { client, extensionCtx }): Promise<Result<ToolOk, ToolErr>> {
    // extensionCtx carries the UI, so the agent calling browser_setup gets the
    // same first-run profile picker the user gets from /browser-setup.
    const result = await performSetup(client, extensionCtx);
    if (result.success) {
      return ok({ text: result.data });
    }
    return err({ kind: "internal", message: result.error });
  },
});