@dikolab/kbdb 0.2.2 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (118) hide show
  1. package/README.md +50 -15
  2. package/dist/cli.cjs +461 -72
  3. package/dist/cli.cjs.map +4 -4
  4. package/dist/cli.mjs +453 -64
  5. package/dist/cli.mjs.map +4 -4
  6. package/dist/kbdb-worker.cjs +395 -60
  7. package/dist/kbdb-worker.cjs.map +4 -4
  8. package/dist/mod.cjs +17 -0
  9. package/dist/mod.cjs.map +2 -2
  10. package/dist/mod.mjs +17 -0
  11. package/dist/mod.mjs.map +2 -2
  12. package/dist/src/shared/cli/functions/dispatch-command.function.d.ts +3 -1
  13. package/dist/src/shared/cli/functions/install-default-skills.function.d.ts +12 -0
  14. package/dist/src/shared/cli/functions/run-check.function.d.ts +47 -5
  15. package/dist/src/shared/cli/functions/run-export.function.d.ts +34 -0
  16. package/dist/src/shared/cli/functions/run-gc.function.d.ts +6 -0
  17. package/dist/src/shared/cli/functions/run-learn.function.d.ts +35 -6
  18. package/dist/src/shared/cli/functions/run-recall.function.d.ts +45 -0
  19. package/dist/src/shared/cli/functions/run-search.function.d.ts +52 -15
  20. package/dist/src/shared/cli/index.d.ts +4 -0
  21. package/dist/src/shared/cli/typings/cli-options.interface.d.ts +13 -0
  22. package/dist/src/shared/cli/typings/learn-result.model.d.ts +19 -0
  23. package/dist/src/shared/cli/typings/search-display.model.d.ts +18 -0
  24. package/dist/src/shared/mcp/classes/mcp-server.class.d.ts +8 -0
  25. package/dist/src/shared/mcp/typings/mcp-client.interface.d.ts +43 -0
  26. package/dist/src/shared/wasm-codec/functions/decode-add-section-result.function.d.ts +48 -0
  27. package/dist/src/shared/wasm-codec/functions/decode-scored-results.function.d.ts +58 -0
  28. package/dist/src/shared/wasm-codec/functions/decode-section-records.function.d.ts +33 -0
  29. package/dist/src/shared/wasm-codec/functions/encode-add-section-params.function.d.ts +52 -0
  30. package/dist/src/shared/wasm-codec/index.d.ts +2 -0
  31. package/dist/src/shared/worker-client/classes/worker-client.class.d.ts +16 -0
  32. package/dist/src/shared/worker-client/index.d.ts +1 -1
  33. package/dist/src/shared/worker-client/typings/add-section-result.model.d.ts +29 -15
  34. package/dist/src/shared/worker-client/typings/export-result.model.d.ts +14 -0
  35. package/dist/src/shared/worker-client/typings/gc-result.model.d.ts +6 -0
  36. package/dist/src/shared/worker-client/typings/search-result.model.d.ts +12 -0
  37. package/dist/src/shared/worker-client/typings/section.model.d.ts +16 -0
  38. package/dist/src/shared/worker-daemon/classes/worker-daemon.class.d.ts +35 -0
  39. package/dist/wasm/fs-database/kbdb_fs_database.d.ts +22 -1
  40. package/dist/wasm/fs-database/kbdb_fs_database.js +28 -1
  41. package/dist/wasm/fs-database/kbdb_fs_database_bg.wasm +0 -0
  42. package/dist/wasm/fs-database/kbdb_fs_database_bg.wasm.d.ts +1 -0
  43. package/dist/wasm/kb-worker/kbdb_worker.d.ts +48 -1
  44. package/dist/wasm/kb-worker/kbdb_worker.js +60 -1
  45. package/dist/wasm/kb-worker/kbdb_worker_bg.wasm +0 -0
  46. package/dist/wasm/kb-worker/kbdb_worker_bg.wasm.d.ts +2 -0
  47. package/dist/wasm/query-parser/kbdb_query_parser_bg.wasm +0 -0
  48. package/dist/worker.mjs +396 -60
  49. package/dist/worker.mjs.map +4 -4
  50. package/docs/details/cli.md +102 -15
  51. package/docs/details/library-api.md +55 -6
  52. package/docs/details/mcp-server.md +8 -4
  53. package/docs/details/search-and-ranking.md +63 -5
  54. package/docs/details/storage.md +2 -0
  55. package/docs/goals/cli.md +255 -14
  56. package/docs/goals/database.md +244 -17
  57. package/docs/goals/document.md +1 -1
  58. package/docs/goals/mcp.md +83 -14
  59. package/docs/goals/query-parser.md +98 -4
  60. package/docs/goals/query-result.md +177 -10
  61. package/docs/goals/skills.md +72 -0
  62. package/docs/goals/worker-daemon.md +54 -7
  63. package/docs/modules/rust/fs-database/functions/integrity.md +11 -0
  64. package/docs/modules/rust/fs-database/functions/section.md +23 -6
  65. package/docs/modules/rust/fs-database/module.md +1 -0
  66. package/docs/modules/rust/fs-database/types/section.md +27 -1
  67. package/docs/modules/rust/overview.md +2 -1
  68. package/docs/modules/rust/query-parser/functions/module.md +2 -2
  69. package/docs/modules/rust/query-parser/functions/pipeline.md +44 -6
  70. package/docs/modules/rust/query-parser/module.md +5 -4
  71. package/docs/modules/rust/query-parser/types/keyphrase.md +11 -0
  72. package/docs/modules/rust/query-parser/types/module.md +3 -3
  73. package/docs/modules/typescript/shared/cli/functions.md +68 -3
  74. package/package.json +1 -1
  75. package/src/shared/cli/functions/dispatch-command.function.ts +53 -1
  76. package/src/shared/cli/functions/format-output.function.ts +36 -12
  77. package/src/shared/cli/functions/generate-help.function.ts +56 -1
  78. package/src/shared/cli/functions/install-default-skills.function.ts +296 -0
  79. package/src/shared/cli/functions/parse-args.function.ts +13 -0
  80. package/src/shared/cli/functions/run-check.function.ts +87 -6
  81. package/src/shared/cli/functions/run-export.function.ts +40 -0
  82. package/src/shared/cli/functions/run-gc.function.ts +6 -0
  83. package/src/shared/cli/functions/run-init.function.ts +2 -0
  84. package/src/shared/cli/functions/run-learn.function.ts +68 -16
  85. package/src/shared/cli/functions/run-recall.function.ts +66 -0
  86. package/src/shared/cli/functions/run-search.function.ts +58 -15
  87. package/src/shared/cli/index.ts +4 -0
  88. package/src/shared/cli/typings/cli-options.interface.ts +13 -0
  89. package/src/shared/cli/typings/learn-result.model.ts +16 -0
  90. package/src/shared/cli/typings/search-display.model.ts +18 -0
  91. package/src/shared/mcp/classes/mcp-server.class.ts +30 -1
  92. package/src/shared/mcp/functions/create-tool-definitions.function.ts +46 -5
  93. package/src/shared/mcp/functions/handle-tool-call.function.ts +70 -2
  94. package/src/shared/mcp/typings/mcp-client.interface.ts +45 -0
  95. package/src/shared/wasm-codec/functions/decode-add-section-result.function.ts +112 -0
  96. package/src/shared/wasm-codec/functions/decode-scored-results.function.ts +86 -0
  97. package/src/shared/wasm-codec/functions/decode-section-records.function.ts +87 -0
  98. package/src/shared/wasm-codec/functions/encode-add-section-params.function.ts +68 -0
  99. package/src/shared/wasm-codec/index.ts +5 -0
  100. package/src/shared/worker-client/classes/worker-client.class.ts +19 -0
  101. package/src/shared/worker-client/index.ts +4 -1
  102. package/src/shared/worker-client/typings/add-section-result.model.ts +30 -15
  103. package/src/shared/worker-client/typings/export-result.model.ts +14 -0
  104. package/src/shared/worker-client/typings/gc-result.model.ts +6 -0
  105. package/src/shared/worker-client/typings/search-result.model.ts +12 -0
  106. package/src/shared/worker-client/typings/section.model.ts +16 -0
  107. package/src/shared/worker-daemon/classes/worker-daemon.class.ts +315 -46
  108. package/src/shared/worker-daemon/functions/create-host-imports.function.ts +19 -4
  109. package/src/shared/worker-daemon/functions/create-ipc-server.function.ts +5 -1
  110. package/src/wasm/fs-database/kbdb_fs_database.d.ts +22 -1
  111. package/src/wasm/fs-database/kbdb_fs_database.js +28 -1
  112. package/src/wasm/fs-database/kbdb_fs_database_bg.wasm +0 -0
  113. package/src/wasm/fs-database/kbdb_fs_database_bg.wasm.d.ts +1 -0
  114. package/src/wasm/kb-worker/kbdb_worker.d.ts +48 -1
  115. package/src/wasm/kb-worker/kbdb_worker.js +60 -1
  116. package/src/wasm/kb-worker/kbdb_worker_bg.wasm +0 -0
  117. package/src/wasm/kb-worker/kbdb_worker_bg.wasm.d.ts +2 -0
  118. package/src/wasm/query-parser/kbdb_query_parser_bg.wasm +0 -0
