aboutsummaryrefslogtreecommitdiff
path: root/src
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
parent3dfc4f1150b80bb9a5e332999f7440b6255c4ec0 (diff)
feat(domains): migrate go_back, go_forward, reload
Diffstat (limited to 'src')
-rw-r--r--src/domains/history.ts76
-rw-r--r--src/registry.ts4
-rw-r--r--src/tools.ts103
3 files changed, 81 insertions, 102 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 },
+ });
+ },
+});
diff --git a/src/registry.ts b/src/registry.ts
index 257a1ac..839ccba 100644
--- a/src/registry.ts
+++ b/src/registry.ts
@@ -8,6 +8,7 @@ import { scrollTool } from "./domains/scroll";
import { handleDialogTool } from "./domains/dialog";
import { screenshotTool } from "./domains/screenshot";
import { navigateTool, openUrlsTool } from "./domains/navigate";
+import { goBackTool, goForwardTool, reloadTool } from "./domains/history";
const TOOLS: ReadonlyArray<AnyBrowserToolDefinition> = [
clickTool,
@@ -22,6 +23,9 @@ const TOOLS: ReadonlyArray<AnyBrowserToolDefinition> = [
screenshotTool,
navigateTool,
openUrlsTool,
+ goBackTool,
+ goForwardTool,
+ reloadTool,
];
export const registerAllTools = (pi: ExtensionAPI, client: BrowserClient): void => {
diff --git a/src/tools.ts b/src/tools.ts
index 3b5d639..77023cd 100644
--- a/src/tools.ts
+++ b/src/tools.ts
@@ -96,108 +96,7 @@ async function applyTruncation(
// ── Registration ─────────────────────────────────────────────────────────────
export function registerTools(pi: ExtensionAPI, daemon: BrowserDaemon): void {
- // ═══════════════════════════════════════════════════════════════════════════
- // browser_go_back
- // ═══════════════════════════════════════════════════════════════════════════
- pi.registerTool({
- name: "browser_go_back",
- label: "Browser Go Back",
- description: "Navigate back one page in history.",
- promptSnippet: "Go back one page in browser history",
- promptGuidelines: [
- "Use browser_go_back to return to the previous page after navigating.",
- ],
- parameters: Type.Object({}),
- async execute() {
- try {
- await daemon.ensureAlive();
- const history = (await daemon.cdp("Page.getNavigationHistory")) as {
- entries: Array<{ id: number; url: string; title: string }>;
- currentIndex: number;
- };
- if (history.currentIndex <= 0) {
- return { content: [{ type: "text" as const, text: "Already at the beginning of history." }], details: undefined };
- }
- const targetEntry = history.entries[history.currentIndex - 1];
- await daemon.cdp("Page.navigateToHistoryEntry", { entryId: targetEntry.id });
- return { content: [{ type: "text" as const, text: `Navigated back to: ${targetEntry.url}` }], details: undefined };
- } catch (err) {
- return {
- isError: true,
- content: [{ type: "text" as const, text: `Failed: ${err instanceof Error ? err.message : String(err)}` }],
- details: undefined,
- };
- }
- },
- });
-
- // ═══════════════════════════════════════════════════════════════════════════
- // browser_go_forward
- // ═══════════════════════════════════════════════════════════════════════════
- pi.registerTool({
- name: "browser_go_forward",
- label: "Browser Go Forward",
- description: "Navigate forward one page in history.",
- promptSnippet: "Go forward one page in browser history",
- promptGuidelines: [
- "Use browser_go_forward to go forward after navigating back.",
- ],
- parameters: Type.Object({}),
- async execute() {
- try {
- await daemon.ensureAlive();
- const history = (await daemon.cdp("Page.getNavigationHistory")) as {
- entries: Array<{ id: number; url: string; title: string }>;
- currentIndex: number;
- };
- if (history.currentIndex >= history.entries.length - 1) {
- return { content: [{ type: "text" as const, text: "Already at the end of history." }], details: undefined };
- }
- const targetEntry = history.entries[history.currentIndex + 1];
- await daemon.cdp("Page.navigateToHistoryEntry", { entryId: targetEntry.id });
- return { content: [{ type: "text" as const, text: `Navigated forward to: ${targetEntry.url}` }], details: undefined };
- } catch (err) {
- return {
- isError: true,
- content: [{ type: "text" as const, text: `Failed: ${err instanceof Error ? err.message : String(err)}` }],
- details: undefined,
- };
- }
- },
- });
-
- // ═══════════════════════════════════════════════════════════════════════════
- // browser_reload
- // ═══════════════════════════════════════════════════════════════════════════
- pi.registerTool({
- name: "browser_reload",
- label: "Browser Reload",
- description: "Reload the current page.",
- promptSnippet: "Reload the current page",
- promptGuidelines: [
- "Use browser_reload to refresh the current page, e.g. after making changes that should be reflected.",
- ],
- parameters: Type.Object({}),
- async execute() {
- try {
- await daemon.ensureAlive();
- await daemon.cdp("Page.reload");
- const info = await daemon.getPageInfo();
- if ("dialog" in info) {
- return { content: [{ type: "text" as const, text: `Reloaded. ⚠️ Dialog open: ${info.dialog.type}` }], details: undefined };
- }
- return { content: [{ type: "text" as const, text: `Reloaded: ${info.title} (${info.url})` }], details: undefined };
- } catch (err) {
- return {
- isError: true,
- content: [{ type: "text" as const, text: `Failed: ${err instanceof Error ? err.message : String(err)}` }],
- details: undefined,
- };
- }
- },
- });
-
- // ═══════════════════════════════════════════════════════════════════════════
+// ═══════════════════════════════════════════════════════════════════════════
// browser_upload_file
// ═══════════════════════════════════════════════════════════════════════════
pi.registerTool({