diff options
| author | amankumarsingh77 <amankumarsingh7702@gmail.com> | 2026-07-27 23:26:50 +0530 |
|---|---|---|
| committer | Aman Kumar <145339995+amankumarsingh77@users.noreply.github.com> | 2026-08-02 19:12:46 +0530 |
| commit | 9f2f75dd9281153888776e5d39d4926655c15f2a (patch) | |
| tree | 89c23c8ad333b5096d39b661b142eaad544cf004 /src/domains/snapshot.ts | |
| parent | 931576443dcb990761cb7e68e354465145f9292d (diff) | |
refactor(domains): drop CDP casts, share the box-model unwrap
ref-resolve.ts and snapshot.ts had the same box-model unwrap, both using
data.model! to work around an untyped response. Extracted to domains/box.ts;
the typed schema makes the assertions unnecessary. Both derived the centre
identically (quad corner + half the model width/height), so centreOf keeps
that derivation; the shared zero-size rejection moved into boxOf, and
snapshot keeps its own off-viewport (x/y < 0) filter.
evalInIsolatedTab now goes through evaluateJson with the tab's own sessionId
and a caller-supplied guard; both callers evaluate expressions ending in
JSON.stringify, so they pass isJsonText rather than a check that checks
nothing. Runtime.evaluate/callFunctionOn result casts in form.ts, forms.ts
and keyboard.ts are replaced by named guards over the in-page result shapes.
Runtime.callFunctionOn's params schema gained arguments/returnByValue/
awaitPromise/userGesture: four domain call sites legitimately pass them and
typebox's additionalProperties:true does not widen the static type, so the
params type was rejecting valid CDP fields. Params schemas are type-only
(only results are compiled), so this is a compile-time widening with no
runtime effect.
tsc is green again for both tsconfigs.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Diffstat (limited to 'src/domains/snapshot.ts')
| -rw-r--r-- | src/domains/snapshot.ts | 35 |
1 files changed, 8 insertions, 27 deletions
diff --git a/src/domains/snapshot.ts b/src/domains/snapshot.ts index 8665e0b..e1c66d3 100644 --- a/src/domains/snapshot.ts +++ b/src/domains/snapshot.ts @@ -3,6 +3,7 @@ import { Type } from "typebox"; import { Container, Image, type ImageTheme, Markdown, Text, truncateToWidth, visibleWidth } from "@mariozechner/pi-tui"; import { getMarkdownTheme, keyHint } from "@mariozechner/pi-coding-agent"; import type { CdpSession } from "../cdp/session"; +import { type Box, boxOf } from "./box"; import { type Result, err, ok } from "../util/result"; import { defineBrowserTool, type ToolErr, type ToolOk } from "../util/tool"; import { applyTruncation } from "../util/truncate"; @@ -41,8 +42,6 @@ export type RawAxNode = { backendDOMNodeId?: number; }; -type Box = { x: number; y: number; width: number; height: number; cx: number; cy: number }; - export type SlimNode = { role: string; name?: string; @@ -273,7 +272,7 @@ export const collectInteractiveTargets = ( const liveValue = async (session: CdpSession, backendId: number): Promise<string | undefined> => { const resolved = await session.call("DOM.resolveNode", { backendNodeId: backendId }); if (!resolved.success) return undefined; - const objectId = (resolved.data as { object?: { objectId?: string } }).object?.objectId; + const objectId = resolved.data.object.objectId; if (!objectId) return undefined; const fn = `function () { if (this.type === "checkbox" || this.type === "radio") return this.checked ? "checked" : "unchecked"; @@ -286,7 +285,7 @@ const liveValue = async (session: CdpSession, backendId: number): Promise<string returnByValue: true, }); if (!r.success) return undefined; - const value = (r.data as { result?: { value?: unknown } }).result?.value; + const value = r.data.result.value; return typeof value === "string" ? value : undefined; }; @@ -358,27 +357,10 @@ export const snapshotTool = defineBrowserTool({ await Promise.allSettled( targets.map(async ({ node, backendId }) => { if (ac.signal.aborted) return; - const r = await session.call("DOM.getBoxModel", { backendNodeId: backendId }); - if (!r.success) return; - // CDP boundary cast: DOM.getBoxModel - const data = r.data as { model?: { content?: ReadonlyArray<number>; width?: number; height?: number } }; - const content = data.model?.content; - if (!content || content.length < 8) return; - // content is [x1,y1, x2,y1, x2,y2, x1,y2] in viewport CSS pixels. - const x = content[0]!; - const y = content[1]!; - const width = data.model!.width ?? 0; - const height = data.model!.height ?? 0; - // Skip zero-size and off-viewport boxes — they can't be clicked. - if (width <= 0 || height <= 0 || x < 0 || y < 0) return; - node.box = { - x: Math.round(x), - y: Math.round(y), - width: Math.round(width), - height: Math.round(height), - cx: Math.round(x + width / 2), - cy: Math.round(y + height / 2), - }; + const box = await boxOf(client, backendId); + if (!box.success) return; + if (box.data.x < 0 || box.data.y < 0) return; + node.box = box.data; // Override the AX value with the live DOM property for form controls. // The AX `value` is stale for freshly-typed / controlled inputs, so the // agent can't trust it as a read-back signal. Reading .value/.checked @@ -410,9 +392,8 @@ export const snapshotTool = defineBrowserTool({ if (args.includeScreenshot) { const shot = await session.call("Page.captureScreenshot", { format: "jpeg", quality: 80 }); if (shot.success) { - const data = (shot.data as { data: string }).data; shotPath = screenshotPath(client.namespace, "jpeg"); - writeFileSync(shotPath, Buffer.from(data, "base64")); + writeFileSync(shotPath, Buffer.from(shot.data.data, "base64")); } } |
