minimem 0.0.6 → 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.
@@ -0,0 +1,2689 @@
1
+ #!/usr/bin/env node
2
+
3
+ // src/minimem.ts
4
+ import { randomUUID } from "crypto";
5
+ import fs2 from "fs/promises";
6
+ import fsSync3 from "fs";
7
+ import path3 from "path";
8
+ import { DatabaseSync } from "sqlite";
9
+ import chokidar from "chokidar";
10
+
11
+ // src/internal.ts
12
+ import crypto from "crypto";
13
+ import fsSync from "fs";
14
+ import fs from "fs/promises";
15
+ import path from "path";
16
+ function logError(context, error, debug) {
17
+ if (!debug) return;
18
+ const message = error instanceof Error ? error.message : String(error);
19
+ debug(`[${context}] Error: ${message}`);
20
+ }
21
+ function ensureDir(dir, debug) {
22
+ try {
23
+ fsSync.mkdirSync(dir, { recursive: true });
24
+ } catch (error) {
25
+ const nodeError = error;
26
+ if (nodeError.code !== "EEXIST") {
27
+ logError("ensureDir", error, debug);
28
+ }
29
+ }
30
+ return dir;
31
+ }
32
+ async function exists(filePath) {
33
+ try {
34
+ await fs.access(filePath);
35
+ return true;
36
+ } catch {
37
+ return false;
38
+ }
39
+ }
40
+ async function walkDir(dir, files) {
41
+ const entries = await fs.readdir(dir, { withFileTypes: true });
42
+ for (const entry of entries) {
43
+ const full = path.join(dir, entry.name);
44
+ if (entry.isDirectory()) {
45
+ await walkDir(full, files);
46
+ continue;
47
+ }
48
+ if (!entry.isFile()) continue;
49
+ if (!entry.name.endsWith(".md")) continue;
50
+ files.push(full);
51
+ }
52
+ }
53
+ async function listMemoryFiles(memoryDir) {
54
+ const result = [];
55
+ const memoryFile = path.join(memoryDir, "MEMORY.md");
56
+ const altMemoryFile = path.join(memoryDir, "memory.md");
57
+ const hasUpper = await exists(memoryFile);
58
+ const hasLower = await exists(altMemoryFile);
59
+ if (hasUpper && hasLower) {
60
+ let upperReal = memoryFile;
61
+ let lowerReal = altMemoryFile;
62
+ try {
63
+ upperReal = await fs.realpath(memoryFile);
64
+ } catch {
65
+ }
66
+ try {
67
+ lowerReal = await fs.realpath(altMemoryFile);
68
+ } catch {
69
+ }
70
+ if (upperReal !== lowerReal) {
71
+ throw new Error(
72
+ `Both MEMORY.md and memory.md exist in ${memoryDir}. Please remove one to avoid ambiguity.`
73
+ );
74
+ }
75
+ result.push(memoryFile);
76
+ } else if (hasUpper) {
77
+ result.push(memoryFile);
78
+ } else if (hasLower) {
79
+ result.push(altMemoryFile);
80
+ }
81
+ const memorySubDir = path.join(memoryDir, "memory");
82
+ if (await exists(memorySubDir)) {
83
+ await walkDir(memorySubDir, result);
84
+ }
85
+ if (result.length <= 1) return result;
86
+ const seen = /* @__PURE__ */ new Set();
87
+ const deduped = [];
88
+ for (const entry of result) {
89
+ let key = entry;
90
+ try {
91
+ key = await fs.realpath(entry);
92
+ } catch {
93
+ }
94
+ if (seen.has(key)) continue;
95
+ seen.add(key);
96
+ deduped.push(entry);
97
+ }
98
+ return deduped;
99
+ }
100
+ function hashText(value) {
101
+ return crypto.createHash("sha256").update(value).digest("hex");
102
+ }
103
+ async function buildFileEntry(absPath, memoryDir) {
104
+ const stat = await fs.stat(absPath);
105
+ const content = await fs.readFile(absPath, "utf-8");
106
+ const hash = hashText(content);
107
+ return {
108
+ path: path.relative(memoryDir, absPath).replace(/\\/g, "/"),
109
+ absPath,
110
+ mtimeMs: stat.mtimeMs,
111
+ size: stat.size,
112
+ hash
113
+ };
114
+ }
115
+ function stripPrivateContent(content) {
116
+ return content.replace(/<private>[\s\S]*?<\/private>/gi, (match) => {
117
+ const lineCount = match.split("\n").length;
118
+ return "\n".repeat(lineCount - 1);
119
+ });
120
+ }
121
+ function chunkMarkdown(content, chunking) {
122
+ const stripped = stripPrivateContent(content);
123
+ const lines = stripped.split("\n");
124
+ if (lines.length === 0) return [];
125
+ const maxChars = Math.max(32, chunking.tokens * 4);
126
+ const overlapChars = Math.max(0, chunking.overlap * 4);
127
+ const chunks = [];
128
+ let current = [];
129
+ let currentChars = 0;
130
+ const flush = () => {
131
+ if (current.length === 0) return;
132
+ const firstEntry = current[0];
133
+ const lastEntry = current[current.length - 1];
134
+ if (!firstEntry || !lastEntry) return;
135
+ const text = current.map((entry) => entry.line).join("\n");
136
+ const startLine = firstEntry.lineNo;
137
+ const endLine = lastEntry.lineNo;
138
+ chunks.push({
139
+ startLine,
140
+ endLine,
141
+ text,
142
+ hash: hashText(text)
143
+ });
144
+ };
145
+ const carryOverlap = () => {
146
+ if (overlapChars <= 0 || current.length === 0) {
147
+ current = [];
148
+ currentChars = 0;
149
+ return;
150
+ }
151
+ let acc = 0;
152
+ const kept = [];
153
+ for (let i = current.length - 1; i >= 0; i -= 1) {
154
+ const entry = current[i];
155
+ if (!entry) continue;
156
+ acc += entry.line.length + 1;
157
+ kept.unshift(entry);
158
+ if (acc >= overlapChars) break;
159
+ }
160
+ current = kept;
161
+ currentChars = kept.reduce((sum, entry) => sum + entry.line.length + 1, 0);
162
+ };
163
+ for (let i = 0; i < lines.length; i += 1) {
164
+ const line = lines[i] ?? "";
165
+ const lineNo = i + 1;
166
+ const segments = [];
167
+ if (line.length === 0) {
168
+ segments.push("");
169
+ } else {
170
+ for (let start = 0; start < line.length; start += maxChars) {
171
+ segments.push(line.slice(start, start + maxChars));
172
+ }
173
+ }
174
+ for (const segment of segments) {
175
+ const lineSize = segment.length + 1;
176
+ if (currentChars + lineSize > maxChars && current.length > 0) {
177
+ flush();
178
+ carryOverlap();
179
+ }
180
+ current.push({ line: segment, lineNo });
181
+ currentChars += lineSize;
182
+ }
183
+ }
184
+ flush();
185
+ return chunks;
186
+ }
187
+ function extractChunkMetadata(text) {
188
+ const typeMatch = text.match(/<!--\s*type:\s*([\w-]+)\s*-->/i);
189
+ return typeMatch ? { type: typeMatch[1].toLowerCase() } : {};
190
+ }
191
+ function parseEmbedding(raw) {
192
+ try {
193
+ const parsed = JSON.parse(raw);
194
+ return Array.isArray(parsed) ? parsed : [];
195
+ } catch {
196
+ return [];
197
+ }
198
+ }
199
+ function cosineSimilarity(a, b) {
200
+ if (a.length === 0 || b.length === 0) return 0;
201
+ const len = Math.min(a.length, b.length);
202
+ let dot = 0;
203
+ let normA = 0;
204
+ let normB = 0;
205
+ for (let i = 0; i < len; i += 1) {
206
+ const av = a[i] ?? 0;
207
+ const bv = b[i] ?? 0;
208
+ dot += av * bv;
209
+ normA += av * av;
210
+ normB += bv * bv;
211
+ }
212
+ if (normA === 0 || normB === 0) return 0;
213
+ return dot / (Math.sqrt(normA) * Math.sqrt(normB));
214
+ }
215
+ function truncateUtf16Safe(text, maxChars) {
216
+ if (text.length <= maxChars) return text;
217
+ return text.slice(0, maxChars);
218
+ }
219
+ function vectorToBlob(embedding) {
220
+ return Buffer.from(new Float32Array(embedding).buffer);
221
+ }
222
+
223
+ // src/search/hybrid.ts
224
+ function buildFtsQuery(raw) {
225
+ const tokens = raw.match(/[A-Za-z0-9_]+/g)?.map((t) => t.trim()).filter(Boolean) ?? [];
226
+ if (tokens.length === 0) return null;
227
+ const quoted = tokens.map((t) => `"${t.replaceAll('"', "")}"`);
228
+ return quoted.join(" AND ");
229
+ }
230
+ function bm25RankToScore(rank) {
231
+ if (!Number.isFinite(rank)) {
232
+ return 0;
233
+ }
234
+ const absRank = Math.abs(rank);
235
+ return 1 / (1 + absRank);
236
+ }
237
+ function mergeHybridResults(params) {
238
+ const byId = /* @__PURE__ */ new Map();
239
+ for (const r of params.vector) {
240
+ byId.set(r.id, {
241
+ id: r.id,
242
+ path: r.path,
243
+ startLine: r.startLine,
244
+ endLine: r.endLine,
245
+ source: r.source,
246
+ snippet: r.snippet,
247
+ vectorScore: r.vectorScore,
248
+ textScore: 0
249
+ });
250
+ }
251
+ for (const r of params.keyword) {
252
+ const existing = byId.get(r.id);
253
+ if (existing) {
254
+ existing.textScore = r.textScore;
255
+ if (r.snippet && r.snippet.length > 0) existing.snippet = r.snippet;
256
+ } else {
257
+ byId.set(r.id, {
258
+ id: r.id,
259
+ path: r.path,
260
+ startLine: r.startLine,
261
+ endLine: r.endLine,
262
+ source: r.source,
263
+ snippet: r.snippet,
264
+ vectorScore: 0,
265
+ textScore: r.textScore
266
+ });
267
+ }
268
+ }
269
+ let vw = params.vectorWeight;
270
+ let tw = params.textWeight;
271
+ if (params.vector.length === 0 && params.keyword.length > 0) {
272
+ vw = 0;
273
+ tw = 1;
274
+ } else if (params.keyword.length === 0 && params.vector.length > 0) {
275
+ vw = 1;
276
+ tw = 0;
277
+ }
278
+ const merged = Array.from(byId.values()).map((entry) => {
279
+ const score = vw * entry.vectorScore + tw * entry.textScore;
280
+ return {
281
+ path: entry.path,
282
+ startLine: entry.startLine,
283
+ endLine: entry.endLine,
284
+ score,
285
+ snippet: entry.snippet,
286
+ source: entry.source
287
+ };
288
+ });
289
+ return merged.sort((a, b) => b.score - a.score);
290
+ }
291
+
292
+ // src/search/search.ts
293
+ function buildKnowledgeFilterSql(opts) {
294
+ const clauses = [];
295
+ const params = [];
296
+ if (opts.knowledgeType) {
297
+ clauses.push(` AND c.knowledge_type = ?`);
298
+ params.push(opts.knowledgeType);
299
+ }
300
+ if (opts.minConfidence !== void 0) {
301
+ clauses.push(` AND c.confidence >= ?`);
302
+ params.push(opts.minConfidence);
303
+ }
304
+ if (opts.domain && opts.domain.length > 0) {
305
+ const domainPlaceholders = opts.domain.map(() => "?").join(", ");
306
+ clauses.push(
307
+ ` AND EXISTS (SELECT 1 FROM json_each(c.domains) AS d WHERE d.value IN (${domainPlaceholders}))`
308
+ );
309
+ params.push(...opts.domain);
310
+ }
311
+ if (opts.entities && opts.entities.length > 0) {
312
+ const entityPlaceholders = opts.entities.map(() => "?").join(", ");
313
+ clauses.push(
314
+ ` AND EXISTS (SELECT 1 FROM json_each(c.entities) AS e WHERE e.value IN (${entityPlaceholders}))`
315
+ );
316
+ params.push(...opts.entities);
317
+ }
318
+ return { sql: clauses.join(""), params };
319
+ }
320
+ async function searchVector(params) {
321
+ if (params.queryVec.length === 0 || params.limit <= 0) return [];
322
+ if (await params.ensureVectorReady(params.queryVec.length)) {
323
+ const rows = params.db.prepare(
324
+ `SELECT c.id, c.path, c.start_line, c.end_line, c.text,
325
+ c.source,
326
+ vec_distance_cosine(v.embedding, ?) AS dist
327
+ FROM ${params.vectorTable} v
328
+ JOIN chunks c ON c.id = v.id
329
+ WHERE c.model = ?${params.sourceFilterVec.sql}
330
+ ORDER BY dist ASC
331
+ LIMIT ?`
332
+ ).all(
333
+ vectorToBlob(params.queryVec),
334
+ params.providerModel,
335
+ ...params.sourceFilterVec.params,
336
+ params.limit
337
+ );
338
+ return rows.map((row) => ({
339
+ id: row.id,
340
+ path: row.path,
341
+ startLine: row.start_line,
342
+ endLine: row.end_line,
343
+ score: 1 - row.dist,
344
+ snippet: truncateUtf16Safe(row.text, params.snippetMaxChars),
345
+ source: row.source
346
+ }));
347
+ }
348
+ const candidates = listChunks({
349
+ db: params.db,
350
+ providerModel: params.providerModel,
351
+ sourceFilter: params.sourceFilterChunks
352
+ });
353
+ const scored = candidates.map((chunk) => ({
354
+ chunk,
355
+ score: cosineSimilarity(params.queryVec, chunk.embedding)
356
+ })).filter((entry) => Number.isFinite(entry.score));
357
+ return scored.sort((a, b) => b.score - a.score).slice(0, params.limit).map((entry) => ({
358
+ id: entry.chunk.id,
359
+ path: entry.chunk.path,
360
+ startLine: entry.chunk.startLine,
361
+ endLine: entry.chunk.endLine,
362
+ score: entry.score,
363
+ snippet: truncateUtf16Safe(entry.chunk.text, params.snippetMaxChars),
364
+ source: entry.chunk.source
365
+ }));
366
+ }
367
+ function listChunks(params) {
368
+ const rows = params.db.prepare(
369
+ `SELECT id, path, start_line, end_line, text, embedding, source
370
+ FROM chunks
371
+ WHERE model = ?${params.sourceFilter.sql}`
372
+ ).all(params.providerModel, ...params.sourceFilter.params);
373
+ return rows.map((row) => ({
374
+ id: row.id,
375
+ path: row.path,
376
+ startLine: row.start_line,
377
+ endLine: row.end_line,
378
+ text: row.text,
379
+ embedding: parseEmbedding(row.embedding),
380
+ source: row.source
381
+ }));
382
+ }
383
+ async function searchKeyword(params) {
384
+ if (params.limit <= 0) return [];
385
+ const ftsQuery = params.buildFtsQuery(params.query);
386
+ if (!ftsQuery) return [];
387
+ const rows = params.db.prepare(
388
+ `SELECT id, path, source, start_line, end_line, text,
389
+ bm25(${params.ftsTable}) AS rank
390
+ FROM ${params.ftsTable}
391
+ WHERE ${params.ftsTable} MATCH ? AND model = ?${params.sourceFilter.sql}
392
+ ORDER BY rank ASC
393
+ LIMIT ?`
394
+ ).all(ftsQuery, params.providerModel, ...params.sourceFilter.params, params.limit);
395
+ return rows.map((row) => {
396
+ const textScore = params.bm25RankToScore(row.rank);
397
+ return {
398
+ id: row.id,
399
+ path: row.path,
400
+ startLine: row.start_line,
401
+ endLine: row.end_line,
402
+ score: textScore,
403
+ textScore,
404
+ snippet: truncateUtf16Safe(row.text, params.snippetMaxChars),
405
+ source: row.source
406
+ };
407
+ });
408
+ }
409
+
410
+ // src/db/schema.ts
411
+ var SCHEMA_VERSION = 4;
412
+ function ensureMemoryIndexSchema(params) {
413
+ params.db.exec(`
414
+ CREATE TABLE IF NOT EXISTS meta (
415
+ key TEXT PRIMARY KEY,
416
+ value TEXT NOT NULL
417
+ );
418
+ `);
419
+ const migrated = migrateIfNeeded(params.db, params.ftsTable);
420
+ params.db.exec(`
421
+ CREATE TABLE IF NOT EXISTS files (
422
+ path TEXT PRIMARY KEY,
423
+ source TEXT NOT NULL DEFAULT 'memory',
424
+ hash TEXT NOT NULL,
425
+ mtime INTEGER NOT NULL,
426
+ size INTEGER NOT NULL
427
+ );
428
+ `);
429
+ params.db.exec(`
430
+ CREATE TABLE IF NOT EXISTS chunks (
431
+ id TEXT PRIMARY KEY,
432
+ path TEXT NOT NULL,
433
+ source TEXT NOT NULL DEFAULT 'memory',
434
+ start_line INTEGER NOT NULL,
435
+ end_line INTEGER NOT NULL,
436
+ hash TEXT NOT NULL,
437
+ model TEXT NOT NULL,
438
+ text TEXT NOT NULL,
439
+ embedding TEXT NOT NULL,
440
+ updated_at INTEGER NOT NULL
441
+ );
442
+ `);
443
+ params.db.exec(`
444
+ CREATE TABLE IF NOT EXISTS ${params.embeddingCacheTable} (
445
+ provider TEXT NOT NULL,
446
+ model TEXT NOT NULL,
447
+ provider_key TEXT NOT NULL,
448
+ hash TEXT NOT NULL,
449
+ embedding TEXT NOT NULL,
450
+ dims INTEGER,
451
+ updated_at INTEGER NOT NULL,
452
+ PRIMARY KEY (provider, model, provider_key, hash)
453
+ );
454
+ `);
455
+ params.db.exec(
456
+ `CREATE INDEX IF NOT EXISTS idx_embedding_cache_updated_at ON ${params.embeddingCacheTable}(updated_at);`
457
+ );
458
+ let ftsAvailable = false;
459
+ let ftsError;
460
+ if (params.ftsEnabled) {
461
+ try {
462
+ params.db.exec(
463
+ `CREATE VIRTUAL TABLE IF NOT EXISTS ${params.ftsTable} USING fts5(
464
+ text,
465
+ id UNINDEXED,
466
+ path UNINDEXED,
467
+ source UNINDEXED,
468
+ model UNINDEXED,
469
+ start_line UNINDEXED,
470
+ end_line UNINDEXED
471
+ );`
472
+ );
473
+ ftsAvailable = true;
474
+ } catch (err) {
475
+ const message = err instanceof Error ? err.message : String(err);
476
+ ftsAvailable = false;
477
+ ftsError = message;
478
+ }
479
+ }
480
+ ensureColumn(params.db, "files", "source", "TEXT NOT NULL DEFAULT 'memory'");
481
+ ensureColumn(params.db, "chunks", "source", "TEXT NOT NULL DEFAULT 'memory'");
482
+ ensureColumn(params.db, "chunks", "type", "TEXT");
483
+ ensureColumn(params.db, "chunks", "knowledge_type", "TEXT");
484
+ ensureColumn(params.db, "chunks", "knowledge_id", "TEXT");
485
+ ensureColumn(params.db, "chunks", "domains", "TEXT");
486
+ ensureColumn(params.db, "chunks", "entities", "TEXT");
487
+ ensureColumn(params.db, "chunks", "confidence", "REAL");
488
+ params.db.exec(`CREATE INDEX IF NOT EXISTS idx_chunks_path ON chunks(path);`);
489
+ params.db.exec(`CREATE INDEX IF NOT EXISTS idx_chunks_source ON chunks(source);`);
490
+ params.db.exec(`CREATE INDEX IF NOT EXISTS idx_chunks_type ON chunks(type);`);
491
+ params.db.exec(`CREATE INDEX IF NOT EXISTS idx_chunks_knowledge_type ON chunks(knowledge_type);`);
492
+ params.db.exec(`CREATE INDEX IF NOT EXISTS idx_chunks_knowledge_id ON chunks(knowledge_id);`);
493
+ params.db.exec(`
494
+ CREATE TABLE IF NOT EXISTS knowledge_links (
495
+ from_id TEXT NOT NULL,
496
+ to_id TEXT NOT NULL,
497
+ relation TEXT NOT NULL,
498
+ layer TEXT,
499
+ weight REAL DEFAULT 0.5,
500
+ source_path TEXT,
501
+ created_at INTEGER,
502
+ PRIMARY KEY (from_id, to_id, relation)
503
+ );
504
+ `);
505
+ params.db.exec(`CREATE INDEX IF NOT EXISTS idx_kl_from ON knowledge_links(from_id);`);
506
+ params.db.exec(`CREATE INDEX IF NOT EXISTS idx_kl_to ON knowledge_links(to_id);`);
507
+ params.db.exec(`CREATE INDEX IF NOT EXISTS idx_kl_layer ON knowledge_links(layer);`);
508
+ params.db.prepare(
509
+ `INSERT OR REPLACE INTO meta (key, value) VALUES ('schema_version', ?)`
510
+ ).run(String(SCHEMA_VERSION));
511
+ return { ftsAvailable, ...ftsError ? { ftsError } : {}, ...migrated ? { migrated } : {} };
512
+ }
513
+ function migrateIfNeeded(db, ftsTable) {
514
+ let storedVersion = 0;
515
+ try {
516
+ const row = db.prepare(
517
+ `SELECT value FROM meta WHERE key = 'schema_version'`
518
+ ).get();
519
+ if (row) {
520
+ storedVersion = parseInt(row.value, 10) || 0;
521
+ }
522
+ } catch {
523
+ storedVersion = 0;
524
+ }
525
+ if (storedVersion >= SCHEMA_VERSION) return false;
526
+ if (storedVersion > 0 && storedVersion < SCHEMA_VERSION) {
527
+ db.exec(`DROP TABLE IF EXISTS files`);
528
+ db.exec(`DROP TABLE IF EXISTS chunks`);
529
+ db.exec(`DROP TABLE IF EXISTS knowledge_links`);
530
+ db.exec(`DROP TABLE IF EXISTS ${ftsTable}`);
531
+ try {
532
+ db.exec(`DROP TABLE IF EXISTS chunks_vec`);
533
+ } catch {
534
+ }
535
+ }
536
+ return storedVersion > 0;
537
+ }
538
+ function ensureColumn(db, table, column, definition) {
539
+ const rows = db.prepare(`PRAGMA table_info(${table})`).all();
540
+ if (rows.some((row) => row.name === column)) return;
541
+ db.exec(`ALTER TABLE ${table} ADD COLUMN ${column} ${definition}`);
542
+ }
543
+
544
+ // src/session.ts
545
+ import * as os from "os";
546
+ function parseFrontmatter(content) {
547
+ const frontmatterRegex = /^---\n([\s\S]*?)\n---\n/;
548
+ const match = content.match(frontmatterRegex);
549
+ if (!match) {
550
+ return { frontmatter: void 0, body: content };
551
+ }
552
+ const yamlContent = match[1];
553
+ const body = content.slice(match[0].length);
554
+ try {
555
+ const frontmatter = parseSimpleYaml(yamlContent);
556
+ return { frontmatter, body };
557
+ } catch {
558
+ return { frontmatter: void 0, body: content };
559
+ }
560
+ }
561
+ function parseSimpleYaml(yaml) {
562
+ const lines = yaml.split("\n");
563
+ return parseYamlBlock(lines, 0, 0, lines.length).value;
564
+ }
565
+ function parseYamlBlock(lines, indent, startIdx, endIdx) {
566
+ const result = {};
567
+ let i = startIdx;
568
+ while (i < endIdx) {
569
+ const line = lines[i];
570
+ if (!line || !line.trim()) {
571
+ i++;
572
+ continue;
573
+ }
574
+ const lineIndent = getIndent(line);
575
+ if (lineIndent < indent) break;
576
+ if (lineIndent > indent) {
577
+ i++;
578
+ continue;
579
+ }
580
+ const keyMatch = line.match(/^(\s*)([\w-]+):\s*(.*)?$/);
581
+ if (!keyMatch) {
582
+ i++;
583
+ continue;
584
+ }
585
+ const [, , key, rawValue] = keyMatch;
586
+ const value = rawValue?.trim() ?? "";
587
+ if (value === "" || value === void 0) {
588
+ const nextNonEmpty = findNextNonEmptyLine(lines, i + 1, endIdx);
589
+ if (nextNonEmpty < endIdx) {
590
+ const nextLine = lines[nextNonEmpty];
591
+ const nextIndent = getIndent(nextLine);
592
+ if (nextIndent > indent) {
593
+ if (nextLine.trimStart().startsWith("- ")) {
594
+ const listResult = parseYamlList(lines, nextIndent, i + 1, endIdx);
595
+ result[key] = listResult.value;
596
+ i = listResult.nextIdx;
597
+ } else {
598
+ const blockResult = parseYamlBlock(lines, nextIndent, i + 1, endIdx);
599
+ result[key] = blockResult.value;
600
+ i = blockResult.nextIdx;
601
+ }
602
+ continue;
603
+ }
604
+ }
605
+ result[key] = null;
606
+ i++;
607
+ } else {
608
+ result[key] = parseYamlValue(value);
609
+ i++;
610
+ }
611
+ }
612
+ return { value: result, nextIdx: i };
613
+ }
614
+ function parseYamlList(lines, indent, startIdx, endIdx) {
615
+ const result = [];
616
+ let i = startIdx;
617
+ while (i < endIdx) {
618
+ const line = lines[i];
619
+ if (!line || !line.trim()) {
620
+ i++;
621
+ continue;
622
+ }
623
+ const lineIndent = getIndent(line);
624
+ if (lineIndent < indent) break;
625
+ if (lineIndent > indent) {
626
+ i++;
627
+ continue;
628
+ }
629
+ const trimmed = line.trimStart();
630
+ if (!trimmed.startsWith("- ")) break;
631
+ const itemContent = trimmed.slice(2).trim();
632
+ if (itemContent === "" || itemContent === void 0) {
633
+ const nextNonEmpty = findNextNonEmptyLine(lines, i + 1, endIdx);
634
+ if (nextNonEmpty < endIdx) {
635
+ const nextIndent = getIndent(lines[nextNonEmpty]);
636
+ if (nextIndent > indent) {
637
+ const blockResult = parseYamlBlock(lines, nextIndent, i + 1, endIdx);
638
+ result.push(blockResult.value);
639
+ i = blockResult.nextIdx;
640
+ continue;
641
+ }
642
+ }
643
+ result.push(null);
644
+ i++;
645
+ } else {
646
+ const kvMatch = itemContent.match(/^([\w-]+):\s*(.*)$/);
647
+ if (kvMatch) {
648
+ const obj = {};
649
+ const [, firstKey, firstVal] = kvMatch;
650
+ obj[firstKey] = parseYamlValue(firstVal?.trim() ?? "");
651
+ const itemKeyIndent = indent + 2;
652
+ let j = i + 1;
653
+ while (j < endIdx) {
654
+ const nextLine = lines[j];
655
+ if (!nextLine || !nextLine.trim()) {
656
+ j++;
657
+ continue;
658
+ }
659
+ const nextLineIndent = getIndent(nextLine);
660
+ if (nextLineIndent < itemKeyIndent) break;
661
+ if (nextLineIndent === itemKeyIndent) {
662
+ const nextKv = nextLine.match(/^\s*([\w-]+):\s*(.*)$/);
663
+ if (nextKv) {
664
+ const [, nk, nv] = nextKv;
665
+ obj[nk] = parseYamlValue(nv?.trim() ?? "");
666
+ j++;
667
+ continue;
668
+ }
669
+ }
670
+ break;
671
+ }
672
+ result.push(obj);
673
+ i = j;
674
+ } else {
675
+ result.push(parseYamlValue(itemContent));
676
+ i++;
677
+ }
678
+ }
679
+ }
680
+ return { value: result, nextIdx: i };
681
+ }
682
+ function getIndent(line) {
683
+ const match = line.match(/^(\s*)/);
684
+ return match ? match[1].length : 0;
685
+ }
686
+ function findNextNonEmptyLine(lines, from, end) {
687
+ for (let i = from; i < end; i++) {
688
+ if (lines[i]?.trim()) return i;
689
+ }
690
+ return end;
691
+ }
692
+ function parseYamlValue(value) {
693
+ if (value === "") return null;
694
+ if (value.startsWith('"') && value.endsWith('"') || value.startsWith("'") && value.endsWith("'")) {
695
+ return value.slice(1, -1);
696
+ }
697
+ if (value === "null" || value === "~") return null;
698
+ if (value === "true") return true;
699
+ if (value === "false") return false;
700
+ const num = Number(value);
701
+ if (!isNaN(num) && value !== "") return num;
702
+ if (value.startsWith("[") && value.endsWith("]")) {
703
+ const inner = value.slice(1, -1);
704
+ if (inner.trim() === "") return [];
705
+ return inner.split(",").map((s) => parseYamlValue(s.trim()));
706
+ }
707
+ return value;
708
+ }
709
+ function serializeFrontmatter(frontmatter) {
710
+ const lines = ["---"];
711
+ if (frontmatter.id) {
712
+ lines.push(`id: ${frontmatter.id}`);
713
+ }
714
+ if (frontmatter.type) {
715
+ lines.push(`type: ${frontmatter.type}`);
716
+ }
717
+ if (frontmatter.session) {
718
+ lines.push("session:");
719
+ const session = frontmatter.session;
720
+ if (session.id) lines.push(` id: ${session.id}`);
721
+ if (session.source) lines.push(` source: ${session.source}`);
722
+ if (session.project) lines.push(` project: ${formatPath(session.project)}`);
723
+ if (session.transcript) lines.push(` transcript: ${formatPath(session.transcript)}`);
724
+ }
725
+ if (frontmatter.created) {
726
+ lines.push(`created: ${frontmatter.created}`);
727
+ }
728
+ if (frontmatter.updated) {
729
+ lines.push(`updated: ${frontmatter.updated}`);
730
+ }
731
+ if (frontmatter.tags && frontmatter.tags.length > 0) {
732
+ lines.push(`tags: [${frontmatter.tags.join(", ")}]`);
733
+ }
734
+ if (frontmatter.domain && frontmatter.domain.length > 0) {
735
+ lines.push(`domain: [${frontmatter.domain.join(", ")}]`);
736
+ }
737
+ if (frontmatter.entities && frontmatter.entities.length > 0) {
738
+ lines.push(`entities: [${frontmatter.entities.join(", ")}]`);
739
+ }
740
+ if (frontmatter.confidence !== void 0) {
741
+ lines.push(`confidence: ${frontmatter.confidence}`);
742
+ }
743
+ if (frontmatter.source) {
744
+ lines.push("source:");
745
+ if (frontmatter.source.origin) lines.push(` origin: ${frontmatter.source.origin}`);
746
+ if (frontmatter.source.trajectories && frontmatter.source.trajectories.length > 0) {
747
+ lines.push(` trajectories: [${frontmatter.source.trajectories.join(", ")}]`);
748
+ }
749
+ if (frontmatter.source.agentId) lines.push(` agentId: ${frontmatter.source.agentId}`);
750
+ }
751
+ if (frontmatter.links && frontmatter.links.length > 0) {
752
+ lines.push("links:");
753
+ for (const link of frontmatter.links) {
754
+ lines.push(` - target: ${link.target}`);
755
+ lines.push(` relation: ${link.relation}`);
756
+ if (link.layer) lines.push(` layer: ${link.layer}`);
757
+ }
758
+ }
759
+ if (frontmatter.supersedes !== void 0) {
760
+ lines.push(`supersedes: ${frontmatter.supersedes === null ? "~" : frontmatter.supersedes}`);
761
+ }
762
+ lines.push("---");
763
+ return lines.join("\n") + "\n";
764
+ }
765
+ function addFrontmatter(content, frontmatter) {
766
+ const { frontmatter: existing, body } = parseFrontmatter(content);
767
+ const merged = {
768
+ ...existing,
769
+ ...frontmatter,
770
+ session: {
771
+ ...existing?.session,
772
+ ...frontmatter.session
773
+ }
774
+ };
775
+ if (!merged.created) {
776
+ merged.created = (/* @__PURE__ */ new Date()).toISOString();
777
+ }
778
+ merged.updated = (/* @__PURE__ */ new Date()).toISOString();
779
+ return serializeFrontmatter(merged) + body;
780
+ }
781
+ function formatPath(filePath) {
782
+ const home = os.homedir();
783
+ if (filePath.startsWith(home)) {
784
+ return "~" + filePath.slice(home.length);
785
+ }
786
+ return filePath;
787
+ }
788
+
789
+ // src/search/graph.ts
790
+ function getLinksFrom(db, fromId, opts) {
791
+ let sql = `SELECT from_id, to_id, relation, layer, weight, source_path FROM knowledge_links WHERE from_id = ?`;
792
+ const params = [fromId];
793
+ if (opts?.relation) {
794
+ sql += ` AND relation = ?`;
795
+ params.push(opts.relation);
796
+ }
797
+ if (opts?.layer) {
798
+ sql += ` AND layer = ?`;
799
+ params.push(opts.layer);
800
+ }
801
+ const rows = db.prepare(sql).all(...params);
802
+ return rows.map(toGraphLink);
803
+ }
804
+ function getLinksTo(db, toId, opts) {
805
+ let sql = `SELECT from_id, to_id, relation, layer, weight, source_path FROM knowledge_links WHERE to_id = ?`;
806
+ const params = [toId];
807
+ if (opts?.relation) {
808
+ sql += ` AND relation = ?`;
809
+ params.push(opts.relation);
810
+ }
811
+ if (opts?.layer) {
812
+ sql += ` AND layer = ?`;
813
+ params.push(opts.layer);
814
+ }
815
+ const rows = db.prepare(sql).all(...params);
816
+ return rows.map(toGraphLink);
817
+ }
818
+ function getNeighbors(db, startId, depth = 1, opts) {
819
+ const visited = /* @__PURE__ */ new Set([startId]);
820
+ const result = [];
821
+ let frontier = [startId];
822
+ for (let d = 1; d <= depth; d++) {
823
+ const nextFrontier = [];
824
+ for (const nodeId of frontier) {
825
+ const outgoing = getLinksFrom(db, nodeId, opts);
826
+ for (const link of outgoing) {
827
+ if (!visited.has(link.toId)) {
828
+ visited.add(link.toId);
829
+ nextFrontier.push(link.toId);
830
+ result.push({ id: link.toId, depth: d, link });
831
+ }
832
+ }
833
+ const incoming = getLinksTo(db, nodeId, opts);
834
+ for (const link of incoming) {
835
+ if (!visited.has(link.fromId)) {
836
+ visited.add(link.fromId);
837
+ nextFrontier.push(link.fromId);
838
+ result.push({ id: link.fromId, depth: d, link });
839
+ }
840
+ }
841
+ }
842
+ frontier = nextFrontier;
843
+ if (frontier.length === 0) break;
844
+ }
845
+ return result;
846
+ }
847
+ function getPathBetween(db, fromId, toId, maxDepth = 3) {
848
+ if (fromId === toId) return [];
849
+ const visited = /* @__PURE__ */ new Set([fromId]);
850
+ const parentLink = /* @__PURE__ */ new Map();
851
+ let frontier = [fromId];
852
+ for (let d = 0; d < maxDepth; d++) {
853
+ const nextFrontier = [];
854
+ for (const nodeId of frontier) {
855
+ const outgoing = getLinksFrom(db, nodeId);
856
+ for (const link of outgoing) {
857
+ if (!visited.has(link.toId)) {
858
+ visited.add(link.toId);
859
+ parentLink.set(link.toId, link);
860
+ if (link.toId === toId) {
861
+ return reconstructPath(parentLink, fromId, toId);
862
+ }
863
+ nextFrontier.push(link.toId);
864
+ }
865
+ }
866
+ const incoming = getLinksTo(db, nodeId);
867
+ for (const link of incoming) {
868
+ if (!visited.has(link.fromId)) {
869
+ visited.add(link.fromId);
870
+ parentLink.set(link.fromId, link);
871
+ if (link.fromId === toId) {
872
+ return reconstructPath(parentLink, fromId, toId);
873
+ }
874
+ nextFrontier.push(link.fromId);
875
+ }
876
+ }
877
+ }
878
+ frontier = nextFrontier;
879
+ if (frontier.length === 0) break;
880
+ }
881
+ return [];
882
+ }
883
+ function reconstructPath(parentLink, fromId, toId) {
884
+ const path4 = [];
885
+ let current = toId;
886
+ while (current !== fromId) {
887
+ const link = parentLink.get(current);
888
+ if (!link) break;
889
+ path4.unshift(link);
890
+ current = link.toId === current ? link.fromId : link.toId;
891
+ }
892
+ return path4;
893
+ }
894
+ function toGraphLink(row) {
895
+ return {
896
+ fromId: row.from_id,
897
+ toId: row.to_id,
898
+ relation: row.relation,
899
+ layer: row.layer,
900
+ weight: row.weight,
901
+ sourcePath: row.source_path
902
+ };
903
+ }
904
+
905
+ // src/db/sqlite-vec.ts
906
+ async function loadSqliteVecExtension(params) {
907
+ try {
908
+ const sqliteVec = await import("sqlite-vec");
909
+ const resolvedPath = params.extensionPath?.trim() ? params.extensionPath.trim() : void 0;
910
+ const extensionPath = resolvedPath ?? sqliteVec.getLoadablePath();
911
+ params.db.enableLoadExtension(true);
912
+ if (resolvedPath) {
913
+ params.db.loadExtension(extensionPath);
914
+ } else {
915
+ sqliteVec.load(params.db);
916
+ }
917
+ return { ok: true, extensionPath };
918
+ } catch (err) {
919
+ const message = err instanceof Error ? err.message : String(err);
920
+ return { ok: false, error: message };
921
+ }
922
+ }
923
+
924
+ // src/embeddings/embeddings.ts
925
+ import fsSync2 from "fs";
926
+ import path2 from "path";
927
+ import os2 from "os";
928
+ var DEFAULT_LOCAL_MODEL = "hf:ggml-org/embeddinggemma-300M-GGUF/embeddinggemma-300M-Q8_0.gguf";
929
+ var DEFAULT_OPENAI_EMBEDDING_MODEL = "text-embedding-3-small";
930
+ var DEFAULT_OPENAI_BASE_URL = "https://api.openai.com/v1";
931
+ var DEFAULT_GEMINI_EMBEDDING_MODEL = "gemini-embedding-001";
932
+ var DEFAULT_GEMINI_BASE_URL = "https://generativelanguage.googleapis.com/v1beta";
933
+ function createNoOpEmbeddingProvider() {
934
+ return {
935
+ id: "none",
936
+ model: "bm25-only",
937
+ embedQuery: async () => [],
938
+ embedBatch: async (texts) => texts.map(() => [])
939
+ };
940
+ }
941
+ function resolveUserPath(filePath) {
942
+ if (filePath.startsWith("~/")) {
943
+ return path2.join(os2.homedir(), filePath.slice(2));
944
+ }
945
+ return filePath;
946
+ }
947
+ function canAutoSelectLocal(options) {
948
+ const modelPath = options.local?.modelPath?.trim();
949
+ if (!modelPath) return false;
950
+ if (/^(hf:|https?:)/i.test(modelPath)) return false;
951
+ const resolved = resolveUserPath(modelPath);
952
+ try {
953
+ return fsSync2.statSync(resolved).isFile();
954
+ } catch {
955
+ return false;
956
+ }
957
+ }
958
+ function isMissingApiKeyError(err) {
959
+ const message = formatError(err);
960
+ return message.includes("API key") || message.includes("apiKey");
961
+ }
962
+ async function importNodeLlamaCpp() {
963
+ const llama = await import("node-llama-cpp");
964
+ return llama;
965
+ }
966
+ async function createLocalEmbeddingProvider(options) {
967
+ const modelPath = options.local?.modelPath?.trim() || DEFAULT_LOCAL_MODEL;
968
+ const modelCacheDir = options.local?.modelCacheDir?.trim();
969
+ const { getLlama, resolveModelFile, LlamaLogLevel } = await importNodeLlamaCpp();
970
+ let llama = null;
971
+ let embeddingModel = null;
972
+ let embeddingContext = null;
973
+ const ensureContext = async () => {
974
+ if (!llama) {
975
+ llama = await getLlama({ logLevel: LlamaLogLevel.error });
976
+ }
977
+ if (!embeddingModel) {
978
+ const resolved = await resolveModelFile(modelPath, modelCacheDir || void 0);
979
+ embeddingModel = await llama.loadModel({ modelPath: resolved });
980
+ }
981
+ if (!embeddingContext) {
982
+ embeddingContext = await embeddingModel.createEmbeddingContext();
983
+ }
984
+ return embeddingContext;
985
+ };
986
+ return {
987
+ id: "local",
988
+ model: modelPath,
989
+ embedQuery: async (text) => {
990
+ const ctx = await ensureContext();
991
+ const embedding = await ctx.getEmbeddingFor(text);
992
+ return Array.from(embedding.vector);
993
+ },
994
+ embedBatch: async (texts) => {
995
+ const ctx = await ensureContext();
996
+ const embeddings = await Promise.all(
997
+ texts.map(async (text) => {
998
+ const embedding = await ctx.getEmbeddingFor(text);
999
+ return Array.from(embedding.vector);
1000
+ })
1001
+ );
1002
+ return embeddings;
1003
+ }
1004
+ };
1005
+ }
1006
+ function normalizeOpenAiModel(model) {
1007
+ const trimmed = model.trim();
1008
+ if (!trimmed) return DEFAULT_OPENAI_EMBEDDING_MODEL;
1009
+ if (trimmed.startsWith("openai/")) return trimmed.slice("openai/".length);
1010
+ return trimmed;
1011
+ }
1012
+ function resolveOpenAiApiKey(options) {
1013
+ const apiKey = options.openai?.apiKey?.trim();
1014
+ if (apiKey) return apiKey;
1015
+ const envKey = process.env.OPENAI_API_KEY?.trim();
1016
+ if (envKey) return envKey;
1017
+ throw new Error("OpenAI API key not found. Set OPENAI_API_KEY env var or pass openai.apiKey option.");
1018
+ }
1019
+ async function createOpenAiEmbeddingProvider(options) {
1020
+ const apiKey = resolveOpenAiApiKey(options);
1021
+ const baseUrl = options.openai?.baseUrl?.trim() || DEFAULT_OPENAI_BASE_URL;
1022
+ const headerOverrides = options.openai?.headers ?? {};
1023
+ const headers = {
1024
+ "Content-Type": "application/json",
1025
+ Authorization: `Bearer ${apiKey}`,
1026
+ ...headerOverrides
1027
+ };
1028
+ const model = normalizeOpenAiModel(options.model || "");
1029
+ const client = { baseUrl, headers, model };
1030
+ const url = `${baseUrl.replace(/\/$/, "")}/embeddings`;
1031
+ const embed = async (input) => {
1032
+ if (input.length === 0) return [];
1033
+ const res = await fetch(url, {
1034
+ method: "POST",
1035
+ headers: client.headers,
1036
+ body: JSON.stringify({ model: client.model, input })
1037
+ });
1038
+ if (!res.ok) {
1039
+ const text = await res.text();
1040
+ throw new Error(`openai embeddings failed: ${res.status} ${text}`);
1041
+ }
1042
+ const payload = await res.json();
1043
+ const data = payload.data ?? [];
1044
+ return data.map((entry) => entry.embedding ?? []);
1045
+ };
1046
+ return {
1047
+ provider: {
1048
+ id: "openai",
1049
+ model: client.model,
1050
+ embedQuery: async (text) => {
1051
+ const [vec] = await embed([text]);
1052
+ return vec ?? [];
1053
+ },
1054
+ embedBatch: embed
1055
+ },
1056
+ client
1057
+ };
1058
+ }
1059
+ function normalizeGeminiModel(model) {
1060
+ const trimmed = model.trim();
1061
+ if (!trimmed) return DEFAULT_GEMINI_EMBEDDING_MODEL;
1062
+ const withoutPrefix = trimmed.replace(/^models\//, "");
1063
+ if (withoutPrefix.startsWith("gemini/")) return withoutPrefix.slice("gemini/".length);
1064
+ if (withoutPrefix.startsWith("google/")) return withoutPrefix.slice("google/".length);
1065
+ return withoutPrefix;
1066
+ }
1067
+ function normalizeGeminiBaseUrl(raw) {
1068
+ const trimmed = raw.replace(/\/+$/, "");
1069
+ const openAiIndex = trimmed.indexOf("/openai");
1070
+ if (openAiIndex > -1) return trimmed.slice(0, openAiIndex);
1071
+ return trimmed;
1072
+ }
1073
+ function buildGeminiModelPath(model) {
1074
+ return model.startsWith("models/") ? model : `models/${model}`;
1075
+ }
1076
+ function resolveGeminiApiKey(options) {
1077
+ const apiKey = options.gemini?.apiKey?.trim();
1078
+ if (apiKey) return apiKey;
1079
+ const googleKey = process.env.GOOGLE_API_KEY?.trim();
1080
+ if (googleKey) return googleKey;
1081
+ const geminiKey = process.env.GEMINI_API_KEY?.trim();
1082
+ if (geminiKey) return geminiKey;
1083
+ throw new Error("Gemini API key not found. Set GOOGLE_API_KEY or GEMINI_API_KEY env var or pass gemini.apiKey option.");
1084
+ }
1085
+ async function createGeminiEmbeddingProvider(options) {
1086
+ const apiKey = resolveGeminiApiKey(options);
1087
+ const rawBaseUrl = options.gemini?.baseUrl?.trim() || DEFAULT_GEMINI_BASE_URL;
1088
+ const baseUrl = normalizeGeminiBaseUrl(rawBaseUrl);
1089
+ const headerOverrides = options.gemini?.headers ?? {};
1090
+ const headers = {
1091
+ "Content-Type": "application/json",
1092
+ "x-goog-api-key": apiKey,
1093
+ ...headerOverrides
1094
+ };
1095
+ const model = normalizeGeminiModel(options.model || "");
1096
+ const modelPath = buildGeminiModelPath(model);
1097
+ const client = { baseUrl, headers, model, modelPath };
1098
+ const embedUrl = `${baseUrl}/${modelPath}:embedContent`;
1099
+ const batchUrl = `${baseUrl}/${modelPath}:batchEmbedContents`;
1100
+ const embedQuery = async (text) => {
1101
+ if (!text.trim()) return [];
1102
+ const res = await fetch(embedUrl, {
1103
+ method: "POST",
1104
+ headers: client.headers,
1105
+ body: JSON.stringify({
1106
+ content: { parts: [{ text }] },
1107
+ taskType: "RETRIEVAL_QUERY"
1108
+ })
1109
+ });
1110
+ if (!res.ok) {
1111
+ const payload2 = await res.text();
1112
+ throw new Error(`gemini embeddings failed: ${res.status} ${payload2}`);
1113
+ }
1114
+ const payload = await res.json();
1115
+ return payload.embedding?.values ?? [];
1116
+ };
1117
+ const embedBatch = async (texts) => {
1118
+ if (texts.length === 0) return [];
1119
+ const requests = texts.map((text) => ({
1120
+ model: modelPath,
1121
+ content: { parts: [{ text }] },
1122
+ taskType: "RETRIEVAL_DOCUMENT"
1123
+ }));
1124
+ const res = await fetch(batchUrl, {
1125
+ method: "POST",
1126
+ headers: client.headers,
1127
+ body: JSON.stringify({ requests })
1128
+ });
1129
+ if (!res.ok) {
1130
+ const payload2 = await res.text();
1131
+ throw new Error(`gemini embeddings failed: ${res.status} ${payload2}`);
1132
+ }
1133
+ const payload = await res.json();
1134
+ const embeddings = Array.isArray(payload.embeddings) ? payload.embeddings : [];
1135
+ return texts.map((_, index) => embeddings[index]?.values ?? []);
1136
+ };
1137
+ return {
1138
+ provider: {
1139
+ id: "gemini",
1140
+ model: client.model,
1141
+ embedQuery,
1142
+ embedBatch
1143
+ },
1144
+ client
1145
+ };
1146
+ }
1147
+ async function createEmbeddingProvider(options) {
1148
+ const requestedProvider = options.provider;
1149
+ const fallback = options.fallback ?? "none";
1150
+ if (requestedProvider === "none") {
1151
+ return {
1152
+ provider: createNoOpEmbeddingProvider(),
1153
+ requestedProvider: "none"
1154
+ };
1155
+ }
1156
+ const createProvider = async (id) => {
1157
+ if (id === "local") {
1158
+ const provider2 = await createLocalEmbeddingProvider(options);
1159
+ return { provider: provider2 };
1160
+ }
1161
+ if (id === "gemini") {
1162
+ const { provider: provider2, client: client2 } = await createGeminiEmbeddingProvider(options);
1163
+ return { provider: provider2, gemini: client2 };
1164
+ }
1165
+ const { provider, client } = await createOpenAiEmbeddingProvider(options);
1166
+ return { provider, openAi: client };
1167
+ };
1168
+ const formatPrimaryError = (err, provider) => provider === "local" ? formatLocalSetupError(err) : formatError(err);
1169
+ if (requestedProvider === "auto") {
1170
+ const missingKeyErrors = [];
1171
+ let localError = null;
1172
+ if (canAutoSelectLocal(options)) {
1173
+ try {
1174
+ const local = await createProvider("local");
1175
+ return { ...local, requestedProvider };
1176
+ } catch (err) {
1177
+ localError = formatLocalSetupError(err);
1178
+ }
1179
+ }
1180
+ for (const provider of ["openai", "gemini"]) {
1181
+ try {
1182
+ const result = await createProvider(provider);
1183
+ return { ...result, requestedProvider };
1184
+ } catch (err) {
1185
+ const message = formatPrimaryError(err, provider);
1186
+ if (isMissingApiKeyError(err)) {
1187
+ missingKeyErrors.push(message);
1188
+ continue;
1189
+ }
1190
+ throw new Error(message);
1191
+ }
1192
+ }
1193
+ return {
1194
+ provider: createNoOpEmbeddingProvider(),
1195
+ requestedProvider,
1196
+ fallbackFrom: "auto",
1197
+ fallbackReason: "No embedding API available. Using BM25 full-text search only."
1198
+ };
1199
+ }
1200
+ try {
1201
+ const primary = await createProvider(requestedProvider);
1202
+ return { ...primary, requestedProvider };
1203
+ } catch (primaryErr) {
1204
+ const reason = formatPrimaryError(primaryErr, requestedProvider);
1205
+ if (fallback && fallback !== "none" && fallback !== requestedProvider) {
1206
+ try {
1207
+ const fallbackResult = await createProvider(fallback);
1208
+ return {
1209
+ ...fallbackResult,
1210
+ requestedProvider,
1211
+ fallbackFrom: requestedProvider,
1212
+ fallbackReason: reason
1213
+ };
1214
+ } catch (fallbackErr) {
1215
+ throw new Error(`${reason}
1216
+
1217
+ Fallback to ${fallback} failed: ${formatError(fallbackErr)}`);
1218
+ }
1219
+ }
1220
+ throw new Error(reason);
1221
+ }
1222
+ }
1223
+ function formatError(err) {
1224
+ if (err instanceof Error) return err.message;
1225
+ return String(err);
1226
+ }
1227
+ function isNodeLlamaCppMissing(err) {
1228
+ if (!(err instanceof Error)) return false;
1229
+ const code = err.code;
1230
+ if (code === "ERR_MODULE_NOT_FOUND") {
1231
+ return err.message.includes("node-llama-cpp");
1232
+ }
1233
+ return false;
1234
+ }
1235
+ function formatLocalSetupError(err) {
1236
+ const detail = formatError(err);
1237
+ const missing = isNodeLlamaCppMissing(err);
1238
+ return [
1239
+ "Local embeddings unavailable.",
1240
+ missing ? "Reason: optional dependency node-llama-cpp is missing (or failed to install)." : detail ? `Reason: ${detail}` : void 0,
1241
+ missing && detail ? `Detail: ${detail}` : null,
1242
+ "To enable local embeddings:",
1243
+ "1) Use Node 22 LTS (recommended for installs/updates)",
1244
+ missing ? "2) Install node-llama-cpp: npm install node-llama-cpp" : null,
1245
+ "3) If you use pnpm: pnpm approve-builds (select node-llama-cpp), then pnpm rebuild node-llama-cpp",
1246
+ 'Or set provider = "openai" or "gemini" (remote).'
1247
+ ].filter(Boolean).join("\n");
1248
+ }
1249
+
1250
+ // src/embeddings/batch-openai.ts
1251
+ var OPENAI_BATCH_ENDPOINT = "/v1/embeddings";
1252
+ var OPENAI_BATCH_COMPLETION_WINDOW = "24h";
1253
+ var OPENAI_BATCH_MAX_REQUESTS = 5e4;
1254
+ function getOpenAiBaseUrl(openAi) {
1255
+ return openAi.baseUrl?.replace(/\/$/, "") ?? "";
1256
+ }
1257
+ function getOpenAiHeaders(openAi, params) {
1258
+ const headers = openAi.headers ? { ...openAi.headers } : {};
1259
+ if (params.json) {
1260
+ if (!headers["Content-Type"] && !headers["content-type"]) {
1261
+ headers["Content-Type"] = "application/json";
1262
+ }
1263
+ } else {
1264
+ delete headers["Content-Type"];
1265
+ delete headers["content-type"];
1266
+ }
1267
+ return headers;
1268
+ }
1269
+ function splitOpenAiBatchRequests(requests) {
1270
+ if (requests.length <= OPENAI_BATCH_MAX_REQUESTS) return [requests];
1271
+ const groups = [];
1272
+ for (let i = 0; i < requests.length; i += OPENAI_BATCH_MAX_REQUESTS) {
1273
+ groups.push(requests.slice(i, i + OPENAI_BATCH_MAX_REQUESTS));
1274
+ }
1275
+ return groups;
1276
+ }
1277
+ async function retryAsync(fn, opts) {
1278
+ let lastError;
1279
+ for (let attempt = 0; attempt < opts.attempts; attempt++) {
1280
+ try {
1281
+ return await fn();
1282
+ } catch (err) {
1283
+ lastError = err;
1284
+ if (!opts.shouldRetry(err) || attempt === opts.attempts - 1) {
1285
+ throw err;
1286
+ }
1287
+ const delay = Math.min(
1288
+ opts.maxDelayMs,
1289
+ opts.minDelayMs * Math.pow(2, attempt) * (1 + Math.random() * opts.jitter)
1290
+ );
1291
+ await new Promise((resolve) => setTimeout(resolve, delay));
1292
+ }
1293
+ }
1294
+ throw lastError;
1295
+ }
1296
+ async function submitOpenAiBatch(params) {
1297
+ const baseUrl = getOpenAiBaseUrl(params.openAi);
1298
+ const jsonl = params.requests.map((request) => JSON.stringify(request)).join("\n");
1299
+ const form = new FormData();
1300
+ form.append("purpose", "batch");
1301
+ form.append(
1302
+ "file",
1303
+ new Blob([jsonl], { type: "application/jsonl" }),
1304
+ `memory-embeddings.${hashText(String(Date.now()))}.jsonl`
1305
+ );
1306
+ const fileRes = await fetch(`${baseUrl}/files`, {
1307
+ method: "POST",
1308
+ headers: getOpenAiHeaders(params.openAi, { json: false }),
1309
+ body: form
1310
+ });
1311
+ if (!fileRes.ok) {
1312
+ const text = await fileRes.text();
1313
+ throw new Error(`openai batch file upload failed: ${fileRes.status} ${text}`);
1314
+ }
1315
+ const filePayload = await fileRes.json();
1316
+ if (!filePayload.id) {
1317
+ throw new Error("openai batch file upload failed: missing file id");
1318
+ }
1319
+ const batchRes = await retryAsync(
1320
+ async () => {
1321
+ const res = await fetch(`${baseUrl}/batches`, {
1322
+ method: "POST",
1323
+ headers: getOpenAiHeaders(params.openAi, { json: true }),
1324
+ body: JSON.stringify({
1325
+ input_file_id: filePayload.id,
1326
+ endpoint: OPENAI_BATCH_ENDPOINT,
1327
+ completion_window: OPENAI_BATCH_COMPLETION_WINDOW,
1328
+ metadata: {
1329
+ source: params.source
1330
+ }
1331
+ })
1332
+ });
1333
+ if (!res.ok) {
1334
+ const text = await res.text();
1335
+ const err = new Error(`openai batch create failed: ${res.status} ${text}`);
1336
+ err.status = res.status;
1337
+ throw err;
1338
+ }
1339
+ return res;
1340
+ },
1341
+ {
1342
+ attempts: 3,
1343
+ minDelayMs: 300,
1344
+ maxDelayMs: 2e3,
1345
+ jitter: 0.2,
1346
+ shouldRetry: (err) => {
1347
+ const status = err.status;
1348
+ return status === 429 || typeof status === "number" && status >= 500;
1349
+ }
1350
+ }
1351
+ );
1352
+ return await batchRes.json();
1353
+ }
1354
+ async function fetchOpenAiBatchStatus(params) {
1355
+ const baseUrl = getOpenAiBaseUrl(params.openAi);
1356
+ const res = await fetch(`${baseUrl}/batches/${params.batchId}`, {
1357
+ headers: getOpenAiHeaders(params.openAi, { json: true })
1358
+ });
1359
+ if (!res.ok) {
1360
+ const text = await res.text();
1361
+ throw new Error(`openai batch status failed: ${res.status} ${text}`);
1362
+ }
1363
+ return await res.json();
1364
+ }
1365
+ async function fetchOpenAiFileContent(params) {
1366
+ const baseUrl = getOpenAiBaseUrl(params.openAi);
1367
+ const res = await fetch(`${baseUrl}/files/${params.fileId}/content`, {
1368
+ headers: getOpenAiHeaders(params.openAi, { json: true })
1369
+ });
1370
+ if (!res.ok) {
1371
+ const text = await res.text();
1372
+ throw new Error(`openai batch file content failed: ${res.status} ${text}`);
1373
+ }
1374
+ return await res.text();
1375
+ }
1376
+ function parseOpenAiBatchOutput(text) {
1377
+ if (!text.trim()) return [];
1378
+ return text.split("\n").map((line) => line.trim()).filter(Boolean).map((line) => JSON.parse(line));
1379
+ }
1380
+ async function readOpenAiBatchError(params) {
1381
+ try {
1382
+ const content = await fetchOpenAiFileContent({
1383
+ openAi: params.openAi,
1384
+ fileId: params.errorFileId
1385
+ });
1386
+ const lines = parseOpenAiBatchOutput(content);
1387
+ const first = lines.find((line) => line.error?.message || line.response?.body?.error);
1388
+ const message = first?.error?.message ?? (typeof first?.response?.body?.error?.message === "string" ? first?.response?.body?.error?.message : void 0);
1389
+ return message;
1390
+ } catch (err) {
1391
+ const message = err instanceof Error ? err.message : String(err);
1392
+ return message ? `error file unavailable: ${message}` : void 0;
1393
+ }
1394
+ }
1395
+ async function waitForOpenAiBatch(params) {
1396
+ const start = Date.now();
1397
+ let current = params.initial;
1398
+ while (true) {
1399
+ const status = current ?? await fetchOpenAiBatchStatus({
1400
+ openAi: params.openAi,
1401
+ batchId: params.batchId
1402
+ });
1403
+ const state = status.status ?? "unknown";
1404
+ if (state === "completed") {
1405
+ if (!status.output_file_id) {
1406
+ throw new Error(`openai batch ${params.batchId} completed without output file`);
1407
+ }
1408
+ return {
1409
+ outputFileId: status.output_file_id,
1410
+ errorFileId: status.error_file_id ?? void 0
1411
+ };
1412
+ }
1413
+ if (["failed", "expired", "cancelled", "canceled"].includes(state)) {
1414
+ const detail = status.error_file_id ? await readOpenAiBatchError({ openAi: params.openAi, errorFileId: status.error_file_id }) : void 0;
1415
+ const suffix = detail ? `: ${detail}` : "";
1416
+ throw new Error(`openai batch ${params.batchId} ${state}${suffix}`);
1417
+ }
1418
+ if (!params.wait) {
1419
+ throw new Error(`openai batch ${params.batchId} still ${state}; wait disabled`);
1420
+ }
1421
+ if (Date.now() - start > params.timeoutMs) {
1422
+ throw new Error(`openai batch ${params.batchId} timed out after ${params.timeoutMs}ms`);
1423
+ }
1424
+ params.debug?.(`openai batch ${params.batchId} ${state}; waiting ${params.pollIntervalMs}ms`);
1425
+ await new Promise((resolve) => setTimeout(resolve, params.pollIntervalMs));
1426
+ current = void 0;
1427
+ }
1428
+ }
1429
+ async function runWithConcurrency(tasks, limit) {
1430
+ if (tasks.length === 0) return [];
1431
+ const resolvedLimit = Math.max(1, Math.min(limit, tasks.length));
1432
+ const results = Array.from({ length: tasks.length });
1433
+ let next = 0;
1434
+ let firstError = null;
1435
+ const workers = Array.from({ length: resolvedLimit }, async () => {
1436
+ while (true) {
1437
+ if (firstError) return;
1438
+ const index = next;
1439
+ next += 1;
1440
+ if (index >= tasks.length) return;
1441
+ try {
1442
+ results[index] = await tasks[index]();
1443
+ } catch (err) {
1444
+ firstError = err;
1445
+ return;
1446
+ }
1447
+ }
1448
+ });
1449
+ await Promise.allSettled(workers);
1450
+ if (firstError) throw firstError;
1451
+ return results;
1452
+ }
1453
+ async function runOpenAiEmbeddingBatches(params) {
1454
+ if (params.requests.length === 0) return /* @__PURE__ */ new Map();
1455
+ const groups = splitOpenAiBatchRequests(params.requests);
1456
+ const byCustomId = /* @__PURE__ */ new Map();
1457
+ const tasks = groups.map((group, groupIndex) => async () => {
1458
+ const batchInfo = await submitOpenAiBatch({
1459
+ openAi: params.openAi,
1460
+ requests: group,
1461
+ source: params.source
1462
+ });
1463
+ if (!batchInfo.id) {
1464
+ throw new Error("openai batch create failed: missing batch id");
1465
+ }
1466
+ params.debug?.("memory embeddings: openai batch created", {
1467
+ batchId: batchInfo.id,
1468
+ status: batchInfo.status,
1469
+ group: groupIndex + 1,
1470
+ groups: groups.length,
1471
+ requests: group.length
1472
+ });
1473
+ if (!params.wait && batchInfo.status !== "completed") {
1474
+ throw new Error(
1475
+ `openai batch ${batchInfo.id} submitted; enable batch.wait to await completion`
1476
+ );
1477
+ }
1478
+ const completed = batchInfo.status === "completed" ? {
1479
+ outputFileId: batchInfo.output_file_id ?? "",
1480
+ errorFileId: batchInfo.error_file_id ?? void 0
1481
+ } : await waitForOpenAiBatch({
1482
+ openAi: params.openAi,
1483
+ batchId: batchInfo.id,
1484
+ wait: params.wait,
1485
+ pollIntervalMs: params.pollIntervalMs,
1486
+ timeoutMs: params.timeoutMs,
1487
+ debug: params.debug,
1488
+ initial: batchInfo
1489
+ });
1490
+ if (!completed.outputFileId) {
1491
+ throw new Error(`openai batch ${batchInfo.id} completed without output file`);
1492
+ }
1493
+ const content = await fetchOpenAiFileContent({
1494
+ openAi: params.openAi,
1495
+ fileId: completed.outputFileId
1496
+ });
1497
+ const outputLines = parseOpenAiBatchOutput(content);
1498
+ const errors = [];
1499
+ const remaining = new Set(group.map((request) => request.custom_id));
1500
+ for (const line of outputLines) {
1501
+ const customId = line.custom_id;
1502
+ if (!customId) continue;
1503
+ remaining.delete(customId);
1504
+ if (line.error?.message) {
1505
+ errors.push(`${customId}: ${line.error.message}`);
1506
+ continue;
1507
+ }
1508
+ const response = line.response;
1509
+ const statusCode = response?.status_code ?? 0;
1510
+ if (statusCode >= 400) {
1511
+ const message = response?.body?.error?.message ?? (typeof response?.body === "string" ? response.body : void 0) ?? "unknown error";
1512
+ errors.push(`${customId}: ${message}`);
1513
+ continue;
1514
+ }
1515
+ const data = response?.body?.data ?? [];
1516
+ const embedding = data[0]?.embedding ?? [];
1517
+ if (embedding.length === 0) {
1518
+ errors.push(`${customId}: empty embedding`);
1519
+ continue;
1520
+ }
1521
+ byCustomId.set(customId, embedding);
1522
+ }
1523
+ if (errors.length > 0) {
1524
+ throw new Error(`openai batch ${batchInfo.id} failed: ${errors.join("; ")}`);
1525
+ }
1526
+ if (remaining.size > 0) {
1527
+ throw new Error(`openai batch ${batchInfo.id} missing ${remaining.size} embedding responses`);
1528
+ }
1529
+ });
1530
+ params.debug?.("memory embeddings: openai batch submit", {
1531
+ requests: params.requests.length,
1532
+ groups: groups.length,
1533
+ wait: params.wait,
1534
+ concurrency: params.concurrency,
1535
+ pollIntervalMs: params.pollIntervalMs,
1536
+ timeoutMs: params.timeoutMs
1537
+ });
1538
+ await runWithConcurrency(tasks, params.concurrency);
1539
+ return byCustomId;
1540
+ }
1541
+
1542
+ // src/embeddings/batch-gemini.ts
1543
+ var GEMINI_BATCH_MAX_REQUESTS = 5e4;
1544
+ function getGeminiBaseUrl(gemini) {
1545
+ return gemini.baseUrl?.replace(/\/$/, "") ?? "";
1546
+ }
1547
+ function getGeminiHeaders(gemini, params) {
1548
+ const headers = gemini.headers ? { ...gemini.headers } : {};
1549
+ if (params.json) {
1550
+ if (!headers["Content-Type"] && !headers["content-type"]) {
1551
+ headers["Content-Type"] = "application/json";
1552
+ }
1553
+ } else {
1554
+ delete headers["Content-Type"];
1555
+ delete headers["content-type"];
1556
+ }
1557
+ return headers;
1558
+ }
1559
+ function getGeminiUploadUrl(baseUrl) {
1560
+ if (baseUrl.includes("/v1beta")) {
1561
+ return baseUrl.replace(/\/v1beta\/?$/, "/upload/v1beta");
1562
+ }
1563
+ return `${baseUrl.replace(/\/$/, "")}/upload`;
1564
+ }
1565
+ function splitGeminiBatchRequests(requests) {
1566
+ if (requests.length <= GEMINI_BATCH_MAX_REQUESTS) return [requests];
1567
+ const groups = [];
1568
+ for (let i = 0; i < requests.length; i += GEMINI_BATCH_MAX_REQUESTS) {
1569
+ groups.push(requests.slice(i, i + GEMINI_BATCH_MAX_REQUESTS));
1570
+ }
1571
+ return groups;
1572
+ }
1573
+ function buildGeminiUploadBody(params) {
1574
+ const boundary = `minimem-${hashText(params.displayName)}`;
1575
+ const jsonPart = JSON.stringify({
1576
+ file: {
1577
+ displayName: params.displayName,
1578
+ mimeType: "application/jsonl"
1579
+ }
1580
+ });
1581
+ const delimiter = `--${boundary}\r
1582
+ `;
1583
+ const closeDelimiter = `--${boundary}--\r
1584
+ `;
1585
+ const parts = [
1586
+ `${delimiter}Content-Type: application/json; charset=UTF-8\r
1587
+ \r
1588
+ ${jsonPart}\r
1589
+ `,
1590
+ `${delimiter}Content-Type: application/jsonl; charset=UTF-8\r
1591
+ \r
1592
+ ${params.jsonl}\r
1593
+ `,
1594
+ closeDelimiter
1595
+ ];
1596
+ const body = new Blob([parts.join("")], { type: "multipart/related" });
1597
+ return {
1598
+ body,
1599
+ contentType: `multipart/related; boundary=${boundary}`
1600
+ };
1601
+ }
1602
+ async function submitGeminiBatch(params) {
1603
+ const baseUrl = getGeminiBaseUrl(params.gemini);
1604
+ const jsonl = params.requests.map(
1605
+ (request) => JSON.stringify({
1606
+ key: request.custom_id,
1607
+ request: {
1608
+ content: request.content,
1609
+ task_type: request.taskType
1610
+ }
1611
+ })
1612
+ ).join("\n");
1613
+ const displayName = `memory-embeddings-${hashText(String(Date.now()))}`;
1614
+ const uploadPayload = buildGeminiUploadBody({ jsonl, displayName });
1615
+ const uploadUrl = `${getGeminiUploadUrl(baseUrl)}/files?uploadType=multipart`;
1616
+ const fileRes = await fetch(uploadUrl, {
1617
+ method: "POST",
1618
+ headers: {
1619
+ ...getGeminiHeaders(params.gemini, { json: false }),
1620
+ "Content-Type": uploadPayload.contentType
1621
+ },
1622
+ body: uploadPayload.body
1623
+ });
1624
+ if (!fileRes.ok) {
1625
+ const text2 = await fileRes.text();
1626
+ throw new Error(`gemini batch file upload failed: ${fileRes.status} ${text2}`);
1627
+ }
1628
+ const filePayload = await fileRes.json();
1629
+ const fileId = filePayload.name ?? filePayload.file?.name;
1630
+ if (!fileId) {
1631
+ throw new Error("gemini batch file upload failed: missing file id");
1632
+ }
1633
+ const batchBody = {
1634
+ batch: {
1635
+ displayName: `memory-embeddings-${params.source}`,
1636
+ inputConfig: {
1637
+ file_name: fileId
1638
+ }
1639
+ }
1640
+ };
1641
+ const batchEndpoint = `${baseUrl}/${params.gemini.modelPath}:asyncBatchEmbedContent`;
1642
+ const batchRes = await fetch(batchEndpoint, {
1643
+ method: "POST",
1644
+ headers: getGeminiHeaders(params.gemini, { json: true }),
1645
+ body: JSON.stringify(batchBody)
1646
+ });
1647
+ if (batchRes.ok) {
1648
+ return await batchRes.json();
1649
+ }
1650
+ const text = await batchRes.text();
1651
+ if (batchRes.status === 404) {
1652
+ throw new Error(
1653
+ "gemini batch create failed: 404 (asyncBatchEmbedContent not available for this model/baseUrl). Disable batch.enabled or switch providers."
1654
+ );
1655
+ }
1656
+ throw new Error(`gemini batch create failed: ${batchRes.status} ${text}`);
1657
+ }
1658
+ async function fetchGeminiBatchStatus(params) {
1659
+ const baseUrl = getGeminiBaseUrl(params.gemini);
1660
+ const name = params.batchName.startsWith("batches/") ? params.batchName : `batches/${params.batchName}`;
1661
+ const statusUrl = `${baseUrl}/${name}`;
1662
+ const res = await fetch(statusUrl, {
1663
+ headers: getGeminiHeaders(params.gemini, { json: true })
1664
+ });
1665
+ if (!res.ok) {
1666
+ const text = await res.text();
1667
+ throw new Error(`gemini batch status failed: ${res.status} ${text}`);
1668
+ }
1669
+ return await res.json();
1670
+ }
1671
+ async function fetchGeminiFileContent(params) {
1672
+ const baseUrl = getGeminiBaseUrl(params.gemini);
1673
+ const file = params.fileId.startsWith("files/") ? params.fileId : `files/${params.fileId}`;
1674
+ const downloadUrl = `${baseUrl}/${file}:download`;
1675
+ const res = await fetch(downloadUrl, {
1676
+ headers: getGeminiHeaders(params.gemini, { json: true })
1677
+ });
1678
+ if (!res.ok) {
1679
+ const text = await res.text();
1680
+ throw new Error(`gemini batch file content failed: ${res.status} ${text}`);
1681
+ }
1682
+ return await res.text();
1683
+ }
1684
+ function parseGeminiBatchOutput(text) {
1685
+ if (!text.trim()) return [];
1686
+ return text.split("\n").map((line) => line.trim()).filter(Boolean).map((line) => JSON.parse(line));
1687
+ }
1688
+ async function waitForGeminiBatch(params) {
1689
+ const start = Date.now();
1690
+ let current = params.initial;
1691
+ while (true) {
1692
+ const status = current ?? await fetchGeminiBatchStatus({
1693
+ gemini: params.gemini,
1694
+ batchName: params.batchName
1695
+ });
1696
+ const state = status.state ?? "UNKNOWN";
1697
+ if (["SUCCEEDED", "COMPLETED", "DONE"].includes(state)) {
1698
+ const outputFileId = status.outputConfig?.file ?? status.outputConfig?.fileId ?? status.metadata?.output?.responsesFile;
1699
+ if (!outputFileId) {
1700
+ throw new Error(`gemini batch ${params.batchName} completed without output file`);
1701
+ }
1702
+ return { outputFileId };
1703
+ }
1704
+ if (["FAILED", "CANCELLED", "CANCELED", "EXPIRED"].includes(state)) {
1705
+ const message = status.error?.message ?? "unknown error";
1706
+ throw new Error(`gemini batch ${params.batchName} ${state}: ${message}`);
1707
+ }
1708
+ if (!params.wait) {
1709
+ throw new Error(`gemini batch ${params.batchName} still ${state}; wait disabled`);
1710
+ }
1711
+ if (Date.now() - start > params.timeoutMs) {
1712
+ throw new Error(`gemini batch ${params.batchName} timed out after ${params.timeoutMs}ms`);
1713
+ }
1714
+ params.debug?.(`gemini batch ${params.batchName} ${state}; waiting ${params.pollIntervalMs}ms`);
1715
+ await new Promise((resolve) => setTimeout(resolve, params.pollIntervalMs));
1716
+ current = void 0;
1717
+ }
1718
+ }
1719
+ async function runWithConcurrency2(tasks, limit) {
1720
+ if (tasks.length === 0) return [];
1721
+ const resolvedLimit = Math.max(1, Math.min(limit, tasks.length));
1722
+ const results = Array.from({ length: tasks.length });
1723
+ let next = 0;
1724
+ let firstError = null;
1725
+ const workers = Array.from({ length: resolvedLimit }, async () => {
1726
+ while (true) {
1727
+ if (firstError) return;
1728
+ const index = next;
1729
+ next += 1;
1730
+ if (index >= tasks.length) return;
1731
+ try {
1732
+ results[index] = await tasks[index]();
1733
+ } catch (err) {
1734
+ firstError = err;
1735
+ return;
1736
+ }
1737
+ }
1738
+ });
1739
+ await Promise.allSettled(workers);
1740
+ if (firstError) throw firstError;
1741
+ return results;
1742
+ }
1743
+ async function runGeminiEmbeddingBatches(params) {
1744
+ if (params.requests.length === 0) return /* @__PURE__ */ new Map();
1745
+ const groups = splitGeminiBatchRequests(params.requests);
1746
+ const byCustomId = /* @__PURE__ */ new Map();
1747
+ const tasks = groups.map((group, groupIndex) => async () => {
1748
+ const batchInfo = await submitGeminiBatch({
1749
+ gemini: params.gemini,
1750
+ requests: group,
1751
+ source: params.source
1752
+ });
1753
+ const batchName = batchInfo.name ?? "";
1754
+ if (!batchName) {
1755
+ throw new Error("gemini batch create failed: missing batch name");
1756
+ }
1757
+ params.debug?.("memory embeddings: gemini batch created", {
1758
+ batchName,
1759
+ state: batchInfo.state,
1760
+ group: groupIndex + 1,
1761
+ groups: groups.length,
1762
+ requests: group.length
1763
+ });
1764
+ if (!params.wait && batchInfo.state && !["SUCCEEDED", "COMPLETED", "DONE"].includes(batchInfo.state)) {
1765
+ throw new Error(
1766
+ `gemini batch ${batchName} submitted; enable batch.wait to await completion`
1767
+ );
1768
+ }
1769
+ const completed = batchInfo.state && ["SUCCEEDED", "COMPLETED", "DONE"].includes(batchInfo.state) ? {
1770
+ outputFileId: batchInfo.outputConfig?.file ?? batchInfo.outputConfig?.fileId ?? batchInfo.metadata?.output?.responsesFile ?? ""
1771
+ } : await waitForGeminiBatch({
1772
+ gemini: params.gemini,
1773
+ batchName,
1774
+ wait: params.wait,
1775
+ pollIntervalMs: params.pollIntervalMs,
1776
+ timeoutMs: params.timeoutMs,
1777
+ debug: params.debug,
1778
+ initial: batchInfo
1779
+ });
1780
+ if (!completed.outputFileId) {
1781
+ throw new Error(`gemini batch ${batchName} completed without output file`);
1782
+ }
1783
+ const content = await fetchGeminiFileContent({
1784
+ gemini: params.gemini,
1785
+ fileId: completed.outputFileId
1786
+ });
1787
+ const outputLines = parseGeminiBatchOutput(content);
1788
+ const errors = [];
1789
+ const remaining = new Set(group.map((request) => request.custom_id));
1790
+ for (const line of outputLines) {
1791
+ const customId = line.key ?? line.custom_id ?? line.request_id;
1792
+ if (!customId) continue;
1793
+ remaining.delete(customId);
1794
+ if (line.error?.message) {
1795
+ errors.push(`${customId}: ${line.error.message}`);
1796
+ continue;
1797
+ }
1798
+ if (line.response?.error?.message) {
1799
+ errors.push(`${customId}: ${line.response.error.message}`);
1800
+ continue;
1801
+ }
1802
+ const embedding = line.embedding?.values ?? line.response?.embedding?.values ?? [];
1803
+ if (embedding.length === 0) {
1804
+ errors.push(`${customId}: empty embedding`);
1805
+ continue;
1806
+ }
1807
+ byCustomId.set(customId, embedding);
1808
+ }
1809
+ if (errors.length > 0) {
1810
+ throw new Error(`gemini batch ${batchName} failed: ${errors.join("; ")}`);
1811
+ }
1812
+ if (remaining.size > 0) {
1813
+ throw new Error(`gemini batch ${batchName} missing ${remaining.size} embedding responses`);
1814
+ }
1815
+ });
1816
+ params.debug?.("memory embeddings: gemini batch submit", {
1817
+ requests: params.requests.length,
1818
+ groups: groups.length,
1819
+ wait: params.wait,
1820
+ concurrency: params.concurrency,
1821
+ pollIntervalMs: params.pollIntervalMs,
1822
+ timeoutMs: params.timeoutMs
1823
+ });
1824
+ await runWithConcurrency2(tasks, params.concurrency);
1825
+ return byCustomId;
1826
+ }
1827
+
1828
+ // src/minimem.ts
1829
+ function resolveMinimemSubdir(memoryDir) {
1830
+ const envDir = process.env.MINIMEM_CONFIG_DIR;
1831
+ if (envDir) return envDir;
1832
+ if (fsSync3.existsSync(path3.join(memoryDir, "config.json"))) return ".";
1833
+ const swarmDir = path3.join(memoryDir, ".swarm", "minimem");
1834
+ if (fsSync3.existsSync(path3.join(swarmDir, "config.json"))) return path3.join(".swarm", "minimem");
1835
+ return ".minimem";
1836
+ }
1837
+ var META_KEY = "memory_index_meta_v1";
1838
+ var SNIPPET_MAX_CHARS = 700;
1839
+ var VECTOR_TABLE = "chunks_vec";
1840
+ var FTS_TABLE = "chunks_fts";
1841
+ var EMBEDDING_CACHE_TABLE = "embedding_cache";
1842
+ var EMBEDDING_RETRY_MAX_ATTEMPTS = 3;
1843
+ var EMBEDDING_RETRY_BASE_DELAY_MS = 500;
1844
+ var EMBEDDING_RETRY_MAX_DELAY_MS = 8e3;
1845
+ var EMBEDDING_QUERY_TIMEOUT_REMOTE_MS = 6e4;
1846
+ var EMBEDDING_QUERY_TIMEOUT_LOCAL_MS = 5 * 6e4;
1847
+ var Minimem = class _Minimem {
1848
+ memoryDir;
1849
+ dbPath;
1850
+ chunking;
1851
+ cache;
1852
+ hybrid;
1853
+ queryConfig;
1854
+ watchConfig;
1855
+ batchConfig;
1856
+ vectorExtensionPath;
1857
+ debug;
1858
+ provider;
1859
+ openAi;
1860
+ gemini;
1861
+ providerKey = "";
1862
+ providerFallbackReason;
1863
+ db;
1864
+ vector;
1865
+ fts;
1866
+ vectorReady = null;
1867
+ watcher = null;
1868
+ watchTimer = null;
1869
+ closed = false;
1870
+ dirty = true;
1871
+ syncing = null;
1872
+ syncLock = false;
1873
+ embeddingOptions;
1874
+ constructor(config) {
1875
+ this.memoryDir = path3.resolve(config.memoryDir);
1876
+ this.dbPath = config.dbPath ?? path3.join(this.memoryDir, resolveMinimemSubdir(this.memoryDir), "index.db");
1877
+ this.chunking = {
1878
+ tokens: config.chunking?.tokens ?? 256,
1879
+ overlap: config.chunking?.overlap ?? 32
1880
+ };
1881
+ this.cache = {
1882
+ enabled: config.cache?.enabled ?? true,
1883
+ maxEntries: config.cache?.maxEntries ?? 1e4
1884
+ };
1885
+ this.hybrid = {
1886
+ enabled: config.hybrid?.enabled ?? true,
1887
+ vectorWeight: config.hybrid?.vectorWeight ?? 0.7,
1888
+ textWeight: config.hybrid?.textWeight ?? 0.3,
1889
+ candidateMultiplier: config.hybrid?.candidateMultiplier ?? 2
1890
+ };
1891
+ this.queryConfig = {
1892
+ maxResults: config.query?.maxResults ?? 10,
1893
+ minScore: config.query?.minScore ?? 0.3
1894
+ };
1895
+ this.watchConfig = {
1896
+ enabled: config.watch?.enabled ?? true,
1897
+ debounceMs: config.watch?.debounceMs ?? 1e3
1898
+ };
1899
+ this.batchConfig = {
1900
+ enabled: config.batch?.enabled ?? false,
1901
+ wait: config.batch?.wait ?? true,
1902
+ concurrency: config.batch?.concurrency ?? 2,
1903
+ pollIntervalMs: config.batch?.pollIntervalMs ?? 2e3,
1904
+ timeoutMs: config.batch?.timeoutMs ?? 60 * 60 * 1e3
1905
+ };
1906
+ this.vectorExtensionPath = config.vectorExtensionPath;
1907
+ this.debug = config.debug;
1908
+ this.embeddingOptions = config.embedding;
1909
+ this.vector = {
1910
+ enabled: true,
1911
+ available: null,
1912
+ extensionPath: this.vectorExtensionPath
1913
+ };
1914
+ this.fts = { enabled: this.hybrid.enabled, available: false };
1915
+ }
1916
+ static async create(config) {
1917
+ const instance = new _Minimem(config);
1918
+ await instance.initialize();
1919
+ return instance;
1920
+ }
1921
+ async initialize() {
1922
+ const providerResult = await createEmbeddingProvider(this.embeddingOptions);
1923
+ this.provider = providerResult.provider;
1924
+ this.openAi = providerResult.openAi;
1925
+ this.gemini = providerResult.gemini;
1926
+ this.providerKey = this.computeProviderKey();
1927
+ this.providerFallbackReason = providerResult.fallbackReason;
1928
+ if (this.provider.id === "none") {
1929
+ this.debug?.("Running in BM25-only mode (no embedding API available)");
1930
+ }
1931
+ this.db = this.openDatabase();
1932
+ this.ensureSchema();
1933
+ const meta = this.readMeta();
1934
+ if (meta?.vectorDims) {
1935
+ this.vector.dims = meta.vectorDims;
1936
+ }
1937
+ if (this.watchConfig.enabled) {
1938
+ this.ensureWatcher();
1939
+ }
1940
+ }
1941
+ openDatabase() {
1942
+ const dbDir = path3.dirname(this.dbPath);
1943
+ ensureDir(dbDir);
1944
+ return new DatabaseSync(this.dbPath);
1945
+ }
1946
+ ensureSchema() {
1947
+ const result = ensureMemoryIndexSchema({
1948
+ db: this.db,
1949
+ embeddingCacheTable: EMBEDDING_CACHE_TABLE,
1950
+ ftsTable: FTS_TABLE,
1951
+ ftsEnabled: this.fts.enabled
1952
+ });
1953
+ this.fts.available = result.ftsAvailable;
1954
+ if (result.ftsError) {
1955
+ this.fts.loadError = result.ftsError;
1956
+ }
1957
+ }
1958
+ computeProviderKey() {
1959
+ const parts = [this.provider.id, this.provider.model];
1960
+ if (this.openAi) {
1961
+ parts.push(this.openAi.baseUrl);
1962
+ }
1963
+ if (this.gemini) {
1964
+ parts.push(this.gemini.baseUrl);
1965
+ }
1966
+ return hashText(parts.join(":"));
1967
+ }
1968
+ readMeta() {
1969
+ try {
1970
+ const row = this.db.prepare(`SELECT value FROM meta WHERE key = ?`).get(META_KEY);
1971
+ if (!row?.value) return null;
1972
+ return JSON.parse(row.value);
1973
+ } catch {
1974
+ return null;
1975
+ }
1976
+ }
1977
+ writeMeta(meta) {
1978
+ this.db.prepare(`INSERT OR REPLACE INTO meta (key, value) VALUES (?, ?)`).run(META_KEY, JSON.stringify(meta));
1979
+ }
1980
+ ensureWatcher() {
1981
+ if (this.watcher) return;
1982
+ const memorySubDir = path3.join(this.memoryDir, "memory");
1983
+ const memoryFile = path3.join(this.memoryDir, "MEMORY.md");
1984
+ this.watcher = chokidar.watch([memoryFile, memorySubDir], {
1985
+ ignoreInitial: true,
1986
+ persistent: true,
1987
+ awaitWriteFinish: { stabilityThreshold: 200, pollInterval: 50 }
1988
+ });
1989
+ const scheduleSync = () => {
1990
+ this.dirty = true;
1991
+ if (this.watchTimer) clearTimeout(this.watchTimer);
1992
+ this.watchTimer = setTimeout(() => {
1993
+ void this.sync({ reason: "watch" }).catch((err) => {
1994
+ this.debug?.(`memory sync failed (watch): ${String(err)}`);
1995
+ });
1996
+ }, this.watchConfig.debounceMs);
1997
+ };
1998
+ this.watcher.on("add", scheduleSync);
1999
+ this.watcher.on("change", scheduleSync);
2000
+ this.watcher.on("unlink", scheduleSync);
2001
+ }
2002
+ /**
2003
+ * Check if the index is stale by comparing file mtimes against stored values.
2004
+ * This is a lightweight check (stat calls only, no file reads).
2005
+ */
2006
+ async isStale() {
2007
+ try {
2008
+ const files = await listMemoryFiles(this.memoryDir);
2009
+ const stored = this.db.prepare(`SELECT path, mtime FROM files WHERE source = ?`).all("memory");
2010
+ if (files.length !== stored.length) {
2011
+ this.debug?.(`Stale: file count changed (${stored.length} -> ${files.length})`);
2012
+ return true;
2013
+ }
2014
+ const storedMap = new Map(stored.map((f) => [f.path, f.mtime]));
2015
+ for (const absPath of files) {
2016
+ const relPath = path3.relative(this.memoryDir, absPath).replace(/\\/g, "/");
2017
+ const storedMtime = storedMap.get(relPath);
2018
+ if (storedMtime === void 0) {
2019
+ this.debug?.(`Stale: new file ${relPath}`);
2020
+ return true;
2021
+ }
2022
+ const stat = await fs2.stat(absPath);
2023
+ const currentMtime = Math.floor(stat.mtimeMs);
2024
+ if (currentMtime !== storedMtime) {
2025
+ this.debug?.(`Stale: mtime changed for ${relPath}`);
2026
+ return true;
2027
+ }
2028
+ }
2029
+ return false;
2030
+ } catch (err) {
2031
+ this.debug?.(`Stale check failed: ${String(err)}`);
2032
+ return true;
2033
+ }
2034
+ }
2035
+ async search(query, opts) {
2036
+ if (this.dirty || !this.watchConfig.enabled && await this.isStale()) {
2037
+ await this.sync({ reason: "search" });
2038
+ }
2039
+ const cleaned = query.trim();
2040
+ if (!cleaned) return [];
2041
+ const minScore = opts?.minScore ?? this.queryConfig.minScore;
2042
+ const maxResults = opts?.maxResults ?? this.queryConfig.maxResults;
2043
+ const candidates = Math.min(
2044
+ 200,
2045
+ Math.max(1, Math.floor(maxResults * this.hybrid.candidateMultiplier))
2046
+ );
2047
+ const sourceFilter = { sql: "", params: [] };
2048
+ const keywordResults = this.hybrid.enabled && this.fts.available ? await searchKeyword({
2049
+ db: this.db,
2050
+ ftsTable: FTS_TABLE,
2051
+ providerModel: this.provider.model,
2052
+ query: cleaned,
2053
+ limit: candidates,
2054
+ snippetMaxChars: SNIPPET_MAX_CHARS,
2055
+ sourceFilter,
2056
+ buildFtsQuery,
2057
+ bm25RankToScore
2058
+ }).catch(() => []) : [];
2059
+ const queryVec = await this.embedQueryWithTimeout(cleaned);
2060
+ const hasVector = queryVec.some((v) => v !== 0);
2061
+ const vectorResults = hasVector ? await searchVector({
2062
+ db: this.db,
2063
+ vectorTable: VECTOR_TABLE,
2064
+ providerModel: this.provider.model,
2065
+ queryVec,
2066
+ limit: candidates,
2067
+ snippetMaxChars: SNIPPET_MAX_CHARS,
2068
+ ensureVectorReady: (dims) => this.ensureVectorReady(dims),
2069
+ sourceFilterVec: sourceFilter,
2070
+ sourceFilterChunks: sourceFilter
2071
+ }).catch(() => []) : [];
2072
+ const typeFilterFn = opts?.type ? (id) => {
2073
+ const row = this.db.prepare(`SELECT type FROM chunks WHERE id = ?`).get(id);
2074
+ return row?.type === opts.type;
2075
+ } : void 0;
2076
+ if (!this.hybrid.enabled) {
2077
+ let results = vectorResults;
2078
+ if (typeFilterFn) results = results.filter((r) => typeFilterFn(r.id));
2079
+ return results.filter((entry) => entry.score >= minScore).slice(0, maxResults).map((r) => ({
2080
+ path: r.path,
2081
+ startLine: r.startLine,
2082
+ endLine: r.endLine,
2083
+ score: r.score,
2084
+ snippet: r.snippet
2085
+ }));
2086
+ }
2087
+ let filteredVector = vectorResults;
2088
+ let filteredKeyword = keywordResults;
2089
+ if (typeFilterFn) {
2090
+ filteredVector = vectorResults.filter((r) => typeFilterFn(r.id));
2091
+ filteredKeyword = keywordResults.filter((r) => typeFilterFn(r.id));
2092
+ }
2093
+ const merged = mergeHybridResults({
2094
+ vector: filteredVector.map((r) => ({
2095
+ id: r.id,
2096
+ path: r.path,
2097
+ startLine: r.startLine,
2098
+ endLine: r.endLine,
2099
+ source: r.source,
2100
+ snippet: r.snippet,
2101
+ vectorScore: r.score
2102
+ })),
2103
+ keyword: filteredKeyword.map((r) => ({
2104
+ id: r.id,
2105
+ path: r.path,
2106
+ startLine: r.startLine,
2107
+ endLine: r.endLine,
2108
+ source: r.source,
2109
+ snippet: r.snippet,
2110
+ textScore: r.textScore
2111
+ })),
2112
+ vectorWeight: this.hybrid.vectorWeight,
2113
+ textWeight: this.hybrid.textWeight
2114
+ });
2115
+ return merged.filter((entry) => entry.score >= minScore).slice(0, maxResults).map((r) => ({
2116
+ path: r.path,
2117
+ startLine: r.startLine,
2118
+ endLine: r.endLine,
2119
+ score: r.score,
2120
+ snippet: r.snippet
2121
+ }));
2122
+ }
2123
+ async sync(opts) {
2124
+ if (this.syncing) {
2125
+ await this.syncing;
2126
+ return;
2127
+ }
2128
+ if (this.syncLock) {
2129
+ return;
2130
+ }
2131
+ this.syncLock = true;
2132
+ this.syncing = this.runSync(opts);
2133
+ try {
2134
+ await this.syncing;
2135
+ } finally {
2136
+ this.syncing = null;
2137
+ this.syncLock = false;
2138
+ }
2139
+ }
2140
+ async runSync(opts) {
2141
+ this.debug?.(`memory sync starting`, { reason: opts?.reason });
2142
+ await this.ensureVectorReady();
2143
+ const meta = this.readMeta();
2144
+ const needsFullReindex = opts?.force || !meta || meta.model !== this.provider.model || meta.provider !== this.provider.id || meta.providerKey !== this.providerKey || meta.chunkTokens !== this.chunking.tokens || meta.chunkOverlap !== this.chunking.overlap || this.vector.available && !meta?.vectorDims;
2145
+ const files = await listMemoryFiles(this.memoryDir);
2146
+ const activePaths = /* @__PURE__ */ new Set();
2147
+ for (const absPath of files) {
2148
+ const entry = await buildFileEntry(absPath, this.memoryDir);
2149
+ activePaths.add(entry.path);
2150
+ const record = this.db.prepare(`SELECT hash FROM files WHERE path = ? AND source = ?`).get(entry.path, "memory");
2151
+ if (!needsFullReindex && record?.hash === entry.hash) {
2152
+ continue;
2153
+ }
2154
+ await this.indexFile(entry);
2155
+ }
2156
+ const staleRows = this.db.prepare(`SELECT path FROM files WHERE source = ?`).all("memory");
2157
+ for (const stale of staleRows) {
2158
+ if (activePaths.has(stale.path)) continue;
2159
+ this.db.prepare(`DELETE FROM files WHERE path = ? AND source = ?`).run(stale.path, "memory");
2160
+ try {
2161
+ this.db.prepare(
2162
+ `DELETE FROM ${VECTOR_TABLE} WHERE id IN (SELECT id FROM chunks WHERE path = ? AND source = ?)`
2163
+ ).run(stale.path, "memory");
2164
+ } catch (err) {
2165
+ logError("deleteStaleVectorEntries", err, this.debug);
2166
+ }
2167
+ this.db.prepare(`DELETE FROM chunks WHERE path = ? AND source = ?`).run(stale.path, "memory");
2168
+ this.db.prepare(`DELETE FROM knowledge_links WHERE source_path = ?`).run(stale.path);
2169
+ if (this.fts.enabled && this.fts.available) {
2170
+ try {
2171
+ this.db.prepare(`DELETE FROM ${FTS_TABLE} WHERE path = ? AND source = ? AND model = ?`).run(stale.path, "memory", this.provider.model);
2172
+ } catch (err) {
2173
+ logError("deleteStaleFtsEntries", err, this.debug);
2174
+ }
2175
+ }
2176
+ }
2177
+ this.writeMeta({
2178
+ model: this.provider.model,
2179
+ provider: this.provider.id,
2180
+ providerKey: this.providerKey,
2181
+ chunkTokens: this.chunking.tokens,
2182
+ chunkOverlap: this.chunking.overlap,
2183
+ vectorDims: this.vector.dims
2184
+ });
2185
+ this.pruneEmbeddingCacheIfNeeded();
2186
+ this.dirty = false;
2187
+ this.debug?.(`memory sync complete`, { files: files.length });
2188
+ }
2189
+ async indexFile(entry) {
2190
+ const content = await fs2.readFile(entry.absPath, "utf-8");
2191
+ const chunks = chunkMarkdown(content, this.chunking);
2192
+ const { frontmatter } = parseFrontmatter(content);
2193
+ const knowledgeType = frontmatter?.type ?? null;
2194
+ const knowledgeId = frontmatter?.id ?? null;
2195
+ const domains = frontmatter?.domain ?? null;
2196
+ const entities = frontmatter?.entities ?? null;
2197
+ const confidence = frontmatter?.confidence ?? null;
2198
+ const links = frontmatter?.links ?? null;
2199
+ const embeddings = await this.embedChunks(chunks);
2200
+ this.db.prepare(
2201
+ `INSERT OR REPLACE INTO files (path, source, hash, mtime, size) VALUES (?, ?, ?, ?, ?)`
2202
+ ).run(entry.path, "memory", entry.hash, Math.floor(entry.mtimeMs), entry.size);
2203
+ try {
2204
+ this.db.prepare(
2205
+ `DELETE FROM ${VECTOR_TABLE} WHERE id IN (SELECT id FROM chunks WHERE path = ? AND source = ?)`
2206
+ ).run(entry.path, "memory");
2207
+ } catch (err) {
2208
+ logError("deleteOldVectorChunks", err, this.debug);
2209
+ }
2210
+ this.db.prepare(`DELETE FROM chunks WHERE path = ? AND source = ?`).run(entry.path, "memory");
2211
+ if (this.fts.enabled && this.fts.available) {
2212
+ try {
2213
+ this.db.prepare(`DELETE FROM ${FTS_TABLE} WHERE path = ? AND source = ? AND model = ?`).run(entry.path, "memory", this.provider.model);
2214
+ } catch (err) {
2215
+ logError("deleteOldFtsChunks", err, this.debug);
2216
+ }
2217
+ }
2218
+ this.db.prepare(`DELETE FROM knowledge_links WHERE source_path = ?`).run(entry.path);
2219
+ const now = Date.now();
2220
+ for (let i = 0; i < chunks.length; i++) {
2221
+ const chunk = chunks[i];
2222
+ const embedding = embeddings[i] ?? [];
2223
+ const chunkId = randomUUID();
2224
+ const meta = extractChunkMetadata(chunk.text);
2225
+ this.db.prepare(
2226
+ `INSERT INTO chunks (id, path, source, start_line, end_line, hash, model, text, embedding, updated_at, type, knowledge_type, knowledge_id, domains, entities, confidence)
2227
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
2228
+ ).run(
2229
+ chunkId,
2230
+ entry.path,
2231
+ "memory",
2232
+ chunk.startLine,
2233
+ chunk.endLine,
2234
+ chunk.hash,
2235
+ this.provider.model,
2236
+ chunk.text,
2237
+ JSON.stringify(embedding),
2238
+ now,
2239
+ meta.type ?? null,
2240
+ knowledgeType,
2241
+ knowledgeId,
2242
+ domains ? JSON.stringify(domains) : null,
2243
+ entities ? JSON.stringify(entities) : null,
2244
+ confidence
2245
+ );
2246
+ if (this.vector.available && embedding.length > 0) {
2247
+ if (!this.vector.dims) {
2248
+ this.vector.dims = embedding.length;
2249
+ this.ensureVectorTable(embedding.length);
2250
+ }
2251
+ try {
2252
+ this.db.prepare(`INSERT INTO ${VECTOR_TABLE} (id, embedding) VALUES (?, ?)`).run(chunkId, vectorToBlob(embedding));
2253
+ } catch (err) {
2254
+ logError("insertVectorChunk", err, this.debug);
2255
+ }
2256
+ }
2257
+ if (this.fts.enabled && this.fts.available) {
2258
+ try {
2259
+ this.db.prepare(
2260
+ `INSERT INTO ${FTS_TABLE} (text, id, path, source, model, start_line, end_line)
2261
+ VALUES (?, ?, ?, ?, ?, ?, ?)`
2262
+ ).run(
2263
+ chunk.text,
2264
+ chunkId,
2265
+ entry.path,
2266
+ "memory",
2267
+ this.provider.model,
2268
+ chunk.startLine,
2269
+ chunk.endLine
2270
+ );
2271
+ } catch (err) {
2272
+ logError("insertFtsChunk", err, this.debug);
2273
+ }
2274
+ }
2275
+ }
2276
+ if (links && knowledgeId) {
2277
+ const upsertLink = this.db.prepare(
2278
+ `INSERT OR REPLACE INTO knowledge_links (from_id, to_id, relation, layer, weight, source_path, created_at)
2279
+ VALUES (?, ?, ?, ?, ?, ?, ?)`
2280
+ );
2281
+ for (const link of links) {
2282
+ upsertLink.run(
2283
+ knowledgeId,
2284
+ link.target,
2285
+ link.relation,
2286
+ link.layer ?? null,
2287
+ 0.5,
2288
+ entry.path,
2289
+ now
2290
+ );
2291
+ }
2292
+ }
2293
+ }
2294
+ async embedChunks(chunks) {
2295
+ if (chunks.length === 0) return [];
2296
+ const hashes = chunks.map((c) => c.hash);
2297
+ const cached = this.loadEmbeddingCache(hashes);
2298
+ const missing = [];
2299
+ for (let i = 0; i < chunks.length; i++) {
2300
+ if (!cached.has(hashes[i])) {
2301
+ missing.push({ index: i, chunk: chunks[i] });
2302
+ }
2303
+ }
2304
+ if (missing.length > 0) {
2305
+ const texts = missing.map((m) => m.chunk.text);
2306
+ const newEmbeddings = await this.embedBatchWithRetry(texts);
2307
+ for (let i = 0; i < missing.length; i++) {
2308
+ const hash = missing[i].chunk.hash;
2309
+ const embedding = newEmbeddings[i] ?? [];
2310
+ cached.set(hash, embedding);
2311
+ this.upsertEmbeddingCache(hash, embedding);
2312
+ }
2313
+ }
2314
+ return hashes.map((h) => cached.get(h) ?? []);
2315
+ }
2316
+ async embedBatchWithRetry(texts) {
2317
+ if (texts.length === 0) return [];
2318
+ if (this.batchConfig.enabled) {
2319
+ try {
2320
+ return await this.embedWithBatchApi(texts);
2321
+ } catch (err) {
2322
+ this.debug?.(`batch embedding failed, falling back to direct: ${String(err)}`);
2323
+ }
2324
+ }
2325
+ let lastError = null;
2326
+ for (let attempt = 0; attempt < EMBEDDING_RETRY_MAX_ATTEMPTS; attempt++) {
2327
+ try {
2328
+ return await this.provider.embedBatch(texts);
2329
+ } catch (err) {
2330
+ lastError = err instanceof Error ? err : new Error(String(err));
2331
+ if (attempt < EMBEDDING_RETRY_MAX_ATTEMPTS - 1) {
2332
+ const delay = Math.min(
2333
+ EMBEDDING_RETRY_MAX_DELAY_MS,
2334
+ EMBEDDING_RETRY_BASE_DELAY_MS * Math.pow(2, attempt)
2335
+ );
2336
+ await new Promise((resolve) => setTimeout(resolve, delay));
2337
+ }
2338
+ }
2339
+ }
2340
+ throw lastError;
2341
+ }
2342
+ async embedWithBatchApi(texts) {
2343
+ if (this.openAi) {
2344
+ const requests = texts.map((text, i) => ({
2345
+ custom_id: `chunk-${i}`,
2346
+ method: "POST",
2347
+ url: OPENAI_BATCH_ENDPOINT,
2348
+ body: { model: this.openAi.model, input: text }
2349
+ }));
2350
+ const results = await runOpenAiEmbeddingBatches({
2351
+ openAi: this.openAi,
2352
+ source: "minimem",
2353
+ requests,
2354
+ wait: this.batchConfig.wait,
2355
+ pollIntervalMs: this.batchConfig.pollIntervalMs,
2356
+ timeoutMs: this.batchConfig.timeoutMs,
2357
+ concurrency: this.batchConfig.concurrency,
2358
+ debug: this.debug
2359
+ });
2360
+ return texts.map((_, i) => results.get(`chunk-${i}`) ?? []);
2361
+ }
2362
+ if (this.gemini) {
2363
+ const requests = texts.map((text, i) => ({
2364
+ custom_id: `chunk-${i}`,
2365
+ content: { parts: [{ text }] },
2366
+ taskType: "RETRIEVAL_DOCUMENT"
2367
+ }));
2368
+ const results = await runGeminiEmbeddingBatches({
2369
+ gemini: this.gemini,
2370
+ source: "minimem",
2371
+ requests,
2372
+ wait: this.batchConfig.wait,
2373
+ pollIntervalMs: this.batchConfig.pollIntervalMs,
2374
+ timeoutMs: this.batchConfig.timeoutMs,
2375
+ concurrency: this.batchConfig.concurrency,
2376
+ debug: this.debug
2377
+ });
2378
+ return texts.map((_, i) => results.get(`chunk-${i}`) ?? []);
2379
+ }
2380
+ throw new Error("Batch API not available for local embeddings");
2381
+ }
2382
+ async embedQueryWithTimeout(text) {
2383
+ const timeout = this.provider.id === "local" ? EMBEDDING_QUERY_TIMEOUT_LOCAL_MS : EMBEDDING_QUERY_TIMEOUT_REMOTE_MS;
2384
+ const ac = new AbortController();
2385
+ const timer = setTimeout(() => ac.abort(), timeout);
2386
+ try {
2387
+ const result = await Promise.race([
2388
+ this.provider.embedQuery(text),
2389
+ new Promise((_, reject) => {
2390
+ ac.signal.addEventListener(
2391
+ "abort",
2392
+ () => reject(new Error("embedding query timeout"))
2393
+ );
2394
+ })
2395
+ ]);
2396
+ return result;
2397
+ } finally {
2398
+ clearTimeout(timer);
2399
+ }
2400
+ }
2401
+ loadEmbeddingCache(hashes) {
2402
+ const result = /* @__PURE__ */ new Map();
2403
+ if (!this.cache.enabled || hashes.length === 0) return result;
2404
+ const placeholders = hashes.map(() => "?").join(",");
2405
+ const rows = this.db.prepare(
2406
+ `SELECT hash, embedding FROM ${EMBEDDING_CACHE_TABLE}
2407
+ WHERE provider = ? AND model = ? AND provider_key = ? AND hash IN (${placeholders})`
2408
+ ).all(this.provider.id, this.provider.model, this.providerKey, ...hashes);
2409
+ const now = Date.now();
2410
+ for (const row of rows) {
2411
+ result.set(row.hash, parseEmbedding(row.embedding));
2412
+ this.db.prepare(
2413
+ `UPDATE ${EMBEDDING_CACHE_TABLE} SET updated_at = ?
2414
+ WHERE provider = ? AND model = ? AND provider_key = ? AND hash = ?`
2415
+ ).run(now, this.provider.id, this.provider.model, this.providerKey, row.hash);
2416
+ }
2417
+ return result;
2418
+ }
2419
+ upsertEmbeddingCache(hash, embedding) {
2420
+ if (!this.cache.enabled) return;
2421
+ const now = Date.now();
2422
+ this.db.prepare(
2423
+ `INSERT OR REPLACE INTO ${EMBEDDING_CACHE_TABLE}
2424
+ (provider, model, provider_key, hash, embedding, dims, updated_at)
2425
+ VALUES (?, ?, ?, ?, ?, ?, ?)`
2426
+ ).run(
2427
+ this.provider.id,
2428
+ this.provider.model,
2429
+ this.providerKey,
2430
+ hash,
2431
+ JSON.stringify(embedding),
2432
+ embedding.length,
2433
+ now
2434
+ );
2435
+ }
2436
+ pruneEmbeddingCacheIfNeeded() {
2437
+ if (!this.cache.enabled) return;
2438
+ const row = this.db.prepare(`SELECT COUNT(*) as count FROM ${EMBEDDING_CACHE_TABLE}`).get();
2439
+ if (row.count <= this.cache.maxEntries) return;
2440
+ const excess = row.count - this.cache.maxEntries;
2441
+ this.db.prepare(
2442
+ `DELETE FROM ${EMBEDDING_CACHE_TABLE}
2443
+ WHERE rowid IN (
2444
+ SELECT rowid FROM ${EMBEDDING_CACHE_TABLE}
2445
+ ORDER BY updated_at ASC
2446
+ LIMIT ?
2447
+ )`
2448
+ ).run(excess);
2449
+ }
2450
+ async ensureVectorReady(dimensions) {
2451
+ if (this.vector.available === true) return true;
2452
+ if (this.vector.available === false) return false;
2453
+ if (!this.vectorReady) {
2454
+ this.vectorReady = this.loadVectorExtension();
2455
+ }
2456
+ const ready = await this.vectorReady;
2457
+ if (ready && dimensions && !this.vector.dims) {
2458
+ this.vector.dims = dimensions;
2459
+ this.ensureVectorTable(dimensions);
2460
+ }
2461
+ return ready;
2462
+ }
2463
+ async loadVectorExtension() {
2464
+ const result = await loadSqliteVecExtension({
2465
+ db: this.db,
2466
+ extensionPath: this.vectorExtensionPath
2467
+ });
2468
+ this.vector.available = result.ok;
2469
+ if (result.error) {
2470
+ this.vector.loadError = result.error;
2471
+ this.debug?.(`sqlite-vec load failed: ${result.error}`);
2472
+ }
2473
+ if (result.extensionPath) {
2474
+ this.vector.extensionPath = result.extensionPath;
2475
+ }
2476
+ return result.ok;
2477
+ }
2478
+ ensureVectorTable(dimensions) {
2479
+ if (!this.vector.available) return;
2480
+ try {
2481
+ this.db.exec(
2482
+ `CREATE VIRTUAL TABLE IF NOT EXISTS ${VECTOR_TABLE} USING vec0(
2483
+ id TEXT PRIMARY KEY,
2484
+ embedding FLOAT[${dimensions}]
2485
+ )`
2486
+ );
2487
+ } catch (err) {
2488
+ this.debug?.(`vector table creation failed: ${String(err)}`);
2489
+ }
2490
+ }
2491
+ async readFile(relativePath) {
2492
+ const absPath = path3.join(this.memoryDir, relativePath);
2493
+ try {
2494
+ return await fs2.readFile(absPath, "utf-8");
2495
+ } catch {
2496
+ return null;
2497
+ }
2498
+ }
2499
+ /**
2500
+ * Read specific lines from a memory file
2501
+ */
2502
+ async readLines(relativePath, opts) {
2503
+ const content = await this.readFile(relativePath);
2504
+ if (content === null) return null;
2505
+ const allLines = content.split("\n");
2506
+ const from = Math.max(1, opts?.from ?? 1);
2507
+ const lines = opts?.lines ?? allLines.length;
2508
+ const startIdx = from - 1;
2509
+ const endIdx = Math.min(startIdx + lines, allLines.length);
2510
+ const selectedLines = allLines.slice(startIdx, endIdx);
2511
+ return {
2512
+ content: selectedLines.join("\n"),
2513
+ startLine: from,
2514
+ endLine: startIdx + selectedLines.length
2515
+ };
2516
+ }
2517
+ /**
2518
+ * Write content to a memory file (creates or overwrites)
2519
+ */
2520
+ async writeFile(relativePath, content) {
2521
+ this.validateMemoryPath(relativePath);
2522
+ const absPath = path3.join(this.memoryDir, relativePath);
2523
+ const dir = path3.dirname(absPath);
2524
+ await fs2.mkdir(dir, { recursive: true });
2525
+ await fs2.writeFile(absPath, content, "utf-8");
2526
+ this.dirty = true;
2527
+ this.debug?.(`memory write: ${relativePath}`);
2528
+ }
2529
+ /**
2530
+ * Append content to a memory file (creates if doesn't exist)
2531
+ */
2532
+ async appendFile(relativePath, content) {
2533
+ this.validateMemoryPath(relativePath);
2534
+ const absPath = path3.join(this.memoryDir, relativePath);
2535
+ const dir = path3.dirname(absPath);
2536
+ await fs2.mkdir(dir, { recursive: true });
2537
+ let toAppend = content;
2538
+ try {
2539
+ const existing = await fs2.readFile(absPath, "utf-8");
2540
+ if (existing.length > 0 && !existing.endsWith("\n")) {
2541
+ toAppend = "\n" + content;
2542
+ }
2543
+ } catch {
2544
+ }
2545
+ await fs2.appendFile(absPath, toAppend, "utf-8");
2546
+ this.dirty = true;
2547
+ this.debug?.(`memory append: ${relativePath}`);
2548
+ }
2549
+ /**
2550
+ * Append content to today's daily log (memory/YYYY-MM-DD.md)
2551
+ */
2552
+ async appendToday(content) {
2553
+ const today = (/* @__PURE__ */ new Date()).toISOString().split("T")[0];
2554
+ const relativePath = `memory/${today}.md`;
2555
+ await this.appendFile(relativePath, content);
2556
+ return relativePath;
2557
+ }
2558
+ /**
2559
+ * List all memory files
2560
+ */
2561
+ async listFiles() {
2562
+ const files = await listMemoryFiles(this.memoryDir);
2563
+ return files.map((f) => path3.relative(this.memoryDir, f).replace(/\\/g, "/"));
2564
+ }
2565
+ /**
2566
+ * Validate that a path is within allowed memory locations
2567
+ */
2568
+ validateMemoryPath(relativePath) {
2569
+ const normalized = relativePath.replace(/\\/g, "/").replace(/^\.\//, "");
2570
+ if (normalized === "MEMORY.md" || normalized === "memory.md") {
2571
+ return;
2572
+ }
2573
+ if (normalized.startsWith("memory/") && normalized.endsWith(".md")) {
2574
+ if (normalized.includes("..")) {
2575
+ throw new Error(`Invalid memory path: ${relativePath} (path traversal not allowed)`);
2576
+ }
2577
+ return;
2578
+ }
2579
+ throw new Error(
2580
+ `Invalid memory path: ${relativePath}. Must be MEMORY.md or memory/*.md`
2581
+ );
2582
+ }
2583
+ async status() {
2584
+ const fileRow = this.db.prepare(`SELECT COUNT(*) as count FROM files`).get();
2585
+ const chunkRow = this.db.prepare(`SELECT COUNT(*) as count FROM chunks`).get();
2586
+ const cacheRow = this.db.prepare(`SELECT COUNT(*) as count FROM ${EMBEDDING_CACHE_TABLE}`).get();
2587
+ return {
2588
+ memoryDir: this.memoryDir,
2589
+ dbPath: this.dbPath,
2590
+ provider: this.provider.id,
2591
+ model: this.provider.model,
2592
+ vectorAvailable: this.vector.available === true,
2593
+ ftsAvailable: this.fts.available,
2594
+ bm25Only: this.provider.id === "none",
2595
+ fallbackReason: this.providerFallbackReason,
2596
+ fileCount: fileRow.count,
2597
+ chunkCount: chunkRow.count,
2598
+ cacheCount: cacheRow.count
2599
+ };
2600
+ }
2601
+ /**
2602
+ * Search with knowledge metadata filters (domain, entities, confidence, type).
2603
+ * Runs a standard search then post-filters by knowledge columns.
2604
+ */
2605
+ async knowledgeSearch(query, opts) {
2606
+ if (this.dirty || !this.watchConfig.enabled && await this.isStale()) {
2607
+ await this.sync({ reason: "knowledgeSearch" });
2608
+ }
2609
+ const cleaned = query.trim();
2610
+ if (!cleaned) return [];
2611
+ const minScore = opts?.minScore ?? this.queryConfig.minScore;
2612
+ const maxResults = opts?.maxResults ?? this.queryConfig.maxResults;
2613
+ const { sql: knowledgeWhere, params: knowledgeParams } = buildKnowledgeFilterSql({
2614
+ domain: opts?.domain,
2615
+ entities: opts?.entities,
2616
+ minConfidence: opts?.minConfidence,
2617
+ knowledgeType: opts?.knowledgeType
2618
+ });
2619
+ if (!knowledgeWhere) {
2620
+ return this.search(query, { maxResults, minScore });
2621
+ }
2622
+ const matchingRows = this.db.prepare(
2623
+ `SELECT id FROM chunks c WHERE c.model = ? AND c.source = 'memory'${knowledgeWhere}`
2624
+ ).all(this.provider.model, ...knowledgeParams);
2625
+ const matchingIds = new Set(matchingRows.map((r) => r.id));
2626
+ if (matchingIds.size === 0) return [];
2627
+ const overFetch = Math.max(maxResults * 3, 30);
2628
+ const results = await this.search(query, {
2629
+ maxResults: overFetch,
2630
+ minScore
2631
+ });
2632
+ const filtered = [];
2633
+ for (const r of results) {
2634
+ const row = this.db.prepare(
2635
+ `SELECT id FROM chunks WHERE path = ? AND start_line = ? AND end_line = ? AND model = ?`
2636
+ ).get(r.path, r.startLine, r.endLine, this.provider.model);
2637
+ if (row && matchingIds.has(row.id)) {
2638
+ filtered.push(r);
2639
+ if (filtered.length >= maxResults) break;
2640
+ }
2641
+ }
2642
+ return filtered;
2643
+ }
2644
+ /**
2645
+ * Get knowledge graph links from or to a node.
2646
+ */
2647
+ getLinks(nodeId, direction = "from", opts) {
2648
+ if (direction === "from") {
2649
+ return getLinksFrom(this.db, nodeId, opts);
2650
+ }
2651
+ return getLinksTo(this.db, nodeId, opts);
2652
+ }
2653
+ /**
2654
+ * Get neighbor nodes via BFS traversal.
2655
+ */
2656
+ getGraphNeighbors(nodeId, depth = 1, opts) {
2657
+ return getNeighbors(this.db, nodeId, depth, opts);
2658
+ }
2659
+ /**
2660
+ * Find shortest path between two knowledge nodes.
2661
+ */
2662
+ getGraphPath(fromId, toId, maxDepth = 3) {
2663
+ return getPathBetween(this.db, fromId, toId, maxDepth);
2664
+ }
2665
+ close() {
2666
+ if (this.closed) return;
2667
+ this.closed = true;
2668
+ if (this.watchTimer) {
2669
+ clearTimeout(this.watchTimer);
2670
+ this.watchTimer = null;
2671
+ }
2672
+ if (this.watcher) {
2673
+ void this.watcher.close();
2674
+ this.watcher = null;
2675
+ }
2676
+ try {
2677
+ this.db.close();
2678
+ } catch (err) {
2679
+ logError("dbClose", err, this.debug);
2680
+ }
2681
+ }
2682
+ };
2683
+
2684
+ export {
2685
+ parseFrontmatter,
2686
+ addFrontmatter,
2687
+ Minimem
2688
+ };
2689
+ //# sourceMappingURL=chunk-BIYUNXYX.js.map