hostlet 0.0.0-stage → 0.1.0-alpha.2

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 (512) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +233 -2
  3. package/bin/hostlet.js +61 -0
  4. package/console/app/api.js +1223 -0
  5. package/console/app/boot.js +165 -0
  6. package/console/app/cards.js +222 -0
  7. package/console/app/compose-quote.js +127 -0
  8. package/console/app/config-form.js +727 -0
  9. package/console/app/document-drafts.js +62 -0
  10. package/console/app/document-tools.mjs +67 -0
  11. package/console/app/documents.js +106 -0
  12. package/console/app/pages.js +6483 -0
  13. package/console/app/plugins.js +509 -0
  14. package/console/app/preview-tools.js +172 -0
  15. package/console/app/reference-labels.js +118 -0
  16. package/console/app/selection.js +372 -0
  17. package/console/app/store.js +57 -0
  18. package/console/app/text-anchor.js +26 -0
  19. package/console/app/topic-local.js +322 -0
  20. package/console/app/topic-nav.js +414 -0
  21. package/console/app/topic-status.js +109 -0
  22. package/console/app/views.js +43 -0
  23. package/console/apple-touch-icon.png +0 -0
  24. package/console/avatars/agy.svg +34 -0
  25. package/console/avatars/alhena.svg +36 -0
  26. package/console/avatars/castor.svg +38 -0
  27. package/console/avatars/claude.svg +34 -0
  28. package/console/avatars/codex.svg +34 -0
  29. package/console/avatars/devin.svg +34 -0
  30. package/console/avatars/echo.svg +34 -0
  31. package/console/avatars/kimi.svg +33 -0
  32. package/console/avatars/opencode.svg +33 -0
  33. package/console/avatars/pinion.svg +34 -0
  34. package/console/avatars/sprocket.svg +34 -0
  35. package/console/avatars/vela.svg +44 -0
  36. package/console/avatars/wasat.svg +38 -0
  37. package/console/clients/agy.svg +1 -0
  38. package/console/clients/claude.svg +1 -0
  39. package/console/clients/codex.svg +1 -0
  40. package/console/clients/devin.svg +1 -0
  41. package/console/clients/kimi.svg +1 -0
  42. package/console/clients/opencode.svg +1 -0
  43. package/console/favicon.ico +0 -0
  44. package/console/favicon.svg +16 -0
  45. package/console/index.html +99 -0
  46. package/console/pdl/engine/README.md +13 -0
  47. package/console/pdl/engine/anchor.js +195 -0
  48. package/console/pdl/engine/core.js +1340 -0
  49. package/console/pdl/engine/frame.js +1252 -0
  50. package/console/pdl/engine/i18n.js +67 -0
  51. package/console/pdl/engine/pdl-check.js +102 -0
  52. package/console/pdl/engine/pdl-doc.js +353 -0
  53. package/console/pdl/engine/pdl-view.js +280 -0
  54. package/console/pdl/engine/pdl.js +1064 -0
  55. package/console/pdl/engine/platform.js +18 -0
  56. package/console/pdl/engine/proj.js +279 -0
  57. package/console/pdl/engine/theme.js +176 -0
  58. package/console/pdl/engine/tree.js +171 -0
  59. package/console/pdl/engine/units.js +546 -0
  60. package/console/pdl/library/README.md +28 -0
  61. package/console/pdl/library/blocks.js +1512 -0
  62. package/console/pdl/library/buttons.js +86 -0
  63. package/console/pdl/library/capsules.js +130 -0
  64. package/console/pdl/library/charts.js +287 -0
  65. package/console/pdl/library/code.js +98 -0
  66. package/console/pdl/library/css/base.css +12 -0
  67. package/console/pdl/library/css/blocks.css +98 -0
  68. package/console/pdl/library/css/capsules.css +42 -0
  69. package/console/pdl/library/css/catalog-page.css +24 -0
  70. package/console/pdl/library/css/chat.css +100 -0
  71. package/console/pdl/library/css/composer-top.css +145 -0
  72. package/console/pdl/library/css/controls.css +195 -0
  73. package/console/pdl/library/css/drag.css +29 -0
  74. package/console/pdl/library/css/edit-layout.css +93 -0
  75. package/console/pdl/library/css/floating-window.css +30 -0
  76. package/console/pdl/library/css/hover.css +50 -0
  77. package/console/pdl/library/css/label-search.css +37 -0
  78. package/console/pdl/library/css/layers.css +43 -0
  79. package/console/pdl/library/css/library-parts.css +33 -0
  80. package/console/pdl/library/css/library.css +117 -0
  81. package/console/pdl/library/css/member-select.css +34 -0
  82. package/console/pdl/library/css/message.css +72 -0
  83. package/console/pdl/library/css/page.css +104 -0
  84. package/console/pdl/library/css/parts.css +338 -0
  85. package/console/pdl/library/css/responsive.css +40 -0
  86. package/console/pdl/library/css/roles.css +28 -0
  87. package/console/pdl/library/css/rows.css +231 -0
  88. package/console/pdl/library/css/settings-rows.css +37 -0
  89. package/console/pdl/library/css/shell.css +59 -0
  90. package/console/pdl/library/css/stream.css +296 -0
  91. package/console/pdl/library/css/theme-calculator.css +18 -0
  92. package/console/pdl/library/css/thumb-choice.css +69 -0
  93. package/console/pdl/library/css/tweaks.css +16 -0
  94. package/console/pdl/library/css/widgets.css +211 -0
  95. package/console/pdl/library/css/workbench.css +59 -0
  96. package/console/pdl/library/feedback.js +83 -0
  97. package/console/pdl/library/floating-window.mjs +100 -0
  98. package/console/pdl/library/fold.js +238 -0
  99. package/console/pdl/library/format.js +53 -0
  100. package/console/pdl/library/frames.js +123 -0
  101. package/console/pdl/library/icons.js +165 -0
  102. package/console/pdl/library/identity.js +149 -0
  103. package/console/pdl/library/inputs.js +801 -0
  104. package/console/pdl/library/label-search.js +214 -0
  105. package/console/pdl/library/markdown-text.js +115 -0
  106. package/console/pdl/library/markdown.js +150 -0
  107. package/console/pdl/library/media-viewer.js +293 -0
  108. package/console/pdl/library/member-select.mjs +32 -0
  109. package/console/pdl/library/message.js +105 -0
  110. package/console/pdl/library/motion.js +56 -0
  111. package/console/pdl/library/overlays.js +165 -0
  112. package/console/pdl/library/page.js +142 -0
  113. package/console/pdl/library/plugin-form.js +154 -0
  114. package/console/pdl/library/registry.js +198 -0
  115. package/console/pdl/library/rows.js +523 -0
  116. package/console/pdl/library/shell.js +151 -0
  117. package/console/pdl/library/stream.js +58 -0
  118. package/console/pdl/library/tabs.js +259 -0
  119. package/console/pdl/library/theme-calculator.js +300 -0
  120. package/console/pdl/library/widgets.js +114 -0
  121. package/console/pdl/load-order.json +74 -0
  122. package/console/pdl/platform/README.md +3 -0
  123. package/console/pdl/platform/rich-markdown.mjs +17 -0
  124. package/console/pdl/platform/web-links.js +26 -0
  125. package/console/pdl/platform/web.js +80 -0
  126. package/console/sdk-browser/mentions.d.mts +16 -0
  127. package/console/sdk-browser/mentions.mjs +84 -0
  128. package/console/theme/assets.js +21 -0
  129. package/console/theme/theme.json +9417 -0
  130. package/console/theme/tokens.json +444 -0
  131. package/dist/core/a2a.d.ts +288 -0
  132. package/dist/core/a2a.js +2197 -0
  133. package/dist/core/address.d.ts +34 -0
  134. package/dist/core/address.js +33 -0
  135. package/dist/core/approval-inputs.d.ts +17 -0
  136. package/dist/core/approval-inputs.js +70 -0
  137. package/dist/core/archive.d.ts +19 -0
  138. package/dist/core/archive.js +135 -0
  139. package/dist/core/attachments.d.ts +17 -0
  140. package/dist/core/attachments.js +143 -0
  141. package/dist/core/blocked-handoffs.d.ts +132 -0
  142. package/dist/core/blocked-handoffs.js +349 -0
  143. package/dist/core/config-schema.d.ts +124 -0
  144. package/dist/core/config-schema.js +192 -0
  145. package/dist/core/config.d.ts +117 -0
  146. package/dist/core/config.js +638 -0
  147. package/dist/core/disk-usage.d.ts +26 -0
  148. package/dist/core/disk-usage.js +83 -0
  149. package/dist/core/documents.d.ts +9 -0
  150. package/dist/core/documents.js +48 -0
  151. package/dist/core/entry.d.ts +316 -0
  152. package/dist/core/entry.js +711 -0
  153. package/dist/core/home.d.ts +76 -0
  154. package/dist/core/home.js +449 -0
  155. package/dist/core/i18n.d.ts +36 -0
  156. package/dist/core/i18n.js +458 -0
  157. package/dist/core/instances.d.ts +224 -0
  158. package/dist/core/instances.js +1272 -0
  159. package/dist/core/members.d.ts +190 -0
  160. package/dist/core/members.js +358 -0
  161. package/dist/core/mentions.d.ts +12 -0
  162. package/dist/core/mentions.js +49 -0
  163. package/dist/core/messages.d.ts +876 -0
  164. package/dist/core/messages.js +916 -0
  165. package/dist/core/notices.d.ts +88 -0
  166. package/dist/core/notices.js +54 -0
  167. package/dist/core/ops.d.ts +117 -0
  168. package/dist/core/ops.js +146 -0
  169. package/dist/core/package.d.ts +2 -0
  170. package/dist/core/package.js +14 -0
  171. package/dist/core/parts.d.ts +25 -0
  172. package/dist/core/parts.js +119 -0
  173. package/dist/core/plugins/catalog.d.ts +14 -0
  174. package/dist/core/plugins/catalog.js +54 -0
  175. package/dist/core/plugins/external.d.ts +7 -0
  176. package/dist/core/plugins/external.js +921 -0
  177. package/dist/core/plugins/index.d.ts +5 -0
  178. package/dist/core/plugins/index.js +51 -0
  179. package/dist/core/plugins/npm.d.ts +12 -0
  180. package/dist/core/plugins/npm.js +99 -0
  181. package/dist/core/plugins/plugin-manifest.d.ts +12 -0
  182. package/dist/core/plugins/plugin-manifest.js +231 -0
  183. package/dist/core/push.d.ts +22 -0
  184. package/dist/core/push.js +63 -0
  185. package/dist/core/resource.d.ts +77 -0
  186. package/dist/core/resource.js +48 -0
  187. package/dist/core/spawn.d.ts +12 -0
  188. package/dist/core/spawn.js +30 -0
  189. package/dist/core/storage-inventory.d.ts +118 -0
  190. package/dist/core/storage-inventory.js +279 -0
  191. package/dist/core/storage.d.ts +45 -0
  192. package/dist/core/storage.js +602 -0
  193. package/dist/core/store.d.ts +15 -0
  194. package/dist/core/store.js +33 -0
  195. package/dist/core/topic-index.d.ts +19 -0
  196. package/dist/core/topic-index.js +70 -0
  197. package/dist/core/topic-ops.d.ts +139 -0
  198. package/dist/core/topic-ops.js +507 -0
  199. package/dist/core/topic-status.d.ts +32 -0
  200. package/dist/core/topic-status.js +105 -0
  201. package/dist/core/topics.d.ts +327 -0
  202. package/dist/core/topics.js +786 -0
  203. package/dist/core/turn-clients.d.ts +1 -0
  204. package/dist/core/turn-clients.js +5 -0
  205. package/dist/core/types.d.ts +2866 -0
  206. package/dist/core/types.js +166 -0
  207. package/dist/core/util.d.ts +10 -0
  208. package/dist/core/util.js +47 -0
  209. package/dist/main.d.ts +12 -0
  210. package/dist/main.js +82 -0
  211. package/dist/plugins/adapter-acp/index.d.ts +3 -0
  212. package/dist/plugins/adapter-acp/index.js +57 -0
  213. package/dist/plugins/adapter-acp/plugin.yaml +100 -0
  214. package/dist/plugins/adapter-agy/command.d.ts +9 -0
  215. package/dist/plugins/adapter-agy/command.js +34 -0
  216. package/dist/plugins/adapter-agy/index.d.ts +5 -0
  217. package/dist/plugins/adapter-agy/index.js +65 -0
  218. package/dist/plugins/adapter-agy/quota.d.ts +2 -0
  219. package/dist/plugins/adapter-agy/quota.js +155 -0
  220. package/dist/plugins/adapter-agy/terminal.d.ts +6 -0
  221. package/dist/plugins/adapter-agy/terminal.js +132 -0
  222. package/dist/plugins/adapter-agy/version.d.ts +9 -0
  223. package/dist/plugins/adapter-agy/version.js +17 -0
  224. package/dist/plugins/adapter-claude/index.d.ts +43 -0
  225. package/dist/plugins/adapter-claude/index.js +412 -0
  226. package/dist/plugins/adapter-claude/quota.d.ts +14 -0
  227. package/dist/plugins/adapter-claude/quota.js +103 -0
  228. package/dist/plugins/adapter-codex/index.d.ts +4 -0
  229. package/dist/plugins/adapter-codex/index.js +688 -0
  230. package/dist/plugins/adapter-codex/responses-model.d.ts +9 -0
  231. package/dist/plugins/adapter-codex/responses-model.js +82 -0
  232. package/dist/plugins/adapter-kimi/index.d.ts +4 -0
  233. package/dist/plugins/adapter-kimi/index.js +51 -0
  234. package/dist/plugins/adapter-kimi/quota.d.ts +3 -0
  235. package/dist/plugins/adapter-kimi/quota.js +171 -0
  236. package/dist/plugins/adapter-opencode/index.d.ts +12 -0
  237. package/dist/plugins/adapter-opencode/index.js +64 -0
  238. package/dist/plugins/annotations/console/cards.css +189 -0
  239. package/dist/plugins/annotations/console/cards.mjs +439 -0
  240. package/dist/plugins/annotations/console/pdl.json +52 -0
  241. package/dist/plugins/annotations/console/view.mjs +241 -0
  242. package/dist/plugins/annotations/console/window.mjs +219 -0
  243. package/dist/plugins/annotations/followups.d.ts +51 -0
  244. package/dist/plugins/annotations/followups.js +112 -0
  245. package/dist/plugins/annotations/index.d.ts +3 -0
  246. package/dist/plugins/annotations/index.js +298 -0
  247. package/dist/plugins/annotations/plugin.yaml +3 -0
  248. package/dist/plugins/ask-choice/index.d.ts +9 -0
  249. package/dist/plugins/ask-choice/index.js +24 -0
  250. package/dist/plugins/ask-choice/plugin.yaml +2 -0
  251. package/dist/plugins/background/index.d.ts +2 -0
  252. package/dist/plugins/background/index.js +398 -0
  253. package/dist/plugins/background/plugin.yaml +7 -0
  254. package/dist/plugins/builtin.json +37 -0
  255. package/dist/plugins/cards/console/pdl.json +516 -0
  256. package/dist/plugins/cards/console/view.mjs +67 -0
  257. package/dist/plugins/cards/index.d.ts +8 -0
  258. package/dist/plugins/cards/index.js +627 -0
  259. package/dist/plugins/cards/plugin.yaml +3 -0
  260. package/dist/plugins/console/browser.d.ts +2 -0
  261. package/dist/plugins/console/browser.js +115 -0
  262. package/dist/plugins/console/console/pdl.json +38 -0
  263. package/dist/plugins/console/console/view.mjs +26 -0
  264. package/dist/plugins/console/index.d.ts +4 -0
  265. package/dist/plugins/console/index.js +94 -0
  266. package/dist/plugins/console/picker.d.ts +2 -0
  267. package/dist/plugins/console/picker.js +48 -0
  268. package/dist/plugins/console/plugin.yaml +3 -0
  269. package/dist/plugins/console/topic-view.d.ts +3 -0
  270. package/dist/plugins/console/topic-view.js +388 -0
  271. package/dist/plugins/handoff/index.d.ts +2 -0
  272. package/dist/plugins/handoff/index.js +147 -0
  273. package/dist/plugins/handoff/plugin.yaml +8 -0
  274. package/dist/plugins/host-api/index.d.ts +7 -0
  275. package/dist/plugins/host-api/index.js +310 -0
  276. package/dist/plugins/host-api/plugin.yaml +2 -0
  277. package/dist/plugins/host-api/signing.d.ts +63 -0
  278. package/dist/plugins/host-api/signing.js +427 -0
  279. package/dist/plugins/host-api/skills/host-ops/SKILL.md +28 -0
  280. package/dist/plugins/host-api/skills/hostlet-plugin-dev/SKILL.md +38 -0
  281. package/dist/plugins/inbox/index.d.ts +15 -0
  282. package/dist/plugins/inbox/index.js +161 -0
  283. package/dist/plugins/inbox/plugin.yaml +2 -0
  284. package/dist/plugins/keys/account-directories.d.ts +18 -0
  285. package/dist/plugins/keys/account-directories.js +86 -0
  286. package/dist/plugins/keys/account-storage.d.ts +18 -0
  287. package/dist/plugins/keys/account-storage.js +112 -0
  288. package/dist/plugins/keys/console/pdl.json +710 -0
  289. package/dist/plugins/keys/console/view.mjs +619 -0
  290. package/dist/plugins/keys/index.d.ts +7 -0
  291. package/dist/plugins/keys/index.js +842 -0
  292. package/dist/plugins/keys/login-link.d.ts +2 -0
  293. package/dist/plugins/keys/login-link.js +15 -0
  294. package/dist/plugins/keys/plugin.yaml +3 -0
  295. package/dist/plugins/keys/quota-history.d.ts +91 -0
  296. package/dist/plugins/keys/quota-history.js +420 -0
  297. package/dist/plugins/mcp/console/pdl.json +412 -0
  298. package/dist/plugins/mcp/console/view.mjs +207 -0
  299. package/dist/plugins/mcp/index.d.ts +7 -0
  300. package/dist/plugins/mcp/index.js +494 -0
  301. package/dist/plugins/mcp/plugin.yaml +3 -0
  302. package/dist/plugins/member-health/index.d.ts +2 -0
  303. package/dist/plugins/member-health/index.js +478 -0
  304. package/dist/plugins/member-health/plugin.yaml +2 -0
  305. package/dist/plugins/memory/capabilities.d.ts +10 -0
  306. package/dist/plugins/memory/capabilities.js +70 -0
  307. package/dist/plugins/memory/console/pdl.json +459 -0
  308. package/dist/plugins/memory/console/view.mjs +254 -0
  309. package/dist/plugins/memory/corpus.d.ts +60 -0
  310. package/dist/plugins/memory/corpus.js +521 -0
  311. package/dist/plugins/memory/engines/fts-tree-diversity.d.ts +8 -0
  312. package/dist/plugins/memory/engines/fts-tree-diversity.js +49 -0
  313. package/dist/plugins/memory/engines/fts-tree-text.d.ts +12 -0
  314. package/dist/plugins/memory/engines/fts-tree-text.js +93 -0
  315. package/dist/plugins/memory/engines/fts-tree.d.ts +4 -0
  316. package/dist/plugins/memory/engines/fts-tree.js +387 -0
  317. package/dist/plugins/memory/engines/search.d.ts +9 -0
  318. package/dist/plugins/memory/engines/search.js +35 -0
  319. package/dist/plugins/memory/index.d.ts +3 -0
  320. package/dist/plugins/memory/index.js +465 -0
  321. package/dist/plugins/memory/notes.d.ts +22 -0
  322. package/dist/plugins/memory/notes.js +140 -0
  323. package/dist/plugins/memory/plugin.yaml +23 -0
  324. package/dist/plugins/model-profiles/index.d.ts +2 -0
  325. package/dist/plugins/model-profiles/index.js +919 -0
  326. package/dist/plugins/model-profiles/plugin.yaml +13 -0
  327. package/dist/plugins/model-profiles/review-source.d.ts +11 -0
  328. package/dist/plugins/model-profiles/review-source.js +640 -0
  329. package/dist/plugins/model-profiles/rules.d.ts +132 -0
  330. package/dist/plugins/model-profiles/rules.js +767 -0
  331. package/dist/plugins/model-profiles/skills/model-profile/SKILL.md +60 -0
  332. package/dist/plugins/model-profiles/sources.d.ts +61 -0
  333. package/dist/plugins/model-profiles/sources.js +375 -0
  334. package/dist/plugins/model-profiles/storage.d.ts +104 -0
  335. package/dist/plugins/model-profiles/storage.js +352 -0
  336. package/dist/plugins/model-profiles/types.d.ts +187 -0
  337. package/dist/plugins/model-profiles/types.js +12 -0
  338. package/dist/plugins/model-profiles/wakeup.d.ts +29 -0
  339. package/dist/plugins/model-profiles/wakeup.js +390 -0
  340. package/dist/plugins/models/index.d.ts +2 -0
  341. package/dist/plugins/models/index.js +63 -0
  342. package/dist/plugins/models/plugin.yaml +8 -0
  343. package/dist/plugins/ops/collector.d.ts +22 -0
  344. package/dist/plugins/ops/collector.js +48 -0
  345. package/dist/plugins/ops/config.d.ts +11 -0
  346. package/dist/plugins/ops/config.js +13 -0
  347. package/dist/plugins/ops/console/pdl.json +676 -0
  348. package/dist/plugins/ops/console/view.mjs +700 -0
  349. package/dist/plugins/ops/index.d.ts +3 -0
  350. package/dist/plugins/ops/index.js +20 -0
  351. package/dist/plugins/ops/journal.d.ts +7 -0
  352. package/dist/plugins/ops/journal.js +109 -0
  353. package/dist/plugins/ops/metrics.d.ts +3 -0
  354. package/dist/plugins/ops/metrics.js +81 -0
  355. package/dist/plugins/ops/plugin.yaml +63 -0
  356. package/dist/plugins/ops/samples.d.ts +54 -0
  357. package/dist/plugins/ops/samples.js +100 -0
  358. package/dist/plugins/ops/trace.d.ts +3 -0
  359. package/dist/plugins/ops/trace.js +104 -0
  360. package/dist/plugins/ops/usage.d.ts +2 -0
  361. package/dist/plugins/ops/usage.js +152 -0
  362. package/dist/plugins/plans/acceptance.d.ts +284 -0
  363. package/dist/plugins/plans/acceptance.js +247 -0
  364. package/dist/plugins/plans/blocked.d.ts +25 -0
  365. package/dist/plugins/plans/blocked.js +66 -0
  366. package/dist/plugins/plans/console/pdl.json +639 -0
  367. package/dist/plugins/plans/console/view.d.mts +2 -0
  368. package/dist/plugins/plans/console/view.mjs +855 -0
  369. package/dist/plugins/plans/dependencies.d.ts +5 -0
  370. package/dist/plugins/plans/dependencies.js +21 -0
  371. package/dist/plugins/plans/index.d.ts +72 -0
  372. package/dist/plugins/plans/index.js +2057 -0
  373. package/dist/plugins/plans/plugin.yaml +44 -0
  374. package/dist/plugins/plans/progress.d.ts +82 -0
  375. package/dist/plugins/plans/progress.js +516 -0
  376. package/dist/plugins/prompt/index.d.ts +8 -0
  377. package/dist/plugins/prompt/index.js +361 -0
  378. package/dist/plugins/prompt/plugin.yaml +2 -0
  379. package/dist/plugins/recovery/console/pdl.json +50 -0
  380. package/dist/plugins/recovery/console/view.mjs +46 -0
  381. package/dist/plugins/recovery/index.d.ts +9 -0
  382. package/dist/plugins/recovery/index.js +454 -0
  383. package/dist/plugins/recovery/plugin.yaml +9 -0
  384. package/dist/plugins/recovery/probe.d.ts +11 -0
  385. package/dist/plugins/recovery/probe.js +128 -0
  386. package/dist/plugins/restart/console/pdl.json +112 -0
  387. package/dist/plugins/restart/console/view.mjs +85 -0
  388. package/dist/plugins/restart/index.d.ts +10 -0
  389. package/dist/plugins/restart/index.js +669 -0
  390. package/dist/plugins/restart/plugin.yaml +9 -0
  391. package/dist/plugins/restart/scope.d.ts +6 -0
  392. package/dist/plugins/restart/scope.js +44 -0
  393. package/dist/plugins/schedule/console/pdl.json +696 -0
  394. package/dist/plugins/schedule/console/view.mjs +398 -0
  395. package/dist/plugins/schedule/events.d.ts +76 -0
  396. package/dist/plugins/schedule/events.js +158 -0
  397. package/dist/plugins/schedule/index.d.ts +32 -0
  398. package/dist/plugins/schedule/index.js +1199 -0
  399. package/dist/plugins/schedule/plugin.yaml +26 -0
  400. package/dist/plugins/schedule/when.d.ts +14 -0
  401. package/dist/plugins/schedule/when.js +83 -0
  402. package/dist/plugins/sessions/console/pdl.json +259 -0
  403. package/dist/plugins/sessions/console/view.mjs +355 -0
  404. package/dist/plugins/sessions/index.d.ts +3 -0
  405. package/dist/plugins/sessions/index.js +126 -0
  406. package/dist/plugins/sessions/participants.d.ts +28 -0
  407. package/dist/plugins/sessions/participants.js +83 -0
  408. package/dist/plugins/sessions/plugin.yaml +3 -0
  409. package/dist/plugins/signals/console/pdl.json +802 -0
  410. package/dist/plugins/signals/console/view.mjs +284 -0
  411. package/dist/plugins/signals/content.d.ts +31 -0
  412. package/dist/plugins/signals/content.js +151 -0
  413. package/dist/plugins/signals/index.d.ts +2 -0
  414. package/dist/plugins/signals/index.js +601 -0
  415. package/dist/plugins/signals/plugin.yaml +32 -0
  416. package/dist/plugins/signals/skills/signal-sources/SKILL.md +27 -0
  417. package/dist/plugins/skills/console/pdl.json +254 -0
  418. package/dist/plugins/skills/console/view.mjs +204 -0
  419. package/dist/plugins/skills/index.d.ts +11 -0
  420. package/dist/plugins/skills/index.js +567 -0
  421. package/dist/plugins/skills/install.d.ts +69 -0
  422. package/dist/plugins/skills/install.js +265 -0
  423. package/dist/plugins/skills/plugin.yaml +24 -0
  424. package/dist/plugins/skills/safety-scan.d.ts +17 -0
  425. package/dist/plugins/skills/safety-scan.js +78 -0
  426. package/dist/plugins/skills/skills/skill-ops/SKILL.md +29 -0
  427. package/dist/plugins/terminal/console/pdl.json +115 -0
  428. package/dist/plugins/terminal/console/view.mjs +178 -0
  429. package/dist/plugins/terminal/index.d.ts +3 -0
  430. package/dist/plugins/terminal/index.js +204 -0
  431. package/dist/plugins/terminal/plugin.yaml +2 -0
  432. package/dist/plugins/tools/index.d.ts +3 -0
  433. package/dist/plugins/tools/index.js +458 -0
  434. package/dist/plugins/tools/plugin.yaml +2 -0
  435. package/dist/plugins/tools/token.d.ts +61 -0
  436. package/dist/plugins/tools/token.js +179 -0
  437. package/dist/plugins/topic-tools/index.d.ts +9 -0
  438. package/dist/plugins/topic-tools/index.js +29 -0
  439. package/dist/plugins/topic-tools/plugin.yaml +2 -0
  440. package/dist/plugins/topic-tools/skills/topic-tidy/SKILL.md +108 -0
  441. package/dist/plugins/works/console/pdl.json +120 -0
  442. package/dist/plugins/works/console/view.mjs +91 -0
  443. package/dist/plugins/works/document-words.d.ts +50 -0
  444. package/dist/plugins/works/document-words.js +27 -0
  445. package/dist/plugins/works/editors.d.ts +3 -0
  446. package/dist/plugins/works/editors.js +162 -0
  447. package/dist/plugins/works/events.d.ts +58 -0
  448. package/dist/plugins/works/events.js +82 -0
  449. package/dist/plugins/works/files.d.ts +23 -0
  450. package/dist/plugins/works/files.js +53 -0
  451. package/dist/plugins/works/index.d.ts +9 -0
  452. package/dist/plugins/works/index.js +1088 -0
  453. package/dist/plugins/works/plugin.yaml +9 -0
  454. package/dist/plugins/works/preview.d.ts +10 -0
  455. package/dist/plugins/works/preview.js +30 -0
  456. package/dist/plugins/works/project.d.ts +3 -0
  457. package/dist/plugins/works/project.js +245 -0
  458. package/dist/plugins/works/resource.d.ts +28 -0
  459. package/dist/plugins/works/resource.js +71 -0
  460. package/dist/plugins/works/retention.d.ts +36 -0
  461. package/dist/plugins/works/retention.js +295 -0
  462. package/dist/plugins/works/skills/works-ops/SKILL.md +14 -0
  463. package/dist/plugins/works/viewers.d.ts +13 -0
  464. package/dist/plugins/works/viewers.js +87 -0
  465. package/dist/plugins/works/web/anchors.js +94 -0
  466. package/dist/plugins/works/web/document-edit.js +245 -0
  467. package/dist/plugins/works/web/preview-bridge.js +142 -0
  468. package/dist/plugins/works/web/reading-position.js +74 -0
  469. package/dist/plugins/works/web/view.html +1118 -0
  470. package/dist/runtime/runtime.d.ts +210 -0
  471. package/dist/runtime/runtime.js +338 -0
  472. package/dist/sdk/adapter-kit.d.ts +1 -0
  473. package/dist/sdk/adapter-kit.js +1 -0
  474. package/dist/sdk/client.d.ts +163 -0
  475. package/dist/sdk/client.js +43 -0
  476. package/dist/sdk/contracts.d.ts +1263 -0
  477. package/dist/sdk/contracts.js +1 -0
  478. package/dist/sdk/index.d.ts +57 -0
  479. package/dist/sdk/index.js +27 -0
  480. package/dist/sdk/source-choice.d.ts +40 -0
  481. package/dist/sdk/source-choice.js +217 -0
  482. package/dist/sdk/topic-text.d.ts +5 -0
  483. package/dist/sdk/topic-text.js +26 -0
  484. package/dist/sdk/transport.d.ts +12 -0
  485. package/dist/sdk/transport.js +34 -0
  486. package/node_modules/@hostlet/adapter-kit/dist/acp.d.ts +88 -0
  487. package/node_modules/@hostlet/adapter-kit/dist/acp.js +674 -0
  488. package/node_modules/@hostlet/adapter-kit/dist/command.d.ts +18 -0
  489. package/node_modules/@hostlet/adapter-kit/dist/command.js +39 -0
  490. package/node_modules/@hostlet/adapter-kit/dist/index.d.ts +8 -0
  491. package/node_modules/@hostlet/adapter-kit/dist/index.js +6 -0
  492. package/node_modules/@hostlet/adapter-kit/dist/models.d.ts +29 -0
  493. package/node_modules/@hostlet/adapter-kit/dist/models.js +108 -0
  494. package/node_modules/@hostlet/adapter-kit/dist/quota.d.ts +3 -0
  495. package/node_modules/@hostlet/adapter-kit/dist/quota.js +4 -0
  496. package/node_modules/@hostlet/adapter-kit/dist/rpc.d.ts +29 -0
  497. package/node_modules/@hostlet/adapter-kit/dist/rpc.js +84 -0
  498. package/node_modules/@hostlet/adapter-kit/dist/upstream-failure.d.ts +5 -0
  499. package/node_modules/@hostlet/adapter-kit/dist/upstream-failure.js +37 -0
  500. package/node_modules/@hostlet/adapter-kit/package.json +16 -0
  501. package/node_modules/@hostlet/platform/dist/desktop.d.ts +16 -0
  502. package/node_modules/@hostlet/platform/dist/desktop.js +276 -0
  503. package/node_modules/@hostlet/platform/dist/files.d.ts +24 -0
  504. package/node_modules/@hostlet/platform/dist/files.js +113 -0
  505. package/node_modules/@hostlet/platform/dist/index.d.ts +6 -0
  506. package/node_modules/@hostlet/platform/dist/index.js +4 -0
  507. package/node_modules/@hostlet/platform/dist/paths.d.ts +4 -0
  508. package/node_modules/@hostlet/platform/dist/paths.js +9 -0
  509. package/node_modules/@hostlet/platform/dist/process.d.ts +45 -0
  510. package/node_modules/@hostlet/platform/dist/process.js +297 -0
  511. package/node_modules/@hostlet/platform/package.json +14 -0
  512. package/package.json +60 -4
