@dikolab/kbdb 0.5.0 → 0.6.1

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 (224) hide show
  1. package/CLA.md +68 -0
  2. package/LICENSE +676 -12
  3. package/LICENSING.md +58 -0
  4. package/README.md +50 -25
  5. package/README.md.bak +322 -0
  6. package/dist/README.md +322 -0
  7. package/dist/{chunk-BJXEVALU.mjs → chunk-QLJ33C74.mjs} +44 -44
  8. package/dist/chunk-QLJ33C74.mjs.map +7 -0
  9. package/dist/cli.cjs +550 -232
  10. package/dist/cli.cjs.map +4 -4
  11. package/dist/cli.d.ts +0 -8
  12. package/dist/cli.mjs +483 -154
  13. package/dist/cli.mjs.map +4 -4
  14. package/dist/kbdb-worker.cjs +36 -5
  15. package/dist/kbdb-worker.cjs.map +3 -3
  16. package/dist/mod.cjs +89 -30
  17. package/dist/mod.cjs.map +4 -4
  18. package/dist/mod.mjs +88 -30
  19. package/dist/mod.mjs.map +4 -4
  20. package/dist/src/cli.d.ts +0 -8
  21. package/dist/src/shared/cli/constants/defaults.constant.d.ts +1 -1
  22. package/dist/src/shared/cli/constants/recfile-command-field-map.constant.d.ts +8 -0
  23. package/dist/src/shared/cli/constants/recfile-field-maps.constant.d.ts +21 -0
  24. package/dist/src/shared/cli/functions/compute-directory-level.function.d.ts +14 -0
  25. package/dist/src/shared/cli/functions/dispatch-command.function.d.ts +2 -28
  26. package/dist/src/shared/cli/functions/format-command-output.function.d.ts +14 -0
  27. package/dist/src/shared/cli/functions/format-db-not-found.function.d.ts +20 -0
  28. package/dist/src/shared/cli/functions/format-output.function.d.ts +1 -0
  29. package/dist/src/shared/cli/functions/format-recfile-output.function.d.ts +11 -0
  30. package/dist/src/shared/cli/functions/render-recfile-value.function.d.ts +11 -0
  31. package/dist/src/shared/cli/functions/resolve-db-dir.function.d.ts +24 -0
  32. package/dist/src/shared/cli/functions/resolve-recfile-key.function.d.ts +9 -0
  33. package/dist/src/shared/cli/functions/run-db-path.function.d.ts +10 -0
  34. package/dist/src/shared/cli/functions/run-export.function.d.ts +8 -4
  35. package/dist/src/shared/cli/functions/run-import.function.d.ts +4 -0
  36. package/dist/src/shared/cli/functions/run-learn.function.d.ts +12 -0
  37. package/dist/src/shared/cli/index.d.ts +4 -1
  38. package/dist/src/shared/cli/typings/cli-options.interface.d.ts +9 -2
  39. package/dist/src/shared/cli/typings/db-resolution.interface.d.ts +9 -0
  40. package/dist/src/shared/cli/typings/recfile-field-map.type.d.ts +10 -0
  41. package/dist/src/shared/version/constants/version.constant.d.ts +1 -1
  42. package/dist/src/shared/wasm-codec/functions/decode-option-u8.function.d.ts +9 -0
  43. package/dist/src/shared/wasm-codec/functions/decode-section-records.function.d.ts +4 -0
  44. package/dist/src/shared/wasm-codec/functions/encode-add-section-params.function.d.ts +2 -45
  45. package/dist/src/shared/wasm-codec/functions/encode-update-section-params.function.d.ts +10 -1
  46. package/dist/src/shared/wasm-codec/index.d.ts +1 -1
  47. package/dist/src/shared/wasm-codec/typings/add-section-input.model.d.ts +6 -0
  48. package/dist/src/shared/wasm-codec/typings/section-record-trailing.model.d.ts +5 -0
  49. package/dist/src/shared/wasm-codec/typings/section-record.model.d.ts +5 -0
  50. package/dist/src/shared/worker-client/functions/build-deno-permissions.function.d.ts +11 -0
  51. package/dist/src/shared/worker-client/functions/read-crash-reason.function.d.ts +9 -0
  52. package/dist/src/shared/worker-client/functions/resolve-spawn-script.function.d.ts +6 -0
  53. package/dist/src/shared/worker-client/functions/spawn-daemon.function.d.ts +1 -1
  54. package/dist/src/shared/worker-client/typings/section.model.d.ts +5 -0
  55. package/dist/src/shared/worker-client/typings/worker-client-options.interface.d.ts +0 -6
  56. package/dist/src/shared/worker-daemon/functions/count-files.function.d.ts +12 -0
  57. package/dist/src/shared/worker-daemon/functions/handle-import.function.d.ts +2 -0
  58. package/dist/src/shared/worker-daemon/functions/handle-read-ops.function.d.ts +8 -0
  59. package/dist/src/shared/worker-daemon/functions/validate-daemon-path.function.d.ts +15 -0
  60. package/dist/src/worker.d.ts +0 -7
  61. package/dist/wasm/default-embedding/kbdb_default_embedding_bg.wasm +0 -0
  62. package/dist/wasm/default-embedding/package.json +1 -1
  63. package/dist/wasm/fs-database/kbdb_fs_database_bg.wasm +0 -0
  64. package/dist/wasm/fs-database/package.json +1 -1
  65. package/dist/wasm/fs-migration/package.json +1 -1
  66. package/dist/wasm/kb-worker/kbdb_worker_bg.wasm +0 -0
  67. package/dist/wasm/kb-worker/package.json +1 -1
  68. package/dist/wasm/query-parser/kbdb_query_parser_bg.wasm +0 -0
  69. package/dist/wasm/query-parser/package.json +1 -1
  70. package/dist/worker.d.ts +0 -7
  71. package/dist/worker.mjs +37 -6
  72. package/dist/worker.mjs.map +3 -3
  73. package/package.json +11 -10
  74. package/dist/chunk-BJXEVALU.mjs.map +0 -7
  75. package/dist/src/shared/cli/functions/resolve-raw-db-path.function.d.ts +0 -10
  76. package/docs/details/README.md +0 -30
  77. package/docs/details/agent-tooling.md +0 -132
  78. package/docs/details/cli.md +0 -777
  79. package/docs/details/knowledge-base.md +0 -180
  80. package/docs/details/library-api.md +0 -495
  81. package/docs/details/mcp-server.md +0 -482
  82. package/docs/details/search-and-ranking.md +0 -575
  83. package/docs/details/storage.md +0 -531
  84. package/docs/goals/agents.md +0 -751
  85. package/docs/goals/architecture.svg +0 -198
  86. package/docs/goals/auto-capture.md +0 -378
  87. package/docs/goals/benchmarking.md +0 -296
  88. package/docs/goals/cli.md +0 -2654
  89. package/docs/goals/concurrency.md +0 -259
  90. package/docs/goals/content-composer.md +0 -609
  91. package/docs/goals/content-parser.md +0 -279
  92. package/docs/goals/database.md +0 -1679
  93. package/docs/goals/document.md +0 -368
  94. package/docs/goals/hybrid-search.md +0 -465
  95. package/docs/goals/mcp.md +0 -1541
  96. package/docs/goals/overview.md +0 -124
  97. package/docs/goals/query-parser.md +0 -373
  98. package/docs/goals/query-result.md +0 -860
  99. package/docs/goals/semantic-dedup.md +0 -233
  100. package/docs/goals/skills.md +0 -810
  101. package/docs/goals/sync.md +0 -217
  102. package/docs/goals/worker-client.md +0 -1005
  103. package/docs/goals/worker-daemon.md +0 -1547
  104. package/docs/modules/overview.md +0 -319
  105. package/docs/modules/rust/embedding/module.md +0 -91
  106. package/docs/modules/rust/fs-database/functions/document.md +0 -48
  107. package/docs/modules/rust/fs-database/functions/integrity.md +0 -79
  108. package/docs/modules/rust/fs-database/functions/io.md +0 -87
  109. package/docs/modules/rust/fs-database/functions/module.md +0 -22
  110. package/docs/modules/rust/fs-database/functions/query-exec.md +0 -44
  111. package/docs/modules/rust/fs-database/functions/section.md +0 -76
  112. package/docs/modules/rust/fs-database/indexes/btree.md +0 -35
  113. package/docs/modules/rust/fs-database/indexes/corpus.md +0 -61
  114. package/docs/modules/rust/fs-database/indexes/inverted.md +0 -60
  115. package/docs/modules/rust/fs-database/indexes/module.md +0 -64
  116. package/docs/modules/rust/fs-database/indexes/positional.md +0 -39
  117. package/docs/modules/rust/fs-database/indexes/vectors.md +0 -110
  118. package/docs/modules/rust/fs-database/module.md +0 -150
  119. package/docs/modules/rust/fs-database/types/catalog.md +0 -53
  120. package/docs/modules/rust/fs-database/types/module.md +0 -12
  121. package/docs/modules/rust/fs-database/types/section.md +0 -127
  122. package/docs/modules/rust/kb-worker/functions/compose.md +0 -57
  123. package/docs/modules/rust/kb-worker/functions/module.md +0 -20
  124. package/docs/modules/rust/kb-worker/functions/retrieve.md +0 -194
  125. package/docs/modules/rust/kb-worker/functions/search.md +0 -173
  126. package/docs/modules/rust/kb-worker/functions/write.md +0 -172
  127. package/docs/modules/rust/kb-worker/module.md +0 -171
  128. package/docs/modules/rust/kb-worker/types/cache.md +0 -90
  129. package/docs/modules/rust/kb-worker/types/context.md +0 -26
  130. package/docs/modules/rust/kb-worker/types/module.md +0 -16
  131. package/docs/modules/rust/kb-worker/types/status.md +0 -29
  132. package/docs/modules/rust/kb-worker/types/token.md +0 -50
  133. package/docs/modules/rust/overview.md +0 -531
  134. package/docs/modules/rust/query-parser/functions/extract.md +0 -56
  135. package/docs/modules/rust/query-parser/functions/module.md +0 -18
  136. package/docs/modules/rust/query-parser/functions/parse.md +0 -33
  137. package/docs/modules/rust/query-parser/functions/pipeline.md +0 -9
  138. package/docs/modules/rust/query-parser/functions/rank.md +0 -85
  139. package/docs/modules/rust/query-parser/module.md +0 -131
  140. package/docs/modules/rust/query-parser/types/cache.md +0 -39
  141. package/docs/modules/rust/query-parser/types/keyphrase.md +0 -71
  142. package/docs/modules/rust/query-parser/types/module.md +0 -18
  143. package/docs/modules/rust/query-parser/types/token.md +0 -46
  144. package/docs/modules/rust/shared/content-composer/functions.md +0 -114
  145. package/docs/modules/rust/shared/content-composer/module.md +0 -147
  146. package/docs/modules/rust/shared/content-composer/types.md +0 -93
  147. package/docs/modules/rust/shared/content-parser/functions.md +0 -72
  148. package/docs/modules/rust/shared/content-parser/module.md +0 -99
  149. package/docs/modules/rust/shared/content-parser/types.md +0 -71
  150. package/docs/modules/rust/shared/corpus/module.md +0 -24
  151. package/docs/modules/rust/shared/corpus/types.md +0 -141
  152. package/docs/modules/rust/shared/document/module.md +0 -25
  153. package/docs/modules/rust/shared/document/types.md +0 -154
  154. package/docs/modules/rust/shared/encode/module.md +0 -22
  155. package/docs/modules/rust/shared/encode/traits.md +0 -304
  156. package/docs/modules/rust/shared/error/module.md +0 -28
  157. package/docs/modules/rust/shared/error/types.md +0 -302
  158. package/docs/modules/rust/shared/kbid/module.md +0 -24
  159. package/docs/modules/rust/shared/kbid/types.md +0 -147
  160. package/docs/modules/rust/shared/memory/functions.md +0 -209
  161. package/docs/modules/rust/shared/memory/module.md +0 -24
  162. package/docs/modules/rust/shared/module.md +0 -196
  163. package/docs/modules/rust/shared/pipeline/functions.md +0 -141
  164. package/docs/modules/rust/shared/pipeline/module.md +0 -62
  165. package/docs/modules/rust/shared/pipeline/types.md +0 -43
  166. package/docs/modules/rust/shared/query/module.md +0 -27
  167. package/docs/modules/rust/shared/query/types.md +0 -236
  168. package/docs/modules/rust/shared/section/module.md +0 -25
  169. package/docs/modules/rust/shared/section/types.md +0 -294
  170. package/docs/modules/rust/shared/term/module.md +0 -25
  171. package/docs/modules/rust/shared/term/types.md +0 -139
  172. package/docs/modules/typescript/cli.md +0 -131
  173. package/docs/modules/typescript/kbdb-worker.md +0 -150
  174. package/docs/modules/typescript/overview.md +0 -400
  175. package/docs/modules/typescript/shared/auto-capture/module.md +0 -73
  176. package/docs/modules/typescript/shared/cli/constants.md +0 -23
  177. package/docs/modules/typescript/shared/cli/functions.md +0 -809
  178. package/docs/modules/typescript/shared/cli/module.md +0 -138
  179. package/docs/modules/typescript/shared/cli/typings.md +0 -197
  180. package/docs/modules/typescript/shared/embedding/functions.md +0 -200
  181. package/docs/modules/typescript/shared/embedding/module.md +0 -77
  182. package/docs/modules/typescript/shared/embedding/typings.md +0 -124
  183. package/docs/modules/typescript/shared/hash/functions.md +0 -20
  184. package/docs/modules/typescript/shared/hash/module.md +0 -21
  185. package/docs/modules/typescript/shared/log/constants.md +0 -82
  186. package/docs/modules/typescript/shared/log/functions.md +0 -131
  187. package/docs/modules/typescript/shared/log/module.md +0 -153
  188. package/docs/modules/typescript/shared/log/typings.md +0 -82
  189. package/docs/modules/typescript/shared/mcp/classes.md +0 -179
  190. package/docs/modules/typescript/shared/mcp/functions.md +0 -386
  191. package/docs/modules/typescript/shared/mcp/module.md +0 -160
  192. package/docs/modules/typescript/shared/mcp/typings.md +0 -529
  193. package/docs/modules/typescript/shared/module.md +0 -110
  194. package/docs/modules/typescript/shared/platform/functions.md +0 -42
  195. package/docs/modules/typescript/shared/platform/module.md +0 -25
  196. package/docs/modules/typescript/shared/runtime/functions.md +0 -26
  197. package/docs/modules/typescript/shared/runtime/module.md +0 -27
  198. package/docs/modules/typescript/shared/runtime/typings.md +0 -13
  199. package/docs/modules/typescript/shared/version/module.md +0 -27
  200. package/docs/modules/typescript/shared/wasm-codec/functions.md +0 -391
  201. package/docs/modules/typescript/shared/wasm-codec/module.md +0 -98
  202. package/docs/modules/typescript/shared/worker-client/classes.md +0 -264
  203. package/docs/modules/typescript/shared/worker-client/functions.md +0 -175
  204. package/docs/modules/typescript/shared/worker-client/module.md +0 -92
  205. package/docs/modules/typescript/shared/worker-client/typings.md +0 -511
  206. package/docs/modules/typescript/shared/worker-daemon/classes.md +0 -239
  207. package/docs/modules/typescript/shared/worker-daemon/constants.md +0 -37
  208. package/docs/modules/typescript/shared/worker-daemon/functions.md +0 -234
  209. package/docs/modules/typescript/shared/worker-daemon/module.md +0 -118
  210. package/docs/modules/typescript/shared/worker-daemon/typings.md +0 -132
  211. package/docs/overview.md +0 -191
  212. package/docs/release-notes/0.1.0.md +0 -189
  213. package/docs/release-notes/0.1.1.md +0 -46
  214. package/docs/release-notes/0.1.2.md +0 -38
  215. package/docs/release-notes/0.1.3.md +0 -42
  216. package/docs/release-notes/0.2.0.md +0 -147
  217. package/docs/release-notes/0.3.0.md +0 -89
  218. package/docs/release-notes/0.3.1.md +0 -121
  219. package/docs/release-notes/0.3.2.md +0 -64
  220. package/docs/release-notes/0.4.0.md +0 -80
  221. package/docs/release-notes/0.4.1.md +0 -149
  222. package/docs/release-notes/0.4.3.md +0 -60
  223. package/docs/release-notes/0.5.0.md +0 -41
  224. package/docs/release-notes/README.md +0 -16
