@dikolab/kbdb 0.1.6 → 0.2.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 (199) hide show
  1. package/README.md +30 -9
  2. package/dist/cli.cjs +181 -50
  3. package/dist/cli.cjs.map +3 -3
  4. package/dist/cli.mjs +181 -50
  5. package/dist/cli.mjs.map +3 -3
  6. package/dist/kbdb-worker.cjs +283 -29
  7. package/dist/kbdb-worker.cjs.map +4 -4
  8. package/dist/mod.cjs +13 -2
  9. package/dist/mod.cjs.map +2 -2
  10. package/dist/mod.d.ts +25 -0
  11. package/dist/mod.mjs +13 -2
  12. package/dist/mod.mjs.map +2 -2
  13. package/dist/src/cli.d.ts +8 -0
  14. package/dist/src/shared/cli/functions/format-output.function.d.ts +7 -7
  15. package/dist/src/shared/cli/functions/run-search.function.d.ts +25 -16
  16. package/dist/src/shared/cli/index.d.ts +1 -1
  17. package/dist/src/shared/cli/typings/cli-options.interface.d.ts +2 -0
  18. package/dist/src/shared/cli/typings/search-display.model.d.ts +25 -7
  19. package/dist/src/shared/mcp/functions/handle-recall.function.d.ts +29 -0
  20. package/dist/src/shared/mcp/typings/mcp-client.interface.d.ts +30 -11
  21. package/dist/src/shared/version/constants/version.constant.d.ts +1 -1
  22. package/dist/src/shared/wasm-codec/functions/decode-recall-result.function.d.ts +28 -0
  23. package/dist/src/shared/wasm-codec/functions/decode-scored-results.function.d.ts +14 -7
  24. package/dist/src/shared/wasm-codec/functions/encode-recall-params.function.d.ts +21 -0
  25. package/dist/src/shared/wasm-codec/functions/encode-search-params.function.d.ts +1 -1
  26. package/dist/src/shared/wasm-codec/index.d.ts +5 -2
  27. package/dist/src/shared/worker-client/classes/worker-client.class.d.ts +13 -3
  28. package/dist/src/shared/worker-client/index.d.ts +2 -1
  29. package/dist/src/shared/worker-client/typings/recall-result.model.d.ts +94 -0
  30. package/dist/src/shared/worker-client/typings/search-params.model.d.ts +2 -0
  31. package/dist/src/shared/worker-client/typings/search-result.model.d.ts +23 -7
  32. package/dist/src/shared/worker-daemon/classes/worker-daemon.class.d.ts +1 -0
  33. package/dist/src/worker.d.ts +7 -0
  34. package/dist/wasm/fs-database/kbdb_fs_database.d.ts +18 -0
  35. package/dist/wasm/fs-database/kbdb_fs_database.js +30 -0
  36. package/dist/wasm/fs-database/kbdb_fs_database_bg.wasm +0 -0
  37. package/dist/wasm/fs-database/kbdb_fs_database_bg.wasm.d.ts +2 -0
  38. package/dist/wasm/kb-worker/kbdb_worker.d.ts +38 -0
  39. package/dist/wasm/kb-worker/kbdb_worker.js +56 -0
  40. package/dist/wasm/kb-worker/kbdb_worker_bg.wasm +0 -0
  41. package/dist/wasm/kb-worker/kbdb_worker_bg.wasm.d.ts +3 -0
  42. package/dist/worker.mjs +283 -29
  43. package/dist/worker.mjs.map +4 -4
  44. package/docs/details/README.md +26 -0
  45. package/docs/details/cli.md +608 -0
  46. package/docs/details/library-api.md +406 -0
  47. package/docs/details/mcp-server.md +430 -0
  48. package/docs/details/search-and-ranking.md +457 -0
  49. package/docs/details/storage.md +529 -0
  50. package/docs/goals/agents.md +751 -0
  51. package/docs/goals/architecture.svg +198 -0
  52. package/docs/goals/cli.md +2308 -0
  53. package/docs/goals/content-composer.md +609 -0
  54. package/docs/goals/content-parser.md +279 -0
  55. package/docs/goals/database.md +1452 -0
  56. package/docs/goals/document.md +368 -0
  57. package/docs/goals/mcp.md +1467 -0
  58. package/docs/goals/overview.md +124 -0
  59. package/docs/goals/query-parser.md +271 -0
  60. package/docs/goals/query-result.md +693 -0
  61. package/docs/goals/skills.md +738 -0
  62. package/docs/goals/worker-client.md +944 -0
  63. package/docs/goals/worker-daemon.md +1447 -0
  64. package/docs/modules/overview.md +298 -0
  65. package/docs/modules/rust/fs-database/functions/document.md +48 -0
  66. package/docs/modules/rust/fs-database/functions/integrity.md +68 -0
  67. package/docs/modules/rust/fs-database/functions/io.md +87 -0
  68. package/docs/modules/rust/fs-database/functions/module.md +22 -0
  69. package/docs/modules/rust/fs-database/functions/query-exec.md +44 -0
  70. package/docs/modules/rust/fs-database/functions/section.md +59 -0
  71. package/docs/modules/rust/fs-database/indexes/btree.md +35 -0
  72. package/docs/modules/rust/fs-database/indexes/corpus.md +61 -0
  73. package/docs/modules/rust/fs-database/indexes/inverted.md +60 -0
  74. package/docs/modules/rust/fs-database/indexes/module.md +64 -0
  75. package/docs/modules/rust/fs-database/indexes/positional.md +39 -0
  76. package/docs/modules/rust/fs-database/module.md +149 -0
  77. package/docs/modules/rust/fs-database/types/catalog.md +53 -0
  78. package/docs/modules/rust/fs-database/types/module.md +12 -0
  79. package/docs/modules/rust/fs-database/types/section.md +101 -0
  80. package/docs/modules/rust/kb-worker/functions/compose.md +57 -0
  81. package/docs/modules/rust/kb-worker/functions/module.md +20 -0
  82. package/docs/modules/rust/kb-worker/functions/retrieve.md +194 -0
  83. package/docs/modules/rust/kb-worker/functions/search.md +173 -0
  84. package/docs/modules/rust/kb-worker/functions/write.md +172 -0
  85. package/docs/modules/rust/kb-worker/module.md +171 -0
  86. package/docs/modules/rust/kb-worker/types/cache.md +90 -0
  87. package/docs/modules/rust/kb-worker/types/context.md +26 -0
  88. package/docs/modules/rust/kb-worker/types/module.md +16 -0
  89. package/docs/modules/rust/kb-worker/types/status.md +29 -0
  90. package/docs/modules/rust/kb-worker/types/token.md +50 -0
  91. package/docs/modules/rust/overview.md +495 -0
  92. package/docs/modules/rust/query-parser/functions/extract.md +56 -0
  93. package/docs/modules/rust/query-parser/functions/module.md +16 -0
  94. package/docs/modules/rust/query-parser/functions/parse.md +33 -0
  95. package/docs/modules/rust/query-parser/functions/pipeline.md +103 -0
  96. package/docs/modules/rust/query-parser/functions/rank.md +85 -0
  97. package/docs/modules/rust/query-parser/module.md +136 -0
  98. package/docs/modules/rust/query-parser/types/cache.md +39 -0
  99. package/docs/modules/rust/query-parser/types/keyphrase.md +60 -0
  100. package/docs/modules/rust/query-parser/types/module.md +17 -0
  101. package/docs/modules/rust/query-parser/types/token.md +42 -0
  102. package/docs/modules/rust/shared/content-composer/functions.md +114 -0
  103. package/docs/modules/rust/shared/content-composer/module.md +147 -0
  104. package/docs/modules/rust/shared/content-composer/types.md +93 -0
  105. package/docs/modules/rust/shared/content-parser/functions.md +72 -0
  106. package/docs/modules/rust/shared/content-parser/module.md +99 -0
  107. package/docs/modules/rust/shared/content-parser/types.md +71 -0
  108. package/docs/modules/rust/shared/corpus/module.md +24 -0
  109. package/docs/modules/rust/shared/corpus/types.md +141 -0
  110. package/docs/modules/rust/shared/document/module.md +25 -0
  111. package/docs/modules/rust/shared/document/types.md +154 -0
  112. package/docs/modules/rust/shared/encode/module.md +22 -0
  113. package/docs/modules/rust/shared/encode/traits.md +304 -0
  114. package/docs/modules/rust/shared/error/module.md +28 -0
  115. package/docs/modules/rust/shared/error/types.md +302 -0
  116. package/docs/modules/rust/shared/kbid/module.md +24 -0
  117. package/docs/modules/rust/shared/kbid/types.md +147 -0
  118. package/docs/modules/rust/shared/memory/functions.md +209 -0
  119. package/docs/modules/rust/shared/memory/module.md +24 -0
  120. package/docs/modules/rust/shared/module.md +168 -0
  121. package/docs/modules/rust/shared/query/module.md +27 -0
  122. package/docs/modules/rust/shared/query/types.md +236 -0
  123. package/docs/modules/rust/shared/section/module.md +25 -0
  124. package/docs/modules/rust/shared/section/types.md +294 -0
  125. package/docs/modules/rust/shared/term/module.md +25 -0
  126. package/docs/modules/rust/shared/term/types.md +139 -0
  127. package/docs/modules/typescript/cli.md +129 -0
  128. package/docs/modules/typescript/kbdb-worker.md +135 -0
  129. package/docs/modules/typescript/overview.md +378 -0
  130. package/docs/modules/typescript/shared/cli/constants.md +23 -0
  131. package/docs/modules/typescript/shared/cli/functions.md +630 -0
  132. package/docs/modules/typescript/shared/cli/module.md +125 -0
  133. package/docs/modules/typescript/shared/cli/typings.md +172 -0
  134. package/docs/modules/typescript/shared/hash/functions.md +20 -0
  135. package/docs/modules/typescript/shared/hash/module.md +21 -0
  136. package/docs/modules/typescript/shared/mcp/classes.md +179 -0
  137. package/docs/modules/typescript/shared/mcp/functions.md +386 -0
  138. package/docs/modules/typescript/shared/mcp/module.md +160 -0
  139. package/docs/modules/typescript/shared/mcp/typings.md +522 -0
  140. package/docs/modules/typescript/shared/module.md +94 -0
  141. package/docs/modules/typescript/shared/platform/functions.md +42 -0
  142. package/docs/modules/typescript/shared/platform/module.md +25 -0
  143. package/docs/modules/typescript/shared/runtime/functions.md +26 -0
  144. package/docs/modules/typescript/shared/runtime/module.md +27 -0
  145. package/docs/modules/typescript/shared/runtime/typings.md +13 -0
  146. package/docs/modules/typescript/shared/version/module.md +27 -0
  147. package/docs/modules/typescript/shared/wasm-codec/functions.md +391 -0
  148. package/docs/modules/typescript/shared/wasm-codec/module.md +98 -0
  149. package/docs/modules/typescript/shared/worker-client/classes.md +264 -0
  150. package/docs/modules/typescript/shared/worker-client/functions.md +175 -0
  151. package/docs/modules/typescript/shared/worker-client/module.md +84 -0
  152. package/docs/modules/typescript/shared/worker-client/typings.md +508 -0
  153. package/docs/modules/typescript/shared/worker-daemon/classes.md +239 -0
  154. package/docs/modules/typescript/shared/worker-daemon/constants.md +37 -0
  155. package/docs/modules/typescript/shared/worker-daemon/functions.md +221 -0
  156. package/docs/modules/typescript/shared/worker-daemon/module.md +112 -0
  157. package/docs/modules/typescript/shared/worker-daemon/typings.md +126 -0
  158. package/docs/overview.md +188 -0
  159. package/docs/release-notes/0.1.0.md +189 -0
  160. package/docs/release-notes/0.1.1.md +46 -0
  161. package/docs/release-notes/0.1.2.md +38 -0
  162. package/docs/release-notes/0.1.3.md +42 -0
  163. package/docs/release-notes/0.2.0.md +147 -0
  164. package/docs/release-notes/README.md +9 -0
  165. package/mod.ts +26 -0
  166. package/package.json +8 -2
  167. package/src/cli.ts +9 -0
  168. package/src/shared/cli/functions/dispatch-command.function.ts +3 -3
  169. package/src/shared/cli/functions/format-output.function.ts +41 -34
  170. package/src/shared/cli/functions/parse-args.function.ts +7 -0
  171. package/src/shared/cli/functions/run-search.function.ts +53 -27
  172. package/src/shared/cli/index.ts +4 -1
  173. package/src/shared/cli/typings/cli-options.interface.ts +2 -0
  174. package/src/shared/cli/typings/search-display.model.ts +26 -7
  175. package/src/shared/mcp/functions/create-tool-definitions.function.ts +64 -4
  176. package/src/shared/mcp/functions/handle-recall.function.ts +66 -0
  177. package/src/shared/mcp/functions/handle-tool-call.function.ts +11 -1
  178. package/src/shared/mcp/typings/mcp-client.interface.ts +33 -15
  179. package/src/shared/version/constants/version.constant.ts +1 -1
  180. package/src/shared/wasm-codec/functions/decode-recall-result.function.ts +267 -0
  181. package/src/shared/wasm-codec/functions/decode-scored-results.function.ts +81 -27
  182. package/src/shared/wasm-codec/functions/encode-recall-params.function.ts +68 -0
  183. package/src/shared/wasm-codec/functions/encode-search-params.function.ts +4 -1
  184. package/src/shared/wasm-codec/index.ts +11 -3
  185. package/src/shared/worker-client/classes/worker-client.class.ts +24 -4
  186. package/src/shared/worker-client/index.ts +11 -1
  187. package/src/shared/worker-client/typings/recall-result.model.ts +98 -0
  188. package/src/shared/worker-client/typings/search-params.model.ts +2 -0
  189. package/src/shared/worker-client/typings/search-result.model.ts +24 -7
  190. package/src/shared/worker-daemon/classes/worker-daemon.class.ts +25 -4
  191. package/src/wasm/fs-database/kbdb_fs_database.d.ts +18 -0
  192. package/src/wasm/fs-database/kbdb_fs_database.js +30 -0
  193. package/src/wasm/fs-database/kbdb_fs_database_bg.wasm +0 -0
  194. package/src/wasm/fs-database/kbdb_fs_database_bg.wasm.d.ts +2 -0
  195. package/src/wasm/kb-worker/kbdb_worker.d.ts +38 -0
  196. package/src/wasm/kb-worker/kbdb_worker.js +56 -0
  197. package/src/wasm/kb-worker/kbdb_worker_bg.wasm +0 -0
  198. package/src/wasm/kb-worker/kbdb_worker_bg.wasm.d.ts +3 -0
  199. package/src/worker.ts +8 -0