@@ -0,0 +1,1223 @@
1
+ /* Data layer: reads over fetch, pushes over server-sent events. Everything the pages show is in HL. */
2
+ 'use strict';
3
+ const HL = { topics: new Map(), trash: [], members: [], adapters: [], mentions: [], owner: { id: 'owner', kind: 'person', name: '', handle: 'me', enabled: true }, busy: {}, drafts: [], notices: [], topic: null, ready: false };
4
+ /* What is open on screen (popups): kept where the engine watches, so opening and closing draws only what changed. */
5
+ const HU = pdlTrack({ pop: null, inbox: false, rulesStage: 0, extQ: { plugins: '' }, extSel: { plugins: '' }, reply: null, quote: '', composeRefs: {}, topicQ: '', topicTag: '', newText: localStorage.getItem('hostlet.newDraft') || '', newSending: false, newParent: '', newFrom: null, mainTabs: {}, runSessionKey: '', runSessionTopic: '', hiddenViewNotices: {} }, 'HU');
6
+ /* What a person sees fail here (a request refused, a script error) is told to the host, which writes it to its log:
7
+ a member asked to look into it reads the same thing, and can call the same route again. */
8
+ function hlReport(kind, message) { try { fetch('/api/console/report', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ kind: kind, message: String(message).slice(0, 600), where: location.hash || '#' }) }).catch(function () {}); } catch (e) { /* telling the host must never be what breaks the page */ } }
9
+ ENGINE_PARTS.report = hlReport;
10
+ /* Keyed row stores for execution panel and inbox (strictly follow tracked-source invariant). */
11
+ const HL_RUN_MEMBER_ROW = pdlKeyed('hlRunMember');
12
+ const HL_RUN_JOB_ROW = pdlKeyed('hlRunJob');
13
+ const HL_RUN_MEMBER_SNAP = new Map(), HL_RUN_JOB_SNAP = new Map();
14
+ function hlRunTouchMember(memberId) { if (memberId) HL_RUN_MEMBER_ROW.touch('m:' + memberId); }
15
+ function hlRunTouchAll() { HL_RUN_MEMBER_ROW.touchAll(); HL_RUN_JOB_ROW.touchAll(); }
16
+ function hlRunSyncParticipants(participants) {
17
+ const seen = new Set();
18
+ for (const p of (participants || [])) {
19
+ const k = 'm:' + p.memberId, j = JSON.stringify(p);
20
+ seen.add(k);
21
+ if (HL_RUN_MEMBER_SNAP.get(k) !== j) {
22
+ HL_RUN_MEMBER_SNAP.set(k, j);
23
+ HL_RUN_MEMBER_ROW.touch(k);
24
+ }
25
+ }
26
+ for (const k of Array.from(HL_RUN_MEMBER_SNAP.keys())) {
27
+ if (!seen.has(k)) { HL_RUN_MEMBER_SNAP.delete(k); HL_RUN_MEMBER_ROW.touch(k); }
28
+ }
29
+ }
30
+ function hlRunSyncJobs(jobs) {
31
+ const seen = new Set();
32
+ for (const job of (jobs || [])) {
33
+ const k = String(job.id), j = JSON.stringify(job);
34
+ seen.add(k);
35
+ if (HL_RUN_JOB_SNAP.get(k) !== j) {
36
+ HL_RUN_JOB_SNAP.set(k, j);
37
+ HL_RUN_JOB_ROW.touch(k);
38
+ }
39
+ }
40
+ for (const k of Array.from(HL_RUN_JOB_SNAP.keys())) {
41
+ if (!seen.has(k)) { HL_RUN_JOB_SNAP.delete(k); HL_RUN_JOB_ROW.touch(k); }
42
+ }
43
+ }
44
+ const HL_INBOX_ROW = pdlKeyed('hlInboxRow');
45
+ const HL_INBOX_EDIT = pdlKeyed('hlInboxEdit');
46
+ const HL_INBOX_SENDING = pdlKeyed('hlInboxSending');
47
+ const HL_INBOX_SNAP = new Map();
48
+ function hlInboxTouchAll() { HL_INBOX_ROW.touchAll(); }
49
+ const HL_STREAM_ROW = pdlKeyed('hlStreamRow');
50
+ let HL_STREAM_FAST = false, HL_STREAM_DIRTY = false, HL_MEMBERS_VER = 0, HL_OWNER_VER = 0;
51
+ function hlStreamMarkDirty() { HL_STREAM_DIRTY = true; HL_STREAM_FAST = false; }
52
+ function hlStreamTouch(mid) { if (mid) HL_STREAM_ROW.touch(mid); }
53
+ function hlStreamTouchFast(mid) {
54
+ if (mid) HL_STREAM_ROW.touch(mid);
55
+ if (!HL_STREAM_DIRTY) HL_STREAM_FAST = true;
56
+ }
57
+ function hlStreamTouchAll() {
58
+ hlStreamMarkDirty();
59
+ HL_STREAM_ROW.touchAll();
60
+ }
61
+ function hlDraftHasContent(d) {
62
+ return !!(d && typeof d !== 'string' && (String(d.text || '').trim() || (Array.isArray(d.refs) && d.refs.length)));
63
+ }
64
+ function hlInboxSync() {
65
+ const seen = new Set();
66
+ const put = function (k, entry, extra) {
67
+ seen.add(k);
68
+ const snap = JSON.stringify(extra !== undefined ? [entry, extra] : entry);
69
+ if (HL_INBOX_SNAP.get(k) !== snap) {
70
+ HL_INBOX_SNAP.set(k, snap);
71
+ if (!HL_INBOX_ROW.set(k, entry)) HL_INBOX_ROW.touch(k);
72
+ }
73
+ };
74
+ for (const m of (HL.cards || [])) {
75
+ const c = typeof hlCardOf === 'function' ? hlCardOf(m) : null;
76
+ if (!c) continue;
77
+ const ck = typeof hlCardKey === 'function' ? hlCardKey(m.id, c.ask) : m.id;
78
+ put('c:' + ck, m, typeof HLC !== 'undefined' ? [HLC.draft[ck], !!HLC.busy[ck]] : undefined);
79
+ }
80
+ for (const m of (HL.mentions || [])) put('m:' + m.messageId, m);
81
+ for (const d of (HL.drafts || [])) {
82
+ if (!hlDraftHasContent(d)) continue;
83
+ const cachedAttachments = hlReadCachedAttachments(d.topicId).map(function (file) {
84
+ return { path: file.path };
85
+ });
86
+ put('d:' + d.topicId, { ...d, cachedAttachments: cachedAttachments });
87
+ }
88
+ for (const n of (HL.notices || [])) put('n:' + n.id, n);
89
+ for (const k of Array.from(HL_INBOX_SNAP.keys())) {
90
+ if (!seen.has(k)) { HL_INBOX_SNAP.delete(k); HL_INBOX_ROW.delete(k); }
91
+ }
92
+ }
93
+ function hlFetch(method, path, body, signal) {
94
+ return hlRequest(method, path, { headers: body ? { 'content-type': 'application/json' } : {}, body: body ? JSON.stringify(body) : undefined, signal: signal });
95
+ }
96
+ let hlFilePickerOpen = false;
97
+ async function hlPickFileSources({ title, accept = '', multiple = false } = {}, signal) {
98
+ if (hlFilePickerOpen || signal?.aborted) return [];
99
+ const address = location.hash;
100
+ hlFilePickerOpen = true;
101
+ try {
102
+ const sources = webLocalConsole()
103
+ ? await hlFetch('POST', '/api/console/files/pick', { title, accept, multiple })
104
+ : (await PLATFORM.selectFiles({ accept, multiple })).map(function (file) { return { file }; });
105
+ return location.hash === address && !signal?.aborted ? sources : [];
106
+ } finally {
107
+ hlFilePickerOpen = false;
108
+ }
109
+ }
110
+ function hlRequest(method, path, request) {
111
+ return span('http.client', { method: method }, function (traceparent) {
112
+ if (traceparent && new URL(path, location.origin).origin === location.origin) {
113
+ request = { ...request, headers: new Headers(request.headers) };
114
+ request.headers.set('traceparent', traceparent);
115
+ }
116
+ return fetch(path, { ...request, method })
117
+ .then(function (res) {
118
+ return res.json().catch(function () { return {}; }).then(function (data) { if (!res.ok) { const msg = data.i18n && typeof tcText === 'function' ? tcText(data.i18n, data.error || res.statusText) : (data.error || res.statusText); hlReport('request', method + ' ' + path + ' → ' + res.status + ' ' + (data.error || res.statusText)); throw Object.assign(new Error(msg), { status: res.status, i18n: data.i18n }); } return data; }); },
119
+ function (e) { if (!request.signal?.aborted) hlReport('request', method + ' ' + path + ' → ' + tc('hl.common.networkFail', (e && e.message) || e)); throw e; });
120
+ });
121
+ }
122
+ window.addEventListener('error', function (e) { hlReport('script', (e.message || '') + (e.filename ? ' @ ' + e.filename.replace(location.origin, '') + ':' + e.lineno : '')); });
123
+ window.addEventListener('unhandledrejection', function (e) { const r = e.reason; hlReport('script', 'unhandled: ' + ((r && (r.stack || r.message)) || r)); });
124
+ function hlFail(error) { toast(error && error.i18n && typeof tcText === 'function' ? tcText(error.i18n, error.message) : String((error && error.message) || error)); }
125
+ function hlMember(id) { return HL.members.find(function (m) { return m.id === id; }); }
126
+ function hlSetOwner(owner) {
127
+ if (HL.owner.handle !== owner.handle || HL.owner.name !== owner.name || HL.owner.avatarPath !== owner.avatarPath) {
128
+ HL_OWNER_VER++;
129
+ hlMdInvalidate();
130
+ if (typeof HL_NAV_ROW !== 'undefined') HL_NAV_ROW.touchAll();
131
+ hlInboxTouchAll();
132
+ hlStreamTouchAll();
133
+ }
134
+ Object.assign(HL.owner, { avatarPath: undefined }, owner);
135
+ }
136
+ function hlTopic(id) { return HL.topics.get(id); }
137
+ function hlRememberTopic(topic) {
138
+ const prev = HL.topics.get(topic.id);
139
+ const oldParent = prev?.parentId;
140
+ if (topic.deletedAt != null) HL.topics.delete(topic.id);
141
+ else HL.topics.set(topic.id, topic);
142
+ const changed = !prev || prev.title !== topic.title || prev.pinned !== topic.pinned || prev.archivedAt !== topic.archivedAt
143
+ || prev.parentId !== topic.parentId || prev.lastActiveAt !== topic.lastActiveAt || prev.deletedAt !== topic.deletedAt;
144
+ if (changed) {
145
+ if (typeof hlNavTouchPath === 'function') {
146
+ hlNavTouchPath(topic.id);
147
+ if (oldParent && oldParent !== topic.parentId) hlNavTouchPath(oldParent);
148
+ }
149
+ }
150
+ }
151
+ /* A response issued before a deletion can still be in flight when the push arrives. */
152
+ const HL_DELETED = new Set();
153
+ function hlMarkDeletedRefs(m) {
154
+ if (HL_DELETED.has(m.replyTo)) m.replyToDeleted = true;
155
+ if (HL_DELETED.has(m.thread)) m.threadDeleted = true;
156
+ return m;
157
+ }
158
+ function hlVisibleMessages(items) { return items.filter(function (m) { return !HL_DELETED.has(m.id); }).map(hlMarkDeletedRefs); }
159
+ function hlVisibleUnread(items) { return items.filter(function (m) { return !HL_DELETED.has(m.messageId); }); }
160
+ function hlQueuedRetries(items, live) {
161
+ return Object.fromEntries((items || []).filter(function (item) {
162
+ return !HL_DELETED.has(item.replyId) && !live[item.replyId];
163
+ }).map(function (item) { return [item.replyId, item]; }));
164
+ }
165
+ function hlUpdateMessage(t, m) {
166
+ if (HL_DELETED.has(m.id)) return false;
167
+ const at = t.messages.findIndex(function (was) { return was.id === m.id; });
168
+ const current = t.live[m.id]?.turnId || t.messages[at]?.turnId;
169
+ if (current && current !== m.turnId) return false;
170
+ hlMarkDeletedRefs(m);
171
+ if (at >= 0) t.messages[at] = m;
172
+ else {
173
+ const loadedFloor = (t.chatView || 'floors') === 'floors' && m.thread && t.messages.some(function (was) { return was.id === m.thread || was.thread === m.thread; });
174
+ if (m.seq == null || (t.before != null && m.seq < t.before && !loadedFloor)) return false;
175
+ const next = t.messages.findIndex(function (was) { return was.seq > m.seq; });
176
+ t.messages.splice(next < 0 ? t.messages.length : next, 0, m);
177
+ }
178
+ if (typeof hlChatFindSettled === 'function') hlChatFindSettled(m);
179
+ return true;
180
+ }
181
+ function hlMsgRemoved(d) {
182
+ if (HL_DELETED.has(d.id)) return;
183
+ HL_DELETED.add(d.id);
184
+ const t = HL.topic;
185
+ if (t && t.id === d.topicId) {
186
+ hlSyncChanged(t);
187
+ hlStreamMarkDirty();
188
+ t.messages = hlVisibleMessages(t.messages);
189
+ delete t.live[d.id];
190
+ delete t.queuedRetries?.[d.id];
191
+ Object.values(t.live).forEach(hlMarkDeletedRefs);
192
+ t.unread = hlVisibleUnread(t.unread);
193
+ let targetCleared = false;
194
+ if (HU.quote === d.id) { HU.quote = ''; targetCleared = true; }
195
+ if (HU.floor && (HU.floor.mid === d.id || HU.floor.thread === d.id)) { HU.floor = null; targetCleared = true; }
196
+ if (targetCleared && typeof hlSyncDraftTarget === 'function') hlSyncDraftTarget(t);
197
+ if (typeof hlTrajForgetMid === 'function') hlTrajForgetMid(d.id);
198
+ else { if (HU.trajFocus === d.id) { HU.trajFocus = ''; HU.trajPick = ''; } }
199
+ t.run?.jobs.forEach(function (job) { if (job.messageId === d.id) { hlRunJobMessage(job); HL_RUN_JOB_ROW.touch(job.id); } });
200
+ }
201
+ HL.cards = hlVisibleMessages(HL.cards);
202
+ delete HLC.draft[d.id];
203
+ delete HLC.busy[d.id];
204
+ HL.mentions = hlVisibleUnread(HL.mentions);
205
+ hlInboxSync();
206
+ }
207
+ function hlRunner(byTopic, topicId, memberId, running) {
208
+ const ids = byTopic[topicId] || [];
209
+ if (running) {
210
+ if (!ids.includes(memberId)) byTopic[topicId] = ids.concat(memberId).sort();
211
+ } else {
212
+ const rest = ids.filter(function (id) { return id !== memberId; });
213
+ if (rest.length) byTopic[topicId] = rest; else delete byTopic[topicId];
214
+ }
215
+ }
216
+ /* The instance list is read whole when it changes. A slower snapshot must not overwrite newer turn events. */
217
+ let HL_BUSY_N = 0, HL_TURN_N = 0;
218
+ function hlBusy() {
219
+ const n = ++HL_BUSY_N, turn = HL_TURN_N;
220
+ return hlFetch('GET', '/api/instances').then(function (list) {
221
+ if (n !== HL_BUSY_N) return;
222
+ HLP.instances = list;
223
+ if (turn !== HL_TURN_N) return;
224
+ const busy = {};
225
+ list.forEach(function (i) { if (i.busy) hlRunner(busy, i.topicId, i.memberId, true); });
226
+ HL.busy = busy;
227
+ });
228
+ }
229
+ const HL_TRASH_KEEP = 'topics.trashKeepDays';
230
+ function hlConfigGroup(scope) { return HLP.config?.groups?.find(function (group) { return group.scope === scope; }) || null; }
231
+ function hlRegistrationGroup(group, registration) {
232
+ if (!group) return null;
233
+ const fields = group.fields.filter(function (field) { return field.registration === registration; });
234
+ const runtime = registration === 'runtime';
235
+ const steps = runtime ? [] : group.steps;
236
+ if (!fields.some(function (field) { return !field.hidden; }) && !steps?.length) return null;
237
+ return Object.assign({}, group, { fields: fields, steps: steps, variants: runtime ? undefined : group.variants,
238
+ stateKey: runtime ? group.scope + ':runtime' : group.scope });
239
+ }
240
+ function hlConfigField(scope, key) { return hlConfigGroup(scope)?.fields?.find(function (field) { return field.key === key; }) || null; }
241
+ function hlConfigApply(config) {
242
+ HLP.config = config;
243
+ if (typeof CF_STATES !== 'undefined') {
244
+ CF_STATES.forEach(function (state, scope) {
245
+ const group = hlConfigGroup(state.detail?.scope || scope);
246
+ if (!group) return;
247
+ Object.keys(state.draft || {}).forEach(function (key) {
248
+ const field = group.fields.find(function (one) { return one.key === key; });
249
+ if (!field) return;
250
+ const current = field.kind === 'secret' ? (field.hasValue ? '1' : '') : field.value == null ? '' : typeof field.value === 'object' ? JSON.stringify(field.value) : String(field.value);
251
+ if (String(state.draft[key] ?? '') === current) delete state.draft[key];
252
+ });
253
+ if (state.mapRows && typeof pcfMapParse === 'function') {
254
+ state.mapRows.forEach(function (rows, key) {
255
+ const field = group.fields.find(function (one) { return one.key === key && one.kind === 'map'; });
256
+ if (!field) return;
257
+ const base = pcfMapParse(field.value == null ? '{}' : typeof field.value === 'object' ? JSON.stringify(field.value) : String(field.value));
258
+ if (rows.length === base.length && rows.every(function (r, i) { return String(r.k ?? '') === base[i].k && String(r.v ?? '') === base[i].v; })) {
259
+ state.mapRows.delete(key);
260
+ }
261
+ });
262
+ }
263
+ });
264
+ }
265
+ }
266
+ function hlConfigRefresh() {
267
+ return hlFetch('GET', '/api/config').then(function (config) { hlConfigApply(config); return config; });
268
+ }
269
+ let HL_APPROVAL_OPS_READ = 0;
270
+ function hlApprovalOpsRead() {
271
+ const read = ++HL_APPROVAL_OPS_READ;
272
+ return hlFetch('GET', '/api/approvals/ops').then(function (data) {
273
+ if (read !== HL_APPROVAL_OPS_READ) return;
274
+ HLP.approvalOps = data;
275
+ hlDrawSoon();
276
+ return data;
277
+ }, function (error) {
278
+ if (read === HL_APPROVAL_OPS_READ && error && error.status !== 404) hlFail(error);
279
+ });
280
+ }
281
+ function hlTrashDays() { return hlConfigField('settings', HL_TRASH_KEEP)?.value; }
282
+ let HL_TRASH_READ = 0;
283
+ function hlTrashRead() {
284
+ // Static console files can be refreshed before the host has restarted with its new routes.
285
+ if (hlTrashDays() === undefined) return;
286
+ const read = ++HL_TRASH_READ;
287
+ return hlFetch('GET', '/api/trash').then(function (rows) {
288
+ if (read !== HL_TRASH_READ) return;
289
+ HL.trash = rows;
290
+ hlDrawSoon();
291
+ }, function (error) { if (read === HL_TRASH_READ) hlFail(error); });
292
+ }
293
+ function hlApplyLang(lang) {
294
+ const next = lang === 'en' ? 'en' : 'zh';
295
+ const changed = S.lang !== next;
296
+ S.lang = next;
297
+ if (typeof document !== 'undefined' && document.documentElement) document.documentElement.lang = next === 'en' ? 'en' : 'zh-CN';
298
+ if (changed) {
299
+ if (typeof hlMdInvalidate === 'function') hlMdInvalidate();
300
+ if (typeof renderForget === 'function') renderForget();
301
+ }
302
+ return changed;
303
+ }
304
+ function hlSyncLang(config) {
305
+ const entry = config?.entries?.find(function (e) { return e.key === 'host.lang'; });
306
+ return entry ? hlApplyLang(entry.value) : false;
307
+ }
308
+ function hlApplyI18n(data) {
309
+ if (!data) return;
310
+ if (data.packs && typeof defineI18n === 'function') {
311
+ Object.keys(data.packs).forEach(function (l) { defineI18n(l, data.packs[l]); });
312
+ }
313
+ if (data.lang) hlApplyLang(data.lang);
314
+ }
315
+ function hlLoadShared() {
316
+ return Promise.all([hlNavStart(), hlFetch('GET', '/api/members'), hlFetch('GET', '/api/config'), hlFetch('GET', '/api/owner'), hlFetch('GET', '/api/drafts'), hlFetch('GET', '/api/notices'), hlFetch('GET', '/api/i18n').catch(function () { return null; }), hlBusy(), hlPluginIndexStart()])
317
+ .then(function ([, members, config, owner, drafts, notices, i18nData]) { Object.assign(HL, { members, drafts, notices, ready: true }); hlSetOwner(owner); hlConfigApply(config); hlApplyI18n(i18nData); hlSyncLang(config); hlInboxSync(); return hlTrashRead(); });
318
+ }
319
+ /* What a topic's page shows beyond its channel: its description, what it uses, and the directories that can be looked at. */
320
+ function hlTopicViewAvailability(id) {
321
+ const main = hlEffectiveMain(id);
322
+ const views = hlViewEntries('panel', id).map(function (view) { return view.id; });
323
+ return JSON.stringify([main, hlTopicViewSpec(id)?.panel, views]);
324
+ }
325
+ function hlAboutRead(id) {
326
+ const topic = HL.topic, none = function () { return []; };
327
+ if (topic?.id !== id) return Promise.resolve();
328
+ const reading = topic.aboutReading = {};
329
+ return Promise.all([hlFetch('GET', '/api/topics/' + id + '/about'), hlFetch('GET', '/api/topics/' + id + '/caps'), hlFetch('GET', '/api/topics/' + id + '/roots').catch(none)])
330
+ .then(function (r) {
331
+ if (HL.topic !== topic || topic.aboutReading !== reading) return;
332
+ const views = hlTopicViewAvailability(id);
333
+ HL.about = { id: id, about: r[0], caps: r[1], roots: r[2] };
334
+ if (S.route === 'chat' && views !== hlTopicViewAvailability(id)) hlPanelApplyTopic(id);
335
+ render(true);
336
+ }, function () {});
337
+ }
338
+ function hlParticipantsRead(topic) {
339
+ const reading = topic.participantsReading = {};
340
+ return hlFetch('GET', '/api/topics/' + topic.id + '/participants').catch(function () { return null; }).then(function (participants) {
341
+ if (HL.topic !== topic || topic.participantsReading !== reading) return;
342
+ delete topic.participantsReading;
343
+ topic.participants = participants;
344
+ hlRunSyncParticipants(participants);
345
+ render(true);
346
+ });
347
+ }
348
+ function hlRunJobMessage(job) {
349
+ if (job.messageDeleted || HL_DELETED.has(job.messageId)) {
350
+ job.messageDeleted = true;
351
+ delete job.address;
352
+ }
353
+ return job;
354
+ }
355
+ /* Keep the newest response for this open topic. */
356
+ function hlRunReadJobs() {
357
+ if (!hlViewShown('run')) return;
358
+ const topic = HL.topic, run = topic.run ??= { jobs: [] };
359
+ const reading = run.reading = {}, path = '/api/jobs?topic=' + encodeURIComponent(topic.id) + '&history=true';
360
+ return hlFetch('GET', path).catch(function () { return []; }).then(function (items) {
361
+ if (HL.topic !== topic || !hlViewShown('run') || run.reading !== reading) return;
362
+ run.jobs = items.filter(function (item) { return item.topicId === topic.id; }).map(hlRunJobMessage);
363
+ hlRunSyncJobs(run.jobs);
364
+ render(true);
365
+ });
366
+ }
367
+ function hlRunMembersId(topic) { return 'hl-run-members-' + topic.id; }
368
+ function hlRunSessionKey(id) { return id == null ? 'unknown' : 'session:' + id; }
369
+ function hlRunSessionFoldKey(topic, memberId, key) { return hlRunMembersId(topic) + '-' + memberId + '-' + encodeURIComponent(key); }
370
+ function hlTurnSessionPath(topic, memberId, sessionId) {
371
+ const filter = sessionId == null ? '&withoutSession=true' : '&sessionId=' + encodeURIComponent(sessionId);
372
+ return '/api/topics/' + topic.id + '/turns?memberId=' + encodeURIComponent(memberId) + filter + '&limit=50&summary=true';
373
+ }
374
+ function hlRunSessionOpen(topic, participant, key) {
375
+ return S.folds[hlRunSessionFoldKey(topic, participant.memberId, key)] ?? (!!participant.sessionId && key === hlRunSessionKey(participant.sessionId));
376
+ }
377
+ function hlTurnId(turn) { return turn?.id || turn?.turnId || turn?.replyId || turn?.messageId; }
378
+ function hlTurnRecords(topic) {
379
+ const records = { ...topic.runTurns, ...topic.traceTurns };
380
+ for (const [id, turn] of Object.entries(topic.turns)) records[id] = { ...records[id], ...turn };
381
+ return records;
382
+ }
383
+ function hlRunTurnsBySession(topic, participant) {
384
+ const id = participant.memberId, current = participant.sessionId || null, groups = new Map();
385
+ function add(sessionId, turn) {
386
+ if (!groups.has(sessionId)) groups.set(sessionId, []);
387
+ groups.get(sessionId).push(turn);
388
+ }
389
+ const records = hlTurnRecords(topic);
390
+ for (const turn of Object.values(records)) {
391
+ if (turn.memberId !== id || (turn.endedAt == null && hlTurnId(topic.live[turn.replyId]) === hlTurnId(turn))) continue;
392
+ add(turn.sessionId || (turn.status === 'running' ? current : null), turn);
393
+ }
394
+ for (const turn of Object.values(topic.live)) {
395
+ const record = records[hlTurnId(turn)];
396
+ if (turn.memberId !== id || record?.endedAt != null) continue;
397
+ add(turn.sessionId || record?.sessionId || current, turn);
398
+ }
399
+ return groups;
400
+ }
401
+ function hlRunSessionTurnsRead(topic, memberId, session, known) {
402
+ const count = session.turnCount;
403
+ if (known >= count) return;
404
+ const id = session.sessionId, key = hlRunSessionFoldKey(topic, memberId, hlRunSessionKey(id));
405
+ const reads = topic.runTurnReads ??= new Map();
406
+ const previous = reads.get(key);
407
+ if (previous?.count === count && (previous.state !== 'failed' || previous.expansion === topic.runExpansion)) {
408
+ /* A reopened row may already be using the request in flight. */
409
+ previous.expansion = topic.runExpansion;
410
+ return;
411
+ }
412
+ const reading = { count: count, expansion: topic.runExpansion, state: 'loading' };
413
+ reads.set(key, reading);
414
+ return hlFetch('GET', hlTurnSessionPath(topic, memberId, id)).catch(function () { return null; }).then(function (turns) {
415
+ if (HL.topic !== topic || reads.get(key) !== reading) return;
416
+ reading.state = turns ? 'loaded' : 'failed';
417
+ /* Pushes may have already delivered a newer state for one of these turns. */
418
+ const ended = new Set(Object.values(topic.turns).filter(function (turn) { return turn.endedAt != null; }).map(function (turn) { return turn.id; }));
419
+ turns?.forEach(function (turn) {
420
+ if (turn.endedAt == null && ended.has(turn.id)) return; /* a silent turn may have ended on a different reply */
421
+ (topic.runTurns ??= {})[hlTurnId(turn)] = turn;
422
+ });
423
+ hlRunTouchMember(memberId);
424
+ render(true);
425
+ });
426
+ }
427
+ function hlTraceRead(topic, key) {
428
+ const reads = topic.traceReads ??= new Map();
429
+ if (reads.has(key)) return;
430
+ const reading = {}, session = hlSessParts(key);
431
+ reads.set(key, reading);
432
+ const path = hlTurnSessionPath(topic, session.memberId, hlSessId(session.sessionKey));
433
+ return hlFetch('GET', path + '&detail=true').then(function (turns) {
434
+ if (HL.topic !== topic || reads.get(key) !== reading) return;
435
+ const records = topic.traceTurns ??= {};
436
+ for (const turn of turns) {
437
+ if (turn.endedAt == null && topic.turns[turn.id]?.endedAt != null) continue;
438
+ records[turn.id] = turn;
439
+ }
440
+ render(true);
441
+ }, function (error) {
442
+ if (reads.get(key) !== reading) return;
443
+ reads.delete(key);
444
+ if (HL.topic === topic) hlFail(error);
445
+ });
446
+ }
447
+ function hlTurnInputRead(topic, turn) {
448
+ if (!turn?.inputPreview || turn.inputText !== undefined) return;
449
+ const reads = topic.turnInputs ??= new Map(), id = hlTurnId(turn);
450
+ if (reads.has(id)) return;
451
+ const read = {};
452
+ reads.set(id, read);
453
+ return hlFetch('GET', '/api/topics/' + topic.id + '/turns/' + encodeURIComponent(id) + '/input').then(function (result) {
454
+ read.text = result.inputText;
455
+ if (HL.topic === topic && reads.get(id) === read) { hlStreamMarkDirty(); hlDrawSoon(); }
456
+ }, function (error) {
457
+ read.failed = true;
458
+ if (HL.topic === topic && reads.get(id) === read) { hlStreamMarkDirty(); hlFail(error); hlDrawSoon(); }
459
+ });
460
+ }
461
+ function hlRetryPartsRead(topic, message) {
462
+ const reads = topic.retryPartsReads ??= new WeakMap();
463
+ if (reads.has(message)) return;
464
+ const read = {};
465
+ reads.set(message, read);
466
+ return hlFetch('GET', '/api/topics/' + topic.id + '/messages/' + encodeURIComponent(message.id)).then(function (full) {
467
+ if (HL.topic !== topic || HL_DELETED.has(message.id) || !topic.messages.includes(message)) return;
468
+ hlUpdateMessage(topic, full);
469
+ hlStreamMarkDirty();
470
+ hlDrawSoon();
471
+ }, function (error) {
472
+ read.failed = true;
473
+ if (HL.topic === topic && topic.messages.includes(message)) { hlStreamMarkDirty(); hlFail(error); hlDrawSoon(); }
474
+ });
475
+ }
476
+ const HL_RUN_QUOTAS = new Map();
477
+ const HL_RUN_QUOTA_AGE = 30000;
478
+ function hlRunQuotaFingerprint(member) { return JSON.stringify([member.id, member.adapter, member.key, member.options, member.env, member.loggedIn]); }
479
+ function hlRunQuotaAllowed(member) {
480
+ return member && (!('loggedIn' in member) || member.loggedIn === true) && HLP.adapters?.find(function (a) { return a.name === member.adapter; })?.capabilities.quota;
481
+ }
482
+ function hlRunQuotaRead(member, force) {
483
+ if (!hlRunQuotaAllowed(member)) return;
484
+ const fingerprint = hlRunQuotaFingerprint(member), previous = HL_RUN_QUOTAS.get(member.id);
485
+ const same = previous?.fingerprint === fingerprint;
486
+ if (same && (previous.until === Infinity || (!force && previous.until > Date.now()))) return;
487
+ const entry = { fingerprint: fingerprint, until: Infinity, windows: same ? previous.windows : [] };
488
+ HL_RUN_QUOTAS.set(member.id, entry);
489
+ hlRunTouchMember(member.id);
490
+ hlDrawSoon();
491
+ const account = 'loggedIn' in member;
492
+ const path = '/api/adapters/' + encodeURIComponent(member.adapter) + '/quota?' + (account ? 'key=' : 'member=') + encodeURIComponent(member.id);
493
+ return hlFetch('GET', path).catch(function () { return []; }).then(function (windows) {
494
+ const current = account ? HLP.keys.find(function (item) { return item.id === member.id; }) : hlMember(member.id);
495
+ if (HL_RUN_QUOTAS.get(member.id) !== entry) return;
496
+ if (!current || hlRunQuotaFingerprint(current) !== fingerprint) { HL_RUN_QUOTAS.delete(member.id); return; }
497
+ entry.windows = windows;
498
+ entry.until = Date.now() + HL_RUN_QUOTA_AGE;
499
+ hlRunTouchMember(member.id);
500
+ hlDrawSoon();
501
+ });
502
+ }
503
+ function hlRunQuotaWindows(member) {
504
+ if (!hlRunQuotaAllowed(member)) return [];
505
+ const entry = HL_RUN_QUOTAS.get(member.id);
506
+ return entry?.fingerprint === hlRunQuotaFingerprint(member) ? entry.windows : [];
507
+ }
508
+ function hlRunQuotaLoading(member) {
509
+ if (!hlRunQuotaAllowed(member)) return false;
510
+ const entry = HL_RUN_QUOTAS.get(member.id);
511
+ return entry?.fingerprint === hlRunQuotaFingerprint(member) && entry.until === Infinity;
512
+ }
513
+ function hlMembersShown() { return S.route === 'members' || S.route === 'member'; }
514
+ function hlQuotaListShown() { return hlMembersShown(); }
515
+ const HL_VISIBLE_MEMBER_QUOTAS = new WeakSet();
516
+ function hlReadMemberQuota(el) {
517
+ const member = hlMember(el.dataset.memberQuota);
518
+ if (member && HLP.adapters?.find(function (a) { return a.name === member.adapter; })?.capabilities.quota) hlRunQuotaRead(member);
519
+ }
520
+ nearView('[data-member-quota]', function (el, visible) {
521
+ if (visible) {
522
+ HL_VISIBLE_MEMBER_QUOTAS.add(el);
523
+ if (hlQuotaListShown()) hlReadMemberQuota(el);
524
+ } else HL_VISIBLE_MEMBER_QUOTAS.delete(el);
525
+ }, 0);
526
+ function hlRefreshVisibleMemberQuotas() {
527
+ if (!hlQuotaListShown()) return;
528
+ document.querySelectorAll('[data-member-quota]').forEach(function (el) {
529
+ if (HL_VISIBLE_MEMBER_QUOTAS.has(el)) hlReadMemberQuota(el);
530
+ });
531
+ }
532
+ /* The workbench and its member rows use the ordinary tab and fold state. A redraw alone never starts another read. */
533
+ let HL_RUN_VIEW = null;
534
+ function hlRunViewChanged() {
535
+ const topic = hlViewShown('run') ? HL.topic : null;
536
+ const newlyShown = topic !== HL_RUN_VIEW;
537
+ if (newlyShown) {
538
+ HL_RUN_VIEW = topic;
539
+ if (topic) {
540
+ hlRunReadJobs();
541
+ if (!HLP.adapters) hlAdaptersRead().then(hlDrawSoon, function () {});
542
+ }
543
+ }
544
+ if (!topic) return;
545
+ const list = hlRunMembersId(topic), key = S.folds[FOLD_ROW + list];
546
+ if (topic.runExpansion?.key !== key) topic.runExpansion = { key: key };
547
+ const expanded = topic.participants?.find(function (p) { return rowOpen(list, 'm:' + p.memberId); });
548
+ if (!expanded) return;
549
+ const groups = hlRunTurnsBySession(topic, expanded);
550
+ for (const session of expanded.sessions) {
551
+ if (hlRunSessionOpen(topic, expanded, hlRunSessionKey(session.sessionId))) {
552
+ hlRunSessionTurnsRead(topic, expanded.memberId, session, groups.get(session.sessionId)?.length || 0);
553
+ }
554
+ }
555
+ const member = !expanded.removed && hlMember(expanded.memberId);
556
+ const fingerprint = hlRunQuotaAllowed(member) ? hlRunQuotaFingerprint(member) : null;
557
+ if (newlyShown || topic.runExpansion.quota !== fingerprint) {
558
+ topic.runExpansion.quota = fingerprint;
559
+ if (fingerprint) hlRunQuotaRead(member);
560
+ }
561
+ }
562
+ onRender(hlRunViewChanged);
563
+ function hlFloorStoreKey(id) { return 'open-floors:' + id; }
564
+ function hlStreamPlace(id) {
565
+ if (!id || HL.topic?.id !== id || S.route !== 'chat') return '';
566
+ if (hlEffectiveMain(id) === HL_CONVERSATION.id) return 'main';
567
+ return S.panel && ENGINE_PARTS.tabVisible('panel').includes(HL_CONVERSATION.id) ? 'panel' : '';
568
+ }
569
+ function hlStreamVisible(id) { return document.visibilityState === 'visible' && !!hlStreamPlace(id); }
570
+ function hlViewedTopic() { return HL.topic && hlStreamVisible(HL.topic.id) ? HL.topic.id : undefined; }
571
+ const HL_READS = new Map();
572
+ function hlReadViewed() {
573
+ const id = hlViewedTopic();
574
+ if (!id || !hlNavUnread(id)) return;
575
+ const pending = HL_READS.get(id);
576
+ if (pending) { pending.again = true; return; }
577
+ const reading = { again: false };
578
+ HL_READS.set(id, reading);
579
+ hlFetch('POST', '/api/topics/' + id + '/read', { messageId: null }).then(function () {
580
+ HL_READS.delete(id);
581
+ if (reading.again && id === hlViewedTopic()) hlReadViewed();
582
+ }, function (error) { HL_READS.delete(id); hlFail(error); });
583
+ }
584
+ let HL_VIEWED;
585
+ function hlViewChanged() {
586
+ const id = hlViewedTopic();
587
+ if (id === HL_VIEWED) return;
588
+ HL_VIEWED = id;
589
+ if (id) hlReadViewed();
590
+ }
591
+ onRender(hlViewChanged);
592
+ const hlDraftWrites = new Map();
593
+ /* A draft save and a send for the same topic use each other's acknowledged revision. */
594
+ function hlDraftWrite(topicId, write) {
595
+ const pending = (hlDraftWrites.get(topicId) || Promise.resolve()).then(write, write);
596
+ hlDraftWrites.set(topicId, pending);
597
+ const release = function () { if (hlDraftWrites.get(topicId) === pending) hlDraftWrites.delete(topicId); };
598
+ pending.then(release, release);
599
+ return pending;
600
+ }
601
+ function hlReadCachedAttachments(id) { return JSON.parse(localStorage.getItem('hostlet.attach.' + id) || '[]'); }
602
+ function hlAttachKeep(id, files) {
603
+ const key = 'hostlet.attach.' + id;
604
+ if (files.length) localStorage.setItem(key, JSON.stringify(files));
605
+ else localStorage.removeItem(key);
606
+ }
607
+ /* A draft save can reach the host before its receipt. Retain its files through reads of earlier revisions. */
608
+ function hlStageDraftAttachments(id, files, pendingRevision) {
609
+ if (!files.length) return;
610
+ const cached = new Map(hlReadCachedAttachments(id).map(function (file) { return [file.path, file]; }));
611
+ for (const file of files) {
612
+ if ((cached.get(file.path)?.pendingRevision ?? 0) < pendingRevision) {
613
+ cached.set(file.path, { ...file, pendingRevision: pendingRevision });
614
+ }
615
+ }
616
+ hlAttachKeep(id, Array.from(cached.values()));
617
+ }
618
+ function hlRestoreDraftAttachments(id, draft) {
619
+ const files = hlReadCachedAttachments(id);
620
+ if (!files.length) return files;
621
+ const attached = hlAttachmentsInText(files, draft.text);
622
+ const referenced = new Set(attached);
623
+ const retained = files.filter(function (file) {
624
+ return referenced.has(file) || (file.pendingRevision ?? 0) > draft.revision;
625
+ });
626
+ if (retained.length !== files.length) hlAttachKeep(id, retained);
627
+ return attached;
628
+ }
629
+ function hlChatViewStoreKey(id) { return 'chatView:' + id; }
630
+ function hlChatViewMode(id) { return id && store.get(hlChatViewStoreKey(id), 'floors') === 'timeline' ? 'timeline' : 'floors'; }
631
+ function hlFloorKeyOf(m) { return m.thread && m.thread !== m.id ? m.thread : m.id; }
632
+ function hlFloorSeqBounds(messages) {
633
+ const byFloor = new Map();
634
+ for (const m of messages || []) {
635
+ if (m.seq == null) continue;
636
+ const key = hlFloorKeyOf(m), cur = byFloor.get(key);
637
+ if (cur == null || m.seq > cur) byFloor.set(key, m.seq);
638
+ }
639
+ let min = Infinity, max = -Infinity;
640
+ for (const seq of byFloor.values()) {
641
+ if (seq < min) min = seq;
642
+ if (seq > max) max = seq;
643
+ }
644
+ return { min: Number.isFinite(min) ? min : undefined, max: Number.isFinite(max) ? max : undefined, byFloor: byFloor };
645
+ }
646
+ function hlPageCursor(messages, mode) {
647
+ return mode === 'floors' ? hlFloorSeqBounds(messages).min : messages?.[0]?.seq;
648
+ }
649
+ function hlEnsureFloor(t, thread) {
650
+ if (!t || !thread || (t.chatView || 'floors') !== 'floors') return;
651
+ if (t.messages.some(function (m) { return m.id === thread; })) return;
652
+ const reads = t.floorReads ??= new Set();
653
+ if (reads.has(thread)) return;
654
+ reads.add(thread);
655
+ hlFetch('GET', '/api/topics/' + t.id + '/messages?thread=' + encodeURIComponent(thread) + '&summary=true').then(function (page) {
656
+ if (HL.topic !== t || (t.chatView || 'floors') !== 'floors') return;
657
+ if (!page.length) return;
658
+ const byId = new Map(hlVisibleMessages(page).map(function (m) { return [m.id, m]; }));
659
+ t.messages.forEach(function (m) { byId.set(m.id, m); });
660
+ t.messages = Array.from(byId.values()).sort(function (a, b) { return a.seq - b.seq; });
661
+ if (typeof hlStreamMarkDirty === 'function') hlStreamMarkDirty();
662
+ hlDrawSoon();
663
+ }, function () {
664
+ reads.delete(thread);
665
+ });
666
+ }
667
+ function hlEnsureDraftTarget(t, mid, kind) {
668
+ if (!t || !mid || t.draftTargets?.[mid]) return;
669
+ hlFetch('GET', '/api/topics/' + t.id + '/messages/' + encodeURIComponent(mid)).then(function (msg) {
670
+ if (HL.topic !== t || !msg) return;
671
+ (t.draftTargets ||= {})[msg.id] = msg;
672
+ if (kind === 'reply' && HU.floor?.mid === mid) {
673
+ const thread = msg.thread && msg.thread !== msg.id ? msg.thread : msg.id;
674
+ HU.floor = { thread: thread, mid: msg.id };
675
+ if (!t.messages.some(function (m) { return m.id === thread; })) {
676
+ if (thread !== msg.id) hlEnsureDraftTarget(t, thread, 'head');
677
+ hlEnsureFloor(t, thread);
678
+ }
679
+ }
680
+ hlDrawSoon();
681
+ }, function () {});
682
+ }
683
+ /* Recent execution summaries can follow the first conversation frame. */
684
+ function hlQueueRecentTurns(topic) {
685
+ const read = bindSpan(function () {
686
+ if (HL.topic !== topic) return;
687
+ hlFetch('GET', '/api/topics/' + topic.id + '/turns?summary=true').then(function (recent) {
688
+ if (HL.topic !== topic || !recent.length) return;
689
+ const historical = Object.fromEntries(recent.map(function (turn) { return [hlTurnId(turn), turn]; }));
690
+ // Pushes or a reconnect can already have a newer record for a turn.
691
+ topic.turns = Object.assign(historical, topic.turns);
692
+ for (const [mid, live] of Object.entries(topic.live)) {
693
+ if (topic.turns[hlTurnId(live)]?.endedAt != null) delete topic.live[mid];
694
+ }
695
+ hlRunTouchAll();
696
+ hlStreamMarkDirty();
697
+ if (S.route === 'chat') hlDrawSoon();
698
+ }, function (error) {
699
+ if (HL.topic === topic && S.route === 'chat') hlFail(error);
700
+ });
701
+ });
702
+ if (document.visibilityState !== 'visible') queueMicrotask(read);
703
+ else requestAnimationFrame(function () { setTimeout(read, 0); });
704
+ }
705
+ function hlOpenTopic(id, signal) {
706
+ const chatView = hlChatViewMode(id);
707
+ const view = hlTopicLocalRead(id, signal).catch(function () { return { exists: false, canUndo: false }; });
708
+ const record = hlNavEnsure(id, signal);
709
+ if (typeof hlDraftFlushTopic === 'function') hlDraftFlushTopic(id);
710
+ const readChannel = function () {
711
+ return Promise.all(['messages?view=' + chatView + '&summary=true', 'state', 'draft'].map(function (name) {
712
+ return hlFetch('GET', '/api/topics/' + id + '/' + name, undefined, signal);
713
+ }));
714
+ };
715
+ const pendingWrite = hlDraftWrites.get(id);
716
+ const channel = pendingWrite ? pendingWrite.then(bindSpan(readChannel), bindSpan(readChannel)) : readChannel();
717
+ return Promise.all([channel, view, record])
718
+ .then(bindSpan(function ([r, localView]) {
719
+ if (signal?.aborted) return null;
720
+ const live = {}, rawDraft = r[2] || { revision: 0, text: '', replyTo: null, quote: null, refs: [] }, messages = hlVisibleMessages(r[0]);
721
+ const refs = Array.isArray(rawDraft.refs) ? rawDraft.refs.slice() : [];
722
+ const draft = { revision: rawDraft.revision ?? 0, text: rawDraft.text || '', replyTo: rawDraft.replyTo || null, quote: rawDraft.quote || null, refs: refs };
723
+ const topicRefs = new Map();
724
+ const text = typeof hlHydrateTopicMarks === 'function' ? hlHydrateTopicMarks(draft.text, { topicRefs: topicRefs }) : draft.text;
725
+ const finished = new Set(messages.filter(function (m) { return m.status !== 'processing'; }).map(function (m) { return m.turnId; }).filter(Boolean));
726
+ r[1].live.forEach(function (t) { if (!finished.has(t.turnId) && !HL_DELETED.has(t.messageId)) live[t.messageId] = hlMarkDeletedRefs(t); });
727
+ hlPanelRemember();
728
+ hlDraftFlush();
729
+ const targetReply = draft.replyTo;
730
+ const targetQuote = !targetReply && draft.quote ? draft.quote : '';
731
+ let floor = null;
732
+ if (targetReply) {
733
+ const msg = messages.find(function (m) { return m.id === targetReply; });
734
+ const thread = msg && msg.thread && msg.thread !== msg.id ? msg.thread : targetReply;
735
+ floor = { thread: thread, mid: targetReply };
736
+ }
737
+ HU.floor = floor;
738
+ HU.quote = targetQuote;
739
+ if (refs.length) HU.composeRefs[id] = refs;
740
+ else delete HU.composeRefs[id];
741
+ HU.trajFocus = '';
742
+ HU.trajPick = '';
743
+ HU.trajPickOf = {};
744
+ HU.trajTarget = {};
745
+ HU.runSessionKey = '';
746
+ HU.runSessionTopic = '';
747
+ S.thread = id;
748
+ const topic = HL.topic = { id: id, chatView: chatView, messages: messages, live: live, unread: hlVisibleUnread(r[1].unread), queuedRetries: hlQueuedRetries(r[1].queuedRetries, live), turns: {}, draft: draft, draftTargets: {}, text: text, topicRefs: topicRefs, attach: hlRestoreDraftAttachments(id, draft), before: hlPageCursor(r[0], chatView), more: r[0].length >= 50,
749
+ openFloors: new Set(store.get(hlFloorStoreKey(id), [])), closedFloors: new Set(),
750
+ foldSeq: messages.reduce(function (n, m) { return Math.max(n, m.seq || 0); }, 0), expandedRetries: new Set(), floorReads: new Set() };
751
+ if (floor && !messages.some(function (m) { return m.id === floor.mid; })) hlEnsureDraftTarget(HL.topic, floor.mid, 'reply');
752
+ else if (floor && !messages.some(function (m) { return m.id === floor.thread; })) hlEnsureFloor(HL.topic, floor.thread);
753
+ else if (targetQuote && !messages.some(function (m) { return m.id === targetQuote; })) hlEnsureDraftTarget(HL.topic, targetQuote, 'quote');
754
+ Object.values(live).forEach(function (turn) { if (turn.thread && turn.thread !== turn.messageId && !turn.threadDeleted) hlEnsureFloor(HL.topic, turn.thread); });
755
+ hlTopicLocalPrepare(HL.topic, localView);
756
+ hlFilesFor();
757
+ hlAboutRead(id);
758
+ hlRunTouchAll();
759
+ hlStreamMarkDirty();
760
+ hlParticipantsRead(HL.topic);
761
+ hlPanelApplyTopic(id);
762
+ hlQueueRecentTurns(topic);
763
+ return topic;
764
+ }));
765
+ }
766
+ function hlSyncChanged(t) { if (t?.resync) t.resync.changed = true; }
767
+ /* The host returns one snapshot of the loaded range and the turns. Keep the same starting
768
+ sequence until the snapshot arrives, even when a newer connection receives an append first. */
769
+ async function hlResyncTopic() {
770
+ const t = HL.topic;
771
+ if (!t || S.route !== 'chat') return;
772
+ const view = t.chatView || 'floors';
773
+ const from = Math.min(t.resync?.from ?? Infinity, t.before ?? hlPageCursor(t.messages, view) ?? 1);
774
+ t.resync?.controller.abort();
775
+ const controller = new AbortController(), { signal } = controller;
776
+ const read = t.resync = { from, controller, changed: false };
777
+ const current = function () { return HL.topic === t && S.route === 'chat' && t.resync === read; };
778
+ try {
779
+ const state = await hlFetch('GET', '/api/topics/' + t.id + '/state?from=' + from + '&view=' + view + '&summary=true', undefined, signal);
780
+ if (!current()) return;
781
+ if (read.changed) return hlResyncTopic();
782
+
783
+ const newer = t.messages.filter(function (m) { return m.seq > state.through; });
784
+ const live = {};
785
+ state.live.forEach(function (turn) {
786
+ if (HL_DELETED.has(turn.messageId)) return;
787
+ const seen = t.live[turn.messageId];
788
+ if (seen?.turnId === turn.turnId) {
789
+ seen.parts.forEach(function (part, i) {
790
+ if (hlNewerStreamPart(part, turn.parts[i])) turn.parts[i] = part;
791
+ });
792
+ turn.model ||= seen.model;
793
+ }
794
+ live[turn.messageId] = hlMarkDeletedRefs(turn);
795
+ });
796
+ if (view === 'floors') {
797
+ const byFloor = hlFloorSeqBounds(t.messages).byFloor;
798
+ const older = t.messages.filter(function (m) { return (byFloor.get(hlFloorKeyOf(m)) ?? m.seq) < from; });
799
+ const byId = new Map();
800
+ older.forEach(function (m) { byId.set(m.id, m); });
801
+ state.messages.forEach(function (m) { byId.set(m.id, m); });
802
+ newer.forEach(function (m) { byId.set(m.id, m); });
803
+ t.messages = hlVisibleMessages(Array.from(byId.values())).sort(function (a, b) { return a.seq - b.seq; });
804
+ } else {
805
+ const older = t.messages.filter(function (m) { return m.seq < from; });
806
+ t.messages = hlVisibleMessages(older.concat(state.messages, newer));
807
+ }
808
+ t.live = live;
809
+ Object.values(live).forEach(function (turn) { if (turn.thread && turn.thread !== turn.messageId && !turn.threadDeleted) hlEnsureFloor(t, turn.thread); });
810
+ t.unread = hlVisibleUnread(state.unread);
811
+ t.queuedRetries = hlQueuedRetries(state.queuedRetries, live);
812
+ t.turns = Object.fromEntries(state.turns.map(function (turn) { return [hlTurnId(turn), turn]; }));
813
+ for (const records of [t.runTurns, t.traceTurns]) {
814
+ for (const [id, turn] of Object.entries(records || {})) {
815
+ if (turn.endedAt == null && hlTurnId(live[turn.replyId]) !== id) delete records[id];
816
+ }
817
+ }
818
+ // Keep already loaded steps on screen while an open execution view refreshes.
819
+ t.runTurnReads?.clear();
820
+ t.traceReads?.clear();
821
+ if (HU.runSessionTopic === t.id && HU.runSessionKey && hlViewShown('run')) hlTraceRead(t, HU.runSessionKey);
822
+ t.before = Math.min(t.before ?? from, from);
823
+ t.more = from > 1 && t.more;
824
+ hlRunTouchAll();
825
+ hlStreamMarkDirty();
826
+ hlParticipantsRead(t);
827
+ hlDrawSoon();
828
+ } catch (error) {
829
+ if (current()) hlFail(error);
830
+ } finally {
831
+ if (t.resync === read) delete t.resync;
832
+ }
833
+ }
834
+ function hlNewerStreamPart(part, before) {
835
+ if (!before) return true;
836
+ if (part.type !== before.type) return false;
837
+ if (part.type === 'text' || part.type === 'thinking') return part.text.length > before.text.length && part.text.startsWith(before.text);
838
+ if (part.type === 'tool') return part.id === before.id && part.status !== 'running' && before.status === 'running';
839
+ return part.type === 'card' && part.ask === before.ask && part.rev > before.rev;
840
+ }
841
+ /* Repeated picks share the read; a different destination takes its place. The topic on screen stays editable while it loads. */
842
+ let HL_TOPIC_OPEN = null;
843
+ function hlCancelTopicOpen() {
844
+ const pending = HL_TOPIC_OPEN;
845
+ HL_TOPIC_OPEN = null;
846
+ if (pending) pending.controller.abort();
847
+ }
848
+ NAV_RESET.push(function (o) { if (!o.popupsOnly) hlCancelTopicOpen(); });
849
+ /* Load the older pages leading back to a selected message. */
850
+ async function hlReadMessageRange(id, from, before, signal, view) {
851
+ const size = 200, history = [], mode = view || 'floors';
852
+ const path = '/api/topics/' + id + '/messages?view=' + mode + '&before=' + before + '&limit=' + size + '&summary=true';
853
+ while (from < before) {
854
+ const page = await hlFetch('GET', path + '&from=' + from, undefined, signal);
855
+ if (signal.aborted) return;
856
+ history.push(...page);
857
+ if (page.length < size) break;
858
+ const max = mode === 'floors' ? hlFloorSeqBounds(page).max : page.at(-1)?.seq;
859
+ if (max == null || max < from) break;
860
+ from = max + 1;
861
+ }
862
+ return history;
863
+ }
864
+ function hlMergeMessageRange(t, history, from) {
865
+ const byId = new Map(hlVisibleMessages(history).map(function (m) { return [m.id, m]; }));
866
+ t.messages.forEach(function (m) { byId.set(m.id, m); }); /* A push or an earlier-page read may already have newer data. */
867
+ t.messages = Array.from(byId.values()).sort(function (a, b) { return a.seq - b.seq; });
868
+ if (t.before == null || from < t.before) {
869
+ t.more = from > 1 && (t.before == null || t.more);
870
+ t.before = from;
871
+ }
872
+ hlStreamMarkDirty();
873
+ }
874
+ function hlSelectTopic(id, show) {
875
+ if (HL_TOPIC_OPEN?.id === id) {
876
+ HL_TOPIC_OPEN.show = show;
877
+ return HL_TOPIC_OPEN.promise;
878
+ }
879
+ hlCancelTopicOpen();
880
+ if (HL.topic?.id === id && S.route === 'chat') {
881
+ show(HL.topic, false);
882
+ return Promise.resolve(HL.topic);
883
+ }
884
+ const job = HL_TOPIC_OPEN = { id: id, show: show, controller: new AbortController() };
885
+ const load = () => hlOpenTopic(id, job.controller.signal).then(bindSpan(function (topic) {
886
+ if (HL_TOPIC_OPEN !== job) return null;
887
+ HL_TOPIC_OPEN = null;
888
+ job.show(topic, true);
889
+ return topic;
890
+ }), function (e) {
891
+ if (HL_TOPIC_OPEN === job) { hlCancelTopicOpen(); hlFail(e); }
892
+ return null;
893
+ });
894
+ if (!ENGINE_PARTS.spans && !ENGINE_PARTS.measurements?.['topic.open']) {
895
+ job.promise = load();
896
+ return job.promise;
897
+ }
898
+ const attributes = { topicId: id };
899
+ if (ACT_CLICK_AT != null) attributes.inputWaitMs = Math.max(0, performance.now() - ACT_CLICK_AT);
900
+ else if (!HL.topic) attributes.initial = true;
901
+ // The diagnostic span waits for presentation. The navigation promise still resolves after opening.
902
+ span('topic.open', attributes, () => {
903
+ job.promise = load();
904
+ return job.promise.then(bindSpan(async function (topic) {
905
+ if (topic) attributes.paint = await hlFirstTopicFrame(topic);
906
+ else attributes.outcome = 'canceled';
907
+ }));
908
+ }).catch(reportError);
909
+ return job.promise;
910
+ }
911
+ /* The engine's facade: the app owns the requests, and components exchange events with it in this page. */
912
+ const hlIo = new Map();
913
+ function hlIoOn(name, fn) {
914
+ if (!hlIo.has(name)) hlIo.set(name, new Set());
915
+ const listeners = hlIo.get(name);
916
+ listeners.add(fn);
917
+ return function () { listeners.delete(fn); };
918
+ }
919
+ const api = { get: function () { return {}; }, post: function () { return {}; }, on: hlIoOn, send: function (name, data) { hlIo.get(name)?.forEach(function (fn) { fn(data); }); }, file: function (p) { return p; }, act: function () { return null; }, now: function () { return Date.now(); }, status: function () { return ''; } };
920
+ ioConnect(api);
921
+ /* A push changes what is known; the page is drawn again once for all that arrived before the next frame. A member that
922
+ streams sends a dozen a second, several members several dozen: drawn one by one they keep the page busy with nothing
923
+ new to show, and a click waits behind them. A page that is not being shown gets no frames, so a timer draws it. */
924
+ let HL_DRAW = null;
925
+ function hlDrawSoon() {
926
+ if (HL_DRAW) return;
927
+ const draw = bindSpan(function () { if (HL_DRAW !== draw) return; HL_DRAW = null; clearTimeout(timer); render(true); }), timer = setTimeout(draw, 200);
928
+ HL_DRAW = draw; requestAnimationFrame(draw);
929
+ }
930
+ /* The event stream is the page's only long-lived connection. A page the browser keeps for going back stays alive in
931
+ the back-forward cache with its stream open, so a few visits in the same tab hold every connection the browser gives
932
+ one host and the next page cannot read anything. Close it when this page is put away; open it again when the same
933
+ page comes back, and the host can release what the old subscription held. */
934
+ /* Deliver each named push once to its subscribers so the receive span measures the whole dispatch.
935
+ Keep native EventTarget semantics for listener exceptions and removal during dispatch. */
936
+ function hlPushStream(url) {
937
+ const source = new EventSource(url), listeners = new Map();
938
+ return {
939
+ addEventListener(name, fn) {
940
+ let group = listeners.get(name);
941
+ if (!group) {
942
+ group = new Set();
943
+ listeners.set(name, group);
944
+ source.addEventListener(name, e => span('sse.receive', { event: name }, () => {
945
+ for (const listener of [...group]) {
946
+ if (!group.has(listener)) continue;
947
+ try { listener(e); } catch (error) { reportError(error); }
948
+ }
949
+ }));
950
+ }
951
+ group.add(fn);
952
+ },
953
+ removeEventListener(name, fn) { listeners.get(name)?.delete(fn); },
954
+ close() { source.close(); },
955
+ };
956
+ }
957
+ let HL_EVENTS = null;
958
+ function hlConnect() {
959
+ if (HL_EVENTS) return;
960
+ let noticeRead = 0, membersRead = 0, stateRead = 0;
961
+ /* A handler that returns false says nothing on this page can have changed (a stream in a topic that is not the open one): no redraw. */
962
+ const es = HL_EVENTS = hlPushStream('/api/events'), on = function (name, fn) { es.addEventListener(name, function (e) { if (fn(JSON.parse(e.data)) !== false) hlDrawSoon(); }); };
963
+ referenceLabelsListen(es);
964
+ hlNavListen(es);
965
+ es.addEventListener('open', function () {
966
+ if (HL.cards.length || HL.mentions.length) {
967
+ HL.cards = [];
968
+ HL.mentions = [];
969
+ hlInboxSync();
970
+ hlDrawSoon();
971
+ }
972
+ });
973
+ es.addEventListener('open', hlResyncTopic); /* a reconnect replaces what was last seen with the host's live state */
974
+ on('cards.changed', function (list) { hlCardsSet(list); });
975
+ on('inbox.changed', function (messages) {
976
+ HL.mentions = hlVisibleUnread(messages || []);
977
+ hlInboxSync();
978
+ });
979
+ const readNotices = function () {
980
+ const read = ++noticeRead;
981
+ hlFetch('GET', '/api/notices').then(function (notices) {
982
+ if (read !== noticeRead) return;
983
+ HL.notices = notices;
984
+ hlInboxSync();
985
+ hlDrawSoon();
986
+ }, function () { /* The stream will refresh the inbox when it reconnects. */ });
987
+ return false;
988
+ };
989
+ es.addEventListener('open', readNotices);
990
+ hlPluginListen(es);
991
+ hlTopicViewListen(es);
992
+ let trashRefresh;
993
+ const trashChanged = function () {
994
+ clearTimeout(trashRefresh);
995
+ trashRefresh = setTimeout(hlTrashRead, 40);
996
+ };
997
+ es.addEventListener('open', trashChanged);
998
+ on('topic.updated', function (d) {
999
+ const previous = hlTopic(d.topic.id);
1000
+ if (d.trashChanged || d.topic.deletedAt !== null) trashChanged();
1001
+ const here = HL.topic?.id === d.topic.id && d.topic.deletedAt === null;
1002
+ const id = HL.topic?.id;
1003
+ const path = id && ((HL.about?.id === id && HL.about.about?.path) || hlTopicPath(id));
1004
+ const upstream = !here && path && path.some(function (topic) { return topic.id === d.topic.id; })
1005
+ && (d.capsChanged?.length || previous?.parentId !== d.topic.parentId);
1006
+ const views = (here || upstream) && S.route === 'chat' ? hlTopicViewAvailability(id) : null;
1007
+ if (previous || here || upstream || HL_TOPIC_CATALOG || hlNavAcceptTopic(d.topic, d.tree)) hlRememberTopic(d.topic);
1008
+ hlNavTopicPatched(d.topic, d.tree);
1009
+ if (here) {
1010
+ hlStreamMarkDirty();
1011
+ if (previous?.archivedAt !== d.topic.archivedAt) {
1012
+ hlRunTouchAll();
1013
+ if (d.topic.archivedAt != null) {
1014
+ HU.floor = null;
1015
+ HU.quote = '';
1016
+ hlComposerPickerClose();
1017
+ } else hlRevealTopic(d.topic.id);
1018
+ }
1019
+ hlParticipantsRead(HL.topic);
1020
+ }
1021
+ if (views !== null && views !== hlTopicViewAvailability(id)) hlPanelApplyTopic(id);
1022
+ if (here || upstream) hlAboutRead(id);
1023
+ });
1024
+ on('topic.purged', function (d) {
1025
+ const gone = new Set(d.topicIds);
1026
+ for (const id of gone) {
1027
+ delete HU.composeRefs[id];
1028
+ hlAttachKeep(id, []);
1029
+ }
1030
+ HL.drafts = HL.drafts.filter(function (draft) { return !gone.has(draft.topicId); });
1031
+ HL.cards = HL.cards.filter(function (card) { return !gone.has(card.topicId); });
1032
+ hlInboxSync();
1033
+ trashChanged();
1034
+ });
1035
+ /* A plugin asks for an address to be shown beside the topic it belongs to (a work a member opened): shown when that topic is the one open. */
1036
+ on('console.open', function (d) {
1037
+ if (HL.topic && HL.topic.id === d.topicId && S.route === 'chat') hlWorkbenchShow(d.url, d.title, d.frame, d.initialUrl);
1038
+ return false;
1039
+ });
1040
+ on('resource.changed', function (change) {
1041
+ if (change.kind === 'moved') documentDraftStore(window).move(change);
1042
+ return hlFileTreeChanged(change);
1043
+ });
1044
+ on('works.viewers', function () {
1045
+ if (HL.topic && S.route === 'chat') {
1046
+ HLF.files = {};
1047
+ HLF.dirs = {};
1048
+ hlAboutRead(HL.topic.id);
1049
+ hlDrawSoon();
1050
+ }
1051
+ return false;
1052
+ });
1053
+ on('notices.changed', readNotices);
1054
+ on('drafts.changed', function () { hlFetch('GET', '/api/drafts').then(function (d) { HL.drafts = d; hlInboxSync(); hlDrawSoon(); }, function () {}); return false; });
1055
+ on('members.changed', function (d) {
1056
+ if (d.removed) HL_RUN_QUOTAS.delete(d.id);
1057
+ const reading = ++membersRead;
1058
+ hlFetch('GET', '/api/members').then(function (m) {
1059
+ if (reading !== membersRead) return;
1060
+ HL.members = m;
1061
+ HL_MEMBERS_VER++;
1062
+ if (typeof HL_NAV_ROW !== 'undefined') HL_NAV_ROW.touchAll();
1063
+ hlRunTouchAll();
1064
+ hlInboxTouchAll();
1065
+ hlStreamTouchAll();
1066
+ if (HL.topic) hlParticipantsRead(HL.topic);
1067
+ hlMdInvalidate();
1068
+ hlRefreshVisibleMemberQuotas();
1069
+ render(true);
1070
+ });
1071
+ });
1072
+ on('turn.started', function (d) {
1073
+ const t = HL.topic?.id === d.turn.topicId ? HL.topic : null;
1074
+ const active = t?.live[d.turn.messageId];
1075
+ if (t?.turns[hlTurnId(d.turn)]?.endedAt != null || (active && (active.turnId === d.turn.turnId || active.startedAt > d.turn.startedAt))) return false;
1076
+ HL_TURN_N++;
1077
+ hlRunner(HL.busy, d.turn.topicId, d.turn.memberId, true);
1078
+ if (t) {
1079
+ hlSyncChanged(t);
1080
+ t.live[d.turn.messageId] = d.turn;
1081
+ delete t.queuedRetries?.[d.turn.messageId];
1082
+ if (d.turn.thread && d.turn.thread !== d.turn.messageId && !d.turn.threadDeleted) hlEnsureFloor(t, d.turn.thread);
1083
+ hlRunTouchMember(d.turn.memberId);
1084
+ hlStreamMarkDirty();
1085
+ hlParticipantsRead(t);
1086
+ }
1087
+ });
1088
+ on('config.changed', function (d) {
1089
+ Promise.all([hlConfigRefresh(), hlFetch('GET', '/api/owner'), d.scope === 'env' && HLP.hostPaths ? hlFetch('GET', '/api/host/paths') : null]).then(function (r) {
1090
+ hlSetOwner(r[1]); if (r[2]) HLP.hostPaths = r[2]; hlSyncLang(r[0]); render(true);
1091
+ if (HL.topic) hlParticipantsRead(HL.topic);
1092
+ if (d.scope === 'settings' && (!d.key || d.key === HL_TRASH_KEEP)) trashChanged();
1093
+ if (d.scope === 'settings' && (!d.key || d.key === 'approval.rules') && (S.route === 'approvals' || S.dialog?.kind === 'hlApprovalRule' || HLP.approvalOps)) hlApprovalOpsRead();
1094
+ });
1095
+ });
1096
+ on('plugin.changed', function () {
1097
+ hlConfigRefresh().then(hlDrawSoon, hlFail);
1098
+ if (S.route === 'members' || S.route === 'member' || S.dialog?.kind === 'hlClients') {
1099
+ hlAdaptersRead(true).then(hlDrawSoon, hlFail);
1100
+ }
1101
+ if (S.route === 'approvals' || S.dialog?.kind === 'hlApprovalRule' || HLP.approvalOps) hlApprovalOpsRead();
1102
+ if (HL.topic) hlParticipantsRead(HL.topic);
1103
+ return false;
1104
+ });
1105
+ on('background.changed', function (d) { if (HL.topic?.id === d.topicId) hlRunReadJobs(); return false; });
1106
+ on('instances.changed', function () {
1107
+ hlBusy().then(function () { render(true); });
1108
+ if (HL.topic) hlParticipantsRead(HL.topic);
1109
+ });
1110
+ on('turn.model', function (d) {
1111
+ const turn = HL.topic?.id === d.topicId && HL.topic.live[d.messageId]; if (!turn || turn.turnId !== d.turnId) return false;
1112
+ turn.model = d.model;
1113
+ hlRunTouchMember(turn.memberId);
1114
+ hlStreamTouchFast(d.messageId);
1115
+ });
1116
+ on('turn.stream', function (d) {
1117
+ const t = HL.topic?.id === d.topicId ? HL.topic : null, turn = t?.live[d.messageId];
1118
+ if (!turn || turn.turnId !== d.turnId) return false;
1119
+ const part = turn.parts[d.index];
1120
+ const offset = d.offset ?? part?.text?.length;
1121
+ if (d.index > turn.parts.length || (!d.part && (part?.text === undefined || part.text.length < offset))) {
1122
+ if (t.resync) hlSyncChanged(t);
1123
+ else void hlResyncTopic();
1124
+ return false;
1125
+ }
1126
+ if (d.part) {
1127
+ if (!part || !hlNewerStreamPart(part, d.part)) turn.parts[d.index] = d.part;
1128
+ } else {
1129
+ part.text += d.delta.slice(part.text.length - offset);
1130
+ }
1131
+ if (typeof hlViewShown === 'function' && hlViewShown('run')) hlRunTouchMember(turn.memberId);
1132
+ if (d.part?.type === 'card') { hlStreamMarkDirty(); hlStreamTouch(d.messageId); }
1133
+ else hlStreamTouchFast(d.messageId);
1134
+ });
1135
+ on('turn.ended', function (d) {
1136
+ HL_TURN_N++;
1137
+ const t = HL.topic?.id === d.turn.topicId ? HL.topic : null;
1138
+ const id = d.turn.replyId, tid = hlTurnId(d.turn), mid = d.discardedReplyId || id;
1139
+ const active = t?.live[mid];
1140
+ if (!t || !Object.values(t.live).some(function (turn) { return turn.memberId === d.turn.memberId && turn.turnId !== tid; })) {
1141
+ hlRunner(HL.busy, d.turn.topicId, d.turn.memberId, false);
1142
+ }
1143
+ if (t) {
1144
+ hlSyncChanged(t);
1145
+ if (d.message) hlUpdateMessage(t, d.message);
1146
+ if (hlTurnId(active) === tid) delete t.live[mid];
1147
+ if (d.discardedReplyId) {
1148
+ if (HU.trajFocus === d.discardedReplyId) HU.trajFocus = id;
1149
+ if (typeof hlTrajRetarget === 'function') hlTrajRetarget(d.discardedReplyId, id);
1150
+ }
1151
+ if (t.turns[tid]) t.turns[tid] = d.turn;
1152
+ else t.turns = { [tid]: d.turn, ...t.turns };
1153
+ hlRunTouchMember(d.turn.memberId);
1154
+ hlStreamMarkDirty();
1155
+ hlParticipantsRead(t);
1156
+ }
1157
+ });
1158
+ on('message.appended', function (d) {
1159
+ const m = d.message, t = HL.topic?.id === m.topicId ? HL.topic : null;
1160
+ if (HL_DELETED.has(m.id)) return false;
1161
+ const active = t?.live[m.id];
1162
+ if (active && active.turnId !== m.turnId) return false;
1163
+ if (t) {
1164
+ if (active) {
1165
+ hlChatFindSettled(m);
1166
+ delete t.live[m.id];
1167
+ hlSyncChanged(t);
1168
+ }
1169
+ if (!t.messages.some(function (x) { return x.id === m.id; })) t.messages.push(m);
1170
+ hlStreamMarkDirty();
1171
+ if (m.thread && m.thread !== m.id && !m.threadDeleted) hlEnsureFloor(t, m.thread);
1172
+ }
1173
+ const card = hlCardPut(m);
1174
+ return !!t || card;
1175
+ });
1176
+ /* A permission card may begin to wait in a live reply, before the message has a position in the channel. */
1177
+ on('message.changed', function (d) {
1178
+ const m = d.message, t = HL.topic?.id === m.topicId ? HL.topic : null;
1179
+ const current = t?.live[m.id]?.turnId || t?.messages.find(function (was) { return was.id === m.id; })?.turnId;
1180
+ if (current && current !== m.turnId) return false;
1181
+ const card = hlCardPut(m);
1182
+ const updated = t && m.status !== 'processing' ? hlUpdateMessage(t, m) : false;
1183
+ if (updated && m.thread && !m.threadDeleted) hlEnsureFloor(t, m.thread);
1184
+ if (updated || card) { hlSyncChanged(t); hlStreamMarkDirty(); }
1185
+ return card || updated;
1186
+ });
1187
+ on('message.deleted', hlMsgRemoved);
1188
+ /* Who has not read what yet; and what each turn that runs has read so far (a message taken in mid-turn is read by that turn). */
1189
+ on('channel.updated', function (d) {
1190
+ const t = HL.topic;
1191
+ if (t?.id === d.topicId) {
1192
+ hlParticipantsRead(t);
1193
+ const read = ++stateRead;
1194
+ hlFetch('GET', '/api/topics/' + d.topicId + '/state').then(function (s) {
1195
+ if (HL.topic !== t || read !== stateRead) return;
1196
+ t.unread = hlVisibleUnread(s.unread);
1197
+ t.queuedRetries = hlQueuedRetries(s.queuedRetries, t.live);
1198
+ s.live.forEach(function (turn) {
1199
+ const live = t.live[turn.messageId];
1200
+ if (live && live.turnId === turn.turnId) live.read = turn.read;
1201
+ });
1202
+ hlStreamMarkDirty();
1203
+ render(true);
1204
+ });
1205
+ }
1206
+ });
1207
+ }
1208
+
1209
+ function hlListen() {
1210
+ document.addEventListener('visibilitychange', function () {
1211
+ hlViewChanged();
1212
+ if (document.visibilityState === 'visible') hlDrawSoon();
1213
+ });
1214
+ setInterval(function () {
1215
+ if (document.visibilityState === 'visible' && S.space === 'workspace') hlDrawSoon();
1216
+ }, 60_000);
1217
+ window.addEventListener('pagehide', function () {
1218
+ HL_EVENTS?.close();
1219
+ HL_EVENTS = null;
1220
+ });
1221
+ window.addEventListener('pageshow', hlConnect);
1222
+ hlConnect();
1223
+ }