-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[Claimed #2240] feat(cli): add browse clipboard commands #2241
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
Open
github-actions
wants to merge
3
commits into
main
Choose a base branch
from
external-contributor-pr-2240
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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 { | ||
| 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", | ||
| ]; | ||
|
|
||
| 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 { | ||
| 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); | ||
| } | ||
| } |
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 { | ||
| 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); | ||
| } | ||
| } |
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 { | ||
| 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, | ||
| ); | ||
| } | ||
| } |
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 { | ||
| 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); | ||
| } | ||
| } |
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 { | ||
| 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, | ||
| ); | ||
| } | ||
| } |
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,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,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); | ||
| }); | ||
| }); |
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.
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
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.
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/--cdptargets are machine-global.Implementation:
context.clipboardrunsnavigator.clipboard.readText/writeTextviaRuntime.evaluateinside the target browser (plusBrowser.grantPermissions);paste/copy/cutare synthesized keystrokes. So scope = whatever clipboard backend that Chromium instance has.Measured (each row actually run):
""→ isolated (separate VM per session)pbpasteuntouched""→ isolated (headless Chromium uses an in-memory clipboard per instance)pbpasteshowed it)--cdpattach to a real browser behaves like headed by construction. Noteclearis implemented aswriteText(""), so on a headed/--cdptarget 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--cdpit 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 (interceptingnavigator.clipboardper context) — happy to file a follow-up ticket if we want that, but it's a core change, not CLI.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.
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.”
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.
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.
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.
thank you!