@dikolab/kbdb 0.3.1 → 0.4.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.
- package/README.md +108 -126
- package/dist/chunk-BJPSPNGX.mjs +223 -0
- package/dist/chunk-BJPSPNGX.mjs.map +7 -0
- package/dist/cli.cjs +956 -319
- package/dist/cli.cjs.map +4 -4
- package/dist/cli.d.ts +9 -0
- package/dist/cli.mjs +840 -321
- package/dist/cli.mjs.map +4 -4
- package/dist/kbdb-worker.cjs +714 -94
- package/dist/kbdb-worker.cjs.map +4 -4
- package/dist/mod.cjs +122 -16
- package/dist/mod.cjs.map +3 -3
- package/dist/mod.d.ts +23 -2
- package/dist/mod.mjs +121 -16
- package/dist/mod.mjs.map +3 -3
- package/dist/src/shared/cli/functions/dispatch-command.function.d.ts +2 -1
- package/dist/src/shared/cli/functions/find-db.function.d.ts +7 -8
- package/dist/src/shared/cli/functions/handle-mcp.function.d.ts +1 -1
- package/dist/src/shared/cli/functions/parse-args.function.d.ts +4 -3
- package/dist/src/shared/cli/functions/resolve-kbid-prefix.function.d.ts +27 -0
- package/dist/src/shared/cli/functions/resolve-raw-db-path.function.d.ts +10 -0
- package/dist/src/shared/cli/functions/run-cli.function.d.ts +3 -1
- package/dist/src/shared/cli/functions/run-content.function.d.ts +8 -3
- package/dist/src/shared/cli/functions/run-import.function.d.ts +27 -0
- package/dist/src/shared/cli/functions/run-recall.function.d.ts +10 -4
- package/dist/src/shared/cli/functions/run-search.function.d.ts +7 -0
- package/dist/src/shared/cli/functions/run-unlearn.function.d.ts +9 -4
- package/dist/src/shared/cli/functions/run-worker-restart.function.d.ts +11 -0
- package/dist/src/shared/cli/functions/run-worker-stop.function.d.ts +11 -0
- package/dist/src/shared/cli/index.d.ts +2 -0
- package/dist/src/shared/cli/typings/cli-deps.interface.d.ts +4 -1
- package/dist/src/shared/cli/typings/cli-options.interface.d.ts +17 -0
- package/dist/src/shared/cli/typings/dispatch-client.type.d.ts +2 -1
- package/dist/src/shared/cli/typings/search-display.model.d.ts +6 -0
- package/dist/src/shared/embedding/classes/api-embedding-provider.class.d.ts +33 -0
- package/dist/src/shared/embedding/classes/onnx-embedding-provider.class.d.ts +33 -0
- package/dist/src/shared/embedding/functions/append-vector-entry.function.d.ts +14 -0
- package/dist/src/shared/embedding/functions/create-embedding-provider.function.d.ts +12 -0
- package/dist/src/shared/embedding/functions/detect-stale-vectors.function.d.ts +16 -0
- package/dist/src/shared/embedding/functions/parse-api-embedding-response.function.d.ts +11 -0
- package/dist/src/shared/embedding/functions/rebuild-vectors.function.d.ts +15 -0
- package/dist/src/shared/embedding/functions/scan-semantic-contradictions.function.d.ts +13 -0
- package/dist/src/shared/embedding/index.d.ts +7 -0
- package/dist/src/shared/embedding/typings/semantic-contradiction.model.d.ts +21 -0
- package/dist/src/shared/embedding/typings/stale-vectors-report.model.d.ts +16 -0
- package/dist/src/shared/mcp/classes/mcp-server.class.d.ts +22 -2
- package/dist/src/shared/mcp/functions/create-mcp-server.function.d.ts +6 -1
- package/dist/src/shared/mcp/functions/has-sampling-capability.function.d.ts +8 -0
- package/dist/src/shared/mcp/functions/run-auto-capture.function.d.ts +26 -0
- package/dist/src/shared/mcp/typings/mcp-client.interface.d.ts +2 -0
- package/dist/src/shared/mcp/typings/mcp-integrity-check-result.model.d.ts +24 -0
- package/dist/src/shared/mcp/typings/mcp-worker-client.interface.d.ts +3 -1
- package/dist/src/shared/version/constants/version.constant.d.ts +1 -1
- package/dist/src/shared/wasm-codec/typings/paged-search-result.model.d.ts +2 -0
- package/dist/src/shared/worker-client/classes/worker-client.class.d.ts +31 -1
- package/dist/src/shared/worker-client/constants/request-timeout.constant.d.ts +2 -0
- package/dist/src/shared/worker-client/constants/retry-defaults.constant.d.ts +2 -0
- package/dist/src/shared/worker-client/functions/get-or-create-client.function.d.ts +20 -0
- package/dist/src/shared/worker-client/functions/retry-on-timeout.function.d.ts +17 -0
- package/dist/src/shared/worker-client/index.d.ts +4 -1
- package/dist/src/shared/worker-client/typings/import-result.model.d.ts +16 -0
- package/dist/src/shared/worker-client/typings/integrity-check-result.model.d.ts +24 -0
- package/dist/src/shared/worker-client/typings/search-params.model.d.ts +4 -0
- package/dist/src/shared/worker-client/typings/search-result.model.d.ts +6 -0
- package/dist/src/shared/worker-client/typings/worker-client-options.interface.d.ts +7 -0
- package/dist/src/shared/worker-daemon/classes/worker-daemon.class.d.ts +5 -0
- package/dist/src/shared/worker-daemon/functions/create-ipc-server.function.d.ts +11 -11
- package/dist/src/shared/worker-daemon/functions/embed-after-learn.function.d.ts +13 -0
- package/dist/src/shared/worker-daemon/functions/handle-hybrid-search.function.d.ts +44 -0
- package/dist/src/shared/worker-daemon/functions/handle-import.function.d.ts +14 -0
- package/dist/src/shared/worker-daemon/functions/handle-read-ops.function.d.ts +7 -1
- package/dist/src/shared/worker-daemon/functions/handle-write-ops.function.d.ts +9 -4
- package/dist/src/shared/worker-daemon/typings/handler-context.interface.d.ts +3 -0
- package/dist/wasm/fs-database/kbdb_fs_database.d.ts +16 -85
- package/dist/wasm/fs-database/kbdb_fs_database.js +16 -85
- package/dist/wasm/fs-database/kbdb_fs_database_bg.wasm +0 -0
- package/dist/wasm/kb-worker/kbdb_worker.d.ts +20 -115
- package/dist/wasm/kb-worker/kbdb_worker.js +20 -115
- package/dist/wasm/kb-worker/kbdb_worker_bg.wasm +0 -0
- package/dist/wasm/query-parser/kbdb_query_parser_bg.wasm +0 -0
- package/dist/worker.d.ts +8 -0
- package/dist/worker.mjs +606 -102
- package/dist/worker.mjs.map +4 -4
- package/docs/details/README.md +4 -0
- package/docs/details/agent-tooling.md +122 -0
- package/docs/details/cli.md +78 -12
- package/docs/details/knowledge-base.md +173 -0
- package/docs/details/library-api.md +42 -2
- package/docs/details/mcp-server.md +39 -8
- package/docs/details/search-and-ranking.md +56 -0
- package/docs/goals/cli.md +110 -34
- package/docs/goals/mcp.md +22 -19
- package/docs/goals/query-parser.md +8 -0
- package/docs/goals/worker-client.md +62 -1
- package/docs/goals/worker-daemon.md +52 -10
- package/docs/modules/typescript/cli.md +1 -1
- package/docs/modules/typescript/shared/cli/functions.md +116 -13
- package/docs/modules/typescript/shared/cli/module.md +33 -20
- package/docs/modules/typescript/shared/cli/typings.md +4 -0
- package/docs/modules/typescript/shared/embedding/functions.md +104 -25
- package/docs/modules/typescript/shared/mcp/module.md +1 -1
- package/docs/modules/typescript/shared/mcp/typings.md +8 -1
- package/docs/modules/typescript/shared/worker-client/module.md +9 -1
- package/docs/modules/typescript/shared/worker-client/typings.md +3 -0
- package/docs/modules/typescript/shared/worker-daemon/module.md +6 -3
- package/docs/overview.md +43 -22
- package/docs/release-notes/0.3.1.md +10 -0
- package/docs/release-notes/0.3.2.md +64 -0
- package/docs/release-notes/0.4.0.md +80 -0
- package/docs/release-notes/README.md +2 -0
- package/mod.ts +33 -15
- package/package.json +11 -10
- package/dist/chunk-JG2UCKLI.mjs +0 -95
- package/dist/chunk-JG2UCKLI.mjs.map +0 -7
- package/src/cli.ts +0 -71
- package/src/shared/auto-capture/classes/auto-capture-controller.class.ts +0 -62
- package/src/shared/auto-capture/constants/auto-capture-defaults.constant.ts +0 -15
- package/src/shared/auto-capture/functions/build-capture-prompt.function.ts +0 -27
- package/src/shared/auto-capture/functions/evaluate-trigger.function.ts +0 -50
- package/src/shared/auto-capture/index.ts +0 -7
- package/src/shared/auto-capture/typings/auto-capture-config.interface.ts +0 -13
- package/src/shared/auto-capture/typings/auto-capture-state.interface.ts +0 -13
- package/src/shared/auto-capture/typings/auto-capture-trigger.interface.ts +0 -7
- package/src/shared/cli/constants/defaults.constant.ts +0 -19
- package/src/shared/cli/constants/global-help.constant.ts +0 -73
- package/src/shared/cli/constants/help-text.constant.ts +0 -4
- package/src/shared/cli/constants/leaf-help.constant.ts +0 -219
- package/src/shared/cli/constants/memory-protocol-body.constant.ts +0 -39
- package/src/shared/cli/constants/query-guidance-body.constant.ts +0 -37
- package/src/shared/cli/constants/subcommand-help.constant.ts +0 -195
- package/src/shared/cli/functions/collect-files.function.ts +0 -55
- package/src/shared/cli/functions/compute-section-kbid.function.ts +0 -49
- package/src/shared/cli/functions/dispatch-command.function.ts +0 -390
- package/src/shared/cli/functions/ensure-db.function.ts +0 -21
- package/src/shared/cli/functions/error-to-message.function.ts +0 -9
- package/src/shared/cli/functions/find-db.function.ts +0 -20
- package/src/shared/cli/functions/format-mcp-output.function.ts +0 -41
- package/src/shared/cli/functions/format-output.function.ts +0 -95
- package/src/shared/cli/functions/format-text-output.function.ts +0 -42
- package/src/shared/cli/functions/generate-group-help.function.ts +0 -41
- package/src/shared/cli/functions/generate-help.function.ts +0 -654
- package/src/shared/cli/functions/handle-data-command.function.ts +0 -67
- package/src/shared/cli/functions/handle-mcp.function.ts +0 -56
- package/src/shared/cli/functions/handle-migrate.function.ts +0 -64
- package/src/shared/cli/functions/hydrate-search-previews.function.ts +0 -29
- package/src/shared/cli/functions/install-default-skills.function.ts +0 -296
- package/src/shared/cli/functions/is-stdin-tty.function.ts +0 -12
- package/src/shared/cli/functions/parse-args.function.ts +0 -156
- package/src/shared/cli/functions/prompt-user.function.ts +0 -36
- package/src/shared/cli/functions/resolve-skill-names.function.ts +0 -43
- package/src/shared/cli/functions/run-agent-create.function.ts +0 -114
- package/src/shared/cli/functions/run-agent-delete.function.ts +0 -47
- package/src/shared/cli/functions/run-agent-get.function.ts +0 -166
- package/src/shared/cli/functions/run-agent-list.function.ts +0 -58
- package/src/shared/cli/functions/run-check.function.ts +0 -159
- package/src/shared/cli/functions/run-cli.function.ts +0 -299
- package/src/shared/cli/functions/run-content.function.ts +0 -47
- package/src/shared/cli/functions/run-db-init.function.ts +0 -44
- package/src/shared/cli/functions/run-db-migrate.function.ts +0 -40
- package/src/shared/cli/functions/run-export.function.ts +0 -40
- package/src/shared/cli/functions/run-gc.function.ts +0 -51
- package/src/shared/cli/functions/run-init.function.ts +0 -93
- package/src/shared/cli/functions/run-learn.function.ts +0 -223
- package/src/shared/cli/functions/run-migrate.function.ts +0 -85
- package/src/shared/cli/functions/run-rebuild.function.ts +0 -47
- package/src/shared/cli/functions/run-recall.function.ts +0 -66
- package/src/shared/cli/functions/run-search.function.ts +0 -133
- package/src/shared/cli/functions/run-skill-delete.function.ts +0 -47
- package/src/shared/cli/functions/run-skill-get.function.ts +0 -90
- package/src/shared/cli/functions/run-skill-learn.function.ts +0 -70
- package/src/shared/cli/functions/run-skill-list.function.ts +0 -74
- package/src/shared/cli/functions/run-status.function.ts +0 -29
- package/src/shared/cli/functions/run-unlearn.function.ts +0 -58
- package/src/shared/cli/functions/version-gate.function.ts +0 -44
- package/src/shared/cli/functions/wrap-command.function.ts +0 -34
- package/src/shared/cli/index.ts +0 -55
- package/src/shared/cli/typings/agent-create-client.interface.ts +0 -47
- package/src/shared/cli/typings/agent-get-client.interface.ts +0 -39
- package/src/shared/cli/typings/check-client.interface.ts +0 -66
- package/src/shared/cli/typings/cli-deps.interface.ts +0 -54
- package/src/shared/cli/typings/cli-options.interface.ts +0 -72
- package/src/shared/cli/typings/command-result.model.ts +0 -15
- package/src/shared/cli/typings/disconnectable-client.interface.ts +0 -29
- package/src/shared/cli/typings/dispatch-client.type.ts +0 -46
- package/src/shared/cli/typings/learn-client.interface.ts +0 -53
- package/src/shared/cli/typings/learn-io.interface.ts +0 -5
- package/src/shared/cli/typings/learn-result.model.ts +0 -27
- package/src/shared/cli/typings/search-client.interface.ts +0 -73
- package/src/shared/cli/typings/search-display.model.ts +0 -54
- package/src/shared/dir-resolver/functions/join-path.function.ts +0 -13
- package/src/shared/dir-resolver/functions/resolve-base-dir.function.ts +0 -25
- package/src/shared/dir-resolver/functions/resolve-wasm-dir.function.ts +0 -5
- package/src/shared/dir-resolver/functions/resolve-worker-script.function.ts +0 -13
- package/src/shared/dir-resolver/index.ts +0 -11
- package/src/shared/dir-resolver/typings/base-dir.interface.ts +0 -4
- package/src/shared/embedding/classes/null-embedding-provider.class.ts +0 -29
- package/src/shared/embedding/constants/near-duplicate-thresholds.constant.ts +0 -17
- package/src/shared/embedding/functions/build-vector-index.function.ts +0 -79
- package/src/shared/embedding/functions/cosine-similarity.function.ts +0 -31
- package/src/shared/embedding/functions/detect-near-duplicates.function.ts +0 -69
- package/src/shared/embedding/functions/jaccard-coefficient.function.ts +0 -26
- package/src/shared/embedding/functions/read-vector-index-meta.function.ts +0 -44
- package/src/shared/embedding/functions/rrf-fuse.function.ts +0 -33
- package/src/shared/embedding/functions/search-vectors.function.ts +0 -59
- package/src/shared/embedding/index.ts +0 -20
- package/src/shared/embedding/typings/embedding-config.interface.ts +0 -23
- package/src/shared/embedding/typings/embedding-provider.interface.ts +0 -25
- package/src/shared/embedding/typings/near-duplicate-candidate.model.ts +0 -25
- package/src/shared/embedding/typings/near-duplicate-result.model.ts +0 -26
- package/src/shared/embedding/typings/vector-index-entry.model.ts +0 -13
- package/src/shared/embedding/typings/vector-index-meta.model.ts +0 -14
- package/src/shared/embedding/typings/vector-search-result.model.ts +0 -8
- package/src/shared/hash/functions/compute-sha256.ts +0 -17
- package/src/shared/hash/index.ts +0 -1
- package/src/shared/mcp/classes/mcp-server.class.ts +0 -480
- package/src/shared/mcp/constants/mcp-error-codes.constant.ts +0 -30
- package/src/shared/mcp/constants/tool-agent-create.constant.ts +0 -39
- package/src/shared/mcp/constants/tool-agent-delete.constant.ts +0 -26
- package/src/shared/mcp/constants/tool-agent-get.constant.ts +0 -24
- package/src/shared/mcp/constants/tool-agent-list.constant.ts +0 -18
- package/src/shared/mcp/constants/tool-auto-capture-review.constant.ts +0 -32
- package/src/shared/mcp/constants/tool-check.constant.ts +0 -21
- package/src/shared/mcp/constants/tool-content.constant.ts +0 -29
- package/src/shared/mcp/constants/tool-export.constant.ts +0 -27
- package/src/shared/mcp/constants/tool-gc.constant.ts +0 -21
- package/src/shared/mcp/constants/tool-learn.constant.ts +0 -58
- package/src/shared/mcp/constants/tool-rebuild.constant.ts +0 -21
- package/src/shared/mcp/constants/tool-recall.constant.ts +0 -36
- package/src/shared/mcp/constants/tool-retrieve.constant.ts +0 -31
- package/src/shared/mcp/constants/tool-search.constant.ts +0 -51
- package/src/shared/mcp/constants/tool-skill-delete.constant.ts +0 -24
- package/src/shared/mcp/constants/tool-skill-get.constant.ts +0 -27
- package/src/shared/mcp/constants/tool-skill-learn.constant.ts +0 -37
- package/src/shared/mcp/constants/tool-skill-list.constant.ts +0 -18
- package/src/shared/mcp/constants/tool-status.constant.ts +0 -20
- package/src/shared/mcp/constants/tool-unlearn.constant.ts +0 -25
- package/src/shared/mcp/functions/as-boolean-or-undefined.function.ts +0 -11
- package/src/shared/mcp/functions/as-string-array-or-undefined.function.ts +0 -17
- package/src/shared/mcp/functions/as-string-array.function.ts +0 -15
- package/src/shared/mcp/functions/as-string.function.ts +0 -10
- package/src/shared/mcp/functions/build-skills-text.function.ts +0 -43
- package/src/shared/mcp/functions/create-mcp-server.function.ts +0 -14
- package/src/shared/mcp/functions/create-prompt-definitions.function.ts +0 -102
- package/src/shared/mcp/functions/create-resource-templates.function.ts +0 -23
- package/src/shared/mcp/functions/create-tool-definitions.function.ts +0 -530
- package/src/shared/mcp/functions/extract-skill-arguments.function.ts +0 -27
- package/src/shared/mcp/functions/handle-agent-create.function.ts +0 -116
- package/src/shared/mcp/functions/handle-agent-delete.function.ts +0 -50
- package/src/shared/mcp/functions/handle-agent-get.function.ts +0 -160
- package/src/shared/mcp/functions/handle-agent-list.function.ts +0 -50
- package/src/shared/mcp/functions/handle-agent-prompt.function.ts +0 -68
- package/src/shared/mcp/functions/handle-completion.function.ts +0 -114
- package/src/shared/mcp/functions/handle-mcp-request.function.ts +0 -202
- package/src/shared/mcp/functions/handle-prompt-get.function.ts +0 -220
- package/src/shared/mcp/functions/handle-recall.function.ts +0 -66
- package/src/shared/mcp/functions/handle-resource-read.function.ts +0 -56
- package/src/shared/mcp/functions/handle-roots-list.function.ts +0 -29
- package/src/shared/mcp/functions/handle-skill-prompt.function.ts +0 -44
- package/src/shared/mcp/functions/handle-tool-auto-capture-review.function.ts +0 -24
- package/src/shared/mcp/functions/handle-tool-call.function.ts +0 -560
- package/src/shared/mcp/functions/handle-tool-check.function.ts +0 -21
- package/src/shared/mcp/functions/handle-tool-content.function.ts +0 -23
- package/src/shared/mcp/functions/handle-tool-export.function.ts +0 -25
- package/src/shared/mcp/functions/handle-tool-gc.function.ts +0 -21
- package/src/shared/mcp/functions/handle-tool-learn.function.ts +0 -69
- package/src/shared/mcp/functions/handle-tool-rebuild.function.ts +0 -21
- package/src/shared/mcp/functions/handle-tool-retrieve.function.ts +0 -40
- package/src/shared/mcp/functions/handle-tool-search.function.ts +0 -47
- package/src/shared/mcp/functions/handle-tool-skill-delete.function.ts +0 -34
- package/src/shared/mcp/functions/handle-tool-skill-get.function.ts +0 -59
- package/src/shared/mcp/functions/handle-tool-skill-learn.function.ts +0 -61
- package/src/shared/mcp/functions/handle-tool-skill-list.function.ts +0 -39
- package/src/shared/mcp/functions/handle-tool-status.function.ts +0 -21
- package/src/shared/mcp/functions/handle-tool-unlearn.function.ts +0 -24
- package/src/shared/mcp/functions/parse-agent-meta.function.ts +0 -27
- package/src/shared/mcp/functions/parse-skill-arguments.function.ts +0 -36
- package/src/shared/mcp/functions/parse-skill-content.function.ts +0 -38
- package/src/shared/mcp/functions/read-agent-meta.function.ts +0 -41
- package/src/shared/mcp/functions/resolve-skill-refs.function.ts +0 -50
- package/src/shared/mcp/functions/run-startup-health-check.function.ts +0 -34
- package/src/shared/mcp/functions/serialize-skill-content.function.ts +0 -12
- package/src/shared/mcp/functions/should-emit-log.function.ts +0 -38
- package/src/shared/mcp/index.ts +0 -28
- package/src/shared/mcp/typings/agent-types.interface.ts +0 -39
- package/src/shared/mcp/typings/log-types.interface.ts +0 -26
- package/src/shared/mcp/typings/mcp-capabilities.interface.ts +0 -16
- package/src/shared/mcp/typings/mcp-client.interface.ts +0 -317
- package/src/shared/mcp/typings/mcp-completion.interface.ts +0 -45
- package/src/shared/mcp/typings/mcp-content-result.model.ts +0 -9
- package/src/shared/mcp/typings/mcp-export-result.model.ts +0 -11
- package/src/shared/mcp/typings/mcp-gc-result.model.ts +0 -15
- package/src/shared/mcp/typings/mcp-integrity-check-result.model.ts +0 -17
- package/src/shared/mcp/typings/mcp-integrity-error.model.ts +0 -15
- package/src/shared/mcp/typings/mcp-notification.interface.ts +0 -9
- package/src/shared/mcp/typings/mcp-rebuild-result.model.ts +0 -9
- package/src/shared/mcp/typings/mcp-request.interface.ts +0 -11
- package/src/shared/mcp/typings/mcp-resource-result.interface.ts +0 -12
- package/src/shared/mcp/typings/mcp-resource-template.interface.ts +0 -11
- package/src/shared/mcp/typings/mcp-response.interface.ts +0 -18
- package/src/shared/mcp/typings/mcp-server-info.interface.ts +0 -7
- package/src/shared/mcp/typings/mcp-server-state.interface.ts +0 -17
- package/src/shared/mcp/typings/mcp-tool-annotations.interface.ts +0 -32
- package/src/shared/mcp/typings/mcp-tool-definition.interface.ts +0 -16
- package/src/shared/mcp/typings/mcp-tool-result.interface.ts +0 -19
- package/src/shared/mcp/typings/mcp-types.interface.ts +0 -158
- package/src/shared/mcp/typings/mcp-worker-client.interface.ts +0 -243
- package/src/shared/mcp/typings/prompt-types.interface.ts +0 -39
- package/src/shared/mcp/typings/remove-section-result.model.ts +0 -10
- package/src/shared/mcp/typings/sampling-types.interface.ts +0 -48
- package/src/shared/mcp/typings/skill-types.interface.ts +0 -29
- package/src/shared/mcp/typings/tool-call-result.model.ts +0 -18
- package/src/shared/mod.ts +0 -1
- package/src/shared/platform/functions/get-ipc-path.function.ts +0 -19
- package/src/shared/platform/functions/get-tmpdir.function.ts +0 -8
- package/src/shared/platform/functions/is-process-alive.function.ts +0 -17
- package/src/shared/platform/index.ts +0 -3
- package/src/shared/runtime/functions/detect-runtime.ts +0 -14
- package/src/shared/runtime/functions/get-args.ts +0 -16
- package/src/shared/runtime/index.ts +0 -3
- package/src/shared/runtime/typings/runtime.ts +0 -2
- package/src/shared/sync/functions/build-import-report.function.ts +0 -40
- package/src/shared/sync/functions/parse-export-entry.function.ts +0 -45
- package/src/shared/sync/functions/resolve-conflict.function.ts +0 -31
- package/src/shared/sync/functions/serialize-export-entry.function.ts +0 -18
- package/src/shared/sync/index.ts +0 -9
- package/src/shared/sync/typings/conflict-resolution.type.ts +0 -8
- package/src/shared/sync/typings/export-entry.interface.ts +0 -13
- package/src/shared/sync/typings/export-options.interface.ts +0 -9
- package/src/shared/sync/typings/import-options.interface.ts +0 -7
- package/src/shared/sync/typings/import-report.interface.ts +0 -15
- package/src/shared/version/constants/version.constant.ts +0 -2
- package/src/shared/version/index.ts +0 -1
- package/src/shared/wasm-codec/functions/decode-add-section-result.function.ts +0 -112
- package/src/shared/wasm-codec/functions/decode-compose-output.function.ts +0 -16
- package/src/shared/wasm-codec/functions/decode-document-manifest-list.function.ts +0 -70
- package/src/shared/wasm-codec/functions/decode-document-manifest.function.ts +0 -87
- package/src/shared/wasm-codec/functions/decode-f32.function.ts +0 -16
- package/src/shared/wasm-codec/functions/decode-key-value-text.function.ts +0 -15
- package/src/shared/wasm-codec/functions/decode-one-recall-result.function.ts +0 -86
- package/src/shared/wasm-codec/functions/decode-option-string.function.ts +0 -24
- package/src/shared/wasm-codec/functions/decode-recall-document.function.ts +0 -59
- package/src/shared/wasm-codec/functions/decode-recall-reference.function.ts +0 -52
- package/src/shared/wasm-codec/functions/decode-recall-result.function.ts +0 -267
- package/src/shared/wasm-codec/functions/decode-recall-sibling.function.ts +0 -43
- package/src/shared/wasm-codec/functions/decode-remove-flag.function.ts +0 -3
- package/src/shared/wasm-codec/functions/decode-scored-results.function.ts +0 -194
- package/src/shared/wasm-codec/functions/decode-section-match.function.ts +0 -80
- package/src/shared/wasm-codec/functions/decode-section-record-list.function.ts +0 -20
- package/src/shared/wasm-codec/functions/decode-section-record-trailing.function.ts +0 -75
- package/src/shared/wasm-codec/functions/decode-section-record.function.ts +0 -85
- package/src/shared/wasm-codec/functions/decode-section-records.function.ts +0 -249
- package/src/shared/wasm-codec/functions/decode-section-type.function.ts +0 -30
- package/src/shared/wasm-codec/functions/decode-string.function.ts +0 -22
- package/src/shared/wasm-codec/functions/decode-u32.function.ts +0 -16
- package/src/shared/wasm-codec/functions/decode-u64.function.ts +0 -16
- package/src/shared/wasm-codec/functions/decode-wasm-list.function.ts +0 -31
- package/src/shared/wasm-codec/functions/encode-add-section-params.function.ts +0 -102
- package/src/shared/wasm-codec/functions/encode-group-params.function.ts +0 -28
- package/src/shared/wasm-codec/functions/encode-identifier-list.function.ts +0 -17
- package/src/shared/wasm-codec/functions/encode-kbid-list.function.ts +0 -17
- package/src/shared/wasm-codec/functions/encode-option-string.function.ts +0 -14
- package/src/shared/wasm-codec/functions/encode-recall-params.function.ts +0 -68
- package/src/shared/wasm-codec/functions/encode-search-params.function.ts +0 -12
- package/src/shared/wasm-codec/functions/encode-section-type.function.ts +0 -18
- package/src/shared/wasm-codec/functions/encode-string.function.ts +0 -9
- package/src/shared/wasm-codec/functions/encode-type-filter-param.function.ts +0 -13
- package/src/shared/wasm-codec/functions/encode-u32.function.ts +0 -5
- package/src/shared/wasm-codec/functions/encode-update-section-params.function.ts +0 -16
- package/src/shared/wasm-codec/index.ts +0 -49
- package/src/shared/wasm-codec/typings/add-section-input.model.ts +0 -45
- package/src/shared/wasm-codec/typings/add-section-result.model.ts +0 -28
- package/src/shared/wasm-codec/typings/document-manifest.model.ts +0 -20
- package/src/shared/wasm-codec/typings/document-section.model.ts +0 -12
- package/src/shared/wasm-codec/typings/item-decoder.type.ts +0 -10
- package/src/shared/wasm-codec/typings/near-duplicate.model.ts +0 -16
- package/src/shared/wasm-codec/typings/paged-search-result.model.ts +0 -22
- package/src/shared/wasm-codec/typings/section-match.model.ts +0 -31
- package/src/shared/wasm-codec/typings/section-record-trailing.model.ts +0 -38
- package/src/shared/wasm-codec/typings/section-record.model.ts +0 -66
- package/src/shared/wasm-codec/typings/section-type-result.model.ts +0 -12
- package/src/shared/worker-client/classes/worker-client.class.ts +0 -466
- package/src/shared/worker-client/functions/compute-context-id.function.ts +0 -18
- package/src/shared/worker-client/functions/create-worker-client.function.ts +0 -70
- package/src/shared/worker-client/functions/discover-daemon.function.ts +0 -47
- package/src/shared/worker-client/functions/handle-ipc-data.function.ts +0 -49
- package/src/shared/worker-client/functions/resolve-db-path.function.ts +0 -21
- package/src/shared/worker-client/functions/send-ipc-request.function.ts +0 -39
- package/src/shared/worker-client/functions/spawn-daemon.function.ts +0 -35
- package/src/shared/worker-client/functions/wait-for-daemon.function.ts +0 -34
- package/src/shared/worker-client/index.ts +0 -44
- package/src/shared/worker-client/typings/add-section-result.model.ts +0 -39
- package/src/shared/worker-client/typings/content-result.model.ts +0 -12
- package/src/shared/worker-client/typings/daemon-info.model.ts +0 -7
- package/src/shared/worker-client/typings/export-result.model.ts +0 -14
- package/src/shared/worker-client/typings/gc-result.model.ts +0 -18
- package/src/shared/worker-client/typings/integrity-check-result.model.ts +0 -43
- package/src/shared/worker-client/typings/json-rpc-response.interface.ts +0 -17
- package/src/shared/worker-client/typings/migrate-result.model.ts +0 -11
- package/src/shared/worker-client/typings/pending-call.interface.ts +0 -7
- package/src/shared/worker-client/typings/rebuild-result.model.ts +0 -12
- package/src/shared/worker-client/typings/recall-document.model.ts +0 -16
- package/src/shared/worker-client/typings/recall-params.model.ts +0 -15
- package/src/shared/worker-client/typings/recall-reference.model.ts +0 -32
- package/src/shared/worker-client/typings/recall-result.model.ts +0 -98
- package/src/shared/worker-client/typings/recall-sibling.model.ts +0 -11
- package/src/shared/worker-client/typings/recall-single-result.model.ts +0 -41
- package/src/shared/worker-client/typings/remove-section-result.model.ts +0 -7
- package/src/shared/worker-client/typings/search-params.model.ts +0 -14
- package/src/shared/worker-client/typings/search-result.model.ts +0 -43
- package/src/shared/worker-client/typings/section.model.ts +0 -43
- package/src/shared/worker-client/typings/status-result.model.ts +0 -14
- package/src/shared/worker-client/typings/version-status.model.ts +0 -28
- package/src/shared/worker-client/typings/worker-client-error.interface.ts +0 -8
- package/src/shared/worker-client/typings/worker-client-options.interface.ts +0 -30
- package/src/shared/worker-daemon/classes/ipc-error.class.ts +0 -22
- package/src/shared/worker-daemon/classes/worker-daemon.class.ts +0 -1291
- package/src/shared/worker-daemon/constants/daemon-defaults.constant.ts +0 -29
- package/src/shared/worker-daemon/constants/wasm-method-map.constant.ts +0 -69
- package/src/shared/worker-daemon/functions/build-ipc-error-response.function.ts +0 -16
- package/src/shared/worker-daemon/functions/check-catalog-version.function.ts +0 -54
- package/src/shared/worker-daemon/functions/check-preconditions.function.ts +0 -28
- package/src/shared/worker-daemon/functions/create-host-imports.function.ts +0 -328
- package/src/shared/worker-daemon/functions/create-ipc-server.function.ts +0 -87
- package/src/shared/worker-daemon/functions/detect-stale-pid.function.ts +0 -33
- package/src/shared/worker-daemon/functions/dispatch-request.function.ts +0 -190
- package/src/shared/worker-daemon/functions/handle-check-version.function.ts +0 -74
- package/src/shared/worker-daemon/functions/handle-config-method.function.ts +0 -27
- package/src/shared/worker-daemon/functions/handle-read-ops.function.ts +0 -236
- package/src/shared/worker-daemon/functions/handle-write-ops.function.ts +0 -257
- package/src/shared/worker-daemon/functions/load-wasm-modules.function.ts +0 -95
- package/src/shared/worker-daemon/functions/parse-auto-capture-section.function.ts +0 -51
- package/src/shared/worker-daemon/functions/parse-catalog-version.function.ts +0 -20
- package/src/shared/worker-daemon/functions/parse-embedding-section.function.ts +0 -38
- package/src/shared/worker-daemon/functions/parse-worker-toml.function.ts +0 -104
- package/src/shared/worker-daemon/functions/read-config.function.ts +0 -83
- package/src/shared/worker-daemon/functions/run-startup-integrity-check.function.ts +0 -55
- package/src/shared/worker-daemon/functions/run-worker.function.ts +0 -57
- package/src/shared/worker-daemon/functions/scan-near-duplicates.function.ts +0 -92
- package/src/shared/worker-daemon/functions/start-daemon.function.ts +0 -74
- package/src/shared/worker-daemon/functions/wasm-bridge.function.ts +0 -144
- package/src/shared/worker-daemon/functions/wasm-memory-helpers.function.ts +0 -41
- package/src/shared/worker-daemon/functions/write-lock.function.ts +0 -49
- package/src/shared/worker-daemon/index.ts +0 -16
- package/src/shared/worker-daemon/typings/allocator-ref.interface.ts +0 -16
- package/src/shared/worker-daemon/typings/daemon-config.interface.ts +0 -20
- package/src/shared/worker-daemon/typings/fs-adapter.interface.ts +0 -22
- package/src/shared/worker-daemon/typings/handler-context.interface.ts +0 -19
- package/src/shared/worker-daemon/typings/ipc-handler.interface.ts +0 -39
- package/src/shared/worker-daemon/typings/memory-ref.interface.ts +0 -13
- package/src/shared/worker-daemon/typings/wasm-fn.type.ts +0 -2
- package/src/shared/worker-daemon/typings/wasm-modules.interface.ts +0 -7
- package/src/shared/worker-daemon/typings/write-lock.interface.ts +0 -13
- package/src/wasm/fs-database/kbdb_fs_database.d.ts +0 -259
- package/src/wasm/fs-database/kbdb_fs_database.js +0 -515
- package/src/wasm/fs-database/kbdb_fs_database_bg.wasm +0 -0
- package/src/wasm/fs-database/kbdb_fs_database_bg.wasm.d.ts +0 -35
- package/src/wasm/fs-database/package.json +0 -13
- package/src/wasm/kb-worker/kbdb_worker.d.ts +0 -650
- package/src/wasm/kb-worker/kbdb_worker.js +0 -1181
- package/src/wasm/kb-worker/kbdb_worker_bg.wasm +0 -0
- package/src/wasm/kb-worker/kbdb_worker_bg.wasm.d.ts +0 -77
- package/src/wasm/kb-worker/package.json +0 -13
- package/src/wasm/query-parser/kbdb_query_parser.d.ts +0 -195
- package/src/wasm/query-parser/kbdb_query_parser.js +0 -403
- package/src/wasm/query-parser/kbdb_query_parser_bg.wasm +0 -0
- package/src/wasm/query-parser/kbdb_query_parser_bg.wasm.d.ts +0 -25
- package/src/wasm/query-parser/package.json +0 -13
- package/src/worker.ts +0 -42
package/dist/cli.mjs
CHANGED
|
@@ -3,9 +3,14 @@ import {
|
|
|
3
3
|
detectRuntime,
|
|
4
4
|
getArgs,
|
|
5
5
|
getIpcPath,
|
|
6
|
+
parseWorkerToml,
|
|
6
7
|
resolveBaseDir,
|
|
7
8
|
resolveWorkerScript
|
|
8
|
-
} from "./chunk-
|
|
9
|
+
} from "./chunk-BJPSPNGX.mjs";
|
|
10
|
+
|
|
11
|
+
// src/cli.ts
|
|
12
|
+
import { readFileSync as readFileSync4 } from "node:fs";
|
|
13
|
+
import { join as join9 } from "node:path";
|
|
9
14
|
|
|
10
15
|
// src/shared/cli/constants/defaults.constant.ts
|
|
11
16
|
var DEFAULT_LIMIT = 20;
|
|
@@ -62,6 +67,7 @@ function parseArgs(argv) {
|
|
|
62
67
|
case "--format":
|
|
63
68
|
case "-f":
|
|
64
69
|
options.format = argv[++i] ?? DEFAULT_FORMAT;
|
|
70
|
+
options.formatExplicit = true;
|
|
65
71
|
break;
|
|
66
72
|
case "--mode":
|
|
67
73
|
case "-m":
|
|
@@ -119,10 +125,28 @@ function parseArgs(argv) {
|
|
|
119
125
|
case "-c":
|
|
120
126
|
options.content = true;
|
|
121
127
|
break;
|
|
128
|
+
case "--relaxed":
|
|
129
|
+
options.relaxed = true;
|
|
130
|
+
break;
|
|
131
|
+
case "--from":
|
|
132
|
+
options.from = argv[++i];
|
|
133
|
+
break;
|
|
134
|
+
case "--dry-run":
|
|
135
|
+
options.dryRun = true;
|
|
136
|
+
break;
|
|
137
|
+
case "--export-format":
|
|
138
|
+
options.exportFormat = argv[++i] ?? "git";
|
|
139
|
+
break;
|
|
140
|
+
case "--timeout-ms":
|
|
141
|
+
options.timeoutMs = parseInt(argv[++i] ?? "", 10) || void 0;
|
|
142
|
+
break;
|
|
143
|
+
case "--retry":
|
|
144
|
+
options.retry = parseInt(argv[++i] ?? "", 10) || void 0;
|
|
145
|
+
break;
|
|
122
146
|
default:
|
|
123
147
|
if (!options.command) {
|
|
124
148
|
options.command = arg;
|
|
125
|
-
} else if (!options.args.length && (options.command === "db" || options.command === "skill" || options.command === "agent")) {
|
|
149
|
+
} else if (!options.args.length && (options.command === "db" || options.command === "skill" || options.command === "agent" || options.command === "worker")) {
|
|
126
150
|
options.command = `${options.command}-${arg}`;
|
|
127
151
|
} else {
|
|
128
152
|
options.args.push(arg);
|
|
@@ -211,7 +235,8 @@ async function runSearch(client, options) {
|
|
|
211
235
|
mode: options.mode,
|
|
212
236
|
limit: options.limit,
|
|
213
237
|
offset: options.offset,
|
|
214
|
-
algo: options.algo
|
|
238
|
+
algo: options.algo,
|
|
239
|
+
relaxed: options.relaxed
|
|
215
240
|
});
|
|
216
241
|
return {
|
|
217
242
|
items: results.items.map((r) => ({
|
|
@@ -231,10 +256,35 @@ async function runSearch(client, options) {
|
|
|
231
256
|
offset: results.offset,
|
|
232
257
|
limit: results.limit,
|
|
233
258
|
has_more: results.has_more,
|
|
234
|
-
relaxed: results.relaxed ?? false
|
|
259
|
+
relaxed: results.relaxed ?? false,
|
|
260
|
+
elapsed_ms: results.elapsed_ms,
|
|
261
|
+
search_mode: results.search_mode,
|
|
262
|
+
retrieval_method: results.retrieval_method
|
|
235
263
|
};
|
|
236
264
|
}
|
|
237
265
|
|
|
266
|
+
// src/shared/cli/functions/resolve-kbid-prefix.function.ts
|
|
267
|
+
var SECTION_TYPES = ["text", "code", "image"];
|
|
268
|
+
async function resolveKbidPrefix(prefix, client) {
|
|
269
|
+
if (prefix.length >= 26) return prefix;
|
|
270
|
+
const seen = /* @__PURE__ */ new Set();
|
|
271
|
+
for (const type of SECTION_TYPES) {
|
|
272
|
+
const records = await client.listByType(type);
|
|
273
|
+
for (const r of records) {
|
|
274
|
+
if (r.kbid.startsWith(prefix)) seen.add(r.kbid);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
if (seen.size === 0) {
|
|
278
|
+
throw new Error(`no section matches prefix "${prefix}"`);
|
|
279
|
+
}
|
|
280
|
+
if (seen.size > 1) {
|
|
281
|
+
throw new Error(
|
|
282
|
+
`ambiguous prefix "${prefix}": ${seen.size.toString()} matches`
|
|
283
|
+
);
|
|
284
|
+
}
|
|
285
|
+
return [...seen][0];
|
|
286
|
+
}
|
|
287
|
+
|
|
238
288
|
// src/shared/cli/functions/run-recall.function.ts
|
|
239
289
|
var MAX_RECALL_DEPTH = 3;
|
|
240
290
|
async function runRecall(client, options) {
|
|
@@ -245,10 +295,13 @@ async function runRecall(client, options) {
|
|
|
245
295
|
MAX_RECALL_DEPTH,
|
|
246
296
|
Math.max(0, options.depth ?? 0)
|
|
247
297
|
);
|
|
248
|
-
|
|
249
|
-
|
|
298
|
+
const resolved = await Promise.all(
|
|
299
|
+
options.args.map((a) => resolveKbidPrefix(a, client))
|
|
300
|
+
);
|
|
301
|
+
if (resolved.length === 1) {
|
|
302
|
+
return client.recall({ kbid: resolved[0], depth });
|
|
250
303
|
}
|
|
251
|
-
return client.recall({ kbids:
|
|
304
|
+
return client.recall({ kbids: resolved, depth });
|
|
252
305
|
}
|
|
253
306
|
|
|
254
307
|
// src/shared/cli/functions/run-export.function.ts
|
|
@@ -363,7 +416,8 @@ function isSupportedFile(path) {
|
|
|
363
416
|
// src/shared/cli/functions/run-unlearn.function.ts
|
|
364
417
|
async function runUnlearn(client, options) {
|
|
365
418
|
const results = [];
|
|
366
|
-
for (const
|
|
419
|
+
for (const arg of options.args) {
|
|
420
|
+
const kbid = await resolveKbidPrefix(arg, client);
|
|
367
421
|
const result = await client.removeSection(kbid);
|
|
368
422
|
results.push({ kbid, removed: result.removed });
|
|
369
423
|
}
|
|
@@ -372,7 +426,10 @@ async function runUnlearn(client, options) {
|
|
|
372
426
|
|
|
373
427
|
// src/shared/cli/functions/run-content.function.ts
|
|
374
428
|
async function runContent(client, options) {
|
|
375
|
-
|
|
429
|
+
const resolved = await Promise.all(
|
|
430
|
+
options.args.map((a) => resolveKbidPrefix(a, client))
|
|
431
|
+
);
|
|
432
|
+
return client.content(resolved);
|
|
376
433
|
}
|
|
377
434
|
|
|
378
435
|
// src/shared/cli/functions/run-check.function.ts
|
|
@@ -410,11 +467,22 @@ async function runRebuild(client, _options) {
|
|
|
410
467
|
|
|
411
468
|
// src/shared/cli/functions/find-db.function.ts
|
|
412
469
|
import { existsSync } from "node:fs";
|
|
413
|
-
import { join as join2, resolve } from "node:path";
|
|
470
|
+
import { dirname, join as join2, resolve } from "node:path";
|
|
414
471
|
function findDb(dbPath) {
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
472
|
+
if (dbPath) {
|
|
473
|
+
const target = join2(resolve(dbPath), ".kbdb");
|
|
474
|
+
return existsSync(target) ? target : null;
|
|
475
|
+
}
|
|
476
|
+
let current = process.cwd();
|
|
477
|
+
let parent = dirname(current);
|
|
478
|
+
while (parent !== current) {
|
|
479
|
+
const candidate2 = join2(current, ".kbdb");
|
|
480
|
+
if (existsSync(candidate2)) return candidate2;
|
|
481
|
+
current = parent;
|
|
482
|
+
parent = dirname(current);
|
|
483
|
+
}
|
|
484
|
+
const candidate = join2(current, ".kbdb");
|
|
485
|
+
return existsSync(candidate) ? candidate : null;
|
|
418
486
|
}
|
|
419
487
|
|
|
420
488
|
// src/shared/cli/functions/run-init.function.ts
|
|
@@ -909,6 +977,18 @@ async function runAgentDelete(client, options) {
|
|
|
909
977
|
};
|
|
910
978
|
}
|
|
911
979
|
|
|
980
|
+
// src/shared/cli/functions/run-import.function.ts
|
|
981
|
+
async function runImport(client, options) {
|
|
982
|
+
const from = options.from;
|
|
983
|
+
if (!from) {
|
|
984
|
+
throw new Error("import requires --from <path>");
|
|
985
|
+
}
|
|
986
|
+
return client.import({
|
|
987
|
+
from,
|
|
988
|
+
dryRun: options.dryRun === true
|
|
989
|
+
});
|
|
990
|
+
}
|
|
991
|
+
|
|
912
992
|
// src/shared/cli/functions/dispatch-command.function.ts
|
|
913
993
|
async function dispatchCommand(client, options, io) {
|
|
914
994
|
switch (options.command) {
|
|
@@ -932,6 +1012,8 @@ async function dispatchCommand(client, options, io) {
|
|
|
932
1012
|
return handleStatus(client, options);
|
|
933
1013
|
case "export":
|
|
934
1014
|
return handleExport(client, options);
|
|
1015
|
+
case "import":
|
|
1016
|
+
return handleImport(client, options);
|
|
935
1017
|
case "db-init":
|
|
936
1018
|
return handleDbInit(options);
|
|
937
1019
|
case "db-migrate":
|
|
@@ -1054,6 +1136,20 @@ async function handleExport(client, options) {
|
|
|
1054
1136
|
};
|
|
1055
1137
|
}
|
|
1056
1138
|
}
|
|
1139
|
+
async function handleImport(client, options) {
|
|
1140
|
+
try {
|
|
1141
|
+
const result = await runImport(client, options);
|
|
1142
|
+
return {
|
|
1143
|
+
output: JSON.stringify(result, null, 2),
|
|
1144
|
+
exitCode: 0
|
|
1145
|
+
};
|
|
1146
|
+
} catch (err) {
|
|
1147
|
+
return {
|
|
1148
|
+
error: `import failed: ${errorToMessage(err)}`,
|
|
1149
|
+
exitCode: 1
|
|
1150
|
+
};
|
|
1151
|
+
}
|
|
1152
|
+
}
|
|
1057
1153
|
async function handleContent(client, options) {
|
|
1058
1154
|
try {
|
|
1059
1155
|
const result = await runContent(client, options);
|
|
@@ -1154,7 +1250,7 @@ async function hydrateSearchPreviews(client, paged) {
|
|
|
1154
1250
|
}
|
|
1155
1251
|
|
|
1156
1252
|
// src/shared/version/constants/version.constant.ts
|
|
1157
|
-
var VERSION = "0.
|
|
1253
|
+
var VERSION = "0.4.0";
|
|
1158
1254
|
|
|
1159
1255
|
// src/shared/cli/functions/generate-help.function.ts
|
|
1160
1256
|
var KNOWN_COMMANDS = /* @__PURE__ */ new Set([
|
|
@@ -1171,7 +1267,8 @@ var KNOWN_COMMANDS = /* @__PURE__ */ new Set([
|
|
|
1171
1267
|
"mcp",
|
|
1172
1268
|
"db",
|
|
1173
1269
|
"skill",
|
|
1174
|
-
"agent"
|
|
1270
|
+
"agent",
|
|
1271
|
+
"worker"
|
|
1175
1272
|
]);
|
|
1176
1273
|
var GROUP_SUBCOMMANDS = {
|
|
1177
1274
|
db: [
|
|
@@ -1189,6 +1286,10 @@ var GROUP_SUBCOMMANDS = {
|
|
|
1189
1286
|
["list", "List all agents"],
|
|
1190
1287
|
["get", "Retrieve an agent by docid"],
|
|
1191
1288
|
["delete", "Remove an agent"]
|
|
1289
|
+
],
|
|
1290
|
+
worker: [
|
|
1291
|
+
["stop", "Stop the worker daemon"],
|
|
1292
|
+
["restart", "Restart the worker daemon"]
|
|
1192
1293
|
]
|
|
1193
1294
|
};
|
|
1194
1295
|
function isKnownCommand(cmd) {
|
|
@@ -1257,6 +1358,10 @@ Agents:
|
|
|
1257
1358
|
agent get Retrieve an agent by docid
|
|
1258
1359
|
agent delete Remove an agent
|
|
1259
1360
|
|
|
1361
|
+
Worker:
|
|
1362
|
+
worker stop Stop the worker daemon
|
|
1363
|
+
worker restart Restart the worker daemon
|
|
1364
|
+
|
|
1260
1365
|
Server:
|
|
1261
1366
|
mcp Start MCP server mode (stdio)
|
|
1262
1367
|
|
|
@@ -1302,7 +1407,8 @@ Run "kbdb ${group} <subcommand> --help" for details.
|
|
|
1302
1407
|
var GROUP_DESCRIPTIONS = {
|
|
1303
1408
|
db: "Database management",
|
|
1304
1409
|
skill: "Skill management",
|
|
1305
|
-
agent: "Agent management"
|
|
1410
|
+
agent: "Agent management",
|
|
1411
|
+
worker: "Worker daemon management"
|
|
1306
1412
|
};
|
|
1307
1413
|
var LEAF_HELP = {
|
|
1308
1414
|
learn: `kbdb learn - Import content from files or stdin
|
|
@@ -1488,11 +1594,11 @@ Options:
|
|
|
1488
1594
|
--db <path> Path to .kbdb directory
|
|
1489
1595
|
|
|
1490
1596
|
Environment:
|
|
1491
|
-
|
|
1597
|
+
KBDB_DB_DIR Fallback database path
|
|
1492
1598
|
|
|
1493
1599
|
Examples:
|
|
1494
1600
|
kbdb mcp --db /path/to/project
|
|
1495
|
-
|
|
1601
|
+
KBDB_DB_DIR=/path/to/project kbdb mcp
|
|
1496
1602
|
`
|
|
1497
1603
|
};
|
|
1498
1604
|
var SUBCOMMAND_HELP = {
|
|
@@ -1655,6 +1761,28 @@ Output:
|
|
|
1655
1761
|
|
|
1656
1762
|
Examples:
|
|
1657
1763
|
kbdb agent delete agent-code-reviewer
|
|
1764
|
+
`,
|
|
1765
|
+
"worker-stop": `kbdb worker stop - Stop the worker daemon
|
|
1766
|
+
|
|
1767
|
+
Usage: kbdb worker stop [options]
|
|
1768
|
+
|
|
1769
|
+
Options:
|
|
1770
|
+
--db <path> Path to .kbdb directory
|
|
1771
|
+
|
|
1772
|
+
Examples:
|
|
1773
|
+
kbdb worker stop
|
|
1774
|
+
kbdb worker stop --db /path/to/project
|
|
1775
|
+
`,
|
|
1776
|
+
"worker-restart": `kbdb worker restart - Restart the worker daemon
|
|
1777
|
+
|
|
1778
|
+
Usage: kbdb worker restart [options]
|
|
1779
|
+
|
|
1780
|
+
Options:
|
|
1781
|
+
--db <path> Path to .kbdb directory
|
|
1782
|
+
|
|
1783
|
+
Examples:
|
|
1784
|
+
kbdb worker restart
|
|
1785
|
+
kbdb worker restart --db /path/to/project
|
|
1658
1786
|
`
|
|
1659
1787
|
};
|
|
1660
1788
|
function generateSubcommandHelp(command, subcommand) {
|
|
@@ -1666,199 +1794,6 @@ function generateSubcommandHelp(command, subcommand) {
|
|
|
1666
1794
|
return generateCommandHelp(command);
|
|
1667
1795
|
}
|
|
1668
1796
|
|
|
1669
|
-
// src/shared/cli/functions/run-cli.function.ts
|
|
1670
|
-
async function runCli(argv, deps2) {
|
|
1671
|
-
const options = parseArgs(argv);
|
|
1672
|
-
if (options.command === "help" || !options.command) {
|
|
1673
|
-
const helpCommand = options.args[0];
|
|
1674
|
-
const helpSubcommand = options.args[1];
|
|
1675
|
-
const helpText = generateHelp(helpCommand, helpSubcommand);
|
|
1676
|
-
if (helpCommand && !isKnownCommand(helpCommand)) {
|
|
1677
|
-
return { output: helpText, exitCode: 1 };
|
|
1678
|
-
}
|
|
1679
|
-
return { output: helpText, exitCode: 0 };
|
|
1680
|
-
}
|
|
1681
|
-
if (options.command === "version") {
|
|
1682
|
-
return { output: VERSION, exitCode: 0 };
|
|
1683
|
-
}
|
|
1684
|
-
if (options.command === "init" || options.command === "db-init") {
|
|
1685
|
-
return handleInit(options, deps2);
|
|
1686
|
-
}
|
|
1687
|
-
if (options.command === "mcp") {
|
|
1688
|
-
return handleMcp(options, deps2);
|
|
1689
|
-
}
|
|
1690
|
-
if (options.command === "migrate" || options.command === "db-migrate") {
|
|
1691
|
-
return handleMigrate(options, deps2);
|
|
1692
|
-
}
|
|
1693
|
-
return handleDataCommand(options, deps2);
|
|
1694
|
-
}
|
|
1695
|
-
function handleInit(options, deps2) {
|
|
1696
|
-
const result = deps2.runInit(options);
|
|
1697
|
-
if (result.ok) {
|
|
1698
|
-
return {
|
|
1699
|
-
output: result.targetPath,
|
|
1700
|
-
exitCode: 0
|
|
1701
|
-
};
|
|
1702
|
-
}
|
|
1703
|
-
return {
|
|
1704
|
-
error: `error: ${result.errorMessage ?? "init failed"}`,
|
|
1705
|
-
exitCode: 1
|
|
1706
|
-
};
|
|
1707
|
-
}
|
|
1708
|
-
async function handleMcp(options, deps2) {
|
|
1709
|
-
const rawDbPath = options.dbPath ?? process.env["KBDB_DIR"];
|
|
1710
|
-
if (!rawDbPath) {
|
|
1711
|
-
return {
|
|
1712
|
-
error: "error: --db or KBDB_DIR required in MCP mode",
|
|
1713
|
-
exitCode: 1
|
|
1714
|
-
};
|
|
1715
|
-
}
|
|
1716
|
-
const dbDir = deps2.findDb(rawDbPath);
|
|
1717
|
-
if (!dbDir) {
|
|
1718
|
-
return {
|
|
1719
|
-
error: `error: no .kbdb database found at ${rawDbPath}`,
|
|
1720
|
-
exitCode: 1
|
|
1721
|
-
};
|
|
1722
|
-
}
|
|
1723
|
-
let client;
|
|
1724
|
-
try {
|
|
1725
|
-
client = await deps2.createWorkerClient({
|
|
1726
|
-
contextPath: dbDir
|
|
1727
|
-
});
|
|
1728
|
-
} catch (err) {
|
|
1729
|
-
return {
|
|
1730
|
-
error: `error: ${errorToMessage(err)}`,
|
|
1731
|
-
exitCode: 1
|
|
1732
|
-
};
|
|
1733
|
-
}
|
|
1734
|
-
try {
|
|
1735
|
-
const gateResult = await versionGate(client, dbDir);
|
|
1736
|
-
if (gateResult) return gateResult;
|
|
1737
|
-
if (deps2.startMcpServer) {
|
|
1738
|
-
await deps2.startMcpServer(dbDir);
|
|
1739
|
-
}
|
|
1740
|
-
return { exitCode: 0 };
|
|
1741
|
-
} catch (err) {
|
|
1742
|
-
return {
|
|
1743
|
-
error: `error: mcp failed: ${errorToMessage(err)}`,
|
|
1744
|
-
exitCode: 1
|
|
1745
|
-
};
|
|
1746
|
-
} finally {
|
|
1747
|
-
client.disconnect();
|
|
1748
|
-
}
|
|
1749
|
-
}
|
|
1750
|
-
async function handleMigrate(options, deps2) {
|
|
1751
|
-
const dbDir = deps2.findDb(options.dbPath);
|
|
1752
|
-
if (!dbDir) {
|
|
1753
|
-
const basePath = options.dbPath ?? process.cwd();
|
|
1754
|
-
return {
|
|
1755
|
-
error: `error: no .kbdb database found at ${basePath}`,
|
|
1756
|
-
exitCode: 1
|
|
1757
|
-
};
|
|
1758
|
-
}
|
|
1759
|
-
let client;
|
|
1760
|
-
try {
|
|
1761
|
-
client = await deps2.createWorkerClient({
|
|
1762
|
-
contextPath: dbDir
|
|
1763
|
-
});
|
|
1764
|
-
} catch (err) {
|
|
1765
|
-
return {
|
|
1766
|
-
error: `error: ${errorToMessage(err)}`,
|
|
1767
|
-
exitCode: 1
|
|
1768
|
-
};
|
|
1769
|
-
}
|
|
1770
|
-
try {
|
|
1771
|
-
const versionStatus = await client.checkVersion(dbDir);
|
|
1772
|
-
if (versionStatus.status === "too_new") {
|
|
1773
|
-
return {
|
|
1774
|
-
error: `error: database version ${versionStatus.currentVersion.toString()} is newer than kbdb version ${versionStatus.requiredVersion.toString()} -- upgrade kbdb`,
|
|
1775
|
-
exitCode: 1
|
|
1776
|
-
};
|
|
1777
|
-
}
|
|
1778
|
-
if (versionStatus.status === "needs_migration") {
|
|
1779
|
-
const migrateResult = await client.migrateDatabase(dbDir);
|
|
1780
|
-
return {
|
|
1781
|
-
output: JSON.stringify(migrateResult, null, 2),
|
|
1782
|
-
exitCode: 0
|
|
1783
|
-
};
|
|
1784
|
-
}
|
|
1785
|
-
return { output: "already up to date", exitCode: 0 };
|
|
1786
|
-
} catch (err) {
|
|
1787
|
-
return {
|
|
1788
|
-
error: `error: migrate failed: ${errorToMessage(err)}`,
|
|
1789
|
-
exitCode: 1
|
|
1790
|
-
};
|
|
1791
|
-
} finally {
|
|
1792
|
-
client.disconnect();
|
|
1793
|
-
}
|
|
1794
|
-
}
|
|
1795
|
-
async function handleDataCommand(options, deps2) {
|
|
1796
|
-
let dbDir = deps2.findDb(options.dbPath);
|
|
1797
|
-
if (!dbDir) {
|
|
1798
|
-
const basePath = options.dbPath ?? process.cwd();
|
|
1799
|
-
if (!deps2.isStdinTty()) {
|
|
1800
|
-
return {
|
|
1801
|
-
error: `error: no .kbdb database found at ${basePath} -- run \`kbdb --init\` to create one`,
|
|
1802
|
-
exitCode: 1
|
|
1803
|
-
};
|
|
1804
|
-
}
|
|
1805
|
-
const answer = await deps2.promptUser(
|
|
1806
|
-
`No .kbdb database found. Create one in ${basePath}? [y/N] `
|
|
1807
|
-
);
|
|
1808
|
-
if (answer.toLowerCase() !== "y") {
|
|
1809
|
-
return {
|
|
1810
|
-
error: "aborted: no database created",
|
|
1811
|
-
exitCode: 1
|
|
1812
|
-
};
|
|
1813
|
-
}
|
|
1814
|
-
const initResult = deps2.runInit(options);
|
|
1815
|
-
if (!initResult.ok) {
|
|
1816
|
-
return {
|
|
1817
|
-
error: `error: ${initResult.errorMessage ?? "init failed"}`,
|
|
1818
|
-
exitCode: 1
|
|
1819
|
-
};
|
|
1820
|
-
}
|
|
1821
|
-
dbDir = initResult.targetPath;
|
|
1822
|
-
}
|
|
1823
|
-
let client;
|
|
1824
|
-
try {
|
|
1825
|
-
client = await deps2.createWorkerClient({
|
|
1826
|
-
contextPath: dbDir
|
|
1827
|
-
});
|
|
1828
|
-
} catch (err) {
|
|
1829
|
-
return {
|
|
1830
|
-
error: `error: ${errorToMessage(err)}`,
|
|
1831
|
-
exitCode: 1
|
|
1832
|
-
};
|
|
1833
|
-
}
|
|
1834
|
-
try {
|
|
1835
|
-
const gateResult = await versionGate(client, dbDir);
|
|
1836
|
-
if (gateResult) return gateResult;
|
|
1837
|
-
return await dispatchCommand(client, options);
|
|
1838
|
-
} finally {
|
|
1839
|
-
client.disconnect();
|
|
1840
|
-
}
|
|
1841
|
-
}
|
|
1842
|
-
async function versionGate(client, dbDir) {
|
|
1843
|
-
const versionStatus = await client.checkVersion(dbDir);
|
|
1844
|
-
if (versionStatus.status === "needs_migration") {
|
|
1845
|
-
return {
|
|
1846
|
-
error: "error: database needs migration -- run `kbdb --migrate`",
|
|
1847
|
-
exitCode: 1
|
|
1848
|
-
};
|
|
1849
|
-
}
|
|
1850
|
-
if (versionStatus.status === "too_new") {
|
|
1851
|
-
return {
|
|
1852
|
-
error: `error: database version ${versionStatus.currentVersion.toString()} is newer than kbdb version ${versionStatus.requiredVersion.toString()} -- upgrade kbdb`,
|
|
1853
|
-
exitCode: 1
|
|
1854
|
-
};
|
|
1855
|
-
}
|
|
1856
|
-
return null;
|
|
1857
|
-
}
|
|
1858
|
-
|
|
1859
|
-
// src/shared/cli/constants/help-text.constant.ts
|
|
1860
|
-
var HELP_TEXT = generateHelp();
|
|
1861
|
-
|
|
1862
1797
|
// src/shared/worker-client/classes/worker-client.class.ts
|
|
1863
1798
|
import { connect } from "node:net";
|
|
1864
1799
|
var WorkerClient = class {
|
|
@@ -1867,10 +1802,13 @@ var WorkerClient = class {
|
|
|
1867
1802
|
* (or Windows named pipe path)
|
|
1868
1803
|
* @param contextId - 16-character context identifier sent as
|
|
1869
1804
|
* `ctx` in every JSON-RPC request
|
|
1805
|
+
* @param requestTimeoutMs - per-request timeout in ms
|
|
1806
|
+
* (0 or undefined disables the timeout)
|
|
1870
1807
|
*/
|
|
1871
|
-
constructor(socketPath, contextId) {
|
|
1808
|
+
constructor(socketPath, contextId, requestTimeoutMs) {
|
|
1872
1809
|
this.socketPath = socketPath;
|
|
1873
1810
|
this.contextId = contextId;
|
|
1811
|
+
this.requestTimeoutMs = requestTimeoutMs;
|
|
1874
1812
|
}
|
|
1875
1813
|
/** Underlying socket; `null` when not connected. */
|
|
1876
1814
|
socket = null;
|
|
@@ -1880,30 +1818,85 @@ var WorkerClient = class {
|
|
|
1880
1818
|
pending = /* @__PURE__ */ new Map();
|
|
1881
1819
|
/** Partial line buffer for split TCP/IPC frames. */
|
|
1882
1820
|
buffer = "";
|
|
1821
|
+
/** Whether the underlying socket is connected. */
|
|
1822
|
+
get isConnected() {
|
|
1823
|
+
return this.socket !== null;
|
|
1824
|
+
}
|
|
1883
1825
|
/**
|
|
1884
1826
|
* Establishes the IPC connection to the worker daemon.
|
|
1885
1827
|
*
|
|
1828
|
+
* Retries up to 5 times with exponential backoff on
|
|
1829
|
+
* transient errors (`ENOENT`, `ECONNREFUSED`).
|
|
1830
|
+
*
|
|
1886
1831
|
* @throws {Error} if the connection cannot be established
|
|
1887
1832
|
*/
|
|
1888
1833
|
async connect() {
|
|
1834
|
+
const maxRetries = 5;
|
|
1835
|
+
let delay = 50;
|
|
1836
|
+
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
1837
|
+
try {
|
|
1838
|
+
await this.tryConnect();
|
|
1839
|
+
return;
|
|
1840
|
+
} catch (err) {
|
|
1841
|
+
const code = err.code;
|
|
1842
|
+
const retriable = code === "ENOENT" || code === "ECONNREFUSED";
|
|
1843
|
+
if (!retriable || attempt === maxRetries) {
|
|
1844
|
+
throw err;
|
|
1845
|
+
}
|
|
1846
|
+
await new Promise((r) => setTimeout(r, delay));
|
|
1847
|
+
delay = Math.min(delay * 2, 1e3);
|
|
1848
|
+
}
|
|
1849
|
+
}
|
|
1850
|
+
}
|
|
1851
|
+
/** Destroys the IPC socket and clears the connection state. */
|
|
1852
|
+
disconnect() {
|
|
1853
|
+
if (this.socket) {
|
|
1854
|
+
this.socket.destroy();
|
|
1855
|
+
this.socket = null;
|
|
1856
|
+
}
|
|
1857
|
+
}
|
|
1858
|
+
/**
|
|
1859
|
+
* Single connection attempt with keepalive and
|
|
1860
|
+
* post-connect failure handlers.
|
|
1861
|
+
*/
|
|
1862
|
+
async tryConnect() {
|
|
1889
1863
|
return new Promise((resolve4, reject) => {
|
|
1890
|
-
|
|
1864
|
+
const sock = connect(this.socketPath, () => {
|
|
1865
|
+
sock.removeListener("error", reject);
|
|
1866
|
+
sock.setKeepAlive(true, 3e4);
|
|
1867
|
+
sock.on("error", (err) => {
|
|
1868
|
+
this.handleSocketFailure(err);
|
|
1869
|
+
});
|
|
1870
|
+
sock.on("close", () => {
|
|
1871
|
+
if (this.pending.size > 0) {
|
|
1872
|
+
this.handleSocketFailure(
|
|
1873
|
+
new Error("connection closed")
|
|
1874
|
+
);
|
|
1875
|
+
}
|
|
1876
|
+
this.socket = null;
|
|
1877
|
+
});
|
|
1878
|
+
this.socket = sock;
|
|
1891
1879
|
resolve4();
|
|
1892
1880
|
});
|
|
1893
|
-
|
|
1894
|
-
|
|
1895
|
-
});
|
|
1896
|
-
this.socket.on("data", (chunk) => {
|
|
1881
|
+
sock.on("error", reject);
|
|
1882
|
+
sock.on("data", (chunk) => {
|
|
1897
1883
|
this.handleData(chunk.toString());
|
|
1898
1884
|
});
|
|
1899
1885
|
});
|
|
1900
1886
|
}
|
|
1901
|
-
/**
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
|
|
1887
|
+
/**
|
|
1888
|
+
* Rejects all pending calls and marks the client as
|
|
1889
|
+
* disconnected.
|
|
1890
|
+
*/
|
|
1891
|
+
handleSocketFailure(err) {
|
|
1892
|
+
const sock = this.socket;
|
|
1893
|
+
this.socket = null;
|
|
1894
|
+
for (const [id, call] of this.pending) {
|
|
1895
|
+
this.pending.delete(id);
|
|
1896
|
+
if (call.timer) clearTimeout(call.timer);
|
|
1897
|
+
call.reject(err);
|
|
1906
1898
|
}
|
|
1899
|
+
if (sock) sock.destroy();
|
|
1907
1900
|
}
|
|
1908
1901
|
/**
|
|
1909
1902
|
* Searches the knowledge base.
|
|
@@ -1912,7 +1905,10 @@ var WorkerClient = class {
|
|
|
1912
1905
|
* @returns paginated search results with metadata
|
|
1913
1906
|
*/
|
|
1914
1907
|
async search(params) {
|
|
1915
|
-
return this.call("search",
|
|
1908
|
+
return this.call("search", {
|
|
1909
|
+
...params,
|
|
1910
|
+
algo: params.algo
|
|
1911
|
+
});
|
|
1916
1912
|
}
|
|
1917
1913
|
/**
|
|
1918
1914
|
* Retrieves full content and context for one or more sections by
|
|
@@ -2163,7 +2159,20 @@ var WorkerClient = class {
|
|
|
2163
2159
|
*/
|
|
2164
2160
|
async export(params) {
|
|
2165
2161
|
return this.call("export", {
|
|
2166
|
-
targetPath: params?.path || void 0
|
|
2162
|
+
targetPath: params?.path || void 0,
|
|
2163
|
+
format: params?.format
|
|
2164
|
+
});
|
|
2165
|
+
}
|
|
2166
|
+
/**
|
|
2167
|
+
* Imports an exported database from the given path.
|
|
2168
|
+
*
|
|
2169
|
+
* @param params - source path and dry-run flag
|
|
2170
|
+
* @returns import report with counts and timing
|
|
2171
|
+
*/
|
|
2172
|
+
async import(params) {
|
|
2173
|
+
return this.call("importDb", {
|
|
2174
|
+
sourcePath: params.from,
|
|
2175
|
+
dryRun: params.dryRun
|
|
2167
2176
|
});
|
|
2168
2177
|
}
|
|
2169
2178
|
/**
|
|
@@ -2186,8 +2195,21 @@ var WorkerClient = class {
|
|
|
2186
2195
|
method,
|
|
2187
2196
|
params: { ctx: this.contextId, ...params }
|
|
2188
2197
|
};
|
|
2198
|
+
const timeoutMs = this.requestTimeoutMs;
|
|
2189
2199
|
return new Promise((resolve4, reject) => {
|
|
2190
|
-
|
|
2200
|
+
const call = { resolve: resolve4, reject };
|
|
2201
|
+
if (timeoutMs) {
|
|
2202
|
+
call.timer = setTimeout(() => {
|
|
2203
|
+
if (!this.pending.has(id)) return;
|
|
2204
|
+
this.pending.delete(id);
|
|
2205
|
+
const err = new Error(
|
|
2206
|
+
`request timeout after ${String(timeoutMs)}ms`
|
|
2207
|
+
);
|
|
2208
|
+
err.code = "REQUEST_TIMEOUT";
|
|
2209
|
+
reject(err);
|
|
2210
|
+
}, timeoutMs);
|
|
2211
|
+
}
|
|
2212
|
+
this.pending.set(id, call);
|
|
2191
2213
|
socket.write(JSON.stringify(request) + "\n");
|
|
2192
2214
|
});
|
|
2193
2215
|
}
|
|
@@ -2208,6 +2230,7 @@ var WorkerClient = class {
|
|
|
2208
2230
|
const pending = this.pending.get(response.id);
|
|
2209
2231
|
if (pending) {
|
|
2210
2232
|
this.pending.delete(response.id);
|
|
2233
|
+
if (pending.timer) clearTimeout(pending.timer);
|
|
2211
2234
|
if (response.error) {
|
|
2212
2235
|
const err = new Error(response.error.message);
|
|
2213
2236
|
err.code = response.error.code;
|
|
@@ -2253,104 +2276,400 @@ import { tmpdir } from "node:os";
|
|
|
2253
2276
|
// src/shared/platform/functions/is-process-alive.function.ts
|
|
2254
2277
|
function isProcessAlive(pid) {
|
|
2255
2278
|
try {
|
|
2256
|
-
process.kill(pid, 0);
|
|
2257
|
-
return true;
|
|
2258
|
-
} catch {
|
|
2259
|
-
return false;
|
|
2279
|
+
process.kill(pid, 0);
|
|
2280
|
+
return true;
|
|
2281
|
+
} catch {
|
|
2282
|
+
return false;
|
|
2283
|
+
}
|
|
2284
|
+
}
|
|
2285
|
+
|
|
2286
|
+
// src/shared/worker-client/functions/discover-daemon.function.ts
|
|
2287
|
+
function discoverDaemon(contextId) {
|
|
2288
|
+
const pidPath = join6(tmpdir(), `kbdb-${contextId}.pid`);
|
|
2289
|
+
if (!existsSync5(pidPath)) return null;
|
|
2290
|
+
const pidStr = readFileSync2(pidPath, "utf-8").trim();
|
|
2291
|
+
const pid = parseInt(pidStr, 10);
|
|
2292
|
+
if (isNaN(pid) || !isProcessAlive(pid)) {
|
|
2293
|
+
try {
|
|
2294
|
+
unlinkSync(pidPath);
|
|
2295
|
+
} catch {
|
|
2296
|
+
}
|
|
2297
|
+
return null;
|
|
2298
|
+
}
|
|
2299
|
+
const socketPath = join6(tmpdir(), `kbdb-${contextId}.sock`);
|
|
2300
|
+
const crashPath = join6(tmpdir(), `kbdb-${contextId}.crash`);
|
|
2301
|
+
try {
|
|
2302
|
+
unlinkSync(crashPath);
|
|
2303
|
+
} catch {
|
|
2304
|
+
}
|
|
2305
|
+
return { pid, socketPath };
|
|
2306
|
+
}
|
|
2307
|
+
|
|
2308
|
+
// src/shared/worker-client/functions/spawn-daemon.function.ts
|
|
2309
|
+
import { spawn } from "node:child_process";
|
|
2310
|
+
function spawnDaemon(contextPath, runtime2, workerScript2) {
|
|
2311
|
+
if (!workerScript2) {
|
|
2312
|
+
throw new Error(
|
|
2313
|
+
"workerScript must be provided (resolve at entry point)"
|
|
2314
|
+
);
|
|
2315
|
+
}
|
|
2316
|
+
const execPath = runtime2 === "deno" ? "deno" : process.execPath;
|
|
2317
|
+
const args = runtime2 === "deno" ? ["run", "--allow-all", workerScript2, contextPath] : [workerScript2, contextPath];
|
|
2318
|
+
const child = spawn(execPath, args, {
|
|
2319
|
+
detached: true,
|
|
2320
|
+
stdio: "ignore"
|
|
2321
|
+
});
|
|
2322
|
+
child.unref();
|
|
2323
|
+
}
|
|
2324
|
+
|
|
2325
|
+
// src/shared/worker-client/functions/wait-for-daemon.function.ts
|
|
2326
|
+
import { existsSync as existsSync6 } from "node:fs";
|
|
2327
|
+
import { join as join7 } from "node:path";
|
|
2328
|
+
import { tmpdir as tmpdir2 } from "node:os";
|
|
2329
|
+
var DEFAULT_TIMEOUT_MS = 1e4;
|
|
2330
|
+
var POLL_INTERVAL_MS = 100;
|
|
2331
|
+
async function waitForDaemon(contextId, timeoutMs = DEFAULT_TIMEOUT_MS) {
|
|
2332
|
+
const pidPath = join7(tmpdir2(), `kbdb-${contextId}.pid`);
|
|
2333
|
+
const start = Date.now();
|
|
2334
|
+
while (Date.now() - start < timeoutMs) {
|
|
2335
|
+
if (existsSync6(pidPath)) return true;
|
|
2336
|
+
await new Promise((r) => setTimeout(r, POLL_INTERVAL_MS));
|
|
2337
|
+
}
|
|
2338
|
+
return false;
|
|
2339
|
+
}
|
|
2340
|
+
|
|
2341
|
+
// src/shared/worker-client/functions/create-worker-client.function.ts
|
|
2342
|
+
async function createWorkerClient(options) {
|
|
2343
|
+
const dbPath = options?.contextPath ?? resolveDbPath(options?.dbPath);
|
|
2344
|
+
const contextId = await computeContextId(dbPath);
|
|
2345
|
+
const socketPath = getIpcPath(contextId);
|
|
2346
|
+
let daemon = discoverDaemon(contextId);
|
|
2347
|
+
if (!daemon && options?.autoSpawn !== false) {
|
|
2348
|
+
const runtime2 = detectRuntime();
|
|
2349
|
+
spawnDaemon(dbPath, runtime2, options?.workerScript);
|
|
2350
|
+
await waitForDaemon(contextId, options?.connectTimeoutMs);
|
|
2351
|
+
daemon = discoverDaemon(contextId);
|
|
2352
|
+
}
|
|
2353
|
+
if (!daemon) {
|
|
2354
|
+
throw new Error(
|
|
2355
|
+
`kbdb worker daemon did not start: ${readCrashReason(contextId)}`
|
|
2356
|
+
);
|
|
2357
|
+
}
|
|
2358
|
+
const client = new WorkerClient(
|
|
2359
|
+
socketPath,
|
|
2360
|
+
contextId,
|
|
2361
|
+
options?.requestTimeoutMs
|
|
2362
|
+
);
|
|
2363
|
+
await client.connect();
|
|
2364
|
+
return client;
|
|
2365
|
+
}
|
|
2366
|
+
function readCrashReason(contextId) {
|
|
2367
|
+
const crashPath = join8(tmpdir3(), `kbdb-${contextId}.crash`);
|
|
2368
|
+
if (!existsSync7(crashPath)) {
|
|
2369
|
+
return "daemon failed to start (no details available)";
|
|
2370
|
+
}
|
|
2371
|
+
try {
|
|
2372
|
+
const raw = readFileSync3(crashPath, "utf-8");
|
|
2373
|
+
const crash = JSON.parse(raw);
|
|
2374
|
+
unlinkSync2(crashPath);
|
|
2375
|
+
return crash.error ?? "unknown error (crash log empty)";
|
|
2376
|
+
} catch {
|
|
2377
|
+
return "daemon failed to start (crash log unreadable)";
|
|
2378
|
+
}
|
|
2379
|
+
}
|
|
2380
|
+
|
|
2381
|
+
// src/shared/cli/functions/run-worker-stop.function.ts
|
|
2382
|
+
async function runWorkerStop(dbDir) {
|
|
2383
|
+
const contextId = await computeContextId(dbDir);
|
|
2384
|
+
const info = discoverDaemon(contextId);
|
|
2385
|
+
if (!info) {
|
|
2386
|
+
return { output: "no daemon running", exitCode: 0 };
|
|
2387
|
+
}
|
|
2388
|
+
try {
|
|
2389
|
+
process.kill(info.pid, "SIGTERM");
|
|
2390
|
+
return {
|
|
2391
|
+
output: `stopped daemon (pid ${info.pid.toString()})`,
|
|
2392
|
+
exitCode: 0
|
|
2393
|
+
};
|
|
2394
|
+
} catch {
|
|
2395
|
+
return {
|
|
2396
|
+
error: "error: failed to stop daemon",
|
|
2397
|
+
exitCode: 1
|
|
2398
|
+
};
|
|
2399
|
+
}
|
|
2400
|
+
}
|
|
2401
|
+
|
|
2402
|
+
// src/shared/cli/functions/run-worker-restart.function.ts
|
|
2403
|
+
async function runWorkerRestart(dbDir) {
|
|
2404
|
+
const stopResult = await runWorkerStop(dbDir);
|
|
2405
|
+
if (stopResult.exitCode !== 0) return stopResult;
|
|
2406
|
+
return {
|
|
2407
|
+
output: "daemon will restart on next command",
|
|
2408
|
+
exitCode: 0
|
|
2409
|
+
};
|
|
2410
|
+
}
|
|
2411
|
+
|
|
2412
|
+
// src/shared/cli/functions/resolve-raw-db-path.function.ts
|
|
2413
|
+
function resolveRawDbPath(options) {
|
|
2414
|
+
return options.dbPath ?? process.env["KBDB_DB_DIR"];
|
|
2415
|
+
}
|
|
2416
|
+
|
|
2417
|
+
// src/shared/worker-client/functions/retry-on-timeout.function.ts
|
|
2418
|
+
async function retryOnTimeout(createClient, operation, maxRetries) {
|
|
2419
|
+
let lastError = new Error("retryOnTimeout exhausted");
|
|
2420
|
+
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
2421
|
+
const client = await createClient();
|
|
2422
|
+
try {
|
|
2423
|
+
const result = await operation(client);
|
|
2424
|
+
return { client, result };
|
|
2425
|
+
} catch (err) {
|
|
2426
|
+
const code = err.code;
|
|
2427
|
+
if (code !== "REQUEST_TIMEOUT" || attempt >= maxRetries) {
|
|
2428
|
+
client.disconnect();
|
|
2429
|
+
throw err;
|
|
2430
|
+
}
|
|
2431
|
+
lastError = err;
|
|
2432
|
+
client.disconnect();
|
|
2433
|
+
}
|
|
2434
|
+
}
|
|
2435
|
+
throw lastError;
|
|
2436
|
+
}
|
|
2437
|
+
|
|
2438
|
+
// src/shared/worker-client/constants/request-timeout.constant.ts
|
|
2439
|
+
var DEFAULT_REQUEST_TIMEOUT_MS = 3e4;
|
|
2440
|
+
|
|
2441
|
+
// src/shared/worker-client/constants/retry-defaults.constant.ts
|
|
2442
|
+
var DEFAULT_RETRY_COUNT = 5;
|
|
2443
|
+
|
|
2444
|
+
// src/shared/cli/functions/run-cli.function.ts
|
|
2445
|
+
async function runCli(argv, deps2) {
|
|
2446
|
+
const options = parseArgs(argv);
|
|
2447
|
+
if (!options.formatExplicit) {
|
|
2448
|
+
options.format = deps2.isStdoutTty() ? "text" : "json";
|
|
2449
|
+
}
|
|
2450
|
+
if (options.command === "help" || !options.command) {
|
|
2451
|
+
const helpCommand = options.args[0];
|
|
2452
|
+
const helpSubcommand = options.args[1];
|
|
2453
|
+
const helpText = generateHelp(helpCommand, helpSubcommand);
|
|
2454
|
+
if (helpCommand && !isKnownCommand(helpCommand)) {
|
|
2455
|
+
return { output: helpText, exitCode: 1 };
|
|
2456
|
+
}
|
|
2457
|
+
return { output: helpText, exitCode: 0 };
|
|
2458
|
+
}
|
|
2459
|
+
if (options.command === "version") {
|
|
2460
|
+
return { output: VERSION, exitCode: 0 };
|
|
2461
|
+
}
|
|
2462
|
+
if (options.command === "init" || options.command === "db-init") {
|
|
2463
|
+
return handleInit(options, deps2);
|
|
2464
|
+
}
|
|
2465
|
+
if (options.command === "mcp") {
|
|
2466
|
+
return handleMcp(options, deps2);
|
|
2467
|
+
}
|
|
2468
|
+
if (options.command === "migrate" || options.command === "db-migrate") {
|
|
2469
|
+
return handleMigrate(options, deps2);
|
|
2470
|
+
}
|
|
2471
|
+
if (options.command === "worker-stop") {
|
|
2472
|
+
return handleWorkerLifecycle(options, deps2, runWorkerStop);
|
|
2473
|
+
}
|
|
2474
|
+
if (options.command === "worker-restart") {
|
|
2475
|
+
return handleWorkerLifecycle(options, deps2, runWorkerRestart);
|
|
2476
|
+
}
|
|
2477
|
+
return handleDataCommand(options, deps2);
|
|
2478
|
+
}
|
|
2479
|
+
function handleInit(options, deps2) {
|
|
2480
|
+
const result = deps2.runInit(options);
|
|
2481
|
+
if (result.ok) {
|
|
2482
|
+
return {
|
|
2483
|
+
output: result.targetPath,
|
|
2484
|
+
exitCode: 0
|
|
2485
|
+
};
|
|
2486
|
+
}
|
|
2487
|
+
return {
|
|
2488
|
+
error: `error: ${result.errorMessage ?? "init failed"}`,
|
|
2489
|
+
exitCode: 1
|
|
2490
|
+
};
|
|
2491
|
+
}
|
|
2492
|
+
function resolveTimeoutMs(options) {
|
|
2493
|
+
return options.timeoutMs ?? (parseInt(process.env["KBDB_DAEMON_TIMEOUT"] ?? "", 10) || DEFAULT_REQUEST_TIMEOUT_MS);
|
|
2494
|
+
}
|
|
2495
|
+
function resolveRetryCount(options) {
|
|
2496
|
+
return options.retry ?? (parseInt(process.env["KBDB_RETRY"] ?? "", 10) || DEFAULT_RETRY_COUNT);
|
|
2497
|
+
}
|
|
2498
|
+
async function handleMcp(options, deps2) {
|
|
2499
|
+
const explicitPath = resolveRawDbPath(options);
|
|
2500
|
+
const dbDir = deps2.findDb(explicitPath);
|
|
2501
|
+
if (!dbDir) {
|
|
2502
|
+
return {
|
|
2503
|
+
error: "error: no .kbdb database found at " + (explicitPath ?? process.cwd()),
|
|
2504
|
+
exitCode: 1
|
|
2505
|
+
};
|
|
2506
|
+
}
|
|
2507
|
+
const timeoutMs = resolveTimeoutMs(options);
|
|
2508
|
+
const maxRetries = resolveRetryCount(options);
|
|
2509
|
+
let client;
|
|
2510
|
+
try {
|
|
2511
|
+
client = await deps2.createWorkerClient({
|
|
2512
|
+
contextPath: dbDir,
|
|
2513
|
+
requestTimeoutMs: timeoutMs
|
|
2514
|
+
});
|
|
2515
|
+
} catch (err) {
|
|
2516
|
+
return {
|
|
2517
|
+
error: `error: ${errorToMessage(err)}`,
|
|
2518
|
+
exitCode: 1
|
|
2519
|
+
};
|
|
2520
|
+
}
|
|
2521
|
+
try {
|
|
2522
|
+
const gateResult = await versionGate(client, dbDir);
|
|
2523
|
+
if (gateResult) return gateResult;
|
|
2524
|
+
if (deps2.startMcpServer) {
|
|
2525
|
+
await deps2.startMcpServer(
|
|
2526
|
+
dbDir,
|
|
2527
|
+
() => deps2.createWorkerClient({
|
|
2528
|
+
contextPath: dbDir,
|
|
2529
|
+
requestTimeoutMs: timeoutMs
|
|
2530
|
+
}),
|
|
2531
|
+
maxRetries
|
|
2532
|
+
);
|
|
2533
|
+
}
|
|
2534
|
+
return { exitCode: 0 };
|
|
2535
|
+
} catch (err) {
|
|
2536
|
+
return {
|
|
2537
|
+
error: `error: mcp failed: ${errorToMessage(err)}`,
|
|
2538
|
+
exitCode: 1
|
|
2539
|
+
};
|
|
2540
|
+
} finally {
|
|
2541
|
+
client.disconnect();
|
|
2260
2542
|
}
|
|
2261
2543
|
}
|
|
2262
|
-
|
|
2263
|
-
|
|
2264
|
-
|
|
2265
|
-
|
|
2266
|
-
|
|
2267
|
-
|
|
2268
|
-
|
|
2269
|
-
|
|
2270
|
-
try {
|
|
2271
|
-
unlinkSync(pidPath);
|
|
2272
|
-
} catch {
|
|
2273
|
-
}
|
|
2274
|
-
return null;
|
|
2544
|
+
async function handleMigrate(options, deps2) {
|
|
2545
|
+
const dbDir = deps2.findDb(options.dbPath);
|
|
2546
|
+
if (!dbDir) {
|
|
2547
|
+
const basePath = options.dbPath ?? process.cwd();
|
|
2548
|
+
return {
|
|
2549
|
+
error: `error: no .kbdb database found at ${basePath}`,
|
|
2550
|
+
exitCode: 1
|
|
2551
|
+
};
|
|
2275
2552
|
}
|
|
2276
|
-
|
|
2277
|
-
const crashPath = join6(tmpdir(), `kbdb-${contextId}.crash`);
|
|
2553
|
+
let client;
|
|
2278
2554
|
try {
|
|
2279
|
-
|
|
2280
|
-
|
|
2555
|
+
client = await deps2.createWorkerClient({
|
|
2556
|
+
contextPath: dbDir
|
|
2557
|
+
});
|
|
2558
|
+
} catch (err) {
|
|
2559
|
+
return {
|
|
2560
|
+
error: `error: ${errorToMessage(err)}`,
|
|
2561
|
+
exitCode: 1
|
|
2562
|
+
};
|
|
2563
|
+
}
|
|
2564
|
+
try {
|
|
2565
|
+
const versionStatus = await client.checkVersion(dbDir);
|
|
2566
|
+
if (versionStatus.status === "too_new") {
|
|
2567
|
+
return {
|
|
2568
|
+
error: `error: database version ${versionStatus.currentVersion.toString()} is newer than kbdb version ${versionStatus.requiredVersion.toString()} -- upgrade kbdb`,
|
|
2569
|
+
exitCode: 1
|
|
2570
|
+
};
|
|
2571
|
+
}
|
|
2572
|
+
if (versionStatus.status === "needs_migration") {
|
|
2573
|
+
const migrateResult = await client.migrateDatabase(dbDir);
|
|
2574
|
+
return {
|
|
2575
|
+
output: JSON.stringify(migrateResult, null, 2),
|
|
2576
|
+
exitCode: 0
|
|
2577
|
+
};
|
|
2578
|
+
}
|
|
2579
|
+
return { output: "already up to date", exitCode: 0 };
|
|
2580
|
+
} catch (err) {
|
|
2581
|
+
return {
|
|
2582
|
+
error: `error: migrate failed: ${errorToMessage(err)}`,
|
|
2583
|
+
exitCode: 1
|
|
2584
|
+
};
|
|
2585
|
+
} finally {
|
|
2586
|
+
client.disconnect();
|
|
2281
2587
|
}
|
|
2282
|
-
return { pid, socketPath };
|
|
2283
2588
|
}
|
|
2284
|
-
|
|
2285
|
-
|
|
2286
|
-
|
|
2287
|
-
|
|
2288
|
-
|
|
2289
|
-
|
|
2290
|
-
|
|
2589
|
+
async function handleDataCommand(options, deps2) {
|
|
2590
|
+
const explicitPath = resolveRawDbPath(options);
|
|
2591
|
+
let dbDir = deps2.findDb(explicitPath);
|
|
2592
|
+
if (!dbDir) {
|
|
2593
|
+
const basePath = explicitPath ?? process.cwd();
|
|
2594
|
+
if (!deps2.isStdinTty()) {
|
|
2595
|
+
return {
|
|
2596
|
+
error: `error: no .kbdb database found at ${basePath} -- run \`kbdb --init\` to create one`,
|
|
2597
|
+
exitCode: 1
|
|
2598
|
+
};
|
|
2599
|
+
}
|
|
2600
|
+
const answer = await deps2.promptUser(
|
|
2601
|
+
`No .kbdb database found. Create one in ${basePath}? [y/N] `
|
|
2291
2602
|
);
|
|
2603
|
+
if (answer.toLowerCase() !== "y") {
|
|
2604
|
+
return {
|
|
2605
|
+
error: "aborted: no database created",
|
|
2606
|
+
exitCode: 1
|
|
2607
|
+
};
|
|
2608
|
+
}
|
|
2609
|
+
const initResult = deps2.runInit(options);
|
|
2610
|
+
if (!initResult.ok) {
|
|
2611
|
+
return {
|
|
2612
|
+
error: `error: ${initResult.errorMessage ?? "init failed"}`,
|
|
2613
|
+
exitCode: 1
|
|
2614
|
+
};
|
|
2615
|
+
}
|
|
2616
|
+
dbDir = initResult.targetPath;
|
|
2292
2617
|
}
|
|
2293
|
-
const
|
|
2294
|
-
const
|
|
2295
|
-
|
|
2296
|
-
|
|
2297
|
-
|
|
2298
|
-
|
|
2299
|
-
|
|
2300
|
-
}
|
|
2301
|
-
|
|
2302
|
-
|
|
2303
|
-
|
|
2304
|
-
|
|
2305
|
-
|
|
2306
|
-
|
|
2307
|
-
|
|
2308
|
-
|
|
2309
|
-
|
|
2310
|
-
|
|
2311
|
-
|
|
2312
|
-
|
|
2313
|
-
|
|
2618
|
+
const timeoutMs = resolveTimeoutMs(options);
|
|
2619
|
+
const maxRetries = resolveRetryCount(options);
|
|
2620
|
+
try {
|
|
2621
|
+
const { client, result } = await retryOnTimeout(
|
|
2622
|
+
() => deps2.createWorkerClient({
|
|
2623
|
+
contextPath: dbDir,
|
|
2624
|
+
requestTimeoutMs: timeoutMs
|
|
2625
|
+
}),
|
|
2626
|
+
async (c) => {
|
|
2627
|
+
const gateResult = await versionGate(c, dbDir);
|
|
2628
|
+
if (gateResult) return gateResult;
|
|
2629
|
+
return dispatchCommand(c, options);
|
|
2630
|
+
},
|
|
2631
|
+
maxRetries
|
|
2632
|
+
);
|
|
2633
|
+
client.disconnect();
|
|
2634
|
+
return result;
|
|
2635
|
+
} catch (err) {
|
|
2636
|
+
return {
|
|
2637
|
+
error: `error: ${errorToMessage(err)}`,
|
|
2638
|
+
exitCode: 1
|
|
2639
|
+
};
|
|
2314
2640
|
}
|
|
2315
|
-
return false;
|
|
2316
2641
|
}
|
|
2317
|
-
|
|
2318
|
-
|
|
2319
|
-
|
|
2320
|
-
|
|
2321
|
-
|
|
2322
|
-
|
|
2323
|
-
|
|
2324
|
-
|
|
2325
|
-
const runtime2 = detectRuntime();
|
|
2326
|
-
spawnDaemon(dbPath, runtime2, options?.workerScript);
|
|
2327
|
-
await waitForDaemon(contextId, options?.connectTimeoutMs);
|
|
2328
|
-
daemon = discoverDaemon(contextId);
|
|
2329
|
-
}
|
|
2330
|
-
if (!daemon) {
|
|
2331
|
-
throw new Error(
|
|
2332
|
-
`kbdb worker daemon did not start: ${readCrashReason(contextId)}`
|
|
2333
|
-
);
|
|
2642
|
+
async function handleWorkerLifecycle(options, deps2, action) {
|
|
2643
|
+
const explicitPath = resolveRawDbPath(options);
|
|
2644
|
+
const dbDir = deps2.findDb(explicitPath);
|
|
2645
|
+
if (!dbDir) {
|
|
2646
|
+
return {
|
|
2647
|
+
error: "error: no .kbdb database found at " + (explicitPath ?? process.cwd()),
|
|
2648
|
+
exitCode: 1
|
|
2649
|
+
};
|
|
2334
2650
|
}
|
|
2335
|
-
|
|
2336
|
-
await client.connect();
|
|
2337
|
-
return client;
|
|
2651
|
+
return action(dbDir);
|
|
2338
2652
|
}
|
|
2339
|
-
function
|
|
2340
|
-
const
|
|
2341
|
-
if (
|
|
2342
|
-
return
|
|
2653
|
+
async function versionGate(client, dbDir) {
|
|
2654
|
+
const versionStatus = await client.checkVersion(dbDir);
|
|
2655
|
+
if (versionStatus.status === "needs_migration") {
|
|
2656
|
+
return {
|
|
2657
|
+
error: "error: database needs migration -- run `kbdb --migrate`",
|
|
2658
|
+
exitCode: 1
|
|
2659
|
+
};
|
|
2343
2660
|
}
|
|
2344
|
-
|
|
2345
|
-
|
|
2346
|
-
|
|
2347
|
-
|
|
2348
|
-
|
|
2349
|
-
} catch {
|
|
2350
|
-
return "daemon failed to start (crash log unreadable)";
|
|
2661
|
+
if (versionStatus.status === "too_new") {
|
|
2662
|
+
return {
|
|
2663
|
+
error: `error: database version ${versionStatus.currentVersion.toString()} is newer than kbdb version ${versionStatus.requiredVersion.toString()} -- upgrade kbdb`,
|
|
2664
|
+
exitCode: 1
|
|
2665
|
+
};
|
|
2351
2666
|
}
|
|
2667
|
+
return null;
|
|
2352
2668
|
}
|
|
2353
2669
|
|
|
2670
|
+
// src/shared/cli/constants/help-text.constant.ts
|
|
2671
|
+
var HELP_TEXT = generateHelp();
|
|
2672
|
+
|
|
2354
2673
|
// src/shared/mcp/classes/mcp-server.class.ts
|
|
2355
2674
|
import { createInterface as createInterface2 } from "node:readline";
|
|
2356
2675
|
|
|
@@ -2384,6 +2703,10 @@ function createToolDefinitions() {
|
|
|
2384
2703
|
type: "string",
|
|
2385
2704
|
enum: ["lexical", "vector", "hybrid"],
|
|
2386
2705
|
description: "Search algorithm (default: lexical)"
|
|
2706
|
+
},
|
|
2707
|
+
relaxed: {
|
|
2708
|
+
type: "boolean",
|
|
2709
|
+
description: "Use relaxed (OR) matching when the strict query returns no results"
|
|
2387
2710
|
}
|
|
2388
2711
|
},
|
|
2389
2712
|
required: ["query"]
|
|
@@ -3135,7 +3458,8 @@ async function handleSearch2(client, args) {
|
|
|
3135
3458
|
mode: args["mode"],
|
|
3136
3459
|
limit: args["limit"],
|
|
3137
3460
|
offset: args["offset"],
|
|
3138
|
-
algo: args["algo"]
|
|
3461
|
+
algo: args["algo"],
|
|
3462
|
+
relaxed: args["relaxed"]
|
|
3139
3463
|
});
|
|
3140
3464
|
const stripped = {
|
|
3141
3465
|
...results,
|
|
@@ -3702,6 +4026,125 @@ function shouldEmitLog(messageLevel, threshold) {
|
|
|
3702
4026
|
return messageIndex >= thresholdIndex;
|
|
3703
4027
|
}
|
|
3704
4028
|
|
|
4029
|
+
// src/shared/auto-capture/functions/build-capture-prompt.function.ts
|
|
4030
|
+
function buildCapturePrompt(recentContext) {
|
|
4031
|
+
return "Review the following conversation context and identify factual knowledge worth storing in the knowledge base.\n\nRules:\n- Include durable facts, decisions, preferences, and corrections that would be useful in future sessions.\n- Exclude ephemeral, task-specific, or temporary information that has no lasting value.\n- Format each piece of extracted knowledge as a concise statement suitable for direct storage.\n- If no durable knowledge is found, respond with an empty list.\n\nConversation context:\n" + recentContext;
|
|
4032
|
+
}
|
|
4033
|
+
|
|
4034
|
+
// src/shared/mcp/functions/has-sampling-capability.function.ts
|
|
4035
|
+
function hasSamplingCapability(hostCapabilities) {
|
|
4036
|
+
if (hostCapabilities === null) return false;
|
|
4037
|
+
return Boolean(hostCapabilities["sampling"]);
|
|
4038
|
+
}
|
|
4039
|
+
|
|
4040
|
+
// src/shared/mcp/functions/run-auto-capture.function.ts
|
|
4041
|
+
function isSamplingResponse(v) {
|
|
4042
|
+
if (typeof v !== "object" || v === null) return false;
|
|
4043
|
+
const obj = v;
|
|
4044
|
+
const content = obj["content"];
|
|
4045
|
+
if (typeof content !== "object" || content === null) return false;
|
|
4046
|
+
return typeof content["text"] === "string";
|
|
4047
|
+
}
|
|
4048
|
+
async function runAutoCapture(controller, config, client, hostCapabilities, requestSampling) {
|
|
4049
|
+
try {
|
|
4050
|
+
const trigger = controller.evaluate();
|
|
4051
|
+
if (!trigger.shouldCapture) return;
|
|
4052
|
+
if (!hasSamplingCapability(hostCapabilities)) return;
|
|
4053
|
+
const prompt = buildCapturePrompt("recent conversation context");
|
|
4054
|
+
const messages = [
|
|
4055
|
+
{ role: "user", content: { type: "text", text: prompt } }
|
|
4056
|
+
];
|
|
4057
|
+
const raw = await requestSampling(messages, 1024);
|
|
4058
|
+
if (!isSamplingResponse(raw)) return;
|
|
4059
|
+
const text = raw.content.text.trim();
|
|
4060
|
+
if (!text) return;
|
|
4061
|
+
if (config.dryRun) return;
|
|
4062
|
+
await client.addSection({
|
|
4063
|
+
sectionType: "text/markdown",
|
|
4064
|
+
content: text,
|
|
4065
|
+
title: "Auto-captured knowledge"
|
|
4066
|
+
});
|
|
4067
|
+
} catch {
|
|
4068
|
+
}
|
|
4069
|
+
}
|
|
4070
|
+
|
|
4071
|
+
// src/shared/auto-capture/functions/evaluate-trigger.function.ts
|
|
4072
|
+
function evaluateTrigger(config, state) {
|
|
4073
|
+
if (!config.enabled) {
|
|
4074
|
+
return {
|
|
4075
|
+
shouldCapture: false,
|
|
4076
|
+
reason: "auto-capture is disabled"
|
|
4077
|
+
};
|
|
4078
|
+
}
|
|
4079
|
+
if (state.totalCapturesThisSession >= config.maxCapturesPerSession) {
|
|
4080
|
+
return {
|
|
4081
|
+
shouldCapture: false,
|
|
4082
|
+
reason: "session capture limit reached"
|
|
4083
|
+
};
|
|
4084
|
+
}
|
|
4085
|
+
const elapsed = state.lastCaptureTimestamp ? Date.now() - state.lastCaptureTimestamp : Infinity;
|
|
4086
|
+
if (elapsed < config.minIntervalMs) {
|
|
4087
|
+
return {
|
|
4088
|
+
shouldCapture: false,
|
|
4089
|
+
reason: "within minimum interval"
|
|
4090
|
+
};
|
|
4091
|
+
}
|
|
4092
|
+
if (state.toolCallsSinceLastCapture < config.triggerAfterToolCalls) {
|
|
4093
|
+
return {
|
|
4094
|
+
shouldCapture: false,
|
|
4095
|
+
reason: "tool call threshold not yet reached"
|
|
4096
|
+
};
|
|
4097
|
+
}
|
|
4098
|
+
return { shouldCapture: true, reason: "trigger threshold met" };
|
|
4099
|
+
}
|
|
4100
|
+
|
|
4101
|
+
// src/shared/auto-capture/classes/auto-capture-controller.class.ts
|
|
4102
|
+
var AutoCaptureController = class {
|
|
4103
|
+
toolCallsSinceLastCapture = 0;
|
|
4104
|
+
lastCaptureTimestamp = 0;
|
|
4105
|
+
totalCapturesThisSession = 0;
|
|
4106
|
+
config;
|
|
4107
|
+
/** @param config - the auto-capture configuration to use */
|
|
4108
|
+
constructor(config) {
|
|
4109
|
+
this.config = config;
|
|
4110
|
+
}
|
|
4111
|
+
/** Record one tool call for trigger counting. */
|
|
4112
|
+
recordToolCall() {
|
|
4113
|
+
this.toolCallsSinceLastCapture += 1;
|
|
4114
|
+
}
|
|
4115
|
+
/**
|
|
4116
|
+
* Evaluates the current state against the trigger criteria.
|
|
4117
|
+
*
|
|
4118
|
+
* When the trigger fires, advances the session counters so the
|
|
4119
|
+
* next evaluation respects rate limiting and the session cap.
|
|
4120
|
+
*
|
|
4121
|
+
* @returns the trigger decision with a human-readable reason
|
|
4122
|
+
*/
|
|
4123
|
+
evaluate() {
|
|
4124
|
+
const trigger = evaluateTrigger(this.config, this.state());
|
|
4125
|
+
if (trigger.shouldCapture) {
|
|
4126
|
+
this.totalCapturesThisSession += 1;
|
|
4127
|
+
this.lastCaptureTimestamp = Date.now();
|
|
4128
|
+
this.toolCallsSinceLastCapture = 0;
|
|
4129
|
+
}
|
|
4130
|
+
return trigger;
|
|
4131
|
+
}
|
|
4132
|
+
/** Reset all counters, e.g. on client disconnect. */
|
|
4133
|
+
reset() {
|
|
4134
|
+
this.toolCallsSinceLastCapture = 0;
|
|
4135
|
+
this.lastCaptureTimestamp = 0;
|
|
4136
|
+
this.totalCapturesThisSession = 0;
|
|
4137
|
+
}
|
|
4138
|
+
/** Returns a snapshot of the current controller state. */
|
|
4139
|
+
state() {
|
|
4140
|
+
return {
|
|
4141
|
+
toolCallsSinceLastCapture: this.toolCallsSinceLastCapture,
|
|
4142
|
+
lastCaptureTimestamp: this.lastCaptureTimestamp,
|
|
4143
|
+
totalCapturesThisSession: this.totalCapturesThisSession
|
|
4144
|
+
};
|
|
4145
|
+
}
|
|
4146
|
+
};
|
|
4147
|
+
|
|
3705
4148
|
// src/shared/mcp/constants/mcp-error-codes.constant.ts
|
|
3706
4149
|
var PARSE_ERROR = -32700;
|
|
3707
4150
|
var METHOD_NOT_FOUND = -32601;
|
|
@@ -3711,15 +4154,50 @@ var RESOURCE_NOT_FOUND = -32002;
|
|
|
3711
4154
|
var McpServer = class {
|
|
3712
4155
|
client;
|
|
3713
4156
|
contextPath;
|
|
4157
|
+
autoCaptureConfig;
|
|
4158
|
+
clientFactory;
|
|
4159
|
+
maxRetries;
|
|
4160
|
+
autoCaptureController = null;
|
|
3714
4161
|
initialized = false;
|
|
3715
4162
|
hostCapabilities = null;
|
|
3716
4163
|
logLevel = null;
|
|
3717
4164
|
subscriptions = /* @__PURE__ */ new Set();
|
|
3718
4165
|
nextRequestId = 1;
|
|
3719
4166
|
pendingRequests = /* @__PURE__ */ new Map();
|
|
3720
|
-
|
|
4167
|
+
/**
|
|
4168
|
+
* @param client - worker client providing knowledge-base operations
|
|
4169
|
+
* @param contextPath - absolute path to the `.kbdb` directory
|
|
4170
|
+
* @param autoCaptureConfig - optional auto-capture configuration;
|
|
4171
|
+
* when provided the server activates auto-capture on connection
|
|
4172
|
+
* @param clientFactory - creates a fresh worker client (for retry)
|
|
4173
|
+
* @param maxRetries - max retries on REQUEST_TIMEOUT
|
|
4174
|
+
*/
|
|
4175
|
+
constructor(client, contextPath, autoCaptureConfig, clientFactory, maxRetries) {
|
|
3721
4176
|
this.client = client;
|
|
3722
4177
|
this.contextPath = contextPath ?? "";
|
|
4178
|
+
this.autoCaptureConfig = autoCaptureConfig ?? null;
|
|
4179
|
+
this.clientFactory = clientFactory ?? null;
|
|
4180
|
+
this.maxRetries = maxRetries ?? DEFAULT_RETRY_COUNT;
|
|
4181
|
+
}
|
|
4182
|
+
async reconnectClient() {
|
|
4183
|
+
if (!this.clientFactory) {
|
|
4184
|
+
throw new Error("no client factory for reconnect");
|
|
4185
|
+
}
|
|
4186
|
+
this.client.disconnect();
|
|
4187
|
+
this.client = await this.clientFactory();
|
|
4188
|
+
}
|
|
4189
|
+
async withRetry(fn) {
|
|
4190
|
+
for (let attempt = 0; ; attempt++) {
|
|
4191
|
+
try {
|
|
4192
|
+
return await fn();
|
|
4193
|
+
} catch (err) {
|
|
4194
|
+
const code = err.code;
|
|
4195
|
+
if (code !== "REQUEST_TIMEOUT" || attempt >= this.maxRetries || !this.clientFactory) {
|
|
4196
|
+
throw err;
|
|
4197
|
+
}
|
|
4198
|
+
await this.reconnectClient();
|
|
4199
|
+
}
|
|
4200
|
+
}
|
|
3723
4201
|
}
|
|
3724
4202
|
async listen() {
|
|
3725
4203
|
const rl = createInterface2({
|
|
@@ -3733,6 +4211,8 @@ var McpServer = class {
|
|
|
3733
4211
|
});
|
|
3734
4212
|
rl.on("close", () => {
|
|
3735
4213
|
void pending.then(() => {
|
|
4214
|
+
this.autoCaptureController?.reset();
|
|
4215
|
+
this.autoCaptureController = null;
|
|
3736
4216
|
this.client.disconnect();
|
|
3737
4217
|
resolve4();
|
|
3738
4218
|
});
|
|
@@ -3847,6 +4327,9 @@ var McpServer = class {
|
|
|
3847
4327
|
if (request.method === "notifications/initialized" || request.method === "notifications/cancelled") {
|
|
3848
4328
|
if (request.method === "notifications/initialized" && !this.initialized) {
|
|
3849
4329
|
this.initialized = true;
|
|
4330
|
+
if (this.autoCaptureConfig !== null) {
|
|
4331
|
+
this.autoCaptureController = new AutoCaptureController(this.autoCaptureConfig);
|
|
4332
|
+
}
|
|
3850
4333
|
void this.runStartupHealthCheck();
|
|
3851
4334
|
}
|
|
3852
4335
|
}
|
|
@@ -3880,7 +4363,7 @@ var McpServer = class {
|
|
|
3880
4363
|
protocolVersion: "2025-11-25",
|
|
3881
4364
|
serverInfo: {
|
|
3882
4365
|
name: "kbdb",
|
|
3883
|
-
version:
|
|
4366
|
+
version: VERSION
|
|
3884
4367
|
},
|
|
3885
4368
|
capabilities: {
|
|
3886
4369
|
tools: {},
|
|
@@ -3921,11 +4404,10 @@ var McpServer = class {
|
|
|
3921
4404
|
const rawName = params["name"];
|
|
3922
4405
|
const name = typeof rawName === "string" ? rawName : "";
|
|
3923
4406
|
const args = params["arguments"] ?? {};
|
|
3924
|
-
const result = await
|
|
3925
|
-
this.client,
|
|
3926
|
-
name,
|
|
3927
|
-
args
|
|
4407
|
+
const result = await this.withRetry(
|
|
4408
|
+
() => handleToolCall(this.client, name, args)
|
|
3928
4409
|
);
|
|
4410
|
+
this.afterToolCall();
|
|
3929
4411
|
return { jsonrpc: "2.0", id, result };
|
|
3930
4412
|
}
|
|
3931
4413
|
case "resources/read": {
|
|
@@ -3933,9 +4415,8 @@ var McpServer = class {
|
|
|
3933
4415
|
const rawUri = params["uri"];
|
|
3934
4416
|
const uri = typeof rawUri === "string" ? rawUri : "";
|
|
3935
4417
|
try {
|
|
3936
|
-
const result = await
|
|
3937
|
-
this.client,
|
|
3938
|
-
uri
|
|
4418
|
+
const result = await this.withRetry(
|
|
4419
|
+
() => handleResourceRead(this.client, uri)
|
|
3939
4420
|
);
|
|
3940
4421
|
return { jsonrpc: "2.0", id, result };
|
|
3941
4422
|
} catch (err) {
|
|
@@ -4040,6 +4521,23 @@ var McpServer = class {
|
|
|
4040
4521
|
const json = JSON.stringify(response);
|
|
4041
4522
|
process.stdout.write(json + "\n");
|
|
4042
4523
|
}
|
|
4524
|
+
/**
|
|
4525
|
+
* Records a tool call in the auto-capture controller and fires
|
|
4526
|
+
* a capture when the trigger condition is met.
|
|
4527
|
+
*/
|
|
4528
|
+
afterToolCall() {
|
|
4529
|
+
const ctrl = this.autoCaptureController;
|
|
4530
|
+
const cfg = this.autoCaptureConfig;
|
|
4531
|
+
if (ctrl === null || cfg === null) return;
|
|
4532
|
+
ctrl.recordToolCall();
|
|
4533
|
+
void runAutoCapture(
|
|
4534
|
+
ctrl,
|
|
4535
|
+
cfg,
|
|
4536
|
+
this.client,
|
|
4537
|
+
this.hostCapabilities,
|
|
4538
|
+
(messages, maxTokens) => this.requestSampling(messages, maxTokens)
|
|
4539
|
+
);
|
|
4540
|
+
}
|
|
4043
4541
|
/**
|
|
4044
4542
|
* Runs an integrity check immediately after the MCP handshake
|
|
4045
4543
|
* completes. Emits a `notifications/message` warning when the
|
|
@@ -4064,8 +4562,14 @@ var McpServer = class {
|
|
|
4064
4562
|
};
|
|
4065
4563
|
|
|
4066
4564
|
// src/shared/mcp/functions/create-mcp-server.function.ts
|
|
4067
|
-
function createMcpServer(client) {
|
|
4068
|
-
return new McpServer(
|
|
4565
|
+
function createMcpServer(client, contextPath, autoCaptureConfig, clientFactory, maxRetries) {
|
|
4566
|
+
return new McpServer(
|
|
4567
|
+
client,
|
|
4568
|
+
contextPath,
|
|
4569
|
+
autoCaptureConfig,
|
|
4570
|
+
clientFactory,
|
|
4571
|
+
maxRetries
|
|
4572
|
+
);
|
|
4069
4573
|
}
|
|
4070
4574
|
|
|
4071
4575
|
// src/cli.ts
|
|
@@ -4074,17 +4578,32 @@ var baseDir = resolveBaseDir(import.meta.url, import.meta.dirname);
|
|
|
4074
4578
|
var workerScript = resolveWorkerScript(baseDir.scriptDir, runtime);
|
|
4075
4579
|
var deps = {
|
|
4076
4580
|
isStdinTty,
|
|
4581
|
+
isStdoutTty: () => process.stdout.isTTY,
|
|
4077
4582
|
promptUser,
|
|
4078
4583
|
createWorkerClient: (opts) => createWorkerClient({ ...opts, workerScript }),
|
|
4079
4584
|
findDb,
|
|
4080
4585
|
runInit,
|
|
4081
|
-
startMcpServer: async (contextPath) => {
|
|
4586
|
+
startMcpServer: async (contextPath, clientFactory, maxRetries) => {
|
|
4082
4587
|
const client = await createWorkerClient({
|
|
4083
4588
|
contextPath,
|
|
4084
4589
|
workerScript
|
|
4085
4590
|
});
|
|
4086
4591
|
try {
|
|
4087
|
-
const
|
|
4592
|
+
const tomlPath = join9(contextPath, "worker.toml");
|
|
4593
|
+
let autoCaptureConfig;
|
|
4594
|
+
try {
|
|
4595
|
+
const tomlText = readFileSync4(tomlPath, "utf-8");
|
|
4596
|
+
const daemonConfig = parseWorkerToml(tomlText, contextPath);
|
|
4597
|
+
autoCaptureConfig = daemonConfig.autoCapture;
|
|
4598
|
+
} catch {
|
|
4599
|
+
}
|
|
4600
|
+
const server = createMcpServer(
|
|
4601
|
+
client,
|
|
4602
|
+
contextPath,
|
|
4603
|
+
autoCaptureConfig,
|
|
4604
|
+
clientFactory,
|
|
4605
|
+
maxRetries
|
|
4606
|
+
);
|
|
4088
4607
|
await server.listen();
|
|
4089
4608
|
} finally {
|
|
4090
4609
|
client.disconnect();
|