bunwv

repository·main·Indexed 41 days ago

https://github.com/naticha/bunwv

A headless browser automation CLI for Bun, powered by Bun.WebView. Designed for AI coding assistants with an 'agent-first' contract featuring silent successes and structured JSON errors. It supports session management, native event dispatching, auto-waiting for elements, and batch command execution via NDJSON.

Tokens
8.7K
Snippets
28
Records
40
Agent score
52%

What's inside bunwv

  1. How bunwv architecture works

    main

    bunwv operates using a client-server model designed for high-performance browser automation via Bun:

    1. CLI Client: The bunwv CLI sends HTTP requests to a background daemon via a Unix socket (located at /tmp/bunwv-*.sock, restricted to chmod 0600).
    2. Daemon: A background process that manages a single Bun.WebView instance. It listens for requests and manages the browser lifecycle.
    3. Browser Engine: Uses Bun.WebView to drive WebKit on macOS, Chrome on Linux, or the native Windows engine.

    Key Technical Features:

    • Native Events: All selector and coordinate-based inputs are dispatched as native events (isTrusted: true).
    • Auto-waiting: Selector-based methods automatically wait for elements to be attached, visible, stable, and unobscured before acting.
    • Event Buffering: Navigation and CDP (Chrome DevTools Protocol) events are buffered using monotonic seq cursors, allowing agents to poll for new events since their last interaction.
  2. Agent-first usage patterns and contracts

    main

    bunwv is optimized for AI agents using discrete tool calls. Key contracts include:

    • Success/Failure Signaling: Successful verbs (e.g., click, type, navigate) print nothing to stdout and exit with code 0. Errors are emitted as JSON on stderr with specific exit codes.
    • Exit Codes:
      • 0: OK
      • 1: Generic error
      • 2: Usage error
      • 3: Timeout
      • 4: Element not found
      • 5: Daemon unreachable
      • 6: Batch partial failure (only in batch --keep-going)
    • Automatic Console Capture: If the page logs an error while a verb is running, bunwv automatically prints {"console":[...]} to stderr.
    • Uniform JSON Envelopes: Use the --json flag with any command to receive a consistent {ok, data?, error?, exitCode} JSON object.
    • Flexible Flag Syntax: Supports --flag value, --flag=value, and repeated flags (e.g., --mod Shift --mod Control). Flags can appear before or after the command.
    • Session Persistence: Use the BUNWV_SESSION environment variable to avoid passing --session <name> in every command.
  3. Understand the Agent-first contract

    main

    The bunwv CLI is designed for AI agents with a specific communication contract:

    • Silent Success: Action verbs (e.g., click, type, navigate, scroll, submit, close) print nothing to stdout and exit with code 0 on success. This reduces noise for agents.
    • JSON Errors on stderr: When an action fails, stderr contains a JSON object: {ok:false, error, exitCode}. Agents should branch on the exitCode, not the error text.
    • Stable Exit Codes:
      • 0: OK
      • 1: Generic error
      • 2: Usage error
      • 3: Timeout
      • 4: Element not found
      • 5: Daemon unreachable
      • 6: Batch partial failure
    • Auto-surfacing Console Errors: If a page logs console.error or console.warn while a command is running, the error is automatically written to stderr alongside the command's response as {"console":[...]}.
    • Global --json flag: Wrapping any command with --json ensures the output is a JSON object: {ok, data?, error?, exitCode}.
    • Session Context: Use the BUNWV_SESSION environment variable to replace the --session <name> flag.
  4. Quick Start with bunwv

    main

    A typical workflow for browser automation involves starting a daemon, navigating to a page, interacting with elements, and closing the session.

    bunwv start                                  # start the daemon
    bunwv navigate http://localhost:3000         # go to a page
    bunwv click --selector "button.submit"       # click an element (auto-waits)
    bunwv type "hello world"                     # type into focused element
    bunwv evaluate "document.title"              # run JS in the page, returns JSON-literal
    bunwv screenshot                             # writes to /tmp/bunwv-screenshot-<session>.jpg
    bunwv close                                  # stop the daemon
    bunwv start
    bunwv navigate http://localhost:3000
    bunwv click --selector "button.submit"
    bunwv type "hello world"
    bunwv evaluate "document.title"
    bunwv screenshot
    bunwv close
  5. Workflow for AI Assistant integration

    main

    bunwv is optimized for AI coding assistants that interact with a browser via screenshots and command sequences. The recommended interaction loop is:

    1. Navigate: Use navigate to reach a target URL.
    2. Screenshot: Capture the page state. The assistant reads the resulting PNG to understand the UI.
    3. Decide: The assistant analyzes the screenshot to determine the next step.
    4. Act: Execute actions like click, type, or submit.
    5. Wait: Use wait-for to pause until a specific selector, URL, or title change occurs.
    6. Verify: Take another screenshot to confirm the action was successful.
    7. Repeat: Continue the loop until the task is complete.
  6. Install bunwv via npm

    main

    Install the bunwv CLI globally using Bun to enable headless browser automation:

    bun install -g @naticha/bunwv
  7. Use batch mode with `bunwv batch`

    main

    To eliminate the startup cost of running multiple commands, use bunwv batch. This mode runs a sequence of commands within a single Bun process.

    Input Format: Each line of stdin must be a JSON array of arguments. Output Format: Each response is an NDJSON envelope sent to stdout.

    Error Handling:

    • Use the --keep-going flag to ensure the full list of commands runs even if one fails. If any command fails in this mode, the process exits with code 6 (batch-partial).
    • Without --keep-going, the batch process stops immediately at the first failure and returns that specific command's exit code.
    • A successful batch returns exit code 0.
    cat <<'EOF' | bunwv batch --session staging --keep-going
    ["navigate","http://localhost:3000/login"]
    ["click","--selector","input[name='email']"]
    ["type","me@example.com"]
    ["press","Tab"]
    ["type","hunter2"]
    ["submit","--button","Sign In"]
    ["wait-for","--url","/dashboard"]
    ["screenshot"]
    EOF
  8. Clear and edit input fields

    main

    To ensure React/SPA state updates correctly, do not use keyboard shortcuts like Cmd+A to clear fields. Instead, use the clear command followed by click and type.

    Pattern for editing existing values:

    1. bunwv clear <selector>
    2. bunwv click --selector <selector>
    3. bunwv type <new-value>
    bunwv clear "input[name='email']"
    bunwv click --selector "input[name='email']"
    bunwv type "new-value@example.com"
  9. Install bunwv as an AI Coding Assistant Skill

    main

    To teach AI assistants (like Claude Code or Cursor) how to use bunwv for browser testing, you can install the skill file using one of the following methods:

    Using skills CLI:

    bunx skills add naticha/bunwv
    # or
    npx skills add naticha/bunwv

    Directly in Claude Code:

    /plugin marketplace add naticha/bunwv
    /plugin install bunwv@bunwv
    bunx skills add naticha/bunwv
  10. Install bunwv

    main

    Install the bunwv CLI globally using Bun to enable headless browser automation.

    Prerequisites:

    • Bun v1.3.14 or higher.
    • On Linux and Windows, Chrome/Chromium must be installed.
    • On macOS, it uses the native WebKit engine by default (zero dependencies).
    bun install -g @naticha/bunwv
  11. Bunwv CLI Overview and Exit Codes

    main

    The bunwv CLI is used for browser automation via a daemon-based architecture. It supports a --json flag to wrap all responses in a consistent JSON envelope: {ok, data?, error?, exitCode}.

    Exit Codes:

    • 0: ok
    • 1: generic error
    • 2: usage error
    • 3: timeout
    • 4: element-not-found
    • 5: daemon-unreachable
    • 6: batch-partial (when using batch with --keep-going and some commands fail)
  12. Wait for Conditions

    main

    Commands to pause execution until the browser reaches a specific state.

    • wait-for <selector> | --url <substring> | --title <substring>: Wait for a selector to appear, a URL to contain a substring, or a title to contain a substring. Supports --timeout ms.
    • wait-for-gone <selector> | --url <substr> | --title <substr>: Wait for the condition to stop being true (e.g., waiting for a loading spinner to disappear).
    # Wait up to 5 seconds for a specific element
    bunwv wait-for ".main-content" --timeout 5000
    
    # Wait for the URL to change to the dashboard
    bunwv wait-for --url "/dashboard"