Skip to content

feat(opencode): add browser tool for visual UI verification - #48755

Closed
maskjelly wants to merge 4 commits into
anomalyco:devfrom
maskjelly:browser-tool
Closed

maskjelly wants to merge 4 commits into
anomalyco:devfrom
maskjelly:browser-tool

Conversation

@maskjelly

Copy link
Copy Markdown

Issue for this PR

Closes #48377
Related: #40782

Supersedes #48505, which was closed by the compliance bot before the description was updated to match the template. Same branch and commits.

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Adds a built-in browser tool so the agent can see and interact with web pages.

  • Actions: open, screenshot, click (selector or coordinates), type, press, scroll, read, back, forward, reload, close.
  • Screenshots come back as image attachments, so the model can actually see the page.
  • Drives a local Chrome / Chromium / Edge over CDP with a small WebSocket client launched via Bun.spawn — no new dependencies.
  • Discovers browsers on macOS / Linux / Windows; OPENCODE_BROWSER_PATH overrides.
  • Temporary profile, idle shutdown after 10 minutes, cleanup on exit.
  • Registered as a regular built-in tool, behind a new browser permission key; docs/tools.mdx and docs/permissions.mdx updated.

Scoped to the browser; OS-level desktop control is out of scope for this PR. Happy to adjust the tool surface (naming, action granularity, gating) based on maintainer feedback.

How did you verify your code works?

  • bun typecheck in packages/opencode — clean.
  • bun test test/tool — 350 pass, including new tests for URL normalization, key parsing, and platform browser discovery.
  • Integration test opens a real page in headless Chrome, captures a screenshot attachment, reads element text, clicks a link, and closes; it skips automatically when no Chrome-family browser is installed.

Screenshots / recordings

N/A — no UI changes. The tool returns page screenshots to the model as attachments.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Add a built-in browser tool that drives headless Chrome, Chromium, or Edge
over the Chrome DevTools Protocol. It opens pages, returns screenshots as
image attachments, and supports clicking, typing, pressing keys, scrolling,
and reading page text so the agent can verify front-end work end to end.

- no new dependencies: raw CDP over a WebSocket connection
- cross-platform browser discovery with OPENCODE_BROWSER_PATH override
- permission key "browser" with per-action patterns
- idle shutdown plus process exit cleanup
- unit tests and a Chrome-gated integration test
Run the browser tool with a visible window when a display is available so
the user can watch the agent work. Pass `headless: true` or set
`OPENCODE_BROWSER_HEADLESS=1` to hide it, and fall back to headless on
Linux hosts without a display.
@4Liberty

Copy link
Copy Markdown

Field report from an agent that used this tool on Windows 11 / desktop 2.0.14 — full measurements in #50781.

In short: screenshot refuses with "needs a visible tab" whenever the tab's view is not composited (the review pane is displaying another session), while snapshot / evaluate / navigate keep working on the same tab. Window focus, tab focus and pane instantiation were each tested and are not the cause; the hidden Chrome_WidgetWin_0 hosting the view is.

The fromSurface: true call is what cannot succeed in that state. A fallback to fromSurface: false (with captureBeyondViewport: false) makes capture work there and leaves the visible case untouched — sketch in the issue.

Also worth knowing: click / type already go through Input.dispatchMouseEvent / Input.insertText, which do not need a surface, so the UnknownVizError I saw on click is likely a host-side visibility gate rather than something this file can fix.

Happy to re-test against a build that includes the fallback.

@The-Code-Monkey

Copy link
Copy Markdown

+1 for this, something that I would genuinely use and have found useful in paid for software. If any help is wanted on this I am very much willing to help push this along.

@maskjelly

Copy link
Copy Markdown
Author

@thdxr please look into this , really requested feature implemented here

@The-Code-Monkey

Copy link
Copy Markdown

Would it be possible to also add the ability to open a browser tab on the right hand side panel instead of just files, that way other things can be possible in the future without leaving the app.

@russrapper15-png

russrapper15-png commented Sep 25, 2026 via email

Copy link
Copy Markdown

@DiazOlveraCo DiazOlveraCo left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix for Windows screenshot issue (fromSurface failure when tab not composited):

The issue is that Page.captureScreenshot with fromSurface: true fails on Windows with "needs a visible tab" when the tab's view isn't composited (e.g., review pane showing another session).

Suggested fix - add a fallback to fromSurface: false:

async function screenshot(browser: Browser, note: string, fullPage = false): Promise<Tool.ExecuteResult> {
  let shot: { data: string }
  try {
    shot = await page<{ data: string }>(browser, "Page.captureScreenshot", {
      format: "jpeg",
      quality: 80,
      fromSurface: true,
      captureBeyondViewport: fullPage,
    })
  } catch (error) {
    if (error instanceof Error && error.message.includes("visible tab")) {
      shot = await page<{ data: string }>(browser, "Page.captureScreenshot", {
        format: "jpeg",
        quality: 80,
        fromSurface: false,
        captureBeyondViewport: false,
      })
    } else {
      throw error
    }
  }
  // ... rest unchanged
}

This preserves full-page capture when fromSurface works, and falls back to viewport-only capture when it doesn't. The click/type/press/scroll actions already use Input.dispatchMouseEvent/Input.insertText which don't need a surface, so they're unaffected.

@maskjelly

maskjelly commented Oct 1, 2026 •

Copy link
Copy Markdown
Author

Thanks for the write-up and the suggested fix, both were spot on.

Pushed 1dc8c2c. captureScreenshot still tries fromSurface: true first, and only falls back to fromSurface: false with captureBeyondViewport: false when the capture fails on visibility. Full-page capture on a visible tab is unchanged.

@4Liberty if you can retest against that build, it'd be good to know it fixes it on your end. Agreed on the click / UnknownVizError point too, that one looks host-side so I've left it out of this change.

@thdxr

thdxr commented Oct 10, 2026

Copy link
Copy Markdown
Member

This branch has been upgraded to V2. We're no longer taking PRs for V1. If you think this is still relevant, please port it over and open it for V2.

— from 𝕺𝖕𝖊𝖓𝕮𝖔𝖉𝖊

@thdxr thdxr closed this Oct 10, 2026
@opencode-agent opencode-agent Bot mentioned this pull request Oct 19, 2026
1 task done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE]:Screen vision (screenshots) + browser control tools for the agent

6 participants