dsh-builtin-browser 1.0.3 → 1.1.0
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 +16 -1
- package/README.en.md +89 -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/desktop-bridge/desktop-identity.json +10 -2
- package/desktop-bridge/install.mjs +28 -2
- package/docs/issue-replies-2026-11-28.md +141 -0
- package/dsh-host-patch/apply.mjs +1 -1
- package/dsh-host-patch/rc3.json +4 -4
- package/dsh-host-patch/rc5.json +4 -4
- package/dsh-host-patch/v023-alpha2.json +2 -2
- package/lib/browser-electron/bridge-status.js +24 -1
- package/lib/browser-electron/desktop-identity.js +47 -8
- package/lib/client.js +2 -1
- package/native-host/install.mjs +5 -0
- package/package.json +20 -2
- package/scripts/build-client.mjs +305 -0
- package/scripts/build.mjs +69 -0
- package/scripts/bundle-host.mjs +28 -0
- package/scripts/fix-exports.mjs +40 -0
- package/scripts/install-electron.mjs +101 -0
- package/scripts/pack.mjs +99 -0
- package/scripts/publish-manifest.mjs +56 -0
- package/scripts/verify-exports.mjs +58 -0
- package/scripts/verify-preview-sdk.mjs +93 -0
- package/skills/dsh-builtin-browser/SKILL.md +98 -0
- package/skills/dsh-builtin-browser/references/batch.md +68 -0
- package/skills/dsh-builtin-browser/references/deliver.md +31 -0
- package/skills/dsh-builtin-browser/references/locate.md +11 -0
- package/skills/dsh-builtin-browser/references/recover.md +11 -0
- package/skills/dsh-builtin-browser/references/verify.md +13 -0
- package/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/anti-bot.ts +251 -0
- package/src/browser/deep-dom.ts +40 -0
- package/src/browser/handles.ts +89 -0
- package/src/browser/runtime.ts +578 -0
- package/src/browser/takeover-target.ts +45 -0
- package/src/browser/types.ts +870 -0
- package/src/browser-electron/authenticated-route.ts +109 -0
- package/src/browser-electron/automatic-maintenance.ts +736 -0
- package/src/browser-electron/bridge-auto.ts +159 -0
- package/src/browser-electron/bridge-connection.ts +386 -0
- package/src/browser-electron/bridge-endpoint.ts +129 -0
- package/src/browser-electron/bridge-install.ts +290 -0
- package/src/browser-electron/bridge-maintenance.ts +189 -0
- package/src/browser-electron/bridge-status.ts +412 -0
- package/src/browser-electron/desktop-bridge-host.ts +253 -0
- package/src/browser-electron/desktop-identity.ts +400 -0
- package/src/browser-electron/desktop-shell.ts +100 -0
- package/src/browser-electron/document-readiness.ts +45 -0
- package/src/browser-electron/electron-provision.ts +347 -0
- package/src/browser-electron/entry.ts +1378 -0
- package/src/browser-electron/ephemeral-profile.ts +189 -0
- package/src/browser-electron/extension-browser.ts +230 -0
- package/src/browser-electron/extension-setup.ts +67 -0
- package/src/browser-electron/file-write.ts +31 -0
- package/src/browser-electron/foreground-watch.ts +142 -0
- package/src/browser-electron/guest-cookie-jar.ts +177 -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 +716 -0
- package/src/browser-electron/guest-runtime.ts +408 -0
- package/src/browser-electron/guest-storage-runtime.ts +54 -0
- package/src/browser-electron/guest-storage.ts +120 -0
- package/src/browser-electron/history-store.ts +224 -0
- package/src/browser-electron/host-main.ts +1385 -0
- package/src/browser-electron/host-patch-maintenance.ts +121 -0
- package/src/browser-electron/http-body.ts +65 -0
- package/src/browser-electron/json-reply.ts +47 -0
- package/src/browser-electron/maintenance-broker.ts +166 -0
- package/src/browser-electron/maintenance-lifecycle.ts +91 -0
- package/src/browser-electron/maintenance-route.ts +68 -0
- package/src/browser-electron/missing-system-browser.ts +114 -0
- package/src/browser-electron/open-slot.ts +49 -0
- package/src/browser-electron/paths.ts +25 -0
- package/src/browser-electron/preferred-browser.ts +130 -0
- package/src/browser-electron/preview-route.ts +215 -0
- package/src/browser-electron/process-ownership.ts +174 -0
- package/src/browser-electron/process.ts +23 -0
- package/src/browser-electron/profile-checkpoint.ts +187 -0
- package/src/browser-electron/profile-command.ts +47 -0
- package/src/browser-electron/profile-extension.ts +129 -0
- package/src/browser-electron/profile-lease.ts +211 -0
- package/src/browser-electron/profile-residue.ts +49 -0
- package/src/browser-electron/provider-core.ts +1107 -0
- package/src/browser-electron/provider-interaction.ts +1034 -0
- package/src/browser-electron/provider-media.ts +589 -0
- package/src/browser-electron/provider-navigation.ts +323 -0
- package/src/browser-electron/provider-observation.ts +664 -0
- package/src/browser-electron/provider-tabs.ts +372 -0
- package/src/browser-electron/provider.ts +666 -0
- package/src/browser-electron/reference-route.ts +598 -0
- package/src/browser-electron/remote-host.ts +1538 -0
- package/src/browser-electron/rpc-input.ts +13 -0
- package/src/browser-electron/rpc-ops.ts +41 -0
- package/src/browser-electron/runtime-diagnostics.ts +38 -0
- package/src/browser-electron/runtime-route.ts +105 -0
- package/src/browser-electron/runtime-status.ts +309 -0
- package/src/browser-electron/settings-route.ts +64 -0
- package/src/browser-electron/settings-store.ts +593 -0
- package/src/browser-electron/shortcuts-route.ts +112 -0
- package/src/browser-electron/sidebar-only-host.ts +300 -0
- package/src/browser-electron/startpage-shortcuts.ts +213 -0
- package/src/browser-electron/system-browser.ts +1850 -0
- package/src/browser-electron/tauri-host.ts +194 -0
- package/src/browser-electron/timing.ts +16 -0
- package/src/browser-electron/virtual-cursor.ts +162 -0
- package/src/browser-electron/windows-system.ts +24 -0
- 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 +1346 -0
- package/src/browser-reference/store.ts +172 -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 +106 -0
- package/src/client/apply-guard.ts +22 -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 +182 -0
- package/src/client/browser/child-tabs.ts +14 -0
- package/src/client/browser/frame-history.ts +125 -0
- package/src/client/browser/frame-state.ts +27 -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 +56 -0
- package/src/client/css-modules.d.ts +4 -0
- package/src/client/definition.tsx +36 -0
- package/src/client/electron/ElectronWebViewImpl.ts +371 -0
- package/src/client/electron/ElectronWebviewPresentation.ts +140 -0
- package/src/client/electron/NativeChildPresentation.ts +53 -0
- package/src/client/electron/NativeViewPage.ts +265 -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 +333 -0
- package/src/client/locales.ts +860 -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 +208 -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 +447 -0
- package/src/client/reference/client/production/asset-store.ts +144 -0
- package/src/client/reference/client/production/availability.ts +72 -0
- package/src/client/reference/client/production/bytes.ts +36 -0
- package/src/client/reference/client/production/composer-attachments.ts +44 -0
- package/src/client/reference/client/production/delivery-client.ts +211 -0
- package/src/client/reference/client/production/draft-adapter.ts +512 -0
- package/src/client/reference/client/production/http-transport.ts +212 -0
- package/src/client/reference/client/production/production-submission.ts +70 -0
- package/src/client/reference/client/production/protocol.ts +234 -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 +1056 -0
- package/src/client/reference/client/production/snapshot-adapter.ts +154 -0
- package/src/client/reference/client/production/source-marker.ts +99 -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 +67 -0
- package/src/client/reference/client/reveal.ts +109 -0
- package/src/client/reference/client/scheme.ts +114 -0
- package/src/client/reference/client/session-schedule.ts +36 -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 +42 -0
- package/src/client/settings/mount-guard.ts +31 -0
- package/src/client/settings/panel.ts +2773 -0
- package/src/client/shortcut-open.ts +30 -0
- package/src/client/startpage/binding.ts +99 -0
- package/src/client/startpage/controller.ts +300 -0
- package/src/client/startpage/model.ts +279 -0
- package/src/client/view/Browser.module.css +751 -0
- package/src/client/view/BrowserBody.tsx +315 -0
- package/src/client/view/BrowserPresentation.ts +9 -0
- package/src/client/view/BrowserTitle.tsx +17 -0
- package/src/client/view/IframePresentation.ts +365 -0
- package/src/client/view/StartPage.tsx +598 -0
- package/src/client/view/TakeoverBanner.tsx +21 -0
- package/src/compat/host-features.ts +92 -0
- package/src/index.ts +110 -0
- 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 +219 -0
- package/src/startpage-settings.ts +103 -0
- package/src/startpage.ts +71 -0
- package/src/takeover-protocol.ts +28 -0
- package/src/tool-browser/block-recovery.ts +85 -0
- package/src/tool-browser/code-tool.ts +555 -0
- package/src/tool-browser/code-worker.ts +725 -0
- package/src/tool-browser/element-fields.ts +167 -0
- package/src/tool-browser/errors.ts +40 -0
- package/src/tool-browser/index.ts +265 -0
- package/src/tool-browser/page-probe.ts +675 -0
- package/src/tool-browser/panel-page.ts +126 -0
- package/src/tool-browser/probe-client.ts +183 -0
- package/src/tool-browser/screenshot-annotations.ts +131 -0
- package/src/tool-browser/screenshot-image.ts +49 -0
- package/src/tool-browser/state.ts +375 -0
- package/src/tool-browser/takeover.ts +199 -0
- package/src/tool-browser/tools-debug.ts +806 -0
- package/src/tool-browser/tools-interaction.ts +956 -0
- package/src/tool-browser/tools-navigation.ts +786 -0
- package/src/tool-browser/tools-observation.ts +474 -0
- package/src/tool-browser/tools-reference.ts +294 -0
- package/src/types/electron-shim.d.ts +144 -0
- package/src/types.ts +85 -0
- package/src/web-guest.ts +35 -0
- package/tools/accept-desktop.mjs +148 -0
- package/dist/client.d.cts +0 -516
- package/dist/client.d.mts +0 -516
- package/dist/index.cjs +0 -92
- package/dist/index.d.mts +0 -2960
- package/dist/index.mjs +0 -24
- package/dsh-builtin-browser-1.0.3-publish.tgz +0 -0
- package/dsh-builtin-browser-1.0.3.tgz +0 -0
- package/dsh-host-patch/sources/main/0001-feat-sidebar-right-add-session-rendering-holds.patch.gz +0 -0
- package/dsh-host-patch/sources/main/0002-feat-desktop-load-trusted-profile-main-extensions.patch.gz +0 -0
- package/dsh-host-patch/sources/main/0003-feat-desktop-report-resolved-and-running-main-extens.patch.gz +0 -0
- package/dsh-host-patch/sources/main/0004-feat-desktop-compose-admitted-native-sidebar-views.patch.gz +0 -0
- package/dsh-host-patch/sources/main/0005-fix-client-admit-coded-browser-errors-in-native-comp.patch.gz +0 -0
- package/dsh-host-patch/sources/main/0006-fix-desktop-acknowledge-native-same-document-navigat.patch.gz +0 -0
- package/dsh-host-patch/sources/main/0007-fix-desktop-restore-auth-in-the-native-lease-partiti.patch.gz +0 -0
- package/dsh-host-patch/sources/main/0008-feat-desktop-adopt-native-children-in-admitted-sideb.patch.gz +0 -0
- package/dsh-host-patch/sources/main/0009-fix-desktop-publish-complete-native-child-close-reco.patch.gz +0 -0
- package/dsh-host-patch/sources/main/0010-fix-desktop-bundle-structured-errors-in-sandboxed-pr.patch.gz +0 -0
- package/dsh-host-patch/sources/main/0011-feat-desktop-offer-persistent-Sidebar-sessions-per-D.patch.gz +0 -0
- package/dsh-host-patch/sources/main/0012-feat-desktop-retain-sidebar-session-cookies-across-r.patch.gz +0 -0
- package/dsh-host-patch/sources/main/0013-fix-desktop-honor-sidebar-session-cookie-sidecar-con.patch.gz +0 -0
- package/dsh-host-patch/sources/main/0014-fix-desktop-retry-cookie-protection-without-failing-.patch.gz +0 -0
- package/dsh-host-patch/sources/main/0015-fix-desktop-preserve-host-only-cookie-identity-in-se.patch.gz +0 -0
- package/dsh-host-patch/sources/main/0016-fix-desktop-restore-session-cookies-before-checkpoin.patch.gz +0 -0
- package/dsh-host-patch/sources/main/0017-fix-desktop-restore-sidebar-cookies-once-before-gues.patch.gz +0 -0
- package/dsh-host-patch/sources/main/0018-fix-sidebar-right-follow-animated-native-slot-placem.patch.gz +0 -0
- package/dsh-host-patch/sources/manifest.json +0 -218
- package/dsh-host-patch/sources/rc3/0001-feat-sidebar-right-add-session-rendering-holds.patch.gz +0 -0
- package/dsh-host-patch/sources/rc3/0002-feat-desktop-load-trusted-profile-main-extensions.patch.gz +0 -0
- package/dsh-host-patch/sources/rc3/0003-feat-desktop-report-resolved-and-running-main-extens.patch.gz +0 -0
- package/dsh-host-patch/sources/rc3/0004-feat-desktop-compose-admitted-native-sidebar-views.patch.gz +0 -0
- package/dsh-host-patch/sources/rc3/0005-fix-client-admit-coded-browser-errors-in-native-comp.patch.gz +0 -0
- package/dsh-host-patch/sources/rc3/0006-fix-desktop-acknowledge-native-same-document-navigat.patch.gz +0 -0
- package/dsh-host-patch/sources/rc3/0007-fix-desktop-restore-auth-in-the-native-lease-partiti.patch.gz +0 -0
- package/dsh-host-patch/sources/rc3/0008-feat-desktop-adopt-native-children-in-admitted-sideb.patch.gz +0 -0
- package/dsh-host-patch/sources/rc3/0009-fix-desktop-publish-complete-native-child-close-reco.patch.gz +0 -0
- package/dsh-host-patch/sources/rc3/0010-fix-desktop-bundle-structured-errors-in-sandboxed-pr.patch.gz +0 -0
- package/dsh-host-patch/sources/rc3/0011-feat-desktop-offer-persistent-Sidebar-sessions-per-D.patch.gz +0 -0
- package/dsh-host-patch/sources/rc3/0012-feat-desktop-retain-sidebar-session-cookies-across-r.patch.gz +0 -0
- package/dsh-host-patch/sources/rc3/0013-fix-desktop-honor-sidebar-session-cookie-sidecar-con.patch.gz +0 -0
- package/dsh-host-patch/sources/rc3/0014-fix-desktop-retry-cookie-protection-without-failing-.patch.gz +0 -0
- package/dsh-host-patch/sources/rc3/0015-fix-desktop-preserve-host-only-cookie-identity-in-se.patch.gz +0 -0
- package/dsh-host-patch/sources/rc3/0016-fix-desktop-restore-session-cookies-before-checkpoin.patch.gz +0 -0
- package/dsh-host-patch/sources/rc3/0017-fix-desktop-restore-sidebar-cookies-once-before-gues.patch.gz +0 -0
- package/dsh-host-patch/sources/rc3/0018-fix-sidebar-right-follow-animated-native-slot-placem.patch.gz +0 -0
- package/dsh-host-patch/sources/rc5/0001-feat-sidebar-right-add-session-rendering-holds.patch.gz +0 -0
- package/dsh-host-patch/sources/rc5/0002-feat-desktop-load-trusted-profile-main-extensions.patch.gz +0 -0
- package/dsh-host-patch/sources/rc5/0003-feat-desktop-report-resolved-and-running-main-extens.patch.gz +0 -0
- package/dsh-host-patch/sources/rc5/0004-feat-desktop-compose-admitted-native-sidebar-views.patch.gz +0 -0
- package/dsh-host-patch/sources/rc5/0005-fix-client-admit-coded-browser-errors-in-native-comp.patch.gz +0 -0
- package/dsh-host-patch/sources/rc5/0006-fix-desktop-acknowledge-native-same-document-navigat.patch.gz +0 -0
- package/dsh-host-patch/sources/rc5/0007-fix-desktop-restore-auth-in-the-native-lease-partiti.patch.gz +0 -0
- package/dsh-host-patch/sources/rc5/0008-feat-desktop-adopt-native-children-in-admitted-sideb.patch.gz +0 -0
- package/dsh-host-patch/sources/rc5/0009-fix-desktop-publish-complete-native-child-close-reco.patch.gz +0 -0
- package/dsh-host-patch/sources/rc5/0010-fix-desktop-bundle-structured-errors-in-sandboxed-pr.patch.gz +0 -0
- package/dsh-host-patch/sources/rc5/0011-feat-desktop-offer-persistent-Sidebar-sessions-per-D.patch.gz +0 -0
- package/dsh-host-patch/sources/rc5/0012-feat-desktop-retain-sidebar-session-cookies-across-r.patch.gz +0 -0
- package/dsh-host-patch/sources/rc5/0013-fix-desktop-honor-sidebar-session-cookie-sidecar-con.patch.gz +0 -0
- package/dsh-host-patch/sources/rc5/0014-fix-desktop-retry-cookie-protection-without-failing-.patch.gz +0 -0
- package/dsh-host-patch/sources/rc5/0015-fix-desktop-preserve-host-only-cookie-identity-in-se.patch.gz +0 -0
- package/dsh-host-patch/sources/rc5/0016-fix-desktop-restore-session-cookies-before-checkpoin.patch.gz +0 -0
- package/dsh-host-patch/sources/rc5/0017-fix-desktop-restore-sidebar-cookies-once-before-gues.patch.gz +0 -0
- package/dsh-host-patch/sources/rc5/0018-fix-sidebar-right-follow-animated-native-slot-placem.patch.gz +0 -0
- package/dsh-host-patch/sources/series.json +0 -260
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { SHORTCUTS_ROUTE, SHORTCUT_ADD_ROUTE, SHORTCUT_REMOVE_ROUTE } from '../../assets/browser-policy.mjs'
|
|
2
|
+
/**
|
|
3
|
+
* HTTP face of the shared start-page shortcut list.
|
|
4
|
+
*
|
|
5
|
+
* Three exact paths, deliberately minimal: the sidebar keeps editing the section
|
|
6
|
+
* through the settings RPC (its own UI), while this face exists so an agent — which
|
|
7
|
+
* has no sidebar and no settings form — can list, add and drop shortcuts on the same
|
|
8
|
+
* data. Both writers go through `ctx.settings`, so there is exactly one list.
|
|
9
|
+
* @module dsh-browser/browser-electron/shortcuts-route
|
|
10
|
+
*/
|
|
11
|
+
import type { IncomingMessage, ServerResponse } from 'node:http'
|
|
12
|
+
import type { BrowserHttpHandler } from './authenticated-route.ts'
|
|
13
|
+
import { isDirectLoopback } from './authenticated-route.ts'
|
|
14
|
+
import { readJsonBody } from './http-body.ts'
|
|
15
|
+
import { replyJson } from './json-reply.ts'
|
|
16
|
+
import { sameOrigin } from './settings-route.ts'
|
|
17
|
+
import { ShortcutError, type createShortcutStore } from './startpage-shortcuts.ts'
|
|
18
|
+
|
|
19
|
+
/** Read the shared shortcut list. */
|
|
20
|
+
export { SHORTCUTS_ROUTE }
|
|
21
|
+
/** Append one shortcut. */
|
|
22
|
+
export { SHORTCUT_ADD_ROUTE }
|
|
23
|
+
/** Drop one shortcut by id. */
|
|
24
|
+
export { SHORTCUT_REMOVE_ROUTE }
|
|
25
|
+
|
|
26
|
+
type Store = ReturnType<typeof createShortcutStore>
|
|
27
|
+
|
|
28
|
+
/** The handlers, one per registered exact path. */
|
|
29
|
+
export interface ShortcutRouteHandlers {
|
|
30
|
+
readonly list: BrowserHttpHandler
|
|
31
|
+
readonly add: BrowserHttpHandler
|
|
32
|
+
readonly remove: BrowserHttpHandler
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function requireWrite(request: IncomingMessage, response: ServerResponse): boolean {
|
|
36
|
+
if (request.method !== 'POST') { replyJson(response, 405, { ok: false, error: 'method-not-allowed' }); return false }
|
|
37
|
+
if (!sameOrigin(request, true)) { replyJson(response, 403, { ok: false, error: 'not-same-origin' }); return false }
|
|
38
|
+
if (!isDirectLoopback(request)) { replyJson(response, 403, { ok: false, error: 'local-request-required' }); return false }
|
|
39
|
+
return true
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Turn a refusal into a status a caller can branch on.
|
|
44
|
+
* @param error - anything a handler threw.
|
|
45
|
+
* @param response - the response to write when it is a known refusal.
|
|
46
|
+
* @returns true when the error was answered.
|
|
47
|
+
*/
|
|
48
|
+
function refuse(error: unknown, response: ServerResponse): boolean {
|
|
49
|
+
if (!(error instanceof ShortcutError)) return false
|
|
50
|
+
const status = error.code === 'unavailable' ? 503
|
|
51
|
+
: error.code === 'conflict' ? 409
|
|
52
|
+
: error.code === 'limit' ? 422
|
|
53
|
+
: 400
|
|
54
|
+
replyJson(response, status, { ok: false, error: error.code, message: error.message })
|
|
55
|
+
return true
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Build every shortcut handler over one store. */
|
|
59
|
+
export function createShortcutRouteHandlers(store: Store): ShortcutRouteHandlers {
|
|
60
|
+
const list: BrowserHttpHandler = (request, response) => {
|
|
61
|
+
if (request.method !== 'GET') { replyJson(response, 405, { ok: false, error: 'method-not-allowed' }); return }
|
|
62
|
+
try {
|
|
63
|
+
const reading = store.list()
|
|
64
|
+
replyJson(response, 200, { ok: true, shortcuts: reading.shortcuts, revision: reading.revision })
|
|
65
|
+
} catch (error) {
|
|
66
|
+
if (refuse(error, response)) return
|
|
67
|
+
replyJson(response, 500, { ok: false, error: String(error) })
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const add: BrowserHttpHandler = async (request, response) => {
|
|
72
|
+
if (!requireWrite(request, response)) return
|
|
73
|
+
let body: Record<string, unknown>
|
|
74
|
+
try {
|
|
75
|
+
body = await readJsonBody<Record<string, unknown>>(request)
|
|
76
|
+
} catch (error) {
|
|
77
|
+
replyJson(response, 400, { ok: false, error: `unreadable body: ${String(error)}` })
|
|
78
|
+
return
|
|
79
|
+
}
|
|
80
|
+
try {
|
|
81
|
+
const result = await store.add({ title: body.title, url: body.url, iconKey: body.iconKey })
|
|
82
|
+
replyJson(response, 200, { ok: true, shortcut: result.shortcut, shortcuts: result.shortcuts })
|
|
83
|
+
} catch (error) {
|
|
84
|
+
if (refuse(error, response)) return
|
|
85
|
+
replyJson(response, 500, { ok: false, error: String(error) })
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const remove: BrowserHttpHandler = async (request, response) => {
|
|
90
|
+
if (!requireWrite(request, response)) return
|
|
91
|
+
let body: Record<string, unknown>
|
|
92
|
+
try {
|
|
93
|
+
body = await readJsonBody<Record<string, unknown>>(request)
|
|
94
|
+
} catch (error) {
|
|
95
|
+
replyJson(response, 400, { ok: false, error: `unreadable body: ${String(error)}` })
|
|
96
|
+
return
|
|
97
|
+
}
|
|
98
|
+
if (typeof body.id !== 'string' || body.id === '') {
|
|
99
|
+
replyJson(response, 400, { ok: false, error: 'id is required' })
|
|
100
|
+
return
|
|
101
|
+
}
|
|
102
|
+
try {
|
|
103
|
+
const result = await store.remove(body.id)
|
|
104
|
+
replyJson(response, result.removed ? 200 : 404, { ok: result.removed, shortcuts: result.shortcuts })
|
|
105
|
+
} catch (error) {
|
|
106
|
+
if (refuse(error, response)) return
|
|
107
|
+
replyJson(response, 500, { ok: false, error: String(error) })
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return { list, add, remove }
|
|
112
|
+
}
|
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
/** A lazy, reconnectable sidebar carrier that preserves the provider's view handles. */
|
|
2
|
+
import { readTabHandle, resolveSessionHandle, resolveTabHandle, sessionWireId, type BrowserSessionWireId, type BrowserTabWireId } from '../browser/handles.ts'
|
|
3
|
+
import type { CarrierLossReason, CarrierPage } from '../browser/types.ts'
|
|
4
|
+
import type {
|
|
5
|
+
CommandOptions, ElectronBrowserViewHost, ElectronViewHandle, PresentOptions,
|
|
6
|
+
} from './provider.ts'
|
|
7
|
+
|
|
8
|
+
interface HostLease {
|
|
9
|
+
readonly host: ElectronBrowserViewHost
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
interface BoundView {
|
|
13
|
+
readonly lease: HostLease
|
|
14
|
+
readonly handle: ElectronViewHandle
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface SidebarView {
|
|
18
|
+
readonly handle: ElectronViewHandle
|
|
19
|
+
readonly scope: BrowserSessionWireId
|
|
20
|
+
bound?: BoundView
|
|
21
|
+
pending?: Promise<BoundView>
|
|
22
|
+
/**
|
|
23
|
+
* The shell's own page id this view was adopted from.
|
|
24
|
+
*
|
|
25
|
+
* Kept across a bridge reconnect so rebinding adopts the SAME page again. A
|
|
26
|
+
* plain `createView` would open a fresh blank page and the next capture would
|
|
27
|
+
* silently read the wrong document.
|
|
28
|
+
*/
|
|
29
|
+
adoptedId?: BrowserTabWireId
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Keep an explicit sidebar selection while its bridge starts or reconnects.
|
|
34
|
+
* Discovery is deferred until a command needs the bridge; it never selects another carrier.
|
|
35
|
+
*/
|
|
36
|
+
export class SidebarOnlyHost implements ElectronBrowserViewHost {
|
|
37
|
+
readonly referenceOverlayOwner = 'client' as const
|
|
38
|
+
private readonly views = new Map<string, SidebarView>()
|
|
39
|
+
private readonly retiredSessions = new Set<BrowserSessionWireId>()
|
|
40
|
+
private lease: HostLease | undefined
|
|
41
|
+
private loading: Promise<HostLease> | undefined
|
|
42
|
+
private disposed = false
|
|
43
|
+
private readonly discovery = new AbortController()
|
|
44
|
+
private lostListener: ((reason: CarrierLossReason) => void) | undefined
|
|
45
|
+
private readonly loader: (signal: AbortSignal) => Promise<ElectronBrowserViewHost>
|
|
46
|
+
constructor(loader: (signal: AbortSignal) => Promise<ElectronBrowserViewHost>) {
|
|
47
|
+
this.loader = loader
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Keep the explicit selection available so discovery failures reach the caller unchanged. */
|
|
51
|
+
available(): boolean { return !this.disposed }
|
|
52
|
+
|
|
53
|
+
/** Return a stable handle without opening a sidebar or discovering a bridge. */
|
|
54
|
+
createView(identity: BrowserTabWireId): ElectronViewHandle {
|
|
55
|
+
this.assertOpen()
|
|
56
|
+
const scope = sessionWireId(readTabHandle(identity))
|
|
57
|
+
if (this.retiredSessions.has(scope)) throw new Error('dsh-builtin-browser: sidebar session was released')
|
|
58
|
+
if (this.views.has(identity)) throw new Error('dsh-builtin-browser: sidebar view is already bound')
|
|
59
|
+
const handle: ElectronViewHandle = {
|
|
60
|
+
id: identity,
|
|
61
|
+
sendCommand: (method, params, options) => this.send(view, method, params, options),
|
|
62
|
+
download: async (url, savePath) => {
|
|
63
|
+
const bound = await this.bind(view)
|
|
64
|
+
this.assertView(view)
|
|
65
|
+
this.assertLease(bound.lease)
|
|
66
|
+
if (bound.handle.download === undefined) throw new Error('dsh-builtin-browser: update the desktop bridge for sidebar downloads')
|
|
67
|
+
await bound.handle.download(url, savePath)
|
|
68
|
+
},
|
|
69
|
+
}
|
|
70
|
+
const view: SidebarView = { handle, scope }
|
|
71
|
+
this.views.set(handle.id, view)
|
|
72
|
+
return handle
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
destroyView(handle: ElectronViewHandle): void {
|
|
76
|
+
const view = this.views.get(handle.id)
|
|
77
|
+
if (view?.handle !== handle) return
|
|
78
|
+
this.views.delete(handle.id)
|
|
79
|
+
if (view.bound !== undefined) view.bound.lease.host.destroyView(view.bound.handle)
|
|
80
|
+
view.bound = undefined
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Presentation must not turn the provider's initial show into a blank-page navigation. */
|
|
84
|
+
showView(handle: ElectronViewHandle, label?: string, options?: PresentOptions): void {
|
|
85
|
+
const view = this.views.get(handle.id)
|
|
86
|
+
const bound = view?.handle === handle ? view.bound : undefined
|
|
87
|
+
if (!this.disposed && bound !== undefined && bound.lease === this.lease) {
|
|
88
|
+
bound.lease.host.showView?.(bound.handle, label, options)
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async presentView(handle: ElectronViewHandle, label?: string, options?: PresentOptions): Promise<void> {
|
|
93
|
+
const view = this.views.get(handle.id)
|
|
94
|
+
if (view?.handle !== handle) throw new Error('dsh-builtin-browser: sidebar view is destroyed')
|
|
95
|
+
const bound = await this.bind(view)
|
|
96
|
+
if (bound.lease.host.presentView === undefined) throw new Error('dsh-builtin-browser: sidebar bridge cannot acknowledge selection')
|
|
97
|
+
await bound.lease.host.presentView(bound.handle, label, options)
|
|
98
|
+
this.assertView(view)
|
|
99
|
+
this.assertLease(bound.lease)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
async listPages(scope: BrowserSessionWireId, options?: { readonly requireRunning?: boolean }): Promise<readonly CarrierPage[]> {
|
|
103
|
+
resolveSessionHandle(scope)
|
|
104
|
+
const lease = await this.load()
|
|
105
|
+
this.assertLease(lease)
|
|
106
|
+
if (this.retiredSessions.has(scope)) throw new Error('dsh-builtin-browser: sidebar session was released')
|
|
107
|
+
const pages = await lease.host.listPages?.(scope, options) ?? []
|
|
108
|
+
for (const page of pages) resolveTabHandle(resolveSessionHandle(scope), page.id)
|
|
109
|
+
return pages
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Bind a page the SHELL opened to a handle the provider can drive.
|
|
114
|
+
*
|
|
115
|
+
* The delegation is the point: discovery happens at the first command, so a page
|
|
116
|
+
* adopted before the bridge is up must still bind later. The adopted handle is
|
|
117
|
+
* stored as this view's binding, which is exactly what `bind` would have created —
|
|
118
|
+
* the difference is only which guest it points at.
|
|
119
|
+
* @param id - the sidebar page id reported by {@link listPages}.
|
|
120
|
+
* @param owner - the conversation the page belongs to.
|
|
121
|
+
* @returns a handle whose commands target that page.
|
|
122
|
+
*/
|
|
123
|
+
async adoptPage(id: BrowserTabWireId, scope = sessionWireId(readTabHandle(id))): Promise<ElectronViewHandle> {
|
|
124
|
+
resolveTabHandle(resolveSessionHandle(scope), id)
|
|
125
|
+
if (this.retiredSessions.has(scope)) throw new Error('dsh-builtin-browser: sidebar session was released')
|
|
126
|
+
const lease = await this.load()
|
|
127
|
+
this.assertLease(lease)
|
|
128
|
+
if (lease.host.adoptPage === undefined) throw new Error('dsh-builtin-browser: this sidebar bridge cannot hand over a page it did not open')
|
|
129
|
+
const adopted = await lease.host.adoptPage(id, scope)
|
|
130
|
+
this.assertLease(lease)
|
|
131
|
+
if (adopted.id !== id) {
|
|
132
|
+
lease.host.destroyView(adopted)
|
|
133
|
+
throw new Error('dsh-builtin-browser: sidebar returned a different tab handle')
|
|
134
|
+
}
|
|
135
|
+
const handle: ElectronViewHandle = {
|
|
136
|
+
id: adopted.id,
|
|
137
|
+
sendCommand: (method, params, options) => this.send(view, method, params, options),
|
|
138
|
+
download: async (url, savePath) => {
|
|
139
|
+
const bound = await this.bind(view)
|
|
140
|
+
this.assertView(view)
|
|
141
|
+
this.assertLease(bound.lease)
|
|
142
|
+
if (bound.handle.download === undefined) throw new Error('dsh-builtin-browser: update the desktop bridge for sidebar downloads')
|
|
143
|
+
await bound.handle.download(url, savePath)
|
|
144
|
+
},
|
|
145
|
+
}
|
|
146
|
+
const view: SidebarView = { handle, scope, bound: { lease, handle: adopted }, adoptedId: id }
|
|
147
|
+
this.views.set(handle.id, view)
|
|
148
|
+
return handle
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
async closePage(id: BrowserTabWireId, scope = sessionWireId(readTabHandle(id))): Promise<void> {
|
|
152
|
+
resolveTabHandle(resolveSessionHandle(scope), id)
|
|
153
|
+
const lease = await this.load()
|
|
154
|
+
this.assertLease(lease)
|
|
155
|
+
if (lease.host.closePage === undefined) throw new Error('dsh-builtin-browser: this sidebar bridge cannot close pages')
|
|
156
|
+
await lease.host.closePage(id, scope)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
async retireSession(scope: BrowserSessionWireId): Promise<void> {
|
|
160
|
+
resolveSessionHandle(scope)
|
|
161
|
+
this.retiredSessions.add(scope)
|
|
162
|
+
for (const view of [...this.views.values()]) if (view.scope === scope) this.destroyView(view.handle)
|
|
163
|
+
const lease = this.lease ?? await this.loading
|
|
164
|
+
if (!this.disposed && lease !== undefined && lease === this.lease) await lease.host.retireSession?.(scope)
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** Retire only this conversation's handles before awaiting an in-flight discovery. */
|
|
168
|
+
async releasePage(scope?: BrowserSessionWireId): Promise<void> {
|
|
169
|
+
if (scope !== undefined) {
|
|
170
|
+
resolveSessionHandle(scope)
|
|
171
|
+
this.retiredSessions.add(scope)
|
|
172
|
+
}
|
|
173
|
+
for (const view of [...this.views.values()]) {
|
|
174
|
+
if (scope === undefined || view.scope === scope) this.destroyView(view.handle)
|
|
175
|
+
}
|
|
176
|
+
const lease = this.lease ?? await this.loading
|
|
177
|
+
if (!this.disposed && lease !== undefined && lease === this.lease) await lease.host.releasePage?.(scope)
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
async collapse(): Promise<void> {
|
|
181
|
+
if (!this.disposed) await this.lease?.host.collapse?.()
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
onLost(listener: (reason: CarrierLossReason) => void): void {
|
|
185
|
+
this.lostListener = listener
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
dispose(): void {
|
|
189
|
+
if (this.disposed) return
|
|
190
|
+
this.disposed = true
|
|
191
|
+
this.discovery.abort()
|
|
192
|
+
this.views.clear()
|
|
193
|
+
const lease = this.lease
|
|
194
|
+
this.lease = undefined
|
|
195
|
+
this.lostListener = undefined
|
|
196
|
+
lease?.host.dispose?.()
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
private assertOpen(): void {
|
|
200
|
+
if (this.disposed) throw new Error('dsh-builtin-browser: sidebar host is disposed')
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
private assertView(view: SidebarView): void {
|
|
204
|
+
this.assertOpen()
|
|
205
|
+
if (this.views.get(view.handle.id) !== view) throw new Error('dsh-builtin-browser: sidebar view is destroyed')
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
private assertLease(lease: HostLease): void {
|
|
209
|
+
this.assertOpen()
|
|
210
|
+
if (this.lease !== lease) throw new Error('dsh-builtin-browser: sidebar bridge changed before the operation')
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
private load(): Promise<HostLease> {
|
|
214
|
+
this.assertOpen()
|
|
215
|
+
if (this.lease !== undefined) return Promise.resolve(this.lease)
|
|
216
|
+
if (this.loading !== undefined) return this.loading
|
|
217
|
+
const pending = Promise.resolve().then(() => this.loader(this.discovery.signal)).then(host => {
|
|
218
|
+
if (this.disposed) {
|
|
219
|
+
host.dispose?.()
|
|
220
|
+
this.assertOpen()
|
|
221
|
+
}
|
|
222
|
+
const lease: HostLease = { host }
|
|
223
|
+
this.lease = lease
|
|
224
|
+
try {
|
|
225
|
+
host.onLost?.(reason => this.lost(lease, reason))
|
|
226
|
+
} catch (error) {
|
|
227
|
+
if (this.lease === lease) this.lease = undefined
|
|
228
|
+
host.dispose?.()
|
|
229
|
+
throw error
|
|
230
|
+
}
|
|
231
|
+
if (this.lease !== lease) throw new Error('dsh-builtin-browser: sidebar bridge was lost during discovery')
|
|
232
|
+
return lease
|
|
233
|
+
})
|
|
234
|
+
this.loading = pending
|
|
235
|
+
const clear = (): void => { if (this.loading === pending) this.loading = undefined }
|
|
236
|
+
void pending.then(clear, clear)
|
|
237
|
+
return pending
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
private lost(lease: HostLease, reason: CarrierLossReason): void {
|
|
241
|
+
if (this.disposed || this.lease !== lease) return
|
|
242
|
+
this.lease = undefined
|
|
243
|
+
this.loading = undefined
|
|
244
|
+
for (const view of this.views.values()) {
|
|
245
|
+
view.bound = undefined
|
|
246
|
+
view.pending = undefined
|
|
247
|
+
}
|
|
248
|
+
// A command already in flight keeps its original outcome; the next call rediscovers.
|
|
249
|
+
setImmediate(() => {
|
|
250
|
+
try { lease.host.dispose?.() } catch { /* The next discovery replaces this failed carrier. */ }
|
|
251
|
+
})
|
|
252
|
+
try { this.lostListener?.(reason) } catch { /* Transport recovery must survive a listener failure. */ }
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
private bind(view: SidebarView): Promise<BoundView> {
|
|
256
|
+
this.assertView(view)
|
|
257
|
+
if (view.bound !== undefined && view.bound.lease === this.lease) return Promise.resolve(view.bound)
|
|
258
|
+
if (view.pending !== undefined) return view.pending
|
|
259
|
+
const pending = this.load().then(lease => {
|
|
260
|
+
this.assertView(view)
|
|
261
|
+
if (this.lease !== lease) throw new Error('dsh-builtin-browser: sidebar bridge changed before binding the view')
|
|
262
|
+
// An adopted view rebinds to its ORIGINAL page: a bridge reconnect must not
|
|
263
|
+
// turn the human's page into a fresh blank one the next capture would read.
|
|
264
|
+
const adoptedId = view.adoptedId
|
|
265
|
+
if (adoptedId !== undefined) {
|
|
266
|
+
if (lease.host.adoptPage === undefined) throw new Error('dsh-builtin-browser: this sidebar bridge cannot hand over a page it did not open')
|
|
267
|
+
return lease.host.adoptPage(adoptedId, view.scope).then(handle => {
|
|
268
|
+
this.assertView(view)
|
|
269
|
+
if (this.lease !== lease || handle.id !== view.handle.id) {
|
|
270
|
+
lease.host.destroyView(handle)
|
|
271
|
+
throw new Error('dsh-builtin-browser: sidebar bridge changed while binding the view')
|
|
272
|
+
}
|
|
273
|
+
const bound = { lease, handle }
|
|
274
|
+
view.bound = bound
|
|
275
|
+
return bound
|
|
276
|
+
})
|
|
277
|
+
}
|
|
278
|
+
const handle = lease.host.createView(view.handle.id as BrowserTabWireId)
|
|
279
|
+
if (this.disposed || this.views.get(view.handle.id) !== view || this.lease !== lease) {
|
|
280
|
+
lease.host.destroyView(handle)
|
|
281
|
+
this.assertView(view)
|
|
282
|
+
throw new Error('dsh-builtin-browser: sidebar bridge changed while binding the view')
|
|
283
|
+
}
|
|
284
|
+
const bound = { lease, handle }
|
|
285
|
+
view.bound = bound
|
|
286
|
+
return bound
|
|
287
|
+
})
|
|
288
|
+
view.pending = pending
|
|
289
|
+
const clear = (): void => { if (view.pending === pending) view.pending = undefined }
|
|
290
|
+
void pending.then(clear, clear)
|
|
291
|
+
return pending
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
private async send(view: SidebarView, method: string, params?: Record<string, unknown>, options?: CommandOptions): Promise<Record<string, unknown>> {
|
|
295
|
+
const bound = await this.bind(view)
|
|
296
|
+
this.assertView(view)
|
|
297
|
+
if (this.lease !== bound.lease) throw new Error('dsh-builtin-browser: sidebar bridge changed before the command')
|
|
298
|
+
return bound.handle.sendCommand(method, params, options)
|
|
299
|
+
}
|
|
300
|
+
}
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sidebar start-page shortcuts, written through the shared settings section.
|
|
3
|
+
*
|
|
4
|
+
* WHY THIS LIVES HERE
|
|
5
|
+
* The start page belongs to `@deepseek-ai/dsh-client-ui-sidebar-browser`, and its
|
|
6
|
+
* preferences are a volatile settings section (`browser-startpage`). A human edits
|
|
7
|
+
* them in the sidebar; an agent has no sidebar, so without a seam of its own it can
|
|
8
|
+
* never add a shortcut. This module is that seam: it reads and writes the SAME
|
|
9
|
+
* section through `ctx.settings`, so both callers see one list — no second store,
|
|
10
|
+
* no file of our own, no drift.
|
|
11
|
+
*
|
|
12
|
+
* The section is validated by its owning entry's schema, so the bounds below exist
|
|
13
|
+
* to give a caller a clear error BEFORE the settings layer rejects an opaque one;
|
|
14
|
+
* they mirror `startpage-settings.ts` and must stay in step with it.
|
|
15
|
+
* @module dsh-browser/browser-electron/startpage-shortcuts
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/** Settings entry id of the start page (its Host companion's plugin id). */
|
|
19
|
+
export const STARTPAGE_NAMESPACE = 'browser-startpage'
|
|
20
|
+
|
|
21
|
+
/** Maximum stored shortcut entries; mirrors the start page schema. */
|
|
22
|
+
export const MAX_SHORTCUTS = 48
|
|
23
|
+
/** Maximum shortcut display title length. */
|
|
24
|
+
export const MAX_TITLE = 80
|
|
25
|
+
/** Maximum stored URL length. */
|
|
26
|
+
export const MAX_URL = 2048
|
|
27
|
+
/** Maximum icon key length. */
|
|
28
|
+
export const MAX_ICON_KEY = 32
|
|
29
|
+
|
|
30
|
+
/** One stored shortcut. */
|
|
31
|
+
export interface ShortcutRecord {
|
|
32
|
+
readonly id: string
|
|
33
|
+
readonly title: string
|
|
34
|
+
readonly url: string
|
|
35
|
+
readonly iconKey?: string
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** The outcome of one list/add/remove call. */
|
|
39
|
+
export interface ShortcutList {
|
|
40
|
+
readonly shortcuts: readonly ShortcutRecord[]
|
|
41
|
+
readonly revision: number | undefined
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** A refusal a caller can act on, rather than a stack trace. */
|
|
45
|
+
export class ShortcutError extends Error {
|
|
46
|
+
readonly code: string
|
|
47
|
+
|
|
48
|
+
/** @param code - stable machine-readable reason. @param message - human sentence. */
|
|
49
|
+
constructor(code: string, message: string) {
|
|
50
|
+
super(message)
|
|
51
|
+
this.name = 'ShortcutError'
|
|
52
|
+
this.code = code
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** The slice of `ctx.settings` this module needs. */
|
|
57
|
+
export interface ShortcutSettings {
|
|
58
|
+
describe(): readonly { readonly ns: string; readonly revision: number; readonly value: unknown }[]
|
|
59
|
+
update(ns: string, patch: object, expectedRevision?: number): Promise<void>
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const ID_PATTERN = /^[a-z0-9][a-z0-9-]{0,47}$/u
|
|
63
|
+
const URL_PATTERN = /^https?:\/\/\S+$/u
|
|
64
|
+
|
|
65
|
+
/** Validate one stored record; a malformed row is dropped rather than rendered. */
|
|
66
|
+
export function isShortcutRecord(value: unknown): value is ShortcutRecord {
|
|
67
|
+
if (value === null || typeof value !== 'object') return false
|
|
68
|
+
const row = value as Record<string, unknown>
|
|
69
|
+
return typeof row.id === 'string' && ID_PATTERN.test(row.id)
|
|
70
|
+
&& typeof row.title === 'string' && row.title.trim() !== '' && row.title.length <= MAX_TITLE
|
|
71
|
+
&& typeof row.url === 'string' && URL_PATTERN.test(row.url) && row.url.length <= MAX_URL
|
|
72
|
+
&& (row.iconKey === undefined || (typeof row.iconKey === 'string' && row.iconKey.length <= MAX_ICON_KEY))
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Normalise one caller-supplied shortcut.
|
|
77
|
+
* @param draft - untrusted input from a tool call or an HTTP body.
|
|
78
|
+
* @returns the record to store.
|
|
79
|
+
*/
|
|
80
|
+
export function normalizeShortcut(draft: { title?: unknown; url?: unknown; iconKey?: unknown; id?: unknown }): ShortcutRecord {
|
|
81
|
+
const title = typeof draft.title === 'string' ? draft.title.trim() : ''
|
|
82
|
+
const url = typeof draft.url === 'string' ? draft.url.trim() : ''
|
|
83
|
+
if (title === '') throw new ShortcutError('title', 'a shortcut needs a title')
|
|
84
|
+
if (title.length > MAX_TITLE) throw new ShortcutError('title', `a title may hold at most ${MAX_TITLE} characters`)
|
|
85
|
+
if (url === '') throw new ShortcutError('url', 'a shortcut needs an http(s) URL')
|
|
86
|
+
if (!URL_PATTERN.test(url)) throw new ShortcutError('url', 'the URL must start with http:// or https:// and contain no spaces')
|
|
87
|
+
if (url.length > MAX_URL) throw new ShortcutError('url', `a URL may hold at most ${MAX_URL} characters`)
|
|
88
|
+
const iconKey = typeof draft.iconKey === 'string' ? draft.iconKey.trim() : ''
|
|
89
|
+
if (iconKey.length > MAX_ICON_KEY) throw new ShortcutError('icon', `an icon key may hold at most ${MAX_ICON_KEY} characters`)
|
|
90
|
+
// Milliseconds alone collide when two adds land in the same tick, so a random
|
|
91
|
+
// suffix keeps the generated id unique without depending on the clock.
|
|
92
|
+
const id = typeof draft.id === 'string' && ID_PATTERN.test(draft.id)
|
|
93
|
+
? draft.id
|
|
94
|
+
: `s-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}`
|
|
95
|
+
return { id, title, url, ...iconKey === '' ? {} : { iconKey } }
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Compare addresses by their parsed form, not their literal spelling:
|
|
100
|
+
* `https://x.com` and `https://x.com/` (or a differently cased host, or an
|
|
101
|
+
* explicit default port) are the same destination. A malformed value falls
|
|
102
|
+
* back to its literal form, so the duplicate check can never reject an add.
|
|
103
|
+
*/
|
|
104
|
+
function canonicalUrl(value: string): string {
|
|
105
|
+
try { return new URL(value).href } catch { return value }
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Read and write the shared shortcut list.
|
|
110
|
+
* @param settings - current settings service, resolved lazily so a late service still works.
|
|
111
|
+
*/
|
|
112
|
+
export function createShortcutStore(settings: () => ShortcutSettings | undefined): {
|
|
113
|
+
list: () => ShortcutList
|
|
114
|
+
add: (draft: { title?: unknown; url?: unknown; iconKey?: unknown }) => Promise<{ shortcut: ShortcutRecord; shortcuts: readonly ShortcutRecord[] }>
|
|
115
|
+
remove: (id: string) => Promise<{ removed: boolean; shortcuts: readonly ShortcutRecord[] }>
|
|
116
|
+
} {
|
|
117
|
+
const read = (): ShortcutList & { raw: readonly unknown[]; revisionNumber: number } => {
|
|
118
|
+
const service = settings()
|
|
119
|
+
if (service === undefined) throw new ShortcutError('unavailable', 'the settings service is not available in this host')
|
|
120
|
+
const row = service.describe().find(entry => entry.ns === STARTPAGE_NAMESPACE)
|
|
121
|
+
if (row === undefined) {
|
|
122
|
+
throw new ShortcutError('unavailable', `the "${STARTPAGE_NAMESPACE}" settings entry is not loaded in this profile`)
|
|
123
|
+
}
|
|
124
|
+
const value = (row.value ?? {}) as { shortcuts?: unknown }
|
|
125
|
+
// Keep the RAW array as well as the filtered one. The sidebar deliberately
|
|
126
|
+
// keeps rows whose `target` is still being repaired (they fail
|
|
127
|
+
// isShortcutRecord but are NOT ours to delete). Writes must be derived from
|
|
128
|
+
// `raw` with an id-level edit; `shortcuts` is only for duplicate checks and
|
|
129
|
+
// for what a caller sees.
|
|
130
|
+
const raw: readonly unknown[] = Array.isArray(value.shortcuts) ? value.shortcuts : []
|
|
131
|
+
const shortcuts = raw.filter(isShortcutRecord)
|
|
132
|
+
return { raw, shortcuts, revision: row.revision, revisionNumber: row.revision }
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Apply one edit, recomputing it from the CURRENT list on a stale revision.
|
|
137
|
+
*
|
|
138
|
+
* Retrying with the previously computed array would write a snapshot taken
|
|
139
|
+
* before the other writer's change and silently drop it (last writer wins).
|
|
140
|
+
* Rebuilding from the fresh list instead re-runs the edit — including its
|
|
141
|
+
* limit and duplicate checks — so a concurrent add survives this one.
|
|
142
|
+
* @param apply - turns the current list into the next list plus its result.
|
|
143
|
+
* @returns the result computed by the attempt that was accepted.
|
|
144
|
+
*/
|
|
145
|
+
interface MutateContext {
|
|
146
|
+
/** The raw, UNFILTERED section array — the only safe base for a write. */
|
|
147
|
+
readonly raw: readonly unknown[]
|
|
148
|
+
/** Filtered records, for duplicate/limit checks and for the returned view. */
|
|
149
|
+
readonly shortcuts: readonly ShortcutRecord[]
|
|
150
|
+
}
|
|
151
|
+
interface MutateEdit<T> {
|
|
152
|
+
/** The value to write back, derived from `raw` (never from `shortcuts`). */
|
|
153
|
+
readonly next: readonly unknown[]
|
|
154
|
+
readonly result: T
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const mutate = async <T>(apply: (context: MutateContext) => MutateEdit<T>): Promise<T> => {
|
|
158
|
+
let lastError: unknown
|
|
159
|
+
for (let attempt = 0; attempt < 3; attempt += 1) {
|
|
160
|
+
const service = settings()
|
|
161
|
+
if (service === undefined) throw new ShortcutError('unavailable', 'the settings service is not available in this host')
|
|
162
|
+
const { raw, shortcuts, revisionNumber } = read()
|
|
163
|
+
const { next, result } = apply({ raw, shortcuts })
|
|
164
|
+
// `apply` returns the SAME `raw` reference when there is nothing to write
|
|
165
|
+
// (a remove for an already-absent id); writing would bump the revision
|
|
166
|
+
// for no change. The equality is against raw, not the filtered view.
|
|
167
|
+
if (next === raw) return result
|
|
168
|
+
try {
|
|
169
|
+
await service.update(STARTPAGE_NAMESPACE, { shortcuts: next }, revisionNumber)
|
|
170
|
+
return result
|
|
171
|
+
} catch (error) {
|
|
172
|
+
// Two writers (the sidebar and an agent) can race between read and write.
|
|
173
|
+
// Only a revision conflict is retried; anything else is a real failure.
|
|
174
|
+
if (!/conflict|revision/iu.test(String(error))) throw error
|
|
175
|
+
lastError = error
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
throw new ShortcutError('conflict', `the shortcut list kept changing while it was edited: ${String(lastError)}`)
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const idOf = (entry: unknown): unknown =>
|
|
182
|
+
entry !== null && typeof entry === 'object' ? (entry as Record<string, unknown>).id : undefined
|
|
183
|
+
|
|
184
|
+
return {
|
|
185
|
+
list: () => {
|
|
186
|
+
const { shortcuts, revision } = read()
|
|
187
|
+
return { shortcuts, revision }
|
|
188
|
+
},
|
|
189
|
+
async add(draft) {
|
|
190
|
+
const shortcut = normalizeShortcut(draft)
|
|
191
|
+
return mutate(({ raw, shortcuts }) => {
|
|
192
|
+
if (shortcuts.length >= MAX_SHORTCUTS) {
|
|
193
|
+
throw new ShortcutError('limit', `the start page holds at most ${MAX_SHORTCUTS} shortcuts`)
|
|
194
|
+
}
|
|
195
|
+
if (shortcuts.some(entry => canonicalUrl(entry.url) === canonicalUrl(shortcut.url))) {
|
|
196
|
+
throw new ShortcutError('conflict', `a shortcut to ${shortcut.url} already exists`)
|
|
197
|
+
}
|
|
198
|
+
// id-level minimal edit: push onto a COPY of the raw array, so rows the
|
|
199
|
+
// sidebar is still repairing are carried through untouched.
|
|
200
|
+
const next = [...raw, shortcut]
|
|
201
|
+
return { next, result: { shortcut, shortcuts: next.filter(isShortcutRecord) } }
|
|
202
|
+
})
|
|
203
|
+
},
|
|
204
|
+
async remove(id) {
|
|
205
|
+
return mutate<{ removed: boolean; shortcuts: readonly ShortcutRecord[] }>(({ raw, shortcuts }) => {
|
|
206
|
+
// Filter a COPY of raw by id; every other row — valid or not — survives.
|
|
207
|
+
const next = raw.filter(entry => idOf(entry) !== id)
|
|
208
|
+
if (next.length === raw.length) return { next: raw, result: { removed: false, shortcuts } }
|
|
209
|
+
return { next, result: { removed: true, shortcuts: next.filter(isShortcutRecord) } }
|
|
210
|
+
})
|
|
211
|
+
},
|
|
212
|
+
}
|
|
213
|
+
}
|