@@ -1,98 +0,0 @@
1
- # wasm-codec -- WASM Binary Codec Module
2
-
3
- *Encodes TypeScript values into the binary wire format
4
- consumed by `kb-worker.wasm` and decodes binary
5
- responses back into typed TypeScript values.*
6
-
7
- ## Source
8
-
9
- `src/shared/wasm-codec/`
10
-
11
- ## Responsibility
12
-
13
- The wasm-codec module provides the binary serialization
14
- layer between the TypeScript host and the Rust WASM
15
- engine. Each function handles one specific encode or
16
- decode operation -- there is no general-purpose codec
17
- framework.
18
-
19
- **Encoders** convert TypeScript values into
20
- `Uint8Array` buffers that can be written into WASM
21
- linear memory via the host bridge. Conventions:
22
-
23
- - Strings are encoded as a little-endian `u32` length
24
- prefix followed by UTF-8 bytes.
25
- - Optional strings use a `0x00` tag byte for `null`
26
- or `0x01` followed by the encoded string.
27
- - Section types use a `0x00` tag for `text`, `0x01`
28
- for `code`, or `0x02` for `image`, followed by the
29
- MIME type string for non-text types.
30
- - Lists use a little-endian `u32` count prefix
31
- followed by each element in sequence.
32
-
33
- **Decoders** parse `Uint8Array` responses from WASM
34
- exports into typed TypeScript values. Each decoder
35
- returns the parsed value directly (not a
36
- `{ value, offset }` pair at the top level, though
37
- primitive helpers do return `DecodeResult<T>` for
38
- composability).
39
-
40
- The module has no runtime dependencies on the WASM
41
- binary itself -- it only manipulates `Uint8Array`
42
- buffers using `DataView` and `TextEncoder`/
43
- `TextDecoder`.
44
-
45
- ## Directory Layout
46
-
47
- ```
48
- src/shared/wasm-codec/
49
- ├── index.ts
50
- └── functions/
51
- ├── decode-compose-output.function.ts
52
- ├── decode-document-manifest.function.ts
53
- ├── decode-f32.function.ts
54
- ├── decode-key-value-text.function.ts
55
- ├── decode-remove-flag.function.ts
56
- ├── decode-scored-results.function.ts
57
- ├── decode-section-records.function.ts
58
- ├── decode-string.function.ts
59
- ├── decode-u32.function.ts
60
- ├── decode-u64.function.ts
61
- ├── encode-add-section-params.function.ts
62
- ├── encode-group-params.function.ts
63
- ├── encode-identifier-list.function.ts
64
- ├── encode-kbid-list.function.ts
65
- ├── encode-option-string.function.ts
66
- ├── encode-search-params.function.ts
67
- ├── encode-section-type.function.ts
68
- ├── encode-string.function.ts
69
- ├── encode-u32.function.ts
70
- └── encode-update-section-params.function.ts
71
- ```
72
-
73
- ## Exports
74
-
75
- - **[functions.md](functions.md)** -- all encode and
76
- decode functions plus exported data types
77
- (`AddSectionInput`, `ScoredResult`, `TermMatch`,
78
- `SectionRecord`, `DocumentManifest`,
79
- `DocumentSection`, `ComposeOutput`, `DecodeResult`).
80
-
81
- ## Usage
82
-
83
- The module is used exclusively by
84
- `WorkerDaemon.handleRequest` and its private dispatch
85
- helpers. Callers encode parameters before calling a
86
- WASM export and decode the response bytes after the
87
- call returns.
88
-
89
- ```ts
90
- import {
91
- encodeSearchParams,
92
- decodeScoredResults,
93
- } from '../../wasm-codec/index.ts';
94
-
95
- const encoded = encodeSearchParams(query, mode, limit);
96
- // ... write encoded to WASM memory, call export ...
97
- const results = decodeScoredResults(responseBytes);
98
- ```
@@ -1,264 +0,0 @@
1
- # worker-client/classes -- WorkerClient
2
-
3
- *IPC client class. Sends JSON-RPC 2.0 requests to the
4
- worker daemon over a Unix socket or Windows named pipe
5
- and returns typed results.*
6
-
7
- ## Source
8
-
9
- `src/shared/worker-client/classes/`
10
-
11
- ## Classes
12
-
13
- ### `WorkerClient`
14
-
15
- **File:** `worker-client.class.ts`
16
-
17
- Holds the IPC connection and exposes every daemon
18
- operation as a typed async method. Instantiated
19
- directly via the constructor -- not created through a
20
- factory function.
21
-
22
- All methods send a JSON-RPC 2.0 request over the IPC
23
- connection and await the response. The `ctx` parameter
24
- is automatically injected into every request.
25
-
26
- ```ts
27
- class WorkerClient {
28
- constructor(
29
- socketPath: string,
30
- contextId: string,
31
- );
32
-
33
- // Lifecycle
34
- connect(): Promise<void>;
35
- disconnect(): void;
36
-
37
- // Status
38
- status(): Promise<StatusResult>;
39
- dbStatus(): Promise<StatusResult>;
40
-
41
- // Search
42
- search(params: SearchParams):
43
- Promise<PagedSearchResult>;
44
- recall(params: RecallParams):
45
- Promise<RecallResult | RecallResult[]>;
46
-
47
- // Sections
48
- readSections(kbids: string[]):
49
- Promise<Section[]>;
50
- addSection(params: AddSectionParams):
51
- Promise<AddSectionResult>;
52
- updateSection(
53
- kbid: string,
54
- params: AddSectionParams,
55
- ): Promise<string>;
56
- removeSection(kbid: string):
57
- Promise<RemoveSectionResult>;
58
-
59
- // Documents
60
- groupSections(
61
- title: string,
62
- kbids: string[],
63
- ): Promise<string>;
64
- removeGrouping(docid: string): Promise<void>;
65
- retrieveDocument(docid: string): Promise<unknown>;
66
-
67
- // Content composition
68
- content(ids: string[]): Promise<ContentResult>;
69
-
70
- // Maintenance
71
- integrityCheck(): Promise<IntegrityCheckResult>;
72
- gc(): Promise<GcResult>;
73
- rebuildIndexes(): Promise<RebuildResult>;
74
-
75
- // Typed listing
76
- listByType(sectionType: string): Promise<Section[]>;
77
- listDocumentsByType(docType: string): Promise<unknown[]>;
78
-
79
- // Cache
80
- invalidate(kbid: string): Promise<void>;
81
- invalidateAll(): Promise<void>;
82
-
83
- // Version and migration
84
- checkVersion(targetPath: string):
85
- Promise<VersionStatus>;
86
- migrateDatabase(targetPath: string):
87
- Promise<MigrateResult>;
88
-
89
- // Directory initialization
90
- initDirectory(targetPath: string): Promise<void>;
91
- }
92
- ```
93
-
94
- ### Constructor
95
-
96
- | Parameter | Type | Description |
97
- |--------------|----------|-------------------------------|
98
- | `socketPath` | `string` | Absolute IPC socket path or |
99
- | | | Windows named pipe path |
100
- | `contextId` | `string` | 16-character context ID sent |
101
- | | | as `ctx` in every request |
102
-
103
- ### Lifecycle
104
-
105
- #### `connect()`
106
-
107
- Establishes the IPC connection to the worker daemon.
108
- Must be called before any other method.
109
-
110
- #### `disconnect()`
111
-
112
- Destroys the IPC socket and clears the connection
113
- state. The daemon continues running. After
114
- disconnecting, all method calls throw until
115
- `connect()` is called again.
116
-
117
- ### Status Methods
118
-
119
- #### `status()`
120
-
121
- Returns the daemon's current initialization state and
122
- cache statistics.
123
-
124
- #### `dbStatus()`
125
-
126
- Returns database metadata from the daemon's cached
127
- state, including document count, section count, index
128
- size, and cache statistics.
129
-
130
- ### Search
131
-
132
- #### `search(params)`
133
-
134
- Executes a ranked search. Returns a `PagedSearchResult`
135
- containing items sorted by descending relevance score
136
- along with pagination metadata.
137
-
138
- #### `recall(params)`
139
-
140
- Progressive context expansion. Returns section
141
- content, metadata, and optionally parent document
142
- manifests, sibling sections, and forward/back
143
- references depending on the `depth` parameter.
144
- Accepts single `kbid` or batch `kbids`.
145
-
146
- ```ts
147
- recall(params: RecallParams):
148
- Promise<RecallResult | RecallResult[]>;
149
- ```
150
-
151
- ### Section Methods
152
-
153
- #### `addSection(params)`
154
-
155
- Adds a new section. Returns `AddSectionResult` with
156
- the assigned `kbid`.
157
-
158
- #### `updateSection(kbid, params)`
159
-
160
- Replaces a section's content and metadata. Returns the
161
- knowledge-base identifier of the updated section.
162
-
163
- #### `removeSection(kbid)`
164
-
165
- Removes a section and its index entries.
166
-
167
- #### `readSections(kbids)`
168
-
169
- Retrieves one or more sections by their identifiers.
170
-
171
- ### Document Methods
172
-
173
- #### `groupSections(title, kbids)`
174
-
175
- Groups sections under a named document. Returns the
176
- assigned document identifier (`docid`).
177
-
178
- #### `removeGrouping(docid)`
179
-
180
- Removes the document grouping record. The sections
181
- themselves are not deleted.
182
-
183
- #### `retrieveDocument(docid)`
184
-
185
- Returns a document record including its sections.
186
-
187
- ### Typed Listing Methods
188
-
189
- #### `listByType(sectionType)`
190
-
191
- Returns all sections whose `sectionType` field
192
- matches the given value. Used to retrieve all
193
- sections of a particular semantic type (e.g.
194
- `'skill'`) without a search query.
195
-
196
- #### `listDocumentsByType(docType)`
197
-
198
- Returns all document manifests whose `type` field
199
- matches the given value. Used to retrieve all
200
- documents of a particular kind (e.g. `'agent'`).
201
-
202
- ### Content Composition
203
-
204
- #### `content(ids)`
205
-
206
- Composes a rendered Markdown document from one or more
207
- kbid or docid identifiers. IDs are auto-detected
208
- (try kbid first, then docid).
209
-
210
- ### Maintenance Methods
211
-
212
- #### `integrityCheck()`
213
-
214
- Runs a full integrity check on the database.
215
-
216
- #### `gc()`
217
-
218
- Runs garbage collection to remove orphaned sections.
219
-
220
- #### `rebuildIndexes()`
221
-
222
- Rebuilds all indexes from section files and document
223
- manifests on disk.
224
-
225
- ### Cache Methods
226
-
227
- #### `invalidate(kbid)`
228
-
229
- Invalidates the cache entry for a single section.
230
-
231
- #### `invalidateAll()`
232
-
233
- Clears all entries from the daemon's in-memory cache.
234
-
235
- ### Version and Migration Methods
236
-
237
- #### `checkVersion(targetPath)`
238
-
239
- Checks the format version of the `.kbdb` database at
240
- the given path against the version required by the
241
- current WASM engine. Read-only -- makes no changes.
242
-
243
- #### `migrateDatabase(targetPath)`
244
-
245
- Runs all pending database migrations for the `.kbdb`
246
- directory at the given path.
247
-
248
- ### Directory Initialization
249
-
250
- #### `initDirectory(targetPath)`
251
-
252
- Initialises a new `.kbdb` database directory by
253
- sending an `initDirectory` request to the worker
254
- daemon.
255
-
256
- ## Testing
257
-
258
- ```
259
- src/shared/worker-client/
260
- ├── worker-client.spec.ts
261
- └── tests/
262
- ├── client-lifecycle.spec.ts
263
- └── e2e-worker-client.spec.ts
264
- ```
@@ -1,175 +0,0 @@
1
- # worker-client/functions -- Factory and Utilities
2
-
3
- *Factory function that returns a connected
4
- WorkerClient, plus internal utilities for path
5
- resolution, context ID computation, daemon discovery,
6
- spawning, and process checking.*
7
-
8
- ## Source
9
-
10
- `src/shared/worker-client/functions/`
11
-
12
- ## Functions
13
-
14
- ### `createWorkerClient`
15
-
16
- **File:** `create-worker-client.function.ts`
17
-
18
- Factory function. Returns a connected `WorkerClient`
19
- instance ready for use.
20
-
21
- ```ts
22
- async function createWorkerClient(
23
- options?: WorkerClientOptions,
24
- ): Promise<WorkerClient>;
25
- ```
26
-
27
- **Resolution sequence:**
28
-
29
- 1. Resolve context path -- use `options.contextPath`
30
- directly if provided; otherwise call
31
- `resolveDbPath(options.dbPath)` which appends
32
- `/.kbdb` to `dbPath` (or `cwd()`).
33
- 2. Compute the context ID via `computeContextId()`.
34
- 3. Discover a running daemon via `discoverDaemon()`.
35
- 4. If no daemon is running and `autoSpawn !== false`,
36
- spawn one via `spawnDaemon()` then poll with
37
- `waitForDaemon()`.
38
- 5. Connect to the IPC socket and return the connected
39
- `WorkerClient`.
40
-
41
- Throws if the daemon fails to start or the IPC
42
- connection cannot be established.
43
-
44
- ### `resolveDbPath`
45
-
46
- **File:** `resolve-db-path.function.ts`
47
-
48
- Resolves the absolute path to the `.kbdb` database
49
- directory.
50
-
51
- ```ts
52
- function resolveDbPath(
53
- targetDir?: string,
54
- ): string;
55
- ```
56
-
57
- Resolves `targetDir` (or `process.cwd()`) to an
58
- absolute path and appends `/.kbdb`. Throws if the
59
- `.kbdb` directory does not exist at the resolved path.
60
-
61
- @throws {Error} if no `.kbdb` directory is found.
62
-
63
- ### `computeContextId`
64
-
65
- **File:** `compute-context-id.function.ts`
66
-
67
- Computes the context ID from a database directory's
68
- absolute path.
69
-
70
- ```ts
71
- async function computeContextId(
72
- absolutePath: string,
73
- ): Promise<string>;
74
- ```
75
-
76
- Produces the SHA-256 hash of the UTF-8 encoded path
77
- string, then truncates the hex digest to 16 lowercase
78
- characters.
79
-
80
- ```
81
- SHA-256("/home/user/project/.kbdb")
82
- → "a3f1...64 hex chars..."
83
- → "a3f1b2c4d5e6f789" (first 16 chars)
84
- ```
85
-
86
- ### `discoverDaemon`
87
-
88
- **File:** `discover-daemon.function.ts`
89
-
90
- Looks for a running daemon matching the given context
91
- ID. Returns synchronously.
92
-
93
- ```ts
94
- function discoverDaemon(
95
- contextId: string,
96
- ): DaemonInfo | null;
97
- ```
98
-
99
- 1. Look for `kbdb-{contextId}.pid` in the OS temp
100
- directory.
101
- 2. If the PID file exists, read the process ID.
102
- 3. Call `isProcessAlive(pid)` from `shared/platform/`
103
- to verify the process is running.
104
- 4. If alive, return `{ pid, socketPath }`.
105
- 5. If the PID file is missing or the process is dead,
106
- removes the stale PID file and returns `null`.
107
-
108
- ### `spawnDaemon`
109
-
110
- **File:** `spawn-daemon.function.ts`
111
-
112
- Spawns a new worker daemon as a detached child process
113
- and immediately unrefs the child so it does not keep
114
- the parent process alive.
115
-
116
- ```ts
117
- function spawnDaemon(
118
- contextPath: string,
119
- runtime: Runtime,
120
- workerScript?: string,
121
- ): void;
122
- ```
123
-
124
- | Parameter | Description |
125
- |----------------|--------------------------------------|
126
- | `contextPath` | Absolute path to the `.kbdb` dir |
127
- | `runtime` | `'deno'` or `'node'` |
128
- | `workerScript` | Absolute path to the daemon script |
129
-
130
- Throws if `workerScript` is not provided. The worker
131
- script path must be resolved at the entry point by
132
- `resolveWorkerScript` from the `dir-resolver` module.
133
-
134
- **Deno:** spawns `deno run --allow-all <script> <path>`
135
-
136
- **Node.js:** spawns `<execPath> <script> <path>`
137
-
138
- After calling `spawnDaemon`, the caller should use
139
- `waitForDaemon` to poll for the PID file before
140
- attempting to connect.
141
-
142
- ### `waitForDaemon`
143
-
144
- **File:** `wait-for-daemon.function.ts`
145
-
146
- Waits for the worker daemon's PID file to appear on
147
- disk after spawning.
148
-
149
- ```ts
150
- async function waitForDaemon(
151
- contextId: string,
152
- timeoutMs?: number,
153
- ): Promise<boolean>;
154
- ```
155
-
156
- Polls every 100 ms until the PID file at
157
- `<tmpdir>/kbdb-<contextId>.pid` exists or the timeout
158
- elapses. Returns `true` when the file appeared before
159
- the timeout, `false` otherwise.
160
-
161
- `timeoutMs` defaults to `10 000` ms.
162
-
163
- ## Testing
164
-
165
- Each function has a `.spec.ts` at the module root:
166
-
167
- ```
168
- src/shared/worker-client/
169
- ├── create-worker-client.spec.ts
170
- ├── resolve-db-path.spec.ts
171
- ├── compute-context-id.spec.ts
172
- ├── discover-daemon.spec.ts
173
- ├── spawn-daemon.spec.ts
174
- └── wait-for-daemon.spec.ts
175
- ```
@@ -1,92 +0,0 @@
1
- # worker-client -- IPC Client Module
2
-
3
- *Sole IPC client for the worker daemon. Resolves the
4
- database path, computes a context ID, discovers or
5
- spawns the daemon, connects via IPC socket, and
6
- provides a typed async API for all operations.*
7
-
8
- ## Source
9
-
10
- `src/shared/worker-client/`
11
-
12
- ## Responsibility
13
-
14
- The worker-client module is the only TypeScript module
15
- that communicates with the worker daemon. It:
16
-
17
- - Resolves the `.kbdb` database directory path.
18
- - Computes the context ID (SHA-256 of the absolute
19
- path, truncated to 16 hex characters).
20
- - Discovers a running daemon or spawns a new one.
21
- - Connects to the daemon's Unix socket or Windows
22
- named pipe.
23
- - Exposes every daemon IPC method as a typed async
24
- function (JSON-RPC 2.0 over IPC).
25
- - Retries transient connection failures (`ENOENT`,
26
- `ECONNREFUSED`) with exponential backoff.
27
- - Rejects all pending calls on post-connect socket
28
- failure and marks the client as disconnected.
29
- - Exposes `isConnected` state and a cached client
30
- factory (`getOrCreateClient`).
31
- - Handles reconnection when the connection drops.
32
-
33
- The module is **not** exported from the library API
34
- (`mod.ts`). Only `src/cli.ts` imports it.
35
-
36
- ```
37
- src/cli.ts ──→ worker-client ──→ src/worker.ts
38
- ```
39
-
40
- See [Worker Client](../../../../goals/worker-client.md)
41
- for the full behavioral specification.
42
-
43
- ## Directory Layout
44
-
45
- ```
46
- src/shared/worker-client/
47
- ├── index.ts
48
- ├── classes/
49
- │ └── worker-client.class.ts
50
- ├── functions/
51
- │ ├── compute-context-id.function.ts
52
- │ ├── create-worker-client.function.ts
53
- │ ├── discover-daemon.function.ts
54
- │ ├── get-or-create-client.function.ts
55
- │ ├── resolve-db-path.function.ts
56
- │ ├── spawn-daemon.function.ts
57
- │ └── wait-for-daemon.function.ts
58
- ├── typings/
59
- │ ├── add-section-result.model.ts
60
- │ ├── content-result.model.ts
61
- │ ├── daemon-info.model.ts
62
- │ ├── gc-result.model.ts
63
- │ ├── integrity-check-result.model.ts
64
- │ ├── migrate-result.model.ts
65
- │ ├── rebuild-result.model.ts
66
- │ ├── remove-section-result.model.ts
67
- │ ├── search-params.model.ts
68
- │ ├── search-result.model.ts
69
- │ ├── section.model.ts
70
- │ ├── status-result.model.ts
71
- │ ├── version-status.model.ts
72
- │ ├── worker-client-error.interface.ts
73
- │ └── worker-client-options.interface.ts
74
- └── *.spec.ts
75
- ```
76
-
77
- ## Exports
78
-
79
- - **[classes.md](classes.md)** -- `WorkerClient`.
80
- - **[functions.md](functions.md)** --
81
- `createWorkerClient`, `getOrCreateClient`,
82
- `clearClientCache`, `resolveDbPath`,
83
- `computeContextId`, `discoverDaemon`,
84
- `spawnDaemon`, `waitForDaemon`.
85
- - **[typings.md](typings.md)** --
86
- `WorkerClientOptions`, `SearchParams`,
87
- `SearchResult`, `Section`, `AddSectionParams`,
88
- `AddSectionResult`, `RemoveSectionResult`,
89
- `ContentResult`, `StatusResult`, `DaemonInfo`,
90
- `VersionCompatibility`, `VersionStatus`,
91
- `MigrateResult`, `IntegrityCheckResult`,
92
- `GcResult`, `RebuildResult`, `WorkerClientError`.