dsh-builtin-browser 1.0.1 → 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 (222) hide show
  1. package/CHANGELOG.md +55 -0
  2. package/README.md +1 -1
  3. package/assets/browser-panel-locales.mjs +37 -5
  4. package/assets/browser-policy.d.mts +29 -0
  5. package/assets/browser-policy.mjs +40 -0
  6. package/desktop-bridge/desktop-discovery.d.mts +1 -1
  7. package/desktop-bridge/desktop-discovery.mjs +59 -9
  8. package/desktop-bridge/desktop-identity.json +12 -0
  9. package/desktop-bridge/install.mjs +14 -7
  10. package/desktop-bridge/plugin-browser-bridge.js +19 -7
  11. package/docs/acceptance-2026-11-27.md +67 -0
  12. package/docs/architecture.md +1 -1
  13. package/docs/development.md +33 -4
  14. package/docs/dsh-0.2.3-host-patch-task.md +34 -0
  15. package/docs/hardcoding-audit.md +28 -8
  16. package/docs/known-issues.md +26 -0
  17. package/docs/limitations.md +6 -2
  18. package/docs/tool-reference.md +1 -1
  19. package/docs/v1.1.0-bu-data-batching-task.md +66 -0
  20. package/docs/v1.1.0-compat-hardcoding-task.md +85 -0
  21. package/dsh-host-patch/README.md +4 -0
  22. package/dsh-host-patch/apply.mjs +61 -13
  23. package/dsh-host-patch/rc3-revision9.json.gz +0 -0
  24. package/dsh-host-patch/rc5-revision9.json.gz +0 -0
  25. package/dsh-host-patch/tauri/desktop-main.cjs +62 -0
  26. package/dsh-host-patch/tauri/sidebar-session.cjs +104 -0
  27. package/dsh-host-patch/tauri-revision1.json +39 -0
  28. package/dsh-host-patch/tauri.json +43 -0
  29. package/dsh-host-patch/v023-alpha2.json +153 -0
  30. package/lib/browser/anti-bot.js +215 -0
  31. package/lib/browser/deep-dom.js +39 -0
  32. package/lib/browser/runtime.js +15 -0
  33. package/lib/browser/takeover-target.js +13 -6
  34. package/lib/browser/types.js +30 -1
  35. package/lib/browser-electron/authenticated-route.js +16 -2
  36. package/lib/browser-electron/automatic-maintenance.js +16 -10
  37. package/lib/browser-electron/bridge-connection.js +3 -8
  38. package/lib/browser-electron/bridge-install.js +2 -1
  39. package/lib/browser-electron/bridge-status.js +55 -26
  40. package/lib/browser-electron/desktop-bridge-host.js +7 -6
  41. package/lib/browser-electron/desktop-identity.js +28 -5
  42. package/lib/browser-electron/desktop-shell.js +64 -0
  43. package/lib/browser-electron/electron-provision.js +105 -18
  44. package/lib/browser-electron/entry.js +129 -44
  45. package/lib/browser-electron/extension-browser.js +4 -4
  46. package/lib/browser-electron/foreground-watch.js +134 -0
  47. package/lib/browser-electron/guest-cookie-jar.js +13 -13
  48. package/lib/browser-electron/guest-route.js +181 -32
  49. package/lib/browser-electron/guest-runtime.js +61 -10
  50. package/lib/browser-electron/guest-storage.js +2 -2
  51. package/lib/browser-electron/history-store.js +1 -5
  52. package/lib/browser-electron/host-main.js +8 -4
  53. package/lib/browser-electron/host-patch-maintenance.js +14 -8
  54. package/lib/browser-electron/maintenance-route.js +2 -1
  55. package/lib/browser-electron/missing-system-browser.js +5 -9
  56. package/lib/browser-electron/preferred-browser.js +7 -3
  57. package/lib/browser-electron/preview-route.js +9 -13
  58. package/lib/browser-electron/process-ownership.js +1 -1
  59. package/lib/browser-electron/profile-command.js +2 -1
  60. package/lib/browser-electron/profile-extension.js +5 -0
  61. package/lib/browser-electron/profile-lease.js +152 -47
  62. package/lib/browser-electron/provider-core.js +527 -0
  63. package/lib/browser-electron/provider-interaction.js +963 -0
  64. package/lib/browser-electron/provider-media.js +518 -0
  65. package/lib/browser-electron/provider-navigation.js +301 -0
  66. package/lib/browser-electron/provider-observation.js +623 -0
  67. package/lib/browser-electron/provider-tabs.js +372 -0
  68. package/lib/browser-electron/provider.js +150 -3301
  69. package/lib/browser-electron/reference-route.js +9 -18
  70. package/lib/browser-electron/remote-host.js +79 -34
  71. package/lib/browser-electron/runtime-status.js +56 -6
  72. package/lib/browser-electron/settings-store.js +3 -6
  73. package/lib/browser-electron/sidebar-only-host.js +1 -1
  74. package/lib/browser-electron/system-browser.js +539 -172
  75. package/lib/browser-electron/tauri-host.js +208 -0
  76. package/lib/browser-electron/virtual-cursor.js +23 -13
  77. package/lib/browser-reference/service.js +22 -4
  78. package/lib/browser-reference/web-sources.js +1 -1
  79. package/lib/client.js +43409 -0
  80. package/lib/compat/host-features.js +75 -0
  81. package/lib/reference-source.js +1 -1
  82. package/lib/reference-text.js +23 -1
  83. package/lib/takeover-protocol.js +1 -1
  84. package/lib/tool-browser/block-recovery.js +69 -0
  85. package/lib/tool-browser/code-tool.js +170 -19
  86. package/lib/tool-browser/code-worker.js +115 -20
  87. package/lib/tool-browser/errors.js +34 -0
  88. package/lib/tool-browser/index.js +66 -3593
  89. package/lib/tool-browser/page-probe.js +227 -28
  90. package/lib/tool-browser/panel-page.js +2 -1
  91. package/lib/tool-browser/probe-client.js +157 -0
  92. package/lib/tool-browser/screenshot-annotations.js +108 -0
  93. package/lib/tool-browser/state.js +337 -0
  94. package/lib/tool-browser/takeover.js +1 -1
  95. package/lib/tool-browser/tools-debug.js +862 -0
  96. package/lib/tool-browser/tools-interaction.js +993 -0
  97. package/lib/tool-browser/tools-navigation.js +807 -0
  98. package/lib/tool-browser/tools-observation.js +461 -0
  99. package/lib/tool-browser/tools-reference.js +293 -0
  100. package/lib/web-guest.js +1 -0
  101. package/package.json +16 -36
  102. package/scripts/build-client.mjs +305 -0
  103. package/scripts/build.mjs +15 -12
  104. package/scripts/fix-exports.mjs +6 -16
  105. package/scripts/install-electron.mjs +55 -5
  106. package/scripts/pack.mjs +99 -0
  107. package/scripts/publish-manifest.mjs +56 -0
  108. package/scripts/verify-exports.mjs +26 -15
  109. package/skills/dsh-builtin-browser/SKILL.md +27 -2
  110. package/skills/dsh-builtin-browser/references/batch.md +15 -0
  111. package/src/browser/anti-bot.ts +251 -0
  112. package/src/browser/deep-dom.ts +40 -0
  113. package/src/browser/runtime.ts +18 -0
  114. package/src/browser/takeover-target.ts +16 -7
  115. package/src/browser/types.ts +56 -3
  116. package/src/browser-electron/authenticated-route.ts +29 -4
  117. package/src/browser-electron/automatic-maintenance.ts +23 -13
  118. package/src/browser-electron/bridge-connection.ts +11 -10
  119. package/src/browser-electron/bridge-install.ts +2 -1
  120. package/src/browser-electron/bridge-status.ts +61 -22
  121. package/src/browser-electron/desktop-bridge-host.ts +9 -7
  122. package/src/browser-electron/desktop-identity.ts +38 -5
  123. package/src/browser-electron/desktop-shell.ts +100 -0
  124. package/src/browser-electron/electron-provision.ts +104 -18
  125. package/src/browser-electron/entry.ts +1378 -1299
  126. package/src/browser-electron/extension-browser.ts +11 -4
  127. package/src/browser-electron/foreground-watch.ts +142 -0
  128. package/src/browser-electron/guest-cookie-jar.ts +3 -2
  129. package/src/browser-electron/guest-route.ts +180 -32
  130. package/src/browser-electron/guest-runtime.ts +61 -10
  131. package/src/browser-electron/guest-storage.ts +5 -2
  132. package/src/browser-electron/history-store.ts +3 -6
  133. package/src/browser-electron/host-main.ts +8 -4
  134. package/src/browser-electron/host-patch-maintenance.ts +18 -9
  135. package/src/browser-electron/maintenance-route.ts +2 -1
  136. package/src/browser-electron/missing-system-browser.ts +11 -10
  137. package/src/browser-electron/preferred-browser.ts +13 -3
  138. package/src/browser-electron/preview-route.ts +9 -12
  139. package/src/browser-electron/process-ownership.ts +9 -3
  140. package/src/browser-electron/profile-command.ts +2 -1
  141. package/src/browser-electron/profile-extension.ts +5 -0
  142. package/src/browser-electron/profile-lease.ts +131 -35
  143. package/src/browser-electron/provider-core.ts +1107 -0
  144. package/src/browser-electron/provider-interaction.ts +1034 -0
  145. package/src/browser-electron/provider-media.ts +589 -0
  146. package/src/browser-electron/provider-navigation.ts +323 -0
  147. package/src/browser-electron/provider-observation.ts +664 -0
  148. package/src/browser-electron/provider-tabs.ts +372 -0
  149. package/src/browser-electron/provider.ts +158 -4063
  150. package/src/browser-electron/reference-route.ts +598 -608
  151. package/src/browser-electron/remote-host.ts +118 -42
  152. package/src/browser-electron/runtime-status.ts +80 -13
  153. package/src/browser-electron/settings-store.ts +10 -6
  154. package/src/browser-electron/sidebar-only-host.ts +4 -2
  155. package/src/browser-electron/system-browser.ts +537 -184
  156. package/src/browser-electron/tauri-host.ts +194 -0
  157. package/src/browser-electron/virtual-cursor.ts +24 -14
  158. package/src/browser-reference/service.ts +38 -7
  159. package/src/browser-reference/web-sources.ts +4 -1
  160. package/src/client/apply-guard.ts +22 -0
  161. package/src/client/browser/BrowserController.ts +3 -3
  162. package/src/client/browser/IframeImpl.ts +182 -176
  163. package/src/client/browser/frame-history.ts +125 -0
  164. package/src/client/browser/frame-state.ts +27 -0
  165. package/src/client/browser/web-reference-source.ts +5 -1
  166. package/src/client/definition.tsx +36 -30
  167. package/src/client/electron/ElectronWebViewImpl.ts +20 -13
  168. package/src/client/electron/ElectronWebviewPresentation.ts +4 -2
  169. package/src/client/electron/NativeChildPresentation.ts +11 -3
  170. package/src/client/electron/NativeViewPage.ts +19 -5
  171. package/src/client/index.ts +333 -293
  172. package/src/client/locales.ts +9 -6
  173. package/src/client/reference/client/plugin.ts +21 -2
  174. package/src/client/reference/client/production/ReferenceCapsules.tsx +3 -3
  175. package/src/client/reference/client/production/assembly.ts +4 -28
  176. package/src/client/reference/client/production/availability.ts +0 -2
  177. package/src/client/reference/client/production/composer-attachments.ts +44 -0
  178. package/src/client/reference/client/production/draft-adapter.ts +4 -0
  179. package/src/client/reference/client/production/http-transport.ts +0 -5
  180. package/src/client/reference/client/production/production-submission.ts +50 -290
  181. package/src/client/reference/client/production/protocol.ts +7 -4
  182. package/src/client/reference/client/production/runtime.ts +41 -86
  183. package/src/client/reference/client/production/source-marker.ts +49 -13
  184. package/src/client/reference/client/production/user-text-rule.ts +10 -1
  185. package/src/client/reference/client/reveal.ts +3 -1
  186. package/src/client/reference/client/session-schedule.ts +36 -0
  187. package/src/client/settings/index.ts +8 -0
  188. package/src/client/settings/mount-guard.ts +31 -0
  189. package/src/client/settings/panel.ts +163 -52
  190. package/src/client/startpage/controller.ts +3 -6
  191. package/src/client/view/BrowserBody.tsx +2 -1
  192. package/src/client/view/IframePresentation.ts +37 -16
  193. package/src/client/view/StartPage.tsx +2 -1
  194. package/src/compat/host-features.ts +92 -0
  195. package/src/reference-source.ts +1 -1
  196. package/src/reference-text.ts +23 -1
  197. package/src/takeover-protocol.ts +11 -2
  198. package/src/tool-browser/block-recovery.ts +85 -0
  199. package/src/tool-browser/code-tool.ts +173 -19
  200. package/src/tool-browser/code-worker.ts +101 -15
  201. package/src/tool-browser/errors.ts +40 -0
  202. package/src/tool-browser/index.ts +66 -3534
  203. package/src/tool-browser/page-probe.ts +229 -28
  204. package/src/tool-browser/panel-page.ts +3 -1
  205. package/src/tool-browser/probe-client.ts +183 -0
  206. package/src/tool-browser/screenshot-annotations.ts +131 -0
  207. package/src/tool-browser/state.ts +375 -0
  208. package/src/tool-browser/takeover.ts +3 -2
  209. package/src/tool-browser/tools-debug.ts +806 -0
  210. package/src/tool-browser/tools-interaction.ts +956 -0
  211. package/src/tool-browser/tools-navigation.ts +786 -0
  212. package/src/tool-browser/tools-observation.ts +474 -0
  213. package/src/tool-browser/tools-reference.ts +294 -0
  214. package/src/web-guest.ts +35 -34
  215. package/dist/client.cjs +0 -43167
  216. package/dist/client.d.cts +0 -516
  217. package/dist/client.d.mts +0 -516
  218. package/dist/index.cjs +0 -92
  219. package/dist/index.d.mts +0 -2960
  220. package/dist/index.mjs +0 -24
  221. package/lib/browser-reference/submission-receipts.js +0 -139
  222. package/src/browser-reference/submission-receipts.ts +0 -122
