Complete browser automation suite for OpenCode using Playwright. Enables AI agents to interact with web pages, take screenshots, and perform visual grounding for intelligent web automation.
# Enable browser automation
export OPENCODE_ENABLE_BROWSER=true
# Run OpenCode with browser tools available
bun dev
# In a separate terminal, start the browser server
cd packages/opencode && node browser-server.jsNavigate to URLs with integrated content returns.
// Basic navigation
await browser_navigate({ url: "https://example.com" })
// With integrated content (no separate fetch needed)
await browser_navigate({
url: "https://example.com",
wait_until: "domcontentloaded",
return_content: "inputs", // Returns list of input fields
})Parameters:
url(string, required) - Target URLwait_until(enum) - "load" | "domcontentloaded" | "networkidle" | "commit" (default: "domcontentloaded")return_content(enum) - Optional: "text" | "links" | "inputs" | "screenshot" | "structured"timeout(number, optional) - Milliseconds to wait
Return Content Types:
"text"- Page readable text (first 500 chars)"links"- List of clickable links (filtered by tagName="a")"inputs"- List of input fields with types"screenshot"- Screenshot automatically captured"structured"- Element count and metadata
Navigate browser history.
await browser_navigate_back()
await browser_navigate_forward()Wait for page states or specific elements.
// Wait for page fully loaded
await browser_wait({ wait_for: "load", timeout: 30000 })
// Wait for element to appear
await browser_wait({ selector: "#content", visible: true })Parameters:
wait_for(enum) - "load" | "domcontentloaded" | "networkidle"selector(string, optional) - CSS selector to wait forvisible(boolean) - Wait for visibility (default: false)timeout(number) - Max wait time in ms (default: 30000)
Click elements by selector, ref, or coordinates.
// By CSS selector
await browser_click({ selector: "button.submit" })
// By coordinates
await browser_click({ x: 640, y: 360 })
// By numeric ref (from list_inputs/list_buttons output)
await browser_click({ element: "search input", ref: "1" })
// With integrated content return
await browser_click({
selector: "a.link",
return_content: "text", // Returns page text after click
})Parameters:
selector(string, optional) - CSS selectorelement(string, optional) - Human-readable element description (fuzzy matched)ref(string, optional) - Numeric ref from list_* outputs (resolved to selector)x,y(numbers, optional) - Coordinatesbutton(enum) - "left" | "right" | "middle" (default: "left")doubleClick(boolean) - Double-click (default: false)return_content(enum) - Optional: "text" | "links" | "inputs" | "screenshot" | "structured"timeout(number, optional) - Timeout in ms
Type text into input fields.
// Simple text entry
await browser_type({
selector: "input[type='email']",
text: "[email protected]",
})
// Clear field, type, and press Enter with content return
await browser_type({
element: "search box",
ref: "1",
text: "query",
submit: true,
return_content: "text", // Returns search results
})
// Slow typing (simulates real user)
await browser_type({
selector: "input",
text: "careful typing",
slowly: true, // 100ms per character
})Parameters:
selector(string, optional) - CSS selectorelement(string, optional) - Human-readable descriptionref(string, optional) - Numeric ref (resolved like in click)text(string, required) - Text to typeclear(boolean) - Clear existing content (default: false)submit(boolean) - Press Enter after typing (default: false)slowly(boolean) - Type slowly (100ms per char) (default: false)return_content(enum) - Optional return content typetimeout(number, optional) - Timeout in ms
Hover over elements (mouseover).
await browser_hover({ selector: ".dropdown-trigger" })Drag & drop elements.
await browser_drag({
sourceSelector: ".draggable",
targetSelector: ".drop-zone",
})Scroll pages or elements with integrated content returns.
// Scroll down 500px
await browser_scroll({ direction: "down", amount: 500 })
// Scroll to element
await browser_scroll({ to_element: "#footer" })
// Scroll within container
await browser_scroll({
selector: ".sidebar",
direction: "down",
amount: 300,
})
// With integrated content return
await browser_scroll({
direction: "down",
return_content: "inputs", // Returns inputs visible after scroll
})Parameters:
direction(enum) - "up" | "down" | "left" | "right"amount(number) - Pixels to scroll (default: 500)to_element(string, optional) - CSS selector to scroll toposition(object, optional) - Absolute scroll position {x, y}selector(string, optional) - Container to scroll (default: page)smooth(boolean) - Use smooth scrolling (default: true)return_content(enum) - Optional return content typetimeout(number, optional) - Timeout in ms
Press keyboard keys.
// Single key
await browser_press_key({ key: "Enter" })
// With modifiers
await browser_press_key({ key: "a", modifiers: ["Control"] })Select option from HTML select dropdown.
await browser_select_option({
selector: "select[name='country']",
value: "USA",
})Fill multiple form fields at once.
await browser_fill_form({
fields: {
"input[name='email']": "[email protected]",
"input[name='password']": "secret",
"select[name='country']": "USA",
},
})Capture page or element screenshots.
// Viewport screenshot
await browser_screenshot()
// Full page screenshot
await browser_screenshot({ full_page: true })
// Screenshot of element
await browser_screenshot({ selector: "#content" })
// Custom quality
await browser_screenshot({
full_page: true,
quality: 85,
})Returns: Base64-encoded PNG image as attachment.
Extract page content in various formats.
// Plain text (extracted via TreeWalker, 99% token reduction)
await browser_content({ format: "text" })
// HTML markup
await browser_content({ format: "html" })
// Structured elements (inputs/buttons/links)
await browser_content({ format: "list_inputs" })
// All interactive elements
await browser_content({ format: "structured" })Format Options:
"text"- Readable text only (TreeWalker extraction)"html"- Raw HTML markup"list_inputs"- Numbered input fields"list_buttons"- Numbered buttons"list_links"- Numbered links"list_textareas"- Numbered textareas"structured"- All interactive elements with metadata
Find elements using fuzzy matching with scoring.
// Find search input
await browser_search({ query: "search input" })
// Get top 5 matches
await browser_search({ query: "button submit", limit: 5 })Scoring Algorithm:
- Exact match: 1000 points
- Substring match: 500 points
- Fuzzy match: 1-10 points per character (cumulative)
- Minimum threshold: 50 points (prevents wrong matches)
Returns: Array of matched elements with selector, bounds, and score.
Get accessibility tree snapshot.
await browser_snapshot()Execute JavaScript in page context.
const result = await browser_evaluate({
code: "document.title",
})Run code with console capture.
await browser_run_code({
code: "console.log('hello')",
})Handle browser dialogs (alert, confirm, prompt).
// Accept alert
await browser_handle_dialog({ action: "accept" })
// Dismiss and return text
await browser_handle_dialog({ action: "dismiss", return_text: true })Capture console logs.
const logs = await browser_console_messages({ level: "error" })Monitor network traffic.
const requests = await browser_network_requests()Manage browser tabs/windows.
// List tabs
await browser_tabs({ action: "list" })
// Switch tab
await browser_tabs({ action: "switch", index: 1 })
// Close tab
await browser_tabs({ action: "close", index: 0 })
// Close all
await browser_tabs({ action: "close_all" })Resize browser window.
await browser_resize({
width: 1280,
height: 720,
})Handle file upload inputs.
await browser_file_upload({
selector: "input[type='file']",
filePath: "/path/to/file.pdf",
})Explicitly initialize browser.
await browser_init({
headed: true, // Show window
profile_path: "/custom/profile",
})Close browser and cleanup.
await browser_close({ close_all: true })Assert element visibility (for test flows).
await browser_verify_element_visible({
selector: ".success-message",
})Assert text presence on page.
await browser_verify_text_visible({
text: "Order confirmed",
})Generate selectors for elements.
await browser_generate_locator({
selector: "button", // Generates human-readable locator
})src/browser/
├── index.ts # Module exports & initialization
├── manager.ts # BrowserManager singleton (Playwright API)
├── tools/ # Individual tool implementations
│ ├── index.ts # Tool registry & exports
│ ├── navigate.ts # URL navigation with integrated return_content
│ ├── click.ts # Element clicking (selector/coordinates)
│ ├── type.ts # Text input with ref resolution
│ ├── scroll.ts # Page/element scrolling
│ ├── search.ts # Element discovery via fuzzy search
│ ├── screenshot.ts # Viewport/full-page capture
│ ├── content.ts # Page content extraction (text/html/structured)
│ ├── wait.ts # Wait for page states or elements
│ ├── hover.ts # Element hover/mouseover
│ ├── drag.ts # Drag & drop operations
│ ├── press-key.ts # Keyboard input
│ ├── select-option.ts # HTML select dropdowns
│ ├── fill-form.ts # Multi-field form filling
│ ├── evaluate.ts # JavaScript evaluation
│ ├── snapshot.ts # Accessibility tree snapshot
│ ├── tabs.ts # Tab/window management
│ ├── console-messages.ts # Console log capture
│ ├── network-requests.ts # Network monitoring
│ ├── handle-dialog.ts # Alert/dialog handling
│ ├── file-upload.ts # File input handling
│ ├── resize.ts # Window/viewport resize
│ ├── run-code.ts # Execute code in page context
│ ├── navigate-back.ts # Browser history navigation
│ ├── verify-*.ts # Assertion tools for testing
│ ├── generate-locator.ts # Element selector generation
│ └── close.ts # Browser/page cleanup
├── test/ # Test utilities
│ ├── test-browser-tools.ts # Tool export verification
│ ├── test-browser.js # Chromium launch test
│ └── test-cli.js # Interactive CLI for testing
└── README.md # This file
browser-server.js # Node.js HTTP server for Playwright bridge
Core singleton managing Playwright browser instance:
- Initialization: Auto-init on first use, supports custom profiles
- Session Management: Persistent profile for cookies/logins
- Page Operations: Single-page support (expandable to tabs)
- Element Discovery: Fuzzy search with scoring algorithm
- Content Extraction: TreeWalker-based text extraction (99% token reduction vs raw HTML)
- Auto-Scroll:
scrollIntoViewIfNeeded()before interactions - Error Handling: Comprehensive try-catch with proper error messages
Runs on http://localhost:9999 to bridge Bun/TypeScript with Playwright:
/init - Initialize browser
/navigate - Navigate to URL
/screenshot - Take screenshot
/click - Click element
/type - Type text
/scroll - Scroll page
/content - Get page content
/elements - List interactive elements
/searchElements - Fuzzy search elements
/listElements - List by type (inputs/buttons/links)
/wait - Wait for conditions
/pageInfo - Get page metadata
/close - Close browser
Reduce tool calls by 70% using integrated return_content:
// Old way: 3+ separate calls
await browser_navigate({ url: "youtube.com" })
await browser_content({ format: "list_inputs" }) // Separate call
await browser_click({ selector: "input", ref: "1" })
await browser_content({ format: "text" }) // Another separate call
// New way: 2 integrated calls
await browser_navigate({
url: "youtube.com",
return_content: "inputs", // Integrated!
})
await browser_click({
selector: "input",
ref: "1",
return_content: "text", // Integrated!
})Use numbered refs from list outputs for reliable element targeting:
// Step 1: Get numbered inputs
const contentResult = await browser_content({ format: "list_inputs" })
// Returns: "Input fields on page (3 total):\n[1] input[text] Search\n[2] input[email] Email\n..."
// Step 2: Click by ref (automatically resolved to selector)
await browser_click({
element: "search input", // Helps with fuzzy fallback
ref: "1", // Direct reference - mapped back to selector
})
// Step 3: Type with same ref
await browser_type({
element: "search input",
ref: "1",
text: "query",
})When refs change, fuzzy search finds elements by description:
// If ref "1" no longer works, fuzzy search takes over
await browser_click({
element: "login button", // Natural language description
ref: "1", // Tries ref first, falls back to fuzzy if numeric
})
// Pure fuzzy search (no ref)
await browser_click({
element: "the big blue submit button on the bottom",
})Fuzzy Scoring:
- Exact match: 1000 pts
- Substring: 500 pts
- Character-by-character: 1-10 pts
- Minimum: 50 pts (prevents wrong matches)
For sites with heavy JavaScript (YouTube, Twitter, etc.):
// Step 1: Screenshot to visually ground AI
await browser_screenshot({ full_page: false })
// Step 2: Use coordinates if selectors fail
await browser_click({ x: 640, y: 360 })
// Step 3: Wait for content to settle
await browser_wait({
selector: ".loaded",
visible: true,
timeout: 5000,
})
// Step 4: Re-observe and continue
await browser_screenshot()Multi-field form automation with error recovery:
// Bulk fill
await browser_fill_form({
fields: {
"input[name='firstName']": "John",
"input[name='lastName']": "Doe",
"input[type='email']": "[email protected]",
"select[name='country']": "USA",
"textarea[name='message']": "Hello!",
},
})
// Then submit
await browser_click({ selector: "button[type='submit']" })# Enable browser automation
export OPENCODE_ENABLE_BROWSER=true
# Custom browser profile (persistent cookies/logins)
export OPENCODE_BROWSER_PROFILE_PATH="~/.opencode/profiles/session1"
# Debug mode (verbose logging)
export OPENCODE_BROWSER_DEBUG=truebun add playwright
bunx playwright install chromium# Terminal 1: Browser server (Node.js)
cd packages/opencode && node browser-server.js
# Terminal 2: OpenCode (Bun)
bun devSymptom: Error: Click failed: Element not found or not visible
Solutions:
- Numeric ref mismatch: Page changed between list and click
- Solution: Re-run
browser_content({ format: "list_inputs" })to refresh refs
- Solution: Re-run
- Fuzzy match too weak: Description doesn't match element
- Solution: Use exact CSS selector or coordinates
- Element not visible: Hidden by CSS or below viewport
- Solution: Tool auto-scrolls, but try explicit
browser_scroll({ to_element: "#target" })
- Solution: Tool auto-scrolls, but try explicit
Symptom: Error: Type failed: Element not found
Solutions:
- Click element first to focus:
await browser_click({ selector: "input" }) - Use
slowly: trueto simulate human typing - Add
clear: trueto ensure clean state
Symptom: Navigation hangs or times out
Solutions:
- Use
wait_until: "networkidle"(more reliable than "load") - Reduce
timeoutto fail fast:timeout: 10000 - Check page state:
await browser_content({ format: "text" })
Symptom: Screenshot attachment is huge (>5MB)
Solutions:
- Use viewport-only (default):
full_page: false - Reduce quality:
quality: 75 - Crop to element:
selector: "#main"
- Use
return_contentconsistently - Reduces tool calls by ~70% - Prefer numbered refs - More stable than fuzzy matching on dynamic sites
- Combine scroll + content - Wait for content to load during scroll
- Close browser when done - Cleanup resources:
await browser_close({ close_all: true }) - Use content format efficiently - "text" is faster than "html" (TreeWalker vs DOM)
# Test tool exports
bun run src/browser/test/test-browser-tools.ts
# Test Chromium launch
bun run src/browser/test/test-browser.js
# Interactive CLI
node src/browser/test/test-cli.js
> init true
> navigate example.com
> screenshot
> elements clickable
> close- Playwright Docs: https://playwright.dev
- Fuzzy Matching: 50-point minimum threshold, cumulative scoring
- TreeWalker: MDN Walker interface, text node extraction only
- Set-of-Marks: Visual grounding technique used in SeeAct, UI-TARS
Last Updated: January 2026
Version: 2.0 (Refactored with return_content integration, numeric ref resolution)
- The browser runs in headed (visible) mode by default.
- Browser profile is persisted at
~/.opencode/browser-profileby default. - All tools auto-initialize the browser on first use.
- Screenshots are returned as base64 PNG images.