dsh-builtin-browser 1.0.3 → 1.0.4

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.
Files changed (293) hide show
  1. package/README.en.md +89 -0
  2. package/browser-extension/constants.mjs +20 -0
  3. package/browser-extension/controller.mjs +293 -0
  4. package/browser-extension/locales.mjs +15 -0
  5. package/browser-extension/manifest.json +12 -0
  6. package/browser-extension/options.html +14 -0
  7. package/browser-extension/options.mjs +9 -0
  8. package/browser-extension/permission-guard.mjs +13 -0
  9. package/browser-extension/worker.mjs +74 -0
  10. package/package.json +20 -2
  11. package/scripts/build-client.mjs +305 -0
  12. package/scripts/build.mjs +69 -0
  13. package/scripts/bundle-host.mjs +28 -0
  14. package/scripts/fix-exports.mjs +40 -0
  15. package/scripts/install-electron.mjs +101 -0
  16. package/scripts/pack.mjs +99 -0
  17. package/scripts/publish-manifest.mjs +56 -0
  18. package/scripts/verify-exports.mjs +58 -0
  19. package/scripts/verify-preview-sdk.mjs +93 -0
  20. package/skills/dsh-builtin-browser/SKILL.md +98 -0
  21. package/skills/dsh-builtin-browser/references/batch.md +68 -0
  22. package/skills/dsh-builtin-browser/references/deliver.md +31 -0
  23. package/skills/dsh-builtin-browser/references/locate.md +11 -0
  24. package/skills/dsh-builtin-browser/references/recover.md +11 -0
  25. package/skills/dsh-builtin-browser/references/verify.md +13 -0
  26. package/skills/dsh-debug-host/SKILL.md +56 -0
  27. package/skills/dsh-debug-self/SKILL.md +50 -0
  28. package/src/bridge-compatibility.ts +39 -0
  29. package/src/browser/anti-bot.ts +251 -0
  30. package/src/browser/deep-dom.ts +40 -0
  31. package/src/browser/handles.ts +89 -0
  32. package/src/browser/runtime.ts +578 -0
  33. package/src/browser/takeover-target.ts +45 -0
  34. package/src/browser/types.ts +870 -0
  35. package/src/browser-electron/authenticated-route.ts +109 -0
  36. package/src/browser-electron/automatic-maintenance.ts +736 -0
  37. package/src/browser-electron/bridge-auto.ts +159 -0
  38. package/src/browser-electron/bridge-connection.ts +386 -0
  39. package/src/browser-electron/bridge-endpoint.ts +129 -0
  40. package/src/browser-electron/bridge-install.ts +290 -0
  41. package/src/browser-electron/bridge-maintenance.ts +189 -0
  42. package/src/browser-electron/bridge-status.ts +386 -0
  43. package/src/browser-electron/desktop-bridge-host.ts +253 -0
  44. package/src/browser-electron/desktop-identity.ts +353 -0
  45. package/src/browser-electron/desktop-shell.ts +100 -0
  46. package/src/browser-electron/document-readiness.ts +45 -0
  47. package/src/browser-electron/electron-provision.ts +347 -0
  48. package/src/browser-electron/entry.ts +1378 -0
  49. package/src/browser-electron/ephemeral-profile.ts +189 -0
  50. package/src/browser-electron/extension-browser.ts +230 -0
  51. package/src/browser-electron/extension-setup.ts +67 -0
  52. package/src/browser-electron/file-write.ts +31 -0
  53. package/src/browser-electron/foreground-watch.ts +142 -0
  54. package/src/browser-electron/guest-cookie-jar.ts +177 -0
  55. package/src/browser-electron/guest-cookie-store.ts +59 -0
  56. package/src/browser-electron/guest-css-runtime.ts +76 -0
  57. package/src/browser-electron/guest-css.ts +68 -0
  58. package/src/browser-electron/guest-route.ts +716 -0
  59. package/src/browser-electron/guest-runtime.ts +408 -0
  60. package/src/browser-electron/guest-storage-runtime.ts +54 -0
  61. package/src/browser-electron/guest-storage.ts +120 -0
  62. package/src/browser-electron/history-store.ts +224 -0
  63. package/src/browser-electron/host-main.ts +1385 -0
  64. package/src/browser-electron/host-patch-maintenance.ts +121 -0
  65. package/src/browser-electron/http-body.ts +65 -0
  66. package/src/browser-electron/json-reply.ts +47 -0
  67. package/src/browser-electron/maintenance-broker.ts +166 -0
  68. package/src/browser-electron/maintenance-lifecycle.ts +91 -0
  69. package/src/browser-electron/maintenance-route.ts +68 -0
  70. package/src/browser-electron/missing-system-browser.ts +114 -0
  71. package/src/browser-electron/open-slot.ts +49 -0
  72. package/src/browser-electron/paths.ts +25 -0
  73. package/src/browser-electron/preferred-browser.ts +130 -0
  74. package/src/browser-electron/preview-route.ts +215 -0
  75. package/src/browser-electron/process-ownership.ts +174 -0
  76. package/src/browser-electron/process.ts +23 -0
  77. package/src/browser-electron/profile-checkpoint.ts +187 -0
  78. package/src/browser-electron/profile-command.ts +47 -0
  79. package/src/browser-electron/profile-extension.ts +129 -0
  80. package/src/browser-electron/profile-lease.ts +211 -0
  81. package/src/browser-electron/profile-residue.ts +49 -0
  82. package/src/browser-electron/provider-core.ts +1107 -0
  83. package/src/browser-electron/provider-interaction.ts +1034 -0
  84. package/src/browser-electron/provider-media.ts +589 -0
  85. package/src/browser-electron/provider-navigation.ts +323 -0
  86. package/src/browser-electron/provider-observation.ts +664 -0
  87. package/src/browser-electron/provider-tabs.ts +372 -0
  88. package/src/browser-electron/provider.ts +666 -0
  89. package/src/browser-electron/reference-route.ts +598 -0
  90. package/src/browser-electron/remote-host.ts +1538 -0
  91. package/src/browser-electron/rpc-input.ts +13 -0
  92. package/src/browser-electron/rpc-ops.ts +41 -0
  93. package/src/browser-electron/runtime-diagnostics.ts +38 -0
  94. package/src/browser-electron/runtime-route.ts +105 -0
  95. package/src/browser-electron/runtime-status.ts +309 -0
  96. package/src/browser-electron/settings-route.ts +64 -0
  97. package/src/browser-electron/settings-store.ts +593 -0
  98. package/src/browser-electron/shortcuts-route.ts +112 -0
  99. package/src/browser-electron/sidebar-only-host.ts +300 -0
  100. package/src/browser-electron/startpage-shortcuts.ts +213 -0
  101. package/src/browser-electron/system-browser.ts +1850 -0
  102. package/src/browser-electron/tauri-host.ts +194 -0
  103. package/src/browser-electron/timing.ts +16 -0
  104. package/src/browser-electron/virtual-cursor.ts +162 -0
  105. package/src/browser-electron/windows-system.ts +24 -0
  106. package/src/browser-reference/delivery.ts +384 -0
  107. package/src/browser-reference/entry.ts +631 -0
  108. package/src/browser-reference/export.ts +339 -0
  109. package/src/browser-reference/import.ts +303 -0
  110. package/src/browser-reference/page-script.ts +1537 -0
  111. package/src/browser-reference/service.ts +1346 -0
  112. package/src/browser-reference/store.ts +172 -0
  113. package/src/browser-reference/text-quote.ts +219 -0
  114. package/src/browser-reference/types.ts +195 -0
  115. package/src/browser-reference/visible.ts +367 -0
  116. package/src/browser-reference/web-sources.ts +106 -0
  117. package/src/client/apply-guard.ts +22 -0
  118. package/src/client/browser/BrowserController.ts +693 -0
  119. package/src/client/browser/BrowserFrame.ts +78 -0
  120. package/src/client/browser/BrowserNavigation.ts +169 -0
  121. package/src/client/browser/BrowserPage.ts +23 -0
  122. package/src/client/browser/BrowserPersistence.ts +44 -0
  123. package/src/client/browser/IframeImpl.ts +182 -0
  124. package/src/client/browser/child-tabs.ts +14 -0
  125. package/src/client/browser/frame-history.ts +125 -0
  126. package/src/client/browser/frame-state.ts +27 -0
  127. package/src/client/browser/iframe-reference.ts +67 -0
  128. package/src/client/browser/initialization.ts +262 -0
  129. package/src/client/browser/omnibox.ts +243 -0
  130. package/src/client/browser/store.ts +38 -0
  131. package/src/client/browser/takeover-channel.ts +72 -0
  132. package/src/client/browser/url.ts +46 -0
  133. package/src/client/browser/web-reference-source.ts +56 -0
  134. package/src/client/css-modules.d.ts +4 -0
  135. package/src/client/definition.tsx +36 -0
  136. package/src/client/electron/ElectronWebViewImpl.ts +371 -0
  137. package/src/client/electron/ElectronWebviewPresentation.ts +140 -0
  138. package/src/client/electron/NativeChildPresentation.ts +53 -0
  139. package/src/client/electron/NativeViewPage.ts +265 -0
  140. package/src/client/electron/pages.ts +23 -0
  141. package/src/client/electron/reference-overlay.ts +53 -0
  142. package/src/client/electron/workspace.ts +33 -0
  143. package/src/client/index.ts +333 -0
  144. package/src/client/locales.ts +860 -0
  145. package/src/client/pages.ts +28 -0
  146. package/src/client/reference/client/BrowserRefLink.tsx +142 -0
  147. package/src/client/reference/client/draft-reference.ts +167 -0
  148. package/src/client/reference/client/index.ts +7 -0
  149. package/src/client/reference/client/ordered-submission.ts +108 -0
  150. package/src/client/reference/client/pending-delivery.ts +216 -0
  151. package/src/client/reference/client/plugin.ts +208 -0
  152. package/src/client/reference/client/production/ReferenceCapsules.tsx +345 -0
  153. package/src/client/reference/client/production/ReferenceDock.module.css +193 -0
  154. package/src/client/reference/client/production/ReferenceDock.tsx +6 -0
  155. package/src/client/reference/client/production/ReferenceFeedback.tsx +15 -0
  156. package/src/client/reference/client/production/ReferenceMath.tsx +44 -0
  157. package/src/client/reference/client/production/ReferenceText.tsx +36 -0
  158. package/src/client/reference/client/production/assembly.ts +447 -0
  159. package/src/client/reference/client/production/asset-store.ts +144 -0
  160. package/src/client/reference/client/production/availability.ts +72 -0
  161. package/src/client/reference/client/production/bytes.ts +36 -0
  162. package/src/client/reference/client/production/composer-attachments.ts +44 -0
  163. package/src/client/reference/client/production/delivery-client.ts +211 -0
  164. package/src/client/reference/client/production/draft-adapter.ts +512 -0
  165. package/src/client/reference/client/production/http-transport.ts +212 -0
  166. package/src/client/reference/client/production/production-submission.ts +70 -0
  167. package/src/client/reference/client/production/protocol.ts +234 -0
  168. package/src/client/reference/client/production/reference-card.ts +240 -0
  169. package/src/client/reference/client/production/reference-labels.ts +29 -0
  170. package/src/client/reference/client/production/reply-link-bridge.ts +222 -0
  171. package/src/client/reference/client/production/reveal.ts +215 -0
  172. package/src/client/reference/client/production/runtime.ts +1056 -0
  173. package/src/client/reference/client/production/snapshot-adapter.ts +154 -0
  174. package/src/client/reference/client/production/source-marker.ts +99 -0
  175. package/src/client/reference/client/production/source-opener.ts +55 -0
  176. package/src/client/reference/client/production/styles.ts +104 -0
  177. package/src/client/reference/client/production/user-text-rule.ts +67 -0
  178. package/src/client/reference/client/reveal.ts +109 -0
  179. package/src/client/reference/client/scheme.ts +114 -0
  180. package/src/client/reference/client/session-schedule.ts +36 -0
  181. package/src/client/reference/client/types.ts +168 -0
  182. package/src/client/reference/css-modules.d.ts +4 -0
  183. package/src/client/reference/index.ts +11 -0
  184. package/src/client/reference-capture-owner.ts +162 -0
  185. package/src/client/reference-intents.ts +36 -0
  186. package/src/client/settings/index.ts +42 -0
  187. package/src/client/settings/mount-guard.ts +31 -0
  188. package/src/client/settings/panel.ts +2768 -0
  189. package/src/client/shortcut-open.ts +30 -0
  190. package/src/client/startpage/binding.ts +99 -0
  191. package/src/client/startpage/controller.ts +300 -0
  192. package/src/client/startpage/model.ts +279 -0
  193. package/src/client/view/Browser.module.css +751 -0
  194. package/src/client/view/BrowserBody.tsx +315 -0
  195. package/src/client/view/BrowserPresentation.ts +9 -0
  196. package/src/client/view/BrowserTitle.tsx +17 -0
  197. package/src/client/view/IframePresentation.ts +365 -0
  198. package/src/client/view/StartPage.tsx +598 -0
  199. package/src/client/view/TakeoverBanner.tsx +21 -0
  200. package/src/compat/host-features.ts +92 -0
  201. package/src/index.ts +110 -0
  202. package/src/reference-owner.ts +5 -0
  203. package/src/reference-page.ts +9 -0
  204. package/src/reference-source.ts +3 -0
  205. package/src/reference-text.ts +219 -0
  206. package/src/startpage-settings.ts +103 -0
  207. package/src/startpage.ts +71 -0
  208. package/src/takeover-protocol.ts +28 -0
  209. package/src/tool-browser/block-recovery.ts +85 -0
  210. package/src/tool-browser/code-tool.ts +555 -0
  211. package/src/tool-browser/code-worker.ts +725 -0
  212. package/src/tool-browser/element-fields.ts +167 -0
  213. package/src/tool-browser/errors.ts +40 -0
  214. package/src/tool-browser/index.ts +265 -0
  215. package/src/tool-browser/page-probe.ts +675 -0
  216. package/src/tool-browser/panel-page.ts +126 -0
  217. package/src/tool-browser/probe-client.ts +183 -0
  218. package/src/tool-browser/screenshot-annotations.ts +131 -0
  219. package/src/tool-browser/screenshot-image.ts +49 -0
  220. package/src/tool-browser/state.ts +375 -0
  221. package/src/tool-browser/takeover.ts +199 -0
  222. package/src/tool-browser/tools-debug.ts +806 -0
  223. package/src/tool-browser/tools-interaction.ts +956 -0
  224. package/src/tool-browser/tools-navigation.ts +786 -0
  225. package/src/tool-browser/tools-observation.ts +474 -0
  226. package/src/tool-browser/tools-reference.ts +294 -0
  227. package/src/types/electron-shim.d.ts +144 -0
  228. package/src/types.ts +85 -0
  229. package/src/web-guest.ts +35 -0
  230. package/tools/accept-desktop.mjs +148 -0
  231. package/dist/client.d.cts +0 -516
  232. package/dist/client.d.mts +0 -516
  233. package/dist/index.cjs +0 -92
  234. package/dist/index.d.mts +0 -2960
  235. package/dist/index.mjs +0 -24
  236. package/dsh-builtin-browser-1.0.3-publish.tgz +0 -0
  237. package/dsh-builtin-browser-1.0.3.tgz +0 -0
  238. package/dsh-host-patch/sources/main/0001-feat-sidebar-right-add-session-rendering-holds.patch.gz +0 -0
  239. package/dsh-host-patch/sources/main/0002-feat-desktop-load-trusted-profile-main-extensions.patch.gz +0 -0
  240. package/dsh-host-patch/sources/main/0003-feat-desktop-report-resolved-and-running-main-extens.patch.gz +0 -0
  241. package/dsh-host-patch/sources/main/0004-feat-desktop-compose-admitted-native-sidebar-views.patch.gz +0 -0
  242. package/dsh-host-patch/sources/main/0005-fix-client-admit-coded-browser-errors-in-native-comp.patch.gz +0 -0
  243. package/dsh-host-patch/sources/main/0006-fix-desktop-acknowledge-native-same-document-navigat.patch.gz +0 -0
  244. package/dsh-host-patch/sources/main/0007-fix-desktop-restore-auth-in-the-native-lease-partiti.patch.gz +0 -0
  245. package/dsh-host-patch/sources/main/0008-feat-desktop-adopt-native-children-in-admitted-sideb.patch.gz +0 -0
  246. package/dsh-host-patch/sources/main/0009-fix-desktop-publish-complete-native-child-close-reco.patch.gz +0 -0
  247. package/dsh-host-patch/sources/main/0010-fix-desktop-bundle-structured-errors-in-sandboxed-pr.patch.gz +0 -0
  248. package/dsh-host-patch/sources/main/0011-feat-desktop-offer-persistent-Sidebar-sessions-per-D.patch.gz +0 -0
  249. package/dsh-host-patch/sources/main/0012-feat-desktop-retain-sidebar-session-cookies-across-r.patch.gz +0 -0
  250. package/dsh-host-patch/sources/main/0013-fix-desktop-honor-sidebar-session-cookie-sidecar-con.patch.gz +0 -0
  251. package/dsh-host-patch/sources/main/0014-fix-desktop-retry-cookie-protection-without-failing-.patch.gz +0 -0
  252. package/dsh-host-patch/sources/main/0015-fix-desktop-preserve-host-only-cookie-identity-in-se.patch.gz +0 -0
  253. package/dsh-host-patch/sources/main/0016-fix-desktop-restore-session-cookies-before-checkpoin.patch.gz +0 -0
  254. package/dsh-host-patch/sources/main/0017-fix-desktop-restore-sidebar-cookies-once-before-gues.patch.gz +0 -0
  255. package/dsh-host-patch/sources/main/0018-fix-sidebar-right-follow-animated-native-slot-placem.patch.gz +0 -0
  256. package/dsh-host-patch/sources/manifest.json +0 -218
  257. package/dsh-host-patch/sources/rc3/0001-feat-sidebar-right-add-session-rendering-holds.patch.gz +0 -0
  258. package/dsh-host-patch/sources/rc3/0002-feat-desktop-load-trusted-profile-main-extensions.patch.gz +0 -0
  259. package/dsh-host-patch/sources/rc3/0003-feat-desktop-report-resolved-and-running-main-extens.patch.gz +0 -0
  260. package/dsh-host-patch/sources/rc3/0004-feat-desktop-compose-admitted-native-sidebar-views.patch.gz +0 -0
  261. package/dsh-host-patch/sources/rc3/0005-fix-client-admit-coded-browser-errors-in-native-comp.patch.gz +0 -0
  262. package/dsh-host-patch/sources/rc3/0006-fix-desktop-acknowledge-native-same-document-navigat.patch.gz +0 -0
  263. package/dsh-host-patch/sources/rc3/0007-fix-desktop-restore-auth-in-the-native-lease-partiti.patch.gz +0 -0
  264. package/dsh-host-patch/sources/rc3/0008-feat-desktop-adopt-native-children-in-admitted-sideb.patch.gz +0 -0
  265. package/dsh-host-patch/sources/rc3/0009-fix-desktop-publish-complete-native-child-close-reco.patch.gz +0 -0
  266. package/dsh-host-patch/sources/rc3/0010-fix-desktop-bundle-structured-errors-in-sandboxed-pr.patch.gz +0 -0
  267. package/dsh-host-patch/sources/rc3/0011-feat-desktop-offer-persistent-Sidebar-sessions-per-D.patch.gz +0 -0
  268. package/dsh-host-patch/sources/rc3/0012-feat-desktop-retain-sidebar-session-cookies-across-r.patch.gz +0 -0
  269. package/dsh-host-patch/sources/rc3/0013-fix-desktop-honor-sidebar-session-cookie-sidecar-con.patch.gz +0 -0
  270. package/dsh-host-patch/sources/rc3/0014-fix-desktop-retry-cookie-protection-without-failing-.patch.gz +0 -0
  271. package/dsh-host-patch/sources/rc3/0015-fix-desktop-preserve-host-only-cookie-identity-in-se.patch.gz +0 -0
  272. package/dsh-host-patch/sources/rc3/0016-fix-desktop-restore-session-cookies-before-checkpoin.patch.gz +0 -0
  273. package/dsh-host-patch/sources/rc3/0017-fix-desktop-restore-sidebar-cookies-once-before-gues.patch.gz +0 -0
  274. package/dsh-host-patch/sources/rc3/0018-fix-sidebar-right-follow-animated-native-slot-placem.patch.gz +0 -0
  275. package/dsh-host-patch/sources/rc5/0001-feat-sidebar-right-add-session-rendering-holds.patch.gz +0 -0
  276. package/dsh-host-patch/sources/rc5/0002-feat-desktop-load-trusted-profile-main-extensions.patch.gz +0 -0
  277. package/dsh-host-patch/sources/rc5/0003-feat-desktop-report-resolved-and-running-main-extens.patch.gz +0 -0
  278. package/dsh-host-patch/sources/rc5/0004-feat-desktop-compose-admitted-native-sidebar-views.patch.gz +0 -0
  279. package/dsh-host-patch/sources/rc5/0005-fix-client-admit-coded-browser-errors-in-native-comp.patch.gz +0 -0
  280. package/dsh-host-patch/sources/rc5/0006-fix-desktop-acknowledge-native-same-document-navigat.patch.gz +0 -0
  281. package/dsh-host-patch/sources/rc5/0007-fix-desktop-restore-auth-in-the-native-lease-partiti.patch.gz +0 -0
  282. package/dsh-host-patch/sources/rc5/0008-feat-desktop-adopt-native-children-in-admitted-sideb.patch.gz +0 -0
  283. package/dsh-host-patch/sources/rc5/0009-fix-desktop-publish-complete-native-child-close-reco.patch.gz +0 -0
  284. package/dsh-host-patch/sources/rc5/0010-fix-desktop-bundle-structured-errors-in-sandboxed-pr.patch.gz +0 -0
  285. package/dsh-host-patch/sources/rc5/0011-feat-desktop-offer-persistent-Sidebar-sessions-per-D.patch.gz +0 -0
  286. package/dsh-host-patch/sources/rc5/0012-feat-desktop-retain-sidebar-session-cookies-across-r.patch.gz +0 -0
  287. package/dsh-host-patch/sources/rc5/0013-fix-desktop-honor-sidebar-session-cookie-sidecar-con.patch.gz +0 -0
  288. package/dsh-host-patch/sources/rc5/0014-fix-desktop-retry-cookie-protection-without-failing-.patch.gz +0 -0
  289. package/dsh-host-patch/sources/rc5/0015-fix-desktop-preserve-host-only-cookie-identity-in-se.patch.gz +0 -0
  290. package/dsh-host-patch/sources/rc5/0016-fix-desktop-restore-session-cookies-before-checkpoin.patch.gz +0 -0
  291. package/dsh-host-patch/sources/rc5/0017-fix-desktop-restore-sidebar-cookies-once-before-gues.patch.gz +0 -0
  292. package/dsh-host-patch/sources/rc5/0018-fix-sidebar-right-follow-animated-native-slot-placem.patch.gz +0 -0
  293. package/dsh-host-patch/sources/series.json +0 -260
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Dependency-free base64 <-> bytes helpers.
3
+ *
4
+ * The package targets both a browser realm (DOM lib) and the Node-based unit
5
+ * lane, and compiles with `types: []` (no @types/node). `btoa`/`atob` are
6
+ * therefore NOT used: this codec works on plain `Uint8Array` everywhere and is
7
+ * what the asset endpoint's `data` field and the image thumbnail consume.
8
+ *
9
+ * @module dsh-browser-reference/production/bytes
10
+ */
11
+
12
+ const ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
13
+
14
+ /** Decode a standard base64 string into bytes. Throws on invalid input. */
15
+ export function decodeBase64(input: string): Uint8Array {
16
+ const clean = input.replace(/[\s]/gu, '')
17
+ if (clean.length % 4 !== 0) throw new Error('invalid base64 length')
18
+ const withoutPadding = clean.replace(/=+$/u, '')
19
+ const lookup = new Map<string, number>()
20
+ for (let i = 0; i < ALPHABET.length; i += 1) lookup.set(ALPHABET[i]!, i)
21
+ const bytes: number[] = []
22
+ let buffer = 0
23
+ let bits = 0
24
+ for (const char of withoutPadding) {
25
+ const value = lookup.get(char)
26
+ if (value === undefined) throw new Error(`invalid base64 character: ${char}`)
27
+ buffer = (buffer << 6) | value
28
+ bits += 6
29
+ if (bits >= 8) {
30
+ bits -= 8
31
+ bytes.push((buffer >> bits) & 0xff)
32
+ buffer &= (1 << bits) - 1
33
+ }
34
+ }
35
+ return new Uint8Array(bytes)
36
+ }
@@ -0,0 +1,44 @@
1
+ import type { ComposerImage } from './production-submission.ts'
2
+
3
+ /** The part of the host conversation service that turns files into composer attachments. */
4
+ interface DraftAttachmentService {
5
+ createDrafts(sessionId: string, files: readonly File[], uploadImages: boolean): readonly { readonly id: string }[]
6
+ releaseDraftAttachments?(attachments: readonly { readonly id: string }[]): void
7
+ }
8
+
9
+ interface AttachmentContext {
10
+ get(name: string): unknown
11
+ }
12
+
13
+ interface AttachmentActions {
14
+ addAttachments?(ids: readonly string[]): boolean
15
+ removeAttachment?(id: string): void
16
+ }
17
+
18
+ /**
19
+ * Put captured images into the composer attachments through the host's own drop path
20
+ * (`conversation.createDrafts` + `inputActions.addAttachments`), so the send serializes them
21
+ * ahead of the text exactly like a pasted image. Returns the ids it attached (none when refused).
22
+ */
23
+ export function attachImages(
24
+ ctx: AttachmentContext,
25
+ sessionId: string,
26
+ images: readonly ComposerImage[],
27
+ actions: AttachmentActions,
28
+ ): readonly string[] {
29
+ const conversation = ctx.get('conversation') as Partial<DraftAttachmentService> | undefined
30
+ if (typeof conversation?.createDrafts !== 'function' || typeof actions.addAttachments !== 'function') return []
31
+ const files = images.map(({ partId, binding }) => {
32
+ const bytes = Uint8Array.from(atob(binding.data), char => char.charCodeAt(0))
33
+ const extension = binding.mime.split('/')[1]?.replace(/[^a-z0-9]/giu, '') || 'bin'
34
+ return new File([bytes], `${partId}.${extension}`, { type: binding.mime })
35
+ })
36
+ const binding = (ctx.get('sessions') as { binding?(id: string): { session?: { getSnapshot?(): { subagent?: unknown } } } | undefined } | undefined)
37
+ ?.binding?.(sessionId)
38
+ const uploadImages = (binding?.session?.getSnapshot?.().subagent ?? null) === null
39
+ const drafts = conversation.createDrafts(sessionId, files, uploadImages)
40
+ const ids = drafts.map(draft => draft.id)
41
+ if (actions.addAttachments(ids)) return ids
42
+ conversation.releaseDraftAttachments?.(drafts)
43
+ return []
44
+ }
@@ -0,0 +1,211 @@
1
+ /**
2
+ * Cancellable, incremental delivery poller.
3
+ *
4
+ * One poll is `GET /reference/deliveries?sessionId=<current>&since=<cursor>`.
5
+ * The cursor is supplied per session by the caller (persisted by the runtime),
6
+ * so switching sessions cannot leak another conversation's deliveries into the
7
+ * current draft. Polling is cancellable: `stop`, `setSession` and `dispose`
8
+ * abort the in-flight request (via the shared cancellation source) and bump a
9
+ * generation token so a late response is dropped. Full frozen snapshots in the
10
+ * response let the runtime restore cards after a page close / DSH restart.
11
+ *
12
+ * @module dsh-browser-reference/production/delivery-client
13
+ */
14
+ import type { DeliveriesResponse } from './protocol.ts'
15
+ import type { ReferenceHttpClient } from './http-transport.ts'
16
+
17
+ /** Shared abort source so the http client and the poller agree on cancellation. */
18
+ export interface CancellationSource {
19
+ /** Current signal handed to every in-flight request. */
20
+ signal(): AbortSignal
21
+ /** Abort the current request and open a fresh signal. */
22
+ reset(): void
23
+ /** Abort permanently (dispose). */
24
+ abort(): void
25
+ }
26
+
27
+ export function createCancellationSource(): CancellationSource {
28
+ let controller = new AbortController()
29
+ let dead = false
30
+ return {
31
+ signal: () => controller.signal,
32
+ reset() {
33
+ if (dead) return
34
+ controller.abort()
35
+ controller = new AbortController()
36
+ },
37
+ abort() {
38
+ dead = true
39
+ controller.abort()
40
+ },
41
+ }
42
+ }
43
+
44
+ export interface PollerScheduler {
45
+ setInterval(callback: () => void, ms: number): unknown
46
+ clearInterval(handle: unknown): void
47
+ }
48
+
49
+ const liveScheduler: PollerScheduler = {
50
+ setInterval: (callback, ms) => setInterval(callback, ms),
51
+ clearInterval: handle => clearInterval(handle as ReturnType<typeof setInterval>),
52
+ }
53
+
54
+ export interface DeliveryPollerOptions {
55
+ readonly http: ReferenceHttpClient
56
+ readonly getSessionId: () => string
57
+ readonly getSince: () => number
58
+ /**
59
+ * Apply one response. The optional `isActive` lets the runtime stop touching
60
+ * the OLD session's draft/ACKs when a session switch lands mid-cycle.
61
+ */
62
+ readonly onCycle: (response: DeliveriesResponse, sessionId: string, isActive?: () => boolean) => Promise<void> | void
63
+ /**
64
+ * Fired once per settled poll (success, cycle error or network failure),
65
+ * ONLY while its generation is still active. The runtime uses it to publish
66
+ * the dock even when `onCycle` never ran — the failure paths that used to
67
+ * leave a cached "ok" banner on screen forever.
68
+ */
69
+ readonly onSettled?: () => void
70
+ readonly intervalMs?: number
71
+ readonly scheduler?: PollerScheduler
72
+ /** Abort source shared with the http client; created internally when absent. */
73
+ readonly cancellation?: CancellationSource
74
+ }
75
+
76
+ export interface DeliveryPoller {
77
+ /** Run exactly one poll cycle; returns the response or undefined when cancelled/failed. */
78
+ pollOnce(): Promise<DeliveriesResponse | undefined>
79
+ start(): void
80
+ stop(): void
81
+ dispose(): void
82
+ /** Switch the active session; cancels any in-flight request. */
83
+ setSession(sessionId: string): void
84
+ isPolling(): boolean
85
+ readonly disposed: boolean
86
+ readonly lastError: unknown
87
+ readonly cancellation: CancellationSource
88
+ }
89
+
90
+ /** Build the poller; `start()` schedules the interval and fires one immediate poll. */
91
+ export function createDeliveryPoller(options: DeliveryPollerOptions): DeliveryPoller {
92
+ const http = options.http
93
+ const scheduler = options.scheduler ?? liveScheduler
94
+ const interval = options.intervalMs ?? 1500
95
+ const cancellation = options.cancellation ?? createCancellationSource()
96
+ let generation = 0
97
+ let polling = false
98
+ let disposed = false
99
+ let handle: unknown
100
+ let lastError: unknown
101
+
102
+ const active = (token: number): boolean => !disposed && token === generation
103
+
104
+ /**
105
+ * One HTTP request in flight at a time.
106
+ *
107
+ * The interval, a focus/visibility wake-up and a manual poll can all fire in
108
+ * the same tick. Without this guard they read the SAME `since`, process the
109
+ * SAME deliveries twice and race to write the cursor: a retryable delivery
110
+ * can be skipped past by the later write and never reach the composer again.
111
+ */
112
+ let inFlight: Promise<DeliveriesResponse | undefined> | undefined
113
+
114
+ /**
115
+ * Stop the interval and cancel the in-flight request.
116
+ *
117
+ * A closure, not a method: `dispose()` is also called detached (a React
118
+ * cleanup or a destructured handle), where `this` is not the poller.
119
+ */
120
+ const stop = (): void => {
121
+ polling = false
122
+ generation += 1
123
+ cancellation.reset()
124
+ inFlight = undefined
125
+ if (handle !== undefined) { scheduler.clearInterval(handle); handle = undefined }
126
+ }
127
+
128
+ const runPoll = async (): Promise<DeliveriesResponse | undefined> => {
129
+ if (disposed) return undefined
130
+ const token = generation
131
+ const isActive = (): boolean => active(token)
132
+ const sessionId = options.getSessionId()
133
+ // No session yet: the server has nothing scoped to answer with, and asking
134
+ // anyway would return every conversation's records for the cursor to skip.
135
+ if (sessionId === '') return undefined
136
+ const since = options.getSince()
137
+ let response: DeliveriesResponse
138
+ try {
139
+ response = await http.deliveries(sessionId, since)
140
+ } catch (error: unknown) {
141
+ if (!isActive()) return undefined
142
+ lastError = error
143
+ // A network failure never calls onCycle, so this is the ONLY place the
144
+ // dock can learn the plugin is offline: publish on the failure path too.
145
+ options.onSettled?.()
146
+ return undefined
147
+ }
148
+ if (!isActive()) return undefined
149
+ // A successful, well-formed poll clears any previous outage so the dock
150
+ // banner disappears on its own (never a sticky false alarm).
151
+ lastError = undefined
152
+ try {
153
+ await options.onCycle(response, sessionId, isActive)
154
+ if (isActive()) options.onSettled?.()
155
+ } catch (error: unknown) {
156
+ lastError = error
157
+ // 循环体抛错以前是静默的:卡片永远不出现、发送永远禁用,而页面上没有任何线索。
158
+ // 这里必须留一行可读的诊断(控制台一处,dock 的可用性横幅由 onSettled 负责)。
159
+ console.warn('[dsh-builtin-browser] delivery cycle failed:', error)
160
+ if (isActive()) options.onSettled?.()
161
+ }
162
+ return response
163
+ }
164
+
165
+ /** Coalesce concurrent triggers onto the in-flight request. */
166
+ const pollOnce = (): Promise<DeliveriesResponse | undefined> => {
167
+ if (disposed) return Promise.resolve(undefined)
168
+ if (inFlight !== undefined) return inFlight
169
+ const task = runPoll().finally(() => {
170
+ // A session switch may already have queued a fresh request; only clear
171
+ // our own slot so the new one is not dropped by our late completion.
172
+ if (inFlight === task) inFlight = undefined
173
+ })
174
+ inFlight = task
175
+ return task
176
+ }
177
+
178
+ return {
179
+ pollOnce,
180
+ start() {
181
+ if (disposed || polling) return
182
+ polling = true
183
+ handle = scheduler.setInterval(() => { void pollOnce() }, interval)
184
+ void pollOnce()
185
+ },
186
+ stop,
187
+ setSession(_sessionId: string) {
188
+ // The live getSessionId/getSince callbacks already point at the new
189
+ // session; cancelling the in-flight request prevents a cross-session
190
+ // response from being applied to the old draft. Drop the old slot too,
191
+ // so a wake-up for the NEW conversation is not answered by the aborted
192
+ // request and does not have to wait a whole interval.
193
+ generation += 1
194
+ cancellation.reset()
195
+ inFlight = undefined
196
+ // While polling is active, fetch the NEW session's cards immediately
197
+ // instead of making the user wait a whole interval (up to 1500 ms).
198
+ if (polling && !disposed) void pollOnce()
199
+ },
200
+ dispose() {
201
+ if (disposed) return
202
+ stop()
203
+ disposed = true
204
+ cancellation.abort()
205
+ },
206
+ isPolling: () => polling,
207
+ get disposed() { return disposed },
208
+ get lastError() { return lastError },
209
+ cancellation,
210
+ }
211
+ }