@@ -1,9 +1,10 @@
1
1
  /// <reference path="../css-modules.d.ts" />
2
- import { GUEST_ROUTE } from '../../../assets/browser-policy.mjs'
2
+ import { GUEST_ROUTE, RELAXED_BROWSER_SANDBOX, WEB_BROWSER_SANDBOX } from '../../../assets/browser-policy.mjs'
3
3
  /** Iframe DOM and revision-tagged load observations. */
4
4
  import type { BrowserTarget } from '../browser/url.ts'
5
5
  import type { BrowserPresentation } from './BrowserPresentation.ts'
6
6
  import css from './Browser.module.css'
7
+ import { GuestFrameHistory } from '../browser/frame-history.ts'
7
8
  import { registerIframePage, registerIframeReference, type GuestReferenceCommand } from '../browser/iframe-reference.ts'
8
9
  import { connectWebReferenceSource } from '../browser/web-reference-source.ts'
9
10
  import { registerTakeoverSummary } from '../browser/takeover-channel.ts'
@@ -32,10 +33,6 @@ function capturedSourceUrl(value: unknown): unknown {
32
33
  return value
33
34
  }
34
35
 
35
- /** Opaque guest documents cannot access the application's origin or open native windows. */
36
- export const WEB_BROWSER_SANDBOX = 'allow-scripts allow-forms'
37
- const RELAXED_BROWSER_SANDBOX = `${WEB_BROWSER_SANDBOX} allow-downloads allow-modals allow-pointer-lock allow-orientation-lock allow-presentation allow-storage-access-by-user-activation`
38
-
39
36
  interface IframeDocument {
40
37
  readonly target: BrowserTarget
41
38
  readonly revision: number
@@ -72,7 +69,7 @@ export class IframePresentation implements BrowserPresentation {
72
69
  private element: HTMLIFrameElement | undefined
73
70
  private document: IframeDocument | undefined
74
71
  private rendered = false
75
- private depth = 1
72
+ private readonly frameHistory = new GuestFrameHistory()
76
73
  private loadId: number | undefined
77
74
  private documentId: string | undefined
78
75
  private loadedId: number | undefined
@@ -82,7 +79,12 @@ export class IframePresentation implements BrowserPresentation {
82
79
  private identityTimer: ReturnType<typeof setTimeout> | undefined
83
80
  private identityRetried = false
84
81
  private readonly receive = (event: MessageEvent): void => {
85
- if (event.origin !== 'null' || event.source !== this.element?.contentWindow) return
82
+ // A sandboxed guest posts from the opaque origin `null`. A carrier that granted the guest its
83
+ // own origin (storage-compatible mode) posts from this application's origin instead. Both can
84
+ // only be this frame's own document; the source check is what proves it, and any other origin
85
+ // is a different frame and is dropped.
86
+ const ownOrigin = event.origin === 'null' || event.origin === location.origin
87
+ if (!ownOrigin || event.source !== this.element?.contentWindow) return
86
88
  const data = event.data
87
89
  if (data?.type === 'dsh.browser.guest.child.request') {
88
90
  if (this.loadId === undefined || data.loadId !== this.loadId || !Number.isSafeInteger(data.requestId)
@@ -128,6 +130,7 @@ export class IframePresentation implements BrowserPresentation {
128
130
  if (data.documentId !== this.documentId) {
129
131
  const nativeLoaded = this.documentId === undefined && this.nativeLoadId === this.loadId
130
132
  this.invalidateGuest()
133
+ this.frameHistory.noteDocumentChange()
131
134
  this.loadId = ++this.nextLoadId
132
135
  this.documentId = data.documentId
133
136
  if (nativeLoaded) this.nativeLoadId = this.loadId
@@ -143,7 +146,11 @@ export class IframePresentation implements BrowserPresentation {
143
146
  clearTimeout(this.identityTimer)
144
147
  this.identityTimer = undefined
145
148
  this.identityRetried = false
146
- this.depth = Number.isSafeInteger(data.depth) && data.depth > 0 ? data.depth : 1
149
+ this.frameHistory.record({
150
+ depth: Number.isSafeInteger(data.depth) && data.depth > 0 ? data.depth : 1,
151
+ reason: typeof data.reason === 'string' ? data.reason : undefined,
152
+ navigationType: typeof data.navigationType === 'string' ? data.navigationType : undefined,
153
+ })
147
154
  if (data.ready === true) this.markLoaded()
148
155
  this.observedUrl = data.url
149
156
  this.events.observed(this.document.revision, data.url)
@@ -153,9 +160,11 @@ export class IframePresentation implements BrowserPresentation {
153
160
  }
154
161
  if (data.ready === true) this.events.guestReady?.(this.document.revision, this.pageId)
155
162
  }
156
-
157
- /** @param events - provider-owned load and remount callbacks. */
158
- constructor(private readonly events: IframePresentationEvents, private readonly owner?: string) {
163
+ private readonly events: IframePresentationEvents
164
+ private readonly owner?: string
165
+ constructor(events: IframePresentationEvents, owner?: string) {
166
+ this.events = events
167
+ this.owner = owner
159
168
  if (owner !== undefined) this.sourceLifetime.signal.addEventListener('abort', registerTakeoverSummary(this.pageId, owner, async signal => {
160
169
  const result = await this.command('summary', { maxChars: 1500, legacy: false }, signal, this.observedUrl ?? '', this.loadId)
161
170
  return result as unknown as TakeoverSummary
@@ -225,19 +234,25 @@ export class IframePresentation implements BrowserPresentation {
225
234
 
226
235
  /** Request history movement in the guest without reading its cross-origin window. */
227
236
  historyStep(delta: number): boolean {
237
+ if (!this.frameHistory.canStep(delta)) return false
228
238
  const win = this.element?.contentWindow
229
239
  if (win === undefined || win === null) return false
230
240
  try {
231
241
  win.postMessage({ type: 'dsh.browser.guest.history', delta: delta < 0 ? -1 : 1 }, '*')
232
- return true
233
242
  } catch {
234
243
  return false
235
244
  }
245
+ return this.frameHistory.step(delta)
236
246
  }
237
247
 
238
- /** History depth reported by the guest. */
248
+ /** Entries this guest iframe itself added, counting the current one; the joint session history is excluded. */
239
249
  historyDepth(): number {
240
- return this.depth
250
+ return this.frameHistory.depth()
251
+ }
252
+
253
+ /** Whether the guest iframe has a forward entry of its own. */
254
+ canGoForward(): boolean {
255
+ return this.frameHistory.canGoForward()
241
256
  }
242
257
 
243
258
  /** @param viewportId - mounted placeholder. @returns removes only the iframe presentation. */
@@ -296,7 +311,13 @@ export class IframePresentation implements BrowserPresentation {
296
311
  this.identityTimer = setTimeout(() => {
297
312
  this.identityTimer = undefined
298
313
  if (revision === undefined || this.document?.revision !== revision) return
299
- this.events.guestFailed?.(revision, 'GUEST_DOCUMENT_IDENTITY_UNAVAILABLE: source iframe did not provide a matching document identity')
314
+ // Nothing answers this handshake today: the guest overlay listens for `dsh-browser-ref:intent`
315
+ // and never posts a `dsh.browser.guest.navigation` identity, and a directly loaded document
316
+ // carries no overlay at all. Failing the document here painted a five-second "load failed"
317
+ // banner over a page that had already loaded, and `guestFailed` also marked the whole carrier
318
+ // unusable. Report the document as loaded instead; the reference channel stays unavailable,
319
+ // which is what the states actually are.
320
+ this.markLoaded()
300
321
  }, 5000)
301
322
  }
302
323
 
@@ -335,7 +356,7 @@ export class IframePresentation implements BrowserPresentation {
335
356
  element.addEventListener('error', () => {
336
357
  if (this.element === element) { this.invalidateGuest(); this.events.failed(current.revision) }
337
358
  })
338
- this.depth = 1
359
+ this.frameHistory.reset(history.length)
339
360
  this.loadId = ++this.nextLoadId
340
361
  this.element = element
341
362
  this.rendered = true
@@ -10,6 +10,7 @@ import type { TranslateNS } from '@deepseek-ai/dsh-client-ui-slots'
10
10
  import type { ContextMenuBindings } from '@deepseek-ai/dsh-client-ui-context-menu/client'
11
11
  import type { SessionId } from '@deepseek-ai/dsh-session/types'
12
12
  import type { TabId } from '@deepseek-ai/dsh-client-ui-dockkit'
13
+ import type { SIDEBAR_BROWSER_LOCALE_NS } from '../locales.ts'
13
14
  import type { BrowserStartPageInjected } from '../startpage/binding.ts'
14
15
  import type { StartPageSnapshot, StartPageWriteResult } from '../startpage/controller.ts'
15
16
  import type { SearchEngineDraft, ShortcutDraft, StartPageEngineView, StartPageFailure, StartPageShortcutView } from '../startpage/model.ts'
@@ -17,7 +18,7 @@ import { MAX_ENGINE_EXTRA_PARAMETERS } from '../startpage/model.ts'
17
18
  import type { BrowserShortcut } from '../../startpage-settings.ts'
18
19
  import css from './Browser.module.css'
19
20
 
20
- type Translate = TranslateNS<'sidebarBrowser'>
21
+ type Translate = TranslateNS<typeof SIDEBAR_BROWSER_LOCALE_NS>
21
22
 
22
23
  const UNDO_MS = 8000
23
24
 
@@ -0,0 +1,92 @@
1
+ /**
2
+ * Runtime feature probes for the DSH host, in one place.
3
+ *
4
+ * Host lines are not comparable by version string (the official 0.2.3-alpha.2 and a community
5
+ * shell's 0.14.0 share nothing), so every check here asks the running host what it offers instead.
6
+ * Dependency-free on purpose: both the Node host half and the browser client half import it.
7
+ * @module dsh-browser/compat/host-features
8
+ */
9
+ import type { DesktopBrowserBridge, DesktopBrowserChildTabs, DesktopNativeChildTabs, DesktopNativeViews } from '../types.ts'
10
+
11
+ interface ServiceLookup { readonly get?: (name: string) => unknown }
12
+ interface ToolRegistry { readonly register: (tool: never, options?: never) => unknown }
13
+
14
+ interface ScopedExecutors { readonly selectExecutor?: (scope?: unknown) => { readonly describe?: (tools: readonly never[]) => { readonly knownNames?: readonly string[] } } }
15
+
16
+ /**
17
+ * Whether DSH's PTC executor is active, so `run_code` already batches every tool.
18
+ *
19
+ * Since 0.2.2-rc.5 dsh-base mounts the `ptcRuntime` provider in every mode and only the PTC
20
+ * overlay selects its executor, so a registry that selects executors per scope is asked which
21
+ * one the calling agent gets; an older registry has no selector and the service alone decides.
22
+ * @param ctx - the plugin context.
23
+ * @param scope - the tool-activation context (`{ agent }`) a conditional registration receives.
24
+ * @returns true when the agent's executor exposes `run_code`, or, without a selector, when `ptcRuntime` is present.
25
+ */
26
+ export function ptcRuntimeActive(ctx: unknown, scope?: { readonly agent?: unknown }): boolean {
27
+ try {
28
+ if ((ctx as ServiceLookup).get?.('ptcRuntime') === undefined) return false
29
+ const tools = (ctx as { readonly tools?: ScopedExecutors }).tools
30
+ if (typeof tools?.selectExecutor !== 'function') return true
31
+ return tools.selectExecutor(scope?.agent)?.describe?.([])?.knownNames?.includes('run_code') === true
32
+ } catch { return false }
33
+ }
34
+
35
+ /**
36
+ * Register a tool, visible only while `when` holds.
37
+ *
38
+ * Registration options arrived with DSH 0.2.2; an older registry ignores the second argument and
39
+ * keeps the tool always visible, which is the safe fallback for every caller here.
40
+ * @param registry - `ctx.tools`.
41
+ * @param tool - the tool definition.
42
+ * @param when - visibility predicate; omitted registers unconditionally.
43
+ * @returns whatever the registry returns.
44
+ */
45
+ export function registerTool<C>(registry: ToolRegistry, tool: unknown, when?: (context?: C) => boolean): unknown {
46
+ const register = registry.register as (tool: unknown, options?: { when: (context?: C) => boolean }) => unknown
47
+ return when === undefined ? register.call(registry, tool) : register.call(registry, tool, { when })
48
+ }
49
+
50
+ interface DesktopCarrier { readonly protocolVersion?: unknown; readonly browser?: DesktopBrowserBridge }
51
+
52
+ /**
53
+ * The renderer's narrow desktop browser bridge (`window.dshDesktop`, protocol 1).
54
+ * @param scope - the global object; injectable for tests.
55
+ * @returns the bridge, or undefined outside a desktop shell that speaks protocol 1.
56
+ */
57
+ export function desktopBrowserBridge(scope: object = globalThis): DesktopBrowserBridge | undefined {
58
+ const carrier = (scope as { dshDesktop?: DesktopCarrier | null }).dshDesktop
59
+ return carrier !== null && typeof carrier === 'object' && carrier.protocolVersion === 1 ? carrier.browser : undefined
60
+ }
61
+
62
+ /**
63
+ * Native views (protocol 1) of the desktop bridge.
64
+ * @param scope - the global object, or the bridge itself.
65
+ * @returns the native views API, or undefined when the shell does not offer protocol 1.
66
+ */
67
+ export function nativeViews(scope: object = globalThis): DesktopNativeViews | undefined {
68
+ const views = (scope as { browser?: { nativeViews?: DesktopNativeViews } }).browser?.nativeViews
69
+ ?? (scope as { dshDesktop?: { browser?: { nativeViews?: DesktopNativeViews } } | null }).dshDesktop?.browser?.nativeViews
70
+ ?? (scope as { nativeViews?: DesktopNativeViews }).nativeViews
71
+ return views?.protocolVersion === 1 ? views : undefined
72
+ }
73
+
74
+ /**
75
+ * URL-only child tab requests (protocol 1).
76
+ * @param bridge - the desktop browser bridge.
77
+ * @returns the API, or undefined when the shell offers another protocol or none.
78
+ */
79
+ export function urlChildTabs(bridge: Pick<DesktopBrowserBridge, 'childTabs'> | undefined): DesktopBrowserChildTabs | undefined {
80
+ const tabs = bridge?.childTabs
81
+ return tabs?.protocolVersion === 1 ? tabs : undefined
82
+ }
83
+
84
+ /**
85
+ * Native opener child tabs (protocol 2).
86
+ * @param bridge - the desktop browser bridge.
87
+ * @returns the API, or undefined when the shell offers another protocol or none.
88
+ */
89
+ export function nativeChildTabs(bridge: Pick<DesktopBrowserBridge, 'childTabs'> | undefined): DesktopNativeChildTabs | undefined {
90
+ const tabs = bridge?.childTabs
91
+ return tabs?.protocolVersion === 2 ? tabs : undefined
92
+ }
@@ -1,3 +1,3 @@
1
- export const WEB_SOURCE_ROUTE = '/dsh-builtin-browser/reference/source'
1
+ export { WEB_SOURCE_ROUTE } from '../assets/browser-policy.mjs'
2
2
  export const WEB_REVEAL_STATUSES = ['revealed', 'partial', 'ambiguous', 'missing', 'not-found', 'page-mismatch', 'unavailable'] as const
3
3
  export type WebRevealStatus = typeof WEB_REVEAL_STATUSES[number]
@@ -1,3 +1,5 @@
1
+ import { GUEST_ROUTE } from '../assets/browser-policy.mjs'
2
+
1
3
  export const MAX_REFERENCE_TEXT = 20_000
2
4
 
3
5
  export type ReferenceTextNode =
@@ -65,6 +67,26 @@ function escapeMarkdown(text: string): string {
65
67
  return text.replace(/[\\`*_{}[\]<>!|#]/gu, '\\$&')
66
68
  }
67
69
 
70
+ /**
71
+ * A captured href comes from the guest document, where the proxy rewrote every link to its own
72
+ * route (`<GUEST_ROUTE>?u=<real>`). The projection must show the site's URL, never this machine's.
73
+ *
74
+ * The captured text is what the model reads and what the composer renders, so a proxy URL here
75
+ * showed up as a blue `http://127.0.0.1:<port>/…` link inside the user's own message.
76
+ * @param href - the href recorded in the snapshot.
77
+ * @returns the site's URL, or the input when it is not a guest-proxy link.
78
+ */
79
+ function realHref(href: string): string {
80
+ const marker = `${GUEST_ROUTE}?`
81
+ if (!href.includes(marker)) return href
82
+ try {
83
+ const raw = new URL(href).searchParams.get('u')
84
+ return raw === null || raw === '' ? href : raw
85
+ } catch {
86
+ return href
87
+ }
88
+ }
89
+
68
90
  export function referenceTextContent(nodes: readonly ReferenceTextNode[]): string {
69
91
  return nodes.map(node => node.type === 'text' ? node.text : node.type === 'math' ? node.latex : node.type === 'rule' ? '\n---\n' : node.type === 'break' ? '\n' : referenceTextContent(node.children)).join('')
70
92
  }
@@ -107,7 +129,7 @@ export function renderReferenceText(nodes: readonly ReferenceTextNode[]): string
107
129
  case 'ruby-text': return `<rt>${text}</rt>`
108
130
  case 'strong': return `**${text}**`
109
131
  case 'emphasis': return `*${text}*`
110
- case 'link': return `[${text}](${node.href.replace(/[\s()<>\\]/gu, char => encodeURIComponent(char).replace(/[()]/gu, token => `%${token.charCodeAt(0).toString(16).toUpperCase()}`))})`
132
+ case 'link': return `[${text}](${realHref(node.href).replace(/[\s()<>\\]/gu, char => encodeURIComponent(char).replace(/[()]/gu, token => `%${token.charCodeAt(0).toString(16).toUpperCase()}`))})`
111
133
  case 'blockquote': return text.trim().split('\n').map(line => `> ${line}`).join('\n')
112
134
  case 'list-item': return text
113
135
  case 'list': {
@@ -1,7 +1,16 @@
1
- export const TAKEOVER_ROUTE = '/dsh-builtin-browser/browser/takeover'
1
+ export { TAKEOVER_ROUTE } from '../assets/browser-policy.mjs'
2
2
  export const TAKEOVER_TOOL = 'dsh_browser_request_takeover'
3
3
 
4
- export type TakeoverOutcome = 'done' | 'skipped' | 'cancelled'
4
+ /**
5
+ * What the tool reports back to the model.
6
+ *
7
+ * `unavailable` is its own outcome on purpose: it means this host has no way to reach a person
8
+ * (no approval channel and no sidebar card), which is a property of the host, not something the
9
+ * person decided. Folding it into `cancelled` told the model that a human had declined, and since
10
+ * the takeover guidance forbids solving a challenge alone, the only reachable answer became "not
11
+ * success" — a dead end rather than a decision.
12
+ */
13
+ export type TakeoverOutcome = 'done' | 'skipped' | 'cancelled' | 'unavailable'
5
14
  export type TakeoverDecision = 'allowed-once' | 'rejected' | 'cancelled' | 'unavailable'
6
15
 
7
16
  export interface TakeoverSummary {
@@ -0,0 +1,85 @@
1
+ /**
2
+ * The tool-facing half of anti-bot handling: one output schema and one projection for a blocked
3
+ * page's verdict, so every tool that reports a challenge reports the same fields — what blocked
4
+ * it, what to do, and the public routes to the same information.
5
+ * @module dsh-browser/tool-browser/block-recovery
6
+ */
7
+
8
+ import { fallbackRoutes, recoveryAdvice, type FallbackRoute } from '../browser/anti-bot.ts'
9
+ import type { BrowserChallenge } from '../browser/types.ts'
10
+
11
+ /** A verdict as tools return it. */
12
+ export interface ChallengeReport {
13
+ blocked: boolean
14
+ kind?: string
15
+ reason?: string
16
+ recovery?: NonNullable<BrowserChallenge['recovery']>
17
+ status?: number
18
+ /** Set when the open already made its one paced retry in a fresh session. */
19
+ retried?: boolean
20
+ advice?: string
21
+ fallbacks?: FallbackRoute[]
22
+ }
23
+
24
+ /** Output schema shared by dsh_browser_open, dsh_browser_snapshot and dsh_browser_challenge. */
25
+ export const CHALLENGE_SCHEMA = {
26
+ type: 'object',
27
+ additionalProperties: false,
28
+ properties: {
29
+ blocked: { type: 'boolean', required: true },
30
+ kind: { type: 'string' },
31
+ reason: { type: 'string' },
32
+ recovery: { type: 'string', enum: ['takeover', 'wait', 'retry', 'environment'] },
33
+ status: { type: 'number' },
34
+ retried: { type: 'boolean' },
35
+ advice: { type: 'string' },
36
+ fallbacks: {
37
+ type: 'array',
38
+ items: {
39
+ type: 'object',
40
+ additionalProperties: false,
41
+ properties: {
42
+ kind: { type: 'string', required: true, enum: ['api', 'search', 'archive'] },
43
+ url: { type: 'string', required: true },
44
+ note: { type: 'string', required: true },
45
+ },
46
+ },
47
+ },
48
+ },
49
+ } as const
50
+
51
+ /**
52
+ * Project a verdict for a tool result: a clean page stays `{ blocked: false }`; a blocked one gains
53
+ * the advice for its kind and the fallback routes for its URL.
54
+ * @param challenge - the provider's verdict.
55
+ * @param url - the page it is about.
56
+ * @param retried - whether the open already retried once.
57
+ * @returns the report.
58
+ */
59
+ export function reportChallenge(challenge: BrowserChallenge, url: string, retried = false): ChallengeReport {
60
+ if (challenge.blocked !== true) return { blocked: false }
61
+ const fallbacks = fallbackRoutes(url)
62
+ return {
63
+ blocked: true,
64
+ ...challenge.kind !== undefined ? { kind: challenge.kind } : {},
65
+ ...challenge.reason !== undefined ? { reason: challenge.reason } : {},
66
+ ...challenge.recovery !== undefined ? { recovery: challenge.recovery } : {},
67
+ ...typeof challenge.status === 'number' ? { status: challenge.status } : {},
68
+ ...retried ? { retried: true } : {},
69
+ advice: recoveryAdvice(challenge),
70
+ ...fallbacks.length > 0 ? { fallbacks } : {},
71
+ }
72
+ }
73
+
74
+ /**
75
+ * The banner a rendered result ends with when the page is blocked.
76
+ * @param report - the tool's challenge report.
77
+ * @returns the banner text, or '' for a clean page.
78
+ */
79
+ export function challengeBanner(report: ChallengeReport | undefined): string {
80
+ if (report?.blocked !== true) return ''
81
+ const head = `CHALLENGE (${report.kind ?? 'blocked'}${report.status !== undefined ? `, HTTP ${report.status}` : ''}): ${report.reason ?? 'human-verification'}.`
82
+ const retried = report.retried === true ? ' Already retried once in a fresh session after a pause.' : ''
83
+ const routes = (report.fallbacks ?? []).map(route => `\n - ${route.kind}: ${route.url} (${route.note})`).join('')
84
+ return `\n\n${head}${retried} ${report.advice ?? recoveryAdvice({ blocked: true })}${routes === '' ? '' : `\nFallback routes:${routes}`}`
85
+ }