aboutsummaryrefslogtreecommitdiff
path: root/src/domains/history.ts
diff options
context:
space:
mode:
authoramankumarsingh77 <amankumarsingh7702@gmail.com>2026-07-28 00:17:13 +0530
committerAman Kumar <145339995+amankumarsingh77@users.noreply.github.com>2026-08-02 19:12:46 +0530
commitbfba73fa2f1373abd8a5a704c9c833d6fce1b44f (patch)
treee684310863ef6d9aaa9a6bd3d4d3100d75ee9069 /src/domains/history.ts
parent5cfa7ccc8f4082db111bc0186c5fa3d44b767ad3 (diff)
refactor(domains): share target resolution and CDP error mapping
The ref-vs-x/y branch turned out to exist in one tool, not four; it is now one helper with tests covering the half-specified-coordinate and stale-ref cases. cdpCall folds the repeated cdp_error mapping at 24 active-session call sites; target-scoped callOnTarget/callBrowser sites are left alone. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Diffstat (limited to 'src/domains/history.ts')
-rw-r--r--src/domains/history.ts17
1 files changed, 9 insertions, 8 deletions
diff --git a/src/domains/history.ts b/src/domains/history.ts
index fbab376..4b5fa56 100644
--- a/src/domains/history.ts
+++ b/src/domains/history.ts
@@ -2,13 +2,14 @@ import { Type } from "typebox";
import type { BrowserClient } from "../client";
import { type Result, err, ok } from "../util/result";
import { defineBrowserTool, type ToolErr, type ToolOk } from "../util/tool";
+import { cdpCall } from "./cdp-call";
type HistoryEntry = { readonly id: number; readonly url: string; readonly title: string };
type History = { readonly entries: ReadonlyArray<HistoryEntry>; readonly currentIndex: number };
const fetchHistory = async (client: BrowserClient): Promise<Result<History, ToolErr>> => {
- const r = await client.session().call("Page.getNavigationHistory");
- if (!r.success) return err({ kind: "cdp_error", message: r.error.message });
+ const r = await cdpCall(client, "Page.getNavigationHistory", {});
+ if (!r.success) return r;
return ok(r.data);
};
@@ -26,8 +27,8 @@ export const goBackTool = defineBrowserTool({
if (h.data.currentIndex <= 0) return ok({ text: "Already at the beginning of history." });
const target = h.data.entries[h.data.currentIndex - 1];
if (!target) return err({ kind: "internal", message: "history entry missing at index" });
- const nav = await client.session().call("Page.navigateToHistoryEntry", { entryId: target.id });
- if (!nav.success) return err({ kind: "cdp_error", message: nav.error.message });
+ const nav = await cdpCall(client, "Page.navigateToHistoryEntry", { entryId: target.id });
+ if (!nav.success) return nav;
return ok({ text: `Navigated back to: ${target.url}`, details: { url: target.url } });
},
});
@@ -46,8 +47,8 @@ export const goForwardTool = defineBrowserTool({
if (h.data.currentIndex >= h.data.entries.length - 1) return ok({ text: "Already at the end of history." });
const target = h.data.entries[h.data.currentIndex + 1];
if (!target) return err({ kind: "internal", message: "history entry missing at index" });
- const nav = await client.session().call("Page.navigateToHistoryEntry", { entryId: target.id });
- if (!nav.success) return err({ kind: "cdp_error", message: nav.error.message });
+ const nav = await cdpCall(client, "Page.navigateToHistoryEntry", { entryId: target.id });
+ if (!nav.success) return nav;
return ok({ text: `Navigated forward to: ${target.url}`, details: { url: target.url } });
},
});
@@ -61,8 +62,8 @@ export const reloadTool = defineBrowserTool({
parameters: Type.Object({}),
concurrency: "serialized",
async handler(_a, { client }): Promise<Result<ToolOk, ToolErr>> {
- const r = await client.session().call("Page.reload");
- if (!r.success) return err({ kind: "cdp_error", message: r.error.message });
+ const r = await cdpCall(client, "Page.reload", {});
+ if (!r.success) return r;
const info = await client.pageInfo();
if (!info.success) return err({ kind: "cdp_error", message: info.error.message });
if ("dialog" in info.data) {