dsh-builtin-browser 0.5.8 → 1.0.1
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 +410 -1
- package/README.en.md +16 -64
- package/README.md +33 -64
- package/assets/browser-carriers.svg +20 -0
- package/assets/browser-extension-identity.d.mts +2 -0
- package/assets/browser-extension-identity.mjs +2 -0
- package/assets/browser-messages.d.mts +5 -0
- package/assets/browser-messages.mjs +64 -0
- package/assets/browser-panel-locales.d.mts +2 -0
- package/assets/browser-panel-locales.mjs +380 -0
- package/assets/browser-policy.d.mts +37 -0
- package/assets/browser-policy.mjs +37 -0
- package/assets/browser-products.d.mts +6 -0
- package/assets/browser-products.mjs +76 -0
- package/assets/screenshots/1-main.png +0 -0
- package/assets/screenshots/2-form-fill.png +0 -0
- package/assets/screenshots/3-challenge.png +0 -0
- package/browser-extension/constants.mjs +20 -0
- package/browser-extension/controller.mjs +293 -0
- package/browser-extension/locales.mjs +15 -0
- package/browser-extension/manifest.json +12 -0
- package/browser-extension/options.html +14 -0
- package/browser-extension/options.mjs +9 -0
- package/browser-extension/permission-guard.mjs +13 -0
- package/browser-extension/worker.mjs +74 -0
- package/cordis.patch.yml +23 -48
- package/desktop-bridge/desktop-discovery.d.mts +2 -0
- package/desktop-bridge/desktop-discovery.mjs +36 -0
- package/desktop-bridge/desktop-identity.json +17 -1
- package/desktop-bridge/desktop-processes.d.mts +1 -0
- package/desktop-bridge/desktop-processes.mjs +15 -0
- package/desktop-bridge/guest-proxy.mjs +109 -0
- package/desktop-bridge/install.mjs +197 -74
- package/desktop-bridge/main.mjs +11 -0
- package/desktop-bridge/maintenance-files.d.mts +8 -0
- package/desktop-bridge/maintenance-files.mjs +28 -0
- package/desktop-bridge/maintenance-progress.ps1 +2 -2
- package/desktop-bridge/native-guests.mjs +41 -0
- package/desktop-bridge/plugin-browser-bridge.js +618 -370
- package/desktop-bridge/profile-loader.mjs +26 -0
- package/dist/client.cjs +43167 -0
- package/dist/client.d.cts +516 -0
- package/dist/client.d.mts +516 -0
- package/dist/index.cjs +92 -0
- package/dist/index.d.mts +2960 -0
- package/dist/index.mjs +24 -0
- package/docs/HANDOVER-2026-11-15.md +178 -0
- package/docs/README.md +10 -19
- package/docs/architecture.md +17 -145
- package/docs/child-tabs.md +13 -0
- package/docs/desktop-acceptance.md +9 -0
- package/docs/development.md +88 -18
- package/docs/extension-browser.md +44 -0
- package/docs/hardcoding-audit.md +64 -0
- package/docs/limitations.md +23 -73
- package/docs/pr64-extension-status.md +46 -0
- package/docs/pr64-maintenance.md +25 -0
- package/docs/pr64-native-children.md +52 -0
- package/docs/pr64-native-views.md +60 -0
- package/docs/tool-reference.md +87 -11
- package/docs/user-guide.md +47 -255
- package/docs/why-browser.md +6 -39
- package/dsh-host-patch/README.md +60 -0
- package/dsh-host-patch/apply.mjs +194 -0
- package/dsh-host-patch/legacy-bridge.mjs +53 -0
- package/dsh-host-patch/rc3-revision1.json +90 -0
- package/dsh-host-patch/rc3-revision2.json +112 -0
- package/dsh-host-patch/rc3-revision3.json.gz +0 -0
- package/dsh-host-patch/rc3-revision4.json.gz +0 -0
- package/dsh-host-patch/rc3-revision5.json.gz +0 -0
- package/dsh-host-patch/rc3-revision6.json.gz +0 -0
- package/dsh-host-patch/rc3-revision7.json.gz +0 -0
- package/dsh-host-patch/rc3-revision8.json.gz +0 -0
- package/dsh-host-patch/rc3.json +148 -0
- package/dsh-host-patch/rc5-revision1.json +90 -0
- package/dsh-host-patch/rc5-revision2.json +112 -0
- package/dsh-host-patch/rc5-revision3.json.gz +0 -0
- package/dsh-host-patch/rc5-revision4.json.gz +0 -0
- package/dsh-host-patch/rc5-revision5.json.gz +0 -0
- package/dsh-host-patch/rc5-revision6.json.gz +0 -0
- package/dsh-host-patch/rc5-revision7.json.gz +0 -0
- package/dsh-host-patch/rc5-revision8.json.gz +0 -0
- package/dsh-host-patch/rc5.json +148 -0
- package/lib/bridge-compatibility.js +38 -0
- package/lib/browser/handles.js +78 -0
- package/lib/browser/runtime.js +155 -43
- package/lib/browser/takeover-target.js +24 -0
- package/lib/browser-electron/authenticated-route.js +23 -0
- package/lib/browser-electron/automatic-maintenance.js +394 -51
- package/lib/browser-electron/bridge-auto.js +2 -1
- package/lib/browser-electron/bridge-connection.js +8 -1
- package/lib/browser-electron/bridge-install.js +27 -2
- package/lib/browser-electron/bridge-maintenance.js +35 -10
- package/lib/browser-electron/bridge-status.js +98 -27
- package/lib/browser-electron/desktop-bridge-host.js +193 -560
- package/lib/browser-electron/desktop-identity.js +40 -2
- package/lib/browser-electron/document-readiness.js +34 -0
- package/lib/browser-electron/electron-provision.js +14 -44
- package/lib/browser-electron/entry.js +659 -252
- package/lib/browser-electron/extension-browser.js +234 -0
- package/lib/browser-electron/extension-setup.js +69 -0
- package/lib/browser-electron/guest-cookie-jar.LICENSE.txt +32 -0
- package/lib/browser-electron/guest-cookie-jar.js +10559 -0
- package/lib/browser-electron/guest-cookie-store.js +74 -0
- package/lib/browser-electron/guest-css-runtime.js +75 -0
- package/lib/browser-electron/guest-css.js +106 -0
- package/lib/browser-electron/guest-route.js +603 -0
- package/lib/browser-electron/guest-runtime.js +356 -0
- package/lib/browser-electron/guest-storage-runtime.js +52 -0
- package/lib/browser-electron/guest-storage.js +163 -0
- package/lib/browser-electron/history-store.js +2 -1
- package/lib/browser-electron/host-main.js +162 -7
- package/lib/browser-electron/host-patch-maintenance.js +141 -0
- package/lib/browser-electron/http-body.js +22 -0
- package/lib/browser-electron/maintenance-broker.js +35 -5
- package/lib/browser-electron/maintenance-lifecycle.js +7 -5
- package/lib/browser-electron/maintenance-route.js +9 -6
- package/lib/browser-electron/open-slot.js +47 -0
- package/lib/browser-electron/paths.js +4 -1
- package/lib/browser-electron/preferred-browser.js +144 -0
- package/lib/browser-electron/preview-route.js +11 -8
- package/lib/browser-electron/process-ownership.js +198 -0
- package/lib/browser-electron/profile-checkpoint.js +222 -0
- package/lib/browser-electron/profile-command.js +60 -0
- package/lib/browser-electron/profile-extension.js +126 -0
- package/lib/browser-electron/profile-lease.js +157 -0
- package/lib/browser-electron/profile-residue.js +61 -0
- package/lib/browser-electron/provider.js +719 -262
- package/lib/browser-electron/reference-route.js +635 -0
- package/lib/browser-electron/remote-host.js +12 -13
- package/lib/browser-electron/runtime-route.js +6 -5
- package/lib/browser-electron/runtime-status.js +30 -29
- package/lib/browser-electron/settings-store.js +10 -5
- package/lib/browser-electron/shortcuts-route.js +108 -0
- package/lib/browser-electron/sidebar-only-host.js +119 -18
- package/lib/browser-electron/startpage-shortcuts.js +172 -0
- package/lib/browser-electron/system-browser.js +352 -371
- package/lib/browser-electron/windows-system.js +2 -1
- package/lib/browser-reference/delivery.js +326 -0
- package/lib/browser-reference/entry.js +597 -0
- package/lib/browser-reference/export.js +298 -0
- package/lib/browser-reference/import.js +295 -0
- package/lib/browser-reference/page-script.js +1406 -0
- package/lib/browser-reference/service.js +1062 -0
- package/lib/browser-reference/store.js +177 -0
- package/lib/browser-reference/submission-receipts.js +139 -0
- package/lib/browser-reference/text-quote.js +167 -0
- package/lib/browser-reference/types.js +11 -0
- package/lib/browser-reference/visible.js +311 -0
- package/lib/browser-reference/web-sources.js +97 -0
- package/lib/index.js +10 -3
- package/lib/reference-owner.js +5 -0
- package/lib/reference-page.js +12 -0
- package/lib/reference-source.js +2 -0
- package/lib/reference-text.js +188 -0
- package/lib/startpage-settings.js +63 -0
- package/lib/startpage.js +44 -0
- package/lib/takeover-protocol.js +2 -0
- package/lib/tool-browser/code-tool.js +416 -0
- package/lib/tool-browser/code-worker.js +872 -0
- package/lib/tool-browser/index.js +940 -76
- package/lib/tool-browser/page-probe.js +3 -3
- package/lib/tool-browser/panel-page.js +95 -0
- package/lib/tool-browser/screenshot-image.js +51 -0
- package/lib/tool-browser/takeover.js +205 -0
- package/lib/types.js +1 -0
- package/lib/web-guest.js +26 -0
- package/native-host/client.d.mts +17 -0
- package/native-host/client.mjs +100 -0
- package/native-host/host.mjs +150 -0
- package/native-host/identity.d.mts +2 -0
- package/native-host/identity.mjs +1 -0
- package/native-host/install.mjs +168 -0
- package/native-host/protocol.mjs +75 -0
- package/package.json +80 -92
- package/scripts/build.mjs +66 -0
- package/scripts/bundle-host.mjs +28 -0
- package/scripts/fix-exports.mjs +50 -0
- package/scripts/install-electron.mjs +11 -26
- package/scripts/verify-exports.mjs +47 -0
- package/scripts/verify-preview-sdk.mjs +93 -0
- package/skills/dsh-builtin-browser/SKILL.md +56 -47
- package/skills/dsh-builtin-browser/references/batch.md +31 -1
- package/skills/dsh-debug-host/SKILL.md +56 -0
- package/skills/dsh-debug-self/SKILL.md +50 -0
- package/src/bridge-compatibility.ts +39 -0
- package/src/browser/handles.ts +89 -0
- package/src/browser/runtime.ts +146 -50
- package/src/browser/takeover-target.ts +36 -0
- package/src/browser/types.ts +19 -7
- package/src/browser-electron/authenticated-route.ts +29 -1
- package/src/browser-electron/automatic-maintenance.ts +368 -56
- package/src/browser-electron/bridge-auto.ts +5 -4
- package/src/browser-electron/bridge-connection.ts +20 -2
- package/src/browser-electron/bridge-endpoint.ts +3 -3
- package/src/browser-electron/bridge-install.ts +40 -4
- package/src/browser-electron/bridge-maintenance.ts +42 -14
- package/src/browser-electron/bridge-status.ts +94 -23
- package/src/browser-electron/desktop-bridge-host.ts +185 -585
- package/src/browser-electron/desktop-identity.ts +53 -2
- package/src/browser-electron/document-readiness.ts +45 -0
- package/src/browser-electron/electron-provision.ts +12 -45
- package/src/browser-electron/entry.ts +1299 -882
- package/src/browser-electron/ephemeral-profile.ts +1 -1
- package/src/browser-electron/extension-browser.ts +223 -0
- package/src/browser-electron/extension-setup.ts +67 -0
- package/src/browser-electron/guest-cookie-jar.ts +176 -0
- package/src/browser-electron/guest-cookie-store.ts +59 -0
- package/src/browser-electron/guest-css-runtime.ts +76 -0
- package/src/browser-electron/guest-css.ts +68 -0
- package/src/browser-electron/guest-route.ts +568 -0
- package/src/browser-electron/guest-runtime.ts +357 -0
- package/src/browser-electron/guest-storage-runtime.ts +54 -0
- package/src/browser-electron/guest-storage.ts +117 -0
- package/src/browser-electron/history-store.ts +5 -4
- package/src/browser-electron/host-main.ts +156 -11
- package/src/browser-electron/host-patch-maintenance.ts +112 -0
- package/src/browser-electron/http-body.ts +23 -0
- package/src/browser-electron/maintenance-broker.ts +29 -5
- package/src/browser-electron/maintenance-lifecycle.ts +14 -9
- package/src/browser-electron/maintenance-route.ts +17 -14
- package/src/browser-electron/missing-system-browser.ts +2 -2
- package/src/browser-electron/open-slot.ts +49 -0
- package/src/browser-electron/paths.ts +5 -1
- package/src/browser-electron/preferred-browser.ts +120 -0
- package/src/browser-electron/preview-route.ts +18 -11
- package/src/browser-electron/process-ownership.ts +168 -0
- package/src/browser-electron/profile-checkpoint.ts +187 -0
- package/src/browser-electron/profile-command.ts +46 -0
- package/src/browser-electron/profile-extension.ts +124 -0
- package/src/browser-electron/profile-lease.ts +115 -0
- package/src/browser-electron/profile-residue.ts +49 -0
- package/src/browser-electron/provider.ts +771 -289
- package/src/browser-electron/reference-route.ts +608 -0
- package/src/browser-electron/remote-host.ts +15 -15
- package/src/browser-electron/runtime-route.ts +11 -10
- package/src/browser-electron/runtime-status.ts +51 -32
- package/src/browser-electron/settings-store.ts +27 -7
- package/src/browser-electron/shortcuts-route.ts +112 -0
- package/src/browser-electron/sidebar-only-host.ts +118 -19
- package/src/browser-electron/startpage-shortcuts.ts +213 -0
- package/src/browser-electron/system-browser.ts +309 -338
- package/src/browser-electron/windows-system.ts +2 -1
- package/src/browser-reference/delivery.ts +384 -0
- package/src/browser-reference/entry.ts +631 -0
- package/src/browser-reference/export.ts +339 -0
- package/src/browser-reference/import.ts +303 -0
- package/src/browser-reference/page-script.ts +1537 -0
- package/src/browser-reference/service.ts +1315 -0
- package/src/browser-reference/store.ts +172 -0
- package/src/browser-reference/submission-receipts.ts +122 -0
- package/src/browser-reference/text-quote.ts +219 -0
- package/src/browser-reference/types.ts +195 -0
- package/src/browser-reference/visible.ts +367 -0
- package/src/browser-reference/web-sources.ts +103 -0
- package/src/client/browser/BrowserController.ts +693 -0
- package/src/client/browser/BrowserFrame.ts +78 -0
- package/src/client/browser/BrowserNavigation.ts +169 -0
- package/src/client/browser/BrowserPage.ts +23 -0
- package/src/client/browser/BrowserPersistence.ts +44 -0
- package/src/client/browser/IframeImpl.ts +176 -0
- package/src/client/browser/child-tabs.ts +14 -0
- package/src/client/browser/iframe-reference.ts +67 -0
- package/src/client/browser/initialization.ts +262 -0
- package/src/client/browser/omnibox.ts +243 -0
- package/src/client/browser/store.ts +38 -0
- package/src/client/browser/takeover-channel.ts +72 -0
- package/src/client/browser/url.ts +46 -0
- package/src/client/browser/web-reference-source.ts +52 -0
- package/src/client/css-modules.d.ts +4 -0
- package/src/client/definition.tsx +30 -0
- package/src/client/electron/ElectronWebViewImpl.ts +364 -0
- package/src/client/electron/ElectronWebviewPresentation.ts +138 -0
- package/src/client/electron/NativeChildPresentation.ts +45 -0
- package/src/client/electron/NativeViewPage.ts +251 -0
- package/src/client/electron/pages.ts +23 -0
- package/src/client/electron/reference-overlay.ts +53 -0
- package/src/client/electron/workspace.ts +33 -0
- package/src/client/index.ts +293 -0
- package/src/client/locales.ts +857 -0
- package/src/client/pages.ts +28 -0
- package/src/client/reference/client/BrowserRefLink.tsx +142 -0
- package/src/client/reference/client/draft-reference.ts +167 -0
- package/src/client/reference/client/index.ts +7 -0
- package/src/client/reference/client/ordered-submission.ts +108 -0
- package/src/client/reference/client/pending-delivery.ts +216 -0
- package/src/client/reference/client/plugin.ts +189 -0
- package/src/client/reference/client/production/ReferenceCapsules.tsx +345 -0
- package/src/client/reference/client/production/ReferenceDock.module.css +193 -0
- package/src/client/reference/client/production/ReferenceDock.tsx +6 -0
- package/src/client/reference/client/production/ReferenceFeedback.tsx +15 -0
- package/src/client/reference/client/production/ReferenceMath.tsx +44 -0
- package/src/client/reference/client/production/ReferenceText.tsx +36 -0
- package/src/client/reference/client/production/assembly.ts +471 -0
- package/src/client/reference/client/production/asset-store.ts +144 -0
- package/src/client/reference/client/production/availability.ts +74 -0
- package/src/client/reference/client/production/bytes.ts +36 -0
- package/src/client/reference/client/production/delivery-client.ts +211 -0
- package/src/client/reference/client/production/draft-adapter.ts +508 -0
- package/src/client/reference/client/production/http-transport.ts +217 -0
- package/src/client/reference/client/production/production-submission.ts +310 -0
- package/src/client/reference/client/production/protocol.ts +231 -0
- package/src/client/reference/client/production/reference-card.ts +240 -0
- package/src/client/reference/client/production/reference-labels.ts +29 -0
- package/src/client/reference/client/production/reply-link-bridge.ts +222 -0
- package/src/client/reference/client/production/reveal.ts +215 -0
- package/src/client/reference/client/production/runtime.ts +1101 -0
- package/src/client/reference/client/production/snapshot-adapter.ts +154 -0
- package/src/client/reference/client/production/source-marker.ts +63 -0
- package/src/client/reference/client/production/source-opener.ts +55 -0
- package/src/client/reference/client/production/styles.ts +104 -0
- package/src/client/reference/client/production/user-text-rule.ts +58 -0
- package/src/client/reference/client/reveal.ts +107 -0
- package/src/client/reference/client/scheme.ts +114 -0
- package/src/client/reference/client/types.ts +168 -0
- package/src/client/reference/css-modules.d.ts +4 -0
- package/src/client/reference/index.ts +11 -0
- package/src/client/reference-capture-owner.ts +162 -0
- package/src/client/reference-intents.ts +36 -0
- package/src/client/settings/index.ts +34 -0
- package/{client.js → src/client/settings/panel.ts} +2657 -2231
- package/src/client/shortcut-open.ts +30 -0
- package/src/client/startpage/binding.ts +99 -0
- package/src/client/startpage/controller.ts +303 -0
- package/src/client/startpage/model.ts +279 -0
- package/src/client/view/Browser.module.css +751 -0
- package/src/client/view/BrowserBody.tsx +314 -0
- package/src/client/view/BrowserPresentation.ts +9 -0
- package/src/client/view/BrowserTitle.tsx +17 -0
- package/src/client/view/IframePresentation.ts +344 -0
- package/src/client/view/StartPage.tsx +597 -0
- package/src/client/view/TakeoverBanner.tsx +21 -0
- package/src/index.ts +110 -73
- package/src/reference-owner.ts +5 -0
- package/src/reference-page.ts +9 -0
- package/src/reference-source.ts +3 -0
- package/src/reference-text.ts +197 -0
- package/src/startpage-settings.ts +103 -0
- package/src/startpage.ts +71 -0
- package/src/takeover-protocol.ts +19 -0
- package/src/tool-browser/code-tool.ts +401 -0
- package/src/tool-browser/code-worker.ts +639 -0
- package/src/tool-browser/index.ts +908 -77
- package/src/tool-browser/page-probe.ts +4 -4
- package/src/tool-browser/panel-page.ts +124 -0
- package/src/tool-browser/screenshot-image.ts +49 -0
- package/src/tool-browser/takeover.ts +198 -0
- package/src/types/electron-shim.d.ts +20 -0
- package/src/types.ts +85 -0
- package/src/web-guest.ts +34 -0
- package/tools/accept-desktop.mjs +148 -0
- package/lib/browser/runtime.d.ts +0 -175
- package/lib/browser/types.d.ts +0 -797
- package/lib/browser-electron/authenticated-route.d.ts +0 -27
- package/lib/browser-electron/automatic-maintenance.d.ts +0 -41
- package/lib/browser-electron/bridge-auto.d.ts +0 -93
- package/lib/browser-electron/bridge-connection.d.ts +0 -93
- package/lib/browser-electron/bridge-endpoint.d.ts +0 -30
- package/lib/browser-electron/bridge-install.d.ts +0 -85
- package/lib/browser-electron/bridge-maintenance.d.ts +0 -24
- package/lib/browser-electron/bridge-status.d.ts +0 -50
- package/lib/browser-electron/desktop-bridge-host.d.ts +0 -248
- package/lib/browser-electron/desktop-identity.d.ts +0 -58
- package/lib/browser-electron/electron-provision.d.ts +0 -92
- package/lib/browser-electron/entry.d.ts +0 -64
- package/lib/browser-electron/ephemeral-profile.d.ts +0 -37
- package/lib/browser-electron/file-write.d.ts +0 -2
- package/lib/browser-electron/history-store.d.ts +0 -98
- package/lib/browser-electron/host-main.d.ts +0 -26
- package/lib/browser-electron/http-body.d.ts +0 -24
- package/lib/browser-electron/json-reply.d.ts +0 -33
- package/lib/browser-electron/maintenance-broker.d.ts +0 -35
- package/lib/browser-electron/maintenance-lifecycle.d.ts +0 -32
- package/lib/browser-electron/maintenance-route.d.ts +0 -18
- package/lib/browser-electron/missing-system-browser.d.ts +0 -54
- package/lib/browser-electron/paths.d.ts +0 -4
- package/lib/browser-electron/preview-route.d.ts +0 -89
- package/lib/browser-electron/process.d.ts +0 -14
- package/lib/browser-electron/provider.d.ts +0 -959
- package/lib/browser-electron/remote-host.d.ts +0 -256
- package/lib/browser-electron/rpc-input.d.ts +0 -4
- package/lib/browser-electron/rpc-ops.d.ts +0 -18
- package/lib/browser-electron/runtime-diagnostics.d.ts +0 -12
- package/lib/browser-electron/runtime-route.d.ts +0 -55
- package/lib/browser-electron/runtime-status.d.ts +0 -116
- package/lib/browser-electron/settings-route.d.ts +0 -15
- package/lib/browser-electron/settings-store.d.ts +0 -224
- package/lib/browser-electron/sidebar-only-host.d.ts +0 -36
- package/lib/browser-electron/system-browser.d.ts +0 -367
- package/lib/browser-electron/timing.d.ts +0 -8
- package/lib/browser-electron/virtual-cursor.d.ts +0 -67
- package/lib/browser-electron/windows-system.d.ts +0 -3
- package/lib/index.d.ts +0 -30
- package/lib/tool-browser/element-fields.d.ts +0 -145
- package/lib/tool-browser/index.d.ts +0 -53
- package/lib/tool-browser/page-probe.d.ts +0 -86
- package/scripts/consumer-install-check.mjs +0 -72
- package/scripts/run-tests.mjs +0 -32
- package/scripts/verify-host-compat.mjs +0 -195
- package/scripts/verify-preview-route.mjs +0 -168
- package/scripts/verify-runtime-status.mjs +0 -179
|
@@ -13,10 +13,18 @@
|
|
|
13
13
|
* over tabs, history, or navigation state.
|
|
14
14
|
* @module dsh-browser/tool-browser
|
|
15
15
|
*/
|
|
16
|
-
import {
|
|
16
|
+
import { SCREENSHOT_MAX_WIDTH, SCREENSHOT_MAX_HEIGHT, screenshotDimension, screenshotBytes, screenshotVisionEnabled, saveScreenshotImage } from './screenshot-image.js';
|
|
17
|
+
import { BrowserTakeovers, installTakeovers } from './takeover.js';
|
|
18
|
+
import { TAKEOVER_TOOL } from '../takeover-protocol.js';
|
|
19
|
+
import { clearLastOpen, getLastOpen, setLastOpen } from '../browser-electron/open-slot.js';
|
|
20
|
+
import { readFileSync, statSync } from 'node:fs';
|
|
17
21
|
import { defineTool } from '@deepseek-ai/dsh-tools';
|
|
18
22
|
import { pageOperationScript } from './page-probe.js';
|
|
19
|
-
import {
|
|
23
|
+
import { CODE_PROMPT, CODE_TOOL, createCodeTool } from './code-tool.js';
|
|
24
|
+
import { SettingsStore, settingsPath } from '../browser-electron/settings-store.js';
|
|
25
|
+
import { createShortcutStore } from '../browser-electron/startpage-shortcuts.js';
|
|
26
|
+
import { createReferenceService, summarizeReference } from '../browser-reference/service.js';
|
|
27
|
+
import { browserSessionHandle, resolveSessionHandle, resolveTabHandle, scopedTabs, sessionWireId, tabWireId } from '../browser/handles.js';
|
|
20
28
|
import { ELEMENT_ITEM_SCHEMA, NODE_ITEM_SCHEMA, projectElement, projectNode, } from './element-fields.js';
|
|
21
29
|
/**
|
|
22
30
|
* Register a cleanup that runs once when the process exits.
|
|
@@ -42,11 +50,99 @@ function registerExitCleanup(cleanup) {
|
|
|
42
50
|
});
|
|
43
51
|
}
|
|
44
52
|
/** Plugin name used by loader diagnostics. */
|
|
53
|
+
/**
|
|
54
|
+
* Which surface an open should use.
|
|
55
|
+
*
|
|
56
|
+
* "panel" - place the page in the DSH sidebar and open nothing else (the normal Web behaviour).
|
|
57
|
+
* "builtin"- drive the plugin's own browser; needed when the thing under test IS DSH.
|
|
58
|
+
* @returns the configured surface, defaulting to "panel".
|
|
59
|
+
*/
|
|
60
|
+
function readSurface() {
|
|
61
|
+
// Try every plausible location and take the first file that actually carries the key. The plugin's
|
|
62
|
+
// own settingsPath() is preferred, but a module instance can hold a stale home while another module in
|
|
63
|
+
// the same process holds the live one — which is exactly why channel read fine and surface did not.
|
|
64
|
+
const declared = globalThis.__dshBrowserSurface;
|
|
65
|
+
if (declared === 'panel' || declared === 'builtin')
|
|
66
|
+
return declared;
|
|
67
|
+
const candidates = [];
|
|
68
|
+
try {
|
|
69
|
+
candidates.push(settingsPath());
|
|
70
|
+
}
|
|
71
|
+
catch { /* no module home */ }
|
|
72
|
+
candidates.push('D:/dsh-home/dsh-builtin-browser-host/settings.json');
|
|
73
|
+
for (const candidate of candidates) {
|
|
74
|
+
if (typeof candidate !== 'string' || candidate === '')
|
|
75
|
+
continue;
|
|
76
|
+
try {
|
|
77
|
+
const parsed = JSON.parse(readFileSync(candidate, 'utf8'));
|
|
78
|
+
const surface = parsed.browser?.surface;
|
|
79
|
+
if (surface === 'builtin' || surface === 'panel')
|
|
80
|
+
return surface;
|
|
81
|
+
}
|
|
82
|
+
catch { /* next candidate */ }
|
|
83
|
+
}
|
|
84
|
+
return 'panel';
|
|
85
|
+
}
|
|
86
|
+
// One line at registration: tool handlers are registered once per host start, and a recomposition does
|
|
87
|
+
// not re-register them. Printing the resolved surface here is the only reliable way to tell WHICH build a
|
|
88
|
+
// live host is actually running, instead of inferring it from behaviour.
|
|
89
|
+
try {
|
|
90
|
+
console.log('[tool-browser] registered, surface=' + readSurface());
|
|
91
|
+
}
|
|
92
|
+
catch { /* logging only */ }
|
|
93
|
+
/**
|
|
94
|
+
* Keep the built-in browser for DSH itself.
|
|
95
|
+
*
|
|
96
|
+
* "builtin" exists to debug DSH: a client bundle, the settings panel, the sidebar UI. Using it for an
|
|
97
|
+
* ordinary site opens a window the reader did not ask for — measured as "why did you open the built-in
|
|
98
|
+
* browser again". A non-loopback target therefore degrades to "panel" silently; the caller's intent does
|
|
99
|
+
* not override the rule.
|
|
100
|
+
* @param surface - the requested surface.
|
|
101
|
+
* @param url - the page being opened.
|
|
102
|
+
* @returns the surface that will actually be used.
|
|
103
|
+
*/
|
|
104
|
+
function enforceSurface(surface, _url) {
|
|
105
|
+
// The caller decides the surface. An earlier version degraded "builtin" to "panel" for anything but a
|
|
106
|
+
// loopback URL, which also blocked the agent from driving the human's OWN browser — the opposite of what
|
|
107
|
+
// the setting is for. Which window appears is decided by the carrier (bundled Electron vs. the human's
|
|
108
|
+
// Edge), not by the URL. Keeping ordinary tests on the sidebar is a working rule, not a code constraint.
|
|
109
|
+
return surface;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Refuse a request that cannot work BEFORE anything is opened.
|
|
113
|
+
*
|
|
114
|
+
* The sidebar renders pages through a same-origin proxy, so asking it to load this machine answers with a
|
|
115
|
+
* network-level failure that reads like a bug in the page. Reporting that afterwards is useless — the call
|
|
116
|
+
* already happened. The rule is therefore enforced up front: a loopback target on the panel surface is
|
|
117
|
+
* refused, and the message names the surface that does work.
|
|
118
|
+
* @param surface - the surface this call would use.
|
|
119
|
+
* @param url - the page being opened.
|
|
120
|
+
*/
|
|
121
|
+
function assertSurfaceCanHost(surface, url) {
|
|
122
|
+
if (surface !== 'panel')
|
|
123
|
+
return;
|
|
124
|
+
let host;
|
|
125
|
+
try {
|
|
126
|
+
host = new URL(url).hostname.toLowerCase();
|
|
127
|
+
}
|
|
128
|
+
catch {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
const local = host === 'localhost' || host === '127.0.0.1' || host === '::1' || host === '[::1]' || /^127\./u.test(host);
|
|
132
|
+
if (!local)
|
|
133
|
+
return;
|
|
134
|
+
throw new Error('dsh-builtin-browser: the sidebar cannot host this machine (' + host
|
|
135
|
+
+ '). It proxies pages through itself, so loading DSH there is the proxy fetching its own server. '
|
|
136
|
+
+ '调试 DSH 请用 surface: "builtin"(内置浏览器),普通网页才走侧栏。');
|
|
137
|
+
}
|
|
45
138
|
export const name = 'tool-browser';
|
|
46
139
|
/** The tool registry, browser seam, and system-prompt registry this tool layer consumes. */
|
|
47
140
|
export const inject = ['tools', 'browser', 'systemPrompt'];
|
|
48
141
|
function createState() {
|
|
49
|
-
return {
|
|
142
|
+
return {
|
|
143
|
+
sessionsByTask: new Map(), pendingOpens: new Map(), configRestrictedTo: undefined,
|
|
144
|
+
restrictedByTask: new Map(), panelUrlByTask: new Map(), pendingPanelOpens: new Map(),
|
|
145
|
+
};
|
|
50
146
|
}
|
|
51
147
|
/** Every live state, for the test hook (introspection only, never shared). */
|
|
52
148
|
const liveStates = new Set();
|
|
@@ -63,6 +159,32 @@ const liveStates = new Set();
|
|
|
63
159
|
* blind it or trap it, and the tool descriptions promise as much. dsh_browser_restrict
|
|
64
160
|
* is in here so a task that restricted everything can still lift it.
|
|
65
161
|
*/
|
|
162
|
+
/** Tool loaded on demand: registers a group of browser tools for the rest of the run. */
|
|
163
|
+
export const LOAD_TOOL = 'dsh_browser_load_tools';
|
|
164
|
+
/** Tools the model sees from the start on the default `core` surface. */
|
|
165
|
+
export const CORE_TOOLS = new Set([
|
|
166
|
+
'dsh_browser_open', 'dsh_browser_snapshot', 'dsh_browser_a11y', 'dsh_browser_click', 'dsh_browser_type',
|
|
167
|
+
'dsh_browser_key', 'dsh_browser_scroll', 'dsh_browser_select', 'dsh_browser_fill', 'dsh_browser_wait',
|
|
168
|
+
'dsh_browser_content', 'dsh_browser_find', 'dsh_browser_scrape', 'dsh_browser_screenshot',
|
|
169
|
+
'dsh_browser_execute', 'dsh_browser_do', 'dsh_browser_state', 'dsh_browser_challenge',
|
|
170
|
+
'dsh_browser_list_tabs', 'dsh_browser_switch_tab', 'dsh_browser_close_tab', 'dsh_browser_back',
|
|
171
|
+
'dsh_browser_reference_capture', 'dsh_browser_reference_reveal', 'dsh_browser_reference_list',
|
|
172
|
+
'dsh_browser_request_takeover', 'dsh_browser_help',
|
|
173
|
+
]);
|
|
174
|
+
/**
|
|
175
|
+
* The rest of the catalog, loaded by group with dsh_browser_load_tools. A loaded tool is never
|
|
176
|
+
* unregistered, so the tool list only grows and the request prefix stays cacheable. Any tool not
|
|
177
|
+
* named here or in CORE_TOOLS belongs to `more`.
|
|
178
|
+
*/
|
|
179
|
+
export const TOOL_GROUPS = {
|
|
180
|
+
forms: ['dsh_browser_check', 'dsh_browser_clear', 'dsh_browser_set_value', 'dsh_browser_get_value', 'dsh_browser_upload', 'dsh_browser_drag', 'dsh_browser_hover', 'dsh_browser_dialog', 'dsh_browser_click_sequence'],
|
|
181
|
+
navigation: ['dsh_browser_forward', 'dsh_browser_refresh', 'dsh_browser_history', 'dsh_browser_replay', 'dsh_browser_visited', 'dsh_browser_download'],
|
|
182
|
+
debug: ['dsh_browser_console', 'dsh_browser_network', 'dsh_browser_wait_for_response', 'dsh_browser_assert', 'dsh_browser_doctor'],
|
|
183
|
+
session: ['dsh_browser_session', 'dsh_browser_reset', 'dsh_browser_reset_session', 'dsh_browser_restrict', 'dsh_browser_auth', 'dsh_browser_shortcuts'],
|
|
184
|
+
more: [],
|
|
185
|
+
};
|
|
186
|
+
/** Guide for the `core` surface. Static, so the prompt prefix caches. */
|
|
187
|
+
const CORE_PROMPT = 'Operate the built-in browser with the dsh_browser_* tools. Each task has its own browser session and window: the human can watch and take over, and other tasks\' pages are not visible to you. The core tools are loaded: dsh_browser_open; dsh_browser_snapshot or dsh_browser_a11y to map a page; click, type, key, scroll, select and fill to act; wait; content, find and scrape to read; screenshot; execute for page JavaScript; the tab tools and back; and reference_capture/reveal/list for citations. Load the other tools by group with dsh_browser_load_tools when a task needs them; loaded tools stay available. To batch, use dsh_browser_do for a fixed list of steps, or dsh_browser_code for a JavaScript program that calls any browser tool as `await tools.dsh_browser_<name>(args)`, loops and branches over the results, overlaps independent reads with Promise.all and prints only what you need. Prefer dsh_browser_code whenever a task takes more than a few actions or spans many pages, and print findings as you go: on a deadline or cancellation everything already printed is still returned. When run_code is available, call the same tools from run_code instead. A blocked page (403, access denied, bot wall) is not the end of the task. Look for another route to the same public information: other pages of the site, its own JSON/API endpoints or sitemap, search-engine results, archived or reader copies, and other reputable sources. Keep each value tied to the source it came from. Report a blocker only once those routes are exhausted, and still deliver everything you did find. On a CAPTCHA, slider or other human verification (dsh_browser_challenge, or a CHALLENGE banner in a result), never retry or solve it: call dsh_browser_request_takeover.';
|
|
66
188
|
export const READ_ONLY_TOOLS = new Set([
|
|
67
189
|
// Tools that only observe.
|
|
68
190
|
'dsh_browser_snapshot', 'dsh_browser_a11y', 'dsh_browser_content', 'dsh_browser_scrape',
|
|
@@ -75,6 +197,9 @@ export const READ_ONLY_TOOLS = new Set([
|
|
|
75
197
|
'dsh_browser_assert', 'dsh_browser_doctor', 'dsh_browser_help',
|
|
76
198
|
// Reading the page's own digest and searching its text: observation like the rest.
|
|
77
199
|
'dsh_browser_find', 'dsh_browser_state',
|
|
200
|
+
// References read the page the human already has open and write only this plugin's own store;
|
|
201
|
+
// restricting an agent's actions must not stop it from recording what it was told to read.
|
|
202
|
+
'dsh_browser_reference_capture', 'dsh_browser_reference_reveal', 'dsh_browser_reference_list',
|
|
78
203
|
// dsh_browser_auth reads cookies out of the browser: an observation, and one the
|
|
79
204
|
// credentials switch gates on its own (see credentials.allowRead in the settings).
|
|
80
205
|
// dsh_browser_auth is deliberately NOT here. It reads cookies on "flush" but WRITES them on
|
|
@@ -170,6 +295,81 @@ function targetIndex(target) {
|
|
|
170
295
|
function taskKey(exec) {
|
|
171
296
|
return exec?.agent?.id ?? 'default';
|
|
172
297
|
}
|
|
298
|
+
const effectProperties = {
|
|
299
|
+
verified: { type: 'boolean', description: 'Whether a visible change was observed. Absent when it could not be checked.' },
|
|
300
|
+
note: { type: 'string', description: 'Why the effect is uncertain.' },
|
|
301
|
+
};
|
|
302
|
+
async function pageFingerprint(browser, session) {
|
|
303
|
+
try {
|
|
304
|
+
const result = await browser.execute(session, {
|
|
305
|
+
script: `(() => {
|
|
306
|
+
const a = document.activeElement
|
|
307
|
+
return JSON.stringify([
|
|
308
|
+
location.href, document.title,
|
|
309
|
+
a ? [a.tagName, a.id || a.getAttribute('name') || a.className || '',
|
|
310
|
+
typeof a.value === 'string' ? a.value.length : null, a.checked, a.selectedIndex,
|
|
311
|
+
a.selectionStart, a.selectionEnd, a.scrollLeft, a.scrollTop] : null,
|
|
312
|
+
window.scrollX, window.scrollY,
|
|
313
|
+
document.body ? document.body.childElementCount : 0,
|
|
314
|
+
document.body ? document.body.innerHTML.length : 0,
|
|
315
|
+
])
|
|
316
|
+
})()`,
|
|
317
|
+
});
|
|
318
|
+
return result.ok && typeof result.value === 'string' ? result.value : undefined;
|
|
319
|
+
}
|
|
320
|
+
catch {
|
|
321
|
+
return undefined;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
async function observedEffect(browser, session, before) {
|
|
325
|
+
const after = await pageFingerprint(browser, session);
|
|
326
|
+
if (before === undefined || after === undefined)
|
|
327
|
+
return { note: 'The effect could not be checked.' };
|
|
328
|
+
return before !== after
|
|
329
|
+
? { verified: true }
|
|
330
|
+
: { verified: false, note: 'No visible change observed; the effect is uncertain.' };
|
|
331
|
+
}
|
|
332
|
+
function effectText(value) {
|
|
333
|
+
return value.verified === true ? ' Visible change observed.' : ` ${value.note ?? 'The effect could not be checked.'}`;
|
|
334
|
+
}
|
|
335
|
+
function rememberSession(browser, state, key, agent, session, panelUrl) {
|
|
336
|
+
const previous = state.sessionsByTask.get(key);
|
|
337
|
+
const previousPanelUrl = state.panelUrlByTask.get(key);
|
|
338
|
+
if (session === undefined)
|
|
339
|
+
state.sessionsByTask.delete(key);
|
|
340
|
+
else
|
|
341
|
+
state.sessionsByTask.set(key, session);
|
|
342
|
+
if (panelUrl === undefined)
|
|
343
|
+
state.panelUrlByTask.delete(key);
|
|
344
|
+
else
|
|
345
|
+
state.panelUrlByTask.set(key, panelUrl);
|
|
346
|
+
if (previous === session && (previousPanelUrl !== undefined) === (panelUrl !== undefined))
|
|
347
|
+
return;
|
|
348
|
+
const cleanup = () => {
|
|
349
|
+
if (state.sessionsByTask.get(key) !== session || state.panelUrlByTask.has(key) !== (panelUrl !== undefined))
|
|
350
|
+
return;
|
|
351
|
+
state.sessionsByTask.delete(key);
|
|
352
|
+
state.panelUrlByTask.delete(key);
|
|
353
|
+
if (session !== undefined)
|
|
354
|
+
void browser.close(session).catch(() => { });
|
|
355
|
+
};
|
|
356
|
+
try {
|
|
357
|
+
if (agent?.ctx !== undefined)
|
|
358
|
+
agent.ctx.effect(() => cleanup);
|
|
359
|
+
else if (key === 'default' && session !== undefined)
|
|
360
|
+
registerExitCleanup(() => browser.close(session).catch(() => { }));
|
|
361
|
+
}
|
|
362
|
+
catch (error) {
|
|
363
|
+
cleanup();
|
|
364
|
+
if (previous !== undefined)
|
|
365
|
+
state.sessionsByTask.set(key, previous);
|
|
366
|
+
if (previousPanelUrl !== undefined)
|
|
367
|
+
state.panelUrlByTask.set(key, previousPanelUrl);
|
|
368
|
+
throw error;
|
|
369
|
+
}
|
|
370
|
+
if (previous !== undefined && previous !== session)
|
|
371
|
+
void browser.close(previous).catch(() => { });
|
|
372
|
+
}
|
|
173
373
|
/**
|
|
174
374
|
* Resolve the calling task's browser session, opening one on first use.
|
|
175
375
|
* Concurrent first calls for the same key share a single open. When the call
|
|
@@ -182,7 +382,8 @@ function taskKey(exec) {
|
|
|
182
382
|
* @param agent - the calling agent, when any (its ctx owns the session).
|
|
183
383
|
* @returns the task's session id.
|
|
184
384
|
*/
|
|
185
|
-
async function ensureSession(browser, state, key, agent) {
|
|
385
|
+
async function ensureSession(browser, state, key, agent, create = false) {
|
|
386
|
+
await state.pendingPanelOpens.get(key);
|
|
186
387
|
const existing = state.sessionsByTask.get(key);
|
|
187
388
|
if (existing !== undefined) {
|
|
188
389
|
// The human closing a browser window ends that session, so a cached id can
|
|
@@ -194,41 +395,25 @@ async function ensureSession(browser, state, key, agent) {
|
|
|
194
395
|
const pending = state.pendingOpens.get(key);
|
|
195
396
|
if (pending !== undefined)
|
|
196
397
|
return pending;
|
|
398
|
+
if (state.panelUrlByTask.has(key)) {
|
|
399
|
+
throw new Error('dsh-builtin-browser: the panel surface has no live CDP session for this operation. '
|
|
400
|
+
+ 'Use the CDP-backed DSH desktop sidebar with a connected desktop bridge and call dsh_browser_open again. '
|
|
401
|
+
+ 'The Web iframe panel cannot provide live snapshots, a11y, content, scrape, screenshots, interactions, '
|
|
402
|
+
+ 'uploads, downloads, or reference capture. Reading tools never start a separate browser.');
|
|
403
|
+
}
|
|
404
|
+
// Starting a browser is `dsh_browser_open`'s job and nobody else's. A read tool asking "what is on
|
|
405
|
+
// screen" used to start one — which, with the human's own Edge already running, handed the command over
|
|
406
|
+
// and opened yet another window, reported as "one look, two Edge windows". A reader with nothing open
|
|
407
|
+
// reports that instead of launching anything.
|
|
408
|
+
if (!create) {
|
|
409
|
+
throw new Error('dsh-builtin-browser: nothing is open in this task yet — call dsh_browser_open first. '
|
|
410
|
+
+ 'Reading tools never start a browser.');
|
|
411
|
+
}
|
|
197
412
|
// The task key rides along as the session label so the window title shows
|
|
198
413
|
// which task's page is currently visible to the human.
|
|
199
414
|
const opening = browser.open(key).then(session => {
|
|
200
|
-
// Tie the session's lifetime to the agent's scoped context FIRST: when
|
|
201
|
-
// the agent (and its DSH session) is disposed, the effect's disposer
|
|
202
|
-
// runs and closes the browser session. Registration happens once per
|
|
203
|
-
// session (only on first open for the key). If the agent is already
|
|
204
|
-
// gone, close the fresh session instead of leaking it.
|
|
205
|
-
state.sessionsByTask.set(key, session);
|
|
206
415
|
state.pendingOpens.delete(key);
|
|
207
|
-
|
|
208
|
-
if (agent?.ctx !== undefined) {
|
|
209
|
-
agent.ctx.effect(() => () => {
|
|
210
|
-
const current = state.sessionsByTask.get(key);
|
|
211
|
-
if (current === undefined)
|
|
212
|
-
return;
|
|
213
|
-
state.sessionsByTask.delete(key);
|
|
214
|
-
void browser.close(current).catch(() => { });
|
|
215
|
-
});
|
|
216
|
-
}
|
|
217
|
-
else if (key === 'default') {
|
|
218
|
-
// Agentless / CLI probe: close the session on process exit so the
|
|
219
|
-
// window is not orphaned.
|
|
220
|
-
// One hook for the whole process, not one listener per session: a default session can
|
|
221
|
-
// be created repeatedly (a CLI probe does), and each registration lived for the life of
|
|
222
|
-
// the process — so the listener list grew without bound and Node warned at eleven.
|
|
223
|
-
registerExitCleanup(() => browser.close(session).catch(() => { }));
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
catch (error) {
|
|
227
|
-
// effect() failed — undo the registration so future calls retry.
|
|
228
|
-
state.sessionsByTask.delete(key);
|
|
229
|
-
void browser.close(session).catch(() => { });
|
|
230
|
-
throw error;
|
|
231
|
-
}
|
|
416
|
+
rememberSession(browser, state, key, agent, session);
|
|
232
417
|
return session;
|
|
233
418
|
}, error => { state.pendingOpens.delete(key); throw error; });
|
|
234
419
|
state.pendingOpens.set(key, opening);
|
|
@@ -280,6 +465,7 @@ function formatSnapshot(snapshot, options = {}) {
|
|
|
280
465
|
/** Register all browser tools with `ctx.tools`. */
|
|
281
466
|
export function apply(ctx, config = {}) {
|
|
282
467
|
const timeoutMs = config.timeoutMs ?? 60_000;
|
|
468
|
+
const surface = config.surface ?? 'core';
|
|
283
469
|
/**
|
|
284
470
|
* Longest wait `dsh_browser_wait` may poll for, leaving room for the tool call itself to
|
|
285
471
|
* finish and report.
|
|
@@ -303,16 +489,32 @@ export function apply(ctx, config = {}) {
|
|
|
303
489
|
// scoped to THIS plugin apply, so parallel contexts never share sessions
|
|
304
490
|
// or leak restrictions into each other.
|
|
305
491
|
const state = createState();
|
|
492
|
+
const takeovers = new BrowserTakeovers();
|
|
493
|
+
installTakeovers(ctx, takeovers);
|
|
306
494
|
liveStates.add(state);
|
|
307
495
|
ctx.effect(() => () => { liveStates.delete(state); });
|
|
308
496
|
// Re-apply resets the restriction: an omitted allowedActions lifts it.
|
|
309
497
|
state.configRestrictedTo = config.allowedActions !== undefined ? [...config.allowedActions] : undefined;
|
|
310
498
|
ctx.systemPrompt.section({
|
|
311
|
-
name: 'tool:browser',
|
|
499
|
+
name: 'tool:browser-human-takeover',
|
|
500
|
+
order: 152,
|
|
501
|
+
text: 'For CAPTCHAs, sliders, logins, two-factor authentication (2FA), and payments, call dsh_browser_request_takeover with a clear reason and any instructions. Never attempt automatic CAPTCHA or slider solving. Wait for the human, then re-read the fresh page summary and inspect the page before continuing. A skipped or cancelled takeover is not successful completion; do not bypass it or retry protected actions automatically.',
|
|
502
|
+
});
|
|
503
|
+
ctx.systemPrompt.section({
|
|
504
|
+
name: 'tool:dsh-browser-reference-presentation',
|
|
505
|
+
order: 151,
|
|
506
|
+
text: 'Browser references are already displayed as interactive capsules. The capsules provide source navigation. Do not repeat browser-reference markers, dsh-browser-ref: URIs, source URLs, or source/citation links in replies; answer using the quoted content instead.',
|
|
507
|
+
});
|
|
508
|
+
ctx.systemPrompt.section({
|
|
509
|
+
name: 'tool:dsh-builtin-browser',
|
|
312
510
|
// Tool guidance band is 100-199; 150 keeps clear of the common 110/120
|
|
313
511
|
// tool sections so ordering does not depend on plugin load sequence.
|
|
314
512
|
order: 150,
|
|
315
|
-
text:
|
|
513
|
+
text: surface === 'code'
|
|
514
|
+
? CODE_PROMPT
|
|
515
|
+
: surface === 'core'
|
|
516
|
+
? CORE_PROMPT
|
|
517
|
+
: 'Use dsh_browser_* tools to operate the built-in browser. Each task has its own session and window (real toolbar): the human can watch and take over, and other tasks\' pages are not visible to you. Map a page with dsh_browser_a11y (semantic roles/names/states) or dsh_browser_snapshot (numbered elements), then drive it: dsh_browser_click/dsh_browser_type take a target {by: css|text|xpath, value}, or coordinates from dsh_browser_screenshot. Prefer dsh_browser_fill (batch) or dsh_browser_set_value/check/select/clear for forms; verify with dsh_browser_get_value. Use dsh_browser_scrape (not hand-written dsh_browser_execute) for list pages; dsh_browser_wait after slow navigation. Use dsh_browser_do for a run of steps, dsh_browser_assert to check results, and dsh_browser_console/network when the screen is unclear. Say what you are doing on the page. On a CAPTCHA (dsh_browser_challenge), stop retrying and ask the human to solve it in the browser window, then re-check.',
|
|
316
518
|
});
|
|
317
519
|
/**
|
|
318
520
|
* Register one tool, turning a name collision into a warning instead of a dead apply.
|
|
@@ -328,9 +530,58 @@ export function apply(ctx, config = {}) {
|
|
|
328
530
|
*/
|
|
329
531
|
// One store for this apply: it caches by mtime, so reading it per download is a stat, not a parse.
|
|
330
532
|
const settingsStore = new SettingsStore();
|
|
533
|
+
// Built on first use, not at apply time: the provider is registered by another plugin's apply,
|
|
534
|
+
// and a tool registered before the browser exists must still work once it does.
|
|
535
|
+
let referenceService;
|
|
536
|
+
const references = () => {
|
|
537
|
+
if (referenceService !== undefined)
|
|
538
|
+
return referenceService;
|
|
539
|
+
const browser = ctx.get('browser');
|
|
540
|
+
if (browser === undefined)
|
|
541
|
+
throw new Error('tool-browser: browser service unavailable');
|
|
542
|
+
// Keep the SAME image fallback as the floating bar and the HTTP capture
|
|
543
|
+
// (browser-electron/entry.ts): a clipped visible-region read lets an image
|
|
544
|
+
// whose source URL cannot be downloaded still become a real reference.
|
|
545
|
+
referenceService = createReferenceService(browser, {
|
|
546
|
+
visibleCapture: async (sessionId, clip, signal) => {
|
|
547
|
+
const shot = await browser.screenshot(sessionWireId(resolveSessionHandle(sessionId)), { clip, format: 'png' }, signal);
|
|
548
|
+
return { dataUrl: shot.dataUrl, mime: 'image/png' };
|
|
549
|
+
},
|
|
550
|
+
});
|
|
551
|
+
return referenceService;
|
|
552
|
+
};
|
|
553
|
+
const referenceCarrier = () => {
|
|
554
|
+
const channel = settingsStore.get().browser.channel;
|
|
555
|
+
return channel === 'sidebar' || channel === 'chrome' || channel === 'edge' ? channel : 'bundled';
|
|
556
|
+
};
|
|
557
|
+
const catalog = new Map();
|
|
558
|
+
const loaded = new Set();
|
|
331
559
|
const registerSafely = (definition) => {
|
|
332
560
|
try {
|
|
333
|
-
|
|
561
|
+
const execute = definition.execute.bind(definition);
|
|
562
|
+
const name = definition.name;
|
|
563
|
+
definition = { ...definition, execute: async (args, exec) => {
|
|
564
|
+
const owner = taskKey(exec);
|
|
565
|
+
if (name !== TAKEOVER_TOOL && takeovers.blocks(owner)
|
|
566
|
+
&& (!READ_ONLY_TOOLS.has(name) || ['dsh_browser_reset', 'dsh_browser_reset_session', 'dsh_browser_reference_reveal'].includes(name))) {
|
|
567
|
+
const requested = args.tabId;
|
|
568
|
+
let tab = requested;
|
|
569
|
+
if (requested?.startsWith('dsh-tab:')) {
|
|
570
|
+
const session = state.sessionsByTask.get(owner);
|
|
571
|
+
if (session)
|
|
572
|
+
tab = resolveTabHandle(browserSessionHandle(owner, session), requested).tab;
|
|
573
|
+
}
|
|
574
|
+
if (tab === undefined || takeovers.blocks(owner, tab)) {
|
|
575
|
+
throw new Error('Browser tab is awaiting human takeover. Agent browser actions are refused until the human confirms completion or skips the request.');
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
return execute(args, exec);
|
|
579
|
+
} };
|
|
580
|
+
catalog.set(definition.name, definition);
|
|
581
|
+
if (surface === 'tools' || definition.name === TAKEOVER_TOOL || (surface === 'core' && CORE_TOOLS.has(definition.name))) {
|
|
582
|
+
ctx.tools.register(definition);
|
|
583
|
+
loaded.add(definition.name);
|
|
584
|
+
}
|
|
334
585
|
}
|
|
335
586
|
catch (error) {
|
|
336
587
|
ctx.logger?.warn?.(`dsh-builtin-browser: could not register ${definition.name} (${String(error)}). Another `
|
|
@@ -338,12 +589,169 @@ export function apply(ctx, config = {}) {
|
|
|
338
589
|
+ 'rest of this plugin\'s tools are unaffected.');
|
|
339
590
|
}
|
|
340
591
|
};
|
|
592
|
+
registerSafely(defineTool({
|
|
593
|
+
name: 'dsh_browser_request_takeover',
|
|
594
|
+
description: 'Hand the selected visible sidebar browser tab in this conversation to the human for a CAPTCHA/slider, login, 2FA or payment. Works on the Web iframe without CDP, including user-opened tabs: adopts the selected tab for this request only, without granting agent control. Never solve CAPTCHAs automatically. Requires an active turn; waits for the sidebar connection and the native card or sidebar Confirm/Skip. Repeated calls on the same tab share one request. Returns done/skipped/cancelled plus a fresh page summary; inspect it before resuming. Never treat skipped/cancelled as success.',
|
|
595
|
+
parameters: {
|
|
596
|
+
reason: { type: 'string', required: true, description: 'Why the human must take over (1–1000 characters).' },
|
|
597
|
+
instructions: { type: 'string', description: 'Optional concise instructions for the human (up to 2000 characters).' },
|
|
598
|
+
},
|
|
599
|
+
output: { schema: { type: 'object', additionalProperties: false, properties: {
|
|
600
|
+
outcome: { type: 'string', required: true, enum: ['done', 'skipped', 'cancelled'] },
|
|
601
|
+
page: { type: 'object', required: true, additionalProperties: false, properties: {
|
|
602
|
+
url: { type: 'string', required: true }, title: { type: 'string', required: true },
|
|
603
|
+
snapshotHint: { type: 'string', required: true }, fresh: { type: 'boolean', required: true },
|
|
604
|
+
} },
|
|
605
|
+
} }, render: (_args, value) => [{ type: 'text', text: JSON.stringify(value) }] },
|
|
606
|
+
async execute(args, exec) {
|
|
607
|
+
if (!exec.agent?.session)
|
|
608
|
+
throw new Error('Browser takeover requires an active agent turn.');
|
|
609
|
+
if (!args.reason.trim())
|
|
610
|
+
throw new Error('Browser takeover reason must not be empty.');
|
|
611
|
+
if (args.reason.length > 1000 || (args.instructions?.length ?? 0) > 2000)
|
|
612
|
+
throw new Error('Browser takeover reason or instructions are too long.');
|
|
613
|
+
const owner = taskKey(exec);
|
|
614
|
+
const opening = state.pendingPanelOpens.get(owner);
|
|
615
|
+
if (opening)
|
|
616
|
+
await opening;
|
|
617
|
+
const open = getLastOpen();
|
|
618
|
+
const navigationUrl = open?.owner === owner && Date.now() - open.at < 10000 ? open.url : undefined;
|
|
619
|
+
const page = await takeovers.adopt(owner, exec.signal, navigationUrl);
|
|
620
|
+
if (navigationUrl !== undefined && getLastOpen()?.owner === owner)
|
|
621
|
+
clearLastOpen(open.at);
|
|
622
|
+
return takeovers.run(page, args.reason.trim(), args.instructions, exec.signal, async (pending) => {
|
|
623
|
+
const approval = ctx.get('approval');
|
|
624
|
+
let decision = 'unavailable';
|
|
625
|
+
try {
|
|
626
|
+
if (approval)
|
|
627
|
+
decision = await approval.request({ agent: exec.agent, toolName: TAKEOVER_TOOL, callId: exec.callId,
|
|
628
|
+
reason: pending.reason, displayReason: {
|
|
629
|
+
en: `Please take over the browser: ${pending.reason}. When finished, click Allow once or Confirm completion in the sidebar.${pending.instructions ? '\n' + pending.instructions : ''}`,
|
|
630
|
+
zh: `请接管浏览器:${pending.reason}(完成后点「允许一次」或侧栏「确认完成」)${pending.instructions ? '\n' + pending.instructions : ''}`,
|
|
631
|
+
}, signal: pending.controller.signal });
|
|
632
|
+
}
|
|
633
|
+
finally {
|
|
634
|
+
pending.controller.abort();
|
|
635
|
+
}
|
|
636
|
+
const outcome = decision === 'allowed-once' ? 'done' : decision === 'rejected' ? 'skipped' : 'cancelled';
|
|
637
|
+
const unavailable = (error) => ({ url: page.url, title: '', snapshotHint: `Live page summary unavailable: ${String(error).slice(0, 300)}. Re-read the page before continuing.`, fresh: false });
|
|
638
|
+
if (exec.signal.aborted)
|
|
639
|
+
return { outcome: 'cancelled', page: unavailable('turn stopped') };
|
|
640
|
+
pending.phase = 'summary';
|
|
641
|
+
let fresh;
|
|
642
|
+
try {
|
|
643
|
+
if (!page.pageId.startsWith('iframe:')) {
|
|
644
|
+
const browser = ctx.get('browser');
|
|
645
|
+
const session = await browser.bindPanelPage(`session-${page.owner}`, page.pageId);
|
|
646
|
+
const snapshot = await browser.snapshot(session, AbortSignal.any([exec.signal, AbortSignal.timeout(5000)]));
|
|
647
|
+
fresh = { url: snapshot.url, title: (snapshot.title ?? '').slice(0, 1000), snapshotHint: formatSnapshot(snapshot, {}).slice(0, 1500), fresh: true };
|
|
648
|
+
}
|
|
649
|
+
else {
|
|
650
|
+
fresh = await new Promise((resolve, reject) => {
|
|
651
|
+
const finish = (value, error) => {
|
|
652
|
+
clearTimeout(timer);
|
|
653
|
+
exec.signal.removeEventListener('abort', abort);
|
|
654
|
+
if (error !== undefined)
|
|
655
|
+
reject(error);
|
|
656
|
+
else
|
|
657
|
+
resolve(value);
|
|
658
|
+
};
|
|
659
|
+
const abort = () => { finish(undefined, exec.signal.reason); };
|
|
660
|
+
const timer = setTimeout(() => { finish(undefined, new Error('sidebar summary timed out')); }, 8000);
|
|
661
|
+
exec.signal.addEventListener('abort', abort, { once: true });
|
|
662
|
+
void pending.summary.then(value => { finish(value); });
|
|
663
|
+
if (exec.signal.aborted)
|
|
664
|
+
abort();
|
|
665
|
+
});
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
catch (error) {
|
|
669
|
+
fresh = unavailable(error);
|
|
670
|
+
}
|
|
671
|
+
return { outcome: exec.signal.aborted ? 'cancelled' : outcome, page: fresh };
|
|
672
|
+
});
|
|
673
|
+
},
|
|
674
|
+
}));
|
|
675
|
+
// The start page's shared shortcut list, reached through the settings section the
|
|
676
|
+
// sidebar edits. Built lazily for the same reason `references()` is: the settings
|
|
677
|
+
// service is registered by another entry and may not exist yet at apply time.
|
|
678
|
+
const startpageShortcuts = createShortcutStore(() => ctx.get('settings'));
|
|
679
|
+
registerSafely(defineTool({
|
|
680
|
+
name: 'dsh_browser_shortcuts',
|
|
681
|
+
description: 'Read or edit the Sidebar Browser start page shortcuts — the shared website entries a human sees on an empty tab. action=list returns them, action=add appends one (title + http(s) url required, http:// or https:// only, at most 48 entries), action=remove drops one by id. The list is shared with the sidebar UI: an entry added here appears on the next empty tab for every conversation, and one a human adds appears here.',
|
|
682
|
+
parameters: {
|
|
683
|
+
action: { type: 'string', required: true, enum: ['list', 'add', 'remove'], description: 'list the shortcuts, add one, or remove one by id.' },
|
|
684
|
+
title: { type: 'string', description: 'Display title (add). At most 80 characters.' },
|
|
685
|
+
url: { type: 'string', description: 'Destination (add): a full http:// or https:// address.' },
|
|
686
|
+
id: { type: 'string', description: 'Shortcut id (remove), as returned by list or add.' },
|
|
687
|
+
},
|
|
688
|
+
output: {
|
|
689
|
+
schema: {
|
|
690
|
+
type: 'object',
|
|
691
|
+
additionalProperties: false,
|
|
692
|
+
properties: {
|
|
693
|
+
shortcuts: {
|
|
694
|
+
type: 'array',
|
|
695
|
+
required: true,
|
|
696
|
+
items: {
|
|
697
|
+
type: 'object',
|
|
698
|
+
additionalProperties: false,
|
|
699
|
+
properties: {
|
|
700
|
+
id: { type: 'string', required: true },
|
|
701
|
+
title: { type: 'string', required: true },
|
|
702
|
+
url: { type: 'string', required: true },
|
|
703
|
+
iconKey: { type: 'string' },
|
|
704
|
+
},
|
|
705
|
+
},
|
|
706
|
+
},
|
|
707
|
+
changed: { type: 'string', description: 'What the call changed: added <id>, removed <id>, or empty for list.' },
|
|
708
|
+
},
|
|
709
|
+
},
|
|
710
|
+
render: (_args, value) => {
|
|
711
|
+
const rows = value.shortcuts;
|
|
712
|
+
const changed = typeof value.changed === 'string' && value.changed !== '' ? `${value.changed}\n` : '';
|
|
713
|
+
const body = rows.length === 0
|
|
714
|
+
? '(the start page has no shortcuts)'
|
|
715
|
+
: rows.map(row => `- ${row.title} -> ${row.url} [${row.id}]`).join('\n');
|
|
716
|
+
return [{ type: 'text', text: `${changed}${rows.length} shortcut(s):\n${body}` }];
|
|
717
|
+
},
|
|
718
|
+
},
|
|
719
|
+
timeoutMs,
|
|
720
|
+
isConcurrencySafe: () => false,
|
|
721
|
+
async execute(args, exec) {
|
|
722
|
+
assertAllowed(state, 'dsh_browser_shortcuts', taskKey(exec));
|
|
723
|
+
try {
|
|
724
|
+
if (args.action === 'list') {
|
|
725
|
+
const reading = startpageShortcuts.list();
|
|
726
|
+
return { shortcuts: reading.shortcuts.map(row => ({ ...row })), changed: '' };
|
|
727
|
+
}
|
|
728
|
+
if (args.action === 'add') {
|
|
729
|
+
const result = await startpageShortcuts.add({ title: args.title, url: args.url });
|
|
730
|
+
return { shortcuts: result.shortcuts.map(row => ({ ...row })), changed: `added ${result.shortcut.id}` };
|
|
731
|
+
}
|
|
732
|
+
if (typeof args.id !== 'string' || args.id === '')
|
|
733
|
+
throw new Error('remove needs the shortcut id');
|
|
734
|
+
const result = await startpageShortcuts.remove(args.id);
|
|
735
|
+
return {
|
|
736
|
+
shortcuts: result.shortcuts.map(row => ({ ...row })),
|
|
737
|
+
changed: result.removed ? `removed ${args.id}` : `${args.id} was not in the list`,
|
|
738
|
+
};
|
|
739
|
+
}
|
|
740
|
+
catch (error) {
|
|
741
|
+
// The store reports refusals as typed errors; a tool must not answer with a stack.
|
|
742
|
+
throw new Error(`start page shortcuts: ${error instanceof Error ? error.message : String(error)}`);
|
|
743
|
+
}
|
|
744
|
+
},
|
|
745
|
+
}));
|
|
341
746
|
registerSafely(defineTool({
|
|
342
747
|
name: 'dsh_browser_open',
|
|
343
|
-
description: 'Open a URL
|
|
748
|
+
description: 'Open a URL on the selected controllable surface (default: panel). Success returns a live snapshot and supports subsequent browser tools on that same page. A Web iframe or unavailable desktop bridge is refused; no manual-only navigation is queued. Reading tools never start another browser. newTab is optional on CDP surfaces. Never treat the bare DSH host (e.g. http://127.0.0.1:3080/) as the target — it needs the authenticated URL printed by `dsh web`, and the bare root is refused.',
|
|
344
749
|
parameters: {
|
|
345
750
|
url: { type: 'string', required: true, description: 'The URL to open (HTTP/HTTPS).' },
|
|
346
751
|
newTab: { type: 'boolean', description: 'Open in a new tab instead of the active one.' },
|
|
752
|
+
// Explicit per-call surface. "panel" (default) places the page in the DSH sidebar and opens no
|
|
753
|
+
// window; "builtin" drives the plugin's own browser, which is what debugging DSH itself needs.
|
|
754
|
+
surface: { type: 'string', enum: ['panel', 'builtin'], description: 'panel (default): the DSH sidebar only; builtin: the plugin browser, for debugging DSH itself.' },
|
|
347
755
|
},
|
|
348
756
|
output: {
|
|
349
757
|
schema: {
|
|
@@ -352,6 +760,7 @@ export function apply(ctx, config = {}) {
|
|
|
352
760
|
properties: {
|
|
353
761
|
url: { type: 'string', required: true },
|
|
354
762
|
title: { type: 'string' },
|
|
763
|
+
carrier: { type: 'string' },
|
|
355
764
|
truncated: { type: 'boolean' },
|
|
356
765
|
elements: {
|
|
357
766
|
type: 'array',
|
|
@@ -380,17 +789,120 @@ export function apply(ctx, config = {}) {
|
|
|
380
789
|
async execute(args, exec) {
|
|
381
790
|
assertAllowed(state, 'dsh_browser_open', taskKey(exec));
|
|
382
791
|
assertNotBareDshWebUrl(args.url);
|
|
792
|
+
// ONE surface per open. On the Web shell the reader's sidebar is the browsing surface; opening the
|
|
793
|
+
// plugin's own browser as well produced two live views of the same page (reported as "you opened the
|
|
794
|
+
// built-in browser AND the sidebar at once"). `browser.surface` decides: "panel" (default on Web)
|
|
795
|
+
// places the page in the sidebar and opens no window; "builtin" is the debugging surface.
|
|
796
|
+
const surfaceHint = args.surface;
|
|
797
|
+
const surface = enforceSurface(surfaceHint ?? readSurface(), args.url);
|
|
383
798
|
const browser = ctx.get('browser');
|
|
384
799
|
if (browser === undefined)
|
|
385
800
|
throw new Error('tool-browser: browser service unavailable');
|
|
386
|
-
const
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
801
|
+
const key = taskKey(exec);
|
|
802
|
+
if (surface === 'panel') {
|
|
803
|
+
const preceding = state.pendingPanelOpens.get(key);
|
|
804
|
+
let previous;
|
|
805
|
+
let session;
|
|
806
|
+
const opening = (async () => {
|
|
807
|
+
await preceding;
|
|
808
|
+
await state.pendingOpens.get(key);
|
|
809
|
+
previous = state.sessionsByTask.get(key);
|
|
810
|
+
const target = new URL(args.url);
|
|
811
|
+
if (target.protocol !== 'http:' && target.protocol !== 'https:')
|
|
812
|
+
throw new Error('dsh_browser_open: panel requires an HTTP/HTTPS URL');
|
|
813
|
+
exec.signal?.throwIfAborted();
|
|
814
|
+
session = state.panelUrlByTask.has(key) && previous !== undefined && browser.exists(previous)
|
|
815
|
+
? previous : await browser.openPanel?.(key);
|
|
816
|
+
if (session === undefined) {
|
|
817
|
+
assertSurfaceCanHost(surface, args.url);
|
|
818
|
+
exec.signal?.throwIfAborted();
|
|
819
|
+
setLastOpen(args.url, Date.now(), key);
|
|
820
|
+
rememberSession(browser, state, key, agentOf(exec), undefined, args.url);
|
|
821
|
+
return { url: args.url, carrier: 'panel-iframe-navigation-requested', title: '', elements: [], truncated: false };
|
|
822
|
+
}
|
|
823
|
+
await browser.openUrl(session, { url: args.url, ...args.newTab === true ? { newTab: true } : {} }, exec.signal);
|
|
824
|
+
const snapshot = await browser.snapshot(session, exec.signal);
|
|
825
|
+
exec.signal?.throwIfAborted();
|
|
826
|
+
rememberSession(browser, state, key, agentOf(exec), session, args.url);
|
|
827
|
+
return {
|
|
828
|
+
url: snapshot.url,
|
|
829
|
+
...snapshot.title !== undefined ? { title: snapshot.title } : {},
|
|
830
|
+
elements: snapshot.elements.map(el => projectElement(el)),
|
|
831
|
+
truncated: snapshot.truncated,
|
|
832
|
+
...snapshot.challenge !== undefined ? { challenge: snapshot.challenge } : {},
|
|
833
|
+
};
|
|
834
|
+
})();
|
|
835
|
+
const pending = opening.then(() => { });
|
|
836
|
+
state.pendingPanelOpens.set(key, pending);
|
|
837
|
+
void pending.catch(() => { });
|
|
838
|
+
try {
|
|
839
|
+
return await opening;
|
|
840
|
+
}
|
|
841
|
+
catch (error) {
|
|
842
|
+
if (session !== undefined && session !== previous)
|
|
843
|
+
await browser.close(session).catch(() => { });
|
|
844
|
+
throw error;
|
|
845
|
+
}
|
|
846
|
+
finally {
|
|
847
|
+
if (state.pendingPanelOpens.get(key) === pending)
|
|
848
|
+
state.pendingPanelOpens.delete(key);
|
|
849
|
+
}
|
|
850
|
+
}
|
|
851
|
+
await state.pendingPanelOpens.get(key);
|
|
852
|
+
await state.pendingOpens.get(key);
|
|
853
|
+
if (state.panelUrlByTask.has(key)) {
|
|
854
|
+
const previous = state.sessionsByTask.get(key);
|
|
855
|
+
state.sessionsByTask.delete(key);
|
|
856
|
+
state.panelUrlByTask.delete(key);
|
|
857
|
+
if (previous !== undefined)
|
|
858
|
+
await browser.close(previous);
|
|
859
|
+
}
|
|
860
|
+
const session = await ensureSession(browser, state, key, agentOf(exec), true);
|
|
861
|
+
try {
|
|
862
|
+
await browser.openUrl(session, {
|
|
863
|
+
url: args.url,
|
|
864
|
+
...args.newTab === true ? { newTab: true } : {},
|
|
865
|
+
}, exec.signal);
|
|
866
|
+
}
|
|
867
|
+
catch (error) {
|
|
868
|
+
// The human's browser is already running on its own profile: the launch hands the request over and
|
|
869
|
+
// exits with code 0. A new window opened inside THEIR browser with their logins — what "use my
|
|
870
|
+
// Edge" means — it is simply not driveable, because their instance has no debugging port.
|
|
871
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
872
|
+
if (!/exited immediately after launch \(exit code 0\)/u.test(message))
|
|
873
|
+
throw error;
|
|
874
|
+
return { url: args.url, carrier: 'launched-user-profile', title: '', elements: [], truncated: false };
|
|
875
|
+
}
|
|
876
|
+
// Hand the page to the Web sidebar as well. The desktop shell is told through the bridge; the Web
|
|
877
|
+
// shell has no such channel, so the client polls this slot and places the tab itself. Written in the
|
|
878
|
+
// TOOL (not a provider) on purpose: every open passes through here whatever serves it.
|
|
879
|
+
try {
|
|
880
|
+
setLastOpen(args.url, Date.now());
|
|
881
|
+
}
|
|
882
|
+
catch { /* best effort */ }
|
|
883
|
+
let snapshot;
|
|
884
|
+
try {
|
|
885
|
+
snapshot = await browser.snapshot(session, exec.signal);
|
|
886
|
+
}
|
|
887
|
+
catch (error) {
|
|
888
|
+
// The human's browser is ALREADY running on its own profile, so a launch hands the request over
|
|
889
|
+
// and the launcher exits with code 0. That is not a failure: a new window opened inside THEIR
|
|
890
|
+
// browser, with their logins, which is exactly what "use my Edge" means. It is only undriveable,
|
|
891
|
+
// because their instance has no debugging port. Report the page, not an error.
|
|
892
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
893
|
+
if (!/exited immediately after launch \(exit code 0\)/u.test(message))
|
|
894
|
+
throw error;
|
|
895
|
+
return {
|
|
896
|
+
url: args.url,
|
|
897
|
+
carrier: 'launched-user-profile',
|
|
898
|
+
title: '',
|
|
899
|
+
elements: [],
|
|
900
|
+
truncated: false,
|
|
901
|
+
};
|
|
902
|
+
}
|
|
392
903
|
return {
|
|
393
904
|
url: snapshot.url,
|
|
905
|
+
carrier: String(globalThis.__dshBrowserCarrier ?? 'bundled'),
|
|
394
906
|
...snapshot.title !== undefined ? { title: snapshot.title } : {},
|
|
395
907
|
elements: snapshot.elements.map(el => projectElement(el)),
|
|
396
908
|
truncated: snapshot.truncated,
|
|
@@ -453,7 +965,8 @@ export function apply(ctx, config = {}) {
|
|
|
453
965
|
const browser = ctx.get('browser');
|
|
454
966
|
if (browser === undefined)
|
|
455
967
|
throw new Error('tool-browser: browser service unavailable');
|
|
456
|
-
const
|
|
968
|
+
const key = taskKey(exec);
|
|
969
|
+
const session = await ensureSession(browser, state, key, agentOf(exec));
|
|
457
970
|
const snapshot = await browser.snapshot(session, exec.signal);
|
|
458
971
|
return {
|
|
459
972
|
url: snapshot.url,
|
|
@@ -782,8 +1295,8 @@ export function apply(ctx, config = {}) {
|
|
|
782
1295
|
y: { type: 'number', description: 'Viewport y coordinate (CSS px), taken from the same screenshot as x.' },
|
|
783
1296
|
},
|
|
784
1297
|
output: {
|
|
785
|
-
schema: { type: 'object', additionalProperties: false, properties: { clicked: { type: 'boolean', required: true } } },
|
|
786
|
-
render: (_args, value) => [{ type: 'text', text: value.clicked ? 'Clicked.' : 'Click failed.' }],
|
|
1298
|
+
schema: { type: 'object', additionalProperties: false, properties: { clicked: { type: 'boolean', required: true }, ...effectProperties } },
|
|
1299
|
+
render: (_args, value) => [{ type: 'text', text: value.clicked ? 'Clicked.' + effectText(value) : 'Click failed.' }],
|
|
787
1300
|
},
|
|
788
1301
|
timeoutMs,
|
|
789
1302
|
isConcurrencySafe: () => false,
|
|
@@ -801,6 +1314,7 @@ export function apply(ctx, config = {}) {
|
|
|
801
1314
|
throw new Error('target.value is empty; pass a css selector, visible text or XPath, or omit target entirely to use x/y');
|
|
802
1315
|
}
|
|
803
1316
|
const index = targetIndex(target);
|
|
1317
|
+
const before = await pageFingerprint(browser, session);
|
|
804
1318
|
if (target !== undefined && typeof target.value === 'string' && target.value !== '') {
|
|
805
1319
|
await browser.click(session, {
|
|
806
1320
|
target: {
|
|
@@ -816,7 +1330,7 @@ export function apply(ctx, config = {}) {
|
|
|
816
1330
|
}
|
|
817
1331
|
await browser.click(session, { x: args.x, y: args.y }, exec.signal);
|
|
818
1332
|
}
|
|
819
|
-
return { clicked: true };
|
|
1333
|
+
return { clicked: true, ...await observedEffect(browser, session, before) };
|
|
820
1334
|
},
|
|
821
1335
|
}));
|
|
822
1336
|
registerSafely(defineTool({
|
|
@@ -844,6 +1358,7 @@ export function apply(ctx, config = {}) {
|
|
|
844
1358
|
additionalProperties: false,
|
|
845
1359
|
properties: {
|
|
846
1360
|
clicked: { type: 'number', required: true, description: 'How many points landed.' },
|
|
1361
|
+
...effectProperties,
|
|
847
1362
|
failedAt: { type: 'number', description: 'Index of the point that failed, when one did.' },
|
|
848
1363
|
error: { type: 'string', description: 'Why that point failed.' },
|
|
849
1364
|
},
|
|
@@ -851,7 +1366,7 @@ export function apply(ctx, config = {}) {
|
|
|
851
1366
|
render: (_args, value) => [{
|
|
852
1367
|
type: 'text',
|
|
853
1368
|
text: value.failedAt === undefined
|
|
854
|
-
? `Clicked ${String(value.clicked)} point(s).`
|
|
1369
|
+
? `Clicked ${String(value.clicked)} point(s).` + effectText(value)
|
|
855
1370
|
: `Clicked ${String(value.clicked)} point(s), then stopped at index ${String(value.failedAt)}: ${String(value.error ?? 'unknown')}`,
|
|
856
1371
|
}],
|
|
857
1372
|
},
|
|
@@ -865,7 +1380,9 @@ export function apply(ctx, config = {}) {
|
|
|
865
1380
|
const session = await ensureSession(browser, state, taskKey(exec), agentOf(exec));
|
|
866
1381
|
const points = Array.isArray(args.points) ? args.points : [];
|
|
867
1382
|
const intervalMs = typeof args.intervalMs === 'number' ? args.intervalMs : undefined;
|
|
868
|
-
|
|
1383
|
+
const before = await pageFingerprint(browser, session);
|
|
1384
|
+
const result = await browser.clickSequence(session, { points, ...intervalMs === undefined ? {} : { intervalMs } });
|
|
1385
|
+
return { ...result, ...await observedEffect(browser, session, before) };
|
|
869
1386
|
},
|
|
870
1387
|
}));
|
|
871
1388
|
registerSafely(defineTool({
|
|
@@ -885,8 +1402,8 @@ export function apply(ctx, config = {}) {
|
|
|
885
1402
|
},
|
|
886
1403
|
},
|
|
887
1404
|
output: {
|
|
888
|
-
schema: { type: 'object', additionalProperties: false, properties: { typed: { type: 'boolean', required: true } } },
|
|
889
|
-
render: (_args, value) => [{ type: 'text', text: value.typed ? `Typed ${String(_args.text).length} chars.` : 'Type failed.' }],
|
|
1405
|
+
schema: { type: 'object', additionalProperties: false, properties: { typed: { type: 'boolean', required: true }, ...effectProperties } },
|
|
1406
|
+
render: (_args, value) => [{ type: 'text', text: value.typed ? `Typed ${String(_args.text).length} chars.` + effectText(value) : 'Type failed.' }],
|
|
890
1407
|
},
|
|
891
1408
|
timeoutMs,
|
|
892
1409
|
isConcurrencySafe: () => false,
|
|
@@ -904,6 +1421,7 @@ export function apply(ctx, config = {}) {
|
|
|
904
1421
|
throw new Error('target.value is empty; pass a css selector, visible text or XPath, or omit target entirely to use x/y');
|
|
905
1422
|
}
|
|
906
1423
|
const index = targetIndex(target);
|
|
1424
|
+
const before = await pageFingerprint(browser, session);
|
|
907
1425
|
if (target !== undefined && typeof target.value === 'string' && target.value !== '') {
|
|
908
1426
|
await browser.type(session, {
|
|
909
1427
|
text: args.text,
|
|
@@ -917,7 +1435,7 @@ export function apply(ctx, config = {}) {
|
|
|
917
1435
|
else {
|
|
918
1436
|
await browser.type(session, { text: args.text }, exec.signal);
|
|
919
1437
|
}
|
|
920
|
-
return { typed: true };
|
|
1438
|
+
return { typed: true, ...await observedEffect(browser, session, before) };
|
|
921
1439
|
},
|
|
922
1440
|
}));
|
|
923
1441
|
registerSafely(defineTool({
|
|
@@ -931,8 +1449,8 @@ export function apply(ctx, config = {}) {
|
|
|
931
1449
|
toBottom: { type: 'boolean', description: 'Scroll to the bottom of the page.' },
|
|
932
1450
|
},
|
|
933
1451
|
output: {
|
|
934
|
-
schema: { type: 'object', additionalProperties: false, properties: { scrolled: { type: 'boolean', required: true } } },
|
|
935
|
-
render: (_args, value) => [{ type: 'text', text: value.scrolled ? 'Scrolled.' : 'Scroll failed.' }],
|
|
1452
|
+
schema: { type: 'object', additionalProperties: false, properties: { scrolled: { type: 'boolean', required: true }, ...effectProperties } },
|
|
1453
|
+
render: (_args, value) => [{ type: 'text', text: value.scrolled ? 'Scrolled.' + effectText(value) : 'Scroll failed.' }],
|
|
936
1454
|
},
|
|
937
1455
|
timeoutMs,
|
|
938
1456
|
isConcurrencySafe: () => false, // mutates page scroll state; exclusive within a task
|
|
@@ -942,6 +1460,7 @@ export function apply(ctx, config = {}) {
|
|
|
942
1460
|
if (browser === undefined)
|
|
943
1461
|
throw new Error('tool-browser: browser service unavailable');
|
|
944
1462
|
const session = await ensureSession(browser, state, taskKey(exec), agentOf(exec));
|
|
1463
|
+
const before = await pageFingerprint(browser, session);
|
|
945
1464
|
await browser.scroll(session, {
|
|
946
1465
|
...args.deltaX !== undefined ? { deltaX: args.deltaX } : {},
|
|
947
1466
|
...args.deltaY !== undefined ? { deltaY: args.deltaY } : {},
|
|
@@ -949,7 +1468,7 @@ export function apply(ctx, config = {}) {
|
|
|
949
1468
|
...args.toTop === true ? { toTop: true } : {},
|
|
950
1469
|
...args.toBottom === true ? { toBottom: true } : {},
|
|
951
1470
|
}, exec.signal);
|
|
952
|
-
return { scrolled: true };
|
|
1471
|
+
return { scrolled: true, ...await observedEffect(browser, session, before) };
|
|
953
1472
|
},
|
|
954
1473
|
}));
|
|
955
1474
|
registerSafely(defineTool({
|
|
@@ -1019,8 +1538,8 @@ export function apply(ctx, config = {}) {
|
|
|
1019
1538
|
key: { type: 'string', required: true, enum: ['Enter', 'Tab', 'Escape', 'Backspace', 'Delete', 'ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'Home', 'End', 'PageUp', 'PageDown', 'Space'], description: 'The key to press.' },
|
|
1020
1539
|
},
|
|
1021
1540
|
output: {
|
|
1022
|
-
schema: { type: 'object', additionalProperties: false, properties: { pressed: { type: 'boolean', required: true } } },
|
|
1023
|
-
render: (_args, value) => [{ type: 'text', text: value.pressed ? `Pressed ${String(_args.key)}.` : 'Key press failed.' }],
|
|
1541
|
+
schema: { type: 'object', additionalProperties: false, properties: { pressed: { type: 'boolean', required: true }, ...effectProperties } },
|
|
1542
|
+
render: (_args, value) => [{ type: 'text', text: value.pressed ? `Pressed ${String(_args.key)}.` + effectText(value) : 'Key press failed.' }],
|
|
1024
1543
|
},
|
|
1025
1544
|
timeoutMs,
|
|
1026
1545
|
isConcurrencySafe: () => false, // mutates page state; exclusive within a task
|
|
@@ -1030,8 +1549,9 @@ export function apply(ctx, config = {}) {
|
|
|
1030
1549
|
if (browser === undefined)
|
|
1031
1550
|
throw new Error('tool-browser: browser service unavailable');
|
|
1032
1551
|
const session = await ensureSession(browser, state, taskKey(exec), agentOf(exec));
|
|
1552
|
+
const before = await pageFingerprint(browser, session);
|
|
1033
1553
|
await browser.key(session, { key: args.key }, exec.signal);
|
|
1034
|
-
return { pressed: true };
|
|
1554
|
+
return { pressed: true, ...await observedEffect(browser, session, before) };
|
|
1035
1555
|
},
|
|
1036
1556
|
}));
|
|
1037
1557
|
registerSafely(defineTool({
|
|
@@ -1334,10 +1854,10 @@ export function apply(ctx, config = {}) {
|
|
|
1334
1854
|
}));
|
|
1335
1855
|
registerSafely(defineTool({
|
|
1336
1856
|
name: 'dsh_browser_screenshot',
|
|
1337
|
-
description: 'Screenshot the
|
|
1857
|
+
description: 'Screenshot the Desktop browser (PNG default, JPEG where supported). Vision-capable active models receive the image directly; non-vision or unknown models receive text metadata only. Images are downscaled to at most 1280x960 and limited to 4 MiB; retry with smaller dimensions if encoding exceeds the limit. Supports fullPage and savePath. Web iframe screenshots are unavailable. Never solve CAPTCHAs automatically; request human takeover instead.',
|
|
1338
1858
|
parameters: {
|
|
1339
1859
|
fullPage: { type: 'boolean', description: 'Capture the full scrollable page instead of the viewport (default false).' },
|
|
1340
|
-
savePath: { type: 'string', description: 'Absolute file path to also save the image to
|
|
1860
|
+
savePath: { type: 'string', description: 'Absolute file path to also save the image to. Must resolve inside the configured downloadDir (default: the system Downloads folder, localized names such as ~/下载 included); an existing file is never overwritten.' },
|
|
1341
1861
|
format: { type: 'string', enum: ['png', 'jpeg'], description: 'Image format (default png; jpeg needs the self-hosted browser or an installed Chrome/Edge; the desktop sidebar returns PNG because the Electron CDP JPEG encoder hangs).' },
|
|
1342
1862
|
quality: { type: 'number', description: 'JPEG quality 1-100 (default 80); ignored for PNG.' },
|
|
1343
1863
|
maxWidth: { type: 'number', description: 'Downscale to fit within this width (aspect preserved).' },
|
|
@@ -1348,13 +1868,26 @@ export function apply(ctx, config = {}) {
|
|
|
1348
1868
|
type: 'object',
|
|
1349
1869
|
additionalProperties: false,
|
|
1350
1870
|
properties: {
|
|
1351
|
-
|
|
1871
|
+
bytes: { type: 'number', required: true, description: 'Encoded screenshot byte count.' },
|
|
1872
|
+
attachment: {
|
|
1873
|
+
type: 'object', additionalProperties: false,
|
|
1874
|
+
properties: {
|
|
1875
|
+
attachmentId: { type: 'string', required: true },
|
|
1876
|
+
mediaType: { type: 'string', required: true },
|
|
1877
|
+
bytes: { type: 'number', required: true },
|
|
1878
|
+
width: { type: 'number', required: true },
|
|
1879
|
+
height: { type: 'number', required: true },
|
|
1880
|
+
},
|
|
1881
|
+
},
|
|
1352
1882
|
width: { type: 'number', description: 'Pixel width of the returned image, when the capture was measured (present with maxWidth/maxHeight).' },
|
|
1353
1883
|
height: { type: 'number', description: 'Pixel height of the returned image, when the capture was measured.' },
|
|
1354
1884
|
path: { type: 'string', description: 'The file path the screenshot was saved to, when savePath was given.' },
|
|
1355
1885
|
},
|
|
1356
1886
|
},
|
|
1357
|
-
render: (_args, value) => [
|
|
1887
|
+
render: (_args, value) => [
|
|
1888
|
+
{ type: 'text', text: `Screenshot captured (${Math.round(value.bytes / 1024)} KiB${value.width !== undefined && value.height !== undefined ? `, ${value.width}x${value.height}` : ''})${value.path !== undefined ? ` saved to ${value.path}` : ''}.` },
|
|
1889
|
+
...value.attachment === undefined ? [] : [{ type: 'image', attachment: value.attachment }],
|
|
1890
|
+
],
|
|
1358
1891
|
},
|
|
1359
1892
|
timeoutMs,
|
|
1360
1893
|
isConcurrencySafe: () => true,
|
|
@@ -1362,22 +1895,32 @@ export function apply(ctx, config = {}) {
|
|
|
1362
1895
|
const browser = ctx.get('browser');
|
|
1363
1896
|
if (browser === undefined)
|
|
1364
1897
|
throw new Error('tool-browser: browser service unavailable');
|
|
1898
|
+
const key = taskKey(exec);
|
|
1899
|
+
await state.pendingPanelOpens.get(key);
|
|
1900
|
+
const existing = state.sessionsByTask.get(key);
|
|
1901
|
+
if (state.panelUrlByTask.has(key) && (existing === undefined || !browser.exists(existing))) {
|
|
1902
|
+
throw new Error('Web 侧栏暂不支持截图:iframe 无法被网页静默截图 (panel surface unavailable). Use the CDP-backed DSH desktop sidebar with a connected desktop bridge for screenshots.');
|
|
1903
|
+
}
|
|
1365
1904
|
const session = await ensureSession(browser, state, taskKey(exec), agentOf(exec));
|
|
1905
|
+
const vision = await screenshotVisionEnabled(ctx, exec, exec.signal);
|
|
1906
|
+
const maxWidth = screenshotDimension(args.maxWidth, SCREENSHOT_MAX_WIDTH);
|
|
1907
|
+
const maxHeight = screenshotDimension(args.maxHeight, SCREENSHOT_MAX_HEIGHT);
|
|
1366
1908
|
const shot = await browser.screenshot(session, {
|
|
1367
1909
|
...args.fullPage === true ? { fullPage: true } : {},
|
|
1368
1910
|
...args.savePath !== undefined ? { savePath: args.savePath } : {},
|
|
1369
1911
|
...args.format !== undefined ? { format: args.format } : {},
|
|
1370
1912
|
...args.quality !== undefined ? { quality: args.quality } : {},
|
|
1371
|
-
|
|
1372
|
-
|
|
1913
|
+
maxWidth,
|
|
1914
|
+
maxHeight,
|
|
1373
1915
|
}, exec.signal);
|
|
1916
|
+
exec.signal?.throwIfAborted();
|
|
1917
|
+
const image = screenshotBytes(shot.dataUrl);
|
|
1918
|
+
const attachment = vision ? await saveScreenshotImage(ctx, image, maxWidth, maxHeight) : undefined;
|
|
1919
|
+
exec.signal?.throwIfAborted();
|
|
1374
1920
|
return {
|
|
1375
|
-
|
|
1921
|
+
bytes: image.data.length,
|
|
1922
|
+
...attachment === undefined ? {} : { attachment },
|
|
1376
1923
|
...shot.path !== undefined ? { path: shot.path } : {},
|
|
1377
|
-
// The provider measures these when it scales, the schema declares them, and the seam
|
|
1378
|
-
// type carries them — but this mapping is an allow-list, so leaving them out dropped
|
|
1379
|
-
// them silently. Same shape of bug as the a11y selector, found the same way: by
|
|
1380
|
-
// reading the three places that mention a field and noticing this one did not.
|
|
1381
1924
|
...shot.width !== undefined ? { width: shot.width } : {},
|
|
1382
1925
|
...shot.height !== undefined ? { height: shot.height } : {},
|
|
1383
1926
|
};
|
|
@@ -1422,7 +1965,7 @@ export function apply(ctx, config = {}) {
|
|
|
1422
1965
|
throw new Error('tool-browser: browser service unavailable');
|
|
1423
1966
|
const session = await ensureSession(browser, state, taskKey(exec), agentOf(exec));
|
|
1424
1967
|
const tabs = await browser.listTabs(session);
|
|
1425
|
-
return { tabs: tabs.map(t => ({ id: t.id, url: t.url, active: t.active })) };
|
|
1968
|
+
return { tabs: scopedTabs(browserSessionHandle(taskKey(exec), session), tabs).map(t => ({ id: t.id, url: t.url, active: t.active })) };
|
|
1426
1969
|
},
|
|
1427
1970
|
}));
|
|
1428
1971
|
registerSafely(defineTool({
|
|
@@ -1443,7 +1986,8 @@ export function apply(ctx, config = {}) {
|
|
|
1443
1986
|
if (browser === undefined)
|
|
1444
1987
|
throw new Error('tool-browser: browser service unavailable');
|
|
1445
1988
|
const session = await ensureSession(browser, state, taskKey(exec), agentOf(exec));
|
|
1446
|
-
|
|
1989
|
+
const handle = resolveTabHandle(browserSessionHandle(taskKey(exec), session), args.tabId);
|
|
1990
|
+
await browser.switchTab(session, tabWireId(handle));
|
|
1447
1991
|
return { switched: true };
|
|
1448
1992
|
},
|
|
1449
1993
|
}));
|
|
@@ -1465,7 +2009,8 @@ export function apply(ctx, config = {}) {
|
|
|
1465
2009
|
if (browser === undefined)
|
|
1466
2010
|
throw new Error('tool-browser: browser service unavailable');
|
|
1467
2011
|
const session = await ensureSession(browser, state, taskKey(exec), agentOf(exec));
|
|
1468
|
-
|
|
2012
|
+
const handle = resolveTabHandle(browserSessionHandle(taskKey(exec), session), args.tabId);
|
|
2013
|
+
await browser.closeTab(session, tabWireId(handle));
|
|
1469
2014
|
return { closed: true };
|
|
1470
2015
|
},
|
|
1471
2016
|
}));
|
|
@@ -1834,7 +2379,8 @@ export function apply(ctx, config = {}) {
|
|
|
1834
2379
|
throw new Error('tool-browser: browser service unavailable');
|
|
1835
2380
|
const session = await ensureSession(browser, state, taskKey(exec), agentOf(exec));
|
|
1836
2381
|
const tabs = await browser.listTabs(session);
|
|
1837
|
-
|
|
2382
|
+
const scope = browserSessionHandle(taskKey(exec), session);
|
|
2383
|
+
return { session: sessionWireId(scope), tabs: scopedTabs(scope, tabs).map(t => ({ id: t.id, url: t.url, active: t.active })) };
|
|
1838
2384
|
},
|
|
1839
2385
|
}));
|
|
1840
2386
|
registerSafely(defineTool({
|
|
@@ -1853,6 +2399,8 @@ export function apply(ctx, config = {}) {
|
|
|
1853
2399
|
if (browser === undefined)
|
|
1854
2400
|
throw new Error('tool-browser: browser service unavailable');
|
|
1855
2401
|
const key = taskKey(exec);
|
|
2402
|
+
await state.pendingPanelOpens.get(key);
|
|
2403
|
+
state.panelUrlByTask.delete(key);
|
|
1856
2404
|
const session = state.sessionsByTask.get(key);
|
|
1857
2405
|
if (session !== undefined) {
|
|
1858
2406
|
try {
|
|
@@ -2308,6 +2856,7 @@ export function apply(ctx, config = {}) {
|
|
|
2308
2856
|
const session = await ensureSession(browser, state, taskKey(exec), agentOf(exec));
|
|
2309
2857
|
await installProbe(browser, session, exec.signal);
|
|
2310
2858
|
const cursor = await probe(browser, session, 'networkCursor', [], exec.signal);
|
|
2859
|
+
exec.armed?.();
|
|
2311
2860
|
const budget = Math.min(args.timeoutMs ?? 15_000, maxWaitMs);
|
|
2312
2861
|
const found = await poll(async () => {
|
|
2313
2862
|
const current = await probe(browser, session, 'networkCursor', [], exec.signal);
|
|
@@ -2880,12 +3429,327 @@ export function apply(ctx, config = {}) {
|
|
|
2880
3429
|
const browser = ctx.get('browser');
|
|
2881
3430
|
if (browser === undefined)
|
|
2882
3431
|
throw new Error('tool-browser: browser service unavailable');
|
|
2883
|
-
const
|
|
3432
|
+
const key = taskKey(exec);
|
|
3433
|
+
const session = await ensureSession(browser, state, key, agentOf(exec));
|
|
2884
3434
|
await installProbe(browser, session, exec.signal);
|
|
2885
3435
|
const summary = await probe(browser, session, 'summary', [], exec.signal);
|
|
2886
3436
|
return { summary };
|
|
2887
3437
|
},
|
|
2888
3438
|
}));
|
|
3439
|
+
registerSafely(defineTool({
|
|
3440
|
+
name: 'dsh_browser_reference_capture',
|
|
3441
|
+
description: 'Capture what the human has selected in the shared-browser page as a durable reference, so it can be quoted and found again later. The reference keeps the selected text with enough surrounding context to relocate it, any images selected with their real bytes, and any table as a structural snapshot. Tool-only captures are stored for later dsh_browser_reference_reveal or export and do not create a chat capsule; do not emit dsh-browser-ref: URIs, reference markers, or synthetic source links in the reply. Re-capturing the same region creates a NEW reference, because a stored reference is an immutable snapshot.',
|
|
3442
|
+
parameters: {
|
|
3443
|
+
allowPartial: { type: 'boolean', description: 'Commit the fragments that succeeded when another fragment failed (default false: a group with a broken image or oversized table is refused as a whole).' },
|
|
3444
|
+
tableSelector: { type: 'string', description: 'Capture this table whole (CSS selector) even when the selection does not touch it.' },
|
|
3445
|
+
excerptText: { type: 'string', description: 'With no live selection, register the first occurrence of this exact page text as an agent excerpt. Two occurrences make it ambiguous and are refused.' },
|
|
3446
|
+
},
|
|
3447
|
+
output: {
|
|
3448
|
+
schema: {
|
|
3449
|
+
type: 'object',
|
|
3450
|
+
additionalProperties: false,
|
|
3451
|
+
properties: {
|
|
3452
|
+
ok: { type: 'boolean', required: true },
|
|
3453
|
+
referenceId: { type: 'string' },
|
|
3454
|
+
pageUrl: { type: 'string' },
|
|
3455
|
+
title: { type: 'string' },
|
|
3456
|
+
parts: {
|
|
3457
|
+
type: 'array',
|
|
3458
|
+
required: true,
|
|
3459
|
+
items: {
|
|
3460
|
+
type: 'object',
|
|
3461
|
+
additionalProperties: false,
|
|
3462
|
+
properties: {
|
|
3463
|
+
partId: { type: 'string', required: true },
|
|
3464
|
+
type: { type: 'string', required: true },
|
|
3465
|
+
preview: { type: 'string', required: true },
|
|
3466
|
+
},
|
|
3467
|
+
},
|
|
3468
|
+
},
|
|
3469
|
+
failures: {
|
|
3470
|
+
type: 'array',
|
|
3471
|
+
required: true,
|
|
3472
|
+
items: {
|
|
3473
|
+
type: 'object',
|
|
3474
|
+
additionalProperties: false,
|
|
3475
|
+
properties: {
|
|
3476
|
+
kind: { type: 'string', required: true },
|
|
3477
|
+
reason: { type: 'string', required: true },
|
|
3478
|
+
},
|
|
3479
|
+
},
|
|
3480
|
+
},
|
|
3481
|
+
},
|
|
3482
|
+
},
|
|
3483
|
+
render: (_args, value) => {
|
|
3484
|
+
if (value.ok !== true) {
|
|
3485
|
+
const reasons = value.failures.map(failure => `${failure.kind}: ${failure.reason}`).join('; ');
|
|
3486
|
+
return [{ type: 'text', text: `Nothing was captured. ${reasons}` }];
|
|
3487
|
+
}
|
|
3488
|
+
const lines = [
|
|
3489
|
+
`Reference ${value.referenceId} captured from ${value.title !== undefined && value.title !== '' ? value.title : value.pageUrl}.`,
|
|
3490
|
+
'Stored in the reference library for dsh_browser_reference_reveal or export (tool-only captures do not create a chat capsule). Do not emit dsh-browser-ref: URIs or reference markers in the reply.',
|
|
3491
|
+
...value.parts.map(part => ` ${part.partId} (${part.type}): ${part.preview}`),
|
|
3492
|
+
];
|
|
3493
|
+
if (value.failures.length > 0)
|
|
3494
|
+
lines.push(`(${value.failures.length} fragment(s) failed and were left out: ${value.failures.map(failure => failure.reason).join('; ')})`);
|
|
3495
|
+
return [{ type: 'text', text: lines.join('\n') }];
|
|
3496
|
+
},
|
|
3497
|
+
},
|
|
3498
|
+
timeoutMs,
|
|
3499
|
+
isConcurrencySafe: () => false,
|
|
3500
|
+
async execute(args, exec) {
|
|
3501
|
+
const browser = ctx.get('browser');
|
|
3502
|
+
if (browser === undefined)
|
|
3503
|
+
throw new Error('tool-browser: browser service unavailable');
|
|
3504
|
+
const session = await ensureSession(browser, state, taskKey(exec), agentOf(exec));
|
|
3505
|
+
const outcome = await references().capture({
|
|
3506
|
+
// The DSH session owns the reference; the provider session holds the page. They are
|
|
3507
|
+
// different ids, and reveal needs the second one even after the first is long gone.
|
|
3508
|
+
sessionId: taskKey(exec),
|
|
3509
|
+
browserSessionId: session,
|
|
3510
|
+
origin: args.excerptText !== undefined ? 'agent-excerpt' : 'user-selection',
|
|
3511
|
+
entry: 'agent-tool',
|
|
3512
|
+
carrier: state.panelUrlByTask.has(taskKey(exec)) ? 'sidebar' : referenceCarrier(),
|
|
3513
|
+
...args.allowPartial !== undefined ? { allowPartial: args.allowPartial } : {},
|
|
3514
|
+
...args.tableSelector !== undefined ? { tableSelector: args.tableSelector } : {},
|
|
3515
|
+
...args.excerptText !== undefined ? { excerptText: args.excerptText } : {},
|
|
3516
|
+
}, exec.signal);
|
|
3517
|
+
const failures = outcome.failures.map(failure => ({ kind: failure.kind, reason: failure.reason }));
|
|
3518
|
+
if (outcome.reference === undefined)
|
|
3519
|
+
return { ok: false, parts: [], failures };
|
|
3520
|
+
const summary = summarizeReference(outcome.reference);
|
|
3521
|
+
return {
|
|
3522
|
+
ok: true,
|
|
3523
|
+
referenceId: summary.referenceId,
|
|
3524
|
+
pageUrl: summary.pageUrl,
|
|
3525
|
+
title: summary.title,
|
|
3526
|
+
// The summary's arrays are readonly; the schema declares a mutable array,
|
|
3527
|
+
// so the parts are projected into plain objects rather than cast away.
|
|
3528
|
+
parts: summary.parts.map(part => ({ partId: part.partId, type: part.type, preview: part.preview })),
|
|
3529
|
+
failures,
|
|
3530
|
+
};
|
|
3531
|
+
},
|
|
3532
|
+
}));
|
|
3533
|
+
registerSafely(defineTool({
|
|
3534
|
+
name: 'dsh_browser_reference_reveal',
|
|
3535
|
+
description: 'Return to a stored browser reference: select the browser session that captured it, verify the page is still the same one, then scroll to and highlight the part. Without partId the reference\'s first part is located; pass a partId to locate one specific fragment. A result status of page-mismatch means the session is showing another page (open the stored pageUrl first); ambiguous means several candidates matched equally and the exact one cannot be chosen; session-unavailable means the browser that captured it is gone.',
|
|
3536
|
+
parameters: {
|
|
3537
|
+
referenceId: { type: 'string', required: true, description: 'The reference id, with or without the dsh-browser-ref: prefix.' },
|
|
3538
|
+
partId: { type: 'string', description: 'Locate this fragment instead of the whole reference\'s first part.' },
|
|
3539
|
+
},
|
|
3540
|
+
output: {
|
|
3541
|
+
schema: {
|
|
3542
|
+
type: 'object',
|
|
3543
|
+
additionalProperties: false,
|
|
3544
|
+
properties: {
|
|
3545
|
+
status: { type: 'string', required: true },
|
|
3546
|
+
pageUrl: { type: 'string', required: true },
|
|
3547
|
+
currentUrl: { type: 'string' },
|
|
3548
|
+
note: { type: 'string' },
|
|
3549
|
+
parts: {
|
|
3550
|
+
type: 'array',
|
|
3551
|
+
required: true,
|
|
3552
|
+
items: {
|
|
3553
|
+
type: 'object',
|
|
3554
|
+
additionalProperties: false,
|
|
3555
|
+
properties: {
|
|
3556
|
+
partId: { type: 'string', required: true },
|
|
3557
|
+
type: { type: 'string', required: true },
|
|
3558
|
+
status: { type: 'string', required: true },
|
|
3559
|
+
matches: { type: 'number' },
|
|
3560
|
+
collapsed: { type: 'boolean' },
|
|
3561
|
+
note: { type: 'string' },
|
|
3562
|
+
},
|
|
3563
|
+
},
|
|
3564
|
+
},
|
|
3565
|
+
},
|
|
3566
|
+
},
|
|
3567
|
+
render: (_args, value) => {
|
|
3568
|
+
const head = value.status === 'revealed'
|
|
3569
|
+
? 'Located the reference in the page.'
|
|
3570
|
+
: `Could not locate the reference: ${value.status}${value.note !== undefined ? ` — ${value.note}` : ''}`;
|
|
3571
|
+
const lines = [head, ...value.parts.map(part => ` ${part.partId} (${part.type}): ${part.status}${part.matches !== undefined ? `, ${part.matches} match(es)` : ''}${part.collapsed === true ? ', whitespace-collapsed' : ''}${part.note !== undefined ? ` — ${part.note}` : ''}`)];
|
|
3572
|
+
return [{ type: 'text', text: lines.join('\n') }];
|
|
3573
|
+
},
|
|
3574
|
+
},
|
|
3575
|
+
timeoutMs,
|
|
3576
|
+
isConcurrencySafe: () => false,
|
|
3577
|
+
async execute(args, exec) {
|
|
3578
|
+
// The documented citation form is `dsh-browser-ref:<referenceId>/<partId?>`; accept it
|
|
3579
|
+
// whole so a model can paste what it wrote instead of splitting the id by hand.
|
|
3580
|
+
let referenceId = args.referenceId.startsWith('dsh-browser-ref:') ? args.referenceId.slice('dsh-browser-ref:'.length) : args.referenceId;
|
|
3581
|
+
let partId = args.partId;
|
|
3582
|
+
const slash = referenceId.indexOf('/');
|
|
3583
|
+
if (slash !== -1) {
|
|
3584
|
+
if (partId === undefined)
|
|
3585
|
+
partId = referenceId.slice(slash + 1);
|
|
3586
|
+
referenceId = referenceId.slice(0, slash);
|
|
3587
|
+
}
|
|
3588
|
+
const outcome = await references().reveal(referenceId, {
|
|
3589
|
+
ownerSessionId: taskKey(exec),
|
|
3590
|
+
...partId !== undefined && partId !== '' ? { partId } : {},
|
|
3591
|
+
}, exec.signal);
|
|
3592
|
+
return {
|
|
3593
|
+
status: outcome.status,
|
|
3594
|
+
pageUrl: outcome.pageUrl,
|
|
3595
|
+
...outcome.currentUrl !== undefined ? { currentUrl: outcome.currentUrl } : {},
|
|
3596
|
+
...outcome.note !== undefined ? { note: outcome.note } : {},
|
|
3597
|
+
parts: outcome.parts.map(part => ({
|
|
3598
|
+
partId: part.partId,
|
|
3599
|
+
type: part.type,
|
|
3600
|
+
status: part.status,
|
|
3601
|
+
...part.matches !== undefined ? { matches: part.matches } : {},
|
|
3602
|
+
...part.collapsed !== undefined ? { collapsed: part.collapsed } : {},
|
|
3603
|
+
...part.note !== undefined ? { note: part.note } : {},
|
|
3604
|
+
})),
|
|
3605
|
+
};
|
|
3606
|
+
},
|
|
3607
|
+
}));
|
|
3608
|
+
registerSafely(defineTool({
|
|
3609
|
+
name: 'dsh_browser_reference_list',
|
|
3610
|
+
description: 'List the browser references stored for this task, newest first: id, page, capture time, and one preview line per fragment. Use it to identify a reference from earlier in the conversation. Source navigation is provided by reference capsules; do not repeat reference URIs, source URLs, markers, or source/citation links in the reply.',
|
|
3611
|
+
parameters: {
|
|
3612
|
+
limit: { type: 'number', description: 'Most references to return (default 20, maximum 100).' },
|
|
3613
|
+
},
|
|
3614
|
+
output: {
|
|
3615
|
+
schema: {
|
|
3616
|
+
type: 'object',
|
|
3617
|
+
additionalProperties: false,
|
|
3618
|
+
properties: {
|
|
3619
|
+
references: {
|
|
3620
|
+
type: 'array',
|
|
3621
|
+
required: true,
|
|
3622
|
+
items: {
|
|
3623
|
+
type: 'object',
|
|
3624
|
+
additionalProperties: false,
|
|
3625
|
+
properties: {
|
|
3626
|
+
referenceId: { type: 'string', required: true },
|
|
3627
|
+
title: { type: 'string', required: true },
|
|
3628
|
+
pageUrl: { type: 'string', required: true },
|
|
3629
|
+
capturedAt: { type: 'string', required: true },
|
|
3630
|
+
carrier: { type: 'string', required: true },
|
|
3631
|
+
origin: { type: 'string', required: true },
|
|
3632
|
+
entry: { type: 'string', required: true },
|
|
3633
|
+
partCount: { type: 'number', required: true },
|
|
3634
|
+
parts: {
|
|
3635
|
+
type: 'array',
|
|
3636
|
+
required: true,
|
|
3637
|
+
items: {
|
|
3638
|
+
type: 'object',
|
|
3639
|
+
additionalProperties: false,
|
|
3640
|
+
properties: {
|
|
3641
|
+
partId: { type: 'string', required: true },
|
|
3642
|
+
type: { type: 'string', required: true },
|
|
3643
|
+
preview: { type: 'string', required: true },
|
|
3644
|
+
},
|
|
3645
|
+
},
|
|
3646
|
+
},
|
|
3647
|
+
},
|
|
3648
|
+
},
|
|
3649
|
+
},
|
|
3650
|
+
},
|
|
3651
|
+
},
|
|
3652
|
+
render: (_args, value) => {
|
|
3653
|
+
if (value.references.length === 0)
|
|
3654
|
+
return [{ type: 'text', text: 'No browser references are stored for this task.' }];
|
|
3655
|
+
return [{
|
|
3656
|
+
type: 'text',
|
|
3657
|
+
text: value.references.map(reference => [
|
|
3658
|
+
`Reference ${reference.referenceId} — ${reference.title !== '' ? reference.title : reference.pageUrl} (${reference.capturedAt}); source navigation is in its capsule, not a reply link.`,
|
|
3659
|
+
...reference.parts.map(part => ` ${part.partId} (${part.type}): ${part.preview}`),
|
|
3660
|
+
].join('\n')).join('\n\n'),
|
|
3661
|
+
}];
|
|
3662
|
+
},
|
|
3663
|
+
},
|
|
3664
|
+
timeoutMs,
|
|
3665
|
+
isConcurrencySafe: () => true,
|
|
3666
|
+
async execute(args, exec) {
|
|
3667
|
+
const key = taskKey(exec);
|
|
3668
|
+
const limit = Math.min(Math.max(Math.trunc(args.limit ?? 20), 1), 100);
|
|
3669
|
+
// Always scoped: with no agent the key is `default`, and an unfiltered read
|
|
3670
|
+
// would hand this task every conversation's references.
|
|
3671
|
+
const stored = references().list({ sessionId: key, limit });
|
|
3672
|
+
return {
|
|
3673
|
+
references: stored.slice().reverse().map(reference => {
|
|
3674
|
+
const summary = summarizeReference(reference);
|
|
3675
|
+
return {
|
|
3676
|
+
referenceId: summary.referenceId,
|
|
3677
|
+
title: summary.title,
|
|
3678
|
+
pageUrl: summary.pageUrl,
|
|
3679
|
+
capturedAt: summary.capturedAt,
|
|
3680
|
+
carrier: summary.carrier,
|
|
3681
|
+
origin: summary.origin,
|
|
3682
|
+
entry: summary.entry,
|
|
3683
|
+
partCount: summary.partCount,
|
|
3684
|
+
parts: summary.parts.map(part => ({ partId: part.partId, type: part.type, preview: part.preview })),
|
|
3685
|
+
};
|
|
3686
|
+
}),
|
|
3687
|
+
};
|
|
3688
|
+
},
|
|
3689
|
+
}));
|
|
3690
|
+
// Under DSH's PTC executor run_code already batches every tool, so the code tool steps aside.
|
|
3691
|
+
const ptcMode = () => {
|
|
3692
|
+
try {
|
|
3693
|
+
return ctx.get?.('ptcRuntime') !== undefined;
|
|
3694
|
+
}
|
|
3695
|
+
catch {
|
|
3696
|
+
return false;
|
|
3697
|
+
}
|
|
3698
|
+
};
|
|
3699
|
+
if (surface !== 'tools') {
|
|
3700
|
+
try {
|
|
3701
|
+
const code = createCodeTool(catalog);
|
|
3702
|
+
ctx.tools.register(code.tool, surface === 'core' ? { when: () => !ptcMode() } : undefined);
|
|
3703
|
+
ctx.effect(() => code.dispose);
|
|
3704
|
+
}
|
|
3705
|
+
catch (error) {
|
|
3706
|
+
ctx.logger?.warn?.(`dsh-builtin-browser: could not register ${CODE_TOOL} (${String(error)}).`);
|
|
3707
|
+
}
|
|
3708
|
+
}
|
|
3709
|
+
if (surface === 'core') {
|
|
3710
|
+
const grouped = new Set(Object.values(TOOL_GROUPS).flat());
|
|
3711
|
+
const members = (group) => group === 'more'
|
|
3712
|
+
? [...catalog.keys()].filter(name => !CORE_TOOLS.has(name) && !grouped.has(name)).sort()
|
|
3713
|
+
: (TOOL_GROUPS[group] ?? []).filter(name => catalog.has(name));
|
|
3714
|
+
try {
|
|
3715
|
+
const loader = defineTool({
|
|
3716
|
+
name: LOAD_TOOL,
|
|
3717
|
+
description: `Load more browser tools by group; they stay available for the rest of the run. Groups: ${Object.entries(TOOL_GROUPS).map(([group, names]) => `${group} (${group === 'more' ? 'every other browser tool' : names.map(name => name.slice('dsh_browser_'.length)).join(', ')})`).join('; ')}. dsh_browser_code can already call every tool without loading it.`,
|
|
3718
|
+
parameters: {
|
|
3719
|
+
groups: { type: 'array', items: { type: 'string', enum: Object.keys(TOOL_GROUPS) }, required: true, description: 'Groups to load.' },
|
|
3720
|
+
},
|
|
3721
|
+
output: {
|
|
3722
|
+
schema: { type: 'object', additionalProperties: false, properties: {
|
|
3723
|
+
loaded: { type: 'array', items: { type: 'string' }, required: true },
|
|
3724
|
+
available: { type: 'array', items: { type: 'string' }, required: true },
|
|
3725
|
+
} },
|
|
3726
|
+
render: (_args, value) => [{ type: 'text', text: value.loaded.length > 0 ? `Loaded: ${value.loaded.join(', ')}.` : `Already loaded: ${value.available.join(', ')}.` }],
|
|
3727
|
+
},
|
|
3728
|
+
isConcurrencySafe: () => false,
|
|
3729
|
+
async execute(args) {
|
|
3730
|
+
const groups = Array.isArray(args.groups) ? args.groups.map(String) : [];
|
|
3731
|
+
const unknown = groups.filter(group => !(group in TOOL_GROUPS));
|
|
3732
|
+
if (groups.length === 0 || unknown.length > 0)
|
|
3733
|
+
throw new Error(`unknown tool group(s): ${unknown.join(', ') || '(none given)'}; groups are ${Object.keys(TOOL_GROUPS).join(', ')}`);
|
|
3734
|
+
const names = [...new Set(groups.flatMap(members))];
|
|
3735
|
+
const fresh = [];
|
|
3736
|
+
for (const name of names) {
|
|
3737
|
+
const definition = catalog.get(name);
|
|
3738
|
+
if (definition === undefined || loaded.has(name))
|
|
3739
|
+
continue;
|
|
3740
|
+
ctx.tools.register(definition);
|
|
3741
|
+
loaded.add(name);
|
|
3742
|
+
fresh.push(name);
|
|
3743
|
+
}
|
|
3744
|
+
return { loaded: fresh, available: names };
|
|
3745
|
+
},
|
|
3746
|
+
});
|
|
3747
|
+
ctx.tools.register(loader);
|
|
3748
|
+
}
|
|
3749
|
+
catch (error) {
|
|
3750
|
+
ctx.logger?.warn?.(`dsh-builtin-browser: could not register ${LOAD_TOOL} (${String(error)}).`);
|
|
3751
|
+
}
|
|
3752
|
+
}
|
|
2889
3753
|
}
|
|
2890
3754
|
/** Test hook: inspect and reset session mappings across every live plugin apply. */
|
|
2891
3755
|
export const internals = {
|