@@ -1,5 +1,28 @@
1
1
  import type { CliOptions } from '../typings/cli-options.interface.ts';
2
2
  import type { LearnResult } from '../typings/learn-result.model.ts';
3
+ /** Enriched add-section result returned by the daemon. */
4
+ interface AddSectionResult {
5
+ /** Knowledge-base identifier assigned to the new section. */
6
+ readonly kbid: string;
7
+ /**
8
+ * Kbid of the section superseded via `--replace`,
9
+ * or `null` / `undefined` when none.
10
+ */
11
+ readonly superseded?: string | null;
12
+ /**
13
+ * Advisory warning strings (e.g. `"heading_missing"`).
14
+ * May be absent from older daemon versions.
15
+ */
16
+ readonly warnings?: string[];
17
+ /**
18
+ * Near-duplicate sections with similarity >= 0.85.
19
+ * May be absent from older daemon versions.
20
+ */
21
+ readonly near_duplicates?: {
22
+ readonly kbid: string;
23
+ readonly similarity: number;
24
+ }[];
25
+ }
3
26
  /**
4
27
  * Minimal client interface required by `runLearn`.
5
28
  *
@@ -10,9 +33,9 @@ export interface LearnClient {
10
33
  /**
11
34
  * Ingests a content section into the knowledge base.
12
35
  *
13
- * @param params - section type label and raw text content
14
- * @returns result object containing the knowledge-base identifier
15
- * assigned to the section
36
+ * @param params - section metadata and raw text content
37
+ * @returns enriched result containing the new kbid plus
38
+ * optional supersession, warnings, and near-duplicate info
16
39
  */
