dsh-builtin-browser 0.5.2 → 0.5.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/CHANGELOG.md +145 -0
  2. package/README.en.md +77 -68
  3. package/README.md +69 -89
  4. package/client.js +147 -15
  5. package/desktop-bridge/desktop-identity.json +9 -0
  6. package/desktop-bridge/install.mjs +220 -1
  7. package/desktop-bridge/plugin-browser-bridge.js +5 -0
  8. package/docs/README.md +1 -1
  9. package/docs/architecture.md +1 -1
  10. package/docs/limitations.md +19 -1
  11. package/docs/tool-reference.md +2 -1
  12. package/lib/browser-electron/bridge-status.d.ts +5 -3
  13. package/lib/browser-electron/bridge-status.js +53 -9
  14. package/lib/browser-electron/desktop-bridge-host.d.ts +25 -25
  15. package/lib/browser-electron/desktop-bridge-host.js +67 -40
  16. package/lib/browser-electron/desktop-identity.d.ts +58 -0
  17. package/lib/browser-electron/desktop-identity.js +236 -0
  18. package/lib/browser-electron/electron-provision.d.ts +59 -0
  19. package/lib/browser-electron/electron-provision.js +176 -0
  20. package/lib/browser-electron/entry.js +12 -0
  21. package/lib/browser-electron/host-main.js +68 -14
  22. package/lib/browser-electron/provider.d.ts +53 -3
  23. package/lib/browser-electron/provider.js +26 -5
  24. package/lib/browser-electron/remote-host.d.ts +3 -3
  25. package/lib/browser-electron/remote-host.js +25 -11
  26. package/lib/browser-electron/settings-store.d.ts +19 -0
  27. package/lib/browser-electron/settings-store.js +5 -1
  28. package/package.json +2 -3
  29. package/src/browser-electron/bridge-status.ts +56 -10
  30. package/src/browser-electron/desktop-bridge-host.ts +70 -41
  31. package/src/browser-electron/desktop-identity.ts +260 -0
  32. package/src/browser-electron/electron-provision.ts +208 -0
  33. package/src/browser-electron/entry.ts +12 -0
  34. package/src/browser-electron/host-main.ts +62 -12
  35. package/src/browser-electron/provider.ts +73 -8
  36. package/src/browser-electron/remote-host.ts +26 -12
  37. package/src/browser-electron/settings-store.ts +24 -1
  38. package/src/types/electron-shim.d.ts +8 -0
@@ -40,6 +40,25 @@ export interface BrowserSettings {
40
40
  readonly closeWithSession: boolean;
41
41
  /** Show the synthetic cursor/click feedback while the agent operates. */
42
42
  readonly virtualCursor: boolean;
43
+ /**
44
+ * Quiet mode: drive the page without taking over the screen.
45
+ *
46
+ * Off (the default) keeps the contract this plugin is built on — human and agent share one
47
+ * window, so an operation raises it and the user can see what is being driven. On, the
48
+ * carrier never activates the application and never steals keyboard focus:
49
+ * - screenshots of a minimized or hidden window go through CDP `Page.captureScreenshot`
50
+ * instead of `capturePage()`, so grabbing an image stops being a reason to show a window;
51
+ * - navigation and tab switching only change which view is visible INSIDE the window; a
52
+ * minimized window stays minimized;
53
+ * - synthesized input still needs a display surface (Chromium drops `Input.*` for a view
54
+ * without one), so that one barrier may restore a minimized window — with
55
+ * `showInactive()`, which shows it without becoming the key window, and without
56
+ * `moveTop()`.
57
+ *
58
+ * Reported as issue #35: a minimized browser window was restored and focused by the next
59
+ * screenshot or click, pulling the user out of whatever they were typing in.
60
+ */
61
+ readonly quiet: boolean;
43
62
  };
44
63
  /** Visual vs non-visual operation strategy. */
