dsh-builtin-browser 0.5.1 → 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 +133 -0
- package/README.en.md +68 -67
- package/README.md +63 -91
- package/client.js +235 -11
- 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/runtime.d.ts +4 -2
- package/lib/browser/runtime.js +4 -0
- package/lib/browser/types.d.ts +37 -0
- package/lib/browser-electron/bridge-status.d.ts +5 -3
- package/lib/browser-electron/bridge-status.js +25 -8
- package/lib/browser-electron/desktop-bridge-host.d.ts +5 -3
- package/lib/browser-electron/desktop-bridge-host.js +47 -13
- 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 +61 -2
- package/lib/browser-electron/host-main.js +68 -14
- package/lib/browser-electron/provider.d.ts +51 -3
- package/lib/browser-electron/provider.js +77 -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/lib/tool-browser/index.js +49 -0
- package/package.json +2 -3
- package/src/browser/runtime.ts +8 -0
- package/src/browser/types.ts +36 -0
- package/src/browser-electron/bridge-status.ts +28 -9
- package/src/browser-electron/desktop-bridge-host.ts +49 -13
- package/src/browser-electron/desktop-identity.ts +260 -0
- package/src/browser-electron/electron-provision.ts +195 -0
- package/src/browser-electron/entry.ts +49 -2
- package/src/browser-electron/host-main.ts +62 -12
- package/src/browser-electron/provider.ts +118 -6
- package/src/browser-electron/remote-host.ts +26 -12
- package/src/browser-electron/settings-store.ts +24 -1
- package/src/tool-browser/index.ts +49 -0
- package/src/types/electron-shim.d.ts +8 -0
|
@@ -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,8 +11,9 @@
|
|
|
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
|
-
import { existsSync, mkdirSync, renameSync } from 'node:fs';
|
|
16
|
+
import { existsSync, mkdirSync, renameSync, statSync } from 'node:fs';
|
|
16
17
|
import { homedir } from 'node:os';
|
|
17
18
|
import { dirname, join } from 'node:path';
|
|
18
19
|
import { setTimeout as wait } from 'node:timers/promises';
|
|
@@ -21,7 +22,7 @@ import { DesktopBridgeViewHost } from './desktop-bridge-host.js';
|
|
|
21
22
|
import { detectBrowser, searchSummary, SystemBrowserViewHost } from './system-browser.js';
|
|
22
23
|
import { claimEphemeralProfile, ephemeralProfileName, sweepAbandonedEphemeralProfiles } from './ephemeral-profile.js';
|
|
23
24
|
import { MissingSystemBrowserHost } from './missing-system-browser.js';
|
|
24
|
-
import { SettingsStore } from './settings-store.js';
|
|
25
|
+
import { SettingsStore, settingsPath } from './settings-store.js';
|
|
25
26
|
import { sameOrigin } from './settings-route.js';
|
|
26
27
|
import { handleBridgeStatus, handlePreviewFrame, handlePreviewSessions, BRIDGE_STATUS_ROUTE, PREVIEW_ROUTE, PREVIEW_SESSIONS_ROUTE } from './preview-route.js';
|
|
27
28
|
export { ELECTRON_BROWSER_PROVIDER_ID, ElectronBrowserProvider, } from './provider.js';
|
|
@@ -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;
|
|
@@ -475,6 +487,53 @@ export function apply(ctx, config) {
|
|
|
475
487
|
}
|
|
476
488
|
}
|
|
477
489
|
installSettingsRoute(ctx, settings, () => { chooseCarrier(); });
|
|
490
|
+
/**
|
|
491
|
+
* Re-run the carrier choice when the settings FILE changes on disk.
|
|
492
|
+
*
|
|
493
|
+
* The route above covers a write through the panel. It does not cover a person editing
|
|
494
|
+
* `settings.json` by hand — and that is how `browser.channel` gets changed when the panel is
|
|
495
|
+
* unreachable or the user prefers the file. Measured: the document said `sidebar` and the plugin
|
|
496
|
+
* kept driving the self-hosted window built for the previous choice, because nothing re-read it.
|
|
497
|
+
*
|
|
498
|
+
* Change detection is on the channel alone, not the mtime: a poll that re-chose the carrier on
|
|
499
|
+
* every unrelated save would tear down and rebuild a working browser session for a switch that
|
|
500
|
+
* cannot affect it. mtime is only the cheap trigger to go and look.
|
|
501
|
+
*/
|
|
502
|
+
const watchSettingsFile = () => {
|
|
503
|
+
const file = settingsPath();
|
|
504
|
+
let lastChannel = settings.get().browser.channel;
|
|
505
|
+
let lastMtime = 0;
|
|
506
|
+
try {
|
|
507
|
+
lastMtime = statSync(file).mtimeMs;
|
|
508
|
+
}
|
|
509
|
+
catch { /* not written yet */ }
|
|
510
|
+
const timer = setInterval(() => {
|
|
511
|
+
if (disposed) {
|
|
512
|
+
clearInterval(timer);
|
|
513
|
+
return;
|
|
514
|
+
}
|
|
515
|
+
let mtime = 0;
|
|
516
|
+
try {
|
|
517
|
+
mtime = statSync(file).mtimeMs;
|
|
518
|
+
}
|
|
519
|
+
catch {
|
|
520
|
+
return;
|
|
521
|
+
}
|
|
522
|
+
if (mtime === lastMtime)
|
|
523
|
+
return;
|
|
524
|
+
lastMtime = mtime;
|
|
525
|
+
const channel = settings.get().browser.channel;
|
|
526
|
+
if (channel === lastChannel)
|
|
527
|
+
return;
|
|
528
|
+
lastChannel = channel;
|
|
529
|
+
ctx.logger?.info?.(`dsh-builtin-browser: settings changed on disk, re-choosing the carrier for channel "${channel}"`);
|
|
530
|
+
chooseCarrier();
|
|
531
|
+
}, 2_000);
|
|
532
|
+
// Unref-ed so a poll never holds the process open.
|
|
533
|
+
timer.unref?.();
|
|
534
|
+
ctx.effect(() => () => { clearInterval(timer); }, "dsh-builtin-browser: settings file watch");
|
|
535
|
+
};
|
|
536
|
+
watchSettingsFile();
|
|
478
537
|
installPreviewRoute(ctx, settings);
|
|
479
538
|
installBridgeStatusRoute(ctx);
|
|
480
539
|
}
|
|
@@ -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 {
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
*/
|
|
9
9
|
import { type VisitedPage } from './history-store.js';
|
|
10
10
|
import type { BrowserSettings } from './settings-store.js';
|
|
11
|
-
import type { BrowserA11yRequest, BrowserA11yResult, BrowserChallenge, BrowserCheckRequest, BrowserClearRequest, BrowserClickRequest, BrowserContentRequest, BrowserContentResult, BrowserElementTarget, BrowserExecuteRequest, BrowserExecuteResult, BrowserFillRequest, BrowserFillResult, BrowserGetValueRequest, BrowserGetValueResult, BrowserHistoryEntry, BrowserOpenRequest, BrowserProvider, BrowserScrapeRequest, BrowserScrapeResult, BrowserScreenshotRequest, BrowserSelectRequest, BrowserSelectResult, BrowserSessionId, BrowserSetValueRequest, BrowserSetValueResult, BrowserSnapshotResult, BrowserTab, BrowserTypeRequest, BrowserWaitRequest, BrowserWaitResult, BrowserScrollRequest, BrowserKeyRequest, ExportedCookie } from '../browser/types.js';
|
|
11
|
+
import type { BrowserA11yRequest, BrowserA11yResult, BrowserChallenge, BrowserCheckRequest, BrowserClearRequest, BrowserClickRequest, BrowserClickSequenceRequest, BrowserClickSequenceResult, BrowserContentRequest, BrowserContentResult, BrowserElementTarget, BrowserExecuteRequest, BrowserExecuteResult, BrowserFillRequest, BrowserFillResult, BrowserGetValueRequest, BrowserGetValueResult, BrowserHistoryEntry, BrowserOpenRequest, BrowserProvider, BrowserScrapeRequest, BrowserScrapeResult, BrowserScreenshotRequest, BrowserSelectRequest, BrowserSelectResult, BrowserSessionId, BrowserSetValueRequest, BrowserSetValueResult, BrowserSnapshotResult, BrowserTab, BrowserTypeRequest, BrowserWaitRequest, BrowserWaitResult, BrowserScrollRequest, BrowserKeyRequest, ExportedCookie } from '../browser/types.js';
|
|
12
12
|
import type { CarrierPage, CarrierLossReason } from '../browser/types.js';
|
|
13
13
|
/** Stable provider id registered with `ctx.browser`. */
|
|
14
14
|
export declare const ELECTRON_BROWSER_PROVIDER_ID = "electron";
|
|
@@ -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";
|
|
@@ -605,6 +630,21 @@ export declare class ElectronBrowserProvider implements BrowserProvider {
|
|
|
605
630
|
click(session: BrowserSessionId, request: BrowserClickRequest | {
|
|
606
631
|
readonly target: BrowserElementTarget;
|
|
607
632
|
}, signal?: AbortSignal): Promise<void>;
|
|
633
|
+
/**
|
|
634
|
+
* Click many points in order, once.
|
|
635
|
+
*
|
|
636
|
+
* The same wire sequence as {@link click}, looped — only the round-trip count changes. Presenting
|
|
637
|
+
* happens ONCE: the display-surface barrier is per view, not per press, and paying it for every
|
|
638
|
+
* point is what made an 86-cell board expensive.
|
|
639
|
+
*
|
|
640
|
+
* Stops at the first point that fails and reports where: a run that pressed through a page which
|
|
641
|
+
* had navigated or lost its surface would fill the wrong cells and still answer success.
|
|
642
|
+
* @param session - the session to click in.
|
|
643
|
+
* @param request - the points and the interval between them.
|
|
644
|
+
* @param signal - cancellation between points.
|
|
645
|
+
* @returns how many points landed, and where it stopped if it did not finish.
|
|
646
|
+
*/
|
|
647
|
+
clickSequence(session: BrowserSessionId, request: BrowserClickSequenceRequest, signal?: AbortSignal): Promise<BrowserClickSequenceResult>;
|
|
608
648
|
/**
|
|
609
649
|
* Type into the focused element, or focus a located element (css/text/xpath)
|
|
610
650
|
* first and then insert the text.
|
|
@@ -870,6 +910,14 @@ export declare class ElectronBrowserProvider implements BrowserProvider {
|
|
|
870
910
|
* diagnostics: it can never be allowed to become the failure.
|
|
871
911
|
*/
|
|
872
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;
|
|
873
921
|
/**
|
|
874
922
|
* Ask the host to show the active tab's view, carrying the session label.
|
|
875
923
|
*
|
|
@@ -738,7 +738,7 @@ export class ElectronBrowserProvider {
|
|
|
738
738
|
signal?.throwIfAborted();
|
|
739
739
|
const timeoutMs = 10_000;
|
|
740
740
|
try {
|
|
741
|
-
await withTimeout(present(handle, s.label), timeoutMs, signal, `browser: presenting the page view timed out after ${timeoutMs}ms`);
|
|
741
|
+
await withTimeout(present(handle, s.label, { quiet: this.isQuiet() }), timeoutMs, signal, `browser: presenting the page view timed out after ${timeoutMs}ms`);
|
|
742
742
|
this.showActive(s);
|
|
743
743
|
}
|
|
744
744
|
catch (error) {
|
|
@@ -1523,6 +1523,64 @@ export class ElectronBrowserProvider {
|
|
|
1523
1523
|
}
|
|
1524
1524
|
this.record(s, 'click', 'target' in request ? { target: request.target } : { x, y }, true);
|
|
1525
1525
|
}
|
|
1526
|
+
/**
|
|
1527
|
+
* Click many points in order, once.
|
|
1528
|
+
*
|
|
1529
|
+
* The same wire sequence as {@link click}, looped — only the round-trip count changes. Presenting
|
|
1530
|
+
* happens ONCE: the display-surface barrier is per view, not per press, and paying it for every
|
|
1531
|
+
* point is what made an 86-cell board expensive.
|
|
1532
|
+
*
|
|
1533
|
+
* Stops at the first point that fails and reports where: a run that pressed through a page which
|
|
1534
|
+
* had navigated or lost its surface would fill the wrong cells and still answer success.
|
|
1535
|
+
* @param session - the session to click in.
|
|
1536
|
+
* @param request - the points and the interval between them.
|
|
1537
|
+
* @param signal - cancellation between points.
|
|
1538
|
+
* @returns how many points landed, and where it stopped if it did not finish.
|
|
1539
|
+
*/
|
|
1540
|
+
async clickSequence(session, request, signal) {
|
|
1541
|
+
const s = this.session(session);
|
|
1542
|
+
const points = Array.isArray(request.points) ? request.points : [];
|
|
1543
|
+
if (points.length === 0)
|
|
1544
|
+
return { clicked: 0 };
|
|
1545
|
+
if (this.settingsSource !== undefined && this.settingsSource().vision.strategy === 'nonVisual') {
|
|
1546
|
+
throw new BrowserError('browser: the configured non-visual strategy refuses coordinate clicks — pass a semantic target instead.', 'BROWSER_NON_VISUAL_COORDINATES');
|
|
1547
|
+
}
|
|
1548
|
+
// One barrier for the whole run: cheap, and it is the view that needs a surface, not each press.
|
|
1549
|
+
const handle = await this.present(s, signal);
|
|
1550
|
+
const timeoutMs = 30_000;
|
|
1551
|
+
// 40 ms is above one frame at 30 fps, which is what keeps the page from reading two presses as a
|
|
1552
|
+
// double click. Faster runs were measured dropping presses outright.
|
|
1553
|
+
const gap = typeof request.intervalMs === 'number' && request.intervalMs >= 0 ? request.intervalMs : 40;
|
|
1554
|
+
const send = (params, label) => withTimeout(handle.sendCommand('Input.dispatchMouseEvent', params), timeoutMs, signal, `browser: click sequence ${label} timed out after ${timeoutMs}ms`);
|
|
1555
|
+
let clicked = 0;
|
|
1556
|
+
for (const [index, point] of points.entries()) {
|
|
1557
|
+
signal?.throwIfAborted();
|
|
1558
|
+
const x = Number(point?.x);
|
|
1559
|
+
const y = Number(point?.y);
|
|
1560
|
+
if (!Number.isFinite(x) || !Number.isFinite(y)) {
|
|
1561
|
+
return { clicked, failedAt: index, error: `point ${index} has no finite x/y` };
|
|
1562
|
+
}
|
|
1563
|
+
try {
|
|
1564
|
+
// The move is load-bearing: Chromium routes a synthesized press to the CURRENT hover target,
|
|
1565
|
+
// and a view that has never received a mouse event has none — the first press would go nowhere
|
|
1566
|
+
// while CDP still answered success.
|
|
1567
|
+
await send({ type: 'mouseMoved', x, y, buttons: 0 }, 'move');
|
|
1568
|
+
await send({ type: 'mousePressed', x, y, button: 'left', buttons: 1, clickCount: 1 }, 'press');
|
|
1569
|
+
await send({ type: 'mouseReleased', x, y, button: 'left', buttons: 0, clickCount: 1 }, 'release');
|
|
1570
|
+
clicked += 1;
|
|
1571
|
+
}
|
|
1572
|
+
catch (error) {
|
|
1573
|
+
// Best-effort release so a failed press cannot leave the button stuck down.
|
|
1574
|
+
void handle
|
|
1575
|
+
.sendCommand('Input.dispatchMouseEvent', { type: 'mouseReleased', x, y, button: 'left', buttons: 0, clickCount: 1 })
|
|
1576
|
+
.catch(() => { });
|
|
1577
|
+
return { clicked, failedAt: index, error: String(error instanceof Error ? error.message : error) };
|
|
1578
|
+
}
|
|
1579
|
+
if (index < points.length - 1 && gap > 0)
|
|
1580
|
+
await new Promise(resolve => setTimeout(resolve, gap));
|
|
1581
|
+
}
|
|
1582
|
+
return { clicked };
|
|
1583
|
+
}
|
|
1526
1584
|
/**
|
|
1527
1585
|
* Type into the focused element, or focus a located element (css/text/xpath)
|
|
1528
1586
|
* first and then insert the text.
|
|
@@ -2574,13 +2632,18 @@ export class ElectronBrowserProvider {
|
|
|
2574
2632
|
// can produce JPEG (CDP JPEG hangs on Electron 43).
|
|
2575
2633
|
const capturable = handle;
|
|
2576
2634
|
if (request?.fullPage !== true && typeof capturable.capture === 'function') {
|
|
2577
|
-
// Ensure the target view is the visible one before capturing.
|
|
2578
|
-
|
|
2635
|
+
// Ensure the target view is the visible one before capturing — unless quiet mode is on.
|
|
2636
|
+
// There, presenting the view is exactly what the user asked to avoid (it restores and
|
|
2637
|
+
// raises the window); the host switches the visible tab inside the window and, if the
|
|
2638
|
+
// window has no surface to capture, falls through to CDP.
|
|
2639
|
+
if (!this.isQuiet())
|
|
2640
|
+
this.showActive(s);
|
|
2579
2641
|
const timeoutMs = 30_000;
|
|
2580
2642
|
const shot = await withTimeout(capturable.capture({
|
|
2581
2643
|
...format === 'jpeg' ? { format, quality: request?.quality } : {},
|
|
2582
2644
|
...request?.maxWidth !== undefined ? { maxWidth: request.maxWidth } : {},
|
|
2583
2645
|
...request?.maxHeight !== undefined ? { maxHeight: request.maxHeight } : {},
|
|
2646
|
+
quiet: this.isQuiet(),
|
|
2584
2647
|
}), timeoutMs, signal, `browser: screenshot timed out after ${timeoutMs}ms`);
|
|
2585
2648
|
if (shot.base64 === '') {
|
|
2586
2649
|
throw new BrowserError('browser: capture returned an empty image (view not painted); retry shortly', 'BROWSER_SCREENSHOT_FAILED');
|
|
@@ -3031,6 +3094,16 @@ export class ElectronBrowserProvider {
|
|
|
3031
3094
|
if (!reported)
|
|
3032
3095
|
process.stderr.write(`[dsh-browser] user action failed: ${message}\n`);
|
|
3033
3096
|
}
|
|
3097
|
+
/**
|
|
3098
|
+
* Whether the operator asked for quiet mode (`ui.quiet`).
|
|
3099
|
+
*
|
|
3100
|
+
* Read live on every call rather than captured at construction: the panel writes the document
|
|
3101
|
+
* and the next operation is expected to honour it, without a restart.
|
|
3102
|
+
* @returns true when the carrier must present views without taking the screen over.
|
|
3103
|
+
*/
|
|
3104
|
+
isQuiet() {
|
|
3105
|
+
return this.settingsSource?.().ui.quiet === true;
|
|
3106
|
+
}
|
|
3034
3107
|
/**
|
|
3035
3108
|
* Ask the host to show the active tab's view, carrying the session label.
|
|
3036
3109
|
*
|
|
@@ -3043,7 +3116,7 @@ export class ElectronBrowserProvider {
|
|
|
3043
3116
|
* silently desynchronising the two and failing every screenshot.
|
|
3044
3117
|
*/
|
|
3045
3118
|
showActive(s) {
|
|
3046
|
-
this.host.showView?.(this.activeTab(s).handle, s.label);
|
|
3119
|
+
this.host.showView?.(this.activeTab(s).handle, s.label, { quiet: this.isQuiet() });
|
|
3047
3120
|
const autoExpand = this.settingsSource === undefined || this.settingsSource().ui.autoExpandOnce;
|
|
3048
3121
|
if (autoExpand)
|
|
3049
3122
|
return;
|