@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,294 @@
1
+ # types::section -- Section Types and Records
2
+
3
+ *Section type enum, MIME type wrapper, and the data
4
+ structures for section CRUD operations across all
5
+ three WASM modules.*
6
+
7
+ ## File
8
+
9
+ `engine/crates/shared/src/section/section.rs`
10
+
11
+ ## Overview
12
+
13
+ Sections are the atomic content units in the
14
+ knowledge base. Each section has a **type** that
15
+ determines how it is stored, whether it is indexed,
16
+ and how it is rendered. The types in this module are
17
+ shared across `fs-database` (storage), `query-parser`
18
+ (indexing decisions), and `kb-worker` (orchestration).
19
+
20
+ See [Database -- Sections](../../../../../goals/database.md)
21
+ for the behavioral specification of section types,
22
+ embedding markers, and MIME-to-language mapping.
23
+
24
+ ## Type Definitions
25
+
26
+ ### `MimeType`
27
+
28
+ ```rust
29
+ #[derive(Clone, Debug, PartialEq, Eq)]
30
+ pub struct MimeType(String);
31
+
32
+ impl MimeType {
33
+ pub fn new(mime: &str) -> Result<Self, MimeError>;
34
+ pub fn as_str(&self) -> &str;
35
+ pub fn subtype(&self) -> &str;
36
+ }
37
+ ```
38
+
39
+ Newtype wrapper for MIME type strings. Validates
40
+ that the string contains a `/` separator (e.g.
41
+ `"text/typescript"`, `"image/png"`,
42
+ `"application/json"`).
43
+
44
+ `subtype()` returns the part after the `/`:
45
+ `"text/typescript"` -> `"typescript"`,
46
+ `"application/json"` -> `"json"`.
47
+
48
+ ### `SectionType`
49
+
50
+ ```rust
51
+ #[derive(Clone, Debug, PartialEq, Eq)]
52
+ pub enum SectionType {
53
+ Text,
54
+ Code(MimeType),
55
+ Image(MimeType),
56
+ }
57
+ ```
58
+
59
+ Three variants corresponding to the three content
60
+ kinds in the knowledge base:
61
+
62
+ | Variant | Content | Indexed | Embeddable via |
63
+ |---------------|--------------|---------|-------------------|
64
+ | `Text` | Raw text | Yes | `$(text: {kbid})` |
65
+ | `Code(mime)` | Raw source | Yes | `$(code: {kbid})` |
66
+ | `Image(mime)` | Base64 bytes | title/description only | `$(image: {kbid})`|
67
+
68
+ ### `SectionType` Methods
69
+
70
+ ```rust
71
+ impl SectionType {
72
+ pub fn is_indexable(&self) -> bool;
73
+ pub fn as_mime_str(&self) -> &str;
74
+ pub fn from_mime(mime: &str) -> Self;
75
+ pub fn language_tag(&self) -> Option<&str>;
76
+ }
77
+ ```
78
+
79
+ #### `is_indexable`
80
+
81
+ ```rust
82
+ pub fn is_indexable(&self) -> bool {
83
+ matches!(self, Self::Text | Self::Code(_))
84
+ }
85
+ ```
86
+
87
+ Returns `true` for `Text` and `Code`, `false` for
88
+ `Image`. Used by `kb-worker` to decide whether to
89
+ tokenize the section body content. Note:
90
+ `kb-worker` independently indexes image sections'
91
+ `title` (required, heading field 2.0x) and
92
+ `description` (optional, body field 1.0x)
93
+ regardless of `is_indexable()` returning `false`.
94
+
95
+ #### `as_mime_str`
96
+
97
+ Returns the MIME string representation:
98
+
99
+ - `Text` -> `"text/markdown"`
100
+ - `Code(mime)` -> the inner MIME string
101
+ (e.g. `"text/typescript"`)
102
+ - `Image(mime)` -> the inner MIME string
103
+ (e.g. `"image/png"`)
104
+
105
+ #### `from_mime`
106
+
107
+ Parses a MIME string into the appropriate variant:
108
+
109
+ | Input | Result |
110
+ |----------------------|-------------------------------------|
111
+ | `"text/markdown"` | `SectionType::Text` |
112
+ | `"text/typescript"` | `SectionType::Code("text/typescript")` |
113
+ | `"text/javascript"` | `SectionType::Code("text/javascript")` |
114
+ | `"text/html"` | `SectionType::Code("text/html")` |
115
+ | `"text/css"` | `SectionType::Code("text/css")` |
116
+ | `"text/xml"` | `SectionType::Code("text/xml")` |
117
+ | `"text/plain"` | `SectionType::Code("text/plain")` |
118
+ | `"text/*"` (other) | `SectionType::Code("text/{subtype}")` |
119
+ | `"application/json"` | `SectionType::Code("application/json")` |
120
+ | `"image/png"` | `SectionType::Image("image/png")` |
121
+ | `"image/jpeg"` | `SectionType::Image("image/jpeg")` |
122
+ | `"image/*"` (other) | `SectionType::Image("image/{subtype}")` |
123
+
124
+ `"text/markdown"` is the canonical type for
125
+ natural-language text sections. Any other `text/*`
126
+ subtype maps to `Code`.
127
+
128
+ #### `language_tag`
129
+
130
+ Returns the fenced code block language tag for code
131
+ sections. Returns `None` for `Text` and `Image`.
132
+
133
+ | MIME Type | Language Tag |
134
+ |----------------------|--------------|
135
+ | `text/typescript` | `"ts"` |
136
+ | `text/javascript` | `"js"` |
137
+ | `text/html` | `"html"` |
138
+ | `text/css` | `"css"` |
139
+ | `text/xml` | `"xml"` |
140
+ | `text/plain` | `None` |
141
+ | `application/json` | `"json"` |
142
+
143
+ Other `text/*` subtypes return the subtype as the
144
+ language tag (e.g. `"text/python"` -> `"python"`,
145
+ `"text/rust"` -> `"rust"`).
146
+
147
+ ## Section Records
148
+
149
+ Data structures for section CRUD operations. Used in
150
+ IPC request/response payloads and as cache entries.
151
+
152
+ ### `SectionRecord`
153
+
154
+ ```rust
155
+ pub struct SectionRecord {
156
+ pub kbid: KbId,
157
+ pub docids: Vec<String>,
158
+ pub section_type: SectionType,
159
+ pub title: Option<String>,
160
+ pub description: Option<String>,
161
+ pub content: Vec<u8>,
162
+ pub size: u32,
163
+ pub created_at: String,
164
+ pub checksum: String,
165
+ pub token_count: u32,
166
+ pub heading_token_count: u32,
167
+ pub body_token_count: u32,
168
+ pub code_token_count: u32,
169
+ }
170
+ ```
171
+
172
+ Full section data as read from disk. `docids` lists
173
+ the document IDs this section belongs to (may be
174
+ empty, one, or many -- many-to-many relationship).
175
+ `content` is raw UTF-8 bytes for text and code
176
+ sections, or base64-encoded bytes for image sections.
177
+ `size` is the byte length of the content body.
178
+ `title` is required for image sections and optional
179
+ for text/code. `description` is always optional.
180
+ Token counts are pre-computed during per-field
181
+ tokenization at indexing time.
182
+
183
+ ### `AddSectionParams`
184
+
185
+ ```rust
186
+ pub struct AddSectionParams {
187
+ pub content: Vec<u8>,
188
+ pub section_type: SectionType,
189
+ pub title: Option<String>,
190
+ pub description: Option<String>,
191
+ pub docid: Option<String>,
192
+ }
193
+ ```
194
+
195
+ Input for creating a new section. The caller passes
196
+ raw content; `fs-database.wasm` applies the
197
+ preprocessing pipeline internally to text and code
198
+ sections (BOM removal, line ending normalization,
199
+ Unicode NFC, whitespace trimming) before computing
200
+ the kbid. Image content is stored verbatim. The
201
+ kbid is derived from the preprocessed `content` by
202
+ `fs-database` during creation -- it is not provided
203
+ by the caller.
204
+
205
+ `title` is required when `section_type` is
206
+ `Image(*)` -- validation fails without it.
207
+ `description` is always optional. Both are
208
+ tokenized during indexing: title into the heading
209
+ field (2.0x weight), description into the body
210
+ field (1.0x weight).
211
+
212
+ ### `AddSectionResult`
213
+
214
+ ```rust
215
+ pub struct AddSectionResult {
216
+ pub kbid: KbId,
217
+ pub section_type: SectionType,
218
+ pub title: Option<String>,
219
+ pub description: Option<String>,
220
+ pub indexed_terms_count: u32,
221
+ pub token_count: u32,
222
+ pub elapsed_ms: u64,
223
+ pub attached: bool,
224
+ }
225
+ ```
226
+
227
+ `indexed_terms_count` reflects the number of
228
+ distinct terms indexed across all fields. For
229
+ image sections, this is the count from
230
+ title/description tokenization (zero only when
231
+ both produce no terms). `token_count` is the total
232
+ token count across all fields. `attached` is `true`
233
+ when the content already existed and a new docid
234
+ was added to the section's `docids` list instead of
235
+ creating a new section.
236
+
237
+ ### `UpdateSectionResult`
238
+
239
+ ```rust
240
+ pub struct UpdateSectionResult {
241
+ pub old_kbid: KbId,
242
+ pub new_kbid: KbId,
243
+ pub reindexed_terms_count: u32,
244
+ pub elapsed_ms: u64,
245
+ }
246
+ ```
247
+
248
+ Updates produce a new section file with a new kb-id
249
+ (content-addressable). The old kb-id is returned so
250
+ the caller and cache can invalidate stale references.
251
+
252
+ ### `RemoveSectionResult`
253
+
254
+ ```rust
255
+ pub struct RemoveSectionResult {
256
+ pub removed: bool,
257
+ pub elapsed_ms: u64,
258
+ }
259
+ ```
260
+
261
+ `removed` is `false` if the kb-id was not found.
262
+
263
+ ## Trait Implementations
264
+
265
+ All structs implement `Clone`, `Debug`, and `Encode`
266
+ (see [encode](../encode/traits.md)).
267
+ `SectionType` also implements `PartialEq` and `Eq`
268
+ for type comparison.
269
+
270
+ ## Error Type
271
+
272
+ ```rust
273
+ #[derive(Debug)]
274
+ pub enum MimeError {
275
+ MissingSeparator(String),
276
+ EmptyType(String),
277
+ EmptySubtype(String),
278
+ }
279
+ ```
280
+
281
+ ## Testing
282
+
283
+ Unit tests:
284
+
285
+ - `SectionType::from_mime` mapping for all documented
286
+ MIME types.
287
+ - `is_indexable` returns `true` for `Text` and `Code`,
288
+ `false` for `Image`.
289
+ - `language_tag` returns correct tags for known MIME
290
+ types and subtype-as-fallback for unknown ones.
291
+ - `from_mime` with `"text/markdown"` produces `Text`,
292
+ not `Code`.
293
+ - `MimeType::subtype` extracts correctly.
294
+ - `Encode` round-trip for all record types.
@@ -0,0 +1,25 @@
1
+ # term -- Normalized Search Term
2
+
3
+ *Newtype wrapper for normalized, stemmed search terms
4
+ used as keys in inverted indexes and query plans.*
5
+
6
+ ## Source
7
+
8
+ `engine/crates/shared/src/term/`
9
+
10
+ ## Responsibility
11
+
12
+ Defines the `Term` newtype with ordering support for
13
+ B+ tree keys. Terms are the atomic unit of the
14
+ inverted index -- every index lookup, ranking
15
+ computation, and query plan operates on `Term`
16
+ values.
17
+
18
+ See [Query Parser -- Tokenization Pipeline](
19
+ ../../../../goals/query-parser.md) for the pipeline
20
+ that produces terms.
21
+
22
+ ## Exports
23
+
24
+ - **[types.md](types.md)** -- `Term` newtype,
25
+ `from_stemmed`, `TermError`.
@@ -0,0 +1,139 @@
1
+ # types::term -- Normalized Search Term
2
+
3
+ *Newtype wrapper around a normalized, stemmed term
4
+ used as the key in inverted indexes and query plans.*
5
+
6
+ ## File
7
+
8
+ `engine/crates/shared/src/term/term.rs`
9
+
10
+ ## Overview
11
+
12
+ A `Term` represents a single searchable token after
13
+ the full tokenization pipeline has run: Unicode
14
+ normalization, case folding, stemming, and stop word
15
+ removal. Terms are the atomic unit of the inverted
16
+ index -- every index lookup, every ranking
17
+ computation, and every query plan operates on `Term`
18
+ values.
19
+
20
+ See [Query Parser -- Tokenization Pipeline](
21
+ ../../../../../goals/query-parser.md) for the pipeline
22
+ that produces terms.
23
+
24
+ ## Type Definition
25
+
26
+ ```rust
27
+ #[derive(Clone, Debug, PartialEq, Eq,
28
+ Hash, Ord, PartialOrd)]
29
+ pub struct Term(String);
30
+ ```
31
+
32
+ The inner `String` is private. Construction goes
33
+ through `Term::new`, which validates the invariants.
34
+
35
+ ## Invariants
36
+
37
+ A valid `Term`:
38
+
39
+ - Is non-empty.
40
+ - Contains only lowercase characters (post-case-fold).
41
+ - Contains no whitespace.
42
+ - Is not a stop word (stop words are filtered before
43
+ `Term` construction).
44
+ - May be a stemmed form (e.g. `"comput"` from
45
+ `"computing"`).
46
+
47
+ These invariants are enforced at construction time
48
+ by the tokenization pipeline. Code that receives a
49
+ `Term` can rely on them without re-checking.
50
+
51
+ ## Construction
52
+
53
+ ```rust
54
+ impl Term {
55
+ pub fn new(s: &str) -> Result<Self, TermError>;
56
+
57
+ pub fn from_stemmed(s: String) -> Self;
58
+ }
59
+ ```
60
+
61
+ ### `Term::new`
62
+
63
+ Validates the input string against the term
64
+ invariants. Returns `TermError::Empty` for empty
65
+ strings, `TermError::ContainsWhitespace` if
66
+ whitespace is found.
67
+
68
+ This is the safe constructor for terms created
69
+ outside the pipeline (e.g. from deserialized index
70
+ data).
71
+
72
+ ### `Term::from_stemmed`
73
+
74
+ Unchecked constructor for terms produced by the
75
+ stemming stage. The pipeline guarantees the
76
+ invariants, so validation is skipped. This is a
77
+ performance path -- only the tokenization pipeline
78
+ should call it.
79
+
80
+ ## Methods
81
+
82
+ ```rust
83
+ impl Term {
84
+ pub fn as_str(&self) -> &str;
85
+
86
+ pub fn len(&self) -> usize;
87
+ }
88
+ ```
89
+
90
+ ### `as_str`
91
+
92
+ Returns the inner string slice. Used for index
93
+ lookups, display, and serialization.
94
+
95
+ ### `len`
96
+
97
+ Returns the byte length. Used by TF-IDF weighting
98
+ and B+ tree key sizing.
99
+
100
+ ## Trait Implementations
101
+
102
+ | Trait | Purpose |
103
+ |--------------|----------------------------------|
104
+ | `Clone` | Terms are stored in multiple structures (index, cache, results) |
105
+ | `Debug` | Diagnostic output |
106
+ | `PartialEq` / `Eq` | Term matching in index lookups |
107
+ | `Hash` | HashMap keys (inverted index, query cache) |
108
+ | `Ord` / `PartialOrd` | B+ tree key ordering in `fs-database` |
109
+ | `Display` | Formats as the raw term string |
110
+ | `Encode` | Binary serialization for shared memory (see [encode](../encode/traits.md)) |
111
+
112
+ `Ord` **is** derived for `Term` (unlike `KbId`)
113
+ because the B+ tree index requires ordered keys for
114
+ range queries and efficient lookups.
115
+
116
+ ## Error Type
117
+
118
+ ```rust
119
+ #[derive(Debug)]
120
+ pub enum TermError {
121
+ Empty,
122
+ ContainsWhitespace,
123
+ }
124
+ ```
125
+
126
+ ## Testing
127
+
128
+ Unit tests:
129
+
130
+ - `new` accepts lowercase, non-empty, no-whitespace
131
+ strings.
132
+ - `new` rejects empty strings and strings with
133
+ spaces, tabs, or newlines.
134
+ - `from_stemmed` produces a valid term without
135
+ validation overhead.
136
+ - Ordering: terms sort lexicographically
137
+ (`"apple" < "banana"`).
138
+ - `Encode` round-trip: encode then decode produces
139
+ an equal `Term`.
@@ -0,0 +1,129 @@
1
+ # src/cli.ts -- CLI and MCP Executable
2
+
3
+ *Thin entry point that dispatches to CLI mode or MCP
4
+ server mode based on the first argument.*
5
+
6
+ ## File
7
+
8
+ `src/cli.ts`
9
+
10
+ ## Built To
11
+
12
+ | Format | Output | Notes |
13
+ |--------|------------------|----------------|
14
+ | ESM | `dist/cli.mjs` | shebang, npm bin |
15
+ | CJS | `dist/cli.cjs` | shebang |
16
+ | `.d.ts`| `dist/cli.d.ts` | |
17
+
18
+ The npm `bin` entry `kbdb` points to `dist/cli.mjs`.
19
+
20
+ ## Run Modes
21
+
22
+ ```
23
+ kbdb mcp [--db <path>] → MCP server mode
24
+ kbdb <command> [options] → CLI mode (default)
25
+ ```
26
+
27
+ The executable inspects its first argument. If `mcp`,
28
+ it starts as a long-lived MCP server. Otherwise, it
29
+ runs a single CLI command and exits.
30
+
31
+ ## Execution Flow
32
+
33
+ ### CLI Mode
34
+
35
+ ```
36
+ src/cli.ts
37
+
38
+ ├─ import detectRuntime from shared/runtime/
39
+ ├─ import getArgs from shared/runtime/
40
+ ├─ import parseArgs from shared/cli/
41
+ ├─ import createWorkerClient from
42
+ │ shared/worker-client/
43
+
44
+ ├─ detect runtime (Deno / Node.js)
45
+ ├─ argv = getArgs()
46
+ ├─ options = parseArgs(argv)
47
+
48
+ ├─ if options.init:
49
+ │ └─ ensureDb(options.dbPath)
50
+
51
+ ├─ if options.command ≠ 'mcp':
52
+ │ ├─ client = await createWorkerClient(options)
53
+ │ ├─ if options.migrate:
54
+ │ │ └─ run database migration
55
+ │ ├─ match command:
56
+ │ │ ├─ learn → runLearn(client, options)
57
+ │ │ ├─ unlearn → runUnlearn(client, options)
58
+ │ │ ├─ search → runSearch(client, options)
59
+ │ │ ├─ content → runContent(client, options)
60
+ │ │ ├─ check → runCheck(client, options)
61
+ │ │ ├─ gc → runGc(client, options)
62
+ │ │ └─ rebuild → runRebuild(client, options)
63
+ │ ├─ formatOutput(result, options.format)
64
+ │ ├─ print to stdout
65
+ │ └─ client.disconnect()
66
+
67
+ └─ exit
68
+ ```
69
+
70
+ ### MCP Mode
71
+
72
+ ```
73
+ src/cli.ts
74
+
75
+ ├─ argv[0] === 'mcp'
76
+ ├─ resolve --db flag or KBDB_DIR env
77
+ ├─ client = await createWorkerClient({ targetDir })
78
+ ├─ server = createMcpServer(client)
79
+ ├─ server.listen() ← stdio, blocks until closed
80
+ └─ client.disconnect()
81
+ ```
82
+
83
+ ## Imports
84
+
85
+ ```ts
86
+ import {
87
+ detectRuntime,
88
+ } from './src/shared/runtime/functions/detect-runtime.function.ts';
89
+ import {
90
+ getArgs,
91
+ } from './src/shared/runtime/functions/get-args.function.ts';
92
+ import {
93
+ parseArgs,
94
+ } from './src/shared/cli/functions/parse-args.function.ts';
95
+ import {
96
+ createWorkerClient,
97
+ } from './src/shared/worker-client/functions/create-worker-client.function.ts';
98
+ import {
99
+ createMcpServer,
100
+ } from './src/shared/mcp/functions/create-mcp-server.function.ts';
101
+ ```
102
+
103
+ ## No Business Logic
104
+
105
+ This file contains **only** runtime detection, arg
106
+ parsing dispatch, and mode selection. All logic lives
107
+ in the shared sub-modules:
108
+
109
+ | Concern | Module |
110
+ |-------------------|---------------------------|
111
+ | Arg parsing | `shared/cli/` |
112
+ | CLI commands | `shared/cli/` |
113
+ | Output formatting | `shared/cli/` |
114
+ | MCP server | `shared/mcp/` |
115
+ | IPC to daemon | `shared/worker-client/` |
116
+ | Runtime detection | `shared/runtime/` |
117
+
118
+ ## Dual Runtime
119
+
120
+ Works on both Deno and Node.js. Runtime-specific
121
+ code is handled by `shared/runtime/` and
122
+ `shared/platform/`.
123
+
124
+ - **Deno**: `deno run --allow-all src/cli.ts learn ./docs`
125
+ - **Node**: `node dist/cli.mjs learn ./docs`
126
+ - **npm bin**: `kbdb learn ./docs`
127
+
128
+ See [CLI](../../goals/cli.md) for the full behavioral
129
+ specification.