dsh-builtin-browser 0.5.6 → 0.5.8

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 (119) hide show
  1. package/CHANGELOG.md +148 -0
  2. package/README.en.md +75 -81
  3. package/README.md +75 -82
  4. package/client.js +831 -384
  5. package/desktop-bridge/install-and-start.cmd +4 -5
  6. package/desktop-bridge/install.mjs +322 -141
  7. package/desktop-bridge/maintenance-progress.ps1 +166 -0
  8. package/desktop-bridge/plugin-browser-bridge.js +157 -19
  9. package/desktop-bridge/request-desktop-quit.ps1 +170 -0
  10. package/docs/tool-reference.md +20 -1
  11. package/docs/user-guide.md +28 -4
  12. package/lib/browser/runtime.d.ts +3 -1
  13. package/lib/browser/runtime.js +9 -0
  14. package/lib/browser/types.d.ts +42 -0
  15. package/lib/browser-electron/authenticated-route.d.ts +27 -0
  16. package/lib/browser-electron/authenticated-route.js +39 -0
  17. package/lib/browser-electron/automatic-maintenance.d.ts +41 -0
  18. package/lib/browser-electron/automatic-maintenance.js +426 -0
  19. package/lib/browser-electron/bridge-auto.d.ts +93 -0
  20. package/lib/browser-electron/bridge-auto.js +113 -0
  21. package/lib/browser-electron/bridge-connection.d.ts +0 -2
  22. package/lib/browser-electron/bridge-connection.js +2 -4
  23. package/lib/browser-electron/bridge-endpoint.js +8 -19
  24. package/lib/browser-electron/bridge-install.d.ts +85 -0
  25. package/lib/browser-electron/bridge-install.js +211 -0
  26. package/lib/browser-electron/bridge-maintenance.d.ts +24 -0
  27. package/lib/browser-electron/bridge-maintenance.js +125 -0
  28. package/lib/browser-electron/bridge-status.d.ts +10 -3
  29. package/lib/browser-electron/bridge-status.js +74 -24
  30. package/lib/browser-electron/desktop-bridge-host.d.ts +11 -5
  31. package/lib/browser-electron/desktop-bridge-host.js +67 -25
  32. package/lib/browser-electron/desktop-identity.js +21 -9
  33. package/lib/browser-electron/electron-provision.d.ts +1 -1
  34. package/lib/browser-electron/electron-provision.js +36 -13
  35. package/lib/browser-electron/entry.js +173 -67
  36. package/lib/browser-electron/ephemeral-profile.js +67 -26
  37. package/lib/browser-electron/history-store.js +2 -2
  38. package/lib/browser-electron/host-main.js +48 -38
  39. package/lib/browser-electron/http-body.d.ts +24 -0
  40. package/lib/browser-electron/http-body.js +33 -0
  41. package/lib/browser-electron/json-reply.d.ts +13 -1
  42. package/lib/browser-electron/json-reply.js +16 -2
  43. package/lib/browser-electron/maintenance-broker.d.ts +35 -0
  44. package/lib/browser-electron/maintenance-broker.js +114 -0
  45. package/lib/browser-electron/maintenance-lifecycle.d.ts +32 -0
  46. package/lib/browser-electron/maintenance-lifecycle.js +67 -0
  47. package/lib/browser-electron/maintenance-route.d.ts +18 -0
  48. package/lib/browser-electron/maintenance-route.js +75 -0
  49. package/lib/browser-electron/paths.d.ts +4 -0
  50. package/lib/browser-electron/paths.js +19 -0
  51. package/lib/browser-electron/process.d.ts +14 -0
  52. package/lib/browser-electron/process.js +24 -0
  53. package/lib/browser-electron/provider.d.ts +16 -9
  54. package/lib/browser-electron/provider.js +87 -18
  55. package/lib/browser-electron/remote-host.js +22 -20
  56. package/lib/browser-electron/runtime-route.d.ts +3 -5
  57. package/lib/browser-electron/runtime-status.d.ts +27 -13
  58. package/lib/browser-electron/runtime-status.js +28 -6
  59. package/lib/browser-electron/settings-route.d.ts +5 -18
  60. package/lib/browser-electron/settings-route.js +4 -14
  61. package/lib/browser-electron/settings-store.d.ts +28 -0
  62. package/lib/browser-electron/settings-store.js +32 -2
  63. package/lib/browser-electron/sidebar-only-host.d.ts +36 -0
  64. package/lib/browser-electron/sidebar-only-host.js +192 -0
  65. package/lib/browser-electron/system-browser.d.ts +6 -11
  66. package/lib/browser-electron/system-browser.js +29 -24
  67. package/lib/browser-electron/timing.d.ts +8 -0
  68. package/lib/browser-electron/timing.js +13 -0
  69. package/lib/browser-electron/windows-system.d.ts +3 -0
  70. package/lib/browser-electron/windows-system.js +19 -0
  71. package/lib/tool-browser/index.js +1045 -7
  72. package/lib/tool-browser/page-probe.d.ts +86 -0
  73. package/lib/tool-browser/page-probe.js +460 -0
  74. package/package.json +5 -3
  75. package/scripts/run-tests.mjs +32 -0
  76. package/scripts/verify-preview-route.mjs +19 -1
  77. package/scripts/verify-runtime-status.mjs +14 -0
  78. package/skills/dsh-builtin-browser/SKILL.md +64 -0
  79. package/skills/dsh-builtin-browser/references/batch.md +23 -0
  80. package/skills/dsh-builtin-browser/references/deliver.md +31 -0
  81. package/skills/dsh-builtin-browser/references/locate.md +11 -0
  82. package/skills/dsh-builtin-browser/references/recover.md +11 -0
  83. package/skills/dsh-builtin-browser/references/verify.md +13 -0
  84. package/src/browser/runtime.ts +11 -0
  85. package/src/browser/types.ts +19 -0
  86. package/src/browser-electron/authenticated-route.ts +56 -0
  87. package/src/browser-electron/automatic-maintenance.ts +414 -0
  88. package/src/browser-electron/bridge-auto.ts +158 -0
  89. package/src/browser-electron/bridge-connection.ts +2 -4
  90. package/src/browser-electron/bridge-endpoint.ts +8 -18
  91. package/src/browser-electron/bridge-install.ts +253 -0
  92. package/src/browser-electron/bridge-maintenance.ts +161 -0
  93. package/src/browser-electron/bridge-status.ts +76 -26
  94. package/src/browser-electron/desktop-bridge-host.ts +60 -26
  95. package/src/browser-electron/desktop-identity.ts +18 -9
  96. package/src/browser-electron/electron-provision.ts +35 -14
  97. package/src/browser-electron/entry.ts +152 -62
  98. package/src/browser-electron/ephemeral-profile.ts +65 -28
  99. package/src/browser-electron/history-store.ts +2 -2
  100. package/src/browser-electron/host-main.ts +40 -34
  101. package/src/browser-electron/http-body.ts +42 -0
  102. package/src/browser-electron/json-reply.ts +18 -3
  103. package/src/browser-electron/maintenance-broker.ts +142 -0
  104. package/src/browser-electron/maintenance-lifecycle.ts +86 -0
  105. package/src/browser-electron/maintenance-route.ts +64 -0
  106. package/src/browser-electron/paths.ts +21 -0
  107. package/src/browser-electron/process.ts +23 -0
  108. package/src/browser-electron/provider.ts +106 -22
  109. package/src/browser-electron/remote-host.ts +23 -23
  110. package/src/browser-electron/runtime-route.ts +3 -5
  111. package/src/browser-electron/runtime-status.ts +56 -19
  112. package/src/browser-electron/settings-route.ts +9 -32
  113. package/src/browser-electron/settings-store.ts +59 -3
  114. package/src/browser-electron/sidebar-only-host.ts +199 -0
  115. package/src/browser-electron/system-browser.ts +26 -23
  116. package/src/browser-electron/timing.ts +16 -0
  117. package/src/browser-electron/windows-system.ts +23 -0
  118. package/src/tool-browser/index.ts +995 -10
  119. package/src/tool-browser/page-probe.ts +474 -0
