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; readonly currentIndex: number }; const fetchHistory = async (client: BrowserClient): Promise> => { const r = await cdpCall(client, "Page.getNavigationHistory", {}); if (!r.success) return r; return ok(r.data); }; 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: [], parameters: Type.Object({}), concurrency: "serialized", async handler(_a, { client }): Promise> { 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 cdpCall(client, "Page.navigateToHistoryEntry", { entryId: target.id }); if (!nav.success) return nav; 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: [], parameters: Type.Object({}), concurrency: "serialized", async handler(_a, { client }): Promise> { 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 cdpCall(client, "Page.navigateToHistoryEntry", { entryId: target.id }); if (!nav.success) return nav; 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: [], parameters: Type.Object({}), concurrency: "serialized", async handler(_a, { client }): Promise> { 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) { 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 }, }); }, });