Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins
TypeScript ai-agentcoding-agentdesktop-appelectronglobali18nlocal-firstmcppipi-agentpi-desktoppluginsreactrusttypescript
fix(browser): require visible view for browser operations
vastsa95675ad1b7b8
diff --git a/apps/desktop/electron/main/browser-host.ts b/apps/desktop/electron/main/browser-host.tsindex 918a1b44a..0d6a5124a 100644--- a/apps/desktop/electron/main/browser-host.ts+++ b/apps/desktop/electron/main/browser-host.ts@@ -121,6 +121,18 @@ export class BrowserHost {};}+ private assertBrowserViewVisible(): void {+ if (+ this.chrome?.visible === true &&+ this.chrome.pluginId === BROWSER_PLUGIN_ID &&+ this.chrome.viewId === BROWSER_VIEW_ID+ ) return;+ throw Object.assign(+ new Error("Browser view must be visible before browser operations can run"),+ { code: "UNAVAILABLE" },+ );+ }+private key(sessionId: string, tabId: string | null): string {return JSON.stringify([sessionId, tabId]);}@@ -219,12 +231,13 @@ export class BrowserHost {}async navigate(input: BrowserNavigateInput, sessionId?: string, tabId?: string): Promise<BrowserState | null> {+ this.assertBrowserViewVisible();const target = String(input.path ?? input.url ?? "").trim();if (!target) return this.getState();const id = sessionId?.trim() || this.active?.sessionId || "";if (tabId !== undefined) {const page = this.pages.get(this.key(id, tabId));- return page ? this.navigatePage(page, target) : null;+ return page ? this.navigatePage(page, target, true) : null;}if (!this.active) {this.active = this.pageFor(id, this.selectedTabs.get(id) ?? null);@@ -234,18 +247,43 @@ export class BrowserHost {this.rememberLocation(id, target);return null;}- return this.navigatePage(this.active, target);+ return this.navigatePage(this.active, target, true);}- private async navigatePage(page: BrowserPage, target: string): Promise<BrowserState | null> {+ private async navigatePage(page: BrowserPage, target: string, requireVisible = false): Promise<BrowserState | null> {const version = ++page.navigationVersion;+ const previousLocation = this.locations.get(page.key);page.navigating = true;this.locations.set(page.key, target);const current = () => this.pages.get(page.key) === page && page.navigationVersion === version;- let state: BrowserState | null;+ const assertVisible = () => {+ if (!requireVisible) return;+ try {+ this.assertBrowserViewVisible();+ } catch (error) {+ page.navigating = false;+ if (previousLocation === undefined) this.locations.delete(page.key);+ else this.locations.set(page.key, previousLocation);+ throw error;+ }+ };+ let root: string | null;try {- const root = await this.deps.getFileRoot(page.sessionId || undefined);+ root = await this.deps.getFileRoot(page.sessionId || undefined);+ } catch (error) {if (!current()) return null;+ assertVisible();+ page.navigating = false;+ page.started = false;+ page.state = { url: target, title: "", isLoading: false, canGoBack: false,+ canGoForward: false, loadError: error instanceof Error ? error.message : String(error) };+ if (this.active === page) { this.publishCurrent(); this.applyGuest(); }+ return null;+ }+ if (!current()) return null;+ assertVisible();+ let state: BrowserState | null;+ try {state = await page.pane.navigateAndWait(target, root);} catch (error) {if (!current()) return null;@@ -270,6 +308,7 @@ export class BrowserHost {}action(action: "back" | "forward" | "reload" | "stop", sessionId?: string, tabId?: string): void {+ this.assertBrowserViewVisible();const page = sessionId !== undefined && tabId !== undefined? this.pages.get(this.key(sessionId, tabId)) : this.active;if (page?.state) page.pane.action(action);@@ -279,13 +318,20 @@ export class BrowserHost {return this.active?.state ? { ...this.active.state, ...this.getContext() } : null;}+ getStateForPlugin(): BrowserState | null {+ this.assertBrowserViewVisible();+ return this.getState();+ }+openExternal(sessionId?: string, tabId?: string): void {+ this.assertBrowserViewVisible();const page = sessionId !== undefined && tabId !== undefined? this.pages.get(this.key(sessionId, tabId)) : this.active;if (page?.started) page.pane.openExternal();}private currentPage(): BrowserPage {+ this.assertBrowserViewVisible();if (!this.active?.started) throw Object.assign(new Error("browser guest is not available"), { code: "UNAVAILABLE" });return this.active;}@@ -317,6 +363,7 @@ export class BrowserHost {async fill(uid: string, text: string): Promise<void> { await this.currentPage().cdp.fill(this.requireWebContents(), uid, text); }async evaluate(expression: string): Promise<unknown> { return this.currentPage().cdp.evaluate(this.requireWebContents(), expression); }console(limit?: number): { messages: ReturnType<BrowserCdp["console"]> } {+ this.assertBrowserViewVisible();if (!this.active?.started) return { messages: [] };const wc = this.requireWebContents();void this.active.cdp.attach(wc);diff --git a/apps/desktop/electron/main/services/plugin-services.ts b/apps/desktop/electron/main/services/plugin-services.tsindex 16211557d..25246a95b 100644--- a/apps/desktop/electron/main/services/plugin-services.ts+++ b/apps/desktop/electron/main/services/plugin-services.ts@@ -626,7 +626,7 @@ export function createPluginServices({action: (action, sessionId, tabId) => browserHost.action(action, sessionId, tabId),setBounds: (pluginId, hole) => browserHost.setGuestHole(pluginId, hole),setVisible: (pluginId, visible) => browserHost.setGuestVisible(pluginId, visible),- getState: () => browserHost.getState(),+ getState: () => browserHost.getStateForPlugin(),openExternal: (sessionId, tabId) => browserHost.openExternal(sessionId, tabId),snapshot: () => browserHost.snapshot(),screenshot: (input, sessionId) => browserHost.screenshot(input, sessionId),diff --git a/apps/desktop/test/browser-host-session.test.mjs b/apps/desktop/test/browser-host-session.test.mjsindex f67263293..3bdd79ae5 100644--- a/apps/desktop/test/browser-host-session.test.mjs+++ b/apps/desktop/test/browser-host-session.test.mjs@@ -147,6 +147,31 @@ test("closing the panel keeps a pending page hidden until the panel returns", ash.surface(); assert.equal(h.panes[0].visible, true);});+test("plugin browser operations are unavailable while the Browser view is hidden", async () => {+ const h = harness(); await open(h, "A", "a", "A.html");+ assert.equal(h.host.getStateForPlugin().url, "A.html");+ const pendingNavigation = h.host.navigate({ url: "https://fixture.invalid/hidden" });+ h.surface(false);+ h.roots.at(-1).resolve("/projects/A");+ await assert.rejects(pendingNavigation, (error) => error?.code === "UNAVAILABLE");+ h.surface(false);+ const unavailable = (error) => error?.code === "UNAVAILABLE";++ assert.equal(h.host.getState().url, "A.html", "host UI state remains readable");+ assert.throws(() => h.host.getStateForPlugin(), unavailable);+ assert.throws(() => h.host.action("reload"), unavailable);+ assert.throws(() => h.host.openExternal(), unavailable);+ assert.throws(() => h.host.console(), unavailable);+ await assert.rejects(h.host.navigate({ url: "https://fixture.invalid/hidden" }), unavailable);+ await assert.rejects(h.host.snapshot(), unavailable);+ await assert.rejects(h.host.screenshot(), unavailable);+ await assert.rejects(h.host.click("uid"), unavailable);+ await assert.rejects(h.host.fill("uid", "text"), unavailable);+ await assert.rejects(h.host.evaluate("document.title"), unavailable);+ await assert.rejects(h.host.cdpCommand("Page.enable"), unavailable);+ assert.equal(h.loads.length, 1, "hidden navigation must not reach the page");+});+test("closing a tab and disposing the host cancel pending ownership and release pages", async () => {const h = harness(); await open(h, "A", "a", "A.html");h.host.setChromeSession("A", "pending", "pending.html"); const root = h.roots.at(-1);diff --git a/docs/spec/07-plugins/03-plugin-api.md b/docs/spec/07-plugins/03-plugin-api.mdindex 045ad2820..a393b39c7 100644--- a/docs/spec/07-plugins/03-plugin-api.md+++ b/docs/spec/07-plugins/03-plugin-api.md@@ -723,6 +723,10 @@ the guest cannot cover chat/composer. `cdp` is deny-by-default; cookie,storage, target, and network-interception methods fail with`PERMISSION_DENIED`. Session identity for agent calls comes from the in-flight`plugins.execute` `sessionId`, not from plugin arguments (D333 / ADR 0170).+Page operations (`navigate`, `action`, `openExternal`, `getState`, snapshot,+screenshot, and CDP calls) are available only while the Browser view is+visible. Calls made while it is hidden fail with `UNAVAILABLE`; use+`BrowserPreview` to ask the host to reveal the Browser view before continuing.`getHistory` returns newest-first entries explicitly recorded by the host, withtext and images interleaved in capture order. Content written throughdiff --git a/docs/zh-CN/spec/07-plugins/03-plugin-api.md b/docs/zh-CN/spec/07-plugins/03-plugin-api.mdindex f60b8bc3a..a92091994 100644--- a/docs/zh-CN/spec/07-plugins/03-plugin-api.md+++ b/docs/zh-CN/spec/07-plugins/03-plugin-api.md@@ -585,6 +585,9 @@ pi.browser.cdp(input: { method: string; params?: unknown }): Promise<unknown>`setBounds` 相对调用插件视图的内容区,并被夹紧,因此访客页不能盖住聊天/输入框。`cdp` 默认拒绝;cookie、storage、target 和网络拦截方法以 `PERMISSION_DENIED` 失败。代理调用的会话身份来自进行中的 `plugins.execute` `sessionId`,而不是插件参数(D333 / ADR 0170)。+页面操作(`navigate`、`action`、`openExternal`、`getState`、snapshot、screenshot 和 CDP 调用)+仅在 Browser 视图可见时可用。视图隐藏时调用会以 `UNAVAILABLE` 失败;应通过 `BrowserPreview`+请求宿主显示 Browser 视图后再继续。`getHistory` 返回由主机明确记录的条目,按最新优先排列,文本和图片按捕获时间混排。通过 `writeText` 写入的内容,以及 Composer 用户主动粘贴事件提供的内容会被记录;主机