aboutsummaryrefslogtreecommitdiff
path: root/src/domains
diff options
context:
space:
mode:
authoramankumarsingh77 <amankumarsingh7702@gmal.com>2026-05-02 16:26:01 +0530
committeramankumarsingh77 <amankumarsingh7702@gmal.com>2026-05-02 16:26:01 +0530
commit1d0873c75e36d7bd1f1a12b6cae46ee10a53ee3b (patch)
treebe7316e826db158be3c8aee9b7cb2545bd4e0471 /src/domains
parent3dfc4f1150b80bb9a5e332999f7440b6255c4ec0 (diff)
feat(domains): migrate go_back, go_forward, reload
Diffstat (limited to 'src/domains')
-rw-r--r--src/domains/history.ts76
1 files changed, 76 insertions, 0 deletions
diff --git a/src/domains/history.ts b/src/domains/history.ts
new file mode 100644
index 0000000..b556056
--- /dev/null
+++ b/src/domains/history.ts
@@ -0,0 +1,76 @@
+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";
+
+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 });
+ return ok(r.data as History);
+};
+
+export const goBackTool = defineBrowserTool({
+ name: "browser_go_back",
+ label: "Browser Go Back",
+ description: "Navigate back one page in history.",
+ promptSnippet: "Go back one page",
+ promptGuidelines: ["Use to return to the previous page after navigating."],
+ parameters: Type.Object({}),
+ async handler(_a, { client }): Promise<Result<ToolOk, ToolErr>> {
+ const h = await fetchHistory(client);
+ if (!h.success) return h;
+ 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 });
+ return ok({ text: `Navigated back to: ${target.url}`, details: { url: target.url } });
+ },
+});
+
+export const goForwardTool = defineBrowserTool({
+ name: "browser_go_forward",
+ label: "Browser Go Forward",
+ description: "Navigate forward one page in history.",
+ promptSnippet: "Go forward one page",
+ promptGuidelines: ["Use to undo a browser_go_back."],
+ parameters: Type.Object({}),
+ async handler(_a, { client }): Promise<Result<ToolOk, ToolErr>> {
+ const h = await fetchHistory(client);
+ if (!h.success) return h;
+ 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 });
+ return ok({ text: `Navigated forward to: ${target.url}`, details: { url: target.url } });
+ },
+});
+
+export const reloadTool = defineBrowserTool({
+ name: "browser_reload",
+ label: "Browser Reload",
+ description: "Reload the current page.",
+ promptSnippet: "Reload the page",
+ promptGuidelines: ["Use to refresh the page, e.g. after server-side changes."],
+ parameters: Type.Object({}),
+ 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 info = await client.pageInfo();
+ if (!info.success) return err({ kind: "cdp_error", message: info.error.message });
+ if ("dialog" in info.data) {
+ return ok({
+ text: `Reloaded. ⚠️ Dialog open: ${info.data.dialog.type}`,
+ details: { dialog: info.data.dialog },
+ });
+ }
+ return ok({
+ text: `Reloaded: ${info.data.title} (${info.data.url})`,
+ details: { page: info.data },
+ });
+ },
+});