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/browse-clipboard-cli.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"browse": minor
---

Add `browse clipboard` commands (read, write, paste, copy, cut, clear) so the browse CLI exposes the SDK clipboard API for agent and terminal workflows.
3 changes: 3 additions & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
},
"topicSeparator": " ",
"topics": {
"clipboard": {
"description": "Read, write, and interact with the browser session clipboard."
},
"cloud": {
"description": "Manage Browserbase cloud resources and APIs."
},
Expand Down
24 changes: 24 additions & 0 deletions packages/cli/src/commands/clipboard/clear.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { BrowseCommand } from "../../base.js";
import {
clipboardScopeNote,
driverCommandFlags,
runDriverCommandFromFlags,
} from "../../lib/driver/command-cli.js";

export default class ClipboardClear extends BrowseCommand {
static override description = `Clear the clipboard for the active browser session.\n\n${clipboardScopeNote}`;

static override examples = [
"browse clipboard clear",
"browse clipboard clear --session research",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do we know whether or not the clipboard API is global to the machine, or if it's truly scoped by session (aka by tab/browser)? If it's global, then I don't think session flags work right? We need to derisk

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Derisked empirically with the local build (packages/cli, this branch). Answer: it depends on the target, and the session flags are correct for the targets that matter — but headed/--cdp targets are machine-global.

Implementation: context.clipboard runs navigator.clipboard.readText/writeText via Runtime.evaluate inside the target browser (plus Browser.grantPermissions); paste/copy/cut are synthesized keystrokes. So scope = whatever clipboard backend that Chromium instance has.

Measured (each row actually run):

Target Result
Browserbase remote, session 1 write → read roundtrip ✓; local Mac clipboard untouched
Browserbase remote, session 2 reads ""isolated (separate VM per session)
Managed local headless, session A write → read ✓; macOS pbpaste untouched
Managed local headless, session B reads ""isolated (headless Chromium uses an in-memory clipboard per instance)
Managed local headed, session 1 write clobbered the real macOS clipboard (pbpaste showed it)
Managed local headed, session 2 read the same value → machine-global, cross-session leak

--cdp attach to a real browser behaves like headed by construction. Note clear is implemented as writeText(""), so on a headed/--cdp target it wipes the user's actual OS clipboard.

So: for the primary agent targets (--remote, managed headless local) the clipboard is effectively session-scoped and the flags work as expected. For headed local and --cdp it is global to the machine. Addressed in 2acce35: the help text claimed page-level scoping ("for the active page") — now describes session scoping accurately with an explicit per-target scope note on all six commands. Making headed local truly session-scoped would require core-level clipboard virtualization (intercepting navigator.clipboard per context) — happy to file a follow-up ticket if we want that, but it's a core change, not CLI.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

yeah I validated this explicitly: clipboard is shared across tabs in the same browse session, but isolated across different sessions (separate managed-local browser instances).
so for example
clip-a2: if you write A2, read => A2
clip-b2: read => empty
clip-b2: write B2, read => B2
clip-a2: read still => A2
So --session is the right scoping boundary here. If useful, I can add a brief note in docs/help text and clarify that “shared within session, isolated across sessions.”

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Matches my results — thanks for double-checking. The one exception to keep in mind is headed local / --cdp targets, where the browser uses the real OS clipboard (machine-global, leaks across sessions and apps). That caveat is now documented in the help text on all six commands as of 2acce35.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

thank you!

];

static override flags = {
...driverCommandFlags,
};

async run(): Promise<void> {
const { flags } = await this.parse(ClipboardClear);
await runDriverCommandFromFlags("clipboard.clear", {}, flags);
}
}
24 changes: 24 additions & 0 deletions packages/cli/src/commands/clipboard/copy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { BrowseCommand } from "../../base.js";
import {
clipboardScopeNote,
driverCommandFlags,
runDriverCommandFromFlags,
} from "../../lib/driver/command-cli.js";

export default class ClipboardCopy extends BrowseCommand {
static override description = `Copy the current page selection to the session clipboard.\n\n${clipboardScopeNote}`;

static override examples = [
"browse clipboard copy",
"browse clipboard copy --session research",
];

static override flags = {
...driverCommandFlags,
};

async run(): Promise<void> {
const { flags } = await this.parse(ClipboardCopy);
await runDriverCommandFromFlags("clipboard.copy", {}, flags);
}
}
24 changes: 24 additions & 0 deletions packages/cli/src/commands/clipboard/cut.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { BrowseCommand } from "../../base.js";
import {
clipboardScopeNote,
driverCommandFlags,
runDriverCommandFromFlags,
} from "../../lib/driver/command-cli.js";

export default class ClipboardCut extends BrowseCommand {
static override description = `Cut the current page selection to the session clipboard.\n\n${clipboardScopeNote}`;

static override examples = [
"browse clipboard cut",
"browse clipboard cut --session research",
];

static override flags = {
...driverCommandFlags,
};

async run(): Promise<void> {
const { flags } = await this.parse(ClipboardCut);
await runDriverCommandFromFlags("clipboard.cut", {}, flags);
}
}
35 changes: 35 additions & 0 deletions packages/cli/src/commands/clipboard/paste.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Flags } from "@oclif/core";

