aboutsummaryrefslogtreecommitdiff
path: root/src/domains/isolated-tab.ts
diff options
context:
space:
mode:
authoramankumarsingh77 <amankumarsingh7702@gmail.com>2026-07-27 23:26:50 +0530
committerAman Kumar <145339995+amankumarsingh77@users.noreply.github.com>2026-08-02 19:12:46 +0530
commit9f2f75dd9281153888776e5d39d4926655c15f2a (patch)
tree89c23c8ad333b5096d39b661b142eaad544cf004 /src/domains/isolated-tab.ts
parent931576443dcb990761cb7e68e354465145f9292d (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/isolated-tab.ts')
-rw-r--r--src/domains/isolated-tab.ts30
1 files changed, 12 insertions, 18 deletions
diff --git a/src/domains/isolated-tab.ts b/src/domains/isolated-tab.ts
index 7969c6b..d0f3def 100644
--- a/src/domains/isolated-tab.ts
+++ b/src/domains/isolated-tab.ts
@@ -14,6 +14,7 @@ import type { BrowserClient } from "../client";
import { type Result, err, ok } from "../util/result";
import { safeJs } from "../util/js-template";
import { ensureHarnessWindow, openHarnessTab } from "../cdp/target-factory";
+import { evaluateJson } from "../cdp/evaluate";
import type { ToolErr } from "../util/tool";
/** A tab attached by its own sessionId, ready to drive via callOnTarget. */
@@ -51,7 +52,7 @@ export const openIsolatedTab = async (client: BrowserClient): Promise<Result<Iso
await client.closeTab(targetId);
return err(toToolErr(attached.error.message));
}
- const { sessionId } = attached.data as { sessionId: string };
+ const { sessionId } = attached.data;
const enabled = await client.session().callOnTarget("Page.enable", {}, sessionId);
if (!enabled.success) {
@@ -91,27 +92,20 @@ export const waitForIsolatedLoad = async (
return err(toToolErr(`page did not finish loading in ${Math.round(timeoutMs / 1000)}s`, "timeout"));
};
-/**
- * Evaluate an expression in the isolated tab and return the raw value. Bounds
- * the eval with a timeout so a blocking dialog cannot hang the tool.
- */
-export const evalInIsolatedTab = async (
+export const isJsonText = (v: unknown): v is string => typeof v === "string";
+
+export const evalInIsolatedTab = async <T>(
client: BrowserClient,
tab: IsolatedTab,
expression: string,
-): Promise<Result<unknown, ToolErr>> => {
- const r = await client.session().callOnTarget(
- "Runtime.evaluate",
- { expression, returnByValue: true, awaitPromise: true },
- tab.sessionId,
- { timeoutMs: EVAL_TIMEOUT_MS },
- );
+ check: (v: unknown) => v is T,
+): Promise<Result<T, ToolErr>> => {
+ const r = await evaluateJson(client.session(), expression, check, {
+ sessionId: tab.sessionId,
+ timeoutMs: EVAL_TIMEOUT_MS,
+ });
if (!r.success) return err(toToolErr(r.error.message, r.error.kind === "timeout" ? "timeout" : "cdp_error"));
- const data = r.data as { result?: { value?: unknown }; exceptionDetails?: unknown };
- if (data.exceptionDetails) {
- return err(toToolErr(`page evaluation failed: ${JSON.stringify(data.exceptionDetails)}`));
- }
- return ok(data.result?.value);
+ return ok(r.data);
};
/** Close an isolated tab and drop its ownership. Best-effort; never throws. */