17
40
  addSection(params: {
18
41
  sectionType: string;
@@ -20,9 +43,14 @@ export interface LearnClient {
20
43
  title?: string;
21
44
  description?: string;
22
45
  docid?: string;
23
- }): Promise<{
24
- kbid: string;
25
- }>;
46
+ tags?: string[];
47
+ replace?: boolean;
48
+ /**
49
+ * Absolute path to the source file. Required for supersession
50
+ * via source key when `replace` is `true`.
51
+ */
52
+ sourcePath?: string;
53
+ }): Promise<AddSectionResult>;
26
54
  }
27
55
  /** Injectable I/O for testing without real stdin. */
28
56
  export interface LearnIO {
@@ -44,3 +72,4 @@ export interface LearnIO {
44
72
  * @returns one `LearnResult` per file successfully ingested
45
73
  */
46
74
  export declare function runLearn(client: LearnClient, options: CliOptions, io?: LearnIO): Promise<LearnResult[]>;
75
+ export {};
@@ -0,0 +1,45 @@
1
+ import type { CliOptions } from '../typings/cli-options.interface.ts';
2
+ import type { RecallResult } from '../../worker-client/typings/recall-result.model.ts';
3
+ /**
4
+ * Minimal client interface required by `runRecall`.
5
+ *
6
+ * Decouples the CLI function from the concrete `WorkerClient`
7
+ * implementation, making it straightforward to test with mocks.
8
+ */
9
+ export interface RecallClient {
10
+ /**
11
+ * Retrieves full content and context for one or more sections
12
+ * by their knowledge-base identifiers.
13
+ *
14
+ * @param params - section identifier(s) and optional expansion
15
+ * depth (0--3)
16
+ * @returns a single `RecallResult` when `kbid` is supplied, or
17
+ * a `RecallResult[]` when `kbids` is supplied
18
+ */
19
+ recall(params: {
20
+ kbid?: string;
21
+ kbids?: string[];
22
+ depth?: number;
23
+ }): Promise<RecallResult | RecallResult[]>;
24
+ }
25
+ /**
26
+ * Retrieves full content and progressive context for one or more
27
+ * sections by kbid via the provided client.
28
+ *
29
+ * Returns an error result when no kbids are provided in
30
+ * `options.args`. For a single kbid, calls `client.recall` with the
31
+ * `kbid` parameter and returns the single `RecallResult`. For
32
+ * multiple kbids, calls `client.recall` with the `kbids` array.
33
+ *
34
+ * The `depth` option is read from `options.depth`, defaulting to 0
35
+ * and clamped to the range 0--3.
36
+ *
37
+ * @param client - recall client to delegate to
38
+ * @param options - parsed CLI options supplying kbids in `args` and
39
+ * expansion depth in `depth`
40
+ * @returns the `RecallResult` or `RecallResult[]` from the client,
41
+ * or an error object when no kbids are provided
42
+ */
43
+ export declare function runRecall(client: RecallClient, options: CliOptions): Promise<RecallResult | RecallResult[] | {
44
+ error: string;
45
+ }>;
@@ -1,5 +1,55 @@
1
1
  import type { CliOptions } from '../typings/cli-options.interface.ts';
2
2
  import type { PagedSearchDisplay } from '../typings/search-display.model.ts';
3
+ /** A single raw search result item returned by the daemon. */
4
+ interface RawSearchItem {
5
+ /** Knowledge-base identifier of the matched section. */
6
+ readonly kbid: string;
7
+ /** Section heading, or null when untitled. */
8
+ readonly heading: string | null;
9
+ /** MIME type of the section content. */
10
+ readonly type: string;
11
+ /** Document IDs this section belongs to. */
12
+ readonly docids: readonly string[];
13
+ /** Relevance score from the ranking engine. */
14
+ readonly score: number;
15
+ /** KWIC excerpt centered on the highest-scoring matched term. */
16
+ readonly snippet: string;
17
+ /** Query terms that matched within this section. */
18
+ readonly matched_terms: readonly string[];
19
+ /**
20
+ * Fields that contained at least one match.
21
+ * May be absent from older daemon versions.
22
+ */
23
+ readonly matched_fields?: readonly string[];
24
+ /**
25
+ * Normalised confidence score (0.0-1.0) relative to the top result.
26
+ * May be absent from older daemon versions.
27
+ */
28
+ readonly confidence?: number;
29
+ /**
30
+ * ISO 8601 creation timestamp of the section.
31
+ * May be absent from older daemon versions.
32
+ */
33
+ readonly created_at?: string;
34
+ }
35
+ /** Raw paginated search response from the daemon. */
36
+ interface RawSearchResponse {
37
+ /** Result items for the current page. */
38
+ readonly items: readonly RawSearchItem[];
39
+ /** Total number of matches across all pages. */
40
+ readonly total: number;
41
+ /** Number of results skipped. */
42
+ readonly offset: number;
43
+ /** Maximum results per page. */
44
+ readonly limit: number;
45
+ /** Whether more results exist beyond this page. */
46
+ readonly has_more: boolean;
47
+ /**
48
+ * When `true`, the engine relaxed the query to produce results.
49
+ * May be absent from older daemon versions.
50
+ */
51
+ readonly relaxed?: boolean;
52
+ }
3
53
  /**
4
54
  * Minimal client interface required by `runSearch`.
5
55
  *
@@ -17,21 +67,7 @@ export interface SearchClient {
17
67
  mode?: string;
18
68
  limit?: number;
19
69
  offset?: number;
20
- }): Promise<{
21
- readonly items: readonly {
22
- readonly kbid: string;
23
- readonly heading: string | null;
24
- readonly type: string;
25
- readonly docids: readonly string[];
26
- readonly score: number;
27
- readonly snippet: string;
28
- readonly matched_terms: readonly string[];
29
- }[];
30
- readonly total: number;
31
- readonly offset: number;
32
- readonly limit: number;
33
- readonly has_more: boolean;
34
- }>;
70
+ }): Promise<RawSearchResponse>;
35
71
  }
36
72
  /**
37
73
  * Executes a search query via the provided client and maps the
@@ -46,3 +82,4 @@ export interface SearchClient {
46
82
  * search parameters
47
83
  */
48
84
  export declare function runSearch(client: SearchClient, options: CliOptions): Promise<PagedSearchDisplay>;
85
+ export {};
@@ -1,6 +1,8 @@
1
1
  export { parseArgs } from './functions/parse-args.function.ts';
2
2
  export { formatOutput } from './functions/format-output.function.ts';
3
3
  export { runSearch } from './functions/run-search.function.ts';
4
+ export { runRecall } from './functions/run-recall.function.ts';
5
+ export { runExport } from './functions/run-export.function.ts';
4
6
  export { runLearn } from './functions/run-learn.function.ts';
5
7
  export { runUnlearn } from './functions/run-unlearn.function.ts';
6
8
  export { runContent } from './functions/run-content.function.ts';
@@ -21,6 +23,8 @@ export type { CliOptions, OutputFormat, SearchMode, } from './typings/cli-option
21
23
  export type { LearnResult } from './typings/learn-result.model.ts';
22
24
  export type { SearchDisplay, PagedSearchDisplay, } from './typings/search-display.model.ts';
23
25
  export type { SearchClient } from './functions/run-search.function.ts';
26
+ export type { RecallClient } from './functions/run-recall.function.ts';
27
+ export type { ExportClient } from './functions/run-export.function.ts';
24
28
  export type { LearnClient } from './functions/run-learn.function.ts';
25
29
  export type { UnlearnClient, UnlearnResult, } from './functions/run-unlearn.function.ts';
26
30
  export type { ContentClient } from './functions/run-content.function.ts';
@@ -50,6 +50,19 @@ export interface CliOptions {
50
50
  body?: string;
51
51
  /** Comma-separated skill kbids for agent create. */
52
52
  skills?: string[];
53
+ /**
54
+ * Recall expansion depth (0-3). Clamped to 3.
55
+ * Defaults to 0.
56
+ */
57
+ depth?: number;
58
+ /** Comma-separated tags for scoping (learn, recall). */
59
+ tags?: string[];
60
+ /**
61
+ * When `true`, enables supersession by source key (learn).
62
+ * Re-learning edited content replaces the original section
63
+ * instead of creating a sibling.
64
+ */
65
+ replace?: boolean;
53
66
  /** Positional arguments following the sub-command. */
54
67
  args: string[];
55
68
  }
@@ -8,4 +8,23 @@ export interface LearnResult {
8
8
  size: number;
9
9
  /** `true` when the file was skipped without being ingested. */
10
10
  skipped: boolean;
11
+ /**
12
+ * Kbid of the section that was superseded via `--replace`,
13
+ * or `null` when no supersession occurred.
14
+ */
15
+ superseded: string | null;
16
+ /**
17
+ * Advisory warning strings returned by the engine
18
+ * (e.g. `"heading_missing"` when no title is provided).
19
+ */
20
+ warnings: string[];
21
+ /**
22
+ * Sections with content similarity >= 0.85 to this section.
23
+ * Each entry identifies a near-duplicate section and its
24
+ * similarity score.
25
+ */
26
+ near_duplicates: {
27
+ kbid: string;
28
+ similarity: number;
29
+ }[];
11
30
  }
@@ -10,10 +10,22 @@ export interface SearchDisplay {
10
10
  docids: readonly string[];
11
11
  /** Relevance score assigned by the ranking engine. */
12
12
  score: number;
13
+ /**
14
+ * Normalised confidence score (0.0-1.0) relative to the
15
+ * top result in this result set.
16
+ */
17
+ confidence: number;
13
18
  /** KWIC snippet from the WASM engine (~150 chars). */
14
19
  snippet: string;
15
20
  /** Query terms that matched within this section. */
16
21
  matched_terms: readonly string[];
22
+ /**
23
+ * Fields that contained at least one match:
24
+ * subset of `["heading", "body", "code"]`.
25
+ */
26
+ matched_fields: readonly string[];
27
+ /** ISO 8601 creation timestamp of the section. */
28
+ created_at: string;
17
29
  /**
18
30
  * CLI-side truncated content preview for terminal display.
19
31
  * Populated when `--content` / `-c` is used.
@@ -32,4 +44,10 @@ export interface PagedSearchDisplay {
32
44
  limit: number;
33
45
  /** Whether more results exist beyond this page. */
34
46
  has_more: boolean;
47
+ /**
48
+ * When `true`, the engine relaxed the query (e.g. dropped
49
+ * rare terms) to return results that would otherwise be
50
+ * empty.
51
+ */
52
+ relaxed: boolean;
35
53
  }
@@ -52,4 +52,12 @@ export declare class McpServer {
52
52
  private processLine;
53
53
  private handleRequest;
54
54
  private send;
55
+ /**
56
+ * Runs an integrity check immediately after the MCP handshake
57
+ * completes. Emits a `notifications/message` warning when the
58
+ * check detects issues so the connected agent is aware before
59
+ * making requests. The server continues operating regardless of
60
+ * the result.
61
+ */
62
+ private runStartupHealthCheck;
55
63
  }
@@ -35,6 +35,17 @@ export interface McpIntegrityCheckResult {
35
35
  /** Time taken to complete the check, in milliseconds. */
36
36
  readonly elapsed_ms: number;
37
37
  }
38
+ /** Export report returned by the daemon. */
39
+ export interface McpExportResult {
40
+ /** Absolute path to the directory where files were written. */
41
+ readonly path: string;
42
+ /** Number of section files copied to the export directory. */
43
+ readonly sections_exported: number;
44
+ /** Number of document manifest files copied. */
45
+ readonly documents_exported: number;
46
+ /** Time taken to complete the export, in milliseconds. */
47
+ readonly elapsed_ms: number;
48
+ }
38
49
  /** Garbage collection report returned by the daemon. */
39
50
  export interface McpGcResult {
40
51
  /** Number of unreferenced sections removed. */
@@ -43,6 +54,12 @@ export interface McpGcResult {
43
54
  readonly removed_bytes: number;
44
55
  /** Time taken to complete garbage collection, in milliseconds. */
45
56
  readonly elapsed_ms: number;
57
+ /**
58
+ * Number of sections whose source_path file no longer contains
59
+ * their heading text. Advisory; not auto-removed by gc.
60
+ * Absent on older daemon versions.
61
+ */
62
+ readonly stale_sources?: number;
46
63
  }
47
64
  /** Index rebuild report returned by the daemon. */
48
65
  export interface McpRebuildResult {
@@ -84,11 +101,20 @@ export interface McpWorkerClient {
84
101
  readonly score: number;
85
102
  readonly snippet: string;
86
103
  readonly matched_terms: readonly string[];
104
+ /** Fields that contained matches (heading, body, code). */
105
+ readonly matched_fields?: readonly string[];
106
+ /** ISO 8601 creation timestamp of the section. */
107
+ readonly created_at?: string;
87
108
  }[];
88
109
  readonly total: number;
89
110
  readonly offset: number;
90
111
  readonly limit: number;
91
112
  readonly has_more: boolean;
113
+ /**
114
+ * `true` when results came from an automatic AND-to-OR
115
+ * fallback query relaxation.
116
+ */
117
+ readonly relaxed?: boolean;
92
118
  }>;
93
119
  /**
94
120
  * Adds a new section to the knowledge base.
@@ -107,6 +133,13 @@ export interface McpWorkerClient {
107
133
  readonly description?: string;
108
134
  /** Optional document ID to group this section under. */
109
135
  readonly docid?: string;
136
+ /** Optional tags for scoping and filtering. */
137
+ readonly tags?: string[];
138
+ /**
139
+ * When `true`, replaces an existing section that has the same
140
+ * source key (supersession).
141
+ */
142
+ readonly replace?: boolean;
110
143
  }): Promise<AddSectionResult>;
111
144
  /**
112
145
  * Removes a section from the knowledge base.
@@ -230,6 +263,16 @@ export interface McpWorkerClient {
230
263
  /** Expansion depth (0-3, default: 0). */
231
264
  readonly depth?: number;
232
265
  }): Promise<unknown>;