import { BrowseCommand } from "../../base.js";
import {
clipboardScopeNote,
driverCommandFlags,
runDriverCommandFromFlags,
} from "../../lib/driver/command-cli.js";

export default class ClipboardPaste extends BrowseCommand {
static override description = `Paste session clipboard text into the focused field on the active page.\n\n${clipboardScopeNote}`;

static override examples = [
"browse clipboard paste",
"browse clipboard paste --shortcut Control+V",
];

static override flags = {
...driverCommandFlags,
shortcut: Flags.string({
description: "Keyboard shortcut to trigger paste.",
helpValue: "<shortcut>",
options: ["ControlOrMeta+V", "Meta+V", "Control+V"],
}),
};

async run(): Promise<void> {
const { flags } = await this.parse(ClipboardPaste);
await runDriverCommandFromFlags(
"clipboard.paste",
{ shortcut: flags.shortcut },
flags,
);
}
}
24 changes: 24 additions & 0 deletions packages/cli/src/commands/clipboard/read.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { BrowseCommand } from "../../base.js";
import {
clipboardScopeNote,
driverCommandFlags,
runDriverCommandFromFlags,
} from "../../lib/driver/command-cli.js";

export default class ClipboardRead extends BrowseCommand {
static override description = `Read text from the session clipboard.\n\n${clipboardScopeNote}`;

static override examples = [
"browse clipboard read",
"browse clipboard read --session research",
];

static override flags = {
...driverCommandFlags,
};

async run(): Promise<void> {
const { flags } = await this.parse(ClipboardRead);
await runDriverCommandFromFlags("clipboard.read", {}, flags);
}
}
37 changes: 37 additions & 0 deletions packages/cli/src/commands/clipboard/write.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Args } from "@oclif/core";

import { BrowseCommand } from "../../base.js";
import {
clipboardScopeNote,
driverCommandFlags,
runDriverCommandFromFlags,
} from "../../lib/driver/command-cli.js";

export default class ClipboardWrite extends BrowseCommand {
static override description = `Write text to the session clipboard.\n\n${clipboardScopeNote}`;

static override examples = [
"browse clipboard write 'hello world'",
"browse clipboard write 'seed text' --session research",
];

static override args = {
text: Args.string({
description: "Text to write to the clipboard.",
required: true,
}),
};

static override flags = {
...driverCommandFlags,
};

async run(): Promise<void> {
const { args, flags } = await this.parse(ClipboardWrite);
await runDriverCommandFromFlags(
"clipboard.write",
{ text: args.text },
flags,
);
}
}
3 changes: 3 additions & 0 deletions packages/cli/src/lib/driver/command-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import type { ConnectionTarget } from "./types.js";
import { outputJson } from "../output.js";
import { runDriverCommandWithTarget } from "./runtime.js";

export const clipboardScopeNote =
"Clipboard scope depends on the session target: Browserbase remote sessions and managed headless local sessions use an isolated per-browser clipboard, while headed local sessions and --cdp attachments share the machine's OS clipboard with every other app and session on that machine.";

