@dikolab/kbdb 0.4.3 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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 -27
  5. package/README.md.bak +320 -0
  6. package/dist/README.md +320 -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/kbdb_fs_migration_bg.wasm +0 -0
  66. package/dist/wasm/fs-migration/package.json +1 -1
  67. package/dist/wasm/kb-worker/kbdb_worker_bg.wasm +0 -0
  68. package/dist/wasm/kb-worker/package.json +1 -1
  69. package/dist/wasm/query-parser/kbdb_query_parser_bg.wasm +0 -0
  70. package/dist/wasm/query-parser/package.json +1 -1
  71. package/dist/worker.d.ts +0 -7
  72. package/dist/worker.mjs +37 -6
  73. package/dist/worker.mjs.map +3 -3
  74. package/package.json +13 -12
  75. package/dist/chunk-BJXEVALU.mjs.map +0 -7
  76. package/dist/src/shared/cli/functions/resolve-raw-db-path.function.d.ts +0 -10
  77. package/docs/details/README.md +0 -30
  78. package/docs/details/agent-tooling.md +0 -132
  79. package/docs/details/cli.md +0 -777
  80. package/docs/details/knowledge-base.md +0 -180
  81. package/docs/details/library-api.md +0 -495
  82. package/docs/details/mcp-server.md +0 -482
  83. package/docs/details/search-and-ranking.md +0 -575
  84. package/docs/details/storage.md +0 -531
  85. package/docs/goals/agents.md +0 -751
  86. package/docs/goals/architecture.svg +0 -198
  87. package/docs/goals/auto-capture.md +0 -378
  88. package/docs/goals/benchmarking.md +0 -296
  89. package/docs/goals/cli.md +0 -2654
  90. package/docs/goals/concurrency.md +0 -259
  91. package/docs/goals/content-composer.md +0 -609
  92. package/docs/goals/content-parser.md +0 -279
  93. package/docs/goals/database.md +0 -1679
  94. package/docs/goals/document.md +0 -368
  95. package/docs/goals/hybrid-search.md +0 -465
  96. package/docs/goals/mcp.md +0 -1541
  97. package/docs/goals/overview.md +0 -124
  98. package/docs/goals/query-parser.md +0 -373
  99. package/docs/goals/query-result.md +0 -860
  100. package/docs/goals/semantic-dedup.md +0 -233
  101. package/docs/goals/skills.md +0 -810
  102. package/docs/goals/sync.md +0 -217
  103. package/docs/goals/worker-client.md +0 -1005
  104. package/docs/goals/worker-daemon.md +0 -1547
  105. package/docs/modules/overview.md +0 -319
  106. package/docs/modules/rust/embedding/module.md +0 -91
  107. package/docs/modules/rust/fs-database/functions/document.md +0 -48
  108. package/docs/modules/rust/fs-database/functions/integrity.md +0 -79
  109. package/docs/modules/rust/fs-database/functions/io.md +0 -87
  110. package/docs/modules/rust/fs-database/functions/module.md +0 -22
  111. package/docs/modules/rust/fs-database/functions/query-exec.md +0 -44
  112. package/docs/modules/rust/fs-database/functions/section.md +0 -76
  113. package/docs/modules/rust/fs-database/indexes/btree.md +0 -35
  114. package/docs/modules/rust/fs-database/indexes/corpus.md +0 -61
  115. package/docs/modules/rust/fs-database/indexes/inverted.md +0 -60
  116. package/docs/modules/rust/fs-database/indexes/module.md +0 -64
  117. package/docs/modules/rust/fs-database/indexes/positional.md +0 -39
  118. package/docs/modules/rust/fs-database/indexes/vectors.md +0 -110
  119. package/docs/modules/rust/fs-database/module.md +0 -150
  120. package/docs/modules/rust/fs-database/types/catalog.md +0 -53
  121. package/docs/modules/rust/fs-database/types/module.md +0 -12
  122. package/docs/modules/rust/fs-database/types/section.md +0 -127
  123. package/docs/modules/rust/kb-worker/functions/compose.md +0 -57
  124. package/docs/modules/rust/kb-worker/functions/module.md +0 -20
  125. package/docs/modules/rust/kb-worker/functions/retrieve.md +0 -194
  126. package/docs/modules/rust/kb-worker/functions/search.md +0 -173
  127. package/docs/modules/rust/kb-worker/functions/write.md +0 -172
  128. package/docs/modules/rust/kb-worker/module.md +0 -171
  129. package/docs/modules/rust/kb-worker/types/cache.md +0 -90
  130. package/docs/modules/rust/kb-worker/types/context.md +0 -26
  131. package/docs/modules/rust/kb-worker/types/module.md +0 -16
  132. package/docs/modules/rust/kb-worker/types/status.md +0 -29
  133. package/docs/modules/rust/kb-worker/types/token.md +0 -50
  134. package/docs/modules/rust/overview.md +0 -531
  135. package/docs/modules/rust/query-parser/functions/extract.md +0 -56
  136. package/docs/modules/rust/query-parser/functions/module.md +0 -18
  137. package/docs/modules/rust/query-parser/functions/parse.md +0 -33
  138. package/docs/modules/rust/query-parser/functions/pipeline.md +0 -9
  139. package/docs/modules/rust/query-parser/functions/rank.md +0 -85
  140. package/docs/modules/rust/query-parser/module.md +0 -131
  141. package/docs/modules/rust/query-parser/types/cache.md +0 -39
  142. package/docs/modules/rust/query-parser/types/keyphrase.md +0 -71
  143. package/docs/modules/rust/query-parser/types/module.md +0 -18
  144. package/docs/modules/rust/query-parser/types/token.md +0 -46
  145. package/docs/modules/rust/shared/content-composer/functions.md +0 -114
  146. package/docs/modules/rust/shared/content-composer/module.md +0 -147
  147. package/docs/modules/rust/shared/content-composer/types.md +0 -93
  148. package/docs/modules/rust/shared/content-parser/functions.md +0 -72
  149. package/docs/modules/rust/shared/content-parser/module.md +0 -99
  150. package/docs/modules/rust/shared/content-parser/types.md +0 -71
  151. package/docs/modules/rust/shared/corpus/module.md +0 -24
  152. package/docs/modules/rust/shared/corpus/types.md +0 -141
  153. package/docs/modules/rust/shared/document/module.md +0 -25
  154. package/docs/modules/rust/shared/document/types.md +0 -154
  155. package/docs/modules/rust/shared/encode/module.md +0 -22
  156. package/docs/modules/rust/shared/encode/traits.md +0 -304
  157. package/docs/modules/rust/shared/error/module.md +0 -28
  158. package/docs/modules/rust/shared/error/types.md +0 -302
  159. package/docs/modules/rust/shared/kbid/module.md +0 -24
  160. package/docs/modules/rust/shared/kbid/types.md +0 -147
  161. package/docs/modules/rust/shared/memory/functions.md +0 -209
  162. package/docs/modules/rust/shared/memory/module.md +0 -24
  163. package/docs/modules/rust/shared/module.md +0 -196
  164. package/docs/modules/rust/shared/pipeline/functions.md +0 -141
  165. package/docs/modules/rust/shared/pipeline/module.md +0 -62
  166. package/docs/modules/rust/shared/pipeline/types.md +0 -43
  167. package/docs/modules/rust/shared/query/module.md +0 -27
  168. package/docs/modules/rust/shared/query/types.md +0 -236
  169. package/docs/modules/rust/shared/section/module.md +0 -25
  170. package/docs/modules/rust/shared/section/types.md +0 -294
  171. package/docs/modules/rust/shared/term/module.md +0 -25
  172. package/docs/modules/rust/shared/term/types.md +0 -139
  173. package/docs/modules/typescript/cli.md +0 -131
  174. package/docs/modules/typescript/kbdb-worker.md +0 -150
  175. package/docs/modules/typescript/overview.md +0 -400
  176. package/docs/modules/typescript/shared/auto-capture/module.md +0 -73
  177. package/docs/modules/typescript/shared/cli/constants.md +0 -23
  178. package/docs/modules/typescript/shared/cli/functions.md +0 -809
  179. package/docs/modules/typescript/shared/cli/module.md +0 -138
  180. package/docs/modules/typescript/shared/cli/typings.md +0 -197
  181. package/docs/modules/typescript/shared/embedding/functions.md +0 -200
  182. package/docs/modules/typescript/shared/embedding/module.md +0 -77
  183. package/docs/modules/typescript/shared/embedding/typings.md +0 -124
  184. package/docs/modules/typescript/shared/hash/functions.md +0 -20
  185. package/docs/modules/typescript/shared/hash/module.md +0 -21
  186. package/docs/modules/typescript/shared/log/constants.md +0 -82
  187. package/docs/modules/typescript/shared/log/functions.md +0 -131
  188. package/docs/modules/typescript/shared/log/module.md +0 -153
  189. package/docs/modules/typescript/shared/log/typings.md +0 -82
  190. package/docs/modules/typescript/shared/mcp/classes.md +0 -179
  191. package/docs/modules/typescript/shared/mcp/functions.md +0 -386
  192. package/docs/modules/typescript/shared/mcp/module.md +0 -160
  193. package/docs/modules/typescript/shared/mcp/typings.md +0 -529
  194. package/docs/modules/typescript/shared/module.md +0 -110
  195. package/docs/modules/typescript/shared/platform/functions.md +0 -42
  196. package/docs/modules/typescript/shared/platform/module.md +0 -25
  197. package/docs/modules/typescript/shared/runtime/functions.md +0 -26
  198. package/docs/modules/typescript/shared/runtime/module.md +0 -27
  199. package/docs/modules/typescript/shared/runtime/typings.md +0 -13
  200. package/docs/modules/typescript/shared/version/module.md +0 -27
  201. package/docs/modules/typescript/shared/wasm-codec/functions.md +0 -391
  202. package/docs/modules/typescript/shared/wasm-codec/module.md +0 -98
  203. package/docs/modules/typescript/shared/worker-client/classes.md +0 -264
  204. package/docs/modules/typescript/shared/worker-client/functions.md +0 -175
  205. package/docs/modules/typescript/shared/worker-client/module.md +0 -92
  206. package/docs/modules/typescript/shared/worker-client/typings.md +0 -511
  207. package/docs/modules/typescript/shared/worker-daemon/classes.md +0 -239
  208. package/docs/modules/typescript/shared/worker-daemon/constants.md +0 -37
  209. package/docs/modules/typescript/shared/worker-daemon/functions.md +0 -234
  210. package/docs/modules/typescript/shared/worker-daemon/module.md +0 -118
  211. package/docs/modules/typescript/shared/worker-daemon/typings.md +0 -132
  212. package/docs/overview.md +0 -191
  213. package/docs/release-notes/0.1.0.md +0 -189
  214. package/docs/release-notes/0.1.1.md +0 -46
  215. package/docs/release-notes/0.1.2.md +0 -38
  216. package/docs/release-notes/0.1.3.md +0 -42
  217. package/docs/release-notes/0.2.0.md +0 -147
  218. package/docs/release-notes/0.3.0.md +0 -89
  219. package/docs/release-notes/0.3.1.md +0 -121
  220. package/docs/release-notes/0.3.2.md +0 -64
  221. package/docs/release-notes/0.4.0.md +0 -80
  222. package/docs/release-notes/0.4.1.md +0 -149
  223. package/docs/release-notes/0.4.3.md +0 -60
  224. package/docs/release-notes/README.md +0 -13
