dsh-builtin-browser 0.5.8 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (401) hide show
  1. package/CHANGELOG.md +410 -1
  2. package/README.en.md +16 -64
  3. package/README.md +33 -64
  4. package/assets/browser-carriers.svg +20 -0
  5. package/assets/browser-extension-identity.d.mts +2 -0
  6. package/assets/browser-extension-identity.mjs +2 -0
  7. package/assets/browser-messages.d.mts +5 -0
  8. package/assets/browser-messages.mjs +64 -0
  9. package/assets/browser-panel-locales.d.mts +2 -0
  10. package/assets/browser-panel-locales.mjs +380 -0
  11. package/assets/browser-policy.d.mts +37 -0
  12. package/assets/browser-policy.mjs +37 -0
  13. package/assets/browser-products.d.mts +6 -0
  14. package/assets/browser-products.mjs +76 -0
  15. package/assets/screenshots/1-main.png +0 -0
  16. package/assets/screenshots/2-form-fill.png +0 -0
  17. package/assets/screenshots/3-challenge.png +0 -0
  18. package/browser-extension/constants.mjs +20 -0
  19. package/browser-extension/controller.mjs +293 -0
  20. package/browser-extension/locales.mjs +15 -0
  21. package/browser-extension/manifest.json +12 -0
  22. package/browser-extension/options.html +14 -0
  23. package/browser-extension/options.mjs +9 -0
  24. package/browser-extension/permission-guard.mjs +13 -0
  25. package/browser-extension/worker.mjs +74 -0
  26. package/cordis.patch.yml +23 -48
  27. package/desktop-bridge/desktop-discovery.d.mts +2 -0
  28. package/desktop-bridge/desktop-discovery.mjs +36 -0
  29. package/desktop-bridge/desktop-identity.json +17 -1
  30. package/desktop-bridge/desktop-processes.d.mts +1 -0
  31. package/desktop-bridge/desktop-processes.mjs +15 -0
  32. package/desktop-bridge/guest-proxy.mjs +109 -0
  33. package/desktop-bridge/install.mjs +197 -74
  34. package/desktop-bridge/main.mjs +11 -0
  35. package/desktop-bridge/maintenance-files.d.mts +8 -0
  36. package/desktop-bridge/maintenance-files.mjs +28 -0
  37. package/desktop-bridge/maintenance-progress.ps1 +2 -2
  38. package/desktop-bridge/native-guests.mjs +41 -0
  39. package/desktop-bridge/plugin-browser-bridge.js +618 -370
  40. package/desktop-bridge/profile-loader.mjs +26 -0
  41. package/dist/client.cjs +43167 -0
  42. package/dist/client.d.cts +516 -0
  43. package/dist/client.d.mts +516 -0
  44. package/dist/index.cjs +92 -0
  45. package/dist/index.d.mts +2960 -0
  46. package/dist/index.mjs +24 -0
  47. package/docs/HANDOVER-2026-11-15.md +178 -0
  48. package/docs/README.md +10 -19
  49. package/docs/architecture.md +17 -145
  50. package/docs/child-tabs.md +13 -0
  51. package/docs/desktop-acceptance.md +9 -0
  52. package/docs/development.md +88 -18
  53. package/docs/extension-browser.md +44 -0
  54. package/docs/hardcoding-audit.md +64 -0
  55. package/docs/limitations.md +23 -73
  56. package/docs/pr64-extension-status.md +46 -0
  57. package/docs/pr64-maintenance.md +25 -0
  58. package/docs/pr64-native-children.md +52 -0
  59. package/docs/pr64-native-views.md +60 -0
  60. package/docs/tool-reference.md +87 -11
  61. package/docs/user-guide.md +47 -255
  62. package/docs/why-browser.md +6 -39
  63. package/dsh-host-patch/README.md +60 -0
  64. package/dsh-host-patch/apply.mjs +194 -0
  65. package/dsh-host-patch/legacy-bridge.mjs +53 -0
  66. package/dsh-host-patch/rc3-revision1.json +90 -0
  67. package/dsh-host-patch/rc3-revision2.json +112 -0
  68. package/dsh-host-patch/rc3-revision3.json.gz +0 -0
  69. package/dsh-host-patch/rc3-revision4.json.gz +0 -0
  70. package/dsh-host-patch/rc3-revision5.json.gz +0 -0
  71. package/dsh-host-patch/rc3-revision6.json.gz +0 -0
  72. package/dsh-host-patch/rc3-revision7.json.gz +0 -0
  73. package/dsh-host-patch/rc3-revision8.json.gz +0 -0
  74. package/dsh-host-patch/rc3.json +148 -0
  75. package/dsh-host-patch/rc5-revision1.json +90 -0
  76. package/dsh-host-patch/rc5-revision2.json +112 -0
  77. package/dsh-host-patch/rc5-revision3.json.gz +0 -0
  78. package/dsh-host-patch/rc5-revision4.json.gz +0 -0
  79. package/dsh-host-patch/rc5-revision5.json.gz +0 -0
  80. package/dsh-host-patch/rc5-revision6.json.gz +0 -0
  81. package/dsh-host-patch/rc5-revision7.json.gz +0 -0
  82. package/dsh-host-patch/rc5-revision8.json.gz +0 -0
  83. package/dsh-host-patch/rc5.json +148 -0
  84. package/lib/bridge-compatibility.js +38 -0
  85. package/lib/browser/handles.js +78 -0
  86. package/lib/browser/runtime.js +155 -43
  87. package/lib/browser/takeover-target.js +24 -0
  88. package/lib/browser-electron/authenticated-route.js +23 -0
  89. package/lib/browser-electron/automatic-maintenance.js +394 -51
  90. package/lib/browser-electron/bridge-auto.js +2 -1
  91. package/lib/browser-electron/bridge-connection.js +8 -1
  92. package/lib/browser-electron/bridge-install.js +27 -2
  93. package/lib/browser-electron/bridge-maintenance.js +35 -10
  94. package/lib/browser-electron/bridge-status.js +98 -27
  95. package/lib/browser-electron/desktop-bridge-host.js +193 -560
  96. package/lib/browser-electron/desktop-identity.js +40 -2
  97. package/lib/browser-electron/document-readiness.js +34 -0
  98. package/lib/browser-electron/electron-provision.js +14 -44
  99. package/lib/browser-electron/entry.js +659 -252
  100. package/lib/browser-electron/extension-browser.js +234 -0
  101. package/lib/browser-electron/extension-setup.js +69 -0
  102. package/lib/browser-electron/guest-cookie-jar.LICENSE.txt +32 -0
  103. package/lib/browser-electron/guest-cookie-jar.js +10559 -0
  104. package/lib/browser-electron/guest-cookie-store.js +74 -0
  105. package/lib/browser-electron/guest-css-runtime.js +75 -0
  106. package/lib/browser-electron/guest-css.js +106 -0
  107. package/lib/browser-electron/guest-route.js +603 -0
  108. package/lib/browser-electron/guest-runtime.js +356 -0
  109. package/lib/browser-electron/guest-storage-runtime.js +52 -0
  110. package/lib/browser-electron/guest-storage.js +163 -0
  111. package/lib/browser-electron/history-store.js +2 -1
  112. package/lib/browser-electron/host-main.js +162 -7
  113. package/lib/browser-electron/host-patch-maintenance.js +141 -0
  114. package/lib/browser-electron/http-body.js +22 -0
  115. package/lib/browser-electron/maintenance-broker.js +35 -5
  116. package/lib/browser-electron/maintenance-lifecycle.js +7 -5
  117. package/lib/browser-electron/maintenance-route.js +9 -6
  118. package/lib/browser-electron/open-slot.js +47 -0
  119. package/lib/browser-electron/paths.js +4 -1
  120. package/lib/browser-electron/preferred-browser.js +144 -0
  121. package/lib/browser-electron/preview-route.js +11 -8
  122. package/lib/browser-electron/process-ownership.js +198 -0
  123. package/lib/browser-electron/profile-checkpoint.js +222 -0
  124. package/lib/browser-electron/profile-command.js +60 -0
  125. package/lib/browser-electron/profile-extension.js +126 -0
  126. package/lib/browser-electron/profile-lease.js +157 -0
  127. package/lib/browser-electron/profile-residue.js +61 -0
  128. package/lib/browser-electron/provider.js +719 -262
  129. package/lib/browser-electron/reference-route.js +635 -0
  130. package/lib/browser-electron/remote-host.js +12 -13
  131. package/lib/browser-electron/runtime-route.js +6 -5
  132. package/lib/browser-electron/runtime-status.js +30 -29
  133. package/lib/browser-electron/settings-store.js +10 -5
  134. package/lib/browser-electron/shortcuts-route.js +108 -0
  135. package/lib/browser-electron/sidebar-only-host.js +119 -18
  136. package/lib/browser-electron/startpage-shortcuts.js +172 -0
  137. package/lib/browser-electron/system-browser.js +352 -371
  138. package/lib/browser-electron/windows-system.js +2 -1
  139. package/lib/browser-reference/delivery.js +326 -0
  140. package/lib/browser-reference/entry.js +597 -0
  141. package/lib/browser-reference/export.js +298 -0
  142. package/lib/browser-reference/import.js +295 -0
  143. package/lib/browser-reference/page-script.js +1406 -0
  144. package/lib/browser-reference/service.js +1062 -0
  145. package/lib/browser-reference/store.js +177 -0
  146. package/lib/browser-reference/submission-receipts.js +139 -0
  147. package/lib/browser-reference/text-quote.js +167 -0
  148. package/lib/browser-reference/types.js +11 -0
  149. package/lib/browser-reference/visible.js +311 -0
  150. package/lib/browser-reference/web-sources.js +97 -0
  151. package/lib/index.js +10 -3
  152. package/lib/reference-owner.js +5 -0
  153. package/lib/reference-page.js +12 -0
  154. package/lib/reference-source.js +2 -0
  155. package/lib/reference-text.js +188 -0
  156. package/lib/startpage-settings.js +63 -0
  157. package/lib/startpage.js +44 -0
  158. package/lib/takeover-protocol.js +2 -0
  159. package/lib/tool-browser/code-tool.js +416 -0
  160. package/lib/tool-browser/code-worker.js +872 -0
  161. package/lib/tool-browser/index.js +940 -76
  162. package/lib/tool-browser/page-probe.js +3 -3
  163. package/lib/tool-browser/panel-page.js +95 -0
  164. package/lib/tool-browser/screenshot-image.js +51 -0
  165. package/lib/tool-browser/takeover.js +205 -0
  166. package/lib/types.js +1 -0
  167. package/lib/web-guest.js +26 -0
  168. package/native-host/client.d.mts +17 -0
  169. package/native-host/client.mjs +100 -0
  170. package/native-host/host.mjs +150 -0
  171. package/native-host/identity.d.mts +2 -0
  172. package/native-host/identity.mjs +1 -0
  173. package/native-host/install.mjs +168 -0
  174. package/native-host/protocol.mjs +75 -0
  175. package/package.json +80 -92
  176. package/scripts/build.mjs +66 -0
  177. package/scripts/bundle-host.mjs +28 -0
  178. package/scripts/fix-exports.mjs +50 -0
  179. package/scripts/install-electron.mjs +11 -26
  180. package/scripts/verify-exports.mjs +47 -0
  181. package/scripts/verify-preview-sdk.mjs +93 -0
  182. package/skills/dsh-builtin-browser/SKILL.md +56 -47
  183. package/skills/dsh-builtin-browser/references/batch.md +31 -1
  184. package/skills/dsh-debug-host/SKILL.md +56 -0
  185. package/skills/dsh-debug-self/SKILL.md +50 -0
  186. package/src/bridge-compatibility.ts +39 -0
  187. package/src/browser/handles.ts +89 -0
  188. package/src/browser/runtime.ts +146 -50
  189. package/src/browser/takeover-target.ts +36 -0
  190. package/src/browser/types.ts +19 -7
  191. package/src/browser-electron/authenticated-route.ts +29 -1
  192. package/src/browser-electron/automatic-maintenance.ts +368 -56
  193. package/src/browser-electron/bridge-auto.ts +5 -4
  194. package/src/browser-electron/bridge-connection.ts +20 -2
  195. package/src/browser-electron/bridge-endpoint.ts +3 -3
  196. package/src/browser-electron/bridge-install.ts +40 -4
  197. package/src/browser-electron/bridge-maintenance.ts +42 -14
  198. package/src/browser-electron/bridge-status.ts +94 -23
  199. package/src/browser-electron/desktop-bridge-host.ts +185 -585
  200. package/src/browser-electron/desktop-identity.ts +53 -2
  201. package/src/browser-electron/document-readiness.ts +45 -0
  202. package/src/browser-electron/electron-provision.ts +12 -45
  203. package/src/browser-electron/entry.ts +1299 -882
  204. package/src/browser-electron/ephemeral-profile.ts +1 -1
  205. package/src/browser-electron/extension-browser.ts +223 -0
  206. package/src/browser-electron/extension-setup.ts +67 -0
  207. package/src/browser-electron/guest-cookie-jar.ts +176 -0
  208. package/src/browser-electron/guest-cookie-store.ts +59 -0
  209. package/src/browser-electron/guest-css-runtime.ts +76 -0
  210. package/src/browser-electron/guest-css.ts +68 -0
  211. package/src/browser-electron/guest-route.ts +568 -0
  212. package/src/browser-electron/guest-runtime.ts +357 -0
  213. package/src/browser-electron/guest-storage-runtime.ts +54 -0
  214. package/src/browser-electron/guest-storage.ts +117 -0
  215. package/src/browser-electron/history-store.ts +5 -4
  216. package/src/browser-electron/host-main.ts +156 -11
  217. package/src/browser-electron/host-patch-maintenance.ts +112 -0
  218. package/src/browser-electron/http-body.ts +23 -0
  219. package/src/browser-electron/maintenance-broker.ts +29 -5
  220. package/src/browser-electron/maintenance-lifecycle.ts +14 -9
  221. package/src/browser-electron/maintenance-route.ts +17 -14
  222. package/src/browser-electron/missing-system-browser.ts +2 -2
  223. package/src/browser-electron/open-slot.ts +49 -0
  224. package/src/browser-electron/paths.ts +5 -1
  225. package/src/browser-electron/preferred-browser.ts +120 -0
  226. package/src/browser-electron/preview-route.ts +18 -11
  227. package/src/browser-electron/process-ownership.ts +168 -0
  228. package/src/browser-electron/profile-checkpoint.ts +187 -0
  229. package/src/browser-electron/profile-command.ts +46 -0
  230. package/src/browser-electron/profile-extension.ts +124 -0
  231. package/src/browser-electron/profile-lease.ts +115 -0
  232. package/src/browser-electron/profile-residue.ts +49 -0
  233. package/src/browser-electron/provider.ts +771 -289
  234. package/src/browser-electron/reference-route.ts +608 -0
  235. package/src/browser-electron/remote-host.ts +15 -15
  236. package/src/browser-electron/runtime-route.ts +11 -10
  237. package/src/browser-electron/runtime-status.ts +51 -32
  238. package/src/browser-electron/settings-store.ts +27 -7
  239. package/src/browser-electron/shortcuts-route.ts +112 -0
  240. package/src/browser-electron/sidebar-only-host.ts +118 -19
  241. package/src/browser-electron/startpage-shortcuts.ts +213 -0
  242. package/src/browser-electron/system-browser.ts +309 -338
  243. package/src/browser-electron/windows-system.ts +2 -1
  244. package/src/browser-reference/delivery.ts +384 -0
  245. package/src/browser-reference/entry.ts +631 -0
  246. package/src/browser-reference/export.ts +339 -0
  247. package/src/browser-reference/import.ts +303 -0
  248. package/src/browser-reference/page-script.ts +1537 -0
  249. package/src/browser-reference/service.ts +1315 -0
  250. package/src/browser-reference/store.ts +172 -0
  251. package/src/browser-reference/submission-receipts.ts +122 -0
  252. package/src/browser-reference/text-quote.ts +219 -0
  253. package/src/browser-reference/types.ts +195 -0
  254. package/src/browser-reference/visible.ts +367 -0
  255. package/src/browser-reference/web-sources.ts +103 -0
  256. package/src/client/browser/BrowserController.ts +693 -0
  257. package/src/client/browser/BrowserFrame.ts +78 -0
  258. package/src/client/browser/BrowserNavigation.ts +169 -0
  259. package/src/client/browser/BrowserPage.ts +23 -0
  260. package/src/client/browser/BrowserPersistence.ts +44 -0
  261. package/src/client/browser/IframeImpl.ts +176 -0
  262. package/src/client/browser/child-tabs.ts +14 -0
  263. package/src/client/browser/iframe-reference.ts +67 -0
  264. package/src/client/browser/initialization.ts +262 -0
  265. package/src/client/browser/omnibox.ts +243 -0
  266. package/src/client/browser/store.ts +38 -0
  267. package/src/client/browser/takeover-channel.ts +72 -0
  268. package/src/client/browser/url.ts +46 -0
  269. package/src/client/browser/web-reference-source.ts +52 -0
  270. package/src/client/css-modules.d.ts +4 -0
  271. package/src/client/definition.tsx +30 -0
  272. package/src/client/electron/ElectronWebViewImpl.ts +364 -0
  273. package/src/client/electron/ElectronWebviewPresentation.ts +138 -0
  274. package/src/client/electron/NativeChildPresentation.ts +45 -0
  275. package/src/client/electron/NativeViewPage.ts +251 -0
  276. package/src/client/electron/pages.ts +23 -0
  277. package/src/client/electron/reference-overlay.ts +53 -0
  278. package/src/client/electron/workspace.ts +33 -0
  279. package/src/client/index.ts +293 -0
  280. package/src/client/locales.ts +857 -0
  281. package/src/client/pages.ts +28 -0
  282. package/src/client/reference/client/BrowserRefLink.tsx +142 -0
  283. package/src/client/reference/client/draft-reference.ts +167 -0
  284. package/src/client/reference/client/index.ts +7 -0
  285. package/src/client/reference/client/ordered-submission.ts +108 -0
  286. package/src/client/reference/client/pending-delivery.ts +216 -0
  287. package/src/client/reference/client/plugin.ts +189 -0
  288. package/src/client/reference/client/production/ReferenceCapsules.tsx +345 -0
  289. package/src/client/reference/client/production/ReferenceDock.module.css +193 -0
  290. package/src/client/reference/client/production/ReferenceDock.tsx +6 -0
  291. package/src/client/reference/client/production/ReferenceFeedback.tsx +15 -0
  292. package/src/client/reference/client/production/ReferenceMath.tsx +44 -0
  293. package/src/client/reference/client/production/ReferenceText.tsx +36 -0
  294. package/src/client/reference/client/production/assembly.ts +471 -0
  295. package/src/client/reference/client/production/asset-store.ts +144 -0
  296. package/src/client/reference/client/production/availability.ts +74 -0
  297. package/src/client/reference/client/production/bytes.ts +36 -0
  298. package/src/client/reference/client/production/delivery-client.ts +211 -0
  299. package/src/client/reference/client/production/draft-adapter.ts +508 -0
  300. package/src/client/reference/client/production/http-transport.ts +217 -0
  301. package/src/client/reference/client/production/production-submission.ts +310 -0
  302. package/src/client/reference/client/production/protocol.ts +231 -0
  303. package/src/client/reference/client/production/reference-card.ts +240 -0
  304. package/src/client/reference/client/production/reference-labels.ts +29 -0
  305. package/src/client/reference/client/production/reply-link-bridge.ts +222 -0
  306. package/src/client/reference/client/production/reveal.ts +215 -0
  307. package/src/client/reference/client/production/runtime.ts +1101 -0
  308. package/src/client/reference/client/production/snapshot-adapter.ts +154 -0
  309. package/src/client/reference/client/production/source-marker.ts +63 -0
  310. package/src/client/reference/client/production/source-opener.ts +55 -0
  311. package/src/client/reference/client/production/styles.ts +104 -0
  312. package/src/client/reference/client/production/user-text-rule.ts +58 -0
  313. package/src/client/reference/client/reveal.ts +107 -0
  314. package/src/client/reference/client/scheme.ts +114 -0
  315. package/src/client/reference/client/types.ts +168 -0
  316. package/src/client/reference/css-modules.d.ts +4 -0
  317. package/src/client/reference/index.ts +11 -0
  318. package/src/client/reference-capture-owner.ts +162 -0
  319. package/src/client/reference-intents.ts +36 -0
  320. package/src/client/settings/index.ts +34 -0
  321. package/{client.js → src/client/settings/panel.ts} +2657 -2231
  322. package/src/client/shortcut-open.ts +30 -0
  323. package/src/client/startpage/binding.ts +99 -0
  324. package/src/client/startpage/controller.ts +303 -0
  325. package/src/client/startpage/model.ts +279 -0
  326. package/src/client/view/Browser.module.css +751 -0
  327. package/src/client/view/BrowserBody.tsx +314 -0
  328. package/src/client/view/BrowserPresentation.ts +9 -0
  329. package/src/client/view/BrowserTitle.tsx +17 -0
  330. package/src/client/view/IframePresentation.ts +344 -0
  331. package/src/client/view/StartPage.tsx +597 -0
  332. package/src/client/view/TakeoverBanner.tsx +21 -0
  333. package/src/index.ts +110 -73
  334. package/src/reference-owner.ts +5 -0
  335. package/src/reference-page.ts +9 -0
  336. package/src/reference-source.ts +3 -0
  337. package/src/reference-text.ts +197 -0
  338. package/src/startpage-settings.ts +103 -0
  339. package/src/startpage.ts +71 -0
  340. package/src/takeover-protocol.ts +19 -0
  341. package/src/tool-browser/code-tool.ts +401 -0
  342. package/src/tool-browser/code-worker.ts +639 -0
  343. package/src/tool-browser/index.ts +908 -77
  344. package/src/tool-browser/page-probe.ts +4 -4
  345. package/src/tool-browser/panel-page.ts +124 -0
  346. package/src/tool-browser/screenshot-image.ts +49 -0
  347. package/src/tool-browser/takeover.ts +198 -0
  348. package/src/types/electron-shim.d.ts +20 -0
  349. package/src/types.ts +85 -0
  350. package/src/web-guest.ts +34 -0
  351. package/tools/accept-desktop.mjs +148 -0
  352. package/lib/browser/runtime.d.ts +0 -175
  353. package/lib/browser/types.d.ts +0 -797
  354. package/lib/browser-electron/authenticated-route.d.ts +0 -27
  355. package/lib/browser-electron/automatic-maintenance.d.ts +0 -41
  356. package/lib/browser-electron/bridge-auto.d.ts +0 -93
  357. package/lib/browser-electron/bridge-connection.d.ts +0 -93
  358. package/lib/browser-electron/bridge-endpoint.d.ts +0 -30
  359. package/lib/browser-electron/bridge-install.d.ts +0 -85
  360. package/lib/browser-electron/bridge-maintenance.d.ts +0 -24
  361. package/lib/browser-electron/bridge-status.d.ts +0 -50
  362. package/lib/browser-electron/desktop-bridge-host.d.ts +0 -248
  363. package/lib/browser-electron/desktop-identity.d.ts +0 -58
  364. package/lib/browser-electron/electron-provision.d.ts +0 -92
  365. package/lib/browser-electron/entry.d.ts +0 -64
  366. package/lib/browser-electron/ephemeral-profile.d.ts +0 -37
  367. package/lib/browser-electron/file-write.d.ts +0 -2
  368. package/lib/browser-electron/history-store.d.ts +0 -98
  369. package/lib/browser-electron/host-main.d.ts +0 -26
  370. package/lib/browser-electron/http-body.d.ts +0 -24
  371. package/lib/browser-electron/json-reply.d.ts +0 -33
  372. package/lib/browser-electron/maintenance-broker.d.ts +0 -35
  373. package/lib/browser-electron/maintenance-lifecycle.d.ts +0 -32
  374. package/lib/browser-electron/maintenance-route.d.ts +0 -18
  375. package/lib/browser-electron/missing-system-browser.d.ts +0 -54
  376. package/lib/browser-electron/paths.d.ts +0 -4
  377. package/lib/browser-electron/preview-route.d.ts +0 -89
  378. package/lib/browser-electron/process.d.ts +0 -14
  379. package/lib/browser-electron/provider.d.ts +0 -959
  380. package/lib/browser-electron/remote-host.d.ts +0 -256
  381. package/lib/browser-electron/rpc-input.d.ts +0 -4
  382. package/lib/browser-electron/rpc-ops.d.ts +0 -18
  383. package/lib/browser-electron/runtime-diagnostics.d.ts +0 -12
  384. package/lib/browser-electron/runtime-route.d.ts +0 -55
  385. package/lib/browser-electron/runtime-status.d.ts +0 -116
  386. package/lib/browser-electron/settings-route.d.ts +0 -15
  387. package/lib/browser-electron/settings-store.d.ts +0 -224
  388. package/lib/browser-electron/sidebar-only-host.d.ts +0 -36
  389. package/lib/browser-electron/system-browser.d.ts +0 -367
  390. package/lib/browser-electron/timing.d.ts +0 -8
  391. package/lib/browser-electron/virtual-cursor.d.ts +0 -67
  392. package/lib/browser-electron/windows-system.d.ts +0 -3
  393. package/lib/index.d.ts +0 -30
  394. package/lib/tool-browser/element-fields.d.ts +0 -145
  395. package/lib/tool-browser/index.d.ts +0 -53
  396. package/lib/tool-browser/page-probe.d.ts +0 -86
  397. package/scripts/consumer-install-check.mjs +0 -72
  398. package/scripts/run-tests.mjs +0 -32
  399. package/scripts/verify-host-compat.mjs +0 -195
  400. package/scripts/verify-preview-route.mjs +0 -168
  401. package/scripts/verify-runtime-status.mjs +0 -179
