-
Notifications
You must be signed in to change notification settings - Fork 1.6k
feat(cli): add browse clipboard commands #2240
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
optimusbuilder
wants to merge
1
commit into
browserbase:main
from
optimusbuilder:feat/browse-clipboard-cli
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import { BrowseCommand } from "../../base.js"; | ||
| import { | ||
| driverCommandFlags, | ||
| runDriverCommandFromFlags, | ||
| } from "../../lib/driver/command-cli.js"; | ||
|
|
||
| export default class ClipboardClear extends BrowseCommand { | ||
| static override description = | ||
| "Clear the browser clipboard for the active page."; | ||
|
|
||
| static override examples = [ | ||
| "browse clipboard clear", | ||
| "browse clipboard clear --session research", | ||
| ]; | ||
|
|
||
| static override flags = { | ||
| ...driverCommandFlags, | ||
| }; | ||
|
|
||
| async run(): Promise<void> { | ||
| const { flags } = await this.parse(ClipboardClear); | ||
| await runDriverCommandFromFlags("clipboard.clear", {}, flags); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import { BrowseCommand } from "../../base.js"; | ||
| import { | ||
| driverCommandFlags, | ||
| runDriverCommandFromFlags, | ||
| } from "../../lib/driver/command-cli.js"; | ||
|
|
||
| export default class ClipboardCopy extends BrowseCommand { | ||
| static override description = | ||
| "Copy the current selection to the browser clipboard on the active page."; | ||
|
|
||
| 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); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import { BrowseCommand } from "../../base.js"; | ||
| import { | ||
| driverCommandFlags, | ||
| runDriverCommandFromFlags, | ||
| } from "../../lib/driver/command-cli.js"; | ||
|
|
||
| export default class ClipboardCut extends BrowseCommand { | ||
| static override description = | ||
| "Cut the current selection to the browser clipboard on the active page."; | ||
|
|
||
| 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); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 { | ||
| driverCommandFlags, | ||
| runDriverCommandFromFlags, | ||
| } from "../../lib/driver/command-cli.js"; | ||
|
|
||
| export default class ClipboardPaste extends BrowseCommand { | ||
| static override description = | ||
| "Paste clipboard text into the focused field on the active page."; | ||
|
|
||
| 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, | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import { BrowseCommand } from "../../base.js"; | ||
| import { | ||
| driverCommandFlags, | ||
| runDriverCommandFromFlags, | ||
| } from "../../lib/driver/command-cli.js"; | ||
|
|
||
| export default class ClipboardRead extends BrowseCommand { | ||
| static override description = | ||
| "Read text from the browser clipboard for the active page."; | ||
|
|
||
| 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); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 { | ||
| driverCommandFlags, | ||
| runDriverCommandFromFlags, | ||
| } from "../../lib/driver/command-cli.js"; | ||
|
|
||
| export default class ClipboardWrite extends BrowseCommand { | ||
| static override description = | ||
| "Write text to the browser clipboard for the active page."; | ||
|
|
||
| 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, | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 }; | ||
| }, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| 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(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is clipboard siloed by browser / tab like this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it’s not tab-siloed.
browse clipboard * runs against the active page (for focus + permission grant), but the clipboard value is shared across tabs in that browser session.
so when you write in tab 0 and switch to tab 1, read returns the same value.
So --session here selects which browse daemon/browser session to target, not per-tab clipboard isolation.