@@ -1,85 +0,0 @@
1
- # functions::rank -- Ranking Functions
2
-
3
- *Scoring functions that `kb-worker.wasm` calls after
4
- retrieving raw index results from `fs-database.wasm`.*
5
-
6
- ## Source
7
-
8
- `engine/crates/query-parser/src/rank/`
9
-
10
- ## BM25F (`bm25f.rs`)
11
-
12
- ```rust
13
- pub fn score_bm25f(
14
- params: &BM25FParams,
15
- raw_match: &RawMatch,
16
- corpus: &CorpusStats,
17
- ) -> f32;
18
- ```
19
-
20
- Primary ranking function. Field-weighted Okapi BM25
21
- with configurable saturation (`k1`), length
22
- normalization (`b`), and per-field boost weights.
23
-
24
- Per-field BM25F formula:
25
-
26
- ```
27
- score_f(t, d) = idf(t)
28
- * (tf_f(t,d) * (k1 + 1))
29
- / (tf_f(t,d) + k1
30
- * (1 - b + b * |d_f| / avgdl_f))
31
-
32
- bm25f(t, d) = w_heading * score_heading(t, d)
33
- + w_body * score_body(t, d)
34
- + w_code * score_code(t, d)
35
- ```
36
-
37
- `raw_match.field_tfs` provides per-field term
38
- frequencies (`tf_f`). `raw_match.field_token_counts`
39
- provides per-field section lengths (`|d_f|`).
40
- `corpus` provides per-field average lengths
41
- (`avgdl_f`). Field weights are applied at query
42
- time, making them tunable without index rebuilds.
43
- See [Query Result](../../../../goals/query-result.md)
44
- for the full scoring specification.
45
-
46
- ## Proximity (`proximity.rs`)
47
-
48
- ```rust
49
- pub fn score_proximity(
50
- positions: &[(Term, Vec<u32>)],
51
- query_terms: &[Term],
52
- ) -> f32;
53
- ```
54
-
55
- Boosts sections where query terms appear close
56
- together. Computes minimum span across all query
57
- terms, inversely weighted by span length.
58
-
59
- ## Zone (`zone.rs`)
60
-
61
- ```rust
62
- pub fn score_zone(
63
- positions: &[(Term, Vec<u32>)],
64
- section_length: u32,
65
- ) -> f32;
66
- ```
67
-
68
- Structural weighting. Terms in the first ~20% of a
69
- section (heading zone) receive higher weight. Uses
70
- `section_length` from `RawMatch.token_count`
71
- (pre-computed during indexing) to determine the
72
- zone boundary.
73
-
74
- ## Freshness (`freshness.rs`)
75
-
76
- ```rust
77
- pub fn score_freshness(
78
- created_at: u64,
79
- now: u64,
80
- half_life_days: f32,
81
- ) -> f32;
82
- ```
83
-
84
- Optional boost based on insertion timestamps.
85
- Exponential decay with configurable half-life.
@@ -1,131 +0,0 @@
1
- # query-parser -- Query Processing and Tokenization
2
-
3
- *Rust crate that provides query parsing, keyword
4
- extraction, and ranking functions. Tokenization is
5
- imported from `kbdb-shared`'s `pipeline` module.
6
- Compiles to `query-parser.wasm`.*
7
-
8
- ## Crate
9
-
10
- | Field | Value |
11
- |-------------|------------------------------------|
12
- | Name | `kbdb-query-parser` |
13
- | Path | `engine/crates/query-parser/` |
14
- | Crate type | `cdylib` (WASM) + `rlib` (tests) |
15
- | WASM output | `dist/wasm/query-parser.wasm` |
16
- | Depends on | `kbdb-shared` |
17
-
18
- `kbdb-query-parser` is a **standalone** WASM module.
19
- Pure computation -- no file I/O, no network. The only
20
- host import is `WebAssembly.Memory`.
21
-
22
- See [Query Parser](../../../goals/query-parser.md)
23
- for the full behavioral specification.
24
-
25
- ## Workspace Position
26
-
27
- ```
28
- engine/
29
- ├── Cargo.toml # workspace root
30
- └── crates/
31
- ├── shared/ # kbdb-shared
32
- ├── fs-database/ # kbdb-fs-database
33
- ├── query-parser/ # this crate
34
- └── kb-worker/ # kbdb-worker
35
- ```
36
-
37
- ## Directory Layout
38
-
39
- ```
40
- engine/crates/query-parser/
41
- ├── Cargo.toml
42
- └── src/
43
- ├── lib.rs # crate root, WASM exports
44
- ├── mem.rs # shared-memory helpers
45
- ├── parse.rs # query string → QueryPlan
46
- ├── extract/
47
- │ ├── mod.rs # extraction facade
48
- │ ├── rake.rs # RAKE algorithm
49
- │ └── tfidf.rs # TF-IDF weighting
50
- ├── rank/
51
- │ ├── mod.rs # ranking facade
52
- │ ├── bm25f.rs # BM25F scoring
53
- │ ├── proximity.rs # proximity scoring
54
- │ ├── zone.rs # zone scoring
55
- │ └── freshness.rs # freshness weighting
56
- └── cache.rs # query-scoped cache
57
- ```
58
-
59
- The tokenization pipeline lives in `kbdb-shared`'s
60
- [pipeline](../shared/pipeline/module.md) module.
61
- This crate re-exports it via WASM FFI
62
- (`qp_tokenize`).
63
-
64
- ## Modules
65
-
66
- ### [types/](types/module.md) -- Structs
67
-
68
- - **[token](types/token.md)** -- `Token`,
69
- `TokenStream`, `Ngram`, `NgramType`, `StemResult`.
70
- - **[keyphrase](types/keyphrase.md)** -- `Keyphrase`,
71
- `WeightedTerm`, `BM25FParams`, `FieldWeights`.
72
- - **[cache](types/cache.md)** -- `QueryCache`.
73
- Short-lived query-scoped cache.
74
-
75
- ### [functions/](functions/module.md) -- Operations
76
-
77
- - **[parse](functions/parse.md)** -- Query string
78
- parsing into `QueryPlan`.
79
- - **[extract](functions/extract.md)** -- RAKE
80
- keyphrase extraction and TF-IDF term weighting.
81
- - **[rank](functions/rank.md)** -- BM25F, proximity,
82
- zone, and freshness scoring functions.
83
-
84
- Tokenization is provided by
85
- [kbdb-shared pipeline](../shared/pipeline/module.md).
86
-
87
- ## WASM Exports
88
-
89
- | Export | Description |
90
- |--------------------------|-------------------------|
91
- | `qp_tokenize` | Full tokenization pipeline |
92
- | `qp_parse_query` | Parse query → QueryPlan |
93
- | `qp_normalize_term` | Normalize a single term |
94
- | `qp_stem` | Stem a single term |
95
- | `qp_extract_keywords` | RAKE keyphrase extraction |
96
- | `qp_compute_tf` | Single-stream term frequency |
97
- | `qp_compute_field_tf` | Per-field term frequency (heading, body, code) |
98
- | `qp_compute_tfidf` | TF-IDF with supplied IDF |
99
- | `qp_score_bm25f` | BM25F ranking score |
100
- | `qp_score_proximity` | Proximity ranking score |
101
- | `qp_score_zone` | Zone ranking score |
102
- | `qp_score_freshness` | Freshness ranking score |
103
- | `qp_split_identifiers` | camelCase/snake_case split |
104
- | `alloc` / `free` | Memory management (from `kbdb-shared`) |
105
-
106
- ## Dependencies
107
-
108
- ```toml
109
- [dependencies]
110
- kbdb-shared = { path = "../shared" }
111
- ```
112
-
113
- Tokenization dependencies (`unicode-segmentation`,
114
- `unicode-normalization`, `rust-stemmers`) are now in
115
- `kbdb-shared` alongside the `pipeline` module.
116
-
117
- ## Build
118
-
119
- ```sh
120
- cd engine
121
- wasm-pack build crates/query-parser \
122
- --target no-modules \
123
- --out-dir ../../../dist/wasm/query-parser
124
- ```
125
-
126
- ## Testing
127
-
128
- ```sh
129
- cd engine && cargo test -p kbdb-query-parser
130
- cd engine && wasm-pack test --node crates/query-parser
131
- ```
@@ -1,39 +0,0 @@
1
- # types::cache -- Query-Scoped Cache
2
-
3
- *Short-lived cache scoped to a single query or
4
- indexing operation.*
5
-
6
- ## Source
7
-
8
- `engine/crates/query-parser/src/cache.rs`
9
-
10
- ## Type Definition
11
-
12
- ```rust
13
- pub struct QueryCache {
14
- tokenized: HashMap<String, Vec<Token>>,
15
- stemmed: HashMap<String, String>,
16
- }
17
-
18
- impl QueryCache {
19
- pub fn new() -> Self;
20
- pub fn get_tokens(&self, input: &str)
21
- -> Option<&[Token]>;
22
- pub fn put_tokens(
23
- &mut self, input: String, tokens: Vec<Token>,
24
- );
25
- pub fn get_stem(&self, word: &str)
26
- -> Option<&str>;
27
- pub fn put_stem(
28
- &mut self, word: String, stem: String,
29
- );
30
- }
31
- ```
32
-
33
- Allocated when processing begins, discarded
34
- immediately after the operation completes. Prevents
35
- redundant tokenization and stemming when the same
36
- term appears multiple times in a query or content.
37
-
38
- Not persisted, not shared across queries, not
39
- visible to other modules.
@@ -1,71 +0,0 @@
1
- # types::keyphrase -- Extraction and Ranking Types
2
-
3
- *Types for keyword extraction results and ranking
4
- function configuration.*
5
-
6
- ## Source
7
-
8
- `engine/crates/query-parser/src/extract/mod.rs`,
9
- `engine/crates/query-parser/src/rank/bm25f.rs`
10
-
11
- ## Type Definitions
12
-
13
- ```rust
14
- pub struct Keyphrase {
15
- pub terms: Vec<String>,
16
- pub score: f32,
17
- }
18
-
19
- pub struct WeightedTerm {
20
- pub term: Term,
21
- pub tf: f32,
22
- pub weight: f32,
23
- }
24
-
25
- pub struct BM25FParams {
26
- pub k1: f32, // term saturation (1.2)
27
- pub b: f32, // length normalization (0.75)
28
- pub field_weights: FieldWeights,
29
- }
30
-
31
- pub struct FieldWeights {
32
- pub heading: f32, // default 2.0
33
- pub body: f32, // default 1.0
34
- pub code: f32, // default 1.5
35
- }
36
- ```
37
-
38
- `Keyphrase` is the output of RAKE extraction --
39
- multi-word candidate phrases scored by word degree /
40
- frequency ratio.
41
-
42
- `WeightedTerm` combines a term with its per-section
43
- TF and final TF-IDF weight. This is an
44
- extraction-time type used during keyword extraction.
45
- At ranking time, the per-field `FieldTf` from
46
- `RawMatch` (see
47
- [shared/query/types](../../shared/query/types.md))
48
- is used instead -- it contains separate heading,
49
- body, and code frequencies pre-computed during
50
- indexing.
51
-
52
- `BM25FParams` configures the primary ranking function
53
- with field-specific boost weights. Each field weight
54
- in `FieldWeights` multiplies the per-field BM25
55
- score computed from the corresponding `FieldTf`
56
- component and per-field corpus average from
57
- `CorpusStats`. See
58
- [FieldTokenCounts](../../shared/query/types.md)
59
- for the per-field token counts used in length
60
- normalization.
61
-
62
- ```rust
63
- pub type SynonymMap = HashMap<String, Vec<String>>;
64
- ```
65
-
66
- `SynonymMap` is a type alias for the bidirectional
67
- synonym lookup used in tokenization pipeline stage 7.
68
- It is constructed once via `build_synonym_map()` from
69
- a curated list of synonym pairs and reused across all
70
- tokenization calls. The map is immutable after
71
- construction.
@@ -1,18 +0,0 @@
1
- # query-parser/types -- Type Definitions
2
-
3
- *Structs defined in the query-parser crate for
4
- keyword extraction and ranking.*
5
-
6
- ## Modules
7
-
8
- - **[keyphrase](keyphrase.md)** -- `Keyphrase`,
9
- `WeightedTerm`, `BM25FParams`, `FieldWeights`,
10
- `SynonymMap`. Types for keyword extraction, ranking
11
- configuration, and synonym expansion.
12
- - **[cache](cache.md)** -- `QueryCache`. Short-lived
13
- cache scoped to a single query or indexing
14
- operation.
15
-
16
- Token types (`Token`, `TokenStream`, `Ngram`,
17
- `NgramType`, `StemResult`) are defined in
18
- [kbdb-shared pipeline](../../shared/pipeline/types.md).
@@ -1,46 +0,0 @@
1
- # types::token -- Tokenization Types
2
-
3
- *Output types from the tokenization pipeline.*
4
-
5
- ## Source
6
-
7
- `engine/crates/shared/src/pipeline/mod.rs`
8
-
9
- **Note:** These types are now defined in `kbdb-shared`.
10
- See [shared/pipeline/types](../../shared/pipeline/types.md)
11
- for the canonical documentation.
12
-
13
- ## Type Definitions
14
-
15
- ```rust
16
- pub struct Token {
17
- pub original: String,
18
- pub stemmed: String,
19
- pub position: u32,
20
- }
21
-
22
- pub struct TokenStream {
23
- pub tokens: Vec<Token>,
24
- pub ngrams: Vec<Ngram>,
25
- }
26
-
27
- pub struct Ngram {
28
- pub terms: Vec<String>,
29
- pub gram_type: NgramType,
30
- }
31
-
32
- pub enum NgramType { Bigram, Trigram }
33
-
34
- pub struct StemResult {
35
- pub original: String,
36
- pub stemmed: String,
37
- }
38
- ```
39
-
40
- `Token` preserves both original and stemmed forms so
41
- the index can support exact-match and stem-match
42
- queries. `position` is the zero-based token offset.
43
-
44
- `TokenStream` is the output of `tokenize()` -- the
45
- full set of tokens and extracted n-grams from a
46
- single input text.
@@ -1,114 +0,0 @@
1
- # content-composer -- Functions
2
-
3
- ## `compose_content`
4
-
5
- ```rust
6
- pub fn compose_content(
7
- request: &ComposeRequest,
8
- ) -> ComposeResult
9
- ```
10
-
11
- Takes a fully resolved request (all sections and
12
- documents pre-fetched, content markers pre-extracted)
13
- and returns the composed Markdown string. The
14
- function is pure -- no I/O, no database access.
15
- Resolution and fetching are the caller's
16
- responsibility (`kb-worker`).
17
-
18
- ### Composition Flow
19
-
20
- 1. Initialize `renderedSections` and
21
- `renderedDocuments` tracking sets.
22
- 2. For each `ResolvedId` in `main_ids` (in order):
23
- - If `Section(kbid)`: render as a section (anchor
24
- + heading + content with marker processing), add
25
- to `renderedSections`.
26
- - If `Document(docid)`: render as a document
27
- section (see Document Rendering below), add to
28
- `renderedDocuments`.
29
- 3. Traverse the full references set and render any
30
- entities not yet in `renderedSections` or
31
- `renderedDocuments` (remaining references, placed
32
- at the bottom of the output).
33
- 4. Return `ComposeResult` with the composed Markdown
34
- and character count.
35
-
36
- ### Content Marker Rendering
37
-
38
- Each `$(type: value)` content marker in a section's
39
- body is replaced by its rendered form:
40
-
41
- | Marker | Rendered output |
42
- |--------------------|-----------------------------------|
43
- | `$(image: {kbid})` | Inline image (base64-decoded content from referenced `image/*` section) |
44
- | `$(code: {kbid})` | Fenced code block with language tag derived from MIME type |
45
- | `$(text: {kbid})` | Inline expansion of referenced section content (recursive, with cycle detection) |
46
- | `$(document: {docid})` | Document section rendering (heading + subsections, recursive) |
47
- | `$(link: {kbid})` | Markdown link `[{kbid}](#{kbid})` (no inline expansion) |
48
- | `$(doc-link: {docid})` | Markdown link `[{docid}](#{docid})` (no inline expansion) |
49
-
50
- Cyclic `$(text:)` and `$(document:)` embeds (entity
51
- already in the render stack) are rendered as Markdown
52
- links to the entity's anchor instead of expanding
53
- inline.
54
-
55
- ### MIME-to-Language Mapping
56
-
57
- For `$(code:)` markers, the fenced code block
58
- language tag is derived from the referenced section's
59
- MIME type:
60
-
61
- | MIME type | Language tag |
62
- |------------------------|-------------|
63
- | `text/typescript` | `ts` |
64
- | `text/javascript` | `js` |
65
- | `text/html` | `html` |
66
- | `text/css` | `css` |
67
- | `text/xml` | `xml` |
68
- | `text/plain` | *(none)* |
69
- | `application/json` | `json` |
70
- | other `text/*` | subtype |
71
-
72
- ### Document Rendering
73
-
74
- When a document is rendered (from `$(document:)`
75
- marker, `$(doc-link:)` target, or main docid input):
76
-
77
- 1. Emit document anchor: `<a id="{docid}"></a>`
78
- 2. Emit heading with document title.
79
- 3. For each section in the document (position order):
80
- - Emit section anchor: `<a id="{kbid}"></a>`
81
- - Emit sub-heading.
82
- - Render section content with marker processing.
83
- - Add kbid to `renderedSections`.
84
- 4. Add docid to `renderedDocuments`.
85
-
86
- ### Tests
87
-
88
- Unit tests in
89
- `engine/crates/shared/src/content-composer/compose.rs`
90
- and `render.rs`:
91
-
92
- - Empty request returns empty markdown.
93
- - Single text section with no markers.
94
- - Single section with `$(image:)` marker.
95
- - Single section with `$(code:)` marker and correct
96
- language tag.
97
- - Recursive `$(text:)` embedding.
98
- - `$(document:)` renders document heading with
99
- section subsections.
100
- - Cyclic `$(text:)` embed becomes link to anchor.
101
- - Cyclic `$(document:)` embed becomes link to
102
- document anchor.
103
- - `$(link:)` renders as Markdown link, target
104
- rendered at bottom.
105
- - `$(doc-link:)` renders as Markdown link pointing
106
- to document anchor.
107
- - Mixed kbids and docids in main list.
108
- - Named anchors assigned correctly to all entities.
109
- - `renderedSections` prevents duplicate rendering.
110
- - `renderedDocuments` prevents duplicate document
111
- rendering.
112
- - Remaining references rendered at bottom only for
113
- unrendered entities.
114
- - Unicode content preserved through composition.
@@ -1,147 +0,0 @@
1
- # content-composer -- Content Composition
2
-
3
- *Composition engine that resolves sections and
4
- documents from identifiers, processes content markers
5
- recursively, and produces a single rendered Markdown
6
- document.*
7
-
8
- ## Module
9
-
10
- | Field | Value |
11
- |-------------|----------------------------------------------|
12
- | Rust module | `content_composer` |
13
- | Source path | `engine/crates/shared/src/content-composer/` |
14
- | Crate | `kbdb-shared` (rlib) |
15
-
16
- The module is declared in `lib.rs` with a path
17
- attribute because the source directory uses a
18
- hyphenated name:
19
-
20
- ```rust
21
- #[path = "content-composer/mod.rs"]
22
- pub mod content_composer;
23
- ```
24
-
25
- ## Directory Layout
26
-
27
- ```
28
- engine/crates/shared/src/content-composer/
29
- mod.rs -- re-exports
30
- types.rs -- ComposeRequest, ComposeResult,
31
- RenderedSection
32
- compose.rs -- compose_content()
33
- render.rs -- section and marker rendering
34
- ```
35
-
36
- ## Purpose
37
-
38
- The content creator takes one or more identifiers
39
- (kbids or docids, mixed) and produces a composed
40
- Markdown document. It resolves each identifier to its
41
- sections, follows embedding and link content markers
42
- recursively, and renders everything into a single
43
- UTF-8 Markdown string with named anchors for
44
- cross-referencing.
45
-
46
- The rendering logic is **pure composition** -- no
47
- I/O, no database access. Resolution and fetching of
48
- sections from the database is handled by `kb-worker`
49
- before calling into this module.
50
-
51
- ## Two-Phase Composition
52
-
53
- ### Phase 1 -- Resolve and Gather (kb-worker)
54
-
55
- 1. Resolve identifiers (try kbid, then docid)
56
- 2. Expand documents to section kbids
57
- 3. Deduplicate while preserving order
58
- 4. Fetch main sections from fs-database
59
- 5. Extract content markers recursively using
60
- `extract_content_markers()` from content-parser
61
- 6. Fetch reference content
62
- 7. Assign HTML named anchors
63
-
64
- ### Phase 2 -- Content Composition (this module)
65
-
66
- Takes the pre-fetched `ComposeRequest` and produces
67
- a Markdown string. Processes content markers in
68
- section bodies, rendering each by type (see
69
- [functions.md](functions.md) for rendering rules).
70
-
71
- ## Cycle Detection
72
-
73
- Two distinct mechanisms prevent infinite loops:
74
-
75
- - **Resolution cycle detection** (Phase 1): a visited
76
- set tracks which kbids and docids have been
77
- processed during recursive marker extraction.
78
- - **Render cycle detection** (Phase 2): a render
79
- stack tracks which entities are currently being
80
- expanded. If a `$(text:)` or `$(document:)` marker
81
- references an entity already in the render stack,
82
- it is a cyclic embed -- rendered as a Markdown link
83
- to the entity's anchor instead of expanding inline.
84
-
85
- The render stack is distinct from the
86
- `renderedSections` / `renderedDocuments` tracking
87
- sets. An entity in the render stack is currently
88
- being expanded (not yet complete). An entity in
89
- `renderedSections` has been fully rendered and will
90
- not be rendered again.
91
-
92
- ## Dependencies
93
-
94
- Sibling modules within `kbdb-shared`:
95
-
96
- - `content_parser::extract_content_markers()` -- for
97
- processing markers within referenced content during
98
- recursive rendering.
99
- - `content_parser::ContentMarkerType` -- for
100
- dispatching marker rendering by type.
101
- - `section::SectionType` -- for type-specific
102
- rendering (text vs code vs image).
103
-
104
- ## Integration Points
105
-
106
- ### Worker Orchestration
107
-
108
- The `kb-worker` crate orchestrates the full flow:
109
- receives a `compose` IPC request, resolves
110
- identifiers via `fs-database`, fetches content,
111
- extracts content markers recursively, builds a
112
- `ComposeRequest`, and calls `compose_content()`.
113
- See [compose](../../kb-worker/functions/compose.md).
114
-
115
- ### Worker Client
116
-
117
- `WorkerClient.content(ids)` sends the `compose` IPC
118
- method to the daemon and returns a `ContentResult`.
119
-
120
- ### CLI
121
-
122
- `runContent(client, options)` calls
123
- `client.content(ids)` and writes the composed
124
- Markdown to stdout. Command: `kbdb content <id>...`
125
-
126
- ### MCP
127
-
128
- The `content` MCP tool routes to
129
- `client.content(ids)` and wraps the result in the
130
- MCP tool response format.
131
-
132
- ## Sub-documents
133
-
134
- - [types.md](types.md) -- `ComposeRequest`,
135
- `ComposeResult`, `ResolvedId`, `SectionData`,
136
- `DocumentData`, `RenderedSection`
137
- - [functions.md](functions.md) --
138
- `compose_content()`
139
-
140
- ## Testing
141
-
142
- Tests live inline in
143
- `engine/crates/shared/src/content-composer/compose.rs`
144
- and
145
- `engine/crates/shared/src/content-composer/render.rs`
146
- (`#[cfg(test)]` modules). See
147
- [functions.md](functions.md) for test descriptions.