hostlet 0.0.0-stage → 0.1.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (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 +916 -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 +130 -0
  330. package/dist/plugins/model-profiles/rules.js +729 -0
  331. package/dist/plugins/model-profiles/skills/model-profile/SKILL.md +59 -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 +84 -0
  335. package/dist/plugins/model-profiles/storage.js +315 -0
  336. package/dist/plugins/model-profiles/types.d.ts +179 -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 +455 -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 +270 -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,521 @@
1
+ import { createHash } from 'node:crypto';
2
+ import { lstat, readdir, readFile } from 'node:fs/promises';
3
+ import { basename, join } from 'node:path';
4
+ import { setImmediate } from 'node:timers/promises';
5
+ import { address, isDirRef, isTopicRef, referenceMark, withExcerpts } from 'hostlet/sdk';
6
+ const BATCH = 200;
7
+ const FILE_BYTES = 512 * 1024;
8
+ function fingerprint(doc, revision) {
9
+ return { ...doc, fingerprint: createHash('sha256').update(JSON.stringify([doc, revision])).digest('hex') };
10
+ }
11
+ /** The page links follow the same upserts, deletions and rebuild as the document fingerprints. */
12
+ class Catalogue extends Map {
13
+ linked = new Map();
14
+ link(page, add) {
15
+ for (const key of page.keys) {
16
+ const entries = this.linked.get(key) ?? new Set();
17
+ if (add)
18
+ this.linked.set(key, entries.add(page));
19
+ else if (entries.delete(page) && !entries.size)
20
+ this.linked.delete(key);
21
+ }
22
+ }
23
+ forget(topicId, id) {
24
+ const entries = this.get(topicId);
25
+ const page = entries?.get(id)?.page;
26
+ if (page)
27
+ this.link(page, false);
28
+ entries?.delete(id);
29
+ }
30
+ forgetTopic(id) {
31
+ for (const stamp of this.get(id)?.values() ?? [])
32
+ if (stamp.page)
33
+ this.link(stamp.page, false);
34
+ this.delete(id);
35
+ }
36
+ remember(doc) {
37
+ this.forget(doc.topicId, doc.id);
38
+ const entries = this.get(doc.topicId) ?? new Map();
39
+ this.set(doc.topicId, entries);
40
+ const file = doc.kind === 'file' ? { mtimeMs: doc.meta.mtimeMs, size: doc.meta.size } : undefined;
41
+ let page;
42
+ if (doc.kind === 'page') {
43
+ const { sectionPlugin, rowAddresses, rowMessages } = doc.meta;
44
+ const items = Object.keys(rowAddresses ?? {}).map((id) => 'item:' + id);
45
+ page = { plugin: sectionPlugin, topicId: doc.topicId, keys: ['plugin:' + sectionPlugin, ...items, ...(rowMessages ?? [])],
46
+ addressed: rowAddresses ? doc : undefined };
47
+ this.link(page, true);
48
+ }
49
+ entries.set(doc.id, { kind: doc.kind, fingerprint: doc.fingerprint, file, page });
50
+ }
51
+ }
52
+ function documentFile(name) {
53
+ return !name.startsWith('.') && /^[^/\\]+\.(md|txt)$/i.test(name);
54
+ }
55
+ async function missing(read) {
56
+ try {
57
+ return await read;
58
+ }
59
+ catch (error) {
60
+ if (error.code !== 'ENOENT')
61
+ throw error;
62
+ return undefined;
63
+ }
64
+ }
65
+ export function whereOf(doc, query, range) {
66
+ const addresses = doc.meta?.rowAddresses;
67
+ if (query && addresses && Object.hasOwn(addresses, query)) {
68
+ return { url: '#' + addresses[query], label: query };
69
+ }
70
+ const topicId = doc.meta?.source?.topicId ?? doc.topicId;
71
+ const messageId = doc.kind === 'message' ? doc.id : doc.meta?.source?.messageId;
72
+ const url = topicId ? `#${messageId ? address.message(topicId, messageId) : address.topic(topicId)}` : '#memory';
73
+ const file = doc.meta?.relPath;
74
+ const label = file ? `${file}${range?.heading ? ` §${range.heading}` : ''} L${range?.line ?? 1}${range && range.endLine !== range.line ? `–L${range.endLine}` : ''}`
75
+ : doc.meta?.source?.doc ?? url;
76
+ return { url, label, ...(topicId ? { topicId } : {}), ...(messageId ? { messageId } : {}), ...(file ? { file, line: 1, ...range } : {}) };
77
+ }
78
+ export function createCorpus(ctx, notes, submit) {
79
+ const topics = ctx.get('topics');
80
+ const messages = ctx.get('messages');
81
+ const members = ctx.get('members');
82
+ let known = new Catalogue();
83
+ const pages = new Map();
84
+ const live = (id) => {
85
+ const topic = topics.get(id);
86
+ return topic && topic.deletedAt === null ? topic : undefined;
87
+ };
88
+ const placeOf = (topic) => ({ topicId: topic.id, topicPath: topics.path(topic.id).map((node) => node.title), tags: topic.tags });
89
+ const topicDoc = (topic, place = placeOf(topic)) => fingerprint({
90
+ ...place, id: `topic:${topic.id}`, kind: 'topic', title: topic.title, who: '主题', text: topic.title, state: 'active',
91
+ createdAt: topic.createdAt, updatedAt: topic.createdAt,
92
+ meta: { parentId: topic.parentId, dirRefs: topics.refsOf(topic.id).map((ref) => ref.path), topicRefs: topic.refs.filter(isTopicRef).map((ref) => ref.topicId), archivedAt: topic.archivedAt },
93
+ });
94
+ const messageDoc = (item, place) => {
95
+ const text = withExcerpts(item, [referenceMark(item).trimStart(), item.content].filter(Boolean).join('\n\n'));
96
+ if (item.from.kind === 'system' || item.seq === null || !text.trim())
97
+ return undefined;
98
+ return fingerprint({
99
+ ...place, id: item.id, kind: 'message', title: '', who: members.get(item.from.id)?.name ?? item.from.id, text,
100
+ state: 'active', createdAt: item.createdAt, updatedAt: item.createdAt,
101
+ meta: { thread: item.thread, replyTo: item.replyTo, read: item.read },
102
+ }, item.parts);
103
+ };
104
+ const noteDoc = (note) => fingerprint({
105
+ id: note.id, kind: 'note', topicId: note.topicId, topicPath: note.topicId ? topics.path(note.topicId).map((node) => node.title) : [],
106
+ tags: note.tags, title: note.title ?? '', who: note.who, text: note.text, state: note.state, version: note.version,
107
+ createdAt: note.at, updatedAt: note.updatedAt, meta: { source: note.source },
108
+ });
109
+ async function* pageDocs(topic, place, plugin) {
110
+ if (!plugin || plugin === 'topics') {
111
+ const summary = topics.summary(topic.id).trim();
112
+ if (summary) {
113
+ yield fingerprint({
114
+ ...place, id: `page:${topic.id}:summary`, kind: 'page', title: '摘要', who: '摘要', text: summary, state: 'active',
115
+ createdAt: topic.createdAt, updatedAt: topic.createdAt, meta: { sectionPlugin: 'topics' },
116
+ });
117
+ }
118
+ }
119
+ for (const { plugin: owner, value: source } of await ctx.runtime.serialBy('memory.sources', {}, plugin)) {
120
+ for (const item of await source.docs(topic.id)) {
121
+ const text = item.text.trim();
122
+ if (!text)
123
+ continue;
124
+ const addresses = item.rowAddresses && Object.keys(item.rowAddresses).length ? item.rowAddresses : undefined;
125
+ const messages = item.rowMessages?.length ? [...new Set(item.rowMessages)] : undefined;
126
+ yield fingerprint({
127
+ ...place, id: `page:${topic.id}:${item.id}`, kind: 'page', title: item.title, who: item.title, text, state: 'active',
128
+ createdAt: topic.createdAt, updatedAt: topic.createdAt, meta: {
129
+ sectionPlugin: owner,
130
+ ...(item.rowIds ? { rowIds: item.rowIds } : {}),
131
+ ...(addresses ? { rowAddresses: addresses } : {}),
132
+ ...(messages ? { rowMessages: messages } : {}),
133
+ },
134
+ });
135
+ }
136
+ }
137
+ }
138
+ async function* fileDocs(topic, place, keepCached, only) {
139
+ const root = join(topic.dir, 'artifacts');
140
+ if (!(await missing(lstat(root)))?.isDirectory())
141
+ return;
142
+ const names = only ? [only] : (await missing(readdir(root))) ?? [];
143
+ for (const name of names) {
144
+ if (!documentFile(name))
145
+ continue;
146
+ const relPath = `artifacts/${name}`;
147
+ const path = join(root, name);
148
+ const stat = await missing(lstat(path));
149
+ if (!stat?.isFile() || stat.size > FILE_BYTES)
150
+ continue;
151
+ const id = `file:${topic.id}:${relPath}`;
152
+ const previous = keepCached && known.get(topic.id)?.get(id)?.file;
153
+ if (previous && previous.mtimeMs === stat.mtimeMs && previous.size === stat.size) {
154
+ keepCached(id);
155
+ continue;
156
+ }
157
+ const text = await missing(readFile(path, 'utf8'));
158
+ if (text === undefined || Buffer.byteLength(text) > FILE_BYTES)
159
+ continue;
160
+ yield fingerprint({
161
+ ...place, id, kind: 'file', title: name, who: relPath, text, state: 'active',
162
+ createdAt: stat.mtimeMs, updatedAt: stat.mtimeMs, meta: { relPath, mtimeMs: stat.mtimeMs, size: stat.size },
163
+ });
164
+ }
165
+ }
166
+ async function* topicDocs(topic) {
167
+ const place = placeOf(topic);
168
+ yield topicDoc(topic, place);
169
+ for (let after = 0;;) {
170
+ const batch = messages.page(topic.id, { after, limit: BATCH });
171
+ for (const message of batch) {
172
+ const doc = messageDoc(message, place);
173
+ if (doc)
174
+ yield doc;
175
+ }
176
+ if (batch.length < BATCH)
177
+ break;
178
+ after = batch.at(-1).seq;
179
+ await setImmediate();
180
+ }
181
+ for (const note of notes.forTopic(topic.id))
182
+ yield noteDoc(note);
183
+ yield* pageDocs(topic, place);
184
+ yield* fileDocs(topic, place);
185
+ }
186
+ async function* allDocs(next) {
187
+ for (const topic of topics.list()) {
188
+ if (topic.deletedAt !== null)
189
+ continue;
190
+ for await (const doc of topicDocs(topic)) {
191
+ next.remember(doc);
192
+ yield doc;
193
+ }
194
+ }
195
+ for (const note of notes.forTopic(null)) {
196
+ const doc = noteDoc(note);
197
+ next.remember(doc);
198
+ yield doc;
199
+ }
200
+ }
201
+ async function reconcile(engine, force) {
202
+ await bindSources();
203
+ const next = new Catalogue();
204
+ const rebuilt = force || engine.needsRebuild();
205
+ if (rebuilt) {
206
+ await engine.rebuild(allDocs(next));
207
+ }
208
+ else {
209
+ const indexed = await engine.fingerprints();
210
+ let changed = [];
211
+ for await (const doc of allDocs(next)) {
212
+ if (indexed.get(doc.id) !== doc.fingerprint)
213
+ changed.push(doc);
214
+ indexed.delete(doc.id);
215
+ if (changed.length === BATCH) {
216
+ await engine.upsert(changed);
217
+ changed = [];
218
+ }
219
+ }
220
+ if (changed.length)
221
+ await engine.upsert(changed);
222
+ if (indexed.size)
223
+ await engine.remove([...indexed.keys()]);
224
+ }
225
+ known = next;
226
+ return rebuilt;
227
+ }
228
+ async function put(engine, docs) {
229
+ if (!docs.length)
230
+ return;
231
+ await engine.upsert(docs);
232
+ for (const doc of docs)
233
+ known.remember(doc);
234
+ }
235
+ async function drop(engine, topicId, ids) {
236
+ if (!ids.length)
237
+ return;
238
+ await engine.remove(ids);
239
+ for (const id of ids)
240
+ known.forget(topicId, id);
241
+ }
242
+ async function refresh(engine, topic, kind, plugin) {
243
+ const entries = known.get(topic.id);
244
+ const stale = new Set();
245
+ for (const [id, stamp] of entries ?? []) {
246
+ if (plugin && stamp.page?.plugin !== plugin)
247
+ continue;
248
+ if (kind === 'all' || (kind !== 'pages' && stamp.kind === 'file') || (kind !== 'files' && stamp.kind === 'page'))
249
+ stale.add(id);
250
+ }
251
+ const place = placeOf(topic);
252
+ const docs = kind === 'all' ? topicDocs(topic) : sections();
253
+ async function* sections() {
254
+ if (kind !== 'files')
255
+ yield* pageDocs(topic, place, plugin);
256
+ if (kind !== 'pages')
257
+ yield* fileDocs(topic, place, (id) => stale.delete(id));
258
+ }
259
+ let batch = [];
260
+ for await (const doc of docs) {
261
+ stale.delete(doc.id);
262
+ if (entries?.get(doc.id)?.fingerprint !== doc.fingerprint)
263
+ batch.push(doc);
264
+ if (batch.length === BATCH) {
265
+ await put(engine, batch);
266
+ batch = [];
267
+ }
268
+ }
269
+ await put(engine, batch);
270
+ await drop(engine, topic.id, [...stale]);
271
+ }
272
+ const lineage = (topic) => JSON.stringify([topic.title, topic.parentId, topic.tags, topic.refs.filter(isDirRef), topic.caps]);
273
+ const tree = new Map(topics.list().map((topic) => [topic.id, lineage(topic)]));
274
+ function later(id) {
275
+ clearTimeout(pages.get(id));
276
+ pages.set(id, setTimeout(() => {
277
+ pages.delete(id);
278
+ submit(async (engine) => {
279
+ const topic = live(id);
280
+ if (topic)
281
+ await refresh(engine, topic, 'sections');
282
+ });
283
+ }, 300));
284
+ }
285
+ async function flushPages(engine) {
286
+ const ids = [...pages.keys()];
287
+ for (const id of ids)
288
+ clearTimeout(pages.get(id));
289
+ pages.clear();
290
+ for (const id of ids) {
291
+ const topic = live(id);
292
+ if (topic)
293
+ await refresh(engine, topic, 'sections');
294
+ }
295
+ }
296
+ const syncMessage = async (engine, id, topicId) => {
297
+ const topic = live(topicId);
298
+ const latest = topic && messages.get(id);
299
+ const doc = latest && messageDoc(latest, placeOf(topic));
300
+ const was = known.get(topicId)?.get(id);
301
+ if (doc && was?.fingerprint !== doc.fingerprint)
302
+ await put(engine, [doc]);
303
+ if (!doc && was)
304
+ await drop(engine, topicId, [id]);
305
+ };
306
+ const messageChanged = ({ message }) => submit((engine) => syncMessage(engine, message.id, message.topicId));
307
+ ctx.on('message.appended', messageChanged);
308
+ const refreshPages = async (engine, ids, plugin) => {
309
+ for (const id of ids) {
310
+ const topic = live(id);
311
+ if (topic)
312
+ await refresh(engine, topic, 'pages', plugin);
313
+ }
314
+ };
315
+ const listeners = new Map();
316
+ const scoped = new Set();
317
+ async function bindSources(name) {
318
+ const affected = new Set(Array.from(known.linked.get('plugin:' + name) ?? [], (page) => page.topicId));
319
+ if (name) {
320
+ for (const release of listeners.get(name) ?? [])
321
+ await release();
322
+ listeners.delete(name);
323
+ scoped.delete(name);
324
+ }
325
+ const bound = new Set(listeners.keys());
326
+ for (const { plugin, value: source } of await ctx.runtime.serialBy('memory.sources', {}, name)) {
327
+ if (source.topics) {
328
+ scoped.add(plugin);
329
+ if (name)
330
+ for (const id of source.topics())
331
+ affected.add(id);
332
+ }
333
+ if (!source.event || bound.has(plugin))
334
+ continue;
335
+ const subscriptions = listeners.get(plugin) ?? [];
336
+ subscriptions.push(ctx.runtime._listen(source.event, (payload) => {
337
+ const change = payload;
338
+ const ids = 'topicIds' in change ? change.topicIds : source.topics ? source.topics(change.topicId) : [change.topicId];
339
+ submit((engine) => refreshPages(engine, ids, plugin));
340
+ }));
341
+ listeners.set(plugin, subscriptions);
342
+ }
343
+ return affected;
344
+ }
345
+ ctx.on('plugin.changed', ({ name, kind }) => {
346
+ if (kind === 'configured')
347
+ return;
348
+ const contributes = ctx.runtime.hasHook('memory.sources', name);
349
+ if (!contributes && !listeners.has(name) && !scoped.has(name) && !known.linked.has('plugin:' + name))
350
+ return;
351
+ submit(async (engine) => {
352
+ const affected = await bindSources(name);
353
+ // A provider with no scope can have gained content anywhere. Read only that provider.
354
+ const ids = !scoped.has(name) && ctx.runtime.hasHook('memory.sources', name) ? topics.list().map((topic) => topic.id) : affected;
355
+ await refreshPages(engine, ids, name);
356
+ });
357
+ });
358
+ const sourceChanged = (id, topicId) => submit(async (engine) => {
359
+ await syncMessage(engine, id, topicId);
360
+ const affected = new Map();
361
+ for (const { plugin, topicId } of known.linked.get(id) ?? []) {
362
+ const ids = affected.get(plugin) ?? new Set();
363
+ affected.set(plugin, ids.add(topicId));
364
+ }
365
+ for (const [plugin, ids] of affected)
366
+ await refreshPages(engine, ids, plugin);
367
+ });
368
+ ctx.on('message.changed', ({ message }) => sourceChanged(message.id, message.topicId));
369
+ ctx.on('message.deleted', ({ id, topicId }) => sourceChanged(id, topicId));
370
+ ctx.on('topic.updated', ({ topic: changed }) => {
371
+ const id = changed.id;
372
+ const next = lineage(changed);
373
+ const moved = tree.has(id) && tree.get(id) !== next;
374
+ tree.set(id, next);
375
+ submit(async (engine) => {
376
+ const topic = live(id);
377
+ if (!topic) {
378
+ await engine.removeTopic(id);
379
+ known.forgetTopic(id);
380
+ return;
381
+ }
382
+ if (moved || !known.get(id)?.has(`topic:${id}`)) {
383
+ await refresh(engine, topic, 'all');
384
+ if (moved) {
385
+ for (const child of topics.list()) {
386
+ if (child.id !== id && topics.path(child.id).some((ancestor) => ancestor.id === id))
387
+ await refresh(engine, child, 'all');
388
+ }
389
+ }
390
+ return;
391
+ }
392
+ const doc = topicDoc(topic);
393
+ if (known.get(id)?.get(doc.id)?.fingerprint !== doc.fingerprint)
394
+ await put(engine, [doc]);
395
+ });
396
+ later(id);
397
+ });
398
+ ctx.on('topic.purged', ({ topicIds }) => {
399
+ for (const id of topicIds) {
400
+ clearTimeout(pages.get(id));
401
+ pages.delete(id);
402
+ tree.delete(id);
403
+ notes.dropTopic(id);
404
+ }
405
+ submit(async (engine) => {
406
+ for (const id of topicIds) {
407
+ await engine.removeTopic(id);
408
+ known.forgetTopic(id);
409
+ }
410
+ });
411
+ });
412
+ ctx.on('turn.ended', ({ turn, message }) => submit(async (engine) => {
413
+ if (message)
414
+ await syncMessage(engine, message.id, message.topicId);
415
+ const topic = live(turn.topicId);
416
+ if (topic)
417
+ await refresh(engine, topic, 'files');
418
+ if (engine.onTurnEnded) {
419
+ try {
420
+ await engine.onTurnEnded({ turn, message });
421
+ }
422
+ catch (error) {
423
+ ctx.runtime.onError(`memory engine ${engine.id} onTurnEnded`, error);
424
+ }
425
+ }
426
+ }));
427
+ ctx.effect(() => {
428
+ for (const timer of pages.values())
429
+ clearTimeout(timer);
430
+ pages.clear();
431
+ });
432
+ function neighbour(message, direction, place) {
433
+ for (let edge = message.seq;;) {
434
+ const page = messages.page(message.topicId, { [direction]: edge, limit: BATCH });
435
+ if (direction === 'before')
436
+ page.reverse();
437
+ for (const item of page) {
438
+ if (item.thread !== message.thread && item.id !== message.thread)
439
+ continue;
440
+ const doc = messageDoc(item, place);
441
+ if (doc)
442
+ return doc;
443
+ }
444
+ if (page.length < BATCH)
445
+ return undefined;
446
+ edge = page.at(-1).seq;
447
+ }
448
+ }
449
+ return {
450
+ reconcile, flushPages, noteDoc,
451
+ async close() {
452
+ for (const subscriptions of listeners.values())
453
+ for (const release of subscriptions)
454
+ await release();
455
+ listeners.clear();
456
+ },
457
+ exactRows(query, opts) {
458
+ const pages = known.linked.get('item:' + query);
459
+ if (!pages)
460
+ return undefined;
461
+ const matches = Array.from(pages, (page) => page.addressed).filter((page) => (!opts.topicIds || opts.topicIds.includes(page.topicId))
462
+ && (!opts.kinds || opts.kinds.includes('page'))
463
+ && (opts.since === undefined || page.createdAt >= opts.since)
464
+ && (opts.until === undefined || page.createdAt <= opts.until))
465
+ .sort((a, b) => b.createdAt - a.createdAt || a.id.localeCompare(b.id));
466
+ return { by: 'exact', total: matches.length, hits: matches.slice(0, opts.limit) };
467
+ },
468
+ get pagesPending() { return pages.size > 0; },
469
+ counts() {
470
+ const counts = { topic: 0, message: 0, note: 0, page: 0, file: 0 };
471
+ for (const entries of known.values())
472
+ for (const { kind } of entries.values())
473
+ counts[kind]++;
474
+ return counts;
475
+ },
476
+ noteChanged(note) {
477
+ submit(async (engine) => {
478
+ const current = notes.get(note.id);
479
+ if (!current || (current.topicId && !live(current.topicId)))
480
+ return;
481
+ const doc = noteDoc(current);
482
+ if (known.get(note.topicId)?.get(note.id)?.fingerprint !== doc.fingerprint)
483
+ await put(engine, [doc]);
484
+ });
485
+ },
486
+ noteRemoved(note) {
487
+ submit((engine) => drop(engine, note.topicId, [note.id]));
488
+ },
489
+ async read(id, context) {
490
+ if (id.startsWith('msg_')) {
491
+ const message = messages.get(id);
492
+ const topic = message && live(message.topicId);
493
+ if (!topic || !message)
494
+ return undefined;
495
+ const place = placeOf(topic);
496
+ const doc = messageDoc(message, place);
497
+ if (!doc)
498
+ return undefined;
499
+ return { ...doc, where: whereOf(doc), ...(context ? { context: { before: neighbour(message, 'before', place), after: neighbour(message, 'after', place) } } : {}) };
500
+ }
501
+ const match = /^(topic|page|file):([^:]+)(?::(.+))?$/.exec(id);
502
+ const topic = match && live(match[2]);
503
+ if (!topic || !match)
504
+ return undefined;
505
+ if (match[1] === 'topic') {
506
+ const doc = topicDoc(topic);
507
+ return { ...doc, where: whereOf(doc) };
508
+ }
509
+ const place = placeOf(topic);
510
+ const file = match[3];
511
+ if (match[1] === 'file' && (!file || !file.startsWith('artifacts/') || basename(file) !== file.slice('artifacts/'.length)))
512
+ return undefined;
513
+ const docs = match[1] === 'file' ? fileDocs(topic, place, undefined, basename(file)) : pageDocs(topic, place);
514
+ for await (const doc of docs) {
515
+ if (doc.id === id && live(topic.id))
516
+ return { ...doc, where: whereOf(doc) };
517
+ }
518
+ return undefined;
519
+ },
520
+ };
521
+ }
@@ -0,0 +1,8 @@
1
+ type Vector = Map<string, number>;
2
+ export declare function tfIdf(tokens: string[], frequency: Map<string, number>, passages: number): Vector;
3
+ /** Greedy DPP with incremental Cholesky updates of the residual diagonal. */
4
+ export declare function diversify<T extends {
5
+ score: number;
6
+ vector: Vector;
7
+ }>(ranked: T[], limit: number): T[];
8
+ export {};
@@ -0,0 +1,49 @@
1
+ export function tfIdf(tokens, frequency, passages) {
2
+ const vector = new Map();
3
+ for (const token of tokens)
4
+ vector.set(token, (vector.get(token) ?? 0) + 1);
5
+ let norm = 0;
6
+ for (const [term, count] of vector) {
7
+ const weight = (1 + Math.log(count)) * (1 + Math.log((passages + 1) / ((frequency.get(term) ?? 0) + 1)));
8
+ vector.set(term, weight);
9
+ norm += weight * weight;
10
+ }
11
+ norm = Math.sqrt(norm);
12
+ for (const [term, weight] of vector)
13
+ vector.set(term, weight / norm);
14
+ return vector;
15
+ }
16
+ function cosine(a, b) {
17
+ const [small, large] = a.size <= b.size ? [a, b] : [b, a];
18
+ let dot = 0;
19
+ for (const [term, value] of small)
20
+ dot += value * (large.get(term) ?? 0);
21
+ return dot;
22
+ }
23
+ /** Greedy DPP with incremental Cholesky updates of the residual diagonal. */
24
+ export function diversify(ranked, limit) {
25
+ const residual = ranked.map(({ score }) => score * score);
26
+ const factors = ranked.map(() => []);
27
+ const result = [];
28
+ for (let step = 0; step < limit && step < ranked.length; step++) {
29
+ const best = residual.indexOf(Math.max(...residual));
30
+ const selected = ranked[best];
31
+ const diagonal = Math.sqrt(residual[best]);
32
+ result.push(selected);
33
+ residual[best] = -1;
34
+ if (diagonal <= 1e-10)
35
+ continue; // Fill remaining slots in rank order when vectors are identical.
36
+ for (const [i, item] of ranked.entries()) {
37
+ if (residual[i] < 0)
38
+ continue;
39
+ const prior = factors[i];
40
+ let value = selected.score * item.score * cosine(selected.vector, item.vector);
41
+ for (let k = 0; k < prior.length; k++)
42
+ value -= factors[best][k] * prior[k];
43
+ const component = value / diagonal;
44
+ prior.push(component);
45
+ residual[i] = Math.max(0, residual[i] - component * component);
46
+ }
47
+ }
48
+ return result;
49
+ }
@@ -0,0 +1,12 @@
1
+ import type { MemoryDoc } from 'hostlet/sdk';
2
+ export declare function tokenize(text: string, expandIdentifiers?: boolean): string[];
3
+ export declare function entitiesOf(doc: MemoryDoc): string[];
4
+ export type Passage = {
5
+ heading: string;
6
+ line: number;
7
+ endLine: number;
8
+ body: string;
9
+ };
10
+ export declare function passagesOf(doc: MemoryDoc): Passage[];
11
+ export declare function sentences(text: string): string[];
12
+ export declare function excerptNear(text: string, tokens: string[]): string;
@@ -0,0 +1,93 @@
1
+ const segmenter = new Intl.Segmenter('zh-CN', { granularity: 'word' });
2
+ const stopwords = new Set(('a an and are as at be by for from how i in is it me of on or that the this to was were what with you ' +
3
+ '的 了 与 和 及 在 把 为 是 有 用 请 我 你 它 这 那 这个 那个 什么 怎么 如何 是否 可以').split(' '));
4
+ const identifiers = /artifacts\/[^\s/\x60<>"#?[\]()]+\.(?:md|txt)(?![\p{L}\p{N}_.-])|[a-z0-9]+(?:[._-][a-z0-9]+)+/giu;
5
+ function words(text) {
6
+ const result = [];
7
+ for (const word of segmenter.segment(text)) {
8
+ if (word.isWordLike && !stopwords.has(word.segment))
9
+ result.push(word.segment);
10
+ }
11
+ return result;
12
+ }
13
+ export function tokenize(text, expandIdentifiers = false) {
14
+ text = text.normalize('NFC').toLowerCase();
15
+ const tokens = [];
16
+ const aliases = [];
17
+ let start = 0;
18
+ for (const match of text.matchAll(identifiers)) {
19
+ for (const word of words(text.slice(start, match.index)))
20
+ tokens.push(word);
21
+ tokens.push(match[0]);
22
+ if (expandIdentifiers) {
23
+ for (const alias of words(match[0].replace(/[._/-]/g, ' ')))
24
+ aliases.push(alias);
25
+ }
26
+ start = match.index + match[0].length;
27
+ }
28
+ return tokens.concat(words(text.slice(start)), aliases);
29
+ }
30
+ export function entitiesOf(doc) {
31
+ const names = [...doc.tags];
32
+ if (doc.kind === 'topic' || doc.kind === 'file')
33
+ names.push(doc.title);
34
+ if (doc.kind === 'message' || doc.kind === 'note')
35
+ names.push(doc.who);
36
+ for (const match of doc.text.matchAll(/(?<!\x60)\x60([\p{L}\p{N}_][\p{L}\p{N}._/-]*)\x60(?!\x60)/gu)) {
37
+ names.push(match[1]);
38
+ }
39
+ const entities = names.filter((name) => name.length <= 100).map((name) => tokenize(name).join(' '));
40
+ return [...new Set(entities.filter((name) => /\p{L}/u.test(name) && (name.length >= 3 || /^\p{Script=Han}{2}$/u.test(name))))];
41
+ }
42
+ export function passagesOf(doc) {
43
+ const lines = doc.text.split(/\r?\n/);
44
+ const passages = [];
45
+ let start = 0;
46
+ let heading = '';
47
+ function append(end) {
48
+ while (end > start && !lines[end - 1].trim())
49
+ end--;
50
+ if (end > start)
51
+ passages.push({ heading, line: start + 1, endLine: end, body: lines.slice(start, end).join('\n') });
52
+ }
53
+ if (doc.kind === 'file' && /\.md$/i.test(doc.meta?.relPath ?? '')) {
54
+ let fence = '';
55
+ for (const [i, line] of lines.entries()) {
56
+ const marker = line.match(/^ {0,3}(\x60{3,}|~{3,})(.*)$/);
57
+ if (fence) {
58
+ if (marker && marker[1][0] === fence[0] && marker[1].length >= fence.length && !marker[2].trim())
59
+ fence = '';
60
+ continue;
61
+ }
62
+ if (marker) {
63
+ fence = marker[1];
64
+ continue;
65
+ }
66
+ const title = line.match(/^ {0,3}#{1,3}[ \t]+(.+?)[ \t]*$/)?.[1];
67
+ if (!title)
68
+ continue;
69
+ append(i);
70
+ start = i;
71
+ heading = title.replace(/[ \t]+#+$/, '');
72
+ }
73
+ }
74
+ append(lines.length);
75
+ if (!passages.length)
76
+ passages.push({ heading: '', line: 1, endLine: 1, body: '' });
77
+ return passages;
78
+ }
79
+ export function sentences(text) {
80
+ return text.split(/[。!?!?;;\n]+|\.(?=\s|$)/);
81
+ }
82
+ export function excerptNear(text, tokens) {
83
+ if (text.length <= 300)
84
+ return text;
85
+ const lower = text.toLowerCase();
86
+ const at = tokens.map((token) => lower.indexOf(token)).find((i) => i >= 0) ?? 0;
87
+ if (at < 230)
88
+ return text;
89
+ let start = at - 65;
90
+ if (/[\uDC00-\uDFFF]/.test(text.charAt(start)))
91
+ start--;
92
+ return '…' + text.slice(start);
93
+ }