@dikolab/kbdb 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (308) hide show
  1. package/LICENSE +15 -0
  2. package/README.md +270 -0
  3. package/dist/chunk-QCCN7OZH.mjs +82 -0
  4. package/dist/chunk-QCCN7OZH.mjs.map +7 -0
  5. package/dist/cli.cjs +3598 -0
  6. package/dist/cli.cjs.map +7 -0
  7. package/dist/cli.mjs +3537 -0
  8. package/dist/cli.mjs.map +7 -0
  9. package/dist/kbdb-worker.cjs +1659 -0
  10. package/dist/kbdb-worker.cjs.map +7 -0
  11. package/dist/mod.cjs +538 -0
  12. package/dist/mod.cjs.map +7 -0
  13. package/dist/mod.d.ts +3 -0
  14. package/dist/mod.mjs +507 -0
  15. package/dist/mod.mjs.map +7 -0
  16. package/dist/src/cli.d.ts +1 -0
  17. package/dist/src/shared/cli/constants/defaults.constant.d.ts +11 -0
  18. package/dist/src/shared/cli/constants/help-text.constant.d.ts +2 -0
  19. package/dist/src/shared/cli/functions/dispatch-command.function.d.ts +37 -0
  20. package/dist/src/shared/cli/functions/ensure-db.function.d.ts +11 -0
  21. package/dist/src/shared/cli/functions/error-to-message.function.d.ts +7 -0
  22. package/dist/src/shared/cli/functions/find-db.function.d.ts +13 -0
  23. package/dist/src/shared/cli/functions/format-output.function.d.ts +15 -0
  24. package/dist/src/shared/cli/functions/generate-help.function.d.ts +31 -0
  25. package/dist/src/shared/cli/functions/is-stdin-tty.function.d.ts +7 -0
  26. package/dist/src/shared/cli/functions/parse-args.function.d.ts +25 -0
  27. package/dist/src/shared/cli/functions/prompt-user.function.d.ts +13 -0
  28. package/dist/src/shared/cli/functions/run-agent-create.function.d.ts +58 -0
  29. package/dist/src/shared/cli/functions/run-agent-delete.function.d.ts +32 -0
  30. package/dist/src/shared/cli/functions/run-agent-get.function.d.ts +46 -0
  31. package/dist/src/shared/cli/functions/run-agent-list.function.d.ts +45 -0
  32. package/dist/src/shared/cli/functions/run-check.function.d.ts +64 -0
  33. package/dist/src/shared/cli/functions/run-cli.function.d.ts +39 -0
  34. package/dist/src/shared/cli/functions/run-content.function.d.ts +40 -0
  35. package/dist/src/shared/cli/functions/run-db-init.function.d.ts +23 -0
  36. package/dist/src/shared/cli/functions/run-db-migrate.function.d.ts +32 -0
  37. package/dist/src/shared/cli/functions/run-gc.function.d.ts +38 -0
  38. package/dist/src/shared/cli/functions/run-init.function.d.ts +27 -0
  39. package/dist/src/shared/cli/functions/run-learn.function.d.ts +46 -0
  40. package/dist/src/shared/cli/functions/run-migrate.function.d.ts +57 -0
  41. package/dist/src/shared/cli/functions/run-rebuild.function.d.ts +40 -0
  42. package/dist/src/shared/cli/functions/run-search.function.d.ts +39 -0
  43. package/dist/src/shared/cli/functions/run-skill-delete.function.d.ts +30 -0
  44. package/dist/src/shared/cli/functions/run-skill-get.function.d.ts +49 -0
  45. package/dist/src/shared/cli/functions/run-skill-learn.function.d.ts +36 -0
  46. package/dist/src/shared/cli/functions/run-skill-list.function.d.ts +43 -0
  47. package/dist/src/shared/cli/functions/run-status.function.d.ts +22 -0
  48. package/dist/src/shared/cli/functions/run-unlearn.function.d.ts +45 -0
  49. package/dist/src/shared/cli/index.d.ts +35 -0
  50. package/dist/src/shared/cli/typings/cli-options.interface.d.ts +53 -0
  51. package/dist/src/shared/cli/typings/command-result.model.d.ts +15 -0
  52. package/dist/src/shared/cli/typings/learn-result.model.d.ts +11 -0
  53. package/dist/src/shared/cli/typings/search-display.model.d.ts +17 -0
  54. package/dist/src/shared/dir-resolver/functions/resolve-base-dir.function.d.ts +3 -0
  55. package/dist/src/shared/dir-resolver/functions/resolve-wasm-dir.function.d.ts +1 -0
  56. package/dist/src/shared/dir-resolver/functions/resolve-worker-script.function.d.ts +2 -0
  57. package/dist/src/shared/dir-resolver/index.d.ts +4 -0
  58. package/dist/src/shared/dir-resolver/typings/base-dir.interface.d.ts +4 -0
  59. package/dist/src/shared/hash/functions/compute-sha256.d.ts +10 -0
  60. package/dist/src/shared/hash/index.d.ts +1 -0
  61. package/dist/src/shared/mcp/classes/mcp-server.class.d.ts +55 -0
  62. package/dist/src/shared/mcp/constants/mcp-error-codes.constant.d.ts +26 -0
  63. package/dist/src/shared/mcp/functions/create-mcp-server.function.d.ts +11 -0
  64. package/dist/src/shared/mcp/functions/create-prompt-definitions.function.d.ts +41 -0
  65. package/dist/src/shared/mcp/functions/create-resource-templates.function.d.ts +7 -0
  66. package/dist/src/shared/mcp/functions/create-tool-definitions.function.d.ts +7 -0
  67. package/dist/src/shared/mcp/functions/extract-skill-arguments.function.d.ts +13 -0
  68. package/dist/src/shared/mcp/functions/handle-agent-create.function.d.ts +29 -0
  69. package/dist/src/shared/mcp/functions/handle-agent-delete.function.d.ts +28 -0
  70. package/dist/src/shared/mcp/functions/handle-agent-get.function.d.ts +29 -0
  71. package/dist/src/shared/mcp/functions/handle-agent-list.function.d.ts +31 -0
  72. package/dist/src/shared/mcp/functions/handle-completion.function.d.ts +58 -0
  73. package/dist/src/shared/mcp/functions/handle-prompt-get.function.d.ts +23 -0
  74. package/dist/src/shared/mcp/functions/handle-resource-read.function.d.ts +26 -0
  75. package/dist/src/shared/mcp/functions/handle-roots-list.function.d.ts +21 -0
  76. package/dist/src/shared/mcp/functions/handle-tool-call.function.d.ts +26 -0
  77. package/dist/src/shared/mcp/functions/parse-skill-content.function.d.ts +15 -0
  78. package/dist/src/shared/mcp/functions/serialize-skill-content.function.d.ts +9 -0
  79. package/dist/src/shared/mcp/functions/should-emit-log.function.d.ts +20 -0
  80. package/dist/src/shared/mcp/index.d.ts +9 -0
  81. package/dist/src/shared/mcp/typings/agent-types.interface.d.ts +38 -0
  82. package/dist/src/shared/mcp/typings/log-types.interface.d.ts +17 -0
  83. package/dist/src/shared/mcp/typings/mcp-client.interface.d.ts +217 -0
  84. package/dist/src/shared/mcp/typings/mcp-types.interface.d.ts +149 -0
  85. package/dist/src/shared/mcp/typings/prompt-types.interface.d.ts +37 -0
  86. package/dist/src/shared/mcp/typings/remove-section-result.model.d.ts +10 -0
  87. package/dist/src/shared/mcp/typings/sampling-types.interface.d.ts +46 -0
  88. package/dist/src/shared/mcp/typings/skill-types.interface.d.ts +28 -0
  89. package/dist/src/shared/mod.d.ts +1 -0
  90. package/dist/src/shared/platform/functions/get-ipc-path.function.d.ts +11 -0
  91. package/dist/src/shared/platform/functions/get-tmpdir.function.d.ts +4 -0
  92. package/dist/src/shared/platform/functions/is-process-alive.function.d.ts +10 -0
  93. package/dist/src/shared/platform/index.d.ts +3 -0
  94. package/dist/src/shared/runtime/functions/detect-runtime.d.ts +8 -0
  95. package/dist/src/shared/runtime/functions/get-args.d.ts +7 -0
  96. package/dist/src/shared/runtime/index.d.ts +3 -0
  97. package/dist/src/shared/runtime/typings/runtime.d.ts +2 -0
  98. package/dist/src/shared/version/constants/version.constant.d.ts +2 -0
  99. package/dist/src/shared/version/index.d.ts +1 -0
  100. package/dist/src/shared/wasm-codec/functions/decode-compose-output.function.d.ts +5 -0
  101. package/dist/src/shared/wasm-codec/functions/decode-document-manifest-list.function.d.ts +14 -0
  102. package/dist/src/shared/wasm-codec/functions/decode-document-manifest.function.d.ts +40 -0
  103. package/dist/src/shared/wasm-codec/functions/decode-f32.function.d.ts +2 -0
  104. package/dist/src/shared/wasm-codec/functions/decode-key-value-text.function.d.ts +1 -0
  105. package/dist/src/shared/wasm-codec/functions/decode-remove-flag.function.d.ts +1 -0
  106. package/dist/src/shared/wasm-codec/functions/decode-scored-results.function.d.ts +10 -0
  107. package/dist/src/shared/wasm-codec/functions/decode-section-record-list.function.d.ts +13 -0
  108. package/dist/src/shared/wasm-codec/functions/decode-section-records.function.d.ts +50 -0
  109. package/dist/src/shared/wasm-codec/functions/decode-string.function.d.ts +5 -0
  110. package/dist/src/shared/wasm-codec/functions/decode-u32.function.d.ts +2 -0
  111. package/dist/src/shared/wasm-codec/functions/decode-u64.function.d.ts +2 -0
  112. package/dist/src/shared/wasm-codec/functions/encode-add-section-params.function.d.ts +10 -0
  113. package/dist/src/shared/wasm-codec/functions/encode-group-params.function.d.ts +10 -0
  114. package/dist/src/shared/wasm-codec/functions/encode-identifier-list.function.d.ts +1 -0
  115. package/dist/src/shared/wasm-codec/functions/encode-kbid-list.function.d.ts +1 -0
  116. package/dist/src/shared/wasm-codec/functions/encode-option-string.function.d.ts +1 -0
  117. package/dist/src/shared/wasm-codec/functions/encode-search-params.function.d.ts +1 -0
  118. package/dist/src/shared/wasm-codec/functions/encode-section-type.function.d.ts +1 -0
  119. package/dist/src/shared/wasm-codec/functions/encode-string.function.d.ts +1 -0
  120. package/dist/src/shared/wasm-codec/functions/encode-type-filter-param.function.d.ts +11 -0
  121. package/dist/src/shared/wasm-codec/functions/encode-u32.function.d.ts +1 -0
  122. package/dist/src/shared/wasm-codec/functions/encode-update-section-params.function.d.ts +2 -0
  123. package/dist/src/shared/wasm-codec/index.d.ts +29 -0
  124. package/dist/src/shared/worker-client/classes/worker-client.class.d.ts +230 -0
  125. package/dist/src/shared/worker-client/functions/compute-context-id.function.d.ts +11 -0
  126. package/dist/src/shared/worker-client/functions/create-worker-client.function.d.ts +18 -0
  127. package/dist/src/shared/worker-client/functions/discover-daemon.function.d.ts +14 -0
  128. package/dist/src/shared/worker-client/functions/resolve-db-path.function.d.ts +11 -0
  129. package/dist/src/shared/worker-client/functions/spawn-daemon.function.d.ts +11 -0
  130. package/dist/src/shared/worker-client/functions/wait-for-daemon.function.d.ts +13 -0
  131. package/dist/src/shared/worker-client/index.d.ts +22 -0
  132. package/dist/src/shared/worker-client/typings/add-section-result.model.d.ts +22 -0
  133. package/dist/src/shared/worker-client/typings/content-result.model.d.ts +12 -0
  134. package/dist/src/shared/worker-client/typings/daemon-info.model.d.ts +7 -0
  135. package/dist/src/shared/worker-client/typings/gc-result.model.d.ts +12 -0
  136. package/dist/src/shared/worker-client/typings/integrity-check-result.model.d.ts +36 -0
  137. package/dist/src/shared/worker-client/typings/migrate-result.model.d.ts +11 -0
  138. package/dist/src/shared/worker-client/typings/rebuild-result.model.d.ts +12 -0
  139. package/dist/src/shared/worker-client/typings/remove-section-result.model.d.ts +7 -0
  140. package/dist/src/shared/worker-client/typings/search-params.model.d.ts +12 -0
  141. package/dist/src/shared/worker-client/typings/search-result.model.d.ts +14 -0
  142. package/dist/src/shared/worker-client/typings/section.model.d.ts +26 -0
  143. package/dist/src/shared/worker-client/typings/status-result.model.d.ts +14 -0
  144. package/dist/src/shared/worker-client/typings/version-status.model.d.ts +24 -0
  145. package/dist/src/shared/worker-client/typings/worker-client-error.interface.d.ts +8 -0
  146. package/dist/src/shared/worker-client/typings/worker-client-options.interface.d.ts +30 -0
  147. package/dist/src/shared/worker-daemon/classes/worker-daemon.class.d.ts +147 -0
  148. package/dist/src/shared/worker-daemon/constants/daemon-defaults.constant.d.ts +24 -0
  149. package/dist/src/shared/worker-daemon/functions/create-host-imports.function.d.ts +68 -0
  150. package/dist/src/shared/worker-daemon/functions/create-ipc-server.function.d.ts +18 -0
  151. package/dist/src/shared/worker-daemon/functions/load-wasm-modules.function.d.ts +19 -0
  152. package/dist/src/shared/worker-daemon/functions/read-config.function.d.ts +12 -0
  153. package/dist/src/shared/worker-daemon/functions/run-worker.function.d.ts +26 -0
  154. package/dist/src/shared/worker-daemon/functions/start-daemon.function.d.ts +13 -0
  155. package/dist/src/shared/worker-daemon/index.d.ts +9 -0
  156. package/dist/src/shared/worker-daemon/typings/daemon-config.interface.d.ts +13 -0
  157. package/dist/src/shared/worker-daemon/typings/ipc-handler.interface.d.ts +36 -0
  158. package/dist/src/shared/worker-daemon/typings/wasm-modules.interface.d.ts +7 -0
  159. package/dist/src/worker.d.ts +1 -0
  160. package/dist/vitest.config.d.ts +2 -0
  161. package/dist/worker.mjs +1615 -0
  162. package/dist/worker.mjs.map +7 -0
  163. package/mod.ts +17 -0
  164. package/package.json +85 -0
  165. package/src/cli.ts +62 -0
  166. package/src/shared/cli/constants/defaults.constant.ts +19 -0
  167. package/src/shared/cli/constants/help-text.constant.ts +4 -0
  168. package/src/shared/cli/functions/dispatch-command.function.ts +338 -0
  169. package/src/shared/cli/functions/ensure-db.function.ts +21 -0
  170. package/src/shared/cli/functions/error-to-message.function.ts +9 -0
  171. package/src/shared/cli/functions/find-db.function.ts +20 -0
  172. package/src/shared/cli/functions/format-output.function.ts +64 -0
  173. package/src/shared/cli/functions/generate-help.function.ts +599 -0
  174. package/src/shared/cli/functions/is-stdin-tty.function.ts +12 -0
  175. package/src/shared/cli/functions/parse-args.function.ts +133 -0
  176. package/src/shared/cli/functions/prompt-user.function.ts +36 -0
  177. package/src/shared/cli/functions/run-agent-create.function.ts +114 -0
  178. package/src/shared/cli/functions/run-agent-delete.function.ts +47 -0
  179. package/src/shared/cli/functions/run-agent-get.function.ts +166 -0
  180. package/src/shared/cli/functions/run-agent-list.function.ts +58 -0
  181. package/src/shared/cli/functions/run-check.function.ts +78 -0
  182. package/src/shared/cli/functions/run-cli.function.ts +299 -0
  183. package/src/shared/cli/functions/run-content.function.ts +47 -0
  184. package/src/shared/cli/functions/run-db-init.function.ts +44 -0
  185. package/src/shared/cli/functions/run-db-migrate.function.ts +40 -0
  186. package/src/shared/cli/functions/run-gc.function.ts +45 -0
  187. package/src/shared/cli/functions/run-init.function.ts +91 -0
  188. package/src/shared/cli/functions/run-learn.function.ts +171 -0
  189. package/src/shared/cli/functions/run-migrate.function.ts +85 -0
  190. package/src/shared/cli/functions/run-rebuild.function.ts +47 -0
  191. package/src/shared/cli/functions/run-search.function.ts +62 -0
  192. package/src/shared/cli/functions/run-skill-delete.function.ts +47 -0
  193. package/src/shared/cli/functions/run-skill-get.function.ts +90 -0
  194. package/src/shared/cli/functions/run-skill-learn.function.ts +70 -0
  195. package/src/shared/cli/functions/run-skill-list.function.ts +74 -0
  196. package/src/shared/cli/functions/run-status.function.ts +29 -0
  197. package/src/shared/cli/functions/run-unlearn.function.ts +58 -0
  198. package/src/shared/cli/index.ts +48 -0
  199. package/src/shared/cli/typings/cli-options.interface.ts +55 -0
  200. package/src/shared/cli/typings/command-result.model.ts +15 -0
  201. package/src/shared/cli/typings/learn-result.model.ts +11 -0
  202. package/src/shared/cli/typings/search-display.model.ts +17 -0
  203. package/src/shared/dir-resolver/functions/resolve-base-dir.function.ts +25 -0
  204. package/src/shared/dir-resolver/functions/resolve-wasm-dir.function.ts +5 -0
  205. package/src/shared/dir-resolver/functions/resolve-worker-script.function.ts +13 -0
  206. package/src/shared/dir-resolver/index.ts +7 -0
  207. package/src/shared/dir-resolver/typings/base-dir.interface.ts +4 -0
  208. package/src/shared/hash/functions/compute-sha256.ts +17 -0
  209. package/src/shared/hash/index.ts +1 -0
  210. package/src/shared/mcp/classes/mcp-server.class.ts +448 -0
  211. package/src/shared/mcp/constants/mcp-error-codes.constant.ts +30 -0
  212. package/src/shared/mcp/functions/create-mcp-server.function.ts +14 -0
  213. package/src/shared/mcp/functions/create-prompt-definitions.function.ts +102 -0
  214. package/src/shared/mcp/functions/create-resource-templates.function.ts +23 -0
  215. package/src/shared/mcp/functions/create-tool-definitions.function.ts +401 -0
  216. package/src/shared/mcp/functions/extract-skill-arguments.function.ts +27 -0
  217. package/src/shared/mcp/functions/handle-agent-create.function.ts +116 -0
  218. package/src/shared/mcp/functions/handle-agent-delete.function.ts +50 -0
  219. package/src/shared/mcp/functions/handle-agent-get.function.ts +160 -0
  220. package/src/shared/mcp/functions/handle-agent-list.function.ts +50 -0
  221. package/src/shared/mcp/functions/handle-completion.function.ts +114 -0
  222. package/src/shared/mcp/functions/handle-prompt-get.function.ts +214 -0
  223. package/src/shared/mcp/functions/handle-resource-read.function.ts +56 -0
  224. package/src/shared/mcp/functions/handle-roots-list.function.ts +29 -0
  225. package/src/shared/mcp/functions/handle-tool-call.function.ts +464 -0
  226. package/src/shared/mcp/functions/parse-skill-content.function.ts +38 -0
  227. package/src/shared/mcp/functions/serialize-skill-content.function.ts +12 -0
  228. package/src/shared/mcp/functions/should-emit-log.function.ts +38 -0
  229. package/src/shared/mcp/index.ts +28 -0
  230. package/src/shared/mcp/typings/agent-types.interface.ts +39 -0
  231. package/src/shared/mcp/typings/log-types.interface.ts +26 -0
  232. package/src/shared/mcp/typings/mcp-client.interface.ts +252 -0
  233. package/src/shared/mcp/typings/mcp-types.interface.ts +158 -0
  234. package/src/shared/mcp/typings/prompt-types.interface.ts +39 -0
  235. package/src/shared/mcp/typings/remove-section-result.model.ts +10 -0
  236. package/src/shared/mcp/typings/sampling-types.interface.ts +48 -0
  237. package/src/shared/mcp/typings/skill-types.interface.ts +29 -0
  238. package/src/shared/mod.ts +1 -0
  239. package/src/shared/platform/functions/get-ipc-path.function.ts +19 -0
  240. package/src/shared/platform/functions/get-tmpdir.function.ts +8 -0
  241. package/src/shared/platform/functions/is-process-alive.function.ts +17 -0
  242. package/src/shared/platform/index.ts +3 -0
  243. package/src/shared/runtime/functions/detect-runtime.ts +14 -0
  244. package/src/shared/runtime/functions/get-args.ts +16 -0
  245. package/src/shared/runtime/index.ts +3 -0
  246. package/src/shared/runtime/typings/runtime.ts +2 -0
  247. package/src/shared/version/constants/version.constant.ts +2 -0
  248. package/src/shared/version/index.ts +1 -0
  249. package/src/shared/wasm-codec/functions/decode-compose-output.function.ts +16 -0
  250. package/src/shared/wasm-codec/functions/decode-document-manifest-list.function.ts +70 -0
  251. package/src/shared/wasm-codec/functions/decode-document-manifest.function.ts +87 -0
  252. package/src/shared/wasm-codec/functions/decode-f32.function.ts +16 -0
  253. package/src/shared/wasm-codec/functions/decode-key-value-text.function.ts +15 -0
  254. package/src/shared/wasm-codec/functions/decode-remove-flag.function.ts +3 -0
  255. package/src/shared/wasm-codec/functions/decode-scored-results.function.ts +54 -0
  256. package/src/shared/wasm-codec/functions/decode-section-record-list.function.ts +20 -0
  257. package/src/shared/wasm-codec/functions/decode-section-records.function.ts +162 -0
  258. package/src/shared/wasm-codec/functions/decode-string.function.ts +22 -0
  259. package/src/shared/wasm-codec/functions/decode-u32.function.ts +16 -0
  260. package/src/shared/wasm-codec/functions/decode-u64.function.ts +16 -0
  261. package/src/shared/wasm-codec/functions/encode-add-section-params.function.ts +34 -0
  262. package/src/shared/wasm-codec/functions/encode-group-params.function.ts +28 -0
  263. package/src/shared/wasm-codec/functions/encode-identifier-list.function.ts +17 -0
  264. package/src/shared/wasm-codec/functions/encode-kbid-list.function.ts +17 -0
  265. package/src/shared/wasm-codec/functions/encode-option-string.function.ts +14 -0
  266. package/src/shared/wasm-codec/functions/encode-search-params.function.ts +9 -0
  267. package/src/shared/wasm-codec/functions/encode-section-type.function.ts +18 -0
  268. package/src/shared/wasm-codec/functions/encode-string.function.ts +9 -0
  269. package/src/shared/wasm-codec/functions/encode-type-filter-param.function.ts +13 -0
  270. package/src/shared/wasm-codec/functions/encode-u32.function.ts +5 -0
  271. package/src/shared/wasm-codec/functions/encode-update-section-params.function.ts +16 -0
  272. package/src/shared/wasm-codec/index.ts +36 -0
  273. package/src/shared/worker-client/classes/worker-client.class.ts +427 -0
  274. package/src/shared/worker-client/functions/compute-context-id.function.ts +18 -0
  275. package/src/shared/worker-client/functions/create-worker-client.function.ts +70 -0
  276. package/src/shared/worker-client/functions/discover-daemon.function.ts +47 -0
  277. package/src/shared/worker-client/functions/resolve-db-path.function.ts +21 -0
  278. package/src/shared/worker-client/functions/spawn-daemon.function.ts +35 -0
  279. package/src/shared/worker-client/functions/wait-for-daemon.function.ts +34 -0
  280. package/src/shared/worker-client/index.ts +31 -0
  281. package/src/shared/worker-client/typings/add-section-result.model.ts +22 -0
  282. package/src/shared/worker-client/typings/content-result.model.ts +12 -0
  283. package/src/shared/worker-client/typings/daemon-info.model.ts +7 -0
  284. package/src/shared/worker-client/typings/gc-result.model.ts +12 -0
  285. package/src/shared/worker-client/typings/integrity-check-result.model.ts +43 -0
  286. package/src/shared/worker-client/typings/migrate-result.model.ts +11 -0
  287. package/src/shared/worker-client/typings/rebuild-result.model.ts +12 -0
  288. package/src/shared/worker-client/typings/remove-section-result.model.ts +7 -0
  289. package/src/shared/worker-client/typings/search-params.model.ts +12 -0
  290. package/src/shared/worker-client/typings/search-result.model.ts +14 -0
  291. package/src/shared/worker-client/typings/section.model.ts +27 -0
  292. package/src/shared/worker-client/typings/status-result.model.ts +14 -0
  293. package/src/shared/worker-client/typings/version-status.model.ts +28 -0
  294. package/src/shared/worker-client/typings/worker-client-error.interface.ts +8 -0
  295. package/src/shared/worker-client/typings/worker-client-options.interface.ts +30 -0
  296. package/src/shared/worker-daemon/classes/worker-daemon.class.ts +1001 -0
  297. package/src/shared/worker-daemon/constants/daemon-defaults.constant.ts +29 -0
  298. package/src/shared/worker-daemon/functions/create-host-imports.function.ts +312 -0
  299. package/src/shared/worker-daemon/functions/create-ipc-server.function.ts +83 -0
  300. package/src/shared/worker-daemon/functions/load-wasm-modules.function.ts +98 -0
  301. package/src/shared/worker-daemon/functions/read-config.function.ts +83 -0
  302. package/src/shared/worker-daemon/functions/run-worker.function.ts +57 -0
  303. package/src/shared/worker-daemon/functions/start-daemon.function.ts +74 -0
  304. package/src/shared/worker-daemon/index.ts +16 -0
  305. package/src/shared/worker-daemon/typings/daemon-config.interface.ts +13 -0
  306. package/src/shared/worker-daemon/typings/ipc-handler.interface.ts +39 -0
  307. package/src/shared/worker-daemon/typings/wasm-modules.interface.ts +7 -0
  308. package/src/worker.ts +34 -0
