From fe00d3361dece3d35f788d3eeb1c750eedbf6294 Mon Sep 17 00:00:00 2001 From: Aman Kumar <145339995+amankumarsingh77@users.noreply.github.com> Date: Wed, 22 Jul 2026 00:59:58 +0530 Subject: feat: add forms domain and durable window binding (v0.9.0) (#14) - Add forms domain with a universal field setter that auto-detects element type and fires framework-compatible input events (React native-setter trick) - Add window-binding helper to capture and bind the real Chrome windowId as the session's durable window identity on every new-window code path - Wire supporting changes across CDP session/ownership, client, navigate, isolated-tab, prompt, and state - Add manual tests for forms and window binding - Bump version 0.8.3 -> 0.9.0 Co-authored-by: Claude Opus 4.8 (1M context) --- src/prompt.ts | 208 ++++++++++++++++++++++++++-------------------------------- 1 file changed, 92 insertions(+), 116 deletions(-) (limited to 'src/prompt.ts') diff --git a/src/prompt.ts b/src/prompt.ts index 7483730..c336c0c 100644 --- a/src/prompt.ts +++ b/src/prompt.ts @@ -2,150 +2,126 @@ * System prompt guidance for browser usage. * * Injects a compact browser control reference into the pi system prompt. - * Per-tool guidelines are injected via promptGuidelines on each tool registration. - * This prompt provides only the cross-cutting patterns and tool reference table. + * This prompt carries only cross-cutting knowledge — the mental model, tool + * routing, workflows, and gotchas. Per-tool specifics (schemas, detailed usage) + * are injected separately via promptGuidelines on each tool registration, so this + * file intentionally does NOT restate full tool descriptions. */ export function getBrowserSystemPrompt(): string { return ` ## Browser Control -You have full control of a real Chrome browser via \`browser_*\` tools. -The browser connects to the user's running Chrome (not a headless instance). - -**These tools coexist with all other standard pi tools** — read, bash, edit, -write, and any other built-in tools are still available. Use browser_* tools -to interact with pages visually, and other tools alongside them. - -### Browser-Specific Tools - -| Tool | Purpose | -|------|---------| -| browser_snapshot | Accessibility-tree snapshot. Assigns every interactive element a stable **ref** (\`[eN]\`) plus \`@(x,y)\`. Refs are the primary handle for interaction tools — they survive re-renders, coordinates don't. | -| browser_screenshot | Capture a PNG or JPEG screenshot of the current page (rendered inline in the TUI; the LLM reads the image via the saved file path) | -| browser_click | Click an element by **ref** (preferred — re-resolves position, survives re-renders) or viewport x/y. Appends a compact diff of page changes. | -| browser_type | Type text into the focused element (real keystrokes; requires a focused field) | -| browser_fill | Fill an input/textarea by **ref** (preferred) or selector — updates React/Vue controlled components, verifies the value, appends a page-changes diff | -| browser_select_option | Select an option in a native by ref -→ browser_click({ ref: "e12" }) // e.g. Save — re-resolves position even if the form reflowed -// each mutating call appends a compact "Page changes" diff — read it to confirm -// the change landed (e.g. a save closed the form, a new field appeared as *[eN]). +browser_wait_for({ selector: "#email" }) // ensure the field is rendered +browser_snapshot() // interactive elements get [eN] refs +→ browser_fill({ ref: "e7", value: "a@b.com" }) +→ browser_select_option({ ref: "e9", label: "India" }) +→ browser_click({ ref: "e12" }) // re-resolves position even after reflow +// read each "Page changes" diff to confirm the step landed \`\`\` -**Why ref over selector/coordinates:** refs are keyed to the element's identity, so -they survive the re-renders that React/Vue forms trigger on every edit and save — -coordinates go stale and selectors you guess often don't match. If a call returns -"ref is stale", the page changed: re-run browser_snapshot for fresh refs. - -Prefer browser_fill for text inputs/textareas — it fires input/change so controlled -components update, and returns the field's value for verification. Use browser_click + -browser_type only for keystroke-sensitive widgets (autocomplete, masked inputs); -browser_type requires an already-focused field and errors if none is focused. -**Data extraction:** +**Navigate + verify:** \`\`\` -browser_execute_js("document.querySelector('.price').innerText") -// or for APIs: -browser_http_get("https://api.example.com/data") +browser_new_tab("https://example.com") → browser_wait_for_load() → browser_screenshot() \`\`\` -**Scrolling:** -\`\`\` -browser_screenshot() → browser_scroll({ deltaY: 500 }) → browser_screenshot() -\`\`\` -Note: deltaY follows W3C wheel-event convention: positive=down, negative=up. Default deltaY=300 scrolls down. +**Scroll:** \`browser_scroll({ deltaY: 500 })\` — W3C wheel convention: positive = down, +negative = up (default 300, down). -**Research Workflow (search + browser):** +### Parallelism + +Observation tools (\`browser_screenshot\`, \`browser_page_info\`, \`browser_execute_js\`, +\`browser_list_tabs\`, \`browser_http_get\`, \`browser_web_search\`, \`browser_read_page\`, …) +run in parallel with each other and with mutations. The harness serializes mutations +(click, type, scroll, navigate, switch_tab) so they never race. Emit independent calls +in one turn: \`\`\` -browser_navigate("https://google.com/search?q=...") → search engine in a tab -browser_open_urls(urls: ["url1", "url2", ...]) → open result pages in parallel tabs -browser_list_tabs() → see all open tabs with targetIds -browser_switch_tab(targetId: "...") → switch to a tab -browser_screenshot() → visually inspect the page -browser_execute_js("document.querySelector('.main').innerText") → extract content +browser_screenshot() + browser_page_info() + browser_execute_js("document.title") \`\`\` -**Temporary Scripts (extending the harness):** +**Multi-agent:** tab switching by one agent changes the active tab for all agents, +but per-tab data (console, network, dialogs) stays isolated. Call \`browser_current_tab\` +before a mutation to confirm you're on the expected tab. Handle any dialog reported by +\`browser_page_info\` promptly — dialogs block interaction and aren't queued across agents. + +**Session boundary & tab hygiene:** you operate only inside this session's own Chrome +window. \`browser_switch_tab\`/\`browser_close_tab\` refuse tabs this session didn't open, +and \`browser_list_tabs\` defaults to owned tabs — never reach into the user's other tabs +or windows. Close tabs when you're done with them: call \`browser_close_tab\` on any tab +you opened (via \`browser_new_tab\`/\`browser_open_urls\`) once you've extracted what you +need. Don't leave a pile of stale tabs behind — keep only what a later step still needs. +(Leftover tabs are closed automatically when the session ends, but close them yourself +as you go.) + +### Extending: temporary scripts + +When built-in tools can't express a multi-step workflow, write a script to a temp file +and run it with \`browser_run_script\`. It executes in the harness process with direct +daemon + Node access. \`\`\` -write("/tmp/extract.js", "...script with daemon access...") → browser_run_script("/tmp/extract.js") +write(.js, \` + const results = []; + for (const url of params.urls) { + await daemon.cdp("Page.navigate", { url }); + await new Promise(r => setTimeout(r, 2000)); + results.push({ url, title: await daemon.evaluateJS("document.title") }); + } + return { content: [{ type: "text", text: JSON.stringify(results, null, 2) }] }; +\`) +browser_run_script(.js, { urls: [...] }) \`\`\` - -### Additional Tools - -| Tool | Purpose | -|------|---------| -| browser_upload_file | Upload a file to a file input by **ref** (preferred) or selector (bypasses file picker) | -| browser_dispatch_key | Dispatch a DOM KeyboardEvent on an element by **ref** (preferred) or selector (for React/Vue inputs); returns details.matched | -| browser_download | Configure download directory and disable save-as prompts | -| browser_viewport_resize | Resize the viewport for responsive testing | -| browser_drag_and_drop | Perform drag-and-drop from one coordinate to another | -| browser_print_to_pdf | Print the current page to a PDF file | -| browser_get_network_log | Get buffered network request/response events | +Script bindings: params, daemon, require, signal, onUpdate, ctx, console, fetch, JSON, +Buffer, setTimeout, clearTimeout. Use a real temp path from the OS — don't hardcode \`/tmp\`. `; } -- cgit v1.2.3