@@ -116,6 +116,34 @@ export interface BrowserSettings {
116
116
  /** Write cookies — importing login state (`dsh_browser_auth` restore). */
117
117
  readonly allowCredentialWrite: boolean;
118
118
  };
119
+ /**
120
+ * Where a downloaded file goes after it lands on disk.
121
+ *
122
+ * An ATTRIBUTE of a download, not a new concept: an empty `tool` means the behaviour is exactly
123
+ * what it was before this existed. The user owns the field, can point it at any MCP tool that
124
+ * takes a path (Feishu drive, Baidu Netdisk), and can empty it again at any time.
125
+ */
126
+ /**
127
+ * Desktop bridge handled without asking.
128
+ *
129
+ * Historical automatic-install preference retained when reading existing settings documents.
130
+ * Runtime automatic installation is disabled; bridge maintenance is an explicit action after
131
+ * DSH Desktop is closed, even when an older document contains autoInstall=true.
132
+ */
133
+ readonly bridge: {
134
+ /** Legacy preference; it does not trigger installation at runtime. Default false. */
135
+ readonly autoInstall: boolean;
136
+ };
137
+ readonly delivery: {
138
+ /** MCP tool that receives the saved file. Empty = save locally and stop there. */
139
+ readonly tool: string;
140
+ /** Flat argument template; {path}, {name} and {size} are substituted into string values. */
141
+ readonly args: Readonly<Record<string, string>>;
142
+ /** `keep-local` reports both outcomes; `stop` reports the handoff failure as the result. */
143
+ readonly onFail: 'keep-local' | 'stop';
144
+ /** Per-site override: page host → tool name. */
145
+ readonly sites: Readonly<Record<string, string>>;
146
+ };
119
147
  }
