@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,29 @@
1
+ /** Default maximum number of items held in daemon memory. */
2
+ export const DEFAULT_ITEM_LIMIT = 1000;
3
+
4
+ /** Default maximum number of index entries held in daemon memory. */
5
+ export const DEFAULT_INDEX_LIMIT = 100;
6
+
7
+ /**
8
+ * Default idle timeout in milliseconds before the daemon shuts
9
+ * down automatically (5 minutes).
10
+ */
11
+ export const DEFAULT_TIMEOUT_MS = 300_000;
12
+
13
+ /**
14
+ * Default time-to-live in milliseconds for each cached item
15
+ * (1 minute).
16
+ */
17
+ export const DEFAULT_ITEM_TTL_MS = 60_000;
18
+
19
+ /** Filename of the daemon configuration file inside `.kbdb/`. */
20
+ export const WORKER_TOML_FILENAME = 'worker.toml';
21
+
22
+ /**
23
+ * Database format version the daemon supports.
24
+ *
25
+ * Read from `catalog.toml` at startup. If the catalog version
26
+ * differs, the daemon prints a structured error to stderr and
27
+ * exits with code 1 before loading any WASM modules.
28
+ */
29
+ export const DB_FORMAT_VERSION = 1;
@@ -0,0 +1,312 @@
1
+ import {
2
+ readFileSync,
3
+ writeFileSync,
4
+ renameSync,
5
+ unlinkSync,
6
+ readdirSync,
7
+ mkdirSync,
8
+ existsSync,
9
+ } from 'node:fs';
10
+ import { join } from 'node:path';
11
+
12
+ // ---------------------------------------------------------------------------
13
+ // FsAdapter interface (enables dependency injection for testing)
14
+ // ---------------------------------------------------------------------------
15
+
16
+ /**
17
+ * Minimal filesystem operations required by the host imports.
18
+ *
19
+ * The default implementation delegates to `node:fs` synchronous
20
+ * APIs. Tests inject a stub to avoid touching the real filesystem.
21
+ */
22
+ export interface FsAdapter {
23
+ /** Read entire file as a Buffer. */
24
+ readFileSync(path: string): Buffer;
25
+ /** Write data buffer to file. */
26
+ writeFileSync(path: string, data: Uint8Array): void;
27
+ /** Rename `oldPath` to `newPath`. */
28
+ renameSync(oldPath: string, newPath: string): void;
29
+ /** Delete a file. */
30
+ unlinkSync(path: string): void;
31
+ /** List directory entries as strings. */
32
+ readdirSync(path: string): string[];
33
+ /** Create a directory (and parents) recursively. */
34
+ mkdirSync(path: string): void;
35
+ /** Return true if the path exists. */
36
+ existsSync(path: string): boolean;
37
+ }
38
+
39
+ // ---------------------------------------------------------------------------
40
+ // MemoryRef interface
41
+ // ---------------------------------------------------------------------------
42
+
43
+ /**
44
+ * A mutable reference to the WASM linear memory.
45
+ *
46
+ * The `memory` field starts as `null` and is wired up after the
47
+ * `kb-worker` WASM module is instantiated (the module exports its
48
+ * own memory rather than importing one). Host import functions
49
+ * read `memory` at call time, not at creation time, so the ref
50
+ * can be populated after the import record is built.
51
+ */
52
+ export interface MemoryRef {
53
+ memory: WebAssembly.Memory | null;
54
+ }
55
+
56
+ // ---------------------------------------------------------------------------
57
+ // AllocatorRef interface
58
+ // ---------------------------------------------------------------------------
59
+
60
+ /**
61
+ * A mutable reference to the WASM allocator function.
62
+ *
63
+ * The `alloc` field starts as `null` and is wired up after the
64
+ * `kb-worker` WASM module is instantiated. Host import functions
65
+ * that must allocate memory in WASM linear memory check this
66
+ * reference before proceeding and return -1 when it is null.
67
+ */
68
+ export interface AllocatorRef {
69
+ /**
70
+ * Allocates `size` bytes in WASM linear memory and returns the
71
+ * byte offset of the allocated region, or `null` when the
72
+ * allocator is not yet available.
73
+ */
74
+ alloc: ((size: number) => number) | null;
75
+ }
76
+
77
+ /** Default `FsAdapter` backed by `node:fs` synchronous APIs. */
78
+ const nodeFsAdapter: FsAdapter = {
79
+ readFileSync: (p) => readFileSync(p),
80
+ writeFileSync: (p, d) => {
81
+ writeFileSync(p, d);
82
+ },
83
+ renameSync: (o, n) => {
84
+ renameSync(o, n);
85
+ },
86
+ unlinkSync: (p) => {
87
+ unlinkSync(p);
88
+ },
89
+ readdirSync: (p) => readdirSync(p),
90
+ mkdirSync: (p) => {
91
+ mkdirSync(p, { recursive: true });
92
+ },
93
+ existsSync: (p) => existsSync(p),
94
+ };
95
+
96
+ /** Default `MemoryRef` with no memory wired. */
97
+ const nullMemory: MemoryRef = { memory: null };
98
+
99
+ /** Default `AllocatorRef` with no allocator wired. */
100
+ const nullAllocator: AllocatorRef = { alloc: null };
101
+
102
+ // ---------------------------------------------------------------------------
103
+ // createHostImports
104
+ // ---------------------------------------------------------------------------
105
+
106
+ /**
107
+ * Creates the host I/O import functions required by `kb-worker.wasm`.
108
+ *
109
+ * All functions read the WASM linear memory through `memoryRef`,
110
+ * a deferred reference wired up after module instantiation (the
111
+ * module exports its own memory). Paths from WASM are resolved
112
+ * relative to `contextPath`.
113
+ *
114
+ * @param contextPath - absolute path to the `.kbdb` database directory
115
+ * @param memoryRef - deferred reference to the WASM module's
116
+ * exported memory; wired after instantiation
117
+ * @param fs - filesystem adapter; defaults to real `node:fs` operations
118
+ * @param allocator - reference to the WASM allocator; wired after
119
+ * `kb-worker` instantiation. Defaults to a null (uninitialized) ref.
120
+ * @returns a record of host import functions keyed by their WASM import
121
+ * names
122
+ */
123
+ export function createHostImports(
124
+ contextPath: string,
125
+ memoryRef: MemoryRef = nullMemory,
126
+ fs: FsAdapter = nodeFsAdapter,
127
+ allocator: AllocatorRef = nullAllocator,
128
+ ): Record<string, WebAssembly.ImportValue> {
129
+ function getBuffer(): ArrayBuffer {
130
+ if (!memoryRef.memory) {
131
+ throw new Error('WASM memory not initialized');
132
+ }
133
+ return memoryRef.memory.buffer;
134
+ }
135
+
136
+ function readPathArg(ptr: number, len: number): string {
137
+ const bytes = new Uint8Array(getBuffer(), ptr, len);
138
+ const relative = new TextDecoder().decode(bytes);
139
+ return join(contextPath, relative);
140
+ }
141
+
142
+ return {
143
+ /**
144
+ * Reads a file at the path encoded in shared memory and writes
145
+ * the result back via the out-pointer protocol.
146
+ *
147
+ * `outPtr` and `outLen` are WASM linear-memory addresses. On
148
+ * success, the host allocates a buffer via `allocator.alloc`,
149
+ * copies the file data there, and writes the allocated pointer
150
+ * as a LE u32 to `outPtr` and the data length as a LE u32 to
151
+ * `outLen`. Returns 0 on success, -1 on error or when the
152
+ * allocator is not yet available.
153
+ */
154
+ host_read_file(
155
+ pathPtr: number,
156
+ pathLen: number,
157
+ outPtr: number,
158
+ outLen: number,
159
+ ): number {
160
+ try {
161
+ if (!allocator.alloc) return -1;
162
+ const filePath = readPathArg(pathPtr, pathLen);
163
+ const data = fs.readFileSync(filePath);
164
+ const dataLen = data.byteLength;
165
+ const dv = new DataView(getBuffer());
166
+ if (dataLen === 0) {
167
+ dv.setUint32(outPtr, 0, true);
168
+ dv.setUint32(outLen, 0, true);
169
+ return 0;
170
+ }
171
+ const dataPtr = allocator.alloc(dataLen);
172
+ if (dataPtr === 0) return -1;
173
+ const memView = new Uint8Array(getBuffer());
174
+ memView.set(
175
+ new Uint8Array(
176
+ data.buffer,
177
+ data.byteOffset,
178
+ data.byteLength,
179
+ ),
180
+ dataPtr,
181
+ );
182
+ dv.setUint32(outPtr, dataPtr, true);
183
+ dv.setUint32(outLen, dataLen, true);
184
+ return 0;
185
+ } catch {
186
+ return -1;
187
+ }
188
+ },
189
+
190
+ /**
191
+ * Writes bytes from shared memory (`dataPtr`, `dataLen`) to the
192
+ * file at the path encoded in shared memory. Returns 0 on
193
+ * success, -1 on error.
194
+ */
195
+ host_write_file(
196
+ pathPtr: number,
197
+ pathLen: number,
198
+ dataPtr: number,
199
+ dataLen: number,
200
+ ): number {
201
+ try {
202
+ const filePath = readPathArg(pathPtr, pathLen);
203
+ const data = new Uint8Array(getBuffer(), dataPtr, dataLen);
204
+ fs.writeFileSync(filePath, data);
205
+ return 0;
206
+ } catch {
207
+ return -1;
208
+ }
209
+ },
210
+
211
+ /**
212
+ * Renames the file at `oldPtr/oldLen` to `newPtr/newLen`.
213
+ * Both paths are resolved relative to `contextPath`. Returns 0
214
+ * on success, -1 on error.
215
+ */
216
+ host_rename(
217
+ oldPtr: number,
218
+ oldLen: number,
219
+ newPtr: number,
220
+ newLen: number,
221
+ ): number {
222
+ try {
223
+ const oldPath = readPathArg(oldPtr, oldLen);
224
+ const newPath = readPathArg(newPtr, newLen);
225
+ fs.renameSync(oldPath, newPath);
226
+ return 0;
227
+ } catch {
228
+ return -1;
229
+ }
230
+ },
231
+
232
+ /**
233
+ * Deletes the file at the path encoded in shared memory.
234
+ * Returns 0 on success, -1 on error.
235
+ */
236
+ host_remove_file(pathPtr: number, pathLen: number): number {
237
+ try {
238
+ const filePath = readPathArg(pathPtr, pathLen);
239
+ fs.unlinkSync(filePath);
240
+ return 0;
241
+ } catch {
242
+ return -1;
243
+ }
244
+ },
245
+
246
+ /**
247
+ * Lists the directory at the path encoded in shared memory and
248
+ * writes the result back via the out-pointer protocol.
249
+ *
250
+ * `outPtr` and `outLen` are WASM linear-memory addresses. On
251
+ * success, the host allocates a buffer via `allocator.alloc`,
252
+ * copies a newline-delimited UTF-8 listing there, and writes
253
+ * the allocated pointer as a LE u32 to `outPtr` and the byte
254
+ * length as a LE u32 to `outLen`. Returns 0 on success, -1 on
255
+ * error or when the allocator is not yet available.
256
+ */
257
+ host_list_dir(
258
+ pathPtr: number,
259
+ pathLen: number,
260
+ outPtr: number,
261
+ outLen: number,
262
+ ): number {
263
+ try {
264
+ if (!allocator.alloc) return -1;
265
+ const dirPath = readPathArg(pathPtr, pathLen);
266
+ const entries = fs.readdirSync(dirPath);
267
+ const text = entries.join('\n');
268
+ const encoded = new TextEncoder().encode(text);
269
+ const dv = new DataView(getBuffer());
270
+ if (encoded.length === 0) {
271
+ dv.setUint32(outPtr, 0, true);
272
+ dv.setUint32(outLen, 0, true);
273
+ return 0;
274
+ }
275
+ const dataPtr = allocator.alloc(encoded.length);
276
+ if (dataPtr === 0) return -1;
277
+ new Uint8Array(getBuffer(), dataPtr, encoded.length).set(
278
+ encoded,
279
+ );
280
+ dv.setUint32(outPtr, dataPtr, true);
281
+ dv.setUint32(outLen, encoded.length, true);
282
+ return 0;
283
+ } catch {
284
+ return -1;
285
+ }
286
+ },
287
+
288
+ /**
289
+ * Creates the directory at the path encoded in shared memory,
290
+ * including any missing parent directories (recursive). Returns
291
+ * 0 on success, -1 on error.
292
+ */
293
+ host_mkdir(pathPtr: number, pathLen: number): number {
294
+ try {
295
+ const dirPath = readPathArg(pathPtr, pathLen);
296
+ fs.mkdirSync(dirPath);
297
+ return 0;
298
+ } catch {
299
+ return -1;
300
+ }
301
+ },
302
+
303
+ /**
304
+ * Checks whether the file at the path encoded in shared memory
305
+ * exists. Returns 1 if it exists, 0 if it does not.
306
+ */
307
+ host_file_exists(pathPtr: number, pathLen: number): number {
308
+ const filePath = readPathArg(pathPtr, pathLen);
309
+ return fs.existsSync(filePath) ? 1 : 0;
310
+ },
311
+ };
312
+ }
@@ -0,0 +1,83 @@
1
+ import { createServer } from 'node:net';
2
+ import type { Server, Socket } from 'node:net';
3
+ import type {
4
+ IpcHandler,
5
+ IpcRequest,
6
+ IpcResponse,
7
+ } from '../typings/ipc-handler.interface.ts';
8
+
9
+ /**
10
+ * Creates a Unix socket (or named pipe on Windows) IPC server that
11
+ * reads newline-delimited JSON-RPC requests and writes responses.
12
+ *
13
+ * Each line received on a socket connection is parsed as an
14
+ * `IpcRequest`, dispatched to `handler`, and the resulting
15
+ * `IpcResponse` is written back as a newline-terminated JSON string.
16
+ * A malformed line that cannot be parsed results in a JSON-RPC
17
+ * parse-error response (`-32700`).
18
+ *
19
+ * @param socketPath - path to the Unix domain socket or Windows
20
+ * named pipe
21
+ * @param handler - async function invoked for each valid request
22
+ * @returns the listening `net.Server` instance
23
+ */
24
+ export function createIpcServer(
25
+ socketPath: string,
26
+ handler: IpcHandler,
27
+ ): Server {
28
+ const server = createServer((socket) => {
29
+ let buffer = '';
30
+
31
+ socket.on('data', (chunk: Buffer) => {
32
+ buffer += chunk.toString();
33
+ const lines = buffer.split('\n');
34
+ buffer = lines.pop() ?? '';
35
+
36
+ for (const line of lines) {
37
+ if (!line.trim()) continue;
38
+ void handleLine(line, handler, socket);
39
+ }
40
+ });
41
+
42
+ socket.on('error', (err: Error) => {
43
+ // eslint-disable-next-line no-console
44
+ console.error(
45
+ `kbdb-worker: ipc connection error: ${err.message}`,
46
+ );
47
+ });
48
+ });
49
+
50
+ server.on('error', (err: Error) => {
51
+ // eslint-disable-next-line no-console
52
+ console.error(`kbdb-worker: ipc server error: ${err.message}`);
53
+ });
54
+
55
+ server.listen(socketPath);
56
+ return server;
57
+ }
58
+
59
+ function bigintReplacer(_key: string, value: unknown): unknown {
60
+ return typeof value === 'bigint' ? Number(value) : value;
61
+ }
62
+
63
+ async function handleLine(
64
+ line: string,
65
+ handler: IpcHandler,
66
+ socket: Socket,
67
+ ): Promise<void> {
68
+ let response: IpcResponse;
69
+ try {
70
+ const request = JSON.parse(line) as IpcRequest;
71
+ response = await handler(request);
72
+ } catch {
73
+ response = {
74
+ jsonrpc: '2.0',
75
+ id: 0,
76
+ error: {
77
+ code: -32700,
78
+ message: 'Parse error',
79
+ },
80
+ };
81
+ }
82
+ socket.write(JSON.stringify(response, bigintReplacer) + '\n');
83
+ }
@@ -0,0 +1,98 @@
1
+ import { readFileSync } from 'node:fs';
2
+ import { join } from 'node:path';
3
+ import type { DaemonConfig } from '../typings/daemon-config.interface.ts';
4
+ import type { WasmModules } from '../typings/wasm-modules.interface.ts';
5
+ import { createHostImports } from './create-host-imports.function.ts';
6
+ import type {
7
+ AllocatorRef,
8
+ MemoryRef,
9
+ } from './create-host-imports.function.ts';
10
+
11
+ /**
12
+ * Loads and instantiates the `kb-worker` WASM module.
13
+ *
14
+ * `kb-worker.wasm` statically links the `fs-database` and
15
+ * `query-parser` crates, so only one module needs to be
16
+ * instantiated. The module exports its own linear memory;
17
+ * host I/O imports read it through a deferred `MemoryRef`
18
+ * that is wired up immediately after instantiation.
19
+ *
20
+ * @param config - daemon configuration, used to supply the context
21
+ * path to the host I/O imports
22
+ * @param wasmDir - absolute path to the directory containing the
23
+ * compiled WASM subdirectories (`query-parser/`, `fs-database/`,
24
+ * `kb-worker/`)
25
+ * @throws when the WASM file cannot be read or instantiated
26
+ */
27
+ export async function loadWasmModules(
28
+ config: DaemonConfig,
29
+ wasmDir: string,
30
+ ): Promise<WasmModules> {
31
+ const memoryRef: MemoryRef = { memory: null };
32
+ const allocator: AllocatorRef = { alloc: null };
33
+
34
+ const hostImports = createHostImports(
35
+ config.contextPath,
36
+ memoryRef,
37
+ undefined,
38
+ allocator,
39
+ );
40
+
41
+ const wkPath = join(wasmDir, 'kb-worker', 'kbdb_worker_bg.wasm');
42
+ const wkBytes = readWasmFileOrThrow(wkPath, 'kb-worker');
43
+
44
+ let wkModule: WebAssembly.Module;
45
+ try {
46
+ wkModule = await WebAssembly.compile(wkBytes);
47
+ } catch (err) {
48
+ rethrow('compile', 'kb-worker', err);
49
+ }
50
+
51
+ let kbWorker: WebAssembly.Instance;
52
+ try {
53
+ kbWorker = await WebAssembly.instantiate(wkModule, {
54
+ env: { ...hostImports },
55
+ });
56
+ } catch (err) {
57
+ rethrow('instantiate', 'kb-worker', err);
58
+ }
59
+
60
+ const memory = kbWorker.exports['memory'] as WebAssembly.Memory;
61
+ memoryRef.memory = memory;
62
+
63
+ allocator.alloc = (size: number): number => {
64
+ const fn_ = kbWorker.exports['kbdb_alloc'] as
65
+ | ((s: number) => number)
66
+ | undefined;
67
+ if (typeof fn_ !== 'function') return 0;
68
+ return fn_(size);
69
+ };
70
+
71
+ return { memory, kbWorker };
72
+ }
73
+
74
+ function readWasmFileOrThrow(path: string, label: string): ArrayBuffer {
75
+ try {
76
+ const buf = readFileSync(path);
77
+ return buf.buffer.slice(
78
+ buf.byteOffset,
79
+ buf.byteOffset + buf.byteLength,
80
+ );
81
+ } catch (err) {
82
+ rethrow('load', label, err, path);
83
+ }
84
+ }
85
+
86
+ function rethrow(
87
+ stage: string,
88
+ label: string,
89
+ err: unknown,
90
+ path?: string,
91
+ ): never {
92
+ const detail = err instanceof Error ? err.message : String(err);
93
+ const suffix = path ? ` (path: ${path})` : '';
94
+ const message = `failed to ${stage} ${label} WASM: ${detail}${suffix}`;
95
+ // eslint-disable-next-line no-console
96
+ console.error(`kbdb-worker: ${message}`);
97
+ throw new Error(message, { cause: err });
98
+ }
@@ -0,0 +1,83 @@
1
+ import { readFileSync } from 'node:fs';
2
+ import { join } from 'node:path';
3
+ import type { DaemonConfig } from '../typings/daemon-config.interface.ts';
4
+ import {
5
+ DEFAULT_ITEM_LIMIT,
6
+ DEFAULT_INDEX_LIMIT,
7
+ DEFAULT_TIMEOUT_MS,
8
+ DEFAULT_ITEM_TTL_MS,
9
+ WORKER_TOML_FILENAME,
10
+ } from '../constants/daemon-defaults.constant.ts';
11
+
12
+ /**
13
+ * Reads the `worker.toml` configuration file from the given
14
+ * context directory and returns a `DaemonConfig`.
15
+ *
16
+ * Falls back to all-default values when the file is absent or
17
+ * unreadable.
18
+ *
19
+ * @param contextPath - absolute path to the `.kbdb` context
20
+ * directory
21
+ */
22
+ export function readConfig(contextPath: string): DaemonConfig {
23
+ const configPath = join(contextPath, WORKER_TOML_FILENAME);
24
+ try {
25
+ const text = readFileSync(configPath, 'utf-8');
26
+ return parseWorkerToml(text, contextPath);
27
+ } catch {
28
+ return {
29
+ contextPath,
30
+ itemLimit: DEFAULT_ITEM_LIMIT,
31
+ indexLimit: DEFAULT_INDEX_LIMIT,
32
+ timeoutMs: DEFAULT_TIMEOUT_MS,
33
+ itemTtlMs: DEFAULT_ITEM_TTL_MS,
34
+ };
35
+ }
36
+ }
37
+
38
+ function parseWorkerToml(
39
+ text: string,
40
+ contextPath: string,
41
+ ): DaemonConfig {
42
+ const config: DaemonConfig = {
43
+ contextPath,
44
+ itemLimit: DEFAULT_ITEM_LIMIT,
45
+ indexLimit: DEFAULT_INDEX_LIMIT,
46
+ timeoutMs: DEFAULT_TIMEOUT_MS,
47
+ itemTtlMs: DEFAULT_ITEM_TTL_MS,
48
+ };
49
+
50
+ const result = { ...config };
51
+
52
+ for (const line of text.split('\n')) {
53
+ const trimmed = line.trim();
54
+ if (trimmed.startsWith('#') || !trimmed.includes('=')) {
55
+ continue;
56
+ }
57
+ const eqIdx = trimmed.indexOf('=');
58
+ const key = trimmed.slice(0, eqIdx).trim();
59
+ const value = trimmed.slice(eqIdx + 1).trim();
60
+ switch (key) {
61
+ case 'item_limit':
62
+ result.itemLimit =
63
+ parseInt(value, 10) || DEFAULT_ITEM_LIMIT;
64
+ break;
65
+ case 'index_limit':
66
+ result.indexLimit =
67
+ parseInt(value, 10) || DEFAULT_INDEX_LIMIT;
68
+ break;
69
+ case 'timeout_ms':
70
+ result.timeoutMs =
71
+ parseInt(value, 10) || DEFAULT_TIMEOUT_MS;
72
+ break;
73
+ case 'item_ttl_ms':
74
+ result.itemTtlMs =
75
+ parseInt(value, 10) || DEFAULT_ITEM_TTL_MS;
76
+ break;
77
+ default:
78
+ break;
79
+ }
80
+ }
81
+
82
+ return result;
83
+ }
@@ -0,0 +1,57 @@
1
+ import { resolve } from 'node:path';
2
+
3
+ /** Result of the worker entry point. */
4
+ export interface WorkerResult {
5
+ /** Error message (if any). */
6
+ error?: string;
7
+ /** Process exit code. `0` means success. */
8
+ exitCode: number;
9
+ }
10
+
11
+ /**
12
+ * Injectable dependencies for the worker entry point.
13
+ */
14
+ export interface WorkerDeps {
15
+ existsSync: (path: string) => boolean;
16
+ startDaemon: (
17
+ contextPath: string,
18
+ wasmDir?: string,
19
+ ) => Promise<void>;
20
+ wasmDir?: string;
21
+ }
22
+
23
+ /**
24
+ * Worker daemon entry point extracted from `src/worker.ts`.
25
+ *
26
+ * Validates the context path argument, resolves it to an
27
+ * absolute path, checks existence, and delegates to
28
+ * `startDaemon`.
29
+ *
30
+ * @param argv - raw arguments (expects `[contextPath]`)
31
+ * @param deps - injectable dependencies
32
+ */
33
+ export async function runWorker(
34
+ argv: string[],
35
+ deps: WorkerDeps,
36
+ ): Promise<WorkerResult> {
37
+ const contextPath = argv[0];
38
+
39
+ if (!contextPath) {
40
+ return {
41
+ error: 'usage: kbdb-worker <context-path>',
42
+ exitCode: 1,
43
+ };
44
+ }
45
+
46
+ const resolved = resolve(contextPath);
47
+
48
+ if (!deps.existsSync(resolved)) {
49
+ return {
50
+ error: `error: path not found: ${resolved}`,
51
+ exitCode: 1,
52
+ };
53
+ }
54
+
55
+ await deps.startDaemon(resolved, deps.wasmDir);
56
+ return { exitCode: 0 };
57
+ }