266
+ /**
267
+ * Exports the database to a target directory as a self-contained
268
+ * snapshot (sections, documents, catalog -- no indexes).
269
+ *
270
+ * @param params - optional export parameters
271
+ * @returns export report with the resolved path and file counts
272
+ */
273
+ export(params?: {
274
+ readonly path?: string;
275
+ }): Promise<McpExportResult>;
233
276
  /** Destroys the IPC socket and clears the connection state. */
234
277
  disconnect(): void;
235
278
  }
@@ -0,0 +1,48 @@
1
+ /**
2
+ * A near-duplicate section identified during add-section indexing.
3
+ */
4
+ export interface NearDuplicate {
5
+ /** Knowledge-base identifier of the near-duplicate section. */
6
+ readonly kbid: string;
7
+ /** Jaccard similarity score (0.0–1.0). */
8
+ readonly similarity: number;
9
+ }
10
+ /**
11
+ * Decoded result of a `worker_add_section` WASM call.
12
+ *
13
+ * Mirrors the Rust `AddSectionResult` struct encoded via the
14
+ * `Encode` trait wire format.
15
+ */
16
+ export interface AddSectionResult {
17
+ /** Knowledge-base identifier assigned to the new section. */
18
+ readonly kbid: string;
19
+ /** Number of unique terms indexed for the section. */
20
+ readonly indexedTermsCount: number;
21
+ /** Wall-clock time the add operation took, in milliseconds. */
22
+ readonly elapsedMs: number;
23
+ /**
24
+ * Knowledge-base identifier of the section that was superseded,
25
+ * or `null` if no supersession occurred.
26
+ */
27
+ readonly superseded: string | null;
28
+ /** Advisory warnings emitted during indexing. */
29
+ readonly warnings: string[];
30
+ /**
31
+ * Near-duplicate sections detected above the similarity threshold
32
+ * (Jaccard ≥ 0.85).
33
+ */
34
+ readonly nearDuplicates: NearDuplicate[];
35
+ }
36
+ /**
37
+ * Decodes the binary `AddSectionResult` returned by the
38
+ * `worker_add_section` WASM export.
39
+ *
40
+ * Wire format (little-endian):
41
+ * - `kbid`: `u32` length + UTF-8 bytes
42
+ * - `indexed_terms_count`: `u32`
43
+ * - `elapsed_ms`: `u64`
44
+ * - `superseded`: `u8` flag (0=None, 1=Some) + optional `String`
45
+ * - `warnings`: `u32` count + each `String`
46
+ * - `near_duplicates`: `u32` count + each (`String` + `f32`)
47
+ */
48
+ export declare function decodeAddSectionResult(bytes: Uint8Array): AddSectionResult;
@@ -1,17 +1,75 @@
1
+ /**
2
+ * A single ranked result item from a paged search response.
3
+ */
1
4
  export interface SectionMatch {
5
+ /** Knowledge-base identifier for the matched section. */
2
6
  kbid: string;
7
+ /** Section heading, or `null` when untitled. */
3
8
  heading: string | null;
9
+ /** MIME type of the section content. */
4
10
  type: string;
11
+ /** Document IDs this section belongs to. */
5
12
  docids: string[];
13
+ /** Relevance score from the ranking engine. */
6
14
  score: number;
15
+ /**
16
+ * Confidence modifier (0.0–1.0) applied after relaxed search.
17
+ * Always 1.0 for strict-mode results.
18
+ */
19
+ confidence: number;
20
+ /** KWIC snippet (~150 chars around the best match). */
7
21
  snippet: string;
22
+ /** Query terms that matched within this section. */
8
23
  matched_terms: string[];
24
+ /**
25
+ * Document fields that contained at least one matched term
26
+ * (e.g. `'title'`, `'body'`).
27
+ */
28
+ matched_fields: string[];
29
+ /** ISO 8601 creation timestamp of the section. */
30
+ created_at: string;
9
31
  }
