From 632600ec0a0a68d9327e19bd419a1ae72047955f Mon Sep 17 00:00:00 2001 From: amankumarsingh77 Date: Tue, 28 Jul 2026 21:55:55 +0530 Subject: refactor: one door from a tool to the browser MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Four routes coexisted: cdpCall, a raw client.session().call, a session held in a local and called directly, and client.evaluateJs — and five files mixed two or three of them. The CdpError-to-ToolErr mapping was hand-copied at nine sites, plus a private copy in isolated-tab.ts. domains/cdp-call.ts now owns all of it: cdpErrToToolErr, cdpCall, cdpCallOnTarget, cdpCallBrowser, evalJs. Every other file under src/domains goes through them, which also removes a pile of `if (!r.success) return err({ kind: "cdp_error", ... })` boilerplate in favour of returning the failed Result as-is. Two scanner rules keep it that way, scoped to src/domains/ so the layer that legitimately owns the session — cdp/ and client.ts — is unaffected. Scoping is new to the rule model; without it the rules fired on the transport itself. One deliberate behaviour change: a CDP timeout now surfaces as kind "timeout" rather than "cdp_error". isolated-tab.ts already did this and the other 13 sites flattened it; the shared mapper uses the honest label rather than preserving the mislabel. Co-Authored-By: Claude Opus 5 (1M context) --- src/domains/form.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'src/domains/form.ts') diff --git a/src/domains/form.ts b/src/domains/form.ts index b97b22d..8af43c3 100644 --- a/src/domains/form.ts +++ b/src/domains/form.ts @@ -3,7 +3,7 @@ import type { BrowserClient } from "../client"; import { safeJs } from "../util/js-template"; import { type Result, err, ok } from "../util/result"; import { defineBrowserTool, type ToolErr, type ToolOk } from "../util/tool"; -import { cdpCall } from "./cdp-call"; +import { cdpCall, evalJs } from "./cdp-call"; import { fillBody } from "./fill-engine"; import { interactiveDiff, resolveRefToObjectId } from "./ref-resolve"; @@ -58,8 +58,8 @@ const runOnElement = async ( const __arg = ${opts.arg}; return (function (arg) {`; const expr = `${prelude} ${opts.fnBody} }).call(el, __arg); })()`; - const r = await client.evaluateJs(expr); - if (!r.success) return err({ kind: "cdp_error", message: r.error.message }); + const r = await evalJs(client, expr); + if (!r.success) return r; return ok(r.data); }; @@ -321,14 +321,14 @@ export const resolveAndCall = async ( if (!objectId.success) return objectId; const handle = objectId.data; try { - const called = await client.session().call("Runtime.callFunctionOn", { + const called = await cdpCall(client, "Runtime.callFunctionOn", { objectId: handle, functionDeclaration, arguments: args.map((v) => ({ value: v })), returnByValue: true, awaitPromise: true, }); - if (!called.success) return err({ kind: "cdp_error", message: called.error.message, details: { ref } }); + if (!called.success) return err({ ...called.error, details: { ref } }); if (called.data.exceptionDetails !== undefined) { return err({ kind: "cdp_error", message: `page function threw: ${JSON.stringify(called.data.exceptionDetails)}`, details: { ref } }); } @@ -338,7 +338,7 @@ export const resolveAndCall = async ( } return ok(value); } finally { - await client.session().call("Runtime.releaseObject", { objectId: handle }); + await cdpCall(client, "Runtime.releaseObject", { objectId: handle }); } }; -- cgit v1.2.3