import { defineTool, type ExtensionAPI } from "@earendil-works/pi-coding-agent"; import { Type } from "typebox"; import { ensureComputerUseSetup, executeAct, executeEvaluateBrowser, executeExpandUi, executeInspectUi, executeLaunchBrowser, executeFind, executeNavigateBrowser, executeObserve, executeReadText, executeSearchUi, executeWaitFor, reconstructStateFromBranch, setComputerUseActivity, shutdownComputerUseSession, } from "../src/bridge.ts"; import { executeDesktopInput, executeDesktopScreenshot } from "../src/desktop.ts"; import { getComputerUseConfig, getLoadedComputerUseConfig, loadComputerUseConfig } from "../src/config.ts"; const stateId = Type.String({ description: "Required state id owning every @e ref used by this operation" }); const point = { x: Type.Number(), y: Type.Number() }; const mouseButton = Type.Optional(Type.Union([Type.Literal("left"), Type.Literal("right"), Type.Literal("middle")])); const clickByRef = Type.Object({ action: Type.Literal("click"), ref: Type.String(), button: mouseButton, clickCount: Type.Optional(Type.Number({ minimum: 1, maximum: 3 })) }); const clickByPoint = Type.Object({ action: Type.Literal("click"), ...point, button: mouseButton, clickCount: Type.Optional(Type.Number({ minimum: 1, maximum: 3 })) }); const uiAction = Type.Union([ Type.Object({ action: Type.Literal("press"), ref: Type.String({ description: "Actionable outline ref" }) }), clickByRef, clickByPoint, Type.Object({ action: Type.Literal("setText"), ref: Type.String({ description: "Editable outline ref" }), text: Type.String() }), Type.Object({ action: Type.Literal("typeText"), ref: Type.Optional(Type.String({ description: "Omit after a click to type into the focus established by that click" })), text: Type.String() }), Type.Object({ action: Type.Literal("keypress"), ref: Type.Optional(Type.String({ description: "Omit to send keys to the focused control" })), keys: Type.Array(Type.String(), { minItems: 1 }) }), Type.Object({ action: Type.Literal("scroll"), ref: Type.Optional(Type.String()), scrollX: Type.Optional(Type.Number()), scrollY: Type.Optional(Type.Number()) }), Type.Object({ action: Type.Literal("drag"), path: Type.Array(Type.Object(point), { minItems: 2 }) }), Type.Object({ action: Type.Literal("moveMouse"), ...point }), ]); const conditionProperties = { ref: Type.Optional(Type.String({ description: "Specific @e ref to test", maxLength: 128 })), scopeRef: Type.Optional(Type.String({ description: "Restrict matching to this @e subtree", maxLength: 128 })), text: Type.Optional(Type.String({ description: "Text that must match", maxLength: 512 })), role: Type.Optional(Type.String({ description: "Exact normalized role", maxLength: 128 })), value: Type.Optional(Type.String({ description: "Exact normalized value; normally pair with ref" })), until: Type.Optional(Type.Union([Type.Literal("present"), Type.Literal("absent")], { description: "Desired condition, default present" })), timeoutMs: Type.Optional(Type.Number({ description: "Maximum wait, default 10000ms", minimum: 100, maximum: 60000 })), }; const findTool = defineTool({ name: "find_roots", label: "Find Roots", description: "Find a bounded, ranked set of controllable UI roots with refs, geometry, and focus state.", promptSnippet: "Find a target root before observe_ui when needed.", parameters: Type.Object({ text: Type.Optional(Type.String({ description: "Ranked app or title text", maxLength: 256 })), app: Type.Optional(Type.String({ description: "Exact normalized app name", maxLength: 256 })), bundleId: Type.Optional(Type.String({ description: "Exact bundle id" })), pid: Type.Optional(Type.Integer({ minimum: 1, description: "Exact positive process id" })), kind: Type.Optional(Type.Union([Type.Literal("window"), Type.Literal("menu"), Type.Literal("sheet"), Type.Literal("popover"), Type.Literal("dialog"), Type.Literal("browser_page")], { description: "Exact root kind" })), }), execute: executeFind, }); const observeTool = defineTool({ name: "observe_ui", label: "Observe UI", description: "Capture the current/frontmost root or one exact @r root and return a compact list of actionable @e controls plus stateId.", promptSnippet: "Primary UI observation tool. Follow with search_ui, expand_ui, inspect_ui, or act_ui.", promptGuidelines: [ "Use mode=semantic to skip OCR and images, visual to force them, and fused for automatic selection.", "Use @e outline refs from observe_ui/search_ui for act_ui; pictureOnly refs are coordinate-only and blocked by UI-tree-only policy.", "Use search_ui for unlisted controls; expand_ui on the root ref or detail=outline for structure.", ], parameters: Type.Object({ root: Type.Optional(Type.String({ description: "Exact @r ref issued by find_roots" })), mode: Type.Optional(Type.Union([Type.Literal("semantic"), Type.Literal("visual"), Type.Literal("fused")], { description: "Observation mode, default fused" })), detail: Type.Optional(Type.Union([Type.Literal("compact"), Type.Literal("outline")], { description: "compact (default) lists actionable refs; outline returns the folded tree" })), }), execute: executeObserve, }); const searchUiTool = defineTool({ name: "search_ui", label: "Search UI", description: "Return a bounded, deterministically ranked search of the cached outline. At least one predicate is required.", promptSnippet: "Find targets not shown in the compact observe_ui output; refine broad searches instead of paging matches.", parameters: Type.Object({ text: Type.Optional(Type.String({ description: "Human-readable text or label", maxLength: 256 })), role: Type.Optional(Type.String({ description: "Exact normalized role, e.g. button", maxLength: 128 })), capability: Type.Optional(Type.String({ description: "Exact capability, e.g. press", maxLength: 128 })), stateId, }), execute: executeSearchUi, }); const expandUiTool = defineTool({ name: "expand_ui", label: "Expand UI", description: "Unfold bounded local outline context for one @e ref; use the root ref for full structure.", promptSnippet: "Expand a specific ref instead of dumping unrelated UI.", parameters: Type.Object({ ref: Type.String(), depth: Type.Optional(Type.Number({ minimum: 1, maximum: 8, description: "Subtree depth, default 3" })), stateId }), execute: executeExpandUi, }); const inspectUiTool = defineTool({ name: "inspect_ui", label: "Inspect UI", description: "Inspect one exact outline ref with fields, geometry, capabilities, and annotations.", promptSnippet: "Use when a target's evidence or provenance matters.", parameters: Type.Object({ ref: Type.String(), stateId }), execute: executeInspectUi, }); const actTool = defineTool({ name: "act_ui", label: "Act", description: "Perform one or more precisely targeted checked actions and return the successor state.", promptSnippet: "Pass dependent click/type steps together and use expect for observable completion.", promptGuidelines: ["After clicking an editable region, omit ref from typeText/keypress so input follows the established focus."], parameters: Type.Object({ stateId, expect: Type.Optional(Type.Object(conditionProperties)), actions: Type.Array(uiAction, { minItems: 1, maxItems: 20 }) }), execute: executeAct, }); const readTextTool = defineTool({ name: "read_text", label: "Read Text", description: "Read a fixed-size page from an @e UI ref or immutable @o truncated-output ref.", promptSnippet: "Use @e with its stateId; @o continuation refs don't need stateId.", parameters: Type.Object({ ref: Type.String(), offset: Type.Optional(Type.Number({ minimum: 0 })), stateId: Type.Optional(stateId) }), execute: executeReadText, }); const waitForTool = defineTool({ name: "wait_for", label: "Wait For", description: "Wait for one scoped UI condition and return the successor state.", promptSnippet: "Use after asynchronous UI changes instead of polling observe_ui.", parameters: Type.Object({ ...conditionProperties, stateId }), execute: executeWaitFor, }); const launchBrowserTool = defineTool({ name: "launch_browser", label: "Launch Browser Context", description: "Launch the configured Pi-managed CDP browser and return an observed browser-page state.", promptSnippet: "Use for browser work that needs a managed CDP context.", promptGuidelines: ["Prefer curl through bash when the page is directly fetchable."], parameters: Type.Object({ url: Type.Optional(Type.String({ maxLength: 8192 })) }), execute: executeLaunchBrowser, }); const navigateBrowserTool = defineTool({ name: "navigate_browser", label: "Navigate Browser", description: "Navigate an observed CDP browser-page state to an HTTP(S) URL.", promptSnippet: "Native browser windows use act_ui; this tool is CDP-only.", parameters: Type.Object({ url: Type.String({ maxLength: 8192 }), stateId }), execute: executeNavigateBrowser, }); const evaluateBrowserTool = defineTool({ name: "evaluate_browser", label: "Evaluate Browser", description: "Evaluate targeted JavaScript in a CDP browser-page state; returned output is strictly bounded.", promptSnippet: "Prefer observe/search/read; return selected fields, aggregates, or bounded slices.", parameters: Type.Object({ stateId, expression: Type.String({ maxLength: 65_536 }) }), execute: executeEvaluateBrowser, }); export const COMPUTER_USE_TOOLS = [findTool, observeTool, searchUiTool, expandUiTool, inspectUiTool, actTool, readTextTool, waitForTool, launchBrowserTool, navigateBrowserTool, evaluateBrowserTool]; const COMPUTER_USE_TOOL_NAMES = new Set(COMPUTER_USE_TOOLS.map((tool) => tool.name)); function formatConfigStatus(): string { const loaded = getLoadedComputerUseConfig(); return [ "pi-computer-use configuration", `browser_use: ${loaded.config.browser_use ? "enabled" : "disabled"}`, `managed_browser: ${loaded.config.managed_browser}`, `headless: ${loaded.config.headless ? "enabled" : "disabled"}`, `cursor_overlay: ${loaded.config.cursor_overlay ? "enabled" : "disabled"}`, `activity_glow: ${loaded.config.activity_glow ? "enabled" : "disabled"}`, `blocked_apps: ${loaded.config.blocked_apps.length ? loaded.config.blocked_apps.join(", ") : "(none)"} (system authentication agents are always blocked)`, ...loaded.blockedAppsErrors.map((error) => `blocked_apps error: ${error}`), "", "Sources:", ...loaded.sources.map((source) => `- ${source.path}: ${source.error ? `error: ${source.error}` : source.exists ? "loaded" : "not found"}`), `- env overrides: ${Object.keys(loaded.env).join(", ") || "none"}`, ].join("\n"); } const desktopScreenshotTool = defineTool({ name: "desktop_screenshot", label: "Desktop Screenshot", description: "Capture a whole display (all apps, menu bar, cursor). Coordinates for desktop_input are global points; the result states the image-to-point mapping.", promptSnippet: "Use for anything outside one app window; screenshot, act with desktop_input, screenshot again to verify.", parameters: Type.Object({ display: Type.Optional(Type.Integer({ minimum: 0, description: "Display index, 0 is the main display" })), maxDimension: Type.Optional(Type.Integer({ minimum: 200, maximum: 4000 })) }), execute: executeDesktopScreenshot, }); const desktopInputTool = defineTool({ name: "desktop_input", label: "Desktop Input", description: "Physical mouse and keyboard input in global screen points, like a person at the keys. Moves the real cursor; keys and text go to the FRONTMOST app, so call activateApp first. Also reads/writes clipboard text.", promptSnippet: "activateApp before typing or keys (input goes to the frontmost app); verify with desktop_screenshot; prefer act_ui for ref-targetable app controls.", parameters: Type.Object({ action: Type.Union([Type.Literal("moveMouse"), Type.Literal("click"), Type.Literal("scroll"), Type.Literal("drag"), Type.Literal("typeText"), Type.Literal("keypress"), Type.Literal("activateApp"), Type.Literal("readClipboard"), Type.Literal("writeClipboard")], { description: "moveMouse/click/scroll need x,y; drag needs path; typeText/writeClipboard need text; keypress needs keys; activateApp needs app (launches it if not running); readClipboard returns clipboard text" }), x: Type.Optional(Type.Number({ description: "Global point x" })), y: Type.Optional(Type.Number({ description: "Global point y (negative above the main display is valid)" })), button: mouseButton, clickCount: Type.Optional(Type.Integer({ minimum: 1, maximum: 3 })), scrollX: Type.Optional(Type.Integer()), scrollY: Type.Optional(Type.Integer()), path: Type.Optional(Type.Array(Type.Object(point), { minItems: 2 })), text: Type.Optional(Type.String()), keys: Type.Optional(Type.Array(Type.String(), { minItems: 1 })), app: Type.Optional(Type.String({ description: "App name or bundle id for activateApp" })), }), execute: executeDesktopInput, }); /** Complete desktop definitions for host-independent docs; runtime registration remains darwin-only. */ export const DESKTOP_TOOLS = [desktopScreenshotTool, desktopInputTool]; export default function computerUseExtension(pi: ExtensionAPI): void { let activityGlowActive = false; for (const tool of COMPUTER_USE_TOOLS) pi.registerTool(tool); if (process.platform === "darwin") for (const tool of DESKTOP_TOOLS) pi.registerTool(tool); pi.registerCommand("computer-use", { description: "Show pi-computer-use configuration", handler: async (_args, ctx) => { loadComputerUseConfig(ctx.cwd); ctx.ui.notify(formatConfigStatus(), "info"); }, }); pi.on("session_start", async (_event, ctx) => { loadComputerUseConfig(ctx.cwd); reconstructStateFromBranch(ctx); if (!ctx.hasUI) return; try { await ensureComputerUseSetup(ctx); } catch (error) { ctx.ui.notify(error instanceof Error ? error.message : String(error), "warning"); } }); pi.on("tool_execution_start", async (event) => { const config = getComputerUseConfig(); if (process.platform !== "darwin" || config.headless || !config.activity_glow || !COMPUTER_USE_TOOL_NAMES.has(event.toolName)) return; activityGlowActive = true; try { await setComputerUseActivity(true); } catch {} }); pi.on("agent_end", async () => { if (!activityGlowActive) return; activityGlowActive = false; try { await setComputerUseActivity(false); } catch {} }); pi.on("session_shutdown", async () => { activityGlowActive = false; try { await setComputerUseActivity(false); } catch {} await shutdownComputerUseSession(); }); }