aboutsummaryrefslogtreecommitdiff
path: root/src/domains/history.ts
blob: b19de2e1880dcba82e05bc6a5b37d5b019a6add8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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 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<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 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<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 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<Result<ToolOk, ToolErr>> {
    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 },
    });
  },
});