120
148
  /** Defaults for every switch: the documented out-of-the-box behaviour. */
121
149
  export declare const DEFAULT_SETTINGS: BrowserSettings;
@@ -9,8 +9,8 @@
9
9
  * @module dsh-browser/browser-electron/settings-store
10
10
  */
11
11
  import { mkdirSync, readFileSync, renameSync, rmSync, statSync, writeFileSync } from 'node:fs';
12
- import { homedir } from 'node:os';
13
12
  import { dirname, join } from 'node:path';
13
+ import { dshHome } from './paths.js';
14
14
  /** Every value {@link HeadlessMode} accepts, for validation and the settings UI. */
15
15
  export const HEADLESS_MODES = ['auto', 'headless', 'windowed'];
16
16
  /** Defaults for every switch: the documented out-of-the-box behaviour. */
@@ -27,7 +27,11 @@ export const DEFAULT_SETTINGS = {
27
27
  // Off unless the host turns it on. A frame can show a CAPTCHA, an account name, or logged-in
28
28
  // content, so watching is an explicit decision rather than something a remote client can arrange.
29
29
  remoteView: { enabled: false },
30
+ bridge: { autoInstall: false },
30
31
  actions: { allowExecute: true, allowDownload: true, allowCredentialWrite: true },
32
+ // Local by default: the file lands in downloadDir and nothing leaves the machine until the user
33
+ // names a tool here. {file_path} is the argument name both Feishu's and Netdisk's upload tools use.
34
+ delivery: { tool: '', args: { file_path: '{path}' }, onFail: 'keep-local', sites: {} },
31
35
  };
32
36
  /**
33
37
  * Absolute path of the settings file, beside the browser profile and the
@@ -35,7 +39,7 @@ export const DEFAULT_SETTINGS = {
35
39
  * @returns the settings file path.
36
40
  */
37
41
  export function settingsPath() {
38
- const home = process.env.DSH_HOME ?? join(homedir(), '.dsh');
42
+ const home = dshHome();
39
43
  return join(home, 'dsh-builtin-browser-host', 'settings.json');
40
44
  }
41
45
  /** Coerce one boolean field, keeping the default when absent or mistyped. */
@@ -181,6 +185,19 @@ export function resolveSettings(raw, warn) {
181
185
  const browser = section(source, 'browser');
182
186
  const actions = section(source, 'actions');
183
187
  const remoteView = section(source, 'remoteView');
188
+ // Read defensively: this section is hand-editable, and a bad value must fall back to "local only"
189
+ // rather than fail the whole document.
190
+ const deliverySection = (section(source, 'delivery') ?? {});
191
+ const strings = (value, fallback) => {
192
+ if (value === null || typeof value !== 'object' || Array.isArray(value))
193
+ return { ...fallback };
194
+ const out = {};
195
+ for (const [key, entry] of Object.entries(value)) {
196
+ if (typeof entry === 'string')
197
+ out[key] = entry;
198
+ }
199
+ return Object.keys(out).length === 0 ? { ...fallback } : out;
200
+ };
184
201
  // A section that is present but unreadable refuses its gates rather than defaulting them
185
202
  // open. The rest of a broken section keeps its defaults: refusing history or presentation
186
203
  // would punish the user for a bad file without protecting anything.
@@ -228,11 +245,24 @@ export function resolveSettings(raw, warn) {
228
245
  // Fail-closed, and stated separately from `gate`'s fallback so the intent is visible: an
229
246
  // unreadable or absent section means nobody watches. Unlike the other switches, whose defaults
230
247
  // are "on", this one has no safe default other than off.
248
+ bridge: {
249
+ autoInstall: section(source, 'bridge') === undefined
250
+ ? DEFAULT_SETTINGS.bridge.autoInstall
251
+ : gate(section(source, 'bridge').autoInstall, DEFAULT_SETTINGS.bridge.autoInstall),
252
+ },
231
253
  remoteView: {
232
254
  enabled: remoteView === undefined
233
255
  ? false
234
256
  : gate(remoteView.enabled, false),
235
257
  },
258
+ delivery: {
259
+ tool: typeof deliverySection.tool === 'string' && /^[A-Za-z0-9_.-]+$/.test(deliverySection.tool)
260
+ ? deliverySection.tool
261
+ : '',
262
+ args: strings(deliverySection.args, DEFAULT_SETTINGS.delivery.args),
263
+ onFail: deliverySection.onFail === 'stop' ? 'stop' : 'keep-local',
264
+ sites: strings(deliverySection.sites, {}),
265
+ },
236
266
  };
237
267
  }