32
+ /**
33
+ * Paginated search response envelope returned by the WASM engine.
34
+ */
10
35
  export interface PagedSearchResult {
36
+ /** Matching sections for the current page. */
11
37
  items: SectionMatch[];
38
+ /** Total number of matches across all pages. */
12
39
  total: number;
40
+ /** Number of results skipped (pagination offset). */
13
41
  offset: number;
42
+ /** Maximum results per page. */
14
43
  limit: number;
44
+ /** Whether more results exist beyond this page. */
15
45
  has_more: boolean;
46
+ /**
47
+ * Whether the engine fell back to relaxed (OR-mode) search
48
+ * after strict (AND-mode) returned no results.
49
+ */
50
+ relaxed: boolean;
16
51
  }
52
+ /**
53
+ * Decodes the binary `PagedSearchResult` returned by the
54
+ * `worker_get_result` WASM export after a `worker_search` call.
55
+ *
56
+ * Wire format (little-endian):
57
+ * - `total`: `u32`
58
+ * - `offset`: `u32`
59
+ * - `limit`: `u32`
60
+ * - `has_more`: `u8` (0/1)
61
+ * - `relaxed`: `u8` (0/1)
62
+ * - `item_count`: `u32`
63
+ * - Per item:
64
+ * - `kbid`: `String`
65
+ * - `heading`: `Option<String>`
66
+ * - `section_type`: `String`
67
+ * - `docids`: `u32` count + `String[]`
68
+ * - `score`: `f32`
69
+ * - `confidence`: `f32`
70
+ * - `snippet`: `String`
71
+ * - `matched_terms`: `u32` count + `String[]`
72
+ * - `matched_fields`: `u32` count + `String[]`
73
+ * - `created_at`: `String`
74
+ */
17
75
  export declare function decodePagedSearchResult(bytes: Uint8Array): PagedSearchResult;
