brainbank 0.1.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 (49) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1059 -0
  3. package/assets/architecture.png +0 -0
  4. package/bin/brainbank +11 -0
  5. package/dist/chunk-2P3EGY6S.js +37 -0
  6. package/dist/chunk-2P3EGY6S.js.map +1 -0
  7. package/dist/chunk-3GAIDXRW.js +105 -0
  8. package/dist/chunk-3GAIDXRW.js.map +1 -0
  9. package/dist/chunk-4ZKBQ33J.js +56 -0
  10. package/dist/chunk-4ZKBQ33J.js.map +1 -0
  11. package/dist/chunk-7QVYU63E.js +7 -0
  12. package/dist/chunk-7QVYU63E.js.map +1 -0
  13. package/dist/chunk-EDKSKLX4.js +490 -0
  14. package/dist/chunk-EDKSKLX4.js.map +1 -0
  15. package/dist/chunk-GOUBW7UA.js +373 -0
  16. package/dist/chunk-GOUBW7UA.js.map +1 -0
  17. package/dist/chunk-MJ3Y24H6.js +185 -0
  18. package/dist/chunk-MJ3Y24H6.js.map +1 -0
  19. package/dist/chunk-N6ZMBFDE.js +224 -0
  20. package/dist/chunk-N6ZMBFDE.js.map +1 -0
  21. package/dist/chunk-YGSEUWLV.js +2053 -0
  22. package/dist/chunk-YGSEUWLV.js.map +1 -0
  23. package/dist/chunk-Z5SU54HP.js +171 -0
  24. package/dist/chunk-Z5SU54HP.js.map +1 -0
  25. package/dist/cli.d.ts +1 -0
  26. package/dist/cli.js +731 -0
  27. package/dist/cli.js.map +1 -0
  28. package/dist/code.d.ts +31 -0
  29. package/dist/code.js +8 -0
  30. package/dist/code.js.map +1 -0
  31. package/dist/docs.d.ts +19 -0
  32. package/dist/docs.js +8 -0
  33. package/dist/docs.js.map +1 -0
  34. package/dist/git.d.ts +31 -0
  35. package/dist/git.js +8 -0
  36. package/dist/git.js.map +1 -0
  37. package/dist/index.d.ts +845 -0
  38. package/dist/index.js +80 -0
  39. package/dist/index.js.map +1 -0
  40. package/dist/memory.d.ts +19 -0
  41. package/dist/memory.js +146 -0
  42. package/dist/memory.js.map +1 -0
  43. package/dist/notes.d.ts +19 -0
  44. package/dist/notes.js +57 -0
  45. package/dist/notes.js.map +1 -0
  46. package/dist/openai-PCTYLOWI.js +8 -0
  47. package/dist/openai-PCTYLOWI.js.map +1 -0
  48. package/dist/types-Da_zLLOl.d.ts +474 -0
  49. package/package.json +91 -0