45
64
  readonly vision: {
@@ -17,7 +17,7 @@ export const HEADLESS_MODES = ['auto', 'headless', 'windowed'];
17
17
  export const DEFAULT_SETTINGS = {
18
18
  history: { enabled: true, maxEntries: 5_000, maxAgeDays: 90 },
19
19
  cookies: { persist: true },
20
- ui: { autoExpandOnce: true, closeWithSession: false, virtualCursor: true },
20
+ ui: { autoExpandOnce: true, closeWithSession: false, virtualCursor: true, quiet: false },
21
21
  vision: { strategy: 'auto' },
22
22
  // Bundled by default: it needs nothing installed, and on the desktop the carrier
23
23
  // is the sidebar anyway. Choosing a system browser is an explicit opt-in because
@@ -197,6 +197,10 @@ export function resolveSettings(raw, warn) {
197
197
  autoExpandOnce: bool(ui?.autoExpandOnce, DEFAULT_SETTINGS.ui.autoExpandOnce),
198
198
  closeWithSession: bool(ui?.closeWithSession, DEFAULT_SETTINGS.ui.closeWithSession),
199
199
  virtualCursor: bool(ui?.virtualCursor, DEFAULT_SETTINGS.ui.virtualCursor),
200
+ // Presentation preference, not a capability gate: a mistyped value keeps the documented
201
+ // default (off) instead of being read as "on", because quiet mode changes how every
202
+ // operation presents itself and should never be the accidental result of a bad edit.
203
+ quiet: bool(ui?.quiet, DEFAULT_SETTINGS.ui.quiet),
200
204
  },
201
205
  vision: { strategy: vision?.strategy === 'nonVisual' ? 'nonVisual' : DEFAULT_SETTINGS.vision.strategy },
202
206
  browser: {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "dsh-builtin-browser",
3
- "version": "0.5.2",
4
- "description": "Shared real browser plugin for DeepSeek Harness: install-and-use — a visible native browser the human can take over, driven by the agent over CDP. DOM-level interaction (React/Vue safe), per-task session isolation, cookie persistence (dsh_browser_auth), CAPTCHA detection, batch form fill (dsh_browser_fill), authenticated download, history/replay, action restriction",
3
+ "version": "0.5.4",
4
+ "description": "Shared real browser plugin for DeepSeek Harness: install-and-use 鈥?a visible native browser the human can take over, driven by the agent over CDP. DOM-level interaction (React/Vue safe), per-task session isolation, cookie persistence (dsh_browser_auth), CAPTCHA detection, batch form fill (dsh_browser_fill), authenticated download, history/replay, action restriction",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",
7
7
  "types": "lib/index.d.ts",
@@ -50,7 +50,6 @@
50
50
  "scripts": {
51
51
  "build": "tsc -p tsconfig.json",
52
52
  "test": "tsc -p tsconfig.json && node --test \"tests/*.test.mjs\"",
53
- "postinstall": "node scripts/install-electron.mjs",
54
53
  "prepack": "npm run build"
55
54
  },
56
55
  "keywords": [
@@ -18,30 +18,34 @@
18
18
  */
19
19
  import { existsSync, readFileSync, readdirSync, statSync } from 'node:fs'
20
20
  import { join } from 'node:path'
21
+ import { fileURLToPath } from 'node:url'
22
+
23
+ import { readDesktopIdentity } from './desktop-identity.js'
21
24
 
22
25
  /**
23
26
  * Find the desktop install to report on.
24
27
  *
25
- * The same shape probe the installer uses, repeated here because the two run in different processes
26
- * and neither can import the other: the product directory is `DeepSeek Harness` on some builds and
27
- * `DSH Desktop` on others, and the shape that identifies an install is stable across both.
28
+ * Reported as issue #40: this used to probe for a *generic* Electron layout and return the first
29
+ * directory that had one. Any Electron app has that layout, so a stranger's app was reported as
30
+ * "DSH Desktop, bridge missing" — together with a ready-to-run installer command aimed at it. The
31
+ * probe now verifies the same identity the installer does (see `desktop-identity.js`), so the
32
+ * answer is about DSH Desktop and nothing else.
28
33
  * @param localAppData - the base directory; defaults to the environment's.
29
34
  * @returns the install directory, or undefined when nothing matches.
30
35
  */
31
36
  export function findDesktopInstall(localAppData = process.env.LOCALAPPDATA ?? ''): string | undefined {
32
37
  if (localAppData === '') return undefined
33
38
  const programs = join(localAppData, 'Programs')
34
- const looksInstalled = (candidate: string): boolean => existsSync(join(candidate, 'resources', 'app', 'lib', 'main.js'))
35
- || existsSync(join(candidate, 'resources', 'app.asar'))
39
+ const isDesktop = (candidate: string): boolean => readDesktopIdentity(candidate).ok
36
40
  for (const named of ['DSH Desktop', 'DeepSeek Harness']) {
37
41
  const candidate = join(programs, named)
38
- if (looksInstalled(candidate)) return candidate
42
+ if (isDesktop(candidate)) return candidate
39
43
  }
40
44
  try {
41
45
  for (const entry of readdirSync(programs, { withFileTypes: true })) {
42
46
  if (!entry.isDirectory()) continue
43
47
  const candidate = join(programs, entry.name)
44
- if (looksInstalled(candidate)) return candidate
48
+ if (isDesktop(candidate)) return candidate
45
49
  }
46
50
  } catch {
47
51
  // No Programs directory: nothing to report on, which is a state rather than an error.
@@ -75,6 +79,28 @@ export interface BridgeStatus {
75
79
  readonly command?: string
76
80
  }
77
81
 
82
+ /**
83
+ * The installer script inside THIS plugin package.
84
+ *
85
+ * Reported as issue #40, fourth item: the panel told users to run
86
+ * `node desktop-bridge/install.mjs "<root>" --restart`, which assumes the reader is standing in a
87
+ * source checkout. Anyone who installed the plugin from npm or the market has no such relative path —
88
+ * the file lives inside the installed package, and there is nowhere sensible for them to run the
89
+ * command from. Resolving it from this module's own location is the difference between a hint and an
90
+ * instruction that works.
91
+ * @returns the absolute path, or undefined when the package layout is unexpected.
92
+ */
93
+ function installerScriptPath(): string | undefined {
94
+ try {
95
+ // lib/browser-electron/bridge-status.js → <package>/desktop-bridge/install.mjs
96
+ const url = new URL('../../desktop-bridge/install.mjs', import.meta.url)
97
+ const path = fileURLToPath(url)
98
+ return safeExists(path) ? path : undefined
99
+ } catch {
100
+ return undefined
101
+ }
102
+ }
103
+
78
104
  /**
79
105
  * The command a user should run for this state, quoted so it survives a path with spaces.
80
106
  * @param installRoot - the install directory, or undefined when none was found.
@@ -82,7 +108,11 @@ export interface BridgeStatus {
82
108
  */
83
109
  function repairCommand(installRoot: string | undefined): string | undefined {
84
110
  if (installRoot === undefined) return undefined
85
- return `node desktop-bridge/install.mjs "${installRoot}" --restart`
111
+ const script = installerScriptPath()
112
+ // No absolute path means the package is laid out in a way this module cannot navigate; the bare
113
+ // relative form is still better than nothing, but it is the fallback, not the default.
114
+ const runner = script === undefined ? 'desktop-bridge/install.mjs' : `"${script}"`
115
+ return `node ${runner} "${installRoot}" --restart`
86
116
  }
87
117
 
88
118
  /**
@@ -100,16 +130,32 @@ export function readBridgeStatus(installRoot: string): BridgeStatus {
100
130
  const archivePath = join(resources, 'app.asar')
101
131
  const backupPath = `${mainPath}.before-bridge`
102
132
 
133
+ // Identity first, and before any other reading. Two reasons, both measured in issue #40: a
134
+ // stranger's app must not be reported as DSH Desktop, and this status also carries a *command* —
135
+ // pointing that command at a directory whose identity was never verified is how the installer got
136
+ // aimed at an application that merely looked like Electron.
137
+ const identity = readDesktopIdentity(installRoot)
138
+ if (!identity.ok) {
139
+ return {
140
+ state: 'missing',
141
+ present: false,
142
+ installRoot,
143
+ archivePresent: safeExists(archivePath),
144
+ detail: `not a DSH Desktop install: ${identity.reason}`,
145
+ }
146
+ }
147
+
103
148
  const archivePresent = safeExists(archivePath)
104
149
  const mainPresent = safeExists(mainPath)
105
150
  if (!mainPresent && !archivePresent) {
151
+ // Unreachable once identity has passed (identity requires the loaded main entry), kept as the
152
+ // fail-closed branch for a tree that changed between the two reads.
106
153
  return {
107
154
  state: 'missing',
108
155
  present: false,
109
156
  installRoot,
110
157
  archivePresent,
111
- command: repairCommand(installRoot),
112
- detail: 'not a DSH Desktop install: no resources/app/lib/main.js and no resources/app.asar',
158
+ detail: 'not a DSH Desktop install: no resources/app/lib/main.js and no resources/app.asar',
113
159
  }
114
160
  }
115
161
  let backupAt
@@ -4,7 +4,7 @@
4
4
  *
5
5
  * WHY
6
6
  * The plugin runs inside the desktop's Node-mode host, where there is no Electron
7
- * API — which is why it self-hosts a whole browser today. The shell, however, can
7
+ * API 鈥?which is why it self-hosts a whole browser today. The shell, however, can
8
8
  * own views, and its sidebar already displays real web pages. This host borrows
9
9
  * that page over the shell's bridge (see apps/desktop/bridge/plugin-browser-bridge.js),
10
10
  * so the agent and the human end up on ONE page instead of two.
@@ -13,7 +13,7 @@
13
13
  * It implements the same `ElectronBrowserViewHost` seam the self-hosted host does,
14
14
  * so the provider, the tools, browsing history, the synthetic cursor and the
15
15
  * teardown rules are all unchanged: only the carrier differs. Everything is
16
- * lazily materialized — a fresh shell has no sidebar guest until something asks
16
+ * lazily materialized 鈥?a fresh shell has no sidebar guest until something asks
17
17
  * for a page, and the bridge creates one on demand.
18
18
  *
19
19
  * FALLBACK
@@ -28,7 +28,7 @@ import { readFileSync } from 'node:fs'
28
28
  import { homedir } from 'node:os'
29
29
  import { join } from 'node:path'
30
30
  import { randomUUID } from 'node:crypto'
31
- import type { ElectronBrowserViewHost, ElectronViewHandle } from './provider.js'
31
+ import type { CommandOptions, ElectronBrowserViewHost, ElectronViewHandle } from './provider.js'
32
32
  import type { BrowserUserAction } from './provider.js'
33
33
  import type { CarrierPage, CarrierLossReason } from '../browser/types.js'
34
34
 
@@ -46,7 +46,7 @@ export function bridgeEndpointPath(): string {
46
46
  * A display state, not a dead carrier. It must never reach `reportLost`, and it must never be
47
47
  * retried as though a guest had vanished: retrying cannot attach a webview the shell has not
48
48
  * rendered, and treating it as loss swapped the surface to a fresh self-hosted window whose page was
49
- * blank — losing the page the user had open.
49
+ * blank 鈥?losing the page the user had open.
50
50
  */
51
51
  const PANEL_NOT_DISPLAYED = 'panel-not-displayed'
52
52
 
@@ -67,8 +67,8 @@ const PANEL_DISPLAY_RETRY_MS = 400
67
67
  *
68
68
  * Thrown instead of a plain Error so callers can distinguish "the panel did not render" from "the
69
69
  * browser broke", which is exactly the distinction issue #37 says was missing. The plugin no longer
70
- * waits for a human to show the conversation — the client half asks the shell to (`openSession`) and
71
- * this host retries while it renders — so reaching here means the shell did not render it at all.
70
+ * waits for a human to show the conversation 鈥?the client half asks the shell to (`openSession`) and
71
+ * this host retries while it renders 鈥?so reaching here means the shell did not render it at all.
72
72
  */
73
73
  export class BrowserPanelNotDisplayedError extends Error {
74
74
  readonly code = PANEL_NOT_DISPLAYED
@@ -78,7 +78,7 @@ export class BrowserPanelNotDisplayedError extends Error {
78
78
  */
79
79
  constructor(detail: string) {
80
80
  super(
81
- 'dsh-builtin-browser: this conversation\'s sidebar page did not attach — the shell was asked to '
81
+ 'dsh-builtin-browser: this conversation\'s sidebar page did not attach 鈥?the shell was asked to '
82
82
  + 'show this conversation and its panel still did not render. Nothing was lost, and the page '
83
83
  + 'appears as soon as the panel mounts. '
84
84
  + `(${detail})`,
@@ -99,7 +99,7 @@ function isGuestGone(error: unknown): boolean {
99
99
  if (error instanceof BrowserPanelNotDisplayedError) return false
100
100
  const message = error instanceof Error ? error.message : String(error)
101
101
  // The panel path reports its own failure when the renderer never produced a page, and that is the
102
- // same class as a vanished guest — worth one retry on a fresh panel — so it is listed here too.
102
+ // same class as a vanished guest 鈥?worth one retry on a fresh panel 鈥?so it is listed here too.
103
103
  return /guest \d+ is not available|sidebar unavailable|produced no page/i.test(message)
104
104
  }
105
105
 
@@ -108,7 +108,7 @@ function isGuestGone(error: unknown): boolean {
108
108
  *
109
109
  * One sidebar exists per shell window, so every view this host hands out refers
110
110
  * to the same guest: the provider keeps its tab bookkeeping, and the tabs simply
111
- * share the visible page. That is the intended behaviour for this carrier — the
111
+ * share the visible page. That is the intended behaviour for this carrier 鈥?the
112
112
  * point is a single page both parties can see.
113
113
  */
114
114
  export class DesktopBridgeViewHost implements ElectronBrowserViewHost {
@@ -117,7 +117,7 @@ export class DesktopBridgeViewHost implements ElectronBrowserViewHost {
117
117
  * One entry per view this host handed out: the conversation it serves, and the guest backing it.
118
118
  *
119
119
  * The conversation belongs to the ENTRY, not to this host. A single plugin process serves every
120
- * conversation — DSH runs them all through one host instance — so an owner held on the host
120
+ * conversation 鈥?DSH runs them all through one host instance 鈥?so an owner held on the host
121
121
  * would be one conversation's id applied to all of them, and a page could still land in
122
122
  * another's sidebar however carefully the bridge checked it.
123
123
  */
@@ -127,7 +127,7 @@ export class DesktopBridgeViewHost implements ElectronBrowserViewHost {
127
127
  *
128
128
  * Every DSH session is its own plugin process, and the sidebar is one surface they all share.
129
129
  * A session can only see its own `views` map, so before the bridge kept a ledger it adopted
130
- * whichever tab it found — including one another session had opened, which is how a URL from
130
+ * whichever tab it found 鈥?including one another session had opened, which is how a URL from
131
131
  * one session appeared in another's sidebar. The id is per process, so two sessions never
132
132
  * collide and one session's tabs are never handed to another; it is deliberately not the DSH
133
133
  * session id, which this layer has no access to and does not need.
@@ -135,17 +135,17 @@ export class DesktopBridgeViewHost implements ElectronBrowserViewHost {
135
135
  * The whole ledger rests on that premise, so it was measured rather than assumed: with two
136
136
  * sessions running, the desktop had TWO host processes (plus one main, two renderers and a
137
137
  * gpu/utility pair). If sessions ever shared a process, every owner check in the bridge would
138
- * compare equal and the bleed would return in a new shape — so a change to how DSH spawns
138
+ * compare equal and the bleed would return in a new shape 鈥?so a change to how DSH spawns
139
139
  * sessions invalidates this file, not just this comment.
140
140
  *
141
141
  * It is now the CONVERSATION id rather than an invented uuid, read from the plugin's ctx by the
142
142
  * same path DSH's own desktop host uses. That matters because the shell keeps one sidebar per
143
143
  * conversation and writes its session id onto each sidebar container (on the React fiber,
144
- * measured on the running app) — so the bridge can only answer "is this sidebar mine" if it has
144
+ * measured on the running app) 鈥?so the bridge can only answer "is this sidebar mine" if it has
145
145
  * a comparable id. With an invented uuid it never could, which is how a command came to be typed
146
146
  * into another conversation's address bar.
147
147
  *
148
- * Falls back to a random uuid when the host cannot supply one — an older DSH, or a non-desktop
148
+ * Falls back to a random uuid when the host cannot supply one 鈥?an older DSH, or a non-desktop
149
149
  * composition. That is no worse than before: the bridge then knows only "this process", and it
150
150
  * refuses to operate on a sidebar it cannot prove is its own.
151
151
  */
@@ -155,8 +155,8 @@ export class DesktopBridgeViewHost implements ElectronBrowserViewHost {
155
155
  * conversation each belongs to.
156
156
  *
157
157
  * The provider destroys its view handles before it asks for a release, so the mapping is dropped
158
- * by then — keeping the guest ids here is what lets us close only our own tabs when several
159
- * conversations are running (requirements §4: each session gets its own page). The owner rides
158
+ * by then 鈥?keeping the guest ids here is what lets us close only our own tabs when several
159
+ * conversations are running (requirements 搂4: each session gets its own page). The owner rides
160
160
  * along for the same reason it does in {@link views}: a release names one conversation.
161
161
  */
162
162
  private readonly orphaned = new Map<number, string>()
@@ -168,7 +168,7 @@ export class DesktopBridgeViewHost implements ElectronBrowserViewHost {
168
168
  * handler is stored anyway so the interface contract holds (a caller may register
169
169
  * one and must not be surprised) and so a future bridge operation can deliver it
170
170
  * without changing the provider. Consequently no user-action event is emitted while
171
- * the desktop sidebar is the carrier — by design, not by omission.
171
+ * the desktop sidebar is the carrier 鈥?by design, not by omission.
172
172
  */
173
173
  private userActionHandler: ((action: BrowserUserAction) => void) | undefined
174
174
  /** Told when the shell behind this bridge exits; see `onLost`. */
@@ -232,7 +232,7 @@ export class DesktopBridgeViewHost implements ElectronBrowserViewHost {
232
232
  //
233
233
  // The sidebar browser renders as a <webview> only where the shell exposes
234
234
  // `dshDesktop.browser` and enables `webviewTag`. Where it renders as an <iframe> there is
235
- // no webContents, no `getWebContentsId()` and no CDP endpoint — and every tool here is
235
+ // no webContents, no `getWebContentsId()` and no CDP endpoint 鈥?and every tool here is
236
236
  // built on CDP, so adopting it means failing every call, with no path back to a carrier
237
237
  // that works. Reported on DSH Desktop 2.0.17 with both conditions measured.
238
238
  //
@@ -279,8 +279,8 @@ export class DesktopBridgeViewHost implements ElectronBrowserViewHost {
279
279
  * Every page this conversation's sidebar panel currently holds.
280
280
  *
281
281
  * The shell owns page creation inside the panel: a link the user or the agent clicks may open a
282
- * new page, and nothing tells the provider. Those pages are real — they are what the human sees
283
- * — but without this the tab model reports only the ones the provider created, so the rest are
282
+ * new page, and nothing tells the provider. Those pages are real 鈥?they are what the human sees
283
+ * 鈥?but without this the tab model reports only the ones the provider created, so the rest are
284
284
  * invisible to `dsh_browser_list_tabs` and unclosable through `dsh_browser_close_tab`.
285
285
  *
286
286
  * The bridge's `list` already answers with the sidebar guests, which is the same information, so
@@ -291,14 +291,14 @@ export class DesktopBridgeViewHost implements ElectronBrowserViewHost {
291
291
  * Every sidebar page this conversation holds that THIS HOST did not open.
292
292
  *
293
293
  * The shell owns page creation inside the panel: a link the user or the agent clicks may open a
294
- * new page, and nothing tells the provider. Those pages are real — they are what the human sees
295
- * — but without this the tab model reports only the ones the provider created, so the rest are
294
+ * new page, and nothing tells the provider. Those pages are real 鈥?they are what the human sees
295
+ * 鈥?but without this the tab model reports only the ones the provider created, so the rest are
296
296
  * invisible to `dsh_browser_list_tabs` and unclosable through `dsh_browser_close_tab`.
297
297
  *
298
298
  * Pages this host opened are EXCLUDED here rather than deduplicated by the caller, because the
299
299
  * caller cannot do it: the provider knows a tab by its own id and the carrier knows a page by the
300
300
  * guest id, and the two never match. The first version returned everything and relied on the
301
- * caller to filter, which reported the same page twice — the mapping that makes the match
301
+ * caller to filter, which reported the same page twice 鈥?the mapping that makes the match
302
302
  * possible exists only here.
303
303
  * @returns the panel's pages that are not ours, or an empty list when the bridge cannot answer.
304
304
  */
@@ -322,7 +322,7 @@ export class DesktopBridgeViewHost implements ElectronBrowserViewHost {
322
322
  /**
323
323
  * Close one sidebar page by the id {@link listPages} reported.
324
324
  *
325
- * The bridge closes sidebar pages by TITLE, and only the ones the caller owns — that is what
325
+ * The bridge closes sidebar pages by TITLE, and only the ones the caller owns 鈥?that is what
326
326
  * stopped one conversation's teardown from destroying another's page. So the title is looked up
327
327
  * through the same listing, and a page that no longer appears is treated as already gone rather
328
328
  * than as an error: the caller asked for it to be closed, and it is.
@@ -340,8 +340,8 @@ export class DesktopBridgeViewHost implements ElectronBrowserViewHost {
340
340
  /**
341
341
  * The guest backing a view, materialized on first use.
342
342
  *
343
- * The conversation that called owns exactly one sidebar page on this carrier — a human and the
344
- * agent look at the same page — so every view resolves to that conversation's own guest, and the
343
+ * The conversation that called owns exactly one sidebar page on this carrier 鈥?a human and the
344
+ * agent look at the same page 鈥?so every view resolves to that conversation's own guest, and the
345
345
  * page does not multiply behind the provider's own tab bookkeeping.
346
346
  *
347
347
  * A cached guest is used as-is: probing it first cost a round-trip on every command, so liveness
@@ -350,14 +350,14 @@ export class DesktopBridgeViewHost implements ElectronBrowserViewHost {
350
350
  * @param url - address to use when this conversation has no page yet.
351
351
  */
352
352
 
353
- private async guestFor(viewId: string, url?: string): Promise<number> {
353
+ private async guestFor(viewId: string, url?: string, options?: CommandOptions): Promise<number> {
354
354
  const entry = this.views.get(viewId)
355
355
  // No liveness check here: verifying the cached guest cost a round-trip on every
356
356
  // command. A guest that has gone away is detected by the command itself failing,
357
357
  // and `createView`'s sendCommand then discards the cache and calls back in here.
358
358
  if (entry?.guest !== undefined) return entry.guest
359
359
 
360
- // The conversation THIS VIEW serves — read from the view, never from the process.
360
+ // The conversation THIS VIEW serves 鈥?read from the view, never from the process.
361
361
  //
362
362
  // The difference is the whole bug. This host is one instance shared by every conversation, so
363
363
  // `this.owner` alone can only be "whichever conversation this process happens to name", and
@@ -369,6 +369,35 @@ export class DesktopBridgeViewHost implements ElectronBrowserViewHost {
369
369
  // conversation's own panel by id through the renderer, rather than by pressing the shell's
370
370
  // shortcut or clicking its card, both of which the shell delivers to whatever it displays.
371
371
  const owner = entry?.owner ?? this.owner
372
+ // A command that must not present anything (issue #46: the live preview) takes a read-only path.
373
+ // It asks the bridge what this conversation ALREADY has and never enters the retry loop below 鈥? // that loop is what asks the shell to display the conversation, which is right for a tool call
374
+ // and wrong for "let me look at it". A page that is not on screen therefore reports as a display
375
+ // state, and the preview route turns that into the panel's paused state.
376
+ if (options?.present === false) {
377
+ // `list` and NOT `ensureSidebar`. This is deliberate and it is the whole fix: every version of
378
+ // `ensureSidebar` can drive the shell — the current one asks the renderer to display the
379
+ // conversation, and the pre-#37 one literally pressed the sidebar shortcut and typed into the
380
+ // address bar. `list` is a pure read on every version, so a preview cannot move anything even
381
+ // when the installed bridge is old (which is exactly the reporter's environment: the symptom
382
+ // appears only once a bridge is installed).
383
+ const listed = await this.connection.call({ op: 'list', owner }, 8_000)
384
+ const pages = Array.isArray(listed.sidebar)
385
+ ? listed.sidebar.filter(page => page !== null && typeof page === 'object' && page.type === 'webview')
386
+ : []
387
+ const wanted = url !== undefined && url !== '' ? url : undefined
388
+ const match = wanted === undefined
389
+ ? pages[0]
390
+ : pages.find(page => typeof page.url === 'string' && (page.url === wanted || page.url.startsWith(wanted))) ?? pages[0]
391
+ const id = match !== undefined ? Number(match.id) : Number.NaN
392
+ if (!Number.isFinite(id) || id <= 0) {
393
+ throw new BrowserPanelNotDisplayedError(
394
+ 'the preview asked for this conversation\'s page without presenting it, and no page is attached '
395
+ + `(the shell holds ${pages.length})`,
396
+ )
397
+ }
398
+ this.views.set(viewId, { guest: id, owner })
399
+ return id
400
+ }
372
401
  const ask = (): Promise<Record<string, unknown>> => this.connection.call({
373
402
  op: 'ensureSidebar',
374
403
  owner,
@@ -381,14 +410,14 @@ export class DesktopBridgeViewHost implements ElectronBrowserViewHost {
381
410
  // panel-not-displayed - the conversation exists but is not the one on screen;
382
411
  // "is not mounted" - its surface store was not minted when the tab was placed.
383
412
  // Both are gone one render later. Measured, this is the whole difference between "the sidebar
384
- // works once I click over to that conversation" and "it works" — the page is real either way,
413
+ // works once I click over to that conversation" and "it works" 鈥?the page is real either way,
385
414
  // and failing the call while the shell is already switching away from it just reported a
386
415
  // problem the plugin had already fixed.
387
416
  const displayRefusal = (value: Record<string, unknown>): boolean =>
388
417
  value.ok !== true
389
418
  && (String(value.code ?? '') === PANEL_NOT_DISPLAYED
390
419
  || String(value.error ?? '').includes('not mounted')
391
- // Bridges installed before issue #37 send the same refusal with NO code — the installed copy
420
+ // Bridges installed before issue #37 send the same refusal with NO code 鈥?the installed copy
392
421
  // on this machine is one of them (`panel-not-displayed` appears nowhere in it). Reading only
393
422
  // the code therefore turned a retryable display state into a hard failure for exactly the
394
423
  // users who had the old bridge, which is the majority until it is reinstalled. The wording
@@ -399,7 +428,7 @@ export class DesktopBridgeViewHost implements ElectronBrowserViewHost {
399
428
  answer = await ask()
400
429
  }
401
430
  if (answer.ok !== true) {
402
- // Reachable only after the retries above have run out, and the code still decides — not the
431
+ // Reachable only after the retries above have run out, and the code still decides 鈥?not the
403
432
  // wording:
404
433
  // panel-not-displayed -> the shell was asked to show the conversation (client half's
405
434
  // `openSession`) and did not render it in ~1.6 s: no loss, no swap.
@@ -436,13 +465,13 @@ export class DesktopBridgeViewHost implements ElectronBrowserViewHost {
436
465
  }
437
466
  return {
438
467
  id: viewId,
439
- sendCommand: async (method: string, params?: Record<string, unknown>) => {
468
+ sendCommand: async (method: string, params?: Record<string, unknown>, options?: CommandOptions) => {
440
469
  // No liveness probe up front. Checking first cost a whole round-trip on
441
470
  // every command; instead the command runs and its failure is what triggers
442
471
  // recovery. Only a genuinely missing guest is retried, so a real error (a
443
472
  // bad selector, a timeout) still surfaces unchanged.
444
473
  const url = navigateUrl(method, params)
445
- let guest = await this.guestFor(viewId, url)
474
+ let guest = await this.guestFor(viewId, url, options)
446
475
  try {
447
476
  return await run(guest, method, params)
448
477
  } catch (error) {
@@ -454,7 +483,7 @@ export class DesktopBridgeViewHost implements ElectronBrowserViewHost {
454
483
  // and removing the entry would lose it back to the process-wide fallback owner.
455
484
  const lost = this.views.get(viewId)
456
485
  if (lost !== undefined) delete lost.guest
457
- guest = await this.guestFor(viewId, url)
486
+ guest = await this.guestFor(viewId, url, options)
458
487
  return await run(guest, method, params)
459
488
  }
460
489
  },
@@ -478,8 +507,8 @@ export class DesktopBridgeViewHost implements ElectronBrowserViewHost {
478
507
  * This used to be empty, on the reasoning that the sidebar is already on screen so there is
479
508
  * nothing to show. That confused "the sidebar is visible" with "this page is the one being
480
509
  * looked at": opening a page left it behind whatever the human had in front, which is issue
481
- * #23. The other two carriers both do this — the self-hosted window re-adds the view and the
482
- * system browser calls `Page.bringToFront` — so this carrier was the odd one out.
510
+ * #23. The other two carriers both do this 鈥?the self-hosted window re-adds the view and the
511
+ * system browser calls `Page.bringToFront` 鈥?so this carrier was the odd one out.
483
512
  *
484
513
  * Best-effort by design: the tab strip belongs to the shell's renderer, and failing to raise
485
514
  * it must not fail the navigation that has already happened.
@@ -487,7 +516,7 @@ export class DesktopBridgeViewHost implements ElectronBrowserViewHost {
487
516
  */
488
517
  showView(handle: ElectronViewHandle): void {
489
518
  // `owner` is required: the bridge refuses to bring forward a guest held by another session.
490
- // It comes from the view, not the process — with several conversations live, a process-wide
519
+ // It comes from the view, not the process 鈥?with several conversations live, a process-wide
491
520
  // owner would raise the wrong conversation's tab.
492
521
  const owner = this.views.get(handle.id)?.owner ?? this.owner
493
522
  const guest = this.views.get(handle.id)?.guest
@@ -506,11 +535,11 @@ export class DesktopBridgeViewHost implements ElectronBrowserViewHost {
506
535
 
507
536
 
508
537
  /**
509
- * Release the sidebar's browser pages (requirements §3, `ui.closeWithSession`).
538
+ * Release the sidebar's browser pages (requirements 搂3, `ui.closeWithSession`).
510
539
  *
511
540
  * Destroying our own view handles is not enough on this carrier: the sidebar
512
541
  * belongs to the shell and would happily keep the page (and its renderer) alive.
513
- * Closing the tabs is what actually ends the page — and only the page: cookies
542
+ * Closing the tabs is what actually ends the page 鈥?and only the page: cookies
514
543
  * live in the partition, history on disk, so both survive.
515
544
  * @param owner - the conversation whose pages to close. Omit to close every conversation's,
516
545
  * which is only correct from a teardown that is itself global.
@@ -518,7 +547,7 @@ export class DesktopBridgeViewHost implements ElectronBrowserViewHost {
518
547
  */
519
548
  async releasePage(owner?: string): Promise<void> {
520
549
  // Only this conversation's tabs. The host is ONE instance shared by every conversation, so a
521
- // release that named none would close pages other conversations are still driving — "every
550
+ // release that named none would close pages other conversations are still driving 鈥?"every
522
551
  // webview currently visible" is precisely what must never be used here.
523
552
  const mine: number[] = []
524
553
  for (const [viewId, entry] of [...this.views]) {
@@ -540,7 +569,7 @@ export class DesktopBridgeViewHost implements ElectronBrowserViewHost {
540
569
  // `owner` is REQUIRED here now that the bridge filters `list` by it. Without it the bridge
541
570
  // falls back to a different owner, so the guests this session holds are filtered out of the
542
571
  // answer, `mine.includes(...)` matches nothing, the title list comes back empty and this
543
- // returns without closing anything — the page stayed open and the human had to close it by
572
+ // returns without closing anything 鈥?the page stayed open and the human had to close it by
544
573
  // hand. Adding the ledger to the bridge without updating this call site is exactly the kind
545
574
  // of half-change that made the ownership work take four rounds.
546
575
  const answer = await this.connection.call({ op: 'list', owner: owner ?? this.owner }, 5_000)