@dikolab/kbdb 0.1.5 → 0.2.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 (193) 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.mjs +13 -2
  11. package/dist/mod.mjs.map +2 -2
  12. package/dist/src/shared/cli/functions/format-output.function.d.ts +7 -7
  13. package/dist/src/shared/cli/functions/run-search.function.d.ts +25 -16
  14. package/dist/src/shared/cli/index.d.ts +1 -1
  15. package/dist/src/shared/cli/typings/cli-options.interface.d.ts +2 -0
  16. package/dist/src/shared/cli/typings/search-display.model.d.ts +25 -7
  17. package/dist/src/shared/mcp/functions/handle-recall.function.d.ts +29 -0
  18. package/dist/src/shared/mcp/typings/mcp-client.interface.d.ts +30 -11
  19. package/dist/src/shared/version/constants/version.constant.d.ts +1 -1
  20. package/dist/src/shared/wasm-codec/functions/decode-recall-result.function.d.ts +28 -0
  21. package/dist/src/shared/wasm-codec/functions/decode-scored-results.function.d.ts +14 -7
  22. package/dist/src/shared/wasm-codec/functions/encode-recall-params.function.d.ts +21 -0
  23. package/dist/src/shared/wasm-codec/functions/encode-search-params.function.d.ts +1 -1
  24. package/dist/src/shared/wasm-codec/index.d.ts +5 -2
  25. package/dist/src/shared/worker-client/classes/worker-client.class.d.ts +13 -3
  26. package/dist/src/shared/worker-client/index.d.ts +2 -1
  27. package/dist/src/shared/worker-client/typings/recall-result.model.d.ts +94 -0
  28. package/dist/src/shared/worker-client/typings/search-params.model.d.ts +2 -0
  29. package/dist/src/shared/worker-client/typings/search-result.model.d.ts +23 -7
  30. package/dist/src/shared/worker-daemon/classes/worker-daemon.class.d.ts +1 -0
  31. package/dist/wasm/fs-database/kbdb_fs_database.d.ts +18 -0
  32. package/dist/wasm/fs-database/kbdb_fs_database.js +30 -0
  33. package/dist/wasm/fs-database/kbdb_fs_database_bg.wasm +0 -0
  34. package/dist/wasm/fs-database/kbdb_fs_database_bg.wasm.d.ts +2 -0
  35. package/dist/wasm/kb-worker/kbdb_worker.d.ts +38 -0
  36. package/dist/wasm/kb-worker/kbdb_worker.js +56 -0
  37. package/dist/wasm/kb-worker/kbdb_worker_bg.wasm +0 -0
  38. package/dist/wasm/kb-worker/kbdb_worker_bg.wasm.d.ts +3 -0
  39. package/dist/worker.mjs +283 -29
  40. package/dist/worker.mjs.map +4 -4
  41. package/docs/details/README.md +26 -0
  42. package/docs/details/cli.md +608 -0
  43. package/docs/details/library-api.md +406 -0
  44. package/docs/details/mcp-server.md +430 -0
  45. package/docs/details/search-and-ranking.md +457 -0
  46. package/docs/details/storage.md +529 -0
  47. package/docs/goals/agents.md +751 -0
  48. package/docs/goals/architecture.svg +198 -0
  49. package/docs/goals/cli.md +2308 -0
  50. package/docs/goals/content-composer.md +609 -0
  51. package/docs/goals/content-parser.md +279 -0
  52. package/docs/goals/database.md +1452 -0
  53. package/docs/goals/document.md +368 -0
  54. package/docs/goals/mcp.md +1467 -0
  55. package/docs/goals/overview.md +124 -0
  56. package/docs/goals/query-parser.md +271 -0
  57. package/docs/goals/query-result.md +693 -0
  58. package/docs/goals/skills.md +738 -0
  59. package/docs/goals/worker-client.md +944 -0
  60. package/docs/goals/worker-daemon.md +1447 -0
  61. package/docs/modules/overview.md +298 -0
  62. package/docs/modules/rust/fs-database/functions/document.md +48 -0
  63. package/docs/modules/rust/fs-database/functions/integrity.md +68 -0
  64. package/docs/modules/rust/fs-database/functions/io.md +87 -0
  65. package/docs/modules/rust/fs-database/functions/module.md +22 -0
  66. package/docs/modules/rust/fs-database/functions/query-exec.md +44 -0
  67. package/docs/modules/rust/fs-database/functions/section.md +59 -0
  68. package/docs/modules/rust/fs-database/indexes/btree.md +35 -0
  69. package/docs/modules/rust/fs-database/indexes/corpus.md +61 -0
  70. package/docs/modules/rust/fs-database/indexes/inverted.md +60 -0
  71. package/docs/modules/rust/fs-database/indexes/module.md +64 -0
  72. package/docs/modules/rust/fs-database/indexes/positional.md +39 -0
  73. package/docs/modules/rust/fs-database/module.md +149 -0
  74. package/docs/modules/rust/fs-database/types/catalog.md +53 -0
  75. package/docs/modules/rust/fs-database/types/module.md +12 -0
  76. package/docs/modules/rust/fs-database/types/section.md +101 -0
  77. package/docs/modules/rust/kb-worker/functions/compose.md +57 -0
  78. package/docs/modules/rust/kb-worker/functions/module.md +20 -0
  79. package/docs/modules/rust/kb-worker/functions/retrieve.md +194 -0
  80. package/docs/modules/rust/kb-worker/functions/search.md +173 -0
  81. package/docs/modules/rust/kb-worker/functions/write.md +172 -0
  82. package/docs/modules/rust/kb-worker/module.md +171 -0
  83. package/docs/modules/rust/kb-worker/types/cache.md +90 -0
  84. package/docs/modules/rust/kb-worker/types/context.md +26 -0
  85. package/docs/modules/rust/kb-worker/types/module.md +16 -0
  86. package/docs/modules/rust/kb-worker/types/status.md +29 -0
  87. package/docs/modules/rust/kb-worker/types/token.md +50 -0
  88. package/docs/modules/rust/overview.md +495 -0
  89. package/docs/modules/rust/query-parser/functions/extract.md +56 -0
  90. package/docs/modules/rust/query-parser/functions/module.md +16 -0
  91. package/docs/modules/rust/query-parser/functions/parse.md +33 -0
  92. package/docs/modules/rust/query-parser/functions/pipeline.md +103 -0
  93. package/docs/modules/rust/query-parser/functions/rank.md +85 -0
  94. package/docs/modules/rust/query-parser/module.md +136 -0
  95. package/docs/modules/rust/query-parser/types/cache.md +39 -0
  96. package/docs/modules/rust/query-parser/types/keyphrase.md +60 -0
  97. package/docs/modules/rust/query-parser/types/module.md +17 -0
  98. package/docs/modules/rust/query-parser/types/token.md +42 -0
  99. package/docs/modules/rust/shared/content-composer/functions.md +114 -0
  100. package/docs/modules/rust/shared/content-composer/module.md +147 -0
  101. package/docs/modules/rust/shared/content-composer/types.md +93 -0
  102. package/docs/modules/rust/shared/content-parser/functions.md +72 -0
  103. package/docs/modules/rust/shared/content-parser/module.md +99 -0
  104. package/docs/modules/rust/shared/content-parser/types.md +71 -0
  105. package/docs/modules/rust/shared/corpus/module.md +24 -0
  106. package/docs/modules/rust/shared/corpus/types.md +141 -0
  107. package/docs/modules/rust/shared/document/module.md +25 -0
  108. package/docs/modules/rust/shared/document/types.md +154 -0
  109. package/docs/modules/rust/shared/encode/module.md +22 -0
  110. package/docs/modules/rust/shared/encode/traits.md +304 -0
  111. package/docs/modules/rust/shared/error/module.md +28 -0
  112. package/docs/modules/rust/shared/error/types.md +302 -0
  113. package/docs/modules/rust/shared/kbid/module.md +24 -0
  114. package/docs/modules/rust/shared/kbid/types.md +147 -0
  115. package/docs/modules/rust/shared/memory/functions.md +209 -0
  116. package/docs/modules/rust/shared/memory/module.md +24 -0
  117. package/docs/modules/rust/shared/module.md +168 -0
  118. package/docs/modules/rust/shared/query/module.md +27 -0
  119. package/docs/modules/rust/shared/query/types.md +236 -0
  120. package/docs/modules/rust/shared/section/module.md +25 -0
  121. package/docs/modules/rust/shared/section/types.md +294 -0
  122. package/docs/modules/rust/shared/term/module.md +25 -0
  123. package/docs/modules/rust/shared/term/types.md +139 -0
  124. package/docs/modules/typescript/cli.md +129 -0
  125. package/docs/modules/typescript/kbdb-worker.md +135 -0
  126. package/docs/modules/typescript/overview.md +378 -0
  127. package/docs/modules/typescript/shared/cli/constants.md +23 -0
  128. package/docs/modules/typescript/shared/cli/functions.md +630 -0
  129. package/docs/modules/typescript/shared/cli/module.md +125 -0
  130. package/docs/modules/typescript/shared/cli/typings.md +172 -0
  131. package/docs/modules/typescript/shared/hash/functions.md +20 -0
  132. package/docs/modules/typescript/shared/hash/module.md +21 -0
  133. package/docs/modules/typescript/shared/mcp/classes.md +179 -0
  134. package/docs/modules/typescript/shared/mcp/functions.md +386 -0
  135. package/docs/modules/typescript/shared/mcp/module.md +160 -0
  136. package/docs/modules/typescript/shared/mcp/typings.md +522 -0
  137. package/docs/modules/typescript/shared/module.md +94 -0
  138. package/docs/modules/typescript/shared/platform/functions.md +42 -0
  139. package/docs/modules/typescript/shared/platform/module.md +25 -0
  140. package/docs/modules/typescript/shared/runtime/functions.md +26 -0
  141. package/docs/modules/typescript/shared/runtime/module.md +27 -0
  142. package/docs/modules/typescript/shared/runtime/typings.md +13 -0
  143. package/docs/modules/typescript/shared/version/module.md +27 -0
  144. package/docs/modules/typescript/shared/wasm-codec/functions.md +391 -0
  145. package/docs/modules/typescript/shared/wasm-codec/module.md +98 -0
  146. package/docs/modules/typescript/shared/worker-client/classes.md +264 -0
  147. package/docs/modules/typescript/shared/worker-client/functions.md +175 -0
  148. package/docs/modules/typescript/shared/worker-client/module.md +84 -0
  149. package/docs/modules/typescript/shared/worker-client/typings.md +508 -0
  150. package/docs/modules/typescript/shared/worker-daemon/classes.md +239 -0
  151. package/docs/modules/typescript/shared/worker-daemon/constants.md +37 -0
  152. package/docs/modules/typescript/shared/worker-daemon/functions.md +221 -0
  153. package/docs/modules/typescript/shared/worker-daemon/module.md +112 -0
  154. package/docs/modules/typescript/shared/worker-daemon/typings.md +126 -0
  155. package/docs/overview.md +188 -0
  156. package/docs/release-notes/0.1.0.md +189 -0
  157. package/docs/release-notes/0.1.1.md +46 -0
  158. package/docs/release-notes/0.1.2.md +38 -0
  159. package/docs/release-notes/0.1.3.md +42 -0
  160. package/docs/release-notes/0.2.0.md +147 -0
  161. package/docs/release-notes/README.md +9 -0
  162. package/package.json +8 -2
  163. package/src/shared/cli/functions/dispatch-command.function.ts +3 -3
  164. package/src/shared/cli/functions/format-output.function.ts +41 -34
  165. package/src/shared/cli/functions/parse-args.function.ts +7 -0
  166. package/src/shared/cli/functions/run-search.function.ts +53 -27
  167. package/src/shared/cli/index.ts +4 -1
  168. package/src/shared/cli/typings/cli-options.interface.ts +2 -0
  169. package/src/shared/cli/typings/search-display.model.ts +26 -7
  170. package/src/shared/mcp/functions/create-tool-definitions.function.ts +64 -4
  171. package/src/shared/mcp/functions/handle-recall.function.ts +66 -0
  172. package/src/shared/mcp/functions/handle-tool-call.function.ts +11 -1
  173. package/src/shared/mcp/typings/mcp-client.interface.ts +33 -15
  174. package/src/shared/version/constants/version.constant.ts +1 -1
  175. package/src/shared/wasm-codec/functions/decode-recall-result.function.ts +267 -0
  176. package/src/shared/wasm-codec/functions/decode-scored-results.function.ts +81 -27
  177. package/src/shared/wasm-codec/functions/encode-recall-params.function.ts +68 -0
  178. package/src/shared/wasm-codec/functions/encode-search-params.function.ts +4 -1
  179. package/src/shared/wasm-codec/index.ts +11 -3
  180. package/src/shared/worker-client/classes/worker-client.class.ts +24 -4
  181. package/src/shared/worker-client/index.ts +11 -1
  182. package/src/shared/worker-client/typings/recall-result.model.ts +98 -0
  183. package/src/shared/worker-client/typings/search-params.model.ts +2 -0
  184. package/src/shared/worker-client/typings/search-result.model.ts +24 -7
  185. package/src/shared/worker-daemon/classes/worker-daemon.class.ts +25 -4
  186. package/src/wasm/fs-database/kbdb_fs_database.d.ts +18 -0
  187. package/src/wasm/fs-database/kbdb_fs_database.js +30 -0
  188. package/src/wasm/fs-database/kbdb_fs_database_bg.wasm +0 -0
  189. package/src/wasm/fs-database/kbdb_fs_database_bg.wasm.d.ts +2 -0
  190. package/src/wasm/kb-worker/kbdb_worker.d.ts +38 -0
  191. package/src/wasm/kb-worker/kbdb_worker.js +56 -0
  192. package/src/wasm/kb-worker/kbdb_worker_bg.wasm +0 -0
  193. package/src/wasm/kb-worker/kbdb_worker_bg.wasm.d.ts +3 -0