@@ -0,0 +1,872 @@
1
+ /**
2
+ * The dsh_browser_code REPL worker: one child process per task, like Browser Use 1.0's
3
+ * repl/worker.py. It keeps one vm context alive across calls (variables and functions survive),
4
+ * prints the last expression, and reaches the browser only by asking the parent to run a
5
+ * registered dsh_browser_* tool. A runaway cell is stopped by killing this process; the browser
6
+ * and the parent are never blocked by it.
7
+ */
8
+ import { Script, createContext, runInContext } from 'node:vm';
9
+ import { inspect } from 'node:util';
10
+ const send = (message) => { process.send?.(message); };
11
+ const pending = new Map();
12
+ let seq = 0;
13
+ function format(value) {
14
+ if (typeof value === 'string')
15
+ return value;
16
+ if (value === undefined)
17
+ return 'undefined';
18
+ if (typeof value === 'function')
19
+ return inspect(value);
20
+ try {
21
+ return JSON.stringify(value) ?? String(value);
22
+ }
23
+ catch {
24
+ return inspect(value, { depth: 4 });
25
+ }
26
+ }
27
+ const log = (...values) => { send({ type: 'log', text: values.map(format).join(' ') }); };
28
+ async function ask(name, args = {}) {
29
+ const id = ++seq;
30
+ const reply = await new Promise(resolve => { pending.set(id, { resolve }); send({ type: 'call', id, name, args }); });
31
+ if (reply.error !== undefined)
32
+ throw new Error(reply.error);
33
+ return reply;
34
+ }
35
+ const value = async (name, args) => (await ask(name, args)).value;
36
+ /** A failed `tools.*` call, named after the tool, like DSH run_code's ToolCallError. */
37
+ class ToolCallError extends Error {
38
+ toolName;
39
+ constructor(toolName, message) {
40
+ super(message);
41
+ this.name = 'ToolCallError';
42
+ this.toolName = toolName;
43
+ }
44
+ }
45
+ /** DSH run_code-style bindings: `await tools.dsh_browser_snapshot({})` resolves to the tool's JSON value. */
46
+ const tools = new Proxy(Object.create(null), {
47
+ get: (_target, name) => typeof name !== 'string' || name === 'then' ? undefined : async (args = {}) => {
48
+ try {
49
+ return (await ask(name, args)).value;
50
+ }
51
+ catch (error) {
52
+ throw new ToolCallError(name, error instanceof Error ? error.message : String(error));
53
+ }
54
+ },
55
+ });
56
+ const text = async (name, args) => (await ask(name, args)).text ?? '';
57
+ const HTML_TAGS = new Set(['a', 'button', 'input', 'select', 'textarea', 'form', 'div', 'span', 'li', 'ul', 'ol', 'table', 'tr', 'td', 'th', 'tbody', 'thead', 'img', 'label', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'nav', 'main', 'header', 'footer', 'section', 'article', 'aside', 'option', 'summary', 'details', 'iframe', 'body', 'svg', 'dialog']);
58
+ function looksLikeSelector(raw) {
59
+ if (/^[#.[]\S/.test(raw))
60
+ return true;
61
+ const tag = /^[a-z][a-z0-9-]*/.exec(raw)?.[0];
62
+ if (tag === undefined || !HTML_TAGS.has(tag))
63
+ return false;
64
+ const rest = raw.slice(tag.length);
65
+ return rest === '' || /^[#.[:]/.test(rest) || /^\s*>/.test(rest);
66
+ }
67
+ /**
68
+ * Turn a helper target into click/type arguments. A string is visible text unless it reads as a
69
+ * CSS selector or an XPath expression.
70
+ * @param target - text, selector, {css|text|xpath, index}, {by, value} or {x, y}.
71
+ */
72
+ export function targetArgs(target) {
73
+ if (typeof target === 'string') {
74
+ if (target.startsWith('//') || target.startsWith('(//'))
75
+ return { target: { by: 'xpath', value: target } };
76
+ return { target: { by: looksLikeSelector(target) ? 'css' : 'text', value: target } };
77
+ }
78
+ if (target === null || typeof target !== 'object')
79
+ throw new Error(`invalid target ${JSON.stringify(target)}`);
80
+ if (typeof target.x === 'number' && typeof target.y === 'number')
81
+ return { x: target.x, y: target.y };
82
+ const index = target.index !== undefined ? { index: target.index } : {};
83
+ if (target.css !== undefined)
84
+ return { target: { by: 'css', value: target.css, ...index } };
85
+ if (target.text !== undefined)
86
+ return { target: { by: 'text', value: target.text, ...index } };
87
+ if (target.xpath !== undefined)
88
+ return { target: { by: 'xpath', value: target.xpath, ...index } };
89
+ if (target.value !== undefined)
90
+ return { target: { by: target.by ?? 'css', value: target.value, ...index } };
91
+ throw new Error(`invalid target ${JSON.stringify(target)}`);
92
+ }
93
+ const NOT_EXPRESSION = /^(return|if|for|while|do|switch|try|catch|finally|throw|const|let|var|function|async\s+function|class|import|export|break|continue|else|\}|\)|\]|\/\/|\/\*|\*)/;
94
+ const CONTINUES = /[([{,+\-*/%=&|?:.<>!^~]$|=>$/;
95
+ const REGEX_BEFORE = new Set(['', '(', '[', '{', ',', ';', ':', '=', '!', '&', '|', '?', '+', '-', '*', '%', '^', '~', '<', '>', '\n']);
96
+ const REGEX_KEYWORDS = new Set(['return', 'throw', 'case', 'in', 'instanceof', 'typeof', 'void', 'delete', 'new', 'await', 'yield', 'else', 'do', 'of']);
97
+ /**
98
+ * Dedent top-level lines while leaving multiline template literal bodies untouched, so unindented
99
+ * lines inside a template literal do not prevent dedenting the surrounding cell (and trailing
100
+ * parenthesized expressions still get turned into `return (...)` instead of triggering ASI calls).
101
+ */
102
+ function dedentTopLevel(code) {
103
+ const trimmed = code.replace(/\s+$/, '');
104
+ const lines = trimmed.split('\n');
105
+ const inTemplateStart = new Array(lines.length).fill(false);
106
+ let lineIdx = 0;
107
+ let i = 0;
108
+ let prevChar = '';
109
+ let prevWord = '';
110
+ const n = trimmed.length;
111
+ const isIdentStart = (ch) => /[A-Za-z_$]/.test(ch);
112
+ const isIdentPart = (ch) => /[\w$]/.test(ch);
113
+ const advance = (ch) => { if (ch === '\n')
114
+ lineIdx++; };
115
+ const skipRegex = () => {
116
+ i++;
117
+ let inClass = false;
118
+ while (i < n) {
119
+ const ch = trimmed[i];
120
+ if (ch === '\\') {
121
+ advance(trimmed[i + 1] ?? '');
122
+ i += 2;
123
+ continue;
124
+ }
125
+ if (ch === '[')
126
+ inClass = true;
127
+ else if (ch === ']')
128
+ inClass = false;
129
+ else if (ch === '/' && !inClass) {
130
+ i++;
131
+ while (i < n && /[a-z]/i.test(trimmed[i]))
132
+ i++;
133
+ return;
134
+ }
135
+ else if (ch === '\n')
136
+ return;
137
+ i++;
138
+ }
139
+ };
140
+ const skipQuoted = (quote) => {
141
+ i++;
142
+ while (i < n) {
143
+ const ch = trimmed[i];
144
+ if (ch === '\\') {
145
+ advance(trimmed[i + 1] ?? '');
146
+ i += 2;
147
+ continue;
148
+ }
149
+ if (quote === '`' && ch === '$' && trimmed[i + 1] === '{') {
150
+ i += 2;
151
+ let exprDepth = 1;
152
+ let exprPrev = '{';
153
+ let exprWord = '';
154
+ while (i < n && exprDepth > 0) {
155
+ const c = trimmed[i];
156
+ if (c === "'" || c === '"' || c === '`') {
157
+ skipQuoted(c);
158
+ exprPrev = '"';
159
+ exprWord = '';
160
+ continue;
161
+ }
162
+ if (c === '/' && trimmed[i + 1] === '/') {
163
+ while (i < n && trimmed[i] !== '\n')
164
+ i++;
165
+ continue;
166
+ }
167
+ if (c === '/' && trimmed[i + 1] === '*') {
168
+ i += 2;
169
+ while (i < n && !(trimmed[i] === '*' && trimmed[i + 1] === '/')) {
170
+ advance(trimmed[i]);
171
+ i++;
172
+ }
173
+ if (i < n)
174
+ i += 2;
175
+ continue;
176
+ }
177
+ if (c === '/' && (REGEX_BEFORE.has(exprPrev) || REGEX_KEYWORDS.has(exprWord))) {
178
+ skipRegex();
179
+ exprPrev = '/';
180
+ exprWord = '';
181
+ continue;
182
+ }
183
+ if (isIdentStart(c)) {
184
+ const start = i;
185
+ i++;
186
+ while (i < n && isIdentPart(trimmed[i]))
187
+ i++;
188
+ exprPrev = 'a';
189
+ exprWord = trimmed.slice(start, i);
190
+ continue;
191
+ }
192
+ if (c === '{')
193
+ exprDepth++;
194
+ else if (c === '}')
195
+ exprDepth--;
196
+ if (c === '\n') {
197
+ lineIdx++;
198
+ exprPrev = '\n';
199
+ exprWord = '';
200
+ }
201
+ else if (!/\s/.test(c)) {
202
+ exprPrev = c;
203
+ exprWord = '';
204
+ }
205
+ i++;
206
+ }
207
+ continue;
208
+ }
209
+ if (ch === quote) {
210
+ i++;
211
+ return;
212
+ }
213
+ if (ch === '\n') {
214
+ lineIdx++;
215
+ if (quote === '`' && lineIdx < lines.length)
216
+ inTemplateStart[lineIdx] = true;
217
+ }
218
+ i++;
219
+ }
220
+ };
221
+ while (i < n) {
222
+ const ch = trimmed[i];
223
+ if (ch === "'" || ch === '"' || ch === '`') {
224
+ skipQuoted(ch);
225
+ prevChar = '"';
226
+ prevWord = '';
227
+ continue;
228
+ }
229
+ if (ch === '/' && trimmed[i + 1] === '/') {
230
+ while (i < n && trimmed[i] !== '\n')
231
+ i++;
232
+ continue;
233
+ }
234
+ if (ch === '/' && trimmed[i + 1] === '*') {
235
+ i += 2;
236
+ while (i < n && !(trimmed[i] === '*' && trimmed[i + 1] === '/')) {
237
+ advance(trimmed[i]);
238
+ i++;
239
+ }
240
+ if (i < n)
241
+ i += 2;
242
+ continue;
243
+ }
244
+ if (ch === '/' && (REGEX_BEFORE.has(prevChar) || REGEX_KEYWORDS.has(prevWord))) {
245
+ skipRegex();
246
+ prevChar = '/';
247
+ prevWord = '';
248
+ continue;
249
+ }
250
+ if (isIdentStart(ch)) {
251
+ const start = i;
252
+ i++;
253
+ while (i < n && isIdentPart(trimmed[i]))
254
+ i++;
255
+ prevChar = 'a';
256
+ prevWord = trimmed.slice(start, i);
257
+ continue;
258
+ }
259
+ if (ch === '\n') {
260
+ lineIdx++;
261
+ prevChar = '\n';
262
+ prevWord = '';
263
+ }
264
+ else if (!/\s/.test(ch)) {
265
+ prevChar = ch;
266
+ prevWord = '';
267
+ }
268
+ i++;
269
+ }
270
+ const indents = lines
271
+ .filter((line, idx) => !inTemplateStart[idx] && line.trim() !== '')
272
+ .map(line => /^[ \t]*/.exec(line)?.[0].length ?? 0);
273
+ const indent = indents.length > 0 ? Math.min(...indents) : 0;
274
+ const dedented = indent === 0
275
+ ? trimmed
276
+ : lines.map((line, idx) => inTemplateStart[idx] ? line : line.slice(indent)).join('\n');
277
+ return { dedented, inTemplateStart };
278
+ }
279
+ /**
280
+ * Lexically scan top-level declarations (skipping comments, strings, nested template literals,
281
+ * regex literals and nested blocks) so destructuring, multiline template strings and hoisted
282
+ * functions all persist cleanly across REPL cells.
283
+ */
284
+ function scanTopLevel(code) {
285
+ const varNames = new Set();
286
+ const fnNames = new Set();
287
+ const replacements = [];
288
+ let hasTopReturn = false;
289
+ let depth = 0;
290
+ let i = 0;
291
+ let prevChar = '';
292
+ let prevWord = '';
293
+ const n = code.length;
294
+ const isIdentStart = (ch) => /[A-Za-z_$]/.test(ch);
295
+ const isIdentPart = (ch) => /[\w$]/.test(ch);
296
+ const skipSpacesAndComments = (allowNewline = true) => {
297
+ while (i < n) {
298
+ const ch = code[i];
299
+ if (ch === ' ' || ch === '\t' || ch === '\r' || (allowNewline && ch === '\n')) {
300
+ i++;
301
+ continue;
302
+ }
303
+ if (ch === '/' && code[i + 1] === '/') {
304
+ while (i < n && code[i] !== '\n')
305
+ i++;
306
+ continue;
307
+ }
308
+ if (ch === '/' && code[i + 1] === '*') {
309
+ const end = code.indexOf('*/', i + 2);
310
+ i = end === -1 ? n : end + 2;
311
+ continue;
312
+ }
313
+ break;
314
+ }
315
+ };
316
+ const skipQuoted = (quote) => {
317
+ i++;
318
+ while (i < n) {
319
+ const ch = code[i];
320
+ if (ch === '\\') {
321
+ i += 2;
322
+ continue;
323
+ }
324
+ if (quote === '`' && ch === '$' && code[i + 1] === '{') {
325
+ i += 2;
326
+ let exprDepth = 1;
327
+ while (i < n && exprDepth > 0) {
328
+ const c = code[i];
329
+ if (c === "'" || c === '"' || c === '`')
330
+ skipQuoted(c);
331
+ else if (c === '/' && code[i + 1] === '/') {
332
+ while (i < n && code[i] !== '\n')
333
+ i++;
334
+ }
335
+ else if (c === '/' && code[i + 1] === '*') {
336
+ const end = code.indexOf('*/', i + 2);
337
+ i = end === -1 ? n : end + 2;
338
+ }
339
+ else {
340
+ if (c === '{')
341
+ exprDepth++;
342
+ else if (c === '}')
343
+ exprDepth--;
344
+ i++;
345
+ }
346
+ }
347
+ continue;
348
+ }
349
+ if (ch === quote) {
350
+ i++;
351
+ return;
352
+ }
353
+ i++;
354
+ }
355
+ };
356
+ const skipRegex = () => {
357
+ i++;
358
+ let inClass = false;
359
+ while (i < n) {
360
+ const ch = code[i];
361
+ if (ch === '\\') {
362
+ i += 2;
363
+ continue;
364
+ }
365
+ if (ch === '[')
366
+ inClass = true;
367
+ else if (ch === ']')
368
+ inClass = false;
369
+ else if (ch === '/' && !inClass) {
370
+ i++;
371
+ while (i < n && /[a-z]/i.test(code[i]))
372
+ i++;
373
+ return;
374
+ }
375
+ else if (ch === '\n')
376
+ return;
377
+ i++;
378
+ }
379
+ };
380
+ const readIdent = () => {
381
+ if (i >= n || !isIdentStart(code[i]))
382
+ return undefined;
383
+ const start = i;
384
+ i++;
385
+ while (i < n && isIdentPart(code[i]))
386
+ i++;
387
+ return code.slice(start, i);
388
+ };
389
+ const skipBalancedExpr = (stopChars) => {
390
+ let exprDepth = 0;
391
+ let localPrev = '=';
392
+ let localWord = '';
393
+ while (i < n) {
394
+ const ch = code[i];
395
+ if (ch === "'" || ch === '"' || ch === '`') {
396
+ skipQuoted(ch);
397
+ localPrev = '"';
398
+ localWord = '';
399
+ continue;
400
+ }
401
+ if (ch === '/' && code[i + 1] === '/') {
402
+ while (i < n && code[i] !== '\n')
403
+ i++;
404
+ continue;
405
+ }
406
+ if (ch === '/' && code[i + 1] === '*') {
407
+ const end = code.indexOf('*/', i + 2);
408
+ i = end === -1 ? n : end + 2;
409
+ continue;
410
+ }
411
+ if (ch === '/' && (REGEX_BEFORE.has(localPrev) || REGEX_KEYWORDS.has(localWord))) {
412
+ skipRegex();
413
+ localPrev = '/';
414
+ localWord = '';
415
+ continue;
416
+ }
417
+ if (ch === '(' || ch === '[' || ch === '{') {
418
+ exprDepth++;
419
+ i++;
420
+ localPrev = ch;
421
+ localWord = '';
422
+ continue;
423
+ }
424
+ if (ch === ')' || ch === ']' || ch === '}') {
425
+ if (exprDepth === 0 && stopChars.includes(ch))
426
+ return;
427
+ exprDepth = Math.max(0, exprDepth - 1);
428
+ i++;
429
+ localPrev = ch;
430
+ localWord = '';
431
+ continue;
432
+ }
433
+ if (exprDepth === 0 && stopChars.includes(ch))
434
+ return;
435
+ if (isIdentStart(ch)) {
436
+ const w = readIdent() ?? '';
437
+ localPrev = 'a';
438
+ localWord = w;
439
+ continue;
440
+ }
441
+ if (!/\s/.test(ch)) {
442
+ localPrev = ch;
443
+ localWord = '';
444
+ }
445
+ else if (ch === '\n' && exprDepth === 0 && stopChars.includes('\n'))
446
+ return;
447
+ i++;
448
+ }
449
+ };
450
+ const parseBindingPattern = () => {
451
+ skipSpacesAndComments();
452
+ if (i >= n)
453
+ return;
454
+ if (code.startsWith('...', i)) {
455
+ i += 3;
456
+ skipSpacesAndComments();
457
+ const name = readIdent();
458
+ if (name !== undefined)
459
+ varNames.add(name);
460
+ return;
461
+ }
462
+ if (code[i] === '[') {
463
+ i++;
464
+ while (i < n) {
465
+ skipSpacesAndComments();
466
+ if (code[i] === ']') {
467
+ i++;
468
+ return;
469
+ }
470
+ if (code[i] === ',') {
471
+ i++;
472
+ continue;
473
+ }
474
+ parseBindingPattern();
475
+ skipSpacesAndComments();
476
+ if (code[i] === '=') {
477
+ i++;
478
+ skipBalancedExpr(',]');
479
+ }
480
+ skipSpacesAndComments();
481
+ if (code[i] === ',')
482
+ i++;
483
+ else if (code[i] === ']') {
484
+ i++;
485
+ return;
486
+ }
487
+ else
488
+ break;
489
+ }
490
+ return;
491
+ }
492
+ if (code[i] === '{') {
493
+ i++;
494
+ while (i < n) {
495
+ skipSpacesAndComments();
496
+ if (code[i] === '}') {
497
+ i++;
498
+ return;
499
+ }
500
+ if (code.startsWith('...', i)) {
501
+ i += 3;
502
+ skipSpacesAndComments();
503
+ const rest = readIdent();
504
+ if (rest !== undefined)
505
+ varNames.add(rest);
506
+ }
507
+ else {
508
+ let keyIdent;
509
+ if (code[i] === '[') {
510
+ i++;
511
+ skipBalancedExpr(']');
512
+ if (code[i] === ']')
513
+ i++;
514
+ }
515
+ else if (code[i] === "'" || code[i] === '"') {
516
+ skipQuoted(code[i]);
517
+ }
518
+ else if (isIdentStart(code[i])) {
519
+ keyIdent = readIdent();
520
+ }
521
+ else {
522
+ while (i < n && !/[\s:,=}]/.test(code[i]))
523
+ i++;
524
+ }
525
+ skipSpacesAndComments();
526
+ if (code[i] === ':') {
527
+ i++;
528
+ parseBindingPattern();
529
+ }
530
+ else if (keyIdent !== undefined) {
531
+ varNames.add(keyIdent);
532
+ }
533
+ }
534
+ skipSpacesAndComments();
535
+ if (code[i] === '=') {
536
+ i++;
537
+ skipBalancedExpr(',}');
538
+ }
539
+ skipSpacesAndComments();
540
+ if (code[i] === ',')
541
+ i++;
542
+ else if (code[i] === '}') {
543
+ i++;
544
+ return;
545
+ }
546
+ else
547
+ break;
548
+ }
549
+ return;
550
+ }
551
+ const name = readIdent();
552
+ if (name !== undefined)
553
+ varNames.add(name);
554
+ };
555
+ while (i < n) {
556
+ const ch = code[i];
557
+ if (ch === "'" || ch === '"' || ch === '`') {
558
+ skipQuoted(ch);
559
+ prevChar = '"';
560
+ prevWord = '';
561
+ continue;
562
+ }
563
+ if (ch === '/' && code[i + 1] === '/') {
564
+ while (i < n && code[i] !== '\n')
565
+ i++;
566
+ continue;
567
+ }
568
+ if (ch === '/' && code[i + 1] === '*') {
569
+ const end = code.indexOf('*/', i + 2);
570
+ i = end === -1 ? n : end + 2;
571
+ continue;
572
+ }
573
+ if (ch === '/' && (REGEX_BEFORE.has(prevChar) || REGEX_KEYWORDS.has(prevWord))) {
574
+ skipRegex();
575
+ prevChar = '/';
576
+ prevWord = '';
577
+ continue;
578
+ }
579
+ if (ch === '{' || ch === '(' || ch === '[') {
580
+ depth++;
581
+ i++;
582
+ prevChar = ch;
583
+ prevWord = '';
584
+ continue;
585
+ }
586
+ if (ch === '}' || ch === ')' || ch === ']') {
587
+ depth = Math.max(0, depth - 1);
588
+ i++;
589
+ prevChar = ch;
590
+ prevWord = '';
591
+ continue;
592
+ }
593
+ if (depth === 0 && isIdentStart(ch) && prevChar !== '.') {
594
+ const wordStart = i;
595
+ const word = readIdent();
596
+ if (word === 'const' || word === 'let' || word === 'var') {
597
+ if (word !== 'var')
598
+ replacements.push({ start: wordStart, end: i, text: 'var' });
599
+ while (i < n) {
600
+ parseBindingPattern();
601
+ skipSpacesAndComments(false);
602
+ if (code[i] === '=') {
603
+ i++;
604
+ skipSpacesAndComments(true);
605
+ skipBalancedExpr(',;\n');
606
+ }
607
+ skipSpacesAndComments(false);
608
+ if (code[i] === ',') {
609
+ i++;
610
+ continue;
611
+ }
612
+ break;
613
+ }
614
+ prevChar = 'a';
615
+ prevWord = '';
616
+ continue;
617
+ }
618
+ if (word === 'async') {
619
+ const saved = i;
620
+ skipSpacesAndComments(false);
621
+ if (code.startsWith('function', i) && !isIdentPart(code[i + 8] ?? '')) {
622
+ i += 8;
623
+ skipSpacesAndComments();
624
+ if (code[i] === '*') {
625
+ i++;
626
+ skipSpacesAndComments();
627
+ }
628
+ const fn = readIdent();
629
+ if (fn !== undefined)
630
+ fnNames.add(fn);
631
+ prevChar = 'a';
632
+ prevWord = 'function';
633
+ continue;
634
+ }
635
+ i = saved;
636
+ }
637
+ if (word === 'function') {
638
+ skipSpacesAndComments();
639
+ if (code[i] === '*') {
640
+ i++;
641
+ skipSpacesAndComments();
642
+ }
643
+ const fn = readIdent();
644
+ if (fn !== undefined)
645
+ fnNames.add(fn);
646
+ prevChar = 'a';
647
+ prevWord = 'function';
648
+ continue;
649
+ }
650
+ if (word === 'class') {
651
+ skipSpacesAndComments();
652
+ const cls = readIdent();
653
+ if (cls !== undefined) {
654
+ varNames.add(cls);
655
+ replacements.push({ start: wordStart, end: wordStart + 5, text: `var ${cls} = class` });
656
+ }
657
+ prevChar = 'a';
658
+ prevWord = 'class';
659
+ continue;
660
+ }
661
+ if (word === 'return')
662
+ hasTopReturn = true;
663
+ prevChar = 'a';
664
+ prevWord = word;
665
+ continue;
666
+ }
667
+ if (ch === '\n') {
668
+ prevChar = '\n';
669
+ prevWord = '';
670
+ }
671
+ else if (!/\s/.test(ch)) {
672
+ prevChar = ch;
673
+ prevWord = '';
674
+ }
675
+ i++;
676
+ }
677
+ let rewritten = code;
678
+ for (let idx = replacements.length - 1; idx >= 0; idx--) {
679
+ const rep = replacements[idx];
680
+ rewritten = rewritten.slice(0, rep.start) + rep.text + rewritten.slice(rep.end);
681
+ }
682
+ return { rewritten, varNames: [...varNames], fnNames: [...fnNames], hasTopReturn };
683
+ }
684
+ /**
685
+ * Make a cell behave like a REPL cell: top-level declarations land on the context (so they
686
+ * survive the call) and a trailing expression is the cell's value.
687
+ * @param code - the model's cell.
688
+ * @returns an async function body.
689
+ */
690
+ export function prepare(code) {
691
+ const { dedented, inTemplateStart } = dedentTopLevel(code);
692
+ const { rewritten, varNames, fnNames, hasTopReturn } = scanTopLevel(dedented);
693
+ const prelude = fnNames.length === 0 ? '' : fnNames.map(name => `globalThis.${name} = ${name};`).join(' ') + '\n';
694
+ const persist = varNames.length === 0
695
+ ? ''
696
+ : varNames.map(name => `try { if (typeof ${name} !== 'undefined' || !(${JSON.stringify(name)} in globalThis)) globalThis.${name} = ${name}; } catch {}`).join(' ');
697
+ const wrap = (body) => {
698
+ if (persist !== '')
699
+ return `try {\n${prelude}${body}\n} finally {\n${persist}\n}`;
700
+ return `${prelude}${body}`;
701
+ };
702
+ if (hasTopReturn)
703
+ return wrap(rewritten);
704
+ const lines = rewritten.split('\n');
705
+ const last = lines.length - 1;
706
+ const line = lines[last] ?? '';
707
+ const previous = lines.slice(0, last).reverse().find(l => l.trim() !== '')?.trimEnd() ?? '';
708
+ if (inTemplateStart[last] || line === '' || /^\s/.test(line) || NOT_EXPRESSION.test(line) || CONTINUES.test(previous) || /[{,]$/.test(line)) {
709
+ return wrap(rewritten);
710
+ }
711
+ // A one-line cell of several statements returns its last one.
712
+ const split = line.lastIndexOf(';', line.replace(/;\s*$/, '').length - 1);
713
+ const head = split > 0 ? line.slice(0, split + 1) + '\n' : '';
714
+ const tail = (split > 0 ? line.slice(split + 1) : line).trim().replace(/^;\s*/, '').replace(/;\s*$/, '');
715
+ for (const candidate of [`${head}return (${tail})`, `return (${line.replace(/^;\s*/, '').replace(/;\s*$/, '')})`]) {
716
+ const transformed = [...lines.slice(0, last), candidate].join('\n');
717
+ if (tail !== '' && compiles(wrap(transformed)))
718
+ return wrap(transformed);
719
+ }
720
+ return wrap(rewritten);
721
+ }
722
+ function compiles(body) {
723
+ try {
724
+ new Script(`(async () => {\n${body}\n})`);
725
+ return true;
726
+ }
727
+ catch {
728
+ return false;
729
+ }
730
+ }
731
+ /**
732
+ * Wrap a page expression so its value comes back as JSON text that keeps strings strings and
733
+ * numbers exact; a script with a top-level `return` runs as a function body.
734
+ * @param script - an expression, or statements with `return`.
735
+ */
736
+ function pageScript(script) {
737
+ const expression = `return (${script.replace(/;\s*$/, '')})`;
738
+ const body = compiles(expression) ? expression : script;
739
+ return `(async () => { const v = await (async () => {\n${body}\n})(); return JSON.stringify(v === undefined ? null : v) })()`;
740
+ }
741
+ const helpers = {
742
+ // Browser Use 1.0 / browser-harness names.
743
+ goto_url: (url) => text('open', { url }),
744
+ new_tab: (url) => text('open', { url, newTab: true }),
745
+ wait_for_load: async (timeoutMs) => {
746
+ const out = await value('wait', timeoutMs !== undefined ? { timeoutMs } : {});
747
+ if (!out.ready)
748
+ throw new Error(`page did not finish loading: ${out.reason}`);
749
+ return true;
750
+ },
751
+ page_info: () => js('({ url: location.href, title: document.title, w: innerWidth, h: innerHeight, sx: scrollX, sy: scrollY, pw: document.documentElement.scrollWidth, ph: document.documentElement.scrollHeight })'),
752
+ list_tabs: () => value('list_tabs'),
753
+ switch_tab: (tabId) => text('switch_tab', { tabId }),
754
+ close_tab: (tabId) => text('close_tab', { tabId }),
755
+ js: (script) => js(script),
756
+ click_at_xy: (x, y) => text('click', { x, y }),
757
+ type_text: (input) => text('type', { text: input }),
758
+ fill_input: (target, input) => text('set_value', { ...targetArgs(target), value: input }),
759
+ press_key: (key) => text('key', { key }),
760
+ scroll: async (...args) => {
761
+ if (args.length === 1 && typeof args[0] === 'object' && args[0] !== null)
762
+ return text('scroll', args[0]);
763
+ if (args.length >= 3) {
764
+ const [x, y, dy] = [Number(args[0]), Number(args[1]), Number(args[2])];
765
+ if (!Number.isFinite(x) || !Number.isFinite(y) || !Number.isFinite(dy)) {
766
+ throw new Error('scroll(x, y, dy): coordinates and deltaY must be finite numbers');
767
+ }
768
+ return await js(`(() => {
769
+ let el = document.elementFromPoint(${JSON.stringify(x)}, ${JSON.stringify(y)});
770
+ while (el && el !== document.documentElement && el !== document.body) {
771
+ const st = getComputedStyle(el);
772
+ if (/(auto|scroll)/.test(st.overflowY) && el.scrollHeight > el.clientHeight) {
773
+ el.scrollBy({ top: ${JSON.stringify(dy)} });
774
+ return 'Scrolled element.';
775
+ }
776
+ el = el.parentElement;
777
+ }
778
+ window.scrollBy({ top: ${JSON.stringify(dy)} });
779
+ return 'Scrolled window.';
780
+ })()`);
781
+ }
782
+ const deltaY = args.length === 1 ? Number(args[0]) : 600;
783
+ return text('scroll', { deltaY });
784
+ },
785
+ screenshot: (opts = {}) => text('screenshot', opts),
786
+ wait_for_element: async (selector, timeoutMs) => {
787
+ const out = await value('wait', { selector, ...timeoutMs !== undefined ? { timeoutMs } : {} });
788
+ if (!out.ready)
789
+ throw new Error(`wait_for_element: ${out.reason}`);
790
+ return true;
791
+ },
792
+ wait_for_network_idle: async (timeoutMs) => {
793
+ const out = await value('do', { steps: [{ wait_idle: {} }], ...timeoutMs !== undefined ? { timeoutMs } : {} });
794
+ if (!out.ok)
795
+ throw new Error(`wait_for_network_idle: ${out.reason ?? 'not idle'}`);
796
+ return true;
797
+ },
798
+ upload_file: (selector, path) => text('upload', { target: { by: 'css', value: selector }, path }),
799
+ // Semantic helpers over the dsh tools.
800
+ click: (target) => text('click', targetArgs(target)),
801
+ type: async (target, input, opts = {}) => {
802
+ if (typeof input !== 'string' && typeof target !== 'string') {
803
+ throw new Error('type(target, text): text must be a string when target is an object');
804
+ }
805
+ const toolArgs = typeof input === 'string' ? { ...targetArgs(target), text: input } : { text: target };
806
+ if (typeof input === 'object' && input !== null)
807
+ opts = input;
808
+ let out = await text('type', toolArgs);
809
+ if (opts.submit === true)
810
+ out += '\n' + await text('key', { key: 'Enter' });
811
+ return out;
812
+ },
813
+ select: (target, optionText) => text('select', { ...targetArgs(target), optionText }),
814
+ fill: async (fields, opts = {}) => {
815
+ const out = await value('fill', { fields, ...opts.submit === true ? { submit: true } : {} });
816
+ const failed = out.fields.filter(field => !field.ok);
817
+ if (failed.length > 0)
818
+ throw new Error(`fill: ${failed.length} field(s) failed: ${failed.map(field => `${field.target ?? '?'}: ${field.error ?? 'failed'}`).join('; ')}`);
819
+ return out;
820
+ },
821
+ back: () => text('back'),
822
+ forward: () => text('forward'),
823
+ refresh: () => text('refresh'),
824
+ read: async (opts = {}) => {
825
+ const out = await value('content', { format: 'markdown', maxChars: 20000, ...opts });
826
+ return (out.content ?? '') + (out.truncated === true ? '\n[truncated; pass selector or maxChars]' : '');
827
+ },
828
+ snapshot: (opts = {}) => text('snapshot', opts),
829
+ find: (needle, opts = {}) => text('find', { text: needle, ...opts }),
830
+ state: () => text('state'),
831
+ refs: {
832
+ capture: (opts = {}) => text('reference_capture', opts),
833
+ list: (opts = {}) => text('reference_list', opts),
834
+ reveal: (opts) => text('reference_reveal', opts),
835
+ },
836
+ call: (name, args = {}) => value(name, args),
837
+ help: (name) => value('__help', name === undefined ? {} : { name }),
838
+ sleep: (ms) => new Promise(done => setTimeout(done, Math.min(Math.max(Number(ms) || 0, 0), 60_000))),
839
+ print: log,
840
+ log,
841
+ console: { log, info: log, warn: log, error: log, debug: log },
842
+ };
843
+ async function js(script) {
844
+ const out = await value('execute', { script: pageScript(script) });
845
+ if (!out.ok)
846
+ throw new Error(`js: ${out.exception ?? 'page script failed'}`);
847
+ return out.value === undefined ? undefined : JSON.parse(out.value);
848
+ }
849
+ const context = createContext({ ...helpers, open: helpers.goto_url, press: helpers.press_key, tools, ToolCallError });
850
+ process.on('message', (message) => {
851
+ if (message.type === 'reply') {
852
+ const entry = pending.get(message.id);
853
+ pending.delete(message.id);
854
+ entry?.resolve(message);
855
+ return;
856
+ }
857
+ if (message.type === 'run') {
858
+ void (async () => {
859
+ try {
860
+ const result = await runInContext(`(async () => {\n${prepare(message.code)}\n})()`, context, { filename: 'cell.js' });
861
+ if (result !== undefined)
862
+ log(result);
863
+ send({ type: 'done', ok: true });
864
+ }
865
+ catch (error) {
866
+ send({ type: 'done', ok: false, error: error instanceof Error ? `${error.name === 'Error' ? '' : `${error.name}: `}${error.message}` : String(error) });
867
+ }
868
+ })();
869
+ }
870
+ });
871
+ process.on('disconnect', () => process.exit(0));
872
+ send({ type: 'ready' });