diff options
| author | amankumarsingh77 <amankumarsingh7702@gmal.com> | 2026-05-06 00:06:49 +0530 |
|---|---|---|
| committer | amankumarsingh77 <amankumarsingh7702@gmal.com> | 2026-05-06 00:06:49 +0530 |
| commit | 589cd294931c01b3748779635d17a13dd240af19 (patch) | |
| tree | b635f9fa443cd120de905660fe0d2665184e9a8c /src/domains/js.ts | |
| parent | 20ceaa1c6ade4950ed7d16caa5269ad2f4805db4 (diff) | |
release: v0.4.0 — accessibility snapshot, network capture, snapshot-first prompts
Adds two new tools and pivots the agent's default workflow away from
screenshots toward structured page data.
browser_snapshot — CDP accessibility tree (roles, names, states, hierarchy)
with click coordinates @(x,y) attached to every interactive element via
DOM.getBoxModel (parallel, 1.5s aggregate budget). Pass directly to
browser_click — no screenshot round-trip. Optional includeScreenshot:true
for visual confirmation. format:"json" returns a slim structure with full
box per node.
browser_network_requests — replaces the deprecated browser_get_network_log
placeholder. Filters by urlPattern (substring or /regex/), method, status,
resource type, recency. Optional response-body capture under a 5s aggregate
budget with 50KB per-body cap. Buffer is page-scoped (cleared on tab switch),
500 records/tab, with a bufferOverflowed flag on drain.
CDP plumbing: in-process aggregator in src/cdp/network-buffer.ts wired into
the existing single-consumer event loop. Accessibility domain enabled
alongside Page/DOM/Runtime/Network on every attach.
Ctrl+O expand/collapse on three tools' renderResult — browser_snapshot,
browser_network_requests, browser_execute_js. Compact-by-default views with
keyHint("app.tools.expand") that respects user keybinding remaps.
Tool prompts pivoted snapshot-first: browser_screenshot is documented as
LAST RESORT for visual verification only; browser_snapshot is the default
for understanding pages; browser_execute_js is the default for surgical
reads. browser_click and browser_open_urls guidance follows.
SKILL.md collapsed from ~342 to ~53 lines — keeping only what isn't already
in tool prompts: the decision tree, connection rules, browser_run_script
daemon bindings.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Diffstat (limited to 'src/domains/js.ts')
| -rw-r--r-- | src/domains/js.ts | 71 |
1 files changed, 65 insertions, 6 deletions
diff --git a/src/domains/js.ts b/src/domains/js.ts index bd4c3e1..14862e8 100644 --- a/src/domains/js.ts +++ b/src/domains/js.ts @@ -3,10 +3,20 @@ import { resolve, isAbsolute } from "node:path"; import { tmpdir } from "node:os"; import { createRequire } from "node:module"; import { Type } from "typebox"; +import { Markdown, Text } from "@mariozechner/pi-tui"; +import { getMarkdownTheme, keyHint } from "@mariozechner/pi-coding-agent"; import { type Result, err, ok } from "../util/result"; import { defineBrowserTool, type ToolErr, type ToolOk } from "../util/tool"; import { applyTruncation } from "../util/truncate"; +const COMPACT_PREVIEW_BYTES = 120; + +const formatBytes = (n: number): string => { + if (n >= 1_048_576) return `${(n / 1_048_576).toFixed(1)} MB`; + if (n >= 1024) return `${(n / 1024).toFixed(1)} KB`; + return `${n} B`; +}; + // AsyncFunction constructor — the documented Node.js mechanism for compiling // arbitrary user-supplied source. Equivalent to `new Function` but produces an // async function we can `await`. The full RCE risk is contained at the source @@ -29,11 +39,15 @@ export const executeJsTool = defineBrowserTool({ name: "browser_execute_js", label: "Browser Execute JS", description: - "Run JavaScript in the page (or a specific iframe target). `return X` gets auto-wrapped in an IIFE for convenience. Always use safeJs / JSON.stringify when interpolating untrusted strings into source.", - promptSnippet: "Execute JS in the page (or iframe)", + "Surgical reads from the DOM. The cheapest, most precise way to extract a specific element's text, attribute, value, or geometry. Prefer over browser_screenshot for ANY data extraction or coordinate lookup. `return X` is auto-wrapped in an IIFE. Always use safeJs / JSON.stringify when interpolating untrusted strings.", + promptSnippet: "Execute JS in the page — surgical DOM reads (preferred over screenshot)", promptGuidelines: [ + "DEFAULT for extracting a specific value: `document.querySelector('.price').innerText`, `input.value`, `el.getAttribute('aria-label')`, etc.", + "For click coordinates not already in browser_snapshot: `JSON.stringify(document.querySelector('SELECTOR').getBoundingClientRect())` — beats a screenshot every time.", + "For 'is this element visible / enabled / checked / focused', a one-liner here beats a screenshot every time.", + "DO NOT call browser_screenshot to read a value or check element state. Use this tool.", "`return foo` is auto-wrapped in an IIFE — both `foo` and `(() => foo)()` work.", - "For iframes, get a targetId via browser_execute_js with `Object.values(document.querySelectorAll('iframe'))` then call browser_execute_js with targetId.", + "For iframes, first run `Object.values(document.querySelectorAll('iframe'))` to find the iframe, then pass its targetId.", "Result must be JSON-serializable (Runtime.evaluate returnByValue=true).", ], parameters: ExecuteJsArgs, @@ -42,13 +56,58 @@ export const executeJsTool = defineBrowserTool({ if (!r.success) return err({ kind: "cdp_error", message: r.error.message }); const valueStr = r.data === undefined ? "undefined" : JSON.stringify(r.data); const truncated = await applyTruncation(valueStr, "js"); + // If the value is JSON, pretty-print for the expanded TUI view. Falls back + // to the raw string if parse fails (e.g. value is "undefined" sentinel). + let pretty: string | undefined; + try { + const parsed = JSON.parse(valueStr); + pretty = JSON.stringify(parsed, null, 2); + } catch { + pretty = undefined; + } return ok({ text: truncated.text, - details: truncated.fullOutputPath !== undefined - ? { valueLength: valueStr.length, fullOutputPath: truncated.fullOutputPath } - : { valueLength: valueStr.length }, + details: { + valueLength: valueStr.length, + // Full value for the expanded renderer. Capped at 200 KB so a giant + // dump doesn't bloat the conversation transcript on disk. + full: valueStr.length > 200_000 ? valueStr.slice(0, 200_000) : valueStr, + ...(pretty !== undefined ? { pretty: pretty.length > 200_000 ? pretty.slice(0, 200_000) : pretty } : {}), + ...(truncated.fullOutputPath !== undefined ? { fullOutputPath: truncated.fullOutputPath } : {}), + }, }); }, + + renderResult(result, expanded, theme) { + const details = result.details as + | { valueLength?: number; full?: string; pretty?: string; fullOutputPath?: string } + | undefined; + if (!details) return new Text(theme.fg("error", "execute_js: no details"), 0, 0); + + const len = details.valueLength ?? 0; + const full = details.full ?? ""; + const isJson = details.pretty !== undefined; + + if (!expanded) { + const preview = full.length > COMPACT_PREVIEW_BYTES ? full.slice(0, COMPACT_PREVIEW_BYTES) + "…" : full; + const md = [ + `**${formatBytes(len)}** ${isJson ? "JSON" : "value"}`, + "```", + preview, + "```", + keyHint("app.tools.expand", "to expand"), + ].join("\n"); + return new Markdown(md, 0, 0, getMarkdownTheme()); + } + + const body = details.pretty ?? full; + const fence = isJson ? "```json" : "```"; + const tail = details.fullOutputPath + ? `\n\nFull value at \`${details.fullOutputPath}\` · ${keyHint("app.tools.expand", "to collapse")}` + : `\n\n${keyHint("app.tools.expand", "to collapse")}`; + const md = [`**${formatBytes(len)}** ${isJson ? "JSON" : "value"}`, fence, body, "```", tail].join("\n"); + return new Markdown(md, 0, 0, getMarkdownTheme()); + }, }); const RunScriptArgs = Type.Object({ |
