-
Notifications
You must be signed in to change notification settings - Fork 1.6k
feat(cli): add browse macro record and replay commands #2257
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
optimusbuilder
wants to merge
2
commits into
browserbase:main
Choose a base branch
from
optimusbuilder:feat/browse-macro-cli
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
2 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 macro` commands to record and replay driver command sequences. |
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,39 @@ | ||
| import { Args } from "@oclif/core"; | ||
| import { promises as fs } from "node:fs"; | ||
|
|
||
| import { BrowseCommand } from "../../base.js"; | ||
| import { fail } from "../../lib/errors.js"; | ||
| import { macroFilePath } from "../../lib/macro/store.js"; | ||
| import { outputJson } from "../../lib/output.js"; | ||
|
|
||
| export default class MacroDelete extends BrowseCommand { | ||
| static override description = "Delete a saved browse macro."; | ||
|
|
||
| static override examples = ["browse macro delete login-flow"]; | ||
|
|
||
| static override args = { | ||
| name: Args.string({ | ||
| description: "Macro name to delete.", | ||
| required: true, | ||
| }), | ||
| }; | ||
|
|
||
| async run(): Promise<void> { | ||
| const { args } = await this.parse(MacroDelete); | ||
| const file = macroFilePath(args.name); | ||
|
|
||
| try { | ||
| await fs.unlink(file); | ||
| } catch (error) { | ||
| if ((error as NodeJS.ErrnoException).code === "ENOENT") { | ||
| fail(`Macro "${args.name}" not found.`); | ||
| } | ||
| throw error; | ||
| } | ||
|
|
||
| outputJson({ | ||
| deleted: true, | ||
| name: args.name, | ||
| }); | ||
| } | ||
| } |
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,22 @@ | ||
| import { BrowseCommand } from "../../base.js"; | ||
| import { listMacroNames } from "../../lib/macro/store.js"; | ||
| import { getActiveRecordingName } from "../../lib/macro/recording.js"; | ||
| import { outputJson } from "../../lib/output.js"; | ||
|
|
||
| export default class MacroList extends BrowseCommand { | ||
| static override description = "List saved browse macros."; | ||
|
|
||
| static override examples = ["browse macro list"]; | ||
|
|
||
| async run(): Promise<void> { | ||
| const [macros, recording] = await Promise.all([ | ||
| listMacroNames(), | ||
| getActiveRecordingName(), | ||
| ]); | ||
|
|
||
| outputJson({ | ||
| macros, | ||
| recording, | ||
| }); | ||
| } | ||
| } |
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,32 @@ | ||
| import { Args } from "@oclif/core"; | ||
|
|
||
| import { BrowseCommand } from "../../base.js"; | ||
| import { startMacroRecording } from "../../lib/macro/recording.js"; | ||
| import { outputJson } from "../../lib/output.js"; | ||
|
|
||
| export default class MacroRecord extends BrowseCommand { | ||
| static override description = | ||
| "Start recording browse driver commands into a named macro."; | ||
|
|
||
| static override examples = [ | ||
| "browse macro record login-flow", | ||
| "browse macro record checkout --session research", | ||
| ]; | ||
|
|
||
| static override args = { | ||
| name: Args.string({ | ||
| description: "Macro name to create.", | ||
| required: true, | ||
| }), | ||
| }; | ||
|
|
||
| async run(): Promise<void> { | ||
| const { args } = await this.parse(MacroRecord); | ||
| await startMacroRecording(args.name); | ||
| outputJson({ | ||
| message: `Recording macro "${args.name}". Run browse commands, then browse macro stop.`, | ||
| name: args.name, | ||
| recording: 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import { Args, Flags } from "@oclif/core"; | ||
|
|
||
| import { BrowseCommand } from "../../base.js"; | ||
| import { | ||
| driverCommandFlags, | ||
| resolveTargetForCommand, | ||
| type DriverFlags, | ||
| } from "../../lib/driver/command-cli.js"; | ||
| import { sessionName } from "../../lib/driver/flags.js"; | ||
| import { replayMacro } from "../../lib/macro/replay.js"; | ||
| import { outputJson } from "../../lib/output.js"; | ||
|
|
||
| export default class MacroRun extends BrowseCommand { | ||
| static override description = | ||
| "Replay a saved macro in the active browse driver session."; | ||
|
|
||
| static override examples = [ | ||
| "browse macro run login-flow", | ||
| "browse macro run checkout --session research --delay 250", | ||
| ]; | ||
|
|
||
| static override args = { | ||
| name: Args.string({ | ||
| description: "Macro name to replay.", | ||
| required: true, | ||
| }), | ||
| }; | ||
|
|
||
| static override flags = { | ||
| ...driverCommandFlags, | ||
| delay: Flags.integer({ | ||
| default: 0, | ||
| description: "Delay in milliseconds between macro steps.", | ||
| helpValue: "<ms>", | ||
| }), | ||
| }; | ||
|
|
||
| async run(): Promise<void> { | ||
| const { args, flags } = await this.parse(MacroRun); | ||
| const session = sessionName(flags.session); | ||
| const target = await resolveTargetForCommand(session, flags as DriverFlags); | ||
| const { macro, results } = await replayMacro({ | ||
| delayMs: flags.delay, | ||
| name: args.name, | ||
| session, | ||
| target, | ||
| }); | ||
|
|
||
| outputJson({ | ||
| name: macro.name, | ||
| results, | ||
| steps: macro.steps.length, | ||
| }); | ||
| } | ||
| } |
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,23 @@ | ||
| import { Args } from "@oclif/core"; | ||
|
|
||
| import { BrowseCommand } from "../../base.js"; | ||
| import { loadMacro } from "../../lib/macro/store.js"; | ||
| import { outputJson } from "../../lib/output.js"; | ||
|
|
||
| export default class MacroShow extends BrowseCommand { | ||
| static override description = "Show the steps in a saved browse macro."; | ||
|
|
||
| static override examples = ["browse macro show login-flow"]; | ||
|
|
||
| static override args = { | ||
| name: Args.string({ | ||
| description: "Macro name to inspect.", | ||
| required: true, | ||
| }), | ||
| }; | ||
|
|
||
| async run(): Promise<void> { | ||
| const { args } = await this.parse(MacroShow); | ||
| outputJson(await loadMacro(args.name)); | ||
| } | ||
| } |
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,19 @@ | ||
| import { BrowseCommand } from "../../base.js"; | ||
| import { stopMacroRecording } from "../../lib/macro/recording.js"; | ||
| import { outputJson } from "../../lib/output.js"; | ||
|
|
||
| export default class MacroStop extends BrowseCommand { | ||
| static override description = "Stop the active macro recording and save it."; | ||
|
|
||
| static override examples = ["browse macro stop"]; | ||
|
|
||
| async run(): Promise<void> { | ||
| const macro = await stopMacroRecording(); | ||
| outputJson({ | ||
| createdAt: macro.createdAt, | ||
| message: `Saved macro "${macro.name}" with ${macro.steps.length} step(s).`, | ||
| name: macro.name, | ||
| steps: macro.steps.length, | ||
| }); | ||
| } | ||
| } |
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,95 @@ | ||
| import type { DriverCommandName } from "../driver/commands/types.js"; | ||
| import { | ||
| clearRecordingState, | ||
| loadMacro, | ||
| readRecordingState, | ||
| saveMacro, | ||
| writeRecordingState, | ||
| } from "./store.js"; | ||
| import type { BrowseMacro, MacroStep } from "./types.js"; | ||
|
|
||
| const NON_RECORDABLE_COMMANDS = new Set<DriverCommandName>([ | ||
| "cursor", | ||
| "refs", | ||
| "snapshot", | ||
| "tab.list", | ||
| ]); | ||
|
|
||
| export async function startMacroRecording(name: string): Promise<void> { | ||
| const active = await readRecordingState(); | ||
| if (active) { | ||
| throw new Error( | ||
| `Already recording macro "${active.name}". Run browse macro stop first.`, | ||
| ); | ||
| } | ||
|
|
||
| try { | ||
| await loadMacro(name); | ||
| throw new Error( | ||
| `Macro "${name}" already exists. Choose a different name or delete the existing macro first.`, | ||
| ); | ||
| } catch (error) { | ||
| if (!(error instanceof Error) || !error.message.includes("not found")) { | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| await writeRecordingState({ | ||
| name, | ||
| startedAt: new Date().toISOString(), | ||
| steps: [], | ||
| }); | ||
| } | ||
|
|
||
| export async function stopMacroRecording(): Promise<BrowseMacro> { | ||
| const active = await readRecordingState(); | ||
| if (!active) { | ||
| throw new Error( | ||
| "No macro recording in progress. Run browse macro record <name> first.", | ||
| ); | ||
| } | ||
|
|
||
| const macro: BrowseMacro = { | ||
| createdAt: active.startedAt, | ||
| name: active.name, | ||
| steps: active.steps, | ||
| }; | ||
|
|
||
| await saveMacro(macro); | ||
| await clearRecordingState(); | ||
| return macro; | ||
| } | ||
|
|
||
| export async function appendMacroStepIfRecording( | ||
| command: DriverCommandName, | ||
| params: unknown, | ||
| ): Promise<void> { | ||
| if (NON_RECORDABLE_COMMANDS.has(command)) { | ||
| return; | ||
| } | ||
|
|
||
| const active = await readRecordingState(); | ||
| if (!active) { | ||
| return; | ||
| } | ||
|
|
||
| const step: MacroStep = { command, params }; | ||
| active.steps.push(step); | ||
| await writeRecordingState(active); | ||
| } | ||
|
|
||
| export async function getActiveRecordingName(): Promise<string | null> { | ||
| const active = await readRecordingState(); | ||
| return active?.name ?? null; | ||
| } | ||
|
|
||
| export async function tryAppendMacroStepIfRecording( | ||
| command: DriverCommandName, | ||
| params: unknown, | ||
| ): Promise<void> { | ||
| try { | ||
| await appendMacroStepIfRecording(command, params); | ||
| } catch { | ||
| // Best-effort recording must not mask successful driver commands. | ||
| } | ||
| } | ||
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,44 @@ | ||
| import type { DriverCommandName } from "../driver/commands/types.js"; | ||
| import { runDriverCommandWithTarget } from "../driver/runtime.js"; | ||
| import type { ConnectionTarget } from "../driver/types.js"; | ||
| import { loadMacro } from "./store.js"; | ||
| import type { BrowseMacro } from "./types.js"; | ||
|
|
||
| export interface ReplayMacroOptions { | ||
| delayMs: number; | ||
| name: string; | ||
| session: string; | ||
| target: ConnectionTarget; | ||
| } | ||
|
|
||
| export interface ReplayMacroResult { | ||
| macro: BrowseMacro; | ||
| results: unknown[]; | ||
| } | ||
|
|
||
| export async function replayMacro( | ||
| options: ReplayMacroOptions, | ||
| ): Promise<ReplayMacroResult> { | ||
| const macro = await loadMacro(options.name); | ||
| const results: unknown[] = []; | ||
|
|
||
| for (const step of macro.steps) { | ||
| const result = await runDriverCommandWithTarget( | ||
| options.session, | ||
| options.target, | ||
| step.command as DriverCommandName, | ||
| step.params, | ||
| ); | ||
| results.push(result); | ||
|
|
||
| if (options.delayMs > 0) { | ||
| await sleep(options.delayMs); | ||
| } | ||
| } | ||
|
|
||
| return { macro, results }; | ||
| } | ||
|
|
||
| function sleep(ms: number): Promise<void> { | ||
| return new Promise((resolve) => setTimeout(resolve, ms)); | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
P2: Silent suppression of macro recording errors can hide dropped steps and produce a misleadingly successful recording UX.
Prompt for AI agents