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/cdp-call.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/cdp-call.ts')
| -rw-r--r-- | src/domains/cdp-call.ts | 45 |
1 files changed, 43 insertions, 2 deletions
diff --git a/src/domains/cdp-call.ts b/src/domains/cdp-call.ts index 3316633..eee2e0a 100644 --- a/src/domains/cdp-call.ts +++ b/src/domains/cdp-call.ts @@ -1,14 +1,55 @@ import type { BrowserClient } from "../client"; import type { CdpMethod, ParamsOf, ResultOf } from "../cdp/commands"; +import type { CdpError } from "../cdp/errors"; import { type Result, err } from "../util/result"; import type { ToolErr } from "../util/tool"; +export const cdpErrToToolErr = (e: CdpError, method: string): ToolErr => ({ + kind: e.kind === "timeout" ? "timeout" : "cdp_error", + message: e.message, + details: { method }, +}); + export const cdpCall = async <M extends CdpMethod>( client: BrowserClient, method: M, params: ParamsOf<M>, + opts?: { timeoutMs?: number }, +): Promise<Result<ResultOf<M>, ToolErr>> => { + const r = await client.session().call(method, params, opts); + if (!r.success) return err(cdpErrToToolErr(r.error, method)); + return r; +}; + +export const cdpCallOnTarget = async <M extends CdpMethod>( + client: BrowserClient, + method: M, + params: ParamsOf<M>, + sessionId: string, + opts?: { timeoutMs?: number }, +): Promise<Result<ResultOf<M>, ToolErr>> => { + const r = await client.session().callOnTarget(method, params, sessionId, opts); + if (!r.success) return err(cdpErrToToolErr(r.error, method)); + return r; +}; + +export const cdpCallBrowser = async <M extends CdpMethod>( + client: BrowserClient, + method: M, + params?: ParamsOf<M>, + opts?: { timeoutMs?: number }, ): Promise<Result<ResultOf<M>, ToolErr>> => { - const r = await client.session().call(method, params); - if (!r.success) return err({ kind: "cdp_error", message: r.error.message, details: { method } }); + const r = await client.session().callBrowser(method, params, opts); + if (!r.success) return err(cdpErrToToolErr(r.error, method)); + return r; +}; + +export const evalJs = async ( + client: BrowserClient, + expression: string, + sessionId?: string, +): Promise<Result<unknown, ToolErr>> => { + const r = await client.evaluateJs(expression, sessionId); + if (!r.success) return err(cdpErrToToolErr(r.error, "Runtime.evaluate")); return r; }; |
