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,195 @@
|
|
|
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
|
+
|
|
35
|
+
import { internals } from './remote-host.js'
|
|
36
|
+
|
|
37
|
+
/** How long a background provision may run before it is abandoned. */
|
|
38
|
+
export const PROVISION_TIMEOUT_MS = 180_000
|
|
39
|
+
|
|
40
|
+
/** What happened, in terms a log line can carry. */
|
|
41
|
+
export interface ProvisionResult {
|
|
42
|
+
/** `present` — nothing to do; `ready` — provisioned now; `skipped` — opted out; `failed`. */
|
|
43
|
+
readonly status: 'present' | 'ready' | 'skipped' | 'failed'
|
|
44
|
+
/** One line for the log; never contains a stack trace a user has to decode. */
|
|
45
|
+
readonly detail: string
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Everything this module touches from the outside, so a test can drive it without Electron. */
|
|
49
|
+
export interface ProvisionDeps {
|
|
50
|
+
/** Whether a spawnable binary already exists. */
|
|
51
|
+
readonly hasBinary: () => boolean
|
|
52
|
+
/** The installed electron package's own installer script, or undefined when it is not there. */
|
|
53
|
+
readonly installerScript: () => string | undefined
|
|
54
|
+
/** Runs the installer; resolves to an exit code, or rejects/returns null on failure. */
|
|
55
|
+
readonly run: (script: string) => Promise<{ code: number | null; detail?: string }>
|
|
56
|
+
/** Where the outcome goes. */
|
|
57
|
+
readonly log: (message: string) => void
|
|
58
|
+
/** Opt-out honoured exactly like the install script honours it. */
|
|
59
|
+
readonly skip: () => boolean
|
|
60
|
+
/** Budget for one provision. */
|
|
61
|
+
readonly timeoutMs: number
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Whether a spawnable Electron binary is already available.
|
|
66
|
+
*
|
|
67
|
+
* Resolved through the same probe the host uses, so this can never disagree with what a launch
|
|
68
|
+
* would find: a second, cheaper-looking check would be the classic way to report "ready" and then
|
|
69
|
+
* fail at spawn time.
|
|
70
|
+
* @returns true when a launch would find a binary.
|
|
71
|
+
*/
|
|
72
|
+
export function hasElectronRuntime(): boolean {
|
|
73
|
+
try {
|
|
74
|
+
internals.resolveElectronPath()
|
|
75
|
+
return true
|
|
76
|
+
} catch {
|
|
77
|
+
return false
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* The installed electron package's installer script.
|
|
83
|
+
*
|
|
84
|
+
* `require.resolve('electron/package.json')` is used rather than `require('electron')`: inside an
|
|
85
|
+
* Electron main process that module name is the built-in API, and requiring the package would
|
|
86
|
+
* download the binary as a side effect of asking about it.
|
|
87
|
+
* @returns the path to the dependency's own install script, or undefined.
|
|
88
|
+
*/
|
|
89
|
+
function electronInstallerScript(): string | undefined {
|
|
90
|
+
try {
|
|
91
|
+
const require = createRequire(import.meta.url)
|
|
92
|
+
const manifestPath = require.resolve('electron/package.json')
|
|
93
|
+
const script = join(dirname(manifestPath), 'install.js')
|
|
94
|
+
return existsSync(script) ? script : undefined
|
|
95
|
+
} catch {
|
|
96
|
+
return undefined
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** The Electron version the installed package asks for, for a log line that names the download. */
|
|
101
|
+
function electronVersion(): string {
|
|
102
|
+
try {
|
|
103
|
+
const require = createRequire(import.meta.url)
|
|
104
|
+
const manifest = JSON.parse(readFileSync(require.resolve('electron/package.json'), 'utf8')) as { version?: unknown }
|
|
105
|
+
return typeof manifest.version === 'string' ? manifest.version : 'unknown'
|
|
106
|
+
} catch {
|
|
107
|
+
return 'unknown'
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** The real dependencies: this process's spawn, fs and logger. */
|
|
112
|
+
export function defaultDeps(log: (message: string) => void = () => {}): ProvisionDeps {
|
|
113
|
+
return {
|
|
114
|
+
hasBinary: hasElectronRuntime,
|
|
115
|
+
installerScript: electronInstallerScript,
|
|
116
|
+
run: async script => {
|
|
117
|
+
const child = spawn(process.execPath, [script], {
|
|
118
|
+
env: { ...process.env, ELECTRON_RUN_AS_NODE: '1' },
|
|
119
|
+
stdio: ['ignore', 'ignore', 'ignore'],
|
|
120
|
+
windowsHide: true,
|
|
121
|
+
})
|
|
122
|
+
return await new Promise(resolve => {
|
|
123
|
+
child.once('error', error => resolve({ code: null, detail: String(error) }))
|
|
124
|
+
child.once('close', code => resolve({ code, detail: undefined }))
|
|
125
|
+
})
|
|
126
|
+
},
|
|
127
|
+
log,
|
|
128
|
+
skip: () => typeof process.env.ELECTRON_SKIP_BINARY_DOWNLOAD === 'string'
|
|
129
|
+
&& process.env.ELECTRON_SKIP_BINARY_DOWNLOAD !== '',
|
|
130
|
+
timeoutMs: PROVISION_TIMEOUT_MS,
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Provision the runtime once per process, when it is missing.
|
|
136
|
+
*
|
|
137
|
+
* Concurrent callers share the first attempt: two tool calls arriving together must not start two
|
|
138
|
+
* downloads into the same directory.
|
|
139
|
+
* @param deps - the effects, defaulted to this process's.
|
|
140
|
+
* @returns what happened. Never throws.
|
|
141
|
+
*/
|
|
142
|
+
export async function ensureElectronRuntime(deps: ProvisionDeps = defaultDeps()): Promise<ProvisionResult> {
|
|
143
|
+
if (deps.hasBinary()) return { status: 'present', detail: 'an Electron runtime is already available' }
|
|
144
|
+
if (deps.skip()) return { status: 'skipped', detail: 'ELECTRON_SKIP_BINARY_DOWNLOAD is set' }
|
|
145
|
+
const script = deps.installerScript()
|
|
146
|
+
if (script === undefined) {
|
|
147
|
+
return { status: 'failed', detail: 'the installed electron package has no install.js; reinstall the plugin in this profile' }
|
|
148
|
+
}
|
|
149
|
+
deps.log(`preparing Electron ${electronVersion()} in the background (a missing runtime needs a download)`)
|
|
150
|
+
const timeout = new Promise<{ code: number | null; detail: string }>(resolve => {
|
|
151
|
+
setTimeout(() => resolve({ code: null, detail: `gave up after ${Math.round(deps.timeoutMs / 1000)}s` }), deps.timeoutMs).unref?.()
|
|
152
|
+
})
|
|
153
|
+
let outcome: { code: number | null; detail?: string }
|
|
154
|
+
try {
|
|
155
|
+
outcome = await Promise.race([deps.run(script), timeout])
|
|
156
|
+
} catch (error) {
|
|
157
|
+
return { status: 'failed', detail: `the download failed: ${String(error)}` }
|
|
158
|
+
}
|
|
159
|
+
if (outcome.code !== 0) {
|
|
160
|
+
return {
|
|
161
|
+
status: 'failed',
|
|
162
|
+
detail: `Electron provisioning did not finish (${outcome.detail ?? `exit ${String(outcome.code)}`}); `
|
|
163
|
+
+ 'the browser still works through a sidebar, Chrome or Edge, and `npx --no dsh-browser-install-electron` retries it',
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return { status: 'ready', detail: 'the Electron runtime is ready' }
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** One attempt per process, shared by every caller. */
|
|
170
|
+
let attempt: Promise<ProvisionResult> | undefined
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Start (or join) the single background attempt.
|
|
174
|
+
*
|
|
175
|
+
* Returns immediately with the shared promise, so a caller that does not care can ignore it and one
|
|
176
|
+
* that does can await it — the returned promise never rejects.
|
|
177
|
+
* @param log - where the outcome goes.
|
|
178
|
+
* @param deps - the effects; defaulted to this process's, overridable so a test can drive it.
|
|
179
|
+
* @returns the shared attempt.
|
|
180
|
+
*/
|
|
181
|
+
export function provisionElectronInBackground(
|
|
182
|
+
log: (message: string) => void,
|
|
183
|
+
deps: ProvisionDeps = defaultDeps(log),
|
|
184
|
+
): Promise<ProvisionResult> {
|
|
185
|
+
attempt ??= ensureElectronRuntime(deps).then(result => {
|
|
186
|
+
log(`electron runtime: ${result.status} — ${result.detail}`)
|
|
187
|
+
return result
|
|
188
|
+
})
|
|
189
|
+
return attempt
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** Forget the attempt. Tests only: a real process provisions at most once. */
|
|
193
|
+
export function resetProvisionStateForTest(): void {
|
|
194
|
+
attempt = undefined
|
|
195
|
+
}
|
|
@@ -15,6 +15,7 @@ import type { IncomingMessage, ServerResponse } from 'node:http'
|
|
|
15
15
|
import z from '@deepseek-ai/schemastery'
|
|
16
16
|
import type { BrowserRuntime } from '../browser/runtime.js'
|
|
17
17
|
import { ElectronBrowserProvider } from './provider.js'
|
|
18
|
+
import { provisionElectronInBackground } from './electron-provision.js'
|
|
18
19
|
import type { ElectronBrowserViewHost } from './provider.js'
|
|
19
20
|
import type { CarrierLossReason } from '../browser/types.js'
|
|
20
21
|
import { randomUUID } from 'node:crypto'
|
|
@@ -153,6 +154,17 @@ export function apply(ctx: Context & { browser: BrowserRuntime }, config: Config
|
|
|
153
154
|
// different lifetimes (a spawned child vs. the shell's view), and pretending
|
|
154
155
|
// otherwise would leave a stray window behind.
|
|
155
156
|
const selfHosted = new RemoteElectronViewHost(defaultHostMainPath())
|
|
157
|
+
// The self-hosted carrier is the only one that needs Electron, and it is prepared HERE, lazily,
|
|
158
|
+
// rather than during package installation (issue #41: a `postinstall` download made a default
|
|
159
|
+
// pnpm 11 profile refuse the install outright, and a failed fetch failed the install too).
|
|
160
|
+
//
|
|
161
|
+
// Non-fatal by construction: this never rejects, never blocks a tool call, and its failure leaves
|
|
162
|
+
// the sidebar / Chrome / Edge carriers untouched. Only the two channels that can use the bundled
|
|
163
|
+
// runtime are worth the download; an explicit chrome/edge choice is not.
|
|
164
|
+
const wantedChannel = settings.get().browser.channel
|
|
165
|
+
if (wantedChannel === 'bundled' || wantedChannel === 'auto') {
|
|
166
|
+
void provisionElectronInBackground(message => ctx.logger?.info?.(`dsh-builtin-browser: ${message}`))
|
|
167
|
+
}
|
|
156
168
|
let unregister = ctx.browser.registerBrowserProvider(new ElectronBrowserProvider(selfHosted, providerConfig))
|
|
157
169
|
let disposed = false
|
|
158
170
|
let carrierChoice = 0
|
|
@@ -565,8 +565,17 @@ function updateWindowTitle(win: HostWindow, viewId: string | undefined, label: s
|
|
|
565
565
|
} catch { /* destroyed */ }
|
|
566
566
|
}
|
|
567
567
|
|
|
568
|
-
/**
|
|
569
|
-
|
|
568
|
+
/**
|
|
569
|
+
* Make one view the visible (topmost) one WITHIN its window, leaving the window itself alone.
|
|
570
|
+
*
|
|
571
|
+
* Split out of {@link showViewInWindow} for quiet-mode capture: a screenshot still has to look
|
|
572
|
+
* at the active tab rather than another tab of the same window, but it must not show, restore,
|
|
573
|
+
* raise or focus the window to do it. Everything here is contentView-local.
|
|
574
|
+
* @param win - the window holding the view.
|
|
575
|
+
* @param viewId - the view to reveal.
|
|
576
|
+
* @param label - session/task label, when known, for the window title.
|
|
577
|
+
*/
|
|
578
|
+
function revealViewInWindow(win: HostWindow, viewId: string, label?: string): void {
|
|
570
579
|
const entry = win.views.get(viewId)
|
|
571
580
|
if (entry === undefined) return
|
|
572
581
|
// A navigation replaced this view's renderer, and the new one has no
|
|
@@ -598,11 +607,39 @@ function showViewInWindow(win: HostWindow, viewId: string, label?: string): void
|
|
|
598
607
|
// Always refresh the title: it reflects the CURRENT page of the visible
|
|
599
608
|
// view, which changes as the agent navigates.
|
|
600
609
|
updateWindowTitle(win, viewId, label)
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
/**
|
|
613
|
+
* Make one view the visible one within its window, and — outside quiet mode — raise the window.
|
|
614
|
+
* @param win - the window holding the view.
|
|
615
|
+
* @param viewId - the view to present.
|
|
616
|
+
* @param label - session/task label, when known, for the window title.
|
|
617
|
+
* @param quiet - `ui.quiet`: present without taking the screen over.
|
|
618
|
+
* @param needSurface - quiet mode only: this presentation is the barrier before synthesized
|
|
619
|
+
* input, so the window has to be visible even if the user minimized it. A navigation or a tab
|
|
620
|
+
* switch does not need that and leaves a minimized window alone.
|
|
621
|
+
*/
|
|
622
|
+
function showViewInWindow(win: HostWindow, viewId: string, label?: string, quiet = false, needSurface = false): void {
|
|
623
|
+
revealViewInWindow(win, viewId, label)
|
|
601
624
|
// Raise this session's window so the human sees the page being worked on
|
|
602
625
|
// (moveTop raises without stealing keyboard focus).
|
|
626
|
+
//
|
|
627
|
+
// Quiet mode (`ui.quiet`): the user asked not to be interrupted, so this must not
|
|
628
|
+
// activate the application, steal keyboard focus, or pull the window in front of the one
|
|
629
|
+
// they are using. Chromium drops `Input.*` for a view with no display surface, so the one
|
|
630
|
+
// case that still needs a visible window — the barrier before synthesized input — restores
|
|
631
|
+
// it with `showInactive()`, which shows it without becoming the key window, and never calls
|
|
632
|
+
// `moveTop()`. Everything else (navigation, tab switching, screenshots) leaves a minimized
|
|
633
|
+
// window minimized.
|
|
603
634
|
try {
|
|
604
|
-
if (
|
|
605
|
-
|
|
635
|
+
if (quiet) {
|
|
636
|
+
if (!needSurface) return
|
|
637
|
+
if (win.window.isMinimized()) win.window.restore()
|
|
638
|
+
if (!win.window.isVisible()) win.window.showInactive()
|
|
639
|
+
} else {
|
|
640
|
+
if (!win.window.isVisible()) win.window.show()
|
|
641
|
+
win.window.moveTop()
|
|
642
|
+
}
|
|
606
643
|
} catch { /* closing */ }
|
|
607
644
|
}
|
|
608
645
|
|
|
@@ -619,7 +656,7 @@ function scheduleWindowClose(win: HostWindow): void {
|
|
|
619
656
|
}
|
|
620
657
|
|
|
621
658
|
/** Handle one command. */
|
|
622
|
-
async function handle(op: string, msg: { id: number; viewId?: string; windowId?: string; method?: string; params?: Record<string, unknown>; url?: string; savePath?: string; cookies?: unknown[]; label?: string; format?: string; quality?: number; maxWidth?: number; maxHeight?: number; message?: string }): Promise<void> {
|
|
659
|
+
async function handle(op: string, msg: { id: number; viewId?: string; windowId?: string; method?: string; params?: Record<string, unknown>; url?: string; savePath?: string; cookies?: unknown[]; label?: string; format?: string; quality?: number; maxWidth?: number; maxHeight?: number; message?: string; quiet?: boolean; needSurface?: boolean }): Promise<void> {
|
|
623
660
|
try {
|
|
624
661
|
switch (op) {
|
|
625
662
|
case 'ping':
|
|
@@ -741,7 +778,7 @@ async function handle(op: string, msg: { id: number; viewId?: string; windowId?:
|
|
|
741
778
|
const viewId = msg.viewId
|
|
742
779
|
if (viewId === undefined) throw new Error('showView missing viewId')
|
|
743
780
|
const win = windowOfView(viewId)
|
|
744
|
-
if (win !== undefined) showViewInWindow(win, viewId, typeof msg.label === 'string' ? msg.label : undefined)
|
|
781
|
+
if (win !== undefined) showViewInWindow(win, viewId, typeof msg.label === 'string' ? msg.label : undefined, msg.quiet === true, msg.needSurface === true)
|
|
745
782
|
reply(msg.id, { ok: true })
|
|
746
783
|
return
|
|
747
784
|
}
|
|
@@ -811,12 +848,25 @@ async function handle(op: string, msg: { id: number; viewId?: string; windowId?:
|
|
|
811
848
|
const wantQuality = typeof msg.quality === 'number' ? msg.quality : 80
|
|
812
849
|
const maxW = typeof msg.maxWidth === 'number' ? msg.maxWidth : 0
|
|
813
850
|
const maxH = typeof msg.maxHeight === 'number' ? msg.maxHeight : 0
|
|
851
|
+
// Quiet mode: a screenshot is not a reason to take the screen over. Skipping the
|
|
852
|
+
// raise below is what makes a minimized window survive a screenshot: capturePage on
|
|
853
|
+
// a hidden view returns an empty image, and the CDP fallback captures without needing
|
|
854
|
+
// a display surface. The default path keeps the raise, because there a screenshot is
|
|
855
|
+
// how the human follows what the agent is doing.
|
|
856
|
+
const quiet = msg.quiet === true
|
|
814
857
|
let mime = 'image/png'
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
858
|
+
if (quiet) {
|
|
859
|
+
// The active tab has to be the visible one INSIDE the window, or the capture may read
|
|
860
|
+
// another tab. Keep that switch contentView-local: show/restore/focus are precisely
|
|
861
|
+
// what quiet mode forbids.
|
|
862
|
+
revealViewInWindow(win, viewId)
|
|
863
|
+
} else {
|
|
864
|
+
try {
|
|
865
|
+
if (!win.window.isVisible()) win.window.show()
|
|
866
|
+
} catch { /* closing */ }
|
|
867
|
+
try { win.window.restore() } catch { /* not minimized */ }
|
|
868
|
+
win.window.focus()
|
|
869
|
+
}
|
|
820
870
|
let base64 = ''
|
|
821
871
|
let renderedSize: { width: number; height: number } | undefined
|
|
822
872
|
try {
|
|
@@ -1143,7 +1193,7 @@ void app.whenReady().then(() => {
|
|
|
1143
1193
|
rl.on('line', line => {
|
|
1144
1194
|
const text = line.trim()
|
|
1145
1195
|
if (text === '') return
|
|
1146
|
-
let msg: { id: number; op?: string; viewId?: string; windowId?: string; method?: string; params?: Record<string, unknown>; url?: string; savePath?: string; cookies?: unknown[]; label?: string; format?: string; quality?: number; maxWidth?: number; maxHeight?: number; message?: string }
|
|
1196
|
+
let msg: { id: number; op?: string; viewId?: string; windowId?: string; method?: string; params?: Record<string, unknown>; url?: string; savePath?: string; cookies?: unknown[]; label?: string; format?: string; quality?: number; maxWidth?: number; maxHeight?: number; message?: string; quiet?: boolean; needSurface?: boolean }
|
|
1147
1197
|
try {
|
|
1148
1198
|
msg = JSON.parse(text) as typeof msg
|
|
1149
1199
|
} catch {
|
|
@@ -144,8 +144,10 @@ export interface ElectronBrowserViewHost {
|
|
|
144
144
|
* @param label - human-readable session/task label, when the provider knows
|
|
145
145
|
* one; the host may surface it (e.g. in the window title) so a human can
|
|
146
146
|
* tell which task's page is currently visible.
|
|
147
|
+
* @param options - presentation policy; `quiet` asks for a surface without taking
|
|
148
|
+
* over the screen (see {@link PresentOptions}).
|
|
147
149
|
*/
|
|
148
|
-
showView?(handle: ElectronViewHandle, label?: string): void
|
|
150
|
+
showView?(handle: ElectronViewHandle, label?: string, options?: PresentOptions): void
|
|
149
151
|
/**
|
|
150
152
|
* Optional: (re)present a view and WAIT until the host confirms it. Chromium
|
|
151
153
|
* silently drops CDP-synthesized input (`Input.*`) for a view that has no
|
|
@@ -158,8 +160,9 @@ export interface ElectronBrowserViewHost {
|
|
|
158
160
|
* assumed to present every view (headless/probe hosts).
|
|
159
161
|
* @param handle - the handle to present.
|
|
160
162
|
* @param label - human-readable session/task label, as in {@link showView}.
|
|
163
|
+
* @param options - presentation policy, as in {@link showView}.
|
|
161
164
|
*/
|
|
162
|
-
presentView?(handle: ElectronViewHandle, label?: string): Promise<void>
|
|
165
|
+
presentView?(handle: ElectronViewHandle, label?: string, options?: PresentOptions): Promise<void>
|
|
163
166
|
/**
|
|
164
167
|
* Optional: pages this carrier holds that the provider never created.
|
|
165
168
|
*
|
|
@@ -610,12 +613,35 @@ export function toCdpCookie(cookie: ExportedCookie): Record<string, unknown> | u
|
|
|
610
613
|
}
|
|
611
614
|
}
|
|
612
615
|
|
|
616
|
+
/**
|
|
617
|
+
* How the carrier should present a view before it is used.
|
|
618
|
+
*
|
|
619
|
+
* `quiet` is the resolved `ui.quiet` switch. It does not mean "invisible": Chromium drops
|
|
620
|
+
* synthesized `Input.*` for a view without a display surface, so input still needs one. It means
|
|
621
|
+
* the carrier must get that surface without taking the screen over — no application activation
|
|
622
|
+
* and no keyboard focus. Navigation and tab switching only reveal the view inside its window; the
|
|
623
|
+
* pre-input barrier may restore a minimized window (it needs the surface), but even then it shows
|
|
624
|
+
* it inactive instead of focusing or raising it. A host that cannot honour this (a headless probe,
|
|
625
|
+
* the desktop sidebar) simply ignores it.
|
|
626
|
+
*/
|
|
627
|
+
export interface PresentOptions {
|
|
628
|
+
readonly quiet?: boolean
|
|
629
|
+
}
|
|
630
|
+
|
|
613
631
|
/** Native capture options the self-hosted view handle understands. */
|
|
614
632
|
export interface ScreenshotOptions {
|
|
615
633
|
readonly format?: 'png' | 'jpeg'
|
|
616
634
|
readonly quality?: number
|
|
617
635
|
readonly maxWidth?: number
|
|
618
636
|
readonly maxHeight?: number
|
|
637
|
+
/**
|
|
638
|
+
* Capture without presenting the window first (`ui.quiet`).
|
|
639
|
+
*
|
|
640
|
+
* The self-hosted host answers this by skipping its restore/show/focus step: a visible window
|
|
641
|
+
* still captures through `capturePage()`, and a minimized or hidden one falls through to the
|
|
642
|
+
* CDP path, which needs no display surface.
|
|
643
|
+
*/
|
|
644
|
+
readonly quiet?: boolean
|
|
619
645
|
}
|
|
620
646
|
/** CDP method for runtime evaluation (the execute path). */
|
|
621
647
|
export const CDP_RUNTIME_EVALUATE = 'Runtime.evaluate'
|
|
@@ -1198,7 +1224,7 @@ export class ElectronBrowserProvider implements BrowserProvider {
|
|
|
1198
1224
|
const timeoutMs = 10_000
|
|
1199
1225
|
try {
|
|
1200
1226
|
await withTimeout(
|
|
1201
|
-
present(handle, s.label),
|
|
1227
|
+
present(handle, s.label, { quiet: this.isQuiet() }),
|
|
1202
1228
|
timeoutMs,
|
|
1203
1229
|
signal,
|
|
1204
1230
|
`browser: presenting the page view timed out after ${timeoutMs}ms`,
|
|
@@ -3263,14 +3289,18 @@ export class ElectronBrowserProvider implements BrowserProvider {
|
|
|
3263
3289
|
// can produce JPEG (CDP JPEG hangs on Electron 43).
|
|
3264
3290
|
const capturable = handle
|
|
3265
3291
|
if (request?.fullPage !== true && typeof capturable.capture === 'function') {
|
|
3266
|
-
// Ensure the target view is the visible one before capturing.
|
|
3267
|
-
|
|
3292
|
+
// Ensure the target view is the visible one before capturing — unless quiet mode is on.
|
|
3293
|
+
// There, presenting the view is exactly what the user asked to avoid (it restores and
|
|
3294
|
+
// raises the window); the host switches the visible tab inside the window and, if the
|
|
3295
|
+
// window has no surface to capture, falls through to CDP.
|
|
3296
|
+
if (!this.isQuiet()) this.showActive(s)
|
|
3268
3297
|
const timeoutMs = 30_000
|
|
3269
3298
|
const shot = await withTimeout(
|
|
3270
3299
|
capturable.capture({
|
|
3271
3300
|
...format === 'jpeg' ? { format, quality: request?.quality } : {},
|
|
3272
3301
|
...request?.maxWidth !== undefined ? { maxWidth: request.maxWidth } : {},
|
|
3273
3302
|
...request?.maxHeight !== undefined ? { maxHeight: request.maxHeight } : {},
|
|
3303
|
+
quiet: this.isQuiet(),
|
|
3274
3304
|
}),
|
|
3275
3305
|
timeoutMs,
|
|
3276
3306
|
signal,
|
|
@@ -3744,6 +3774,17 @@ export class ElectronBrowserProvider implements BrowserProvider {
|
|
|
3744
3774
|
if (!reported) process.stderr.write(`[dsh-browser] user action failed: ${message}\n`)
|
|
3745
3775
|
}
|
|
3746
3776
|
|
|
3777
|
+
/**
|
|
3778
|
+
* Whether the operator asked for quiet mode (`ui.quiet`).
|
|
3779
|
+
*
|
|
3780
|
+
* Read live on every call rather than captured at construction: the panel writes the document
|
|
3781
|
+
* and the next operation is expected to honour it, without a restart.
|
|
3782
|
+
* @returns true when the carrier must present views without taking the screen over.
|
|
3783
|
+
*/
|
|
3784
|
+
private isQuiet(): boolean {
|
|
3785
|
+
return this.settingsSource?.().ui.quiet === true
|
|
3786
|
+
}
|
|
3787
|
+
|
|
3747
3788
|
/**
|
|
3748
3789
|
* Ask the host to show the active tab's view, carrying the session label.
|
|
3749
3790
|
*
|
|
@@ -3756,7 +3797,7 @@ export class ElectronBrowserProvider implements BrowserProvider {
|
|
|
3756
3797
|
* silently desynchronising the two and failing every screenshot.
|
|
3757
3798
|
*/
|
|
3758
3799
|
private showActive(s: Session): void {
|
|
3759
|
-
this.host.showView?.(this.activeTab(s).handle, s.label)
|
|
3800
|
+
this.host.showView?.(this.activeTab(s).handle, s.label, { quiet: this.isQuiet() })
|
|
3760
3801
|
const autoExpand = this.settingsSource === undefined || this.settingsSource().ui.autoExpandOnce
|
|
3761
3802
|
if (autoExpand) return
|
|
3762
3803
|
// Best effort: a carrier that owns no presentation (self-hosted) simply has none.
|
|
@@ -35,7 +35,7 @@ import { existsSync, readFileSync, readdirSync, readlinkSync } from 'node:fs'
|
|
|
35
35
|
import { basename, dirname, isAbsolute, join } from 'node:path'
|
|
36
36
|
import { createServer, type Server, type Socket } from 'node:net'
|
|
37
37
|
import { fileURLToPath } from 'node:url'
|
|
38
|
-
import type { BrowserUserAction, ElectronBrowserViewHost, ElectronViewHandle, ScreenshotOptions } from './provider.js'
|
|
38
|
+
import type { BrowserUserAction, ElectronBrowserViewHost, ElectronViewHandle, PresentOptions, ScreenshotOptions } from './provider.js'
|
|
39
39
|
import type { ExportedCookie } from '../browser/types.js'
|
|
40
40
|
|
|
41
41
|
/** How long to wait for the child to signal readiness before failing. */
|
|
@@ -139,13 +139,13 @@ function resolveElectronPathImpl(inputs: ElectronPathInputs): string {
|
|
|
139
139
|
if (hostExe !== undefined) return hostExe
|
|
140
140
|
throw new Error(
|
|
141
141
|
'dsh-builtin-browser: cannot locate a spawnable Electron binary. ' +
|
|
142
|
-
'The electron package ships with the plugin
|
|
143
|
-
'
|
|
144
|
-
'
|
|
145
|
-
'
|
|
146
|
-
'
|
|
147
|
-
'
|
|
148
|
-
'
|
|
142
|
+
'The electron package ships with the plugin and its binary is downloaded on first use, not at install time ' +
|
|
143
|
+
'(issue #41: an install-time download made default profiles refuse the plugin). The plugin prepares it ' +
|
|
144
|
+
'automatically when the bundled carrier is selected; to do it by hand, run ' +
|
|
145
|
+
'`npx --no dsh-browser-install-electron` in the profile that installed the plugin, then retry. ' +
|
|
146
|
+
'Alternatively set ELECTRON_PATH to an electron executable. Note: the host\'s own executable cannot be ' +
|
|
147
|
+
'reused when it is a packaged Electron app (e.g. DSH Desktop.exe) — only a real electron binary can be ' +
|
|
148
|
+
'spawned.',
|
|
149
149
|
)
|
|
150
150
|
}
|
|
151
151
|
|
|
@@ -1196,13 +1196,19 @@ export class RemoteElectronViewHost implements ElectronBrowserViewHost {
|
|
|
1196
1196
|
return view
|
|
1197
1197
|
}
|
|
1198
1198
|
|
|
1199
|
-
showView(handle: ElectronViewHandle, label?: string): void {
|
|
1199
|
+
showView(handle: ElectronViewHandle, label?: string, options?: PresentOptions): void {
|
|
1200
1200
|
// Fire-and-forget by design (visibility is best-effort), but a rejected
|
|
1201
1201
|
// promise must not become an unhandled rejection (crash on Node >= 15).
|
|
1202
1202
|
// The label (session/task id) rides along so the child can show it in the
|
|
1203
1203
|
// window title — a human can then tell which task's page is visible.
|
|
1204
|
+
// `quiet` is only sent when it is on: the child's default is the documented
|
|
1205
|
+
// "present and raise" behaviour, so an old child keeps working either way.
|
|
1204
1206
|
void this.ready()
|
|
1205
|
-
.then(() => this.client?.call('showView', {
|
|
1207
|
+
.then(() => this.client?.call('showView', {
|
|
1208
|
+
viewId: handle.id,
|
|
1209
|
+
...label !== undefined ? { label } : {},
|
|
1210
|
+
...options?.quiet === true ? { quiet: true } : {},
|
|
1211
|
+
}))
|
|
1206
1212
|
.catch(() => { /* host unavailable */ })
|
|
1207
1213
|
}
|
|
1208
1214
|
|
|
@@ -1221,7 +1227,7 @@ export class RemoteElectronViewHost implements ElectronBrowserViewHost {
|
|
|
1221
1227
|
* in order: its reply is queued behind the showView and so only arrives once
|
|
1222
1228
|
* the visibility change has been applied.
|
|
1223
1229
|
*/
|
|
1224
|
-
async presentView(handle: ElectronViewHandle, label?: string): Promise<void> {
|
|
1230
|
+
async presentView(handle: ElectronViewHandle, label?: string, options?: PresentOptions): Promise<void> {
|
|
1225
1231
|
await this.ready()
|
|
1226
1232
|
const client = this.client
|
|
1227
1233
|
if (client === undefined) throw new HostGoneError('browser host unavailable')
|
|
@@ -1232,7 +1238,15 @@ export class RemoteElectronViewHost implements ElectronBrowserViewHost {
|
|
|
1232
1238
|
await withTimeout(handle.ensureMaterialized(), PRESENT_TIMEOUT_MS, 'browser host did not materialize the view')
|
|
1233
1239
|
}
|
|
1234
1240
|
await withTimeout(
|
|
1235
|
-
client.call('showView', {
|
|
1241
|
+
client.call('showView', {
|
|
1242
|
+
viewId: handle.id,
|
|
1243
|
+
...label !== undefined ? { label } : {},
|
|
1244
|
+
// `needSurface` tells the child this is the pre-input barrier: under quiet mode this
|
|
1245
|
+
// is the only presentation allowed to un-minimize a window, because Chromium drops
|
|
1246
|
+
// `Input.*` without a display surface. One line below in the child, and only read
|
|
1247
|
+
// there when `quiet` is set.
|
|
1248
|
+
...options?.quiet === true ? { quiet: true, needSurface: true } : {},
|
|
1249
|
+
}),
|
|
1236
1250
|
PRESENT_TIMEOUT_MS,
|
|
1237
1251
|
'browser host did not confirm the view presentation',
|
|
1238
1252
|
)
|
|
@@ -48,6 +48,25 @@ export interface BrowserSettings {
|
|
|
48
48
|
readonly closeWithSession: boolean
|
|
49
49
|
/** Show the synthetic cursor/click feedback while the agent operates. */
|
|
50
50
|
readonly virtualCursor: boolean
|
|
51
|
+
/**
|
|
52
|
+
* Quiet mode: drive the page without taking over the screen.
|
|
53
|
+
*
|
|
54
|
+
* Off (the default) keeps the contract this plugin is built on — human and agent share one
|
|
55
|
+
* window, so an operation raises it and the user can see what is being driven. On, the
|
|
56
|
+
* carrier never activates the application and never steals keyboard focus:
|
|
57
|
+
* - screenshots of a minimized or hidden window go through CDP `Page.captureScreenshot`
|
|
58
|
+
* instead of `capturePage()`, so grabbing an image stops being a reason to show a window;
|
|
59
|
+
* - navigation and tab switching only change which view is visible INSIDE the window; a
|
|
60
|
+
* minimized window stays minimized;
|
|
61
|
+
* - synthesized input still needs a display surface (Chromium drops `Input.*` for a view
|
|
62
|
+
* without one), so that one barrier may restore a minimized window — with
|
|
63
|
+
* `showInactive()`, which shows it without becoming the key window, and without
|
|
64
|
+
* `moveTop()`.
|
|
65
|
+
*
|
|
66
|
+
* Reported as issue #35: a minimized browser window was restored and focused by the next
|
|
67
|
+
* screenshot or click, pulling the user out of whatever they were typing in.
|
|
68
|
+
*/
|
|
69
|
+
readonly quiet: boolean
|
|
51
70
|
}
|
|
52
71
|
/** Visual vs non-visual operation strategy. */
|
|
53
72
|
readonly vision: {
|
|
@@ -111,7 +130,7 @@ export interface BrowserSettings {
|
|
|
111
130
|
export const DEFAULT_SETTINGS: BrowserSettings = {
|
|
112
131
|
history: { enabled: true, maxEntries: 5_000, maxAgeDays: 90 },
|
|
113
132
|
cookies: { persist: true },
|
|
114
|
-
ui: { autoExpandOnce: true, closeWithSession: false, virtualCursor: true },
|
|
133
|
+
ui: { autoExpandOnce: true, closeWithSession: false, virtualCursor: true, quiet: false },
|
|
115
134
|
vision: { strategy: 'auto' },
|
|
116
135
|
// Bundled by default: it needs nothing installed, and on the desktop the carrier
|
|
117
136
|
// is the sidebar anyway. Choosing a system browser is an explicit opt-in because
|
|
@@ -296,6 +315,10 @@ export function resolveSettings(raw: unknown, warn?: (message: string) => void):
|
|
|
296
315
|
autoExpandOnce: bool(ui?.autoExpandOnce, DEFAULT_SETTINGS.ui.autoExpandOnce),
|
|
297
316
|
closeWithSession: bool(ui?.closeWithSession, DEFAULT_SETTINGS.ui.closeWithSession),
|
|
298
317
|
virtualCursor: bool(ui?.virtualCursor, DEFAULT_SETTINGS.ui.virtualCursor),
|
|
318
|
+
// Presentation preference, not a capability gate: a mistyped value keeps the documented
|
|
319
|
+
// default (off) instead of being read as "on", because quiet mode changes how every
|
|
320
|
+
// operation presents itself and should never be the accidental result of a bad edit.
|
|
321
|
+
quiet: bool(ui?.quiet, DEFAULT_SETTINGS.ui.quiet),
|
|
299
322
|
},
|
|
300
323
|
vision: { strategy: vision?.strategy === 'nonVisual' ? 'nonVisual' : DEFAULT_SETTINGS.vision.strategy },
|
|
301
324
|
browser: {
|
|
@@ -87,6 +87,14 @@ declare module 'electron' {
|
|
|
87
87
|
isMinimized(): boolean
|
|
88
88
|
isFocused(): boolean
|
|
89
89
|
show(): void
|
|
90
|
+
/**
|
|
91
|
+
* Show the window without activating the application or taking keyboard focus.
|
|
92
|
+
*
|
|
93
|
+
* Not in the type definitions of older Electron releases, but present since Electron 2 —
|
|
94
|
+
* quiet mode (`ui.quiet`) depends on it to give a view a display surface without stealing
|
|
95
|
+
* the foreground.
|
|
96
|
+
*/
|
|
97
|
+
showInactive(): void
|
|
90
98
|
restore(): void
|
|
91
99
|
focus(): void
|
|
92
100
|
moveTop(): void
|