Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

5.3 KB
Raw Blame
/**
 * Browser — bundled first-party plugin (ADR 0170).
 *
 * The view is chrome only. The guest page and debugger stay host-owned and are
 * reached through the public `pi.browser.*` API gated by `browser.cdp`.
 */

const ACTIONS = [
  "navigate",
  "snapshot",
  "screenshot",
  "click",
  "fill",
  "evaluate",
  "console",
  "cdp",
];

// Read-only actions that stay available in Plan and Goal modes (ADR 0211).
// navigate visits a URL/path; snapshot reads the accessibility tree;
// screenshot captures the visible page; console returns existing console
// messages. The mutating actions (click/fill/evaluate/cdp) remain
// Agent-only.
const PLAN_SAFE_ACTIONS = ["navigate", "snapshot", "screenshot", "console"];

export async function onLoad() {
  await pi.agent.registerTool({
    name: "Browser",
    description: [
      "Drive PI-Desktop's work-panel browser via CDP.",
      'Call ToolSearch for "browser" or "cdp" to load this tool.',
      "Browser operations use this session's connected page, including while its side panel is hidden or another session is visible.",
      "Navigate first if no page is connected. Opening the first page adds its Browser tab automatically.",
      "For a workspace HTML file, call BrowserPreview first; it opens a live-reloading Browser tab.",
      "Do not ask the user to open Browser manually or switch back to this session. Background operations never take focus.",
      "Then snapshot the accessibility tree, click/fill by uid, take screenshots, evaluate JavaScript,",
      "read console output, or send an allowlisted raw CDP method.",
    ].join(" "),
    risk: "medium",
    planSafeActions: PLAN_SAFE_ACTIONS,
    schema: {
      type: "object",
      properties: {
        action: {
          type: "string",
          enum: ACTIONS,
          description: "Action on the calling session's connected Browser page; the side panel need not be visible.",
        },
        url: { type: "string", description: "http(s) URL for action=navigate." },
        path: {
          type: "string",
          description: "Workspace-relative HTML file for action=navigate.",
        },
        uid: {
          type: "string",
          description: "Snapshot uid (e1, e2, …) for click/fill.",
        },
        text: { type: "string", description: "Text to type for action=fill." },
        expression: {
          type: "string",
          description: "JavaScript for action=evaluate.",
        },
        method: { type: "string", description: "Allowlisted CDP method for action=cdp." },
        params: {
          type: "object",
          description: "CDP parameters for action=cdp.",
        },
        fullPage: {
          type: "boolean",
          description: "Capture the full page for action=screenshot.",
        },
        limit: {
          type: "number",
          description: "Max console messages for action=console.",
        },
      },
      required: ["action"],
    },
    execute: async (args) => {
      const action = String(args?.action ?? "").trim();
      if (!ACTIONS.includes(action)) {
        return {
          ok: false,
          error: `unknown action "${action}"; use ${ACTIONS.join(", ")}`,
        };
      }
      switch (action) {
        case "navigate": {
          const state = await pi.browser.navigate({
            url: args?.url ? String(args.url) : undefined,
            path: args?.path ? String(args.path) : undefined,
          });
          return { ok: true, action, state };
        }
        case "snapshot":
          return { ok: true, action, ...(await pi.browser.snapshot()) };
        case "screenshot": {
          const shot = await pi.browser.screenshot({
            fullPage: args?.fullPage === true,
          });
          return {
            ok: true,
            action,
            text: shot.path
              ? `Captured screenshot (${shot.mimeType}) saved at ${shot.path}.`
              : `Captured screenshot (${shot.mimeType}).`,
            path: shot.path,
            mimeType: shot.mimeType,
            images: [{ mimeType: shot.mimeType, data: shot.data }],
          };
        }
        case "click":
          await pi.browser.click({ uid: String(args?.uid ?? "") });
          return { ok: true, action, uid: args?.uid };
        case "fill":
          await pi.browser.fill({
            uid: String(args?.uid ?? ""),
            text: String(args?.text ?? ""),
          });
          return { ok: true, action, uid: args?.uid };
        case "evaluate":
          return {
            ok: true,
            action,
            result: await pi.browser.evaluate({
              expression: String(args?.expression ?? ""),
            }),
          };
        case "console":
          return {
            ok: true,
            action,
            ...(await pi.browser.console({
              limit: typeof args?.limit === "number" ? args.limit : undefined,
            })),
          };
        case "cdp":
          return {
            ok: true,
            action,
            result: await pi.browser.cdp({
              method: String(args?.method ?? ""),
              params: args?.params,
            }),
          };
        default:
          return {
            ok: false,
            error: `unknown action "${action}"; use ${ACTIONS.join(", ")}`,
          };
      }
    },
  });
}

export async function onUnload() {
  await pi.agent.unregisterTool("Browser");
}