@@ -0,0 +1,630 @@
1
+ # cli/functions -- Command Functions
2
+
3
+ *CLI orchestrator, command dispatcher, argument parser,
4
+ utility helpers, and one function per CLI sub-command.*
5
+
6
+ ## Source
7
+
8
+ `src/shared/cli/functions/`
9
+
10
+ ## Functions
11
+
12
+ ### `runCli`
13
+
14
+ **File:** `run-cli.function.ts`
15
+
16
+ Full CLI orchestrator. Parses arguments, routes to the
17
+ appropriate sub-flow, and returns a `CommandResult`
18
+ without touching process globals.
19
+
20
+ ```ts
21
+ async function runCli(
22
+ argv: string[],
23
+ deps: CliDeps,
24
+ ): Promise<CommandResult>;
25
+ ```
26
+
27
+ `CliDeps` is an injectable dependency record:
28
+
29
+ ```ts
30
+ interface CliDeps {
31
+ isStdinTty: () => boolean;
32
+ promptUser: (question: string) => Promise<string>;
33
+ createWorkerClient: (opts: {
34
+ contextPath: string;
35
+ }) => Promise<DisconnectableClient>;
36
+ findDb: (basePath?: string) => string | null;
37
+ runInit: (options: CliOptions) => InitResult;
38
+ startMcpServer?: (contextPath: string) => Promise<void>;
39
+ }
40
+ ```
41
+
42
+ Routes:
43
+
44
+ | Command | Handler |
45
+ |-------------|----------------------------------|
46
+ | `help` | returns `HELP_TEXT` |
47
+ | `version` | returns `VERSION` |
48
+ | `init` | calls `deps.runInit` |
49
+ | `mcp` | calls `deps.startMcpServer` |
50
+ | `migrate` | version check + migrate daemon |
51
+ | *(other)* | `dispatchCommand(client, opts)` |
52
+
53
+ ### `dispatchCommand`
54
+
55
+ **File:** `dispatch-command.function.ts`
56
+
57
+ Routes a parsed CLI command to the appropriate handler
58
+ and returns a `CommandResult` without touching process
59
+ globals.
60
+
61
+ ```ts
62
+ async function dispatchCommand(
63
+ client: DispatchClient,
64
+ options: CliOptions,
65
+ io?: LearnIO,
66
+ ): Promise<CommandResult>;
67
+ ```
68
+
69
+ `DispatchClient` is the intersection of all per-command
70
+ client interfaces (`SearchClient & LearnClient &
71
+ UnlearnClient & ContentClient & CheckClient &
72
+ GcClient & RebuildClient & SkillClient & AgentClient`).
73
+
74
+ | Command | Handler |
75
+ |----------------|----------------------------------|
76
+ | `learn` | `runLearn(client, options, io)` |
77
+ | `unlearn` | `runUnlearn(client, options)` |
78
+ | `search` | `runSearch(client, options)` |
79
+ | `content` | `runContent(client, options)` |
80
+ | `check` | `runCheck(client, options)` |
81
+ | `gc` | `runGc(client, options)` |
82
+ | `rebuild` | `runRebuild(client, options)` |
83
+ | `skill-learn` | `runSkillLearn(client, options)` |
84
+ | `skill-list` | `runSkillList(client, options)` |
85
+ | `skill-get` | `runSkillGet(client, options)` |
86
+ | `skill-delete` | `runSkillDelete(client, options)`|
87
+ | `agent-create` | `runAgentCreate(client, options)`|
88
+ | `agent-list` | `runAgentList(client, options)` |
89
+ | `agent-get` | `runAgentGet(client, options)` |
90
+ | `agent-delete` | `runAgentDelete(client, options)`|
91
+
92
+ ### `parseArgs`
93
+
94
+ **File:** `parse-args.function.ts`
95
+
96
+ Parses command-line arguments into a typed `CliOptions`
97
+ object. Supports both Deno (`Deno.args`) and Node.js
98
+ (`process.argv.slice(2)`) argument sources.
99
+
100
+ ```ts
101
+ function parseArgs(
102
+ argv: string[],
103
+ ): CliOptions;
104
+ ```
105
+
106
+ Throws on invalid or missing required arguments.
107
+ Prints help text and exits for `--help` / `--version`.
108
+
109
+ **Compound command handling**: when the first positional
110
+ argument is `db`, `skill`, or `agent`, the parser
111
+ consumes the next positional as the subcommand and sets
112
+ `command` to the hyphenated form:
113
+
114
+ | First positional | Second positional | `command` value |
115
+ |------------------|-------------------|------------------|
116
+ | `db` | `init` | `db-init` |
117
+ | `db` | `migrate` | `db-migrate` |
118
+ | `skill` | `learn` | `skill-learn` |
119
+ | `skill` | `list` | `skill-list` |
120
+ | `skill` | `get` | `skill-get` |
121
+ | `skill` | `delete` | `skill-delete` |
122
+ | `agent` | `create` | `agent-create` |
123
+ | `agent` | `list` | `agent-list` |
124
+ | `agent` | `get` | `agent-get` |
125
+ | `agent` | `delete` | `agent-delete` |
126
+
127
+ **New options parsed:**
128
+
129
+ | Flag | `CliOptions` field | Description |
130
+ |-----------------|--------------------|-----------------------|
131
+ | `--name <n>` | `name` | Skill or agent name |
132
+ | `--persona <p>` | `persona` | Agent persona file |
133
+ | `--body <b>` | `body` | Skill body file |
134
+ | `--skills <s>` | `skills` | Comma-separated kbids |
135
+
136
+ ### `findDb`
137
+
138
+ **File:** `find-db.function.ts`
139
+
140
+ Resolves the absolute path to the `.kbdb` directory
141
+ under the given base path. Returns `null` when the
142
+ directory does not exist. Pure read-only -- never
143
+ creates the directory.
144
+
145
+ ```ts
146
+ function findDb(
147
+ dbPath?: string,
148
+ ): string | null;
149
+ ```
150
+
151
+ ### `runInit`
152
+
153
+ **File:** `run-init.function.ts`
154
+
155
+ Creates the `.kbdb` directory scaffold directly on disk
156
+ without requiring a running worker daemon.
157
+
158
+ ```ts
159
+ function runInit(
160
+ options: CliOptions,
161
+ ): InitResult;
162
+ ```
163
+
164
+ ```ts
165
+ interface InitResult {
166
+ readonly ok: boolean;
167
+ readonly targetPath: string;
168
+ readonly errorMessage?: string;
169
+ }
170
+ ```
171
+
172
+ ### `runMigrate`
173
+
174
+ **File:** `run-migrate.function.ts`
175
+
176
+ Checks the database version and runs pending migrations
177
+ when needed.
178
+
179
+ ```ts
180
+ async function runMigrate(
181
+ client: MigrateClient,
182
+ options: CliOptions,
183
+ ): Promise<MigrateCommandResult>;
184
+ ```
185
+
186
+ ```ts
187
+ interface MigrateCommandResult {
188
+ readonly status: VersionCompatibility;
189
+ readonly versionStatus: VersionStatus;
190
+ readonly migrateResult?: MigrateResult;
191
+ }
192
+ ```
193
+
194
+ Resolution order:
195
+
196
+ | Version status | Action |
197
+ |-------------------|-------------------------------------|
198
+ | `compatible` | returns without running migrations |
199
+ | `needs_migration` | calls `client.migrateDatabase` |
200
+ | `too_new` | returns without running migrations |
201
+
202
+ ### `runLearn`
203
+
204
+ **File:** `run-learn.function.ts`
205
+
206
+ Imports and indexes documents into the knowledge base.
207
+
208
+ ```ts
209
+ async function runLearn(
210
+ client: LearnClient,
211
+ options: CliOptions,
212
+ io?: LearnIO,
213
+ ): Promise<LearnResult[]>;
214
+ ```
215
+
216
+ - If input is a **directory**: recursively scans for
217
+ supported text files (`.md`, `.txt`, `.rst`,
218
+ `.adoc`). Image files are silently skipped during
219
+ directory scans. Computes content hash for each,
220
+ skips duplicates, calls `client.addSection()` per
221
+ file.
222
+ - If input is a **text file**: adds that single file.
223
+ `--title` and `--description` are optional.
224
+ - If input is **stdin** (`-`): reads `text/markdown`.
225
+ - Returns a `LearnResult[]` with one record per file
226
+ processed.
227
+
228
+ ### `runUnlearn`
229
+
230
+ **File:** `run-unlearn.function.ts`
231
+
232
+ Removes knowledge base contents by KB ID.
233
+
234
+ ```ts
235
+ async function runUnlearn(
236
+ client: UnlearnClient,
237
+ options: CliOptions,
238
+ ): Promise<unknown>;
239
+ ```
240
+
241
+ Accepts a list of KB IDs from `options.args`. Calls
242
+ `client.removeSection()` for each.
243
+
244
+ ### `runSearch`
245
+
246
+ **File:** `run-search.function.ts`
247
+
248
+ Searches the knowledge base by query string.
249
+
250
+ ```ts
251
+ async function runSearch(
252
+ client: SearchClient,
253
+ options: CliOptions,
254
+ ): Promise<SearchDisplay[]>;
255
+ ```
256
+
257
+ Calls `client.search()` with the query, mode, and
258
+ limit from options. Returns `SearchDisplay[]` for
259
+ use by `dispatchCommand`.
260
+
261
+ ### `runContent`
262
+
263
+ **File:** `run-content.function.ts`
264
+
265
+ Composes and outputs a rendered Markdown document from
266
+ one or more kbid or docid identifiers.
267
+
268
+ ```ts
269
+ async function runContent(
270
+ client: ContentClient,
271
+ options: CliOptions,
272
+ ): Promise<unknown>;
273
+ ```
274
+
275
+ Takes identifiers from `options.args`. Calls
276
+ `client.content(ids)`.
277
+
278
+ ### `runCheck`
279
+
280
+ **File:** `run-check.function.ts`
281
+
282
+ Runs an integrity check on the database.
283
+
284
+ ```ts
285
+ async function runCheck(
286
+ client: CheckClient,
287
+ options: CliOptions,
288
+ ): Promise<{ ok: boolean }>;
289
+ ```
290
+
291
+ Calls `client.integrityCheck()`.
292
+
293
+ ### `runGc`
294
+
295
+ **File:** `run-gc.function.ts`
296
+
297
+ Runs garbage collection on the database.
298
+
299
+ ```ts
300
+ async function runGc(
301
+ client: GcClient,
302
+ options: CliOptions,
303
+ ): Promise<unknown>;
304
+ ```
305
+
306
+ Calls `client.gc()`.
307
+
308
+ ### `runRebuild`
309
+
310
+ **File:** `run-rebuild.function.ts`
311
+
312
+ Rebuilds all indexes from sections and documents.
313
+
314
+ ```ts
315
+ async function runRebuild(
316
+ client: RebuildClient,
317
+ options: CliOptions,
318
+ ): Promise<unknown>;
319
+ ```
320
+
321
+ Calls `client.rebuildIndexes()`.
322
+
323
+ ### `ensureDb`
324
+
325
+ **File:** `ensure-db.function.ts`
326
+
327
+ Ensures the database directory exists, prompting the
328
+ user if stdin is a TTY or failing if stdin is piped.
329
+
330
+ ```ts
331
+ async function ensureDb(
332
+ dbPath: string,
333
+ ): Promise<void>;
334
+ ```
335
+
336
+ ### `formatOutput`
337
+
338
+ **File:** `format-output.function.ts`
339
+
340
+ Formats a result object for the selected output format.
341
+
342
+ ```ts
343
+ function formatOutput(
344
+ result: unknown,
345
+ format: OutputFormat,
346
+ ): string;
347
+ ```
348
+
349
+ | Format | Output |
350
+ |--------|-----------------------------|
351
+ | `json` | JSON string (default) |
352
+ | `text` | Human-readable list |
353
+ | `mcp` | MCP tool result format |
354
+
355
+ ### `errorToMessage`
356
+
357
+ **File:** `error-to-message.function.ts`
358
+
359
+ Converts an unknown thrown value to a human-readable
360
+ message.
361
+
362
+ ```ts
363
+ function errorToMessage(
364
+ err: unknown,
365
+ ): string;
366
+ ```
367
+
368
+ Returns `err.message` for `Error` instances, or
369
+ `String(err)` for all other values.
370
+
371
+ ### `isStdinTty`
372
+
373
+ **File:** `is-stdin-tty.function.ts`
374
+
375
+ Returns `true` when stdin is an interactive terminal.
376
+
377
+ ```ts
378
+ function isStdinTty(
379
+ stdin?: NodeJS.ReadStream,
380
+ ): boolean;
381
+ ```
382
+
383
+ `stdin` defaults to `process.stdin`.
384
+
385
+ ### `promptUser`
386
+
387
+ **File:** `prompt-user.function.ts`
388
+
389
+ Prompts the user on stdin (TTY only) and returns the
390
+ trimmed reply.
391
+
392
+ ```ts
393
+ async function promptUser(
394
+ question: string,
395
+ io?: PromptIO,
396
+ ): Promise<string>;
397
+ ```
398
+
399
+ `io` is an injectable interface for testing:
400
+
401
+ ```ts
402
+ interface PromptIO {
403
+ ask: (question: string) => Promise<string>;
404
+ }
405
+ ```
406
+
407
+ Defaults to readline on `process.stdin`/`process.stdout`.
408
+
409
+ ### `runDbInit`
410
+
411
+ **File:** `run-db-init.function.ts`
412
+
413
+ Creates the `.kbdb` directory scaffold directly on disk
414
+ without requiring a running worker daemon. Replaces
415
+ the deprecated `runInit` for the `kbdb db init`
416
+ subcommand.
417
+
418
+ ```ts
419
+ async function runDbInit(
420
+ options: CliOptions,
421
+ ): Promise<CommandResult>;
422
+ ```
423
+
424
+ Output on success: JSON `{ "path": "...", "created": true }`.
425
+
426
+ ### `runDbMigrate`
427
+
428
+ **File:** `run-db-migrate.function.ts`
429
+
430
+ Checks the database version and runs pending migrations
431
+ when needed. Successor to the deprecated `runMigrate`
432
+ for the `kbdb db migrate` subcommand.
433
+
434
+ ```ts
435
+ async function runDbMigrate(
436
+ client: MigrateClient,
437
+ options: CliOptions,
438
+ ): Promise<CommandResult>;
439
+ ```
440
+
441
+ Calls `client.migrateDatabase(dbDir)` after resolving
442
+ the database directory from options.
443
+
444
+ ### `runSkillLearn`
445
+
446
+ **File:** `run-skill-learn.function.ts`
447
+
448
+ Creates a new skill definition in the knowledge base.
449
+ Reads the skill body from the file path in
450
+ `options.body` (or stdin when `options.body` is `-`),
451
+ constructs the skill JSON, and adds it as a section
452
+ with `sectionType: 'skill'`.
453
+
454
+ ```ts
455
+ async function runSkillLearn(
456
+ client: SkillLearnClient,
457
+ options: CliOptions,
458
+ ): Promise<CommandResult>;
459
+ ```
460
+
461
+ | Parameter | Source | Required |
462
+ |-------------------|-----------------|----------|
463
+ | `options.name` | `--name` | yes |
464
+ | `options.body` | `--body` | yes |
465
+ | `options.description` | `--description` | no |
466
+
467
+ Output: JSON `{ "kbid": "...", "name": "...",
468
+ "arguments": ["argName", ...] }`.
469
+
470
+ ### `runSkillList`
471
+
472
+ **File:** `run-skill-list.function.ts`
473
+
474
+ Lists all skill sections stored in the knowledge base.
475
+
476
+ ```ts
477
+ async function runSkillList(
478
+ client: SkillListClient,
479
+ options: CliOptions,
480
+ ): Promise<CommandResult>;
481
+ ```
482
+
483
+ Calls `client.listByType('skill')`. Output: JSON array
484
+ of `{ kbid, name, description, argumentCount }`.
485
+
486
+ ### `runSkillGet`
487
+
488
+ **File:** `run-skill-get.function.ts`
489
+
490
+ Retrieves a single skill by name or kbid.
491
+
492
+ ```ts
493
+ async function runSkillGet(
494
+ client: SkillGetClient,
495
+ options: CliOptions,
496
+ ): Promise<CommandResult>;
497
+ ```
498
+
499
+ The name-or-kbid argument is taken from `options.args[0]`.
500
+ Output: JSON `SkillDefinition` with `kbid` field.
501
+
502
+ ### `runSkillDelete`
503
+
504
+ **File:** `run-skill-delete.function.ts`
505
+
506
+ Removes a skill section from the knowledge base.
507
+
508
+ ```ts
509
+ async function runSkillDelete(
510
+ client: SkillDeleteClient,
511
+ options: CliOptions,
512
+ ): Promise<CommandResult>;
513
+ ```
514
+
515
+ The kbid argument is taken from `options.args[0]`.
516
+ Calls `client.removeSection(kbid)`. Output: JSON
517
+ `{ "kbid": "...", "removed": true }`.
518
+
519
+ ### `runAgentCreate`
520
+
521
+ **File:** `run-agent-create.function.ts`
522
+
523
+ Creates a new agent document with a persona and
524
+ ordered skill references. Reads the persona from the
525
+ file path in `options.persona` (or stdin when `-`),
526
+ validates the skill kbids from `options.skills`, and
527
+ creates the agent document manifest.
528
+
529
+ ```ts
530
+ async function runAgentCreate(
531
+ client: AgentCreateClient,
532
+ options: CliOptions,
533
+ ): Promise<CommandResult>;
534
+ ```
535
+
536
+ | Parameter | Source | Required |
537
+ |---------------------|-------------|----------|
538
+ | `options.name` | `--name` | yes |
539
+ | `options.persona` | `--persona` | yes |
540
+ | `options.skills` | `--skills` | yes |
541
+ | `options.description` | `--description` | no |
542
+
543
+ Output: JSON `{ "docid": "agent-...", "name": "...",
544
+ "skills": [...] }`.
545
+
546
+ ### `runAgentList`
547
+
548
+ **File:** `run-agent-list.function.ts`
549
+
550
+ Lists all agent documents stored in the knowledge base.
551
+
552
+ ```ts
553
+ async function runAgentList(
554
+ client: AgentListClient,
555
+ options: CliOptions,
556
+ ): Promise<CommandResult>;
557
+ ```
558
+
559
+ Calls `client.listDocumentsByType('agent')`. Output:
560
+ JSON array of `{ docid, name, description, skillCount }`.
561
+
562
+ ### `runAgentGet`
563
+
564
+ **File:** `run-agent-get.function.ts`
565
+
566
+ Retrieves a single agent by document ID, with resolved
567
+ skill definitions.
568
+
569
+ ```ts
570
+ async function runAgentGet(
571
+ client: AgentGetClient,
572
+ options: CliOptions,
573
+ ): Promise<CommandResult>;
574
+ ```
575
+
576
+ The docid argument is taken from `options.args[0]`.
577
+ Output: JSON `AgentDefinition` with resolved skills.
578
+
579
+ ### `runAgentDelete`
580
+
581
+ **File:** `run-agent-delete.function.ts`
582
+
583
+ Removes an agent document manifest. The skill sections
584
+ referenced by the agent are not deleted.
585
+
586
+ ```ts
587
+ async function runAgentDelete(
588
+ client: AgentDeleteClient,
589
+ options: CliOptions,
590
+ ): Promise<CommandResult>;
591
+ ```
592
+
593
+ The docid argument is taken from `options.args[0]`.
594
+ Output: JSON `{ "docid": "...", "removed": true }`.
595
+
596
+ ## Testing
597
+
598
+ Each function has a `.spec.ts` at the module root:
599
+
600
+ ```
601
+ src/shared/cli/
602
+ ├── run-cli.spec.ts
603
+ ├── dispatch-command.spec.ts
604
+ ├── parse-args.spec.ts
605
+ ├── find-db.spec.ts
606
+ ├── run-init.spec.ts
607
+ ├── run-migrate.spec.ts
608
+ ├── run-db-init.spec.ts
609
+ ├── run-db-migrate.spec.ts
610
+ ├── run-learn.spec.ts
611
+ ├── run-unlearn.spec.ts
612
+ ├── run-search.spec.ts
613
+ ├── run-content.spec.ts
614
+ ├── run-check.spec.ts
615
+ ├── run-gc.spec.ts
616
+ ├── run-rebuild.spec.ts
617
+ ├── run-skill-learn.spec.ts
618
+ ├── run-skill-list.spec.ts
619
+ ├── run-skill-get.spec.ts
620
+ ├── run-skill-delete.spec.ts
621
+ ├── run-agent-create.spec.ts
622
+ ├── run-agent-list.spec.ts
623
+ ├── run-agent-get.spec.ts
624
+ ├── run-agent-delete.spec.ts
625
+ ├── ensure-db.spec.ts
626
+ ├── format-output.spec.ts
627
+ ├── error-to-message.spec.ts
628
+ ├── is-stdin-tty.spec.ts
629
+ └── prompt-user.spec.ts
630
+ ```