@@ -36,6 +36,33 @@ export interface SectionRecord {
36
36
  codeTokenCount: number;
37
37
  /** Application-level type name assigned to this section. */
38
38
  typeName: string;
39
+ /**
40
+ * Stable logical identity derived from source path and heading.
41
+ * `null` for stdin input or sections without a source path.
42
+ */
43
+ sourceKey: string | null;
44
+ /**
45
+ * Absolute path of the source file that was learned.
46
+ * `null` for stdin input or programmatic API calls.
47
+ */
48
+ sourcePath: string | null;
49
+ /**
50
+ * ISO 8601 modified time of the source file at learn time.
51
+ * `null` when no source file exists.
52
+ */
53
+ sourceMtime: string | null;
54
+ /**
55
+ * ISO 8601 timestamp of the last supersession.
56
+ * `null` on first creation.
57
+ */
58
+ updatedAt: string | null;
59
+ /** User-supplied tags for scoping and filtering. */
60
+ tags: string[];
61
+ /**
62
+ * The kbid of the section this one replaced via supersession.
63
+ * `null` on first creation or non-superseding additions.
64
+ */
65
+ supersedes: string | null;
39
66
  }
40
67
  /**
41
68
  * Decodes a list of `SectionRecord` values from WASM binary output.
@@ -44,6 +71,12 @@ export interface SectionRecord {
44
71
  * Each record is decoded in field order as produced by the Rust
45
72
  * `SectionRecord::encode` implementation.
46
73
  *
74
+ * Trailing fields (`sourceKey`, `sourcePath`, `sourceMtime`,
75
+ * `updatedAt`, `tags`, `supersedes`) were added after the initial
76
+ * format. When decoding legacy bytes that stop after `typeName`,
77
+ * those fields default to `null` / `[]`. This matches the Rust
78
+ * decoder's backward-compatibility guards.
79
+ *
47
80
  * @param bytes - raw bytes returned by a WASM section-list export
48
81
  * @returns decoded array of section records, empty when count is zero
49
82
  */
