dsh-builtin-browser 0.5.2 → 0.5.3
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 +103 -0
- package/README.en.md +68 -68
- package/README.md +63 -92
- package/client.js +79 -10
- package/desktop-bridge/desktop-identity.json +9 -0
- package/desktop-bridge/install.mjs +220 -1
- 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 +25 -8
- 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 +164 -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 +35 -2
- package/lib/browser-electron/provider.js +19 -4
- 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 +28 -9
- package/src/browser-electron/desktop-identity.ts +260 -0
- package/src/browser-electron/electron-provision.ts +195 -0
- package/src/browser-electron/entry.ts +12 -0
- package/src/browser-electron/host-main.ts +62 -12
- package/src/browser-electron/provider.ts +47 -6
- 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
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/** How long a background provision may run before it is abandoned. */
|
|
2
|
+
export declare const PROVISION_TIMEOUT_MS = 180000;
|
|
3
|
+
/** What happened, in terms a log line can carry. */
|
|
4
|
+
export interface ProvisionResult {
|
|
5
|
+
/** `present` — nothing to do; `ready` — provisioned now; `skipped` — opted out; `failed`. */
|
|
6
|
+
readonly status: 'present' | 'ready' | 'skipped' | 'failed';
|
|
7
|
+
/** One line for the log; never contains a stack trace a user has to decode. */
|
|
8
|
+
readonly detail: string;
|
|
9
|
+
}
|
|
10
|
+
/** Everything this module touches from the outside, so a test can drive it without Electron. */
|
|
11
|
+
export interface ProvisionDeps {
|
|
12
|
+
/** Whether a spawnable binary already exists. */
|
|
13
|
+
readonly hasBinary: () => boolean;
|
|
14
|
+
/** The installed electron package's own installer script, or undefined when it is not there. */
|
|
15
|
+
readonly installerScript: () => string | undefined;
|
|
16
|
+
/** Runs the installer; resolves to an exit code, or rejects/returns null on failure. */
|
|
17
|
+
readonly run: (script: string) => Promise<{
|
|
18
|
+
code: number | null;
|
|
19
|
+
detail?: string;
|
|
20
|
+
}>;
|
|
21
|
+
/** Where the outcome goes. */
|
|
22
|
+
readonly log: (message: string) => void;
|
|
23
|
+
/** Opt-out honoured exactly like the install script honours it. */
|
|
24
|
+
readonly skip: () => boolean;
|
|
25
|
+
/** Budget for one provision. */
|
|
26
|
+
readonly timeoutMs: number;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Whether a spawnable Electron binary is already available.
|
|
30
|
+
*
|
|
31
|
+
* Resolved through the same probe the host uses, so this can never disagree with what a launch
|
|
32
|
+
* would find: a second, cheaper-looking check would be the classic way to report "ready" and then
|
|
33
|
+
* fail at spawn time.
|
|
34
|
+
* @returns true when a launch would find a binary.
|
|
35
|
+
*/
|
|
36
|
+
export declare function hasElectronRuntime(): boolean;
|
|
37
|
+
/** The real dependencies: this process's spawn, fs and logger. */
|
|
38
|
+
export declare function defaultDeps(log?: (message: string) => void): ProvisionDeps;
|
|
39
|
+
/**
|
|
40
|
+
* Provision the runtime once per process, when it is missing.
|
|
41
|
+
*
|
|
42
|
+
* Concurrent callers share the first attempt: two tool calls arriving together must not start two
|
|
43
|
+
* downloads into the same directory.
|
|
44
|
+
* @param deps - the effects, defaulted to this process's.
|
|
45
|
+
* @returns what happened. Never throws.
|
|
46
|
+
*/
|
|
47
|
+
export declare function ensureElectronRuntime(deps?: ProvisionDeps): Promise<ProvisionResult>;
|
|
48
|
+
/**
|
|
49
|
+
* Start (or join) the single background attempt.
|
|
50
|
+
*
|
|
51
|
+
* Returns immediately with the shared promise, so a caller that does not care can ignore it and one
|
|
52
|
+
* that does can await it — the returned promise never rejects.
|
|
53
|
+
* @param log - where the outcome goes.
|
|
54
|
+
* @param deps - the effects; defaulted to this process's, overridable so a test can drive it.
|
|
55
|
+
* @returns the shared attempt.
|
|
56
|
+
*/
|
|
57
|
+
export declare function provisionElectronInBackground(log: (message: string) => void, deps?: ProvisionDeps): Promise<ProvisionResult>;
|
|
58
|
+
/** Forget the attempt. Tests only: a real process provisions at most once. */
|
|
59
|
+
export declare function resetProvisionStateForTest(): void;
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Prepare the Electron runtime *when it is needed*, instead of making installation depend on it.
|
|
3
|
+
*
|
|
4
|
+
* Reported as issue #41, from a real plugin-market install of 0.5.2:
|
|
5
|
+
*
|
|
6
|
+
* [ERR_PNPM_IGNORED_BUILDS] Ignored build scripts: dsh-builtin-browser@0.5.2
|
|
7
|
+
*
|
|
8
|
+
* and, once the build script was allowed:
|
|
9
|
+
*
|
|
10
|
+
* postinstall: Preparing Electron 44.10.1 … TypeError: fetch failed
|
|
11
|
+
* postinstall: Electron installation failed (exit 1). … [ELIFECYCLE] Command failed with exit code 1
|
|
12
|
+
*
|
|
13
|
+
* Two defects in one design, and both are about WHERE the download lives:
|
|
14
|
+
*
|
|
15
|
+
* 1. A `postinstall` puts a ~100 MB network download inside package installation. pnpm blocks
|
|
16
|
+
* install scripts by default (`strict-dep-builds`), so the plugin could not be installed at all
|
|
17
|
+
* on a default pnpm 11 profile; and if the download fails for any reason the *whole install*
|
|
18
|
+
* fails, which is a network condition reported as "the plugin is broken".
|
|
19
|
+
* 2. Nothing forced the download to happen at install time. The carrier that needs Electron is one
|
|
20
|
+
* of four, and on the desktop the sidebar needs none.
|
|
21
|
+
*
|
|
22
|
+
* So installation is offline by construction now, and this module does the work later: on plugin
|
|
23
|
+
* start when the operator explicitly asked for the bundled carrier, and never fatally — a failure
|
|
24
|
+
* here is a log line plus the existing, actionable resolution error that names the command to run.
|
|
25
|
+
*
|
|
26
|
+
* Being non-fatal is the point, not a detail: this runs inside a request-serving process, and a
|
|
27
|
+
* download that hangs must not be able to hold a browser tool call open.
|
|
28
|
+
* @module dsh-browser/browser-electron/electron-provision
|
|
29
|
+
*/
|
|
30
|
+
import { spawn } from 'node:child_process';
|
|
31
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
32
|
+
import { createRequire } from 'node:module';
|
|
33
|
+
import { dirname, join } from 'node:path';
|
|
34
|
+
import { internals } from './remote-host.js';
|
|
35
|
+
/** How long a background provision may run before it is abandoned. */
|
|
36
|
+
export const PROVISION_TIMEOUT_MS = 180_000;
|
|
37
|
+
/**
|
|
38
|
+
* Whether a spawnable Electron binary is already available.
|
|
39
|
+
*
|
|
40
|
+
* Resolved through the same probe the host uses, so this can never disagree with what a launch
|
|
41
|
+
* would find: a second, cheaper-looking check would be the classic way to report "ready" and then
|
|
42
|
+
* fail at spawn time.
|
|
43
|
+
* @returns true when a launch would find a binary.
|
|
44
|
+
*/
|
|
45
|
+
export function hasElectronRuntime() {
|
|
46
|
+
try {
|
|
47
|
+
internals.resolveElectronPath();
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* The installed electron package's installer script.
|
|
56
|
+
*
|
|
57
|
+
* `require.resolve('electron/package.json')` is used rather than `require('electron')`: inside an
|
|
58
|
+
* Electron main process that module name is the built-in API, and requiring the package would
|
|
59
|
+
* download the binary as a side effect of asking about it.
|
|
60
|
+
* @returns the path to the dependency's own install script, or undefined.
|
|
61
|
+
*/
|
|
62
|
+
function electronInstallerScript() {
|
|
63
|
+
try {
|
|
64
|
+
const require = createRequire(import.meta.url);
|
|
65
|
+
const manifestPath = require.resolve('electron/package.json');
|
|
66
|
+
const script = join(dirname(manifestPath), 'install.js');
|
|
67
|
+
return existsSync(script) ? script : undefined;
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
return undefined;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
/** The Electron version the installed package asks for, for a log line that names the download. */
|
|
74
|
+
function electronVersion() {
|
|
75
|
+
try {
|
|
76
|
+
const require = createRequire(import.meta.url);
|
|
77
|
+
const manifest = JSON.parse(readFileSync(require.resolve('electron/package.json'), 'utf8'));
|
|
78
|
+
return typeof manifest.version === 'string' ? manifest.version : 'unknown';
|
|
79
|
+
}
|
|
80
|
+
catch {
|
|
81
|
+
return 'unknown';
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
/** The real dependencies: this process's spawn, fs and logger. */
|
|
85
|
+
export function defaultDeps(log = () => { }) {
|
|
86
|
+
return {
|
|
87
|
+
hasBinary: hasElectronRuntime,
|
|
88
|
+
installerScript: electronInstallerScript,
|
|
89
|
+
run: async (script) => {
|
|
90
|
+
const child = spawn(process.execPath, [script], {
|
|
91
|
+
env: { ...process.env, ELECTRON_RUN_AS_NODE: '1' },
|
|
92
|
+
stdio: ['ignore', 'ignore', 'ignore'],
|
|
93
|
+
windowsHide: true,
|
|
94
|
+
});
|
|
95
|
+
return await new Promise(resolve => {
|
|
96
|
+
child.once('error', error => resolve({ code: null, detail: String(error) }));
|
|
97
|
+
child.once('close', code => resolve({ code, detail: undefined }));
|
|
98
|
+
});
|
|
99
|
+
},
|
|
100
|
+
log,
|
|
101
|
+
skip: () => typeof process.env.ELECTRON_SKIP_BINARY_DOWNLOAD === 'string'
|
|
102
|
+
&& process.env.ELECTRON_SKIP_BINARY_DOWNLOAD !== '',
|
|
103
|
+
timeoutMs: PROVISION_TIMEOUT_MS,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Provision the runtime once per process, when it is missing.
|
|
108
|
+
*
|
|
109
|
+
* Concurrent callers share the first attempt: two tool calls arriving together must not start two
|
|
110
|
+
* downloads into the same directory.
|
|
111
|
+
* @param deps - the effects, defaulted to this process's.
|
|
112
|
+
* @returns what happened. Never throws.
|
|
113
|
+
*/
|
|
114
|
+
export async function ensureElectronRuntime(deps = defaultDeps()) {
|
|
115
|
+
if (deps.hasBinary())
|
|
116
|
+
return { status: 'present', detail: 'an Electron runtime is already available' };
|
|
117
|
+
if (deps.skip())
|
|
118
|
+
return { status: 'skipped', detail: 'ELECTRON_SKIP_BINARY_DOWNLOAD is set' };
|
|
119
|
+
const script = deps.installerScript();
|
|
120
|
+
if (script === undefined) {
|
|
121
|
+
return { status: 'failed', detail: 'the installed electron package has no install.js; reinstall the plugin in this profile' };
|
|
122
|
+
}
|
|
123
|
+
deps.log(`preparing Electron ${electronVersion()} in the background (a missing runtime needs a download)`);
|
|
124
|
+
const timeout = new Promise(resolve => {
|
|
125
|
+
setTimeout(() => resolve({ code: null, detail: `gave up after ${Math.round(deps.timeoutMs / 1000)}s` }), deps.timeoutMs).unref?.();
|
|
126
|
+
});
|
|
127
|
+
let outcome;
|
|
128
|
+
try {
|
|
129
|
+
outcome = await Promise.race([deps.run(script), timeout]);
|
|
130
|
+
}
|
|
131
|
+
catch (error) {
|
|
132
|
+
return { status: 'failed', detail: `the download failed: ${String(error)}` };
|
|
133
|
+
}
|
|
134
|
+
if (outcome.code !== 0) {
|
|
135
|
+
return {
|
|
136
|
+
status: 'failed',
|
|
137
|
+
detail: `Electron provisioning did not finish (${outcome.detail ?? `exit ${String(outcome.code)}`}); `
|
|
138
|
+
+ 'the browser still works through a sidebar, Chrome or Edge, and `npx --no dsh-browser-install-electron` retries it',
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
return { status: 'ready', detail: 'the Electron runtime is ready' };
|
|
142
|
+
}
|
|
143
|
+
/** One attempt per process, shared by every caller. */
|
|
144
|
+
let attempt;
|
|
145
|
+
/**
|
|
146
|
+
* Start (or join) the single background attempt.
|
|
147
|
+
*
|
|
148
|
+
* Returns immediately with the shared promise, so a caller that does not care can ignore it and one
|
|
149
|
+
* that does can await it — the returned promise never rejects.
|
|
150
|
+
* @param log - where the outcome goes.
|
|
151
|
+
* @param deps - the effects; defaulted to this process's, overridable so a test can drive it.
|
|
152
|
+
* @returns the shared attempt.
|
|
153
|
+
*/
|
|
154
|
+
export function provisionElectronInBackground(log, deps = defaultDeps(log)) {
|
|
155
|
+
attempt ??= ensureElectronRuntime(deps).then(result => {
|
|
156
|
+
log(`electron runtime: ${result.status} — ${result.detail}`);
|
|
157
|
+
return result;
|
|
158
|
+
});
|
|
159
|
+
return attempt;
|
|
160
|
+
}
|
|
161
|
+
/** Forget the attempt. Tests only: a real process provisions at most once. */
|
|
162
|
+
export function resetProvisionStateForTest() {
|
|
163
|
+
attempt = undefined;
|
|
164
|
+
}
|
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
*/
|
|
12
12
|
import z from '@deepseek-ai/schemastery';
|
|
13
13
|
import { ElectronBrowserProvider } from './provider.js';
|
|
14
|
+
import { provisionElectronInBackground } from './electron-provision.js';
|
|
14
15
|
import { randomUUID } from 'node:crypto';
|
|
15
16
|
import { existsSync, mkdirSync, renameSync, statSync } from 'node:fs';
|
|
16
17
|
import { homedir } from 'node:os';
|
|
@@ -116,6 +117,17 @@ export function apply(ctx, config) {
|
|
|
116
117
|
// different lifetimes (a spawned child vs. the shell's view), and pretending
|
|
117
118
|
// otherwise would leave a stray window behind.
|
|
118
119
|
const selfHosted = new RemoteElectronViewHost(defaultHostMainPath());
|
|
120
|
+
// The self-hosted carrier is the only one that needs Electron, and it is prepared HERE, lazily,
|
|
121
|
+
// rather than during package installation (issue #41: a `postinstall` download made a default
|
|
122
|
+
// pnpm 11 profile refuse the install outright, and a failed fetch failed the install too).
|
|
123
|
+
//
|
|
124
|
+
// Non-fatal by construction: this never rejects, never blocks a tool call, and its failure leaves
|
|
125
|
+
// the sidebar / Chrome / Edge carriers untouched. Only the two channels that can use the bundled
|
|
126
|
+
// runtime are worth the download; an explicit chrome/edge choice is not.
|
|
127
|
+
const wantedChannel = settings.get().browser.channel;
|
|
128
|
+
if (wantedChannel === 'bundled' || wantedChannel === 'auto') {
|
|
129
|
+
void provisionElectronInBackground(message => ctx.logger?.info?.(`dsh-builtin-browser: ${message}`));
|
|
130
|
+
}
|
|
119
131
|
let unregister = ctx.browser.registerBrowserProvider(new ElectronBrowserProvider(selfHosted, providerConfig));
|
|
120
132
|
let disposed = false;
|
|
121
133
|
let carrierChoice = 0;
|
|
@@ -536,8 +536,17 @@ function updateWindowTitle(win, viewId, label) {
|
|
|
536
536
|
}
|
|
537
537
|
catch { /* destroyed */ }
|
|
538
538
|
}
|
|
539
|
-
/**
|
|
540
|
-
|
|
539
|
+
/**
|
|
540
|
+
* Make one view the visible (topmost) one WITHIN its window, leaving the window itself alone.
|
|
541
|
+
*
|
|
542
|
+
* Split out of {@link showViewInWindow} for quiet-mode capture: a screenshot still has to look
|
|
543
|
+
* at the active tab rather than another tab of the same window, but it must not show, restore,
|
|
544
|
+
* raise or focus the window to do it. Everything here is contentView-local.
|
|
545
|
+
* @param win - the window holding the view.
|
|
546
|
+
* @param viewId - the view to reveal.
|
|
547
|
+
* @param label - session/task label, when known, for the window title.
|
|
548
|
+
*/
|
|
549
|
+
function revealViewInWindow(win, viewId, label) {
|
|
541
550
|
const entry = win.views.get(viewId);
|
|
542
551
|
if (entry === undefined)
|
|
543
552
|
return;
|
|
@@ -578,12 +587,43 @@ function showViewInWindow(win, viewId, label) {
|
|
|
578
587
|
// Always refresh the title: it reflects the CURRENT page of the visible
|
|
579
588
|
// view, which changes as the agent navigates.
|
|
580
589
|
updateWindowTitle(win, viewId, label);
|
|
590
|
+
}
|
|
591
|
+
/**
|
|
592
|
+
* Make one view the visible one within its window, and — outside quiet mode — raise the window.
|
|
593
|
+
* @param win - the window holding the view.
|
|
594
|
+
* @param viewId - the view to present.
|
|
595
|
+
* @param label - session/task label, when known, for the window title.
|
|
596
|
+
* @param quiet - `ui.quiet`: present without taking the screen over.
|
|
597
|
+
* @param needSurface - quiet mode only: this presentation is the barrier before synthesized
|
|
598
|
+
* input, so the window has to be visible even if the user minimized it. A navigation or a tab
|
|
599
|
+
* switch does not need that and leaves a minimized window alone.
|
|
600
|
+
*/
|
|
601
|
+
function showViewInWindow(win, viewId, label, quiet = false, needSurface = false) {
|
|
602
|
+
revealViewInWindow(win, viewId, label);
|
|
581
603
|
// Raise this session's window so the human sees the page being worked on
|
|
582
604
|
// (moveTop raises without stealing keyboard focus).
|
|
605
|
+
//
|
|
606
|
+
// Quiet mode (`ui.quiet`): the user asked not to be interrupted, so this must not
|
|
607
|
+
// activate the application, steal keyboard focus, or pull the window in front of the one
|
|
608
|
+
// they are using. Chromium drops `Input.*` for a view with no display surface, so the one
|
|
609
|
+
// case that still needs a visible window — the barrier before synthesized input — restores
|
|
610
|
+
// it with `showInactive()`, which shows it without becoming the key window, and never calls
|
|
611
|
+
// `moveTop()`. Everything else (navigation, tab switching, screenshots) leaves a minimized
|
|
612
|
+
// window minimized.
|
|
583
613
|
try {
|
|
584
|
-
if (
|
|
585
|
-
|
|
586
|
-
|
|
614
|
+
if (quiet) {
|
|
615
|
+
if (!needSurface)
|
|
616
|
+
return;
|
|
617
|
+
if (win.window.isMinimized())
|
|
618
|
+
win.window.restore();
|
|
619
|
+
if (!win.window.isVisible())
|
|
620
|
+
win.window.showInactive();
|
|
621
|
+
}
|
|
622
|
+
else {
|
|
623
|
+
if (!win.window.isVisible())
|
|
624
|
+
win.window.show();
|
|
625
|
+
win.window.moveTop();
|
|
626
|
+
}
|
|
587
627
|
}
|
|
588
628
|
catch { /* closing */ }
|
|
589
629
|
}
|
|
@@ -751,7 +791,7 @@ async function handle(op, msg) {
|
|
|
751
791
|
throw new Error('showView missing viewId');
|
|
752
792
|
const win = windowOfView(viewId);
|
|
753
793
|
if (win !== undefined)
|
|
754
|
-
showViewInWindow(win, viewId, typeof msg.label === 'string' ? msg.label : undefined);
|
|
794
|
+
showViewInWindow(win, viewId, typeof msg.label === 'string' ? msg.label : undefined, msg.quiet === true, msg.needSurface === true);
|
|
755
795
|
reply(msg.id, { ok: true });
|
|
756
796
|
return;
|
|
757
797
|
}
|
|
@@ -829,17 +869,31 @@ async function handle(op, msg) {
|
|
|
829
869
|
const wantQuality = typeof msg.quality === 'number' ? msg.quality : 80;
|
|
830
870
|
const maxW = typeof msg.maxWidth === 'number' ? msg.maxWidth : 0;
|
|
831
871
|
const maxH = typeof msg.maxHeight === 'number' ? msg.maxHeight : 0;
|
|
872
|
+
// Quiet mode: a screenshot is not a reason to take the screen over. Skipping the
|
|
873
|
+
// raise below is what makes a minimized window survive a screenshot: capturePage on
|
|
874
|
+
// a hidden view returns an empty image, and the CDP fallback captures without needing
|
|
875
|
+
// a display surface. The default path keeps the raise, because there a screenshot is
|
|
876
|
+
// how the human follows what the agent is doing.
|
|
877
|
+
const quiet = msg.quiet === true;
|
|
832
878
|
let mime = 'image/png';
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
879
|
+
if (quiet) {
|
|
880
|
+
// The active tab has to be the visible one INSIDE the window, or the capture may read
|
|
881
|
+
// another tab. Keep that switch contentView-local: show/restore/focus are precisely
|
|
882
|
+
// what quiet mode forbids.
|
|
883
|
+
revealViewInWindow(win, viewId);
|
|
836
884
|
}
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
885
|
+
else {
|
|
886
|
+
try {
|
|
887
|
+
if (!win.window.isVisible())
|
|
888
|
+
win.window.show();
|
|
889
|
+
}
|
|
890
|
+
catch { /* closing */ }
|
|
891
|
+
try {
|
|
892
|
+
win.window.restore();
|
|
893
|
+
}
|
|
894
|
+
catch { /* not minimized */ }
|
|
895
|
+
win.window.focus();
|
|
840
896
|
}
|
|
841
|
-
catch { /* not minimized */ }
|
|
842
|
-
win.window.focus();
|
|
843
897
|
let base64 = '';
|
|
844
898
|
let renderedSize;
|
|
845
899
|
try {
|
|
@@ -43,8 +43,10 @@ export interface ElectronBrowserViewHost {
|
|
|
43
43
|
* @param label - human-readable session/task label, when the provider knows
|
|
44
44
|
* one; the host may surface it (e.g. in the window title) so a human can
|
|
45
45
|
* tell which task's page is currently visible.
|
|
46
|
+
* @param options - presentation policy; `quiet` asks for a surface without taking
|
|
47
|
+
* over the screen (see {@link PresentOptions}).
|
|
46
48
|
*/
|
|
47
|
-
showView?(handle: ElectronViewHandle, label?: string): void;
|
|
49
|
+
showView?(handle: ElectronViewHandle, label?: string, options?: PresentOptions): void;
|
|
48
50
|
/**
|
|
49
51
|
* Optional: (re)present a view and WAIT until the host confirms it. Chromium
|
|
50
52
|
* silently drops CDP-synthesized input (`Input.*`) for a view that has no
|
|
@@ -57,8 +59,9 @@ export interface ElectronBrowserViewHost {
|
|
|
57
59
|
* assumed to present every view (headless/probe hosts).
|
|
58
60
|
* @param handle - the handle to present.
|
|
59
61
|
* @param label - human-readable session/task label, as in {@link showView}.
|
|
62
|
+
* @param options - presentation policy, as in {@link showView}.
|
|
60
63
|
*/
|
|
61
|
-
presentView?(handle: ElectronViewHandle, label?: string): Promise<void>;
|
|
64
|
+
presentView?(handle: ElectronViewHandle, label?: string, options?: PresentOptions): Promise<void>;
|
|
62
65
|
/**
|
|
63
66
|
* Optional: pages this carrier holds that the provider never created.
|
|
64
67
|
*
|
|
@@ -419,12 +422,34 @@ export declare function toExportedCookies(raw: unknown): ExportedCookie[];
|
|
|
419
422
|
* @returns the CDP cookie, or undefined when it lacks a usable domain.
|
|
420
423
|
*/
|
|
421
424
|
export declare function toCdpCookie(cookie: ExportedCookie): Record<string, unknown> | undefined;
|
|
425
|
+
/**
|
|
426
|
+
* How the carrier should present a view before it is used.
|
|
427
|
+
*
|
|
428
|
+
* `quiet` is the resolved `ui.quiet` switch. It does not mean "invisible": Chromium drops
|
|
429
|
+
* synthesized `Input.*` for a view without a display surface, so input still needs one. It means
|
|
430
|
+
* the carrier must get that surface without taking the screen over — no application activation
|
|
431
|
+
* and no keyboard focus. Navigation and tab switching only reveal the view inside its window; the
|
|
432
|
+
* pre-input barrier may restore a minimized window (it needs the surface), but even then it shows
|
|
433
|
+
* it inactive instead of focusing or raising it. A host that cannot honour this (a headless probe,
|
|
434
|
+
* the desktop sidebar) simply ignores it.
|
|
435
|
+
*/
|
|
436
|
+
export interface PresentOptions {
|
|
437
|
+
readonly quiet?: boolean;
|
|
438
|
+
}
|
|
422
439
|
/** Native capture options the self-hosted view handle understands. */
|
|
423
440
|
export interface ScreenshotOptions {
|
|
424
441
|
readonly format?: 'png' | 'jpeg';
|
|
425
442
|
readonly quality?: number;
|
|
426
443
|
readonly maxWidth?: number;
|
|
427
444
|
readonly maxHeight?: number;
|
|
445
|
+
/**
|
|
446
|
+
* Capture without presenting the window first (`ui.quiet`).
|
|
447
|
+
*
|
|
448
|
+
* The self-hosted host answers this by skipping its restore/show/focus step: a visible window
|
|
449
|
+
* still captures through `capturePage()`, and a minimized or hidden one falls through to the
|
|
450
|
+
* CDP path, which needs no display surface.
|
|
451
|
+
*/
|
|
452
|
+
readonly quiet?: boolean;
|
|
428
453
|
}
|
|
429
454
|
/** CDP method for runtime evaluation (the execute path). */
|
|
430
455
|
export declare const CDP_RUNTIME_EVALUATE = "Runtime.evaluate";
|
|
@@ -885,6 +910,14 @@ export declare class ElectronBrowserProvider implements BrowserProvider {
|
|
|
885
910
|
* diagnostics: it can never be allowed to become the failure.
|
|
886
911
|
*/
|
|
887
912
|
private notifyUserActionError;
|
|
913
|
+
/**
|
|
914
|
+
* Whether the operator asked for quiet mode (`ui.quiet`).
|
|
915
|
+
*
|
|
916
|
+
* Read live on every call rather than captured at construction: the panel writes the document
|
|
917
|
+
* and the next operation is expected to honour it, without a restart.
|
|
918
|
+
* @returns true when the carrier must present views without taking the screen over.
|
|
919
|
+
*/
|
|
920
|
+
private isQuiet;
|
|
888
921
|
/**
|
|
889
922
|
* Ask the host to show the active tab's view, carrying the session label.
|
|
890
923
|
*
|