@wipcomputer/memory-crystal 0.7.34-alpha.2 → 0.7.34-alpha.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cc-poller.js CHANGED
@@ -1,42 +1,1939 @@
1
1
  #!/usr/bin/env node
2
- import {
3
- createCrystal,
4
- resolveConfig
5
- } from "./chunk-2GBYLMEF.js";
6
- import {
7
- ensureLdm,
8
- ldmPaths,
9
- resolveStatePath,
10
- stateWritePath
11
- } from "./chunk-DFQ72B7M.js";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __esm = (fn, res) => function __init() {
5
+ return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
6
+ };
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+
12
+ // src/llm.ts
13
+ import { existsSync, readFileSync } from "fs";
14
+ import { join } from "path";
15
+ import { homedir } from "os";
16
+ import { execSync } from "child_process";
17
+ function dbCacheGet(key) {
18
+ if (!_cacheDb) return null;
19
+ try {
20
+ const row = _cacheDb.prepare(
21
+ "SELECT result FROM llm_cache WHERE cache_key = ? AND created_at > ?"
22
+ ).get(key, new Date(Date.now() - CACHE_TTL_DAYS * 864e5).toISOString());
23
+ if (row) {
24
+ _cacheDb.prepare("UPDATE llm_cache SET hit_count = hit_count + 1, last_hit_at = ? WHERE cache_key = ?").run((/* @__PURE__ */ new Date()).toISOString(), key);
25
+ return row.result;
26
+ }
27
+ } catch {
28
+ }
29
+ return null;
30
+ }
31
+ function dbCacheSet(key, type, query, intent, result, provider) {
32
+ if (!_cacheDb) return;
33
+ try {
34
+ _cacheDb.prepare(
35
+ "INSERT OR REPLACE INTO llm_cache (cache_key, cache_type, query, intent, result, provider, created_at, hit_count, last_hit_at) VALUES (?, ?, ?, ?, ?, ?, ?, 0, NULL)"
36
+ ).run(key, type, query, intent || null, result, provider, (/* @__PURE__ */ new Date()).toISOString());
37
+ } catch {
38
+ }
39
+ }
40
+ function getOpSecret(itemName, fieldLabel) {
41
+ try {
42
+ const saTokenPath = join(homedir(), ".openclaw/secrets/op-sa-token");
43
+ if (!existsSync(saTokenPath)) return void 0;
44
+ const saToken = readFileSync(saTokenPath, "utf-8").trim();
45
+ const result = execSync(
46
+ `OP_SERVICE_ACCOUNT_TOKEN="${saToken}" op item get "${itemName}" --vault "Agent Secrets" --fields "${fieldLabel}" --reveal`,
47
+ { encoding: "utf-8", timeout: 5e3, stdio: ["pipe", "pipe", "pipe"] }
48
+ ).trim();
49
+ return result || void 0;
50
+ } catch {
51
+ return void 0;
52
+ }
53
+ }
54
+ async function detectProvider() {
55
+ if (detectionDone && detectedProvider) return detectedProvider;
56
+ detectionDone = true;
57
+ if (samplingServer) {
58
+ detectedProvider = { provider: "sampling", baseURL: "", apiKey: "", model: "client-selected" };
59
+ process.stderr.write("[memory-crystal] LLM provider: MCP Sampling (via client)\n");
60
+ return detectedProvider;
61
+ }
62
+ try {
63
+ const resp = await fetch("http://localhost:18791/v1/models", { signal: AbortSignal.timeout(1e3) });
64
+ if (resp.ok) {
65
+ const data = await resp.json();
66
+ const model = data?.data?.[0]?.id || "default";
67
+ detectedProvider = { provider: "mlx", baseURL: "http://localhost:18791/v1", apiKey: "not-needed", model };
68
+ process.stderr.write(`[memory-crystal] LLM provider: MLX (${model})
69
+ `);
70
+ return detectedProvider;
71
+ }
72
+ } catch {
73
+ }
74
+ try {
75
+ const resp = await fetch("http://localhost:11434/api/tags", { signal: AbortSignal.timeout(1e3) });
76
+ if (resp.ok) {
77
+ const data = await resp.json();
78
+ const models = data?.models || [];
79
+ const embeddingOnly = ["nomic-embed-text", "mxbai-embed", "all-minilm", "snowflake-arctic-embed"];
80
+ const chatModel = models.find((m) => !embeddingOnly.some((e) => m.name.startsWith(e)));
81
+ if (chatModel) {
82
+ detectedProvider = { provider: "ollama", baseURL: "http://localhost:11434/v1", apiKey: "ollama", model: chatModel.name };
83
+ process.stderr.write(`[memory-crystal] LLM provider: Ollama (${chatModel.name})
84
+ `);
85
+ return detectedProvider;
86
+ }
87
+ }
88
+ } catch {
89
+ }
90
+ const openaiKey = process.env.OPENAI_API_KEY || getOpSecret("OpenAI API", "api key");
91
+ if (openaiKey) {
92
+ detectedProvider = { provider: "openai", baseURL: "https://api.openai.com/v1", apiKey: openaiKey, model: "gpt-4o-mini" };
93
+ process.stderr.write("[memory-crystal] LLM provider: OpenAI API\n");
94
+ return detectedProvider;
95
+ }
96
+ const anthropicKey = process.env.ANTHROPIC_API_KEY || getOpSecret("Anthropic Auth Token - remote bunkers", "Auth Token");
97
+ if (anthropicKey && !anthropicKey.startsWith("sk-ant-oat")) {
98
+ detectedProvider = { provider: "anthropic", baseURL: "https://api.anthropic.com", apiKey: anthropicKey, model: "claude-haiku-4-5-20251001" };
99
+ process.stderr.write("[memory-crystal] LLM provider: Anthropic API\n");
100
+ return detectedProvider;
101
+ }
102
+ detectedProvider = { provider: "none", baseURL: "", apiKey: "", model: "" };
103
+ process.stderr.write("[memory-crystal] LLM provider: none (deep search unavailable)\n");
104
+ return detectedProvider;
105
+ }
106
+ async function chatComplete(config, messages, maxTokens = 300) {
107
+ if (config.provider === "sampling") {
108
+ return samplingComplete(messages, maxTokens);
109
+ }
110
+ if (config.provider === "anthropic") {
111
+ return anthropicComplete(config, messages, maxTokens);
112
+ }
113
+ const resp = await fetch(`${config.baseURL}/chat/completions`, {
114
+ method: "POST",
115
+ headers: {
116
+ "Content-Type": "application/json",
117
+ "Authorization": `Bearer ${config.apiKey}`
118
+ },
119
+ body: JSON.stringify({
120
+ model: config.model,
121
+ messages,
122
+ max_tokens: maxTokens,
123
+ temperature: 0.7
124
+ })
125
+ });
126
+ if (!resp.ok) throw new Error(`LLM request failed: ${resp.status}`);
127
+ const data = await resp.json();
128
+ return data.choices?.[0]?.message?.content || "";
129
+ }
130
+ async function anthropicComplete(config, messages, maxTokens) {
131
+ const systemMsg = messages.find((m) => m.role === "system");
132
+ const userMessages = messages.filter((m) => m.role !== "system");
133
+ const body = {
134
+ model: config.model,
135
+ max_tokens: maxTokens,
136
+ messages: userMessages
137
+ };
138
+ if (systemMsg) body.system = systemMsg.content;
139
+ const resp = await fetch("https://api.anthropic.com/v1/messages", {
140
+ method: "POST",
141
+ headers: {
142
+ "Content-Type": "application/json",
143
+ "x-api-key": config.apiKey,
144
+ "anthropic-version": "2023-06-01"
145
+ },
146
+ body: JSON.stringify(body)
147
+ });
148
+ if (!resp.ok) throw new Error(`Anthropic request failed: ${resp.status}`);
149
+ const data = await resp.json();
150
+ return data.content?.[0]?.text || "";
151
+ }
152
+ async function samplingComplete(messages, maxTokens) {
153
+ if (!samplingServer) throw new Error("MCP sampling server not set");
154
+ const systemMsg = messages.find((m) => m.role === "system");
155
+ const userMessages = messages.filter((m) => m.role !== "system");
156
+ const result = await samplingServer.createMessage({
157
+ messages: userMessages.map((m) => ({
158
+ role: m.role,
159
+ content: { type: "text", text: m.content }
160
+ })),
161
+ systemPrompt: systemMsg?.content,
162
+ maxTokens,
163
+ modelPreferences: {
164
+ // Request cheap, fast model (Haiku-class). We don't need Opus for query expansion.
165
+ costPriority: 0.9,
166
+ speedPriority: 0.8,
167
+ intelligencePriority: 0.3,
168
+ hints: [{ name: "haiku" }]
169
+ }
170
+ });
171
+ if (result?.content?.type === "text") return result.content.text;
172
+ if (typeof result?.content === "string") return result.content;
173
+ return "";
174
+ }
175
+ async function expandQuery(query, intent) {
176
+ const cacheKey = intent ? `expand:${query}||${intent}` : `expand:${query}`;
177
+ const dbCached = dbCacheGet(cacheKey);
178
+ if (dbCached) {
179
+ try {
180
+ return JSON.parse(dbCached);
181
+ } catch {
182
+ }
183
+ }
184
+ const cached = expansionCache.get(cacheKey);
185
+ if (cached) return cached;
186
+ const config = await detectProvider();
187
+ if (config.provider === "none") return [];
188
+ try {
189
+ const intentContext = intent ? `
190
+ Query intent: ${intent}. Use this to guide your variations toward the intended domain.` : "";
191
+ const result = await chatComplete(config, [
192
+ { role: "system", content: EXPAND_PROMPT + intentContext },
193
+ { role: "user", content: query }
194
+ ], 300);
195
+ const lines = result.trim().split("\n");
196
+ const queryLower = query.toLowerCase();
197
+ const queryTerms = queryLower.replace(/[^a-z0-9\s]/g, " ").split(/\s+/).filter(Boolean);
198
+ const hasQueryTerm = (text) => {
199
+ const lower = text.toLowerCase();
200
+ if (queryTerms.length === 0) return true;
201
+ return queryTerms.some((term) => lower.includes(term));
202
+ };
203
+ const variations = lines.map((line) => {
204
+ const colonIdx = line.indexOf(":");
205
+ if (colonIdx === -1) return null;
206
+ const type = line.slice(0, colonIdx).trim();
207
+ if (type !== "lex" && type !== "vec" && type !== "hyde") return null;
208
+ const text = line.slice(colonIdx + 1).trim();
209
+ if (!text || !hasQueryTerm(text)) return null;
210
+ return { type, text };
211
+ }).filter((v) => v !== null);
212
+ if (variations.length > 0) {
213
+ expansionCache.set(cacheKey, variations);
214
+ dbCacheSet(cacheKey, "expansion", query, intent, JSON.stringify(variations), config.provider);
215
+ return variations;
216
+ }
217
+ } catch (err) {
218
+ process.stderr.write(`[memory-crystal] Query expansion failed: ${err.message}
219
+ `);
220
+ }
221
+ const fallback = [
222
+ { type: "lex", text: query },
223
+ { type: "vec", text: query },
224
+ { type: "hyde", text: `Information about ${query}` }
225
+ ];
226
+ return fallback;
227
+ }
228
+ async function rerankResults(query, passages) {
229
+ const config = await detectProvider();
230
+ if (config.provider === "none") {
231
+ return passages.map((_, i) => ({ index: i, score: 1 - i * 0.01 }));
232
+ }
233
+ const { createHash: createHash2 } = await import("crypto");
234
+ const contentHash = createHash2("sha256").update(passages.map((p) => p.slice(0, 200)).sort().join("|")).digest("hex").slice(0, 16);
235
+ const rerankCacheKey = `rerank:${query}||${contentHash}`;
236
+ const dbCachedRerank = dbCacheGet(rerankCacheKey);
237
+ if (dbCachedRerank) {
238
+ try {
239
+ return JSON.parse(dbCachedRerank);
240
+ } catch {
241
+ }
242
+ }
243
+ try {
244
+ const passageList = passages.map((p, i) => `[${i}] ${p.slice(0, 500)}`).join("\n\n");
245
+ const result = await chatComplete(config, [
246
+ { role: "system", content: RERANK_PROMPT },
247
+ { role: "user", content: `Query: ${query}
248
+
249
+ Passages:
250
+ ${passageList}` }
251
+ ], 200);
252
+ const results = [];
253
+ for (const line of result.trim().split("\n")) {
254
+ const match = line.match(/^(\d+):\s*([\d.]+)/);
255
+ if (match) {
256
+ results.push({ index: parseInt(match[1]), score: parseFloat(match[2]) });
257
+ }
258
+ }
259
+ const scored = new Set(results.map((r) => r.index));
260
+ for (let i = 0; i < passages.length; i++) {
261
+ if (!scored.has(i)) results.push({ index: i, score: 0 });
262
+ }
263
+ const sorted = results.sort((a, b) => b.score - a.score);
264
+ dbCacheSet(rerankCacheKey, "rerank", query, void 0, JSON.stringify(sorted), config.provider);
265
+ return sorted;
266
+ } catch (err) {
267
+ process.stderr.write(`[memory-crystal] Reranking failed: ${err.message}
268
+ `);
269
+ return passages.map((_, i) => ({ index: i, score: 1 - i * 0.01 }));
270
+ }
271
+ }
272
+ var samplingServer, expansionCache, _cacheDb, CACHE_TTL_DAYS, detectedProvider, detectionDone, EXPAND_PROMPT, RERANK_PROMPT;
273
+ var init_llm = __esm({
274
+ "src/llm.ts"() {
275
+ "use strict";
276
+ samplingServer = null;
277
+ expansionCache = /* @__PURE__ */ new Map();
278
+ _cacheDb = null;
279
+ CACHE_TTL_DAYS = parseInt(process.env.CRYSTAL_CACHE_TTL_DAYS || "7", 10);
280
+ detectedProvider = null;
281
+ detectionDone = false;
282
+ EXPAND_PROMPT = `You are a search query expander. Given a search query, generate exactly 3 variations to improve search recall.
283
+
284
+ Output exactly 3 lines in this format (no other text):
285
+ lex: <keyword-focused variation for full-text search>
286
+ vec: <semantic variation rephrased for embedding similarity>
287
+ hyde: <hypothetical document snippet that would answer this query>
288
+
289
+ Rules:
290
+ - Each variation must contain at least one term from the original query
291
+ - Keep variations concise (under 30 words each)
292
+ - lex should use specific keywords and synonyms
293
+ - vec should rephrase the intent naturally
294
+ - hyde should be a short passage as if answering the query`;
295
+ RERANK_PROMPT = `You are a search result re-ranker. Given a query and a list of text passages, rate each passage's relevance to the query.
296
+
297
+ Output one line per passage in this exact format:
298
+ <index>: <score>
299
+
300
+ Where index is the passage number (0-based) and score is a float from 0.0 to 1.0.
301
+ - 1.0 = perfectly relevant, directly answers the query
302
+ - 0.7 = highly relevant, closely related
303
+ - 0.4 = somewhat relevant, tangentially related
304
+ - 0.1 = barely relevant
305
+ - 0.0 = not relevant at all
306
+
307
+ Rate ALL passages. Output nothing else.`;
308
+ }
309
+ });
310
+
311
+ // src/search-pipeline.ts
312
+ var search_pipeline_exports = {};
313
+ __export(search_pipeline_exports, {
314
+ deepSearch: () => deepSearch
315
+ });
316
+ async function deepSearch(crystal, query, options = {}) {
317
+ const limit = options.limit || 5;
318
+ const candidateLimit = options.candidateLimit || DEFAULT_CANDIDATE_LIMIT;
319
+ const intent = options.intent;
320
+ const filter = options.filter;
321
+ const explain = options.explain || false;
322
+ const provider = await detectProvider();
323
+ if (provider.provider === "none") {
324
+ return crystal.search(query, limit, filter);
325
+ }
326
+ const db = crystal.sqliteDb;
327
+ if (!db) return crystal.search(query, limit, filter);
328
+ const sinceDate = filter?.since ? crystal.parseSince(filter.since) : void 0;
329
+ const untilDate = filter?.until ? crystal.parseSince(filter.until) : void 0;
330
+ const internalFilter = { ...filter, sinceDate, untilDate };
331
+ const initialFts = crystal.searchFTS(query, 20, internalFilter);
332
+ const topScore = initialFts[0]?.score ?? 0;
333
+ const secondScore = initialFts[1]?.score ?? 0;
334
+ const hasStrongSignal = !intent && initialFts.length > 0 && topScore >= STRONG_SIGNAL_MIN_SCORE && topScore - secondScore >= STRONG_SIGNAL_MIN_GAP;
335
+ const expanded = hasStrongSignal ? [] : await expandQuery(query, intent);
336
+ const allResultLists = [];
337
+ if (initialFts.length > 0) allResultLists.push(initialFts);
338
+ const [queryEmbedding] = await crystal.embed([query]);
339
+ const originalVec = crystal.searchVec(queryEmbedding, 30, internalFilter);
340
+ if (originalVec.length > 0) allResultLists.push(originalVec);
341
+ for (const variation of expanded) {
342
+ if (variation.type === "lex") {
343
+ const ftsResults = crystal.searchFTS(variation.text, 20, internalFilter);
344
+ if (ftsResults.length > 0) allResultLists.push(ftsResults);
345
+ } else {
346
+ const [embedding] = await crystal.embed([variation.text]);
347
+ const vecResults = crystal.searchVec(embedding, 20, internalFilter);
348
+ if (vecResults.length > 0) allResultLists.push(vecResults);
349
+ }
350
+ }
351
+ const weights = allResultLists.map((_, i) => i < 2 ? 2 : 1);
352
+ const fused = crystal.reciprocalRankFusion(allResultLists, weights);
353
+ const candidates = fused.slice(0, candidateLimit);
354
+ if (candidates.length === 0) return [];
355
+ const ftsScoreMap = /* @__PURE__ */ new Map();
356
+ const vecScoreMap = /* @__PURE__ */ new Map();
357
+ if (explain) {
358
+ for (const r of initialFts) ftsScoreMap.set(r.text.slice(0, 200), r.score);
359
+ for (const r of originalVec) vecScoreMap.set(r.text.slice(0, 200), r.score);
360
+ }
361
+ const passages = candidates.map((c) => c.text.slice(0, 500));
362
+ const rerankQuery = intent ? `${intent}: ${query}` : query;
363
+ const reranked = await rerankResults(rerankQuery, passages);
364
+ const now = Date.now();
365
+ const blended = reranked.map((r) => {
366
+ const candidate = candidates[r.index];
367
+ if (!candidate) return null;
368
+ const rrfRank = r.index + 1;
369
+ let rrfWeight;
370
+ if (rrfRank <= 3) rrfWeight = 0.75;
371
+ else if (rrfRank <= 10) rrfWeight = 0.6;
372
+ else rrfWeight = 0.4;
373
+ const rrfScore = 1 / rrfRank;
374
+ const blendedScore = rrfWeight * rrfScore + (1 - rrfWeight) * r.score;
375
+ const ageDays = candidate.created_at ? (now - new Date(candidate.created_at).getTime()) / 864e5 : 0;
376
+ const recency = candidate.created_at ? crystal.recencyWeight(ageDays) : 1;
377
+ const finalScore = blendedScore * recency;
378
+ const freshness = candidate.created_at ? crystal.freshnessLabel(ageDays) : void 0;
379
+ const result = {
380
+ ...candidate,
381
+ score: finalScore,
382
+ freshness
383
+ };
384
+ if (explain) {
385
+ const dedup = candidate.text.slice(0, 200);
386
+ result.explain = {
387
+ fts_score: ftsScoreMap.get(dedup),
388
+ vec_score: vecScoreMap.get(dedup),
389
+ rrf_rank: rrfRank,
390
+ rrf_score: rrfScore,
391
+ rerank_score: r.score,
392
+ recency_weight: recency,
393
+ final_score: finalScore
394
+ };
395
+ }
396
+ return result;
397
+ }).filter((r) => r !== null);
398
+ const sorted = blended.sort((a, b) => b.score - a.score).slice(0, limit);
399
+ const topNormScore = sorted[0]?.score || 1;
400
+ return sorted.map((r) => ({ ...r, score: Math.min(r.score / topNormScore * 0.95, 0.95) }));
401
+ }
402
+ var STRONG_SIGNAL_MIN_SCORE, STRONG_SIGNAL_MIN_GAP, DEFAULT_CANDIDATE_LIMIT;
403
+ var init_search_pipeline = __esm({
404
+ "src/search-pipeline.ts"() {
405
+ "use strict";
406
+ init_llm();
407
+ STRONG_SIGNAL_MIN_SCORE = 0.85;
408
+ STRONG_SIGNAL_MIN_GAP = 0.15;
409
+ DEFAULT_CANDIDATE_LIMIT = 40;
410
+ }
411
+ });
412
+
413
+ // src/core.ts
414
+ import * as lancedb from "@lancedb/lancedb";
415
+ import Database from "better-sqlite3";
416
+ import * as sqliteVec from "sqlite-vec";
417
+ import { readFileSync as readFileSync2, existsSync as existsSync2, mkdirSync, readdirSync, statSync } from "fs";
418
+ import { execSync as execSync2 } from "child_process";
419
+ import { join as join2, relative, extname, basename } from "path";
420
+ import { createHash } from "crypto";
421
+ import http from "http";
422
+ import https from "https";
423
+ async function embedOpenAI(texts, apiKey, model) {
424
+ return new Promise((resolve, reject) => {
425
+ const body = JSON.stringify({ input: texts, model });
426
+ const req = https.request({
427
+ hostname: "api.openai.com",
428
+ path: "/v1/embeddings",
429
+ method: "POST",
430
+ headers: {
431
+ "Content-Type": "application/json",
432
+ "Authorization": `Bearer ${apiKey}`,
433
+ "Content-Length": Buffer.byteLength(body)
434
+ },
435
+ timeout: 3e4
436
+ }, (res) => {
437
+ let data = "";
438
+ res.on("data", (chunk) => data += chunk);
439
+ res.on("end", () => {
440
+ if (res.statusCode !== 200) {
441
+ reject(new Error(`OpenAI API error ${res.statusCode}: ${data.slice(0, 200)}`));
442
+ return;
443
+ }
444
+ const parsed = JSON.parse(data);
445
+ resolve(parsed.data.map((d) => d.embedding));
446
+ });
447
+ });
448
+ req.on("error", reject);
449
+ req.on("timeout", () => {
450
+ req.destroy();
451
+ reject(new Error("OpenAI timeout"));
452
+ });
453
+ req.write(body);
454
+ req.end();
455
+ });
456
+ }
457
+ async function embedOllama(texts, host, model) {
458
+ const results = [];
459
+ for (const text of texts) {
460
+ const result = await new Promise((resolve, reject) => {
461
+ const url = new URL("/api/embeddings", host);
462
+ const body = JSON.stringify({ model, prompt: text });
463
+ const req = http.request({
464
+ hostname: url.hostname,
465
+ port: url.port,
466
+ path: url.pathname,
467
+ method: "POST",
468
+ headers: {
469
+ "Content-Type": "application/json",
470
+ "Content-Length": Buffer.byteLength(body)
471
+ },
472
+ timeout: 15e3
473
+ }, (res) => {
474
+ let data = "";
475
+ res.on("data", (chunk) => data += chunk);
476
+ res.on("end", () => {
477
+ if (res.statusCode !== 200) {
478
+ reject(new Error(`Ollama error ${res.statusCode}: ${data.slice(0, 200)}`));
479
+ return;
480
+ }
481
+ resolve(JSON.parse(data).embedding);
482
+ });
483
+ });
484
+ req.on("error", reject);
485
+ req.on("timeout", () => {
486
+ req.destroy();
487
+ reject(new Error("Ollama timeout"));
488
+ });
489
+ req.write(body);
490
+ req.end();
491
+ });
492
+ results.push(result);
493
+ }
494
+ return results;
495
+ }
496
+ async function embedGoogle(texts, apiKey, model) {
497
+ return new Promise((resolve, reject) => {
498
+ const body = JSON.stringify({
499
+ requests: texts.map((text) => ({ model: `models/${model}`, content: { parts: [{ text }] } }))
500
+ });
501
+ const req = https.request({
502
+ hostname: "generativelanguage.googleapis.com",
503
+ path: `/v1beta/models/${model}:batchEmbedContents?key=${apiKey}`,
504
+ method: "POST",
505
+ headers: {
506
+ "Content-Type": "application/json",
507
+ "Content-Length": Buffer.byteLength(body)
508
+ },
509
+ timeout: 3e4
510
+ }, (res) => {
511
+ let data = "";
512
+ res.on("data", (chunk) => data += chunk);
513
+ res.on("end", () => {
514
+ if (res.statusCode !== 200) {
515
+ reject(new Error(`Google API error ${res.statusCode}: ${data.slice(0, 200)}`));
516
+ return;
517
+ }
518
+ const parsed = JSON.parse(data);
519
+ resolve(parsed.embeddings.map((e) => e.values));
520
+ });
521
+ });
522
+ req.on("error", reject);
523
+ req.on("timeout", () => {
524
+ req.destroy();
525
+ reject(new Error("Google timeout"));
526
+ });
527
+ req.write(body);
528
+ req.end();
529
+ });
530
+ }
531
+ var Crystal = class _Crystal {
532
+ config;
533
+ lanceDb = null;
534
+ sqliteDb = null;
535
+ chunksTable = null;
536
+ vecDimensions = null;
537
+ constructor(config) {
538
+ this.config = config;
539
+ if (!existsSync2(config.dataDir)) {
540
+ mkdirSync(config.dataDir, { recursive: true });
541
+ }
542
+ }
543
+ // ── Initialization ──
544
+ async init() {
545
+ const lanceDir = join2(this.config.dataDir, "lance");
546
+ const sqlitePath = join2(this.config.dataDir, "crystal.db");
547
+ if (!existsSync2(lanceDir)) mkdirSync(lanceDir, { recursive: true });
548
+ this.lanceDb = await lancedb.connect(lanceDir);
549
+ this.sqliteDb = new Database(sqlitePath);
550
+ this.sqliteDb.pragma("journal_mode = WAL");
551
+ sqliteVec.load(this.sqliteDb);
552
+ this.initSqliteTables();
553
+ this.initChunksTables();
554
+ await this.initLanceTables();
555
+ }
556
+ initSqliteTables() {
557
+ const db = this.sqliteDb;
558
+ db.exec(`
559
+ CREATE TABLE IF NOT EXISTS sources (
560
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
561
+ type TEXT NOT NULL,
562
+ uri TEXT NOT NULL,
563
+ title TEXT,
564
+ agent_id TEXT NOT NULL,
565
+ metadata TEXT DEFAULT '{}',
566
+ ingested_at TEXT NOT NULL,
567
+ chunk_count INTEGER DEFAULT 0
568
+ );
569
+
570
+ CREATE TABLE IF NOT EXISTS capture_state (
571
+ agent_id TEXT NOT NULL,
572
+ source_id TEXT NOT NULL,
573
+ last_message_count INTEGER DEFAULT 0,
574
+ capture_count INTEGER DEFAULT 0,
575
+ last_capture_at TEXT,
576
+ PRIMARY KEY (agent_id, source_id)
577
+ );
578
+
579
+ CREATE TABLE IF NOT EXISTS memories (
580
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
581
+ text TEXT NOT NULL,
582
+ category TEXT NOT NULL DEFAULT 'fact',
583
+ confidence REAL NOT NULL DEFAULT 1.0,
584
+ source_ids TEXT DEFAULT '[]',
585
+ status TEXT NOT NULL DEFAULT 'active',
586
+ created_at TEXT NOT NULL,
587
+ updated_at TEXT NOT NULL
588
+ );
589
+
590
+ CREATE TABLE IF NOT EXISTS entities (
591
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
592
+ name TEXT NOT NULL UNIQUE,
593
+ type TEXT NOT NULL DEFAULT 'concept',
594
+ description TEXT,
595
+ properties TEXT DEFAULT '{}',
596
+ created_at TEXT NOT NULL,
597
+ updated_at TEXT NOT NULL
598
+ );
599
+
600
+ CREATE TABLE IF NOT EXISTS relationships (
601
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
602
+ source_id INTEGER NOT NULL REFERENCES entities(id),
603
+ target_id INTEGER NOT NULL REFERENCES entities(id),
604
+ type TEXT NOT NULL,
605
+ description TEXT,
606
+ weight REAL DEFAULT 1.0,
607
+ valid_from TEXT NOT NULL,
608
+ valid_until TEXT,
609
+ created_at TEXT NOT NULL
610
+ );
611
+
612
+ CREATE INDEX IF NOT EXISTS idx_sources_agent ON sources(agent_id);
613
+ CREATE INDEX IF NOT EXISTS idx_memories_status ON memories(status);
614
+ CREATE INDEX IF NOT EXISTS idx_entities_name ON entities(name);
615
+ CREATE INDEX IF NOT EXISTS idx_relationships_source ON relationships(source_id);
616
+ CREATE INDEX IF NOT EXISTS idx_relationships_target ON relationships(target_id);
617
+
618
+ -- LLM cache (persistent expansion + reranking results)
619
+ CREATE TABLE IF NOT EXISTS llm_cache (
620
+ cache_key TEXT PRIMARY KEY,
621
+ cache_type TEXT NOT NULL,
622
+ query TEXT NOT NULL,
623
+ intent TEXT,
624
+ result TEXT NOT NULL,
625
+ provider TEXT NOT NULL,
626
+ created_at TEXT NOT NULL,
627
+ hit_count INTEGER DEFAULT 0,
628
+ last_hit_at TEXT
629
+ );
630
+ CREATE INDEX IF NOT EXISTS idx_llm_cache_type ON llm_cache(cache_type);
631
+ CREATE INDEX IF NOT EXISTS idx_llm_cache_created ON llm_cache(created_at);
632
+
633
+ -- Source file indexing (optional feature)
634
+ CREATE TABLE IF NOT EXISTS source_collections (
635
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
636
+ name TEXT NOT NULL UNIQUE,
637
+ root_path TEXT NOT NULL,
638
+ glob_patterns TEXT NOT NULL DEFAULT '["**/*"]',
639
+ ignore_patterns TEXT NOT NULL DEFAULT '[]',
640
+ file_count INTEGER DEFAULT 0,
641
+ chunk_count INTEGER DEFAULT 0,
642
+ last_sync_at TEXT,
643
+ created_at TEXT NOT NULL
644
+ );
645
+
646
+ CREATE TABLE IF NOT EXISTS source_files (
647
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
648
+ collection_id INTEGER NOT NULL REFERENCES source_collections(id) ON DELETE CASCADE,
649
+ file_path TEXT NOT NULL,
650
+ file_hash TEXT NOT NULL,
651
+ file_size INTEGER NOT NULL,
652
+ chunk_count INTEGER DEFAULT 0,
653
+ last_indexed_at TEXT NOT NULL
654
+ );
655
+
656
+ CREATE UNIQUE INDEX IF NOT EXISTS idx_source_files_path ON source_files(collection_id, file_path);
657
+ CREATE INDEX IF NOT EXISTS idx_source_files_collection ON source_files(collection_id);
658
+ `);
659
+ }
660
+ initChunksTables() {
661
+ const db = this.sqliteDb;
662
+ db.exec(`
663
+ CREATE TABLE IF NOT EXISTS chunks (
664
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
665
+ text TEXT NOT NULL,
666
+ text_hash TEXT NOT NULL,
667
+ role TEXT,
668
+ source_type TEXT,
669
+ source_id TEXT,
670
+ agent_id TEXT,
671
+ token_count INTEGER,
672
+ created_at TEXT NOT NULL
673
+ );
674
+
675
+ CREATE INDEX IF NOT EXISTS idx_chunks_agent ON chunks(agent_id);
676
+ CREATE INDEX IF NOT EXISTS idx_chunks_source ON chunks(source_type);
677
+ CREATE INDEX IF NOT EXISTS idx_chunks_hash ON chunks(text_hash);
678
+ CREATE INDEX IF NOT EXISTS idx_chunks_created ON chunks(created_at);
679
+
680
+ -- FTS5 full-text search table
681
+ CREATE VIRTUAL TABLE IF NOT EXISTS chunks_fts USING fts5(
682
+ text,
683
+ tokenize='porter unicode61'
684
+ );
685
+
686
+ -- Sync trigger: populate FTS on chunk insert
687
+ CREATE TRIGGER IF NOT EXISTS chunks_fts_insert AFTER INSERT ON chunks
688
+ BEGIN
689
+ INSERT INTO chunks_fts(rowid, text) VALUES (NEW.id, NEW.text);
690
+ END;
691
+
692
+ -- Sync trigger: clean up FTS and vec on chunk delete
693
+ CREATE TRIGGER IF NOT EXISTS chunks_cleanup AFTER DELETE ON chunks
694
+ BEGIN
695
+ DELETE FROM chunks_vec WHERE chunk_id = OLD.id;
696
+ INSERT INTO chunks_fts(chunks_fts, rowid, text) VALUES('delete', OLD.id, OLD.text);
697
+ END;
698
+ `);
699
+ const vecTable = db.prepare(
700
+ `SELECT name FROM sqlite_master WHERE type='table' AND name='chunks_vec'`
701
+ ).get();
702
+ if (vecTable) {
703
+ try {
704
+ const row = db.prepare("SELECT embedding FROM chunks_vec LIMIT 1").get();
705
+ if (row?.embedding) {
706
+ this.vecDimensions = row.embedding.length / 4;
707
+ }
708
+ } catch {
709
+ }
710
+ }
711
+ }
712
+ ensureVecTable(dimensions) {
713
+ const db = this.sqliteDb;
714
+ const existing = db.prepare(
715
+ `SELECT name FROM sqlite_master WHERE type='table' AND name='chunks_vec'`
716
+ ).get();
717
+ if (!existing) {
718
+ db.exec(`
719
+ CREATE VIRTUAL TABLE chunks_vec USING vec0(
720
+ chunk_id INTEGER PRIMARY KEY,
721
+ embedding float[${dimensions}] distance_metric=cosine
722
+ );
723
+ `);
724
+ }
725
+ this.vecDimensions = dimensions;
726
+ }
727
+ async initLanceTables() {
728
+ const db = this.lanceDb;
729
+ const tableNames = await db.tableNames();
730
+ if (tableNames.includes("chunks")) {
731
+ this.chunksTable = await db.openTable("chunks");
732
+ }
733
+ }
734
+ // ── Embedding ──
735
+ async embed(texts) {
736
+ if (texts.length === 0) return [];
737
+ const cfg = this.config;
738
+ switch (cfg.embeddingProvider) {
739
+ case "openai": {
740
+ if (!cfg.openaiApiKey) throw new Error("OpenAI API key required");
741
+ const model = cfg.openaiModel || "text-embedding-3-small";
742
+ const maxCharsPerBatch = 8e5;
743
+ const results = [];
744
+ let batch = [];
745
+ let batchChars = 0;
746
+ for (const text of texts) {
747
+ if (batchChars + text.length > maxCharsPerBatch && batch.length > 0) {
748
+ results.push(...await embedOpenAI(batch, cfg.openaiApiKey, model));
749
+ batch = [];
750
+ batchChars = 0;
751
+ }
752
+ batch.push(text);
753
+ batchChars += text.length;
754
+ }
755
+ if (batch.length > 0) {
756
+ results.push(...await embedOpenAI(batch, cfg.openaiApiKey, model));
757
+ }
758
+ return results;
759
+ }
760
+ case "ollama":
761
+ return embedOllama(texts, cfg.ollamaHost || "http://localhost:11434", cfg.ollamaModel || "nomic-embed-text");
762
+ case "google":
763
+ if (!cfg.googleApiKey) throw new Error("Google API key required");
764
+ return embedGoogle(texts, cfg.googleApiKey, cfg.googleModel || "text-embedding-004");
765
+ default:
766
+ throw new Error(`Unknown embedding provider: ${cfg.embeddingProvider}`);
767
+ }
768
+ }
769
+ // ── Chunking ──
770
+ chunkText(text, targetTokens = 400, overlapTokens = 80) {
771
+ const targetChars = targetTokens * 4;
772
+ const overlapChars = overlapTokens * 4;
773
+ const chunks = [];
774
+ let start = 0;
775
+ while (start < text.length) {
776
+ let end = Math.min(start + targetChars, text.length);
777
+ if (end < text.length) {
778
+ const minBreak = start + Math.floor(targetChars * 0.5);
779
+ const paraBreak = text.lastIndexOf("\n\n", end);
780
+ if (paraBreak > minBreak) {
781
+ end = paraBreak;
782
+ } else {
783
+ const sentBreak = text.lastIndexOf(". ", end);
784
+ if (sentBreak > minBreak) {
785
+ end = sentBreak + 1;
786
+ }
787
+ }
788
+ }
789
+ const chunk = text.slice(start, end).trim();
790
+ if (chunk.length > 0) chunks.push(chunk);
791
+ if (end >= text.length) break;
792
+ start = end - overlapChars;
793
+ if (start <= (chunks.length > 0 ? end - targetChars : 0)) {
794
+ start = end;
795
+ }
796
+ }
797
+ return chunks;
798
+ }
799
+ // ── Ingest ──
800
+ async ingest(chunks) {
801
+ if (chunks.length === 0) return 0;
802
+ const db = this.sqliteDb;
803
+ const newChunks = chunks.filter((c) => {
804
+ const hash = createHash("sha256").update(c.text).digest("hex");
805
+ return !db.prepare("SELECT 1 FROM chunks WHERE text_hash = ?").get(hash);
806
+ });
807
+ if (newChunks.length === 0) return 0;
808
+ const texts = newChunks.map((c) => c.text);
809
+ const embeddings = await this.embed(texts);
810
+ if (!this.vecDimensions && embeddings.length > 0) {
811
+ this.ensureVecTable(embeddings[0].length);
812
+ }
813
+ const insertChunk = db.prepare(`
814
+ INSERT INTO chunks (text, text_hash, role, source_type, source_id, agent_id, token_count, created_at)
815
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?)
816
+ `);
817
+ const insertVec = db.prepare(`
818
+ INSERT INTO chunks_vec (chunk_id, embedding) VALUES (?, ?)
819
+ `);
820
+ const transaction = db.transaction(() => {
821
+ for (let i = 0; i < newChunks.length; i++) {
822
+ const c = newChunks[i];
823
+ const hash = createHash("sha256").update(c.text).digest("hex");
824
+ const result = insertChunk.run(
825
+ c.text,
826
+ hash,
827
+ c.role,
828
+ c.source_type,
829
+ c.source_id,
830
+ c.agent_id,
831
+ c.token_count,
832
+ c.created_at || (/* @__PURE__ */ new Date()).toISOString()
833
+ );
834
+ const chunkId = typeof result.lastInsertRowid === "bigint" ? result.lastInsertRowid : BigInt(result.lastInsertRowid);
835
+ insertVec.run(chunkId, new Float32Array(embeddings[i]));
836
+ }
837
+ });
838
+ transaction();
839
+ const records = newChunks.map((chunk, i) => ({
840
+ text: chunk.text,
841
+ vector: embeddings[i],
842
+ role: chunk.role,
843
+ source_type: chunk.source_type,
844
+ source_id: chunk.source_id,
845
+ agent_id: chunk.agent_id,
846
+ token_count: chunk.token_count,
847
+ created_at: chunk.created_at || (/* @__PURE__ */ new Date()).toISOString()
848
+ }));
849
+ try {
850
+ if (!this.chunksTable) {
851
+ this.chunksTable = await this.lanceDb.createTable("chunks", records);
852
+ } else {
853
+ await this.chunksTable.add(records);
854
+ }
855
+ } catch (err) {
856
+ console.warn("LanceDB dual-write failed (non-fatal):", err.message);
857
+ }
858
+ return newChunks.length;
859
+ }
860
+ // ── Delta Sync (export/import pre-embedded chunks) ──
861
+ /** Export interface for delta sync payloads. */
862
+ static DELTA_VERSION = 1;
863
+ /** Export chunks with IDs greater than sinceId. Returns pre-embedded chunks for delta sync.
864
+ * Core calls this to build delta payloads for Nodes. */
865
+ exportChunksSince(sinceId) {
866
+ const db = this.sqliteDb;
867
+ const rows = db.prepare(`
868
+ SELECT c.id, c.text, c.text_hash, c.role, c.source_type, c.source_id,
869
+ c.agent_id, c.token_count, c.created_at, v.embedding
870
+ FROM chunks c
871
+ LEFT JOIN chunks_vec v ON v.chunk_id = c.id
872
+ WHERE c.id > ?
873
+ ORDER BY c.id ASC
874
+ `).all(sinceId);
875
+ return rows.map((row) => ({
876
+ id: row.id,
877
+ text: row.text,
878
+ text_hash: row.text_hash,
879
+ role: row.role,
880
+ source_type: row.source_type,
881
+ source_id: row.source_id,
882
+ agent_id: row.agent_id,
883
+ token_count: row.token_count,
884
+ created_at: row.created_at,
885
+ // Convert Float32Array buffer to number[] for JSON serialization
886
+ embedding: row.embedding ? Array.from(new Float32Array(row.embedding.buffer, row.embedding.byteOffset, row.embedding.byteLength / 4)) : null
887
+ }));
888
+ }
889
+ /** Get the highest chunk ID in the database. Used for watermark tracking. */
890
+ getMaxChunkId() {
891
+ const db = this.sqliteDb;
892
+ const row = db.prepare("SELECT MAX(id) as maxId FROM chunks").get();
893
+ return row.maxId || 0;
894
+ }
895
+ /** Import pre-embedded chunks from Core. Node calls this to apply delta payloads.
896
+ * Skips chunks that already exist (by text_hash). Does NOT re-embed. */
897
+ importChunks(exported) {
898
+ if (exported.length === 0) return 0;
899
+ const db = this.sqliteDb;
900
+ const firstWithEmbed = exported.find((c) => c.embedding && c.embedding.length > 0);
901
+ if (firstWithEmbed && !this.vecDimensions) {
902
+ this.ensureVecTable(firstWithEmbed.embedding.length);
903
+ }
904
+ const insertChunk = db.prepare(`
905
+ INSERT INTO chunks (text, text_hash, role, source_type, source_id, agent_id, token_count, created_at)
906
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?)
907
+ `);
908
+ const insertVec = db.prepare(`
909
+ INSERT INTO chunks_vec (chunk_id, embedding) VALUES (?, ?)
910
+ `);
911
+ const checkHash = db.prepare("SELECT 1 FROM chunks WHERE text_hash = ?");
912
+ let imported = 0;
913
+ const transaction = db.transaction(() => {
914
+ for (const chunk of exported) {
915
+ if (checkHash.get(chunk.text_hash)) continue;
916
+ const result = insertChunk.run(
917
+ chunk.text,
918
+ chunk.text_hash,
919
+ chunk.role,
920
+ chunk.source_type,
921
+ chunk.source_id,
922
+ chunk.agent_id,
923
+ chunk.token_count,
924
+ chunk.created_at
925
+ );
926
+ if (chunk.embedding && chunk.embedding.length > 0) {
927
+ const chunkId = typeof result.lastInsertRowid === "bigint" ? result.lastInsertRowid : BigInt(result.lastInsertRowid);
928
+ insertVec.run(chunkId, new Float32Array(chunk.embedding));
929
+ }
930
+ imported++;
931
+ }
932
+ });
933
+ transaction();
934
+ return imported;
935
+ }
936
+ // ── Recency helpers ──
937
+ recencyWeight(ageDays) {
938
+ return Math.max(0.3, Math.exp(-ageDays * 0.1));
939
+ }
940
+ /** Parse relative time strings ("24h", "7d", "30d") or ISO dates into ISO date strings. */
941
+ parseSince(since) {
942
+ const match = since.match(/^(\d+)(h|d)$/);
943
+ if (match) {
944
+ const [, num, unit] = match;
945
+ const ms = unit === "h" ? parseInt(num) * 36e5 : parseInt(num) * 864e5;
946
+ return new Date(Date.now() - ms).toISOString();
947
+ }
948
+ const parsed = new Date(since);
949
+ if (!isNaN(parsed.getTime())) return parsed.toISOString();
950
+ return void 0;
951
+ }
952
+ freshnessLabel(ageDays) {
953
+ if (ageDays < 3) return "fresh";
954
+ if (ageDays < 7) return "recent";
955
+ if (ageDays < 14) return "aging";
956
+ return "stale";
957
+ }
958
+ // ── Search (Hybrid: BM25 + Vector + RRF fusion + Recency) ──
959
+ async search(query, limit = 5, filter) {
960
+ const db = this.sqliteDb;
961
+ const sqliteChunks = db.prepare("SELECT COUNT(*) as count FROM chunks").get()?.count || 0;
962
+ let lanceChunks = 0;
963
+ if (this.chunksTable) {
964
+ try {
965
+ lanceChunks = await this.chunksTable.countRows();
966
+ } catch {
967
+ }
968
+ }
969
+ if (sqliteChunks === 0 || lanceChunks > 0 && sqliteChunks < lanceChunks * 0.5) {
970
+ return this.searchLanceFallback(query, limit, filter);
971
+ }
972
+ const sinceDate = filter?.since ? this.parseSince(filter.since) : void 0;
973
+ const untilDate = filter?.until ? this.parseSince(filter.until) : void 0;
974
+ const [embedding] = await this.embed([query]);
975
+ const fetchLimit = Math.max(limit * 5, 50);
976
+ const vecResults = this.searchVec(embedding, fetchLimit, { ...filter, sinceDate, untilDate });
977
+ const ftsResults = this.searchFTS(query, fetchLimit, { ...filter, sinceDate, untilDate });
978
+ const fused = this.reciprocalRankFusion([ftsResults, vecResults], [2, 1]);
979
+ const now = Date.now();
980
+ const scored = fused.map((r) => {
981
+ const ageDays = r.created_at ? (now - new Date(r.created_at).getTime()) / 864e5 : 0;
982
+ const recency = r.created_at ? this.recencyWeight(ageDays) : 1;
983
+ const rescaled = r.score * recency;
984
+ return {
985
+ ...r,
986
+ score: rescaled,
987
+ freshness: r.created_at ? this.freshnessLabel(ageDays) : void 0
988
+ };
989
+ });
990
+ const sorted = scored.sort((a, b) => b.score - a.score).slice(0, limit);
991
+ const topScore = sorted[0]?.score || 1;
992
+ return sorted.map((r) => ({ ...r, score: Math.min(r.score / topScore * 0.95, 0.95) }));
993
+ }
994
+ /** Deep search: query expansion + LLM re-ranking + position-aware blending.
995
+ * Falls back to standard search if no LLM provider is available.
996
+ * Supports intent disambiguation, candidateLimit tuning, and explain traces. */
997
+ async deepSearch(query, limit = 5, filter, options) {
998
+ const { deepSearch: deepSearchFn } = await Promise.resolve().then(() => (init_search_pipeline(), search_pipeline_exports));
999
+ return deepSearchFn(this, query, { limit, filter, ...options });
1000
+ }
1001
+ /** Structured search: pass pre-expanded queries to skip LLM expansion.
1002
+ * Each query is typed (lex, vec, hyde) and searched independently, then fused with RRF. */
1003
+ async structuredSearch(queries, limit = 5, filter) {
1004
+ const db = this.sqliteDb;
1005
+ const sinceDate = filter?.since ? this.parseSince(filter.since) : void 0;
1006
+ const untilDate = filter?.until ? this.parseSince(filter.until) : void 0;
1007
+ const internalFilter = { ...filter, sinceDate, untilDate };
1008
+ const allResultLists = [];
1009
+ for (const q of queries) {
1010
+ if (q.type === "lex") {
1011
+ const fts = this.searchFTS(q.text, Math.max(limit * 5, 50), internalFilter);
1012
+ if (fts.length > 0) allResultLists.push(fts);
1013
+ } else {
1014
+ const [embedding] = await this.embed([q.text]);
1015
+ const vec = this.searchVec(embedding, Math.max(limit * 5, 50), internalFilter);
1016
+ if (vec.length > 0) allResultLists.push(vec);
1017
+ }
1018
+ }
1019
+ const weights = allResultLists.map((_, i) => i === 0 ? 2 : 1);
1020
+ const fused = this.reciprocalRankFusion(allResultLists, weights);
1021
+ const now = Date.now();
1022
+ const scored = fused.map((r) => {
1023
+ const ageDays = r.created_at ? (now - new Date(r.created_at).getTime()) / 864e5 : 0;
1024
+ const recency = r.created_at ? this.recencyWeight(ageDays) : 1;
1025
+ return { ...r, score: r.score * recency, freshness: r.created_at ? this.freshnessLabel(ageDays) : void 0 };
1026
+ });
1027
+ const sorted = scored.sort((a, b) => b.score - a.score).slice(0, limit);
1028
+ const topScore = sorted[0]?.score || 1;
1029
+ return sorted.map((r) => ({ ...r, score: Math.min(r.score / topScore * 0.95, 0.95) }));
1030
+ }
1031
+ /** Vector search via sqlite-vec. Two-step pattern: MATCH first, then JOIN. */
1032
+ searchVec(embedding, limit, filter) {
1033
+ const db = this.sqliteDb;
1034
+ if (!this.vecDimensions) return [];
1035
+ const vecRows = db.prepare(`
1036
+ SELECT chunk_id, distance
1037
+ FROM chunks_vec
1038
+ WHERE embedding MATCH ? AND k = ?
1039
+ `).all(new Float32Array(embedding), limit);
1040
+ if (vecRows.length === 0) return [];
1041
+ const ids = vecRows.map((r) => r.chunk_id);
1042
+ const distMap = new Map(vecRows.map((r) => [r.chunk_id, r.distance]));
1043
+ const placeholders = ids.map(() => "?").join(",");
1044
+ let sql = `SELECT id, text, role, source_type, source_id, agent_id, created_at FROM chunks WHERE id IN (${placeholders})`;
1045
+ const params = [...ids];
1046
+ if (filter?.agent_id) {
1047
+ sql += " AND agent_id = ?";
1048
+ params.push(filter.agent_id);
1049
+ }
1050
+ if (filter?.source_type) {
1051
+ sql += " AND source_type = ?";
1052
+ params.push(filter.source_type);
1053
+ }
1054
+ if (filter?.sinceDate) {
1055
+ sql += " AND created_at >= ?";
1056
+ params.push(filter.sinceDate);
1057
+ }
1058
+ if (filter?.untilDate) {
1059
+ sql += " AND created_at < ?";
1060
+ params.push(filter.untilDate);
1061
+ }
1062
+ const rows = db.prepare(sql).all(...params);
1063
+ return rows.map((row) => ({
1064
+ text: row.text,
1065
+ role: row.role,
1066
+ score: 1 - (distMap.get(row.id) || 1),
1067
+ // cosine similarity from distance
1068
+ source_type: row.source_type,
1069
+ source_id: row.source_id,
1070
+ agent_id: row.agent_id,
1071
+ created_at: row.created_at
1072
+ }));
1073
+ }
1074
+ /** Full-text search via FTS5 with BM25 scoring. */
1075
+ searchFTS(query, limit, filter) {
1076
+ const db = this.sqliteDb;
1077
+ const ftsQuery = this.buildFTS5Query(query);
1078
+ if (!ftsQuery) return [];
1079
+ let sql = `
1080
+ SELECT c.id, c.text, c.role, c.source_type, c.source_id, c.agent_id, c.created_at,
1081
+ bm25(chunks_fts) as bm25_score
1082
+ FROM chunks_fts f
1083
+ JOIN chunks c ON c.id = f.rowid
1084
+ WHERE chunks_fts MATCH ?
1085
+ `;
1086
+ const params = [ftsQuery];
1087
+ if (filter?.agent_id) {
1088
+ sql += " AND c.agent_id = ?";
1089
+ params.push(filter.agent_id);
1090
+ }
1091
+ if (filter?.source_type) {
1092
+ sql += " AND c.source_type = ?";
1093
+ params.push(filter.source_type);
1094
+ }
1095
+ if (filter?.sinceDate) {
1096
+ sql += " AND c.created_at >= ?";
1097
+ params.push(filter.sinceDate);
1098
+ }
1099
+ if (filter?.untilDate) {
1100
+ sql += " AND c.created_at < ?";
1101
+ params.push(filter.untilDate);
1102
+ }
1103
+ sql += " ORDER BY bm25_score LIMIT ?";
1104
+ params.push(limit);
1105
+ const rows = db.prepare(sql).all(...params);
1106
+ return rows.map((row) => ({
1107
+ text: row.text,
1108
+ role: row.role,
1109
+ // BM25 scores are negative (lower = better). Normalize to [0..1).
1110
+ // |x| / (1 + |x|) maps: strong(-10)->0.91, medium(-2)->0.67, weak(-0.5)->0.33
1111
+ score: Math.abs(row.bm25_score) / (1 + Math.abs(row.bm25_score)),
1112
+ source_type: row.source_type,
1113
+ source_id: row.source_id,
1114
+ agent_id: row.agent_id,
1115
+ created_at: row.created_at
1116
+ }));
1117
+ }
1118
+ /** Build a safe FTS5 query from user input. */
1119
+ buildFTS5Query(query) {
1120
+ const terms = query.split(/\s+/).map((t) => t.replace(/[^\p{L}\p{N}']/gu, "").toLowerCase()).filter((t) => t.length > 0);
1121
+ if (terms.length === 0) return null;
1122
+ if (terms.length === 1) return `"${terms[0]}"*`;
1123
+ return terms.map((t) => `"${t}"*`).join(" AND ");
1124
+ }
1125
+ /**
1126
+ * Reciprocal Rank Fusion. Ported from QMD (MIT License, Tobi Lutke, 2024-2026).
1127
+ * Fuses multiple ranked result lists into one using RRF scoring.
1128
+ * Uses text content as dedup key (instead of QMD's file path).
1129
+ */
1130
+ reciprocalRankFusion(resultLists, weights = [], k = 60) {
1131
+ const scores = /* @__PURE__ */ new Map();
1132
+ for (let listIdx = 0; listIdx < resultLists.length; listIdx++) {
1133
+ const list = resultLists[listIdx];
1134
+ if (!list) continue;
1135
+ const weight = weights[listIdx] ?? 1;
1136
+ for (let rank = 0; rank < list.length; rank++) {
1137
+ const result = list[rank];
1138
+ if (!result) continue;
1139
+ const rrfContribution = weight / (k + rank + 1);
1140
+ const dedup = result.text.slice(0, 200);
1141
+ const existing = scores.get(dedup);
1142
+ if (existing) {
1143
+ existing.rrfScore += rrfContribution;
1144
+ existing.topRank = Math.min(existing.topRank, rank);
1145
+ } else {
1146
+ scores.set(dedup, {
1147
+ result,
1148
+ rrfScore: rrfContribution,
1149
+ topRank: rank
1150
+ });
1151
+ }
1152
+ }
1153
+ }
1154
+ for (const entry of scores.values()) {
1155
+ if (entry.topRank === 0) {
1156
+ entry.rrfScore += 0.05;
1157
+ } else if (entry.topRank <= 2) {
1158
+ entry.rrfScore += 0.02;
1159
+ }
1160
+ }
1161
+ return Array.from(scores.values()).sort((a, b) => b.rrfScore - a.rrfScore).map((e) => ({ ...e.result, score: e.rrfScore }));
1162
+ }
1163
+ /** LanceDB fallback for search (used when sqlite-vec tables are empty, pre-migration). */
1164
+ async searchLanceFallback(query, limit, filter) {
1165
+ if (!this.chunksTable) return [];
1166
+ const [embedding] = await this.embed([query]);
1167
+ const fetchLimit = Math.max(limit * 3, 30);
1168
+ let queryBuilder = this.chunksTable.vectorSearch(embedding).distanceType("cosine").limit(fetchLimit);
1169
+ if (filter?.agent_id) {
1170
+ queryBuilder = queryBuilder.where(`agent_id = '${filter.agent_id}'`);
1171
+ }
1172
+ if (filter?.source_type) {
1173
+ queryBuilder = queryBuilder.where(`source_type = '${filter.source_type}'`);
1174
+ }
1175
+ const results = await queryBuilder.toArray();
1176
+ const now = Date.now();
1177
+ return results.map((row) => {
1178
+ const cosine = row._distance != null ? 1 - row._distance : 0;
1179
+ const createdAt = row.created_at || "";
1180
+ const ageDays = createdAt ? (now - new Date(createdAt).getTime()) / 864e5 : 0;
1181
+ const weight = createdAt ? this.recencyWeight(ageDays) : 1;
1182
+ return {
1183
+ text: row.text,
1184
+ role: row.role,
1185
+ score: cosine * weight,
1186
+ source_type: row.source_type,
1187
+ source_id: row.source_id,
1188
+ agent_id: row.agent_id,
1189
+ created_at: createdAt,
1190
+ freshness: createdAt ? this.freshnessLabel(ageDays) : void 0
1191
+ };
1192
+ }).sort((a, b) => b.score - a.score).slice(0, limit);
1193
+ }
1194
+ // ── Remember (explicit fact storage) ──
1195
+ async remember(text, category = "fact") {
1196
+ const db = this.sqliteDb;
1197
+ const now = (/* @__PURE__ */ new Date()).toISOString();
1198
+ const stmt = db.prepare(`
1199
+ INSERT INTO memories (text, category, confidence, source_ids, status, created_at, updated_at)
1200
+ VALUES (?, ?, 1.0, '[]', 'active', ?, ?)
1201
+ `);
1202
+ const result = stmt.run(text, category, now, now);
1203
+ await this.ingest([{
1204
+ text,
1205
+ role: "system",
1206
+ source_type: "manual",
1207
+ source_id: `memory:${result.lastInsertRowid}`,
1208
+ agent_id: "system",
1209
+ token_count: Math.ceil(text.length / 4),
1210
+ created_at: now
1211
+ }]);
1212
+ return result.lastInsertRowid;
1213
+ }
1214
+ // ── Forget (deprecate a memory) ──
1215
+ forget(memoryId) {
1216
+ const db = this.sqliteDb;
1217
+ const now = (/* @__PURE__ */ new Date()).toISOString();
1218
+ const result = db.prepare(`
1219
+ UPDATE memories SET status = 'deprecated', updated_at = ? WHERE id = ? AND status = 'active'
1220
+ `).run(now, memoryId);
1221
+ return result.changes > 0;
1222
+ }
1223
+ // ── Status ──
1224
+ async status() {
1225
+ const db = this.sqliteDb;
1226
+ const sqliteChunks = db.prepare("SELECT COUNT(*) as count FROM chunks").get()?.count || 0;
1227
+ let lanceChunks = 0;
1228
+ if (this.chunksTable) {
1229
+ try {
1230
+ lanceChunks = await this.chunksTable.countRows();
1231
+ } catch {
1232
+ }
1233
+ }
1234
+ const chunks = Math.max(sqliteChunks, lanceChunks);
1235
+ const oldest = db.prepare("SELECT MIN(created_at) as ts FROM chunks").get()?.ts || null;
1236
+ const newest = db.prepare("SELECT MAX(created_at) as ts FROM chunks").get()?.ts || null;
1237
+ const memories = db.prepare("SELECT COUNT(*) as count FROM memories WHERE status = ?").get("active")?.count || 0;
1238
+ const sources = db.prepare("SELECT COUNT(*) as count FROM sources").get()?.count || 0;
1239
+ const chunkAgentRows = db.prepare("SELECT DISTINCT agent_id FROM chunks WHERE agent_id IS NOT NULL").all();
1240
+ const sourceAgentRows = db.prepare("SELECT DISTINCT agent_id FROM sources").all();
1241
+ const captureAgentRows = db.prepare("SELECT DISTINCT agent_id FROM capture_state").all();
1242
+ const agents = [.../* @__PURE__ */ new Set([
1243
+ ...chunkAgentRows.map((r) => r.agent_id),
1244
+ ...sourceAgentRows.map((r) => r.agent_id),
1245
+ ...captureAgentRows.map((r) => r.agent_id)
1246
+ ])];
1247
+ const captureInfo = db.prepare(
1248
+ "SELECT COUNT(*) as count, MAX(last_capture_at) as latest FROM capture_state"
1249
+ ).get();
1250
+ return {
1251
+ chunks,
1252
+ memories,
1253
+ sources,
1254
+ agents,
1255
+ oldestChunk: oldest,
1256
+ newestChunk: newest,
1257
+ embeddingProvider: this.config.embeddingProvider,
1258
+ dataDir: this.config.dataDir,
1259
+ capturedSessions: captureInfo?.count || 0,
1260
+ latestCapture: captureInfo?.latest || null
1261
+ };
1262
+ }
1263
+ // ── Capture State (for incremental ingestion) ──
1264
+ getCaptureState(agentId, sourceId) {
1265
+ const db = this.sqliteDb;
1266
+ const row = db.prepare("SELECT last_message_count, capture_count FROM capture_state WHERE agent_id = ? AND source_id = ?").get(agentId, sourceId);
1267
+ if (!row) return { lastMessageCount: 0, captureCount: 0 };
1268
+ return {
1269
+ lastMessageCount: row.last_message_count,
1270
+ captureCount: row.capture_count
1271
+ };
1272
+ }
1273
+ setCaptureState(agentId, sourceId, messageCount, captureCount) {
1274
+ const db = this.sqliteDb;
1275
+ db.prepare(`
1276
+ INSERT OR REPLACE INTO capture_state (agent_id, source_id, last_message_count, capture_count, last_capture_at)
1277
+ VALUES (?, ?, ?, ?, ?)
1278
+ `).run(agentId, sourceId, messageCount, captureCount, (/* @__PURE__ */ new Date()).toISOString());
1279
+ }
1280
+ // ── Source File Indexing (optional feature) ──
1281
+ //
1282
+ // Add directories as "collections", sync to index/re-index changed files.
1283
+ // All source chunks get source_type='file' so they're searchable alongside
1284
+ // conversations and memories. Nothing here is required... you can use MC
1285
+ // without ever touching sources.
1286
+ // Default patterns for files worth indexing
1287
+ static DEFAULT_INCLUDE = [
1288
+ "**/*.ts",
1289
+ "**/*.js",
1290
+ "**/*.tsx",
1291
+ "**/*.jsx",
1292
+ "**/*.py",
1293
+ "**/*.rs",
1294
+ "**/*.go",
1295
+ "**/*.java",
1296
+ "**/*.md",
1297
+ "**/*.txt",
1298
+ "**/*.json",
1299
+ "**/*.yaml",
1300
+ "**/*.yml",
1301
+ "**/*.toml",
1302
+ "**/*.sh",
1303
+ "**/*.bash",
1304
+ "**/*.zsh",
1305
+ "**/*.css",
1306
+ "**/*.html",
1307
+ "**/*.svg",
1308
+ "**/*.sql",
1309
+ "**/*.graphql",
1310
+ "**/*.c",
1311
+ "**/*.cpp",
1312
+ "**/*.h",
1313
+ "**/*.hpp",
1314
+ "**/*.swift",
1315
+ "**/*.kt",
1316
+ "**/*.rb",
1317
+ "**/*.env.example",
1318
+ "**/*.gitignore",
1319
+ "**/Makefile",
1320
+ "**/Dockerfile",
1321
+ "**/Cargo.toml",
1322
+ "**/package.json",
1323
+ "**/tsconfig.json"
1324
+ ];
1325
+ static DEFAULT_IGNORE = [
1326
+ "**/node_modules/**",
1327
+ "**/.git/**",
1328
+ "**/dist/**",
1329
+ "**/build/**",
1330
+ "**/.next/**",
1331
+ "**/.cache/**",
1332
+ "**/coverage/**",
1333
+ "**/__pycache__/**",
1334
+ "**/target/**",
1335
+ "**/vendor/**",
1336
+ "**/.venv/**",
1337
+ "**/*.lock",
1338
+ "**/package-lock.json",
1339
+ "**/yarn.lock",
1340
+ "**/bun.lockb",
1341
+ "**/*.min.js",
1342
+ "**/*.min.css",
1343
+ "**/*.map",
1344
+ "**/*.png",
1345
+ "**/*.jpg",
1346
+ "**/*.jpeg",
1347
+ "**/*.gif",
1348
+ "**/*.ico",
1349
+ "**/*.webp",
1350
+ "**/*.woff",
1351
+ "**/*.woff2",
1352
+ "**/*.ttf",
1353
+ "**/*.eot",
1354
+ "**/*.mp3",
1355
+ "**/*.mp4",
1356
+ "**/*.wav",
1357
+ "**/*.ogg",
1358
+ "**/*.webm",
1359
+ "**/*.zip",
1360
+ "**/*.tar",
1361
+ "**/*.gz",
1362
+ "**/*.br",
1363
+ "**/*.sqlite",
1364
+ "**/*.db",
1365
+ "**/*.lance/**",
1366
+ "**/*.jsonl",
1367
+ "**/secrets/**",
1368
+ "**/.env"
1369
+ ];
1370
+ /** Add a directory as a source collection for indexing. */
1371
+ async sourcesAdd(rootPath, name, options) {
1372
+ const db = this.sqliteDb;
1373
+ const now = (/* @__PURE__ */ new Date()).toISOString();
1374
+ const includePatterns = JSON.stringify(options?.include || _Crystal.DEFAULT_INCLUDE);
1375
+ const ignorePatterns = JSON.stringify(options?.ignore || _Crystal.DEFAULT_IGNORE);
1376
+ const existing = db.prepare("SELECT * FROM source_collections WHERE name = ?").get(name);
1377
+ if (existing) {
1378
+ throw new Error(`Collection "${name}" already exists. Use sourcesSync() to update it.`);
1379
+ }
1380
+ db.prepare(`
1381
+ INSERT INTO source_collections (name, root_path, glob_patterns, ignore_patterns, created_at)
1382
+ VALUES (?, ?, ?, ?, ?)
1383
+ `).run(name, rootPath, includePatterns, ignorePatterns, now);
1384
+ const row = db.prepare("SELECT * FROM source_collections WHERE name = ?").get(name);
1385
+ return row;
1386
+ }
1387
+ /** Remove a source collection and its file records. Chunks remain in LanceDB. */
1388
+ sourcesRemove(name) {
1389
+ const db = this.sqliteDb;
1390
+ const col = db.prepare("SELECT id FROM source_collections WHERE name = ?").get(name);
1391
+ if (!col) return false;
1392
+ db.prepare("DELETE FROM source_files WHERE collection_id = ?").run(col.id);
1393
+ db.prepare("DELETE FROM source_collections WHERE id = ?").run(col.id);
1394
+ return true;
1395
+ }
1396
+ /** Sync a collection: scan files, detect changes, re-index what changed. */
1397
+ async sourcesSync(name, options) {
1398
+ const db = this.sqliteDb;
1399
+ const startTime = Date.now();
1400
+ const batchSize = options?.batchSize || 20;
1401
+ const col = db.prepare("SELECT * FROM source_collections WHERE name = ?").get(name);
1402
+ if (!col) throw new Error(`Collection "${name}" not found. Add it first with sourcesAdd().`);
1403
+ const includePatterns = JSON.parse(col.glob_patterns);
1404
+ const ignorePatterns = JSON.parse(col.ignore_patterns);
1405
+ const files = this.scanDirectory(col.root_path, includePatterns, ignorePatterns);
1406
+ const existingFiles = /* @__PURE__ */ new Map();
1407
+ const rows = db.prepare("SELECT id, file_path, file_hash FROM source_files WHERE collection_id = ?").all(col.id);
1408
+ for (const row of rows) {
1409
+ existingFiles.set(row.file_path, { id: row.id, file_hash: row.file_hash });
1410
+ }
1411
+ let added = 0;
1412
+ let updated = 0;
1413
+ let removed = 0;
1414
+ let chunksAdded = 0;
1415
+ const now = (/* @__PURE__ */ new Date()).toISOString();
1416
+ const toIndex = [];
1417
+ for (const absPath of files) {
1418
+ const relPath = relative(col.root_path, absPath);
1419
+ let content;
1420
+ try {
1421
+ content = readFileSync2(absPath, "utf-8");
1422
+ } catch {
1423
+ continue;
1424
+ }
1425
+ const stat = statSync(absPath);
1426
+ if (stat.size > 500 * 1024) continue;
1427
+ const hash = createHash("sha256").update(content).digest("hex");
1428
+ const existing = existingFiles.get(relPath);
1429
+ if (existing) {
1430
+ existingFiles.delete(relPath);
1431
+ if (existing.file_hash === hash) continue;
1432
+ toIndex.push({ relPath, absPath, hash, size: stat.size, isUpdate: true });
1433
+ } else {
1434
+ toIndex.push({ relPath, absPath, hash, size: stat.size, isUpdate: false });
1435
+ }
1436
+ }
1437
+ if (options?.dryRun) {
1438
+ const newFiles = toIndex.filter((f) => !f.isUpdate).length;
1439
+ const updatedFiles = toIndex.filter((f) => f.isUpdate).length;
1440
+ return {
1441
+ collection: name,
1442
+ added: newFiles,
1443
+ updated: updatedFiles,
1444
+ removed: existingFiles.size,
1445
+ chunks_added: 0,
1446
+ duration_ms: Date.now() - startTime
1447
+ };
1448
+ }
1449
+ for (let i = 0; i < toIndex.length; i += batchSize) {
1450
+ const batch = toIndex.slice(i, i + batchSize);
1451
+ const allChunks = [];
1452
+ for (const file of batch) {
1453
+ const content = readFileSync2(file.absPath, "utf-8");
1454
+ const ext = extname(file.absPath);
1455
+ const fileName = basename(file.absPath);
1456
+ const header = `File: ${file.relPath}
1457
+
1458
+ `;
1459
+ const textChunks = this.chunkText(header + content, 400, 80);
1460
+ const fileChunks = textChunks.map((text) => ({
1461
+ text,
1462
+ role: "system",
1463
+ source_type: "file",
1464
+ source_id: `file:${name}:${file.relPath}`,
1465
+ agent_id: "system",
1466
+ token_count: Math.ceil(text.length / 4),
1467
+ created_at: now
1468
+ }));
1469
+ allChunks.push(...fileChunks);
1470
+ if (file.isUpdate) {
1471
+ db.prepare(`
1472
+ UPDATE source_files SET file_hash = ?, file_size = ?, chunk_count = ?, last_indexed_at = ?
1473
+ WHERE collection_id = ? AND file_path = ?
1474
+ `).run(file.hash, file.size, fileChunks.length, now, col.id, file.relPath);
1475
+ updated++;
1476
+ } else {
1477
+ db.prepare(`
1478
+ INSERT INTO source_files (collection_id, file_path, file_hash, file_size, chunk_count, last_indexed_at)
1479
+ VALUES (?, ?, ?, ?, ?, ?)
1480
+ `).run(col.id, file.relPath, file.hash, file.size, fileChunks.length, now);
1481
+ added++;
1482
+ }
1483
+ }
1484
+ if (allChunks.length > 0) {
1485
+ const ingested = await this.ingest(allChunks);
1486
+ chunksAdded += ingested;
1487
+ }
1488
+ }
1489
+ for (const [relPath, { id }] of existingFiles) {
1490
+ db.prepare("DELETE FROM source_files WHERE id = ?").run(id);
1491
+ removed++;
1492
+ }
1493
+ const fileCount = db.prepare("SELECT COUNT(*) as count FROM source_files WHERE collection_id = ?").get(col.id).count;
1494
+ const chunkCount = db.prepare("SELECT SUM(chunk_count) as total FROM source_files WHERE collection_id = ?").get(col.id).total || 0;
1495
+ db.prepare("UPDATE source_collections SET file_count = ?, chunk_count = ?, last_sync_at = ? WHERE id = ?").run(fileCount, chunkCount, now, col.id);
1496
+ return {
1497
+ collection: name,
1498
+ added,
1499
+ updated,
1500
+ removed,
1501
+ chunks_added: chunksAdded,
1502
+ duration_ms: Date.now() - startTime
1503
+ };
1504
+ }
1505
+ /** Get status of all source collections. */
1506
+ sourcesStatus() {
1507
+ const db = this.sqliteDb;
1508
+ const collections = db.prepare("SELECT name, root_path, file_count, chunk_count, last_sync_at FROM source_collections").all();
1509
+ const totalFiles = collections.reduce((sum, c) => sum + c.file_count, 0);
1510
+ const totalChunks = collections.reduce((sum, c) => sum + c.chunk_count, 0);
1511
+ return {
1512
+ collections: collections.map((c) => ({
1513
+ name: c.name,
1514
+ root_path: c.root_path,
1515
+ file_count: c.file_count,
1516
+ chunk_count: c.chunk_count,
1517
+ last_sync_at: c.last_sync_at
1518
+ })),
1519
+ total_files: totalFiles,
1520
+ total_chunks: totalChunks
1521
+ };
1522
+ }
1523
+ /** Scan a directory recursively, matching include/ignore patterns. */
1524
+ scanDirectory(rootPath, includePatterns, ignorePatterns) {
1525
+ const results = [];
1526
+ const allowedExtensions = /* @__PURE__ */ new Set();
1527
+ const allowedExactNames = /* @__PURE__ */ new Set();
1528
+ for (const pattern of includePatterns) {
1529
+ const extMatch = pattern.match(/\*\*\/\*(\.\w+)$/);
1530
+ if (extMatch) {
1531
+ allowedExtensions.add(extMatch[1]);
1532
+ }
1533
+ const nameMatch = pattern.match(/\*\*\/([^*]+)$/);
1534
+ if (nameMatch && !nameMatch[1].startsWith("*.")) {
1535
+ allowedExactNames.add(nameMatch[1]);
1536
+ }
1537
+ }
1538
+ const ignoreDirs = /* @__PURE__ */ new Set();
1539
+ for (const pattern of ignorePatterns) {
1540
+ const dirMatch = pattern.match(/\*\*\/([^/*]+)\/\*\*$/);
1541
+ if (dirMatch) {
1542
+ ignoreDirs.add(dirMatch[1]);
1543
+ }
1544
+ }
1545
+ const ignoreFiles = /* @__PURE__ */ new Set();
1546
+ for (const pattern of ignorePatterns) {
1547
+ const fileMatch = pattern.match(/\*\*\/\*(\.\w+)$/);
1548
+ if (fileMatch) {
1549
+ ignoreFiles.add(fileMatch[1]);
1550
+ }
1551
+ const exactMatch = pattern.match(/\*\*\/([^*]+)$/);
1552
+ if (exactMatch && !exactMatch[1].includes("/")) {
1553
+ ignoreFiles.add(exactMatch[1]);
1554
+ }
1555
+ }
1556
+ const walk = (dir) => {
1557
+ let entries;
1558
+ try {
1559
+ entries = readdirSync(dir);
1560
+ } catch {
1561
+ return;
1562
+ }
1563
+ for (const entry of entries) {
1564
+ const fullPath = join2(dir, entry);
1565
+ let stat;
1566
+ try {
1567
+ stat = statSync(fullPath);
1568
+ } catch {
1569
+ continue;
1570
+ }
1571
+ if (stat.isDirectory()) {
1572
+ if (ignoreDirs.has(entry)) continue;
1573
+ if (entry.startsWith(".")) continue;
1574
+ walk(fullPath);
1575
+ } else if (stat.isFile()) {
1576
+ const ext = extname(entry);
1577
+ if (ignoreFiles.has(ext)) continue;
1578
+ if (ignoreFiles.has(entry)) continue;
1579
+ if (allowedExtensions.has(ext) || allowedExactNames.has(entry)) {
1580
+ results.push(fullPath);
1581
+ }
1582
+ }
1583
+ }
1584
+ };
1585
+ walk(rootPath);
1586
+ return results;
1587
+ }
1588
+ // ── Orphan Cleanup ──
1589
+ /** Clean orphaned entries in chunks_vec and chunks_fts that no longer have
1590
+ * corresponding rows in the chunks table. Returns counts of what was found/cleaned. */
1591
+ cleanOrphans(options) {
1592
+ const db = this.sqliteDb;
1593
+ const dryRun = options?.dryRun ?? false;
1594
+ const orphanedVec = db.prepare(
1595
+ "SELECT COUNT(*) as cnt FROM chunks_vec WHERE chunk_id NOT IN (SELECT id FROM chunks)"
1596
+ ).get().cnt;
1597
+ const orphanedFts = db.prepare(
1598
+ "SELECT COUNT(*) as cnt FROM chunks_fts WHERE rowid NOT IN (SELECT id FROM chunks)"
1599
+ ).get().cnt;
1600
+ if (dryRun) {
1601
+ return { orphanedVec, orphanedFts, cleanedVec: 0, cleanedFts: 0, dryRun: true };
1602
+ }
1603
+ let cleanedVec = 0;
1604
+ if (orphanedVec > 0) {
1605
+ const ids = db.prepare(
1606
+ "SELECT chunk_id FROM chunks_vec WHERE chunk_id NOT IN (SELECT id FROM chunks)"
1607
+ ).all();
1608
+ const del = db.prepare("DELETE FROM chunks_vec WHERE chunk_id = ?");
1609
+ const BATCH = 1e3;
1610
+ for (let i = 0; i < ids.length; i += BATCH) {
1611
+ const batch = ids.slice(i, i + BATCH);
1612
+ db.transaction(() => {
1613
+ for (const r of batch) {
1614
+ del.run(r.chunk_id);
1615
+ cleanedVec++;
1616
+ }
1617
+ })();
1618
+ }
1619
+ }
1620
+ let cleanedFts = 0;
1621
+ if (orphanedFts > 0) {
1622
+ db.exec("DELETE FROM chunks_fts");
1623
+ db.exec("INSERT INTO chunks_fts(rowid, text) SELECT id, text FROM chunks");
1624
+ cleanedFts = orphanedFts;
1625
+ }
1626
+ return { orphanedVec, orphanedFts, cleanedVec, cleanedFts, dryRun: false };
1627
+ }
1628
+ // ── Cleanup ──
1629
+ close() {
1630
+ this.sqliteDb?.close();
1631
+ }
1632
+ };
1633
+ function resolveConfig(overrides) {
1634
+ const HOME3 = process.env.HOME || "";
1635
+ const ldmMemory = join2(HOME3, ".ldm", "memory");
1636
+ let dataDir = overrides?.dataDir || process.env.CRYSTAL_DATA_DIR;
1637
+ if (!dataDir) {
1638
+ if (existsSync2(join2(ldmMemory, "crystal.db"))) {
1639
+ dataDir = ldmMemory;
1640
+ } else {
1641
+ const legacyDir = join2(HOME3, ".openclaw", "memory-crystal");
1642
+ if (existsSync2(join2(legacyDir, "crystal.db"))) {
1643
+ dataDir = legacyDir;
1644
+ } else {
1645
+ dataDir = ldmMemory;
1646
+ }
1647
+ }
1648
+ }
1649
+ loadEnvFile(join2(dataDir, ".env"));
1650
+ const openaiApiKey = overrides?.openaiApiKey || process.env.OPENAI_API_KEY || opRead("OpenAI API", "api key");
1651
+ const googleApiKey = overrides?.googleApiKey || process.env.GOOGLE_API_KEY || opRead("Google AI", "api key");
1652
+ const remoteToken = overrides?.remoteToken || process.env.CRYSTAL_REMOTE_TOKEN || opRead("Memory Crystal Remote", "token");
1653
+ return {
1654
+ dataDir,
1655
+ embeddingProvider: overrides?.embeddingProvider || process.env.CRYSTAL_EMBEDDING_PROVIDER || "openai",
1656
+ openaiApiKey,
1657
+ openaiModel: overrides?.openaiModel || process.env.CRYSTAL_OPENAI_MODEL || "text-embedding-3-small",
1658
+ ollamaHost: overrides?.ollamaHost || process.env.CRYSTAL_OLLAMA_HOST || "http://localhost:11434",
1659
+ ollamaModel: overrides?.ollamaModel || process.env.CRYSTAL_OLLAMA_MODEL || "nomic-embed-text",
1660
+ googleApiKey,
1661
+ googleModel: overrides?.googleModel || process.env.CRYSTAL_GOOGLE_MODEL || "text-embedding-004",
1662
+ remoteUrl: overrides?.remoteUrl || process.env.CRYSTAL_REMOTE_URL,
1663
+ remoteToken
1664
+ };
1665
+ }
1666
+ function loadEnvFile(path) {
1667
+ if (!existsSync2(path)) return;
1668
+ const content = readFileSync2(path, "utf8");
1669
+ for (const line of content.split("\n")) {
1670
+ const trimmed = line.trim();
1671
+ if (!trimmed || trimmed.startsWith("#")) continue;
1672
+ const eqIdx = trimmed.indexOf("=");
1673
+ if (eqIdx === -1) continue;
1674
+ const key = trimmed.slice(0, eqIdx).trim();
1675
+ let value = trimmed.slice(eqIdx + 1).trim();
1676
+ if (value.startsWith('"') && value.endsWith('"') || value.startsWith("'") && value.endsWith("'")) {
1677
+ value = value.slice(1, -1);
1678
+ }
1679
+ if (key && !process.env[key]) {
1680
+ process.env[key] = value;
1681
+ }
1682
+ }
1683
+ }
1684
+ function opRead(item, field) {
1685
+ try {
1686
+ const HOME3 = process.env.HOME || "";
1687
+ let saTokenPath = join2(HOME3, ".ldm", "secrets", "op-sa-token");
1688
+ if (!existsSync2(saTokenPath)) {
1689
+ saTokenPath = join2(HOME3, ".openclaw", "secrets", "op-sa-token");
1690
+ }
1691
+ if (!existsSync2(saTokenPath)) return void 0;
1692
+ const saToken = readFileSync2(saTokenPath, "utf8").trim();
1693
+ return execSync2(`op read "op://Agent Secrets/${item}/${field}" 2>/dev/null`, {
1694
+ encoding: "utf8",
1695
+ env: { ...process.env, OP_SERVICE_ACCOUNT_TOKEN: saToken },
1696
+ timeout: 1e4
1697
+ }).trim() || void 0;
1698
+ } catch {
1699
+ return void 0;
1700
+ }
1701
+ }
1702
+ var RemoteCrystal = class {
1703
+ url;
1704
+ token;
1705
+ constructor(url, token) {
1706
+ this.url = url.replace(/\/$/, "");
1707
+ this.token = token;
1708
+ }
1709
+ async init() {
1710
+ const resp = await fetch(`${this.url}/health`);
1711
+ if (!resp.ok) {
1712
+ throw new Error(`Remote crystal unreachable: ${resp.status}`);
1713
+ }
1714
+ }
1715
+ async request(path, body) {
1716
+ const resp = await fetch(`${this.url}${path}`, {
1717
+ method: body ? "POST" : "GET",
1718
+ headers: {
1719
+ "Authorization": `Bearer ${this.token}`,
1720
+ "Content-Type": "application/json"
1721
+ },
1722
+ ...body ? { body: JSON.stringify(body) } : {}
1723
+ });
1724
+ if (!resp.ok) {
1725
+ const err = await resp.text();
1726
+ throw new Error(`Remote crystal error ${resp.status}: ${err}`);
1727
+ }
1728
+ return resp.json();
1729
+ }
1730
+ async search(query, limit = 5, filter) {
1731
+ const data = await this.request("/search", { query, limit, agent_id: filter?.agent_id });
1732
+ return data.results || [];
1733
+ }
1734
+ async ingest(chunks) {
1735
+ const data = await this.request("/ingest", { chunks });
1736
+ return data.ingested || 0;
1737
+ }
1738
+ async remember(text, category = "fact") {
1739
+ const data = await this.request("/remember", { text, category });
1740
+ return data.id;
1741
+ }
1742
+ forget(memoryId) {
1743
+ return this.request("/forget", { id: memoryId }).then((d) => d.ok);
1744
+ }
1745
+ async status() {
1746
+ const data = await this.request("/status");
1747
+ return {
1748
+ chunks: data.chunks || 0,
1749
+ memories: data.memories || 0,
1750
+ sources: 0,
1751
+ agents: data.agents || [],
1752
+ oldestChunk: data.oldestChunk,
1753
+ newestChunk: data.newestChunk,
1754
+ embeddingProvider: "remote",
1755
+ dataDir: this.url,
1756
+ capturedSessions: data.capturedSessions || 0,
1757
+ latestCapture: data.newestChunk
1758
+ };
1759
+ }
1760
+ // Expose chunkText from a local Crystal instance for cc-hook to use
1761
+ chunkText(text) {
1762
+ const targetChars = 400 * 4;
1763
+ const overlapChars = 80 * 4;
1764
+ if (text.length <= targetChars) return [text];
1765
+ const chunks = [];
1766
+ let start = 0;
1767
+ while (start < text.length) {
1768
+ let end = start + targetChars;
1769
+ if (end >= text.length) {
1770
+ chunks.push(text.slice(start));
1771
+ break;
1772
+ }
1773
+ const paraBreak = text.lastIndexOf("\n\n", end);
1774
+ if (paraBreak > start + targetChars * 0.5) end = paraBreak;
1775
+ else {
1776
+ const sentBreak = text.lastIndexOf(". ", end);
1777
+ if (sentBreak > start + targetChars * 0.5) end = sentBreak + 1;
1778
+ }
1779
+ chunks.push(text.slice(start, end));
1780
+ start = end - overlapChars;
1781
+ }
1782
+ return chunks;
1783
+ }
1784
+ };
1785
+ function createCrystal(config) {
1786
+ if (config.remoteUrl && config.remoteToken) {
1787
+ return new RemoteCrystal(config.remoteUrl, config.remoteToken);
1788
+ }
1789
+ return new Crystal(config);
1790
+ }
1791
+
1792
+ // src/ldm.ts
1793
+ import { existsSync as existsSync3, mkdirSync as mkdirSync2, readFileSync as readFileSync3, writeFileSync, copyFileSync, chmodSync, readdirSync as readdirSync2 } from "fs";
1794
+ import { join as join3, dirname } from "path";
1795
+ import { execSync as execSync3 } from "child_process";
1796
+ import { fileURLToPath } from "url";
1797
+ var HOME = process.env.HOME || "";
1798
+ var LDM_ROOT = join3(HOME, ".ldm");
1799
+ function loadAgentConfig(id) {
1800
+ const cfgPath = join3(LDM_ROOT, "agents", id, "config.json");
1801
+ try {
1802
+ if (existsSync3(cfgPath)) return JSON.parse(readFileSync3(cfgPath, "utf-8"));
1803
+ } catch {
1804
+ }
1805
+ return null;
1806
+ }
1807
+ function getAgentId(harnessHint) {
1808
+ if (process.env.CRYSTAL_AGENT_ID) return process.env.CRYSTAL_AGENT_ID;
1809
+ const agentsDir = join3(LDM_ROOT, "agents");
1810
+ if (existsSync3(agentsDir)) {
1811
+ try {
1812
+ for (const d of readdirSync2(agentsDir)) {
1813
+ const cfg = loadAgentConfig(d);
1814
+ if (!cfg || !cfg.agentId) continue;
1815
+ if (!harnessHint) return cfg.agentId;
1816
+ if (harnessHint === "claude-code" && cfg.harness === "claude-code-cli") return cfg.agentId;
1817
+ if (harnessHint === "openclaw" && cfg.harness === "openclaw") return cfg.agentId;
1818
+ }
1819
+ } catch {
1820
+ }
1821
+ }
1822
+ return harnessHint === "openclaw" ? "oc-lesa-mini" : "cc-mini";
1823
+ }
1824
+ function ldmPaths(agentId) {
1825
+ const id = agentId || getAgentId();
1826
+ const agentRoot = join3(LDM_ROOT, "agents", id);
1827
+ return {
1828
+ root: LDM_ROOT,
1829
+ bin: join3(LDM_ROOT, "bin"),
1830
+ secrets: join3(LDM_ROOT, "secrets"),
1831
+ state: join3(LDM_ROOT, "state"),
1832
+ config: join3(LDM_ROOT, "config.json"),
1833
+ crystalDb: join3(LDM_ROOT, "memory", "crystal.db"),
1834
+ crystalLance: join3(LDM_ROOT, "memory", "lance"),
1835
+ agentRoot,
1836
+ transcripts: join3(agentRoot, "memory", "transcripts"),
1837
+ sessions: join3(agentRoot, "memory", "sessions"),
1838
+ daily: join3(agentRoot, "memory", "daily"),
1839
+ journals: join3(agentRoot, "memory", "journals"),
1840
+ workspace: join3(agentRoot, "memory", "workspace")
1841
+ };
1842
+ }
1843
+ function loadConfig() {
1844
+ const configPath = join3(LDM_ROOT, "config.json");
1845
+ try {
1846
+ if (existsSync3(configPath)) {
1847
+ return JSON.parse(readFileSync3(configPath, "utf-8"));
1848
+ }
1849
+ } catch {
1850
+ }
1851
+ return null;
1852
+ }
1853
+ function saveConfig(config) {
1854
+ const configPath = join3(LDM_ROOT, "config.json");
1855
+ writeFileSync(configPath, JSON.stringify(config, null, 2) + "\n");
1856
+ }
1857
+ function scaffoldLdm(agentId) {
1858
+ const paths = ldmPaths(agentId);
1859
+ mkdirSync2(join3(paths.root, "memory"), { recursive: true });
1860
+ mkdirSync2(paths.crystalLance, { recursive: true });
1861
+ mkdirSync2(paths.bin, { recursive: true });
1862
+ mkdirSync2(paths.secrets, { recursive: true, mode: 448 });
1863
+ mkdirSync2(paths.state, { recursive: true });
1864
+ mkdirSync2(paths.transcripts, { recursive: true });
1865
+ mkdirSync2(paths.sessions, { recursive: true });
1866
+ mkdirSync2(paths.daily, { recursive: true });
1867
+ mkdirSync2(paths.journals, { recursive: true });
1868
+ mkdirSync2(paths.workspace, { recursive: true });
1869
+ const id = agentId || getAgentId();
1870
+ let config = loadConfig();
1871
+ if (!config) {
1872
+ config = {
1873
+ version: "1.0.0",
1874
+ agents: [id],
1875
+ createdAt: (/* @__PURE__ */ new Date()).toISOString(),
1876
+ updatedAt: (/* @__PURE__ */ new Date()).toISOString()
1877
+ };
1878
+ } else {
1879
+ if (!config.agents.includes(id)) {
1880
+ config.agents.push(id);
1881
+ }
1882
+ config.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
1883
+ }
1884
+ saveConfig(config);
1885
+ return paths;
1886
+ }
1887
+ var LEGACY_OC_DIR = join3(HOME, ".openclaw");
1888
+ function resolveStatePath(filename) {
1889
+ const paths = ldmPaths();
1890
+ const ldmPath = join3(paths.state, filename);
1891
+ if (existsSync3(ldmPath)) return ldmPath;
1892
+ const legacyPath = join3(LEGACY_OC_DIR, "memory", filename);
1893
+ if (existsSync3(legacyPath)) return legacyPath;
1894
+ return ldmPath;
1895
+ }
1896
+ function stateWritePath(filename) {
1897
+ const paths = ldmPaths();
1898
+ const dir = paths.state;
1899
+ if (!existsSync3(dir)) mkdirSync2(dir, { recursive: true });
1900
+ return join3(dir, filename);
1901
+ }
1902
+ function ensureLdm(agentId) {
1903
+ const paths = ldmPaths(agentId);
1904
+ if (existsSync3(paths.transcripts) && existsSync3(paths.config)) {
1905
+ return paths;
1906
+ }
1907
+ return scaffoldLdm(agentId);
1908
+ }
12
1909
 
13
1910
  // src/cc-poller.ts
14
1911
  import {
15
- readFileSync,
16
- writeFileSync,
1912
+ readFileSync as readFileSync4,
1913
+ writeFileSync as writeFileSync2,
17
1914
  appendFileSync,
18
- existsSync,
19
- mkdirSync,
20
- statSync,
1915
+ existsSync as existsSync4,
1916
+ mkdirSync as mkdirSync3,
1917
+ statSync as statSync2,
21
1918
  openSync,
22
1919
  readSync,
23
1920
  closeSync,
24
- copyFileSync,
25
- readdirSync
1921
+ copyFileSync as copyFileSync2,
1922
+ readdirSync as readdirSync3
26
1923
  } from "fs";
27
- import { join, basename } from "path";
28
- var HOME = process.env.HOME || "";
1924
+ import { join as join4, basename as basename2 } from "path";
1925
+ var HOME2 = process.env.HOME || "";
29
1926
  var CC_AGENT_ID = process.env.CRYSTAL_AGENT_ID || "cc-mini";
30
1927
  var PRIVATE_MODE_PATH = resolveStatePath("memory-capture-state.json");
31
1928
  var WATERMARK_PATH = resolveStatePath("cc-capture-watermark.json");
32
1929
  var CC_ENABLED_PATH = resolveStatePath("cc-capture-enabled.json");
33
- var CC_PROJECTS_DIR = join(HOME, ".claude", "projects");
34
- var SESSION_EXPORT_DIR = process.env.CRYSTAL_SESSION_EXPORT_DIR || join(ldmPaths().agentRoot, "memory", "sessions");
1930
+ var CC_PROJECTS_DIR = join4(HOME2, ".claude", "projects");
1931
+ var SESSION_EXPORT_DIR = process.env.CRYSTAL_SESSION_EXPORT_DIR || join4(ldmPaths().agentRoot, "memory", "sessions");
35
1932
  var EXPORT_WATERMARK_PATH = resolveStatePath("cc-export-watermark.json");
36
1933
  function isPrivateMode() {
37
1934
  try {
38
- if (existsSync(PRIVATE_MODE_PATH)) {
39
- const state = JSON.parse(readFileSync(PRIVATE_MODE_PATH, "utf-8"));
1935
+ if (existsSync4(PRIVATE_MODE_PATH)) {
1936
+ const state = JSON.parse(readFileSync4(PRIVATE_MODE_PATH, "utf-8"));
40
1937
  return state.enabled === false;
41
1938
  }
42
1939
  } catch {
@@ -45,8 +1942,8 @@ function isPrivateMode() {
45
1942
  }
46
1943
  function isCaptureEnabled() {
47
1944
  try {
48
- if (existsSync(CC_ENABLED_PATH)) {
49
- const state = JSON.parse(readFileSync(CC_ENABLED_PATH, "utf-8"));
1945
+ if (existsSync4(CC_ENABLED_PATH)) {
1946
+ const state = JSON.parse(readFileSync4(CC_ENABLED_PATH, "utf-8"));
50
1947
  return state.enabled !== false;
51
1948
  }
52
1949
  } catch {
@@ -55,8 +1952,8 @@ function isCaptureEnabled() {
55
1952
  }
56
1953
  function loadWatermark() {
57
1954
  try {
58
- if (existsSync(WATERMARK_PATH)) {
59
- return JSON.parse(readFileSync(WATERMARK_PATH, "utf-8"));
1955
+ if (existsSync4(WATERMARK_PATH)) {
1956
+ return JSON.parse(readFileSync4(WATERMARK_PATH, "utf-8"));
60
1957
  }
61
1958
  } catch {
62
1959
  }
@@ -65,10 +1962,10 @@ function loadWatermark() {
65
1962
  function saveWatermark(wm) {
66
1963
  const writePath = stateWritePath("cc-capture-watermark.json");
67
1964
  wm.lastRun = (/* @__PURE__ */ new Date()).toISOString();
68
- writeFileSync(writePath, JSON.stringify(wm, null, 2));
1965
+ writeFileSync2(writePath, JSON.stringify(wm, null, 2));
69
1966
  }
70
1967
  function extractMessages(filePath, lastByteOffset) {
71
- const fileSize = statSync(filePath).size;
1968
+ const fileSize = statSync2(filePath).size;
72
1969
  if (lastByteOffset >= fileSize) {
73
1970
  return { messages: [], newByteOffset: fileSize };
74
1971
  }
@@ -110,19 +2007,19 @@ function extractMessages(filePath, lastByteOffset) {
110
2007
  }
111
2008
  function discoverSessionFiles() {
112
2009
  const files = [];
113
- if (!existsSync(CC_PROJECTS_DIR)) return files;
2010
+ if (!existsSync4(CC_PROJECTS_DIR)) return files;
114
2011
  try {
115
- for (const projectDir of readdirSync(CC_PROJECTS_DIR)) {
116
- const projectPath = join(CC_PROJECTS_DIR, projectDir);
2012
+ for (const projectDir of readdirSync3(CC_PROJECTS_DIR)) {
2013
+ const projectPath = join4(CC_PROJECTS_DIR, projectDir);
117
2014
  try {
118
- if (!statSync(projectPath).isDirectory()) continue;
2015
+ if (!statSync2(projectPath).isDirectory()) continue;
119
2016
  } catch {
120
2017
  continue;
121
2018
  }
122
2019
  try {
123
- for (const file of readdirSync(projectPath)) {
2020
+ for (const file of readdirSync3(projectPath)) {
124
2021
  if (file.endsWith(".jsonl") && !file.startsWith(".")) {
125
- files.push(join(projectPath, file));
2022
+ files.push(join4(projectPath, file));
126
2023
  }
127
2024
  }
128
2025
  } catch {
@@ -136,8 +2033,8 @@ function discoverSessionFiles() {
136
2033
  function appendDailyLog(messages, sessionFile) {
137
2034
  try {
138
2035
  const paths = ldmPaths();
139
- if (!existsSync(paths.root)) return;
140
- if (!existsSync(paths.daily)) mkdirSync(paths.daily, { recursive: true });
2036
+ if (!existsSync4(paths.root)) return;
2037
+ if (!existsSync4(paths.daily)) mkdirSync3(paths.daily, { recursive: true });
141
2038
  const now = /* @__PURE__ */ new Date();
142
2039
  const dateStr = now.toISOString().slice(0, 10);
143
2040
  const timeStr = now.toLocaleTimeString("en-US", {
@@ -146,15 +2043,15 @@ function appendDailyLog(messages, sessionFile) {
146
2043
  hour12: false,
147
2044
  timeZone: "America/Los_Angeles"
148
2045
  });
149
- const logPath = join(paths.daily, `${dateStr}.md`);
2046
+ const logPath = join4(paths.daily, `${dateStr}.md`);
150
2047
  const userMsg = messages.find((m) => m.role === "user");
151
2048
  if (!userMsg) return;
152
2049
  const snippet = userMsg.text.slice(0, 120).replace(/\n/g, " ").trim();
153
- const sessionId = basename(sessionFile, ".jsonl").slice(0, 8);
2050
+ const sessionId = basename2(sessionFile, ".jsonl").slice(0, 8);
154
2051
  const line = `- **${timeStr}** [${sessionId}] ${snippet}${userMsg.text.length > 120 ? "..." : ""}
155
2052
  `;
156
- if (!existsSync(logPath)) {
157
- writeFileSync(logPath, `# ${dateStr} - CC Daily Log
2053
+ if (!existsSync4(logPath)) {
2054
+ writeFileSync2(logPath, `# ${dateStr} - CC Daily Log
158
2055
 
159
2056
  `);
160
2057
  }
@@ -166,20 +2063,20 @@ function archiveTranscript(transcriptPath) {
166
2063
  try {
167
2064
  if (isPrivateMode()) return;
168
2065
  const paths = ensureLdm();
169
- const dest = join(paths.transcripts, basename(transcriptPath));
170
- if (existsSync(dest)) {
171
- const srcMtime = statSync(transcriptPath).mtimeMs;
172
- const dstMtime = statSync(dest).mtimeMs;
2066
+ const dest = join4(paths.transcripts, basename2(transcriptPath));
2067
+ if (existsSync4(dest)) {
2068
+ const srcMtime = statSync2(transcriptPath).mtimeMs;
2069
+ const dstMtime = statSync2(dest).mtimeMs;
173
2070
  if (srcMtime <= dstMtime) return;
174
2071
  }
175
- copyFileSync(transcriptPath, dest);
2072
+ copyFileSync2(transcriptPath, dest);
176
2073
  } catch {
177
2074
  }
178
2075
  }
179
2076
  function loadExportWatermark() {
180
2077
  try {
181
- if (existsSync(EXPORT_WATERMARK_PATH)) {
182
- return JSON.parse(readFileSync(EXPORT_WATERMARK_PATH, "utf-8"));
2078
+ if (existsSync4(EXPORT_WATERMARK_PATH)) {
2079
+ return JSON.parse(readFileSync4(EXPORT_WATERMARK_PATH, "utf-8"));
183
2080
  }
184
2081
  } catch {
185
2082
  }
@@ -187,7 +2084,7 @@ function loadExportWatermark() {
187
2084
  }
188
2085
  function saveExportWatermark(data) {
189
2086
  const writePath = stateWritePath("cc-export-watermark.json");
190
- writeFileSync(writePath, JSON.stringify(data, null, 2));
2087
+ writeFileSync2(writePath, JSON.stringify(data, null, 2));
191
2088
  }
192
2089
  function formatTimestamp(ts) {
193
2090
  if (!ts) return "";
@@ -204,14 +2101,14 @@ function formatTimestamp(ts) {
204
2101
  function exportSessionToMarkdown(filePath) {
205
2102
  try {
206
2103
  const exportWm = loadExportWatermark();
207
- const fileName = basename(filePath);
208
- const currentSize = statSync(filePath).size;
2104
+ const fileName = basename2(filePath);
2105
+ const currentSize = statSync2(filePath).size;
209
2106
  const lastSize = exportWm[fileName] || 0;
210
2107
  if (currentSize === lastSize) return;
211
- if (!existsSync(SESSION_EXPORT_DIR)) mkdirSync(SESSION_EXPORT_DIR, { recursive: true });
212
- const content = readFileSync(filePath, "utf-8");
2108
+ if (!existsSync4(SESSION_EXPORT_DIR)) mkdirSync3(SESSION_EXPORT_DIR, { recursive: true });
2109
+ const content = readFileSync4(filePath, "utf-8");
213
2110
  const lines = content.split("\n").filter((l) => l.trim());
214
- const sessionId = basename(filePath, ".jsonl");
2111
+ const sessionId = basename2(filePath, ".jsonl");
215
2112
  let firstTs = null;
216
2113
  let lastTs = null;
217
2114
  let model = "unknown";
@@ -282,11 +2179,11 @@ ${t.content}
282
2179
  `;
283
2180
  }
284
2181
  }
285
- const mtime = statSync(filePath).mtime;
2182
+ const mtime = statSync2(filePath).mtime;
286
2183
  const date = mtime.toISOString().split("T")[0];
287
2184
  const shortId = sessionId.slice(0, 8);
288
- const outPath = join(SESSION_EXPORT_DIR, `${date}-session-${shortId}.md`);
289
- writeFileSync(outPath, md);
2185
+ const outPath = join4(SESSION_EXPORT_DIR, `${date}-session-${shortId}.md`);
2186
+ writeFileSync2(outPath, md);
290
2187
  exportWm[fileName] = currentSize;
291
2188
  saveExportWatermark(exportWm);
292
2189
  } catch {
@@ -369,7 +2266,7 @@ async function pollOnce() {
369
2266
  wm.files[filePath] = { lastByteOffset: 0, lastTimestamp: (/* @__PURE__ */ new Date()).toISOString() };
370
2267
  }
371
2268
  const lastOffset = wm.files[filePath].lastByteOffset;
372
- const fileSize = statSync(filePath).size;
2269
+ const fileSize = statSync2(filePath).size;
373
2270
  if (lastOffset >= fileSize) continue;
374
2271
  const { messages, newByteOffset } = extractMessages(filePath, lastOffset);
375
2272
  if (messages.length === 0) {
@@ -393,11 +2290,11 @@ async function pollOnce() {
393
2290
  archiveTranscript(filePath);
394
2291
  appendDailyLog(messages, filePath);
395
2292
  exportSessionToMarkdown(filePath);
396
- process.stderr.write(`[cc-poller] ${count} chunks (${totalTokens} tokens) from ${basename(filePath)}
2293
+ process.stderr.write(`[cc-poller] ${count} chunks (${totalTokens} tokens) from ${basename2(filePath)}
397
2294
  `);
398
2295
  } catch (err) {
399
- errors.push(`${basename(filePath)}: ${err.message}`);
400
- process.stderr.write(`[cc-poller] error on ${basename(filePath)}: ${err.message}
2296
+ errors.push(`${basename2(filePath)}: ${err.message}`);
2297
+ process.stderr.write(`[cc-poller] error on ${basename2(filePath)}: ${err.message}
401
2298
  `);
402
2299
  }
403
2300
  }
@@ -417,16 +2314,16 @@ function showStatus() {
417
2314
  Session files: ${sessionFiles.length}
418
2315
  `);
419
2316
  for (const filePath of sessionFiles) {
420
- const fileSize = statSync(filePath).size;
2317
+ const fileSize = statSync2(filePath).size;
421
2318
  const wmEntry = wm.files[filePath];
422
2319
  const lastOffset = wmEntry?.lastByteOffset || 0;
423
2320
  const behind = fileSize - lastOffset;
424
- const mtime = statSync(filePath).mtime;
2321
+ const mtime = statSync2(filePath).mtime;
425
2322
  const age = Date.now() - mtime.getTime();
426
2323
  const ageStr = age < 6e4 ? `${Math.round(age / 1e3)}s ago` : age < 36e5 ? `${Math.round(age / 6e4)}m ago` : age < 864e5 ? `${Math.round(age / 36e5)}h ago` : `${Math.round(age / 864e5)}d ago`;
427
2324
  const status = behind === 0 ? "IN SYNC" : behind < 1024 ? `${behind}B behind` : behind < 1048576 ? `${(behind / 1024).toFixed(1)}KB behind` : `${(behind / 1048576).toFixed(1)}MB behind`;
428
2325
  const statusIcon = behind === 0 ? "OK" : behind > 1048576 ? "CRITICAL" : "BEHIND";
429
- console.log(` ${basename(filePath, ".jsonl").slice(0, 8)} ${(fileSize / 1048576).toFixed(1)}MB modified ${ageStr} ${status} ${statusIcon}`);
2326
+ console.log(` ${basename2(filePath, ".jsonl").slice(0, 8)} ${(fileSize / 1048576).toFixed(1)}MB modified ${ageStr} ${status} ${statusIcon}`);
430
2327
  }
431
2328
  }
432
2329
  async function healthCheck() {
@@ -438,8 +2335,8 @@ async function healthCheck() {
438
2335
  let activeFiles = 0;
439
2336
  let totalBehind = 0;
440
2337
  for (const filePath of sessionFiles) {
441
- const fileSize = statSync(filePath).size;
442
- const mtime = statSync(filePath).mtime;
2338
+ const fileSize = statSync2(filePath).size;
2339
+ const mtime = statSync2(filePath).mtime;
443
2340
  const age = Date.now() - mtime.getTime();
444
2341
  const isActive = age < 72e5;
445
2342
  if (isActive) activeFiles++;
@@ -450,15 +2347,15 @@ async function healthCheck() {
450
2347
  if (isActive || behind > 0) {
451
2348
  const ageStr = age < 36e5 ? `${Math.round(age / 6e4)}m ago` : `${Math.round(age / 36e5)}h ago`;
452
2349
  const statusIcon = behind === 0 ? "OK" : behind > 1048576 ? "CRITICAL" : "WARNING";
453
- console.log(` ${basename(filePath, ".jsonl").slice(0, 8)} ${(fileSize / 1048576).toFixed(1)}MB last write: ${ageStr} ${statusIcon}`);
2350
+ console.log(` ${basename2(filePath, ".jsonl").slice(0, 8)} ${(fileSize / 1048576).toFixed(1)}MB last write: ${ageStr} ${statusIcon}`);
454
2351
  }
455
2352
  }
456
2353
  console.log("\nDaily MD Logs:");
457
2354
  const paths = ldmPaths();
458
2355
  const today = (/* @__PURE__ */ new Date()).toISOString().slice(0, 10);
459
- const todayLog = join(paths.daily, `${today}.md`);
460
- if (existsSync(todayLog)) {
461
- const content = readFileSync(todayLog, "utf-8");
2356
+ const todayLog = join4(paths.daily, `${today}.md`);
2357
+ if (existsSync4(todayLog)) {
2358
+ const content = readFileSync4(todayLog, "utf-8");
462
2359
  const lineCount = content.split("\n").filter((l) => l.startsWith("- ")).length;
463
2360
  console.log(` ${today}.md exists, ${lineCount} entries OK`);
464
2361
  } else if (activeFiles > 0) {