Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/add-dialog-command.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@browserbasehq/browse-cli": minor
---

Add `browse dialog <mode>` command to auto-handle JavaScript dialogs (alert, confirm, prompt). Modes: `accept`, `dismiss`, `off`. Also adds `browse dialog_history` to inspect handled dialogs.
119 changes: 119 additions & 0 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,18 @@ let networkCounter = 0;
let networkSession: string | null = null;
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot Mar 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1: Custom agent: Exception and error message sanitization

New dialog subcommands print raw caught error messages directly to users instead of sanitized, typed error output.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At packages/cli/src/index.ts, line 2831:

<comment>New dialog subcommands print raw caught error messages directly to users instead of sanitized, typed error output.</comment>

<file context>
@@ -2815,29 +2815,54 @@ networkCmd
+      const result = await runCommand("dialog", ["accept"]);
+      output(result, opts.json ?? false);
+    } catch (e) {
+      console.error("Error:", e instanceof Error ? e.message : e);
       process.exit(1);
     }
</file context>
Fix with Cubic

const pendingRequests = new Map<string, PendingRequest>();

// ==================== DIALOG HANDLING STATE ====================

type DialogMode = "accept" | "dismiss" | "off";
let dialogMode: DialogMode = "off";
const dialogInstalledSessions = new WeakSet<object>();
const dialogHistory: Array<{
type: string;
message: string;
handled: DialogMode;
timestamp: string;
}> = [];

/** Sanitize a string for use in a filename */
function sanitizeForFilename(str: string, maxLen: number = 30): string {
return str
Expand Down Expand Up @@ -1582,6 +1594,51 @@ async function executeCommand(
}
}

// Dialog handling
case "dialog": {
const [mode] = args as [DialogMode];
dialogMode = mode;

if (mode !== "off" && page) {
const cdpSession = page.mainFrame().session;
if (!dialogInstalledSessions.has(cdpSession)) {
cdpSession.on(
"Page.javascriptDialogOpening",
async (params: {
type: string;
message: string;
defaultPrompt?: string;
}) => {
if (dialogMode === "off") return;
dialogHistory.push({
type: params.type,
message: params.message,
handled: dialogMode,
timestamp: new Date().toISOString(),
});
try {
await cdpSession.send("Page.handleJavaScriptDialog", {
accept: dialogMode === "accept",
promptText:
dialogMode === "accept"
? (params.defaultPrompt ?? "")
: undefined,
});
} catch {
// Dialog may have been closed by navigation or externally
}
},
);
dialogInstalledSessions.add(cdpSession);
}
}

return { mode: dialogMode };
}
case "dialog_history": {
return { dialogs: dialogHistory };
}

// Daemon control
case "stop": {
process.nextTick(() => {
Expand Down Expand Up @@ -2756,6 +2813,68 @@ networkCmd
}
});

// ==================== DIALOG HANDLING ====================

const dialogCmd = program
.command("dialog")
.description("Dialog handling commands");

dialogCmd
.command("accept")
.description("Auto-accept all dialogs (alerts, confirms, prompts)")
.action(async () => {
const opts = program.opts<GlobalOpts>();
try {
const result = await runCommand("dialog", ["accept"]);
output(result, opts.json ?? false);
} catch (e) {
console.error("Error:", e instanceof Error ? e.message : e);
process.exit(1);
}
});

dialogCmd
.command("dismiss")
.description("Auto-dismiss all dialogs")
.action(async () => {
const opts = program.opts<GlobalOpts>();
try {
const result = await runCommand("dialog", ["dismiss"]);
output(result, opts.json ?? false);
} catch (e) {
console.error("Error:", e instanceof Error ? e.message : e);
process.exit(1);
}
});

dialogCmd
.command("off")
.description("Disable automatic dialog handling")
.action(async () => {
const opts = program.opts<GlobalOpts>();
try {
const result = await runCommand("dialog", ["off"]);
output(result, opts.json ?? false);
} catch (e) {
console.error("Error:", e instanceof Error ? e.message : e);
process.exit(1);
}
});

dialogCmd
.command("history")
.description("Show history of handled dialogs")
.action(async () => {
const opts = program.opts<GlobalOpts>();
try {
const result = await runCommand("dialog_history", []);
output(result, opts.json ?? false);
} catch (e) {
console.error("Error:", e instanceof Error ? e.message : e);
process.exit(1);
}
});

// ==================== RUN ====================

program.parse();
Loading