@@ -1,10 +1,62 @@
1
+ /**
2
+ * Parameters for adding a new section to the knowledge base.
3
+ *
4
+ * Maps to the Rust `AddSectionParams` struct encoded for the
5
+ * `worker_add_section` WASM export.
6
+ */
1
7
  export interface AddSectionInput {
8
+ /** Semantic content category. */
2
9
  sectionType: 'text' | 'code' | 'image';
10
+ /** MIME type; required for `code` and `image` section types. */
3
11
  mimeType?: string;
12
+ /** Raw section content. */
4
13
  content: string;
14
+ /** Optional section heading. */
5
15
  title?: string | null;
16
+ /** Optional description summary. */
6
17
  description?: string | null;
18
+ /** Optional document identifier to attach the section to. */
7
19
  docid?: string | null;
20
+ /**
21
+ * Application-level type name for the section (e.g. `'skill'`,
22
+ * `'fact'`). `null` means no custom type.
23
+ */
8
24
  typeName?: string | null;
25
+ /**
26
+ * Absolute path of the source file being learned. `undefined`
27
+ * means stdin input or programmatic API call.
28
+ */
29
+ sourcePath?: string;
30
+ /**
31
+ * ISO 8601 modified time of the source file. `undefined` means
32
+ * no source file exists.
33
+ */
34
+ sourceMtime?: string;
35
+ /**
36
+ * User-supplied tags for scoping and filtering. Defaults to
37
+ * an empty list when not provided.
38
+ */
39
+ tags?: string[];
40
+ /**
41
+ * When `true`, supersede any existing section with the same
42
+ * source key. Defaults to `false`.
43
+ */
44
+ replace?: boolean;
9
45
  }
