diff options
| author | amankumarsingh77 <amankumarsingh7702@gmail.com> | 2026-07-28 21:55:55 +0530 |
|---|---|---|
| committer | Aman Kumar <145339995+amankumarsingh77@users.noreply.github.com> | 2026-08-02 19:12:46 +0530 |
| commit | 632600ec0a0a68d9327e19bd419a1ae72047955f (patch) | |
| tree | 5ef59b561d055b8f9173154c252e6d464f3a042b /src/domains/isolated-tab.ts | |
| parent | 893a9d338ceb5d37aefece1df637c9a5888a120e (diff) | |
refactor: one door from a tool to the browser
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) <noreply@anthropic.com>
Diffstat (limited to 'src/domains/isolated-tab.ts')
| -rw-r--r-- | src/domains/isolated-tab.ts | 27 |
1 files changed, 13 insertions, 14 deletions
diff --git a/src/domains/isolated-tab.ts b/src/domains/isolated-tab.ts index ba00756..103c9cc 100644 --- a/src/domains/isolated-tab.ts +++ b/src/domains/isolated-tab.ts @@ -4,6 +4,7 @@ import { safeJs } from "../util/js-template"; import { ensureHarnessWindow, openHarnessTab } from "../cdp/target-factory"; import { evaluateJson } from "../cdp/evaluate"; import type { ToolErr } from "../util/tool"; +import { cdpCallBrowser, cdpCallOnTarget, cdpErrToToolErr, evalJs } from "./cdp-call"; export type IsolatedTab = { readonly targetId: string; @@ -13,32 +14,30 @@ export type IsolatedTab = { const EVAL_TIMEOUT_MS = 15_000; const READY_POLL_INTERVAL_MS = 50; -const toToolErr = (message: string, kind: ToolErr["kind"] = "cdp_error"): ToolErr => ({ kind, message }); - export const openIsolatedTab = async (client: BrowserClient): Promise<Result<IsolatedTab, ToolErr>> => { // Routed through the target factory so an isolated tab lands in the pinned profile, not another profile's cookies. const window = await ensureHarnessWindow(client); - if (!window.success) return err(toToolErr(window.error.message)); + if (!window.success) return err(cdpErrToToolErr(window.error, "Target.createTarget")); let targetId: string; if (window.data.freshlyCreated) { targetId = window.data.targetId; } else { const opened = await openHarnessTab(client, window.data.targetId); - if (!opened.success) return err(toToolErr(opened.error.message)); + if (!opened.success) return err(cdpErrToToolErr(opened.error, "Target.createTarget")); targetId = opened.data; } - const attached = await client.session().callBrowser("Target.attachToTarget", { targetId, flatten: true }); + const attached = await cdpCallBrowser(client, "Target.attachToTarget", { targetId, flatten: true }); if (!attached.success) { await client.closeTab(targetId); - return err(toToolErr(attached.error.message)); + return attached; } const { sessionId } = attached.data; - const enabled = await client.session().callOnTarget("Page.enable", {}, sessionId); + const enabled = await cdpCallOnTarget(client, "Page.enable", {}, sessionId); if (!enabled.success) { await client.closeTab(targetId); - return err(toToolErr(enabled.error.message)); + return enabled; } return ok({ targetId, sessionId }); }; @@ -48,10 +47,10 @@ export const navigateIsolatedTab = async ( tab: IsolatedTab, url: string, ): Promise<Result<void, ToolErr>> => { - const nav = await client.session().callOnTarget("Page.navigate", { url }, tab.sessionId, { + const nav = await cdpCallOnTarget(client, "Page.navigate", { url }, tab.sessionId, { timeoutMs: EVAL_TIMEOUT_MS, }); - if (!nav.success) return err(toToolErr(nav.error.message)); + if (!nav.success) return nav; return ok(undefined); }; @@ -63,12 +62,12 @@ export const waitForIsolatedLoad = async ( ): Promise<Result<void, ToolErr>> => { const start = Date.now(); while (Date.now() - start < timeoutMs) { - if (signal?.aborted) return err(toToolErr("aborted", "internal")); - const r = await client.evaluateJs(safeJs`document.readyState`, tab.sessionId); + if (signal?.aborted) return err({ kind: "internal", message: "aborted" }); + const r = await evalJs(client, safeJs`document.readyState`, tab.sessionId); if (r.success && r.data === "complete") return ok(undefined); await new Promise((res) => setTimeout(res, READY_POLL_INTERVAL_MS)); } - return err(toToolErr(`page did not finish loading in ${Math.round(timeoutMs / 1000)}s`, "timeout")); + return err({ kind: "timeout", message: `page did not finish loading in ${Math.round(timeoutMs / 1000)}s` }); }; export const isJsonText = (v: unknown): v is string => typeof v === "string"; @@ -83,7 +82,7 @@ export const evalInIsolatedTab = async <T>( sessionId: tab.sessionId, timeoutMs: EVAL_TIMEOUT_MS, }); - if (!r.success) return err(toToolErr(r.error.message, r.error.kind === "timeout" ? "timeout" : "cdp_error")); + if (!r.success) return err(cdpErrToToolErr(r.error, "Runtime.evaluate")); return ok(r.data); }; |
