diff --git a/packages/app/component-tests/browser-pane.spec.ts b/packages/app/component-tests/browser-pane.spec.ts index 45541a7e8650..492b1f3bebe0 100644 --- a/packages/app/component-tests/browser-pane.spec.ts +++ b/packages/app/component-tests/browser-pane.spec.ts @@ -1223,3 +1223,23 @@ story("keeps the selected device and its rotated viewport with its browser tab", await moreDevices.hover() await expect(pixel).toBeChecked() }) + +story("hands a sign-in to the system browser and reports the user's answer", async ({ page }) => { + const root = page.getByTestId("browser-pane-fixture") + await root.getByRole("button", { name: "Start handoff" }).click() + + const banner = root.locator('[data-component="browser-handoff"]') + await expect(banner).toHaveAttribute("data-state", "pending") + await expect(banner).toContainText("Sign in to alpha.example in your system browser") + await expect(banner).toContainText("https://alpha.example/") + await expect(banner.getByRole("button", { name: "Cancel" })).toBeVisible() + + await banner.getByRole("button", { name: "Open" }).click() + await expect(banner).toHaveAttribute("data-state", "opened") + await expect(banner).toContainText("Finish signing in to alpha.example") + await expect(banner).toContainText("OpenCode never sees that session") + + await banner.getByRole("button", { name: "Done" }).click() + await expect(banner).toHaveAttribute("data-state", "completed") + await expect(banner).toContainText("Finished in the system browser") +}) diff --git a/packages/gui-plugins/src/browser/connection.test.ts b/packages/gui-plugins/src/browser/connection.test.ts index d3d023da9612..bc6c5cfee0c7 100644 --- a/packages/gui-plugins/src/browser/connection.test.ts +++ b/packages/gui-plugins/src/browser/connection.test.ts @@ -38,6 +38,7 @@ function fixture(reject?: Error) { const registered: Parameters[0][] = [] const closed: string[] = [] const highlights: Parameters[0][] = [] + const handoffs: Parameters[0][] = [] const outcomes: string[] = [] const events: PaneEvent[] = [] @@ -49,6 +50,9 @@ function fixture(reject?: Error) { }, load: async () => undefined, command: async () => undefined, + handoff: async (input) => { + handoffs.push(input) + }, inspect: async () => undefined, highlight: async (input) => { highlights.push(input) @@ -80,7 +84,7 @@ function fixture(reject?: Error) { const binding = registered[0].binding const emit = (event: PaneEvent) => listeners.get(binding)?.(event) - return { registration, binding, emit, listeners, closed, highlights, outcomes, events } + return { registration, binding, emit, listeners, closed, highlights, handoffs, outcomes, events } } test("highlights of picked elements reach the page", async () => { @@ -94,6 +98,19 @@ test("highlights of picked elements reach the page", async () => { ]) }) +test("the user's handoff answers reach main with the id and the choice", async () => { + const app = fixture() + app.registration.handoff("handoff-1", "open") + app.registration.handoff("handoff-1", "done") + app.registration.handoff("handoff-1", "cancel") + await Bun.sleep(0) + expect(app.handoffs).toEqual([ + { binding: app.binding, id: "handoff-1", action: "open" }, + { binding: app.binding, id: "handoff-1", action: "done" }, + { binding: app.binding, id: "handoff-1", action: "cancel" }, + ]) +}) + test("a closed binding stops its events and answers, and closes once with main", async () => { const app = fixture() app.emit({ type: "focus", tabID }) diff --git a/packages/gui-plugins/src/browser/connection.ts b/packages/gui-plugins/src/browser/connection.ts index 52669ab9beef..ee7a4abdd2df 100644 --- a/packages/gui-plugins/src/browser/connection.ts +++ b/packages/gui-plugins/src/browser/connection.ts @@ -17,6 +17,7 @@ export type Registration = { /** Creates a restored tab's page, which then reports its embed. */ load(tabID: Browser.TabID): void command(command: Browser.Action): Promise + handoff(id: string, action: "open" | "done" | "cancel"): Promise inspect(tabID: Browser.TabID, enabled: boolean): void highlight(tabID: Browser.TabID, ref?: Browser.Ref): void zoom(tabID: Browser.TabID, zoom: Zoom): void @@ -62,6 +63,7 @@ export function openBinding(input: { void ready.then(() => client.load({ binding, tabID })).catch(() => undefined) }, command: (command) => ready.then(() => client.command({ binding, command })), + handoff: (id, action) => ready.then(() => client.handoff({ binding, id, action })), inspect(tabID, enabled) { if (status.closed) return void ready.then(() => client.inspect({ binding, tabID, enabled })).catch(() => undefined) diff --git a/packages/gui-plugins/src/browser/handoff.test.ts b/packages/gui-plugins/src/browser/handoff.test.ts new file mode 100644 index 000000000000..0add456960de --- /dev/null +++ b/packages/gui-plugins/src/browser/handoff.test.ts @@ -0,0 +1,188 @@ +import { expect, test } from "bun:test" +import { Browser } from "@opencode/plugin-browser/rpc" +import { createHandoffs } from "./handoff" +import type { HandoffEvent } from "./ipc" + +const tab = { + first: Browser.TabID.make(`tab_${crypto.randomUUID()}`), + second: Browser.TabID.make(`tab_${crypto.randomUUID()}`), +} + +const flush = () => new Promise((resolve) => setTimeout(resolve, 0)) + +function setup(options: { openExternal?: (url: string) => Promise; timeoutMs?: number } = {}) { + const opened: string[] = [] + const events: HandoffEvent[] = [] + let ids = 0 + + const handoffs = createHandoffs({ + openExternal: + options.openExternal ?? + ((url) => { + opened.push(url) + + return Promise.resolve() + }), + timeoutMs: options.timeoutMs, + createID: () => `handoff-${++ids}`, + }) + + const begin = (input: Partial[0]> = {}) => + handoffs.begin({ + binding: "binding", + tabID: tab.first, + url: "https://github.com/settings/tokens", + signal: new AbortController().signal, + emit: (event) => events.push(event), + ...input, + }) + + return { handoffs, opened, events, begin } +} + +test("nothing opens until the user's Open, then the answer settles once", async () => { + const { handoffs, opened, events, begin } = setup() + + const result = begin() + await flush() + + expect(opened).toEqual([]) + expect(events).toEqual([ + { + type: "handoff", + tabID: tab.first, + id: "handoff-1", + host: "github.com", + url: "https://github.com/settings/tokens", + state: "pending", + }, + ]) + + expect(handoffs.settle("binding", "handoff-2", "open")).toBe(false) + expect(handoffs.settle("other-binding", "handoff-1", "open")).toBe(false) + expect(handoffs.settle("binding", "handoff-1", "open")).toBe(true) + expect(handoffs.settle("binding", "handoff-1", "open")).toBe(true) + await flush() + + expect(opened).toEqual(["https://github.com/settings/tokens"]) + expect(events.at(-1)).toEqual({ + type: "handoff", + tabID: tab.first, + id: "handoff-1", + host: "github.com", + url: "https://github.com/settings/tokens", + state: "opened", + }) + + expect(handoffs.settle("binding", "handoff-1", "done")).toBe(true) + expect(handoffs.settle("binding", "handoff-1", "done")).toBe(false) + expect(await result).toEqual({ state: "completed", host: "github.com" }) + expect(events.at(-1)).toEqual({ + type: "handoff", + tabID: tab.first, + id: "handoff-1", + host: "github.com", + state: "completed", + }) +}) + +test("the user can abandon the handoff before or after opening", async () => { + const { handoffs, opened, events, begin } = setup() + + const result = begin() + await flush() + handoffs.settle("binding", "handoff-1", "cancel") + expect(await result).toEqual({ state: "cancelled", host: "github.com" }) + expect(opened).toEqual([]) + expect(events.at(-1)?.state).toBe("cancelled") + + const second = begin() + await flush() + handoffs.settle("binding", "handoff-2", "open") + await flush() + handoffs.settle("binding", "handoff-2", "cancel") + expect(await second).toEqual({ state: "cancelled", host: "github.com" }) + expect(opened).toEqual(["https://github.com/settings/tokens"]) +}) + +test("a handoff nobody answers times out and frees the pane", async () => { + const { opened, events, begin } = setup({ timeoutMs: 5 }) + + const result = begin() + expect(await result).toEqual({ state: "timedOut", host: "github.com" }) + expect(opened).toEqual([]) + expect(events.map((event) => event.state)).toEqual(["pending", "timedOut"]) +}) + +test("cancelling the browser request ends a waiting handoff", async () => { + const abort = new AbortController() + const { opened, events, begin } = setup() + + const result = begin({ signal: abort.signal }) + await flush() + abort.abort() + expect(await result).toEqual({ state: "cancelled", host: "github.com" }) + expect(opened).toEqual([]) + expect(events.at(-1)?.state).toBe("cancelled") +}) + +test("one pane waits on one handoff at a time", async () => { + const { handoffs, opened, begin } = setup() + + const first = begin() + await flush() + expect(() => begin()).toThrow("A handoff is already waiting") + expect(() => begin({ tabID: tab.second })).toThrow("A handoff is already waiting") + expect(opened).toEqual([]) + + handoffs.settle("binding", "handoff-1", "cancel") + await first +}) + +test("only web pages without embedded credentials can move to the system browser", async () => { + const { opened, begin } = setup() + + for (const url of ["file:///tmp/secret.txt", "about:blank", "https://user:pass@github.com/", "not a url"]) { + await expect(begin({ url })).rejects.toThrow() + } + + expect(opened).toEqual([]) +}) + +test("a launch the user started but the system refused never echoes the URL", async () => { + const { handoffs, events, begin } = setup({ + openExternal: () => Promise.reject(new Error("LSOpenURLsWithRole() failed for https://github.com/settings/tokens")), + }) + + const result = begin() + await flush() + handoffs.settle("binding", "handoff-1", "open") + + let message = "" + + try { + await result + } catch (error) { + message = error instanceof Error ? error.message : String(error) + } + + expect(message).toContain("Could not open the system browser") + expect(message).not.toContain("github.com") + expect(events.at(-1)?.state).toBe("failed") +}) + +test("a closed tab or binding ends its waiting handoff", async () => { + const { handoffs, events, begin } = setup() + + const result = begin() + await flush() + handoffs.cancelTab("binding", tab.second) + expect(events.at(-1)?.state).toBe("pending") + handoffs.cancelTab("binding", tab.first) + expect(await result).toEqual({ state: "cancelled", host: "github.com" }) + + const second = begin({ tabID: tab.second }) + await flush() + handoffs.cancelBinding("binding") + expect(await second).toEqual({ state: "cancelled", host: "github.com" }) +}) diff --git a/packages/gui-plugins/src/browser/handoff.ts b/packages/gui-plugins/src/browser/handoff.ts new file mode 100644 index 000000000000..bb451f0af3bc --- /dev/null +++ b/packages/gui-plugins/src/browser/handoff.ts @@ -0,0 +1,154 @@ +import type { Browser } from "@opencode/plugin-browser/rpc" +import type { HandoffEvent } from "./ipc" +import { destinationOrigin } from "./policy" + +/** The outcomes a handoff settles with; a launch the user started that failed rejects instead. */ +export type HandoffOutcome = Extract + +export type HandoffResult = { readonly state: HandoffOutcome; readonly host: string } + +type Pending = { + readonly id: string + readonly tabID: Browser.TabID + readonly url: string + opened: boolean + readonly launch: () => Promise + readonly finish: (state: HandoffOutcome) => void +} + +/** + * Waits for the user to move one page to the system browser and say they are done. + * + * Nothing opens until the user chooses Open in the Review pane: the pane never launches the user's browser on the + * agent's word alone. The pane also cannot inherit the external browser's session — copying its cookies or + * credentials would be exactly the transfer this feature exists to avoid — so a handoff reports only whether the + * user finished, and the page here stays signed out. The URL is used once, on the user's click, then dropped; + * events and results carry the host only, except the URL the banner shows while asking. + */ +export function createHandoffs(input: { + openExternal: (url: string) => Promise + timeoutMs?: number + createID?: () => string +}) { + const pending = new Map() + const timeoutMs = input.timeoutMs ?? 10 * 60_000 + const createID = input.createID ?? (() => `handoff_${crypto.randomUUID()}`) + + async function begin(request: { + binding: string + tabID: Browser.TabID + url: string + signal: AbortSignal + emit: (event: HandoffEvent) => void + }): Promise { + if (pending.has(request.binding)) + throw new Error( + "A handoff is already waiting in this pane. Finish or cancel it in the Review pane before starting another.", + ) + + if (!destinationOrigin(request.url)) + throw new Error( + "Only HTTP and HTTPS pages can move to the system browser. Open the sign-in page in this tab first.", + ) + + if (request.signal.aborted) + throw new Error( + "Browser request was cancelled before the system browser opened. Inspect the tab before retrying.", + ) + + const url = new URL(request.url) + const host = url.host + const id = createID() + const result = Promise.withResolvers() + + let timer: ReturnType | undefined + + // The user's Open click, not the agent's request, launches the browser. Electron's rejection can name the URL, + // so the error the agent reads is this one instead. + const launch = async () => { + try { + await input.openExternal(url.href) + } catch { + if (!close()) return + + request.emit({ type: "handoff", tabID: request.tabID, id, host, state: "failed" }) + result.reject( + new Error( + "Could not open the system browser. The page stayed in the Review pane; use its Open in system browser button, then finish sign-in there.", + ), + ) + + return + } + + if (pending.get(request.binding)?.id === id) + request.emit({ type: "handoff", tabID: request.tabID, id, host, url: url.href, state: "opened" }) + } + + const close = () => { + if (pending.get(request.binding)?.id !== id) return false + + pending.delete(request.binding) + + if (timer) clearTimeout(timer) + + request.signal.removeEventListener("abort", abort) + + return true + } + + const finish = (state: HandoffOutcome) => { + if (!close()) return + + request.emit({ type: "handoff", tabID: request.tabID, id, host, state }) + result.resolve({ state, host }) + } + + const abort = () => finish("cancelled") + + pending.set(request.binding, { id, tabID: request.tabID, url: url.href, opened: false, launch, finish }) + + timer = setTimeout(() => finish("timedOut"), timeoutMs) + + request.signal.addEventListener("abort", abort, { once: true }) + request.emit({ type: "handoff", tabID: request.tabID, id, host, url: url.href, state: "pending" }) + + return result.promise + } + + function settle(binding: string, id: string, action: "open" | "done" | "cancel") { + const current = pending.get(binding) + + if (!current || current.id !== id) return false + + if (action === "open") { + // A second Open while the browser is starting must not launch it twice. + if (current.opened) return true + current.opened = true + + void current.launch() + + return true + } + + current.finish(action === "done" ? "completed" : "cancelled") + + return true + } + + function cancelTab(binding: string, tabID: Browser.TabID) { + const current = pending.get(binding) + + if (current?.tabID === tabID) current.finish("cancelled") + } + + function cancelBinding(binding: string) { + pending.get(binding)?.finish("cancelled") + } + + function cancelAll() { + Array.from(pending.values()).forEach((current) => current.finish("cancelled")) + } + + return { begin, settle, cancelTab, cancelBinding, cancelAll } +} diff --git a/packages/gui-plugins/src/browser/i18n/en.ts b/packages/gui-plugins/src/browser/i18n/en.ts index 8ca25bff0fbd..4e3e0eff8ecd 100644 --- a/packages/gui-plugins/src/browser/i18n/en.ts +++ b/packages/gui-plugins/src/browser/i18n/en.ts @@ -55,6 +55,19 @@ export default { "external.open": "Open in system browser", "external.menu": "Open in browser", "external.unavailable": "This address only works on the server's machine.", + "handoff.pending.title": "Sign in to {{host}} in your system browser", + "handoff.pending.description": + "The in-app browser can't complete a passkey, security key, or this site's single sign-on. Open this page in your default browser to sign in; OpenCode never sees that session.", + "handoff.opened.title": "Finish signing in to {{host}}", + "handoff.opened.description": + "Continue in the system browser that just opened. OpenCode never sees that session or its credentials, and this tab stays signed out.", + "handoff.completed.title": "Finished in the system browser", + "handoff.cancelled.title": "Sign-in handoff cancelled", + "handoff.timedOut.title": "Sign-in handoff timed out", + "handoff.failed.title": "Couldn't open the system browser", + "handoff.open": "Open", + "handoff.done": "Done", + "handoff.cancel": "Cancel", options: "Browser options", "zoom.in": "Zoom in", "zoom.out": "Zoom out", diff --git a/packages/gui-plugins/src/browser/ipc.ts b/packages/gui-plugins/src/browser/ipc.ts index cbb73cbcf624..186e7f1fc785 100644 --- a/packages/gui-plugins/src/browser/ipc.ts +++ b/packages/gui-plugins/src/browser/ipc.ts @@ -47,6 +47,16 @@ export type PaneElement = typeof PaneElement.Type export const PaneEvent = Schema.Union([ Schema.Struct({ type: Schema.Literal("focus"), tabID: Browser.TabID }), Schema.Struct({ type: Schema.Literal("preview"), path: text(2_048) }), + // The pane asks the user before opening the system browser, then waits for their answer; it never receives that + // session. The URL appears only while asking: it names the page the user's browser would open. + Schema.Struct({ + type: Schema.Literal("handoff"), + tabID: Browser.TabID, + id: text(128), + host: detail(2_048), + url: Schema.optionalKey(detail(2_048)), + state: Schema.Literals(["pending", "opened", "completed", "cancelled", "timedOut", "failed"]), + }), Schema.Struct({ type: Schema.Literal("state"), state: Schema.NullOr(Browser.State), @@ -86,6 +96,9 @@ export const PaneEvent = Schema.Union([ export type PaneEvent = typeof PaneEvent.Type +/** A sign-in handoff's state in a tab, as the pane reports it; the pane never receives the external session. */ +export type HandoffEvent = Extract + /** * The `error` of the last `state` event of a binding that main closed. Other errors on a `state` event, such as * `"page_crashed"` or a refused address, leave the binding open. @@ -127,6 +140,10 @@ export const BrowserPane = Ipc.define({ // Creates the page of a restored tab the first time the pane shows it. load: { input: Schema.Struct({ binding, tabID: Browser.TabID }) }, command: { input: Schema.Struct({ binding, command: Browser.Action }) }, + // The user opened, finished, or abandoned a handoff in this window; only their click opens the system browser. + handoff: { + input: Schema.Struct({ binding, id: text(128), action: Schema.Literals(["open", "done", "cancel"]) }), + }, // Starts or stops the page's element picker; the page reports picks and exits as inspect events. inspect: { input: Schema.Struct({ binding, tabID: Browser.TabID, enabled: Schema.Boolean }) }, // Highlights a picked element briefly, or clears any highlight when ref is omitted. diff --git a/packages/gui-plugins/src/browser/main.ts b/packages/gui-plugins/src/browser/main.ts index 3e8a4658356b..f2758095424d 100644 --- a/packages/gui-plugins/src/browser/main.ts +++ b/packages/gui-plugins/src/browser/main.ts @@ -84,6 +84,7 @@ const setup: Plugin.MainSetup = (ctx) => { register: async (input, caller) => (await load()).register(caller.window, input.binding, input), load: async (input, caller) => (await pane()).load(caller.window, input.binding, input.tabID), command: async (input, caller) => (await pane()).command(caller.window, input.binding, input.command), + handoff: async (input, caller) => (await pane()).handoff(caller.window, input.binding, input.id, input.action), inspect: async (input, caller) => (await pane()).inspect(caller.window, input.binding, input.tabID, input.enabled), highlight: async (input, caller) => (await pane()).highlight(caller.window, input.binding, input.tabID, input.ref), zoom: async (input, caller) => (await pane()).zoom(caller.window, input.binding, input.tabID, input.zoom), diff --git a/packages/gui-plugins/src/browser/model.ts b/packages/gui-plugins/src/browser/model.ts index 5453610231eb..7a9c6a19204a 100644 --- a/packages/gui-plugins/src/browser/model.ts +++ b/packages/gui-plugins/src/browser/model.ts @@ -16,7 +16,7 @@ import { import { recordable, remember, withIcon } from "./history" import type definition from "./index" import { workspaceFileURL } from "./link" -import { BrowserPane, type PaneEvent, type Viewport } from "./ipc" +import { BrowserPane, type HandoffEvent, type PaneEvent, type Viewport } from "./ipc" import { addressFocusTab, createInitialState, @@ -53,6 +53,8 @@ type ModelState = { machine: State /** Presentation details for the tabs in each desktop binding. */ pages: Record | undefined> + /** The handoff waiting in each tab, or its last answer until the banner clears. */ + handoffs: Record | undefined> } /** A mounted pane. Commands target the newest visible pane. */ @@ -74,6 +76,9 @@ type PaneHandle = { focused: () => void } +/** How long a finished handoff's banner stays, so the user reads how the sign-in ended. */ +const HANDOFF_BANNER_MS = 6_000 + export type Model = ReturnType // Attachments belong to the shell session tab, not the session route: native pages and the agent's @@ -102,7 +107,13 @@ export function createModel(ctx: SetupContext) { const owner = getOwner() const history = ctx.stores.history - const [state, setState] = createStore({ machine: createInitialState(untrack(ipc)), pages: {} }) + + const [state, setState] = createStore({ + machine: createInitialState(untrack(ipc)), + pages: {}, + handoffs: {}, + }) + const machine = () => state.machine const [panes, setPanes] = createSignal([]) const visiblePane = () => panes().find((handle) => handle.visible()) @@ -111,6 +122,8 @@ export function createModel(ctx: SetupContext) { const registrations = new Map() const registerTimers = new Map>() const retryTimers = new Map>() + // A finished handoff's banner clears itself once the user has read its outcome. + const handoffTimers = new Map>() const servers = new Map void>() const listeners = new Map void>() const inspectors = new Map void>>() @@ -153,6 +166,7 @@ export function createModel(ctx: SetupContext) { if (current?.attempt !== effect.attempt) return registrations.delete(effect.sessionKey) setState("pages", effect.sessionKey, undefined) + clearHandoffs(effect.sessionKey) current.registration.close() return @@ -281,6 +295,8 @@ export function createModel(ctx: SetupContext) { if (event.type === "page") return page(sessionKey, event) + if (event.type === "handoff") return handoffEvent(sessionKey, event) + // Only the pane on screen holds the page that had focus. if (sessions.current()?.key !== sessionKey) return @@ -335,6 +351,11 @@ export function createModel(ctx: SetupContext) { Object.keys(state.pages[effect.sessionKey] ?? {}).forEach((id) => { if (!keep.has(id)) setState("pages", effect.sessionKey, id, undefined) }) + Object.keys(state.handoffs[effect.sessionKey] ?? {}).forEach((id) => { + if (keep.has(id)) return + cancel(handoffTimers, `${effect.sessionKey}\n${id}`) + setState("handoffs", effect.sessionKey, id, undefined) + }) layout .stored(ref) .filter((tabID) => !keep.has(tabID)) @@ -378,6 +399,24 @@ export function createModel(ctx: SetupContext) { if (visits) history.set({ visits }) } + // A handoff's answer stays briefly so the banner can say how the sign-in ended, then clears itself. + const handoffEvent = (sessionKey: string, event: HandoffEvent) => { + const tabKey = `${sessionKey}\n${event.tabID}` + + if (event.state === "pending" || event.state === "opened") cancel(handoffTimers, tabKey) + else + schedule(handoffTimers, tabKey, HANDOFF_BANNER_MS, () => setState("handoffs", sessionKey, event.tabID, undefined)) + setState("handoffs", sessionKey, { [event.tabID]: event }) + } + + // Ends a session's banners; the pane cancels their handoffs when the binding or the session goes away. + const clearHandoffs = (sessionKey: string) => { + Array.from(handoffTimers.keys()) + .filter((item) => item.startsWith(`${sessionKey}\n`)) + .forEach((item) => cancel(handoffTimers, item)) + setState("handoffs", sessionKey, undefined) + } + const detach = (sessionKey: string) => { const drive = drives.get(sessionKey) drives.delete(sessionKey) @@ -386,6 +425,7 @@ export function createModel(ctx: SetupContext) { .filter((item) => item.startsWith(`${sessionKey}\n`)) .forEach((item) => recorded.delete(item)) setState("pages", sessionKey, undefined) + clearHandoffs(sessionKey) } // A server whose plugin lacked the browser RPC may have it once its plugins finish loading, its event stream @@ -618,6 +658,18 @@ export function createModel(ctx: SetupContext) { }, /** The page's presentation details, once reported by its native tab. */ page: (session: Session, tabID: string) => state.pages[session.key]?.[tabID], + /** The handoff waiting in this tab, or its outcome until its banner clears. */ + handoffFor: (session: Session, tabID: string) => state.handoffs[session.key]?.[tabID], + /** The user's Open, Done, or Cancel for the handoff waiting in this tab; one the desktop no longer knows ends here. */ + handoff(session: Session, tabID: string, id: string, action: "open" | "done" | "cancel") { + const answer = registration(session)?.handoff(id, action) + + if (answer) + void answer.catch(() => { + // The pane finished this handoff while its outcome event never reached the window; the banner cannot answer. + if (state.handoffs[session.key]?.[tabID]?.id === id) setState("handoffs", session.key, tabID, undefined) + }) + }, zoom(session: Session, tabID: Browser.TabID, zoom: Zoom) { registration(session)?.zoom(tabID, zoom) }, diff --git a/packages/gui-plugins/src/browser/pane.ts b/packages/gui-plugins/src/browser/pane.ts index c882ecc89826..7a39ce066c7b 100644 --- a/packages/gui-plugins/src/browser/pane.ts +++ b/packages/gui-plugins/src/browser/pane.ts @@ -9,6 +9,7 @@ import type { BuiltinMainStorage } from "@opencode/plugin/gui/builtin" import type { Embeds, Persisted, ServerEndpoints, Windows } from "@opencode/plugin/gui/main" import { createBrowserPage, type BrowserPage, type Shared } from "./chromium" import { browserFailure } from "./errors" +import { createHandoffs } from "./handoff" import { createBrowserNetwork, type BrowserNetwork } from "./network" import { destinationOrigin, fileURLWithin, refusal } from "./policy" import { createRefs } from "./refs" @@ -82,6 +83,8 @@ export function createBrowserPane(input: { const disposals = new WeakMap>() // Keep long-lived RPC requests off Chromium's shared HTTP connection pool. const runtime = ManagedRuntime.make(NodeHttpClient.layerNodeHttp) + // Handoffs wait for a person, not for Chromium, and outlive any one page attempt. + const handoffs = createHandoffs({ openExternal: (url) => electron.shell.openExternal(url) }) return { async register(window: number, binding: string, target: Target) { @@ -355,13 +358,21 @@ export function createBrowserPane(input: { }, async close(window: number, binding: string) { owned(window, binding) + handoffs.cancelBinding(binding) dispatchBinding(binding, { type: "client.closed" }) }, + /** The user opened, finished, or abandoned a handoff shown in this window; only their Open launches the browser. */ + handoff(window: number, binding: string, id: string, action: "open" | "done" | "cancel") { + owned(window, binding) + + if (!handoffs.settle(binding, id, action)) throw new Error("browser.pane.handoff.unknown") + }, /** * The plugin is going away (reload, disable, or quit). Windows keep each pane's tabs as a * suspended attachment, like an idle server eviction, and register again once the pane is back. */ async dispose() { + handoffs.cancelAll() dispatch({ type: "disposed" }) await Promise.all(releasing) await runtime.dispose() @@ -419,6 +430,7 @@ export function createBrowserPane(input: { if (page) release(page) }) handles.delete(effect.binding) + handoffs.cancelBinding(effect.binding) clearTimeout(handle.timer) handle.registered.reject(new Error("browser.pane.registration.closed")) handle.cleanup() @@ -617,6 +629,7 @@ export function createBrowserPane(input: { } if (action.type === "tabs.close") { + handoffs.cancelTab(binding, action.tabID) const closing = page(binding, action.tabID) dispatchBinding(binding, { type: "tab.closed", tabID: action.tabID }) @@ -625,6 +638,18 @@ export function createBrowserPane(input: { return { value: inventory(binding), files: [] } } + if (action.type === "handoff") + return { + value: await handoffs.begin({ + binding, + tabID: action.tabID, + url: slot.tab.url, + signal, + emit: (event) => emit(binding, event), + }), + files: [], + } + const target = load(binding, action.tabID) if (!target) throw new Error("Browser tab is unavailable. Call browser.tabs.list({}) and use an existing tabID.") diff --git a/packages/gui-plugins/src/browser/panel.fixture.tsx b/packages/gui-plugins/src/browser/panel.fixture.tsx index 2cec1215021e..c83022ccc4fb 100644 --- a/packages/gui-plugins/src/browser/panel.fixture.tsx +++ b/packages/gui-plugins/src/browser/panel.fixture.tsx @@ -125,6 +125,7 @@ type StoryHost = Pick & { reconnect: () => void external: string | undefined comments: readonly ComposerNote[] + handoff: () => void }> } @@ -415,6 +416,25 @@ export function browserPaneFixture(input: PaneHost | StoryHost) { const embed = (tab: (typeof tabs)[number]) => emit(tab.owner, { type: "embed", tabID: tab.id, embed: `embed-${tab.title}` }) + // The agent's browser.handoff tool: the banner asks the user before anything opens in the system browser. + let handoffs = 0 + + // The desktop reports the handoff's next state for each answer the banner offers. + const handoffAnswers = { open: "opened", done: "completed", cancel: "cancelled" } as const + + const startHandoff = (key = store.session) => { + const tab = current(key) + + emit(key, { + type: "handoff", + tabID: tab.id, + id: `handoff-${++handoffs}`, + host: new URL(tab.url).host, + url: tab.url, + state: "pending", + }) + } + // Address rejection counters and cleanup run through the real model over this main-process Ipc boundary. const pane: PaneClient = { register: async (value) => { @@ -472,6 +492,22 @@ export function browserPaneFixture(input: PaneHost | StoryHost) { if (command.type === "stop") setStore("loading", false) report() }, + // The pane answers the banner: the desktop settles the waiting handoff and reports how it ended. + handoff: async (value) => { + const key = Array.from(bindings).find((item) => item[1] === value.binding)?.[0] + + if (!key) throw new Error("Unknown handoff binding") + const tab = current(key) + + emit(key, { + type: "handoff", + tabID: tab.id, + id: value.id, + host: new URL(tab.url).host, + url: value.action === "open" ? tab.url : undefined, + state: handoffAnswers[value.action], + }) + }, // The desktop confirms each picker change, as the page does once inspect mode is armed. inspect: async (value) => { const key = Array.from(bindings).find((item) => item[1] === value.binding)?.[0] @@ -604,6 +640,7 @@ export function browserPaneFixture(input: PaneHost | StoryHost) { reconnect={() => model.command({ key: store.session }, { type: "reload", tabID: current().id })} comments={store.comments} external={store.external.at(-1)} + handoff={startHandoff} > @@ -629,6 +666,12 @@ export function browserPaneFixture(input: PaneHost | StoryHost) { > Switch browser tab +