aboutsummaryrefslogtreecommitdiff
path: root/src/domains/files.ts
diff options
context:
space:
mode:
authoramankumarsingh77 <amankumarsingh7702@gmail.com>2026-07-28 21:55:55 +0530
committerAman Kumar <145339995+amankumarsingh77@users.noreply.github.com>2026-08-02 19:12:46 +0530
commit632600ec0a0a68d9327e19bd419a1ae72047955f (patch)
tree5ef59b561d055b8f9173154c252e6d464f3a042b /src/domains/files.ts
parent893a9d338ceb5d37aefece1df637c9a5888a120e (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/files.ts')
-rw-r--r--src/domains/files.ts14
1 files changed, 7 insertions, 7 deletions
diff --git a/src/domains/files.ts b/src/domains/files.ts
index 534a674..89fdaf9 100644
--- a/src/domains/files.ts
+++ b/src/domains/files.ts
@@ -6,7 +6,7 @@ import { type Result, err, ok } from "../util/result";
import { defineBrowserTool, type ToolErr, type ToolOk } from "../util/tool";
import { safeJs } from "../util/js-template";
import { pdfPath } from "../util/paths";
-import { cdpCall } from "./cdp-call";
+import { cdpCall, cdpCallBrowser, evalJs } from "./cdp-call";
import { resolveRefToBackendId } from "./ref-resolve";
const UploadArgs = Type.Object({
@@ -52,8 +52,8 @@ const tryCdpUpload = async (
if (!nodeId) return err({ kind: "invalid_state", message: `Selector matched 0 file inputs: ${selector}` });
const set = await cdpCall(client, "DOM.setFileInputFiles", { files: [filePath], nodeId });
if (!set.success) return set;
- const verify = await client.evaluateJs(safeJs`document.querySelector(${selector})?.files?.length || 0`);
- if (!verify.success) return err({ kind: "cdp_error", message: verify.error.message });
+ const verify = await evalJs(client, safeJs`document.querySelector(${selector})?.files?.length || 0`);
+ if (!verify.success) return verify;
if (Number(verify.data ?? 0) === 0) {
return err({ kind: "invalid_state", message: "CDP upload reported success but file count is 0" });
}
@@ -92,8 +92,8 @@ const jsFallbackUpload = async (
return input.files.length;
})()
`;
- 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;
if (!Number(r.data ?? 0)) return err({ kind: "invalid_state", message: "JS fallback set 0 files" });
return ok(undefined);
};
@@ -181,12 +181,12 @@ export const downloadTool = defineBrowserTool({
message: `Download path unusable: ${e instanceof Error ? e.message : String(e)}`,
});
}
- const r = await client.session().callBrowser("Browser.setDownloadBehavior", {
+ const r = await cdpCallBrowser(client, "Browser.setDownloadBehavior", {
behavior: "allow",
downloadPath: args.downloadPath,
eventsEnabled: true,
});
- if (!r.success) return err({ kind: "cdp_error", message: r.error.message });
+ if (!r.success) return r;
return ok({
text: `Downloads will save to: ${args.downloadPath}`,
details: { downloadPath: args.downloadPath },