46
+ /**
47
+ * Encodes `AddSectionInput` as the binary format expected by the
48
+ * `worker_add_section` WASM export.
49
+ *
50
+ * Wire format (little-endian):
51
+ * - `SectionType` tag byte (+ optional MIME string for code/image)
52
+ * - `content`: `u32` length + UTF-8 bytes
53
+ * - `title`: `u8` flag + optional `String`
54
+ * - `description`: `u8` flag + optional `String`
55
+ * - `docid`: `u8` flag + optional `String`
56
+ * - `typeName`: `u8` flag + optional `String`
57
+ * - `sourcePath`: `u8` flag + optional `String`
58
+ * - `sourceMtime`: `u8` flag + optional `String`
59
+ * - `tags`: `u32` count + each `String`
60
+ * - `replace`: `u8` (0 or 1)
61
+ */
10
62
  export declare function encodeAddSectionParams(params: AddSectionInput): Uint8Array;
@@ -23,8 +23,10 @@ export { decodeKeyValueText } from './functions/decode-key-value-text.function.t
23
23
  export { decodeRemoveFlag } from './functions/decode-remove-flag.function.ts';
24
24
  export { encodeRecallParams } from './functions/encode-recall-params.function.ts';
25
25
  export { decodeRecallResult } from './functions/decode-recall-result.function.ts';
26
+ export { decodeAddSectionResult } from './functions/decode-add-section-result.function.ts';
26
27
  export type { DecodeResult } from './functions/decode-string.function.ts';
27
28
  export type { AddSectionInput } from './functions/encode-add-section-params.function.ts';
29
+ export type { AddSectionResult, NearDuplicate, } from './functions/decode-add-section-result.function.ts';
28
30
  export type { SectionMatch, PagedSearchResult, } from './functions/decode-scored-results.function.ts';
29
31
  export type { SectionRecord } from './functions/decode-section-records.function.ts';
30
32
  export type { DocumentManifest, DocumentSection, } from './functions/decode-document-manifest.function.ts';
@@ -13,6 +13,7 @@ import type { VersionStatus } from '../typings/version-status.model.ts';
13
13
  import type { SectionRecord } from '../../wasm-codec/functions/decode-section-records.function.ts';
14
14
  import type { DocumentManifest } from '../../wasm-codec/functions/decode-document-manifest.function.ts';
15
15
  import type { RecallParams, RecallResult } from '../typings/recall-result.model.ts';
16
+ import type { ExportResult } from '../typings/export-result.model.ts';
16
17
  /**
17
18
  * IPC client that communicates with the worker daemon over a Unix
18
19
  * domain socket (or Windows named pipe) using JSON-RPC 2.0 with
@@ -221,6 +222,21 @@ export declare class WorkerClient {
221
222
  * @returns all document manifests with a matching type name
222
223
  */
223
224
  listDocumentsByType(docType: string): Promise<DocumentManifest[]>;
225
+ /**
226
+ * Exports the database to a target directory on disk.
227
+ *
228
+ * Copies section files, document manifests, and `catalog.toml`
229
+ * to `params.path`. Index files are not exported because they
230
+ * are derivable from the section and document data.
231
+ *
232
+ * @param params - optional export parameters
233
+ * @param params.path - target directory path; defaults to
234
+ * `./kbdb-export/` when not provided
235
+ * @returns export report with the resolved path and file counts
236
+ */
237
+ export(params?: {
238
+ path?: string;
239
+ }): Promise<ExportResult>;
224
240
  /**
225
241
  * Sends a JSON-RPC 2.0 request and awaits the response.
226
242
  *
@@ -12,7 +12,7 @@ export type { Section, AddSectionParams, } from './typings/section.model.ts';
12
12
  export type { StatusResult } from './typings/status-result.model.ts';
13
13
  export type { WorkerClientError } from './typings/worker-client-error.interface.ts';
14
14
  export type { DaemonInfo } from './typings/daemon-info.model.ts';
15
- export type { AddSectionResult } from './typings/add-section-result.model.ts';
15
+ export type { AddSectionResult, NearDuplicateEntry, } from './typings/add-section-result.model.ts';
16
16
  export type { RemoveSectionResult } from './typings/remove-section-result.model.ts';
17
17
  export type { ContentResult } from './typings/content-result.model.ts';
18
18
  export type { IntegrityCheckResult, IntegrityError, } from './typings/integrity-check-result.model.ts';