@@ -0,0 +1,56 @@
1
+ # functions::extract -- Keyword Extraction
2
+
3
+ *RAKE keyphrase extraction and TF-IDF term weighting.*
4
+
5
+ ## Source
6
+
7
+ `engine/crates/query-parser/src/extract/`
8
+
9
+ ## RAKE (`rake.rs`)
10
+
11
+ ```rust
12
+ pub fn extract_keyphrases(
13
+ tokens: &[Token],
14
+ stop_words: &HashSet<String>,
15
+ ) -> Vec<Keyphrase>;
16
+ ```
17
+
18
+ Rapid Automatic Keyword Extraction. Splits on stop
19
+ words to identify candidate phrases. Scores each
20
+ candidate by the ratio of word degree to word
21
+ frequency. Unsupervised, no ML dependencies.
22
+
23
+ ## TF-IDF (`tfidf.rs`)
24
+
25
+ ```rust
26
+ pub fn compute_tf(
27
+ tokens: &[Token],
28
+ ) -> Vec<(Term, f32)>;
29
+
30
+ pub fn compute_field_tf(
31
+ heading_tokens: &[Token],
32
+ body_tokens: &[Token],
33
+ code_tokens: &[Token],
34
+ ) -> Vec<(Term, FieldTf)>;
35
+
36
+ pub fn compute_tfidf(
37
+ tf: &[(Term, f32)],
38
+ idf: &dyn Fn(&Term) -> f32,
39
+ ) -> Vec<WeightedTerm>;
40
+ ```
41
+
42
+ `compute_tf` calculates per-section term frequency
43
+ for a single token stream.
44
+
45
+ `compute_field_tf` computes per-field term
46
+ frequencies from separately tokenized field streams.
47
+ `kb-worker` calls this during indexing after running
48
+ the tokenization pipeline separately for each field
49
+ (title → heading, body + description → body,
50
+ code → code). Returns `FieldTf` values with heading,
51
+ body, and code frequencies per term.
52
+
53
+ `compute_tfidf` combines TF with corpus-wide IDF
54
+ (provided by `fs-database.wasm` via `kb-worker.wasm`)
55
+ to produce final weights reflecting each term's
56
+ discriminative value.
@@ -0,0 +1,16 @@
1
+ # query-parser/functions -- Operations
2
+
3
+ *Functions for tokenization, query parsing, keyword
4
+ extraction, and ranking.*
5
+
6
+ ## Modules
7
+
8
+ - **[pipeline](pipeline.md)** -- 8-stage tokenization
9
+ pipeline. Converts raw text into normalized, stemmed
10
+ tokens with n-grams.
11
+ - **[parse](parse.md)** -- Query string parsing into
12
+ structured `QueryPlan`.
13
+ - **[extract](extract.md)** -- RAKE keyphrase
14
+ extraction and TF-IDF term weighting.
15
+ - **[rank](rank.md)** -- BM25F, proximity, zone, and
16
+ freshness scoring functions.
@@ -0,0 +1,33 @@
1
+ # functions::parse -- Query Parsing
2
+
3
+ *Parse a search query string into a structured
4
+ `QueryPlan`.*
5
+
6
+ ## Source
7
+
8
+ `engine/crates/query-parser/src/parse.rs`
9
+
10
+ ## Function
11
+
12
+ ```rust
13
+ pub fn parse_query(input: &str) -> QueryPlan;
14
+ ```
15
+
16
+ ## Parsing Stages
17
+
18
+ 1. **Tokenize** -- Run the query through the
19
+ tokenization pipeline.
20
+ 2. **Parse syntax** -- Recognize operators: quoted
21
+ phrases (`"..."`), boolean AND/OR, exclusions
22
+ (`-term`).
23
+ 3. **Build plan** -- Produce the `QueryPlan` with
24
+ terms, phrase groups, exclusions, and match mode.
25
+
26
+ ## Examples
27
+
28
+ | Input | terms | phrases | exclusions | mode |
29
+ |----------------------|--------------|------------------|------------|------|
30
+ | `rust wasm` | [rust, wasm] | [] | [] | And |
31
+ | `"file database"` | [] | [[file, databas]]| [] | And |
32
+ | `rust -draft` | [rust] | [] | [draft] | And |
33
+ | `rust OR wasm` | [rust, wasm] | [] | [] | Or |
@@ -0,0 +1,103 @@
1
+ # functions::pipeline -- Tokenization Pipeline
2
+
3
+ *8-stage pipeline that converts raw text into
4
+ normalized, stemmed tokens. Used for both indexing
5
+ and query processing.*
6
+
7
+ ## Source
8
+
9
+ `engine/crates/query-parser/src/pipeline/`
10
+
11
+ ## Entry Point
12
+
13
+ ```rust
14
+ pub fn tokenize(input: &str) -> TokenStream;
15
+ ```
16
+
17
+ Runs all eight stages in sequence. Each stage is a
18
+ pure function.
19
+
20
+ ## Stages
21
+
22
+ ### 1. Content Marker Stripping
23
+
24
+ ```rust
25
+ use kbdb_shared::content_parser::strip_content_markers;
26
+ ```
27
+
28
+ Removes all six content marker types from the input
29
+ text before tokenization:
30
+ `$(image:)`, `$(code:)`, `$(text:)`, `$(document:)`,
31
+ `$(link:)`, `$(doc-link:)`.
32
+
33
+ Delegates to `strip_content_markers()` imported from
34
+ `kbdb_shared::content_parser` (see
35
+ [Content Parser](../../shared/content-parser/module.md)).
36
+ These are structural references, not searchable
37
+ content.
38
+
39
+ ### 2. Unicode Normalization (`unicode.rs`)
40
+
41
+ ```rust
42
+ pub fn normalize_nfc(input: &str) -> String;
43
+ ```
44
+
45
+ NFC normalization for consistent codepoints.
46
+
47
+ ### 3. Word Boundary Detection (`segment.rs`)
48
+
49
+ ```rust
50
+ pub fn segment_words(input: &str) -> Vec<&str>;
51
+ ```
52
+
53
+ Unicode Segmentation (UAX #29).
54
+
55
+ ### 4. Identifier Splitting (`identifiers.rs`)
56
+
57
+ ```rust
58
+ pub fn split_identifiers(word: &str) -> Vec<String>;
59
+ ```
60
+
61
+ - `camelCase` → `["camel", "case"]`
62
+ - `snake_case` → `["snake", "case"]`
63
+ - `PascalCase` → `["pascal", "case"]`
64
+ - `HTMLParser` → `["html", "parser"]`
65
+
66
+ ### 5. Case Folding (`case.rs`)
67
+
68
+ ```rust
69
+ pub fn fold_case(word: &str) -> String;
70
+ ```
71
+
72
+ Unicode case folding (lowercase).
73
+
74
+ ### 6. Stop Word Removal (`stopwords.rs`)
75
+
76
+ ```rust
77
+ pub fn is_stop_word(word: &str) -> bool;
78
+ pub fn remove_stop_words(
79
+ words: &[String],
80
+ ) -> Vec<String>;
81
+ ```
82
+
83
+ Filters common function words. Single-character
84
+ tokens also removed.
85
+
86
+ ### 7. Stemming (`stem.rs`)
87
+
88
+ ```rust
89
+ pub fn stem_word(word: &str) -> StemResult;
90
+ ```
91
+
92
+ Snowball / Porter2. Preserves both original and
93
+ stemmed forms.
94
+
95
+ ### 8. N-gram Extraction (`ngram.rs`)
96
+
97
+ ```rust
98
+ pub fn extract_ngrams(
99
+ tokens: &[Token],
100
+ ) -> Vec<Ngram>;
101
+ ```
102
+
103
+ Bigrams and trigrams for phrase indexing.
@@ -0,0 +1,85 @@
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.
@@ -0,0 +1,136 @@
1
+ # query-parser -- Query Processing and Tokenization
2
+
3
+ *Rust crate that provides the tokenization pipeline,
4
+ query parsing, keyword extraction, and ranking
5
+ functions. Compiles to `query-parser.wasm`.*
6
+
7
+ ## Crate
8
+
9
+ | Field | Value |
10
+ |-------------|------------------------------------|
11
+ | Name | `kbdb-query-parser` |
12
+ | Path | `engine/crates/query-parser/` |
13
+ | Crate type | `cdylib` (WASM) + `rlib` (tests) |
14
+ | WASM output | `dist/wasm/query-parser.wasm` |
15
+ | Depends on | `kbdb-shared` |
16
+
17
+ `kbdb-query-parser` is a **standalone** WASM module.
18
+ Pure computation -- no file I/O, no network. The only
19
+ host import is `WebAssembly.Memory`.
20
+
21
+ See [Query Parser](../../../goals/query-parser.md)
22
+ for the full behavioral specification.
23
+
24
+ ## Workspace Position
25
+
26
+ ```
27
+ engine/
28
+ ├── Cargo.toml # workspace root
29
+ └── crates/
30
+ ├── shared/ # kbdb-shared
31
+ ├── fs-database/ # kbdb-fs-database
32
+ ├── query-parser/ # this crate
33
+ └── kb-worker/ # kbdb-worker
34
+ ```
35
+
36
+ ## Directory Layout
37
+
38
+ ```
39
+ engine/crates/query-parser/
40
+ ├── Cargo.toml
41
+ └── src/
42
+ ├── lib.rs # crate root, WASM exports
43
+ ├── mem.rs # shared-memory helpers
44
+ ├── pipeline/
45
+ │ ├── mod.rs # pipeline orchestrator
46
+ │ ├── markers.rs # stage 1: strip markers
47
+ │ ├── unicode.rs # stage 2: NFC normalize
48
+ │ ├── segment.rs # stage 3: word boundaries
49
+ │ ├── identifiers.rs # stage 4: camelCase split
50
+ │ ├── case.rs # stage 5: case folding
51
+ │ ├── stopwords.rs # stage 6: stop words
52
+ │ ├── stem.rs # stage 7: Snowball stemmer
53
+ │ └── ngram.rs # stage 8: n-gram extract
54
+ ├── parse.rs # query string → QueryPlan
55
+ ├── extract/
56
+ │ ├── mod.rs # extraction facade
57
+ │ ├── rake.rs # RAKE algorithm
58
+ │ └── tfidf.rs # TF-IDF weighting
59
+ ├── rank/
60
+ │ ├── mod.rs # ranking facade
61
+ │ ├── bm25f.rs # BM25F scoring
62
+ │ ├── proximity.rs # proximity scoring
63
+ │ ├── zone.rs # zone scoring
64
+ │ └── freshness.rs # freshness weighting
65
+ └── cache.rs # query-scoped cache
66
+ ```
67
+
68
+ ## Modules
69
+
70
+ ### [types/](types/module.md) -- Structs
71
+
72
+ - **[token](types/token.md)** -- `Token`,
73
+ `TokenStream`, `Ngram`, `NgramType`, `StemResult`.
74
+ - **[keyphrase](types/keyphrase.md)** -- `Keyphrase`,
75
+ `WeightedTerm`, `BM25FParams`, `FieldWeights`.
76
+ - **[cache](types/cache.md)** -- `QueryCache`.
77
+ Short-lived query-scoped cache.
78
+
79
+ ### [functions/](functions/module.md) -- Operations
80
+
81
+ - **[pipeline](functions/pipeline.md)** -- 8-stage
82
+ tokenization pipeline. Marker stripping, NFC,
83
+ segmentation, identifier splitting, case folding,
84
+ stop words, stemming, n-grams.
85
+ - **[parse](functions/parse.md)** -- Query string
86
+ parsing into `QueryPlan`.
87
+ - **[extract](functions/extract.md)** -- RAKE
88
+ keyphrase extraction and TF-IDF term weighting.
89
+ - **[rank](functions/rank.md)** -- BM25F, proximity,
90
+ zone, and freshness scoring functions.
91
+
92
+ ## WASM Exports
93
+
94
+ | Export | Description |
95
+ |--------------------------|-------------------------|
96
+ | `qp_tokenize` | Full tokenization pipeline |
97
+ | `qp_parse_query` | Parse query → QueryPlan |
98
+ | `qp_normalize_term` | Normalize a single term |
99
+ | `qp_stem` | Stem a single term |
100
+ | `qp_extract_keywords` | RAKE keyphrase extraction |
101
+ | `qp_compute_tf` | Single-stream term frequency |
102
+ | `qp_compute_field_tf` | Per-field term frequency (heading, body, code) |
103
+ | `qp_compute_tfidf` | TF-IDF with supplied IDF |
104
+ | `qp_score_bm25f` | BM25F ranking score |
105
+ | `qp_score_proximity` | Proximity ranking score |
106
+ | `qp_score_zone` | Zone ranking score |
107
+ | `qp_score_freshness` | Freshness ranking score |
108
+ | `qp_split_identifiers` | camelCase/snake_case split |
109
+ | `alloc` / `free` | Memory management (from `kbdb-shared`) |
110
+
111
+ ## Dependencies
112
+
113
+ ```toml
114
+ [dependencies]
115
+ kbdb-shared = { path = "../shared" }
116
+ unicode-segmentation = "1"
117
+ unicode-normalization = "0.1"
118
+ rust-stemmers = "1"
119
+ regex = { version = "1", default-features = false }
120
+ ```
121
+
122
+ ## Build
123
+
124
+ ```sh
125
+ cd engine
126
+ wasm-pack build crates/query-parser \
127
+ --target no-modules \
128
+ --out-dir ../../../dist/wasm/query-parser
129
+ ```
130
+
131
+ ## Testing
132
+
133
+ ```sh
134
+ cd engine && cargo test -p kbdb-query-parser
135
+ cd engine && wasm-pack test --node crates/query-parser
136
+ ```
@@ -0,0 +1,39 @@
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.
@@ -0,0 +1,60 @@
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.
@@ -0,0 +1,17 @@
1
+ # query-parser/types -- Type Definitions
2
+
3
+ *Structs defined in the query-parser crate for
4
+ tokenization, keyword extraction, and ranking.*
5
+
6
+ ## Modules
7
+
8
+ - **[token](token.md)** -- `Token`, `TokenStream`,
9
+ `Ngram`, `NgramType`, `StemResult`. Output types
10
+ from the tokenization pipeline.
11
+ - **[keyphrase](keyphrase.md)** -- `Keyphrase`,
12
+ `WeightedTerm`, `BM25FParams`, `FieldWeights`.
13
+ Types for keyword extraction and ranking
14
+ configuration.
15
+ - **[cache](cache.md)** -- `QueryCache`. Short-lived
16
+ cache scoped to a single query or indexing
17
+ operation.
@@ -0,0 +1,42 @@
1
+ # types::token -- Tokenization Types
2
+
3
+ *Output types from the tokenization pipeline.*
4
+
5
+ ## Source
6
+
7
+ `engine/crates/query-parser/src/pipeline/mod.rs`
8
+
9
+ ## Type Definitions
10
+
11
+ ```rust
12
+ pub struct Token {
13
+ pub original: String,
14
+ pub stemmed: String,
15
+ pub position: u32,
16
+ }
17
+
18
+ pub struct TokenStream {
19
+ pub tokens: Vec<Token>,
20
+ pub ngrams: Vec<Ngram>,
21
+ }
22
+
23
+ pub struct Ngram {
24
+ pub terms: Vec<String>,
25
+ pub gram_type: NgramType,
26
+ }
27
+
28
+ pub enum NgramType { Bigram, Trigram }
29
+
30
+ pub struct StemResult {
31
+ pub original: String,
32
+ pub stemmed: String,
33
+ }
34
+ ```
35
+
36
+ `Token` preserves both original and stemmed forms so
37
+ the index can support exact-match and stem-match
38
+ queries. `position` is the zero-based token offset.
39
+
40
+ `TokenStream` is the output of `tokenize()` -- the
41
+ full set of tokens and extracted n-grams from a
42
+ single input text.