mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-29 11:42:17 +00:00
fix(browser): report preview addresses consistently to browser tools
This commit is contained in:
parent
795a94a01f
commit
9bb116eba0
6 changed files with 18 additions and 17 deletions
|
|
@ -337,9 +337,9 @@ export function createBrowserPage(
|
|||
|
||||
const cdp = createCdp(contents)
|
||||
const documents = new Map<string, string>()
|
||||
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<string, Element>()
|
||||
// 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<number, { controller: AbortController; interrupted: Interrupted }>()
|
||||
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<Browser.Target> {
|
||||
// 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) {
|
||||
|
|
|
|||
|
|
@ -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<string, Hop>()
|
||||
|
|
@ -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 }),
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
),
|
||||
|
|
|
|||
|
|
@ -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:<port>/`.",
|
||||
"- `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.",
|
||||
"",
|
||||
|
|
|
|||
|
|
@ -275,10 +275,11 @@ export function normalizeAction(action: Exclude<Browser.Input, { readonly type:
|
|||
if (action.type !== "navigate" && action.type !== "tabs.open") return action
|
||||
if (action.type === "tabs.open" && action.url === undefined) return action
|
||||
const value = action.url?.trim() || "about:blank"
|
||||
if (/^oc-preview:/i.test(value)) throw new Tool.Error({ message: "This is an internal preview address. Use browser.preview({ path }) with a workspace-relative path, or click a link in the preview tab." })
|
||||
// A filesystem path would otherwise gain a scheme and parse as a hostname: /tmp/x becomes https://tmp/x.
|
||||
if (/^(?:[\\/.~]|[a-zA-Z]:[\\/])/.test(value) || /^file:/i.test(value))
|
||||
throw new Tool.Error({
|
||||
message: `${JSON.stringify(value)} 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(value)} }) instead; preview renders HTML pages, images, PDF, SVG, Markdown and other files in the Review pane. To script the page (click, evaluate, wait), serve its folder over HTTP on the server, for example with \`bunx serve <folder>\`, and open that http://localhost:<port>/ 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) })
|
||||
|
|
|
|||
|
|
@ -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',
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue