shadow-claw 1.23.3 → 1.24.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (286) hide show
  1. package/README.md +42 -5
  2. package/bin/assert-version-bump.mjs +60 -38
  3. package/bin/assert-version-bump.test.mjs +74 -11
  4. package/bin/cli.mjs +166 -0
  5. package/bin/cli.test.mjs +60 -0
  6. package/bin/commands/backup.mjs +70 -0
  7. package/bin/commands/clients.mjs +41 -0
  8. package/bin/commands/peer-id.mjs +76 -0
  9. package/bin/commands/peer-id.test.mjs +104 -0
  10. package/bin/commands/send.mjs +51 -0
  11. package/bin/commands/tasks.mjs +59 -0
  12. package/bin/commands/webrtc-listen.mjs +254 -0
  13. package/bin/prerender-dsd-shell/prerender-dsd-shell.test.mjs +46 -0
  14. package/bin/rolldown.config.test.mjs +401 -8
  15. package/bin/utils/control-client.mjs +243 -0
  16. package/bin/utils/control-client.test.mjs +83 -0
  17. package/bin/utils/ulid.mjs +45 -0
  18. package/bin/utils/webrtc-control-client.mjs +699 -0
  19. package/bin/utils/webrtc-control-client.test.mjs +318 -0
  20. package/dist/electron/main.cjs +1 -1
  21. package/dist/public/AGENTS.md +5 -3
  22. package/dist/public/README.md +42 -5
  23. package/dist/public/agent.worker.js +6 -6
  24. package/dist/public/{app-routes-B0ePT49v.js → app-routes-Bs-riIvr.js} +1 -1
  25. package/dist/public/backup-controller-C_s9in2t.js +1 -0
  26. package/dist/public/components/settings/shadow-claw-control-plane/shadow-claw-control-plane.css +1 -0
  27. package/dist/public/components/settings/shadow-claw-control-plane/shadow-claw-control-plane.html +1 -0
  28. package/dist/public/components/settings/shadow-claw-storage/shadow-claw-storage.html +1 -1
  29. package/dist/public/components/settings/shadow-claw-task-server/shadow-claw-task-server.css +1 -1
  30. package/dist/public/components/settings/shadow-claw-task-server/shadow-claw-task-server.html +1 -1
  31. package/dist/public/components/shadow-claw-files/shadow-claw-files.css +1 -1
  32. package/dist/public/components/shadow-claw-files/shadow-claw-files.html +1 -1
  33. package/dist/public/components/shadow-claw-settings/shadow-claw-settings.html +1 -1
  34. package/dist/public/config-CrHchneq.js +1 -0
  35. package/dist/public/{configurePeerJs-B7kNU8AQ.js → configurePeerJs-CxpZeDJJ.js} +1 -1
  36. package/dist/public/{connections-xd_66YVU.js → connections-DvbgdkaB.js} +1 -1
  37. package/dist/public/{custom-element-security-CZ92anB8.js → custom-element-security-NCqQ5lmC.js} +1 -1
  38. package/dist/public/docs/README.md +3 -1
  39. package/dist/public/docs/architecture/overview.md +12 -12
  40. package/dist/public/docs/architecture/storage.md +5 -2
  41. package/dist/public/docs/decisions/bundled-typescript-architecture.md +1 -1
  42. package/dist/public/docs/decisions/indexeddb-and-opfs-storage.md +6 -3
  43. package/dist/public/docs/decisions/native-web-components-and-signals.md +1 -1
  44. package/dist/public/docs/example/article/index.html +2 -2
  45. package/dist/public/docs/guides/configuring-messaging-channels.md +41 -6
  46. package/dist/public/docs/guides/server-development-configuration.md +24 -0
  47. package/dist/public/docs/publishing/index.html +19 -76
  48. package/dist/public/docs/skill-creator/index.html +2 -2
  49. package/dist/public/docs/subsystems/backup.md +56 -0
  50. package/dist/public/docs/subsystems/cli.md +124 -0
  51. package/dist/public/docs/subsystems/control-plane.md +158 -0
  52. package/dist/public/docs/subsystems/notifications.md +54 -2
  53. package/dist/public/docs/subsystems/reactive-ui.md +1 -0
  54. package/dist/public/downloadGroupFile-By0GR6eV.js +1 -0
  55. package/dist/public/e2e/README.md +5 -1
  56. package/dist/public/{e2e-bridge-DkS5xFGX.js → e2e-bridge-IGWsRl4i.js} +1 -1
  57. package/dist/public/file-viewer-D0eflgt6.js +1 -0
  58. package/dist/public/files/main/~/docs/publishing-to-github-pages.md +17 -82
  59. package/dist/public/getAllTasks-vpBlgGsc.js +1 -0
  60. package/dist/public/getGroupDir-P1h9wl6S.js +1 -0
  61. package/dist/public/{git-BkjrsRXX.js → git-7Vq_SJSp.js} +2 -2
  62. package/dist/public/{iframe-sanitizer-L6Jyu__Z.js → iframe-sanitizer-y2Z1pPwY.js} +1 -1
  63. package/dist/public/index.html +2 -2
  64. package/dist/public/index.js +1 -1
  65. package/dist/public/{initChatSplitResize-BslPXAXS.js → initChatSplitResize-CzV2E7Ul.js} +1 -1
  66. package/dist/public/initControlPlane-C9jpnWev.js +1 -0
  67. package/dist/public/llms.txt +1 -1
  68. package/dist/public/main/index.html +2 -2
  69. package/dist/public/main/memory/index.html +2 -2
  70. package/dist/public/{markdown-6MBrIo-S.js → markdown-BnpKxSxl.js} +1 -1
  71. package/dist/public/mcp-reconnect-9emsqGRe.js +1 -0
  72. package/dist/public/{memoryStorage-CjGseKI1.js → memoryStorage-C0KvLNUp.js} +1 -1
  73. package/dist/public/{orchestrator-B9KmIj8Q.js → orchestrator-3h9xxHUm.js} +676 -676
  74. package/dist/public/orchestrator-nJj59RqD.js +15 -0
  75. package/dist/public/pages/main/~/docs/publishing-to-github-pages.md +17 -82
  76. package/dist/public/{peerjs-Dv8JNm7v.js → peerjs-BP4laMXj.js} +1 -1
  77. package/dist/public/{push-client-Dqq06wbB.js → push-client-CrjEXK3U.js} +1 -1
  78. package/dist/public/service-worker/fetch-proxy.js +1 -1
  79. package/dist/public/service-worker/push-handler.js +1 -1
  80. package/dist/public/service-worker.js +1 -1
  81. package/dist/public/{shadow-claw-Bd1rfuVC.js → shadow-claw-CbwETBWf.js} +2 -2
  82. package/dist/public/{shadow-claw-accounts-DL2l3oM0.js → shadow-claw-accounts-B4sBTQEj.js} +3 -3
  83. package/dist/public/{shadow-claw-channels-BGXDsQ38.js → shadow-claw-channels-BDWG5Qa3.js} +1 -1
  84. package/dist/public/{shadow-claw-chat-NXg6zz_l.js → shadow-claw-chat-Crv0DTCY.js} +6 -6
  85. package/dist/public/shadow-claw-control-plane-CX1IGbh9.js +199 -0
  86. package/dist/public/{shadow-claw-conversations-C8eqGpP9.js → shadow-claw-conversations-_9qAaRXk.js} +1 -1
  87. package/dist/public/shadow-claw-dialog-BrOwAOdk.js +1 -0
  88. package/dist/public/shadow-claw-file-viewer-Blf6yGVD.js +577 -0
  89. package/dist/public/shadow-claw-files-CYySBDCl.js +840 -0
  90. package/dist/public/{shadow-claw-git-C0ntz7aX.js → shadow-claw-git-v8S0JjuR.js} +3 -3
  91. package/dist/public/{shadow-claw-integrations-DCXV2r69.js → shadow-claw-integrations-D7ufpwhX.js} +1 -1
  92. package/dist/public/{shadow-claw-llm-CudgFQsS.js → shadow-claw-llm-GWgoVfhI.js} +2 -2
  93. package/dist/public/{shadow-claw-mcp-remote-B8RkpX28.js → shadow-claw-mcp-remote-kY4LMFNN.js} +1 -1
  94. package/dist/public/{shadow-claw-networking-BLiRsrsK.js → shadow-claw-networking-qHbP95z9.js} +1 -1
  95. package/dist/public/{shadow-claw-notifications-CQhQcjtL.js → shadow-claw-notifications-YyHOK2Fr.js} +1 -1
  96. package/dist/public/{shadow-claw-page-header-NeRD0tKU.js → shadow-claw-page-header-DMKbkv3s.js} +1 -1
  97. package/dist/public/{shadow-claw-pages-DCHnFh6r.js → shadow-claw-pages-DFZHhzFZ.js} +3 -3
  98. package/dist/public/{shadow-claw-settings-VAfjKFQg.js → shadow-claw-settings-DhxrKIfA.js} +10 -3
  99. package/dist/public/shadow-claw-storage-BFynbP0j.js +219 -0
  100. package/dist/public/{shadow-claw-task-server-Dk33N_1G.js → shadow-claw-task-server-3BIeb5oZ.js} +25 -4
  101. package/dist/public/{shadow-claw-tasks-xA1VfmWN.js → shadow-claw-tasks-Caxc9atL.js} +4 -4
  102. package/dist/public/{shadow-claw-terminal-Du2r0ciq.js → shadow-claw-terminal-Beb5qF-U.js} +1 -1
  103. package/dist/public/{shadow-claw-tools-CZj4rq7l.js → shadow-claw-tools-BfJx3A9W.js} +2 -2
  104. package/dist/public/{shadow-claw-webvm-JYUkWICG.js → shadow-claw-webvm-BCS8ykeP.js} +1 -1
  105. package/dist/public/static-main/~/docs/publishing-to-github-pages.md +17 -82
  106. package/dist/public/static-main-manifest.json +1 -1
  107. package/dist/public/{syncWebMcpRegistration-DfpvASnh.js → syncWebMcpRegistration-B_M2fCjH.js} +1 -1
  108. package/dist/server.js +286 -189
  109. package/package.json +2 -1
  110. package/src/__mocks__/jszip.cjs +15 -5
  111. package/src/components/settings/shadow-claw-accounts/shadow-claw-accounts.test.ts +35 -0
  112. package/src/components/settings/shadow-claw-control-plane/shadow-claw-control-plane.css +117 -0
  113. package/src/components/settings/shadow-claw-control-plane/shadow-claw-control-plane.html +81 -0
  114. package/src/components/settings/shadow-claw-control-plane/shadow-claw-control-plane.test.ts +127 -0
  115. package/src/components/settings/shadow-claw-control-plane/shadow-claw-control-plane.ts +292 -0
  116. package/src/components/settings/shadow-claw-git/shadow-claw-git.test.ts +72 -0
  117. package/src/components/settings/shadow-claw-integrations/connection-test-auth.test.ts +20 -30
  118. package/src/components/settings/shadow-claw-integrations/shadow-claw-integrations.test.ts +116 -0
  119. package/src/components/settings/shadow-claw-llm/model-ranking.test.ts +143 -51
  120. package/src/components/settings/shadow-claw-llm/shadow-claw-llm.test.ts +307 -4
  121. package/src/components/settings/shadow-claw-llm/shadow-claw-llm.ts +8 -8
  122. package/src/components/settings/shadow-claw-mcp-remote/shadow-claw-mcp-remote.test.ts +31 -0
  123. package/src/components/settings/shadow-claw-peerjs/shadow-claw-peerjs.test.ts +186 -0
  124. package/src/components/settings/shadow-claw-storage/shadow-claw-storage.html +17 -0
  125. package/src/components/settings/shadow-claw-storage/shadow-claw-storage.test.ts +120 -0
  126. package/src/components/settings/shadow-claw-storage/shadow-claw-storage.ts +56 -0
  127. package/src/components/settings/shadow-claw-task-server/shadow-claw-task-server.css +23 -2
  128. package/src/components/settings/shadow-claw-task-server/shadow-claw-task-server.html +1 -1
  129. package/src/components/settings/shadow-claw-task-server/shadow-claw-task-server.test.ts +19 -0
  130. package/src/components/settings/shadow-claw-task-server/shadow-claw-task-server.ts +2 -1
  131. package/src/components/shadow-claw/utils/initChatSplitResize.test.ts +180 -13
  132. package/src/components/shadow-claw/utils/setupEffects.test.ts +39 -0
  133. package/src/components/shadow-claw-a2ui/catalog/basic/shared.test.ts +117 -0
  134. package/src/components/shadow-claw-a2ui/shadow-claw-a2ui.test.ts +1159 -1
  135. package/src/components/shadow-claw-chat/shadow-claw-chat.test.ts +1523 -19
  136. package/src/components/shadow-claw-chat/utils/escapeHtml.test.ts +13 -0
  137. package/src/components/shadow-claw-chat/utils/getPeerChatDisplayStatus.test.ts +20 -0
  138. package/src/components/shadow-claw-conversations/shadow-claw-conversations.test.ts +494 -0
  139. package/src/components/shadow-claw-dialog/shadow-claw-dialog.ts +13 -2
  140. package/src/components/shadow-claw-file-viewer/shadow-claw-file-viewer.test.ts +378 -0
  141. package/src/components/shadow-claw-file-viewer/shadow-claw-file-viewer.ts +11 -9
  142. package/src/components/shadow-claw-files/shadow-claw-files.css +41 -17
  143. package/src/components/shadow-claw-files/shadow-claw-files.html +35 -0
  144. package/src/components/shadow-claw-files/shadow-claw-files.test.ts +627 -1
  145. package/src/components/shadow-claw-files/shadow-claw-files.ts +170 -6
  146. package/src/components/shadow-claw-pages/utils/resolveWorkspaceLinkPath.test.ts +31 -0
  147. package/src/components/shadow-claw-pages/utils/resolveWorkspaceLinkPath.ts +2 -1
  148. package/src/components/shadow-claw-pages/utils/rewriteWorkspacePreviewHtml.test.ts +15 -0
  149. package/src/components/shadow-claw-pdf-viewer/utils/installGetOrInsertComputedPolyfill.test.ts +65 -0
  150. package/src/components/shadow-claw-settings/shadow-claw-settings.html +7 -0
  151. package/src/components/shadow-claw-settings/shadow-claw-settings.test.ts +221 -0
  152. package/src/components/shadow-claw-settings/shadow-claw-settings.ts +2 -0
  153. package/src/components/shadow-claw-tasks/shadow-claw-tasks.test.ts +407 -0
  154. package/src/components/shadow-claw-terminal/utils/consumeEscapeSequence.test.ts +58 -0
  155. package/src/components/shadow-claw-tools/shadow-claw-tools.test.ts +80 -0
  156. package/src/config/config.test.ts +9 -0
  157. package/src/config/config.ts +15 -0
  158. package/src/core/app-routes.test.ts +56 -0
  159. package/src/core/app-routes.ts +8 -1
  160. package/src/core/backup-controller.test.ts +101 -0
  161. package/src/core/backup-controller.ts +137 -0
  162. package/src/core/control-plane-client.test.ts +408 -0
  163. package/src/core/control-plane-client.ts +425 -0
  164. package/src/core/orchestrator/orchestrator.ts +4 -0
  165. package/src/core/orchestrator/utils/compactContext.test.ts +78 -0
  166. package/src/core/orchestrator/utils/operations/channel.test.ts +254 -9
  167. package/src/core/orchestrator/utils/operations/provider.test.ts +326 -34
  168. package/src/core/orchestrator/utils/operations/vm.test.ts +114 -2
  169. package/src/core/orchestrator/utils/syncProxyConfigToServiceWorker.test.ts +60 -0
  170. package/src/core/utils/initControlPlane.test.ts +276 -0
  171. package/src/core/utils/initControlPlane.ts +437 -0
  172. package/src/core/utils/initializeApp.ts +18 -0
  173. package/src/db/getSession.test.ts +13 -1
  174. package/src/db/openDatabase.test.ts +10 -1
  175. package/src/db/openDatabase.ts +3 -1
  176. package/src/server/app.test.ts +12 -0
  177. package/src/server/app.ts +18 -8
  178. package/src/server/client-registry.test.ts +250 -0
  179. package/src/server/client-registry.ts +477 -0
  180. package/src/server/config.ts +15 -0
  181. package/src/server/control-plane-types.ts +120 -0
  182. package/src/server/control-plane.test.ts +472 -0
  183. package/src/server/control-plane.ts +581 -0
  184. package/src/server/routes/backup.test.ts +263 -0
  185. package/src/server/routes/backup.ts +262 -0
  186. package/src/server/routes/integrations-email.test.ts +532 -0
  187. package/src/server/routes/llamafile.test.ts +172 -48
  188. package/src/server/routes/mesh-llm.test.ts +192 -0
  189. package/src/server/routes/ollama.test.ts +49 -0
  190. package/src/server/server-peer.test.ts +86 -0
  191. package/src/server/server-peer.ts +330 -0
  192. package/src/server/server.ts +35 -0
  193. package/src/server/services/llamafile-manager.test.ts +230 -6
  194. package/src/server/services/transformers-runtime.test.ts +186 -18
  195. package/src/server/webrtc.test.ts +21 -0
  196. package/src/server/webrtc.ts +72 -0
  197. package/src/service-worker/fetch-proxy.test.ts +64 -1
  198. package/src/service-worker/push-handler.ts +39 -0
  199. package/src/shell/vm.test.ts +744 -12
  200. package/src/storage/ensureMainGroupIndex.test.ts +153 -0
  201. package/src/storage/getGroupFile.test.ts +66 -0
  202. package/src/storage/memoryStorage.test.ts +82 -2
  203. package/src/storage/migrateLegacyOpfs.test.ts +327 -0
  204. package/src/storage/migrateLegacyOpfs.ts +117 -0
  205. package/src/storage/writeGroupFileBytes.test.ts +143 -0
  206. package/src/stores/chat-ui.test.ts +20 -0
  207. package/src/stores/orchestrator.test.ts +134 -0
  208. package/src/stores/toast.test.ts +50 -0
  209. package/src/stores/tools.test.ts +147 -10
  210. package/src/subsystems/channels/imessage.test.ts +59 -0
  211. package/src/subsystems/channels/peerjs.test.ts +500 -62
  212. package/src/subsystems/channels/peerjs.ts +71 -4
  213. package/src/subsystems/channels/telegram.test.ts +177 -0
  214. package/src/subsystems/email/catalog.test.ts +1 -0
  215. package/src/subsystems/email/connections.test.ts +39 -1
  216. package/src/subsystems/git/credentials.test.ts +160 -825
  217. package/src/subsystems/git/git.test.ts +463 -0
  218. package/src/subsystems/notifications/__mocks__/node-sqlite.cjs +12 -1
  219. package/src/subsystems/notifications/push-routes.test.ts +33 -0
  220. package/src/subsystems/notifications/push-routes.ts +23 -0
  221. package/src/subsystems/notifications/push-store.ts +1 -1
  222. package/src/subsystems/providers/builtin-ai-tasks.test.ts +74 -0
  223. package/src/subsystems/providers/litert-lm-provider.test.ts +212 -51
  224. package/src/subsystems/providers/model-registry.test.ts +115 -0
  225. package/src/subsystems/providers/providers.test.ts +94 -1
  226. package/src/subsystems/providers/transformers-js-provider.test.ts +279 -0
  227. package/src/subsystems/providers/utils/createModelCacheFetch.test.ts +2 -2
  228. package/src/subsystems/providers/utils/downloadModelToCache.test.ts +2 -2
  229. package/src/subsystems/providers/utils/loadModelStream.test.ts +2 -2
  230. package/src/subsystems/skills/activateSkill.test.ts +101 -0
  231. package/src/subsystems/skills/discoverSkills.test.ts +251 -0
  232. package/src/subsystems/tools/cron.test.ts +64 -0
  233. package/src/subsystems/tools/declarative.test.ts +255 -89
  234. package/src/testing/assets/v86.9pfs/libv86.mjs +63 -0
  235. package/src/testing/assets/v86.ext2/libv86.mjs +3 -0
  236. package/src/testing/assets_missing_v86/assets/v86.9pfs/libv86.mjs +1 -0
  237. package/src/testing/jest-setup.ts +328 -40
  238. package/src/testing/mock-v86.mjs +49 -0
  239. package/src/ui/a2ui/types.ts +4 -3
  240. package/src/ui/a2ui/utils/normaliseComponentsToMap.test.ts +102 -0
  241. package/src/ui/a2ui/utils/resolveChildIds.test.ts +63 -0
  242. package/src/ui/a2ui/utils/resolveDynamicNumber.test.ts +13 -1
  243. package/src/worker/tools/bash/utils/waitForVMReady.test.ts +40 -0
  244. package/src/worker/tools/email/email.test.ts +193 -8
  245. package/src/worker/tools/email/utils/asEmailAttachmentInputs.test.ts +47 -0
  246. package/src/worker/tools/git/git.test.ts +214 -3
  247. package/src/worker/tools/rooms/invite-to-room.test.ts +54 -0
  248. package/src/worker/tools/rooms/leave-room.test.ts +40 -0
  249. package/src/worker/tools/rooms/list-room-members.test.ts +75 -0
  250. package/src/worker/tools/tasks/tasks-utils.test.ts +22 -0
  251. package/src/worker/tools/ui/ask-user.test.ts +50 -0
  252. package/src/worker/tools/ui/clear-chat.test.ts +19 -0
  253. package/src/worker/tools/ui/get-current-time.test.ts +19 -0
  254. package/src/worker/tools/ui/javascript.test.ts +116 -0
  255. package/src/worker/tools/ui/list-components.test.ts +10 -0
  256. package/src/worker/tools/ui/list-tool-profiles.test.ts +42 -0
  257. package/src/worker/tools/ui/manage-tools.test.ts +34 -0
  258. package/src/worker/tools/ui/render-component.test.ts +346 -0
  259. package/src/worker/tools/ui/send-notification.test.ts +40 -0
  260. package/src/worker/tools/ui/utils/getAllowFullInternetAccess.test.ts +31 -0
  261. package/src/worker/tools/ui/utils/parseBooleanConfig.test.ts +25 -0
  262. package/src/worker/tools/workspace/attach-file.test.ts +84 -0
  263. package/src/worker/tools/workspace/copy-file.test.ts +69 -0
  264. package/src/worker/tools/workspace/create-directory.test.ts +40 -0
  265. package/src/worker/tools/workspace/delete-file.test.ts +73 -0
  266. package/src/worker/tools/workspace/diff-files.test.ts +94 -0
  267. package/src/worker/tools/workspace/list-files.test.ts +36 -0
  268. package/src/worker/tools/workspace/move-file.test.ts +66 -0
  269. package/src/worker/tools/workspace/open-file.test.ts +57 -0
  270. package/src/worker/tools/workspace/patch-file.test.ts +81 -0
  271. package/src/worker/tools/workspace/read-file.test.ts +116 -0
  272. package/src/worker/tools/workspace/send-file.test.ts +82 -0
  273. package/src/worker/tools/workspace/update-memory.test.ts +29 -0
  274. package/src/worker/tools/workspace/write-file.test.ts +28 -0
  275. package/src/worker/utils/rate-limit.test.ts +199 -0
  276. package/src/worker/utils/tool-state.test.ts +29 -0
  277. package/src/worker/utils/transformers-js-utils.test.ts +59 -0
  278. package/dist/public/config-AL77QAx4.js +0 -1
  279. package/dist/public/downloadGroupFile-BABv_1mJ.js +0 -1
  280. package/dist/public/file-viewer-CCSABMsE.js +0 -1
  281. package/dist/public/mcp-reconnect-DoicazQd.js +0 -1
  282. package/dist/public/orchestrator-4su7iyWj.js +0 -15
  283. package/dist/public/shadow-claw-dialog-ByyHgxM9.js +0 -1
  284. package/dist/public/shadow-claw-file-viewer-9Qkwc30T.js +0 -577
  285. package/dist/public/shadow-claw-files-aDyq6Hv7.js +0 -781
  286. package/dist/public/shadow-claw-storage-Cf31np81.js +0 -202
@@ -0,0 +1,15 @@
1
+ import{D as e,E as t,F as n,I as r,M as i,O as a,P as o,R as s,T as c,_ as l,g as u,j as d,l as f,o as p,r as m,s as h,t as g,v as _,w as v}from"./config-CrHchneq.js";import{n as y,r as b}from"./txPromise-EBECky1b.js";import{t as x}from"./getConfig-D89uJgo5.js";import{p as S}from"./custom-element-security-NCqQ5lmC.js";import{n as C}from"./toast-60iDlgiH.js";import{At as w,C as T,Ct as E,Dt as ee,En as te,Et as ne,Ft as D,G as O,Gt as k,H as A,Ht as j,I as M,It as N,Jt as P,Kt as F,Lt as I,M as re,Mt as ie,N as L,Nt as R,On as ae,Ot as z,P as oe,Pt as se,St as B,Tn as ce,Tt as le,U as V,Ut as ue,V as de,W as fe,Wt as pe,X as me,Xt as he,Y as ge,Yt as _e,_n as ve,an as ye,at as be,bn as xe,bt as Se,dn as H,dt as Ce,fn as we,ft as Te,gn as Ee,gt as De,hn as Oe,in as ke,j as Ae,jt as je,kt as U,l as Me,ln as Ne,mn as W,on as Pe,ot as Fe,pn as G,qt as Ie,rn as K,t as q,u as Le,un as Re,ut as ze,vn as Be,yn as Ve}from"./orchestrator-3h9xxHUm.js";import{l as He,r as Ue}from"./getGroupDir-P1h9wl6S.js";import{t as J}from"./setConfig-DFMYnYLE.js";import{t as Y}from"./ulid-BY7rQVLN.js";import{a as We,o as Ge,r as Ke,s as qe}from"./syncWebMcpRegistration-B_M2fCjH.js";import{n as Je,r as Ye}from"./crypto-C8c5wMzN.js";import{a as Xe,i as Ze,r as X,t as Qe}from"./peerjs-BP4laMXj.js";async function $e(e){if(!(!e||e.name===`shadowclaw`)){try{if(await x(e,m.DB_MIGRATED_FROM_LEGACY)===`true`)return}catch{}try{let t=await new Promise(e=>{let t=indexedDB.open(l);t.onsuccess=()=>{let n=t.result;if(!n.objectStoreNames||n.objectStoreNames.length===0){n.close(),e(null);return}e(n)},t.onerror=()=>e(null)});if(t){let n=Array.from(t.objectStoreNames).filter(t=>e.objectStoreNames.contains(t));for(let r of n)await new Promise(n=>{try{let i=t.transaction(r,`readonly`).objectStore(r).getAll();i.onsuccess=()=>{let t=i.result||[];if(t.length>0)try{let i=e.transaction(r,`readwrite`),a=i.objectStore(r);for(let e of t)a.put(e);i.oncomplete=()=>n(),i.onerror=()=>n(),i.onabort=()=>n()}catch{n()}else n()},i.onerror=()=>n()}catch{n()}});t.close()}}catch(e){console.warn(`Legacy IndexedDB migration warning:`,e)}finally{try{await J(e,m.DB_MIGRATED_FROM_LEGACY,`true`)}catch{}}}}async function et(e,t,n){for await(let[r,i]of e.entries())if(i.kind===`directory`)await et(i,await t.getDirectoryHandle(r,{create:!0}),[...n,r]);else if(i.kind===`file`){let e=await i.getFile(),a=await t.getFileHandle(r,{create:!0});try{await Ne(a,e)}catch(t){if(!(t instanceof Error?t.message:String(t)).includes(`Writable file streams are not supported`))throw t;await Re([...n,r],e)}}}async function tt(e){if(!e)return;try{if(await x(e,m.OPFS_MIGRATED_FROM_LEGACY)===`true`)return}catch{}let t=Ue();if(t!==`shadowclaw`&&!(typeof navigator>`u`||!navigator.storage||typeof navigator.storage.getDirectory!=`function`))try{let n=await navigator.storage.getDirectory(),r;try{r=await n.getDirectoryHandle(_)}catch{try{await J(e,m.OPFS_MIGRATED_FROM_LEGACY,`true`)}catch{}return}let i=await n.getDirectoryHandle(t,{create:!0});await et(r,i,[t])}catch(e){console.warn(`Legacy OPFS migration warning:`,e)}finally{try{await J(e,m.OPFS_MIGRATED_FROM_LEGACY,`true`)}catch{}}}function nt(){return new Promise(async(e,t)=>{let n,r=d(),i=indexedDB.open(r,2);i.onupgradeneeded=()=>{let e=i.result;if(!e.objectStoreNames.contains(`messages`)){let t=e.createObjectStore(`messages`,{keyPath:`id`});t.createIndex(`by-group-time`,[`groupId`,`timestamp`]),t.createIndex(`by-group`,`groupId`)}if(e.objectStoreNames.contains(`sessions`)||e.createObjectStore(`sessions`,{keyPath:`groupId`}),!e.objectStoreNames.contains(`tasks`)){let t=e.createObjectStore(`tasks`,{keyPath:`id`});t.createIndex(`by-group`,`groupId`),t.createIndex(`by-enabled`,`enabled`)}e.objectStoreNames.contains(`config`)||e.createObjectStore(`config`,{keyPath:`key`}),e.objectStoreNames.contains(`pendingShares`)||e.createObjectStore(`pendingShares`,{keyPath:`id`}).createIndex(`by-createdAt`,`createdAt`)},i.onsuccess=async()=>{n=i.result,b(n);try{await $e(n),await tt(n)}catch(e){console.warn(`Error running legacy migration:`,e)}e(n)},i.onerror=()=>{t(Error(`Failed to open IndexedDB: ${i.error?.message}`))}})}var rt=class{activeGroupId;displayCallback;messageCallback;type;typingCallback;constructor(){this.type=`browser`,this.messageCallback=null,this.typingCallback=null,this.displayCallback=null,this.activeGroupId=p}getActiveGroup(){return this.activeGroupId}onDisplay(e){this.displayCallback=e}onMessage(e){this.messageCallback=e}onTyping(e){this.typingCallback=e}setActiveGroup(e){this.activeGroupId=e}setTyping(e,t){this.typingCallback?.(e,t)}start(){}stop(){}submit(e,t,n=[],r,i,a){let o=t||this.activeGroupId,s={id:Y(),groupId:o,sender:`You`,content:e,timestamp:Date.now(),channel:`browser`,attachments:n,a2uiAction:r,freshContext:i,subagent:a};this.messageCallback?.(s)}async send(e,t,n){this.displayCallback?.(e,t,!0)}},it=class{_entries;constructor(){this._entries=[]}find(e){return this.resolve(e)?.channel??null}get(e){return this._entries.find(t=>t.prefix===e)?.channel}getBadge(e){return this.resolve(e)?.badge??``}getChannelType(e){return this.resolve(e)?.channel.type??null}onMessage(e){for(let t of this._entries)t.channel.onMessage(e)}onTyping(e){for(let t of this._entries)t.channel.onTyping&&t.channel.onTyping(e)}prefixes(){return this._entries.map(e=>e.prefix)}register(e,t,n){let r=typeof n==`string`?{badge:n}:n??{};this._entries.push({prefix:e,channel:t,badge:r.badge??e.replace(/:$/,``),autoTrigger:r.autoTrigger??!1}),this._entries.sort((e,t)=>t.prefix.length-e.prefix.length)}resolve(e){for(let t of this._entries)if(e.startsWith(t.prefix))return t;return null}shouldAutoTrigger(e){return this.resolve(e)?.autoTrigger??!1}startAll(){for(let e of this._entries)e.channel.start()}stopAll(){for(let e of this._entries)e.channel.stop()}},at=class{abortController=null;apiKey=``;apiMode=`unknown`;cursor=null;fileReader=null;messageCallback=null;reconnectDelayMs=1e3;registeredChatIds=new Set;running=!1;seenMessageIds=new Map;serverUrl=``;type=`imessage`;configure(e,t,n){this.serverUrl=ot(e),this.apiKey=t.trim(),this.registeredChatIds=new Set(n.map(e=>e.trim()).filter(Boolean)),this.cursor=null,this.apiMode=`unknown`}handleIncomingMessage(e){this.pruneSeenMessageIds();let t=String(e.chatId||e.conversationId||``).trim();if(!t||this.registeredChatIds.size>0&&!this.registeredChatIds.has(t))return;let n=`${e.text||e.body||``}`.trim();if(!n)return;let r=`${e.id||e.guid||`${t}:${e.timestamp||e.createdAt||n}`}`;if(this.seenMessageIds.has(r))return;this.seenMessageIds.set(r,Date.now());let i={id:r,groupId:`im:${t}`,sender:`${e.sender||e.handle||e.from||`iMessage`}`,content:n,timestamp:st(e.timestamp||e.createdAt),channel:`imessage`};this.messageCallback?.(i)}handlePollResponse(e){e.nextCursor!==void 0&&e.nextCursor!==null?this.cursor=String(e.nextCursor):e.cursor!==void 0&&e.cursor!==null&&(this.cursor=String(e.cursor));let t=Array.isArray(e.messages)?e.messages:[];for(let e of t)this.handleIncomingMessage(e)}isConfigured(){return this.serverUrl.length>0}onMessage(e){this.messageCallback=e}pruneSeenMessageIds(){let e=Date.now();for(let[t,n]of this.seenMessageIds.entries())e-n>6e4&&this.seenMessageIds.delete(t)}setTyping(e,t){if(!t)return;let n=e.replace(/^im:/,``);this.requestJson(`/messages/typing`,{method:`POST`,body:JSON.stringify({chatId:n,typing:!0})}).catch(()=>{})}start(){!this.isConfigured()||this.running||(this.running=!0,this.abortController=new AbortController,this.poll())}stop(){this.running=!1,this.abortController?.abort(),this.abortController=null}async ensureApiMode(){if(this.apiMode!==`unknown`)return this.apiMode;try{await this.requestJson(`/v1/info`,{method:`GET`}),this.apiMode=`beeper-desktop`}catch{this.apiMode=`legacy`}return this.apiMode}async poll(){for(;this.running&&this.abortController&&!this.abortController.signal.aborted;)try{let e=new URLSearchParams({timeout:`25`});this.cursor&&e.set(`cursor`,this.cursor);let t=await this.requestJson(`/messages?${e.toString()}`,{method:`GET`},u+5e3,this.abortController.signal);this.reconnectDelayMs=1e3,this.handlePollResponse(t)}catch(e){if(e instanceof Error&&e.name===`AbortError`)break;console.error(`iMessage bridge poll error:`,e),await ct(this.reconnectDelayMs),this.reconnectDelayMs=Math.min(this.reconnectDelayMs*2,3e4)}}async requestJson(e,t,n=u,r){let i=new AbortController,a=setTimeout(()=>i.abort(),n),o=()=>i.abort();r?.addEventListener(`abort`,o,{once:!0});try{let n=new Headers(t.headers);n.set(`Accept`,`application/json`),!n.has(`Content-Type`)&&t.body&&!(t.body instanceof FormData)&&n.set(`Content-Type`,`application/json`),this.apiKey&&(n.set(`Authorization`,`Bearer ${this.apiKey}`),n.set(`X-API-Key`,this.apiKey));let r=await fetch(`${this.serverUrl}${e}`,{...t,headers:n,signal:i.signal});if(!r.ok){let e=await r.text();throw Error(`iMessage bridge request failed: ${r.status} ${e}`)}return r.status===204?{messages:[]}:await r.json()}finally{clearTimeout(a),r?.removeEventListener(`abort`,o)}}async send(e,t,n){let r=e.replace(/^im:/,``),{remainingText:i,attachments:a}=lt(t);for(let t of a){if(!this.fileReader){await this.sendText(r,`[${t.alt}] (file: ${t.path})`);continue}try{let n=await this.fileReader(e,t.path);if(!n||n.size===0)throw Error(`Attachment file is empty or unavailable.`);await this.sendAttachment(r,n,t)}catch(e){console.warn(`iMessage attachment send failed for ${t.path}:`,e),await this.sendText(r,`[${t.alt}] (file: ${t.path})`)}}let o=i.trim();o&&await this.sendText(r,o)}async sendAttachment(e,t,n){if(await this.ensureApiMode()===`beeper-desktop`){await this.sendAttachmentViaBeeperDesktop(e,t,n);return}await this.sendAttachmentViaLegacyBridge(e,t,n)}async sendAttachmentViaBeeperDesktop(e,t,n){let r=n.path.split(`/`).pop()||`attachment.bin`,i=new FormData;i.append(`file`,t,r),i.append(`fileName`,r),t.type&&i.append(`mimeType`,t.type);let a=`${(await this.requestJson(`/v1/assets/upload`,{method:`POST`,body:i})).uploadID||``}`.trim();if(!a)throw Error(`Beeper asset upload failed: missing uploadID`);await this.requestJson(`/v1/chats/${encodeURIComponent(e)}/messages`,{method:`POST`,body:JSON.stringify({text:n.alt||void 0,attachment:{uploadID:a,fileName:r,mimeType:t.type||void 0}})})}async sendAttachmentViaLegacyBridge(e,t,n){let r=n.path.split(`/`).pop()||`attachment.bin`,i=new FormData;i.append(`chatId`,e),n.alt&&i.append(`text`,n.alt),i.append(`file`,t,r),await this.requestJson(`/messages/send`,{method:`POST`,body:i})}async sendText(e,t){if(await this.ensureApiMode()===`beeper-desktop`){await this.requestJson(`/v1/chats/${encodeURIComponent(e)}/messages`,{method:`POST`,body:JSON.stringify({text:t})});return}await this.requestJson(`/messages/send`,{method:`POST`,body:JSON.stringify({chatId:e,text:t})})}};function ot(e){return e.trim().replace(/\/+$/,``)}function st(e){if(typeof e==`number`&&Number.isFinite(e))return e>0xe8d4a51000?e:e*1e3;if(typeof e==`string`){let t=Number(e);if(Number.isFinite(t))return st(t);let n=Date.parse(e);if(Number.isFinite(n))return n}return Date.now()}function ct(e){return new Promise(t=>setTimeout(t,e))}function lt(e){let t=[],n=/!?\[([^\]]*)\]\(([^)]+)\)/g,r=null,i=e;for(;(r=n.exec(e))!==null;){let e=r[1]||``,n=ut((r[2]||``).trim());n&&(t.push({alt:e,path:n}),i=i.replace(r[0],``))}return{remainingText:i,attachments:t}}function ut(e){if(!e)return null;let t=e.trim();if(/^[a-zA-Z][a-zA-Z\d+.-]*:/.test(t)||t.startsWith(`//`))return null;let n=t.replace(/\\/g,`/`).replace(/^\/+/,``).replace(/^\.\//,``);if(!n)return null;let r=n.split(`/`).filter(Boolean);return r.some(e=>e===`..`)?null:r.join(`/`)}var dt=class{messageCallback=null;running=!1;type=`room`;typingCallback=null;_manager=null;deliverInbound(e){this.messageCallback?.(e)}onMessage(e){this.messageCallback=e}onTyping(e){this.typingCallback=e}setManager(e){this._manager=e}setTyping(e,t){}start(){this.running=!0}stop(){this.running=!1}async send(e,t,n){if(!this._manager)return;let r=_e(e);this._manager.broadcast(r,t,n)}};const ft=1e3;var pt=class{abortController=null;fileReader=null;messageCallback=null;offset=0;pollRetryDelayMs=ft;pollSessionId=0;registeredChatIds=new Set;running=!1;token=``;type=`telegram`;useProxy=!1;buildFileUrl(e){let t=e.split(`/`).filter(Boolean).map(e=>encodeURIComponent(e)).join(`/`);return`${this.useProxy?`/telegram/file/bot`:`https://api.telegram.org/file/bot`}${this.token}/${t}`}buildMethodUrl(t){let n=t.replace(/^\/+/,``);return`${this.useProxy?e:c}${this.token}/${n}`}configure(e,t,n=!1){this.token=e.trim(),this.registeredChatIds=new Set(t.map(e=>e.trim()).filter(Boolean)),this.useProxy=!!n}isConfigured(){return this.token.length>0}nextPollRetryDelay(){let e=this.pollRetryDelayMs;return this.pollRetryDelayMs=Math.min(this.pollRetryDelayMs*2,6e4),e}onMessage(e){this.messageCallback=e}registerChatId(e){let t=e.trim();t&&this.registeredChatIds.add(t)}resetPollBackoff(){this.pollRetryDelayMs=ft}setTyping(e,t){if(!t)return;let n=e.replace(/^tg:/,``);this.apiCall(`sendChatAction`,{chat_id:n,action:`typing`}).catch(()=>{})}start(){if(!this.token||this.running)return;this.running=!0,this.resetPollBackoff();let e=++this.pollSessionId,t=new AbortController;this.abortController=t,this.poll(e,t)}stop(){this.running=!1,this.resetPollBackoff(),this.pollSessionId+=1,this.abortController?.abort(),this.abortController=null}async apiCall(e,t){let n=await fetch(this.buildMethodUrl(e),{method:`POST`,headers:{"Content-Type":`application/json`},body:JSON.stringify(t)});if(!n.ok){let t=await n.text();throw Error(`Telegram API ${e} failed: ${n.status} ${t}`)}return await n.json()}async extractAttachments(e){let t=[],n=Array.isArray(e.photo)?e.photo.at(-1):null;n?.file_id&&t.push({id:n.file_id,fileName:`photo-${e.message_id}.png`,mimeType:`image/png`,previewDisposition:`inline`}),e.document?.file_id&&t.push({id:e.document.file_id,fileName:e.document.file_name||`document-${e.message_id}`,mimeType:e.document.mime_type}),e.video?.file_id&&t.push({id:e.video.file_id,fileName:e.video.file_name||`video-${e.message_id}.mp4`,mimeType:e.video.mime_type||`video/mp4`}),e.audio?.file_id&&t.push({id:e.audio.file_id,fileName:e.audio.file_name||`audio-${e.message_id}.mp3`,mimeType:e.audio.mime_type||`audio/mpeg`}),e.voice?.file_id&&t.push({id:e.voice.file_id,fileName:`voice-${e.message_id}.ogg`,mimeType:e.voice.mime_type||`audio/ogg`}),e.animation?.file_id&&t.push({id:e.animation.file_id,fileName:e.animation.file_name||`animation-${e.message_id}.mp4`,mimeType:e.animation.mime_type||`video/mp4`}),e.sticker?.file_id&&t.push({id:e.sticker.file_id,fileName:`sticker-${e.message_id}.${gt(e.sticker)}`,mimeType:e.sticker.is_animated?`application/x-tgsticker`:e.sticker.is_video?`video/webm`:`image/webp`});let r=[];for(let e of t){let t=await this.fetchTelegramFileInfo(e.id);r.push({id:e.id,fileName:e.fileName,mimeType:e.mimeType,size:t.file_size,previewDisposition:e.previewDisposition,source:{kind:`remote-url`,url:this.buildFileUrl(t.file_path)}})}return r}async fetchTelegramFileInfo(e){let t=await this.apiCall(`getFile`,{file_id:e});if(!t.ok||!t.result?.file_path)throw Error(`Telegram getFile failed for attachment ${e}`);return t.result}async handleUpdate(e){let t=e.message;if(!t)return;let n=String(t.chat.id);if(t.text===`/chatid`){this.apiCall(`sendMessage`,{chat_id:n,text:`Chat ID: ${n}\nRegister this ID in ShadowClaw settings.`}).catch(console.error);return}if(t.text===`/ping`){this.apiCall(`sendMessage`,{chat_id:n,text:`Pong! ShadowClaw is running.`}).catch(console.error);return}if(!this.registeredChatIds.has(n))return;let r=await this.extractAttachments(t),i=`${t.text||t.caption||``}`.trim()||(t.photo?.length?`[Photo]`:null)||(t.voice?`[Voice message]`:null)||(t.video?`[Video]`:null)||(t.audio?`[Audio]`:null)||(t.animation?`[Animation]`:null)||(t.document?`[Document: ${t.document.file_name||`unnamed`}]`:null)||(t.sticker?`[Sticker: ${t.sticker.emoji||``}]`:null)||(t.location?`[Location: ${t.location.latitude}, ${t.location.longitude}]`:null)||(t.contact?`[Contact: ${t.contact.first_name}]`:null)||`[Unsupported message type]`,a=t.from?.first_name||t.from?.username||String(t.from?.id||`Unknown`),o={id:String(t.message_id),groupId:`tg:${n}`,sender:a,content:i,timestamp:t.date*1e3,channel:`telegram`,attachments:r};this.messageCallback?.(o)}async poll(e,t){for(;this.running&&this.pollSessionId===e&&!t.signal.aborted;)try{let e=await fetch(this.buildMethodUrl(`getUpdates?offset=${this.offset}&timeout=30`),{signal:t.signal});if(!e.ok){console.error(`Telegram poll error: HTTP ${e.status}`),await this.waitForRetry(this.nextPollRetryDelay());continue}let n=await e.json();if(!n.ok||!Array.isArray(n.result)){await this.waitForRetry(this.nextPollRetryDelay());continue}this.resetPollBackoff();for(let e of n.result)this.offset=e.update_id+1,await this.handleUpdate(e)}catch(e){if(e instanceof Error&&e.name===`AbortError`)break;console.error(`Telegram poll error:`,e),await this.waitForRetry(this.nextPollRetryDelay())}}async send(e,n,r){let i=e.replace(/^tg:/,``),{remainingText:a,attachments:o}=_t(n);for(let t of o){if(this.fileReader)try{let n=await this.fileReader(e,t.path);if(n&&n.size>0){let e=t.path.toLowerCase();e.endsWith(`.png`)||e.endsWith(`.jpg`)||e.endsWith(`.jpeg`)||e.endsWith(`.gif`)||e.endsWith(`.webp`)?await this.sendPhoto(i,n,t.alt,t.path):await this.sendDocument(i,n,t.alt,t.path);continue}}catch(e){console.warn(`TelegramChannel: failed to read file ${t.path}:`,e)}await this.apiCall(`sendMessage`,{chat_id:i,text:`[${t.alt}] (file: ${t.path})`})}let s=a.trim();if(s){let e=mt(s,t);for(let t of e)await this.apiCall(`sendMessage`,{chat_id:i,text:t})}}async sendDocument(e,t,n,r){let i=r.split(`/`).pop()||`document.file`,a=new FormData;a.append(`chat_id`,e),a.append(`document`,t,i),n&&a.append(`caption`,n);let o=await fetch(this.buildMethodUrl(`sendDocument`),{method:`POST`,body:a});if(!o.ok){let e=await o.text();throw Error(`Telegram sendDocument failed: ${o.status} ${e}`)}}async sendPhoto(e,t,n,r){let i=r.split(`/`).pop()||`image.png`,a=new FormData;a.append(`chat_id`,e),a.append(`photo`,t,i),n&&a.append(`caption`,n);let o=await fetch(this.buildMethodUrl(`sendPhoto`),{method:`POST`,body:a});if(!o.ok){let e=await o.text();throw Error(`Telegram sendPhoto failed: ${o.status} ${e}`)}}async waitForRetry(e){await ht(e)}};function mt(e,t){if(e.length<=t)return[e];let n=[],r=e;for(;r.length>0;){if(r.length<=t){n.push(r);break}let e=t,i=r.lastIndexOf(`
2
+ `,t);i>t*.5&&(e=i),n.push(r.slice(0,e)),r=r.slice(e)}return n}function ht(e){return new Promise(t=>setTimeout(t,e))}function gt(e){return e?.is_animated?`tgs`:e?.is_video?`webm`:`webp`}function _t(e){let t=[],n=/!?\[([^\]]*)\]\(([^)]+)\)/g,r,i=e;for(;(r=n.exec(e))!==null;){let e=r[1],n=vt((r[2]||``).trim());n&&(t.push({alt:e,path:n}),i=i.replace(r[0],``))}return{remainingText:i,attachments:t}}function vt(e){if(!e)return null;let t=e.trim();if(/^[a-zA-Z][a-zA-Z\d+.-]*:/.test(t)||t.startsWith(`//`))return null;let n=t.replace(/\\/g,`/`).replace(/^\/+/,``).replace(/^\.\//,``);if(!n)return null;let r=n.split(`/`).filter(Boolean);return r.some(e=>e===`..`)?null:r.join(`/`)}function yt(e,t){let n=JSON.stringify(e.dataModel??{},null,2),r=t?`${t}`:`The user`;return`[A2UI ACTION]\nsurfaceId: "${e.surfaceId}"\nactionId: "${e.actionId}"\n`+(t?`firedBy: "${t}"\n`:``)+`dataModel:\n${n}\n\nInstructions: ${r} triggered the "${e.actionId}" action on surface "${e.surfaceId}". The dataModel above contains the current form values. Use the appropriate tool(s) to fulfill the action based on the actionId and dataModel values. After you have a result, call render_component with action "updateDataModel" on surfaceId "${e.surfaceId}" to update the surface (e.g. set path "/result" to the output value). Do NOT respond with plain text only — always update the surface via render_component.`}var bt=class{static formatMessagesXml(e){let t=e=>e.replace(/&/g,`&amp;`).replace(/</g,`&lt;`).replace(/>/g,`&gt;`).replace(/"/g,`&quot;`);return`<messages>\n${e.map(e=>`<message sender="${t(e.sender)}" time="${new Date(e.timestamp).toISOString()}">${t(e.content)}</message>`).join(`
3
+ `)}\n</messages>`}static formatOutbound(e){return e.replace(/<internal>[\s\S]*?<\/internal>/g,``).trim()}registry;constructor(e){this.registry=e}findChannel(e){return this.registry.find(e)}setTyping(e,t){this.findChannel(e)?.setTyping(e,t)}async send(e,t,n){let r=this.findChannel(e);if(!r){console.warn(`No channel for groupId: ${e}`);return}await r.send(e,t,n)}};const xt={"litert-community/gemma-4-E2B-it-litert-lm":`https://huggingface.co/litert-community/gemma-4-E2B-it-litert-lm/resolve/main/gemma-4-E2B-it-web.litertlm`,"litert-community/gemma-4-E4B-it-litert-lm":`https://huggingface.co/litert-community/gemma-4-E4B-it-litert-lm/resolve/main/gemma-4-E4B-it-web.litertlm`},St=Object.keys(xt);function Ct(){return typeof navigator<`u`&&`gpu`in navigator&&typeof WebAssembly<`u`&&`Suspending`in WebAssembly}let wt=null,Tt=null,Et=null,Dt=null;const Ot=k;async function kt(e,t,n){return pe(e,t,n,{cacheName:`shadow-claw-litertlm-models`,chunkSize:Ot})}async function At(e,t,n){if(Tt===e&&wt)return wt;if(Et&&Dt===e)return Et;Dt=e,Et=(async()=>{if(wt){try{await wt.delete?.()}catch{}wt=null,Tt=null}let r=xt[e];if(!r)throw Error(`LiteRT-LM: Model '${e} is not supported. Supported models: ${St.join(`, `)}`);let i=await import(`./dist-DUy1CvmT.js`).catch(()=>{throw Error(`LiteRT-LM: The @litert-lm/core package is not installed. Run: npm install @litert-lm/core`)}),a=i.Engine??i.default?.Engine;if(typeof a?.create!=`function`)throw Error(`LiteRT-LM: Could not locate Engine.create in @litert-lm/core. Ensure you have the latest version installed.`);let o=await kt(r,t??(()=>{}),n),s=await a.create({model:o,mainExecutorSettings:{maxNumTokens:8192}});return wt=s,Tt=e,s})();try{return await Et}finally{Et=null,Dt=null}}function jt(e){if(!e)return null;let t=e.match(/<\|tool_call>call:([A-Za-z0-9_]+)(\{[\s\S]*?)(?:<tool_call\|>|$)/);if(!t)return null;let n=t[1],r=t[2];r=r.replace(/<\|"\|>/g,`"`);function i(e){return e.replace(/([{,]\s*)([A-Za-z_$][A-Za-z0-9_$]*)(\s*:)/g,`$1"$2"$3`)}let a={},o=[r,i(r)];for(let e of o)try{let t=JSON.parse(e);if(t&&typeof t==`object`&&!Array.isArray(t)){a=t;break}}catch{}if(Object.keys(a).length===0){let e=r.indexOf(`{`),t=r.lastIndexOf(`}`);if(e>=0&&t>e){let n=r.slice(e,t+1);for(let e of[n,i(n)])try{let t=JSON.parse(e);if(t&&typeof t==`object`&&!Array.isArray(t)){a=t;break}}catch{}}}return{name:n,input:a}}function Mt(e){let t=G(String(e||``),`litert_lm`).trim();if(!t)return null;let n=e=>{try{let t=JSON.parse(e);if(t&&typeof t==`object`&&(t.type===`tool_use`||t.type===`response`))return t.tools_calls&&!t.tool_calls&&(t.tool_calls=t.tools_calls),{...t,format:`json`}}catch{}return null},r=n(t);if(r)return r;let i=t.indexOf(`{`),a=t.lastIndexOf(`}`);if(i>=0&&a>i&&(r=n(t.slice(i,a+1)),r))return r;if(t.includes(`"tool_use"`)){let e=/"name"\s*:\s*"([^"]+)"/.exec(t);if(e){let n={},r=/"input"\s*:\s*(\{.*?\})\s*(?:]|\}|"|$)/s.exec(t);if(r)try{n=JSON.parse(r[1])}catch{}return{type:`tool_use`,format:`json`,tool_calls:[{id:`litert_${Date.now()}`,name:e[1],input:n}]}}}let o=jt(t);return o?{type:`tool_use`,format:`model_specific`,tool_calls:[{id:`litert_${Date.now()}`,name:o.name,input:o.input}]}:null}function Nt(e,t,n){let r=[],i=e||``;if(n.length>0){let e=[``,`TOOL CALLING INSTRUCTIONS:`,`Return ONLY valid JSON (no markdown fences).`,`To call a tool: {"type":"tool_use","tool_calls":[{"name":"<tool>","input":{...}}]}`,`To reply: {"type":"response","response":"<your answer>"}`,`IMPORTANT: When you need to use a tool, output ONLY the JSON above — do NOT describe the call as text.`,``,`Available tools:`,n.map(e=>{let t=Object.keys(e.input_schema.properties||{}).join(`, `),n=e.description.split(`. `)[0];return`${e.name}(${t}): ${n}`}).join(`
4
+ `)].join(`
5
+ `);i=i?i+e:e}i&&r.push({role:`system`,content:i});let a=[];for(let e of t)if(e.role===`user`){let t=Array.isArray(e.content)?e.content.map(e=>e?.type===`text`?e.text:e?.type===`tool_result`?`[TOOL_RESULT ${e.tool_use_id}] ${String(e.content||``)}`:``).filter(Boolean).join(`
6
+ `):String(e.content||``);t&&a.push(t)}else e.role;return{preface:r,turns:a}}async function Pt(e,t,n,r,i,a,o,s,c,l){if(!Ct()){await a({type:`response`,payload:{groupId:t,text:`⚠️ LiteRT-LM requires WebGPU and WebAssembly.Suspending. These are not both available in this browser.`}});return}await a(we(t,`info`,`Starting`,`Provider: LiteRT-LM (Browser · WebGPU) · Model: ${s}`)),await a({type:`model-download-progress`,payload:{groupId:t,status:`running`,progress:0,message:`Loading LiteRT-LM model: ${s}…`}});let u=e=>(e/(1024*1024)).toFixed(0),d=-1,f=(e,n,r)=>{let i=n?e/n:null,o=i==null?-1:Math.floor(i*100);o===d&&!r||(d=o,a({type:`model-download-progress`,payload:{groupId:t,status:`running`,progress:i,message:r?`Loaded ${s} from cache (${u(e)} MB )`:n?`Downloading ${s}: ${u(e)} / ${u(n)} MB`:`Downloading ${s}: ${u(e)} MB`}}))},p;try{p=await At(s,f,o)}catch(e){if(o?.aborted||e?.name===`AbortError`){await a({type:`model-download-progress`,payload:{groupId:t,status:`error`,progress:null,message:`LiteRT-LM model download cancelled.`}});return}await a({type:`model-download-progress`,payload:{groupId:t,status:`error`,progress:null,message:`LiteRT-LM model failed to load: ${e?.message??String(e)}`}}),await a({type:`response`,payload:{groupId:t,text:`⚠️ LiteRT-LM failed to initialize: ${e?.message??String(e)}`}});return}await a({type:`model-download-progress`,payload:{groupId:t,status:`done`,progress:1,message:`LiteRT-LM model ready.`}});let m=c??[],{preface:h,turns:g}=Nt(n,r,m),_;try{_=await p.createConversation({preface:{messages:h}})}catch(e){await a({type:`response`,payload:{groupId:t,text:`⚠️ LiteRT-LM failed to create conversation: ${e?.message??String(e)}`}});return}let v=g.slice(0,-1),y=g[g.length-1]??`Hello.`;for(let e of v){if(o?.aborted)return;try{await _.sendMessage(e)}catch{}}if(o?.aborted)return;let b=y;for(let n=0;n<10;n++){if(o?.aborted)return;await a({type:`streaming-start`,payload:{groupId:t}});let n=``;try{let e=_.sendMessageStreaming(b);for await(let r of e){if(o?.aborted)break;let e=r?.content?.[0]?.text??``;if(e){let r=G(e,`litert_lm`);r&&(n+=r,await a({type:`streaming-chunk`,payload:{groupId:t,text:r}}))}}}catch(e){if(o?.aborted)return;await a({type:`response`,payload:{groupId:t,text:`⚠️ LiteRT-LM generation error: ${e?.message??String(e)}`}});return}let r=m.length>0?Mt(n):null;if(r?.type===`tool_use`){await a({type:`streaming-end`,payload:{groupId:t}});let n=Array.isArray(r.tool_calls)?r.tool_calls:[];if(n.length===0){await a({type:`streaming-done`,payload:{groupId:t,text:`(no response)`}}),await a({type:`response`,payload:{groupId:t,text:`(no response)`}});return}let i=[],o=r.format===`model_specific`;for(let r of n){await a(H(t,r.name,`running`)),ye(e=>{a(e)});let n;try{n=await se(e,r.name,r.input||{},t,{allowedTools:m,invokeContext:l})}catch(e){n=`Error: ${e?.message??String(e)}`}finally{ye(null)}await a(H(t,r.name,`done`));let s=typeof n==`string`?n:JSON.stringify(n);o?i.push(`<|tool_response>${s}<|tool_response|>`):i.push(`[TOOL_RESULT ${r.id??r.name}] ${s}`)}if(o)b=i.join(`
7
+ `);else{let e=g[g.length-1]??``;b=[...i,``,`Based on the above tool results, please respond to the original request.`,e?`Original request: "${e}"`:``].filter(Boolean).join(`
8
+ `)}continue}let i;i=r?.type===`response`&&r.response?G(r.response,`litert_lm`):G(n,`litert_lm`),await a({type:`streaming-done`,payload:{groupId:t,text:i}}),await a({type:`response`,payload:{groupId:t,text:i}});return}await a({type:`response`,payload:{groupId:t,text:`⚠️ LiteRT-LM reached the maximum number of tool-calling iterations without producing a final response.`}})}const Ft=new class{chunkCallbacks=new Map;loadRejects=new Map;loadResolves=new Map;pendingRejects=new Map;pendingResolves=new Map;progressCallbacks=new Map;thinkingCallbacks=new Map;worker=null;async generate(e,t,n,r,i,a){let o=this.getWorker();return new Promise((s,c)=>{this.pendingResolves.set(n,s),this.pendingRejects.set(n,c),this.chunkCallbacks.set(n,e=>r(e.text)),i&&this.thinkingCallbacks.set(n,i),o.postMessage({type:`generate`,payload:{messages:e,maxTokens:t,groupId:n,tools:a}})})}async load(e,t,n,r,i){let a=this.getWorker();return new Promise((o,s)=>{this.loadResolves.set(r,o),this.loadRejects.set(r,s),this.progressCallbacks.set(r,i),a.postMessage({type:`load`,payload:{modelId:e,device:t,dtypeStrategy:n,groupId:r}})})}cleanup(e){this.pendingResolves.delete(e),this.pendingRejects.delete(e),this.progressCallbacks.delete(e),this.chunkCallbacks.delete(e),this.thinkingCallbacks.delete(e)}getWorker(){return this.worker||(this.worker=new Worker(S(new URL(`./transformers-js.worker.js`,import.meta.url).href),{type:`module`}),this.worker.onmessage=e=>{let{type:t,payload:n}=e.data,r=n?.groupId;switch(t){case`progress`:this.progressCallbacks.get(r)?.(n),n.status===`done`&&(this.loadResolves.get(r)?.(),this.loadResolves.delete(r),this.loadRejects.delete(r));break;case`chunk`:this.chunkCallbacks.get(r)?.(n);break;case`thinking-chunk`:this.thinkingCallbacks.get(r)?.(n.text);break;case`done`:this.pendingResolves.get(r)?.(n.text),this.cleanup(r);break;case`error`:this.pendingRejects.get(r)?.(n.error),this.loadRejects.get(r)?.(n.error),this.cleanup(r),this.loadResolves.delete(r),this.loadRejects.delete(r);break}}),this.worker}};function It(e){return typeof e?.content==`string`?e.content:Array.isArray(e?.content)?e.content.map(e=>e?.type===`text`?e.text||``:e?.type===`tool_use`?`[TOOL_CALL ${e.name}] ${JSON.stringify(e.input||{})}`:e?.type===`tool_result`?`[TOOL_RESULT ${e.tool_use_id}] ${String(e.content||``)}`:e?.type===`attachment`?`[ATTACHMENT ${e.mediaType}] ${e.fileName} (${e.mimeType})`:``).filter(Boolean).join(`
9
+ `):``}function Lt(e){let t=G(String(e||``),`transformers_js`).trim();if(!t)return null;let n=t.match(/<tool_call>([\s\S]*?)<\/tool_call>/);if(n)try{let e=JSON.parse(n[1]);if(e.name)return{type:`tool_use`,tool_calls:[{name:e.name,input:e.arguments||e.parameters||{}}]}}catch{}try{return JSON.parse(t)}catch{let e=t.indexOf(`{`),n=t.lastIndexOf(`}`);if(e>=0&&n>e)try{return JSON.parse(t.slice(e,n+1))}catch{return null}return null}}async function Rt(e,t,n,r,i,a,o,s,c,l){let u=s||[],d=await x(e,m.TRANSFORMERS_JS_BACKEND)||`auto`,f=await x(e,m.TRANSFORMERS_JS_DTYPE_STRATEGY)||`auto`;await a(we(t,`info`,`Starting`,`Provider: Transformers.js (Local Worker) · Model: ${c} · Backend: ${d} · Dtype: ${f}`)),await Ft.load(c,d,f,t,e=>{a({type:`model-download-progress`,payload:e})});let p=[...r];for(let r=0;r<10;r++){let r=[{role:`system`,content:n},...p.map(e=>({role:e.role===`assistant`?`assistant`:`user`,content:It(e)}))];await a({type:`streaming-start`,payload:{groupId:t}});let o=``,s=``,c=await Ft.generate(r,i,t,e=>{s+=e,a({type:`streaming-chunk`,payload:{groupId:t,text:e}})},e=>{o+=e,a(we(t,`text`,`Thinking`,o))},u);await a({type:`typing`,payload:{groupId:t}});let d=Lt(c);if(d?.type===`tool_use`?await a({type:`streaming-end`,payload:{groupId:t}}):await a({type:`streaming-done`,payload:{groupId:t,text:d?.response?G(d.response,`transformers_js`):G(c,`transformers_js`)}}),!d||d.type===`response`){await a({type:`response`,payload:{groupId:t,text:d?.response?G(d.response,`transformers_js`):G(c,`transformers_js`)}});return}let f=Array.isArray(d.tool_calls)?d.tool_calls:[];if(f.length===0){await a({type:`response`,payload:{groupId:t,text:G(d.response||c,`transformers_js`)}});return}let m=[];for(let n of f){await a(H(t,n.name,`running`)),ye(e=>{a(e)});let r;try{r=await se(e,n.name,n.input||{},t,{allowedTools:u,invokeContext:l})}finally{ye(null)}await a(H(t,n.name,`done`)),m.push({type:`tool_result`,tool_use_id:n.id||`call_${Date.now()}`,content:typeof r==`string`?r:JSON.stringify(r)})}p.push({role:`assistant`,content:[...d.response?[{type:`text`,text:d.response}]:[],...f.map(e=>({type:`tool_use`,id:e.id||`call_${Date.now()}_${Math.random()}`,name:e.name,input:e.input||{}}))]}),p.push({role:`user`,content:m})}}function zt(e,t,n){let r=t.input_tokens||0,i=t.output_tokens||0,a=t.cache_read_input_tokens||0,o=t.cache_creation_input_tokens||0;return{type:`token-usage`,payload:{groupId:e,inputTokens:r,outputTokens:i,cacheReadTokens:a,cacheCreationTokens:o,totalTokens:r+i+a+o,contextLimit:n}}}function Z(e,t,n,r){K(we(e,t,n,r))}async function*Bt(e,t){let n=e.getReader(),r=new TextDecoder,i=``;try{for(;!t?.aborted;){let{done:e,value:t}=await n.read();if(e)break;i+=r.decode(t,{stream:!0});let a=i.split(`
10
+
11
+ `);i=a.pop()||``;for(let e of a){let t=e.split(`
12
+ `);for(let e of t)if(!(!e||e.startsWith(`:`))&&e.startsWith(`data: `)){let t=e.slice(6).trim();if(t===`[DONE]`)continue;try{yield JSON.parse(t)}catch{}}}}}finally{n.releaseLock()}}const Vt=6e4,Ht=new Map;function Ut(e){let t=Ht.get(e);if(t)return t;let n={recentCalls:[],nextAllowedAt:0,remoteLimit:null,remoteRemaining:null,remoteResetAt:null};return Ht.set(e,n),n}function Wt(){return Date.now()}function Gt(e){if(!e)return null;let t=Number(e.trim());return Number.isFinite(t)?t:null}function Kt(e){if(!e)return null;let t=e.trim(),n=Number(t);if(Number.isFinite(n))return n<=0?null:Math.round(n*1e3);let r=Date.parse(t);if(!Number.isFinite(r))return null;let i=r-Wt();return i>0?i:null}function qt(e){let t=Gt(e);return t===null||t<=0?null:t>=0xe8d4a51000?Math.round(t):t>=1e9?Math.round(t*1e3):Wt()+Math.round(t*1e3)}async function Jt(e,t){e<=0||await new Promise((n,r)=>{let i=setTimeout(()=>{o(),n()},e),a=()=>{o(),r(new DOMException(`Aborted`,`AbortError`))},o=()=>{i&&=(clearTimeout(i),null),t?.removeEventListener(`abort`,a)};if(t?.aborted){a();return}t?.addEventListener(`abort`,a)})}function Yt(e,t){if(!Number.isFinite(t)||t<=0)return 0;let n=Wt();if(e.recentCalls=e.recentCalls.filter(e=>n-e<Vt),e.recentCalls.length<t)return 0;let r=e.recentCalls[0]||n;return Math.max(0,Vt-(n-r))}function Xt(e){let t=Wt(),n=0;return e.nextAllowedAt>t&&(n=Math.max(n,e.nextAllowedAt-t)),e.remoteRemaining!==null&&e.remoteRemaining<=0&&e.remoteResetAt!==null&&e.remoteResetAt>t&&(n=Math.max(n,e.remoteResetAt-t)),n}async function Zt(e,t,n,r){let i=Ut(e);for(;;){let e=Yt(i,n.callsPerMinute),a=n.autoAdapt?Xt(i):0,o=Math.max(e,a);if(o<=0){let e=Wt();if(n.callsPerMinute>0&&i.recentCalls.push(e),n.autoAdapt&&i.remoteRemaining!==null&&i.remoteRemaining>0&&i.remoteResetAt!==null&&i.remoteResetAt>e){let t=i.remoteResetAt-e,n=Math.max(1,i.remoteRemaining),r=Math.ceil(t/n);i.nextAllowedAt=Math.max(i.nextAllowedAt,e+r)}return}o>=1e3&&K({type:`thinking-log`,payload:{groupId:t,level:`info`,label:`Rate limiter`,message:`Waiting ${Math.ceil(o/1e3)}s before next provider call to avoid 429s.`,timestamp:Date.now()}}),await Jt(o,r)}}function Qt(e,t,n){if(!n.autoAdapt||!t||typeof t.get!=`function`)return;let r=Ut(e),i=Wt(),a=Gt(t.get(`x-ratelimit-limit`)),o=Gt(t.get(`x-ratelimit-remaining`)),s=qt(t.get(`x-ratelimit-reset`));a!==null&&(r.remoteLimit=Math.max(0,Math.floor(a))),o!==null&&(r.remoteRemaining=Math.max(0,Math.floor(o))),s!==null&&(r.remoteResetAt=s);let c=Kt(t.get(`retry-after`));c!==null&&(r.nextAllowedAt=Math.max(r.nextAllowedAt,i+c)),r.remoteRemaining!==null&&r.remoteRemaining<=0&&r.remoteResetAt!==null&&r.remoteResetAt>i&&(r.nextAllowedAt=Math.max(r.nextAllowedAt,r.remoteResetAt))}var $t=class{callbacks;contentBlocks=[];format;stopReason=`end_turn`;usage={input_tokens:0,output_tokens:0};#e=new Map;#t=``;#n=``;#r=new Map;constructor(e,t={}){this.format=e,this.callbacks=t}finalize(){return this.format===`openai`||this.format===`mesh-llm`?this.#a():this.#i()}push(e){this.format===`openai`||this.format===`mesh-llm`?this.#s(e):this.#o(e)}#i(){let e=[];for(let[,t]of[...this.#e.entries()].sort((e,t)=>e[0]-t[0]))if(t.type===`text`){let n=this.#c(t.text||``);n&&e.push({type:`text`,text:n})}else if(t.type===`tool_use`){let n;try{n=t.partialJson?JSON.parse(t.partialJson):{}}catch{n={}}e.push({type:`tool_use`,id:t.id||`call_${Date.now()}_${Math.random()}`,name:t.name,input:n})}else t.type===`thinking`?typeof t.text==`string`&&t.text.length>0&&e.push({type:`thinking`,thinking:t.text,...typeof t.id==`string`?{signature:t.id}:{}}):t.type===`redacted_thinking`&&typeof t.text==`string`&&t.text.length>0&&e.push({type:`redacted_thinking`,data:t.text});return{content:e,stop_reason:this.stopReason,usage:{...this.usage}}}#a(){let e=[];if(this.#n){let t=this.#c(this.#n);t&&e.push({type:`text`,text:t})}if(this.#t&&e.unshift({type:`thinking`,thinking:this.#t}),this.#r.size>0){this.stopReason=`tool_use`;for(let[,t]of[...this.#r.entries()].sort((e,t)=>e[0]-t[0])){let n;try{n=t.args?JSON.parse(t.args):{}}catch{n={}}e.push({type:`tool_use`,id:t.id||`call_${Date.now()}_${Math.random()}`,name:t.name,input:n})}}return{content:e,stop_reason:this.stopReason,usage:{...this.usage}}}#o(e){switch(e.type){case`message_start`:e.message?.usage&&(this.usage.input_tokens=e.message.usage.input_tokens||0,this.usage.cache_read_input_tokens=e.message.usage.cache_read_input_tokens||0,this.usage.cache_creation_input_tokens=e.message.usage.cache_creation_input_tokens||0);break;case`content_block_start`:{let t=e.index??this.#e.size,n=e.content_block||{};this.#e.set(t,{type:n.type||`text`,id:n.type===`thinking`?n.signature:n.id,name:n.name,text:n.text||n.thinking||n.data||``,partialJson:n.type===`tool_use`?``:void 0}),n.type===`tool_use`&&n.name&&this.callbacks.onToolStart?.(n.name);break}case`content_block_delta`:{let t=e.index??0,n=this.#e.get(t);if(!n){let r=e.delta?.type===`input_json_delta`?`tool_use`:`text`;n={type:r,text:r===`text`?``:void 0,partialJson:r===`tool_use`?``:void 0},this.#e.set(t,n)}let r=e.delta;if(!r)break;if(r.type===`text_delta`&&typeof r.text==`string`){let e=this.#c(r.text);if(!e)break;n.text=(n.text||``)+e,this.callbacks.onText?.(e)}r.type===`thinking_delta`&&typeof r.thinking==`string`&&(n.type=`thinking`,n.text=(n.text||``)+r.thinking,this.callbacks.onThinking?.(r.thinking)),r.type===`signature_delta`&&typeof r.signature==`string`&&(n.type=`thinking`,n.id=r.signature),r.type===`redacted_thinking_delta`&&typeof r.data==`string`&&(n.type=`redacted_thinking`,n.text=(n.text||``)+r.data),r.type===`input_json_delta`&&typeof r.partial_json==`string`&&(n.partialJson=(n.partialJson||``)+r.partial_json);break}case`content_block_stop`:break;case`message_delta`:e.delta?.stop_reason&&(this.stopReason=e.delta.stop_reason),e.usage&&(this.usage.output_tokens=e.usage.output_tokens||0,this.callbacks.onUsage?.(this.usage));break;case`message_stop`:break;default:break}}#s(e){e.usage&&(this.usage.input_tokens=e.usage.prompt_tokens||0,this.usage.output_tokens=e.usage.completion_tokens||0,this.usage.cache_read_input_tokens=e.usage.prompt_tokens_details?.cached_tokens||0,this.callbacks.onUsage?.(this.usage));let t=e.choices?.[0];if(!t)return;let n=t.delta;if(n){if(typeof n.content==`string`){let e=this.#c(n.content);e&&(this.#n+=e,this.callbacks.onText?.(e))}if(typeof n.reasoning==`string`)this.#t+=n.reasoning,this.callbacks.onThinking?.(n.reasoning);else if(n.reasoning&&typeof n.reasoning==`object`){let e=n.reasoning;typeof e.content==`string`?(this.#t+=e.content,this.callbacks.onThinking?.(e.content)):typeof e.text==`string`&&(this.#t+=e.text,this.callbacks.onThinking?.(e.text))}if(Array.isArray(n.tool_calls))for(let e of n.tool_calls){let t=e.index??0;this.#r.has(t)||(this.#r.set(t,{id:e.id||``,name:e.function?.name||``,args:``}),e.function?.name&&this.callbacks.onToolStart?.(e.function.name));let n=this.#r.get(t);n&&(e.id&&(n.id=e.id),e.function?.name&&(n.name=e.function.name),e.function?.arguments&&(n.args+=e.function.arguments))}t.finish_reason&&(t.finish_reason===`tool_calls`?this.stopReason=`tool_use`:t.finish_reason===`stop`?this.stopReason=`end_turn`:this.stopReason=t.finish_reason)}}#c(e){return G(e,this.callbacks.source??this.format)}};const en=new Map;function tn(e){return en.get(e)}function nn(e){en.delete(e)}function rn(e,t){let n=e.trim().match(/^\[tool_code\]([\s\S]*?)\[\/tool_code\]$/i);if(!n?.[1])return{toolBlocks:[],residualText:e};let r=n[1].trim(),i=r.match(/^print\s*\(\s*([a-zA-Z_][a-zA-Z0-9_]*)\s*\(\s*(.*?)\s*\)\s*\)$/s),a=r.match(/^([a-zA-Z_][a-zA-Z0-9_]*)\s*\(\s*(.*?)\s*\)$/s),o=i||a;if(!o?.[1])return{toolBlocks:[],residualText:e};let s=o[1];if(!t.has(s))return{toolBlocks:[],residualText:e};let c=(o[2]||``).trim(),l={};if(c){if(!c.startsWith(`{`)||!c.endsWith(`}`))return{toolBlocks:[],residualText:e};try{let t=JSON.parse(c);if(!t||typeof t!=`object`||Array.isArray(t))return{toolBlocks:[],residualText:e};l=t}catch{return{toolBlocks:[],residualText:e}}}return{toolBlocks:[{type:`tool_use`,id:`tool_code_${Date.now()}_${Math.random()}`,name:s,input:l}],residualText:``}}function an(e,t){if(!e||e.stop_reason===`tool_use`||!Array.isArray(e.content))return e;let n=new Set((t||[]).map(e=>typeof e?.name==`string`?e.name:``).filter(Boolean));if(n.size===0)return e;let r=[],i=!1;for(let t of e.content){if(t?.type!==`text`||typeof t.text!=`string`){r.push(t);continue}let e=rn(t.text,n);if(e.toolBlocks.length>0){i=!0,e.residualText.trim()&&r.push({type:`text`,text:e.residualText}),r.push(...e.toolBlocks);continue}r.push(t)}return i?{...e,content:r,stop_reason:`tool_use`}:e}function on(e){let t=e.filter(e=>e?.type===`tool_result`).map(e=>typeof e.content==`string`?e.content.trim():Array.isArray(e.content)?e.content.filter(e=>e.type===`text`).map(e=>e.text).join(`
13
+ `).trim():JSON.stringify(e.content).trim()).filter(Boolean);return t.length===0?``:t.join(`
14
+
15
+ `).slice(0,1e4)}function sn(e){let t=e.trim();return t?`Tool result:\n${t}`:``}async function cn(e,t,r){let{apiKey:i,assistantName:a,contextCompression:o=!1,contextLimit:s,enabledTools:c,groupId:l,isScheduledTask:u=!1,maxIterations:d,maxTokens:f,memory:p,messages:m,model:h,provider:g,providerHeaders:_={},providerRuntimeOverrides:v,reasoning:y,rateLimitAutoAdapt:b=!0,rateLimitCallsPerMinute:x=0,storageHandle:S,streaming:C=!1,subagentModelSelectionMode:w,subagentMaxTokens:T,subagentPinnedModel:E,subagentPinnedProvider:D,systemPrompt:O,workspaceGroupId:k}=t,A=!!t.subagentTask,j=A?l.startsWith(`subagent:`)?l:`subagent:${Y()}`:l,M=k||l;A&&ke(j,[]);let N={autoAdapt:b!==!1,callsPerMinute:Number.isFinite(x)?Math.max(0,Math.floor(x)):0};S&&He(S);let P=n(g);if(!P){K({payload:{groupId:j,error:`Unknown provider: ${g}`},type:`error`});return}let F=P,I=C&&F.supportsStreaming===!0&&F.format!==`prompt_api`;K({type:`typing`,payload:{groupId:j}}),Z(j,`info`,`Starting`,`Provider: ${F.name} · Model: ${h} · Max tokens: ${f} · Streaming: ${I?`on`:`off`}${C&&!I?` (provider does not support streaming)`:``}`);try{let t=[...m],n=0,l=typeof d==`number`&&d>0?d:50,b=Array.isArray(c)?c:Ee,x=O,C=``,k={apiKey:i,assistantName:a??``,db:e,enabledTools:b,invokeSubagent:t=>dn(e,t,r),maxTokens:f,memory:p??``,model:h,provider:g,subagentModelSelectionMode:w,subagentMaxTokens:T,subagentPinnedProvider:D,subagentPinnedModel:E,providerHeaders:_,providerRuntimeOverrides:v,storageHandle:S,streaming:!1,systemPrompt:x??``},A=[];for(;n<l;){n++;let c=tn(j);c&&(b=c.enabledTools,x=ne(a,p,b,c.systemPromptOverride));let l=s??U(h),d=te(x),{messages:m,estimatedTokens:v}=ce(t,{contextLimit:l,maxOutputTokens:f,skimTop:o,systemPromptTokens:d}),S=v+d,w=Math.max(100,Math.min(f,l-S-500)),T=z(F,m,b,{contextCompression:o,maxTokens:w,model:h,reasoning:y,system:x});Z(j,`api-call`,`API call #${n}`,`${m.length} messages in context`);let E={...ee(F,i),..._},D=F.baseUrl,O;O=I?await un(g,F,D,E,T,j,h,N,r):await ln(g,F,D,E,T,j,N,r),O=an(O,b),O.usage&&K(zt(j,O.usage,U(h)));for(let e of O.content)e.type===`text`&&e.text&&Z(j,`text`,`Response text`,e.text.length>200?e.text.slice(0,200)+`…`:e.text);if(O.stop_reason===`tool_use`){let n=new Set(b.map(e=>typeof e?.name==`string`?e.name:``).filter(Boolean)),r=O.content.filter(e=>e.type===`text`).map(e=>e.text).join(``).replace(/<internal>[\s\S]*?<\/internal>/g,``).trim();r&&K({payload:{groupId:j,text:r},type:`intermediate-response`}),I&&K({payload:{groupId:j},type:`streaming-end`});let i=[];for(let t of O.content)if(t.type===`tool_use`){let r=n.has(t.name),a=`${t.name}:${JSON.stringify(t.input)}`,o=A.filter(e=>e===a).length;if(A.push(a),!r){let e=`SYSTEM ERROR: Tool '${t.name}' is disabled or unavailable for this request. Do not call it again. Try a different approach or ask the user for help.`;Z(j,`warning`,`Tool blocked`,e),i.push({content:e,tool_use_id:t.id,type:`tool_result`});continue}let s=JSON.stringify(t.input),c=s.length>300?s.slice(0,300)+`…`:s;Z(j,`tool-call`,`Tool: ${t.name}`,c),K(H(j,t.name,`running`));let l;o>=3?(l=`SYSTEM ERROR: You have repeatedly called this tool with the exact same input (${o+1} times). This is a rigid loop. STOP calling this tool with these arguments. Try a different approach, fix the underlying issue, or ask the user for help.`,console.warn(`[Worker] Blocked repetitive tool call:`,a)):l=await se(e,t.name,t.input,M,{allowedTools:b,isScheduledTask:u,invokeContext:k});let d=typeof l==`string`?l:JSON.stringify(l),f=d.length>500?d.slice(0,500)+`…`:d;Z(j,`tool-result`,`Result: ${t.name}`,f),K(H(j,t.name,`done`)),Array.isArray(l)?i.push({content:l,tool_use_id:t.id,type:`tool_result`}):i.push({content:typeof l==`string`?l.slice(0,1e5):JSON.stringify(l).slice(0,1e5),tool_use_id:t.id,type:`tool_result`})}C=on(i),t.push({role:`assistant`,content:O.content}),t.push({role:`user`,content:i}),K({type:`typing`,payload:{groupId:j}})}else{let e=O.content.filter(e=>e.type===`text`).map(e=>e.text).join(``).replace(/<internal>[\s\S]*?<\/internal>/g,``).trim(),t=(e.toLowerCase()===`(no response)`?``:e)||(C?sn(C):`(no response)`);I&&K({payload:{groupId:j,text:t},type:`streaming-done`}),K({payload:{groupId:j,text:t},type:`response`});return}}K({payload:{text:`⚠️ Reached maximum tool-use iterations (${l}). Stopping to avoid excessive API usage.`,groupId:j},type:`response`})}catch(e){if(e instanceof Error&&e.name===`AbortError`)return;let t=e instanceof Error?e.message:String(e);I&&K({payload:{groupId:j,error:t},type:`streaming-error`}),K({type:`error`,payload:{groupId:j,error:t}})}finally{nn(j),A&&Pe(j)}}async function ln(e,t,n,r,i,a,o,s){return N(async()=>{await Zt(e,a,o,s);let c=await fetch(n,{body:JSON.stringify(i),headers:r,method:`POST`,signal:s});if(Qt(e,c.headers,o),!c.ok){let e=await c.text(),n=Error(`${t.name} API error ${c.status}: ${e}`);throw n.status=c.status,n}return je(t,await c.json())},{baseDelayMs:2e3,jitterFactor:.5,maxDelayMs:3e4,maxRetries:3,onRetry:(e,t,n,r)=>{let i=r instanceof Error?r.message:String(r);Z(a,`warning`,`Retrying API call (${e}/${t})`,i),K({type:`show-toast`,payload:{message:`LLM API: Retrying (${e}/${t})… ${i.slice(0,120)}`,type:`warning`,duration:5e3}})},shouldRetry:e=>D(e),signal:s})}async function un(e,t,n,r,i,a,o,s,c){let l={...i,stream:!0,...(t.format===`openai`||t.format===`mesh-llm`)&&{stream_options:{include_usage:!0}}};await Zt(e,a,s,c);let u=await fetch(n,{body:JSON.stringify(l),headers:r,method:`POST`,signal:c});if(Qt(e,u.headers,s),!u.ok){let e=await u.text(),n=Error(`${t.name} API error ${u.status}: ${e}`);throw n.status=u.status,n}if(!u.body)throw Error(`Streaming response has no body`);K({payload:{groupId:a},type:`streaming-start`});let d=0,f=``,p=0,m=``,h=(e=!1)=>{let t=Date.now();m&&(!e&&t-p<50||(Z(a,`info`,`Reasoning`,m.length>400?`${m.slice(0,400)}…`:m),m=``,p=t))},g=new $t(t.format,{source:t.id,onText:e=>{let t=Date.now();f+=e,t-d>=50&&(K({payload:{groupId:a,text:f},type:`streaming-chunk`}),f=``,d=t)},onToolStart:e=>{K(H(a,e,`running`))},onUsage:e=>{K(zt(a,e,U(o)))},onThinking:e=>{m+=e,h(!1)}});for await(let e of Bt(u.body,c))g.push(e);f&&K({payload:{groupId:a,text:f},type:`streaming-chunk`}),h(!0);let _=g.finalize();return t.format===`mesh-llm`&&(_=w(_)),_}async function dn(e,t,n){let r=t.provider,i=async e=>{K(e)};if(r===`transformers_js_browser`){await Rt(e,t.groupId,t.systemPrompt,t.messages,t.maxTokens,i,n,t.enabledTools,t.model);return}if(r===`prompt_api`){if(!R())throw Error(`Prompt API provider is not supported in this environment.`);await ie(e,t.groupId,t.systemPrompt,t.messages,t.maxTokens,i,n,t.enabledTools);return}if(r===`litert_lm_browser`){if(!Ct())throw Error(`Litert LM Browser provider is not supported in this environment.`);await Pt(e,t.groupId,t.systemPrompt,t.messages,t.maxTokens,i,n,t.model,t.enabledTools);return}await cn(e,t,n)}async function fn(e,t,r,i,a=!1,s=!1){let c=s?`subagent:${Y()}`:r;if(e.inFlightTriggerByGroup.set(c,i),e.setState(`thinking`,c),e.router?.setTyping(c,!0),e.events.emit(`typing`,{groupId:c,typing:!0}),i.startsWith(`[SCHEDULED TASK]`)){e.pendingScheduledTasks.add(r);let n={id:Y(),groupId:r,sender:`Scheduler`,content:i,timestamp:Date.now(),channel:De(e,r),isFromMe:!1,isTrigger:!0};await le(t,n),e.events.emit(`message`,n)}let l=``;try{l=await ve(t,r,`MEMORY.md`)}catch{}let u=(await Be(t)).find(e=>e.groupId===r),d=u?.pinnedProvider??e.provider,f=u?.pinnedModel??(u?.pinnedProvider?n(u.pinnedProvider)?.defaultModel??e.model:e.model),p=typeof u?.pinnedMaxTokens==`number`&&Number.isFinite(u.pinnedMaxTokens)&&u.pinnedMaxTokens>0?Math.floor(u.pinnedMaxTokens):e.maxTokens,h=f;d===`prompt_api`&&(f===`browser-built-in`||!f)&&!R()&&(h=await x(t,m.PROMPT_API_FALLBACK_MODEL)||`onnx-community/Qwen3-0.6B-ONNX`);let g=Math.max(1,Math.min(p,o(h))),_=n(d)??e.providerConfig;e.inFlightEffectiveProviderByGroup.set(c,{providerId:d,providerConfig:_,model:f});let v=Array.isArray(u?.toolTags)?W.allTools.filter(e=>u.toolTags.includes(e.name)):W.enabledTools,y=(await Oe(t,c)).tools.filter(e=>!W.allTools.some(t=>t.name===e.name)),b=Array.isArray(u?.toolTags)?y.filter(e=>u.toolTags.includes(e.name)):y.filter(e=>W.isDeclarativeToolEnabled(e.name)),S=[...v,...b],C=await I(t,c),w=C.skills.length>0&&!S.some(e=>e.name===qe.name)?[...S,qe]:S,T=u?.subagentModelSelectionMode===`manual`?`manual`:`automatic`,E=u?.subagentMaxTokens,ee=u?.subagentPinnedProvider,D=u?.subagentPinnedModel,k=u?.subagentFastProvider,A=u?.subagentFastModel,M=u?.subagentSmartProvider,N=u?.subagentSmartModel,P=u?.subagentPowerfulProvider,F=u?.subagentPowerfulModel,L=u?.providerRuntimeOverrides,ae=q.getPeerState(r)||void 0,z=ne(e.assistantName,l,w,W.systemPromptOverride,ae,C.skills),oe=U(h),se=te(z)+(w?.reduce((e,t)=>e+te(JSON.stringify(t)),0)??0),B=await Ve(r,200);a&&(B=B.length>0?[B[B.length-1]]:[{role:`user`,content:i}]);let V=ce(B,{contextLimit:oe,systemPromptTokens:se,maxOutputTokens:g,skimTop:e.contextCompressionEnabled}),ue=V.messages,de=V.estimatedTokens+se;if(q.tokenUsage){let e=q.tokenUsage,t=(e.inputTokens||0)+(e.cacheReadTokens||0)+(e.outputTokens||0);de=Math.max(de,t)}if(e.events.emit(`context-usage`,{estimatedTokens:de,contextLimit:oe,usagePercent:Math.min(100,Math.max(V.usagePercent,de/oe*100)),truncatedCount:V.truncatedCount}),V.usagePercent>80&&V.truncatedCount>0&&B.length>10&&(e.events.emit(`show-toast`,{message:`Context ${V.usagePercent.toFixed(0)}% full — auto-compacting older messages…`,type:`info`,duration:4e3}),queueMicrotask(()=>Ae(e,t,r))),d===`transformers_js_browser`){let n=new AbortController;e.promptControllers.set(c,n);let r={apiKey:``,assistantName:e.assistantName,db:t,enabledTools:w,invokeSubagent:async e=>{await dn(t,e,n.signal)},maxTokens:g,memory:l??``,model:f,provider:d,providerHeaders:ge(e,d,``,L),providerRuntimeOverrides:L,streaming:!1,subagentModelSelectionMode:T,subagentMaxTokens:E,subagentPinnedProvider:ee,subagentPinnedModel:D,subagentFastProvider:k,subagentFastModel:A,subagentSmartProvider:M,subagentSmartModel:N,subagentPowerfulProvider:P,subagentPowerfulModel:F,systemPrompt:z};try{await Rt(t,c,z,ue,g,async n=>{await re(e,t,n)},n.signal,w,f,r)}catch(n){if(n instanceof Error&&n.name===`AbortError`)return;await ze(e,t,c,`⚠️ Error: ${n instanceof Error?n.message:String(n)}`)}finally{e.promptControllers.delete(c)}return}if(d===`prompt_api`){if(R()||await j(),!R()){await ze(e,t,c,`⚠️ Error: Prompt API is not available in this browser. Switch provider or enable experimental browser flags.`);return}let n=new AbortController;e.promptControllers.set(c,n);let r={apiKey:``,assistantName:e.assistantName,db:t,enabledTools:w,invokeSubagent:async e=>{await dn(t,e,n.signal)},maxTokens:g,memory:l??``,model:f,provider:d,providerHeaders:ge(e,d,``,L),providerRuntimeOverrides:L,streaming:!1,subagentModelSelectionMode:T,subagentMaxTokens:E,subagentPinnedProvider:ee,subagentPinnedModel:D,subagentFastProvider:k,subagentFastModel:A,subagentSmartProvider:M,subagentSmartModel:N,subagentPowerfulProvider:P,subagentPowerfulModel:F,systemPrompt:z};try{await ie(t,c,z,ue,g,async n=>{await re(e,t,n)},n.signal,w,r)}catch(n){if(n instanceof Error&&n.name===`AbortError`)return;await ze(e,t,c,`⚠️ Error: ${n instanceof Error?n.message:String(n)}`)}finally{e.promptControllers.delete(c)}return}if(d===`litert_lm_browser`){if(!Ct()){await ze(e,t,c,`⚠️ LiteRT-LM requires WebGPU and WebAssembly.Suspending. These are not both available in this browser.`);return}let n=new AbortController;e.promptControllers.set(c,n);let r={apiKey:``,assistantName:e.assistantName,db:t,enabledTools:w,invokeSubagent:async e=>{await dn(t,e,n.signal)},maxTokens:g,memory:l??``,model:f,provider:d,providerHeaders:ge(e,d,``,L),providerRuntimeOverrides:L,streaming:!1,subagentModelSelectionMode:T,subagentMaxTokens:E,subagentPinnedProvider:ee,subagentPinnedModel:D,subagentFastProvider:k,subagentFastModel:A,subagentSmartProvider:M,subagentSmartModel:N,subagentPowerfulProvider:P,subagentPowerfulModel:F,systemPrompt:z};try{await Pt(t,c,z,ue,g,async n=>{await re(e,t,n)},n.signal,f,w,r)}catch(n){if(n instanceof Error&&n.name===`AbortError`)return;await ze(e,t,c,`⚠️ Error: ${n instanceof Error?n.message:String(n)}`)}finally{e.promptControllers.delete(c)}return}let fe=e.streamingEnabled&&_.supportsStreaming===!0&&(_.format===`openai`||_.format===`anthropic`);d===`transformers_js_local`&&be(e,e.events,c);let pe=e.createProviderRequestId(c);e.agentWorker?.postMessage({type:`invoke`,payload:{apiKey:d===e.provider?await O(e):await e.getApiKeyForSpecificProvider(t,d),assistantName:e.assistantName,contextCompression:e.contextCompressionEnabled,contextLimit:U(h),enabledTools:w,groupId:c,isScheduledTask:e.schedulerTriggeredGroups.has(r),maxIterations:e.maxIterations,maxTokens:g,memory:l,messages:ue,model:f,provider:d,providerHeaders:ge(e,d,pe,L),providerRuntimeOverrides:L,reasoning:me(e),rateLimitAutoAdapt:e.rateLimitAutoAdapt,rateLimitCallsPerMinute:e.rateLimitCallsPerMinute,storageHandle:await x(t,m.STORAGE_HANDLE),streaming:fe,subagentModelSelectionMode:T,subagentMaxTokens:E,subagentPinnedProvider:ee,subagentPinnedModel:D,subagentFastProvider:k,subagentFastModel:A,subagentSmartProvider:M,subagentSmartModel:N,subagentPowerfulProvider:P,subagentPowerfulModel:F,systemPrompt:z,subagentTask:s}})}function pn(e,t,n){if(!e.enabledChannelTypes.includes(n.channel))return null;let r=n.content.trim(),i=t.replace(/[.*+?^${}()|[\]\\]/g,`\\$&`),a=r;if(e.requireMention){let e=RegExp(`^@${i}\\b\\s*(?:-|:)?\\s*`,`i`);if(!e.test(a))return null;a=a.replace(e,``).trim()}let o=a.match(/^\/([a-zA-Z0-9_]+)(?:\s+([\s\S]+))?$/);if(!o)return null;let s=o[1];if(!e.allowedTools.includes(s))return null;let c=(o[2]||``).trim();if(!c)return{toolName:s,input:{}};let l=c.startsWith(`'`)&&c.endsWith(`'`)||c.startsWith(`"`)&&c.endsWith(`"`)?c.slice(1,-1).trim():c;try{let e=JSON.parse(l);return e&&typeof e==`object`?{toolName:s,input:e}:null}catch{return null}}function mn(e,t){let n=e.trim().match(/^\/([a-z0-9](?:[a-z0-9]|-(?!-))*[a-z0-9]|[a-z0-9])(?:\s+([\s\S]+))?$/u);if(!n)return null;let r=t.find(e=>e.name===n[1]&&e.userInvocable!==!1);return r?{skill:r,arguments:(n[2]||``).trim()}:null}async function hn(e,t,n){if(n.a2uiEnvelopes&&n.a2uiEnvelopes.length>0)for(let t of n.a2uiEnvelopes)e.events.emit(`a2ui-surface`,{groupId:n.groupId,envelope:t});n.a2uiAction&&e.events.emit(`a2ui-action`,{groupId:n.groupId,action:n.a2uiAction});let r=!!n.content,i=(n.attachments?.length??0)>0;if(!r&&!i&&(n.a2uiEnvelopes?.length??0)>0)return;let a=pn(e.directToolCommandPolicy,e.assistantName,n),o=a?null:mn(n.content,(await I(t,n.groupId)).skills),s=n.channel===`browser`,c=e.channelRegistry.shouldAutoTrigger(n.groupId),l=!1;(n.channel===`browser`||n.groupId.startsWith(`room:`))&&(l=e.triggerPattern.test(n.content.trim())),o&&(l=!0),s&&n.groupId.startsWith(`peer:`)&&e.peerCompletedContexts.delete(n.groupId);let u=!!a;if(!l&&e.peerjsMyPeerId){if(n.content.includes(`@${e.peerjsMyPeerId}`))l=!0;else if(e.peerjsMyAlias&&n.content.includes(`@${e.peerjsMyAlias}`))l=!0;else for(let[t,r]of Object.entries(e.peerjsPeerAliases))if(r===e.peerjsMyPeerId&&n.content.includes(`@${t}`)){l=!0;break}}n.content.trim().startsWith(`[SCHEDULED TASK]`)&&(l=!0),n.content.trim().startsWith(`[A2UI ACTION]`)&&(l=!0);let d=!1;d=u||l?!0:s?!(n.groupId.startsWith(`peer:`)||n.groupId.startsWith(`room:`)):c,d&&!s&&!o&&n.groupId.startsWith(`peer:`)&&e.peerCompletedContexts.has(n.groupId)&&(d=!1);let f=await Xe(t,n.groupId,n.attachments||[]),p={...n,attachments:f,isFromMe:!1,isTrigger:d};if(d&&!u&&!o?.skill.execution&&e.messageQueue.push(o?{...n,content:`[SKILL COMMAND] Activate the "${o.skill.name}" skill and follow its instructions.\n${o.arguments}`}:n),await le(t,p),e.events.emit(`message`,p),o?.skill.execution&&e.agentWorker){let t=o.skill.execution.tools.map(e=>{let t=e.input&&typeof e.input==`object`?{...e.input}:{};return o.arguments&&Object.keys(t).length===0&&(t.rawInput=o.arguments),{...e,input:t}});e.agentWorker.postMessage({type:`execute-skill-tools`,payload:{groupId:n.groupId,skillName:o.skill.name,tools:t}});return}if(n.channel===`peerjs`&&Te(n.groupId),s&&(n.groupId.startsWith(`peer:`)||n.groupId.startsWith(`room:`))&&e.router?.send(n.groupId,n.content,f).catch(e=>{console.error(`Failed to route browser message to peer:`,e)}),a&&e.agentWorker){e.agentWorker.postMessage({type:`execute-direct-tool`,payload:{groupId:n.groupId,name:a.toolName,input:a.input}});return}gn(e,t)}async function gn(e,t){if(e.processing||e.messageQueue.length===0)return;let r=e.messageQueue[0]?.groupId,i=e.providerConfig,a=e.provider;if(r)try{let e=(await Be(t)).find(e=>e.groupId===r);if(e?.pinnedProvider){let t=n(e.pinnedProvider);t&&(i=t,a=e.pinnedProvider)}}catch{}let o=i?.requiresApiKey!==!1,s=!0;if(o&&(s=a===e.provider?!!await O(e):!!await e.getApiKeyForSpecificProvider(t,a)),o&&!s){let t=`API key not configured. Go to Settings to add your API key.`;e.events.emit(`provider-help`,{providerId:a,reason:t,helpType:ae(a,t,o)});let n=e.messageQueue.shift();e.events.emit(`error`,{groupId:n.groupId,error:t});return}e.processing=!0;let c=e.messageQueue.shift();try{await fn(e,t,c.groupId,c.content,c.freshContext,c.subagent)}catch(e){console.error(`Failed to invoke agent:`,e)}finally{e.processing=!1,e.messageQueue.length>0&&gn(e,t)}}var _n=class{listeners;constructor(){this.listeners=new Map}emit(e,t){this.listeners.get(e)?.forEach(e=>e(t))}off(e,t){this.listeners.get(e)?.delete(t)}on(e,t){this.listeners.has(e)||this.listeners.set(e,new Set),this.listeners.get(e)?.add(t)}};function Q(e,t){return{jsonrpc:`2.0`,method:e,params:t}}var vn=class{static isRoomMethod(e){return e===X.JOIN||e===X.ROSTER||e===X.LEAVE||e===X.MESSAGE||e===X.INVITE||e===X.RELAY||e===X.A2UI||e===X.A2UI_ACTION}roomsSignal=new C.State([]);_opts;_rooms=new Map;_seen=new Map;_surfaceOwners=new Map;constructor(e){this._opts=e}broadcast(e,t,n){let r=this._rooms.get(e);if(!r)return null;let i=this._opts.getLocalMember(),a=Y(),o={roomId:e,messageId:a,senderPeerId:i.peerId,senderAlias:i.alias,text:t,attachments:n?.map(e=>({fileName:e.fileName,mimeType:e.mimeType,path:e.path,size:e.size}))};return this._markSeen(e,a),this._deliverToMembers(r,Q(X.MESSAGE,o)),a}broadcastA2UI(e,t){let n=this._rooms.get(e);if(!n)return null;let r=this._myPeerId;this._surfaceOwners.set(t.surfaceId,r);let i=Y(),a={roomId:e,broadcastId:i,ownerPeerId:r,senderPeerId:r,envelope:t};return this._markSeen(e,i),this._deliverToMembers(n,Q(X.A2UI,a)),i}broadcastA2UIAction(e,t){let n=this._rooms.get(e);if(!n)return null;let r=this._opts.getLocalMember(),i=this._surfaceOwners.get(t.surfaceId)??``,a=Y(),o={roomId:e,broadcastId:a,ownerPeerId:i,senderPeerId:r.peerId,senderAlias:r.alias,action:t};return this._markSeen(e,a),this._deliverToMembers(n,Q(X.A2UI_ACTION,o)),a}createRoom(e){let t=this._opts.getLocalMember(),n={roomId:Y(),name:e,hostPeerId:t.peerId,members:[t],createdAt:Date.now()};return this._save(n),n}get(e){return this._rooms.get(e)??null}getSurfaceOwner(e){return this._surfaceOwners.get(e)}handleNotification(e,t,n){switch(t){case X.JOIN:this._onJoin(e,n);break;case X.ROSTER:this._onRoster(n);break;case X.LEAVE:this._onLeave(e,n);break;case X.MESSAGE:this._onMessage(n);break;case X.INVITE:this._opts.onInvite?.(n);break;case X.RELAY:this._onRelay(n);break;case X.A2UI:this._onA2UI(n);break;case X.A2UI_ACTION:this._onA2UIAction(n);break}}invite(e,t){let n=this._rooms.get(e);if(!n)return!1;let r=this._opts.getLocalMember(),i={roomId:e,roomName:n.name,hostPeerId:n.hostPeerId,fromPeerId:r.peerId,fromAlias:r.alias},a=this._opts.transport;return a.connectToPeer(t),a.sendToPeer(t,Q(X.INVITE,i))}isHost(e){let t=this._rooms.get(e);return!!t&&t.hostPeerId===this._myPeerId}joinRoom(e,t,n){let r=this._opts.getLocalMember(),i=this._rooms.get(e)??{roomId:e,name:n,hostPeerId:t,members:[{peerId:t,alias:t,kind:`agent`},r],createdAt:Date.now()};this._save(i);let a=this._opts.transport;a.connectToPeer(t);let o={roomId:e,member:r};return a.sendToPeer(t,Q(X.JOIN,o)),i}leaveRoom(e){let t=this._rooms.get(e);if(!t)return;let n=this._myPeerId;if(t.hostPeerId===n){let r={roomId:e,peerId:n,disbanded:!0};this._deliverToMembers(t,Q(X.LEAVE,r))}else{let r={roomId:e,peerId:n};this._opts.transport.sendToPeer(t.hostPeerId,Q(X.LEAVE,r))}this._rooms.delete(e),this._seen.delete(e),this._opts.removeRoom?.(e),this._publish()}list(){return Array.from(this._rooms.values())}loadRooms(e){this._rooms.clear();for(let t of e)this._rooms.set(t.roomId,t);this._publish()}_broadcastRoster(e){let t={roomId:e.roomId,name:e.name,hostPeerId:e.hostPeerId,members:e.members};this._deliverToMembers(e,Q(X.ROSTER,t))}_deliverToMembers(e,t){let n=this._myPeerId;for(let r of e.members)r.peerId!==n&&this._deliverToPeer(e,r.peerId,t)}_deliverToPeer(e,t,n){let r=this._opts.transport;if(r.isConnected(t)){r.sendToPeer(t,n);return}let i=this._myPeerId;if(e.hostPeerId!==i&&e.hostPeerId!==t&&r.isConnected(e.hostPeerId)){let i={roomId:e.roomId,targetPeerId:t,inner:n};r.sendToPeer(e.hostPeerId,Q(X.RELAY,i)),r.connectToPeer(t);return}r.connectToPeer(t),r.sendToPeer(t,n)}_hasSeen(e,t){return this._seen.get(e)?.has(t)??!1}_markSeen(e,t){let n=this._seen.get(e);if(n||(n=new Set,this._seen.set(e,n)),n.add(t),n.size>500){let e=n.values().next().value;e!==void 0&&n.delete(e)}}get _myPeerId(){return this._opts.transport.myPeerId}_onA2UI(e){if(!e||typeof e.roomId!=`string`||typeof e.broadcastId!=`string`||!e.envelope||!this._rooms.get(e.roomId)||this._hasSeen(e.roomId,e.broadcastId))return;this._markSeen(e.roomId,e.broadcastId);let t=e.envelope.surfaceId;this._surfaceOwners.get(t)!==this._myPeerId&&this._surfaceOwners.set(t,e.ownerPeerId);let n={id:e.broadcastId,groupId:P(e.roomId),sender:e.senderPeerId,content:``,timestamp:Date.now(),channel:`room`,a2uiEnvelopes:[e.envelope]};this._opts.onMessage(n)}_onA2UIAction(e){if(!e||typeof e.roomId!=`string`||typeof e.broadcastId!=`string`||!e.action||!this._rooms.get(e.roomId)||this._hasSeen(e.roomId,e.broadcastId)||(this._markSeen(e.roomId,e.broadcastId),this._surfaceOwners.get(e.action.surfaceId)!==this._myPeerId))return;let t={id:e.broadcastId,groupId:P(e.roomId),sender:e.senderAlias||e.senderPeerId,content:yt(e.action,e.senderAlias),timestamp:Date.now(),channel:`room`,a2uiAction:e.action};this._opts.onMessage(t)}_onJoin(e,t){if(!t||typeof t.roomId!=`string`)return;let n=this._rooms.get(t.roomId);if(!n||n.hostPeerId!==this._myPeerId)return;let r=t.member??{peerId:e,alias:e,kind:`human`};r.peerId=e;let i=n.members.findIndex(t=>t.peerId===e);i>=0?n.members[i]=r:n.members.push(r),this._save(n),this._broadcastRoster(n)}_onLeave(e,t){if(!t||typeof t.roomId!=`string`)return;let n=this._rooms.get(t.roomId);if(n){if(t.disbanded){this._rooms.delete(t.roomId),this._seen.delete(t.roomId),this._opts.removeRoom?.(t.roomId),this._publish();return}n.hostPeerId===this._myPeerId&&(n.members=n.members.filter(n=>n.peerId!==(t.peerId||e)),this._save(n),this._broadcastRoster(n))}}_onMessage(e){if(!e||typeof e.roomId!=`string`||typeof e.messageId!=`string`||!this._rooms.get(e.roomId)||this._hasSeen(e.roomId,e.messageId))return;this._markSeen(e.roomId,e.messageId);let t={id:e.messageId,groupId:P(e.roomId),sender:e.senderAlias||e.senderPeerId,content:e.text??``,timestamp:Date.now(),channel:`room`,attachments:e.attachments?.map(e=>({fileName:e.fileName,mimeType:e.mimeType,path:e.path,size:e.size}))};this._opts.onMessage(t)}_onRelay(e){!e||typeof e.targetPeerId!=`string`||!e.inner||this.isHost(e.roomId)&&this._opts.transport.sendToPeer(e.targetPeerId,e.inner)}_onRoster(e){if(!e||typeof e.roomId!=`string`)return;let t=this._rooms.get(e.roomId),n={roomId:e.roomId,name:e.name??t?.name??e.roomId,hostPeerId:e.hostPeerId,members:Array.isArray(e.members)?e.members:[],createdAt:t?.createdAt??Date.now()};this._save(n)}_publish(){this.roomsSignal.set(this.list())}_save(e){this._rooms.set(e.roomId,e),this._opts.persistRoom?.(e),this._publish()}};function yn(){return new Promise(async(e,t)=>{try{let n=await y();if(!n)return t(Error(`failed to get transaction, cannot get enabled tasks.`));let r=n.transaction(`tasks`,`readonly`);if(!r)return t(Error(`failed to get transaction`));let i=r.objectStore(`tasks`).index(`by-enabled`).getAll(1);i.onsuccess=()=>{e(i.result.map(e=>({...e,enabled:!0})))},i.onerror=()=>t(i.error)}catch(e){t(e)}})}function bn(e,t){if(typeof e!=`string`||!e.trim())return!1;let n=e.trim().split(/\s+/);if(n.length!==5)return!1;let[r,i,a,o,s]=n;return xn(r,t.getMinutes())&&xn(i,t.getHours())&&xn(a,t.getDate())&&xn(o,t.getMonth()+1)&&xn(s,t.getDay())}function xn(e,t){return e===`*`||e.split(`,`).some(e=>{if(e.includes(`/`)){let[n,r]=e.split(`/`),i=parseInt(r,10);if(isNaN(i)||i<=0)return!1;if(n===`*`)return t%i===0;if(n.includes(`-`)){let[e,r]=n.split(`-`).map(Number);return t>=e&&t<=r&&(t-e)%i===0}let a=parseInt(n,10);return t>=a&&(t-a)%i===0}if(e.includes(`-`)){let[n,r]=e.split(`-`).map(Number);return t>=n&&t<=r}return parseInt(e,10)===t})}var Sn=class{interval=null;onTaskChange;runner;constructor(e,t){this.runner=e,this.onTaskChange=t}ranThisMinute(e,t){if(!e.lastRun)return!1;let n=new Date(e.lastRun);return n.getFullYear()===t.getFullYear()&&n.getMonth()===t.getMonth()&&n.getDate()===t.getDate()&&n.getHours()===t.getHours()&&n.getMinutes()===t.getMinutes()}start(){this.interval||(this.interval=setInterval(()=>this.tick(),v),this.tick())}stop(){this.interval&&=(clearInterval(this.interval),null)}async tick(){try{let e=await yn(),t=new Date;for(let n of e){let e=n.schedule||``;e!==``&&bn(e,t)&&!this.ranThisMinute(n,t)&&(await Me(n.id,t.getTime()),this.onTaskChange?.(),this.runner(n).catch(e=>{console.error(`Task ${n.id} failed:`,e)}))}}catch(e){console.error(`Scheduler tick error:`,e)}}};function Cn(e){if(!e)return[];try{let t=JSON.parse(e);if(Array.isArray(t))return E(t.map(e=>String(e)))}catch{}return E(e.split(`,`))}async function wn(e,t){e.channelEnabledByType.telegram=await Se(`telegram`,t),e.channelEnabledByType.imessage=await Se(`imessage`,t);let n=await e.loadSecretConfig(t,m.TELEGRAM_BOT_TOKEN),r=Cn(await x(t,m.TELEGRAM_CHAT_IDS)),i=await x(t,m.TELEGRAM_USE_PROXY)===`true`;e.telegramBotToken=n,e.telegramChatIds=r,e.telegramUseProxy=i,e.telegram.configure(n,r,i);let a=async(e,n)=>{try{let r=await xe(t,e,n),i=new Uint8Array(r.byteLength);i.set(r);let a=Ze(n.split(`/`).pop()||n);return new Blob([i],{type:a})}catch(e){return console.warn(`Orchestrator: channel fileReader failed for ${n}:`,e),null}};e.telegram.fileReader=a,e.imessage.fileReader=a;let o=(await x(t,m.IMESSAGE_SERVER_URL)||``).trim().replace(/\/+$/,``),s=await e.loadSecretConfig(t,m.IMESSAGE_API_KEY),c=Cn(await x(t,m.IMESSAGE_CHAT_IDS));e.imessageServerUrl=o,e.imessageApiKey=s,e.imessageChatIds=c,e.imessage.configure(o,s,c),e.channelEnabledByType.peerjs=await Se(`peerjs`,t);let l=(await x(t,m.PEERJS_MY_PEER_ID)||``).trim(),u=Cn(await x(t,m.PEERJS_TRUSTED_PEER_IDS)),d=(await x(t,m.PEERJS_SERVER_HOST)||``).trim(),f=await x(t,m.PEERJS_SERVER_PORT),p=f&&parseInt(f,10)||0,h=(await x(t,m.PEERJS_SERVER_PATH)||``).trim(),g=await x(t,m.PEERJS_SERVER_SECURE)!==`false`,_={},v=await x(t,m.PEERJS_PEER_ALIASES);if(v)try{_=JSON.parse(v)}catch(e){console.warn(`Failed to parse peerjs_peer_aliases`,e)}e.peerjsMyPeerId=l,e.peerjsMyAlias=(await x(t,m.PEERJS_MY_ALIAS)||``).trim(),e.peerjsTrustedPeerIds=u,e.peerjsPeerAliases=_,e.peerjsServerHost=d,e.peerjsServerPort=p,e.peerjsServerPath=h,e.peerjsServerSecure=g;let y=d?{host:d,port:p||void 0,path:h||void 0,secure:g}:{};e.peerjs.configure(l,u,y)}const $={allowedTools:[`clear_chat`,`show_toast`],enabledChannelTypes:[`telegram`],requireMention:!0};function Tn(e){if(!e)return{allowedTools:[...$.allowedTools],enabledChannelTypes:[...$.enabledChannelTypes],requireMention:$.requireMention};try{let t=JSON.parse(e),n=Array.isArray(t.enabledChannelTypes)?t.enabledChannelTypes.filter(e=>typeof e==`string`&&e.trim().length>0):$.enabledChannelTypes;return{allowedTools:Array.isArray(t.allowedTools)?t.allowedTools.filter(e=>typeof e==`string`&&e.trim().length>0):$.allowedTools,enabledChannelTypes:n,requireMention:typeof t.requireMention==`boolean`?t.requireMention:$.requireMention}}catch{return{allowedTools:[...$.allowedTools],enabledChannelTypes:[...$.enabledChannelTypes],requireMention:$.requireMention}}}function En(e,t){typeof navigator>`u`||!navigator.serviceWorker||navigator.serviceWorker.addEventListener(`message`,t=>{if(t.data?.type===`request-proxy-config`){T(e);return}if(t.data?.type!==`scheduled-task-trigger`)return;let{taskId:n,groupId:r,prompt:i,taskType:a,tools:o}=t.data;r&&(e.schedulerTriggeredGroups.add(r),(async()=>{let t=q.tasks.find(e=>e.id===n);if(t){q.runTask(t);return}if(a===`tools`&&Array.isArray(o)&&o.length>0){q.runTask({id:n||`push-task-${Date.now()}`,groupId:r,createdAt:Date.now(),enabled:!0,prompt:i||``,type:`tools`,tools:o,lastRun:null});return}i&&B(e,i,r)})().catch(e=>console.error(`Push-triggered task ${n} failed:`,e)).finally(()=>{e.schedulerTriggeredGroups.delete(r)}))})}function Dn(e){return new vn({transport:{get myPeerId(){return e.peerjs.myPeerId||e.peerjsMyPeerId},sendToPeer:(t,n)=>e.peerjs.sendRoomNotification(t,n),isConnected:t=>e.peerjs.isPeerConnected(t),connectToPeer:t=>e.peerjs.connectPeer(t)},getLocalMember:()=>({peerId:e.peerjs.myPeerId||e.peerjsMyPeerId,alias:e.peerjsMyAlias||e.peerjs.myPeerId||e.peerjsMyPeerId||e.assistantName,kind:`agent`,agentName:e.assistantName}),onMessage:t=>e.roomChannel.deliverInbound(t),onInvite:t=>M(e,t),persistRoom:t=>{e.db&&he(e.db,t).catch(e=>console.error(`Failed to persist room:`,e))},removeRoom:t=>{e.db&&F(e.db,t).catch(e=>console.error(`Failed to delete room:`,e))}})}async function On(e,t){e.initializeChannelRegistry(),e.channelRegistry.onMessage(n=>{hn(e,t,n).catch(e=>{console.error(`Failed to enqueue inbound message:`,e)})}),e.channelRegistry.onTyping((t,n)=>{e.events.emit(`typing`,{groupId:t,typing:n}),t.startsWith(`peer:`)&&q.setRemoteAgentTyping(t,n)}),e.peerjs.onTaskComplete(t=>{e.peerCompletedContexts.add(t)}),await wn(e,t),Ce(e);try{let n=await Ie(t);e.roomManager.loadRooms(n)}catch(e){console.error(`Failed to load rooms:`,e)}}async function kn(e,t){e.assistantName=await x(t,m.ASSISTANT_NAME)||`ShadowClaw`,e.triggerPattern=a(e.assistantName)}async function An(e,t){let n=await x(t,m.VM_BOOT_MODE);e.vmBootMode=n===`disabled`||n===`auto`||n===`9p`||n===`ext2`?n:`disabled`,e.streamingEnabled=await x(t,m.STREAMING_ENABLED)!==`false`,e.webMcpToolsEnabled=await x(t,m.WEBMCP_TOOLS_ENABLED)!==`false`,e.vmBashFullInternetAccess=await x(t,m.VM_BASH_FULL_INTERNET_ACCESS)===`true`;let r=await x(t,m.WEBMCP_MODE);(r===`native`||r===`polyfill`)&&We(r),e.contextCompressionEnabled=await x(t,m.CONTEXT_COMPRESSION_ENABLED)===`true`;let i=await x(t,m.REASONING_EFFORT);e.reasoningEffort=i?i.trim().toLowerCase():`none`,e.directToolCommandPolicy=Tn(await x(t,m.DIRECT_TOOL_COMMAND_POLICY)),e.useProxy=await x(t,m.USE_PROXY)===`true`,e.proxyUrl=await x(t,m.PROXY_URL)||`/proxy`,e.gitProxyUrl=await x(t,m.GIT_PROXY_URL)||`/git-proxy`,e.taskServerEnabled=await x(t,m.TASK_SERVER_ENABLED)===`true`,e.taskServerUrl=await x(t,m.TASK_SERVER_URL)||(await import(`./config-CrHchneq.js`).then(e=>e.k)).DEFAULT_TASK_SERVER_URL}async function jn(e,t){let n=await x(t,m.LLAMAFILE_MODE);n===`cli`||n===`server`?e.llamafileMode=n:await J(t,m.LLAMAFILE_MODE,e.llamafileMode);let r=await x(t,m.LLAMAFILE_HOST);r&&(e.llamafileHost=r);let i=await x(t,m.LLAMAFILE_PORT);if(i){let t=parseInt(i,10);Number.isFinite(t)&&t>=1&&t<=65535&&(e.llamafilePort=t)}await x(t,m.LLAMAFILE_OFFLINE)===`false`&&(e.llamafileOffline=!1),de(e);let a=await x(t,m.MESH_LLM_HOST);a&&(e.meshLlmHost=a),A(e)}async function Mn(e,t){let[r,a,c,l,u,d,f,p,h]=await Promise.all([x(t,m.PROVIDER),x(t,m.BEDROCK_REGION_FALLBACK),x(t,m.BEDROCK_PROFILE_FALLBACK),x(t,m.BEDROCK_AUTH_MODE),x(t,m.MODEL),x(t,m.MAX_TOKENS),x(t,m.MAX_ITERATIONS),x(t,m.RATE_LIMIT_CALLS_PER_MINUTE),x(t,m.RATE_LIMIT_AUTO_ADAPT)]);r&&n(r)&&(e.provider=r,e.providerConfig=n(r)||i()),await e.loadApiKeyForProvider(t,e.provider),e.bedrockRegionFallback=(a||``).trim(),e.bedrockProfileFallback=(c||``).trim(),e.bedrockAuthMode=(l||`provider_chain`).trim();let g=()=>{let t=()=>{fe(e).then(t=>s.fetchModelInfo(e.providerConfig,t||void 0,ge(e,e.provider)))};typeof globalThis.requestIdleCallback==`function`?globalThis.requestIdleCallback(t,{timeout:5e3}):setTimeout(t,2e3)};typeof window<`u`&&typeof document<`u`?document.readyState===`complete`?g():window.addEventListener(`load`,g,{once:!0}):g(),u?e.model=u:e.model=e.providerConfig.defaultModel;let _=e.model;e.provider===`prompt_api`&&!R()&&(e.model===`browser-built-in`||!e.model)&&(_=await x(t,m.PROMPT_API_FALLBACK_MODEL)||`onnx-community/Qwen3-0.6B-ONNX`,ue(_).catch(()=>{}));let v=o(_);if(d===`8192`||e.provider===`prompt_api`&&!R()&&d===`4096`?e.maxTokens=v:e.maxTokens=Math.max(1,parseInt(d||String(v),10)),f&&(e.maxIterations=parseInt(f,10)||50),p){let t=parseInt(p,10);e.rateLimitCallsPerMinute=Number.isFinite(t)?Math.max(0,t):0}e.rateLimitAutoAdapt=h!==`false`}async function Nn(e,t){e.agentWorker=new Worker(S(new URL(`./agent.worker.js`,import.meta.url).href),{type:`module`}),e.agentWorker.onmessage=n=>re(e,t,n.data),e.agentWorker.onerror=e=>{console.error(`Agent worker error:`,e)};let n=await x(t,m.STORAGE_HANDLE);n&&e.agentWorker.postMessage({payload:{storageHandle:n},type:`set-storage`}),T(e),e.scheduler=new Sn(async t=>{await L(e,t)},()=>{e.events.emit(`task-change`,{type:`executed`})}),oe().then(t=>{t&&e.scheduler?.start()}),En(e,t)}var Pn=class{agentWorker=null;assistantName=g;bedrockAuthMode=`provider_chain`;bedrockProfileFallback=``;bedrockRegionFallback=``;browserChat=new rt;channelEnabledByType={browser:!0,peerjs:!1,telegram:!1,imessage:!1};channelRegistry=new it;contextCompressionEnabled=!1;db=null;directToolCommandPolicy={...$};events=new _n;gitProxyUrl=`/git-proxy`;imessage=new at;imessageApiKey=``;imessageChatIds=[];imessageServerUrl=``;inFlightEffectiveProviderByGroup=new Map;inFlightProviderRequestIds=new Map;inFlightTriggerByGroup=new Map;llamafileHost=`127.0.0.1`;llamafileMode=`cli`;llamafileOffline=!0;llamafilePort=8080;maxIterations=50;maxTokens=h;meshLlmHost=``;messageQueue=[];model=i().defaultModel;peerCompletedContexts=new Set;peerjs=new Qe;peerjsMyAlias=``;peerjsMyPeerId=``;peerjsPeerAliases={};peerjsServerHost=``;peerjsServerPath=``;peerjsServerPort=0;peerjsServerSecure=!0;peerjsTrustedPeerIds=[];pendingScheduledTasks=new Set;processing=!1;promptControllers=new Map;provider=f;providerConfig=i();proxyUrl=`/proxy`;pushSubscriptionWarned=!1;rateLimitAutoAdapt=!0;rateLimitCallsPerMinute=0;reasoningEffort=`none`;roomChannel=new dt;roomManager;router=null;scheduler=null;schedulerTriggeredGroups=new Set;state=`idle`;streamingEnabled=!0;taskServerEnabled=!1;taskServerUrl=`/schedule`;telegram=new pt;telegramBotToken=``;telegramChatIds=[];telegramUseProxy=!1;transformersProgressPollers=new Map;triggerPattern=a(g);useProxy=!1;vmBashFullInternetAccess=!1;vmBootMode=`disabled`;vmStatus={ready:!1,booting:!1,bootAttempted:!1,error:null};webMcpEffectCleanup=null;webMcpRegistrationLock=Promise.resolve();webMcpToolsEnabled=!0;#e=null;#t=null;constructor(){this.roomManager=Dn(this),this.initializeChannelRegistry()}clearProviderRequest(e){this.inFlightProviderRequestIds.delete(e)}createProviderRequestId(e){if(this.provider!==`llamafile`)return this.inFlightProviderRequestIds.delete(e),``;let t=`${e}:${Date.now().toString(36)}:${Math.random().toString(36).slice(2,10)}`;return this.inFlightProviderRequestIds.set(e,t),t}initializeChannelRegistry(){this.channelRegistry=new it,this.channelRegistry.register(`br:`,this.browserChat,{badge:`Browser`,autoTrigger:!0}),this.channelRegistry.register(`tg:`,this.telegram,{badge:`Telegram`,autoTrigger:!1}),this.channelRegistry.register(`im:`,this.imessage,{badge:`iMessage`,autoTrigger:!0}),this.channelRegistry.register(`peer:`,this.peerjs,{badge:`PeerJS`,autoTrigger:!1}),this.channelRegistry.register(`room:`,this.roomChannel,{badge:`Room`,autoTrigger:!1}),this.roomChannel.setManager(this.roomManager),this.peerjs.setRoomNotificationHandler((e,t,n)=>this.roomManager.handleNotification(e,t,n)),this.router=new bt(this.channelRegistry)}setState(e,t){this.state=e,this.events.emit(`state-change`,{state:e,groupId:t})}shutdown(){this.channelRegistry.stopAll(),this.scheduler?.stop();for(let e of this.transformersProgressPollers.keys())Fe(this,e);this.agentWorker?.terminate(),typeof this.webMcpEffectCleanup==`function`&&this.webMcpEffectCleanup(),Ge()}stopCurrentRequest(e=p){if(this.state!==`thinking`&&this.state!==`responding`)return;Fe(this,e);let t=this.inFlightProviderRequestIds.get(e)||``;this.clearProviderRequest(e),this.agentWorker?.postMessage({type:`cancel`,payload:{groupId:e}}),this.provider===`llamafile`&&t&&V(t);let n=this.promptControllers.get(e);n&&(n.abort(),this.promptControllers.delete(e)),this.inFlightTriggerByGroup.delete(e),this.inFlightEffectiveProviderByGroup.delete(e),this.events.emit(`typing`,{groupId:e,typing:!1}),this.router?.setTyping(e,!1),this.setState(`idle`,e)}async getApiKey(){if(!this.#t)return null;let e=Date.now();if(this.#e&&this.#e.expiresAt>e)return this.#e.value;try{let t=await Je(this.#t);return t===null?null:(this.#e={expiresAt:e+3e4,value:t},t)}catch(e){return console.error(`[Orchestrator] Failed to decrypt API key:`,e),null}}async getApiKeyForSpecificProvider(e,t){let n=await x(e,r(t));if(!n&&t===`openrouter`){let t=await x(e,m.API_KEY);t&&(n=t)}if(!n)return``;try{return await Je(n)||``}catch(e){return console.error(`[Orchestrator] Failed to decrypt API key:`,e),``}}async handleCompactDone(e,t,n){await Le(e,t),await le(e,{channel:this.channelRegistry.getChannelType(t)??`browser`,content:`📝 **Context Compacted**\n\n${n}`,groupId:t,id:Y(),isFromMe:!0,isTrigger:!1,sender:this.assistantName,timestamp:Date.now()}),this.events.emit(`context-compacted`,{groupId:t,summary:n}),this.events.emit(`typing`,{groupId:t,typing:!1}),this.setState(`idle`,t)}async init(){let e=await nt();return this.db=e,await Promise.all([kn(this,e),Mn(this,e),jn(this,e),An(this,e)]),await Nn(this,e),On(this,e).then(()=>{this.browserChat.onDisplay(()=>{})}),this.events.emit(`ready`,void 0),await W.load(e),Ke(this,e),e}async loadApiKeyForProvider(e,t){let n=await x(e,r(t));if(!n&&t===`openrouter`){let i=await x(e,m.API_KEY);i&&(n=i,await J(e,r(t),i))}if(!n)this.#t=``;else try{this.#t=n}catch(e){console.warn(`[Orchestrator] Failed to load API key:`,e),this.#t=``}this.#e=null}async loadSecretConfig(e,t){let n=await x(e,t);if(!n)return``;try{return await Je(n)||``}catch{let r=await Ye(n);return r&&await J(e,t,r),n}}async newSession(e,t=p){await Le(e,t),this.events.emit(`session-reset`,{groupId:t})}async refreshContextUsage(e,t=p){let r=``;try{r=await ve(e,t,`MEMORY.md`)}catch{}let i=W.enabledTools,a=q.getPeerState(t)||void 0,s=ne(this.assistantName,r,i,W.systemPromptOverride,a),c=(await Be(e)).find(e=>e.groupId===t),l=c?.pinnedModel??(c?.pinnedProvider?n(c.pinnedProvider)?.defaultModel??this.model:this.model),u=l;(c?.pinnedProvider??this.provider)===`prompt_api`&&(l===`browser-built-in`||!l)&&!R()&&(u=await x(this.db,m.PROMPT_API_FALLBACK_MODEL)||`onnx-community/Qwen3-0.6B-ONNX`);let d=typeof c?.pinnedMaxTokens==`number`&&Number.isFinite(c.pinnedMaxTokens)&&c.pinnedMaxTokens>0?Math.floor(c.pinnedMaxTokens):this.maxTokens,f=Math.max(1,Math.min(d,o(u))),h=U(u),g=te(s)+(i?.reduce((e,t)=>e+te(JSON.stringify(t)),0)??0),_=ce(await Ve(t,200),{contextLimit:h,systemPromptTokens:g,maxOutputTokens:f,skimTop:this.contextCompressionEnabled}),v=_.estimatedTokens+g;if(q.tokenUsage){let e=q.tokenUsage,t=(e.inputTokens||0)+(e.cacheReadTokens||0)+(e.outputTokens||0);v=Math.max(v,t)}this.events.emit(`context-usage`,{estimatedTokens:v,contextLimit:h,usagePercent:Math.min(100,Math.max(_.usagePercent,v/h*100)),truncatedCount:_.truncatedCount})}async restartCurrentRequest(e=p){if(this.state!==`thinking`&&this.state!==`responding`||!this.db)return!1;let t=this.inFlightTriggerByGroup.get(e);return t?(this.stopCurrentRequest(e),await fn(this,this.db,e,t),!0):!1}async routeRoomA2UIAction(e,t){let n=_e(e),r=this.peerjs.myPeerId||this.peerjsMyPeerId,i=this.roomManager.getSurfaceOwner(t.surfaceId);if(!i||i===r){if(!this.db)return;await hn(this,this.db,{id:Y(),groupId:e,sender:this.peerjsMyAlias||r||`you`,content:yt(t),timestamp:Date.now(),channel:`room`,a2uiAction:t});return}this.roomManager.broadcastA2UIAction(n,t)}async saveSecretConfig(e,t,n){if(!n){await J(e,t,``);return}let r=await Ye(n);if(!r)throw Error(`Failed to encrypt secret config for ${t}`);await J(e,t,r)}async setApiKey(e,t){this.#t=await Ye(t),this.#e=null;let n=await Ye(t);if(!n)throw Error(`key failed to encrypt. config cannot set.`);await J(e,r(this.provider),n)}submitMessage(e,t=p){this.browserChat.submit(e,t)}};export{Pn as Orchestrator};
@@ -51,10 +51,10 @@ on:
51
51
  branches: [main]
52
52
  workflow_dispatch:
53
53
  inputs:
54
- shadowclaw_ref:
55
- description: "ShadowClaw version tag (e.g. v1.20.0), commit SHA, or branch"
54
+ shadowclaw_version:
55
+ description: "ShadowClaw npm version (e.g. latest, 1.23.3)"
56
56
  required: false
57
- default: ""
57
+ default: "latest"
58
58
 
59
59
  permissions:
60
60
  contents: read
@@ -66,87 +66,33 @@ concurrency:
66
66
  cancel-in-progress: true
67
67
 
68
68
  jobs:
69
- build:
69
+ build-and-deploy:
70
+ environment:
71
+ name: github-pages
72
+ url: ${{ steps.deployment.outputs.page_url }}
70
73
  runs-on: ubuntu-latest
71
74
  steps:
72
- - name: Checkout content repo
73
- uses: actions/checkout@v4
74
- with:
75
- path: content
75
+ - name: Checkout repository
76
+ uses: actions/checkout@v6
76
77
 
77
- - name: Resolve ShadowClaw version / ref
78
- id: resolve-ref
79
- shell: bash
80
- run: |
81
- REF="${{ github.event.inputs.shadowclaw_ref }}"
82
- if [ -z "$REF" ]; then
83
- REF=$(node -e '
84
- try {
85
- const fs = require("fs");
86
- const configPath = "content/site-config.json";
87
- if (fs.existsSync(configPath)) {
88
- const cfg = JSON.parse(fs.readFileSync(configPath, "utf8"));
89
- const v = cfg.shadowClawVersion || cfg.shadowClawRef || cfg.shadowclawVersion || cfg.shadowclawRef || "";
90
- if (v) process.stdout.write(String(v).trim());
91
- }
92
- } catch (e) {
93
- process.stdout.write("");
94
- }
95
- ')
96
- fi
97
- if [ -z "$REF" ] && [ -f "content/.shadowclaw-version" ]; then
98
- REF=$(tr -d ' \n\r\t' < content/.shadowclaw-version)
99
- fi
100
- if [ -z "$REF" ]; then
101
- REF="${SHADOWCLAW_REF:-main}"
102
- fi
103
- echo "Resolved ShadowClaw ref: $REF"
104
- echo "ref=$REF" >> "$GITHUB_OUTPUT"
105
-
106
- - name: Checkout ShadowClaw core (build toolchain)
107
- uses: actions/checkout@v4
108
- with:
109
- repository: xt-ml/shadow-claw
110
- path: shadow-claw
111
- ref: ${{ steps.resolve-ref.outputs.ref }}
112
-
113
- - uses: actions/setup-node@v4
78
+ - name: Setup Node.js
79
+ uses: actions/setup-node@v6
114
80
  with:
115
81
  node-version: "24"
116
- cache: npm
117
- cache-dependency-path: shadow-claw/package-lock.json
118
-
119
- - name: Install ShadowClaw build dependencies
120
- run: npm ci
121
- working-directory: shadow-claw
122
82
 
123
- - name: Inject content into ShadowClaw build root
124
- run: |
125
- rm -rf shadow-claw/pages
126
- if [ -d "content/pages" ]; then cp -r content/pages shadow-claw/; fi
127
- if [ -f "content/site-config.json" ]; then cp content/site-config.json shadow-claw/; fi
128
- mkdir -p shadow-claw/pages
129
-
130
- - name: Build production bundle
131
- working-directory: shadow-claw
83
+ - name: Build static site via ShadowClaw CLI
132
84
  env:
133
- NODE_ENV: production
134
85
  PAGES_ORIGIN: "https://${{ github.repository_owner }}.github.io/${{ github.event.repository.name }}/"
135
86
  PAGES_BASE_PATH: "/${{ github.event.repository.name }}/"
136
- run: npm run build:prod
87
+ run: |
88
+ VERSION="${{ github.event.inputs.shadowclaw_version || 'latest' }}"
89
+ npx --yes "shadow-claw@$VERSION" build --prod
137
90
 
138
91
  - name: Upload Pages artifact
139
92
  uses: actions/upload-pages-artifact@v3
140
93
  with:
141
- path: shadow-claw/dist/public
94
+ path: dist/public
142
95
 
143
- deploy:
144
- needs: build
145
- runs-on: ubuntu-latest
146
- environment:
147
- name: github-pages
148
- url: ${{ steps.deployment.outputs.page_url }}
149
- steps:
150
96
  - name: Deploy to GitHub Pages
151
97
  id: deployment
152
98
  uses: actions/deploy-pages@v4
@@ -154,18 +100,7 @@ jobs:
154
100
 
155
101
  ### Version Pinning
156
102
 
157
- You can pin your template site to a specific ShadowClaw release tag (e.g. `v1.20.0`) or git commit SHA (e.g. `62253c53`) to ensure reproducible builds:
158
-
159
- - **In `site-config.json` at the repository root**:
160
-
161
- ```json
162
- {
163
- "shadowClawVersion": "v1.20.0"
164
- }
165
- ```
166
-
167
- - **In `.shadowclaw-version`**: Create a file named `.shadowclaw-version` in the root of your content repository containing the version tag or commit SHA.
168
- - **Workflow Dispatch**: Run the workflow manually from GitHub Actions UI and supply any tag, SHA, or branch in the `shadowclaw_ref` input.
103
+ ShadowClaw builds via `npx --yes shadow-claw@latest build --prod`. To pin to a specific npm release (e.g. `1.23.3`), specify the version in `.github/workflows/deploy-pages.yml` or supply `shadowclaw_version` when triggering the GitHub Actions workflow manually.
169
104
 
170
105
  For a **custom apex domain** (e.g. `example.com`), override:
171
106
 
@@ -1 +1 @@
1
- import"./config-AL77QAx4.js";import{n as e}from"./txPromise-EBECky1b.js";import{n as t}from"./toast-60iDlgiH.js";import{Zt as n,bn as r,cn as i,nn as a}from"./orchestrator-B9KmIj8Q.js";import{t as o}from"./ulid-BY7rQVLN.js";const s={apng:`image/apng`,avif:`image/avif`,bmp:`image/bmp`,css:`text/css`,gif:`image/gif`,htm:`text/html`,html:`text/html`,jpeg:`image/jpeg`,jpg:`image/jpeg`,js:`text/javascript`,json:`application/json`,markdown:`text/markdown`,md:`text/markdown`,mp3:`audio/mpeg`,mp4:`video/mp4`,oga:`audio/ogg`,ogg:`audio/ogg`,ogv:`video/ogg`,pdf:`application/pdf`,png:`image/png`,svg:`image/svg+xml`,txt:`text/plain`,wav:`audio/wav`,webm:`video/webm`,webp:`image/webp`};function c(e){return e.trim().replace(/[\u0000-\u001f\u007f]+/g,``).replace(/[\\/:*?"<>|]+/g,`-`).replace(/\s+/g,` `).replace(/^\.+/,``).trim()||`attachment`}function l(e,t=``){if(t)return t;let n=e.toLowerCase().split(`.`).pop()||``;return s[n]||`application/octet-stream`}function u(e){return e.previewDisposition===`inline`||l(e.fileName,e.mimeType||``)===`image/png`}function d(e,t=``,n=Date.now()){let r=c(e),i=t.trim().replace(/[^a-zA-Z0-9._-]+/g,`-`).replace(/^-+|-+$/g,``);return`attachments/${n}-${i?`${i}-`:``}${r}`}async function f(e,t,n=[]){let r=[];for(let a of n){let n=c(a.fileName||``),o=a.path||d(n,a.id),s=a.source,u=l(n,a.mimeType||``),f=a.size;if(s){let r=await p(s);u=l(n,a.mimeType||r.type),f=typeof f==`number`?f:r.size,await i(e,t,o,r)}r.push(m({...a,fileName:n,mimeType:u,size:f,path:o,previewDisposition:a.previewDisposition||(u===`image/png`?`inline`:`file`)}))}return r}async function p(e){if(e.kind===`local-file`)return e.file;if(e.kind===`inline-text`)return new Blob([e.text],{type:e.mimeType||`text/plain`});let t=await fetch(e.url,{headers:e.headers});if(!t.ok)throw Error(`Attachment download failed: HTTP ${t.status} ${t.statusText}`);return await t.blob()}function m(e){let{source:t,...n}=e;return n}const h=new Set([`TASK_STATE_COMPLETED`,`TASK_STATE_FAILED`,`TASK_STATE_CANCELED`,`TASK_STATE_REJECTED`]),g=new Set([`TASK_STATE_INPUT_REQUIRED`,`TASK_STATE_AUTH_REQUIRED`]),_={SEND_MESSAGE:`SendMessage`,SEND_STREAMING_MESSAGE:`SendStreamingMessage`,GET_TASK:`GetTask`,CANCEL_TASK:`CancelTask`,GET_AGENT_CARD:`GetAgentCard`},v={EVENT:`agui/event`},y={STATUS_UPDATE:`tasks/statusUpdate`,ARTIFACT_UPDATE:`tasks/artifactUpdate`},b={JOIN:`room/join`,ROSTER:`room/roster`,LEAVE:`room/leave`,MESSAGE:`room/message`,INVITE:`room/invite`,RELAY:`room/relay`,A2UI:`room/a2ui`,A2UI_ACTION:`room/a2ui-action`},x={TASK_NOT_FOUND:-32001,TASK_NOT_CANCELABLE:-32002,PUSH_NOTIFICATION_NOT_SUPPORTED:-32003,UNSUPPORTED_OPERATION:-32004,CONTENT_TYPE_NOT_SUPPORTED:-32005,INVALID_AGENT_RESPONSE:-32006,PARSE_ERROR:-32700,INVALID_REQUEST:-32600,METHOD_NOT_FOUND:-32601,INVALID_PARAMS:-32602,INTERNAL_ERROR:-32603};function S(e){return typeof e==`object`&&!!e&&e.jsonrpc===`2.0`&&typeof e.id==`string`&&typeof e.method==`string`}function C(e){return typeof e==`object`&&!!e&&e.jsonrpc===`2.0`&&typeof e.id==`string`&&!(`method`in e)}function w(e){let t={url:`webrtc://${e.peerId}`,protocolBinding:`https://xt-ml.github.io/shadow-claw/bindings/webrtc-datachannel/v1`,protocolVersion:`1.0`},n={streaming:e.streaming??!0,pushNotifications:!1};return{name:e.name,description:e.description??`AI assistant agent (${e.name})`,version:e.version??`1.0.0`,supportedInterfaces:[t],capabilities:n,defaultInputModes:[`text/plain`,`application/json`],defaultOutputModes:[`text/plain`,`application/json`],skills:e.skills??[],iconUrl:e.iconUrl}}function T(){return{jsonrpc:`2.0`,id:o(),method:_.GET_AGENT_CARD}}function E(e,t){return{jsonrpc:`2.0`,id:e,result:t}}function D(e){if(e.error)return console.warn(`GetAgentCard returned error:`,e.error),null;let t=e.result;return!t||typeof t!=`object`||typeof t.name!=`string`||typeof t.version!=`string`?null:t}var O=class{_cards=new Map;clear(){this._cards.clear()}delete(e){this._cards.delete(e)}entries(){return this._cards.entries()}get(e){return this._cards.get(e)}getDisplayName(e){return this._cards.get(e)?.name??e}has(e){return this._cards.has(e)}set(e,t){this._cards.set(e,t)}supportsStreaming(e){return this._cards.get(e)?.capabilities?.streaming??!1}};const k={maxTasks:100,maxHistoryPerTask:50},A=new Map([[`TASK_STATE_SUBMITTED`,new Set([`TASK_STATE_WORKING`,`TASK_STATE_REJECTED`,`TASK_STATE_CANCELED`,`TASK_STATE_FAILED`])],[`TASK_STATE_WORKING`,new Set([`TASK_STATE_COMPLETED`,`TASK_STATE_FAILED`,`TASK_STATE_CANCELED`,`TASK_STATE_INPUT_REQUIRED`,`TASK_STATE_AUTH_REQUIRED`])],[`TASK_STATE_INPUT_REQUIRED`,new Set([`TASK_STATE_WORKING`,`TASK_STATE_CANCELED`,`TASK_STATE_FAILED`])],[`TASK_STATE_AUTH_REQUIRED`,new Set([`TASK_STATE_WORKING`,`TASK_STATE_CANCELED`,`TASK_STATE_FAILED`])]]);var j=class{_config;_contextActiveTask=new Map;_listeners=new Set;_taskOrder=[];_tasks=new Map;constructor(e){this._config={...k,...e}}addArtifact(e,t,n=!1){let r=this._tasks.get(e);if(!r)return;if(r.artifacts||=[],n){let e=r.artifacts.find(e=>e.artifactId===t.artifactId);e?e.parts.push(...t.parts):r.artifacts.push(t)}else r.artifacts.push(t);let i={taskId:r.id,contextId:r.contextId??``,artifact:t,append:n,lastChunk:!1};this._emit({type:`artifactUpdate`,taskId:r.id,contextId:r.contextId??``,payload:i})}cancelTask(e){let t=this._tasks.get(e);return!t||h.has(t.status.state)?!1:(this._transitionState(t,`TASK_STATE_CANCELED`,{messageId:o(),role:`ROLE_AGENT`,parts:[{text:`Task canceled by peer request.`}]}),!0)}clear(){this._tasks.clear(),this._taskOrder=[],this._contextActiveTask.clear()}emitRunError(e,t,n){let r=this._tasks.get(e);if(!r)return;let i={type:`RUN_ERROR`,message:t,code:n,timestamp:Date.now()};this._emitAGUI(r,i)}emitRunFinished(e){let t=this._tasks.get(e);if(!t)return;let n={type:`RUN_FINISHED`,threadId:t.contextId??t.id,runId:t.id,timestamp:Date.now()};this._emitAGUI(t,n)}emitRunStarted(e){let t=this._tasks.get(e);if(!t)return;let n={type:`RUN_STARTED`,threadId:t.contextId??t.id,runId:t.id,timestamp:Date.now()};this._emitAGUI(t,n)}emitStateSnapshot(e,t){let n=this._tasks.get(e);if(!n)return;let r={type:`STATE_SNAPSHOT`,snapshot:t,timestamp:Date.now()};this._emitAGUI(n,r)}emitTextMessageContent(e,t,n){let r=this._tasks.get(e);if(!r)return;let i={type:`TEXT_MESSAGE_CONTENT`,messageId:t,delta:n,timestamp:Date.now()};this._emitAGUI(r,i)}emitTextMessageEnd(e,t){let n=this._tasks.get(e);if(!n)return;let r={type:`TEXT_MESSAGE_END`,messageId:t,timestamp:Date.now()};this._emitAGUI(n,r)}emitTextMessageStart(e,t,n=`assistant`){let r=this._tasks.get(e);if(!r)return;let i={type:`TEXT_MESSAGE_START`,messageId:t,role:n,timestamp:Date.now()};this._emitAGUI(r,i)}emitToolCallEnd(e,t){let n=this._tasks.get(e);if(!n)return;let r={type:`TOOL_CALL_END`,toolCallId:t,timestamp:Date.now()};this._emitAGUI(n,r)}emitToolCallStart(e,t,n,r){let i=this._tasks.get(e);if(!i)return;let a={type:`TOOL_CALL_START`,toolCallId:t,toolCallName:n,parentMessageId:r,timestamp:Date.now()};this._emitAGUI(i,a)}getActiveTaskForContext(e){let t=this._contextActiveTask.get(e);if(t)return this._tasks.get(t)}getActiveTasks(){return Array.from(this._tasks.values()).filter(e=>!h.has(e.status.state))}getAllTasks(){return Array.from(this._tasks.values())}getTask(e){return this._tasks.get(e)}handleSendMessage(e){let t=e.message,n=t.contextId??o(),r=t.taskId??this._contextActiveTask.get(n);return r&&this._tasks.has(r)?this._continueTask(r,t):this._createTask(n,t)}markCompleted(e,t){let n=this._tasks.get(e);n&&this._transitionState(n,`TASK_STATE_COMPLETED`,t)}markFailed(e,t){let n=this._tasks.get(e);n&&this._transitionState(n,`TASK_STATE_FAILED`,{messageId:o(),role:`ROLE_AGENT`,parts:[{text:t}]})}markInputRequired(e,t){let n=this._tasks.get(e);n&&this._transitionState(n,`TASK_STATE_INPUT_REQUIRED`,t)}markWorking(e){let t=this._tasks.get(e);t&&this._transitionState(t,`TASK_STATE_WORKING`)}off(e){this._listeners.delete(e)}on(e){this._listeners.add(e)}_addTask(e){for(this._tasks.set(e.id,e),this._taskOrder.push(e.id);this._taskOrder.length>this._config.maxTasks;){let e=this._taskOrder.shift(),t=this._tasks.get(e);if(t&&h.has(t.status.state))this._tasks.delete(e),t.contextId&&this._contextActiveTask.delete(t.contextId);else{this._taskOrder.unshift(e);break}}}_continueTask(e,t){let n=this._tasks.get(e);return g.has(n.status.state)&&this._transitionState(n,`TASK_STATE_WORKING`),n.history||=[],n.history.push(t),n.history.length>this._config.maxHistoryPerTask&&(n.history=n.history.slice(-this._config.maxHistoryPerTask)),this._touchTask(e),{task:n}}_createTask(e,t){let n=o(),r={id:n,contextId:e,status:{state:`TASK_STATE_SUBMITTED`,timestamp:new Date().toISOString()},history:[t]};return this._addTask(r),this._contextActiveTask.set(e,n),this._emitStatusUpdate(r),{task:r}}_emit(e){for(let t of this._listeners)try{t(e)}catch(e){console.error(`PeerTaskManager: listener error`,e)}}_emitAGUI(e,t){this._emit({type:`aguiEvent`,taskId:e.id,contextId:e.contextId??``,payload:t})}_emitStatusUpdate(e){let t={taskId:e.id,contextId:e.contextId??``,status:e.status};this._emit({type:`statusUpdate`,taskId:e.id,contextId:e.contextId??``,payload:t})}_touchTask(e){let t=this._taskOrder.indexOf(e);t!==-1&&(this._taskOrder.splice(t,1),this._taskOrder.push(e))}_transitionState(e,t,n){let r=e.status.state,i=A.get(r);if(i&&!i.has(t)){console.warn(`PeerTaskManager: invalid transition ${r} → ${t} for task ${e.id}`);return}if(h.has(r)){console.warn(`PeerTaskManager: task ${e.id} is in terminal state ${r}, cannot transition`);return}e.status={state:t,message:n,timestamp:new Date().toISOString()},h.has(t)&&e.contextId&&this._contextActiveTask.get(e.contextId)===e.id&&this._contextActiveTask.delete(e.contextId),this._emitStatusUpdate(e)}};const M=new t.State(null);var N=class i{static _RECONNECT_BASE_MS=1e3;static _RECONNECT_MAX_MS=6e4;connectedPeersSignal=new t.State([]);connections=new Map;messageCallback=null;myPeerId=``;peerCards=new O;running=!1;serverConfig={};transferProgressSignal=M;trustedPeerIds=new Set;type=`peerjs`;typingCallback=null;_inboundRemap=new Map;_localCard=null;_onTaskCompleteCallback=null;peer=null;_pendingInboundFile=null;_pendingRequests=new Map;_reconnectAttempts=0;_reconnectTimer=null;_roomNotificationHandler=null;_taskManagers=new Map;constructor(){typeof globalThis.addEventListener==`function`&&globalThis.addEventListener(`peerjs-dc-handle-chunk`,e=>{if(e?.detail?.chunkInfo){let{count:t,total:n}=e.detail.chunkInfo,r=t+1;M.set({count:r,total:n,direction:`receive`}),r>=n&&setTimeout(()=>M.set(null),1500)}})}completeActiveTask(e){let t=e.replace(/^peer:/,``),n=this._taskManagers.get(t);if(!n)return!1;let r=n.getActiveTasks();if(r.length===0)return!1;let i=r[r.length-1];return n.markWorking(i.id),n.markCompleted(i.id),!0}configure(e,t,n={}){this.myPeerId=e.trim(),this.trustedPeerIds=new Set(t.map(e=>e.trim()).filter(Boolean)),this.serverConfig=n}connectPeer(e){this._getOrOpenConnection(e)}getTaskManager(e){let t=this._taskManagers.get(e);return t||(t=new j,this._taskManagers.set(e,t),t.on(t=>{this._forwardTaskEvent(e,t)})),t}isConfigured(){return this.myPeerId.length>0}isPeerConnected(e){let t=this.connections.get(e);return!!t&&!!t.open}onMessage(e){this.messageCallback=e}onTaskComplete(e){this._onTaskCompleteCallback=e}onTyping(e){this.typingCallback=e}sendRoomNotification(e,t){let n=this._getOrOpenConnection(e);if(!n)return!1;if(n.open)try{return n.send(t),!0}catch(t){return console.error(`PeerJsChannel: failed to send room notification to ${e}:`,t),!1}return this._waitForOpen(n).then(()=>{try{n.send(t)}catch(t){console.error(`PeerJsChannel: failed to flush room notification to ${e}:`,t)}}).catch(()=>{}),!0}setRoomNotificationHandler(e){this._roomNotificationHandler=e}setTyping(e,t){let n=e.replace(/^peer:/,``),r=this.connections.get(n);if(r)try{r.send({type:`typing`,typing:t})}catch{}}start(){!this.isConfigured()||this.running||(this.running=!0,this._localCard=w({peerId:this.myPeerId,name:this.myPeerId,description:`ShadowClaw AI assistant`,streaming:!0}),this._initPeer())}stop(){if(this.running=!1,this._reconnectTimer!==null&&(clearTimeout(this._reconnectTimer),this._reconnectTimer=null),this._reconnectAttempts=0,this.connections.forEach(e=>{try{e.close()}catch{}}),this.connections.clear(),this.peerCards.clear(),this._taskManagers.clear(),this._pendingRequests.forEach(({timer:e})=>clearTimeout(e)),this._pendingRequests.clear(),this._updateConnectedPeersSignal(),this.peer){try{this.peer.destroy()}catch{}this.peer=null}}async send(t,n,i){let a=t.replace(/^peer:/,``),s=this._getOrOpenConnection(a);if(!s){console.warn(`PeerJsChannel: no connection available for ${a}`);return}try{await this._waitForOpen(s);let{attachments:a}=P(n),c=[...(i||[]).filter(e=>!!e.path),...a.map(e=>({path:e.path,mimeType:`application/octet-stream`}))],l=new Map,u=[];try{let n=await e();for(let e of c)if(e.path)try{let i=await r(n,t,e.path),a=e.path;l.has(e.path)||l.set(e.path,a);let o=e.mimeType||`application/octet-stream`;s.send({type:`__file_header`,name:a,mimeType:o,size:i.length});let c=this._trackSendProgress(s,i.length);s.send(i.buffer),await c,u.push({kind:`file`,name:a,mimeType:o,size:i.length})}catch(t){console.warn(`PeerJsChannel: failed to process file ${e.path}:`,t)}}catch(e){console.error(`PeerJsChannel: failed to access DB`,e)}let d=n;for(let[e,t]of l){let n=e.replace(/[.*+?^${}()|[\]\\]/g,`\\$&`);d=d.replace(RegExp(`(!?\\[.*?\\])\\(${n}\\)`,`g`),`$1(${t})`)}let f=[...u,{kind:`text`,text:d}],p=o(),m={jsonrpc:`2.0`,id:p,method:_.SEND_MESSAGE,params:{message:{messageId:o(),role:`ROLE_AGENT`,parts:f}}};s.send(m),this._registerPendingRequest(p,e=>{})}catch(e){console.error(`PeerJsChannel: send failed for ${a}:`,e),window.dispatchEvent(new CustomEvent(`shadow-claw-peer-error`,{detail:{remotePeerId:a,error:e instanceof Error?e.message:String(e)}}))}}async sendA2UI(e,t){let n=e.replace(/^peer:/,``),r=this._getOrOpenConnection(n);if(!r){console.warn(`PeerJsChannel.sendA2UI: no connection available for ${n}`);return}try{await this._waitForOpen(r);let e={jsonrpc:`2.0`,method:`message/send`,id:o(),params:{message:{role:`agent`,parts:[{kind:`a2ui`,envelope:t}]}}};r.send(e)}catch(e){console.error(`PeerJsChannel.sendA2UI: failed for ${n}:`,e)}}async sendA2UIAction(e,t){let n=e.replace(/^peer:/,``),r=this._getOrOpenConnection(n);if(!r){console.warn(`PeerJsChannel.sendA2UIAction: no connection available for ${n}`);return}try{await this._waitForOpen(r);let e={jsonrpc:`2.0`,method:`message/send`,id:o(),params:{message:{role:`user`,parts:[{kind:`a2ui-action`,action:t}]}}};r.send(e)}catch(e){console.error(`PeerJsChannel.sendA2UIAction: failed for ${n}:`,e)}}_exchangeAgentCards(e,t){if(!this._localCard)return;let n=T();t.send(n),this._registerPendingRequest(n.id,t=>{let n=D(t);n&&(this.peerCards.set(e,n),console.log(`PeerJsChannel: received AgentCard from ${e}: "${n.name}"`))})}_forwardTaskEvent(e,t){let n=this.connections.get(e);if(!n)return;let r;switch(t.type){case`statusUpdate`:r=y.STATUS_UPDATE;break;case`artifactUpdate`:r=y.ARTIFACT_UPDATE;break;case`aguiEvent`:r=v.EVENT;break;default:return}let i={jsonrpc:`2.0`,method:r,params:t.payload};try{n.send(i)}catch(n){console.error(`PeerJsChannel: failed to forward ${t.type} to ${e}:`,n)}}_getOrOpenConnection(e){let t=this.connections.get(e);if(t)return t;if(!this.peer)return null;try{let t=this.peer.connect(e,{reliable:!0,serialization:`binary`});return t?(this._handleIncomingConnection(t),t):null}catch(t){return console.error(`PeerJsChannel: failed to connect to ${e}:`,t),null}}_handleA2ACancelTask(e,t){let n=t.params;if(!n?.taskId){this._sendJsonRpcError(e,t.id,{code:x.INVALID_PARAMS,message:`Missing taskId in CancelTask params`});return}let r=this._taskManagers.get(e);if(!r){this._sendJsonRpcError(e,t.id,{code:x.TASK_NOT_FOUND,message:`Task not found: ${n.taskId}`});return}if(!r.cancelTask(n.taskId)){this._sendJsonRpcError(e,t.id,{code:x.TASK_NOT_CANCELABLE,message:`Task not cancelable: ${n.taskId}`});return}let i=this.connections.get(e);if(i){let e=r.getTask(n.taskId),a={jsonrpc:`2.0`,id:t.id,result:{task:e}};i.send(a)}}_handleA2AGetTask(e,t){let n=t.params;if(!n?.taskId){this._sendJsonRpcError(e,t.id,{code:x.INVALID_PARAMS,message:`Missing taskId in GetTask params`});return}let r=this._taskManagers.get(e)?.getTask(n.taskId);if(!r){this._sendJsonRpcError(e,t.id,{code:x.TASK_NOT_FOUND,message:`Task not found: ${n.taskId}`});return}let i=this.connections.get(e);if(i){let e={jsonrpc:`2.0`,id:t.id,result:{task:r}};i.send(e)}}_handleA2AJsonRpc(e,t){if(C(t)){let e=this._pendingRequests.get(t.id);e&&(clearTimeout(e.timer),this._pendingRequests.delete(t.id),e.resolve(t));return}let n=t.method;if(n){if(n.startsWith(`room/`)){this._roomNotificationHandler?.(e,n,t.params);return}if(S(t)){switch(n){case _.GET_AGENT_CARD:this._handleGetAgentCard(e,t);break;case _.SEND_MESSAGE:this._handleA2ASendMessage(e,t);break;case _.CANCEL_TASK:this._handleA2ACancelTask(e,t);break;case _.GET_TASK:this._handleA2AGetTask(e,t);break;default:if(n===`message/send`){let n=t.params;n?.message?.parts&&Array.isArray(n.message.parts)&&this._processInboundA2AEnvelope(e,n.message.parts).catch(e=>console.error(`PeerJsChannel: failed to process inbound A2A envelope`,e))}else this._sendJsonRpcError(e,t.id,{code:x.METHOD_NOT_FOUND,message:`Method not found: ${n}`})}return}switch(n){case v.EVENT:this._handleAGUIEventNotification(e,t.params);break;case y.STATUS_UPDATE:this._handleTaskStatusNotification(e,t.params);break;case y.ARTIFACT_UPDATE:break;case`message/send`:{let n=t.params;n?.message?.parts&&Array.isArray(n.message.parts)&&this._processInboundA2AEnvelope(e,n.message.parts).catch(e=>console.error(`PeerJsChannel: failed to process inbound A2A envelope`,e))}break}}}_handleA2ASendMessage(e,t){let n=t.params;if(!n?.message){this._sendJsonRpcError(e,t.id,{code:x.INVALID_PARAMS,message:`Missing message in SendMessage params`});return}let r=this._taskManagers.get(e);r||(r=new j,this._taskManagers.set(e,r),r.on(t=>{this._forwardTaskEvent(e,t)}));let i=r.handleSendMessage(n),a=this.connections.get(e);if(a){let e={jsonrpc:`2.0`,id:t.id,result:i};a.send(e)}n.message.parts&&Array.isArray(n.message.parts)&&this._processInboundA2AEnvelope(e,n.message.parts,i.task?.id,i.task?.contextId).catch(e=>console.error(`PeerJsChannel: failed to process A2A SendMessage`,e))}_handleAGUIEventNotification(e,t){if(!t||typeof t!=`object`)return;let n=t;window.dispatchEvent(new CustomEvent(`shadow-claw-agui-event`,{detail:{remotePeerId:e,event:n}}))}_handleGetAgentCard(e,t){if(!this._localCard){this._sendJsonRpcError(e,t.id,{code:x.INTERNAL_ERROR,message:`Agent card not available`});return}let n=E(t.id,this._localCard),r=this.connections.get(e);r&&r.send(n)}_handleInboundData(t,r){if(r instanceof ArrayBuffer||r instanceof Uint8Array){if(this._pendingInboundFile){let i=this._pendingInboundFile.name,o=`peer:${t}`,s=new Uint8Array(r);e().then(async e=>{let t=i,r=1;for(;await a(e,o,t);){let e=i.lastIndexOf(`.`);if(e>0&&e<i.length-1){let n=i.substring(0,e),a=i.substring(e);t=`${n} (${r})${a}`}else t=`${i} (${r})`;r++}t!==i&&this._inboundRemap.set(i,t),n(e,o,t,s).catch(e=>{console.error(`PeerJsChannel: failed to write inbound file bytes`,e)})}),this._pendingInboundFile=null}return}if(!r||typeof r!=`object`)return;let i=r;if(i.type===`__file_header`){this._pendingInboundFile={name:i.name,mimeType:i.mimeType,size:i.size};return}if(i.type===`typing`){this.typingCallback?.(`peer:${t}`,!!i.typing);return}if(i.type===`chat`){let e=typeof i.text==`string`?i.text.trim():``;e&&this.messageCallback?.({id:o(),groupId:`peer:${t}`,sender:t,content:e,timestamp:Date.now(),channel:`peerjs`});return}if(i.jsonrpc===`2.0`){this._handleA2AJsonRpc(t,i);return}}_handleIncomingConnection(e){let t=e.peer;if(this.trustedPeerIds.size>0&&!this.trustedPeerIds.has(t)){console.warn(`PeerJsChannel: rejecting connection from untrusted peer: ${t}`);try{e.close()}catch{}return}this.connections.set(t,e),this._updateConnectedPeersSignal(),e.on(`open`,()=>{console.log(`PeerJsChannel: connection opened with ${t}`),this._exchangeAgentCards(t,e)}),e.on(`data`,e=>{this._handleInboundData(t,e)}),e.on(`close`,()=>{console.log(`PeerJsChannel: connection closed with ${t}`),this.connections.delete(t),this.peerCards.delete(t),this._taskManagers.delete(t),this._updateConnectedPeersSignal()}),e.on(`error`,e=>{console.error(`PeerJsChannel: connection error with ${t}:`,e),this.connections.delete(t),this._updateConnectedPeersSignal(),window.dispatchEvent(new CustomEvent(`shadow-claw-peer-error`,{detail:{remotePeerId:t,error:e.message}}))}),e.on(`iceStateChanged`,e=>{(e===`failed`||e===`disconnected`||e===`closed`)&&(console.warn(`PeerJsChannel: ICE state changed to ${e} for ${t}`),this.connections.delete(t),this._updateConnectedPeersSignal())})}_handleTaskStatusNotification(e,t){if(!t||typeof t!=`object`)return;let n=t;if(n.status?.state&&h.has(n.status.state)){let t=`peer:${e}`;this._onTaskCompleteCallback?.(t)}window.dispatchEvent(new CustomEvent(`shadow-claw-task-status`,{detail:{remotePeerId:e,...n}}))}async _initPeer(){let{Peer:e}=await import(`./bundler-Ca-Xwln8.js`),t={},n=this.serverConfig.host??`0.peerjs.com`,r=this.serverConfig.port??443,i=this.serverConfig.path??`/`,a=this.serverConfig.secure??!0;t.host=n,t.port=r,t.path=i,t.secure=a,t.debug=3,t.config={iceServers:this.serverConfig.iceServers??[{urls:`stun:stun.l.google.com:19302`},{urls:`stun:stun1.l.google.com:19302`}]},this.peer=new e(this.myPeerId,t),this.peer.on(`open`,e=>{console.log(`PeerJsChannel: connected to signaling server, peer ID: ${e}`),this._reconnectAttempts=0}),this.peer.on(`connection`,e=>{this._handleIncomingConnection(e)}),this.peer.on(`error`,e=>{console.error(`PeerJsChannel: peer error [${e.type}]:`,e.message),(e.type===`peer-unavailable`||e.type===`unavailable-id`)&&window.dispatchEvent(new CustomEvent(`shadow-claw-peer-error`,{detail:{error:e.message}})),!(e.type===`peer-unavailable`||e.type===`network`||e.type===`disconnected`)&&e.type===`unavailable-id`&&console.warn(`PeerJsChannel: peer ID is already in use. Stop the channel and reconfigure.`)}),this.peer.on(`disconnected`,()=>{this.running&&this._scheduleReconnect()}),this.peer.on(`close`,()=>{console.log(`PeerJsChannel: peer closed`)})}_registerPendingRequest(e,t){let n=setTimeout(()=>{this._pendingRequests.delete(e),console.warn(`PeerJsChannel: JSON-RPC request ${e} timed out (10s)`)},1e4);this._pendingRequests.set(e,{resolve:t,timer:n})}_scheduleReconnect(){if(this._reconnectTimer!==null)return;let e=Math.min(i._RECONNECT_BASE_MS*2**this._reconnectAttempts,i._RECONNECT_MAX_MS);this._reconnectAttempts++,console.log(`PeerJsChannel: disconnected from signaling server, reconnecting in ${(e/1e3).toFixed(1)}s (attempt ${this._reconnectAttempts})`),this._reconnectTimer=setTimeout(()=>{if(this._reconnectTimer=null,this.running)try{this.peer?.reconnect?.()}catch{this._scheduleReconnect()}},e)}_sendJsonRpcError(e,t,n){let r=this.connections.get(e);if(!r)return;let i={jsonrpc:`2.0`,id:t,error:n};try{r.send(i)}catch{}}_trackSendProgress(e,t){return new Promise(n=>{let r=e._dc;if(!r||t===0){this.transferProgressSignal.set(null),n();return}this.transferProgressSignal.set({count:0,total:t,direction:`send`});let i=setInterval(()=>{let e=r.bufferedAmount,a=Math.max(0,t-e);this.transferProgressSignal.set({count:a,total:t,direction:`send`}),e===0&&(clearInterval(i),this.transferProgressSignal.set({count:t,total:t,direction:`send`}),setTimeout(()=>this.transferProgressSignal.set(null),1500),n())},80)})}_updateConnectedPeersSignal(){this.connectedPeersSignal.set(Array.from(this.connections.keys()))}_waitForOpen(e){return e.open?Promise.resolve():new Promise((t,n)=>{let r=setTimeout(()=>{n(Error(`Connection open timeout (5s)`))},5e3);e.on(`open`,()=>{clearTimeout(r),t()}),e.on(`error`,e=>{clearTimeout(r),n(e)})})}async _processInboundA2AEnvelope(e,t,n,r){let i=`peer:${e}`,a=``,s=[],c=[],l;for(let e of t)if(e.kind===`text`&&typeof e.text==`string`)a+=e.text;else if(e.kind===`file`){let t=e.name||`attachment`;if(this._inboundRemap.has(t)){let e=this._inboundRemap.get(t),n=t.replace(/[.*+?^${}()|[\]\\]/g,`\\$&`);a=a.replace(RegExp(`(!?\\[.*?\\])\\(${n}\\)`,`g`),`$1(${e})`),this._inboundRemap.delete(t),t=e}let n=t.split(`/`).pop()||`attachment`;s.push({fileName:n,mimeType:e.mimeType,path:t,size:e.size||0})}else e.kind===`a2ui`&&e.envelope?c.push(e.envelope):e.kind===`a2ui-action`&&e.action&&(l=e.action);a=a.trim(),!(!a&&s.length===0&&c.length===0&&!l)&&this.messageCallback?.({id:o(),groupId:i,sender:e,content:a,timestamp:Date.now(),channel:`peerjs`,attachments:s.length>0?s:void 0,a2uiEnvelopes:c.length>0?c:void 0,a2uiAction:l,taskId:n,contextId:r})}};function P(e){let t=[],n=/!?\[([^\]]*)\]\(([^)]+)\)/g,r,i=e;for(;(r=n.exec(e))!==null;){let e=r[1],n=F((r[2]||``).trim());n&&t.push({alt:e,path:n})}return{remainingText:i,attachments:t}}function F(e){if(!e)return null;let t=e.trim();if(/^[a-zA-Z][a-zA-Z\d+.-]*:/.test(t)||t.startsWith(`//`))return null;let n=t.replace(/\\/g,`/`).replace(/^\/+/,``).replace(/^\.\//,``);if(!n)return null;let r=n.split(`/`).filter(Boolean);return r.some(e=>e===`..`)?null:r.join(`/`)}export{f as a,l as i,M as n,u as o,b as r,N as t};
1
+ import"./config-CrHchneq.js";import{n as e}from"./txPromise-EBECky1b.js";import{n as t}from"./toast-60iDlgiH.js";import{Zt as n,bn as r,cn as i,nn as a}from"./orchestrator-3h9xxHUm.js";import{t as o}from"./ulid-BY7rQVLN.js";const s={apng:`image/apng`,avif:`image/avif`,bmp:`image/bmp`,css:`text/css`,gif:`image/gif`,htm:`text/html`,html:`text/html`,jpeg:`image/jpeg`,jpg:`image/jpeg`,js:`text/javascript`,json:`application/json`,markdown:`text/markdown`,md:`text/markdown`,mp3:`audio/mpeg`,mp4:`video/mp4`,oga:`audio/ogg`,ogg:`audio/ogg`,ogv:`video/ogg`,pdf:`application/pdf`,png:`image/png`,svg:`image/svg+xml`,txt:`text/plain`,wav:`audio/wav`,webm:`video/webm`,webp:`image/webp`};function c(e){return e.trim().replace(/[\u0000-\u001f\u007f]+/g,``).replace(/[\\/:*?"<>|]+/g,`-`).replace(/\s+/g,` `).replace(/^\.+/,``).trim()||`attachment`}function l(e,t=``){if(t)return t;let n=e.toLowerCase().split(`.`).pop()||``;return s[n]||`application/octet-stream`}function u(e){return e.previewDisposition===`inline`||l(e.fileName,e.mimeType||``)===`image/png`}function d(e,t=``,n=Date.now()){let r=c(e),i=t.trim().replace(/[^a-zA-Z0-9._-]+/g,`-`).replace(/^-+|-+$/g,``);return`attachments/${n}-${i?`${i}-`:``}${r}`}async function f(e,t,n=[]){let r=[];for(let a of n){let n=c(a.fileName||``),o=a.path||d(n,a.id),s=a.source,u=l(n,a.mimeType||``),f=a.size;if(s){let r=await p(s);u=l(n,a.mimeType||r.type),f=typeof f==`number`?f:r.size,await i(e,t,o,r)}r.push(m({...a,fileName:n,mimeType:u,size:f,path:o,previewDisposition:a.previewDisposition||(u===`image/png`?`inline`:`file`)}))}return r}async function p(e){if(e.kind===`local-file`)return e.file;if(e.kind===`inline-text`)return new Blob([e.text],{type:e.mimeType||`text/plain`});let t=await fetch(e.url,{headers:e.headers});if(!t.ok)throw Error(`Attachment download failed: HTTP ${t.status} ${t.statusText}`);return await t.blob()}function m(e){let{source:t,...n}=e;return n}const h=new Set([`TASK_STATE_COMPLETED`,`TASK_STATE_FAILED`,`TASK_STATE_CANCELED`,`TASK_STATE_REJECTED`]),g=new Set([`TASK_STATE_INPUT_REQUIRED`,`TASK_STATE_AUTH_REQUIRED`]),_={SEND_MESSAGE:`SendMessage`,SEND_STREAMING_MESSAGE:`SendStreamingMessage`,GET_TASK:`GetTask`,CANCEL_TASK:`CancelTask`,GET_AGENT_CARD:`GetAgentCard`},v={EVENT:`agui/event`},y={STATUS_UPDATE:`tasks/statusUpdate`,ARTIFACT_UPDATE:`tasks/artifactUpdate`},b={JOIN:`room/join`,ROSTER:`room/roster`,LEAVE:`room/leave`,MESSAGE:`room/message`,INVITE:`room/invite`,RELAY:`room/relay`,A2UI:`room/a2ui`,A2UI_ACTION:`room/a2ui-action`},x={TASK_NOT_FOUND:-32001,TASK_NOT_CANCELABLE:-32002,PUSH_NOTIFICATION_NOT_SUPPORTED:-32003,UNSUPPORTED_OPERATION:-32004,CONTENT_TYPE_NOT_SUPPORTED:-32005,INVALID_AGENT_RESPONSE:-32006,PARSE_ERROR:-32700,INVALID_REQUEST:-32600,METHOD_NOT_FOUND:-32601,INVALID_PARAMS:-32602,INTERNAL_ERROR:-32603};function S(e){return typeof e==`object`&&!!e&&e.jsonrpc===`2.0`&&typeof e.id==`string`&&typeof e.method==`string`}function C(e){return typeof e==`object`&&!!e&&e.jsonrpc===`2.0`&&typeof e.id==`string`&&!(`method`in e)}function w(e){let t={url:`webrtc://${e.peerId}`,protocolBinding:`https://xt-ml.github.io/shadow-claw/bindings/webrtc-datachannel/v1`,protocolVersion:`1.0`},n={streaming:e.streaming??!0,pushNotifications:!1};return{name:e.name,description:e.description??`AI assistant agent (${e.name})`,version:e.version??`1.0.0`,supportedInterfaces:[t],capabilities:n,defaultInputModes:[`text/plain`,`application/json`],defaultOutputModes:[`text/plain`,`application/json`],skills:e.skills??[],iconUrl:e.iconUrl}}function T(){return{jsonrpc:`2.0`,id:o(),method:_.GET_AGENT_CARD}}function E(e,t){return{jsonrpc:`2.0`,id:e,result:t}}function D(e){if(e.error)return console.warn(`GetAgentCard returned error:`,e.error),null;let t=e.result;return!t||typeof t!=`object`||typeof t.name!=`string`||typeof t.version!=`string`?null:t}var O=class{_cards=new Map;clear(){this._cards.clear()}delete(e){this._cards.delete(e)}entries(){return this._cards.entries()}get(e){return this._cards.get(e)}getDisplayName(e){return this._cards.get(e)?.name??e}has(e){return this._cards.has(e)}set(e,t){this._cards.set(e,t)}supportsStreaming(e){return this._cards.get(e)?.capabilities?.streaming??!1}};const k={maxTasks:100,maxHistoryPerTask:50},A=new Map([[`TASK_STATE_SUBMITTED`,new Set([`TASK_STATE_WORKING`,`TASK_STATE_REJECTED`,`TASK_STATE_CANCELED`,`TASK_STATE_FAILED`])],[`TASK_STATE_WORKING`,new Set([`TASK_STATE_COMPLETED`,`TASK_STATE_FAILED`,`TASK_STATE_CANCELED`,`TASK_STATE_INPUT_REQUIRED`,`TASK_STATE_AUTH_REQUIRED`])],[`TASK_STATE_INPUT_REQUIRED`,new Set([`TASK_STATE_WORKING`,`TASK_STATE_CANCELED`,`TASK_STATE_FAILED`])],[`TASK_STATE_AUTH_REQUIRED`,new Set([`TASK_STATE_WORKING`,`TASK_STATE_CANCELED`,`TASK_STATE_FAILED`])]]);var j=class{_config;_contextActiveTask=new Map;_listeners=new Set;_taskOrder=[];_tasks=new Map;constructor(e){this._config={...k,...e}}addArtifact(e,t,n=!1){let r=this._tasks.get(e);if(!r)return;if(r.artifacts||=[],n){let e=r.artifacts.find(e=>e.artifactId===t.artifactId);e?e.parts.push(...t.parts):r.artifacts.push(t)}else r.artifacts.push(t);let i={taskId:r.id,contextId:r.contextId??``,artifact:t,append:n,lastChunk:!1};this._emit({type:`artifactUpdate`,taskId:r.id,contextId:r.contextId??``,payload:i})}cancelTask(e){let t=this._tasks.get(e);return!t||h.has(t.status.state)?!1:(this._transitionState(t,`TASK_STATE_CANCELED`,{messageId:o(),role:`ROLE_AGENT`,parts:[{text:`Task canceled by peer request.`}]}),!0)}clear(){this._tasks.clear(),this._taskOrder=[],this._contextActiveTask.clear()}emitRunError(e,t,n){let r=this._tasks.get(e);if(!r)return;let i={type:`RUN_ERROR`,message:t,code:n,timestamp:Date.now()};this._emitAGUI(r,i)}emitRunFinished(e){let t=this._tasks.get(e);if(!t)return;let n={type:`RUN_FINISHED`,threadId:t.contextId??t.id,runId:t.id,timestamp:Date.now()};this._emitAGUI(t,n)}emitRunStarted(e){let t=this._tasks.get(e);if(!t)return;let n={type:`RUN_STARTED`,threadId:t.contextId??t.id,runId:t.id,timestamp:Date.now()};this._emitAGUI(t,n)}emitStateSnapshot(e,t){let n=this._tasks.get(e);if(!n)return;let r={type:`STATE_SNAPSHOT`,snapshot:t,timestamp:Date.now()};this._emitAGUI(n,r)}emitTextMessageContent(e,t,n){let r=this._tasks.get(e);if(!r)return;let i={type:`TEXT_MESSAGE_CONTENT`,messageId:t,delta:n,timestamp:Date.now()};this._emitAGUI(r,i)}emitTextMessageEnd(e,t){let n=this._tasks.get(e);if(!n)return;let r={type:`TEXT_MESSAGE_END`,messageId:t,timestamp:Date.now()};this._emitAGUI(n,r)}emitTextMessageStart(e,t,n=`assistant`){let r=this._tasks.get(e);if(!r)return;let i={type:`TEXT_MESSAGE_START`,messageId:t,role:n,timestamp:Date.now()};this._emitAGUI(r,i)}emitToolCallEnd(e,t){let n=this._tasks.get(e);if(!n)return;let r={type:`TOOL_CALL_END`,toolCallId:t,timestamp:Date.now()};this._emitAGUI(n,r)}emitToolCallStart(e,t,n,r){let i=this._tasks.get(e);if(!i)return;let a={type:`TOOL_CALL_START`,toolCallId:t,toolCallName:n,parentMessageId:r,timestamp:Date.now()};this._emitAGUI(i,a)}getActiveTaskForContext(e){let t=this._contextActiveTask.get(e);if(t)return this._tasks.get(t)}getActiveTasks(){return Array.from(this._tasks.values()).filter(e=>!h.has(e.status.state))}getAllTasks(){return Array.from(this._tasks.values())}getTask(e){return this._tasks.get(e)}handleSendMessage(e){let t=e.message,n=t.contextId??o(),r=t.taskId??this._contextActiveTask.get(n);return r&&this._tasks.has(r)?this._continueTask(r,t):this._createTask(n,t)}markCompleted(e,t){let n=this._tasks.get(e);n&&this._transitionState(n,`TASK_STATE_COMPLETED`,t)}markFailed(e,t){let n=this._tasks.get(e);n&&this._transitionState(n,`TASK_STATE_FAILED`,{messageId:o(),role:`ROLE_AGENT`,parts:[{text:t}]})}markInputRequired(e,t){let n=this._tasks.get(e);n&&this._transitionState(n,`TASK_STATE_INPUT_REQUIRED`,t)}markWorking(e){let t=this._tasks.get(e);t&&this._transitionState(t,`TASK_STATE_WORKING`)}off(e){this._listeners.delete(e)}on(e){this._listeners.add(e)}_addTask(e){for(this._tasks.set(e.id,e),this._taskOrder.push(e.id);this._taskOrder.length>this._config.maxTasks;){let e=this._taskOrder.shift(),t=this._tasks.get(e);if(t&&h.has(t.status.state))this._tasks.delete(e),t.contextId&&this._contextActiveTask.delete(t.contextId);else{this._taskOrder.unshift(e);break}}}_continueTask(e,t){let n=this._tasks.get(e);return g.has(n.status.state)&&this._transitionState(n,`TASK_STATE_WORKING`),n.history||=[],n.history.push(t),n.history.length>this._config.maxHistoryPerTask&&(n.history=n.history.slice(-this._config.maxHistoryPerTask)),this._touchTask(e),{task:n}}_createTask(e,t){let n=o(),r={id:n,contextId:e,status:{state:`TASK_STATE_SUBMITTED`,timestamp:new Date().toISOString()},history:[t]};return this._addTask(r),this._contextActiveTask.set(e,n),this._emitStatusUpdate(r),{task:r}}_emit(e){for(let t of this._listeners)try{t(e)}catch(e){console.error(`PeerTaskManager: listener error`,e)}}_emitAGUI(e,t){this._emit({type:`aguiEvent`,taskId:e.id,contextId:e.contextId??``,payload:t})}_emitStatusUpdate(e){let t={taskId:e.id,contextId:e.contextId??``,status:e.status};this._emit({type:`statusUpdate`,taskId:e.id,contextId:e.contextId??``,payload:t})}_touchTask(e){let t=this._taskOrder.indexOf(e);t!==-1&&(this._taskOrder.splice(t,1),this._taskOrder.push(e))}_transitionState(e,t,n){let r=e.status.state,i=A.get(r);if(i&&!i.has(t)){console.warn(`PeerTaskManager: invalid transition ${r} → ${t} for task ${e.id}`);return}if(h.has(r)){console.warn(`PeerTaskManager: task ${e.id} is in terminal state ${r}, cannot transition`);return}e.status={state:t,message:n,timestamp:new Date().toISOString()},h.has(t)&&e.contextId&&this._contextActiveTask.get(e.contextId)===e.id&&this._contextActiveTask.delete(e.contextId),this._emitStatusUpdate(e)}};const M=new t.State(null);var N=class i{static _RECONNECT_BASE_MS=1e3;static _RECONNECT_MAX_MS=6e4;connectedPeersSignal=new t.State([]);connections=new Map;messageCallback=null;myPeerId=``;peerCards=new O;running=!1;serverConfig={};transferProgressSignal=M;trustedPeerIds=new Set;type=`peerjs`;typingCallback=null;_inboundRemap=new Map;_localCard=null;_onTaskCompleteCallback=null;peer=null;_pendingInboundFile=null;_pendingRequests=new Map;_reconnectAttempts=0;_reconnectTimer=null;_roomNotificationHandler=null;_taskManagers=new Map;constructor(){typeof globalThis.addEventListener==`function`&&globalThis.addEventListener(`peerjs-dc-handle-chunk`,e=>{if(e?.detail?.chunkInfo){let{count:t,total:n}=e.detail.chunkInfo,r=t+1;M.set({count:r,total:n,direction:`receive`}),r>=n&&setTimeout(()=>M.set(null),1500)}})}completeActiveTask(e){let t=e.replace(/^peer:/,``),n=this._taskManagers.get(t);if(!n)return!1;let r=n.getActiveTasks();if(r.length===0)return!1;let i=r[r.length-1];return n.markWorking(i.id),n.markCompleted(i.id),!0}configure(e,t,n={}){this.myPeerId=e.trim(),this.trustedPeerIds=new Set(t.map(e=>e.trim()).filter(Boolean)),this.serverConfig=n}connectPeer(e){this._getOrOpenConnection(e)}getTaskManager(e){let t=this._taskManagers.get(e);return t||(t=new j,this._taskManagers.set(e,t),t.on(t=>{this._forwardTaskEvent(e,t)})),t}isConfigured(){return this.myPeerId.length>0}isPeerConnected(e){let t=this.connections.get(e);return!!t&&!!t.open}onMessage(e){this.messageCallback=e}onTaskComplete(e){this._onTaskCompleteCallback=e}onTyping(e){this.typingCallback=e}sendRoomNotification(e,t){let n=this._getOrOpenConnection(e);if(!n)return!1;if(n.open)try{return n.send(t),!0}catch(t){return console.error(`PeerJsChannel: failed to send room notification to ${e}:`,t),!1}return this._waitForOpen(n).then(()=>{try{n.send(t)}catch(t){console.error(`PeerJsChannel: failed to flush room notification to ${e}:`,t)}}).catch(()=>{}),!0}setRoomNotificationHandler(e){this._roomNotificationHandler=e}setTyping(e,t){let n=e.replace(/^peer:/,``),r=this.connections.get(n);if(r)try{r.send({type:`typing`,typing:t})}catch{}}start(){!this.isConfigured()||this.running||(this.running=!0,this._localCard=w({peerId:this.myPeerId,name:this.myPeerId,description:`ShadowClaw AI assistant`,streaming:!0}),this._initPeer())}stop(){if(this.running=!1,this._reconnectTimer!==null&&(clearTimeout(this._reconnectTimer),this._reconnectTimer=null),this._reconnectAttempts=0,this.connections.forEach(e=>{try{e.close()}catch{}}),this.connections.clear(),this.peerCards.clear(),this._taskManagers.clear(),this._pendingRequests.forEach(({timer:e})=>clearTimeout(e)),this._pendingRequests.clear(),this._updateConnectedPeersSignal(),this.peer){try{this.peer.destroy()}catch{}this.peer=null}}async send(t,n,i){let a=t.replace(/^peer:/,``),s=this._getOrOpenConnection(a);if(!s){console.warn(`PeerJsChannel: no connection available for ${a}`);return}try{await this._waitForOpen(s);let{attachments:a}=P(n),c=[...(i||[]).filter(e=>!!e.path),...a.map(e=>({path:e.path,mimeType:`application/octet-stream`}))],l=new Map,u=[];try{let n=await e();for(let e of c)if(e.path)try{let i=await r(n,t,e.path),a=e.path;l.has(e.path)||l.set(e.path,a);let o=e.mimeType||`application/octet-stream`;s.send({type:`__file_header`,name:a,mimeType:o,size:i.length});let c=this._trackSendProgress(s,i.length);s.send(i.buffer),await c,u.push({kind:`file`,name:a,mimeType:o,size:i.length})}catch(t){console.warn(`PeerJsChannel: failed to process file ${e.path}:`,t)}}catch(e){console.error(`PeerJsChannel: failed to access DB`,e)}let d=n;for(let[e,t]of l){let n=e.replace(/[.*+?^${}()|[\]\\]/g,`\\$&`);d=d.replace(RegExp(`(!?\\[.*?\\])\\(${n}\\)`,`g`),`$1(${t})`)}let f=[...u,{kind:`text`,text:d}],p=o(),m={jsonrpc:`2.0`,id:p,method:_.SEND_MESSAGE,params:{message:{messageId:o(),role:`ROLE_AGENT`,parts:f}}};s.send(m),this._registerPendingRequest(p,e=>{})}catch(e){console.error(`PeerJsChannel: send failed for ${a}:`,e),window.dispatchEvent(new CustomEvent(`shadow-claw-peer-error`,{detail:{remotePeerId:a,error:e instanceof Error?e.message:String(e)}}))}}async sendA2UI(e,t){let n=e.replace(/^peer:/,``),r=this._getOrOpenConnection(n);if(!r){console.warn(`PeerJsChannel.sendA2UI: no connection available for ${n}`);return}try{await this._waitForOpen(r);let e={jsonrpc:`2.0`,method:`message/send`,id:o(),params:{message:{role:`agent`,parts:[{kind:`a2ui`,envelope:t}]}}};r.send(e)}catch(e){console.error(`PeerJsChannel.sendA2UI: failed for ${n}:`,e)}}async sendA2UIAction(e,t){let n=e.replace(/^peer:/,``),r=this._getOrOpenConnection(n);if(!r){console.warn(`PeerJsChannel.sendA2UIAction: no connection available for ${n}`);return}try{await this._waitForOpen(r);let e={jsonrpc:`2.0`,method:`message/send`,id:o(),params:{message:{role:`user`,parts:[{kind:`a2ui-action`,action:t}]}}};r.send(e)}catch(e){console.error(`PeerJsChannel.sendA2UIAction: failed for ${n}:`,e)}}_exchangeAgentCards(e,t){if(!this._localCard)return;let n=T();t.send(n),this._registerPendingRequest(n.id,t=>{let n=D(t);n&&(this.peerCards.set(e,n),console.log(`PeerJsChannel: received AgentCard from ${e}: "${n.name}"`))})}_forwardTaskEvent(e,t){let n=this.connections.get(e);if(!n)return;let r;switch(t.type){case`statusUpdate`:r=y.STATUS_UPDATE;break;case`artifactUpdate`:r=y.ARTIFACT_UPDATE;break;case`aguiEvent`:r=v.EVENT;break;default:return}let i={jsonrpc:`2.0`,method:r,params:t.payload};try{n.send(i)}catch(n){console.error(`PeerJsChannel: failed to forward ${t.type} to ${e}:`,n)}}_getOrOpenConnection(e){let t=this.connections.get(e);if(t)return t;if(!this.peer)return null;try{let t=this.peer.connect(e,{reliable:!0,serialization:`binary`});return t?(this._handleIncomingConnection(t),t):null}catch(t){return console.error(`PeerJsChannel: failed to connect to ${e}:`,t),null}}_handleA2ACancelTask(e,t){let n=t.params;if(!n?.taskId){this._sendJsonRpcError(e,t.id,{code:x.INVALID_PARAMS,message:`Missing taskId in CancelTask params`});return}let r=this._taskManagers.get(e);if(!r){this._sendJsonRpcError(e,t.id,{code:x.TASK_NOT_FOUND,message:`Task not found: ${n.taskId}`});return}if(!r.cancelTask(n.taskId)){this._sendJsonRpcError(e,t.id,{code:x.TASK_NOT_CANCELABLE,message:`Task not cancelable: ${n.taskId}`});return}let i=this.connections.get(e);if(i){let e=r.getTask(n.taskId),a={jsonrpc:`2.0`,id:t.id,result:{task:e}};i.send(a)}}_handleA2AGetTask(e,t){let n=t.params;if(!n?.taskId){this._sendJsonRpcError(e,t.id,{code:x.INVALID_PARAMS,message:`Missing taskId in GetTask params`});return}let r=this._taskManagers.get(e)?.getTask(n.taskId);if(!r){this._sendJsonRpcError(e,t.id,{code:x.TASK_NOT_FOUND,message:`Task not found: ${n.taskId}`});return}let i=this.connections.get(e);if(i){let e={jsonrpc:`2.0`,id:t.id,result:{task:r}};i.send(e)}}_handleA2AJsonRpc(e,t){if(C(t)){let e=this._pendingRequests.get(t.id);e&&(clearTimeout(e.timer),this._pendingRequests.delete(t.id),e.resolve(t));return}let n=t.method;if(n){if(n.startsWith(`room/`)){this._roomNotificationHandler?.(e,n,t.params);return}if(S(t)){switch(n){case _.GET_AGENT_CARD:this._handleGetAgentCard(e,t);break;case _.SEND_MESSAGE:this._handleA2ASendMessage(e,t);break;case _.CANCEL_TASK:this._handleA2ACancelTask(e,t);break;case _.GET_TASK:this._handleA2AGetTask(e,t);break;default:if(n===`message/send`){let n=t.params;n?.message?.parts&&Array.isArray(n.message.parts)&&this._processInboundA2AEnvelope(e,n.message.parts).catch(e=>console.error(`PeerJsChannel: failed to process inbound A2A envelope`,e))}else this._sendJsonRpcError(e,t.id,{code:x.METHOD_NOT_FOUND,message:`Method not found: ${n}`})}return}switch(n){case v.EVENT:this._handleAGUIEventNotification(e,t.params);break;case y.STATUS_UPDATE:this._handleTaskStatusNotification(e,t.params);break;case y.ARTIFACT_UPDATE:break;case`message/send`:{let n=t.params;n?.message?.parts&&Array.isArray(n.message.parts)&&this._processInboundA2AEnvelope(e,n.message.parts).catch(e=>console.error(`PeerJsChannel: failed to process inbound A2A envelope`,e))}break}}}_handleA2ASendMessage(e,t){let n=t.params;if(!n?.message){this._sendJsonRpcError(e,t.id,{code:x.INVALID_PARAMS,message:`Missing message in SendMessage params`});return}let r=this._taskManagers.get(e);r||(r=new j,this._taskManagers.set(e,r),r.on(t=>{this._forwardTaskEvent(e,t)}));let i=r.handleSendMessage(n),a=this.connections.get(e);if(a){let e={jsonrpc:`2.0`,id:t.id,result:i};a.send(e)}n.message.parts&&Array.isArray(n.message.parts)&&this._processInboundA2AEnvelope(e,n.message.parts,i.task?.id,i.task?.contextId).catch(e=>console.error(`PeerJsChannel: failed to process A2A SendMessage`,e))}_handleAGUIEventNotification(e,t){if(!t||typeof t!=`object`)return;let n=t;window.dispatchEvent(new CustomEvent(`shadow-claw-agui-event`,{detail:{remotePeerId:e,event:n}}))}_handleGetAgentCard(e,t){if(!this._localCard){this._sendJsonRpcError(e,t.id,{code:x.INTERNAL_ERROR,message:`Agent card not available`});return}let n=E(t.id,this._localCard),r=this.connections.get(e);r&&r.send(n)}_handleInboundData(t,r){if(r instanceof ArrayBuffer||r instanceof Uint8Array){if(this._pendingInboundFile){let i=this._pendingInboundFile.name,o=`peer:${t}`,s=new Uint8Array(r);e().then(async e=>{let t=i,r=1;for(;await a(e,o,t);){let e=i.lastIndexOf(`.`);if(e>0&&e<i.length-1){let n=i.substring(0,e),a=i.substring(e);t=`${n} (${r})${a}`}else t=`${i} (${r})`;r++}t!==i&&this._inboundRemap.set(i,t),n(e,o,t,s).catch(e=>{console.error(`PeerJsChannel: failed to write inbound file bytes`,e)})}),this._pendingInboundFile=null}return}if(!r||typeof r!=`object`)return;let i=r;if(i.type===`__file_header`){this._pendingInboundFile={name:i.name,mimeType:i.mimeType,size:i.size};return}if(i.type===`typing`){this.typingCallback?.(`peer:${t}`,!!i.typing);return}if(i.type===`chat`){let e=typeof i.text==`string`?i.text.trim():``;e&&this.messageCallback?.({id:o(),groupId:`peer:${t}`,sender:t,content:e,timestamp:Date.now(),channel:`peerjs`});return}if(i.jsonrpc===`2.0`){this._handleA2AJsonRpc(t,i);return}if(i.type===`command:execute`){let e=i.payload||{},n=e.action||``,r=e.args||{},a=e.commandId||``;import(`./initControlPlane-C9jpnWev.js`).then(({executeClientControlCommand:e})=>e(n,r)).then(e=>{let r=this.connections.get(t);if(r){let t=!!e?.error;r.send({id:o(),type:`command:result`,payload:{commandId:a,action:n,success:!t,data:e,...t?{error:e.error}:{}}})}}).catch(e=>{let r=this.connections.get(t);r&&r.send({id:o(),type:`command:result`,payload:{commandId:a,action:n,success:!1,error:e?.message||String(e)}})});return}}_isTrustedPeer(e){if(this.trustedPeerIds.size===0||this.trustedPeerIds.has(e))return!0;for(let t of this.trustedPeerIds){let n=t.trim();if(n.endsWith(`*`)&&e.startsWith(n.slice(0,-1))||n===`cli`&&e.startsWith(`cli-`))return!0}return!1}_handleIncomingConnection(e){let t=e.peer;if(!this._isTrustedPeer(t)){console.warn(`PeerJsChannel: rejecting connection from untrusted peer: ${t}`);try{e.close()}catch{}return}this.connections.set(t,e),this._updateConnectedPeersSignal(),e.on(`open`,()=>{console.log(`PeerJsChannel: connection opened with ${t}`),this._exchangeAgentCards(t,e)}),e.on(`data`,e=>{this._handleInboundData(t,e)}),e.on(`close`,()=>{console.log(`PeerJsChannel: connection closed with ${t}`),this.connections.delete(t),this.peerCards.delete(t),this._taskManagers.delete(t),this._updateConnectedPeersSignal()}),e.on(`error`,e=>{console.error(`PeerJsChannel: connection error with ${t}:`,e),this.connections.delete(t),this._updateConnectedPeersSignal(),window.dispatchEvent(new CustomEvent(`shadow-claw-peer-error`,{detail:{remotePeerId:t,error:e.message}}))}),e.on(`iceStateChanged`,e=>{(e===`failed`||e===`disconnected`||e===`closed`)&&(console.warn(`PeerJsChannel: ICE state changed to ${e} for ${t}`),this.connections.delete(t),this._updateConnectedPeersSignal())})}_handleTaskStatusNotification(e,t){if(!t||typeof t!=`object`)return;let n=t;if(n.status?.state&&h.has(n.status.state)){let t=`peer:${e}`;this._onTaskCompleteCallback?.(t)}window.dispatchEvent(new CustomEvent(`shadow-claw-task-status`,{detail:{remotePeerId:e,...n}}))}async _initPeer(){let{Peer:e}=await import(`./bundler-Ca-Xwln8.js`),t={},n=this.serverConfig.host??`0.peerjs.com`,r=this.serverConfig.port??443,i=this.serverConfig.path??`/`,a=this.serverConfig.secure??!0;t.host=n,t.port=r,t.path=i,t.secure=a,t.debug=3,t.config={iceServers:this.serverConfig.iceServers??[{urls:`stun:stun.l.google.com:19302`},{urls:`stun:stun1.l.google.com:19302`}]},this.peer=new e(this.myPeerId,t),this.peer.on(`open`,e=>{console.log(`PeerJsChannel: connected to signaling server, peer ID: ${e}`),this._reconnectAttempts=0}),this.peer.on(`connection`,e=>{this._handleIncomingConnection(e)}),this.peer.on(`error`,e=>{console.error(`PeerJsChannel: peer error [${e.type}]:`,e.message),(e.type===`peer-unavailable`||e.type===`unavailable-id`)&&window.dispatchEvent(new CustomEvent(`shadow-claw-peer-error`,{detail:{error:e.message}})),!(e.type===`peer-unavailable`||e.type===`network`||e.type===`disconnected`)&&e.type===`unavailable-id`&&console.warn(`PeerJsChannel: peer ID is already in use. Stop the channel and reconfigure.`)}),this.peer.on(`disconnected`,()=>{this.running&&this._scheduleReconnect()}),this.peer.on(`close`,()=>{console.log(`PeerJsChannel: peer closed`)})}_registerPendingRequest(e,t){let n=setTimeout(()=>{this._pendingRequests.delete(e),console.warn(`PeerJsChannel: JSON-RPC request ${e} timed out (10s)`)},1e4);this._pendingRequests.set(e,{resolve:t,timer:n})}_scheduleReconnect(){if(this._reconnectTimer!==null)return;let e=Math.min(i._RECONNECT_BASE_MS*2**this._reconnectAttempts,i._RECONNECT_MAX_MS);this._reconnectAttempts++,console.log(`PeerJsChannel: disconnected from signaling server, reconnecting in ${(e/1e3).toFixed(1)}s (attempt ${this._reconnectAttempts})`),this._reconnectTimer=setTimeout(()=>{if(this._reconnectTimer=null,this.running)try{this.peer?.reconnect?.()}catch{this._scheduleReconnect()}},e)}_sendJsonRpcError(e,t,n){let r=this.connections.get(e);if(!r)return;let i={jsonrpc:`2.0`,id:t,error:n};try{r.send(i)}catch{}}_trackSendProgress(e,t){return new Promise(n=>{let r=e._dc;if(!r||t===0){this.transferProgressSignal.set(null),n();return}this.transferProgressSignal.set({count:0,total:t,direction:`send`});let i=setInterval(()=>{let e=r.bufferedAmount,a=Math.max(0,t-e);this.transferProgressSignal.set({count:a,total:t,direction:`send`}),e===0&&(clearInterval(i),this.transferProgressSignal.set({count:t,total:t,direction:`send`}),setTimeout(()=>this.transferProgressSignal.set(null),1500),n())},80)})}_updateConnectedPeersSignal(){this.connectedPeersSignal.set(Array.from(this.connections.keys()))}_waitForOpen(e){return e.open?Promise.resolve():new Promise((t,n)=>{let r=setTimeout(()=>{n(Error(`Connection open timeout (5s)`))},5e3);e.on(`open`,()=>{clearTimeout(r),t()}),e.on(`error`,e=>{clearTimeout(r),n(e)})})}async _processInboundA2AEnvelope(e,t,n,r){let i=`peer:${e}`,a=``,s=[],c=[],l;for(let e of t)if(e.kind===`text`&&typeof e.text==`string`)a+=e.text;else if(e.kind===`file`){let t=e.name||`attachment`;if(this._inboundRemap.has(t)){let e=this._inboundRemap.get(t),n=t.replace(/[.*+?^${}()|[\]\\]/g,`\\$&`);a=a.replace(RegExp(`(!?\\[.*?\\])\\(${n}\\)`,`g`),`$1(${e})`),this._inboundRemap.delete(t),t=e}let n=t.split(`/`).pop()||`attachment`;s.push({fileName:n,mimeType:e.mimeType,path:t,size:e.size||0})}else e.kind===`a2ui`&&e.envelope?c.push(e.envelope):e.kind===`a2ui-action`&&e.action&&(l=e.action);a=a.trim(),!(!a&&s.length===0&&c.length===0&&!l)&&this.messageCallback?.({id:o(),groupId:i,sender:e,content:a,timestamp:Date.now(),channel:`peerjs`,attachments:s.length>0?s:void 0,a2uiEnvelopes:c.length>0?c:void 0,a2uiAction:l,taskId:n,contextId:r})}};function P(e){let t=[],n=/!?\[([^\]]*)\]\(([^)]+)\)/g,r,i=e;for(;(r=n.exec(e))!==null;){let e=r[1],n=F((r[2]||``).trim());n&&t.push({alt:e,path:n})}return{remainingText:i,attachments:t}}function F(e){if(!e)return null;let t=e.trim();if(/^[a-zA-Z][a-zA-Z\d+.-]*:/.test(t)||t.startsWith(`//`))return null;let n=t.replace(/\\/g,`/`).replace(/^\/+/,``).replace(/^\.\//,``);if(!n)return null;let r=n.split(`/`).filter(Boolean);return r.some(e=>e===`..`)?null:r.join(`/`)}export{f as a,l as i,M as n,u as o,b as r,N as t};
@@ -1 +1 @@
1
- import{r as e}from"./config-AL77QAx4.js";import{n as t}from"./txPromise-EBECky1b.js";import{t as n}from"./getConfig-D89uJgo5.js";async function r(r){let i=await n(await t(),e.PUSH_PROXY_URL);if(!i)return r;let a=r.startsWith(`/`)?r:`/${r}`;return`${i.endsWith(`/`)?i.slice(0,-1):i}${a}`}function i(e){let t=(e+`=`.repeat((4-e.length%4)%4)).replace(/-/g,`+`).replace(/_/g,`/`),n=atob(t);return Uint8Array.from([...n].map(e=>e.charCodeAt(0)))}async function a(){let e=await r(`/push/vapid-public-key`);return(await(await fetch(e)).json()).publicKey}async function o(){let e=await navigator.serviceWorker.ready,t=await a(),n=await e.pushManager.subscribe({userVisibleOnly:!0,applicationServerKey:i(t)}),o=await r(`/push/subscribe`);return await fetch(o,{method:`POST`,headers:{"Content-Type":`application/json`},body:JSON.stringify(n)}),n}async function s(){let e=await(await navigator.serviceWorker.ready).pushManager.getSubscription();if(!e)return;let{endpoint:t}=e;await e.unsubscribe();let n=await r(`/push/subscribe`);await fetch(n,{method:`DELETE`,headers:{"Content-Type":`application/json`},body:JSON.stringify({endpoint:t})})}async function c(){return(await navigator.serviceWorker.ready).pushManager.getSubscription()}export{s as i,r as n,o as r,c as t};
1
+ import{r as e}from"./config-CrHchneq.js";import{n as t}from"./txPromise-EBECky1b.js";import{t as n}from"./getConfig-D89uJgo5.js";async function r(r){let i=await n(await t(),e.PUSH_PROXY_URL);if(!i)return r;let a=r.startsWith(`/`)?r:`/${r}`;return`${i.endsWith(`/`)?i.slice(0,-1):i}${a}`}function i(e){let t=(e+`=`.repeat((4-e.length%4)%4)).replace(/-/g,`+`).replace(/_/g,`/`),n=atob(t);return Uint8Array.from([...n].map(e=>e.charCodeAt(0)))}async function a(){let e=await r(`/push/vapid-public-key`);return(await(await fetch(e)).json()).publicKey}async function o(){let e=await navigator.serviceWorker.ready,t=await a(),n=await e.pushManager.subscribe({userVisibleOnly:!0,applicationServerKey:i(t)}),o=await r(`/push/subscribe`);return await fetch(o,{method:`POST`,headers:{"Content-Type":`application/json`},body:JSON.stringify(n)}),n}async function s(){let e=await(await navigator.serviceWorker.ready).pushManager.getSubscription();if(!e)return;let{endpoint:t}=e;await e.unsubscribe();let n=await r(`/push/subscribe`);await fetch(n,{method:`DELETE`,headers:{"Content-Type":`application/json`},body:JSON.stringify({endpoint:t})})}async function c(){return(await navigator.serviceWorker.ready).pushManager.getSubscription()}export{s as i,r as n,o as r,c as t};
@@ -1 +1 @@
1
- (function(){function e(e){return e.replace(/^\/+|\/+$/g,``)}function t(t){let n=e(t);return n.endsWith(`/index.html`)?n=n.slice(0,-11):n.endsWith(`index.html`)&&(n=n.slice(0,-10)),e(n)}function n(t){let n=e(t),r=n.split(`/`).filter(Boolean);if(r.length===0)return null;let i=r[0].toLowerCase();if(i===`pages`){if(r.length>=3){let e=r[1];return{page:`pages`,groupId:e===`main`?`br:main`:e.startsWith(`br-`)?`br:${e.slice(3)}`:e,path:r.slice(2).join(`/`)}}return{page:`pages`,groupId:`br:main`,path:r.slice(1).join(`/`)}}if(i===`files`){let e=r[1]||`main`;return{page:`files`,groupId:e===`main`?`br:main`:e.startsWith(`br-`)?`br:${e.slice(3)}`:e,path:r.slice(2).join(`/`)||void 0}}if(i===`chat`){let e=r[1]||`main`;return{page:`chat`,groupId:e===`main`?`br:main`:e.startsWith(`br-`)?`br:${e.slice(3)}`:e}}if(i===`tasks`){let e=r[1]||`main`;return{page:`tasks`,groupId:e===`main`?`br:main`:e.startsWith(`br-`)?`br:${e.slice(3)}`:e}}return i===`main`?{page:`pages`,groupId:`br:main`,path:r.slice(1).join(`/`)}:{page:`pages`,groupId:`br:main`,path:n}}function r(){if(typeof document<`u`){let e=document.getElementById(`shadow-claw-static-routing`);if(e&&e.textContent)try{let t=JSON.parse(e.textContent);if(t&&typeof t==`object`&&t.routes)return t}catch(e){console.warn(`Failed to parse embedded static routing manifest:`,e)}}return null}function i(e,i){let a=i||r();if(!a||!a.routes)return null;let o=t(e);if(!o)return null;for(let[e,r]of Object.entries(a.routes))if(!(!r||!r.prettyPath)&&t(r.prettyPath)===o)return n(e);return null}function a(e){try{return decodeURIComponent(e)}catch{return e}}let o=null,s=new Set([`chat`,`files`,`tasks`,`pages`,`settings`,`tools`,`channels`]);function c(){o=null}typeof globalThis<`u`&&(globalThis.__applyBasePathCacheReset=c);function l(){let e=typeof window<`u`?window:typeof self<`u`?self:globalThis;if(e&&e.__SHADOWCLAW_DEPLOY_ID__){let t=String(e.__SHADOWCLAW_DEPLOY_ID__).trim();if(t)return t.replace(/[^a-zA-Z0-9_-]/g,`-`)}let t=globalThis.process;if(t&&t.env&&t.env.SHADOWCLAW_DEPLOY_ID){let e=String(t.env.SHADOWCLAW_DEPLOY_ID).trim();if(e)return e.replace(/[^a-zA-Z0-9_-]/g,`-`)}let n=u();return!n||n===`/`?``:n.replace(/^\/+|\/+$/g,``).replace(/[^a-zA-Z0-9_-]/g,`-`)}function u(){if(o!==null)return o;if((typeof window>`u`||globalThis.WorkerGlobalScope!==void 0&&typeof self<`u`&&self instanceof globalThis.WorkerGlobalScope)&&typeof self<`u`&&self.location){let e=(self.location.pathname||`/`).split(`/`).filter(Boolean);if(e.length<=1)return o=`/`,o;let t=e[0].toLowerCase(),n=t===`service-worker.js`||t===`sw.js`||t===`manifest.json`||t===`sitemap.xml`||t===`favicon.ico`||t===`index.html`||t===`service-worker`||t===`assets`||t===`dist`||t===`public`;if(n&&e.length===2)return o=`/`,o;if(!n)return o=`/`+e[0]+`/`,o}let e=typeof window<`u`&&window.location?window.location:typeof self<`u`&&self.location?self.location:null;if(!e)return`/`;if(typeof document<`u`&&typeof document.querySelector==`function`){let t=document.querySelector(`base[href]`);if(t){let n=t.getAttribute(`href`);if(n)try{let t=new URL(n,e.origin).pathname;return t.endsWith(`/`)||(t+=`/`),o=t,o}catch{}}}let t=e.pathname||`/`;if(t===`/`)return o=`/`,o;let n=t.split(`/`).filter(Boolean),r=n.findIndex(e=>s.has(e.toLowerCase()));return r>=0?r===0?(o=`/`,o):(o=`/`+n.slice(0,r).join(`/`)+`/`,o):i(t)?(o=`/`,o):n.length===1&&!t.includes(`.`)?(o=`/`+n[0]+`/`,o):(o=`/`,o)}function d(e){return e.replace(/^\/+|\/+$/g,``)}function f(e){return e&&(e===`main`?`br:main`:e.startsWith(`br-`)?`br:${e.slice(3)}`:e)}function p(e){if(!e)return;let t=d(e.replace(/\\/g,`/`));if(!t)return;let n=t.split(`/`).filter(Boolean).filter(e=>e!==`.`&&e!==`..`);if(n.length!==0)return n.join(`/`)}function m(e){let t=e.split(`/`).filter(Boolean).map(e=>a(e));if(t.length<3||t[0]!==`files`)return null;let n=f(t[1])||t[1],r=p(t.slice(2).join(`/`));return!n||!r?null:{groupId:n,path:r}}let h=[`cdnjs.cloudflare.com`,`esm.sh`,`unpkg.com`,`cdn.jsdelivr.net`,`esm.run`,`huggingface.co`,`hf.co`,`hf-mirror.com`],g=[`openrouter.ai`,`router.huggingface.co`,`api-inference.huggingface.co`,`api.telegram.org`];function _(e,t){if(e.origin===t)return!0;let n=e.hostname.toLowerCase();if(h.includes(n)||n.endsWith(`.huggingface.co`)||n.endsWith(`.hf.co`)||n.endsWith(`.hf-mirror.com`)||n.endsWith(`.esm.sh`)||n.endsWith(`.jsdelivr.net`)||g.includes(n)||n.endsWith(`.openrouter.ai`))return!0;let r=n===`localhost`||n===`127.0.0.1`,i=e.pathname.endsWith(`/share/share-target.html`),a=e.pathname===`/proxy`||e.pathname.startsWith(`/git-proxy/`)||i;return r&&a}let v=!1,y=`/proxy`,b={"3gp":`video/3gpp`,aac:`audio/aac`,apng:`image/apng`,avi:`video/x-msvideo`,avif:`image/avif`,bin:`application/octet-stream`,css:`text/css; charset=utf-8`,db:`application/vnd.sqlite3`,dll:`application/octet-stream`,dmg:`application/octet-stream`,dylib:`application/octet-stream`,exe:`application/vnd.microsoft.portable-executable`,flac:`audio/flac`,flv:`video/x-flv`,gif:`image/gif`,gz:`application/gzip`,heic:`image/heic`,heif:`image/heif`,htm:`text/html; charset=utf-8`,html:`text/html; charset=utf-8`,iso:`application/octet-stream`,jpeg:`image/jpeg`,jpg:`image/jpeg`,js:`application/javascript; charset=utf-8`,json:`application/json; charset=utf-8`,m4a:`audio/mp4`,m4v:`video/mp4`,mjs:`application/javascript; charset=utf-8`,mkv:`video/x-matroska`,mov:`video/mp4`,mp3:`audio/mpeg`,mp4:`video/mp4`,pdf:`application/pdf`,png:`image/png`,rar:`application/vnd.rar`,so:`application/octet-stream`,sqlite:`application/vnd.sqlite3`,sqlite3:`application/vnd.sqlite3`,svg:`image/svg+xml`,tar:`application/x-tar`,tif:`image/tiff`,tiff:`image/tiff`,ts:`video/mp2t`,txt:`text/plain; charset=utf-8`,wasm:`application/wasm`,wav:`audio/wav`,weba:`audio/webm`,webm:`video/webm`,webp:`image/webp`,wmv:`video/x-ms-wmv`,zip:`application/zip`};function x(e){let t=e.split(`.`).pop()?.toLowerCase()||``;return b[t]||`application/octet-stream`}function S(e){return!(e.method!==`GET`||e.mode===`navigate`||e.destination===`document`)}async function C(e,t){let n=navigator.storage;if(!n||typeof n.getDirectory!=`function`)return null;try{let r=await n.getDirectory(),i=l(),a=i?`shadowclaw-${i}`:`shadowclaw`,o=await(await r.getDirectoryHandle(a,{create:!0})).getDirectoryHandle(`groups`,{create:!0}),s=e.replace(/:/g,`-`),c=await o.getDirectoryHandle(s,{create:!1}),u=t.split(`/`).filter(Boolean);if(u.length===0)return null;let d=u.pop();if(!d)return null;let f=c;for(let e of u)f=await f.getDirectoryHandle(e,{create:!1});return await(await f.getFileHandle(d,{create:!1})).getFile()}catch{return null}}async function w(e){let t=m(new URL(e.url).pathname);if(!t||!S(e))return null;try{let e=await C(t.groupId,t.path);return e?new Response(e,{status:200,headers:{"Content-Type":e.type||x(t.path),"Content-Length":String(e.size),"Cache-Control":`no-store`,"Access-Control-Allow-Origin":`*`}}):null}catch{return null}}let T=!1,E=[];function D(){T=!0;for(let e of E)e();E=[]}self.clients.matchAll({type:`window`}).then(e=>{e.length===0?D():(e.forEach(e=>e.postMessage({type:`request-proxy-config`})),setTimeout(D,2e3))}),self.addEventListener(`message`,e=>{e.data?.type===`set-proxy-config`?(v=!!e.data.payload?.useProxy,y=e.data.payload?.proxyUrl||`/proxy`,console.log(`fetch proxy: config updated`,{useProxy:v,proxyUrl:y}),D()):e.data?.type===`set-use-proxy`&&(v=!!e.data.payload?.useProxy,D())}),self.addEventListener(`fetch`,e=>{let t=new URL(e.request.url);if(t.origin===location.origin&&m(t.pathname)&&S(e.request)){e.respondWith((async()=>await w(e.request)||fetch(e.request).catch(e=>(console.error(`fetch proxy fallback error:`,e),new Response(`Network error`,{status:502}))))());return}_(t,location.origin)||t.protocol.startsWith(`http`)&&e.respondWith((async()=>{if(T||await new Promise(e=>E.push(e)),!v)return fetch(e.request);try{let t={};e.request.headers.forEach((e,n)=>{t[n]=e});let n;if(e.request.method!==`GET`&&e.request.method!==`HEAD`)try{n=await e.request.clone().text()}catch(e){console.error(`fetch proxy: error reading body text:`,e)}let r=new URL(y,location.origin);return r.pathname=(r.pathname.endsWith(`/`)?``:`/`)+`proxy/`+e.request.url,await fetch(r.href,{method:`POST`,headers:{"Content-Type":`application/json`},body:JSON.stringify({url:e.request.url,method:e.request.method,headers:t,body:n})})}catch(t){return console.error(`fetch proxy error:`,t),fetch(e.request).catch(e=>(console.error(`fetch proxy fallback error:`,e),new Response(`Network error`,{status:502})))}})())})})();
1
+ (function(){function e(e){return e.replace(/^\/+|\/+$/g,``)}function t(t){let n=e(t);return n.endsWith(`/index.html`)?n=n.slice(0,-11):n.endsWith(`index.html`)&&(n=n.slice(0,-10)),e(n)}function n(t){let n=e(t),r=n.split(`/`).filter(Boolean);if(r.length===0)return null;let i=r[0].toLowerCase();if(i===`pages`){if(r.length>=3){let e=r[1];return{page:`pages`,groupId:e===`main`?`br:main`:e.startsWith(`br-`)?`br:${e.slice(3)}`:e,path:r.slice(2).join(`/`)}}return{page:`pages`,groupId:`br:main`,path:r.slice(1).join(`/`)}}if(i===`files`){let e=r[1]||`main`;return{page:`files`,groupId:e===`main`?`br:main`:e.startsWith(`br-`)?`br:${e.slice(3)}`:e,path:r.slice(2).join(`/`)||void 0}}if(i===`chat`){let e=r[1]||`main`;return{page:`chat`,groupId:e===`main`?`br:main`:e.startsWith(`br-`)?`br:${e.slice(3)}`:e}}if(i===`tasks`){let e=r[1]||`main`;return{page:`tasks`,groupId:e===`main`?`br:main`:e.startsWith(`br-`)?`br:${e.slice(3)}`:e}}return i===`main`?{page:`pages`,groupId:`br:main`,path:r.slice(1).join(`/`)}:{page:`pages`,groupId:`br:main`,path:n}}function r(){if(typeof document<`u`){let e=document.getElementById(`shadow-claw-static-routing`);if(e&&e.textContent)try{let t=JSON.parse(e.textContent);if(t&&typeof t==`object`&&t.routes)return t}catch(e){console.warn(`Failed to parse embedded static routing manifest:`,e)}}return null}function i(e,i){let a=i||r();if(!a||!a.routes)return null;let o=t(e);if(!o)return null;for(let[e,r]of Object.entries(a.routes))if(!(!r||!r.prettyPath)&&t(r.prettyPath)===o)return n(e);return null}function a(e){try{return decodeURIComponent(e)}catch{return e}}let o=null,s=new Set([`chat`,`files`,`tasks`,`pages`,`settings`,`tools`,`channels`]);function c(){o=null}typeof globalThis<`u`&&(globalThis.__applyBasePathCacheReset=c);function l(){let e=typeof window<`u`?window:typeof self<`u`?self:globalThis;if(e&&e.__SHADOWCLAW_DEPLOY_ID__){let t=String(e.__SHADOWCLAW_DEPLOY_ID__).trim();if(t)return t.replace(/[^a-zA-Z0-9_-]/g,`-`)}let t=globalThis.process;if(t&&t.env&&t.env.SHADOWCLAW_DEPLOY_ID){let e=String(t.env.SHADOWCLAW_DEPLOY_ID).trim();if(e)return e.replace(/[^a-zA-Z0-9_-]/g,`-`)}let n=u();return!n||n===`/`?``:n.replace(/^\/+|\/+$/g,``).replace(/[^a-zA-Z0-9_-]/g,`-`)}function u(){if(o!==null)return o;if((typeof window>`u`||globalThis.WorkerGlobalScope!==void 0&&typeof self<`u`&&self instanceof globalThis.WorkerGlobalScope)&&typeof self<`u`&&self.location){let e=(self.location.pathname||`/`).split(`/`).filter(Boolean);if(e.length<=1)return o=`/`,o;let t=e[0].toLowerCase(),n=t===`service-worker.js`||t===`sw.js`||t===`manifest.json`||t===`sitemap.xml`||t===`favicon.ico`||t===`index.html`||t===`service-worker`||t===`assets`||t===`dist`||t===`public`;if(n&&e.length===2)return o=`/`,o;if(!n)return o=`/`+e[0]+`/`,o}let e=typeof window<`u`&&window.location?window.location:typeof self<`u`&&self.location?self.location:null;if(!e)return`/`;if(typeof document<`u`&&typeof document.querySelector==`function`){let t=document.querySelector(`base[href]`);if(t){let n=t.getAttribute(`href`);if(n)try{let t=new URL(n,e.origin).pathname;return t.endsWith(`/`)||(t+=`/`),o=t,o}catch{}}}let t=e.pathname||`/`;if(t===`/`)return o=`/`,o;let n=t.split(`/`).filter(Boolean),r=n.findIndex(e=>s.has(e.toLowerCase()));return r>=0?r===0?(o=`/`,o):(o=`/`+n.slice(0,r).join(`/`)+`/`,o):i(t)?(o=`/`,o):n.length===1&&!t.includes(`.`)?(o=`/`+n[0]+`/`,o):(o=`/`,o)}function d(e){return e.replace(/^\/+|\/+$/g,``)}function f(e){return e&&(e===`main`?`br:main`:e.startsWith(`br-`)?`br:${e.slice(3)}`:e)}function p(e){if(!e)return;let t=d(e.replace(/\\/g,`/`));if(!t)return;let n=t.split(`/`).filter(Boolean).filter(e=>e!==`.`&&e!==`..`);if(n.length!==0)return n.join(`/`)}function m(e){let t=e,n=u();n!==`/`&&t.startsWith(n)&&(t=`/`+t.slice(n.length));let r=t.split(`/`).filter(Boolean).map(e=>a(e));if(r.length<3||r[0]!==`files`)return null;let i=f(r[1])||r[1],o=p(r.slice(2).join(`/`));return!i||!o?null:{groupId:i,path:o}}let h=[`cdnjs.cloudflare.com`,`esm.sh`,`unpkg.com`,`cdn.jsdelivr.net`,`esm.run`,`huggingface.co`,`hf.co`,`hf-mirror.com`],g=[`openrouter.ai`,`router.huggingface.co`,`api-inference.huggingface.co`,`api.telegram.org`];function _(e,t){if(e.origin===t)return!0;let n=e.hostname.toLowerCase();if(h.includes(n)||n.endsWith(`.huggingface.co`)||n.endsWith(`.hf.co`)||n.endsWith(`.hf-mirror.com`)||n.endsWith(`.esm.sh`)||n.endsWith(`.jsdelivr.net`)||g.includes(n)||n.endsWith(`.openrouter.ai`))return!0;let r=n===`localhost`||n===`127.0.0.1`,i=e.pathname.endsWith(`/share/share-target.html`),a=e.pathname===`/proxy`||e.pathname.startsWith(`/git-proxy/`)||i;return r&&a}let v=!1,y=`/proxy`,b={"3gp":`video/3gpp`,aac:`audio/aac`,apng:`image/apng`,avi:`video/x-msvideo`,avif:`image/avif`,bin:`application/octet-stream`,css:`text/css; charset=utf-8`,db:`application/vnd.sqlite3`,dll:`application/octet-stream`,dmg:`application/octet-stream`,dylib:`application/octet-stream`,exe:`application/vnd.microsoft.portable-executable`,flac:`audio/flac`,flv:`video/x-flv`,gif:`image/gif`,gz:`application/gzip`,heic:`image/heic`,heif:`image/heif`,htm:`text/html; charset=utf-8`,html:`text/html; charset=utf-8`,iso:`application/octet-stream`,jpeg:`image/jpeg`,jpg:`image/jpeg`,js:`application/javascript; charset=utf-8`,json:`application/json; charset=utf-8`,m4a:`audio/mp4`,m4v:`video/mp4`,mjs:`application/javascript; charset=utf-8`,mkv:`video/x-matroska`,mov:`video/mp4`,mp3:`audio/mpeg`,mp4:`video/mp4`,pdf:`application/pdf`,png:`image/png`,rar:`application/vnd.rar`,so:`application/octet-stream`,sqlite:`application/vnd.sqlite3`,sqlite3:`application/vnd.sqlite3`,svg:`image/svg+xml`,tar:`application/x-tar`,tif:`image/tiff`,tiff:`image/tiff`,ts:`video/mp2t`,txt:`text/plain; charset=utf-8`,wasm:`application/wasm`,wav:`audio/wav`,weba:`audio/webm`,webm:`video/webm`,webp:`image/webp`,wmv:`video/x-ms-wmv`,zip:`application/zip`};function x(e){let t=e.split(`.`).pop()?.toLowerCase()||``;return b[t]||`application/octet-stream`}function S(e){return!(e.method!==`GET`||e.mode===`navigate`||e.destination===`document`)}async function C(e,t){let n=navigator.storage;if(!n||typeof n.getDirectory!=`function`)return null;try{let r=await n.getDirectory(),i=l(),a=i?`shadowclaw-${i}`:`shadowclaw`,o=await(await r.getDirectoryHandle(a,{create:!0})).getDirectoryHandle(`groups`,{create:!0}),s=e.replace(/:/g,`-`),c=await o.getDirectoryHandle(s,{create:!1}),u=t.split(`/`).filter(Boolean);if(u.length===0)return null;let d=u.pop();if(!d)return null;let f=c;for(let e of u)f=await f.getDirectoryHandle(e,{create:!1});return await(await f.getFileHandle(d,{create:!1})).getFile()}catch{return null}}async function w(e){let t=m(new URL(e.url).pathname);if(!t||!S(e))return null;try{let e=await C(t.groupId,t.path);return e?new Response(e,{status:200,headers:{"Content-Type":e.type||x(t.path),"Content-Length":String(e.size),"Cache-Control":`no-store`,"Access-Control-Allow-Origin":`*`}}):null}catch{return null}}let T=!1,E=[];function D(){T=!0;for(let e of E)e();E=[]}self.clients.matchAll({type:`window`}).then(e=>{e.length===0?D():(e.forEach(e=>e.postMessage({type:`request-proxy-config`})),setTimeout(D,2e3))}),self.addEventListener(`message`,e=>{e.data?.type===`set-proxy-config`?(v=!!e.data.payload?.useProxy,y=e.data.payload?.proxyUrl||`/proxy`,console.log(`fetch proxy: config updated`,{useProxy:v,proxyUrl:y}),D()):e.data?.type===`set-use-proxy`&&(v=!!e.data.payload?.useProxy,D())}),self.addEventListener(`fetch`,e=>{let t=new URL(e.request.url);if(t.origin===location.origin&&m(t.pathname)&&S(e.request)){e.respondWith((async()=>await w(e.request)||fetch(e.request).catch(e=>(console.error(`fetch proxy fallback error:`,e),new Response(`Network error`,{status:502}))))());return}_(t,location.origin)||t.protocol.startsWith(`http`)&&e.respondWith((async()=>{if(T||await new Promise(e=>E.push(e)),!v)return fetch(e.request);try{let t={};e.request.headers.forEach((e,n)=>{t[n]=e});let n;if(e.request.method!==`GET`&&e.request.method!==`HEAD`)try{n=await e.request.clone().text()}catch(e){console.error(`fetch proxy: error reading body text:`,e)}let r=new URL(y,location.origin);return r.pathname=(r.pathname.endsWith(`/`)?``:`/`)+`proxy/`+e.request.url,await fetch(r.href,{method:`POST`,headers:{"Content-Type":`application/json`},body:JSON.stringify({url:e.request.url,method:e.request.method,headers:t,body:n})})}catch(t){return console.error(`fetch proxy error:`,t),fetch(e.request).catch(e=>(console.error(`fetch proxy fallback error:`,e),new Response(`Network error`,{status:502})))}})())})})();
@@ -1 +1 @@
1
- (function(){self.addEventListener(`push`,e=>{let t={title:`ShadowClaw`,body:`New notification`};if(e.data)try{t=e.data.json()}catch{t={title:`ShadowClaw`,body:e.data.text()}}if(t.type===`scheduled-task`){e.waitUntil(self.clients.matchAll({type:`window`,includeUncontrolled:!0}).then(e=>{for(let n of e)n.postMessage({type:`scheduled-task-trigger`,taskId:t.taskId,groupId:t.groupId,prompt:t.prompt,taskType:t.taskType,tools:t.tools,channel:t.channel});return self.registration.showNotification(`ShadowClaw — Scheduled Task${t.taskType?` (${t.taskType})`:``}`,{body:t.prompt?`${t.prompt.slice(0,120)}${t.tools?.length?` | Tools: ${t.tools.map(e=>typeof e==`object`&&e&&`name`in e?e.name:String(e)).join(`, `)}`:``}`.slice(0,120):`A scheduled task has been triggered.`,icon:`/assets/icons/512.png`,badge:`/assets/icons/192.png`,data:{type:`scheduled-task`,taskId:t.taskId,groupId:t.groupId,prompt:t.prompt,taskType:t.taskType,tools:t.tools,channel:t.channel}})}));return}e.waitUntil(self.registration.showNotification(t.title||`ShadowClaw`,{body:t.body||``,icon:`/assets/icons/512.png`,badge:`/assets/icons/192.png`}))}),self.addEventListener(`notificationclick`,e=>{e.notification.close(),e.waitUntil(self.clients.matchAll({type:`window`,includeUncontrolled:!0}).then(e=>{for(let t of e)if(t.url.includes(self.location.origin)&&`focus`in t)return t.focus();let t=self.registration?.scope?new URL(`.`,self.registration.scope).href:`/`;return self.clients.openWindow(t)}))})})();
1
+ (function(){self.addEventListener(`push`,e=>{let t={title:`ShadowClaw`,body:`New notification`};if(e.data)try{t=e.data.json()}catch{t={title:`ShadowClaw`,body:e.data.text()}}if(t.type===`remote-command`){e.waitUntil(self.clients.matchAll({type:`window`,includeUncontrolled:!0}).then(e=>{for(let n of e)n.postMessage({type:`remote-command-trigger`,clientId:t.clientId,action:t.action,args:t.args,prompt:t.prompt});return self.registration.showNotification(`ShadowClaw — Remote Command`,{body:t.prompt||`Remote command received: ${t.action}`,icon:`/assets/icons/512.png`,badge:`/assets/icons/192.png`,data:{type:`remote-command`,clientId:t.clientId,action:t.action,args:t.args}})}));return}if(t.type===`scheduled-task`){e.waitUntil(self.clients.matchAll({type:`window`,includeUncontrolled:!0}).then(e=>{for(let n of e)n.postMessage({type:`scheduled-task-trigger`,taskId:t.taskId,groupId:t.groupId,prompt:t.prompt,taskType:t.taskType,tools:t.tools,channel:t.channel});return self.registration.showNotification(`ShadowClaw — Scheduled Task${t.taskType?` (${t.taskType})`:``}`,{body:t.prompt?`${t.prompt.slice(0,120)}${t.tools?.length?` | Tools: ${t.tools.map(e=>typeof e==`object`&&e&&`name`in e?e.name:String(e)).join(`, `)}`:``}`.slice(0,120):`A scheduled task has been triggered.`,icon:`/assets/icons/512.png`,badge:`/assets/icons/192.png`,data:{type:`scheduled-task`,taskId:t.taskId,groupId:t.groupId,prompt:t.prompt,taskType:t.taskType,tools:t.tools,channel:t.channel}})}));return}e.waitUntil(self.registration.showNotification(t.title||`ShadowClaw`,{body:t.body||``,icon:`/assets/icons/512.png`,badge:`/assets/icons/192.png`}))}),self.addEventListener(`notificationclick`,e=>{e.notification.close(),e.waitUntil(self.clients.matchAll({type:`window`,includeUncontrolled:!0}).then(e=>{for(let t of e)if(t.url.includes(self.location.origin)&&`focus`in t)return t.focus();let t=self.registration?.scope?new URL(`.`,self.registration.scope).href:`/`;return self.clients.openWindow(t)}))})})();