@@ -0,0 +1,299 @@
1
+ import { VERSION } from '../../version/index.ts';
2
+ import type { CliOptions } from '../typings/cli-options.interface.ts';
3
+ import type { CommandResult } from '../typings/command-result.model.ts';
4
+ import type { InitResult } from './run-init.function.ts';
5
+ import type { VersionStatus } from '../../worker-client/typings/version-status.model.ts';
6
+ import type { MigrateResult } from '../../worker-client/typings/migrate-result.model.ts';
7
+ import { parseArgs } from './parse-args.function.ts';
8
+ import { errorToMessage } from './error-to-message.function.ts';
9
+ import { dispatchCommand } from './dispatch-command.function.ts';
10
+ import type { DispatchClient } from './dispatch-command.function.ts';
11
+ import {
12
+ generateHelp,
13
+ isKnownCommand,
14
+ } from './generate-help.function.ts';
15
+
16
+ export interface DisconnectableClient extends DispatchClient {
17
+ checkVersion(targetPath: string): Promise<VersionStatus>;
18
+ migrateDatabase(targetPath: string): Promise<MigrateResult>;
19
+ disconnect(): void;
20
+ }
21
+
22
+ /**
23
+ * Injectable dependencies for the CLI orchestrator.
24
+ *
25
+ * Every external side-effect is injected so the function
26
+ * is fully testable without touching process globals,
27
+ * filesystem, or network.
28
+ */
29
+ export interface CliDeps {
30
+ isStdinTty: () => boolean;
31
+ promptUser: (question: string) => Promise<string>;
32
+ createWorkerClient: (opts: {
33
+ contextPath: string;
34
+ }) => Promise<DisconnectableClient>;
35
+ findDb: (basePath?: string) => string | null;
36
+ runInit: (options: CliOptions) => InitResult;
37
+ startMcpServer?: (contextPath: string) => Promise<void>;
38
+ }
39
+
40
+ /**
41
+ * Full CLI orchestrator extracted from `src/cli.ts`.
42
+ *
43
+ * Parses arguments, routes to the appropriate sub-flow
44
+ * (help, version, init, mcp, migrate, data commands), and
45
+ * returns a `CommandResult` without touching process globals.
46
+ *
47
+ * @param argv - raw CLI arguments (typically `process.argv.slice(2)`)
48
+ * @param deps - injectable dependencies
49
+ */
50
+ export async function runCli(
51
+ argv: string[],
52
+ deps: CliDeps,
53
+ ): Promise<CommandResult> {
54
+ const options = parseArgs(argv);
55
+
56
+ if (options.command === 'help' || !options.command) {
57
+ const helpCommand = options.args[0];
58
+ const helpSubcommand = options.args[1];
59
+ const helpText = generateHelp(helpCommand, helpSubcommand);
60
+ if (helpCommand && !isKnownCommand(helpCommand)) {
61
+ return { output: helpText, exitCode: 1 };
62
+ }
63
+ return { output: helpText, exitCode: 0 };
64
+ }
65
+
66
+ if (options.command === 'version') {
67
+ return { output: VERSION, exitCode: 0 };
68
+ }
69
+
70
+ if (options.command === 'init' || options.command === 'db-init') {
71
+ return handleInit(options, deps);
72
+ }
73
+
74
+ if (options.command === 'mcp') {
75
+ return handleMcp(options, deps);
76
+ }
77
+
78
+ if (
79
+ options.command === 'migrate' ||
80
+ options.command === 'db-migrate'
81
+ ) {
82
+ return handleMigrate(options, deps);
83
+ }
84
+
85
+ return handleDataCommand(options, deps);
86
+ }
87
+
88
+ function handleInit(options: CliOptions, deps: CliDeps): CommandResult {
89
+ const result = deps.runInit(options);
90
+ if (result.ok) {
91
+ return {
92
+ output: result.targetPath,
93
+ exitCode: 0,
94
+ };
95
+ }
96
+ return {
97
+ error: `error: ${result.errorMessage ?? 'init failed'}`,
98
+ exitCode: 1,
99
+ };
100
+ }
101
+
102
+ async function handleMcp(
103
+ options: CliOptions,
104
+ deps: CliDeps,
105
+ ): Promise<CommandResult> {
106
+ const rawDbPath = options.dbPath ?? process.env['KBDB_DIR'];
107
+ if (!rawDbPath) {
108
+ return {
109
+ error: 'error: --db or KBDB_DIR required in MCP mode',
110
+ exitCode: 1,
111
+ };
112
+ }
113
+ const dbDir = deps.findDb(rawDbPath);
114
+ if (!dbDir) {
115
+ return {
116
+ error: `error: no .kbdb database found at ${rawDbPath}`,
117
+ exitCode: 1,
118
+ };
119
+ }
120
+
121
+ let client: DisconnectableClient;
122
+ try {
123
+ client = await deps.createWorkerClient({
124
+ contextPath: dbDir,
125
+ });
126
+ } catch (err) {
127
+ return {
128
+ error: `error: ${errorToMessage(err)}`,
129
+ exitCode: 1,
130
+ };
131
+ }
132
+
133
+ try {
134
+ const gateResult = await versionGate(client, dbDir);
135
+ if (gateResult) return gateResult;
136
+
137
+ if (deps.startMcpServer) {
138
+ await deps.startMcpServer(dbDir);
139
+ }
140
+ return { exitCode: 0 };
141
+ } catch (err) {
142
+ return {
143
+ error: `error: mcp failed: ${errorToMessage(err)}`,
144
+ exitCode: 1,
145
+ };
146
+ } finally {
147
+ client.disconnect();
148
+ }
149
+ }
150
+
151
+ async function handleMigrate(
152
+ options: CliOptions,
153
+ deps: CliDeps,
154
+ ): Promise<CommandResult> {
155
+ const dbDir = deps.findDb(options.dbPath);
156
+ if (!dbDir) {
157
+ const basePath = options.dbPath ?? process.cwd();
158
+ return {
159
+ error: `error: no .kbdb database found at ${basePath}`,
160
+ exitCode: 1,
161
+ };
162
+ }
163
+
164
+ let client: DisconnectableClient;
165
+ try {
166
+ client = await deps.createWorkerClient({
167
+ contextPath: dbDir,
168
+ });
169
+ } catch (err) {
170
+ return {
171
+ error: `error: ${errorToMessage(err)}`,
172
+ exitCode: 1,
173
+ };
174
+ }
175
+
176
+ try {
177
+ const versionStatus = await client.checkVersion(dbDir);
178
+
179
+ if (versionStatus.status === 'too_new') {
180
+ return {
181
+ error:
182
+ `error: database version` +
183
+ ` ${versionStatus.currentVersion.toString()}` +
184
+ ` is newer than kbdb version` +
185
+ ` ${versionStatus.requiredVersion.toString()}` +
186
+ ` -- upgrade kbdb`,
187
+ exitCode: 1,
188
+ };
189
+ }
190
+
191
+ if (versionStatus.status === 'needs_migration') {
192
+ const migrateResult = await client.migrateDatabase(dbDir);
193
+ return {
194
+ output: JSON.stringify(migrateResult, null, 2),
195
+ exitCode: 0,
196
+ };
197
+ }
198
+
199
+ return { output: 'already up to date', exitCode: 0 };
200
+ } catch (err) {
201
+ return {
202
+ error: `error: migrate failed: ${errorToMessage(err)}`,
203
+ exitCode: 1,
204
+ };
205
+ } finally {
206
+ client.disconnect();
207
+ }
208
+ }
209
+
210
+ async function handleDataCommand(
211
+ options: CliOptions,
212
+ deps: CliDeps,
213
+ ): Promise<CommandResult> {
214
+ let dbDir = deps.findDb(options.dbPath);
215
+
216
+ if (!dbDir) {
217
+ const basePath = options.dbPath ?? process.cwd();
218
+
219
+ if (!deps.isStdinTty()) {
220
+ return {
221
+ error:
222
+ `error: no .kbdb database found at ${basePath}` +
223
+ ' -- run `kbdb --init` to create one',
224
+ exitCode: 1,
225
+ };
226
+ }
227
+
228
+ const answer = await deps.promptUser(
229
+ `No .kbdb database found. Create one in ${basePath}? [y/N] `,
230
+ );
231
+
232
+ if (answer.toLowerCase() !== 'y') {
233
+ return {
234
+ error: 'aborted: no database created',
235
+ exitCode: 1,
236
+ };
237
+ }
238
+
239
+ const initResult = deps.runInit(options);
240
+ if (!initResult.ok) {
241
+ return {
242
+ error: `error: ${initResult.errorMessage ?? 'init failed'}`,
243
+ exitCode: 1,
244
+ };
245
+ }
246
+ dbDir = initResult.targetPath;
247
+ }
248
+
249
+ let client: DisconnectableClient;
250
+ try {
251
+ client = await deps.createWorkerClient({
252
+ contextPath: dbDir,
253
+ });
254
+ } catch (err) {
255
+ return {
256
+ error: `error: ${errorToMessage(err)}`,
257
+ exitCode: 1,
258
+ };
259
+ }
260
+
261
+ try {
262
+ const gateResult = await versionGate(client, dbDir);
263
+ if (gateResult) return gateResult;
264
+
265
+ return await dispatchCommand(client, options);
266
+ } finally {
267
+ client.disconnect();
268
+ }
269
+ }
270
+
271
+ async function versionGate(
272
+ client: DisconnectableClient,
273
+ dbDir: string,
274
+ ): Promise<CommandResult | null> {
275
+ const versionStatus = await client.checkVersion(dbDir);
276
+
277
+ if (versionStatus.status === 'needs_migration') {
278
+ return {
279
+ error:
280
+ 'error: database needs migration' +
281
+ ' -- run `kbdb --migrate`',
282
+ exitCode: 1,
283
+ };
284
+ }
285
+
286
+ if (versionStatus.status === 'too_new') {
287
+ return {
288
+ error:
289
+ `error: database version` +
290
+ ` ${versionStatus.currentVersion.toString()}` +
291
+ ` is newer than kbdb version` +
292
+ ` ${versionStatus.requiredVersion.toString()}` +
293
+ ` -- upgrade kbdb`,
294
+ exitCode: 1,
295
+ };
296
+ }
297
+
298
+ return null;
299
+ }
@@ -0,0 +1,47 @@
1
+ import type { CliOptions } from '../typings/cli-options.interface.ts';
2
+
3
+ /** Result returned by content composition. */
4
+ interface ContentResult {
5
+ /** MIME type of the composed output, always `'text/markdown'`. */
6
+ readonly type: 'text/markdown';
7
+ /** The composed Markdown string. */
8
+ readonly data: string;
9
+ /** Character count of `data` (not byte count). */
10
+ readonly total: number;
11
+ }
12
+
13
+ /**
14
+ * Minimal client interface required by `runContent`.
15
+ *
16
+ * Decouples the CLI function from the concrete `WorkerClient`
17
+ * implementation, making it straightforward to test with mocks.
18
+ */
19
+ export interface ContentClient {
20
+ /**
21
+ * Composes a rendered Markdown document from one or more kbid
22
+ * or docid identifiers.
23
+ *
24
+ * @param ids - list of kbid or docid identifiers (may be mixed)
25
+ * @returns composed Markdown result with character count
26
+ */
27
+ content(ids: string[]): Promise<ContentResult>;
28
+ }
29
+
30
+ /**
31
+ * Composes and returns a rendered Markdown document from the
32
+ * identifiers listed in `options.args`.
33
+ *
34
+ * Delegates to `client.content(ids)`. The caller is responsible for
35
+ * writing the result to stdout.
36
+ *
37
+ * @param client - content client to delegate composition to
38
+ * @param options - parsed CLI options whose `args` list the kbid or
39
+ * docid identifiers to compose
40
+ * @returns the composed content result from the daemon
41
+ */
42
+ export async function runContent(
43
+ client: ContentClient,
44
+ options: CliOptions,
45
+ ): Promise<ContentResult> {
46
+ return client.content(options.args);
47
+ }
@@ -0,0 +1,44 @@
1
+ import type { CliOptions } from '../typings/cli-options.interface.ts';
2
+ import type { CommandResult } from '../typings/command-result.model.ts';
3
+ import type { InitResult } from './run-init.function.ts';
4
+
5
+ /**
6
+ * Callable signature for the init operation injected into
7
+ * `runDbInit`.
8
+ */
9
+ export type RunInitFn = (options: CliOptions) => InitResult;
10
+
11
+ /**
12
+ * Executes the `db init` compound command by delegating to the
13
+ * provided `runInit` function and mapping the result to a
14
+ * `CommandResult`.
15
+ *
16
+ * Accepts `runInit` as an injectable parameter so the function
17
+ * can be tested without touching the filesystem.
18
+ *
19
+ * @param options - parsed CLI options (forwarded to `runInitFn`)
20
+ * @param runInitFn - function that performs the actual
21
+ * initialisation
22
+ * @returns a `CommandResult` with JSON output on success or an
23
+ * error message on failure
24
+ */
25
+ export function runDbInit(
26
+ options: CliOptions,
27
+ runInitFn: RunInitFn,
28
+ ): CommandResult {
29
+ const result = runInitFn(options);
30
+ if (result.ok) {
31
+ return {
32
+ output: JSON.stringify(
33
+ { path: result.targetPath, created: true },
34
+ null,
35
+ 2,
36
+ ),
37
+ exitCode: 0,
38
+ };
39
+ }
40
+ return {
41
+ error: result.errorMessage ?? 'init failed',
42
+ exitCode: 1,
43
+ };
44
+ }
@@ -0,0 +1,40 @@
1
+ import type { CliOptions } from '../typings/cli-options.interface.ts';
2
+ import type { MigrateResult } from '../../worker-client/typings/migrate-result.model.ts';
3
+
4
+ /**
5
+ * Minimal client interface required by `runDbMigrate`.
6
+ *
7
+ * Intentionally narrower than `MigrateClient` from
8
+ * `run-migrate.function.ts` — this variant skips the version
9
+ * check and unconditionally runs migrations. Callers that need
10
+ * version-aware migration should use `runMigrate` instead.
11
+ */
12
+ export interface DbMigrateClient {
13
+ /**
14
+ * Runs all pending database migrations.
15
+ *
16
+ * @param targetPath - absolute path to the `.kbdb` database root
17
+ */
18
+ migrateDatabase(targetPath: string): Promise<MigrateResult>;
19
+ }
20
+
21
+ /**
22
+ * Runs database migrations unconditionally via `client.migrateDatabase`.
23
+ *
24
+ * Resolves the target path from `options.dbPath` when provided,
25
+ * falling back to `process.cwd()`. Does not perform a version
26
+ * compatibility check before migrating — use `runMigrate` for
27
+ * version-aware migration flows.
28
+ *
29
+ * @param client - database migration client to delegate to
30
+ * @param options - parsed CLI options supplying the optional
31
+ * `dbPath`
32
+ * @returns the migration report returned by the daemon
33
+ */
34
+ export async function runDbMigrate(
35
+ client: DbMigrateClient,
36
+ options: CliOptions,
37
+ ): Promise<MigrateResult> {
38
+ const dbPath = options.dbPath ?? process.cwd();
39
+ return client.migrateDatabase(dbPath);
40
+ }
@@ -0,0 +1,45 @@
1
+ import type { CliOptions } from '../typings/cli-options.interface.ts';
2
+
3
+ /**
4
+ * Garbage collection report returned by the daemon.
5
+ */
6
+ interface GcResult {
7
+ /** Number of unreferenced sections removed. */
8
+ readonly removed_sections: number;
9
+ /** Total bytes freed by removing unreferenced sections. */
10
+ readonly removed_bytes: number;
11
+ /** Time taken to complete garbage collection, in milliseconds. */
12
+ readonly elapsed_ms: number;
13
+ }
14
+
15
+ /**
16
+ * Minimal client interface required by `runGc`.
17
+ *
18
+ * Decouples the CLI function from the concrete `WorkerClient`
19
+ * implementation, making it straightforward to test with mocks.
20
+ */
21
+ export interface GcClient {
22
+ /**
23
+ * Removes unreferenced sections from the database.
24
+ *
25
+ * @returns a report of sections removed and bytes freed
26
+ */
27
+ gc(): Promise<GcResult>;
28
+ }
29
+
30
+ /**
31
+ * Runs garbage collection on the database and returns the report.
32
+ *
33
+ * Delegates to `client.gc()`. The caller is responsible for printing
34
+ * the result to stdout.
35
+ *
36
+ * @param client - gc client to delegate to
37
+ * @param _options - parsed CLI options (unused; reserved for future flags)
38
+ * @returns the garbage collection report from the daemon
39
+ */
40
+ export async function runGc(
41
+ client: GcClient,
42
+ _options: CliOptions,
43
+ ): Promise<GcResult> {
44
+ return client.gc();
45
+ }
@@ -0,0 +1,91 @@
1
+ import { existsSync, mkdirSync, writeFileSync } from 'node:fs';
2
+ import { join, resolve } from 'node:path';
3
+ import type { CliOptions } from '../typings/cli-options.interface.ts';
4
+
5
+ /**
6
+ * Result returned by `runInit` describing whether the directory was
7
+ * successfully initialised.
8
+ */
9
+ export interface InitResult {
10
+ /** `true` when the database directory was created successfully. */
11
+ readonly ok: boolean;
12
+ /** Absolute path to the directory that was (or should have been) initialised. */
13
+ readonly targetPath: string;
14
+ /**
15
+ * Human-readable error message when `ok` is `false`.
16
+ * `undefined` when `ok` is `true`.
17
+ */
18
+ readonly errorMessage?: string;
19
+ }
20
+
21
+ const DB_FORMAT_VERSION = 1;
22
+
23
+ const CATALOG_TOML = `version = ${DB_FORMAT_VERSION.toString()}
24
+ section_count = 0
25
+ document_count = 0
26
+ created_at = 0
27
+ updated_at = 0
28
+ checksum = ""
29
+ `;
30
+
31
+ const WORKER_TOML = '# kbdb worker configuration\n';
32
+
33
+ const GITIGNORE = [
34
+ '# kbdb binary indexes (auto-generated)',
35
+ 'corpus.bin',
36
+ 'inverted.idx',
37
+ 'positional.idx',
38
+ '',
39
+ ].join('\n');
40
+
41
+ /**
42
+ * Creates the `.kbdb` directory scaffold directly on disk.
43
+ *
44
+ * This mirrors the Rust `init_directory` in
45
+ * `engine/crates/fs-database/src/scaffold.rs` and does not
46
+ * require a running worker daemon.
47
+ */
48
+ function createScaffold(dbDir: string): void {
49
+ mkdirSync(dbDir, { recursive: true });
50
+ mkdirSync(join(dbDir, 'sections'));
51
+ mkdirSync(join(dbDir, 'documents'));
52
+ writeFileSync(join(dbDir, 'catalog.toml'), CATALOG_TOML);
53
+ writeFileSync(join(dbDir, 'corpus.bin'), '');
54
+ writeFileSync(join(dbDir, 'inverted.idx'), '');
55
+ writeFileSync(join(dbDir, 'positional.idx'), '');
56
+ writeFileSync(join(dbDir, 'worker.toml'), WORKER_TOML);
57
+ writeFileSync(join(dbDir, '.gitignore'), GITIGNORE);
58
+ }
59
+
60
+ /**
61
+ * Initialises a new `.kbdb` database directory.
62
+ *
63
+ * Creates the full scaffold directly on disk without requiring a
64
+ * running worker daemon. Resolves the target path from
65
+ * `options.dbPath` or `process.cwd()`.
66
+ *
67
+ * @param options - parsed CLI options supplying the optional `dbPath`
68
+ * @returns init result indicating success or failure
69
+ */
70
+ export function runInit(options: CliOptions): InitResult {
71
+ const basePath = options.dbPath
72
+ ? resolve(options.dbPath)
73
+ : process.cwd();
74
+ const dbDir = join(basePath, '.kbdb');
75
+
76
+ try {
77
+ if (existsSync(dbDir)) {
78
+ return {
79
+ ok: false,
80
+ targetPath: dbDir,
81
+ errorMessage: `database directory already exists: ${dbDir}`,
82
+ };
83
+ }
84
+ createScaffold(dbDir);
85
+ return { ok: true, targetPath: dbDir };
86
+ } catch (err: unknown) {
87
+ const errorMessage =
88
+ err instanceof Error ? err.message : String(err);
89
+ return { ok: false, targetPath: dbDir, errorMessage };
90
+ }
91
+ }