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.
- package/CHANGELOG.md +145 -0
- package/README.en.md +77 -68
- package/README.md +69 -89
- package/client.js +147 -15
- package/desktop-bridge/desktop-identity.json +9 -0
- package/desktop-bridge/install.mjs +220 -1
- package/desktop-bridge/plugin-browser-bridge.js +5 -0
- package/docs/README.md +1 -1
- package/docs/architecture.md +1 -1
- package/docs/limitations.md +19 -1
- package/docs/tool-reference.md +2 -1
- package/lib/browser-electron/bridge-status.d.ts +5 -3
- package/lib/browser-electron/bridge-status.js +53 -9
- package/lib/browser-electron/desktop-bridge-host.d.ts +25 -25
- package/lib/browser-electron/desktop-bridge-host.js +67 -40
- package/lib/browser-electron/desktop-identity.d.ts +58 -0
- package/lib/browser-electron/desktop-identity.js +236 -0
- package/lib/browser-electron/electron-provision.d.ts +59 -0
- package/lib/browser-electron/electron-provision.js +176 -0
- package/lib/browser-electron/entry.js +12 -0
- package/lib/browser-electron/host-main.js +68 -14
- package/lib/browser-electron/provider.d.ts +53 -3
- package/lib/browser-electron/provider.js +26 -5
- package/lib/browser-electron/remote-host.d.ts +3 -3
- package/lib/browser-electron/remote-host.js +25 -11
- package/lib/browser-electron/settings-store.d.ts +19 -0
- package/lib/browser-electron/settings-store.js +5 -1
- package/package.json +2 -3
- package/src/browser-electron/bridge-status.ts +56 -10
- package/src/browser-electron/desktop-bridge-host.ts +70 -41
- package/src/browser-electron/desktop-identity.ts +260 -0
- package/src/browser-electron/electron-provision.ts +208 -0
- package/src/browser-electron/entry.ts +12 -0
- package/src/browser-electron/host-main.ts +62 -12
- package/src/browser-electron/provider.ts +73 -8
- package/src/browser-electron/remote-host.ts +26 -12
- package/src/browser-electron/settings-store.ts +24 -1
- package/src/types/electron-shim.d.ts +8 -0
|
@@ -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
|
|
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
|
|
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
|
|
@@ -39,7 +39,7 @@ export function bridgeEndpointPath() {
|
|
|
39
39
|
* A display state, not a dead carrier. It must never reach `reportLost`, and it must never be
|
|
40
40
|
* retried as though a guest had vanished: retrying cannot attach a webview the shell has not
|
|
41
41
|
* rendered, and treating it as loss swapped the surface to a fresh self-hosted window whose page was
|
|
42
|
-
* blank
|
|
42
|
+
* blank 鈥?losing the page the user had open.
|
|
43
43
|
*/
|
|
44
44
|
const PANEL_NOT_DISPLAYED = 'panel-not-displayed';
|
|
45
45
|
/**
|
|
@@ -58,8 +58,8 @@ const PANEL_DISPLAY_RETRY_MS = 400;
|
|
|
58
58
|
*
|
|
59
59
|
* Thrown instead of a plain Error so callers can distinguish "the panel did not render" from "the
|
|
60
60
|
* browser broke", which is exactly the distinction issue #37 says was missing. The plugin no longer
|
|
61
|
-
* waits for a human to show the conversation
|
|
62
|
-
* this host retries while it renders
|
|
61
|
+
* waits for a human to show the conversation 鈥?the client half asks the shell to (`openSession`) and
|
|
62
|
+
* this host retries while it renders 鈥?so reaching here means the shell did not render it at all.
|
|
63
63
|
*/
|
|
64
64
|
export class BrowserPanelNotDisplayedError extends Error {
|
|
65
65
|
code = PANEL_NOT_DISPLAYED;
|
|
@@ -67,7 +67,7 @@ export class BrowserPanelNotDisplayedError extends Error {
|
|
|
67
67
|
* @param detail - the bridge's own explanation, appended so the message says why.
|
|
68
68
|
*/
|
|
69
69
|
constructor(detail) {
|
|
70
|
-
super('dsh-builtin-browser: this conversation\'s sidebar page did not attach
|
|
70
|
+
super('dsh-builtin-browser: this conversation\'s sidebar page did not attach 鈥?the shell was asked to '
|
|
71
71
|
+ 'show this conversation and its panel still did not render. Nothing was lost, and the page '
|
|
72
72
|
+ 'appears as soon as the panel mounts. '
|
|
73
73
|
+ `(${detail})`);
|
|
@@ -87,7 +87,7 @@ function isGuestGone(error) {
|
|
|
87
87
|
return false;
|
|
88
88
|
const message = error instanceof Error ? error.message : String(error);
|
|
89
89
|
// The panel path reports its own failure when the renderer never produced a page, and that is the
|
|
90
|
-
// same class as a vanished guest
|
|
90
|
+
// same class as a vanished guest 鈥?worth one retry on a fresh panel 鈥?so it is listed here too.
|
|
91
91
|
return /guest \d+ is not available|sidebar unavailable|produced no page/i.test(message);
|
|
92
92
|
}
|
|
93
93
|
/**
|
|
@@ -95,7 +95,7 @@ function isGuestGone(error) {
|
|
|
95
95
|
*
|
|
96
96
|
* One sidebar exists per shell window, so every view this host hands out refers
|
|
97
97
|
* to the same guest: the provider keeps its tab bookkeeping, and the tabs simply
|
|
98
|
-
* share the visible page. That is the intended behaviour for this carrier
|
|
98
|
+
* share the visible page. That is the intended behaviour for this carrier 鈥?the
|
|
99
99
|
* point is a single page both parties can see.
|
|
100
100
|
*/
|
|
101
101
|
export class DesktopBridgeViewHost {
|
|
@@ -104,7 +104,7 @@ export class DesktopBridgeViewHost {
|
|
|
104
104
|
* One entry per view this host handed out: the conversation it serves, and the guest backing it.
|
|
105
105
|
*
|
|
106
106
|
* The conversation belongs to the ENTRY, not to this host. A single plugin process serves every
|
|
107
|
-
* conversation
|
|
107
|
+
* conversation 鈥?DSH runs them all through one host instance 鈥?so an owner held on the host
|
|
108
108
|
* would be one conversation's id applied to all of them, and a page could still land in
|
|
109
109
|
* another's sidebar however carefully the bridge checked it.
|
|
110
110
|
*/
|
|
@@ -114,7 +114,7 @@ export class DesktopBridgeViewHost {
|
|
|
114
114
|
*
|
|
115
115
|
* Every DSH session is its own plugin process, and the sidebar is one surface they all share.
|
|
116
116
|
* A session can only see its own `views` map, so before the bridge kept a ledger it adopted
|
|
117
|
-
* whichever tab it found
|
|
117
|
+
* whichever tab it found 鈥?including one another session had opened, which is how a URL from
|
|
118
118
|
* one session appeared in another's sidebar. The id is per process, so two sessions never
|
|
119
119
|
* collide and one session's tabs are never handed to another; it is deliberately not the DSH
|
|
120
120
|
* session id, which this layer has no access to and does not need.
|
|
@@ -122,17 +122,17 @@ export class DesktopBridgeViewHost {
|
|
|
122
122
|
* The whole ledger rests on that premise, so it was measured rather than assumed: with two
|
|
123
123
|
* sessions running, the desktop had TWO host processes (plus one main, two renderers and a
|
|
124
124
|
* gpu/utility pair). If sessions ever shared a process, every owner check in the bridge would
|
|
125
|
-
* compare equal and the bleed would return in a new shape
|
|
125
|
+
* compare equal and the bleed would return in a new shape 鈥?so a change to how DSH spawns
|
|
126
126
|
* sessions invalidates this file, not just this comment.
|
|
127
127
|
*
|
|
128
128
|
* It is now the CONVERSATION id rather than an invented uuid, read from the plugin's ctx by the
|
|
129
129
|
* same path DSH's own desktop host uses. That matters because the shell keeps one sidebar per
|
|
130
130
|
* conversation and writes its session id onto each sidebar container (on the React fiber,
|
|
131
|
-
* measured on the running app)
|
|
131
|
+
* measured on the running app) 鈥?so the bridge can only answer "is this sidebar mine" if it has
|
|
132
132
|
* a comparable id. With an invented uuid it never could, which is how a command came to be typed
|
|
133
133
|
* into another conversation's address bar.
|
|
134
134
|
*
|
|
135
|
-
* Falls back to a random uuid when the host cannot supply one
|
|
135
|
+
* Falls back to a random uuid when the host cannot supply one 鈥?an older DSH, or a non-desktop
|
|
136
136
|
* composition. That is no worse than before: the bridge then knows only "this process", and it
|
|
137
137
|
* refuses to operate on a sidebar it cannot prove is its own.
|
|
138
138
|
*/
|
|
@@ -142,8 +142,8 @@ export class DesktopBridgeViewHost {
|
|
|
142
142
|
* conversation each belongs to.
|
|
143
143
|
*
|
|
144
144
|
* The provider destroys its view handles before it asks for a release, so the mapping is dropped
|
|
145
|
-
* by then
|
|
146
|
-
* conversations are running (requirements
|
|
145
|
+
* by then 鈥?keeping the guest ids here is what lets us close only our own tabs when several
|
|
146
|
+
* conversations are running (requirements 搂4: each session gets its own page). The owner rides
|
|
147
147
|
* along for the same reason it does in {@link views}: a release names one conversation.
|
|
148
148
|
*/
|
|
149
149
|
orphaned = new Map();
|
|
@@ -155,7 +155,7 @@ export class DesktopBridgeViewHost {
|
|
|
155
155
|
* handler is stored anyway so the interface contract holds (a caller may register
|
|
156
156
|
* one and must not be surprised) and so a future bridge operation can deliver it
|
|
157
157
|
* without changing the provider. Consequently no user-action event is emitted while
|
|
158
|
-
* the desktop sidebar is the carrier
|
|
158
|
+
* the desktop sidebar is the carrier 鈥?by design, not by omission.
|
|
159
159
|
*/
|
|
160
160
|
userActionHandler;
|
|
161
161
|
/** Told when the shell behind this bridge exits; see `onLost`. */
|
|
@@ -219,7 +219,7 @@ export class DesktopBridgeViewHost {
|
|
|
219
219
|
//
|
|
220
220
|
// The sidebar browser renders as a <webview> only where the shell exposes
|
|
221
221
|
// `dshDesktop.browser` and enables `webviewTag`. Where it renders as an <iframe> there is
|
|
222
|
-
// no webContents, no `getWebContentsId()` and no CDP endpoint
|
|
222
|
+
// no webContents, no `getWebContentsId()` and no CDP endpoint 鈥?and every tool here is
|
|
223
223
|
// built on CDP, so adopting it means failing every call, with no path back to a carrier
|
|
224
224
|
// that works. Reported on DSH Desktop 2.0.17 with both conditions measured.
|
|
225
225
|
//
|
|
@@ -264,8 +264,8 @@ export class DesktopBridgeViewHost {
|
|
|
264
264
|
* Every page this conversation's sidebar panel currently holds.
|
|
265
265
|
*
|
|
266
266
|
* The shell owns page creation inside the panel: a link the user or the agent clicks may open a
|
|
267
|
-
* new page, and nothing tells the provider. Those pages are real
|
|
268
|
-
*
|
|
267
|
+
* new page, and nothing tells the provider. Those pages are real 鈥?they are what the human sees
|
|
268
|
+
* 鈥?but without this the tab model reports only the ones the provider created, so the rest are
|
|
269
269
|
* invisible to `dsh_browser_list_tabs` and unclosable through `dsh_browser_close_tab`.
|
|
270
270
|
*
|
|
271
271
|
* The bridge's `list` already answers with the sidebar guests, which is the same information, so
|
|
@@ -276,14 +276,14 @@ export class DesktopBridgeViewHost {
|
|
|
276
276
|
* Every sidebar page this conversation holds that THIS HOST did not open.
|
|
277
277
|
*
|
|
278
278
|
* The shell owns page creation inside the panel: a link the user or the agent clicks may open a
|
|
279
|
-
* new page, and nothing tells the provider. Those pages are real
|
|
280
|
-
*
|
|
279
|
+
* new page, and nothing tells the provider. Those pages are real 鈥?they are what the human sees
|
|
280
|
+
* 鈥?but without this the tab model reports only the ones the provider created, so the rest are
|
|
281
281
|
* invisible to `dsh_browser_list_tabs` and unclosable through `dsh_browser_close_tab`.
|
|
282
282
|
*
|
|
283
283
|
* Pages this host opened are EXCLUDED here rather than deduplicated by the caller, because the
|
|
284
284
|
* caller cannot do it: the provider knows a tab by its own id and the carrier knows a page by the
|
|
285
285
|
* guest id, and the two never match. The first version returned everything and relied on the
|
|
286
|
-
* caller to filter, which reported the same page twice
|
|
286
|
+
* caller to filter, which reported the same page twice 鈥?the mapping that makes the match
|
|
287
287
|
* possible exists only here.
|
|
288
288
|
* @returns the panel's pages that are not ours, or an empty list when the bridge cannot answer.
|
|
289
289
|
*/
|
|
@@ -308,7 +308,7 @@ export class DesktopBridgeViewHost {
|
|
|
308
308
|
/**
|
|
309
309
|
* Close one sidebar page by the id {@link listPages} reported.
|
|
310
310
|
*
|
|
311
|
-
* The bridge closes sidebar pages by TITLE, and only the ones the caller owns
|
|
311
|
+
* The bridge closes sidebar pages by TITLE, and only the ones the caller owns 鈥?that is what
|
|
312
312
|
* stopped one conversation's teardown from destroying another's page. So the title is looked up
|
|
313
313
|
* through the same listing, and a page that no longer appears is treated as already gone rather
|
|
314
314
|
* than as an error: the caller asked for it to be closed, and it is.
|
|
@@ -326,8 +326,8 @@ export class DesktopBridgeViewHost {
|
|
|
326
326
|
/**
|
|
327
327
|
* The guest backing a view, materialized on first use.
|
|
328
328
|
*
|
|
329
|
-
* The conversation that called owns exactly one sidebar page on this carrier
|
|
330
|
-
* agent look at the same page
|
|
329
|
+
* The conversation that called owns exactly one sidebar page on this carrier 鈥?a human and the
|
|
330
|
+
* agent look at the same page 鈥?so every view resolves to that conversation's own guest, and the
|
|
331
331
|
* page does not multiply behind the provider's own tab bookkeeping.
|
|
332
332
|
*
|
|
333
333
|
* A cached guest is used as-is: probing it first cost a round-trip on every command, so liveness
|
|
@@ -335,14 +335,14 @@ export class DesktopBridgeViewHost {
|
|
|
335
335
|
* @param viewId - the view whose guest is wanted.
|
|
336
336
|
* @param url - address to use when this conversation has no page yet.
|
|
337
337
|
*/
|
|
338
|
-
async guestFor(viewId, url) {
|
|
338
|
+
async guestFor(viewId, url, options) {
|
|
339
339
|
const entry = this.views.get(viewId);
|
|
340
340
|
// No liveness check here: verifying the cached guest cost a round-trip on every
|
|
341
341
|
// command. A guest that has gone away is detected by the command itself failing,
|
|
342
342
|
// and `createView`'s sendCommand then discards the cache and calls back in here.
|
|
343
343
|
if (entry?.guest !== undefined)
|
|
344
344
|
return entry.guest;
|
|
345
|
-
// The conversation THIS VIEW serves
|
|
345
|
+
// The conversation THIS VIEW serves 鈥?read from the view, never from the process.
|
|
346
346
|
//
|
|
347
347
|
// The difference is the whole bug. This host is one instance shared by every conversation, so
|
|
348
348
|
// `this.owner` alone can only be "whichever conversation this process happens to name", and
|
|
@@ -354,6 +354,33 @@ export class DesktopBridgeViewHost {
|
|
|
354
354
|
// conversation's own panel by id through the renderer, rather than by pressing the shell's
|
|
355
355
|
// shortcut or clicking its card, both of which the shell delivers to whatever it displays.
|
|
356
356
|
const owner = entry?.owner ?? this.owner;
|
|
357
|
+
// A command that must not present anything (issue #46: the live preview) takes a read-only path.
|
|
358
|
+
// 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
|
|
359
|
+
// and wrong for "let me look at it". A page that is not on screen therefore reports as a display
|
|
360
|
+
// state, and the preview route turns that into the panel's paused state.
|
|
361
|
+
if (options?.present === false) {
|
|
362
|
+
// `list` and NOT `ensureSidebar`. This is deliberate and it is the whole fix: every version of
|
|
363
|
+
// `ensureSidebar` can drive the shell — the current one asks the renderer to display the
|
|
364
|
+
// conversation, and the pre-#37 one literally pressed the sidebar shortcut and typed into the
|
|
365
|
+
// address bar. `list` is a pure read on every version, so a preview cannot move anything even
|
|
366
|
+
// when the installed bridge is old (which is exactly the reporter's environment: the symptom
|
|
367
|
+
// appears only once a bridge is installed).
|
|
368
|
+
const listed = await this.connection.call({ op: 'list', owner }, 8_000);
|
|
369
|
+
const pages = Array.isArray(listed.sidebar)
|
|
370
|
+
? listed.sidebar.filter(page => page !== null && typeof page === 'object' && page.type === 'webview')
|
|
371
|
+
: [];
|
|
372
|
+
const wanted = url !== undefined && url !== '' ? url : undefined;
|
|
373
|
+
const match = wanted === undefined
|
|
374
|
+
? pages[0]
|
|
375
|
+
: pages.find(page => typeof page.url === 'string' && (page.url === wanted || page.url.startsWith(wanted))) ?? pages[0];
|
|
376
|
+
const id = match !== undefined ? Number(match.id) : Number.NaN;
|
|
377
|
+
if (!Number.isFinite(id) || id <= 0) {
|
|
378
|
+
throw new BrowserPanelNotDisplayedError('the preview asked for this conversation\'s page without presenting it, and no page is attached '
|
|
379
|
+
+ `(the shell holds ${pages.length})`);
|
|
380
|
+
}
|
|
381
|
+
this.views.set(viewId, { guest: id, owner });
|
|
382
|
+
return id;
|
|
383
|
+
}
|
|
357
384
|
const ask = () => this.connection.call({
|
|
358
385
|
op: 'ensureSidebar',
|
|
359
386
|
owner,
|
|
@@ -366,13 +393,13 @@ export class DesktopBridgeViewHost {
|
|
|
366
393
|
// panel-not-displayed - the conversation exists but is not the one on screen;
|
|
367
394
|
// "is not mounted" - its surface store was not minted when the tab was placed.
|
|
368
395
|
// Both are gone one render later. Measured, this is the whole difference between "the sidebar
|
|
369
|
-
// works once I click over to that conversation" and "it works"
|
|
396
|
+
// works once I click over to that conversation" and "it works" 鈥?the page is real either way,
|
|
370
397
|
// and failing the call while the shell is already switching away from it just reported a
|
|
371
398
|
// problem the plugin had already fixed.
|
|
372
399
|
const displayRefusal = (value) => value.ok !== true
|
|
373
400
|
&& (String(value.code ?? '') === PANEL_NOT_DISPLAYED
|
|
374
401
|
|| String(value.error ?? '').includes('not mounted')
|
|
375
|
-
// Bridges installed before issue #37 send the same refusal with NO code
|
|
402
|
+
// Bridges installed before issue #37 send the same refusal with NO code 鈥?the installed copy
|
|
376
403
|
// on this machine is one of them (`panel-not-displayed` appears nowhere in it). Reading only
|
|
377
404
|
// the code therefore turned a retryable display state into a hard failure for exactly the
|
|
378
405
|
// users who had the old bridge, which is the majority until it is reinstalled. The wording
|
|
@@ -383,7 +410,7 @@ export class DesktopBridgeViewHost {
|
|
|
383
410
|
answer = await ask();
|
|
384
411
|
}
|
|
385
412
|
if (answer.ok !== true) {
|
|
386
|
-
// Reachable only after the retries above have run out, and the code still decides
|
|
413
|
+
// Reachable only after the retries above have run out, and the code still decides 鈥?not the
|
|
387
414
|
// wording:
|
|
388
415
|
// panel-not-displayed -> the shell was asked to show the conversation (client half's
|
|
389
416
|
// `openSession`) and did not render it in ~1.6 s: no loss, no swap.
|
|
@@ -421,13 +448,13 @@ export class DesktopBridgeViewHost {
|
|
|
421
448
|
};
|
|
422
449
|
return {
|
|
423
450
|
id: viewId,
|
|
424
|
-
sendCommand: async (method, params) => {
|
|
451
|
+
sendCommand: async (method, params, options) => {
|
|
425
452
|
// No liveness probe up front. Checking first cost a whole round-trip on
|
|
426
453
|
// every command; instead the command runs and its failure is what triggers
|
|
427
454
|
// recovery. Only a genuinely missing guest is retried, so a real error (a
|
|
428
455
|
// bad selector, a timeout) still surfaces unchanged.
|
|
429
456
|
const url = navigateUrl(method, params);
|
|
430
|
-
let guest = await this.guestFor(viewId, url);
|
|
457
|
+
let guest = await this.guestFor(viewId, url, options);
|
|
431
458
|
try {
|
|
432
459
|
return await run(guest, method, params);
|
|
433
460
|
}
|
|
@@ -442,7 +469,7 @@ export class DesktopBridgeViewHost {
|
|
|
442
469
|
const lost = this.views.get(viewId);
|
|
443
470
|
if (lost !== undefined)
|
|
444
471
|
delete lost.guest;
|
|
445
|
-
guest = await this.guestFor(viewId, url);
|
|
472
|
+
guest = await this.guestFor(viewId, url, options);
|
|
446
473
|
return await run(guest, method, params);
|
|
447
474
|
}
|
|
448
475
|
},
|
|
@@ -465,8 +492,8 @@ export class DesktopBridgeViewHost {
|
|
|
465
492
|
* This used to be empty, on the reasoning that the sidebar is already on screen so there is
|
|
466
493
|
* nothing to show. That confused "the sidebar is visible" with "this page is the one being
|
|
467
494
|
* looked at": opening a page left it behind whatever the human had in front, which is issue
|
|
468
|
-
* #23. The other two carriers both do this
|
|
469
|
-
* system browser calls `Page.bringToFront`
|
|
495
|
+
* #23. The other two carriers both do this 鈥?the self-hosted window re-adds the view and the
|
|
496
|
+
* system browser calls `Page.bringToFront` 鈥?so this carrier was the odd one out.
|
|
470
497
|
*
|
|
471
498
|
* Best-effort by design: the tab strip belongs to the shell's renderer, and failing to raise
|
|
472
499
|
* it must not fail the navigation that has already happened.
|
|
@@ -474,7 +501,7 @@ export class DesktopBridgeViewHost {
|
|
|
474
501
|
*/
|
|
475
502
|
showView(handle) {
|
|
476
503
|
// `owner` is required: the bridge refuses to bring forward a guest held by another session.
|
|
477
|
-
// It comes from the view, not the process
|
|
504
|
+
// It comes from the view, not the process 鈥?with several conversations live, a process-wide
|
|
478
505
|
// owner would raise the wrong conversation's tab.
|
|
479
506
|
const owner = this.views.get(handle.id)?.owner ?? this.owner;
|
|
480
507
|
const guest = this.views.get(handle.id)?.guest;
|
|
@@ -488,11 +515,11 @@ export class DesktopBridgeViewHost {
|
|
|
488
515
|
this.userActionHandler = handler;
|
|
489
516
|
}
|
|
490
517
|
/**
|
|
491
|
-
* Release the sidebar's browser pages (requirements
|
|
518
|
+
* Release the sidebar's browser pages (requirements 搂3, `ui.closeWithSession`).
|
|
492
519
|
*
|
|
493
520
|
* Destroying our own view handles is not enough on this carrier: the sidebar
|
|
494
521
|
* belongs to the shell and would happily keep the page (and its renderer) alive.
|
|
495
|
-
* Closing the tabs is what actually ends the page
|
|
522
|
+
* Closing the tabs is what actually ends the page 鈥?and only the page: cookies
|
|
496
523
|
* live in the partition, history on disk, so both survive.
|
|
497
524
|
* @param owner - the conversation whose pages to close. Omit to close every conversation's,
|
|
498
525
|
* which is only correct from a teardown that is itself global.
|
|
@@ -500,7 +527,7 @@ export class DesktopBridgeViewHost {
|
|
|
500
527
|
*/
|
|
501
528
|
async releasePage(owner) {
|
|
502
529
|
// Only this conversation's tabs. The host is ONE instance shared by every conversation, so a
|
|
503
|
-
// release that named none would close pages other conversations are still driving
|
|
530
|
+
// release that named none would close pages other conversations are still driving 鈥?"every
|
|
504
531
|
// webview currently visible" is precisely what must never be used here.
|
|
505
532
|
const mine = [];
|
|
506
533
|
for (const [viewId, entry] of [...this.views]) {
|
|
@@ -526,7 +553,7 @@ export class DesktopBridgeViewHost {
|
|
|
526
553
|
// `owner` is REQUIRED here now that the bridge filters `list` by it. Without it the bridge
|
|
527
554
|
// falls back to a different owner, so the guests this session holds are filtered out of the
|
|
528
555
|
// answer, `mine.includes(...)` matches nothing, the title list comes back empty and this
|
|
529
|
-
// returns without closing anything
|
|
556
|
+
// returns without closing anything 鈥?the page stayed open and the human had to close it by
|
|
530
557
|
// hand. Adding the ledger to the bridge without updating this call site is exactly the kind
|
|
531
558
|
// of half-change that made the ownership work take four rounds.
|
|
532
559
|
const answer = await this.connection.call({ op: 'list', owner: owner ?? this.owner }, 5_000);
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/** The identity rule, as data. */
|
|
2
|
+
interface IdentityRule {
|
|
3
|
+
readonly packageNames: readonly string[];
|
|
4
|
+
readonly mainEntry: string;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* The rule shipped in `desktop-bridge/desktop-identity.json`, used when the file cannot be read.
|
|
8
|
+
*
|
|
9
|
+
* A copy exists here only as a fallback for a broken package layout; the test suite asserts that it
|
|
10
|
+
* equals the JSON, so the two can never drift apart silently.
|
|
11
|
+
*/
|
|
12
|
+
export declare const FALLBACK_RULE: IdentityRule;
|
|
13
|
+
/** A verified install. */
|
|
14
|
+
export interface DesktopIdentity {
|
|
15
|
+
readonly ok: true;
|
|
16
|
+
/** The install root that was verified. */
|
|
17
|
+
readonly root: string;
|
|
18
|
+
/** Which shape was verified — the archive when it exists, the extraction only without one. */
|
|
19
|
+
readonly layout: 'packed' | 'unpacked';
|
|
20
|
+
/** `name` from the package the caller will actually run. */
|
|
21
|
+
readonly packageName: string;
|
|
22
|
+
/** `version`, when the package declares one. */
|
|
23
|
+
readonly version?: string;
|
|
24
|
+
}
|
|
25
|
+
/** A refusal, with a reason a human can act on. */
|
|
26
|
+
export interface IdentityRefusal {
|
|
27
|
+
readonly ok: false;
|
|
28
|
+
readonly reason: string;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Read the identity rule.
|
|
32
|
+
*
|
|
33
|
+
* Resolved relative to this module, which in both the repository and the published tarball puts
|
|
34
|
+
* `desktop-bridge/` beside `lib/`. A missing file is not fatal — the fallback is the same rule —
|
|
35
|
+
* but a *malformed* one is an error the caller should not ignore, so it is reported through the
|
|
36
|
+
* refusal path rather than silently swapped for the fallback.
|
|
37
|
+
* @returns the rule, or an error string.
|
|
38
|
+
*/
|
|
39
|
+
export declare function readIdentityRule(): IdentityRule | {
|
|
40
|
+
readonly error: string;
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Read one file out of an asar archive without extracting the tree.
|
|
44
|
+
* @param archive - the archive path.
|
|
45
|
+
* @param path - a slash-separated path inside the archive.
|
|
46
|
+
* @returns the bytes, or undefined when the entry is absent or unreadable.
|
|
47
|
+
*/
|
|
48
|
+
export declare function readAsarFile(archive: string, path: string): Buffer | undefined;
|
|
49
|
+
/**
|
|
50
|
+
* Whether an install directory is DSH Desktop.
|
|
51
|
+
*
|
|
52
|
+
* Everything the caller does next (stop the app, delete a tree, extract, rename, write) depends on
|
|
53
|
+
* this being right, so every uncertainty resolves to a refusal that names what was found.
|
|
54
|
+
* @param installRoot - the directory a user or the discovery probe named.
|
|
55
|
+
* @returns the verified identity, or a refusal with a reason.
|
|
56
|
+
*/
|
|
57
|
+
export declare function readDesktopIdentity(installRoot: string): DesktopIdentity | IdentityRefusal;
|
|
58
|
+
export {};
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What proves a directory is a DSH Desktop install.
|
|
3
|
+
*
|
|
4
|
+
* Reported as issue #40. Every reader of an install directory used to accept a *generic* Electron
|
|
5
|
+
* layout (`resources/app/lib/main.js` or `resources/app.asar`) as proof, and that is not proof of
|
|
6
|
+
* anything: any Electron app has it. The consequence was measured — a throwaway app in a temp
|
|
7
|
+
* directory, whose own package.json said `unrelated-meeting-app`, was accepted by discovery, got a
|
|
8
|
+
* "bridge not installed" verdict with a ready-to-run installer command aimed at it, and (with that
|
|
9
|
+
* command) had its entry file rewritten and the bridge copied in.
|
|
10
|
+
*
|
|
11
|
+
* So identity is one rule, in one file (`desktop-bridge/desktop-identity.json`), read by both the
|
|
12
|
+
* installer and this module. Nothing else may decide what a DSH Desktop install is.
|
|
13
|
+
*
|
|
14
|
+
* Two properties are load-bearing and are why this is not a one-line `name ===` check:
|
|
15
|
+
*
|
|
16
|
+
* - **The archive wins.** Electron loads `resources/app.asar` in preference to `resources/app`, and
|
|
17
|
+
* a desktop update leaves the old extraction lying around. Reading identity from the extraction
|
|
18
|
+
* while the archive holds a different build would verify the wrong bytes, so when the archive is
|
|
19
|
+
* present that is what is read — and the sibling directory is not consulted at all.
|
|
20
|
+
* - **Unreadable means not DSH.** Every failure path (missing file, truncated pickle, unsupported
|
|
21
|
+
* archive version, malformed JSON, unreadable name) refuses. There is no "assume yes" branch: the
|
|
22
|
+
* caller is about to write into, or delete from, this directory.
|
|
23
|
+
* @module dsh-browser/browser-electron/desktop-identity
|
|
24
|
+
*/
|
|
25
|
+
import { closeSync, existsSync, openSync, readFileSync, readSync } from 'node:fs';
|
|
26
|
+
import { join } from 'node:path';
|
|
27
|
+
/**
|
|
28
|
+
* The rule shipped in `desktop-bridge/desktop-identity.json`, used when the file cannot be read.
|
|
29
|
+
*
|
|
30
|
+
* A copy exists here only as a fallback for a broken package layout; the test suite asserts that it
|
|
31
|
+
* equals the JSON, so the two can never drift apart silently.
|
|
32
|
+
*/
|
|
33
|
+
export const FALLBACK_RULE = {
|
|
34
|
+
packageNames: ['@deepseek-ai/dsh-desktop', 'dsh-desktop'],
|
|
35
|
+
mainEntry: 'lib/main.js',
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Read the identity rule.
|
|
39
|
+
*
|
|
40
|
+
* Resolved relative to this module, which in both the repository and the published tarball puts
|
|
41
|
+
* `desktop-bridge/` beside `lib/`. A missing file is not fatal — the fallback is the same rule —
|
|
42
|
+
* but a *malformed* one is an error the caller should not ignore, so it is reported through the
|
|
43
|
+
* refusal path rather than silently swapped for the fallback.
|
|
44
|
+
* @returns the rule, or an error string.
|
|
45
|
+
*/
|
|
46
|
+
export function readIdentityRule() {
|
|
47
|
+
try {
|
|
48
|
+
const path = new URL('../../desktop-bridge/desktop-identity.json', import.meta.url);
|
|
49
|
+
const raw = JSON.parse(readFileSync(path, 'utf8'));
|
|
50
|
+
const names = raw.packageNames;
|
|
51
|
+
const main = raw.mainEntry;
|
|
52
|
+
if (!Array.isArray(names) || names.length === 0 || names.some(n => typeof n !== 'string')) {
|
|
53
|
+
return { error: 'desktop-identity.json: packageNames must be a non-empty array of strings' };
|
|
54
|
+
}
|
|
55
|
+
if (typeof main !== 'string' || main === '') {
|
|
56
|
+
return { error: 'desktop-identity.json: mainEntry must be a non-empty string' };
|
|
57
|
+
}
|
|
58
|
+
return { packageNames: names, mainEntry: main };
|
|
59
|
+
}
|
|
60
|
+
catch (error) {
|
|
61
|
+
if (error instanceof Error && 'code' in error && error.code === 'ENOENT') {
|
|
62
|
+
return FALLBACK_RULE;
|
|
63
|
+
}
|
|
64
|
+
return { error: `desktop-identity.json could not be read: ${String(error)}` };
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Parse an asar header, the same way the installer does.
|
|
69
|
+
*
|
|
70
|
+
* Layout, for the next reader: an 8-byte outer pickle (`[size][jsonLength]`), then the header
|
|
71
|
+
* pickle whose first 4 bytes are the JSON length, then the JSON tree. File content starts right
|
|
72
|
+
* after the header pickle.
|
|
73
|
+
* @param archive - the archive path.
|
|
74
|
+
* @returns the header tree and the offset where content starts, or an error string.
|
|
75
|
+
*/
|
|
76
|
+
function readAsarHeader(archive) {
|
|
77
|
+
let fd;
|
|
78
|
+
try {
|
|
79
|
+
fd = openSync(archive, 'r');
|
|
80
|
+
const outer = Buffer.alloc(8);
|
|
81
|
+
if (readSync(fd, outer, 0, 8, 0) !== 8)
|
|
82
|
+
return { error: 'the archive header is truncated' };
|
|
83
|
+
const headerSize = outer.readUInt32LE(4);
|
|
84
|
+
if (headerSize < 8 || headerSize > 64 * 1024 * 1024)
|
|
85
|
+
return { error: `implausible archive header size (${headerSize})` };
|
|
86
|
+
const header = Buffer.alloc(headerSize);
|
|
87
|
+
if (readSync(fd, header, 0, headerSize, 8) !== headerSize)
|
|
88
|
+
return { error: 'the archive header is truncated' };
|
|
89
|
+
const jsonLength = header.readUInt32LE(4);
|
|
90
|
+
if (jsonLength <= 0 || jsonLength > headerSize - 8)
|
|
91
|
+
return { error: 'the archive header has an implausible JSON length' };
|
|
92
|
+
return { header: JSON.parse(header.toString('utf8', 8, 8 + jsonLength)), contentBase: 8 + headerSize };
|
|
93
|
+
}
|
|
94
|
+
catch (error) {
|
|
95
|
+
return { error: `the archive header could not be read: ${String(error)}` };
|
|
96
|
+
}
|
|
97
|
+
finally {
|
|
98
|
+
if (fd !== undefined) {
|
|
99
|
+
try {
|
|
100
|
+
closeSync(fd);
|
|
101
|
+
}
|
|
102
|
+
catch { /* already closed */ }
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Walk a slash-separated path inside an asar header tree.
|
|
108
|
+
* @param tree - the header's `files` node.
|
|
109
|
+
* @param path - e.g. `lib/main.js`.
|
|
110
|
+
* @returns the entry, or undefined when the path does not exist.
|
|
111
|
+
*/
|
|
112
|
+
function entryAt(tree, path) {
|
|
113
|
+
let node;
|
|
114
|
+
let files = tree;
|
|
115
|
+
for (const part of path.split('/')) {
|
|
116
|
+
if (files === undefined)
|
|
117
|
+
return undefined;
|
|
118
|
+
node = files[part];
|
|
119
|
+
if (node === undefined)
|
|
120
|
+
return undefined;
|
|
121
|
+
files = node.files;
|
|
122
|
+
}
|
|
123
|
+
return node;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Read one file out of an asar archive without extracting the tree.
|
|
127
|
+
* @param archive - the archive path.
|
|
128
|
+
* @param path - a slash-separated path inside the archive.
|
|
129
|
+
* @returns the bytes, or undefined when the entry is absent or unreadable.
|
|
130
|
+
*/
|
|
131
|
+
export function readAsarFile(archive, path) {
|
|
132
|
+
const parsed = readAsarHeader(archive);
|
|
133
|
+
if ('error' in parsed)
|
|
134
|
+
return undefined;
|
|
135
|
+
const entry = entryAt(parsed.header.files, path);
|
|
136
|
+
if (entry === undefined || entry.files !== undefined || entry.unpacked === true)
|
|
137
|
+
return undefined;
|
|
138
|
+
if (typeof entry.offset !== 'string' || typeof entry.size !== 'number' || entry.size < 0)
|
|
139
|
+
return undefined;
|
|
140
|
+
let fd;
|
|
141
|
+
try {
|
|
142
|
+
fd = openSync(archive, 'r');
|
|
143
|
+
const buffer = Buffer.alloc(entry.size);
|
|
144
|
+
const read = readSync(fd, buffer, 0, entry.size, parsed.contentBase + Number(entry.offset));
|
|
145
|
+
return read === entry.size ? buffer : undefined;
|
|
146
|
+
}
|
|
147
|
+
catch {
|
|
148
|
+
return undefined;
|
|
149
|
+
}
|
|
150
|
+
finally {
|
|
151
|
+
if (fd !== undefined) {
|
|
152
|
+
try {
|
|
153
|
+
closeSync(fd);
|
|
154
|
+
}
|
|
155
|
+
catch { /* already closed */ }
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Whether an install directory is DSH Desktop.
|
|
161
|
+
*
|
|
162
|
+
* Everything the caller does next (stop the app, delete a tree, extract, rename, write) depends on
|
|
163
|
+
* this being right, so every uncertainty resolves to a refusal that names what was found.
|
|
164
|
+
* @param installRoot - the directory a user or the discovery probe named.
|
|
165
|
+
* @returns the verified identity, or a refusal with a reason.
|
|
166
|
+
*/
|
|
167
|
+
export function readDesktopIdentity(installRoot) {
|
|
168
|
+
const rule = readIdentityRule();
|
|
169
|
+
if ('error' in rule)
|
|
170
|
+
return { ok: false, reason: rule.error };
|
|
171
|
+
const resources = join(installRoot, 'resources');
|
|
172
|
+
const appDir = join(resources, 'app');
|
|
173
|
+
const archive = join(resources, 'app.asar');
|
|
174
|
+
let archivePresent;
|
|
175
|
+
try {
|
|
176
|
+
archivePresent = existsSync(archive);
|
|
177
|
+
}
|
|
178
|
+
catch {
|
|
179
|
+
return { ok: false, reason: `cannot inspect ${archive}` };
|
|
180
|
+
}
|
|
181
|
+
const layout = archivePresent ? 'packed' : 'unpacked';
|
|
182
|
+
let manifestText;
|
|
183
|
+
let mainPresent;
|
|
184
|
+
if (archivePresent) {
|
|
185
|
+
const parsed = readAsarHeader(archive);
|
|
186
|
+
if ('error' in parsed) {
|
|
187
|
+
return { ok: false, reason: `${archive} could not be read: ${parsed.error}` };
|
|
188
|
+
}
|
|
189
|
+
const manifest = entryAt(parsed.header.files, 'package.json');
|
|
190
|
+
if (manifest === undefined || manifest.files !== undefined || manifest.unpacked === true) {
|
|
191
|
+
return { ok: false, reason: `${archive} has no plain package.json; an archive without one is not proof of identity` };
|
|
192
|
+
}
|
|
193
|
+
manifestText = readAsarFile(archive, 'package.json')?.toString('utf8');
|
|
194
|
+
if (manifestText === undefined) {
|
|
195
|
+
return { ok: false, reason: `${archive}: package.json is listed but its bytes cannot be read` };
|
|
196
|
+
}
|
|
197
|
+
mainPresent = entryAt(parsed.header.files, rule.mainEntry) !== undefined;
|
|
198
|
+
}
|
|
199
|
+
else {
|
|
200
|
+
const manifestPath = join(appDir, 'package.json');
|
|
201
|
+
try {
|
|
202
|
+
manifestText = readFileSync(manifestPath, 'utf8');
|
|
203
|
+
}
|
|
204
|
+
catch {
|
|
205
|
+
return { ok: false, reason: `${manifestPath} is missing; a directory without a package manifest is not a DSH Desktop install` };
|
|
206
|
+
}
|
|
207
|
+
mainPresent = existsSync(join(appDir, ...rule.mainEntry.split('/')));
|
|
208
|
+
}
|
|
209
|
+
let manifest;
|
|
210
|
+
try {
|
|
211
|
+
manifest = JSON.parse(manifestText);
|
|
212
|
+
}
|
|
213
|
+
catch {
|
|
214
|
+
return { ok: false, reason: 'the package manifest is not valid JSON' };
|
|
215
|
+
}
|
|
216
|
+
const name = typeof manifest.name === 'string' ? manifest.name : '';
|
|
217
|
+
if (!rule.packageNames.includes(name)) {
|
|
218
|
+
return {
|
|
219
|
+
ok: false,
|
|
220
|
+
reason: `the app calls itself ${name === '' ? '(no name)' : JSON.stringify(name)}, which is not one of `
|
|
221
|
+
+ `${rule.packageNames.join(' / ')} — refusing to touch a different application`,
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
// The main entry is the file the installer patches and the shell runs; if it is not there, this
|
|
225
|
+
// is not an install this plugin can reason about even though the manifest looks right.
|
|
226
|
+
if (!mainPresent) {
|
|
227
|
+
return { ok: false, reason: `${rule.mainEntry} is not present under ${archivePresent ? archive : appDir}` };
|
|
228
|
+
}
|
|
229
|
+
return {
|
|
230
|
+
ok: true,
|
|
231
|
+
root: installRoot,
|
|
232
|
+
layout,
|
|
233
|
+
packageName: name,
|
|
234
|
+
...typeof manifest.version === 'string' ? { version: manifest.version } : {},
|
|
235
|
+
};
|
|
236
|
+
}
|