export const driverCommandFlags = {
"auto-connect": autoConnectFlag,
cdp: cdpFlag,
Expand Down
51 changes: 51 additions & 0 deletions packages/cli/src/lib/driver/commands/clipboard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { z } from "zod";

import type { DriverCommandHandlers } from "./types.js";

const writeParamsSchema = z.object({
text: z.string(),
});

const pasteParamsSchema = z.object({
shortcut: z.enum(["ControlOrMeta+V", "Meta+V", "Control+V"]).optional(),
});

export const clipboardHandlers: DriverCommandHandlers = {
async "clipboard.read"(manager) {
const context = await manager.browserContext();
const text = await context.clipboard.readText();
return { text };
},

async "clipboard.write"(manager, params) {
const { text } = writeParamsSchema.parse(params);
const context = await manager.browserContext();
await context.clipboard.writeText(text);
return { ok: true };
},

async "clipboard.clear"(manager) {
const context = await manager.browserContext();
await context.clipboard.clear();
return { ok: true };
},

async "clipboard.paste"(manager, params) {
const { shortcut } = pasteParamsSchema.parse(params ?? {});
const context = await manager.browserContext();
await context.clipboard.paste(shortcut ? { shortcut } : undefined);
return { ok: true };
},

async "clipboard.copy"(manager) {
const context = await manager.browserContext();
await context.clipboard.copy();
return { ok: true };
},

async "clipboard.cut"(manager) {
const context = await manager.browserContext();
await context.clipboard.cut();
return { ok: true };
},
};
2 changes: 2 additions & 0 deletions packages/cli/src/lib/driver/commands/registry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { DriverSessionManager } from "../session-manager.js";
import { clipboardHandlers } from "./clipboard.js";
import { elementsHandlers } from "./elements.js";
import { keyboardHandlers } from "./keyboard.js";
import { mouseHandlers } from "./mouse.js";
Expand All @@ -12,6 +13,7 @@ import type { DriverCommandHandlers, DriverCommandName } from "./types.js";

const handlers: DriverCommandHandlers = {
...navigationHandlers,
...clipboardHandlers,
...elementsHandlers,
...keyboardHandlers,
...mouseHandlers,
Expand Down
6 changes: 6 additions & 0 deletions packages/cli/src/lib/driver/commands/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import { z } from "zod";
export const DRIVER_COMMAND_NAMES = [
"back",
"click",
"clipboard.clear",
"clipboard.copy",
"clipboard.cut",
"clipboard.paste",
"clipboard.read",
"clipboard.write",
"cursor",
"eval",
"fill",
Expand Down
106 changes: 106 additions & 0 deletions packages/cli/tests/clipboard.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { describe, expect, it, vi } from "vitest";

import { clipboardHandlers } from "../src/lib/driver/commands/clipboard.js";
import type { DriverSessionManager } from "../src/lib/driver/session-manager.js";

function makeManager(clipboard: Record<string, ReturnType<typeof vi.fn>>) {
return {
browserContext: vi.fn().mockResolvedValue({ clipboard }),
} as unknown as DriverSessionManager;
}

describe("clipboard driver handlers", () => {
it("reads clipboard text", async () => {
const readText = vi.fn().mockResolvedValue("copied value");
const manager = makeManager({ readText });

await expect(
clipboardHandlers["clipboard.read"]!(manager, {}),
).resolves.toEqual({ text: "copied value" });
expect(readText).toHaveBeenCalledOnce();
});

it("writes clipboard text", async () => {
const writeText = vi.fn().mockResolvedValue(undefined);
const manager = makeManager({ writeText });

await expect(
clipboardHandlers["clipboard.write"]!(manager, { text: "hello" }),
).resolves.toEqual({ ok: true });
expect(writeText).toHaveBeenCalledWith("hello");
});

it("pastes with an optional shortcut", async () => {
const paste = vi.fn().mockResolvedValue(undefined);
const manager = makeManager({ paste });

await expect(
clipboardHandlers["clipboard.paste"]!(manager, {
shortcut: "Control+V",
}),
).resolves.toEqual({ ok: true });
expect(paste).toHaveBeenCalledWith({ shortcut: "Control+V" });
});

it("clears, copies, and cuts via clipboard helpers", async () => {
const clear = vi.fn().mockResolvedValue(undefined);
const copy = vi.fn().mockResolvedValue(undefined);
const cut = vi.fn().mockResolvedValue(undefined);
const manager = makeManager({ clear, copy, cut });

await expect(
clipboardHandlers["clipboard.clear"]!(manager, {}),
).resolves.toEqual({ ok: true });
await expect(
clipboardHandlers["clipboard.copy"]!(manager, {}),
).resolves.toEqual({ ok: true });
await expect(
clipboardHandlers["clipboard.cut"]!(manager, {}),
).resolves.toEqual({
ok: true,
});
expect(clear).toHaveBeenCalledOnce();
expect(copy).toHaveBeenCalledOnce();
expect(cut).toHaveBeenCalledOnce();
});

it("rejects write without text", async () => {
const writeText = vi.fn().mockResolvedValue(undefined);
const manager = makeManager({ writeText });

await expect(
clipboardHandlers["clipboard.write"]!(manager, {}),
).rejects.toThrow();
expect(writeText).not.toHaveBeenCalled();
});

it("rejects write with non-string text", async () => {
const writeText = vi.fn().mockResolvedValue(undefined);
const manager = makeManager({ writeText });

await expect(
clipboardHandlers["clipboard.write"]!(manager, { text: 42 }),
).rejects.toThrow();
expect(writeText).not.toHaveBeenCalled();
});

it("rejects paste with an unsupported shortcut", async () => {
const paste = vi.fn().mockResolvedValue(undefined);
const manager = makeManager({ paste });

await expect(
clipboardHandlers["clipboard.paste"]!(manager, { shortcut: "Alt+V" }),
).rejects.toThrow();
expect(paste).not.toHaveBeenCalled();
});

it("pastes without options when no shortcut is given", async () => {
const paste = vi.fn().mockResolvedValue(undefined);
const manager = makeManager({ paste });

await expect(
clipboardHandlers["clipboard.paste"]!(manager, undefined),
).resolves.toEqual({ ok: true });
expect(paste).toHaveBeenCalledWith(undefined);
});
});
2 changes: 2 additions & 0 deletions packages/cli/tests/driver-commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ describe("driver commands", () => {
"snapshot",
"tab.switch",
"network.on",
"clipboard.read",
"clipboard.write",
"upload",
"viewport",
]),
Expand Down
Loading