From 9bb116eba018c09fe5b4db7e7948bd7b58e7aa8d Mon Sep 17 00:00:00 2001 From: Luke Parker <10430890+Hona@users.noreply.github.com> Date: Fri, 13 Nov 2026 16:59:35 +1000 Subject: [PATCH] fix(browser): report preview addresses consistently to browser tools --- packages/gui-plugins/src/browser/chromium.ts | 16 ++++++++-------- packages/gui-plugins/src/browser/diagnostics.ts | 6 +++--- packages/plugin-browser/src/rpc.ts | 6 +++--- packages/plugin-browser/src/skill.ts | 2 +- packages/plugin-browser/src/tools.ts | 3 ++- packages/plugin-browser/test/tools.test.ts | 2 +- 6 files changed, 18 insertions(+), 17 deletions(-) diff --git a/packages/gui-plugins/src/browser/chromium.ts b/packages/gui-plugins/src/browser/chromium.ts index c9befc9a5bc..fc858ce5514 100644 --- a/packages/gui-plugins/src/browser/chromium.ts +++ b/packages/gui-plugins/src/browser/chromium.ts @@ -337,9 +337,9 @@ export function createBrowserPage( const cdp = createCdp(contents) const documents = new Map() - const sourceURLs = () => [...new Set([contents.getURL(), ...documents.values()])].sort() + const sourceURLs = () => [...new Set([state().url, ...documents.values()])].sort() const files = createBrowserFiles(sourceURLs) - const diagnostics = createDiagnostics(cdp) + const diagnostics = createDiagnostics(cdp, displayURL) const profiling = createProfiling(contents, cdp, files, sourceURLs, options.shared) const refs = new Map() // Elements the user picked. Their refs outlive later snapshots, so a comment keeps pointing at @@ -388,7 +388,7 @@ export function createBrowserPage( const runs = new Map() let revision = 0 cdp.on("Page.frameNavigated", ({ frame }) => { - documents.set(frame.id, frame.url) + documents.set(frame.id, displayURL(frame.url)) revision++ }) cdp.on("Page.frameDetached", ({ frameId, reason }) => { @@ -665,7 +665,7 @@ export function createBrowserPage( cdp.on("Page.javascriptDialogOpening", (event) => dispatchDialog({ type: "dialog.opened", - url: event.url, + url: displayURL(event.url), dialogType: event.type, message: event.message.slice(0, Browser.MAX_TEXT), defaultPrompt: event.defaultPrompt ?? "", @@ -1305,7 +1305,7 @@ export function createBrowserPage( async function inspect(action: Browser.Action): Promise { // Most CDP queries cannot run while a JavaScript dialog blocks the renderer. if (action.type === "dialog") - return selectDialogTarget(dialogGate, documentGeneration(documentState), contents.getURL()) + return selectDialogTarget(dialogGate, documentGeneration(documentState), state().url) const fileIDs = action.type === "heap.compare" ? [action.before, action.after] : "fileID" in action ? [action.fileID] : [] @@ -1343,7 +1343,7 @@ export function createBrowserPage( ? sourceURLs() : urls.length ? urls - : [contents.getURL()], + : [state().url], ), ].sort(), key: JSON.stringify([documentGeneration(documentState), revision, selected]), @@ -1378,7 +1378,7 @@ export function createBrowserPage( } async function frames() { - return agentFrames(await frameTrees()) + return agentFrames(await frameTrees()).map((frame) => ({ ...frame, url: frame.parentID ? displayURL(frame.url) : state().url })) } async function frameTrees() { @@ -1407,7 +1407,7 @@ export function createBrowserPage( if (access.type !== "frame") throw new Error(access.message) - return access.frame + return { ...access.frame, url: access.frame.parentID ? displayURL(access.frame.url) : state().url } } async function frameContext(frameID: string) { diff --git a/packages/gui-plugins/src/browser/diagnostics.ts b/packages/gui-plugins/src/browser/diagnostics.ts index a835cf26d57..5d4f8c66cc9 100644 --- a/packages/gui-plugins/src/browser/diagnostics.ts +++ b/packages/gui-plugins/src/browser/diagnostics.ts @@ -16,7 +16,7 @@ import { const levels = ["debug", "info", "warning", "error"] as const -export function createDiagnostics(cdp: Cdp) { +export function createDiagnostics(cdp: Cdp, displayURL: (url: string) => string) { const messages: Browser.ConsoleEntry[] = [] // Every hop by its public id, oldest first. const requests = new Map() @@ -43,7 +43,7 @@ export function createDiagnostics(cdp: Cdp) { textTruncated: text.length > 2_000, } - messages.push(source ? { ...entry, source } : entry) + messages.push(source ? { ...entry, source: { ...source, url: displayURL(source.url) } } : entry) if (messages.length > 500) { messages.shift() @@ -148,7 +148,7 @@ export function createDiagnostics(cdp: Cdp) { }) cdp.on("Network.requestWillBeSent", (event, sessionID) => - apply(`${sessionID ?? ""}:${event.requestId}`, { type: "request", id: `${scope}:${++sequence}`, sessionID, event }), + apply(`${sessionID ?? ""}:${event.requestId}`, { type: "request", id: `${scope}:${++sequence}`, sessionID, event: { ...event, request: { ...event.request, url: displayURL(event.request.url) } } }), ) cdp.on("Network.requestWillBeSentExtraInfo", (event, sessionID) => apply(`${sessionID ?? ""}:${event.requestId}`, { type: "request.extra", headers: event.headers }), diff --git a/packages/plugin-browser/src/rpc.ts b/packages/plugin-browser/src/rpc.ts index e3d318e33d8..577e4aa415b 100644 --- a/packages/plugin-browser/src/rpc.ts +++ b/packages/plugin-browser/src/rpc.ts @@ -242,7 +242,7 @@ export const Operations = [ ), operation( "tabs.open", - "Open a browser tab at an http(s) address, show it in the Review pane, and select it. Defaults to about:blank. Website traffic uses the connected server's network; localhost reaches that server. A local file is not a web address: show it with browser.preview({ path }).", + "Open an HTTP(S) tab in the Review pane. Defaults to about:blank. Website traffic uses the connected server's network; localhost reaches that server. To open another workspace file, use browser.preview({ path }) with its workspace-relative path, not a file: or internal preview URL.", { url: optional(short.annotate({ description: "An http:// or https:// address, or about:blank." })), focus: optional( @@ -269,12 +269,12 @@ export const Operations = [ operation( "preview", 'Show a file to the user. Opens the file in the Review pane and focuses its tab for viewing. Images and screenshots (PNG, JPEG, GIF, WebP, charts, plots, photos), SVG, audio, video (MP4, WebM), PDF documents, HTML pages, Markdown, Mermaid diagrams, CSV and TSV tables, and fonts render as a media preview; code and other text files display highlighted source. Use this to present an artifact, output, or result you created or changed instead of pasting its contents, describing it, or opening a file:// URL in a browser tab; it is the way to show a local HTML file. The path is server-local: relative to the workspace or absolute, for example preview({ path: "out/report.html" }). Fails when no file is there, or when no desktop app is connected (then tell the user the path).', - { path: short.annotate({ description: "Server-local path to the file, relative to the workspace or absolute." }) }, + { path: short.annotate({ description: "Server-local path, relative to the workspace or absolute. Pass another workspace path to move to another file; do not pass a file: or oc-preview: URL." }) }, Schema.Struct({ path: short }), ), operation( "navigate", - "Navigate this tab to an http(s) address or about:blank and wait for the document load. Element refs expire. A local file is not a web address: show it with browser.preview({ path }).", + "Navigate this tab to HTTP(S) or about:blank and wait for the document load. Element refs expire. In a workspace preview, click a relative link or call browser.preview({ path }) for another workspace file. Do not navigate to the file: display address or the isolated origin returned by page JavaScript.", { ...tab, url: short }, Tab, ), diff --git a/packages/plugin-browser/src/skill.ts b/packages/plugin-browser/src/skill.ts index c3fb2d3dd39..c5f82015a19 100644 --- a/packages/plugin-browser/src/skill.ts +++ b/packages/plugin-browser/src/skill.ts @@ -34,7 +34,7 @@ export const info = Skill.Info.make({ "## Show the user", "", "- `tools.browser.preview({ path })` opens a file you made, such as an image, HTML page, PDF, Markdown, Mermaid diagram, CSV or video, in the Review pane. Use it instead of pasting or describing the file. It fails if no file is at the path.", - "- `tabs.open` and `navigate` take http(s) addresses only. To show a local HTML file use `preview`; to script one, serve its folder over HTTP on the server and open `http://localhost:/`.", + "- `tabs.open` and `navigate` take web addresses. Use `preview({path})` with a workspace path to move between local HTML pages, or click a relative link. A preview's file: display URL identifies the file; page JavaScript sees its real, isolated oc-preview: origin. Neither is a navigation argument.", "- New tabs open in front by default so the user can watch. Pass `focus: false` only when the user asked you to work in the background.", "- `screenshot` works on a hidden tab; only `fullPage: true` needs the tab on screen in the Review pane, and on macOS no screenshot works while the OpenCode window is minimized or hidden. It returns the image for you to look at and a server-local file path.", "", diff --git a/packages/plugin-browser/src/tools.ts b/packages/plugin-browser/src/tools.ts index 0a65ea1d034..9e33160e089 100644 --- a/packages/plugin-browser/src/tools.ts +++ b/packages/plugin-browser/src/tools.ts @@ -275,10 +275,11 @@ export function normalizeAction(action: Exclude\`, and open that http://localhost:/ address.`, + message: `${JSON.stringify(value)} is a local file, not a web address. Use browser.preview({ path }) with its workspace-relative filesystem path, or click a link in the current preview.`, }) // The desktop refuses these too (gui-plugins browser/policy.ts `agentRefusal`); this names the reason early. if (Browser.extensionOrigin(value)) throw new Tool.Error({ message: Browser.extensionPageRefusal(value) }) diff --git a/packages/plugin-browser/test/tools.test.ts b/packages/plugin-browser/test/tools.test.ts index 84bc7175a0f..13afb105afa 100644 --- a/packages/plugin-browser/test/tools.test.ts +++ b/packages/plugin-browser/test/tools.test.ts @@ -42,7 +42,7 @@ test("URL normalization rejects filesystem paths instead of treating them as hos "~/report.html", ]) expect(() => navigate(path)).toThrow( - `${JSON.stringify(path)} is a local file, not a web address, so the browser cannot open it. To show it to the user, call browser.preview({ path: ${JSON.stringify(path)} }) instead;`, + `${JSON.stringify(path)} is a local file, not a web address. Use browser.preview({ path })`, ) expect(() => navigate("ftp://example.com/file")).toThrow( '"ftp://example.com/file" is not a browser URL. Use an http:// or https:// address',