@@ -0,0 +1,2053 @@
1
+ import {
2
+ isIgnoredDir,
3
+ isIgnoredFile,
4
+ isSupported
5
+ } from "./chunk-EDKSKLX4.js";
6
+ import {
7
+ reciprocalRankFusion
8
+ } from "./chunk-4ZKBQ33J.js";
9
+ import {
10
+ cosineSimilarity
11
+ } from "./chunk-2P3EGY6S.js";
12
+ import {
13
+ __name
14
+ } from "./chunk-7QVYU63E.js";
15
+
16
+ // src/core/config.ts
17
+ import * as path from "path";
18
+ var DEFAULTS = {
19
+ repoPath: ".",
20
+ dbPath: ".brainbank/brainbank.db",
21
+ gitDepth: 500,
22
+ maxFileSize: 512e3,
23
+ // 500KB
24
+ maxDiffBytes: 8192,
25
+ hnswM: 16,
26
+ hnswEfConstruction: 200,
27
+ hnswEfSearch: 50,
28
+ embeddingDims: 384,
29
+ maxElements: 2e6
30
+ };
31
+ function resolveConfig(partial = {}) {
32
+ const repoPath = path.resolve(partial.repoPath ?? DEFAULTS.repoPath);
33
+ const rawDbPath = partial.dbPath ?? DEFAULTS.dbPath;
34
+ const dbPath = path.isAbsolute(rawDbPath) ? rawDbPath : path.join(repoPath, rawDbPath);
35
+ return {
36
+ repoPath,
37
+ dbPath,
38
+ gitDepth: partial.gitDepth ?? DEFAULTS.gitDepth,
39
+ maxFileSize: partial.maxFileSize ?? DEFAULTS.maxFileSize,
40
+ maxDiffBytes: partial.maxDiffBytes ?? DEFAULTS.maxDiffBytes,
41
+ hnswM: partial.hnswM ?? DEFAULTS.hnswM,
42
+ hnswEfConstruction: partial.hnswEfConstruction ?? DEFAULTS.hnswEfConstruction,
43
+ hnswEfSearch: partial.hnswEfSearch ?? DEFAULTS.hnswEfSearch,
44
+ embeddingDims: partial.embeddingDims ?? DEFAULTS.embeddingDims,
45
+ maxElements: partial.maxElements ?? DEFAULTS.maxElements,
46
+ embeddingProvider: partial.embeddingProvider,
47
+ reranker: partial.reranker
48
+ };
49
+ }
50
+ __name(resolveConfig, "resolveConfig");
51
+
52
+ // src/vector/hnsw.ts
53
+ var HNSWIndex = class {
54
+ constructor(_dims, _maxElements = 2e6, _M = 16, _efConstruction = 200, _efSearch = 50) {
55
+ this._dims = _dims;
56
+ this._maxElements = _maxElements;
57
+ this._M = _M;
58
+ this._efConstruction = _efConstruction;
59
+ this._efSearch = _efSearch;
60
+ }
61
+ static {
62
+ __name(this, "HNSWIndex");
63
+ }
64
+ _index = null;
65
+ _count = 0;
66
+ /**
67
+ * Initialize the HNSW index.
68
+ * Must be called before add/search.
69
+ */
70
+ async init() {
71
+ const HNSWLib = await import("hnswlib-node");
72
+ const HNSW = HNSWLib.default?.HierarchicalNSW ?? HNSWLib.HierarchicalNSW;
73
+ this._index = new HNSW("cosine", this._dims);
74
+ this._index.initIndex(this._maxElements, this._M, this._efConstruction);
75
+ this._index.setEf(this._efSearch);
76
+ return this;
77
+ }
78
+ /**
79
+ * Add a vector with an integer ID.
80
+ * The vector should be pre-normalized for cosine distance.
81
+ */
82
+ add(vector, id) {
83
+ if (!this._index) throw new Error("HNSW index not initialized \u2014 call init() first");
84
+ this._index.addPoint(Array.from(vector), id);
85
+ this._count++;
86
+ }
87
+ /**
88
+ * Search for the k nearest neighbors.
89
+ * Returns results sorted by score (highest first).
90
+ * Score is 1 - cosine_distance (1.0 = identical).
91
+ */
92
+ search(query, k) {
93
+ if (!this._index || this._count === 0) return [];
94
+ const actualK = Math.min(k, this._count);
95
+ const result = this._index.searchKnn(Array.from(query), actualK);
96
+ return result.neighbors.map((id, i) => ({
97
+ id,
98
+ score: 1 - result.distances[i]
99
+ }));
100
+ }
101
+ /** Number of vectors in the index. */
102
+ get size() {
103
+ return this._count;
104
+ }
105
+ };
106
+
107
+ // src/embeddings/local.ts
108
+ var LocalEmbedding = class {
109
+ static {
110
+ __name(this, "LocalEmbedding");
111
+ }
112
+ dims = 384;
113
+ _pipeline = null;
114
+ _modelName;
115
+ _cacheDir;
116
+ constructor(options = {}) {
117
+ this._modelName = options.model ?? "Xenova/all-MiniLM-L6-v2";
118
+ this._cacheDir = options.cacheDir ?? ".model-cache";
119
+ }
120
+ /**
121
+ * Lazy-load the transformer pipeline.
122
+ * Singleton — created once and reused.
123
+ */
124
+ async _getPipeline() {
125
+ if (this._pipeline) return this._pipeline;
126
+ const { pipeline, env } = await import("@xenova/transformers");
127
+ env.cacheDir = this._cacheDir;
128
+ env.allowLocalModels = true;
129
+ this._pipeline = await pipeline("feature-extraction", this._modelName, {
130
+ quantized: true
131
+ });
132
+ return this._pipeline;
133
+ }
134
+ /**
135
+ * Embed a single text string.
136
+ * Returns a normalized Float32Array of length 384.
137
+ */
138
+ async embed(text) {
139
+ const pipe = await this._getPipeline();
140
+ const output = await pipe(text, { pooling: "mean", normalize: true });
141
+ return output.data;
142
+ }
143
+ /**
144
+ * Embed multiple texts.
145
+ * Processes sequentially to avoid OOM on large batches.
146
+ */
147
+ async embedBatch(texts) {
148
+ const results = [];
149
+ for (const text of texts) {
150
+ results.push(await this.embed(text));
151
+ }
152
+ return results;
153
+ }
154
+ async close() {
155
+ this._pipeline = null;
156
+ }
157
+ };
158
+
159
+ // src/vector/mmr.ts
160
+ function searchMMR(index, query, vectorCache, k, lambda = 0.7) {
161
+ const candidates = index.search(query, k * 3);
162
+ if (candidates.length <= k) return candidates;
163
+ const selected = [];
164
+ const remaining = [...candidates];
165
+ while (selected.length < k && remaining.length > 0) {
166
+ let bestScore = -Infinity;
167
+ let bestIdx = 0;
168
+ for (let i = 0; i < remaining.length; i++) {
169
+ const relevance = remaining[i].score;
170
+ let maxSim = 0;
171
+ for (const sel of selected) {
172
+ const candidateVec = vectorCache.get(remaining[i].id);
173
+ const selectedVec = vectorCache.get(sel.id);
174
+ if (candidateVec && selectedVec) {
175
+ maxSim = Math.max(maxSim, cosineSimilarity(candidateVec, selectedVec));
176
+ }
177
+ }
178
+ const mmrScore = lambda * relevance - (1 - lambda) * maxSim;
179
+ if (mmrScore > bestScore) {
180
+ bestScore = mmrScore;
181
+ bestIdx = i;
182
+ }
183
+ }
184
+ selected.push(remaining[bestIdx]);
185
+ remaining.splice(bestIdx, 1);
186
+ }
187
+ return selected;
188
+ }
189
+ __name(searchMMR, "searchMMR");
190
+
191
+ // src/query/search.ts
192
+ var UnifiedSearch = class {
193
+ static {
194
+ __name(this, "UnifiedSearch");
195
+ }
196
+ _deps;
197
+ constructor(deps) {
198
+ this._deps = deps;
199
+ }
200
+ /**
201
+ * Search across all indices.
202
+ * Returns combined results sorted by score.
203
+ */
204
+ async search(query, options = {}) {
205
+ const {
206
+ codeK = 6,
207
+ gitK = 5,
208
+ memoryK = 4,
209
+ minScore = 0.25,
210
+ useMMR = true,
211
+ mmrLambda = 0.7
212
+ } = options;
213
+ const queryVec = await this._deps.embedding.embed(query);
214
+ const results = [];
215
+ if (this._deps.codeHnsw && this._deps.codeHnsw.size > 0) {
216
+ const hits = useMMR ? searchMMR(this._deps.codeHnsw, queryVec, this._deps.codeVecs, codeK, mmrLambda) : this._deps.codeHnsw.search(queryVec, codeK);
217
+ if (hits.length > 0) {
218
+ const ids = hits.map((h) => h.id);
219
+ const scoreMap = new Map(hits.map((h) => [h.id, h.score]));
220
+ const placeholders = ids.map(() => "?").join(",");
221
+ const rows = this._deps.db.prepare(
222
+ `SELECT * FROM code_chunks WHERE id IN (${placeholders})`
223
+ ).all(...ids);
224
+ for (const r of rows) {
225
+ const score = scoreMap.get(r.id) ?? 0;
226
+ if (score >= minScore) {
227
+ results.push({
228
+ type: "code",
229
+ score,
230
+ filePath: r.file_path,
231
+ content: r.content,
232
+ metadata: {
233
+ chunkType: r.chunk_type,
234
+ name: r.name,
235
+ startLine: r.start_line,
236
+ endLine: r.end_line,
237
+ language: r.language
238
+ }
239
+ });
240
+ }
241
+ }
242
+ }
243
+ }
244
+ if (this._deps.gitHnsw && this._deps.gitHnsw.size > 0) {
245
+ const hits = this._deps.gitHnsw.search(queryVec, gitK * 2);
246
+ if (hits.length > 0) {
247
+ const ids = hits.map((h) => h.id);
248
+ const scoreMap = new Map(hits.map((h) => [h.id, h.score]));
249
+ const placeholders = ids.map(() => "?").join(",");
250
+ const rows = this._deps.db.prepare(
251
+ `SELECT * FROM git_commits WHERE id IN (${placeholders}) AND is_merge = 0`
252
+ ).all(...ids);
253
+ for (const r of rows) {
254
+ const score = scoreMap.get(r.id) ?? 0;
255
+ if (score >= minScore) {
256
+ results.push({
257
+ type: "commit",
258
+ score,
259
+ content: r.message,
260
+ metadata: {
261
+ hash: r.hash,
262
+ shortHash: r.short_hash,
263
+ author: r.author,
264
+ date: r.date,
265
+ files: JSON.parse(r.files_json ?? "[]"),
266
+ additions: r.additions,
267
+ deletions: r.deletions,
268
+ diff: r.diff
269
+ }
270
+ });
271
+ }
272
+ }
273
+ }
274
+ }
275
+ if (this._deps.memHnsw && this._deps.memHnsw.size > 0) {
276
+ const hits = useMMR ? searchMMR(this._deps.memHnsw, queryVec, this._deps.memVecs, memoryK, mmrLambda) : this._deps.memHnsw.search(queryVec, memoryK);
277
+ if (hits.length > 0) {
278
+ const ids = hits.map((h) => h.id);
279
+ const scoreMap = new Map(hits.map((h) => [h.id, h.score]));
280
+ const placeholders = ids.map(() => "?").join(",");
281
+ const rows = this._deps.db.prepare(
282
+ `SELECT * FROM memory_patterns WHERE id IN (${placeholders}) AND success_rate >= 0.5`
283
+ ).all(...ids);
284
+ for (const r of rows) {
285
+ const score = scoreMap.get(r.id) ?? 0;
286
+ if (score >= minScore) {
287
+ results.push({
288
+ type: "pattern",
289
+ score,
290
+ content: r.approach,
291
+ metadata: {
292
+ taskType: r.task_type,
293
+ task: r.task,
294
+ outcome: r.outcome,
295
+ successRate: r.success_rate,
296
+ critique: r.critique
297
+ }
298
+ });
299
+ }
300
+ }
301
+ }
302
+ }
303
+ results.sort((a, b) => b.score - a.score);
304
+ if (this._deps.reranker && results.length > 1) {
305
+ return this._rerank(query, results);
306
+ }
307
+ return results;
308
+ }
309
+ /**
310
+ * Re-rank results using position-aware blending.
311
+ *
312
+ * Top 1-3: 75% retrieval / 25% reranker (preserves exact matches)
313
+ * Top 4-10: 60% retrieval / 40% reranker
314
+ * Top 11+: 40% retrieval / 60% reranker (trust reranker more)
315
+ */
316
+ async _rerank(query, results) {
317
+ const reranker = this._deps.reranker;
318
+ const documents = results.map((r) => r.content);
319
+ const scores = await reranker.rank(query, documents);
320
+ const blended = results.map((r, i) => {
321
+ const pos = i + 1;
322
+ const rrfWeight = pos <= 3 ? 0.75 : pos <= 10 ? 0.6 : 0.4;
323
+ return {
324
+ ...r,
325
+ score: rrfWeight * r.score + (1 - rrfWeight) * (scores[i] ?? 0)
326
+ };
327
+ });
328
+ return blended.sort((a, b) => b.score - a.score);
329
+ }
330
+ };
331
+
332
+ // src/query/fts-utils.ts
333
+ function sanitizeFTS(query) {
334
+ const clean = query.replace(/[{}[\]()^~*:]/g, " ").replace(/\bAND\b|\bOR\b|\bNOT\b|\bNEAR\b/gi, "").trim();
335
+ const words = clean.split(/\s+/).filter((w) => w.length > 1);
336
+ if (words.length === 0) return "";
337
+ return words.map((w) => `"${w}"`).join(" ");
338
+ }
339
+ __name(sanitizeFTS, "sanitizeFTS");
340
+ function normalizeBM25(rawScore) {
341
+ const abs = Math.abs(rawScore);
342
+ return 1 / (1 + Math.exp(-0.3 * (abs - 5)));
343
+ }
344
+ __name(normalizeBM25, "normalizeBM25");
345
+
346
+ // src/query/bm25.ts
347
+ var BM25Search = class {
348
+ constructor(_db) {
349
+ this._db = _db;
350
+ }
351
+ static {
352
+ __name(this, "BM25Search");
353
+ }
354
+ /**
355
+ * Full-text keyword search across all FTS5 indices.
356
+ * Uses BM25 scoring — lower scores = better matches.
357
+ * Query syntax: simple words, OR, NOT, "exact phrases", prefix*
358
+ */
359
+ search(query, options = {}) {
360
+ const { codeK = 8, gitK = 5, memoryK = 4 } = options;
361
+ const results = [];
362
+ const ftsQuery = sanitizeFTS(query);
363
+ if (!ftsQuery) return [];
364
+ if (codeK > 0) {
365
+ try {
366
+ const rows = this._db.prepare(`
367
+ SELECT c.id, c.file_path, c.chunk_type, c.name, c.start_line, c.end_line,
368
+ c.content, c.language, bm25(fts_code, 5.0, 3.0, 1.0) AS score
369
+ FROM fts_code f
370
+ JOIN code_chunks c ON c.id = f.rowid
371
+ WHERE fts_code MATCH ?
372
+ ORDER BY score ASC
373
+ LIMIT ?
374
+ `).all(ftsQuery, codeK);
375
+ for (const r of rows) {
376
+ results.push({
377
+ type: "code",
378
+ score: normalizeBM25(r.score),
379
+ filePath: r.file_path,
380
+ content: r.content,
381
+ metadata: {
382
+ chunkType: r.chunk_type,
383
+ name: r.name,
384
+ startLine: r.start_line,
385
+ endLine: r.end_line,
386
+ language: r.language,
387
+ searchType: "bm25"
388
+ }
389
+ });
390
+ }
391
+ } catch {
392
+ }
393
+ }
394
+ if (gitK > 0) {
395
+ try {
396
+ const rows = this._db.prepare(`
397
+ SELECT c.id, c.hash, c.short_hash, c.message, c.author, c.date,
398
+ c.files_json, c.diff, c.additions, c.deletions,
399
+ bm25(fts_commits, 5.0, 2.0, 1.0) AS score
400
+ FROM fts_commits f
401
+ JOIN git_commits c ON c.id = f.rowid
402
+ WHERE fts_commits MATCH ? AND c.is_merge = 0
403
+ ORDER BY score ASC
404
+ LIMIT ?
405
+ `).all(ftsQuery, gitK);
406
+ for (const r of rows) {
407
+ results.push({
408
+ type: "commit",
409
+ score: normalizeBM25(r.score),
410
+ content: r.message,
411
+ metadata: {
412
+ hash: r.hash,
413
+ shortHash: r.short_hash,
414
+ author: r.author,
415
+ date: r.date,
416
+ files: JSON.parse(r.files_json ?? "[]"),
417
+ additions: r.additions,
418
+ deletions: r.deletions,
419
+ diff: r.diff,
420
+ searchType: "bm25"
421
+ }
422
+ });
423
+ }
424
+ } catch {
425
+ }
426
+ }
427
+ if (memoryK > 0) {
428
+ try {
429
+ const rows = this._db.prepare(`
430
+ SELECT p.id, p.task_type, p.task, p.approach, p.outcome,
431
+ p.success_rate, p.critique,
432
+ bm25(fts_patterns, 3.0, 5.0, 5.0, 1.0) AS score
433
+ FROM fts_patterns f
434
+ JOIN memory_patterns p ON p.id = f.rowid
435
+ WHERE fts_patterns MATCH ? AND p.success_rate >= 0.5
436
+ ORDER BY score ASC
437
+ LIMIT ?
438
+ `).all(ftsQuery, memoryK);
439
+ for (const r of rows) {
440
+ results.push({
441
+ type: "pattern",
442
+ score: normalizeBM25(r.score),
443
+ content: r.approach,
444
+ metadata: {
445
+ taskType: r.task_type,
446
+ task: r.task,
447
+ outcome: r.outcome,
448
+ successRate: r.success_rate,
449
+ critique: r.critique,
450
+ searchType: "bm25"
451
+ }
452
+ });
453
+ }
454
+ } catch {
455
+ }
456
+ }
457
+ return results.sort((a, b) => b.score - a.score);
458
+ }
459
+ /**
460
+ * Rebuild the FTS index from scratch.
461
+ * Call this after bulk imports or if FTS gets out of sync.
462
+ */
463
+ rebuild() {
464
+ try {
465
+ this._db.prepare("INSERT INTO fts_code(fts_code) VALUES('rebuild')").run();
466
+ this._db.prepare("INSERT INTO fts_commits(fts_commits) VALUES('rebuild')").run();
467
+ this._db.prepare("INSERT INTO fts_patterns(fts_patterns) VALUES('rebuild')").run();
468
+ } catch {
469
+ }
470
+ }
471
+ };
472
+
473
+ // src/query/context-builder.ts
474
+ var ContextBuilder = class {
475
+ constructor(_search, _coEdits) {
476
+ this._search = _search;
477
+ this._coEdits = _coEdits;
478
+ }
479
+ static {
480
+ __name(this, "ContextBuilder");
481
+ }
482
+ /**
483
+ * Build a full context block for a task.
484
+ * Returns clean markdown ready for system prompt injection.
485
+ */
486
+ async build(task, options = {}) {
487
+ const {
488
+ codeResults = 6,
489
+ gitResults = 5,
490
+ memoryResults = 4,
491
+ affectedFiles = [],
492
+ minScore = 0.25,
493
+ useMMR = true,
494
+ mmrLambda = 0.7
495
+ } = options;
496
+ const results = await this._search.search(task, {
497
+ codeK: codeResults,
498
+ gitK: gitResults,
499
+ memoryK: memoryResults,
500
+ minScore,
501
+ useMMR,
502
+ mmrLambda
503
+ });
504
+ const parts = [`# Context for: "${task}"
505
+ `];
506
+ const codeHits = results.filter((r) => r.type === "code").slice(0, codeResults);
507
+ if (codeHits.length > 0) {
508
+ parts.push("## Relevant Code\n");
509
+ const byFile = /* @__PURE__ */ new Map();
510
+ for (const r of codeHits) {
511
+ const key = r.filePath ?? "unknown";
512
+ if (!byFile.has(key)) byFile.set(key, []);
513
+ byFile.get(key).push(r);
514
+ }
515
+ for (const [file, chunks] of byFile) {
516
+ parts.push(`### ${file}`);
517
+ for (const c of chunks) {
518
+ const m = c.metadata;
519
+ const label = m.name ? `${m.chunkType} \`${m.name}\` (L${m.startLine}-${m.endLine})` : `L${m.startLine}-${m.endLine}`;
520
+ parts.push(`**${label}** \u2014 ${Math.round(c.score * 100)}% match`);
521
+ parts.push("```" + (m.language || ""));
522
+ parts.push(c.content);
523
+ parts.push("```\n");
524
+ }
525
+ }
526
+ }
527
+ const gitHits = results.filter((r) => r.type === "commit").slice(0, gitResults);
528
+ if (gitHits.length > 0) {
529
+ parts.push("## Related Git History\n");
530
+ for (const c of gitHits) {
531
+ const m = c.metadata;
532
+ const score = Math.round(c.score * 100);
533
+ const files = (m.files ?? []).slice(0, 4).join(", ");
534
+ parts.push(`**[${m.shortHash}]** ${c.content} *(${m.author}, ${m.date?.slice(0, 10)}, ${score}%)*`);
535
+ if (files) parts.push(` Files: ${files}`);
536
+ if (m.diff) {
537
+ const snippet = m.diff.split("\n").filter((l) => l.startsWith("+") || l.startsWith("-") || l.startsWith("@@")).slice(0, 10).join("\n");
538
+ if (snippet) {
539
+ parts.push("```diff");
540
+ parts.push(snippet);
541
+ parts.push("```");
542
+ }
543
+ }
544
+ parts.push("");
545
+ }
546
+ }
547
+ if (affectedFiles.length > 0) {
548
+ const coEditLines = [];
549
+ for (const file of affectedFiles.slice(0, 3)) {
550
+ const suggestions = this._coEdits.suggest(file, 4);
551
+ if (suggestions.length > 0) {
552
+ coEditLines.push(
553
+ `- **${file}** \u2192 also tends to change: ${suggestions.map((s) => `${s.file} (${s.count}x)`).join(", ")}`
554
+ );
555
+ }
556
+ }
557
+ if (coEditLines.length > 0) {
558
+ parts.push("## Co-Edit Patterns\n");
559
+ parts.push(...coEditLines);
560
+ parts.push("");
561
+ }
562
+ }
563
+ const memHits = results.filter((r) => r.type === "pattern").slice(0, memoryResults);
564
+ if (memHits.length > 0) {
565
+ parts.push("## Learned Patterns\n");
566
+ for (const p of memHits) {
567
+ const m = p.metadata;
568
+ const score = Math.round(p.score * 100);
569
+ const success = Math.round((m.successRate ?? 0) * 100);
570
+ parts.push(`**${m.taskType}** \u2014 ${success}% success, ${score}% match`);
571
+ parts.push(`Task: ${m.task}`);
572
+ parts.push(`Approach: ${p.content}`);
573
+ if (m.critique) parts.push(`Lesson: ${m.critique}`);
574
+ parts.push("");
575
+ }
576
+ }
577
+ return parts.join("\n");
578
+ }
579
+ };
580
+
581
+ // src/core/collection.ts
582
+ var Collection = class {
583
+ constructor(_name, _db, _embedding, _hnsw, _vecs, _reranker) {
584
+ this._name = _name;
585
+ this._db = _db;
586
+ this._embedding = _embedding;
587
+ this._hnsw = _hnsw;
588
+ this._vecs = _vecs;
589
+ this._reranker = _reranker;
590
+ }
591
+ static {
592
+ __name(this, "Collection");
593
+ }
594
+ /** Collection name. */
595
+ get name() {
596
+ return this._name;
597
+ }
598
+ /** Add an item. Returns its ID. */
599
+ async add(content, options = {}) {
600
+ const opts = "tags" in options || "ttl" in options || "metadata" in options ? options : { metadata: options };
601
+ const metadata = opts.metadata ?? {};
602
+ const tags = opts.tags ?? [];
603
+ const expiresAt = opts.ttl ? Math.floor(Date.now() / 1e3) + parseDuration(opts.ttl) : null;
604
+ const result = this._db.prepare(
605
+ "INSERT INTO kv_data (collection, content, meta_json, tags_json, expires_at) VALUES (?, ?, ?, ?, ?)"
606
+ ).run(this._name, content, JSON.stringify(metadata), JSON.stringify(tags), expiresAt);
607
+ const id = Number(result.lastInsertRowid);
608
+ const vec = await this._embedding.embed(content);
609
+ this._db.prepare(
610
+ "INSERT INTO kv_vectors (data_id, embedding) VALUES (?, ?)"
611
+ ).run(id, Buffer.from(vec.buffer));
612
+ this._hnsw.add(vec, id);
613
+ this._vecs.set(id, vec);
614
+ return id;
615
+ }
616
+ /** Add multiple items. Returns their IDs. */
617
+ async addMany(items) {
618
+ const ids = [];
619
+ for (const item of items) {
620
+ ids.push(await this.add(item.content, {
621
+ metadata: item.metadata,
622
+ tags: item.tags,
623
+ ttl: item.ttl
624
+ }));
625
+ }
626
+ return ids;
627
+ }
628
+ /** Search this collection. */
629
+ async search(query, options = {}) {
630
+ const { k = 5, mode = "hybrid", minScore = 0.15, tags } = options;
631
+ this._pruneExpired();
632
+ if (mode === "keyword") return this._filterByTags(this._searchBM25(query, k, minScore), tags);
633
+ if (mode === "vector") return this._filterByTags(await this._searchVector(query, k, minScore), tags);
634
+ const [vectorHits, bm25Hits] = await Promise.all([
635
+ this._searchVector(query, k, 0),
636
+ Promise.resolve(this._searchBM25(query, k, 0))
637
+ ]);
638
+ const fused = reciprocalRankFusion([
639
+ vectorHits.map((h) => ({ type: "document", score: h.score ?? 0, content: h.content, metadata: { id: h.id } })),
640
+ bm25Hits.map((h) => ({ type: "document", score: h.score ?? 0, content: h.content, metadata: { id: h.id } }))
641
+ ]);
642
+ const allById = /* @__PURE__ */ new Map();
643
+ for (const h of [...vectorHits, ...bm25Hits]) allById.set(h.id, h);
644
+ const results = [];
645
+ for (const r of fused) {
646
+ const item = allById.get(r.metadata.id);
647
+ if (!item) continue;
648
+ const scored = { ...item, score: r.score };
649
+ if (scored.score >= minScore) results.push(scored);
650
+ if (results.length >= k) break;
651
+ }
652
+ if (this._reranker && results.length > 1) {
653
+ const documents = results.map((r) => r.content);
654
+ const scores = await this._reranker.rank(query, documents);
655
+ const blended = results.map((r, i) => ({
656
+ ...r,
657
+ score: 0.6 * (r.score ?? 0) + 0.4 * (scores[i] ?? 0)
658
+ }));
659
+ return this._filterByTags(
660
+ blended.sort((a, b) => (b.score ?? 0) - (a.score ?? 0)),
661
+ tags
662
+ );
663
+ }
664
+ return this._filterByTags(results, tags);
665
+ }
666
+ /** List items (newest first). */
667
+ list(options = {}) {
668
+ const { limit = 20, offset = 0, tags } = options;
669
+ this._pruneExpired();
670
+ const rows = this._db.prepare(
671
+ "SELECT * FROM kv_data WHERE collection = ? AND (expires_at IS NULL OR expires_at > ?) ORDER BY created_at DESC, id DESC LIMIT ? OFFSET ?"
672
+ ).all(this._name, Math.floor(Date.now() / 1e3), limit, offset);
673
+ return this._filterByTags(rows.map((r) => this._rowToItem(r)), tags);
674
+ }
675
+ /** Count items in this collection. */
676
+ count() {
677
+ return this._db.prepare(
678
+ "SELECT COUNT(*) as c FROM kv_data WHERE collection = ?"
679
+ ).get(this._name).c;
680
+ }
681
+ /** Keep only the N most recent items, remove the rest. */
682
+ async trim(options) {
683
+ const before = this.count();
684
+ if (before <= options.keep) return { removed: 0 };
685
+ const toRemove = this._db.prepare(`
686
+ SELECT id FROM kv_data
687
+ WHERE collection = ?
688
+ ORDER BY created_at DESC, id DESC
689
+ LIMIT -1 OFFSET ?
690
+ `).all(this._name, options.keep);
691
+ for (const row of toRemove) {
692
+ this._removeById(row.id);
693
+ }
694
+ return { removed: toRemove.length };
695
+ }
696
+ /** Remove items older than a duration string (e.g. '30d', '12h'). */
697
+ async prune(options) {
698
+ const seconds = parseDuration(options.olderThan);
699
+ const cutoff = Math.floor(Date.now() / 1e3) - seconds;
700
+ const toRemove = this._db.prepare(
701
+ "SELECT id FROM kv_data WHERE collection = ? AND created_at < ?"
702
+ ).all(this._name, cutoff);
703
+ for (const row of toRemove) {
704
+ this._removeById(row.id);
705
+ }
706
+ return { removed: toRemove.length };
707
+ }
708
+ /** Remove a specific item by ID. */
709
+ remove(id) {
710
+ this._removeById(id);
711
+ }
712
+ /** Clear all items in this collection. */
713
+ clear() {
714
+ const rows = this._db.prepare(
715
+ "SELECT id FROM kv_data WHERE collection = ?"
716
+ ).all(this._name);
717
+ for (const row of rows) {
718
+ this._removeById(row.id);
719
+ }
720
+ }
721
+ // ── Private ──────────────────────────────────────
722
+ _removeById(id) {
723
+ this._vecs.delete(id);
724
+ this._db.prepare("DELETE FROM kv_data WHERE id = ?").run(id);
725
+ }
726
+ async _searchVector(query, k, minScore) {
727
+ if (this._hnsw.size === 0) return [];
728
+ const queryVec = await this._embedding.embed(query);
729
+ const hits = this._hnsw.search(queryVec, k * 3);
730
+ const ids = hits.map((h) => h.id);
731
+ if (ids.length === 0) return [];
732
+ const scoreMap = new Map(hits.map((h) => [h.id, h.score]));
733
+ const placeholders = ids.map(() => "?").join(",");
734
+ const rows = this._db.prepare(
735
+ `SELECT * FROM kv_data WHERE id IN (${placeholders}) AND collection = ?`
736
+ ).all(...ids, this._name);
737
+ return rows.map((r) => ({ ...this._rowToItem(r), score: scoreMap.get(r.id) ?? 0 })).filter((r) => r.score >= minScore).sort((a, b) => (b.score ?? 0) - (a.score ?? 0)).slice(0, k);
738
+ }
739
+ _searchBM25(query, k, minScore) {
740
+ const ftsQuery = sanitizeFTS(query);
741
+ if (!ftsQuery) return [];
742
+ try {
743
+ const rows = this._db.prepare(`
744
+ SELECT d.*, bm25(fts_kv, 5.0, 1.0) AS score
745
+ FROM fts_kv f
746
+ JOIN kv_data d ON d.id = f.rowid
747
+ WHERE fts_kv MATCH ? AND d.collection = ?
748
+ ORDER BY score ASC
749
+ LIMIT ?
750
+ `).all(ftsQuery, this._name, k);
751
+ return rows.map((r) => ({
752
+ ...this._rowToItem(r),
753
+ score: normalizeBM25(r.score)
754
+ })).filter((r) => (r.score ?? 0) >= minScore);
755
+ } catch {
756
+ return [];
757
+ }
758
+ }
759
+ _rowToItem(r) {
760
+ return {
761
+ id: r.id,
762
+ collection: r.collection,
763
+ content: r.content,
764
+ metadata: JSON.parse(r.meta_json || "{}"),
765
+ tags: JSON.parse(r.tags_json || "[]"),
766
+ createdAt: r.created_at,
767
+ expiresAt: r.expires_at ?? void 0
768
+ };
769
+ }
770
+ /** Filter results by tags (item must have ALL specified tags). */
771
+ _filterByTags(items, tags) {
772
+ if (!tags || tags.length === 0) return items;
773
+ return items.filter(
774
+ (item) => tags.every((t) => item.tags.includes(t))
775
+ );
776
+ }
777
+ /** Remove expired items (TTL). Called automatically on search/list. */
778
+ _pruneExpired() {
779
+ const now = Math.floor(Date.now() / 1e3);
780
+ const expired = this._db.prepare(
781
+ "SELECT id FROM kv_data WHERE collection = ? AND expires_at IS NOT NULL AND expires_at <= ?"
782
+ ).all(this._name, now);
783
+ for (const row of expired) {
784
+ this._removeById(row.id);
785
+ }
786
+ }
787
+ };
788
+ function parseDuration(s) {
789
+ const match = s.match(/^(\d+)([dhms])$/);
790
+ if (!match) throw new Error(`Invalid duration: "${s}". Use format like '30d', '12h', '5m'.`);
791
+ const n = parseInt(match[1], 10);
792
+ switch (match[2]) {
793
+ case "d":
794
+ return n * 86400;
795
+ case "h":
796
+ return n * 3600;
797
+ case "m":
798
+ return n * 60;
799
+ case "s":
800
+ return n;
801
+ default:
802
+ return n;
803
+ }
804
+ }
805
+ __name(parseDuration, "parseDuration");
806
+
807
+ // src/core/brainbank.ts
808
+ import { EventEmitter } from "events";
809
+
810
+ // src/storage/database.ts
811
+ import BetterSqlite3 from "better-sqlite3";
812
+ import * as fs from "fs";
813
+ import * as path2 from "path";
814
+
815
+ // src/core/schema.ts
816
+ var SCHEMA_VERSION = 4;
817
+ function createSchema(db) {
818
+ db.exec(`
819
+ -- \u2500\u2500 Schema versioning \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
820
+ CREATE TABLE IF NOT EXISTS schema_version (
821
+ version INTEGER PRIMARY KEY,
822
+ applied_at INTEGER NOT NULL DEFAULT (unixepoch())
823
+ );
824
+ INSERT OR IGNORE INTO schema_version (version) VALUES (${SCHEMA_VERSION});
825
+
826
+ -- \u2500\u2500 Code chunks \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
827
+ CREATE TABLE IF NOT EXISTS code_chunks (
828
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
829
+ file_path TEXT NOT NULL,
830
+ chunk_type TEXT NOT NULL,
831
+ name TEXT,
832
+ start_line INTEGER NOT NULL,
833
+ end_line INTEGER NOT NULL,
834
+ content TEXT NOT NULL,
835
+ language TEXT NOT NULL,
836
+ file_hash TEXT,
837
+ indexed_at INTEGER NOT NULL DEFAULT (unixepoch())
838
+ );
839
+
840
+ CREATE TABLE IF NOT EXISTS code_vectors (
841
+ chunk_id INTEGER PRIMARY KEY REFERENCES code_chunks(id) ON DELETE CASCADE,
842
+ embedding BLOB NOT NULL
843
+ );
844
+
845
+ CREATE TABLE IF NOT EXISTS indexed_files (
846
+ file_path TEXT PRIMARY KEY,
847
+ file_hash TEXT NOT NULL,
848
+ indexed_at INTEGER NOT NULL DEFAULT (unixepoch())
849
+ );
850
+
851
+ -- \u2500\u2500 Git history \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
852
+ CREATE TABLE IF NOT EXISTS git_commits (
853
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
854
+ hash TEXT UNIQUE NOT NULL,
855
+ short_hash TEXT NOT NULL,
856
+ message TEXT NOT NULL,
857
+ author TEXT NOT NULL,
858
+ date TEXT NOT NULL,
859
+ timestamp INTEGER NOT NULL,
860
+ files_json TEXT NOT NULL,
861
+ diff TEXT,
862
+ additions INTEGER DEFAULT 0,
863
+ deletions INTEGER DEFAULT 0,
864
+ is_merge INTEGER DEFAULT 0
865
+ );
866
+
867
+ CREATE TABLE IF NOT EXISTS commit_files (
868
+ commit_id INTEGER NOT NULL REFERENCES git_commits(id),
869
+ file_path TEXT NOT NULL
870
+ );
871
+
872
+ CREATE TABLE IF NOT EXISTS co_edits (
873
+ file_a TEXT NOT NULL,
874
+ file_b TEXT NOT NULL,
875
+ count INTEGER NOT NULL DEFAULT 1,
876
+ PRIMARY KEY (file_a, file_b)
877
+ );
878
+
879
+ CREATE TABLE IF NOT EXISTS git_vectors (
880
+ commit_id INTEGER PRIMARY KEY REFERENCES git_commits(id) ON DELETE CASCADE,
881
+ embedding BLOB NOT NULL
882
+ );
883
+
884
+ -- \u2500\u2500 Agent memory \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
885
+ CREATE TABLE IF NOT EXISTS memory_patterns (
886
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
887
+ task_type TEXT NOT NULL,
888
+ task TEXT NOT NULL,
889
+ approach TEXT NOT NULL,
890
+ outcome TEXT,
891
+ success_rate REAL NOT NULL DEFAULT 0.5,
892
+ critique TEXT,
893
+ tokens_used INTEGER,
894
+ latency_ms INTEGER,
895
+ created_at INTEGER NOT NULL DEFAULT (unixepoch())
896
+ );
897
+
898
+ CREATE TABLE IF NOT EXISTS memory_vectors (
899
+ pattern_id INTEGER PRIMARY KEY REFERENCES memory_patterns(id) ON DELETE CASCADE,
900
+ embedding BLOB NOT NULL
901
+ );
902
+
903
+ CREATE TABLE IF NOT EXISTS distilled_strategies (
904
+ task_type TEXT PRIMARY KEY,
905
+ strategy TEXT NOT NULL,
906
+ confidence REAL NOT NULL DEFAULT 0.8,
907
+ updated_at INTEGER NOT NULL DEFAULT (unixepoch())
908
+ );
909
+
910
+ -- \u2500\u2500 Indices \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
911
+ CREATE INDEX IF NOT EXISTS idx_cc_file ON code_chunks(file_path);
912
+ CREATE INDEX IF NOT EXISTS idx_cf_path ON commit_files(file_path);
913
+ CREATE INDEX IF NOT EXISTS idx_gc_ts ON git_commits(timestamp DESC);
914
+ CREATE INDEX IF NOT EXISTS idx_gc_hash ON git_commits(hash);
915
+ CREATE INDEX IF NOT EXISTS idx_mp_type ON memory_patterns(task_type);
916
+ CREATE INDEX IF NOT EXISTS idx_mp_success ON memory_patterns(success_rate);
917
+ CREATE INDEX IF NOT EXISTS idx_mp_created ON memory_patterns(created_at);
918
+
919
+ -- \u2500\u2500 FTS5 Full-Text Search \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
920
+ -- Code chunks: search by file path, name, and content
921
+ CREATE VIRTUAL TABLE IF NOT EXISTS fts_code USING fts5(
922
+ file_path,
923
+ name,
924
+ content,
925
+ content='code_chunks',
926
+ content_rowid='id',
927
+ tokenize='porter unicode61'
928
+ );
929
+
930
+ -- Git commits: search by message, author, and diff
931
+ CREATE VIRTUAL TABLE IF NOT EXISTS fts_commits USING fts5(
932
+ message,
933
+ author,
934
+ diff,
935
+ content='git_commits',
936
+ content_rowid='id',
937
+ tokenize='porter unicode61'
938
+ );
939
+
940
+ -- Memory patterns: search by task type, task, approach, and critique
941
+ CREATE VIRTUAL TABLE IF NOT EXISTS fts_patterns USING fts5(
942
+ task_type,
943
+ task,
944
+ approach,
945
+ critique,
946
+ content='memory_patterns',
947
+ content_rowid='id',
948
+ tokenize='porter unicode61'
949
+ );
950
+
951
+ -- \u2500\u2500 FTS5 Sync Triggers \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
952
+ -- Auto-sync FTS indices on INSERT/UPDATE/DELETE
953
+
954
+ CREATE TRIGGER IF NOT EXISTS trg_fts_code_insert AFTER INSERT ON code_chunks BEGIN
955
+ INSERT INTO fts_code(rowid, file_path, name, content)
956
+ VALUES (new.id, new.file_path, COALESCE(new.name, ''), new.content);
957
+ END;
958
+ CREATE TRIGGER IF NOT EXISTS trg_fts_code_delete AFTER DELETE ON code_chunks BEGIN
959
+ INSERT INTO fts_code(fts_code, rowid, file_path, name, content)
960
+ VALUES ('delete', old.id, old.file_path, COALESCE(old.name, ''), old.content);
961
+ END;
962
+
963
+ CREATE TRIGGER IF NOT EXISTS trg_fts_commits_insert AFTER INSERT ON git_commits BEGIN
964
+ INSERT INTO fts_commits(rowid, message, author, diff)
965
+ VALUES (new.id, new.message, new.author, COALESCE(new.diff, ''));
966
+ END;
967
+ CREATE TRIGGER IF NOT EXISTS trg_fts_commits_delete AFTER DELETE ON git_commits BEGIN
968
+ INSERT INTO fts_commits(fts_commits, rowid, message, author, diff)
969
+ VALUES ('delete', old.id, old.message, old.author, COALESCE(old.diff, ''));
970
+ END;
971
+
972
+ CREATE TRIGGER IF NOT EXISTS trg_fts_patterns_insert AFTER INSERT ON memory_patterns BEGIN
973
+ INSERT INTO fts_patterns(rowid, task_type, task, approach, critique)
974
+ VALUES (new.id, new.task_type, new.task, new.approach, COALESCE(new.critique, ''));
975
+ END;
976
+ CREATE TRIGGER IF NOT EXISTS trg_fts_patterns_delete AFTER DELETE ON memory_patterns BEGIN
977
+ INSERT INTO fts_patterns(fts_patterns, rowid, task_type, task, approach, critique)
978
+ VALUES ('delete', old.id, old.task_type, old.task, old.approach, COALESCE(old.critique, ''));
979
+ END;
980
+
981
+ -- \u2500\u2500 Note Memory \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
982
+ CREATE TABLE IF NOT EXISTS note_memories (
983
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
984
+ title TEXT NOT NULL,
985
+ summary TEXT NOT NULL,
986
+ decisions_json TEXT NOT NULL DEFAULT '[]',
987
+ files_json TEXT NOT NULL DEFAULT '[]',
988
+ patterns_json TEXT NOT NULL DEFAULT '[]',
989
+ open_json TEXT NOT NULL DEFAULT '[]',
990
+ tags_json TEXT NOT NULL DEFAULT '[]',
991
+ tier TEXT NOT NULL DEFAULT 'short',
992
+ created_at INTEGER NOT NULL DEFAULT (unixepoch())
993
+ );
994
+
995
+ CREATE TABLE IF NOT EXISTS note_vectors (
996
+ note_id INTEGER PRIMARY KEY REFERENCES note_memories(id) ON DELETE CASCADE,
997
+ embedding BLOB NOT NULL
998
+ );
999
+
1000
+ CREATE VIRTUAL TABLE IF NOT EXISTS fts_notes USING fts5(
1001
+ title,
1002
+ summary,
1003
+ decisions,
1004
+ patterns,
1005
+ tags,
1006
+ content='note_memories',
1007
+ content_rowid='id',
1008
+ tokenize='porter unicode61'
1009
+ );
1010
+
1011
+ CREATE TRIGGER IF NOT EXISTS trg_fts_notes_insert AFTER INSERT ON note_memories BEGIN
1012
+ INSERT INTO fts_notes(rowid, title, summary, decisions, patterns, tags)
1013
+ VALUES (new.id, new.title, new.summary, new.decisions_json, new.patterns_json, new.tags_json);
1014
+ END;
1015
+ CREATE TRIGGER IF NOT EXISTS trg_fts_notes_delete AFTER DELETE ON note_memories BEGIN
1016
+ INSERT INTO fts_notes(fts_notes, rowid, title, summary, decisions, patterns, tags)
1017
+ VALUES ('delete', old.id, old.title, old.summary, old.decisions_json, old.patterns_json, old.tags_json);
1018
+ END;
1019
+
1020
+ CREATE INDEX IF NOT EXISTS idx_nm_tier ON note_memories(tier);
1021
+ CREATE INDEX IF NOT EXISTS idx_nm_created ON note_memories(created_at DESC);
1022
+
1023
+ -- \u2500\u2500 Document Collections \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
1024
+ CREATE TABLE IF NOT EXISTS collections (
1025
+ name TEXT PRIMARY KEY,
1026
+ path TEXT NOT NULL,
1027
+ pattern TEXT NOT NULL DEFAULT '**/*.md',
1028
+ ignore_json TEXT NOT NULL DEFAULT '[]',
1029
+ context TEXT,
1030
+ created_at INTEGER NOT NULL DEFAULT (unixepoch())
1031
+ );
1032
+
1033
+ CREATE TABLE IF NOT EXISTS doc_chunks (
1034
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
1035
+ collection TEXT NOT NULL REFERENCES collections(name) ON DELETE CASCADE,
1036
+ file_path TEXT NOT NULL,
1037
+ title TEXT NOT NULL,
1038
+ content TEXT NOT NULL,
1039
+ seq INTEGER NOT NULL DEFAULT 0,
1040
+ pos INTEGER NOT NULL DEFAULT 0,
1041
+ content_hash TEXT NOT NULL,
1042
+ indexed_at INTEGER NOT NULL DEFAULT (unixepoch())
1043
+ );
1044
+
1045
+ CREATE TABLE IF NOT EXISTS doc_vectors (
1046
+ chunk_id INTEGER PRIMARY KEY REFERENCES doc_chunks(id) ON DELETE CASCADE,
1047
+ embedding BLOB NOT NULL
1048
+ );
1049
+
1050
+ CREATE INDEX IF NOT EXISTS idx_dc_collection ON doc_chunks(collection);
1051
+ CREATE INDEX IF NOT EXISTS idx_dc_file ON doc_chunks(file_path);
1052
+ CREATE INDEX IF NOT EXISTS idx_dc_hash ON doc_chunks(content_hash);
1053
+
1054
+ CREATE VIRTUAL TABLE IF NOT EXISTS fts_docs USING fts5(
1055
+ title,
1056
+ content,
1057
+ file_path,
1058
+ collection,
1059
+ content='doc_chunks',
1060
+ content_rowid='id',
1061
+ tokenize='porter unicode61'
1062
+ );
1063
+
1064
+ CREATE TRIGGER IF NOT EXISTS trg_fts_docs_insert AFTER INSERT ON doc_chunks BEGIN
1065
+ INSERT INTO fts_docs(rowid, title, content, file_path, collection)
1066
+ VALUES (new.id, new.title, new.content, new.file_path, new.collection);
1067
+ END;
1068
+ CREATE TRIGGER IF NOT EXISTS trg_fts_docs_delete AFTER DELETE ON doc_chunks BEGIN
1069
+ INSERT INTO fts_docs(fts_docs, rowid, title, content, file_path, collection)
1070
+ VALUES ('delete', old.id, old.title, old.content, old.file_path, old.collection);
1071
+ END;
1072
+
1073
+ -- \u2500\u2500 Path Contexts \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
1074
+ CREATE TABLE IF NOT EXISTS path_contexts (
1075
+ collection TEXT NOT NULL,
1076
+ path TEXT NOT NULL,
1077
+ context TEXT NOT NULL,
1078
+ PRIMARY KEY (collection, path)
1079
+ );
1080
+
1081
+ -- \u2500\u2500 Dynamic Collections (KV Store) \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
1082
+ CREATE TABLE IF NOT EXISTS kv_data (
1083
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
1084
+ collection TEXT NOT NULL,
1085
+ content TEXT NOT NULL,
1086
+ meta_json TEXT NOT NULL DEFAULT '{}',
1087
+ tags_json TEXT NOT NULL DEFAULT '[]',
1088
+ expires_at INTEGER,
1089
+ created_at INTEGER NOT NULL DEFAULT (unixepoch())
1090
+ );
1091
+
1092
+ CREATE TABLE IF NOT EXISTS kv_vectors (
1093
+ data_id INTEGER PRIMARY KEY REFERENCES kv_data(id) ON DELETE CASCADE,
1094
+ embedding BLOB NOT NULL
1095
+ );
1096
+
1097
+ CREATE VIRTUAL TABLE IF NOT EXISTS fts_kv USING fts5(
1098
+ content,
1099
+ collection,
1100
+ content='kv_data',
1101
+ content_rowid='id',
1102
+ tokenize='porter unicode61'
1103
+ );
1104
+
1105
+ CREATE TRIGGER IF NOT EXISTS trg_fts_kv_insert AFTER INSERT ON kv_data BEGIN
1106
+ INSERT INTO fts_kv(rowid, content, collection)
1107
+ VALUES (new.id, new.content, new.collection);
1108
+ END;
1109
+ CREATE TRIGGER IF NOT EXISTS trg_fts_kv_delete AFTER DELETE ON kv_data BEGIN
1110
+ INSERT INTO fts_kv(fts_kv, rowid, content, collection)
1111
+ VALUES ('delete', old.id, old.content, old.collection);
1112
+ END;
1113
+
1114
+ CREATE INDEX IF NOT EXISTS idx_kv_collection ON kv_data(collection);
1115
+ CREATE INDEX IF NOT EXISTS idx_kv_created ON kv_data(created_at DESC);
1116
+
1117
+ -- \u2500\u2500 Embedding Metadata \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
1118
+ CREATE TABLE IF NOT EXISTS embedding_meta (
1119
+ key TEXT PRIMARY KEY,
1120
+ value TEXT NOT NULL
1121
+ );
1122
+ `);
1123
+ }
1124
+ __name(createSchema, "createSchema");
1125
+
1126
+ // src/storage/database.ts
1127
+ var Database = class {
1128
+ static {
1129
+ __name(this, "Database");
1130
+ }
1131
+ db;
1132
+ constructor(dbPath) {
1133
+ const dir = path2.dirname(dbPath);
1134
+ if (!fs.existsSync(dir)) {
1135
+ fs.mkdirSync(dir, { recursive: true });
1136
+ }
1137
+ this.db = new BetterSqlite3(dbPath);
1138
+ this.db.pragma("journal_mode = WAL");
1139
+ this.db.pragma("synchronous = NORMAL");
1140
+ this.db.pragma("foreign_keys = ON");
1141
+ createSchema(this.db);
1142
+ }
1143
+ /**
1144
+ * Run a function inside a transaction.
1145
+ * Auto-commits on success, auto-rollbacks on error.
1146
+ */
1147
+ transaction(fn) {
1148
+ const tx = this.db.transaction(fn);
1149
+ return tx();
1150
+ }
1151
+ /**
1152
+ * Run a prepared statement on multiple rows.
1153
+ * Wraps in a single transaction for performance.
1154
+ */
1155
+ batch(sql, rows) {
1156
+ const stmt = this.db.prepare(sql);
1157
+ const tx = this.db.transaction(() => {
1158
+ for (const row of rows) {
1159
+ stmt.run(...row);
1160
+ }
1161
+ });
1162
+ tx();
1163
+ }
1164
+ /** Prepare a reusable statement. */
1165
+ prepare(sql) {
1166
+ return this.db.prepare(sql);
1167
+ }
1168
+ /** Execute raw SQL (no results). */
1169
+ exec(sql) {
1170
+ this.db.exec(sql);
1171
+ }
1172
+ /** Close the database. */
1173
+ close() {
1174
+ this.db.close();
1175
+ }
1176
+ };
1177
+
1178
+ // src/core/reembed.ts
1179
+ var TABLES = [
1180
+ {
1181
+ name: "code",
1182
+ textTable: "code_chunks",
1183
+ vectorTable: "code_vectors",
1184
+ idColumn: "id",
1185
+ fkColumn: "chunk_id",
1186
+ textBuilder: /* @__PURE__ */ __name((r) => [
1187
+ `File: ${r.file_path}`,
1188
+ r.name ? `${r.chunk_type}: ${r.name}` : r.chunk_type,
1189
+ r.content
1190
+ ].join("\n"), "textBuilder")
1191
+ },
1192
+ {
1193
+ name: "git",
1194
+ textTable: "git_commits",
1195
+ vectorTable: "git_vectors",
1196
+ idColumn: "id",
1197
+ fkColumn: "commit_id",
1198
+ textBuilder: /* @__PURE__ */ __name((r) => [
1199
+ r.message,
1200
+ r.diff ?? ""
1201
+ ].filter(Boolean).join("\n"), "textBuilder")
1202
+ },
1203
+ {
1204
+ name: "memory",
1205
+ textTable: "memory_patterns",
1206
+ vectorTable: "memory_vectors",
1207
+ idColumn: "id",
1208
+ fkColumn: "pattern_id",
1209
+ textBuilder: /* @__PURE__ */ __name((r) => [
1210
+ r.task_type,
1211
+ r.task,
1212
+ r.approach,
1213
+ r.outcome ?? ""
1214
+ ].filter(Boolean).join("\n"), "textBuilder")
1215
+ },
1216
+ {
1217
+ name: "notes",
1218
+ textTable: "note_memories",
1219
+ vectorTable: "note_vectors",
1220
+ idColumn: "id",
1221
+ fkColumn: "note_id",
1222
+ textBuilder: /* @__PURE__ */ __name((r) => [
1223
+ r.title,
1224
+ r.summary,
1225
+ r.decisions_json !== "[]" ? `Decisions: ${r.decisions_json}` : "",
1226
+ r.tags_json !== "[]" ? `Tags: ${r.tags_json}` : ""
1227
+ ].filter(Boolean).join("\n"), "textBuilder")
1228
+ },
1229
+ {
1230
+ name: "docs",
1231
+ textTable: "doc_chunks",
1232
+ vectorTable: "doc_vectors",
1233
+ idColumn: "id",
1234
+ fkColumn: "chunk_id",
1235
+ textBuilder: /* @__PURE__ */ __name((r) => [
1236
+ r.title ? `# ${r.title}` : "",
1237
+ r.content
1238
+ ].filter(Boolean).join("\n"), "textBuilder")
1239
+ },
1240
+ {
1241
+ name: "kv",
1242
+ textTable: "kv_data",
1243
+ vectorTable: "kv_vectors",
1244
+ idColumn: "id",
1245
+ fkColumn: "data_id",
1246
+ textBuilder: /* @__PURE__ */ __name((r) => r.content, "textBuilder")
1247
+ }
1248
+ ];
1249
+ async function reembedAll(db, embedding, hnswMap, options = {}) {
1250
+ const { batchSize = 50, onProgress } = options;
1251
+ const result = {};
1252
+ let total = 0;
1253
+ for (const table of TABLES) {
1254
+ const count = await reembedTable(db, embedding, table, batchSize, onProgress);
1255
+ result[table.name] = count;
1256
+ total += count;
1257
+ const entry = hnswMap.get(table.name);
1258
+ if (entry && count > 0) {
1259
+ await rebuildHnsw(db, table, entry.hnsw, entry.vecs);
1260
+ }
1261
+ }
1262
+ const meta = {
1263
+ provider: embedding.constructor?.name ?? "unknown",
1264
+ dims: String(embedding.dims),
1265
+ reembedded_at: (/* @__PURE__ */ new Date()).toISOString()
1266
+ };
1267
+ const upsert = db.prepare(
1268
+ "INSERT OR REPLACE INTO embedding_meta (key, value) VALUES (?, ?)"
1269
+ );
1270
+ for (const [k, v] of Object.entries(meta)) {
1271
+ upsert.run(k, v);
1272
+ }
1273
+ return {
1274
+ code: result.code ?? 0,
1275
+ git: result.git ?? 0,
1276
+ memory: result.memory ?? 0,
1277
+ notes: result.notes ?? 0,
1278
+ docs: result.docs ?? 0,
1279
+ kv: result.kv ?? 0,
1280
+ total
1281
+ };
1282
+ }
1283
+ __name(reembedAll, "reembedAll");
1284
+ async function reembedTable(db, embedding, table, batchSize, onProgress) {
1285
+ const rows = db.prepare(
1286
+ `SELECT * FROM ${table.textTable}`
1287
+ ).all();
1288
+ if (rows.length === 0) return 0;
1289
+ db.prepare(`DELETE FROM ${table.vectorTable}`).run();
1290
+ const insertVec = db.prepare(
1291
+ `INSERT INTO ${table.vectorTable} (${table.fkColumn}, embedding) VALUES (?, ?)`
1292
+ );
1293
+ let processed = 0;
1294
+ for (let i = 0; i < rows.length; i += batchSize) {
1295
+ const batch = rows.slice(i, i + batchSize);
1296
+ const texts = batch.map((r) => table.textBuilder(r));
1297
+ const vectors = await embedding.embedBatch(texts);
1298
+ db.transaction(() => {
1299
+ for (let j = 0; j < batch.length; j++) {
1300
+ const id = batch[j][table.idColumn];
1301
+ const vec = vectors[j];
1302
+ insertVec.run(id, Buffer.from(vec.buffer));
1303
+ }
1304
+ });
1305
+ processed += batch.length;
1306
+ onProgress?.(table.name, processed, rows.length);
1307
+ }
1308
+ return processed;
1309
+ }
1310
+ __name(reembedTable, "reembedTable");
1311
+ async function rebuildHnsw(db, table, hnsw, vecs) {
1312
+ vecs.clear();
1313
+ const rows = db.prepare(
1314
+ `SELECT ${table.fkColumn} as id, embedding FROM ${table.vectorTable}`
1315
+ ).all();
1316
+ for (const row of rows) {
1317
+ const vec = new Float32Array(new Uint8Array(row.embedding).buffer);
1318
+ hnsw.add(vec, row.id);
1319
+ vecs.set(row.id, vec);
1320
+ }
1321
+ }
1322
+ __name(rebuildHnsw, "rebuildHnsw");
1323
+ function getEmbeddingMeta(db) {
1324
+ try {
1325
+ const provider = db.prepare(
1326
+ "SELECT value FROM embedding_meta WHERE key = 'provider'"
1327
+ ).get();
1328
+ const dims = db.prepare(
1329
+ "SELECT value FROM embedding_meta WHERE key = 'dims'"
1330
+ ).get();
1331
+ if (!provider || !dims) return null;
1332
+ return { provider: provider.value, dims: Number(dims.value) };
1333
+ } catch {
1334
+ return null;
1335
+ }
1336
+ }
1337
+ __name(getEmbeddingMeta, "getEmbeddingMeta");
1338
+ function setEmbeddingMeta(db, embedding) {
1339
+ const upsert = db.prepare(
1340
+ "INSERT OR REPLACE INTO embedding_meta (key, value) VALUES (?, ?)"
1341
+ );
1342
+ upsert.run("provider", embedding.constructor?.name ?? "unknown");
1343
+ upsert.run("dims", String(embedding.dims));
1344
+ upsert.run("indexed_at", (/* @__PURE__ */ new Date()).toISOString());
1345
+ }
1346
+ __name(setEmbeddingMeta, "setEmbeddingMeta");
1347
+ function detectProviderMismatch(db, embedding) {
1348
+ const meta = getEmbeddingMeta(db);
1349
+ if (!meta) return null;
1350
+ const currentName = embedding.constructor?.name ?? "unknown";
1351
+ const mismatch = meta.dims !== embedding.dims || meta.provider !== currentName;
1352
+ return {
1353
+ mismatch,
1354
+ stored: `${meta.provider}/${meta.dims}`,
1355
+ current: `${currentName}/${embedding.dims}`
1356
+ };
1357
+ }
1358
+ __name(detectProviderMismatch, "detectProviderMismatch");
1359
+
1360
+ // src/core/watch.ts
1361
+ import * as fs2 from "fs";
1362
+ import * as path3 from "path";
1363
+ function createWatcher(reindexFn, indexers, repoPath, options = {}) {
1364
+ const {
1365
+ paths = [repoPath],
1366
+ debounceMs = 2e3,
1367
+ onIndex,
1368
+ onError
1369
+ } = options;
1370
+ let active = true;
1371
+ const watchers = [];
1372
+ const pending = /* @__PURE__ */ new Set();
1373
+ let timer = null;
1374
+ const customPatterns = [];
1375
+ for (const indexer of indexers.values()) {
1376
+ if (indexer.watchPatterns) {
1377
+ customPatterns.push({ indexer, patterns: indexer.watchPatterns() });
1378
+ }
1379
+ }
1380
+ function matchCustomIndexer(filePath) {
1381
+ const rel = path3.relative(repoPath, filePath);
1382
+ for (const { indexer, patterns } of customPatterns) {
1383
+ for (const pattern of patterns) {
1384
+ if (matchGlob(rel, pattern)) return indexer;
1385
+ }
1386
+ }
1387
+ return null;
1388
+ }
1389
+ __name(matchCustomIndexer, "matchCustomIndexer");
1390
+ function matchGlob(filePath, pattern) {
1391
+ if (pattern.startsWith("**/")) {
1392
+ const suffix = pattern.slice(3);
1393
+ const ext = suffix.startsWith("*.") ? suffix.slice(1) : null;
1394
+ if (ext) return filePath.endsWith(ext);
1395
+ return path3.basename(filePath) === suffix;
1396
+ }
1397
+ if (pattern.startsWith("*.")) {
1398
+ return filePath.endsWith(pattern.slice(1));
1399
+ }
1400
+ return filePath === pattern;
1401
+ }
1402
+ __name(matchGlob, "matchGlob");
1403
+ async function flush() {
1404
+ if (pending.size === 0) return;
1405
+ const files = [...pending];
1406
+ pending.clear();
1407
+ let needsReindex = false;
1408
+ for (const filePath of files) {
1409
+ const absPath = path3.resolve(repoPath, filePath);
1410
+ const customIndexer = matchCustomIndexer(absPath);
1411
+ if (customIndexer?.onFileChange) {
1412
+ try {
1413
+ const handled = await customIndexer.onFileChange(absPath, detectEvent(absPath));
1414
+ if (handled) {
1415
+ onIndex?.(filePath, customIndexer.name);
1416
+ continue;
1417
+ }
1418
+ } catch (err) {
1419
+ onError?.(err instanceof Error ? err : new Error(String(err)));
1420
+ }
1421
+ }
1422
+ if (isSupported(filePath)) {
1423
+ needsReindex = true;
1424
+ onIndex?.(filePath, "code");
1425
+ }
1426
+ }
1427
+ if (needsReindex) {
1428
+ try {
1429
+ await reindexFn();
1430
+ } catch (err) {
1431
+ onError?.(err instanceof Error ? err : new Error(String(err)));
1432
+ }
1433
+ }
1434
+ }
1435
+ __name(flush, "flush");
1436
+ function detectEvent(filePath) {
1437
+ try {
1438
+ fs2.accessSync(filePath);
1439
+ return "update";
1440
+ } catch {
1441
+ return "delete";
1442
+ }
1443
+ }
1444
+ __name(detectEvent, "detectEvent");
1445
+ function shouldWatch(filename) {
1446
+ if (!filename) return false;
1447
+ const parts = filename.split(path3.sep);
1448
+ for (const part of parts) {
1449
+ if (isIgnoredDir(part)) return false;
1450
+ }
1451
+ if (isIgnoredFile(path3.basename(filename))) return false;
1452
+ if (isSupported(filename)) return true;
1453
+ if (matchCustomIndexer(path3.resolve(repoPath, filename))) return true;
1454
+ return false;
1455
+ }
1456
+ __name(shouldWatch, "shouldWatch");
1457
+ for (const watchPath of paths) {
1458
+ const resolved = path3.resolve(watchPath);
1459
+ try {
1460
+ const watcher = fs2.watch(resolved, { recursive: true }, (_event, filename) => {
1461
+ if (!active || !filename) return;
1462
+ if (!shouldWatch(filename)) return;
1463
+ pending.add(filename);
1464
+ if (timer) clearTimeout(timer);
1465
+ timer = setTimeout(() => flush(), debounceMs);
1466
+ });
1467
+ watcher.on("error", (err) => {
1468
+ onError?.(err instanceof Error ? err : new Error(String(err)));
1469
+ });
1470
+ watchers.push(watcher);
1471
+ } catch (err) {
1472
+ onError?.(err instanceof Error ? err : new Error(String(err)));
1473
+ }
1474
+ }
1475
+ return {
1476
+ close() {
1477
+ active = false;
1478
+ if (timer) clearTimeout(timer);
1479
+ for (const w of watchers) w.close();
1480
+ watchers.length = 0;
1481
+ },
1482
+ get active() {
1483
+ return active;
1484
+ }
1485
+ };
1486
+ }
1487
+ __name(createWatcher, "createWatcher");
1488
+
1489
+ // src/core/brainbank.ts
1490
+ var BrainBank = class extends EventEmitter {
1491
+ static {
1492
+ __name(this, "BrainBank");
1493
+ }
1494
+ _config;
1495
+ _db;
1496
+ _embedding;
1497
+ _modules = /* @__PURE__ */ new Map();
1498
+ // Cross-module search (created if code/git/memory are present)
1499
+ _search;
1500
+ _bm25;
1501
+ _contextBuilder;
1502
+ _initialized = false;
1503
+ _watcher;
1504
+ // Collections
1505
+ _collections = /* @__PURE__ */ new Map();
1506
+ _kvHnsw;
1507
+ _kvVecs = /* @__PURE__ */ new Map();
1508
+ // Shared HNSW pool for multi-repo (code:frontend, code:backend share one HNSW)
1509
+ _sharedHnsw = /* @__PURE__ */ new Map();
1510
+ constructor(config = {}) {
1511
+ super();
1512
+ this._config = resolveConfig(config);
1513
+ }
1514
+ // ── Indexer Registration ────────────────────────
1515
+ /**
1516
+ * Register an indexer. Chainable.
1517
+ *
1518
+ * brain.use(code({ repoPath: '.' })).use(docs());
1519
+ */
1520
+ use(indexer) {
1521
+ if (this._initialized) {
1522
+ throw new Error(
1523
+ `BrainBank: Cannot add indexer '${indexer.name}' after initialization. Call .use() before any operations.`
1524
+ );
1525
+ }
1526
+ this._modules.set(indexer.name, indexer);
1527
+ return this;
1528
+ }
1529
+ /** Get the list of registered indexer names. */
1530
+ get indexers() {
1531
+ return [...this._modules.keys()];
1532
+ }
1533
+ /** @deprecated Use .indexers instead. */
1534
+ get modules() {
1535
+ return this.indexers;
1536
+ }
1537
+ /** Check if an indexer is loaded. Also matches type prefix (e.g. 'code' matches 'code:frontend'). */
1538
+ has(name) {
1539
+ if (this._modules.has(name)) return true;
1540
+ for (const key of this._modules.keys()) {
1541
+ if (key.startsWith(name + ":")) return true;
1542
+ }
1543
+ return false;
1544
+ }
1545
+ /** Get an indexer instance. Throws if not loaded. */
1546
+ indexer(name) {
1547
+ const mod = this._modules.get(name);
1548
+ if (!mod) {
1549
+ const first = this._findFirstByType(name);
1550
+ if (first) return first;
1551
+ throw new Error(
1552
+ `BrainBank: Indexer '${name}' is not loaded. Add .use(${name}()) to your BrainBank instance.`
1553
+ );
1554
+ }
1555
+ return mod;
1556
+ }
1557
+ /** @deprecated Use .indexer() instead. */
1558
+ module(name) {
1559
+ return this.indexer(name);
1560
+ }
1561
+ /** Find all indexers whose name equals or starts with the type prefix. */
1562
+ _findAllByType(type) {
1563
+ return [...this._modules.values()].filter(
1564
+ (m) => m.name === type || m.name.startsWith(type + ":")
1565
+ );
1566
+ }
1567
+ /** Find the first indexer that matches the type. */
1568
+ _findFirstByType(type) {
1569
+ for (const m of this._modules.values()) {
1570
+ if (m.name === type || m.name.startsWith(type + ":")) return m;
1571
+ }
1572
+ return void 0;
1573
+ }
1574
+ // ── Initialization ──────────────────────────────
1575
+ /**
1576
+ * Initialize database, HNSW indices, and load existing vectors.
1577
+ * Only initializes registered modules.
1578
+ * Automatically called by index/search methods if not yet initialized.
1579
+ */
1580
+ async initialize() {
1581
+ if (this._initialized) return;
1582
+ const config = this._config;
1583
+ this._db = new Database(config.dbPath);
1584
+ this._embedding = config.embeddingProvider ?? new LocalEmbedding();
1585
+ this._kvHnsw = new HNSWIndex(
1586
+ config.embeddingDims,
1587
+ config.maxElements ?? 5e5,
1588
+ config.hnswM,
1589
+ config.hnswEfConstruction,
1590
+ config.hnswEfSearch
1591
+ );
1592
+ await this._kvHnsw.init();
1593
+ this._loadVectors("kv_vectors", "data_id", this._kvHnsw, this._kvVecs);
1594
+ const ctx = {
1595
+ db: this._db,
1596
+ embedding: this._embedding,
1597
+ config,
1598
+ createHnsw: /* @__PURE__ */ __name(async (maxElements) => {
1599
+ return new HNSWIndex(
1600
+ config.embeddingDims,
1601
+ maxElements ?? config.maxElements,
1602
+ config.hnswM,
1603
+ config.hnswEfConstruction,
1604
+ config.hnswEfSearch
1605
+ ).init();
1606
+ }, "createHnsw"),
1607
+ loadVectors: /* @__PURE__ */ __name((table, idCol, hnsw, cache) => {
1608
+ this._loadVectors(table, idCol, hnsw, cache);
1609
+ }, "loadVectors"),
1610
+ getOrCreateSharedHnsw: /* @__PURE__ */ __name(async (type, maxElements) => {
1611
+ const existing = this._sharedHnsw.get(type);
1612
+ if (existing) return { ...existing, isNew: false };
1613
+ const hnsw = await new HNSWIndex(
1614
+ config.embeddingDims,
1615
+ maxElements ?? config.maxElements,
1616
+ config.hnswM,
1617
+ config.hnswEfConstruction,
1618
+ config.hnswEfSearch
1619
+ ).init();
1620
+ const vecCache = /* @__PURE__ */ new Map();
1621
+ this._sharedHnsw.set(type, { hnsw, vecCache });
1622
+ return { hnsw, vecCache, isNew: true };
1623
+ }, "getOrCreateSharedHnsw"),
1624
+ collection: /* @__PURE__ */ __name((name) => this.collection(name), "collection")
1625
+ };
1626
+ for (const mod of this._modules.values()) {
1627
+ await mod.initialize(ctx);
1628
+ }
1629
+ const codeShared = this._sharedHnsw.get("code");
1630
+ const gitShared = this._sharedHnsw.get("git");
1631
+ const memMod = this._modules.get("memory");
1632
+ if (codeShared || gitShared || memMod) {
1633
+ this._search = new UnifiedSearch({
1634
+ db: this._db,
1635
+ codeHnsw: codeShared?.hnsw,
1636
+ gitHnsw: gitShared?.hnsw,
1637
+ memHnsw: memMod?.hnsw,
1638
+ codeVecs: codeShared?.vecCache ?? /* @__PURE__ */ new Map(),
1639
+ gitVecs: gitShared?.vecCache ?? /* @__PURE__ */ new Map(),
1640
+ memVecs: memMod?.vecCache ?? /* @__PURE__ */ new Map(),
1641
+ embedding: this._embedding,
1642
+ reranker: this._config.reranker
1643
+ });
1644
+ this._bm25 = new BM25Search(this._db);
1645
+ }
1646
+ if (this._search) {
1647
+ const firstGit = this._findFirstByType("git");
1648
+ this._contextBuilder = new ContextBuilder(this._search, firstGit?.coEdits);
1649
+ }
1650
+ setEmbeddingMeta(this._db, this._embedding);
1651
+ const mismatch = detectProviderMismatch(this._db, this._embedding);
1652
+ if (mismatch?.mismatch) {
1653
+ this.emit("warning", {
1654
+ type: "provider_mismatch",
1655
+ message: `Embedding provider changed (${mismatch.stored} \u2192 ${mismatch.current}). Run brain.reembed() to regenerate vectors.`
1656
+ });
1657
+ }
1658
+ this._initialized = true;
1659
+ this.emit("initialized", { indexers: this.indexers });
1660
+ }
1661
+ // ── Collections ─────────────────────────────────
1662
+ /**
1663
+ * Get or create a dynamic collection.
1664
+ * Collections are the universal data primitive — store anything, search semantically.
1665
+ *
1666
+ * const errors = brain.collection('debug_errors');
1667
+ * await errors.add('Fixed null check', { file: 'api.ts' });
1668
+ * const hits = await errors.search('null pointer');
1669
+ */
1670
+ collection(name) {
1671
+ let coll = this._collections.get(name);
1672
+ if (coll) return coll;
1673
+ if (!this._initialized) {
1674
+ throw new Error(
1675
+ "BrainBank: Must call initialize() before using collections. Or use await brain.collection() after an async operation."
1676
+ );
1677
+ }
1678
+ if (!this._kvHnsw) {
1679
+ throw new Error("BrainBank: Collections HNSW not initialized. Call initialize() first.");
1680
+ }
1681
+ coll = new Collection(name, this._db, this._embedding, this._kvHnsw, this._kvVecs, this._config.reranker);
1682
+ this._collections.set(name, coll);
1683
+ return coll;
1684
+ }
1685
+ /** List all collection names that have data. */
1686
+ listCollectionNames() {
1687
+ const rows = this._db.prepare(
1688
+ "SELECT DISTINCT collection FROM kv_data ORDER BY collection"
1689
+ ).all();
1690
+ return rows.map((r) => r.collection);
1691
+ }
1692
+ // ── Indexing ─────────────────────────────────────
1693
+ /**
1694
+ * Index code and git history in one call.
1695
+ * Incremental — only processes changes since last run.
1696
+ */
1697
+ async index(options = {}) {
1698
+ await this.initialize();
1699
+ const result = {};
1700
+ const codeMods = this._findAllByType("code");
1701
+ for (const mod of codeMods) {
1702
+ const label = mod.name === "code" ? "code" : mod.name;
1703
+ options.onProgress?.(label, "Starting...");
1704
+ const r = await mod.index({
1705
+ forceReindex: options.forceReindex,
1706
+ onProgress: /* @__PURE__ */ __name((f, i, t) => options.onProgress?.(label, `[${i}/${t}] ${f}`), "onProgress")
1707
+ });
1708
+ if (result.code) {
1709
+ result.code.indexed += r.indexed;
1710
+ result.code.skipped += r.skipped;
1711
+ result.code.chunks = (result.code.chunks ?? 0) + (r.chunks ?? 0);
1712
+ } else {
1713
+ result.code = r;
1714
+ }
1715
+ }
1716
+ const gitMods = this._findAllByType("git");
1717
+ for (const mod of gitMods) {
1718
+ const label = mod.name === "git" ? "git" : mod.name;
1719
+ options.onProgress?.(label, "Starting...");
1720
+ const r = await mod.index({
1721
+ depth: options.gitDepth ?? this._config.gitDepth,
1722
+ onProgress: /* @__PURE__ */ __name((f, i, t) => options.onProgress?.(label, `[${i}/${t}] ${f}`), "onProgress")
1723
+ });
1724
+ if (result.git) {
1725
+ result.git.indexed += r.indexed;
1726
+ result.git.skipped += r.skipped;
1727
+ } else {
1728
+ result.git = r;
1729
+ }
1730
+ }
1731
+ this.emit("indexed", result);
1732
+ return result;
1733
+ }
1734
+ /** Index only code files. */
1735
+ async indexCode(options = {}) {
1736
+ await this.initialize();
1737
+ return this.module("code").index(options);
1738
+ }
1739
+ /** Index only git history. */
1740
+ async indexGit(options = {}) {
1741
+ await this.initialize();
1742
+ return this.module("git").index(options);
1743
+ }
1744
+ // ── Document Collections ────────────────────────
1745
+ /** Register a document collection. */
1746
+ async addCollection(collection) {
1747
+ await this.initialize();
1748
+ this.module("docs").addCollection(collection);
1749
+ }
1750
+ /** Remove a collection and all its indexed data. */
1751
+ async removeCollection(name) {
1752
+ await this.initialize();
1753
+ this.module("docs").removeCollection(name);
1754
+ }
1755
+ /** List all registered collections. */
1756
+ listCollections() {
1757
+ return this.module("docs").listCollections();
1758
+ }
1759
+ /** Index all (or specific) document collections. */
1760
+ async indexDocs(options = {}) {
1761
+ await this.initialize();
1762
+ const results = await this.module("docs").indexCollections(options);
1763
+ this.emit("docsIndexed", results);
1764
+ return results;
1765
+ }
1766
+ /** Search documents only. */
1767
+ async searchDocs(query, options) {
1768
+ await this.initialize();
1769
+ return this.module("docs").search(query, options);
1770
+ }
1771
+ // ── Context Metadata ────────────────────────────
1772
+ /** Add context description for a collection path. */
1773
+ addContext(collection, path4, context) {
1774
+ this.module("docs").addContext(collection, path4, context);
1775
+ }
1776
+ /** Remove context for a collection path. */
1777
+ removeContext(collection, path4) {
1778
+ this.module("docs").removeContext(collection, path4);
1779
+ }
1780
+ /** List all context entries. */
1781
+ listContexts() {
1782
+ return this.module("docs").listContexts();
1783
+ }
1784
+ // ── Context ─────────────────────────────────────
1785
+ /**
1786
+ * Get formatted context for a task.
1787
+ * Returns markdown ready for system prompt injection.
1788
+ */
1789
+ async getContext(task, options = {}) {
1790
+ await this.initialize();
1791
+ const sections = [];
1792
+ if (this._contextBuilder) {
1793
+ const coreContext = await this._contextBuilder.build(task, options);
1794
+ if (coreContext) sections.push(coreContext);
1795
+ }
1796
+ if (this.has("docs")) {
1797
+ const docResults = await this.searchDocs(task, { k: options.codeResults ?? 4 });
1798
+ if (docResults.length > 0) {
1799
+ const docSection = docResults.map((r) => {
1800
+ const header = r.context ? `**[${r.metadata.collection}]** ${r.metadata.title} \u2014 _${r.context}_` : `**[${r.metadata.collection}]** ${r.metadata.title}`;
1801
+ return `${header}
1802
+
1803
+ ${r.content}`;
1804
+ }).join("\n\n---\n\n");
1805
+ sections.push(`## Relevant Documents
1806
+
1807
+ ${docSection}`);
1808
+ }
1809
+ }
1810
+ return sections.join("\n\n");
1811
+ }
1812
+ // ── Search ──────────────────────────────────────
1813
+ /** Semantic search across all loaded modules. */
1814
+ async search(query, options) {
1815
+ await this.initialize();
1816
+ if (!this._search) {
1817
+ if (this.has("docs")) return this.searchDocs(query, { k: 8 });
1818
+ return [];
1819
+ }
1820
+ return this._search.search(query, options);
1821
+ }
1822
+ /** Semantic search over code only. */
1823
+ async searchCode(query, k = 8) {
1824
+ this.module("code");
1825
+ await this.initialize();
1826
+ return this._search.search(query, { codeK: k, gitK: 0, memoryK: 0 });
1827
+ }
1828
+ /** Semantic search over commits only. */
1829
+ async searchCommits(query, k = 8) {
1830
+ this.module("git");
1831
+ await this.initialize();
1832
+ return this._search.search(query, { codeK: 0, gitK: k, memoryK: 0 });
1833
+ }
1834
+ // ── Hybrid Search ───────────────────────────────
1835
+ /**
1836
+ * Hybrid search: vector + BM25 fused with Reciprocal Rank Fusion.
1837
+ * Best quality — catches both exact keyword matches and conceptual similarities.
1838
+ */
1839
+ async hybridSearch(query, options) {
1840
+ await this.initialize();
1841
+ const cols = options?.collections ?? {};
1842
+ const codeK = cols.code ?? options?.codeK ?? 6;
1843
+ const gitK = cols.git ?? options?.gitK ?? 5;
1844
+ const docsK = cols.docs ?? 8;
1845
+ const resultLists = [];
1846
+ if (this._search) {
1847
+ const searchOpts = { ...options, codeK, gitK };
1848
+ const [vectorResults, bm25Results] = await Promise.all([
1849
+ this._search.search(query, searchOpts),
1850
+ Promise.resolve(this._bm25.search(query, searchOpts))
1851
+ ]);
1852
+ resultLists.push(vectorResults, bm25Results);
1853
+ }
1854
+ if (this.has("docs")) {
1855
+ const docResults = await this.searchDocs(query, { k: docsK });
1856
+ if (docResults.length > 0) resultLists.push(docResults);
1857
+ }
1858
+ const reserved = /* @__PURE__ */ new Set(["code", "git", "docs"]);
1859
+ for (const [name, k] of Object.entries(cols)) {
1860
+ if (reserved.has(name)) continue;
1861
+ const col = this.collection(name);
1862
+ const hits = await col.search(query, { k });
1863
+ if (hits.length > 0) {
1864
+ resultLists.push(hits.map((h) => ({
1865
+ type: "collection",
1866
+ score: h.score ?? 0,
1867
+ content: h.content,
1868
+ metadata: { collection: name, id: h.id, ...h.metadata }
1869
+ })));
1870
+ }
1871
+ }
1872
+ if (resultLists.length === 0) return [];
1873
+ const fused = reciprocalRankFusion(resultLists);
1874
+ if (this._config.reranker && fused.length > 1) {
1875
+ const documents = fused.map((r) => r.content);
1876
+ const scores = await this._config.reranker.rank(query, documents);
1877
+ const blended = fused.map((r, i) => {
1878
+ const pos = i + 1;
1879
+ const rrfWeight = pos <= 3 ? 0.75 : pos <= 10 ? 0.6 : 0.4;
1880
+ return {
1881
+ ...r,
1882
+ score: rrfWeight * r.score + (1 - rrfWeight) * (scores[i] ?? 0)
1883
+ };
1884
+ });
1885
+ return blended.sort((a, b) => b.score - a.score);
1886
+ }
1887
+ return fused;
1888
+ }
1889
+ /** BM25 keyword search only (no embeddings needed). */
1890
+ searchBM25(query, options) {
1891
+ if (!this._bm25) return [];
1892
+ return this._bm25.search(query, options);
1893
+ }
1894
+ /** Rebuild FTS5 indices. */
1895
+ rebuildFTS() {
1896
+ this._bm25?.rebuild();
1897
+ }
1898
+ // ── Query ───────────────────────────────────────
1899
+ /** Get git history for a specific file. */
1900
+ async fileHistory(filePath, limit = 20) {
1901
+ this.module("git");
1902
+ await this.initialize();
1903
+ return this._db.prepare(`
1904
+ SELECT c.short_hash, c.message, c.author, c.date, c.additions, c.deletions
1905
+ FROM git_commits c
1906
+ INNER JOIN commit_files cf ON c.id = cf.commit_id
1907
+ WHERE cf.file_path LIKE ? AND c.is_merge = 0
1908
+ ORDER BY c.timestamp DESC LIMIT ?
1909
+ `).all(`%${filePath}%`, limit);
1910
+ }
1911
+ /** Get co-edit suggestions for a file. */
1912
+ coEdits(filePath, limit = 5) {
1913
+ const gitMod = this.module("git");
1914
+ return gitMod.suggest(filePath, limit);
1915
+ }
1916
+ // ── Stats ───────────────────────────────────────
1917
+ /** Get statistics for all loaded modules. */
1918
+ stats() {
1919
+ const result = {};
1920
+ if (this.has("code")) {
1921
+ const mod = this.indexer("code");
1922
+ result.code = {
1923
+ files: this._db.prepare("SELECT COUNT(DISTINCT file_path) as c FROM code_chunks").get().c,
1924
+ chunks: this._db.prepare("SELECT COUNT(*) as c FROM code_chunks").get().c,
1925
+ hnswSize: mod.hnsw?.size ?? 0
1926
+ };
1927
+ }
1928
+ if (this.has("git")) {
1929
+ const mod = this.indexer("git");
1930
+ result.git = {
1931
+ commits: this._db.prepare("SELECT COUNT(*) as c FROM git_commits").get().c,
1932
+ filesTracked: this._db.prepare("SELECT COUNT(DISTINCT file_path) as c FROM commit_files").get().c,
1933
+ coEdits: this._db.prepare("SELECT COUNT(*) as c FROM co_edits").get().c,
1934
+ hnswSize: mod.hnsw?.size ?? 0
1935
+ };
1936
+ }
1937
+ if (this.has("docs")) {
1938
+ const mod = this.indexer("docs");
1939
+ result.documents = mod.stats();
1940
+ }
1941
+ return result;
1942
+ }
1943
+ // ── Watch Mode ───────────────────────────────────
1944
+ /**
1945
+ * Start watching for file changes and auto-re-index.
1946
+ * Works with built-in and custom indexers.
1947
+ *
1948
+ * const watcher = brain.watch({
1949
+ * onIndex: (file, indexer) => console.log(`${indexer}: ${file}`),
1950
+ * });
1951
+ * // later: watcher.close();
1952
+ */
1953
+ watch(options = {}) {
1954
+ if (!this._initialized) {
1955
+ throw new Error("BrainBank: Not initialized. Call initialize() before watch().");
1956
+ }
1957
+ this._watcher?.close();
1958
+ this._watcher = createWatcher(
1959
+ async () => {
1960
+ await this.index();
1961
+ },
1962
+ this._modules,
1963
+ this._config.repoPath,
1964
+ options
1965
+ );
1966
+ return this._watcher;
1967
+ }
1968
+ // ── Re-embedding ────────────────────────────────
1969
+ /**
1970
+ * Re-embed all existing text with the current embedding provider.
1971
+ * Use this when switching providers (e.g. Local → OpenAI).
1972
+ * Does NOT re-parse files, git history, or documents — only regenerates vectors.
1973
+ *
1974
+ * @example
1975
+ * const brain = new BrainBank({ embeddingProvider: new OpenAIEmbedding() });
1976
+ * await brain.initialize();
1977
+ * const result = await brain.reembed();
1978
+ * // → { code: 1200, git: 500, docs: 80, kv: 45, notes: 12, total: 1837 }
1979
+ */
1980
+ async reembed(options = {}) {
1981
+ if (!this._initialized) {
1982
+ throw new Error("BrainBank: Not initialized. Call initialize() before reembed().");
1983
+ }
1984
+ const hnswMap = /* @__PURE__ */ new Map();
1985
+ if (this._kvHnsw) {
1986
+ hnswMap.set("kv", { hnsw: this._kvHnsw, vecs: this._kvVecs });
1987
+ }
1988
+ const codeMod = this._modules.get("code");
1989
+ const gitMod = this._modules.get("git");
1990
+ const memMod = this._modules.get("memory");
1991
+ const docsMod = this._modules.get("docs");
1992
+ const notesMod = this._modules.get("notes");
1993
+ if (codeMod?.hnsw) hnswMap.set("code", { hnsw: codeMod.hnsw, vecs: codeMod.vecCache });
1994
+ if (gitMod?.hnsw) hnswMap.set("git", { hnsw: gitMod.hnsw, vecs: gitMod.vecCache });
1995
+ if (memMod?.hnsw) hnswMap.set("memory", { hnsw: memMod.hnsw, vecs: memMod.vecCache });
1996
+ if (notesMod?.hnsw) hnswMap.set("notes", { hnsw: notesMod.hnsw, vecs: notesMod.vecCache });
1997
+ if (docsMod?.hnsw) hnswMap.set("docs", { hnsw: docsMod.hnsw, vecs: docsMod.vecCache });
1998
+ const result = await reembedAll(
1999
+ this._db,
2000
+ this._embedding,
2001
+ hnswMap,
2002
+ options
2003
+ );
2004
+ this.emit("reembedded", result);
2005
+ return result;
2006
+ }
2007
+ // ── Lifecycle ────────────────────────────────────
2008
+ /** Close database and release resources. */
2009
+ close() {
2010
+ this._watcher?.close();
2011
+ for (const indexer of this._modules.values()) {
2012
+ indexer.close?.();
2013
+ }
2014
+ if (this._db) this._db.close();
2015
+ this._initialized = false;
2016
+ this._collections.clear();
2017
+ }
2018
+ /** Whether the brainbank has been initialized. */
2019
+ get isInitialized() {
2020
+ return this._initialized;
2021
+ }
2022
+ /** The resolved configuration. */
2023
+ get config() {
2024
+ return this._config;
2025
+ }
2026
+ // ── Internals ───────────────────────────────────
2027
+ /** Load vectors from SQLite into HNSW index. */
2028
+ _loadVectors(table, idCol, hnsw, cache) {
2029
+ const rows = this._db.prepare(`SELECT ${idCol}, embedding FROM ${table}`).all();
2030
+ for (const row of rows) {
2031
+ const vec = new Float32Array(row.embedding.buffer.slice(
2032
+ row.embedding.byteOffset,
2033
+ row.embedding.byteOffset + row.embedding.byteLength
2034
+ ));
2035
+ hnsw.add(vec, row[idCol]);
2036
+ cache.set(row[idCol], vec);
2037
+ }
2038
+ }
2039
+ };
2040
+
2041
+ export {
2042
+ DEFAULTS,
2043
+ resolveConfig,
2044
+ HNSWIndex,
2045
+ LocalEmbedding,
2046
+ searchMMR,
2047
+ UnifiedSearch,
2048
+ BM25Search,
2049
+ ContextBuilder,
2050
+ Collection,
2051
+ BrainBank
2052
+ };
2053
+ //# sourceMappingURL=chunk-YGSEUWLV.js.map