aboutsummaryrefslogtreecommitdiff
path: root/src/domains/dialog.ts
blob: aeb26bb77a203deb800b096a550667abf7f38f50 (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
import { Type } from "typebox";
import { type Result, ok } from "../util/result";
import { defineBrowserTool, type ToolErr, type ToolOk } from "../util/tool";
import { cdpCall } from "./cdp-call";

const HandleDialogArgs = Type.Object({
  accept: Type.Boolean({ description: "true = accept, false = dismiss" }),
  promptText: Type.Optional(Type.String({ description: "Text to type if dialog is a prompt()" })),
});

export const handleDialogTool = defineBrowserTool({
  name: "browser_handle_dialog",
  label: "Browser Handle Dialog",
  description:
    "Accept or dismiss the currently open JS dialog (alert/confirm/prompt/beforeunload).",
  promptSnippet: "Accept or dismiss a JS dialog",
  promptGuidelines: [
    "Use after browser_page_info reports a dialog is open. Until handled, no other browser action will work.",
    "For prompt() dialogs, supply promptText with the value to submit.",
  ],
  parameters: HandleDialogArgs,
  concurrency: "serialized",
  async handler(args, { client }): Promise<Result<ToolOk, ToolErr>> {
    const r = await cdpCall(client, "Page.handleJavaScriptDialog", {
      accept: args.accept,
      ...(args.promptText !== undefined ? { promptText: args.promptText } : {}),
    });
    if (!r.success) return r;
    return ok({
      text: `Dialog ${args.accept ? "accepted" : "dismissed"}`,
      details: { accept: args.accept },
    });
  },
});