@dikolab/kbdb 0.4.3 → 0.6.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/CLA.md +68 -0
- package/LICENSE +676 -12
- package/LICENSING.md +58 -0
- package/README.md +50 -27
- package/README.md.bak +320 -0
- package/dist/README.md +320 -0
- package/dist/{chunk-BJXEVALU.mjs → chunk-QLJ33C74.mjs} +44 -44
- package/dist/chunk-QLJ33C74.mjs.map +7 -0
- package/dist/cli.cjs +550 -232
- package/dist/cli.cjs.map +4 -4
- package/dist/cli.d.ts +0 -8
- package/dist/cli.mjs +483 -154
- package/dist/cli.mjs.map +4 -4
- package/dist/kbdb-worker.cjs +36 -5
- package/dist/kbdb-worker.cjs.map +3 -3
- package/dist/mod.cjs +89 -30
- package/dist/mod.cjs.map +4 -4
- package/dist/mod.mjs +88 -30
- package/dist/mod.mjs.map +4 -4
- package/dist/src/cli.d.ts +0 -8
- package/dist/src/shared/cli/constants/defaults.constant.d.ts +1 -1
- package/dist/src/shared/cli/constants/recfile-command-field-map.constant.d.ts +8 -0
- package/dist/src/shared/cli/constants/recfile-field-maps.constant.d.ts +21 -0
- package/dist/src/shared/cli/functions/compute-directory-level.function.d.ts +14 -0
- package/dist/src/shared/cli/functions/dispatch-command.function.d.ts +2 -28
- package/dist/src/shared/cli/functions/format-command-output.function.d.ts +14 -0
- package/dist/src/shared/cli/functions/format-db-not-found.function.d.ts +20 -0
- package/dist/src/shared/cli/functions/format-output.function.d.ts +1 -0
- package/dist/src/shared/cli/functions/format-recfile-output.function.d.ts +11 -0
- package/dist/src/shared/cli/functions/render-recfile-value.function.d.ts +11 -0
- package/dist/src/shared/cli/functions/resolve-db-dir.function.d.ts +24 -0
- package/dist/src/shared/cli/functions/resolve-recfile-key.function.d.ts +9 -0
- package/dist/src/shared/cli/functions/run-db-path.function.d.ts +10 -0
- package/dist/src/shared/cli/functions/run-export.function.d.ts +8 -4
- package/dist/src/shared/cli/functions/run-import.function.d.ts +4 -0
- package/dist/src/shared/cli/functions/run-learn.function.d.ts +12 -0
- package/dist/src/shared/cli/index.d.ts +4 -1
- package/dist/src/shared/cli/typings/cli-options.interface.d.ts +9 -2
- package/dist/src/shared/cli/typings/db-resolution.interface.d.ts +9 -0
- package/dist/src/shared/cli/typings/recfile-field-map.type.d.ts +10 -0
- package/dist/src/shared/version/constants/version.constant.d.ts +1 -1
- package/dist/src/shared/wasm-codec/functions/decode-option-u8.function.d.ts +9 -0
- package/dist/src/shared/wasm-codec/functions/decode-section-records.function.d.ts +4 -0
- package/dist/src/shared/wasm-codec/functions/encode-add-section-params.function.d.ts +2 -45
- package/dist/src/shared/wasm-codec/functions/encode-update-section-params.function.d.ts +10 -1
- package/dist/src/shared/wasm-codec/index.d.ts +1 -1
- package/dist/src/shared/wasm-codec/typings/add-section-input.model.d.ts +6 -0
- package/dist/src/shared/wasm-codec/typings/section-record-trailing.model.d.ts +5 -0
- package/dist/src/shared/wasm-codec/typings/section-record.model.d.ts +5 -0
- package/dist/src/shared/worker-client/functions/build-deno-permissions.function.d.ts +11 -0
- package/dist/src/shared/worker-client/functions/read-crash-reason.function.d.ts +9 -0
- package/dist/src/shared/worker-client/functions/resolve-spawn-script.function.d.ts +6 -0
- package/dist/src/shared/worker-client/functions/spawn-daemon.function.d.ts +1 -1
- package/dist/src/shared/worker-client/typings/section.model.d.ts +5 -0
- package/dist/src/shared/worker-client/typings/worker-client-options.interface.d.ts +0 -6
- package/dist/src/shared/worker-daemon/functions/count-files.function.d.ts +12 -0
- package/dist/src/shared/worker-daemon/functions/handle-import.function.d.ts +2 -0
- package/dist/src/shared/worker-daemon/functions/handle-read-ops.function.d.ts +8 -0
- package/dist/src/shared/worker-daemon/functions/validate-daemon-path.function.d.ts +15 -0
- package/dist/src/worker.d.ts +0 -7
- package/dist/wasm/default-embedding/kbdb_default_embedding_bg.wasm +0 -0
- package/dist/wasm/default-embedding/package.json +1 -1
- package/dist/wasm/fs-database/kbdb_fs_database_bg.wasm +0 -0
- package/dist/wasm/fs-database/package.json +1 -1
- package/dist/wasm/fs-migration/kbdb_fs_migration_bg.wasm +0 -0
- package/dist/wasm/fs-migration/package.json +1 -1
- package/dist/wasm/kb-worker/kbdb_worker_bg.wasm +0 -0
- package/dist/wasm/kb-worker/package.json +1 -1
- package/dist/wasm/query-parser/kbdb_query_parser_bg.wasm +0 -0
- package/dist/wasm/query-parser/package.json +1 -1
- package/dist/worker.d.ts +0 -7
- package/dist/worker.mjs +37 -6
- package/dist/worker.mjs.map +3 -3
- package/package.json +13 -12
- package/dist/chunk-BJXEVALU.mjs.map +0 -7
- package/dist/src/shared/cli/functions/resolve-raw-db-path.function.d.ts +0 -10
- package/docs/details/README.md +0 -30
- package/docs/details/agent-tooling.md +0 -132
- package/docs/details/cli.md +0 -777
- package/docs/details/knowledge-base.md +0 -180
- package/docs/details/library-api.md +0 -495
- package/docs/details/mcp-server.md +0 -482
- package/docs/details/search-and-ranking.md +0 -575
- package/docs/details/storage.md +0 -531
- package/docs/goals/agents.md +0 -751
- package/docs/goals/architecture.svg +0 -198
- package/docs/goals/auto-capture.md +0 -378
- package/docs/goals/benchmarking.md +0 -296
- package/docs/goals/cli.md +0 -2654
- package/docs/goals/concurrency.md +0 -259
- package/docs/goals/content-composer.md +0 -609
- package/docs/goals/content-parser.md +0 -279
- package/docs/goals/database.md +0 -1679
- package/docs/goals/document.md +0 -368
- package/docs/goals/hybrid-search.md +0 -465
- package/docs/goals/mcp.md +0 -1541
- package/docs/goals/overview.md +0 -124
- package/docs/goals/query-parser.md +0 -373
- package/docs/goals/query-result.md +0 -860
- package/docs/goals/semantic-dedup.md +0 -233
- package/docs/goals/skills.md +0 -810
- package/docs/goals/sync.md +0 -217
- package/docs/goals/worker-client.md +0 -1005
- package/docs/goals/worker-daemon.md +0 -1547
- package/docs/modules/overview.md +0 -319
- package/docs/modules/rust/embedding/module.md +0 -91
- package/docs/modules/rust/fs-database/functions/document.md +0 -48
- package/docs/modules/rust/fs-database/functions/integrity.md +0 -79
- package/docs/modules/rust/fs-database/functions/io.md +0 -87
- package/docs/modules/rust/fs-database/functions/module.md +0 -22
- package/docs/modules/rust/fs-database/functions/query-exec.md +0 -44
- package/docs/modules/rust/fs-database/functions/section.md +0 -76
- package/docs/modules/rust/fs-database/indexes/btree.md +0 -35
- package/docs/modules/rust/fs-database/indexes/corpus.md +0 -61
- package/docs/modules/rust/fs-database/indexes/inverted.md +0 -60
- package/docs/modules/rust/fs-database/indexes/module.md +0 -64
- package/docs/modules/rust/fs-database/indexes/positional.md +0 -39
- package/docs/modules/rust/fs-database/indexes/vectors.md +0 -110
- package/docs/modules/rust/fs-database/module.md +0 -150
- package/docs/modules/rust/fs-database/types/catalog.md +0 -53
- package/docs/modules/rust/fs-database/types/module.md +0 -12
- package/docs/modules/rust/fs-database/types/section.md +0 -127
- package/docs/modules/rust/kb-worker/functions/compose.md +0 -57
- package/docs/modules/rust/kb-worker/functions/module.md +0 -20
- package/docs/modules/rust/kb-worker/functions/retrieve.md +0 -194
- package/docs/modules/rust/kb-worker/functions/search.md +0 -173
- package/docs/modules/rust/kb-worker/functions/write.md +0 -172
- package/docs/modules/rust/kb-worker/module.md +0 -171
- package/docs/modules/rust/kb-worker/types/cache.md +0 -90
- package/docs/modules/rust/kb-worker/types/context.md +0 -26
- package/docs/modules/rust/kb-worker/types/module.md +0 -16
- package/docs/modules/rust/kb-worker/types/status.md +0 -29
- package/docs/modules/rust/kb-worker/types/token.md +0 -50
- package/docs/modules/rust/overview.md +0 -531
- package/docs/modules/rust/query-parser/functions/extract.md +0 -56
- package/docs/modules/rust/query-parser/functions/module.md +0 -18
- package/docs/modules/rust/query-parser/functions/parse.md +0 -33
- package/docs/modules/rust/query-parser/functions/pipeline.md +0 -9
- package/docs/modules/rust/query-parser/functions/rank.md +0 -85
- package/docs/modules/rust/query-parser/module.md +0 -131
- package/docs/modules/rust/query-parser/types/cache.md +0 -39
- package/docs/modules/rust/query-parser/types/keyphrase.md +0 -71
- package/docs/modules/rust/query-parser/types/module.md +0 -18
- package/docs/modules/rust/query-parser/types/token.md +0 -46
- package/docs/modules/rust/shared/content-composer/functions.md +0 -114
- package/docs/modules/rust/shared/content-composer/module.md +0 -147
- package/docs/modules/rust/shared/content-composer/types.md +0 -93
- package/docs/modules/rust/shared/content-parser/functions.md +0 -72
- package/docs/modules/rust/shared/content-parser/module.md +0 -99
- package/docs/modules/rust/shared/content-parser/types.md +0 -71
- package/docs/modules/rust/shared/corpus/module.md +0 -24
- package/docs/modules/rust/shared/corpus/types.md +0 -141
- package/docs/modules/rust/shared/document/module.md +0 -25
- package/docs/modules/rust/shared/document/types.md +0 -154
- package/docs/modules/rust/shared/encode/module.md +0 -22
- package/docs/modules/rust/shared/encode/traits.md +0 -304
- package/docs/modules/rust/shared/error/module.md +0 -28
- package/docs/modules/rust/shared/error/types.md +0 -302
- package/docs/modules/rust/shared/kbid/module.md +0 -24
- package/docs/modules/rust/shared/kbid/types.md +0 -147
- package/docs/modules/rust/shared/memory/functions.md +0 -209
- package/docs/modules/rust/shared/memory/module.md +0 -24
- package/docs/modules/rust/shared/module.md +0 -196
- package/docs/modules/rust/shared/pipeline/functions.md +0 -141
- package/docs/modules/rust/shared/pipeline/module.md +0 -62
- package/docs/modules/rust/shared/pipeline/types.md +0 -43
- package/docs/modules/rust/shared/query/module.md +0 -27
- package/docs/modules/rust/shared/query/types.md +0 -236
- package/docs/modules/rust/shared/section/module.md +0 -25
- package/docs/modules/rust/shared/section/types.md +0 -294
- package/docs/modules/rust/shared/term/module.md +0 -25
- package/docs/modules/rust/shared/term/types.md +0 -139
- package/docs/modules/typescript/cli.md +0 -131
- package/docs/modules/typescript/kbdb-worker.md +0 -150
- package/docs/modules/typescript/overview.md +0 -400
- package/docs/modules/typescript/shared/auto-capture/module.md +0 -73
- package/docs/modules/typescript/shared/cli/constants.md +0 -23
- package/docs/modules/typescript/shared/cli/functions.md +0 -809
- package/docs/modules/typescript/shared/cli/module.md +0 -138
- package/docs/modules/typescript/shared/cli/typings.md +0 -197
- package/docs/modules/typescript/shared/embedding/functions.md +0 -200
- package/docs/modules/typescript/shared/embedding/module.md +0 -77
- package/docs/modules/typescript/shared/embedding/typings.md +0 -124
- package/docs/modules/typescript/shared/hash/functions.md +0 -20
- package/docs/modules/typescript/shared/hash/module.md +0 -21
- package/docs/modules/typescript/shared/log/constants.md +0 -82
- package/docs/modules/typescript/shared/log/functions.md +0 -131
- package/docs/modules/typescript/shared/log/module.md +0 -153
- package/docs/modules/typescript/shared/log/typings.md +0 -82
- package/docs/modules/typescript/shared/mcp/classes.md +0 -179
- package/docs/modules/typescript/shared/mcp/functions.md +0 -386
- package/docs/modules/typescript/shared/mcp/module.md +0 -160
- package/docs/modules/typescript/shared/mcp/typings.md +0 -529
- package/docs/modules/typescript/shared/module.md +0 -110
- package/docs/modules/typescript/shared/platform/functions.md +0 -42
- package/docs/modules/typescript/shared/platform/module.md +0 -25
- package/docs/modules/typescript/shared/runtime/functions.md +0 -26
- package/docs/modules/typescript/shared/runtime/module.md +0 -27
- package/docs/modules/typescript/shared/runtime/typings.md +0 -13
- package/docs/modules/typescript/shared/version/module.md +0 -27
- package/docs/modules/typescript/shared/wasm-codec/functions.md +0 -391
- package/docs/modules/typescript/shared/wasm-codec/module.md +0 -98
- package/docs/modules/typescript/shared/worker-client/classes.md +0 -264
- package/docs/modules/typescript/shared/worker-client/functions.md +0 -175
- package/docs/modules/typescript/shared/worker-client/module.md +0 -92
- package/docs/modules/typescript/shared/worker-client/typings.md +0 -511
- package/docs/modules/typescript/shared/worker-daemon/classes.md +0 -239
- package/docs/modules/typescript/shared/worker-daemon/constants.md +0 -37
- package/docs/modules/typescript/shared/worker-daemon/functions.md +0 -234
- package/docs/modules/typescript/shared/worker-daemon/module.md +0 -118
- package/docs/modules/typescript/shared/worker-daemon/typings.md +0 -132
- package/docs/overview.md +0 -191
- package/docs/release-notes/0.1.0.md +0 -189
- package/docs/release-notes/0.1.1.md +0 -46
- package/docs/release-notes/0.1.2.md +0 -38
- package/docs/release-notes/0.1.3.md +0 -42
- package/docs/release-notes/0.2.0.md +0 -147
- package/docs/release-notes/0.3.0.md +0 -89
- package/docs/release-notes/0.3.1.md +0 -121
- package/docs/release-notes/0.3.2.md +0 -64
- package/docs/release-notes/0.4.0.md +0 -80
- package/docs/release-notes/0.4.1.md +0 -149
- package/docs/release-notes/0.4.3.md +0 -60
- package/docs/release-notes/README.md +0 -13
package/dist/cli.mjs
CHANGED
|
@@ -7,10 +7,9 @@ import {
|
|
|
7
7
|
log,
|
|
8
8
|
parseLogLevel,
|
|
9
9
|
parseWorkerToml,
|
|
10
|
-
resolveBaseDir,
|
|
11
10
|
resolveLogLevel,
|
|
12
11
|
resolveWorkerScript
|
|
13
|
-
} from "./chunk-
|
|
12
|
+
} from "./chunk-QLJ33C74.mjs";
|
|
14
13
|
|
|
15
14
|
// src/cli.ts
|
|
16
15
|
import { readFileSync as readFileSync4 } from "node:fs";
|
|
@@ -18,7 +17,7 @@ import { join as join9 } from "node:path";
|
|
|
18
17
|
|
|
19
18
|
// src/shared/cli/constants/defaults.constant.ts
|
|
20
19
|
var DEFAULT_LIMIT = 20;
|
|
21
|
-
var DEFAULT_FORMAT = "
|
|
20
|
+
var DEFAULT_FORMAT = "rec";
|
|
22
21
|
var DEFAULT_MODE = "sections";
|
|
23
22
|
var SUPPORTED_EXTENSIONS = [
|
|
24
23
|
".md",
|
|
@@ -130,6 +129,18 @@ function parseArgs(argv) {
|
|
|
130
129
|
case "--replace":
|
|
131
130
|
options.replace = true;
|
|
132
131
|
break;
|
|
132
|
+
case "--level":
|
|
133
|
+
case "--weight": {
|
|
134
|
+
const raw = argv[++i];
|
|
135
|
+
const n = Number(raw);
|
|
136
|
+
if (!Number.isInteger(n) || n < 1 || n > 6) {
|
|
137
|
+
throw new Error(
|
|
138
|
+
`Invalid level "${raw}": must be integer 1-6`
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
options.level = n;
|
|
142
|
+
break;
|
|
143
|
+
}
|
|
133
144
|
case "--depth":
|
|
134
145
|
case "-d":
|
|
135
146
|
options.depth = Math.min(
|
|
@@ -176,18 +187,8 @@ function parseArgs(argv) {
|
|
|
176
187
|
return options;
|
|
177
188
|
}
|
|
178
189
|
|
|
179
|
-
// src/shared/cli/functions/format-output.function.ts
|
|
180
|
-
function
|
|
181
|
-
switch (format) {
|
|
182
|
-
case "json":
|
|
183
|
-
return JSON.stringify(paged, null, 2);
|
|
184
|
-
case "text":
|
|
185
|
-
return formatText(paged);
|
|
186
|
-
case "mcp":
|
|
187
|
-
return formatMcp(paged);
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
function formatText(paged) {
|
|
190
|
+
// src/shared/cli/functions/format-text-output.function.ts
|
|
191
|
+
function formatTextOutput(paged) {
|
|
191
192
|
if (paged.items.length === 0) return "No results found.";
|
|
192
193
|
const start = paged.offset + 1;
|
|
193
194
|
const end = paged.offset + paged.items.length;
|
|
@@ -207,7 +208,9 @@ Showing ${String(start)}-${String(end)} of ${String(paged.total)} results`;
|
|
|
207
208
|
Use --offset ${String(end)} to see more` : "";
|
|
208
209
|
return lines + footer + hint;
|
|
209
210
|
}
|
|
210
|
-
|
|
211
|
+
|
|
212
|
+
// src/shared/cli/functions/format-mcp-output.function.ts
|
|
213
|
+
function formatMcpOutput(paged) {
|
|
211
214
|
const mcpItems = paged.items.map((r) => ({
|
|
212
215
|
kbid: r.kbid,
|
|
213
216
|
heading: r.heading,
|
|
@@ -236,6 +239,168 @@ function formatMcp(paged) {
|
|
|
236
239
|
);
|
|
237
240
|
}
|
|
238
241
|
|
|
242
|
+
// src/shared/cli/functions/resolve-recfile-key.function.ts
|
|
243
|
+
function resolveRecfileKey(obj, key) {
|
|
244
|
+
const parts = key.split(".");
|
|
245
|
+
let current = obj;
|
|
246
|
+
for (const part of parts) {
|
|
247
|
+
if (current == null || typeof current !== "object") {
|
|
248
|
+
return void 0;
|
|
249
|
+
}
|
|
250
|
+
current = current[part];
|
|
251
|
+
}
|
|
252
|
+
return current;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// src/shared/cli/functions/render-recfile-value.function.ts
|
|
256
|
+
function renderRecfileValue(value) {
|
|
257
|
+
if (value === null || value === void 0) return null;
|
|
258
|
+
if (Array.isArray(value)) {
|
|
259
|
+
if (value.length === 0) return null;
|
|
260
|
+
return value.map(String).join(", ");
|
|
261
|
+
}
|
|
262
|
+
if (typeof value === "boolean") return value ? "true" : "false";
|
|
263
|
+
if (typeof value === "number") return String(value);
|
|
264
|
+
if (typeof value === "string") return value === "" ? null : value;
|
|
265
|
+
return JSON.stringify(value);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// src/shared/cli/functions/format-recfile-output.function.ts
|
|
269
|
+
function formatRecfileOutput(data, fields) {
|
|
270
|
+
const records = Array.isArray(data) ? data : [data];
|
|
271
|
+
return records.map((record) => formatRecord(record, fields)).filter(Boolean).join("\n\n");
|
|
272
|
+
}
|
|
273
|
+
function formatRecord(record, fields) {
|
|
274
|
+
if (!record || typeof record !== "object") return "";
|
|
275
|
+
const lines = [];
|
|
276
|
+
for (const [field, key] of fields) {
|
|
277
|
+
const value = resolveRecfileKey(record, key);
|
|
278
|
+
const rendered = renderRecfileValue(value);
|
|
279
|
+
if (rendered === null) continue;
|
|
280
|
+
lines.push(formatField(field, rendered));
|
|
281
|
+
}
|
|
282
|
+
return lines.join("\n");
|
|
283
|
+
}
|
|
284
|
+
function formatField(field, value) {
|
|
285
|
+
const lines = value.split("\n");
|
|
286
|
+
if (lines.length === 1) return `${field}: ${lines[0]}`;
|
|
287
|
+
return [
|
|
288
|
+
`${field}: ${lines[0]}`,
|
|
289
|
+
...lines.slice(1).map((line) => `+ ${line}`)
|
|
290
|
+
].join("\n");
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// src/shared/cli/constants/recfile-field-maps.constant.ts
|
|
294
|
+
var SEARCH_FIELDS = [
|
|
295
|
+
["kbid", "kbid"],
|
|
296
|
+
["heading", "heading"],
|
|
297
|
+
["score", "score"],
|
|
298
|
+
["confidence", "confidence"],
|
|
299
|
+
["type", "type"],
|
|
300
|
+
["terms", "matched_terms"],
|
|
301
|
+
["fields", "matched_fields"],
|
|
302
|
+
["snippet", "snippet"],
|
|
303
|
+
["level", "level"]
|
|
304
|
+
];
|
|
305
|
+
var LEARN_FIELDS = [
|
|
306
|
+
["kbid", "kbid"],
|
|
307
|
+
["path", "path"],
|
|
308
|
+
["size", "size"],
|
|
309
|
+
["skipped", "skipped"],
|
|
310
|
+
["superseded", "superseded"],
|
|
311
|
+
["warnings", "warnings"],
|
|
312
|
+
["level", "level"]
|
|
313
|
+
];
|
|
314
|
+
var UNLEARN_FIELDS = [
|
|
315
|
+
["removed", "removed"],
|
|
316
|
+
["elapsed_ms", "elapsed_ms"]
|
|
317
|
+
];
|
|
318
|
+
var RECALL_FIELDS = [
|
|
319
|
+
["kbid", "kbid"],
|
|
320
|
+
["heading", "heading"],
|
|
321
|
+
["type", "type"],
|
|
322
|
+
["docids", "docids"],
|
|
323
|
+
["content", "content"],
|
|
324
|
+
["level", "level"]
|
|
325
|
+
];
|
|
326
|
+
var EXPORT_FIELDS = [
|
|
327
|
+
["path", "path"],
|
|
328
|
+
["sections", "sections_exported"],
|
|
329
|
+
["documents", "documents_exported"],
|
|
330
|
+
["elapsed_ms", "elapsed_ms"]
|
|
331
|
+
];
|
|
332
|
+
var IMPORT_FIELDS = [
|
|
333
|
+
["sections", "sections_imported"],
|
|
334
|
+
["documents", "documents_imported"],
|
|
335
|
+
["rebuilt", "rebuilt"],
|
|
336
|
+
["dry_run", "dry_run"],
|
|
337
|
+
["elapsed_ms", "elapsed_ms"]
|
|
338
|
+
];
|
|
339
|
+
var STATUS_FIELDS = [
|
|
340
|
+
["initialized", "initialized"],
|
|
341
|
+
["path", "contextPath"],
|
|
342
|
+
["content_cached", "cacheStats.contentCount"],
|
|
343
|
+
["index_cached", "cacheStats.indexCount"]
|
|
344
|
+
];
|
|
345
|
+
var CHECK_FIELDS = [
|
|
346
|
+
["ok", "ok"],
|
|
347
|
+
["sections_checked", "sections_checked"],
|
|
348
|
+
["documents_checked", "documents_checked"],
|
|
349
|
+
["references_checked", "references_checked"],
|
|
350
|
+
["elapsed_ms", "elapsed_ms"]
|
|
351
|
+
];
|
|
352
|
+
var GC_FIELDS = [
|
|
353
|
+
["removed_sections", "removed_sections"],
|
|
354
|
+
["removed_bytes", "removed_bytes"],
|
|
355
|
+
["elapsed_ms", "elapsed_ms"]
|
|
356
|
+
];
|
|
357
|
+
var REBUILD_FIELDS = [
|
|
358
|
+
["sections_reindexed", "sections_reindexed"],
|
|
359
|
+
["terms_indexed", "terms_indexed"],
|
|
360
|
+
["elapsed_ms", "elapsed_ms"]
|
|
361
|
+
];
|
|
362
|
+
|
|
363
|
+
// src/shared/cli/functions/format-output.function.ts
|
|
364
|
+
function formatOutput(paged, format) {
|
|
365
|
+
switch (format) {
|
|
366
|
+
case "json":
|
|
367
|
+
return JSON.stringify(paged, null, 2);
|
|
368
|
+
case "text":
|
|
369
|
+
return formatTextOutput(paged);
|
|
370
|
+
case "rec":
|
|
371
|
+
return formatRecfileOutput(paged.items, SEARCH_FIELDS);
|
|
372
|
+
case "mcp":
|
|
373
|
+
return formatMcpOutput(paged);
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
// src/shared/cli/constants/recfile-command-field-map.constant.ts
|
|
378
|
+
var RECFILE_COMMAND_FIELD_MAP = {
|
|
379
|
+
learn: LEARN_FIELDS,
|
|
380
|
+
unlearn: UNLEARN_FIELDS,
|
|
381
|
+
recall: RECALL_FIELDS,
|
|
382
|
+
export: EXPORT_FIELDS,
|
|
383
|
+
import: IMPORT_FIELDS,
|
|
384
|
+
status: STATUS_FIELDS,
|
|
385
|
+
check: CHECK_FIELDS,
|
|
386
|
+
gc: GC_FIELDS,
|
|
387
|
+
rebuild: REBUILD_FIELDS
|
|
388
|
+
};
|
|
389
|
+
|
|
390
|
+
// src/shared/cli/functions/format-command-output.function.ts
|
|
391
|
+
function formatCommandOutput(data, format, command) {
|
|
392
|
+
switch (format) {
|
|
393
|
+
case "rec": {
|
|
394
|
+
const fields = RECFILE_COMMAND_FIELD_MAP[command];
|
|
395
|
+
return fields ? formatRecfileOutput(data, fields) : JSON.stringify(data, null, 2);
|
|
396
|
+
}
|
|
397
|
+
case "json":
|
|
398
|
+
case "text":
|
|
399
|
+
case "mcp":
|
|
400
|
+
return JSON.stringify(data, null, 2);
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
239
404
|
// src/shared/cli/functions/run-search.function.ts
|
|
240
405
|
async function runSearch(client, options) {
|
|
241
406
|
log("searching: " + options.args.join(" "), 0 /* DEBUG */);
|
|
@@ -332,15 +497,64 @@ async function runRecall(client, options) {
|
|
|
332
497
|
}
|
|
333
498
|
|
|
334
499
|
// src/shared/cli/functions/run-export.function.ts
|
|
500
|
+
import { resolve } from "node:path";
|
|
335
501
|
async function runExport(client, options) {
|
|
336
502
|
log("running export", 0 /* DEBUG */);
|
|
337
|
-
const path = options.args[0];
|
|
503
|
+
const path = options.args.length > 0 ? resolve(process.cwd(), options.args[0]) : void 0;
|
|
338
504
|
return client.export({ path });
|
|
339
505
|
}
|
|
340
506
|
|
|
341
507
|
// src/shared/cli/functions/run-learn.function.ts
|
|
342
|
-
import { readFileSync
|
|
508
|
+
import { readFileSync } from "node:fs";
|
|
509
|
+
|
|
510
|
+
// src/shared/cli/functions/collect-files.function.ts
|
|
511
|
+
import { readdirSync, statSync } from "node:fs";
|
|
343
512
|
import { basename, extname, join } from "node:path";
|
|
513
|
+
function collectFiles(target) {
|
|
514
|
+
try {
|
|
515
|
+
const stat = statSync(target);
|
|
516
|
+
if (stat.isFile()) {
|
|
517
|
+
return isSupportedFile(target) ? [target] : [];
|
|
518
|
+
}
|
|
519
|
+
if (stat.isDirectory()) {
|
|
520
|
+
return scanDirectory(target);
|
|
521
|
+
}
|
|
522
|
+
} catch {
|
|
523
|
+
}
|
|
524
|
+
return [];
|
|
525
|
+
}
|
|
526
|
+
function scanDirectory(dir) {
|
|
527
|
+
const files = [];
|
|
528
|
+
for (const entry of readdirSync(dir)) {
|
|
529
|
+
if (entry.startsWith(".")) continue;
|
|
530
|
+
const full = join(dir, entry);
|
|
531
|
+
try {
|
|
532
|
+
const stat = statSync(full);
|
|
533
|
+
if (stat.isDirectory()) {
|
|
534
|
+
files.push(...scanDirectory(full));
|
|
535
|
+
} else if (isSupportedFile(full)) {
|
|
536
|
+
files.push(full);
|
|
537
|
+
}
|
|
538
|
+
} catch {
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
return files;
|
|
542
|
+
}
|
|
543
|
+
function isSupportedFile(path) {
|
|
544
|
+
if (basename(path).startsWith(".")) return false;
|
|
545
|
+
const ext = extname(path).toLowerCase();
|
|
546
|
+
return SUPPORTED_EXTENSIONS.includes(ext);
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
// src/shared/cli/functions/compute-directory-level.function.ts
|
|
550
|
+
var MAX_LEVEL = 6;
|
|
551
|
+
function computeDirectoryLevel(filePath, basePath) {
|
|
552
|
+
const relative = filePath.slice(basePath.length).replace(/^\//, "");
|
|
553
|
+
const depth = relative.split("/").length - 1;
|
|
554
|
+
return Math.min(depth + 1, MAX_LEVEL);
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
// src/shared/cli/functions/run-learn.function.ts
|
|
344
558
|
async function runLearn(client, options, io) {
|
|
345
559
|
log(
|
|
346
560
|
"learning " + options.args.length.toString() + " targets",
|
|
@@ -361,7 +575,8 @@ async function runLearn(client, options, io) {
|
|
|
361
575
|
description: options.description,
|
|
362
576
|
docid: options.docid,
|
|
363
577
|
tags: options.tags,
|
|
364
|
-
replace: options.replace
|
|
578
|
+
replace: options.replace,
|
|
579
|
+
level: options.level
|
|
365
580
|
});
|
|
366
581
|
results.push(mapAddSectionResult(result, "-", content.length));
|
|
367
582
|
continue;
|
|
@@ -377,7 +592,8 @@ async function runLearn(client, options, io) {
|
|
|
377
592
|
docid: options.docid,
|
|
378
593
|
tags: options.tags,
|
|
379
594
|
replace: options.replace,
|
|
380
|
-
sourcePath: file
|
|
595
|
+
sourcePath: file,
|
|
596
|
+
level: options.level ?? computeDirectoryLevel(file, target)
|
|
381
597
|
});
|
|
382
598
|
results.push(
|
|
383
599
|
mapAddSectionResult(result, file, content.length)
|
|
@@ -402,52 +618,17 @@ function mapAddSectionResult(result, path, size) {
|
|
|
402
618
|
};
|
|
403
619
|
}
|
|
404
620
|
function readProcessStdin() {
|
|
405
|
-
return new Promise((
|
|
621
|
+
return new Promise((resolve7) => {
|
|
406
622
|
const chunks = [];
|
|
407
623
|
process.stdin.on("data", (chunk) => {
|
|
408
624
|
chunks.push(chunk);
|
|
409
625
|
});
|
|
410
626
|
process.stdin.on("end", () => {
|
|
411
|
-
|
|
627
|
+
resolve7(Buffer.concat(chunks).toString("utf-8"));
|
|
412
628
|
});
|
|
413
629
|
process.stdin.resume();
|
|
414
630
|
});
|
|
415
631
|
}
|
|
416
|
-
function collectFiles(target) {
|
|
417
|
-
try {
|
|
418
|
-
const stat = statSync(target);
|
|
419
|
-
if (stat.isFile()) {
|
|
420
|
-
return isSupportedFile(target) ? [target] : [];
|
|
421
|
-
}
|
|
422
|
-
if (stat.isDirectory()) {
|
|
423
|
-
return scanDirectory(target);
|
|
424
|
-
}
|
|
425
|
-
} catch {
|
|
426
|
-
}
|
|
427
|
-
return [];
|
|
428
|
-
}
|
|
429
|
-
function scanDirectory(dir) {
|
|
430
|
-
const files = [];
|
|
431
|
-
for (const entry of readdirSync(dir)) {
|
|
432
|
-
if (entry.startsWith(".")) continue;
|
|
433
|
-
const full = join(dir, entry);
|
|
434
|
-
try {
|
|
435
|
-
const stat = statSync(full);
|
|
436
|
-
if (stat.isDirectory()) {
|
|
437
|
-
files.push(...scanDirectory(full));
|
|
438
|
-
} else if (isSupportedFile(full)) {
|
|
439
|
-
files.push(full);
|
|
440
|
-
}
|
|
441
|
-
} catch {
|
|
442
|
-
}
|
|
443
|
-
}
|
|
444
|
-
return files;
|
|
445
|
-
}
|
|
446
|
-
function isSupportedFile(path) {
|
|
447
|
-
if (basename(path).startsWith(".")) return false;
|
|
448
|
-
const ext = extname(path).toLowerCase();
|
|
449
|
-
return SUPPORTED_EXTENSIONS.includes(ext);
|
|
450
|
-
}
|
|
451
632
|
|
|
452
633
|
// src/shared/cli/functions/run-unlearn.function.ts
|
|
453
634
|
async function runUnlearn(client, options) {
|
|
@@ -508,10 +689,10 @@ async function runRebuild(client, _options) {
|
|
|
508
689
|
|
|
509
690
|
// src/shared/cli/functions/find-db.function.ts
|
|
510
691
|
import { existsSync } from "node:fs";
|
|
511
|
-
import { dirname, join as join2, resolve } from "node:path";
|
|
692
|
+
import { dirname, join as join2, resolve as resolve2 } from "node:path";
|
|
512
693
|
function findDb(dbPath) {
|
|
513
694
|
if (dbPath) {
|
|
514
|
-
const target = join2(
|
|
695
|
+
const target = join2(resolve2(dbPath), ".kbdb");
|
|
515
696
|
return existsSync(target) ? target : null;
|
|
516
697
|
}
|
|
517
698
|
let current = process.cwd();
|
|
@@ -528,7 +709,7 @@ function findDb(dbPath) {
|
|
|
528
709
|
|
|
529
710
|
// src/shared/cli/functions/run-init.function.ts
|
|
530
711
|
import { existsSync as existsSync3, mkdirSync as mkdirSync2, writeFileSync as writeFileSync2 } from "node:fs";
|
|
531
|
-
import { join as join4, resolve as
|
|
712
|
+
import { join as join4, resolve as resolve3 } from "node:path";
|
|
532
713
|
|
|
533
714
|
// src/shared/cli/functions/install-default-skills.function.ts
|
|
534
715
|
import { createHash } from "node:crypto";
|
|
@@ -659,7 +840,7 @@ function createScaffold(dbDir) {
|
|
|
659
840
|
}
|
|
660
841
|
function runInit(options) {
|
|
661
842
|
log("running init", 0 /* DEBUG */);
|
|
662
|
-
const basePath = options.dbPath ?
|
|
843
|
+
const basePath = options.dbPath ? resolve3(options.dbPath) : process.cwd();
|
|
663
844
|
const dbDir = join4(basePath, ".kbdb");
|
|
664
845
|
try {
|
|
665
846
|
if (existsSync3(dbDir)) {
|
|
@@ -696,14 +877,14 @@ async function promptUser(question, io) {
|
|
|
696
877
|
return answer.trim();
|
|
697
878
|
}
|
|
698
879
|
function askReadline(question) {
|
|
699
|
-
return new Promise((
|
|
880
|
+
return new Promise((resolve7) => {
|
|
700
881
|
const rl = createInterface({
|
|
701
882
|
input: process.stdin,
|
|
702
883
|
output: process.stdout
|
|
703
884
|
});
|
|
704
885
|
rl.question(question, (answer) => {
|
|
705
886
|
rl.close();
|
|
706
|
-
|
|
887
|
+
resolve7(answer);
|
|
707
888
|
});
|
|
708
889
|
});
|
|
709
890
|
}
|
|
@@ -1033,6 +1214,7 @@ async function runAgentDelete(client, options) {
|
|
|
1033
1214
|
}
|
|
1034
1215
|
|
|
1035
1216
|
// src/shared/cli/functions/run-import.function.ts
|
|
1217
|
+
import { resolve as resolve4 } from "node:path";
|
|
1036
1218
|
async function runImport(client, options) {
|
|
1037
1219
|
const from = options.from;
|
|
1038
1220
|
if (!from) {
|
|
@@ -1041,11 +1223,24 @@ async function runImport(client, options) {
|
|
|
1041
1223
|
throw new Error(msg);
|
|
1042
1224
|
}
|
|
1043
1225
|
return client.import({
|
|
1044
|
-
from,
|
|
1226
|
+
from: resolve4(process.cwd(), from),
|
|
1045
1227
|
dryRun: options.dryRun === true
|
|
1046
1228
|
});
|
|
1047
1229
|
}
|
|
1048
1230
|
|
|
1231
|
+
// src/shared/cli/functions/hydrate-search-previews.function.ts
|
|
1232
|
+
async function hydrateSearchPreviews(client, paged) {
|
|
1233
|
+
await Promise.all(
|
|
1234
|
+
paged.items.map(async (result) => {
|
|
1235
|
+
try {
|
|
1236
|
+
const contentResult = await client.content([result.kbid]);
|
|
1237
|
+
result.preview = contentResult.data;
|
|
1238
|
+
} catch {
|
|
1239
|
+
}
|
|
1240
|
+
})
|
|
1241
|
+
);
|
|
1242
|
+
}
|
|
1243
|
+
|
|
1049
1244
|
// src/shared/cli/functions/dispatch-command.function.ts
|
|
1050
1245
|
async function dispatchCommand(client, options, io) {
|
|
1051
1246
|
log("dispatching command: " + options.command, 0 /* DEBUG */);
|
|
@@ -1120,7 +1315,7 @@ async function handleLearn(client, options, io) {
|
|
|
1120
1315
|
try {
|
|
1121
1316
|
const results = await runLearn(client, options, io);
|
|
1122
1317
|
return {
|
|
1123
|
-
output:
|
|
1318
|
+
output: formatCommandOutput(results, options.format, "learn"),
|
|
1124
1319
|
exitCode: 0
|
|
1125
1320
|
};
|
|
1126
1321
|
} catch (err) {
|
|
@@ -1134,7 +1329,11 @@ async function handleUnlearn(client, options) {
|
|
|
1134
1329
|
try {
|
|
1135
1330
|
const results = await runUnlearn(client, options);
|
|
1136
1331
|
return {
|
|
1137
|
-
output:
|
|
1332
|
+
output: formatCommandOutput(
|
|
1333
|
+
results,
|
|
1334
|
+
options.format,
|
|
1335
|
+
"unlearn"
|
|
1336
|
+
),
|
|
1138
1337
|
exitCode: 0
|
|
1139
1338
|
};
|
|
1140
1339
|
} catch (err) {
|
|
@@ -1171,7 +1370,7 @@ async function handleRecall(client, options) {
|
|
|
1171
1370
|
};
|
|
1172
1371
|
}
|
|
1173
1372
|
return {
|
|
1174
|
-
output:
|
|
1373
|
+
output: formatCommandOutput(result, options.format, "recall"),
|
|
1175
1374
|
exitCode: 0
|
|
1176
1375
|
};
|
|
1177
1376
|
} catch (err) {
|
|
@@ -1185,7 +1384,7 @@ async function handleExport(client, options) {
|
|
|
1185
1384
|
try {
|
|
1186
1385
|
const result = await runExport(client, options);
|
|
1187
1386
|
return {
|
|
1188
|
-
output:
|
|
1387
|
+
output: formatCommandOutput(result, options.format, "export"),
|
|
1189
1388
|
exitCode: 0
|
|
1190
1389
|
};
|
|
1191
1390
|
} catch (err) {
|
|
@@ -1199,7 +1398,7 @@ async function handleImport(client, options) {
|
|
|
1199
1398
|
try {
|
|
1200
1399
|
const result = await runImport(client, options);
|
|
1201
1400
|
return {
|
|
1202
|
-
output:
|
|
1401
|
+
output: formatCommandOutput(result, options.format, "import"),
|
|
1203
1402
|
exitCode: 0
|
|
1204
1403
|
};
|
|
1205
1404
|
} catch (err) {
|
|
@@ -1212,6 +1411,9 @@ async function handleImport(client, options) {
|
|
|
1212
1411
|
async function handleContent(client, options) {
|
|
1213
1412
|
try {
|
|
1214
1413
|
const result = await runContent(client, options);
|
|
1414
|
+
if (options.format === "rec") {
|
|
1415
|
+
return { output: result.data, exitCode: 0 };
|
|
1416
|
+
}
|
|
1215
1417
|
return {
|
|
1216
1418
|
output: JSON.stringify(result, null, 2),
|
|
1217
1419
|
exitCode: 0
|
|
@@ -1227,7 +1429,7 @@ async function handleCheck(client, options) {
|
|
|
1227
1429
|
try {
|
|
1228
1430
|
const result = await runCheck(client, options);
|
|
1229
1431
|
return {
|
|
1230
|
-
output:
|
|
1432
|
+
output: formatCommandOutput(result, options.format, "check"),
|
|
1231
1433
|
exitCode: result.ok ? 0 : 1
|
|
1232
1434
|
};
|
|
1233
1435
|
} catch (err) {
|
|
@@ -1241,7 +1443,7 @@ async function handleGc(client, options) {
|
|
|
1241
1443
|
try {
|
|
1242
1444
|
const result = await runGc(client, options);
|
|
1243
1445
|
return {
|
|
1244
|
-
output:
|
|
1446
|
+
output: formatCommandOutput(result, options.format, "gc"),
|
|
1245
1447
|
exitCode: 0
|
|
1246
1448
|
};
|
|
1247
1449
|
} catch (err) {
|
|
@@ -1255,7 +1457,7 @@ async function handleRebuild(client, options) {
|
|
|
1255
1457
|
try {
|
|
1256
1458
|
const result = await runRebuild(client, options);
|
|
1257
1459
|
return {
|
|
1258
|
-
output:
|
|
1460
|
+
output: formatCommandOutput(result, options.format, "rebuild"),
|
|
1259
1461
|
exitCode: 0
|
|
1260
1462
|
};
|
|
1261
1463
|
} catch (err) {
|
|
@@ -1269,7 +1471,7 @@ async function handleStatus(client, options) {
|
|
|
1269
1471
|
try {
|
|
1270
1472
|
const result = await runStatus(client, options);
|
|
1271
1473
|
return {
|
|
1272
|
-
output:
|
|
1474
|
+
output: formatCommandOutput(result, options.format, "status"),
|
|
1273
1475
|
exitCode: 0
|
|
1274
1476
|
};
|
|
1275
1477
|
} catch (err) {
|
|
@@ -1286,7 +1488,11 @@ async function handleDbMigrate(client, options) {
|
|
|
1286
1488
|
try {
|
|
1287
1489
|
const result = await runDbMigrate(client, options);
|
|
1288
1490
|
return {
|
|
1289
|
-
output:
|
|
1491
|
+
output: formatCommandOutput(
|
|
1492
|
+
result,
|
|
1493
|
+
options.format,
|
|
1494
|
+
"db-migrate"
|
|
1495
|
+
),
|
|
1290
1496
|
exitCode: 0
|
|
1291
1497
|
};
|
|
1292
1498
|
} catch (err) {
|
|
@@ -1296,20 +1502,9 @@ async function handleDbMigrate(client, options) {
|
|
|
1296
1502
|
};
|
|
1297
1503
|
}
|
|
1298
1504
|
}
|
|
1299
|
-
async function hydrateSearchPreviews(client, paged) {
|
|
1300
|
-
await Promise.all(
|
|
1301
|
-
paged.items.map(async (result) => {
|
|
1302
|
-
try {
|
|
1303
|
-
const contentResult = await client.content([result.kbid]);
|
|
1304
|
-
result.preview = contentResult.data;
|
|
1305
|
-
} catch {
|
|
1306
|
-
}
|
|
1307
|
-
})
|
|
1308
|
-
);
|
|
1309
|
-
}
|
|
1310
1505
|
|
|
1311
1506
|
// src/shared/version/constants/version.constant.ts
|
|
1312
|
-
var VERSION = "0.
|
|
1507
|
+
var VERSION = "0.6.0";
|
|
1313
1508
|
|
|
1314
1509
|
// src/shared/cli/functions/generate-help.function.ts
|
|
1315
1510
|
var KNOWN_COMMANDS = /* @__PURE__ */ new Set([
|
|
@@ -1332,7 +1527,8 @@ var KNOWN_COMMANDS = /* @__PURE__ */ new Set([
|
|
|
1332
1527
|
var GROUP_SUBCOMMANDS = {
|
|
1333
1528
|
db: [
|
|
1334
1529
|
["init", "Initialise a new .kbdb directory"],
|
|
1335
|
-
["migrate", "Migrate database to current format"]
|
|
1530
|
+
["migrate", "Migrate database to current format"],
|
|
1531
|
+
["path", "Show resolved database path"]
|
|
1336
1532
|
],
|
|
1337
1533
|
skill: [
|
|
1338
1534
|
["learn", "Create a new skill definition"],
|
|
@@ -1396,6 +1592,7 @@ Knowledge Base:
|
|
|
1396
1592
|
Database:
|
|
1397
1593
|
db init Initialise a new .kbdb directory
|
|
1398
1594
|
db migrate Migrate database to current format
|
|
1595
|
+
db path Show resolved database path
|
|
1399
1596
|
|
|
1400
1597
|
Maintenance:
|
|
1401
1598
|
check Verify knowledge base integrity
|
|
@@ -1692,6 +1889,30 @@ Output:
|
|
|
1692
1889
|
Examples:
|
|
1693
1890
|
kbdb db migrate
|
|
1694
1891
|
kbdb db migrate --db /path/to/project
|
|
1892
|
+
`,
|
|
1893
|
+
"db-path": `kbdb db path - Show resolved database path
|
|
1894
|
+
|
|
1895
|
+
Usage: kbdb db path [options]
|
|
1896
|
+
|
|
1897
|
+
Prints the absolute path of the .kbdb database
|
|
1898
|
+
that would be used by the current command
|
|
1899
|
+
context. Useful for debugging database
|
|
1900
|
+
resolution.
|
|
1901
|
+
|
|
1902
|
+
Resolution order:
|
|
1903
|
+
1. --db <path> Explicit flag
|
|
1904
|
+
2. cwd traversal Walk up from cwd
|
|
1905
|
+
3. KBDB_DB_DIR Environment variable
|
|
1906
|
+
|
|
1907
|
+
Options:
|
|
1908
|
+
--db <path> Path to .kbdb directory
|
|
1909
|
+
--format json Show full diagnostic
|
|
1910
|
+
-h, --help Show this help message
|
|
1911
|
+
|
|
1912
|
+
Examples:
|
|
1913
|
+
kbdb db path
|
|
1914
|
+
kbdb db path --format json
|
|
1915
|
+
kbdb db path --db /path/to/project
|
|
1695
1916
|
`,
|
|
1696
1917
|
"skill-learn": `kbdb skill learn - Create a new skill definition
|
|
1697
1918
|
|
|
@@ -1853,6 +2074,73 @@ function generateSubcommandHelp(command, subcommand) {
|
|
|
1853
2074
|
return generateCommandHelp(command);
|
|
1854
2075
|
}
|
|
1855
2076
|
|
|
2077
|
+
// src/shared/cli/functions/resolve-db-dir.function.ts
|
|
2078
|
+
function resolveDbDir(options, findDb2) {
|
|
2079
|
+
if (options.dbPath) {
|
|
2080
|
+
const flagResult = findDb2(options.dbPath);
|
|
2081
|
+
return flagResult ? { path: flagResult, source: "flag" } : null;
|
|
2082
|
+
}
|
|
2083
|
+
if (!options.skipTraversal) {
|
|
2084
|
+
const traversalResult = findDb2();
|
|
2085
|
+
if (traversalResult) {
|
|
2086
|
+
return { path: traversalResult, source: "traversal" };
|
|
2087
|
+
}
|
|
2088
|
+
}
|
|
2089
|
+
const envDir = process.env["KBDB_DB_DIR"];
|
|
2090
|
+
if (envDir) {
|
|
2091
|
+
const envResult = findDb2(envDir);
|
|
2092
|
+
if (envResult) {
|
|
2093
|
+
return { path: envResult, source: "env" };
|
|
2094
|
+
}
|
|
2095
|
+
}
|
|
2096
|
+
return null;
|
|
2097
|
+
}
|
|
2098
|
+
|
|
2099
|
+
// src/shared/cli/functions/format-db-not-found.function.ts
|
|
2100
|
+
function formatDbNotFound(options) {
|
|
2101
|
+
if (options.dbPath) {
|
|
2102
|
+
return `error: no .kbdb database found at ${options.dbPath}`;
|
|
2103
|
+
}
|
|
2104
|
+
const envValue = process.env["KBDB_DB_DIR"] ?? "(not set)";
|
|
2105
|
+
const lines = [
|
|
2106
|
+
"error: no .kbdb database found",
|
|
2107
|
+
" --db: (not set)"
|
|
2108
|
+
];
|
|
2109
|
+
if (!options.skipTraversal) {
|
|
2110
|
+
lines.push(` cwd: ${process.cwd()} (searched upward)`);
|
|
2111
|
+
}
|
|
2112
|
+
lines.push(` KBDB_DB_DIR: ${envValue}`);
|
|
2113
|
+
if (!options.skipTraversal) {
|
|
2114
|
+
lines.push('Run "kbdb db init" to create a database.');
|
|
2115
|
+
}
|
|
2116
|
+
return lines.join("\n");
|
|
2117
|
+
}
|
|
2118
|
+
|
|
2119
|
+
// src/shared/cli/functions/run-db-path.function.ts
|
|
2120
|
+
function runDbPath(options, findDb2) {
|
|
2121
|
+
const resolution = resolveDbDir({ dbPath: options.dbPath }, findDb2);
|
|
2122
|
+
if (!resolution) {
|
|
2123
|
+
return {
|
|
2124
|
+
error: formatDbNotFound({ dbPath: options.dbPath }),
|
|
2125
|
+
exitCode: 1
|
|
2126
|
+
};
|
|
2127
|
+
}
|
|
2128
|
+
if (options.format === "json") {
|
|
2129
|
+
const diagnostic = {
|
|
2130
|
+
path: resolution.path,
|
|
2131
|
+
source: resolution.source,
|
|
2132
|
+
flag: options.dbPath ?? null,
|
|
2133
|
+
cwd: process.cwd(),
|
|
2134
|
+
env: process.env["KBDB_DB_DIR"] ?? null
|
|
2135
|
+
};
|
|
2136
|
+
return {
|
|
2137
|
+
output: JSON.stringify(diagnostic, null, 2),
|
|
2138
|
+
exitCode: 0
|
|
2139
|
+
};
|
|
2140
|
+
}
|
|
2141
|
+
return { output: resolution.path, exitCode: 0 };
|
|
2142
|
+
}
|
|
2143
|
+
|
|
1856
2144
|
// src/shared/worker-client/classes/worker-client.class.ts
|
|
1857
2145
|
import { connect } from "node:net";
|
|
1858
2146
|
|
|
@@ -1997,7 +2285,7 @@ var WorkerClient = class {
|
|
|
1997
2285
|
* post-connect failure handlers.
|
|
1998
2286
|
*/
|
|
1999
2287
|
async tryConnect() {
|
|
2000
|
-
return new Promise((
|
|
2288
|
+
return new Promise((resolve7, reject) => {
|
|
2001
2289
|
const sock = connect(this.socketPath, () => {
|
|
2002
2290
|
sock.removeListener("error", reject);
|
|
2003
2291
|
sock.setKeepAlive(true, 3e4);
|
|
@@ -2018,7 +2306,7 @@ var WorkerClient = class {
|
|
|
2018
2306
|
this.socket = null;
|
|
2019
2307
|
});
|
|
2020
2308
|
this.socket = sock;
|
|
2021
|
-
|
|
2309
|
+
resolve7();
|
|
2022
2310
|
});
|
|
2023
2311
|
sock.on("error", reject);
|
|
2024
2312
|
sock.on("data", (chunk) => {
|
|
@@ -2346,8 +2634,8 @@ var WorkerClient = class {
|
|
|
2346
2634
|
params: { ctx: this.contextId, ...params }
|
|
2347
2635
|
};
|
|
2348
2636
|
const timeoutMs = this.requestTimeoutMs;
|
|
2349
|
-
return new Promise((
|
|
2350
|
-
const call = { resolve:
|
|
2637
|
+
return new Promise((resolve7, reject) => {
|
|
2638
|
+
const call = { resolve: resolve7, reject };
|
|
2351
2639
|
if (timeoutMs) {
|
|
2352
2640
|
call.timer = setTimeout(() => {
|
|
2353
2641
|
if (!this.pending.has(id)) return;
|
|
@@ -2416,16 +2704,11 @@ var WorkerClient = class {
|
|
|
2416
2704
|
}
|
|
2417
2705
|
};
|
|
2418
2706
|
|
|
2419
|
-
// src/shared/worker-client/functions/create-worker-client.function.ts
|
|
2420
|
-
import { existsSync as existsSync7, readFileSync as readFileSync3, unlinkSync as unlinkSync2 } from "node:fs";
|
|
2421
|
-
import { join as join8 } from "node:path";
|
|
2422
|
-
import { tmpdir as tmpdir3 } from "node:os";
|
|
2423
|
-
|
|
2424
2707
|
// src/shared/worker-client/functions/resolve-db-path.function.ts
|
|
2425
2708
|
import { existsSync as existsSync5 } from "node:fs";
|
|
2426
|
-
import { join as join6, resolve as
|
|
2709
|
+
import { join as join6, resolve as resolve5 } from "node:path";
|
|
2427
2710
|
function resolveDbPath(targetDir) {
|
|
2428
|
-
const base = targetDir ?
|
|
2711
|
+
const base = targetDir ? resolve5(targetDir) : process.cwd();
|
|
2429
2712
|
const dbPath = join6(base, ".kbdb");
|
|
2430
2713
|
if (!existsSync5(dbPath)) {
|
|
2431
2714
|
const msg = `database not found: ${dbPath}`;
|
|
@@ -2443,16 +2726,35 @@ async function computeContextId(absolutePath) {
|
|
|
2443
2726
|
|
|
2444
2727
|
// src/shared/worker-client/functions/spawn-daemon.function.ts
|
|
2445
2728
|
import { spawn } from "node:child_process";
|
|
2446
|
-
|
|
2447
|
-
|
|
2729
|
+
|
|
2730
|
+
// src/shared/worker-client/functions/build-deno-permissions.function.ts
|
|
2731
|
+
function buildDenoPermissions() {
|
|
2732
|
+
return [
|
|
2733
|
+
"--allow-read",
|
|
2734
|
+
"--allow-write",
|
|
2735
|
+
"--allow-env",
|
|
2736
|
+
"--allow-run",
|
|
2737
|
+
"--allow-sys"
|
|
2738
|
+
];
|
|
2739
|
+
}
|
|
2740
|
+
|
|
2741
|
+
// src/shared/worker-client/functions/spawn-daemon.function.ts
|
|
2742
|
+
function spawnDaemon(contextPath, runtime, workerScript) {
|
|
2743
|
+
if (!workerScript) {
|
|
2448
2744
|
const msg = "workerScript must be provided (resolve at entry point)";
|
|
2449
2745
|
log(msg, 3 /* ERROR */);
|
|
2450
2746
|
throw new Error(msg);
|
|
2451
2747
|
}
|
|
2452
|
-
const execPath =
|
|
2748
|
+
const execPath = runtime === "deno" ? "deno" : process.execPath;
|
|
2453
2749
|
const logLevelEnv = process.env["KBDB_LOG_LEVEL"];
|
|
2454
2750
|
const logArgs = logLevelEnv ? ["--log-level", logLevelEnv] : [];
|
|
2455
|
-
const args =
|
|
2751
|
+
const args = runtime === "deno" ? [
|
|
2752
|
+
"run",
|
|
2753
|
+
...buildDenoPermissions(),
|
|
2754
|
+
workerScript,
|
|
2755
|
+
contextPath,
|
|
2756
|
+
...logArgs
|
|
2757
|
+
] : [workerScript, contextPath, ...logArgs];
|
|
2456
2758
|
log(
|
|
2457
2759
|
"spawning daemon: " + execPath + " " + args.join(" "),
|
|
2458
2760
|
0 /* DEBUG */
|
|
@@ -2498,6 +2800,40 @@ async function waitForDaemon(contextId, timeoutMs = DEFAULT_TIMEOUT_MS) {
|
|
|
2498
2800
|
return false;
|
|
2499
2801
|
}
|
|
2500
2802
|
|
|
2803
|
+
// src/shared/worker-client/functions/read-crash-reason.function.ts
|
|
2804
|
+
import { existsSync as existsSync7, readFileSync as readFileSync3, unlinkSync as unlinkSync2 } from "node:fs";
|
|
2805
|
+
import { join as join8 } from "node:path";
|
|
2806
|
+
import { tmpdir as tmpdir3 } from "node:os";
|
|
2807
|
+
function readCrashReason(contextId) {
|
|
2808
|
+
const crashPath = join8(tmpdir3(), `kbdb-${contextId}.crash`);
|
|
2809
|
+
if (!existsSync7(crashPath)) {
|
|
2810
|
+
return "daemon failed to start (no details available)";
|
|
2811
|
+
}
|
|
2812
|
+
try {
|
|
2813
|
+
const raw = readFileSync3(crashPath, "utf-8");
|
|
2814
|
+
const crash = JSON.parse(raw);
|
|
2815
|
+
unlinkSync2(crashPath);
|
|
2816
|
+
return crash.error ?? "unknown error (crash log empty)";
|
|
2817
|
+
} catch {
|
|
2818
|
+
return "daemon failed to start (crash log unreadable)";
|
|
2819
|
+
}
|
|
2820
|
+
}
|
|
2821
|
+
|
|
2822
|
+
// src/shared/worker-client/functions/resolve-spawn-script.function.ts
|
|
2823
|
+
import { dirname as dirname2, resolve as resolve6 } from "node:path";
|
|
2824
|
+
import { fileURLToPath } from "node:url";
|
|
2825
|
+
function resolveSpawnScript(runtime) {
|
|
2826
|
+
return resolveWorkerScript(resolveScriptDir(runtime), runtime);
|
|
2827
|
+
}
|
|
2828
|
+
function resolveScriptDir(runtime) {
|
|
2829
|
+
if (typeof __dirname !== "undefined") return __dirname;
|
|
2830
|
+
const thisDir = dirname2(fileURLToPath(import.meta.url));
|
|
2831
|
+
if (runtime === "deno") {
|
|
2832
|
+
return resolve6(thisDir, "..", "..", "..");
|
|
2833
|
+
}
|
|
2834
|
+
return thisDir;
|
|
2835
|
+
}
|
|
2836
|
+
|
|
2501
2837
|
// src/shared/worker-client/functions/create-worker-client.function.ts
|
|
2502
2838
|
async function createWorkerClient(options) {
|
|
2503
2839
|
const dbPath = options?.contextPath ?? resolveDbPath(options?.dbPath);
|
|
@@ -2511,9 +2847,10 @@ async function createWorkerClient(options) {
|
|
|
2511
2847
|
log("no daemon found", 0 /* DEBUG */);
|
|
2512
2848
|
}
|
|
2513
2849
|
if (!daemon && options?.autoSpawn !== false) {
|
|
2514
|
-
const
|
|
2850
|
+
const runtime = detectRuntime();
|
|
2515
2851
|
log("auto-spawning daemon", 0 /* DEBUG */);
|
|
2516
|
-
|
|
2852
|
+
const workerScript = resolveSpawnScript(runtime);
|
|
2853
|
+
spawnDaemon(dbPath, runtime, workerScript);
|
|
2517
2854
|
log("waiting for daemon after spawn", 0 /* DEBUG */);
|
|
2518
2855
|
await waitForDaemon(contextId, options?.connectTimeoutMs);
|
|
2519
2856
|
daemon = discoverDaemon(contextId);
|
|
@@ -2532,20 +2869,6 @@ async function createWorkerClient(options) {
|
|
|
2532
2869
|
await client.connect();
|
|
2533
2870
|
return client;
|
|
2534
2871
|
}
|
|
2535
|
-
function readCrashReason(contextId) {
|
|
2536
|
-
const crashPath = join8(tmpdir3(), `kbdb-${contextId}.crash`);
|
|
2537
|
-
if (!existsSync7(crashPath)) {
|
|
2538
|
-
return "daemon failed to start (no details available)";
|
|
2539
|
-
}
|
|
2540
|
-
try {
|
|
2541
|
-
const raw = readFileSync3(crashPath, "utf-8");
|
|
2542
|
-
const crash = JSON.parse(raw);
|
|
2543
|
-
unlinkSync2(crashPath);
|
|
2544
|
-
return crash.error ?? "unknown error (crash log empty)";
|
|
2545
|
-
} catch {
|
|
2546
|
-
return "daemon failed to start (crash log unreadable)";
|
|
2547
|
-
}
|
|
2548
|
-
}
|
|
2549
2872
|
|
|
2550
2873
|
// src/shared/cli/functions/run-worker-stop.function.ts
|
|
2551
2874
|
async function runWorkerStop(dbDir) {
|
|
@@ -2609,11 +2932,6 @@ async function runWorkerRestart(dbDir) {
|
|
|
2609
2932
|
};
|
|
2610
2933
|
}
|
|
2611
2934
|
|
|
2612
|
-
// src/shared/cli/functions/resolve-raw-db-path.function.ts
|
|
2613
|
-
function resolveRawDbPath(options) {
|
|
2614
|
-
return options.dbPath ?? process.env["KBDB_DB_DIR"];
|
|
2615
|
-
}
|
|
2616
|
-
|
|
2617
2935
|
// src/shared/worker-client/functions/retry-on-timeout.function.ts
|
|
2618
2936
|
async function retryOnTimeout(createClient, operation, maxRetries) {
|
|
2619
2937
|
let lastError = new Error("retryOnTimeout exhausted");
|
|
@@ -2682,9 +3000,6 @@ async function runCli(argv, deps2) {
|
|
|
2682
3000
|
}
|
|
2683
3001
|
const nonInteractive = options.nonInteractive || process.env["KBDB_NON_INTERACTIVE"] === "1" || process.env["KBDB_NON_INTERACTIVE"] === "true";
|
|
2684
3002
|
const effectiveDeps = nonInteractive ? { ...deps2, isStdinTty: () => false } : deps2;
|
|
2685
|
-
if (!options.formatExplicit) {
|
|
2686
|
-
options.format = effectiveDeps.isStdoutTty() ? "text" : "json";
|
|
2687
|
-
}
|
|
2688
3003
|
if (options.command === "help" || !options.command) {
|
|
2689
3004
|
const helpCommand = options.args[0];
|
|
2690
3005
|
const helpSubcommand = options.args[1];
|
|
@@ -2700,6 +3015,9 @@ async function runCli(argv, deps2) {
|
|
|
2700
3015
|
if (options.command === "init" || options.command === "db-init") {
|
|
2701
3016
|
return handleInit(options, effectiveDeps);
|
|
2702
3017
|
}
|
|
3018
|
+
if (options.command === "db-path") {
|
|
3019
|
+
return runDbPath(options, effectiveDeps.findDb);
|
|
3020
|
+
}
|
|
2703
3021
|
if (options.command === "mcp") {
|
|
2704
3022
|
return handleMcp(options, effectiveDeps);
|
|
2705
3023
|
}
|
|
@@ -2742,14 +3060,20 @@ function resolveRetryCount(options) {
|
|
|
2742
3060
|
return options.retry ?? (parseInt(process.env["KBDB_RETRY"] ?? "", 10) || DEFAULT_RETRY_COUNT);
|
|
2743
3061
|
}
|
|
2744
3062
|
async function handleMcp(options, deps2) {
|
|
2745
|
-
const
|
|
2746
|
-
|
|
2747
|
-
|
|
3063
|
+
const resolution = resolveDbDir(
|
|
3064
|
+
{ dbPath: options.dbPath, skipTraversal: true },
|
|
3065
|
+
deps2.findDb
|
|
3066
|
+
);
|
|
3067
|
+
if (!resolution) {
|
|
2748
3068
|
return {
|
|
2749
|
-
error:
|
|
3069
|
+
error: formatDbNotFound({
|
|
3070
|
+
dbPath: options.dbPath,
|
|
3071
|
+
skipTraversal: true
|
|
3072
|
+
}),
|
|
2750
3073
|
exitCode: 1
|
|
2751
3074
|
};
|
|
2752
3075
|
}
|
|
3076
|
+
const dbDir = resolution.path;
|
|
2753
3077
|
const timeoutMs = resolveTimeoutMs(options);
|
|
2754
3078
|
const maxRetries = resolveRetryCount(options);
|
|
2755
3079
|
let client;
|
|
@@ -2788,14 +3112,17 @@ async function handleMcp(options, deps2) {
|
|
|
2788
3112
|
}
|
|
2789
3113
|
}
|
|
2790
3114
|
async function handleMigrate(options, deps2) {
|
|
2791
|
-
const
|
|
2792
|
-
|
|
2793
|
-
|
|
3115
|
+
const resolution = resolveDbDir(
|
|
3116
|
+
{ dbPath: options.dbPath },
|
|
3117
|
+
deps2.findDb
|
|
3118
|
+
);
|
|
3119
|
+
if (!resolution) {
|
|
2794
3120
|
return {
|
|
2795
|
-
error:
|
|
3121
|
+
error: formatDbNotFound({ dbPath: options.dbPath }),
|
|
2796
3122
|
exitCode: 1
|
|
2797
3123
|
};
|
|
2798
3124
|
}
|
|
3125
|
+
const dbDir = resolution.path;
|
|
2799
3126
|
let client;
|
|
2800
3127
|
try {
|
|
2801
3128
|
client = await deps2.createWorkerClient({
|
|
@@ -2833,13 +3160,18 @@ async function handleMigrate(options, deps2) {
|
|
|
2833
3160
|
}
|
|
2834
3161
|
}
|
|
2835
3162
|
async function handleDataCommand(options, deps2) {
|
|
2836
|
-
const
|
|
2837
|
-
|
|
3163
|
+
const resolution = resolveDbDir(
|
|
3164
|
+
{ dbPath: options.dbPath },
|
|
3165
|
+
deps2.findDb
|
|
3166
|
+
);
|
|
3167
|
+
let dbDir = resolution?.path ?? null;
|
|
2838
3168
|
if (!dbDir) {
|
|
2839
|
-
const basePath =
|
|
3169
|
+
const basePath = options.dbPath ?? process.cwd();
|
|
2840
3170
|
if (!deps2.isStdinTty()) {
|
|
2841
3171
|
return {
|
|
2842
|
-
error:
|
|
3172
|
+
error: formatDbNotFound({
|
|
3173
|
+
dbPath: options.dbPath
|
|
3174
|
+
}),
|
|
2843
3175
|
exitCode: 1
|
|
2844
3176
|
};
|
|
2845
3177
|
}
|
|
@@ -2887,14 +3219,17 @@ async function handleDataCommand(options, deps2) {
|
|
|
2887
3219
|
}
|
|
2888
3220
|
}
|
|
2889
3221
|
async function handleWorkerLifecycle(options, deps2, action) {
|
|
2890
|
-
const
|
|
2891
|
-
|
|
2892
|
-
|
|
3222
|
+
const resolution = resolveDbDir(
|
|
3223
|
+
{ dbPath: options.dbPath },
|
|
3224
|
+
deps2.findDb
|
|
3225
|
+
);
|
|
3226
|
+
if (!resolution) {
|
|
2893
3227
|
return {
|
|
2894
|
-
error:
|
|
3228
|
+
error: formatDbNotFound({ dbPath: options.dbPath }),
|
|
2895
3229
|
exitCode: 1
|
|
2896
3230
|
};
|
|
2897
3231
|
}
|
|
3232
|
+
const dbDir = resolution.path;
|
|
2898
3233
|
return action(dbDir);
|
|
2899
3234
|
}
|
|
2900
3235
|
async function versionGate(client, dbDir) {
|
|
@@ -4474,7 +4809,7 @@ var McpServer = class {
|
|
|
4474
4809
|
});
|
|
4475
4810
|
log("MCP server listening on stdin", 0 /* DEBUG */);
|
|
4476
4811
|
let pending = Promise.resolve();
|
|
4477
|
-
await new Promise((
|
|
4812
|
+
await new Promise((resolve7) => {
|
|
4478
4813
|
rl.on("line", (line) => {
|
|
4479
4814
|
pending = pending.then(() => this.dispatchLine(line));
|
|
4480
4815
|
});
|
|
@@ -4484,7 +4819,7 @@ var McpServer = class {
|
|
|
4484
4819
|
this.autoCaptureController = null;
|
|
4485
4820
|
this.client.disconnect();
|
|
4486
4821
|
log("MCP stdin closed", 0 /* DEBUG */);
|
|
4487
|
-
|
|
4822
|
+
resolve7();
|
|
4488
4823
|
});
|
|
4489
4824
|
});
|
|
4490
4825
|
});
|
|
@@ -4528,13 +4863,13 @@ var McpServer = class {
|
|
|
4528
4863
|
const id = this.nextRequestId++;
|
|
4529
4864
|
const request = { jsonrpc: "2.0", id, method, params };
|
|
4530
4865
|
process.stdout.write(JSON.stringify(request) + "\n");
|
|
4531
|
-
return new Promise((
|
|
4866
|
+
return new Promise((resolve7, reject) => {
|
|
4532
4867
|
const timer = setTimeout(() => {
|
|
4533
4868
|
this.pendingRequests.delete(id);
|
|
4534
4869
|
reject(new Error(`Request ${method} timed out`));
|
|
4535
4870
|
}, 3e4);
|
|
4536
4871
|
this.pendingRequests.set(id, {
|
|
4537
|
-
resolve:
|
|
4872
|
+
resolve: resolve7,
|
|
4538
4873
|
reject,
|
|
4539
4874
|
timer
|
|
4540
4875
|
});
|
|
@@ -4847,22 +5182,16 @@ function createMcpServer(client, contextPath, autoCaptureConfig, clientFactory,
|
|
|
4847
5182
|
}
|
|
4848
5183
|
|
|
4849
5184
|
// src/cli.ts
|
|
4850
|
-
var runtime = detectRuntime();
|
|
4851
|
-
var baseDir = resolveBaseDir(import.meta.url, import.meta.dirname);
|
|
4852
|
-
var workerScript = resolveWorkerScript(baseDir.scriptDir, runtime);
|
|
4853
5185
|
var deps = {
|
|
4854
5186
|
isStdinTty,
|
|
4855
5187
|
isStdoutTty: () => process.stdout.isTTY,
|
|
4856
5188
|
promptUser,
|
|
4857
|
-
createWorkerClient: (opts) => createWorkerClient(
|
|
5189
|
+
createWorkerClient: (opts) => createWorkerClient(opts),
|
|
4858
5190
|
findDb,
|
|
4859
5191
|
runInit,
|
|
4860
5192
|
startMcpServer: async (contextPath, clientFactory, maxRetries) => {
|
|
4861
5193
|
log("starting MCP server", 1 /* INFO */);
|
|
4862
|
-
const client = await createWorkerClient({
|
|
4863
|
-
contextPath,
|
|
4864
|
-
workerScript
|
|
4865
|
-
});
|
|
5194
|
+
const client = await createWorkerClient({ contextPath });
|
|
4866
5195
|
try {
|
|
4867
5196
|
const tomlPath = join9(contextPath, "worker.toml");
|
|
4868
5197
|
let autoCaptureConfig;
|