@dikolab/kbdb 0.1.6 → 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.
- package/README.md +30 -9
- package/dist/cli.cjs +181 -50
- package/dist/cli.cjs.map +3 -3
- package/dist/cli.mjs +181 -50
- package/dist/cli.mjs.map +3 -3
- package/dist/kbdb-worker.cjs +283 -29
- package/dist/kbdb-worker.cjs.map +4 -4
- package/dist/mod.cjs +13 -2
- package/dist/mod.cjs.map +2 -2
- package/dist/mod.mjs +13 -2
- package/dist/mod.mjs.map +2 -2
- package/dist/src/shared/cli/functions/format-output.function.d.ts +7 -7
- package/dist/src/shared/cli/functions/run-search.function.d.ts +25 -16
- package/dist/src/shared/cli/index.d.ts +1 -1
- package/dist/src/shared/cli/typings/cli-options.interface.d.ts +2 -0
- package/dist/src/shared/cli/typings/search-display.model.d.ts +25 -7
- package/dist/src/shared/mcp/functions/handle-recall.function.d.ts +29 -0
- package/dist/src/shared/mcp/typings/mcp-client.interface.d.ts +30 -11
- package/dist/src/shared/version/constants/version.constant.d.ts +1 -1
- package/dist/src/shared/wasm-codec/functions/decode-recall-result.function.d.ts +28 -0
- package/dist/src/shared/wasm-codec/functions/decode-scored-results.function.d.ts +14 -7
- package/dist/src/shared/wasm-codec/functions/encode-recall-params.function.d.ts +21 -0
- package/dist/src/shared/wasm-codec/functions/encode-search-params.function.d.ts +1 -1
- package/dist/src/shared/wasm-codec/index.d.ts +5 -2
- package/dist/src/shared/worker-client/classes/worker-client.class.d.ts +13 -3
- package/dist/src/shared/worker-client/index.d.ts +2 -1
- package/dist/src/shared/worker-client/typings/recall-result.model.d.ts +94 -0
- package/dist/src/shared/worker-client/typings/search-params.model.d.ts +2 -0
- package/dist/src/shared/worker-client/typings/search-result.model.d.ts +23 -7
- package/dist/src/shared/worker-daemon/classes/worker-daemon.class.d.ts +1 -0
- package/dist/wasm/fs-database/kbdb_fs_database.d.ts +18 -0
- package/dist/wasm/fs-database/kbdb_fs_database.js +30 -0
- package/dist/wasm/fs-database/kbdb_fs_database_bg.wasm +0 -0
- package/dist/wasm/fs-database/kbdb_fs_database_bg.wasm.d.ts +2 -0
- package/dist/wasm/kb-worker/kbdb_worker.d.ts +38 -0
- package/dist/wasm/kb-worker/kbdb_worker.js +56 -0
- package/dist/wasm/kb-worker/kbdb_worker_bg.wasm +0 -0
- package/dist/wasm/kb-worker/kbdb_worker_bg.wasm.d.ts +3 -0
- package/dist/worker.mjs +283 -29
- package/dist/worker.mjs.map +4 -4
- package/docs/details/README.md +26 -0
- package/docs/details/cli.md +608 -0
- package/docs/details/library-api.md +406 -0
- package/docs/details/mcp-server.md +430 -0
- package/docs/details/search-and-ranking.md +457 -0
- package/docs/details/storage.md +529 -0
- package/docs/goals/agents.md +751 -0
- package/docs/goals/architecture.svg +198 -0
- package/docs/goals/cli.md +2308 -0
- package/docs/goals/content-composer.md +609 -0
- package/docs/goals/content-parser.md +279 -0
- package/docs/goals/database.md +1452 -0
- package/docs/goals/document.md +368 -0
- package/docs/goals/mcp.md +1467 -0
- package/docs/goals/overview.md +124 -0
- package/docs/goals/query-parser.md +271 -0
- package/docs/goals/query-result.md +693 -0
- package/docs/goals/skills.md +738 -0
- package/docs/goals/worker-client.md +944 -0
- package/docs/goals/worker-daemon.md +1447 -0
- package/docs/modules/overview.md +298 -0
- package/docs/modules/rust/fs-database/functions/document.md +48 -0
- package/docs/modules/rust/fs-database/functions/integrity.md +68 -0
- package/docs/modules/rust/fs-database/functions/io.md +87 -0
- package/docs/modules/rust/fs-database/functions/module.md +22 -0
- package/docs/modules/rust/fs-database/functions/query-exec.md +44 -0
- package/docs/modules/rust/fs-database/functions/section.md +59 -0
- package/docs/modules/rust/fs-database/indexes/btree.md +35 -0
- package/docs/modules/rust/fs-database/indexes/corpus.md +61 -0
- package/docs/modules/rust/fs-database/indexes/inverted.md +60 -0
- package/docs/modules/rust/fs-database/indexes/module.md +64 -0
- package/docs/modules/rust/fs-database/indexes/positional.md +39 -0
- package/docs/modules/rust/fs-database/module.md +149 -0
- package/docs/modules/rust/fs-database/types/catalog.md +53 -0
- package/docs/modules/rust/fs-database/types/module.md +12 -0
- package/docs/modules/rust/fs-database/types/section.md +101 -0
- package/docs/modules/rust/kb-worker/functions/compose.md +57 -0
- package/docs/modules/rust/kb-worker/functions/module.md +20 -0
- package/docs/modules/rust/kb-worker/functions/retrieve.md +194 -0
- package/docs/modules/rust/kb-worker/functions/search.md +173 -0
- package/docs/modules/rust/kb-worker/functions/write.md +172 -0
- package/docs/modules/rust/kb-worker/module.md +171 -0
- package/docs/modules/rust/kb-worker/types/cache.md +90 -0
- package/docs/modules/rust/kb-worker/types/context.md +26 -0
- package/docs/modules/rust/kb-worker/types/module.md +16 -0
- package/docs/modules/rust/kb-worker/types/status.md +29 -0
- package/docs/modules/rust/kb-worker/types/token.md +50 -0
- package/docs/modules/rust/overview.md +495 -0
- package/docs/modules/rust/query-parser/functions/extract.md +56 -0
- package/docs/modules/rust/query-parser/functions/module.md +16 -0
- package/docs/modules/rust/query-parser/functions/parse.md +33 -0
- package/docs/modules/rust/query-parser/functions/pipeline.md +103 -0
- package/docs/modules/rust/query-parser/functions/rank.md +85 -0
- package/docs/modules/rust/query-parser/module.md +136 -0
- package/docs/modules/rust/query-parser/types/cache.md +39 -0
- package/docs/modules/rust/query-parser/types/keyphrase.md +60 -0
- package/docs/modules/rust/query-parser/types/module.md +17 -0
- package/docs/modules/rust/query-parser/types/token.md +42 -0
- package/docs/modules/rust/shared/content-composer/functions.md +114 -0
- package/docs/modules/rust/shared/content-composer/module.md +147 -0
- package/docs/modules/rust/shared/content-composer/types.md +93 -0
- package/docs/modules/rust/shared/content-parser/functions.md +72 -0
- package/docs/modules/rust/shared/content-parser/module.md +99 -0
- package/docs/modules/rust/shared/content-parser/types.md +71 -0
- package/docs/modules/rust/shared/corpus/module.md +24 -0
- package/docs/modules/rust/shared/corpus/types.md +141 -0
- package/docs/modules/rust/shared/document/module.md +25 -0
- package/docs/modules/rust/shared/document/types.md +154 -0
- package/docs/modules/rust/shared/encode/module.md +22 -0
- package/docs/modules/rust/shared/encode/traits.md +304 -0
- package/docs/modules/rust/shared/error/module.md +28 -0
- package/docs/modules/rust/shared/error/types.md +302 -0
- package/docs/modules/rust/shared/kbid/module.md +24 -0
- package/docs/modules/rust/shared/kbid/types.md +147 -0
- package/docs/modules/rust/shared/memory/functions.md +209 -0
- package/docs/modules/rust/shared/memory/module.md +24 -0
- package/docs/modules/rust/shared/module.md +168 -0
- package/docs/modules/rust/shared/query/module.md +27 -0
- package/docs/modules/rust/shared/query/types.md +236 -0
- package/docs/modules/rust/shared/section/module.md +25 -0
- package/docs/modules/rust/shared/section/types.md +294 -0
- package/docs/modules/rust/shared/term/module.md +25 -0
- package/docs/modules/rust/shared/term/types.md +139 -0
- package/docs/modules/typescript/cli.md +129 -0
- package/docs/modules/typescript/kbdb-worker.md +135 -0
- package/docs/modules/typescript/overview.md +378 -0
- package/docs/modules/typescript/shared/cli/constants.md +23 -0
- package/docs/modules/typescript/shared/cli/functions.md +630 -0
- package/docs/modules/typescript/shared/cli/module.md +125 -0
- package/docs/modules/typescript/shared/cli/typings.md +172 -0
- package/docs/modules/typescript/shared/hash/functions.md +20 -0
- package/docs/modules/typescript/shared/hash/module.md +21 -0
- package/docs/modules/typescript/shared/mcp/classes.md +179 -0
- package/docs/modules/typescript/shared/mcp/functions.md +386 -0
- package/docs/modules/typescript/shared/mcp/module.md +160 -0
- package/docs/modules/typescript/shared/mcp/typings.md +522 -0
- package/docs/modules/typescript/shared/module.md +94 -0
- package/docs/modules/typescript/shared/platform/functions.md +42 -0
- package/docs/modules/typescript/shared/platform/module.md +25 -0
- package/docs/modules/typescript/shared/runtime/functions.md +26 -0
- package/docs/modules/typescript/shared/runtime/module.md +27 -0
- package/docs/modules/typescript/shared/runtime/typings.md +13 -0
- package/docs/modules/typescript/shared/version/module.md +27 -0
- package/docs/modules/typescript/shared/wasm-codec/functions.md +391 -0
- package/docs/modules/typescript/shared/wasm-codec/module.md +98 -0
- package/docs/modules/typescript/shared/worker-client/classes.md +264 -0
- package/docs/modules/typescript/shared/worker-client/functions.md +175 -0
- package/docs/modules/typescript/shared/worker-client/module.md +84 -0
- package/docs/modules/typescript/shared/worker-client/typings.md +508 -0
- package/docs/modules/typescript/shared/worker-daemon/classes.md +239 -0
- package/docs/modules/typescript/shared/worker-daemon/constants.md +37 -0
- package/docs/modules/typescript/shared/worker-daemon/functions.md +221 -0
- package/docs/modules/typescript/shared/worker-daemon/module.md +112 -0
- package/docs/modules/typescript/shared/worker-daemon/typings.md +126 -0
- package/docs/overview.md +188 -0
- package/docs/release-notes/0.1.0.md +189 -0
- package/docs/release-notes/0.1.1.md +46 -0
- package/docs/release-notes/0.1.2.md +38 -0
- package/docs/release-notes/0.1.3.md +42 -0
- package/docs/release-notes/0.2.0.md +147 -0
- package/docs/release-notes/README.md +9 -0
- package/package.json +8 -2
- package/src/shared/cli/functions/dispatch-command.function.ts +3 -3
- package/src/shared/cli/functions/format-output.function.ts +41 -34
- package/src/shared/cli/functions/parse-args.function.ts +7 -0
- package/src/shared/cli/functions/run-search.function.ts +53 -27
- package/src/shared/cli/index.ts +4 -1
- package/src/shared/cli/typings/cli-options.interface.ts +2 -0
- package/src/shared/cli/typings/search-display.model.ts +26 -7
- package/src/shared/mcp/functions/create-tool-definitions.function.ts +64 -4
- package/src/shared/mcp/functions/handle-recall.function.ts +66 -0
- package/src/shared/mcp/functions/handle-tool-call.function.ts +11 -1
- package/src/shared/mcp/typings/mcp-client.interface.ts +33 -15
- package/src/shared/version/constants/version.constant.ts +1 -1
- package/src/shared/wasm-codec/functions/decode-recall-result.function.ts +267 -0
- package/src/shared/wasm-codec/functions/decode-scored-results.function.ts +81 -27
- package/src/shared/wasm-codec/functions/encode-recall-params.function.ts +68 -0
- package/src/shared/wasm-codec/functions/encode-search-params.function.ts +4 -1
- package/src/shared/wasm-codec/index.ts +11 -3
- package/src/shared/worker-client/classes/worker-client.class.ts +24 -4
- package/src/shared/worker-client/index.ts +11 -1
- package/src/shared/worker-client/typings/recall-result.model.ts +98 -0
- package/src/shared/worker-client/typings/search-params.model.ts +2 -0
- package/src/shared/worker-client/typings/search-result.model.ts +24 -7
- package/src/shared/worker-daemon/classes/worker-daemon.class.ts +25 -4
- package/src/wasm/fs-database/kbdb_fs_database.d.ts +18 -0
- package/src/wasm/fs-database/kbdb_fs_database.js +30 -0
- package/src/wasm/fs-database/kbdb_fs_database_bg.wasm +0 -0
- package/src/wasm/fs-database/kbdb_fs_database_bg.wasm.d.ts +2 -0
- package/src/wasm/kb-worker/kbdb_worker.d.ts +38 -0
- package/src/wasm/kb-worker/kbdb_worker.js +56 -0
- package/src/wasm/kb-worker/kbdb_worker_bg.wasm +0 -0
- package/src/wasm/kb-worker/kbdb_worker_bg.wasm.d.ts +3 -0
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
# Modules Overview -- Build Pipeline
|
|
2
|
+
|
|
3
|
+
*How the Rust engine compiles to WASM, lands in the
|
|
4
|
+
TypeScript source tree, and gets bundled into the
|
|
5
|
+
final `dist/` output.*
|
|
6
|
+
|
|
7
|
+
## Build Pipeline
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
1. Rust/WASM 2. TypeScript
|
|
11
|
+
───────────── ────────────────
|
|
12
|
+
|
|
13
|
+
engine/crates/ src/shared/
|
|
14
|
+
├── shared (rlib) ├── worker-daemon/
|
|
15
|
+
├── fs-database (cdylib) │ ├── wasm/ ← 3 WASM modules
|
|
16
|
+
├── query-parser (cdylib) │ │ ├── fs-database.*
|
|
17
|
+
└── kb-worker (cdylib) │ │ ├── query-parser.*
|
|
18
|
+
│ │ │ └── kb-worker.*
|
|
19
|
+
│ wasm-pack build │ └── functions/
|
|
20
|
+
└─────────────────────┘ └── load-wasm-modules
|
|
21
|
+
.function.ts
|
|
22
|
+
│
|
|
23
|
+
│ esbuild + tsc
|
|
24
|
+
▼
|
|
25
|
+
dist/
|
|
26
|
+
├── cli.mjs
|
|
27
|
+
├── worker.mjs
|
|
28
|
+
├── mod.mjs
|
|
29
|
+
└── ...
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Phase 1 -- Build Rust to WASM
|
|
33
|
+
|
|
34
|
+
The Rust engine compiles to three standalone `.wasm`
|
|
35
|
+
files. The `kbdb-shared` crate is an `rlib` -- it
|
|
36
|
+
produces no artifact, compiled **into** each consumer
|
|
37
|
+
via dead code elimination.
|
|
38
|
+
|
|
39
|
+
### Crates
|
|
40
|
+
|
|
41
|
+
| Crate | Type | Output |
|
|
42
|
+
|--------------------|----------|-----------------------|
|
|
43
|
+
| `kbdb-shared` | `rlib` | none (compiled into consumers) |
|
|
44
|
+
| `kbdb-fs-database` | `cdylib` | `fs-database.wasm` |
|
|
45
|
+
| `kbdb-query-parser`| `cdylib` | `query-parser.wasm` |
|
|
46
|
+
| `kbdb-worker` | `cdylib` | `kb-worker.wasm` |
|
|
47
|
+
|
|
48
|
+
### Build Order
|
|
49
|
+
|
|
50
|
+
```
|
|
51
|
+
1. kbdb-shared (leaf, no deps)
|
|
52
|
+
2. kbdb-fs-database ┐ (both depend on shared,
|
|
53
|
+
kbdb-query-parser ┘ built in parallel)
|
|
54
|
+
3. kbdb-worker (depends on all three)
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Build Command
|
|
58
|
+
|
|
59
|
+
```sh
|
|
60
|
+
npm run build:wasm
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
This runs `wasm-pack build` for each cdylib crate.
|
|
64
|
+
Output lands in the TypeScript source tree at
|
|
65
|
+
`src/shared/worker-daemon/wasm/`:
|
|
66
|
+
|
|
67
|
+
```sh
|
|
68
|
+
wasm-pack build crates/fs-database \
|
|
69
|
+
--target no-modules \
|
|
70
|
+
--out-dir ../../../../src/shared/worker-daemon/wasm/fs-database
|
|
71
|
+
|
|
72
|
+
wasm-pack build crates/query-parser \
|
|
73
|
+
--target no-modules \
|
|
74
|
+
--out-dir ../../../../src/shared/worker-daemon/wasm/query-parser
|
|
75
|
+
|
|
76
|
+
wasm-pack build crates/kb-worker \
|
|
77
|
+
--target no-modules \
|
|
78
|
+
--out-dir ../../../../src/shared/worker-daemon/wasm/kb-worker
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### Size Optimization
|
|
82
|
+
|
|
83
|
+
```toml
|
|
84
|
+
# engine/Cargo.toml [profile.release]
|
|
85
|
+
opt-level = "z" # optimize for size
|
|
86
|
+
lto = true # link-time optimization
|
|
87
|
+
codegen-units = 1 # single codegen unit
|
|
88
|
+
strip = true # remove debug symbols
|
|
89
|
+
panic = "abort" # no unwind tables
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
Each `.wasm` includes only the `kbdb-shared` code it
|
|
93
|
+
actually references -- LLVM eliminates unused code
|
|
94
|
+
per consumer.
|
|
95
|
+
|
|
96
|
+
**Full Rust architecture:**
|
|
97
|
+
[rust/overview.md](rust/overview.md)
|
|
98
|
+
|
|
99
|
+
---
|
|
100
|
+
|
|
101
|
+
## Phase 2 -- WASM in TypeScript Source
|
|
102
|
+
|
|
103
|
+
The WASM build output lands inside the TypeScript
|
|
104
|
+
source tree, where the `worker-daemon` module loads
|
|
105
|
+
it at runtime:
|
|
106
|
+
|
|
107
|
+
```
|
|
108
|
+
src/shared/worker-daemon/
|
|
109
|
+
├── wasm/ ← gitignored
|
|
110
|
+
│ ├── fs-database/
|
|
111
|
+
│ │ ├── fs-database.wasm
|
|
112
|
+
│ │ ├── fs-database.js ← JS glue
|
|
113
|
+
│ │ └── fs-database.d.ts ← type defs
|
|
114
|
+
│ ├── query-parser/
|
|
115
|
+
│ │ ├── query-parser.wasm
|
|
116
|
+
│ │ ├── query-parser.js
|
|
117
|
+
│ │ └── query-parser.d.ts
|
|
118
|
+
│ └── kb-worker/
|
|
119
|
+
│ ├── kb-worker.wasm
|
|
120
|
+
│ ├── kb-worker.js
|
|
121
|
+
│ └── kb-worker.d.ts
|
|
122
|
+
│
|
|
123
|
+
└── functions/
|
|
124
|
+
└── load-wasm-modules.function.ts
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
The `wasm/` directory is **gitignored** -- it is a
|
|
128
|
+
build artifact, regenerated by `npm run build:wasm`.
|
|
129
|
+
Never edit files inside it manually.
|
|
130
|
+
|
|
131
|
+
### How TypeScript Loads WASM
|
|
132
|
+
|
|
133
|
+
The `loadWasmModules` function in
|
|
134
|
+
`shared/worker-daemon/` performs the loading sequence:
|
|
135
|
+
|
|
136
|
+
```
|
|
137
|
+
loadWasmModules(config)
|
|
138
|
+
│
|
|
139
|
+
├─ 1. Allocate shared WebAssembly.Memory
|
|
140
|
+
│
|
|
141
|
+
├─ 2. Compile + instantiate query-parser.wasm
|
|
142
|
+
│ (standalone, shared memory only)
|
|
143
|
+
│
|
|
144
|
+
├─ 3. Compile + instantiate fs-database.wasm
|
|
145
|
+
│ (standalone, shared memory + host I/O)
|
|
146
|
+
│ ↑ steps 2 and 3 run in parallel
|
|
147
|
+
│
|
|
148
|
+
└─ 4. Compile + instantiate kb-worker.wasm
|
|
149
|
+
(imports query-parser + fs-database exports)
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
All three modules share one `WebAssembly.Memory`
|
|
153
|
+
instance. Data crosses module boundaries via
|
|
154
|
+
`(pointer, length)` pairs encoded with the `Encode`
|
|
155
|
+
trait from `kbdb-shared`.
|
|
156
|
+
|
|
157
|
+
Only the `worker-daemon` module imports WASM. No other
|
|
158
|
+
TypeScript module directly accesses WASM files or the
|
|
159
|
+
`WebAssembly` API.
|
|
160
|
+
|
|
161
|
+
---
|
|
162
|
+
|
|
163
|
+
## Phase 3 -- TypeScript Modules
|
|
164
|
+
|
|
165
|
+
All TypeScript logic lives under `src/shared/` in
|
|
166
|
+
8 sub-modules. Two thin entry points at the project
|
|
167
|
+
root dispatch into them.
|
|
168
|
+
|
|
169
|
+
### Entry Points
|
|
170
|
+
|
|
171
|
+
| File | Purpose |
|
|
172
|
+
|--------------------|---------------------------|
|
|
173
|
+
| `src/cli.ts` | CLI + MCP executable |
|
|
174
|
+
| `src/worker.ts` | Worker daemon executable |
|
|
175
|
+
| `mod.ts` | Library API (npm / JSR) |
|
|
176
|
+
|
|
177
|
+
### Shared Modules
|
|
178
|
+
|
|
179
|
+
| Module | Responsibility |
|
|
180
|
+
|------------------|----------------------------|
|
|
181
|
+
| `cli/` | CLI arg parsing, command handlers, output formatting |
|
|
182
|
+
| `mcp/` | MCP server, tool + resource handlers |
|
|
183
|
+
| `worker-client/` | IPC client -- discovers/spawns daemon, typed async API |
|
|
184
|
+
| `worker-daemon/` | Daemon process -- loads WASM, IPC server, config |
|
|
185
|
+
| `version/` | Package version constant |
|
|
186
|
+
| `runtime/` | Deno / Node.js detection |
|
|
187
|
+
| `platform/` | OS abstractions (tmpdir, PID, IPC paths) |
|
|
188
|
+
| `hash/` | SHA-256 via Web Crypto API |
|
|
189
|
+
|
|
190
|
+
### Module Dependencies
|
|
191
|
+
|
|
192
|
+
```
|
|
193
|
+
src/cli.ts
|
|
194
|
+
├── shared/cli/ ──→ shared/worker-client/
|
|
195
|
+
└── shared/mcp/ ──→ shared/worker-client/
|
|
196
|
+
├── shared/platform/
|
|
197
|
+
└── shared/hash/
|
|
198
|
+
|
|
199
|
+
src/worker.ts
|
|
200
|
+
└── shared/worker-daemon/
|
|
201
|
+
├── shared/platform/
|
|
202
|
+
└── wasm/ (fs-database, query-parser,
|
|
203
|
+
kb-worker)
|
|
204
|
+
|
|
205
|
+
mod.ts
|
|
206
|
+
└── shared/version/
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
**Full TypeScript architecture:**
|
|
210
|
+
[typescript/overview.md](typescript/overview.md)
|
|
211
|
+
|
|
212
|
+
---
|
|
213
|
+
|
|
214
|
+
## Phase 4 -- Build to dist/
|
|
215
|
+
|
|
216
|
+
esbuild bundles the TypeScript entry points into
|
|
217
|
+
`dist/` as ESM (with code-splitting) and CJS. `tsc`
|
|
218
|
+
emits declaration files separately.
|
|
219
|
+
|
|
220
|
+
### Build Command
|
|
221
|
+
|
|
222
|
+
```sh
|
|
223
|
+
npm run build # TypeScript only
|
|
224
|
+
npm run build:all # WASM + TypeScript
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
### Build Steps
|
|
228
|
+
|
|
229
|
+
```
|
|
230
|
+
1. esbuild bundles:
|
|
231
|
+
├── src/cli.ts → dist/cli.mjs + dist/cli.cjs
|
|
232
|
+
├── src/worker.ts → dist/worker.mjs + dist/worker.cjs
|
|
233
|
+
├── mod.ts → dist/mod.mjs + dist/mod.cjs
|
|
234
|
+
└── shared code → dist/chunk-*.mjs (code-split)
|
|
235
|
+
|
|
236
|
+
2. tsc --emitDeclarationOnly:
|
|
237
|
+
└── *.d.ts files for all entry points
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
### Output
|
|
241
|
+
|
|
242
|
+
```
|
|
243
|
+
dist/
|
|
244
|
+
├── cli.mjs ← CLI + MCP (ESM, shebang)
|
|
245
|
+
├── cli.cjs ← CLI + MCP (CJS, shebang)
|
|
246
|
+
├── worker.mjs ← daemon (ESM, shebang)
|
|
247
|
+
├── worker.cjs ← daemon (CJS, shebang)
|
|
248
|
+
├── mod.mjs ← library API (ESM)
|
|
249
|
+
├── mod.cjs ← library API (CJS)
|
|
250
|
+
├── mod.d.ts ← declarations
|
|
251
|
+
├── cli.d.ts
|
|
252
|
+
├── worker.d.ts
|
|
253
|
+
├── chunk-*.mjs ← shared code chunks
|
|
254
|
+
└── src/
|
|
255
|
+
└── shared/
|
|
256
|
+
└── **/*.d.ts ← per-file declarations
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
WASM files are **not** bundled into `dist/`. They ship
|
|
260
|
+
as separate `.wasm` binaries in the npm package and
|
|
261
|
+
are loaded at runtime by the daemon via
|
|
262
|
+
`WebAssembly.compile()`.
|
|
263
|
+
|
|
264
|
+
---
|
|
265
|
+
|
|
266
|
+
## Full Build Sequence
|
|
267
|
+
|
|
268
|
+
```sh
|
|
269
|
+
# 1. Build WASM (Rust → .wasm → src/shared/worker-daemon/wasm/)
|
|
270
|
+
npm run build:wasm
|
|
271
|
+
|
|
272
|
+
# 2. Build TypeScript (src/ → dist/)
|
|
273
|
+
npm run build
|
|
274
|
+
|
|
275
|
+
# Or both in one command:
|
|
276
|
+
npm run build:all
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
### build:all Sequence
|
|
280
|
+
|
|
281
|
+
```
|
|
282
|
+
build:all
|
|
283
|
+
│
|
|
284
|
+
├─ build:wasm
|
|
285
|
+
│ ├─ cargo build (4 crates)
|
|
286
|
+
│ └─ wasm-pack build (3 cdylib → wasm/)
|
|
287
|
+
│
|
|
288
|
+
└─ build
|
|
289
|
+
├─ esbuild (2 entry points → dist/)
|
|
290
|
+
└─ tsc --emitDeclarationOnly (→ dist/*.d.ts)
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
## Module Documentation Index
|
|
294
|
+
|
|
295
|
+
| Layer | Overview | Modules |
|
|
296
|
+
|------------|-----------------------------------|---------|
|
|
297
|
+
| Rust/WASM | [rust/overview.md](rust/overview.md) | 4 crates, 9 shared namespaces |
|
|
298
|
+
| TypeScript | [typescript/overview.md](typescript/overview.md) | 2 entry points, 8 shared modules |
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# functions::document -- Document Manifest CRUD
|
|
2
|
+
|
|
3
|
+
*Document manifest operations: read, write, remove,
|
|
4
|
+
and section reference updates.*
|
|
5
|
+
|
|
6
|
+
## Source
|
|
7
|
+
|
|
8
|
+
`engine/crates/fs-database/src/document.rs`
|
|
9
|
+
|
|
10
|
+
## Functions
|
|
11
|
+
|
|
12
|
+
```rust
|
|
13
|
+
pub fn read_document(docid: &str)
|
|
14
|
+
-> Result<DocumentManifest, FsError>;
|
|
15
|
+
|
|
16
|
+
pub fn write_document(manifest: &DocumentManifest)
|
|
17
|
+
-> Result<(), FsError>;
|
|
18
|
+
|
|
19
|
+
pub fn remove_document(docid: &str)
|
|
20
|
+
-> Result<u32, FsError>;
|
|
21
|
+
|
|
22
|
+
pub fn update_section_ref(
|
|
23
|
+
old_kbid: &KbId, new_kbid: &KbId,
|
|
24
|
+
) -> Result<u32, FsError>;
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
### `write_document`
|
|
28
|
+
|
|
29
|
+
Serializes the manifest to TOML and writes to
|
|
30
|
+
`documents/{docid}.toml` atomically. Appends the
|
|
31
|
+
docid to each referenced section's `docids` array
|
|
32
|
+
in its header. Updates catalog.
|
|
33
|
+
|
|
34
|
+
### `remove_document`
|
|
35
|
+
|
|
36
|
+
Deletes the manifest file. Removes the docid from
|
|
37
|
+
each section's `docids` array in its header.
|
|
38
|
+
Sections themselves are not deleted -- only the
|
|
39
|
+
grouping is removed. Returns the number of
|
|
40
|
+
ungrouped sections.
|
|
41
|
+
|
|
42
|
+
### `update_section_ref`
|
|
43
|
+
|
|
44
|
+
Scans all document manifests and replaces occurrences
|
|
45
|
+
of `old_kbid` with `new_kbid`, preserving position.
|
|
46
|
+
Called during section updates (content-addressable
|
|
47
|
+
storage produces new kb-ids on content change).
|
|
48
|
+
Returns the number of updated documents.
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# functions::integrity -- Integrity and Maintenance
|
|
2
|
+
|
|
3
|
+
*Integrity checks, garbage collection, and full index
|
|
4
|
+
rebuild.*
|
|
5
|
+
|
|
6
|
+
## Source
|
|
7
|
+
|
|
8
|
+
`engine/crates/fs-database/src/integrity.rs`
|
|
9
|
+
|
|
10
|
+
## Functions
|
|
11
|
+
|
|
12
|
+
```rust
|
|
13
|
+
pub fn check_integrity()
|
|
14
|
+
-> Result<IntegrityReport, FsError>;
|
|
15
|
+
|
|
16
|
+
pub fn garbage_collect()
|
|
17
|
+
-> Result<u32, FsError>;
|
|
18
|
+
|
|
19
|
+
pub fn rebuild_indexes()
|
|
20
|
+
-> Result<(), FsError>;
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
### `check_integrity`
|
|
24
|
+
|
|
25
|
+
- Verify section file checksums match content.
|
|
26
|
+
- Detect orphaned sections (not referenced by any
|
|
27
|
+
document).
|
|
28
|
+
- Detect orphaned documents (referencing missing
|
|
29
|
+
sections).
|
|
30
|
+
- Find broken content marker references across all
|
|
31
|
+
six marker types:
|
|
32
|
+
- `$(image: {kbid})` -- referenced section must
|
|
33
|
+
exist and have type `image/*`.
|
|
34
|
+
- `$(code: {kbid})` -- referenced section must
|
|
35
|
+
exist and have type `text/*` or
|
|
36
|
+
`application/json`.
|
|
37
|
+
- `$(text: {kbid})` -- referenced section must
|
|
38
|
+
exist and have type `text/markdown`.
|
|
39
|
+
- `$(document: {docid})` -- referenced document
|
|
40
|
+
manifest must exist.
|
|
41
|
+
- `$(link: {kbid})` -- referenced section must
|
|
42
|
+
exist (any type).
|
|
43
|
+
- `$(doc-link: {docid})` -- referenced document
|
|
44
|
+
manifest must exist.
|
|
45
|
+
- Detect circular references in `$(text:)` and
|
|
46
|
+
`$(document:)` marker chains.
|
|
47
|
+
|
|
48
|
+
### `garbage_collect`
|
|
49
|
+
|
|
50
|
+
Remove section files not referenced by any document
|
|
51
|
+
manifest. Returns the number of removed files.
|
|
52
|
+
|
|
53
|
+
### `rebuild_indexes`
|
|
54
|
+
|
|
55
|
+
Reconstruct all index files (`terms.idx`,
|
|
56
|
+
`positions.idx`, `corpus.dat`) from sections and
|
|
57
|
+
document manifests. Recovers from index corruption.
|
|
58
|
+
|
|
59
|
+
Rebuild recomputes per-field TFs (heading, body,
|
|
60
|
+
code) from section content via per-field
|
|
61
|
+
tokenization, recomputes per-field token counts
|
|
62
|
+
(`heading_token_count`, `body_token_count`,
|
|
63
|
+
`code_token_count`) in section headers, and
|
|
64
|
+
recalculates per-field corpus averages
|
|
65
|
+
(`avg_heading_length`, `avg_body_length`,
|
|
66
|
+
`avg_code_length`) in `corpus.dat`. Image sections
|
|
67
|
+
have their title and description re-tokenized into
|
|
68
|
+
heading and body fields respectively.
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# functions::io -- Host Import Wrappers
|
|
2
|
+
|
|
3
|
+
*Raw extern FFI bindings for filesystem operations
|
|
4
|
+
provided by the TypeScript host.*
|
|
5
|
+
|
|
6
|
+
## Source
|
|
7
|
+
|
|
8
|
+
`engine/crates/fs-database/src/io.rs`
|
|
9
|
+
|
|
10
|
+
## Overview
|
|
11
|
+
|
|
12
|
+
`fs-database.wasm` cannot access the filesystem
|
|
13
|
+
directly. The TypeScript daemon provides file I/O
|
|
14
|
+
functions as WASM imports when instantiating the
|
|
15
|
+
module. All paths are relative to the `.kbdb` context
|
|
16
|
+
directory -- the host resolves them to absolute paths
|
|
17
|
+
and enforces sandbox boundaries.
|
|
18
|
+
|
|
19
|
+
## Host Imports
|
|
20
|
+
|
|
21
|
+
```rust
|
|
22
|
+
#[link(wasm_import_module = "kbdb_host")]
|
|
23
|
+
extern "C" {
|
|
24
|
+
fn host_read_file(
|
|
25
|
+
path_ptr: *const u8, path_len: u32,
|
|
26
|
+
buf_ptr: *mut u8, buf_len: u32,
|
|
27
|
+
) -> i32;
|
|
28
|
+
|
|
29
|
+
fn host_write_file(
|
|
30
|
+
path_ptr: *const u8, path_len: u32,
|
|
31
|
+
data_ptr: *const u8, data_len: u32,
|
|
32
|
+
) -> i32;
|
|
33
|
+
|
|
34
|
+
fn host_remove_file(
|
|
35
|
+
path_ptr: *const u8, path_len: u32,
|
|
36
|
+
) -> i32;
|
|
37
|
+
|
|
38
|
+
fn host_list_dir(
|
|
39
|
+
path_ptr: *const u8, path_len: u32,
|
|
40
|
+
buf_ptr: *mut u8, buf_len: u32,
|
|
41
|
+
) -> i32;
|
|
42
|
+
|
|
43
|
+
fn host_mkdir(
|
|
44
|
+
path_ptr: *const u8, path_len: u32,
|
|
45
|
+
) -> i32;
|
|
46
|
+
|
|
47
|
+
fn host_file_exists(
|
|
48
|
+
path_ptr: *const u8, path_len: u32,
|
|
49
|
+
) -> i32;
|
|
50
|
+
|
|
51
|
+
fn host_rename(
|
|
52
|
+
from_ptr: *const u8, from_len: u32,
|
|
53
|
+
to_ptr: *const u8, to_len: u32,
|
|
54
|
+
) -> i32;
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Return Conventions
|
|
59
|
+
|
|
60
|
+
- Positive integer = bytes written or entry count.
|
|
61
|
+
- `0` = success (for void operations).
|
|
62
|
+
- `-1` = file not found.
|
|
63
|
+
- `-2` = permission denied.
|
|
64
|
+
- `-3` = I/O error.
|
|
65
|
+
|
|
66
|
+
The `io.rs` module wraps these raw imports in safe
|
|
67
|
+
Rust functions that return `Result<T, IoError>`.
|
|
68
|
+
|
|
69
|
+
## Shared Memory
|
|
70
|
+
|
|
71
|
+
All data exchange with the host uses the shared
|
|
72
|
+
`WebAssembly.Memory` buffer. Strings and byte slices
|
|
73
|
+
are passed as `(pointer, length)` pairs. The crate
|
|
74
|
+
re-exports `alloc` and `free` from `kbdb-shared` for
|
|
75
|
+
the host to manage buffer allocation.
|
|
76
|
+
|
|
77
|
+
## Atomic Writes (`atomic.rs`)
|
|
78
|
+
|
|
79
|
+
```rust
|
|
80
|
+
pub fn atomic_write(
|
|
81
|
+
path: &str, data: &[u8],
|
|
82
|
+
) -> Result<(), FsError>;
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Writes data to a temporary file in the same
|
|
86
|
+
directory, then renames it into place via
|
|
87
|
+
`host_rename`. Prevents partial writes on crash.
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# fs-database/functions -- Operations
|
|
2
|
+
|
|
3
|
+
*Functions exported by the fs-database crate for
|
|
4
|
+
section CRUD, document manifest management, query
|
|
5
|
+
execution, and maintenance.*
|
|
6
|
+
|
|
7
|
+
## Modules
|
|
8
|
+
|
|
9
|
+
- **[io](io.md)** -- Host import wrappers. Raw extern
|
|
10
|
+
FFI bindings for filesystem operations, return
|
|
11
|
+
conventions, shared memory protocol.
|
|
12
|
+
- **[section](section.md)** -- Section CRUD operations.
|
|
13
|
+
`read_section`, `add_section`, `update_section`,
|
|
14
|
+
`remove_section`.
|
|
15
|
+
- **[document](document.md)** -- Document manifest
|
|
16
|
+
CRUD. `read_document`, `write_document`,
|
|
17
|
+
`remove_document`, `update_section_ref`.
|
|
18
|
+
- **[query-exec](query-exec.md)** -- Query plan
|
|
19
|
+
execution against indexes. Returns raw matches with
|
|
20
|
+
term frequencies and positional data.
|
|
21
|
+
- **[integrity](integrity.md)** -- Integrity checks,
|
|
22
|
+
garbage collection, full index rebuild.
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# functions::query-exec -- Query Plan Execution
|
|
2
|
+
|
|
3
|
+
*Execute pre-parsed query plans against the indexes
|
|
4
|
+
and return raw matches.*
|
|
5
|
+
|
|
6
|
+
## Source
|
|
7
|
+
|
|
8
|
+
`engine/crates/fs-database/src/query_exec.rs`
|
|
9
|
+
|
|
10
|
+
## Function
|
|
11
|
+
|
|
12
|
+
```rust
|
|
13
|
+
pub fn execute_plan(
|
|
14
|
+
plan: &QueryPlan,
|
|
15
|
+
inverted: &InvertedIndex,
|
|
16
|
+
positional: &PositionalIndex,
|
|
17
|
+
corpus: &CorpusStats,
|
|
18
|
+
) -> Vec<RawMatch>;
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Does not parse queries or rank results -- those are
|
|
22
|
+
`query-parser.wasm` responsibilities orchestrated by
|
|
23
|
+
`kb-worker.wasm`.
|
|
24
|
+
|
|
25
|
+
## Execution Steps
|
|
26
|
+
|
|
27
|
+
1. Look up each term in `plan.terms` in the inverted
|
|
28
|
+
index.
|
|
29
|
+
2. Combine posting lists based on `plan.mode`:
|
|
30
|
+
- `And` -- intersect all posting lists.
|
|
31
|
+
- `Or` -- union all posting lists.
|
|
32
|
+
3. Remove sections matching any term in
|
|
33
|
+
`plan.exclusions`.
|
|
34
|
+
4. For phrase queries (`plan.phrases`), verify
|
|
35
|
+
adjacent positions in the positional index.
|
|
36
|
+
5. For each surviving section, collect per-field
|
|
37
|
+
term frequencies (heading_tf, body_tf, code_tf
|
|
38
|
+
from inverted index), positional data from the
|
|
39
|
+
positional index, `token_count` and
|
|
40
|
+
`field_token_counts` from the section header
|
|
41
|
+
into a `RawMatch`.
|
|
42
|
+
|
|
43
|
+
Returns raw matches. `kb-worker.wasm` passes these to
|
|
44
|
+
`query-parser.wasm`'s ranking functions for scoring.
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# functions::section -- Section CRUD
|
|
2
|
+
|
|
3
|
+
*Content-addressable section file operations: read,
|
|
4
|
+
add, update, remove.*
|
|
5
|
+
|
|
6
|
+
## Source
|
|
7
|
+
|
|
8
|
+
`engine/crates/fs-database/src/section/mod.rs`
|
|
9
|
+
|
|
10
|
+
## Functions
|
|
11
|
+
|
|
12
|
+
```rust
|
|
13
|
+
pub fn read_section(kbid: &KbId)
|
|
14
|
+
-> Result<SectionRecord, FsError>;
|
|
15
|
+
|
|
16
|
+
pub fn read_sections(kbids: &[KbId])
|
|
17
|
+
-> Result<Vec<SectionRecord>, FsError>;
|
|
18
|
+
|
|
19
|
+
pub fn add_section(params: &AddSectionParams)
|
|
20
|
+
-> Result<AddSectionResult, FsError>;
|
|
21
|
+
|
|
22
|
+
pub fn update_section(
|
|
23
|
+
kbid: &KbId, new_content: &[u8],
|
|
24
|
+
) -> Result<UpdateSectionResult, FsError>;
|
|
25
|
+
|
|
26
|
+
pub fn remove_section(kbid: &KbId)
|
|
27
|
+
-> Result<bool, FsError>;
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
### `add_section`
|
|
31
|
+
|
|
32
|
+
1. Compute kb-id from content via `compute_kbid`.
|
|
33
|
+
2. Create shard directory if needed (`host_mkdir`).
|
|
34
|
+
3. Serialize TOML header + `---` separator + body.
|
|
35
|
+
TOML header includes: kbid, docids, section_type,
|
|
36
|
+
title, description, size, created_at, checksum,
|
|
37
|
+
token_count, heading_token_count,
|
|
38
|
+
body_token_count, code_token_count.
|
|
39
|
+
4. Write atomically via `atomic_write`.
|
|
40
|
+
5. Update catalog via `increment_sections(1)`.
|
|
41
|
+
|
|
42
|
+
Validation: image sections (`image/*` type) require
|
|
43
|
+
a `title` in `AddSectionParams`.
|
|
44
|
+
|
|
45
|
+
### `update_section`
|
|
46
|
+
|
|
47
|
+
1. Read existing section to determine type.
|
|
48
|
+
2. Create new section file (new kb-id).
|
|
49
|
+
3. Call `update_section_ref` to update document
|
|
50
|
+
manifest references from old to new kb-id.
|
|
51
|
+
4. Mark old section file for garbage collection.
|
|
52
|
+
5. Update catalog.
|
|
53
|
+
|
|
54
|
+
### `remove_section`
|
|
55
|
+
|
|
56
|
+
1. Delete the section file.
|
|
57
|
+
2. Remove kb-id from any document manifests.
|
|
58
|
+
3. Update catalog via `increment_sections(-1)`.
|
|
59
|
+
4. Trigger index update to remove terms.
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# indexes::btree -- B+ Tree
|
|
2
|
+
|
|
3
|
+
*Generic B+ tree implementation for ordered key-value
|
|
4
|
+
storage in binary index files.*
|
|
5
|
+
|
|
6
|
+
## Source
|
|
7
|
+
|
|
8
|
+
`engine/crates/fs-database/src/index/btree.rs`
|
|
9
|
+
|
|
10
|
+
## Type Definition
|
|
11
|
+
|
|
12
|
+
```rust
|
|
13
|
+
pub struct BPlusTree<K, V> {
|
|
14
|
+
// on-disk B+ tree with configurable page size
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
impl<K: Ord + Encode, V: Encode> BPlusTree<K, V> {
|
|
18
|
+
pub fn open(data: &[u8])
|
|
19
|
+
-> Result<Self, IndexError>;
|
|
20
|
+
pub fn lookup(&self, key: &K)
|
|
21
|
+
-> Option<&V>;
|
|
22
|
+
pub fn range(&self, start: &K, end: &K)
|
|
23
|
+
-> Vec<(&K, &V)>;
|
|
24
|
+
pub fn insert(&mut self, key: K, value: V);
|
|
25
|
+
pub fn remove(&mut self, key: &K) -> bool;
|
|
26
|
+
pub fn serialize(&self) -> Vec<u8>;
|
|
27
|
+
}
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Page size is tuned for WASM memory constraints.
|
|
31
|
+
Interior nodes store keys and child pointers. Leaf
|
|
32
|
+
nodes store keys and values with sibling links for
|
|
33
|
+
range scans.
|
|
34
|
+
|
|
35
|
+
Used by both `InvertedIndex` and `PositionalIndex`.
|