238
268
  /**
@@ -0,0 +1,36 @@
1
+ import type { CarrierLossReason, CarrierPage } from '../browser/types.js';
2
+ import type { ElectronBrowserViewHost, ElectronViewHandle, PresentOptions } from './provider.js';
3
+ /**
4
+ * Keep an explicit sidebar selection while its bridge starts or reconnects.
5
+ * Discovery is deferred until a command needs the bridge; it never selects another carrier.
6
+ */
7
+ export declare class SidebarOnlyHost implements ElectronBrowserViewHost {
8
+ private readonly loader;
9
+ private readonly views;
10
+ private lease;
11
+ private loading;
12
+ private disposed;
13
+ private lostListener;
14
+ constructor(loader: () => Promise<ElectronBrowserViewHost>);
15
+ /** Keep the explicit selection available so discovery failures reach the caller unchanged. */
16
+ available(): boolean;
17
+ /** Return a stable handle without opening a sidebar or discovering a bridge. */
18
+ createView(owner?: string): ElectronViewHandle;
19
+ destroyView(handle: ElectronViewHandle): void;
20
+ /** Presentation must not turn the provider's initial show into a blank-page navigation. */
21
+ showView(handle: ElectronViewHandle, label?: string, options?: PresentOptions): void;
22
+ listPages(owner?: string): Promise<readonly CarrierPage[]>;
23
+ closePage(id: string, owner?: string): Promise<void>;
24
+ /** Retire only this conversation's handles before awaiting an in-flight discovery. */
25
+ releasePage(owner?: string): Promise<void>;
26
+ collapse(): Promise<void>;
27
+ onLost(listener: (reason: CarrierLossReason) => void): void;
28
+ dispose(): void;
29
+ private assertOpen;
30
+ private assertView;
31
+ private assertLease;
32
+ private load;
33
+ private lost;
34
+ private bind;
35
+ private send;
36
+ }
@@ -0,0 +1,192 @@
1
+ /** A lazy, reconnectable sidebar carrier that preserves the provider's view handles. */
2
+ import { randomUUID } from 'node:crypto';
3
+ /**
4
+ * Keep an explicit sidebar selection while its bridge starts or reconnects.
5
+ * Discovery is deferred until a command needs the bridge; it never selects another carrier.
6
+ */
7
+ export class SidebarOnlyHost {
8
+ loader;
9
+ views = new Map();
10
+ lease;
11
+ loading;
12
+ disposed = false;
13
+ lostListener;
14
+ constructor(loader) {
15
+ this.loader = loader;
16
+ }
17
+ /** Keep the explicit selection available so discovery failures reach the caller unchanged. */
18
+ available() { return !this.disposed; }
19
+ /** Return a stable handle without opening a sidebar or discovering a bridge. */
20
+ createView(owner) {
21
+ this.assertOpen();
22
+ const handle = {
23
+ id: randomUUID(),
24
+ sendCommand: (method, params, options) => this.send(view, method, params, options),
25
+ download: async (url, savePath) => {
26
+ const bound = await this.bind(view);
27
+ this.assertView(view);
28
+ this.assertLease(bound.lease);
29
+ if (bound.handle.download === undefined)
30
+ throw new Error('dsh-builtin-browser: update the desktop bridge for sidebar downloads');
31
+ await bound.handle.download(url, savePath);
32
+ },
33
+ };
34
+ const view = { handle, owner };
35
+ this.views.set(handle.id, view);
36
+ return handle;
37
+ }
38
+ destroyView(handle) {
39
+ const view = this.views.get(handle.id);
40
+ if (view?.handle !== handle)
41
+ return;
42
+ this.views.delete(handle.id);
43
+ if (view.bound !== undefined)
44
+ view.bound.lease.host.destroyView(view.bound.handle);
45
+ view.bound = undefined;
46
+ }
47
+ /** Presentation must not turn the provider's initial show into a blank-page navigation. */
48
+ showView(handle, label, options) {
49
+ const view = this.views.get(handle.id);
50
+ const bound = view?.handle === handle ? view.bound : undefined;
51
+ if (!this.disposed && bound !== undefined && bound.lease === this.lease) {
52
+ bound.lease.host.showView?.(bound.handle, label, options);
53
+ }
54
+ }
55
+ async listPages(owner) {
56
+ const lease = await this.load();
57
+ this.assertLease(lease);
58
+ return lease.host.listPages === undefined ? [] : lease.host.listPages(owner);
59
+ }
60
+ async closePage(id, owner) {
61
+ const lease = await this.load();
62
+ this.assertLease(lease);
63
+ if (lease.host.closePage === undefined)
64
+ throw new Error('dsh-builtin-browser: this sidebar bridge cannot close pages');
65
+ await lease.host.closePage(id, owner);
66
+ }
67
+ /** Retire only this conversation's handles before awaiting an in-flight discovery. */
68
+ async releasePage(owner) {
69
+ for (const view of [...this.views.values()]) {
70
+ if (owner === undefined || view.owner === owner)
71
+ this.destroyView(view.handle);
72
+ }
73
+ const lease = this.lease ?? await this.loading?.catch(() => undefined);
74
+ if (!this.disposed && lease !== undefined && lease === this.lease)
75
+ await lease.host.releasePage?.(owner);
76
+ }
77
+ async collapse() {
78
+ if (!this.disposed)
79
+ await this.lease?.host.collapse?.();
80
+ }
81
+ onLost(listener) {
82
+ this.lostListener = listener;
83
+ }
84
+ dispose() {
85
+ if (this.disposed)
86
+ return;
87
+ this.disposed = true;
88
+ this.views.clear();
89
+ const lease = this.lease;
90
+ this.lease = undefined;
91
+ this.lostListener = undefined;
92
+ lease?.host.dispose?.();
93
+ }
94
+ assertOpen() {
95
+ if (this.disposed)
96
+ throw new Error('dsh-builtin-browser: sidebar host is disposed');
97
+ }
98
+ assertView(view) {
99
+ this.assertOpen();
100
+ if (this.views.get(view.handle.id) !== view)
101
+ throw new Error('dsh-builtin-browser: sidebar view is destroyed');
102
+ }
103
+ assertLease(lease) {
104
+ this.assertOpen();
105
+ if (this.lease !== lease)
106
+ throw new Error('dsh-builtin-browser: sidebar bridge changed before the operation');
107
+ }
108
+ load() {
109
+ this.assertOpen();
110
+ if (this.lease !== undefined)
111
+ return Promise.resolve(this.lease);
112
+ if (this.loading !== undefined)
113
+ return this.loading;
114
+ const pending = Promise.resolve().then(this.loader).then(host => {
115
+ if (this.disposed) {
116
+ host.dispose?.();
117
+ this.assertOpen();
118
+ }
119
+ const lease = { host };
120
+ this.lease = lease;
121
+ try {
122
+ host.onLost?.(reason => this.lost(lease, reason));
123
+ }
124
+ catch (error) {
125
+ if (this.lease === lease)
126
+ this.lease = undefined;
127
+ host.dispose?.();
128
+ throw error;
129
+ }
130
+ if (this.lease !== lease)
131
+ throw new Error('dsh-builtin-browser: sidebar bridge was lost during discovery');
132
+ return lease;
133
+ });
134
+ this.loading = pending;
135
+ const clear = () => { if (this.loading === pending)
136
+ this.loading = undefined; };
137
+ void pending.then(clear, clear);
138
+ return pending;
139
+ }
140
+ lost(lease, reason) {
141
+ if (this.disposed || this.lease !== lease)
142
+ return;
143
+ this.lease = undefined;
144
+ this.loading = undefined;
145
+ for (const view of this.views.values()) {
146
+ view.bound = undefined;
147
+ view.pending = undefined;
148
+ }
149
+ // A command already in flight keeps its original outcome; the next call rediscovers.
150
+ try {
151
+ lease.host.dispose?.();
152
+ }
153
+ catch { /* The next discovery replaces this failed carrier. */ }
154
+ try {
155
+ this.lostListener?.(reason);
156
+ }
157
+ catch { /* Transport recovery must survive a listener failure. */ }
158
+ }
159
+ bind(view) {
160
+ this.assertView(view);
161
+ if (view.bound !== undefined && view.bound.lease === this.lease)
162
+ return Promise.resolve(view.bound);
163
+ if (view.pending !== undefined)
164
+ return view.pending;
165
+ const pending = this.load().then(lease => {
166
+ this.assertView(view);
167
+ if (this.lease !== lease)
168
+ throw new Error('dsh-builtin-browser: sidebar bridge changed before binding the view');
169
+ const handle = lease.host.createView(view.owner);
170
+ if (this.disposed || this.views.get(view.handle.id) !== view || this.lease !== lease) {
171
+ lease.host.destroyView(handle);
172
+ this.assertView(view);
173
+ throw new Error('dsh-builtin-browser: sidebar bridge changed while binding the view');
174
+ }
175
+ const bound = { lease, handle };
176
+ view.bound = bound;
177
+ return bound;
178
+ });
179
+ view.pending = pending;
180
+ const clear = () => { if (view.pending === pending)
181
+ view.pending = undefined; };
182
+ void pending.then(clear, clear);
183
+ return pending;
184
+ }
185
+ async send(view, method, params, options) {
186
+ const bound = await this.bind(view);
187
+ this.assertView(view);
188
+ if (this.lease !== bound.lease)
189
+ throw new Error('dsh-builtin-browser: sidebar bridge changed before the command');
190
+ return bound.handle.sendCommand(method, params, options);
191
+ }
192
+ }
@@ -85,6 +85,7 @@ export declare class SystemBrowserViewHost implements ElectronBrowserViewHost {
85
85
  * and why a display-less host needs headless at all (issue #32).
86
86
  */
87
87
  private readonly headlessMode;
88
+ private readonly isQuiet;
88
89
  /**
89
90
  * viewId -> the session that view rides, and the client that issued it.
90
91
  *
@@ -118,14 +119,6 @@ export declare class SystemBrowserViewHost implements ElectronBrowserViewHost {
118
119
  */
119
120
  private clientSuspect;
120
121
  private child;
121
- /**
122
- * True when the browser in use was adopted from a previous run rather than launched here.
123
- *
124
- * Worth recording because the two differ on teardown: a browser this host spawned is its to
125
- * kill, while one it merely took over belongs to a process that is already gone — closing it is
126
- * still right (it holds the profile nobody else can use), but it is a decision, not cleanup.
127
- */
128
- private adoptedRunning;
129
122
  /** Whether this connection's surplus blank pages have been collapsed already. */
130
123
  private blanksNormalized;
131
124
  private starting;
@@ -156,7 +149,7 @@ export declare class SystemBrowserViewHost implements ElectronBrowserViewHost {
156
149
  * Whether this launch should get a window. See {@link resolveLaunchMode} for how `auto` decides
157
150
  * and why a display-less host needs headless at all (issue #32).
158
151
  */
159
- headlessMode?: HeadlessMode);
152
+ headlessMode?: HeadlessMode, isQuiet?: () => boolean);
160
153
  /** Which product this host would drive (for diagnostics). */
161
154
  get kind(): DetectedBrowser['kind'];
162
155
  /** Whether the browser has actually been started yet (diagnostics and tests). */
@@ -204,7 +197,7 @@ export declare class SystemBrowserViewHost implements ElectronBrowserViewHost {
204
197
  * not a browser at all. What makes adoption safe HERE is that this runs BEFORE launching, on a
205
198
  * file that lives inside this plugin's own profile directory — only a browser started with this
206
199
  * `--user-data-dir` writes it — and the endpoint is then asked to identify itself.
207
- * @param why - a short phrase for the log, naming which attempt this is.
200
+ * @param _why - a short phrase for the log, naming which attempt this is.
208
201
  * @returns a connected client for the running browser, or undefined when there is none.
209
202
  */
210
203
  private adoptRunningBrowser;
@@ -284,7 +277,9 @@ export declare class SystemBrowserViewHost implements ElectronBrowserViewHost {
284
277
  * two, and the order is honoured either way.
285
278
  * @param handle - the view to present.
286
279
  */
287
- presentView(handle: ElectronViewHandle): Promise<void>;
280
+ presentView(handle: ElectronViewHandle, _label?: string, options?: {
281
+ readonly quiet?: boolean;
282
+ }): Promise<void>;
288
283
  /**
289
284
  * Best effort at bringing the browser's window forward.
290
285
  *
@@ -116,8 +116,8 @@ const PATH_NAMES = {
116
116
  const INSTALL_LOCATIONS = {
117
117
  chrome: {
118
118
  win32: [
119
- String.raw `C:\Program Files\Google\Chrome\Application\chrome.exe`,
120
- String.raw `C:\Program Files (x86)\Google\Chrome\Application\chrome.exe`,
119
+ String.raw `%ProgramFiles%\Google\Chrome\Application\chrome.exe`,
120
+ String.raw `%ProgramFiles(x86)%\Google\Chrome\Application\chrome.exe`,
121
121
  // Where a per-user install lands, which is what Chrome's own installer chooses when it
122
122
  // cannot (or should not) write to Program Files. Missing this meant the plugin reported
123
123
  // "no browser found" on machines that plainly had one.
@@ -138,8 +138,8 @@ const INSTALL_LOCATIONS = {
138
138
  },
139
139
  edge: {
140
140
  win32: [
141
- String.raw `C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe`,
142
- String.raw `C:\Program Files\Microsoft\Edge\Application\msedge.exe`,
141
+ String.raw `%ProgramFiles(x86)%\Microsoft\Edge\Application\msedge.exe`,
142
+ String.raw `%ProgramFiles%\Microsoft\Edge\Application\msedge.exe`,
143
143
  String.raw `%LOCALAPPDATA%\Microsoft\Edge\Application\msedge.exe`,
144
144
  ],
145
145
  darwin: ['/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge'],
@@ -151,8 +151,8 @@ const INSTALL_LOCATIONS = {
151
151
  },
152
152
  brave: {
153
153
  win32: [
154
- String.raw `C:\Program Files\BraveSoftware\Brave-Browser\Application\brave.exe`,
155
- String.raw `C:\Program Files (x86)\BraveSoftware\Brave-Browser\Application\brave.exe`,
154
+ String.raw `%ProgramFiles%\BraveSoftware\Brave-Browser\Application\brave.exe`,
155
+ String.raw `%ProgramFiles(x86)%\BraveSoftware\Brave-Browser\Application\brave.exe`,
156
156
  String.raw `%LOCALAPPDATA%\BraveSoftware\Brave-Browser\Application\brave.exe`,
157
157
  ],
158
158
  darwin: ['/Applications/Brave Browser.app/Contents/MacOS/Brave Browser'],
@@ -483,6 +483,7 @@ export class SystemBrowserViewHost {
483
483
  ephemeralDir;
484
484
  launcher;
485
485
  headlessMode;
486
+ isQuiet;
486
487
  /**
487
488
  * viewId -> the session that view rides, and the client that issued it.
488
489
  *
@@ -516,14 +517,6 @@ export class SystemBrowserViewHost {
516
517
  */
517
518
  clientSuspect = false;
518
519
  child;
519
- /**
520
- * True when the browser in use was adopted from a previous run rather than launched here.
521
- *
522
- * Worth recording because the two differ on teardown: a browser this host spawned is its to
523
- * kill, while one it merely took over belongs to a process that is already gone — closing it is
524
- * still right (it holds the profile nobody else can use), but it is a decision, not cleanup.
525
- */
526
- adoptedRunning = false;
527
520
  /** Whether this connection's surplus blank pages have been collapsed already. */
528
521
  blanksNormalized = false;
529
522
  starting;
@@ -554,13 +547,14 @@ export class SystemBrowserViewHost {
554
547
  * Whether this launch should get a window. See {@link resolveLaunchMode} for how `auto` decides
555
548
  * and why a display-less host needs headless at all (issue #32).
556
549
  */
557
- headlessMode = 'auto') {
550
+ headlessMode = 'auto', isQuiet = () => false) {
558
551
  this.browser = browser;
559
552
  this.profileDir = profileDir;
560
553
  this.extraArgs = extraArgs;
561
554
  this.ephemeralDir = ephemeralDir;
562
555
  this.launcher = launcher;
563
556
  this.headlessMode = headlessMode;
557
+ this.isQuiet = isQuiet;
564
558
  }
565
559
  /** Which product this host would drive (for diagnostics). */
566
560
  get kind() {
@@ -676,10 +670,10 @@ export class SystemBrowserViewHost {
676
670
  * not a browser at all. What makes adoption safe HERE is that this runs BEFORE launching, on a
677
671
  * file that lives inside this plugin's own profile directory — only a browser started with this
678
672
  * `--user-data-dir` writes it — and the endpoint is then asked to identify itself.
679
- * @param why - a short phrase for the log, naming which attempt this is.
673
+ * @param _why - a short phrase for the log, naming which attempt this is.
680
674
  * @returns a connected client for the running browser, or undefined when there is none.
681
675
  */
682
- async adoptRunningBrowser(why) {
676
+ async adoptRunningBrowser(_why) {
683
677
  const portFile = join(this.profileDir, 'DevToolsActivePort');
684
678
  let port;
685
679
  try {
@@ -717,7 +711,6 @@ export class SystemBrowserViewHost {
717
711
  return undefined;
718
712
  }
719
713
  this.client = client;
720
- this.adoptedRunning = true;
721
714
  return client;
722
715
  }
723
716
  catch {
@@ -740,6 +733,14 @@ export class SystemBrowserViewHost {
740
733
  const adopted = await this.adoptRunningBrowser('before launch');
741
734
  if (adopted !== undefined)
742
735
  return adopted;
736
+ // Whether a window exists is decided by `browser.headless` ALONE (plus the operator's
737
+ // DSH_BROWSER_HEADLESS override inside resolveLaunchMode). `ui.quiet` must not force
738
+ // headless: it describes how the agent presents its work — no activation, no raising,
739
+ // background targets — not whether the human is allowed to see a browser at all.
740
+ // Overriding the setting here made an explicit `headless: windowed`/`headed` invisible on
741
+ // Chrome/Edge, so the human could not take over a login or a challenge, and an already
742
+ // headless process cannot become visible by toggling quiet later. Quiet still governs
743
+ // presentView() and createTarget() below, which is where activation belongs.
743
744
  const launch = resolveLaunchMode(this.headlessMode);
744
745
  const args = [
745
746
  '--remote-debugging-port=0',
@@ -863,8 +864,6 @@ export class SystemBrowserViewHost {
863
864
  return undefined;
864
865
  }
865
866
  })();
866
- // Kept for the failure message and for the exit listener's diagnostics; no longer a judgement.
867
- const spawnedAt = Date.now();
868
867
  let child;
869
868
  try {
870
869
  child = this.launcher(this.browser.path, args);
@@ -914,8 +913,8 @@ export class SystemBrowserViewHost {
914
913
  const after = statSync(portFile);
915
914
  const contents = readFileSync(portFile, 'utf8');
916
915
  // Unchanged since before the launch: this is the previous browser's file, whatever its
917
- // mtime says. Comparing against the snapshot rather than the spawn time is what keeps this
918
- // correct on a filesystem whose timestamps are coarser than the clock behind spawnedAt.
916
+ // mtime says. Comparing against the snapshot rather than a wall-clock instant is what
917
+ // keeps this correct on a filesystem whose timestamps are coarser than a millisecond.
919
918
  if (portFileBefore !== undefined
920
919
  && after.mtimeMs <= portFileBefore.mtimeMs
921
920
  && contents === portFileBefore.contents)
@@ -1102,6 +1101,7 @@ export class SystemBrowserViewHost {
1102
1101
  const ours = new Set(this.sessions.values());
1103
1102
  return (listed?.targetInfos ?? [])
1104
1103
  .filter(entry => entry.type === 'page' && typeof entry.targetId === 'string')
1104
+ .filter(entry => !/^edge:\/\/sync-confirmation-dialog(?:\/|$)/.test(entry.url ?? ''))
1105
1105
  .filter(entry => !ours.has(entry.targetId))
1106
1106
  .map(entry => ({
1107
1107
  id: entry.targetId,
@@ -1259,7 +1259,9 @@ export class SystemBrowserViewHost {
1259
1259
  * two, and the order is honoured either way.
1260
1260
  * @param handle - the view to present.
1261
1261
  */
1262
- async presentView(handle) {
1262
+ async presentView(handle, _label, options) {
1263
+ if (options?.quiet === true || this.isQuiet())
1264
+ return;
1263
1265
  try {
1264
1266
  await handle.sendCommand('Page.bringToFront');
1265
1267
  }
@@ -1479,7 +1481,10 @@ export class SystemBrowserViewHost {
1479
1481
  }
1480
1482
  /** Create one page, refusing to go on without an id. */
1481
1483
  async createTarget(client) {
1482
- const created = await client.send('Target.createTarget', { url: 'about:blank' });
1484
+ const created = await client.send('Target.createTarget', {
1485
+ url: 'about:blank',
1486
+ ...this.isQuiet() ? { background: true } : {},
1487
+ });
1483
1488
  const targetId = created?.targetId;
1484
1489
  if (typeof targetId !== 'string')
1485
1490
  throw new Error('dsh-builtin-browser: the browser did not create a page');
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Bound an await with a timeout, without dictating the caller's error wording.
3
+ *
4
+ * The two hosts phrase their timeout errors differently and those strings reach the panel, so
5
+ * the shared primitive takes a factory instead of a message.
6
+ * @module dsh-browser/browser-electron/timing
7
+ */
8
+ export declare function withTimeoutShared<T>(promise: Promise<T>, ms: number, timedOut: () => Error): Promise<T>;
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Bound an await with a timeout, without dictating the caller's error wording.
3
+ *
4
+ * The two hosts phrase their timeout errors differently and those strings reach the panel, so
5
+ * the shared primitive takes a factory instead of a message.
6
+ * @module dsh-browser/browser-electron/timing
7
+ */
8
+ export function withTimeoutShared(promise, ms, timedOut) {
9
+ return new Promise((resolve, reject) => {
10
+ const timer = setTimeout(() => { reject(timedOut()); }, ms);
11
+ promise.then((value) => { clearTimeout(timer); resolve(value); }, (error) => { clearTimeout(timer); reject(error); });
12
+ });
13
+ }
@@ -0,0 +1,3 @@
1
+ /** PowerShell's `-EncodedCommand` expects a UTF-16LE script encoded as base64. */
2
+ export declare function encodePowerShellScript(script: string): string;
3
+ export declare function windowsPowerShellPath(environment?: NodeJS.ProcessEnv, exists?: (path: string) => boolean): string;
@@ -0,0 +1,19 @@
1
+ /** Locate Windows system tools from the running system rather than an assumed system drive. */
2
+ import { existsSync } from 'node:fs';
3
+ import { win32 } from 'node:path';
4
+ /** PowerShell's `-EncodedCommand` expects a UTF-16LE script encoded as base64. */
5
+ export function encodePowerShellScript(script) {
6
+ return Buffer.from(script, 'utf16le').toString('base64');
7
+ }
8
+ export function windowsPowerShellPath(environment = process.env, exists = existsSync) {
9
+ const read = (name) => Object.entries(environment)
10
+ .find(([key]) => key.toUpperCase() === name)?.[1];
11
+ const root = read('SYSTEMROOT') || read('WINDIR');
12
+ if (root === undefined || !/^(?:[A-Za-z]:[\\/]|\\\\[^\\/]+[\\/][^\\/]+)/.test(root)) {
13
+ throw new Error('Windows system directory is unavailable; SystemRoot or WINDIR must identify the current system');
14
+ }
15
+ const path = win32.join(root, 'System32', 'WindowsPowerShell', 'v1.0', 'powershell.exe');
16
+ if (!exists(path))
17
+ throw new Error('Windows PowerShell is unavailable in the detected system directory');
18
+ return path;
19
+ }