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/readpage/read-page.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'src/domains/readpage') diff --git a/src/domains/readpage/read-page.ts b/src/domains/readpage/read-page.ts index 0a6fadc..9d46c7e 100644 --- a/src/domains/readpage/read-page.ts +++ b/src/domains/readpage/read-page.ts @@ -4,6 +4,7 @@ import { parseJson } from "../../schemas/parse"; import { type Result, err, ok } from "../../util/result"; import { applyTruncation } from "../../util/truncate"; import { defineBrowserTool, type ToolErr, type ToolOk } from "../../util/tool"; +import { cdpCallBrowser, evalJs } from "../cdp-call"; import { closeIsolatedTab, evalInIsolatedTab, @@ -107,11 +108,11 @@ const readOwnedTab = async ( if (!client.owns(targetId)) { return err({ kind: "invalid_state", message: `Tab ${targetId} is not owned by this session.` }); } - const attached = await client.session().callBrowser("Target.attachToTarget", { targetId, flatten: true }); - if (!attached.success) return err({ kind: "cdp_error", message: attached.error.message }); + const attached = await cdpCallBrowser(client, "Target.attachToTarget", { targetId, flatten: true }); + if (!attached.success) return attached; const sessionId = attached.data.sessionId; - const captured = await client.evaluateJs(buildPageCaptureExpr(), sessionId); - if (!captured.success) return err({ kind: "cdp_error", message: captured.error.message }); + const captured = await evalJs(client, buildPageCaptureExpr(), sessionId); + if (!captured.success) return captured; return captureToResult(captured.data); }; -- cgit v1.2.3