dsh-builtin-browser 0.5.5 → 0.5.7
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 +143 -1
- package/README.en.md +67 -97
- package/README.md +67 -98
- package/client.js +618 -263
- package/desktop-bridge/install.mjs +122 -33
- package/desktop-bridge/plugin-browser-bridge.js +9 -4
- package/docs/development.md +10 -0
- package/docs/tool-reference.md +20 -1
- package/docs/user-guide.md +16 -4
- package/lib/browser/runtime.d.ts +3 -1
- package/lib/browser/runtime.js +9 -0
- package/lib/browser/types.d.ts +42 -0
- package/lib/browser-electron/authenticated-route.d.ts +27 -0
- package/lib/browser-electron/authenticated-route.js +39 -0
- package/lib/browser-electron/bridge-connection.d.ts +8 -0
- package/lib/browser-electron/bridge-connection.js +8 -0
- package/lib/browser-electron/bridge-endpoint.d.ts +30 -0
- package/lib/browser-electron/bridge-endpoint.js +130 -0
- package/lib/browser-electron/bridge-install.d.ts +48 -0
- package/lib/browser-electron/bridge-install.js +98 -0
- package/lib/browser-electron/bridge-status.js +21 -2
- package/lib/browser-electron/desktop-bridge-host.d.ts +30 -27
- package/lib/browser-electron/desktop-bridge-host.js +71 -50
- package/lib/browser-electron/electron-provision.d.ts +35 -2
- package/lib/browser-electron/electron-provision.js +83 -20
- package/lib/browser-electron/entry.js +144 -7
- package/lib/browser-electron/host-main.js +48 -27
- package/lib/browser-electron/json-reply.d.ts +21 -0
- package/lib/browser-electron/json-reply.js +17 -0
- package/lib/browser-electron/preview-route.js +1 -12
- package/lib/browser-electron/provider.d.ts +9 -2
- package/lib/browser-electron/provider.js +76 -6
- package/lib/browser-electron/remote-host.js +10 -9
- package/lib/browser-electron/runtime-route.d.ts +55 -0
- package/lib/browser-electron/runtime-route.js +66 -0
- package/lib/browser-electron/runtime-status.d.ts +102 -0
- package/lib/browser-electron/runtime-status.js +88 -0
- package/lib/browser-electron/settings-route.d.ts +5 -18
- package/lib/browser-electron/settings-route.js +4 -14
- package/lib/browser-electron/settings-store.d.ts +17 -0
- package/lib/browser-electron/settings-store.js +24 -0
- package/lib/browser-electron/system-browser.d.ts +5 -2
- package/lib/browser-electron/system-browser.js +12 -4
- package/lib/tool-browser/index.js +1041 -4
- package/lib/tool-browser/page-probe.d.ts +86 -0
- package/lib/tool-browser/page-probe.js +460 -0
- package/package.json +5 -3
- package/scripts/consumer-install-check.mjs +72 -0
- package/scripts/verify-runtime-status.mjs +179 -0
- package/skills/dsh-builtin-browser/SKILL.md +64 -0
- package/skills/dsh-builtin-browser/references/batch.md +23 -0
- package/skills/dsh-builtin-browser/references/deliver.md +31 -0
- package/skills/dsh-builtin-browser/references/locate.md +11 -0
- package/skills/dsh-builtin-browser/references/recover.md +11 -0
- package/skills/dsh-builtin-browser/references/verify.md +13 -0
- package/src/browser/runtime.ts +11 -0
- package/src/browser/types.ts +19 -0
- package/src/browser-electron/authenticated-route.ts +56 -0
- package/src/browser-electron/bridge-connection.ts +9 -0
- package/src/browser-electron/bridge-endpoint.ts +146 -0
- package/src/browser-electron/bridge-install.ts +122 -0
- package/src/browser-electron/bridge-status.ts +22 -2
- package/src/browser-electron/desktop-bridge-host.ts +68 -50
- package/src/browser-electron/electron-provision.ts +103 -20
- package/src/browser-electron/entry.ts +141 -7
- package/src/browser-electron/host-main.ts +41 -22
- package/src/browser-electron/json-reply.ts +32 -0
- package/src/browser-electron/preview-route.ts +1 -12
- package/src/browser-electron/provider.ts +90 -6
- package/src/browser-electron/remote-host.ts +10 -9
- package/src/browser-electron/runtime-route.ts +104 -0
- package/src/browser-electron/runtime-status.ts +188 -0
- package/src/browser-electron/settings-route.ts +9 -32
- package/src/browser-electron/settings-store.ts +39 -0
- package/src/browser-electron/system-browser.ts +9 -3
- package/src/tool-browser/index.ts +991 -7
- package/src/tool-browser/page-probe.ts +474 -0
|
@@ -11,7 +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
|
+
import { provisionElectronInBackground, provisionSnapshot, hasElectronRuntime } from './electron-provision.js';
|
|
15
15
|
import { randomUUID } from 'node:crypto';
|
|
16
16
|
import { existsSync, mkdirSync, renameSync, statSync } from 'node:fs';
|
|
17
17
|
import { homedir } from 'node:os';
|
|
@@ -24,7 +24,14 @@ import { claimEphemeralProfile, ephemeralProfileName, sweepAbandonedEphemeralPro
|
|
|
24
24
|
import { MissingSystemBrowserHost } from './missing-system-browser.js';
|
|
25
25
|
import { SettingsStore, settingsPath } from './settings-store.js';
|
|
26
26
|
import { sameOrigin } from './settings-route.js';
|
|
27
|
+
import { authenticatedRoute, isDirectLoopback } from './authenticated-route.js';
|
|
27
28
|
import { handleBridgeStatus, handlePreviewFrame, handlePreviewSessions, BRIDGE_STATUS_ROUTE, PREVIEW_ROUTE, PREVIEW_SESSIONS_ROUTE } from './preview-route.js';
|
|
29
|
+
import { findDesktopInstall, readBridgeStatus } from './bridge-status.js';
|
|
30
|
+
import { readBridgeEndpoint } from './bridge-endpoint.js';
|
|
31
|
+
import { runBridgeAction } from './bridge-install.js';
|
|
32
|
+
import { replyJson } from './json-reply.js';
|
|
33
|
+
import { readRuntimeStatus } from './runtime-status.js';
|
|
34
|
+
import { handleRuntimeProvision, handleRuntimeStatus, RUNTIME_PROVISION_ROUTE, RUNTIME_STATUS_ROUTE } from './runtime-route.js';
|
|
28
35
|
export { ELECTRON_BROWSER_PROVIDER_ID, ElectronBrowserProvider, } from './provider.js';
|
|
29
36
|
export { RemoteElectronViewHost, defaultHostMainPath } from './remote-host.js';
|
|
30
37
|
/** Cordis plugin name used by loader diagnostics. */
|
|
@@ -344,7 +351,7 @@ export function apply(ctx, config) {
|
|
|
344
351
|
}
|
|
345
352
|
// The headless mode rides along so a display-less host (systemd, container, SSH-only server)
|
|
346
353
|
// still gets a working browser instead of "exited immediately after launch" — issue #32.
|
|
347
|
-
adopt(new SystemBrowserViewHost(detected, profileDir, [], persist ? undefined : profileDir, undefined, settings.get().browser.headless), `will drive the installed ${detected.kind} (${detected.path}${persist ? '' : ', ephemeral profile'})`);
|
|
354
|
+
adopt(new SystemBrowserViewHost(detected, profileDir, [], persist ? undefined : profileDir, undefined, settings.get().browser.headless, () => settings.get().ui.quiet), `will drive the installed ${detected.kind} (${detected.path}${persist ? '' : ', ephemeral profile'})`);
|
|
348
355
|
}
|
|
349
356
|
}
|
|
350
357
|
else if (adoptedCarrier !== undefined) {
|
|
@@ -380,7 +387,7 @@ export function apply(ctx, config) {
|
|
|
380
387
|
return;
|
|
381
388
|
let sidebar;
|
|
382
389
|
try {
|
|
383
|
-
sidebar = await DesktopBridgeViewHost.discover(sessionId);
|
|
390
|
+
sidebar = await DesktopBridgeViewHost.discover(sessionId, () => settings.get().ui.quiet);
|
|
384
391
|
}
|
|
385
392
|
catch {
|
|
386
393
|
sidebar = undefined; // discovery is documented not to throw; never let it stop the retries
|
|
@@ -447,13 +454,13 @@ export function apply(ctx, config) {
|
|
|
447
454
|
host.effect(() => host.webServer.register({
|
|
448
455
|
kind: 'exact',
|
|
449
456
|
path: PREVIEW_ROUTE,
|
|
450
|
-
handler: (request, response) => handlePreviewFrame(request, response, settings, browser),
|
|
457
|
+
handler: authenticated(ctx, (request, response) => handlePreviewFrame(request, response, settings, browser)),
|
|
451
458
|
}, 'dsh-builtin-browser: live view frame'));
|
|
452
459
|
// The list the panel needs before it can build a frame URL at all: session ids only.
|
|
453
460
|
host.effect(() => host.webServer.register({
|
|
454
461
|
kind: 'exact',
|
|
455
462
|
path: PREVIEW_SESSIONS_ROUTE,
|
|
456
|
-
handler: (request, response) => handlePreviewSessions(request, response, settings, browser),
|
|
463
|
+
handler: authenticated(ctx, (request, response) => handlePreviewSessions(request, response, settings, browser)),
|
|
457
464
|
}, 'dsh-builtin-browser: live view sessions'));
|
|
458
465
|
});
|
|
459
466
|
}
|
|
@@ -469,6 +476,65 @@ export function apply(ctx, config) {
|
|
|
469
476
|
* exactly like the other two.
|
|
470
477
|
* @param ctx - the plugin context.
|
|
471
478
|
*/
|
|
479
|
+
/** Where the panel asks to install or remove the bridge. */
|
|
480
|
+
const BRIDGE_ACTION_ROUTE = '/dsh-builtin-browser/bridge-action';
|
|
481
|
+
let bridgeActionRunning = false;
|
|
482
|
+
/**
|
|
483
|
+
* Install, repair or remove the bridge, from a button.
|
|
484
|
+
*
|
|
485
|
+
* The panel used to print a command. This step closes and reopens the desktop app, so a
|
|
486
|
+
* half-finished manual run leaves the shell in a state the user then has to diagnose — the
|
|
487
|
+
* packaged installer already handles the whole sequence unattended, and this hands it the one
|
|
488
|
+
* decision that is not its own: which action the user pressed.
|
|
489
|
+
* @param request - the incoming request (POST, JSON body `{action}`).
|
|
490
|
+
* @param response - the response to write.
|
|
491
|
+
*/
|
|
492
|
+
async function handleBridgeAction(request, response) {
|
|
493
|
+
if (request.method !== 'POST') {
|
|
494
|
+
replyJson(response, 405, { error: 'method-not-allowed' });
|
|
495
|
+
return;
|
|
496
|
+
}
|
|
497
|
+
// A write: it stops the shell. Same admission as a settings write, not the read rule.
|
|
498
|
+
if (!sameOrigin(request, true)) {
|
|
499
|
+
replyJson(response, 403, { error: 'not-same-origin' });
|
|
500
|
+
return;
|
|
501
|
+
}
|
|
502
|
+
if (!isDirectLoopback(request)) {
|
|
503
|
+
replyJson(response, 403, { error: 'local-request-required' });
|
|
504
|
+
return;
|
|
505
|
+
}
|
|
506
|
+
let action;
|
|
507
|
+
try {
|
|
508
|
+
const body = await readBody(request);
|
|
509
|
+
action = JSON.parse(body === '' ? '{}' : body).action;
|
|
510
|
+
}
|
|
511
|
+
catch (error) {
|
|
512
|
+
replyJson(response, 400, { ok: false, error: `unreadable body: ${String(error)}` });
|
|
513
|
+
return;
|
|
514
|
+
}
|
|
515
|
+
if (action !== 'install' && action !== 'uninstall') {
|
|
516
|
+
replyJson(response, 400, { ok: false, error: 'action must be "install" or "uninstall"' });
|
|
517
|
+
return;
|
|
518
|
+
}
|
|
519
|
+
const root = findDesktopInstall();
|
|
520
|
+
if (root === undefined) {
|
|
521
|
+
// Nothing to patch: say so instead of running an installer against a guess.
|
|
522
|
+
replyJson(response, 409, { ok: false, error: 'no DSH Desktop install was found on this machine' });
|
|
523
|
+
return;
|
|
524
|
+
}
|
|
525
|
+
if (bridgeActionRunning) {
|
|
526
|
+
replyJson(response, 409, { ok: false, error: 'bridge-action-in-progress' });
|
|
527
|
+
return;
|
|
528
|
+
}
|
|
529
|
+
bridgeActionRunning = true;
|
|
530
|
+
try {
|
|
531
|
+
const result = await runBridgeAction(action, root);
|
|
532
|
+
replyJson(response, 200, { ok: result.ok, action, code: result.code, output: result.output, command: result.command });
|
|
533
|
+
}
|
|
534
|
+
finally {
|
|
535
|
+
bridgeActionRunning = false;
|
|
536
|
+
}
|
|
537
|
+
}
|
|
472
538
|
function installBridgeStatusRoute(ctx) {
|
|
473
539
|
const injectStatus = ctx.inject;
|
|
474
540
|
if (typeof injectStatus !== 'function')
|
|
@@ -478,8 +544,15 @@ export function apply(ctx, config) {
|
|
|
478
544
|
host.effect(() => host.webServer.register({
|
|
479
545
|
kind: 'exact',
|
|
480
546
|
path: BRIDGE_STATUS_ROUTE,
|
|
481
|
-
handler: (request, response) => handleBridgeStatus(request, response),
|
|
547
|
+
handler: authenticated(ctx, (request, response) => handleBridgeStatus(request, response)),
|
|
482
548
|
}, 'dsh-builtin-browser: bridge status'));
|
|
549
|
+
// The same information, one action later: install/repair or remove the bridge without the
|
|
550
|
+
// user opening a terminal. Admitted as a write, because it closes and restarts the shell.
|
|
551
|
+
host.effect(() => host.webServer.register({
|
|
552
|
+
kind: 'exact',
|
|
553
|
+
path: BRIDGE_ACTION_ROUTE,
|
|
554
|
+
handler: authenticated(ctx, (request, response) => handleBridgeAction(request, response)),
|
|
555
|
+
}, 'dsh-builtin-browser: bridge action'));
|
|
483
556
|
});
|
|
484
557
|
}
|
|
485
558
|
catch {
|
|
@@ -487,6 +560,62 @@ export function apply(ctx, config) {
|
|
|
487
560
|
}
|
|
488
561
|
}
|
|
489
562
|
installSettingsRoute(ctx, settings, () => { chooseCarrier(); });
|
|
563
|
+
/**
|
|
564
|
+
* Expose readiness — "can the browser serve a call right now" — for the settings panel.
|
|
565
|
+
*
|
|
566
|
+
* The panel had switches for every behaviour and no answer to the one question a new install
|
|
567
|
+
* raises: the bundled runtime is downloaded on first need (issue #41 moved it out of `postinstall`
|
|
568
|
+
* for good reasons), so for the first minute of a fresh install the honest state is "preparing",
|
|
569
|
+
* and until this route existed that state was a log line no panel could read. Both readings are
|
|
570
|
+
* cheap and side-effect free: a path probe for the runtime, an install-tree inspection for the
|
|
571
|
+
* bridge — the same two the launch path and the bridge-status route already use.
|
|
572
|
+
*
|
|
573
|
+
* Skipped, like its siblings, on a surface with no web server: a CLI has no panel to show it to.
|
|
574
|
+
* @param ctx - the plugin context.
|
|
575
|
+
* @param settings - the settings document, for the channel being honoured.
|
|
576
|
+
*/
|
|
577
|
+
function installRuntimeStatusRoute(ctx, settings) {
|
|
578
|
+
const injectRuntime = ctx.inject;
|
|
579
|
+
if (typeof injectRuntime !== 'function')
|
|
580
|
+
return;
|
|
581
|
+
const deps = {
|
|
582
|
+
report: () => readRuntimeStatus({
|
|
583
|
+
channel: () => settings.get().browser.channel,
|
|
584
|
+
hasBinary: hasElectronRuntime,
|
|
585
|
+
provision: provisionSnapshot,
|
|
586
|
+
bridge: () => {
|
|
587
|
+
const root = findDesktopInstall();
|
|
588
|
+
return root === undefined ? undefined : readBridgeStatus(root);
|
|
589
|
+
},
|
|
590
|
+
// Installed is not running: the install tree survives a reboot, the endpoint does not. Both
|
|
591
|
+
// readings are cheap and side-effect free — a file read and a signal-0 pid check.
|
|
592
|
+
endpoint: () => readBridgeEndpoint(),
|
|
593
|
+
}),
|
|
594
|
+
// `force` is what makes the panel's retry button mean anything: without it the answer would be
|
|
595
|
+
// the settled failure the user is trying to replace. An attempt still running is joined.
|
|
596
|
+
provision: () => {
|
|
597
|
+
void provisionElectronInBackground(message => ctx.logger?.info?.(`dsh-builtin-browser: ${message}`), undefined, { force: true });
|
|
598
|
+
return provisionSnapshot();
|
|
599
|
+
},
|
|
600
|
+
};
|
|
601
|
+
try {
|
|
602
|
+
injectRuntime(['webServer'], (host) => {
|
|
603
|
+
host.effect(() => host.webServer.register({
|
|
604
|
+
kind: 'exact',
|
|
605
|
+
path: RUNTIME_STATUS_ROUTE,
|
|
606
|
+
handler: authenticated(ctx, (request, response) => handleRuntimeStatus(request, response, deps)),
|
|
607
|
+
}, 'dsh-builtin-browser: runtime status'));
|
|
608
|
+
host.effect(() => host.webServer.register({
|
|
609
|
+
kind: 'exact',
|
|
610
|
+
path: RUNTIME_PROVISION_ROUTE,
|
|
611
|
+
handler: authenticated(ctx, (request, response) => handleRuntimeProvision(request, response, deps)),
|
|
612
|
+
}, 'dsh-builtin-browser: runtime provision'));
|
|
613
|
+
});
|
|
614
|
+
}
|
|
615
|
+
catch {
|
|
616
|
+
// No web server: the panel is unreachable, and the readiness report has no reader.
|
|
617
|
+
}
|
|
618
|
+
}
|
|
490
619
|
/**
|
|
491
620
|
* Re-run the carrier choice when the settings FILE changes on disk.
|
|
492
621
|
*
|
|
@@ -536,6 +665,7 @@ export function apply(ctx, config) {
|
|
|
536
665
|
watchSettingsFile();
|
|
537
666
|
installPreviewRoute(ctx, settings);
|
|
538
667
|
installBridgeStatusRoute(ctx);
|
|
668
|
+
installRuntimeStatusRoute(ctx, settings);
|
|
539
669
|
}
|
|
540
670
|
/** Route the settings panel reads and writes. */
|
|
541
671
|
const SETTINGS_ROUTE = '/dsh-builtin-browser/settings';
|
|
@@ -581,6 +711,13 @@ export function adoptLegacyProfileRoot(profileRoot, legacyRoot = join(homedir(),
|
|
|
581
711
|
// Nothing here is worth failing an apply over; the fallback is the pre-existing behaviour.
|
|
582
712
|
}
|
|
583
713
|
}
|
|
714
|
+
/** Bind the host's signed browser-session authentication to a plugin route. */
|
|
715
|
+
function authenticated(ctx, handler) {
|
|
716
|
+
return authenticatedRoute(() => {
|
|
717
|
+
const surface = ctx;
|
|
718
|
+
return surface.get('connection');
|
|
719
|
+
}, handler);
|
|
720
|
+
}
|
|
584
721
|
/**
|
|
585
722
|
* Expose the settings document over the host's web server, which is how the
|
|
586
723
|
* settings panel reads and writes it. A host without that service (headless /
|
|
@@ -598,7 +735,7 @@ function installSettingsRoute(ctx, settings, onChannelChange) {
|
|
|
598
735
|
host.effect(() => host.webServer.register({
|
|
599
736
|
kind: 'exact',
|
|
600
737
|
path: SETTINGS_ROUTE,
|
|
601
|
-
handler: (request, response) => handleSettingsRequest(request, response, settings, onChannelChange),
|
|
738
|
+
handler: authenticated(ctx, (request, response) => handleSettingsRequest(request, response, settings, onChannelChange)),
|
|
602
739
|
}, 'dsh-builtin-browser: settings'));
|
|
603
740
|
});
|
|
604
741
|
}
|
|
@@ -362,7 +362,12 @@ function windowFor(windowId) {
|
|
|
362
362
|
const existing = windows.get(key);
|
|
363
363
|
if (existing !== undefined)
|
|
364
364
|
return existing;
|
|
365
|
-
|
|
365
|
+
// `show: false` on purpose: creating a window must not be a presentation decision. With `show: true`
|
|
366
|
+
// the window appeared and took the screen the moment the first tool call created it — before any
|
|
367
|
+
// `showView` message could carry `ui.quiet`, so quiet mode had no effect at all on this carrier
|
|
368
|
+
// (measured: the setting was on and the window still popped up). The first presentation decides:
|
|
369
|
+
// quiet shows it inactive, a normal call shows it normally.
|
|
370
|
+
const w = new BrowserWindow({ width: 1400, height: 900, show: false, title: 'dsh-browser' });
|
|
366
371
|
w.on('closed', () => {
|
|
367
372
|
const closing = windows.get(key);
|
|
368
373
|
windows.delete(key);
|
|
@@ -595,35 +600,46 @@ function revealViewInWindow(win, viewId, label) {
|
|
|
595
600
|
* @param label - session/task label, when known, for the window title.
|
|
596
601
|
* @param quiet - `ui.quiet`: present without taking the screen over.
|
|
597
602
|
* @param needSurface - quiet mode only: this presentation is the barrier before synthesized
|
|
598
|
-
* input, so
|
|
599
|
-
*
|
|
603
|
+
* input, so a newly created window needs an invisible display surface. A minimized window
|
|
604
|
+
* stays minimized because restoring it can activate it.
|
|
600
605
|
*/
|
|
601
606
|
function showViewInWindow(win, viewId, label, quiet = false, needSurface = false) {
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
// window minimized.
|
|
613
|
-
try {
|
|
614
|
-
if (quiet) {
|
|
615
|
-
if (!needSurface)
|
|
607
|
+
if (quiet) {
|
|
608
|
+
if (win.window.isMinimized()) {
|
|
609
|
+
if (needSurface)
|
|
610
|
+
throw new Error('quiet mode will not restore a minimized browser window; restore it manually to send input');
|
|
611
|
+
return;
|
|
612
|
+
}
|
|
613
|
+
if (win.window.isVisible() && win.window.getOpacity() > 0) {
|
|
614
|
+
if (win.visibleViewId !== viewId) {
|
|
615
|
+
if (needSurface)
|
|
616
|
+
throw new Error('quiet mode will not replace a visible browser tab; select the target tab manually to send input');
|
|
616
617
|
return;
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
win.window.showInactive();
|
|
618
|
+
}
|
|
619
|
+
revealViewInWindow(win, viewId, label);
|
|
620
|
+
return;
|
|
621
621
|
}
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
622
|
+
revealViewInWindow(win, viewId, label);
|
|
623
|
+
if (!needSurface || win.window.isVisible())
|
|
624
|
+
return;
|
|
625
|
+
win.window.setOpacity(0);
|
|
626
|
+
if (win.window.getOpacity() > 0)
|
|
627
|
+
throw new Error('quiet mode could not create an invisible browser surface');
|
|
628
|
+
win.window.setSkipTaskbar(true);
|
|
629
|
+
win.window.setFocusable(false);
|
|
630
|
+
win.window.showInactive();
|
|
631
|
+
return;
|
|
632
|
+
}
|
|
633
|
+
revealViewInWindow(win, viewId, label);
|
|
634
|
+
try {
|
|
635
|
+
if (win.window.getOpacity() === 0) {
|
|
636
|
+
win.window.setFocusable(true);
|
|
637
|
+
win.window.setOpacity(1);
|
|
638
|
+
win.window.setSkipTaskbar(false);
|
|
626
639
|
}
|
|
640
|
+
if (!win.window.isVisible())
|
|
641
|
+
win.window.show();
|
|
642
|
+
win.window.moveTop();
|
|
627
643
|
}
|
|
628
644
|
catch { /* closing */ }
|
|
629
645
|
}
|
|
@@ -839,7 +855,11 @@ async function handle(op, msg) {
|
|
|
839
855
|
// and `document.hasFocus()` inside the page even read true.
|
|
840
856
|
const contents = entry.webContentsView.webContents;
|
|
841
857
|
const hadWebFocus = contents.isFocused();
|
|
842
|
-
|
|
858
|
+
if (msg.quiet === true && win.window.getOpacity() > 0 && !hadWebFocus) {
|
|
859
|
+
throw new Error('quiet mode will not take keyboard focus from the visible browser; focus the target page manually');
|
|
860
|
+
}
|
|
861
|
+
if (msg.quiet !== true || !hadWebFocus)
|
|
862
|
+
contents.focus();
|
|
843
863
|
// Focus lands asynchronously, and a key dispatched in the same turn as
|
|
844
864
|
// the request is still dropped, so hold the reply until the renderer can
|
|
845
865
|
// take input. Only on the call where focus actually had to move.
|
|
@@ -880,7 +900,8 @@ async function handle(op, msg) {
|
|
|
880
900
|
// The active tab has to be the visible one INSIDE the window, or the capture may read
|
|
881
901
|
// another tab. Keep that switch contentView-local: show/restore/focus are precisely
|
|
882
902
|
// what quiet mode forbids.
|
|
883
|
-
|
|
903
|
+
if (!win.window.isVisible() || win.window.getOpacity() === 0)
|
|
904
|
+
revealViewInWindow(win, viewId);
|
|
884
905
|
}
|
|
885
906
|
else {
|
|
886
907
|
try {
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One JSON reply, written the same way by every route this plugin serves.
|
|
3
|
+
*
|
|
4
|
+
* Its own module rather than a helper inside one route file: there are four endpoints now, and the
|
|
5
|
+
* second one that needed this had to import it from the live-view route — which reads as though the
|
|
6
|
+
* readiness endpoints belong to the preview. They do not; only the header rules are shared.
|
|
7
|
+
*
|
|
8
|
+
* The header set is the substance, not boilerplate:
|
|
9
|
+
* - `content-length` computed from the bytes actually written, so a proxy cannot truncate;
|
|
10
|
+
* - `no-store` because every answer here is a reading of the current moment;
|
|
11
|
+
* - `nosniff` + a same-origin resource policy, because these bodies name paths and pids.
|
|
12
|
+
* @module dsh-browser/browser-electron/json-reply
|
|
13
|
+
*/
|
|
14
|
+
import type { ServerResponse } from 'node:http';
|
|
15
|
+
/**
|
|
16
|
+
* Write one JSON body with no caching and no sniffing.
|
|
17
|
+
* @param response - the response to write.
|
|
18
|
+
* @param status - the HTTP status.
|
|
19
|
+
* @param body - anything JSON-serialisable.
|
|
20
|
+
*/
|
|
21
|
+
export declare function replyJson(response: ServerResponse, status: number, body: unknown): void;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Write one JSON body with no caching and no sniffing.
|
|
3
|
+
* @param response - the response to write.
|
|
4
|
+
* @param status - the HTTP status.
|
|
5
|
+
* @param body - anything JSON-serialisable.
|
|
6
|
+
*/
|
|
7
|
+
export function replyJson(response, status, body) {
|
|
8
|
+
const text = JSON.stringify(body);
|
|
9
|
+
response.writeHead(status, {
|
|
10
|
+
'content-type': 'application/json; charset=utf-8',
|
|
11
|
+
'content-length': Buffer.byteLength(text),
|
|
12
|
+
'cache-control': 'no-store, private',
|
|
13
|
+
'x-content-type-options': 'nosniff',
|
|
14
|
+
'cross-origin-resource-policy': 'same-origin',
|
|
15
|
+
});
|
|
16
|
+
response.end(text);
|
|
17
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { replyJson } from './json-reply.js';
|
|
1
2
|
import { sameOrigin } from './settings-route.js';
|
|
2
3
|
import { findDesktopInstall, readBridgeStatus } from './bridge-status.js';
|
|
3
4
|
/** Where the frame endpoint lives. The viewer must use exactly this path, same origin. */
|
|
@@ -49,18 +50,6 @@ export async function handlePreviewSessions(request, response, settings, browser
|
|
|
49
50
|
const sessions = browser.watchableSessions?.() ?? [];
|
|
50
51
|
replyJson(response, 200, { enabled: true, sessions: [...sessions] });
|
|
51
52
|
}
|
|
52
|
-
/** Write one JSON body with no caching and no sniffing. */
|
|
53
|
-
function replyJson(response, status, body) {
|
|
54
|
-
const text = JSON.stringify(body);
|
|
55
|
-
response.writeHead(status, {
|
|
56
|
-
'content-type': 'application/json; charset=utf-8',
|
|
57
|
-
'content-length': Buffer.byteLength(text),
|
|
58
|
-
'cache-control': 'no-store, private',
|
|
59
|
-
'x-content-type-options': 'nosniff',
|
|
60
|
-
'cross-origin-resource-policy': 'same-origin',
|
|
61
|
-
});
|
|
62
|
-
response.end(text);
|
|
63
|
-
}
|
|
64
53
|
/**
|
|
65
54
|
* Serve one frame, or say precisely why not.
|
|
66
55
|
*
|
|
@@ -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, 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';
|
|
11
|
+
import type { BrowserA11yRequest, BrowserA11yResult, BrowserChallenge, BrowserCheckRequest, BrowserClearRequest, BrowserClickRequest, BrowserClickSequenceRequest, BrowserClickSequenceResult, BrowserContentRequest, BrowserContentResult, BrowserElementTarget, BrowserExecuteRequest, BrowserPageOperationRequest, 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";
|
|
@@ -261,7 +261,7 @@ export interface ElectronViewHandle {
|
|
|
261
261
|
* Optional: an adapter that cannot focus a view omits it, and the provider
|
|
262
262
|
* then behaves exactly as before.
|
|
263
263
|
*/
|
|
264
|
-
focus?(): Promise<void>;
|
|
264
|
+
focus?(options?: PresentOptions): Promise<void>;
|
|
265
265
|
/**
|
|
266
266
|
* Save a URL straight to disk, when the backing view can do that itself.
|
|
267
267
|
*
|
|
@@ -452,6 +452,8 @@ export interface PresentOptions {
|
|
|
452
452
|
export interface CommandOptions {
|
|
453
453
|
/** Present the view first (default), or only use a page that is already there. */
|
|
454
454
|
readonly present?: boolean;
|
|
455
|
+
/** Only use a guest already attached to this handle, without sidebar initialization. */
|
|
456
|
+
readonly initialize?: boolean;
|
|
455
457
|
}
|
|
456
458
|
/** Native capture options the self-hosted view handle understands. */
|
|
457
459
|
export interface ScreenshotOptions {
|
|
@@ -617,6 +619,11 @@ export declare class ElectronBrowserProvider implements BrowserProvider {
|
|
|
617
619
|
private focusView;
|
|
618
620
|
/** Execute JS in the active tab's page context. */
|
|
619
621
|
execute(session: BrowserSessionId, request: BrowserExecuteRequest, signal?: AbortSignal): Promise<BrowserExecuteResult>;
|
|
622
|
+
/** Run a fixed browser operation; the request cannot contain a script. */
|
|
623
|
+
pageOperation(session: BrowserSessionId, request: BrowserPageOperationRequest, signal?: AbortSignal): Promise<BrowserExecuteResult>;
|
|
624
|
+
/** Send page pointer input through CDP while preserving presentation and cancellation policy. */
|
|
625
|
+
private dispatchPointer;
|
|
626
|
+
private evaluatePage;
|
|
620
627
|
/**
|
|
621
628
|
* Poll until the active tab's page is ready (and optional URL/selector
|
|
622
629
|
* match), or the budget runs out. Returns a verdict instead of throwing on
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
* shell that owns the `BrowserWindow`.
|
|
7
7
|
* @module dsh-browser/browser-electron
|
|
8
8
|
*/
|
|
9
|
+
import { pageOperationScript } from '../tool-browser/page-probe.js';
|
|
9
10
|
import { randomUUID } from 'node:crypto';
|
|
10
11
|
import { existsSync, lstatSync, mkdirSync, realpathSync } from 'node:fs';
|
|
11
12
|
import { writeFileNoReplace } from './file-write.js';
|
|
@@ -661,7 +662,7 @@ export class ElectronBrowserProvider {
|
|
|
661
662
|
*/
|
|
662
663
|
async documentProbe(handle) {
|
|
663
664
|
try {
|
|
664
|
-
const result = await withTimeout(handleSendEvaluate(handle, 'String(performance.timeOrigin) + "|" + document.readyState'), 3_000, undefined, 'browser: document probe timed out');
|
|
665
|
+
const result = await withTimeout(handleSendEvaluate(handle, 'String(performance.timeOrigin) + "|" + document.readyState', undefined, { present: false, initialize: false }), 3_000, undefined, 'browser: document probe timed out');
|
|
665
666
|
if (!result.ok)
|
|
666
667
|
return null;
|
|
667
668
|
if (typeof result.value !== 'string')
|
|
@@ -794,14 +795,17 @@ export class ElectronBrowserProvider {
|
|
|
794
795
|
async focusView(handle) {
|
|
795
796
|
if (handle.focus === undefined)
|
|
796
797
|
return;
|
|
798
|
+
const quiet = this.isQuiet();
|
|
797
799
|
try {
|
|
798
800
|
// Bounded, unlike the rest of this call: the child RPC has no timeout of its own,
|
|
799
801
|
// and this runs before typing, so an unresponsive child would hang dsh_browser_key for
|
|
800
802
|
// good. A timeout is caught below exactly like any other focus failure, so the
|
|
801
803
|
// best-effort contract is unchanged.
|
|
802
|
-
await withTimeout(handle.focus(), 5_000, undefined, 'browser: focusing the view timed out');
|
|
804
|
+
await withTimeout(handle.focus(quiet ? { quiet } : undefined), 5_000, undefined, 'browser: focusing the view timed out');
|
|
803
805
|
}
|
|
804
|
-
catch {
|
|
806
|
+
catch (error) {
|
|
807
|
+
if (quiet)
|
|
808
|
+
throw error;
|
|
805
809
|
// Best-effort: a view that refuses focus must not fail the caller's key
|
|
806
810
|
// press; the dispatch below reports a real channel failure itself.
|
|
807
811
|
}
|
|
@@ -809,6 +813,69 @@ export class ElectronBrowserProvider {
|
|
|
809
813
|
/** Execute JS in the active tab's page context. */
|
|
810
814
|
async execute(session, request, signal) {
|
|
811
815
|
this.assertActionAllowed('execute');
|
|
816
|
+
return this.evaluatePage(session, request, signal);
|
|
817
|
+
}
|
|
818
|
+
/** Run a fixed browser operation; the request cannot contain a script. */
|
|
819
|
+
async pageOperation(session, request, signal) {
|
|
820
|
+
const script = pageOperationScript(request);
|
|
821
|
+
if (request.operation === 'drag') {
|
|
822
|
+
return this.dispatchPointer(session, request.from, request.to, request.steps, signal);
|
|
823
|
+
}
|
|
824
|
+
if (request.operation === 'probe' && request.method === 'hoverAt') {
|
|
825
|
+
const point = { x: Number(request.args?.[0]), y: Number(request.args?.[1]) };
|
|
826
|
+
return this.dispatchPointer(session, point, undefined, 1, signal);
|
|
827
|
+
}
|
|
828
|
+
return this.evaluatePage(session, { script }, signal);
|
|
829
|
+
}
|
|
830
|
+
/** Send page pointer input through CDP while preserving presentation and cancellation policy. */
|
|
831
|
+
async dispatchPointer(session, from, to, steps, signal) {
|
|
832
|
+
const coordinates = [from.x, from.y, ...to === undefined ? [] : [to.x, to.y]];
|
|
833
|
+
if (coordinates.some(value => !Number.isFinite(value) || value < 0)) {
|
|
834
|
+
throw new BrowserError('browser: pointer coordinates must be finite nonnegative viewport pixels', 'BROWSER_POINTER_INVALID');
|
|
835
|
+
}
|
|
836
|
+
if (to !== undefined && this.settingsSource?.().vision.strategy === 'nonVisual') {
|
|
837
|
+
throw new BrowserError('browser: coordinate dragging requires the visual strategy', 'BROWSER_NON_VISUAL_COORDINATES');
|
|
838
|
+
}
|
|
839
|
+
const s = this.session(session);
|
|
840
|
+
const handle = await this.present(s, signal);
|
|
841
|
+
const viewport = await withTimeout(handleSendEvaluate(handle, '({width:innerWidth,height:innerHeight})'), 5_000, signal, 'browser: pointer viewport check timed out');
|
|
842
|
+
if (!viewport.ok)
|
|
843
|
+
throw new BrowserError('browser: could not read the pointer viewport', 'BROWSER_POINTER_INVALID');
|
|
844
|
+
const size = viewport.value;
|
|
845
|
+
if (from.x >= size.width || from.y >= size.height || (to !== undefined && (to.x >= size.width || to.y >= size.height))) {
|
|
846
|
+
return { ok: true, value: JSON.stringify(to === undefined ? false : { ok: false, reason: 'drag points must be inside the current viewport' }) };
|
|
847
|
+
}
|
|
848
|
+
const send = async (params) => {
|
|
849
|
+
signal?.throwIfAborted();
|
|
850
|
+
this.assertSameView(session, handle, this.activeTab(s).handle);
|
|
851
|
+
await withTimeout(handle.sendCommand('Input.dispatchMouseEvent', { ...params }), 5_000, signal, 'browser: pointer input timed out');
|
|
852
|
+
};
|
|
853
|
+
await send({ type: 'mouseMoved', x: from.x, y: from.y, buttons: 0 });
|
|
854
|
+
if (to === undefined)
|
|
855
|
+
return { ok: true, value: 'true' };
|
|
856
|
+
let at = from;
|
|
857
|
+
let held = true;
|
|
858
|
+
try {
|
|
859
|
+
await send({ type: 'mousePressed', x: from.x, y: from.y, button: 'left', buttons: 1, clickCount: 1 });
|
|
860
|
+
const count = Math.max(1, Math.min(Math.trunc(steps) || 12, 60));
|
|
861
|
+
for (let step = 1; step <= count; step += 1) {
|
|
862
|
+
const next = { x: from.x + (to.x - from.x) * step / count, y: from.y + (to.y - from.y) * step / count };
|
|
863
|
+
await send({ type: 'mouseMoved', ...next, button: 'left', buttons: 1 });
|
|
864
|
+
at = next;
|
|
865
|
+
}
|
|
866
|
+
// Native HTML drag targets need a final drag-over before releasing at the destination.
|
|
867
|
+
await send({ type: 'mouseMoved', ...to, button: 'left', buttons: 1 });
|
|
868
|
+
await send({ type: 'mouseReleased', ...to, button: 'left', buttons: 0, clickCount: 1 });
|
|
869
|
+
held = false;
|
|
870
|
+
return { ok: true, value: JSON.stringify({ ok: true, moved: count, at: to }) };
|
|
871
|
+
}
|
|
872
|
+
finally {
|
|
873
|
+
if (held) {
|
|
874
|
+
await withTimeout(handle.sendCommand('Input.dispatchMouseEvent', { type: 'mouseReleased', ...at, button: 'left', buttons: 0, clickCount: 1 }), 1_000, undefined, 'browser: pointer release timed out').catch(() => { });
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
async evaluatePage(session, request, signal) {
|
|
812
879
|
const s = this.session(session);
|
|
813
880
|
const { handle } = this.activeTab(s);
|
|
814
881
|
signal?.throwIfAborted();
|
|
@@ -3122,7 +3189,10 @@ export class ElectronBrowserProvider {
|
|
|
3122
3189
|
* silently desynchronising the two and failing every screenshot.
|
|
3123
3190
|
*/
|
|
3124
3191
|
showActive(s) {
|
|
3125
|
-
|
|
3192
|
+
const quiet = this.isQuiet();
|
|
3193
|
+
this.host.showView?.(this.activeTab(s).handle, s.label, { quiet });
|
|
3194
|
+
if (quiet)
|
|
3195
|
+
return;
|
|
3126
3196
|
const autoExpand = this.settingsSource === undefined || this.settingsSource().ui.autoExpandOnce;
|
|
3127
3197
|
if (autoExpand)
|
|
3128
3198
|
return;
|
|
@@ -3269,13 +3339,13 @@ function scriptPoint(out) {
|
|
|
3269
3339
|
const y = Number(point?.y);
|
|
3270
3340
|
return Number.isFinite(x) && Number.isFinite(y) ? { x, y } : undefined;
|
|
3271
3341
|
}
|
|
3272
|
-
async function handleSendEvaluate(handle, expression, signal) {
|
|
3342
|
+
async function handleSendEvaluate(handle, expression, signal, options) {
|
|
3273
3343
|
signal?.throwIfAborted();
|
|
3274
3344
|
const result = await handle.sendCommand(CDP_RUNTIME_EVALUATE, {
|
|
3275
3345
|
expression,
|
|
3276
3346
|
returnByValue: true,
|
|
3277
3347
|
awaitPromise: true,
|
|
3278
|
-
});
|
|
3348
|
+
}, options);
|
|
3279
3349
|
if (result.exceptionDetails !== undefined) {
|
|
3280
3350
|
const detail = result.exceptionDetails;
|
|
3281
3351
|
return { ok: false, exception: detail.exception?.description ?? detail.text ?? 'unknown exception' };
|
|
@@ -97,11 +97,12 @@ function resolveElectronPathImpl(inputs) {
|
|
|
97
97
|
throw new Error('dsh-builtin-browser: cannot locate a spawnable Electron binary. ' +
|
|
98
98
|
'The electron package ships with the plugin and its binary is downloaded on first use, not at install time ' +
|
|
99
99
|
'(issue #41: an install-time download made default profiles refuse the plugin). The plugin prepares it ' +
|
|
100
|
-
'automatically when the bundled carrier is selected
|
|
101
|
-
'
|
|
102
|
-
'
|
|
103
|
-
'
|
|
104
|
-
'
|
|
100
|
+
'automatically when the bundled carrier is selected, so right after an install this can mean the ' +
|
|
101
|
+
'download is still running: Settings → Browser → Readiness shows that state and can retry it. To do it ' +
|
|
102
|
+
'by hand instead, run `npx --no dsh-browser-install-electron` in the profile that installed the ' +
|
|
103
|
+
'plugin, then retry. Alternatively set ELECTRON_PATH to an electron executable. Note: the host\'s own ' +
|
|
104
|
+
'executable cannot be reused when it is a packaged Electron app (e.g. DSH Desktop.exe) — only a real ' +
|
|
105
|
+
'electron binary can be spawned.');
|
|
105
106
|
}
|
|
106
107
|
/** DSH install anchors: electron installed separately (profile / global prefix), newest wins. */
|
|
107
108
|
function anchoredElectronBinary() {
|
|
@@ -744,8 +745,8 @@ class RemoteView {
|
|
|
744
745
|
});
|
|
745
746
|
}
|
|
746
747
|
/** Give this view web focus in the child, so keyboard input reaches the page. */
|
|
747
|
-
focus() {
|
|
748
|
-
return this.client.call('focus', { viewId: this.id });
|
|
748
|
+
focus(options) {
|
|
749
|
+
return this.client.call('focus', { viewId: this.id, ...options?.quiet ? { quiet: true } : {} });
|
|
749
750
|
}
|
|
750
751
|
/** Ask the child to download a URL to a local file (keeps cookies/login). */
|
|
751
752
|
async download(url, savePath) {
|
|
@@ -1321,8 +1322,8 @@ class DeferredRemoteView {
|
|
|
1321
1322
|
async capture(opts) {
|
|
1322
1323
|
return this.withRecovery(view => view.capture(opts));
|
|
1323
1324
|
}
|
|
1324
|
-
async focus() {
|
|
1325
|
-
return this.withRecovery(view => view.focus());
|
|
1325
|
+
async focus(options) {
|
|
1326
|
+
return this.withRecovery(view => view.focus(options));
|
|
1326
1327
|
}
|
|
1327
1328
|
async flushAuth() {
|
|
1328
1329
|
return this.withRecovery(view => view.flushAuth());
|