claude-memory-layer 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (127) hide show
  1. package/.claude-plugin/commands/memory-forget.md +42 -0
  2. package/.claude-plugin/commands/memory-history.md +34 -0
  3. package/.claude-plugin/commands/memory-import.md +56 -0
  4. package/.claude-plugin/commands/memory-list.md +37 -0
  5. package/.claude-plugin/commands/memory-search.md +36 -0
  6. package/.claude-plugin/commands/memory-stats.md +34 -0
  7. package/.claude-plugin/hooks.json +59 -0
  8. package/.claude-plugin/plugin.json +24 -0
  9. package/.history/package_20260201112328.json +45 -0
  10. package/.history/package_20260201113602.json +45 -0
  11. package/.history/package_20260201113713.json +45 -0
  12. package/.history/package_20260201114110.json +45 -0
  13. package/Memo.txt +558 -0
  14. package/README.md +520 -0
  15. package/context.md +636 -0
  16. package/dist/.claude-plugin/commands/memory-forget.md +42 -0
  17. package/dist/.claude-plugin/commands/memory-history.md +34 -0
  18. package/dist/.claude-plugin/commands/memory-import.md +56 -0
  19. package/dist/.claude-plugin/commands/memory-list.md +37 -0
  20. package/dist/.claude-plugin/commands/memory-search.md +36 -0
  21. package/dist/.claude-plugin/commands/memory-stats.md +34 -0
  22. package/dist/.claude-plugin/hooks.json +59 -0
  23. package/dist/.claude-plugin/plugin.json +24 -0
  24. package/dist/cli/index.js +3539 -0
  25. package/dist/cli/index.js.map +7 -0
  26. package/dist/core/index.js +4408 -0
  27. package/dist/core/index.js.map +7 -0
  28. package/dist/hooks/session-end.js +2971 -0
  29. package/dist/hooks/session-end.js.map +7 -0
  30. package/dist/hooks/session-start.js +2969 -0
  31. package/dist/hooks/session-start.js.map +7 -0
  32. package/dist/hooks/stop.js +3123 -0
  33. package/dist/hooks/stop.js.map +7 -0
  34. package/dist/hooks/user-prompt-submit.js +2960 -0
  35. package/dist/hooks/user-prompt-submit.js.map +7 -0
  36. package/dist/services/memory-service.js +2931 -0
  37. package/dist/services/memory-service.js.map +7 -0
  38. package/package.json +45 -0
  39. package/plan.md +1642 -0
  40. package/scripts/build.ts +102 -0
  41. package/spec.md +624 -0
  42. package/specs/citations-system/context.md +243 -0
  43. package/specs/citations-system/plan.md +495 -0
  44. package/specs/citations-system/spec.md +371 -0
  45. package/specs/endless-mode/context.md +305 -0
  46. package/specs/endless-mode/plan.md +620 -0
  47. package/specs/endless-mode/spec.md +455 -0
  48. package/specs/entity-edge-model/context.md +401 -0
  49. package/specs/entity-edge-model/plan.md +459 -0
  50. package/specs/entity-edge-model/spec.md +391 -0
  51. package/specs/evidence-aligner-v2/context.md +401 -0
  52. package/specs/evidence-aligner-v2/plan.md +303 -0
  53. package/specs/evidence-aligner-v2/spec.md +312 -0
  54. package/specs/mcp-desktop-integration/context.md +278 -0
  55. package/specs/mcp-desktop-integration/plan.md +550 -0
  56. package/specs/mcp-desktop-integration/spec.md +494 -0
  57. package/specs/post-tool-use-hook/context.md +319 -0
  58. package/specs/post-tool-use-hook/plan.md +469 -0
  59. package/specs/post-tool-use-hook/spec.md +364 -0
  60. package/specs/private-tags/context.md +288 -0
  61. package/specs/private-tags/plan.md +412 -0
  62. package/specs/private-tags/spec.md +345 -0
  63. package/specs/progressive-disclosure/context.md +346 -0
  64. package/specs/progressive-disclosure/plan.md +663 -0
  65. package/specs/progressive-disclosure/spec.md +415 -0
  66. package/specs/task-entity-system/context.md +297 -0
  67. package/specs/task-entity-system/plan.md +301 -0
  68. package/specs/task-entity-system/spec.md +314 -0
  69. package/specs/vector-outbox-v2/context.md +470 -0
  70. package/specs/vector-outbox-v2/plan.md +562 -0
  71. package/specs/vector-outbox-v2/spec.md +466 -0
  72. package/specs/web-viewer-ui/context.md +384 -0
  73. package/specs/web-viewer-ui/plan.md +797 -0
  74. package/specs/web-viewer-ui/spec.md +516 -0
  75. package/src/cli/index.ts +570 -0
  76. package/src/core/canonical-key.ts +186 -0
  77. package/src/core/citation-generator.ts +63 -0
  78. package/src/core/consolidated-store.ts +279 -0
  79. package/src/core/consolidation-worker.ts +384 -0
  80. package/src/core/context-formatter.ts +276 -0
  81. package/src/core/continuity-manager.ts +336 -0
  82. package/src/core/edge-repo.ts +324 -0
  83. package/src/core/embedder.ts +124 -0
  84. package/src/core/entity-repo.ts +342 -0
  85. package/src/core/event-store.ts +672 -0
  86. package/src/core/evidence-aligner.ts +635 -0
  87. package/src/core/graduation.ts +365 -0
  88. package/src/core/index.ts +32 -0
  89. package/src/core/matcher.ts +210 -0
  90. package/src/core/metadata-extractor.ts +203 -0
  91. package/src/core/privacy/filter.ts +179 -0
  92. package/src/core/privacy/index.ts +20 -0
  93. package/src/core/privacy/tag-parser.ts +145 -0
  94. package/src/core/progressive-retriever.ts +415 -0
  95. package/src/core/retriever.ts +235 -0
  96. package/src/core/task/blocker-resolver.ts +325 -0
  97. package/src/core/task/index.ts +9 -0
  98. package/src/core/task/task-matcher.ts +238 -0
  99. package/src/core/task/task-projector.ts +345 -0
  100. package/src/core/task/task-resolver.ts +414 -0
  101. package/src/core/types.ts +841 -0
  102. package/src/core/vector-outbox.ts +295 -0
  103. package/src/core/vector-store.ts +182 -0
  104. package/src/core/vector-worker.ts +488 -0
  105. package/src/core/working-set-store.ts +244 -0
  106. package/src/hooks/post-tool-use.ts +127 -0
  107. package/src/hooks/session-end.ts +78 -0
  108. package/src/hooks/session-start.ts +57 -0
  109. package/src/hooks/stop.ts +78 -0
  110. package/src/hooks/user-prompt-submit.ts +54 -0
  111. package/src/mcp/handlers.ts +212 -0
  112. package/src/mcp/index.ts +47 -0
  113. package/src/mcp/tools.ts +78 -0
  114. package/src/server/api/citations.ts +101 -0
  115. package/src/server/api/events.ts +101 -0
  116. package/src/server/api/index.ts +18 -0
  117. package/src/server/api/search.ts +98 -0
  118. package/src/server/api/sessions.ts +111 -0
  119. package/src/server/api/stats.ts +97 -0
  120. package/src/server/index.ts +91 -0
  121. package/src/services/memory-service.ts +626 -0
  122. package/src/services/session-history-importer.ts +367 -0
  123. package/tests/canonical-key.test.ts +101 -0
  124. package/tests/evidence-aligner.test.ts +152 -0
  125. package/tests/matcher.test.ts +112 -0
  126. package/tsconfig.json +24 -0
  127. package/vitest.config.ts +15 -0
@@ -0,0 +1,2960 @@
1
+ #!/usr/bin/env node
2
+ import { createRequire } from 'module';
3
+ import { fileURLToPath } from 'url';
4
+ import { dirname } from 'path';
5
+ const require = createRequire(import.meta.url);
6
+ const __filename = fileURLToPath(import.meta.url);
7
+ const __dirname = dirname(__filename);
8
+
9
+ // src/services/memory-service.ts
10
+ import * as path from "path";
11
+ import * as os from "os";
12
+ import * as fs from "fs";
13
+ import * as crypto2 from "crypto";
14
+
15
+ // src/core/event-store.ts
16
+ import { Database } from "duckdb";
17
+ import { randomUUID } from "crypto";
18
+
19
+ // src/core/canonical-key.ts
20
+ import { createHash } from "crypto";
21
+ var MAX_KEY_LENGTH = 200;
22
+ function makeCanonicalKey(title, context) {
23
+ let normalized = title.normalize("NFKC");
24
+ normalized = normalized.toLowerCase();
25
+ normalized = normalized.replace(/[^\p{L}\p{N}\s]/gu, "");
26
+ normalized = normalized.replace(/\s+/g, " ").trim();
27
+ let key = normalized;
28
+ if (context?.project) {
29
+ key = `${context.project}::${key}`;
30
+ }
31
+ if (key.length > MAX_KEY_LENGTH) {
32
+ const hashSuffix = createHash("md5").update(key).digest("hex").slice(0, 8);
33
+ key = key.slice(0, MAX_KEY_LENGTH - 9) + "_" + hashSuffix;
34
+ }
35
+ return key;
36
+ }
37
+ function makeDedupeKey(content, sessionId) {
38
+ const contentHash = createHash("sha256").update(content).digest("hex");
39
+ return `${sessionId}:${contentHash}`;
40
+ }
41
+
42
+ // src/core/event-store.ts
43
+ var EventStore = class {
44
+ constructor(dbPath) {
45
+ this.dbPath = dbPath;
46
+ this.db = new Database(dbPath);
47
+ }
48
+ db;
49
+ initialized = false;
50
+ /**
51
+ * Initialize database schema
52
+ */
53
+ async initialize() {
54
+ if (this.initialized)
55
+ return;
56
+ await this.db.run(`
57
+ CREATE TABLE IF NOT EXISTS events (
58
+ id VARCHAR PRIMARY KEY,
59
+ event_type VARCHAR NOT NULL,
60
+ session_id VARCHAR NOT NULL,
61
+ timestamp TIMESTAMP NOT NULL,
62
+ content TEXT NOT NULL,
63
+ canonical_key VARCHAR NOT NULL,
64
+ dedupe_key VARCHAR UNIQUE,
65
+ metadata JSON
66
+ )
67
+ `);
68
+ await this.db.run(`
69
+ CREATE TABLE IF NOT EXISTS event_dedup (
70
+ dedupe_key VARCHAR PRIMARY KEY,
71
+ event_id VARCHAR NOT NULL,
72
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
73
+ )
74
+ `);
75
+ await this.db.run(`
76
+ CREATE TABLE IF NOT EXISTS sessions (
77
+ id VARCHAR PRIMARY KEY,
78
+ started_at TIMESTAMP NOT NULL,
79
+ ended_at TIMESTAMP,
80
+ project_path VARCHAR,
81
+ summary TEXT,
82
+ tags JSON
83
+ )
84
+ `);
85
+ await this.db.run(`
86
+ CREATE TABLE IF NOT EXISTS insights (
87
+ id VARCHAR PRIMARY KEY,
88
+ insight_type VARCHAR NOT NULL,
89
+ content TEXT NOT NULL,
90
+ canonical_key VARCHAR NOT NULL,
91
+ confidence FLOAT,
92
+ source_events JSON,
93
+ created_at TIMESTAMP,
94
+ last_updated TIMESTAMP
95
+ )
96
+ `);
97
+ await this.db.run(`
98
+ CREATE TABLE IF NOT EXISTS embedding_outbox (
99
+ id VARCHAR PRIMARY KEY,
100
+ event_id VARCHAR NOT NULL,
101
+ content TEXT NOT NULL,
102
+ status VARCHAR DEFAULT 'pending',
103
+ retry_count INT DEFAULT 0,
104
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
105
+ processed_at TIMESTAMP,
106
+ error_message TEXT
107
+ )
108
+ `);
109
+ await this.db.run(`
110
+ CREATE TABLE IF NOT EXISTS projection_offsets (
111
+ projection_name VARCHAR PRIMARY KEY,
112
+ last_event_id VARCHAR,
113
+ last_timestamp TIMESTAMP,
114
+ updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
115
+ )
116
+ `);
117
+ await this.db.run(`
118
+ CREATE TABLE IF NOT EXISTS memory_levels (
119
+ event_id VARCHAR PRIMARY KEY,
120
+ level VARCHAR NOT NULL DEFAULT 'L0',
121
+ promoted_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
122
+ )
123
+ `);
124
+ await this.db.run(`
125
+ CREATE TABLE IF NOT EXISTS entries (
126
+ entry_id VARCHAR PRIMARY KEY,
127
+ created_ts TIMESTAMP NOT NULL,
128
+ entry_type VARCHAR NOT NULL,
129
+ title VARCHAR NOT NULL,
130
+ content_json JSON NOT NULL,
131
+ stage VARCHAR NOT NULL DEFAULT 'raw',
132
+ status VARCHAR DEFAULT 'active',
133
+ superseded_by VARCHAR,
134
+ build_id VARCHAR,
135
+ evidence_json JSON,
136
+ canonical_key VARCHAR,
137
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
138
+ )
139
+ `);
140
+ await this.db.run(`
141
+ CREATE TABLE IF NOT EXISTS entities (
142
+ entity_id VARCHAR PRIMARY KEY,
143
+ entity_type VARCHAR NOT NULL,
144
+ canonical_key VARCHAR NOT NULL,
145
+ title VARCHAR NOT NULL,
146
+ stage VARCHAR NOT NULL DEFAULT 'raw',
147
+ status VARCHAR NOT NULL DEFAULT 'active',
148
+ current_json JSON NOT NULL,
149
+ title_norm VARCHAR,
150
+ search_text VARCHAR,
151
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
152
+ updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
153
+ )
154
+ `);
155
+ await this.db.run(`
156
+ CREATE TABLE IF NOT EXISTS entity_aliases (
157
+ entity_type VARCHAR NOT NULL,
158
+ canonical_key VARCHAR NOT NULL,
159
+ entity_id VARCHAR NOT NULL,
160
+ is_primary BOOLEAN DEFAULT FALSE,
161
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
162
+ PRIMARY KEY(entity_type, canonical_key)
163
+ )
164
+ `);
165
+ await this.db.run(`
166
+ CREATE TABLE IF NOT EXISTS edges (
167
+ edge_id VARCHAR PRIMARY KEY,
168
+ src_type VARCHAR NOT NULL,
169
+ src_id VARCHAR NOT NULL,
170
+ rel_type VARCHAR NOT NULL,
171
+ dst_type VARCHAR NOT NULL,
172
+ dst_id VARCHAR NOT NULL,
173
+ meta_json JSON,
174
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
175
+ )
176
+ `);
177
+ await this.db.run(`
178
+ CREATE TABLE IF NOT EXISTS vector_outbox (
179
+ job_id VARCHAR PRIMARY KEY,
180
+ item_kind VARCHAR NOT NULL,
181
+ item_id VARCHAR NOT NULL,
182
+ embedding_version VARCHAR NOT NULL,
183
+ status VARCHAR NOT NULL DEFAULT 'pending',
184
+ retry_count INT DEFAULT 0,
185
+ error VARCHAR,
186
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
187
+ updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
188
+ UNIQUE(item_kind, item_id, embedding_version)
189
+ )
190
+ `);
191
+ await this.db.run(`
192
+ CREATE TABLE IF NOT EXISTS build_runs (
193
+ build_id VARCHAR PRIMARY KEY,
194
+ started_at TIMESTAMP NOT NULL,
195
+ finished_at TIMESTAMP,
196
+ extractor_model VARCHAR NOT NULL,
197
+ extractor_prompt_hash VARCHAR NOT NULL,
198
+ embedder_model VARCHAR NOT NULL,
199
+ embedding_version VARCHAR NOT NULL,
200
+ idris_version VARCHAR NOT NULL,
201
+ schema_version VARCHAR NOT NULL,
202
+ status VARCHAR NOT NULL DEFAULT 'running',
203
+ error VARCHAR
204
+ )
205
+ `);
206
+ await this.db.run(`
207
+ CREATE TABLE IF NOT EXISTS pipeline_metrics (
208
+ id VARCHAR PRIMARY KEY,
209
+ ts TIMESTAMP NOT NULL,
210
+ stage VARCHAR NOT NULL,
211
+ latency_ms DOUBLE NOT NULL,
212
+ success BOOLEAN NOT NULL,
213
+ error VARCHAR,
214
+ session_id VARCHAR
215
+ )
216
+ `);
217
+ await this.db.run(`
218
+ CREATE TABLE IF NOT EXISTS working_set (
219
+ id VARCHAR PRIMARY KEY,
220
+ event_id VARCHAR NOT NULL,
221
+ added_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
222
+ relevance_score FLOAT DEFAULT 1.0,
223
+ topics JSON,
224
+ expires_at TIMESTAMP
225
+ )
226
+ `);
227
+ await this.db.run(`
228
+ CREATE TABLE IF NOT EXISTS consolidated_memories (
229
+ memory_id VARCHAR PRIMARY KEY,
230
+ summary TEXT NOT NULL,
231
+ topics JSON,
232
+ source_events JSON,
233
+ confidence FLOAT DEFAULT 0.5,
234
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
235
+ accessed_at TIMESTAMP,
236
+ access_count INTEGER DEFAULT 0
237
+ )
238
+ `);
239
+ await this.db.run(`
240
+ CREATE TABLE IF NOT EXISTS continuity_log (
241
+ log_id VARCHAR PRIMARY KEY,
242
+ from_context_id VARCHAR,
243
+ to_context_id VARCHAR,
244
+ continuity_score FLOAT,
245
+ transition_type VARCHAR,
246
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
247
+ )
248
+ `);
249
+ await this.db.run(`
250
+ CREATE TABLE IF NOT EXISTS endless_config (
251
+ key VARCHAR PRIMARY KEY,
252
+ value JSON,
253
+ updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
254
+ )
255
+ `);
256
+ await this.db.run(`CREATE INDEX IF NOT EXISTS idx_entries_type ON entries(entry_type)`);
257
+ await this.db.run(`CREATE INDEX IF NOT EXISTS idx_entries_stage ON entries(stage)`);
258
+ await this.db.run(`CREATE INDEX IF NOT EXISTS idx_entries_canonical ON entries(canonical_key)`);
259
+ await this.db.run(`CREATE INDEX IF NOT EXISTS idx_entities_type_key ON entities(entity_type, canonical_key)`);
260
+ await this.db.run(`CREATE INDEX IF NOT EXISTS idx_entities_status ON entities(status)`);
261
+ await this.db.run(`CREATE INDEX IF NOT EXISTS idx_edges_src ON edges(src_id, rel_type)`);
262
+ await this.db.run(`CREATE INDEX IF NOT EXISTS idx_edges_dst ON edges(dst_id, rel_type)`);
263
+ await this.db.run(`CREATE INDEX IF NOT EXISTS idx_edges_rel ON edges(rel_type)`);
264
+ await this.db.run(`CREATE INDEX IF NOT EXISTS idx_outbox_status ON vector_outbox(status)`);
265
+ await this.db.run(`CREATE INDEX IF NOT EXISTS idx_working_set_expires ON working_set(expires_at)`);
266
+ await this.db.run(`CREATE INDEX IF NOT EXISTS idx_working_set_relevance ON working_set(relevance_score DESC)`);
267
+ await this.db.run(`CREATE INDEX IF NOT EXISTS idx_consolidated_confidence ON consolidated_memories(confidence DESC)`);
268
+ await this.db.run(`CREATE INDEX IF NOT EXISTS idx_continuity_created ON continuity_log(created_at)`);
269
+ this.initialized = true;
270
+ }
271
+ /**
272
+ * Append event to store (AXIOMMIND Principle 2: Append-only)
273
+ * Returns existing event ID if duplicate (Principle 3: Idempotency)
274
+ */
275
+ async append(input) {
276
+ await this.initialize();
277
+ const canonicalKey = makeCanonicalKey(input.content);
278
+ const dedupeKey = makeDedupeKey(input.content, input.sessionId);
279
+ const existing = await this.db.all(
280
+ `SELECT event_id FROM event_dedup WHERE dedupe_key = ?`,
281
+ [dedupeKey]
282
+ );
283
+ if (existing.length > 0) {
284
+ return {
285
+ success: true,
286
+ eventId: existing[0].event_id,
287
+ isDuplicate: true
288
+ };
289
+ }
290
+ const id = randomUUID();
291
+ const timestamp = input.timestamp.toISOString();
292
+ try {
293
+ await this.db.run(
294
+ `INSERT INTO events (id, event_type, session_id, timestamp, content, canonical_key, dedupe_key, metadata)
295
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
296
+ [
297
+ id,
298
+ input.eventType,
299
+ input.sessionId,
300
+ timestamp,
301
+ input.content,
302
+ canonicalKey,
303
+ dedupeKey,
304
+ JSON.stringify(input.metadata || {})
305
+ ]
306
+ );
307
+ await this.db.run(
308
+ `INSERT INTO event_dedup (dedupe_key, event_id) VALUES (?, ?)`,
309
+ [dedupeKey, id]
310
+ );
311
+ await this.db.run(
312
+ `INSERT INTO memory_levels (event_id, level) VALUES (?, 'L0')`,
313
+ [id]
314
+ );
315
+ return { success: true, eventId: id, isDuplicate: false };
316
+ } catch (error) {
317
+ return {
318
+ success: false,
319
+ error: error instanceof Error ? error.message : String(error)
320
+ };
321
+ }
322
+ }
323
+ /**
324
+ * Get events by session ID
325
+ */
326
+ async getSessionEvents(sessionId) {
327
+ await this.initialize();
328
+ const rows = await this.db.all(
329
+ `SELECT * FROM events WHERE session_id = ? ORDER BY timestamp ASC`,
330
+ [sessionId]
331
+ );
332
+ return rows.map(this.rowToEvent);
333
+ }
334
+ /**
335
+ * Get recent events
336
+ */
337
+ async getRecentEvents(limit = 100) {
338
+ await this.initialize();
339
+ const rows = await this.db.all(
340
+ `SELECT * FROM events ORDER BY timestamp DESC LIMIT ?`,
341
+ [limit]
342
+ );
343
+ return rows.map(this.rowToEvent);
344
+ }
345
+ /**
346
+ * Get event by ID
347
+ */
348
+ async getEvent(id) {
349
+ await this.initialize();
350
+ const rows = await this.db.all(
351
+ `SELECT * FROM events WHERE id = ?`,
352
+ [id]
353
+ );
354
+ if (rows.length === 0)
355
+ return null;
356
+ return this.rowToEvent(rows[0]);
357
+ }
358
+ /**
359
+ * Create or update session
360
+ */
361
+ async upsertSession(session) {
362
+ await this.initialize();
363
+ const existing = await this.db.all(
364
+ `SELECT id FROM sessions WHERE id = ?`,
365
+ [session.id]
366
+ );
367
+ if (existing.length === 0) {
368
+ await this.db.run(
369
+ `INSERT INTO sessions (id, started_at, project_path, tags)
370
+ VALUES (?, ?, ?, ?)`,
371
+ [
372
+ session.id,
373
+ (session.startedAt || /* @__PURE__ */ new Date()).toISOString(),
374
+ session.projectPath || null,
375
+ JSON.stringify(session.tags || [])
376
+ ]
377
+ );
378
+ } else {
379
+ const updates = [];
380
+ const values = [];
381
+ if (session.endedAt) {
382
+ updates.push("ended_at = ?");
383
+ values.push(session.endedAt.toISOString());
384
+ }
385
+ if (session.summary) {
386
+ updates.push("summary = ?");
387
+ values.push(session.summary);
388
+ }
389
+ if (session.tags) {
390
+ updates.push("tags = ?");
391
+ values.push(JSON.stringify(session.tags));
392
+ }
393
+ if (updates.length > 0) {
394
+ values.push(session.id);
395
+ await this.db.run(
396
+ `UPDATE sessions SET ${updates.join(", ")} WHERE id = ?`,
397
+ values
398
+ );
399
+ }
400
+ }
401
+ }
402
+ /**
403
+ * Get session by ID
404
+ */
405
+ async getSession(id) {
406
+ await this.initialize();
407
+ const rows = await this.db.all(
408
+ `SELECT * FROM sessions WHERE id = ?`,
409
+ [id]
410
+ );
411
+ if (rows.length === 0)
412
+ return null;
413
+ const row = rows[0];
414
+ return {
415
+ id: row.id,
416
+ startedAt: new Date(row.started_at),
417
+ endedAt: row.ended_at ? new Date(row.ended_at) : void 0,
418
+ projectPath: row.project_path,
419
+ summary: row.summary,
420
+ tags: row.tags ? JSON.parse(row.tags) : void 0
421
+ };
422
+ }
423
+ /**
424
+ * Add to embedding outbox (Single-Writer Pattern)
425
+ */
426
+ async enqueueForEmbedding(eventId, content) {
427
+ await this.initialize();
428
+ const id = randomUUID();
429
+ await this.db.run(
430
+ `INSERT INTO embedding_outbox (id, event_id, content, status, retry_count)
431
+ VALUES (?, ?, ?, 'pending', 0)`,
432
+ [id, eventId, content]
433
+ );
434
+ return id;
435
+ }
436
+ /**
437
+ * Get pending outbox items
438
+ */
439
+ async getPendingOutboxItems(limit = 32) {
440
+ await this.initialize();
441
+ const rows = await this.db.all(
442
+ `UPDATE embedding_outbox
443
+ SET status = 'processing'
444
+ WHERE id IN (
445
+ SELECT id FROM embedding_outbox
446
+ WHERE status = 'pending'
447
+ ORDER BY created_at
448
+ LIMIT ?
449
+ )
450
+ RETURNING *`,
451
+ [limit]
452
+ );
453
+ return rows.map((row) => ({
454
+ id: row.id,
455
+ eventId: row.event_id,
456
+ content: row.content,
457
+ status: row.status,
458
+ retryCount: row.retry_count,
459
+ createdAt: new Date(row.created_at),
460
+ errorMessage: row.error_message
461
+ }));
462
+ }
463
+ /**
464
+ * Mark outbox items as done
465
+ */
466
+ async completeOutboxItems(ids) {
467
+ if (ids.length === 0)
468
+ return;
469
+ const placeholders = ids.map(() => "?").join(",");
470
+ await this.db.run(
471
+ `DELETE FROM embedding_outbox WHERE id IN (${placeholders})`,
472
+ ids
473
+ );
474
+ }
475
+ /**
476
+ * Mark outbox items as failed
477
+ */
478
+ async failOutboxItems(ids, error) {
479
+ if (ids.length === 0)
480
+ return;
481
+ const placeholders = ids.map(() => "?").join(",");
482
+ await this.db.run(
483
+ `UPDATE embedding_outbox
484
+ SET status = CASE WHEN retry_count >= 3 THEN 'failed' ELSE 'pending' END,
485
+ retry_count = retry_count + 1,
486
+ error_message = ?
487
+ WHERE id IN (${placeholders})`,
488
+ [error, ...ids]
489
+ );
490
+ }
491
+ /**
492
+ * Update memory level
493
+ */
494
+ async updateMemoryLevel(eventId, level) {
495
+ await this.initialize();
496
+ await this.db.run(
497
+ `UPDATE memory_levels SET level = ?, promoted_at = CURRENT_TIMESTAMP WHERE event_id = ?`,
498
+ [level, eventId]
499
+ );
500
+ }
501
+ /**
502
+ * Get memory level statistics
503
+ */
504
+ async getLevelStats() {
505
+ await this.initialize();
506
+ const rows = await this.db.all(
507
+ `SELECT level, COUNT(*) as count FROM memory_levels GROUP BY level`
508
+ );
509
+ return rows;
510
+ }
511
+ // ============================================================
512
+ // Endless Mode Helper Methods
513
+ // ============================================================
514
+ /**
515
+ * Get database instance for Endless Mode stores
516
+ */
517
+ getDatabase() {
518
+ return this.db;
519
+ }
520
+ /**
521
+ * Get config value for endless mode
522
+ */
523
+ async getEndlessConfig(key) {
524
+ await this.initialize();
525
+ const rows = await this.db.all(
526
+ `SELECT value FROM endless_config WHERE key = ?`,
527
+ [key]
528
+ );
529
+ if (rows.length === 0)
530
+ return null;
531
+ return JSON.parse(rows[0].value);
532
+ }
533
+ /**
534
+ * Set config value for endless mode
535
+ */
536
+ async setEndlessConfig(key, value) {
537
+ await this.initialize();
538
+ await this.db.run(
539
+ `INSERT OR REPLACE INTO endless_config (key, value, updated_at)
540
+ VALUES (?, ?, CURRENT_TIMESTAMP)`,
541
+ [key, JSON.stringify(value)]
542
+ );
543
+ }
544
+ /**
545
+ * Get all sessions
546
+ */
547
+ async getAllSessions() {
548
+ await this.initialize();
549
+ const rows = await this.db.all(
550
+ `SELECT * FROM sessions ORDER BY started_at DESC`
551
+ );
552
+ return rows.map((row) => ({
553
+ id: row.id,
554
+ startedAt: new Date(row.started_at),
555
+ endedAt: row.ended_at ? new Date(row.ended_at) : void 0,
556
+ projectPath: row.project_path,
557
+ summary: row.summary,
558
+ tags: row.tags ? JSON.parse(row.tags) : void 0
559
+ }));
560
+ }
561
+ /**
562
+ * Close database connection
563
+ */
564
+ async close() {
565
+ await this.db.close();
566
+ }
567
+ /**
568
+ * Convert database row to MemoryEvent
569
+ */
570
+ rowToEvent(row) {
571
+ return {
572
+ id: row.id,
573
+ eventType: row.event_type,
574
+ sessionId: row.session_id,
575
+ timestamp: new Date(row.timestamp),
576
+ content: row.content,
577
+ canonicalKey: row.canonical_key,
578
+ dedupeKey: row.dedupe_key,
579
+ metadata: row.metadata ? JSON.parse(row.metadata) : void 0
580
+ };
581
+ }
582
+ };
583
+
584
+ // src/core/vector-store.ts
585
+ import * as lancedb from "@lancedb/lancedb";
586
+ var VectorStore = class {
587
+ constructor(dbPath) {
588
+ this.dbPath = dbPath;
589
+ }
590
+ db = null;
591
+ table = null;
592
+ tableName = "conversations";
593
+ /**
594
+ * Initialize LanceDB connection
595
+ */
596
+ async initialize() {
597
+ if (this.db)
598
+ return;
599
+ this.db = await lancedb.connect(this.dbPath);
600
+ try {
601
+ const tables = await this.db.tableNames();
602
+ if (tables.includes(this.tableName)) {
603
+ this.table = await this.db.openTable(this.tableName);
604
+ }
605
+ } catch {
606
+ this.table = null;
607
+ }
608
+ }
609
+ /**
610
+ * Add or update vector record
611
+ */
612
+ async upsert(record) {
613
+ await this.initialize();
614
+ if (!this.db) {
615
+ throw new Error("Database not initialized");
616
+ }
617
+ const data = {
618
+ id: record.id,
619
+ eventId: record.eventId,
620
+ sessionId: record.sessionId,
621
+ eventType: record.eventType,
622
+ content: record.content,
623
+ vector: record.vector,
624
+ timestamp: record.timestamp,
625
+ metadata: JSON.stringify(record.metadata || {})
626
+ };
627
+ if (!this.table) {
628
+ this.table = await this.db.createTable(this.tableName, [data]);
629
+ } else {
630
+ await this.table.add([data]);
631
+ }
632
+ }
633
+ /**
634
+ * Add multiple vector records in batch
635
+ */
636
+ async upsertBatch(records) {
637
+ if (records.length === 0)
638
+ return;
639
+ await this.initialize();
640
+ if (!this.db) {
641
+ throw new Error("Database not initialized");
642
+ }
643
+ const data = records.map((record) => ({
644
+ id: record.id,
645
+ eventId: record.eventId,
646
+ sessionId: record.sessionId,
647
+ eventType: record.eventType,
648
+ content: record.content,
649
+ vector: record.vector,
650
+ timestamp: record.timestamp,
651
+ metadata: JSON.stringify(record.metadata || {})
652
+ }));
653
+ if (!this.table) {
654
+ this.table = await this.db.createTable(this.tableName, data);
655
+ } else {
656
+ await this.table.add(data);
657
+ }
658
+ }
659
+ /**
660
+ * Search for similar vectors
661
+ */
662
+ async search(queryVector, options = {}) {
663
+ await this.initialize();
664
+ if (!this.table) {
665
+ return [];
666
+ }
667
+ const { limit = 5, minScore = 0.7, sessionId } = options;
668
+ let query = this.table.search(queryVector).limit(limit * 2);
669
+ if (sessionId) {
670
+ query = query.where(`sessionId = '${sessionId}'`);
671
+ }
672
+ const results = await query.toArray();
673
+ return results.filter((r) => {
674
+ const score = 1 - (r._distance || 0);
675
+ return score >= minScore;
676
+ }).slice(0, limit).map((r) => ({
677
+ id: r.id,
678
+ eventId: r.eventId,
679
+ content: r.content,
680
+ score: 1 - (r._distance || 0),
681
+ sessionId: r.sessionId,
682
+ eventType: r.eventType,
683
+ timestamp: r.timestamp
684
+ }));
685
+ }
686
+ /**
687
+ * Delete vector by event ID
688
+ */
689
+ async delete(eventId) {
690
+ if (!this.table)
691
+ return;
692
+ await this.table.delete(`eventId = '${eventId}'`);
693
+ }
694
+ /**
695
+ * Get total count of vectors
696
+ */
697
+ async count() {
698
+ if (!this.table)
699
+ return 0;
700
+ const result = await this.table.countRows();
701
+ return result;
702
+ }
703
+ /**
704
+ * Check if vector exists for event
705
+ */
706
+ async exists(eventId) {
707
+ if (!this.table)
708
+ return false;
709
+ const results = await this.table.search([]).where(`eventId = '${eventId}'`).limit(1).toArray();
710
+ return results.length > 0;
711
+ }
712
+ };
713
+
714
+ // src/core/embedder.ts
715
+ import { pipeline } from "@xenova/transformers";
716
+ var Embedder = class {
717
+ pipeline = null;
718
+ modelName;
719
+ initialized = false;
720
+ constructor(modelName = "Xenova/all-MiniLM-L6-v2") {
721
+ this.modelName = modelName;
722
+ }
723
+ /**
724
+ * Initialize the embedding pipeline
725
+ */
726
+ async initialize() {
727
+ if (this.initialized)
728
+ return;
729
+ this.pipeline = await pipeline("feature-extraction", this.modelName);
730
+ this.initialized = true;
731
+ }
732
+ /**
733
+ * Generate embedding for a single text
734
+ */
735
+ async embed(text) {
736
+ await this.initialize();
737
+ if (!this.pipeline) {
738
+ throw new Error("Embedding pipeline not initialized");
739
+ }
740
+ const output = await this.pipeline(text, {
741
+ pooling: "mean",
742
+ normalize: true
743
+ });
744
+ const vector = Array.from(output.data);
745
+ return {
746
+ vector,
747
+ model: this.modelName,
748
+ dimensions: vector.length
749
+ };
750
+ }
751
+ /**
752
+ * Generate embeddings for multiple texts in batch
753
+ */
754
+ async embedBatch(texts) {
755
+ await this.initialize();
756
+ if (!this.pipeline) {
757
+ throw new Error("Embedding pipeline not initialized");
758
+ }
759
+ const results = [];
760
+ const batchSize = 32;
761
+ for (let i = 0; i < texts.length; i += batchSize) {
762
+ const batch = texts.slice(i, i + batchSize);
763
+ for (const text of batch) {
764
+ const output = await this.pipeline(text, {
765
+ pooling: "mean",
766
+ normalize: true
767
+ });
768
+ const vector = Array.from(output.data);
769
+ results.push({
770
+ vector,
771
+ model: this.modelName,
772
+ dimensions: vector.length
773
+ });
774
+ }
775
+ }
776
+ return results;
777
+ }
778
+ /**
779
+ * Get embedding dimensions for the current model
780
+ */
781
+ async getDimensions() {
782
+ const result = await this.embed("test");
783
+ return result.dimensions;
784
+ }
785
+ /**
786
+ * Check if embedder is ready
787
+ */
788
+ isReady() {
789
+ return this.initialized && this.pipeline !== null;
790
+ }
791
+ /**
792
+ * Get model name
793
+ */
794
+ getModelName() {
795
+ return this.modelName;
796
+ }
797
+ };
798
+ var defaultEmbedder = null;
799
+ function getDefaultEmbedder() {
800
+ if (!defaultEmbedder) {
801
+ defaultEmbedder = new Embedder();
802
+ }
803
+ return defaultEmbedder;
804
+ }
805
+
806
+ // src/core/vector-outbox.ts
807
+ var DEFAULT_CONFIG = {
808
+ embeddingVersion: "v1",
809
+ maxRetries: 3,
810
+ stuckThresholdMs: 5 * 60 * 1e3,
811
+ // 5 minutes
812
+ cleanupDays: 7
813
+ };
814
+
815
+ // src/core/vector-worker.ts
816
+ var DEFAULT_CONFIG2 = {
817
+ batchSize: 32,
818
+ pollIntervalMs: 1e3,
819
+ maxRetries: 3
820
+ };
821
+ var VectorWorker = class {
822
+ eventStore;
823
+ vectorStore;
824
+ embedder;
825
+ config;
826
+ running = false;
827
+ pollTimeout = null;
828
+ constructor(eventStore, vectorStore, embedder, config = {}) {
829
+ this.eventStore = eventStore;
830
+ this.vectorStore = vectorStore;
831
+ this.embedder = embedder;
832
+ this.config = { ...DEFAULT_CONFIG2, ...config };
833
+ }
834
+ /**
835
+ * Start the worker polling loop
836
+ */
837
+ start() {
838
+ if (this.running)
839
+ return;
840
+ this.running = true;
841
+ this.poll();
842
+ }
843
+ /**
844
+ * Stop the worker
845
+ */
846
+ stop() {
847
+ this.running = false;
848
+ if (this.pollTimeout) {
849
+ clearTimeout(this.pollTimeout);
850
+ this.pollTimeout = null;
851
+ }
852
+ }
853
+ /**
854
+ * Process a single batch of outbox items
855
+ */
856
+ async processBatch() {
857
+ const items = await this.eventStore.getPendingOutboxItems(this.config.batchSize);
858
+ if (items.length === 0) {
859
+ return 0;
860
+ }
861
+ const successful = [];
862
+ const failed = [];
863
+ try {
864
+ const embeddings = await this.embedder.embedBatch(items.map((i) => i.content));
865
+ const records = [];
866
+ for (let i = 0; i < items.length; i++) {
867
+ const item = items[i];
868
+ const embedding = embeddings[i];
869
+ const event = await this.eventStore.getEvent(item.eventId);
870
+ if (!event) {
871
+ failed.push(item.id);
872
+ continue;
873
+ }
874
+ records.push({
875
+ id: `vec_${item.id}`,
876
+ eventId: item.eventId,
877
+ sessionId: event.sessionId,
878
+ eventType: event.eventType,
879
+ content: item.content,
880
+ vector: embedding.vector,
881
+ timestamp: event.timestamp.toISOString(),
882
+ metadata: event.metadata
883
+ });
884
+ successful.push(item.id);
885
+ }
886
+ if (records.length > 0) {
887
+ await this.vectorStore.upsertBatch(records);
888
+ }
889
+ if (successful.length > 0) {
890
+ await this.eventStore.completeOutboxItems(successful);
891
+ }
892
+ if (failed.length > 0) {
893
+ await this.eventStore.failOutboxItems(failed, "Event not found");
894
+ }
895
+ return successful.length;
896
+ } catch (error) {
897
+ const allIds = items.map((i) => i.id);
898
+ const errorMessage = error instanceof Error ? error.message : String(error);
899
+ await this.eventStore.failOutboxItems(allIds, errorMessage);
900
+ throw error;
901
+ }
902
+ }
903
+ /**
904
+ * Poll for new items
905
+ */
906
+ async poll() {
907
+ if (!this.running)
908
+ return;
909
+ try {
910
+ await this.processBatch();
911
+ } catch (error) {
912
+ console.error("Vector worker error:", error);
913
+ }
914
+ this.pollTimeout = setTimeout(() => this.poll(), this.config.pollIntervalMs);
915
+ }
916
+ /**
917
+ * Process all pending items (blocking)
918
+ */
919
+ async processAll() {
920
+ let totalProcessed = 0;
921
+ let processed;
922
+ do {
923
+ processed = await this.processBatch();
924
+ totalProcessed += processed;
925
+ } while (processed > 0);
926
+ return totalProcessed;
927
+ }
928
+ /**
929
+ * Check if worker is running
930
+ */
931
+ isRunning() {
932
+ return this.running;
933
+ }
934
+ };
935
+ function createVectorWorker(eventStore, vectorStore, embedder, config) {
936
+ const worker = new VectorWorker(eventStore, vectorStore, embedder, config);
937
+ return worker;
938
+ }
939
+
940
+ // src/core/matcher.ts
941
+ var DEFAULT_CONFIG3 = {
942
+ weights: {
943
+ semanticSimilarity: 0.4,
944
+ ftsScore: 0.25,
945
+ recencyBonus: 0.2,
946
+ statusWeight: 0.15
947
+ },
948
+ minCombinedScore: 0.92,
949
+ minGap: 0.03,
950
+ suggestionThreshold: 0.75
951
+ };
952
+ var Matcher = class {
953
+ config;
954
+ constructor(config = {}) {
955
+ this.config = {
956
+ ...DEFAULT_CONFIG3,
957
+ ...config,
958
+ weights: { ...DEFAULT_CONFIG3.weights, ...config.weights }
959
+ };
960
+ }
961
+ /**
962
+ * Calculate combined score using AXIOMMIND weighted formula
963
+ */
964
+ calculateCombinedScore(semanticScore, ftsScore = 0, recencyDays = 0, isActive = true) {
965
+ const { weights } = this.config;
966
+ const recencyBonus = Math.max(0, 1 - recencyDays / 30);
967
+ const statusMultiplier = isActive ? 1 : 0.7;
968
+ const combinedScore = weights.semanticSimilarity * semanticScore + weights.ftsScore * ftsScore + weights.recencyBonus * recencyBonus + weights.statusWeight * statusMultiplier;
969
+ return Math.min(1, combinedScore);
970
+ }
971
+ /**
972
+ * Classify match confidence based on AXIOMMIND thresholds
973
+ */
974
+ classifyConfidence(topScore, secondScore) {
975
+ const { minCombinedScore, minGap, suggestionThreshold } = this.config;
976
+ const gap = secondScore !== null ? topScore - secondScore : Infinity;
977
+ if (topScore >= minCombinedScore && gap >= minGap) {
978
+ return "high";
979
+ }
980
+ if (topScore >= suggestionThreshold) {
981
+ return "suggested";
982
+ }
983
+ return "none";
984
+ }
985
+ /**
986
+ * Match search results to find best memory
987
+ */
988
+ matchSearchResults(results, getEventAge) {
989
+ if (results.length === 0) {
990
+ return {
991
+ match: null,
992
+ confidence: "none"
993
+ };
994
+ }
995
+ const scoredResults = results.map((result) => {
996
+ const ageDays = getEventAge(result.eventId);
997
+ const combinedScore = this.calculateCombinedScore(
998
+ result.score,
999
+ 0,
1000
+ // FTS score - would need to be passed in
1001
+ ageDays,
1002
+ true
1003
+ // Assume active
1004
+ );
1005
+ return {
1006
+ result,
1007
+ combinedScore
1008
+ };
1009
+ });
1010
+ scoredResults.sort((a, b) => b.combinedScore - a.combinedScore);
1011
+ const topResult = scoredResults[0];
1012
+ const secondScore = scoredResults.length > 1 ? scoredResults[1].combinedScore : null;
1013
+ const confidence = this.classifyConfidence(topResult.combinedScore, secondScore);
1014
+ const match = {
1015
+ event: {
1016
+ id: topResult.result.eventId,
1017
+ eventType: topResult.result.eventType,
1018
+ sessionId: topResult.result.sessionId,
1019
+ timestamp: new Date(topResult.result.timestamp),
1020
+ content: topResult.result.content,
1021
+ canonicalKey: "",
1022
+ // Would need to be fetched
1023
+ dedupeKey: ""
1024
+ // Would need to be fetched
1025
+ },
1026
+ score: topResult.combinedScore
1027
+ };
1028
+ const gap = secondScore !== null ? topResult.combinedScore - secondScore : void 0;
1029
+ const alternatives = confidence === "suggested" ? scoredResults.slice(1, 4).map((sr) => ({
1030
+ event: {
1031
+ id: sr.result.eventId,
1032
+ eventType: sr.result.eventType,
1033
+ sessionId: sr.result.sessionId,
1034
+ timestamp: new Date(sr.result.timestamp),
1035
+ content: sr.result.content,
1036
+ canonicalKey: "",
1037
+ dedupeKey: ""
1038
+ },
1039
+ score: sr.combinedScore
1040
+ })) : void 0;
1041
+ return {
1042
+ match: confidence !== "none" ? match : null,
1043
+ confidence,
1044
+ gap,
1045
+ alternatives
1046
+ };
1047
+ }
1048
+ /**
1049
+ * Calculate days between two dates
1050
+ */
1051
+ static calculateAgeDays(timestamp) {
1052
+ const now = /* @__PURE__ */ new Date();
1053
+ const diffMs = now.getTime() - timestamp.getTime();
1054
+ return diffMs / (1e3 * 60 * 60 * 24);
1055
+ }
1056
+ /**
1057
+ * Get current configuration
1058
+ */
1059
+ getConfig() {
1060
+ return { ...this.config };
1061
+ }
1062
+ };
1063
+ var defaultMatcher = null;
1064
+ function getDefaultMatcher() {
1065
+ if (!defaultMatcher) {
1066
+ defaultMatcher = new Matcher();
1067
+ }
1068
+ return defaultMatcher;
1069
+ }
1070
+
1071
+ // src/core/retriever.ts
1072
+ var DEFAULT_OPTIONS = {
1073
+ topK: 5,
1074
+ minScore: 0.7,
1075
+ maxTokens: 2e3,
1076
+ includeSessionContext: true
1077
+ };
1078
+ var Retriever = class {
1079
+ eventStore;
1080
+ vectorStore;
1081
+ embedder;
1082
+ matcher;
1083
+ constructor(eventStore, vectorStore, embedder, matcher) {
1084
+ this.eventStore = eventStore;
1085
+ this.vectorStore = vectorStore;
1086
+ this.embedder = embedder;
1087
+ this.matcher = matcher;
1088
+ }
1089
+ /**
1090
+ * Retrieve relevant memories for a query
1091
+ */
1092
+ async retrieve(query, options = {}) {
1093
+ const opts = { ...DEFAULT_OPTIONS, ...options };
1094
+ const queryEmbedding = await this.embedder.embed(query);
1095
+ const searchResults = await this.vectorStore.search(queryEmbedding.vector, {
1096
+ limit: opts.topK * 2,
1097
+ // Get extra for filtering
1098
+ minScore: opts.minScore,
1099
+ sessionId: opts.sessionId
1100
+ });
1101
+ const matchResult = this.matcher.matchSearchResults(
1102
+ searchResults,
1103
+ (eventId) => this.getEventAgeDays(eventId)
1104
+ );
1105
+ const memories = await this.enrichResults(searchResults.slice(0, opts.topK), opts);
1106
+ const context = this.buildContext(memories, opts.maxTokens);
1107
+ return {
1108
+ memories,
1109
+ matchResult,
1110
+ totalTokens: this.estimateTokens(context),
1111
+ context
1112
+ };
1113
+ }
1114
+ /**
1115
+ * Retrieve memories from a specific session
1116
+ */
1117
+ async retrieveFromSession(sessionId) {
1118
+ return this.eventStore.getSessionEvents(sessionId);
1119
+ }
1120
+ /**
1121
+ * Get recent memories across all sessions
1122
+ */
1123
+ async retrieveRecent(limit = 100) {
1124
+ return this.eventStore.getRecentEvents(limit);
1125
+ }
1126
+ /**
1127
+ * Enrich search results with full event data
1128
+ */
1129
+ async enrichResults(results, options) {
1130
+ const memories = [];
1131
+ for (const result of results) {
1132
+ const event = await this.eventStore.getEvent(result.eventId);
1133
+ if (!event)
1134
+ continue;
1135
+ let sessionContext;
1136
+ if (options.includeSessionContext) {
1137
+ sessionContext = await this.getSessionContext(event.sessionId, event.id);
1138
+ }
1139
+ memories.push({
1140
+ event,
1141
+ score: result.score,
1142
+ sessionContext
1143
+ });
1144
+ }
1145
+ return memories;
1146
+ }
1147
+ /**
1148
+ * Get surrounding context from the same session
1149
+ */
1150
+ async getSessionContext(sessionId, eventId) {
1151
+ const sessionEvents = await this.eventStore.getSessionEvents(sessionId);
1152
+ const eventIndex = sessionEvents.findIndex((e) => e.id === eventId);
1153
+ if (eventIndex === -1)
1154
+ return void 0;
1155
+ const start = Math.max(0, eventIndex - 1);
1156
+ const end = Math.min(sessionEvents.length, eventIndex + 2);
1157
+ const contextEvents = sessionEvents.slice(start, end);
1158
+ if (contextEvents.length <= 1)
1159
+ return void 0;
1160
+ return contextEvents.filter((e) => e.id !== eventId).map((e) => `[${e.eventType}]: ${e.content.slice(0, 200)}...`).join("\n");
1161
+ }
1162
+ /**
1163
+ * Build context string from memories (respecting token limit)
1164
+ */
1165
+ buildContext(memories, maxTokens) {
1166
+ const parts = [];
1167
+ let currentTokens = 0;
1168
+ for (const memory of memories) {
1169
+ const memoryText = this.formatMemory(memory);
1170
+ const memoryTokens = this.estimateTokens(memoryText);
1171
+ if (currentTokens + memoryTokens > maxTokens) {
1172
+ break;
1173
+ }
1174
+ parts.push(memoryText);
1175
+ currentTokens += memoryTokens;
1176
+ }
1177
+ if (parts.length === 0) {
1178
+ return "";
1179
+ }
1180
+ return `## Relevant Memories
1181
+
1182
+ ${parts.join("\n\n---\n\n")}`;
1183
+ }
1184
+ /**
1185
+ * Format a single memory for context
1186
+ */
1187
+ formatMemory(memory) {
1188
+ const { event, score, sessionContext } = memory;
1189
+ const date = event.timestamp.toISOString().split("T")[0];
1190
+ let text = `**${event.eventType}** (${date}, score: ${score.toFixed(2)})
1191
+ ${event.content}`;
1192
+ if (sessionContext) {
1193
+ text += `
1194
+
1195
+ _Context:_ ${sessionContext}`;
1196
+ }
1197
+ return text;
1198
+ }
1199
+ /**
1200
+ * Estimate token count (rough approximation)
1201
+ */
1202
+ estimateTokens(text) {
1203
+ return Math.ceil(text.length / 4);
1204
+ }
1205
+ /**
1206
+ * Get event age in days (for recency scoring)
1207
+ */
1208
+ getEventAgeDays(eventId) {
1209
+ return 0;
1210
+ }
1211
+ };
1212
+ function createRetriever(eventStore, vectorStore, embedder, matcher) {
1213
+ return new Retriever(eventStore, vectorStore, embedder, matcher);
1214
+ }
1215
+
1216
+ // src/core/graduation.ts
1217
+ var DEFAULT_CRITERIA = {
1218
+ L0toL1: {
1219
+ minAccessCount: 1,
1220
+ minConfidence: 0.5,
1221
+ minCrossSessionRefs: 0,
1222
+ maxAgeDays: 30
1223
+ },
1224
+ L1toL2: {
1225
+ minAccessCount: 3,
1226
+ minConfidence: 0.7,
1227
+ minCrossSessionRefs: 1,
1228
+ maxAgeDays: 60
1229
+ },
1230
+ L2toL3: {
1231
+ minAccessCount: 5,
1232
+ minConfidence: 0.85,
1233
+ minCrossSessionRefs: 2,
1234
+ maxAgeDays: 90
1235
+ },
1236
+ L3toL4: {
1237
+ minAccessCount: 10,
1238
+ minConfidence: 0.92,
1239
+ minCrossSessionRefs: 3,
1240
+ maxAgeDays: 180
1241
+ }
1242
+ };
1243
+ var GraduationPipeline = class {
1244
+ eventStore;
1245
+ criteria;
1246
+ metrics = /* @__PURE__ */ new Map();
1247
+ constructor(eventStore, criteria = {}) {
1248
+ this.eventStore = eventStore;
1249
+ this.criteria = {
1250
+ L0toL1: { ...DEFAULT_CRITERIA.L0toL1, ...criteria.L0toL1 },
1251
+ L1toL2: { ...DEFAULT_CRITERIA.L1toL2, ...criteria.L1toL2 },
1252
+ L2toL3: { ...DEFAULT_CRITERIA.L2toL3, ...criteria.L2toL3 },
1253
+ L3toL4: { ...DEFAULT_CRITERIA.L3toL4, ...criteria.L3toL4 }
1254
+ };
1255
+ }
1256
+ /**
1257
+ * Record an access to an event (used for graduation scoring)
1258
+ */
1259
+ recordAccess(eventId, fromSessionId, confidence = 1) {
1260
+ const existing = this.metrics.get(eventId);
1261
+ if (existing) {
1262
+ existing.accessCount++;
1263
+ existing.lastAccessed = /* @__PURE__ */ new Date();
1264
+ existing.confidence = Math.max(existing.confidence, confidence);
1265
+ } else {
1266
+ this.metrics.set(eventId, {
1267
+ eventId,
1268
+ accessCount: 1,
1269
+ lastAccessed: /* @__PURE__ */ new Date(),
1270
+ crossSessionRefs: 0,
1271
+ confidence
1272
+ });
1273
+ }
1274
+ }
1275
+ /**
1276
+ * Evaluate if an event should graduate to the next level
1277
+ */
1278
+ async evaluateGraduation(eventId, currentLevel) {
1279
+ const metrics = this.metrics.get(eventId);
1280
+ if (!metrics) {
1281
+ return {
1282
+ eventId,
1283
+ fromLevel: currentLevel,
1284
+ toLevel: currentLevel,
1285
+ success: false,
1286
+ reason: "No metrics available for event"
1287
+ };
1288
+ }
1289
+ const nextLevel = this.getNextLevel(currentLevel);
1290
+ if (!nextLevel) {
1291
+ return {
1292
+ eventId,
1293
+ fromLevel: currentLevel,
1294
+ toLevel: currentLevel,
1295
+ success: false,
1296
+ reason: "Already at maximum level"
1297
+ };
1298
+ }
1299
+ const criteria = this.getCriteria(currentLevel, nextLevel);
1300
+ const evaluation = this.checkCriteria(metrics, criteria);
1301
+ if (evaluation.passed) {
1302
+ await this.eventStore.updateMemoryLevel(eventId, nextLevel);
1303
+ return {
1304
+ eventId,
1305
+ fromLevel: currentLevel,
1306
+ toLevel: nextLevel,
1307
+ success: true
1308
+ };
1309
+ }
1310
+ return {
1311
+ eventId,
1312
+ fromLevel: currentLevel,
1313
+ toLevel: currentLevel,
1314
+ success: false,
1315
+ reason: evaluation.reason
1316
+ };
1317
+ }
1318
+ /**
1319
+ * Run graduation evaluation for all events at a given level
1320
+ */
1321
+ async graduateBatch(level) {
1322
+ const results = [];
1323
+ for (const [eventId, metrics] of this.metrics) {
1324
+ const result = await this.evaluateGraduation(eventId, level);
1325
+ results.push(result);
1326
+ }
1327
+ return results;
1328
+ }
1329
+ /**
1330
+ * Extract insights from graduated events (L1+)
1331
+ */
1332
+ extractInsights(events) {
1333
+ const insights = [];
1334
+ const patterns = this.detectPatterns(events);
1335
+ for (const pattern of patterns) {
1336
+ insights.push({
1337
+ id: crypto.randomUUID(),
1338
+ insightType: "pattern",
1339
+ content: pattern.description,
1340
+ canonicalKey: pattern.key,
1341
+ confidence: pattern.confidence,
1342
+ sourceEvents: pattern.eventIds,
1343
+ createdAt: /* @__PURE__ */ new Date(),
1344
+ lastUpdated: /* @__PURE__ */ new Date()
1345
+ });
1346
+ }
1347
+ const preferences = this.detectPreferences(events);
1348
+ for (const pref of preferences) {
1349
+ insights.push({
1350
+ id: crypto.randomUUID(),
1351
+ insightType: "preference",
1352
+ content: pref.description,
1353
+ canonicalKey: pref.key,
1354
+ confidence: pref.confidence,
1355
+ sourceEvents: pref.eventIds,
1356
+ createdAt: /* @__PURE__ */ new Date(),
1357
+ lastUpdated: /* @__PURE__ */ new Date()
1358
+ });
1359
+ }
1360
+ return insights;
1361
+ }
1362
+ /**
1363
+ * Get the next level in the graduation pipeline
1364
+ */
1365
+ getNextLevel(current) {
1366
+ const levels = ["L0", "L1", "L2", "L3", "L4"];
1367
+ const currentIndex = levels.indexOf(current);
1368
+ if (currentIndex === -1 || currentIndex >= levels.length - 1) {
1369
+ return null;
1370
+ }
1371
+ return levels[currentIndex + 1];
1372
+ }
1373
+ /**
1374
+ * Get criteria for level transition
1375
+ */
1376
+ getCriteria(from, to) {
1377
+ const key = `${from}to${to}`;
1378
+ return this.criteria[key] || DEFAULT_CRITERIA.L0toL1;
1379
+ }
1380
+ /**
1381
+ * Check if metrics meet criteria
1382
+ */
1383
+ checkCriteria(metrics, criteria) {
1384
+ if (metrics.accessCount < criteria.minAccessCount) {
1385
+ return {
1386
+ passed: false,
1387
+ reason: `Access count ${metrics.accessCount} < ${criteria.minAccessCount}`
1388
+ };
1389
+ }
1390
+ if (metrics.confidence < criteria.minConfidence) {
1391
+ return {
1392
+ passed: false,
1393
+ reason: `Confidence ${metrics.confidence} < ${criteria.minConfidence}`
1394
+ };
1395
+ }
1396
+ if (metrics.crossSessionRefs < criteria.minCrossSessionRefs) {
1397
+ return {
1398
+ passed: false,
1399
+ reason: `Cross-session refs ${metrics.crossSessionRefs} < ${criteria.minCrossSessionRefs}`
1400
+ };
1401
+ }
1402
+ const ageDays = (Date.now() - metrics.lastAccessed.getTime()) / (1e3 * 60 * 60 * 24);
1403
+ if (ageDays > criteria.maxAgeDays) {
1404
+ return {
1405
+ passed: false,
1406
+ reason: `Event too old: ${ageDays.toFixed(1)} days > ${criteria.maxAgeDays}`
1407
+ };
1408
+ }
1409
+ return { passed: true };
1410
+ }
1411
+ /**
1412
+ * Detect patterns in events
1413
+ */
1414
+ detectPatterns(events) {
1415
+ const keyGroups = /* @__PURE__ */ new Map();
1416
+ for (const event of events) {
1417
+ const existing = keyGroups.get(event.canonicalKey) || [];
1418
+ existing.push(event);
1419
+ keyGroups.set(event.canonicalKey, existing);
1420
+ }
1421
+ const patterns = [];
1422
+ for (const [key, groupEvents] of keyGroups) {
1423
+ if (groupEvents.length >= 2) {
1424
+ patterns.push({
1425
+ key,
1426
+ description: `Repeated topic: ${key.slice(0, 50)}`,
1427
+ confidence: Math.min(1, groupEvents.length / 5),
1428
+ eventIds: groupEvents.map((e) => e.id)
1429
+ });
1430
+ }
1431
+ }
1432
+ return patterns;
1433
+ }
1434
+ /**
1435
+ * Detect user preferences from events
1436
+ */
1437
+ detectPreferences(events) {
1438
+ const preferenceKeywords = ["prefer", "like", "want", "always", "never", "favorite"];
1439
+ const preferences = [];
1440
+ for (const event of events) {
1441
+ if (event.eventType !== "user_prompt")
1442
+ continue;
1443
+ const lowerContent = event.content.toLowerCase();
1444
+ for (const keyword of preferenceKeywords) {
1445
+ if (lowerContent.includes(keyword)) {
1446
+ preferences.push({
1447
+ key: `preference_${keyword}_${event.id.slice(0, 8)}`,
1448
+ description: `User preference: ${event.content.slice(0, 100)}`,
1449
+ confidence: 0.7,
1450
+ eventIds: [event.id]
1451
+ });
1452
+ break;
1453
+ }
1454
+ }
1455
+ }
1456
+ return preferences;
1457
+ }
1458
+ /**
1459
+ * Get graduation statistics
1460
+ */
1461
+ async getStats() {
1462
+ return this.eventStore.getLevelStats();
1463
+ }
1464
+ };
1465
+ function createGraduationPipeline(eventStore) {
1466
+ return new GraduationPipeline(eventStore);
1467
+ }
1468
+
1469
+ // src/core/metadata-extractor.ts
1470
+ function createToolObservationEmbedding(toolName, metadata, success) {
1471
+ const parts = [];
1472
+ parts.push(`Tool: ${toolName}`);
1473
+ if (metadata.filePath) {
1474
+ parts.push(`File: ${metadata.filePath}`);
1475
+ }
1476
+ if (metadata.command) {
1477
+ parts.push(`Command: ${metadata.command}`);
1478
+ }
1479
+ if (metadata.pattern) {
1480
+ parts.push(`Pattern: ${metadata.pattern}`);
1481
+ }
1482
+ if (metadata.url) {
1483
+ try {
1484
+ const url = new URL(metadata.url);
1485
+ parts.push(`URL: ${url.hostname}`);
1486
+ } catch {
1487
+ }
1488
+ }
1489
+ parts.push(`Result: ${success ? "Success" : "Failed"}`);
1490
+ return parts.join("\n");
1491
+ }
1492
+
1493
+ // src/core/working-set-store.ts
1494
+ import { randomUUID as randomUUID2 } from "crypto";
1495
+ var WorkingSetStore = class {
1496
+ constructor(eventStore, config) {
1497
+ this.eventStore = eventStore;
1498
+ this.config = config;
1499
+ }
1500
+ get db() {
1501
+ return this.eventStore.getDatabase();
1502
+ }
1503
+ /**
1504
+ * Add an event to the working set
1505
+ */
1506
+ async add(eventId, relevanceScore = 1, topics) {
1507
+ const expiresAt = new Date(
1508
+ Date.now() + this.config.workingSet.timeWindowHours * 60 * 60 * 1e3
1509
+ );
1510
+ await this.db.run(
1511
+ `INSERT OR REPLACE INTO working_set (id, event_id, added_at, relevance_score, topics, expires_at)
1512
+ VALUES (?, ?, CURRENT_TIMESTAMP, ?, ?, ?)`,
1513
+ [
1514
+ randomUUID2(),
1515
+ eventId,
1516
+ relevanceScore,
1517
+ JSON.stringify(topics || []),
1518
+ expiresAt.toISOString()
1519
+ ]
1520
+ );
1521
+ await this.enforceLimit();
1522
+ }
1523
+ /**
1524
+ * Get the current working set
1525
+ */
1526
+ async get() {
1527
+ await this.cleanup();
1528
+ const rows = await this.db.all(
1529
+ `SELECT ws.*, e.*
1530
+ FROM working_set ws
1531
+ JOIN events e ON ws.event_id = e.id
1532
+ ORDER BY ws.relevance_score DESC, ws.added_at DESC
1533
+ LIMIT ?`,
1534
+ [this.config.workingSet.maxEvents]
1535
+ );
1536
+ const events = rows.map((row) => ({
1537
+ id: row.id,
1538
+ eventType: row.event_type,
1539
+ sessionId: row.session_id,
1540
+ timestamp: new Date(row.timestamp),
1541
+ content: row.content,
1542
+ canonicalKey: row.canonical_key,
1543
+ dedupeKey: row.dedupe_key,
1544
+ metadata: row.metadata ? JSON.parse(row.metadata) : void 0
1545
+ }));
1546
+ return {
1547
+ recentEvents: events,
1548
+ lastActivity: events.length > 0 ? events[0].timestamp : /* @__PURE__ */ new Date(),
1549
+ continuityScore: await this.calculateContinuityScore()
1550
+ };
1551
+ }
1552
+ /**
1553
+ * Get working set items (metadata only)
1554
+ */
1555
+ async getItems() {
1556
+ const rows = await this.db.all(
1557
+ `SELECT * FROM working_set ORDER BY relevance_score DESC, added_at DESC`
1558
+ );
1559
+ return rows.map((row) => ({
1560
+ id: row.id,
1561
+ eventId: row.event_id,
1562
+ addedAt: new Date(row.added_at),
1563
+ relevanceScore: row.relevance_score,
1564
+ topics: row.topics ? JSON.parse(row.topics) : void 0,
1565
+ expiresAt: new Date(row.expires_at)
1566
+ }));
1567
+ }
1568
+ /**
1569
+ * Update relevance score for an event
1570
+ */
1571
+ async updateRelevance(eventId, score) {
1572
+ await this.db.run(
1573
+ `UPDATE working_set SET relevance_score = ? WHERE event_id = ?`,
1574
+ [score, eventId]
1575
+ );
1576
+ }
1577
+ /**
1578
+ * Prune specific events from working set (after consolidation)
1579
+ */
1580
+ async prune(eventIds) {
1581
+ if (eventIds.length === 0)
1582
+ return;
1583
+ const placeholders = eventIds.map(() => "?").join(",");
1584
+ await this.db.run(
1585
+ `DELETE FROM working_set WHERE event_id IN (${placeholders})`,
1586
+ eventIds
1587
+ );
1588
+ }
1589
+ /**
1590
+ * Get the count of items in working set
1591
+ */
1592
+ async count() {
1593
+ const result = await this.db.all(
1594
+ `SELECT COUNT(*) as count FROM working_set`
1595
+ );
1596
+ return result[0]?.count || 0;
1597
+ }
1598
+ /**
1599
+ * Clear the entire working set
1600
+ */
1601
+ async clear() {
1602
+ await this.db.run(`DELETE FROM working_set`);
1603
+ }
1604
+ /**
1605
+ * Check if an event is in the working set
1606
+ */
1607
+ async contains(eventId) {
1608
+ const result = await this.db.all(
1609
+ `SELECT COUNT(*) as count FROM working_set WHERE event_id = ?`,
1610
+ [eventId]
1611
+ );
1612
+ return (result[0]?.count || 0) > 0;
1613
+ }
1614
+ /**
1615
+ * Refresh expiration for an event (rehears al - keep relevant items longer)
1616
+ */
1617
+ async refresh(eventId) {
1618
+ const newExpiresAt = new Date(
1619
+ Date.now() + this.config.workingSet.timeWindowHours * 60 * 60 * 1e3
1620
+ );
1621
+ await this.db.run(
1622
+ `UPDATE working_set SET expires_at = ? WHERE event_id = ?`,
1623
+ [newExpiresAt.toISOString(), eventId]
1624
+ );
1625
+ }
1626
+ /**
1627
+ * Clean up expired items
1628
+ */
1629
+ async cleanup() {
1630
+ await this.db.run(
1631
+ `DELETE FROM working_set WHERE expires_at < datetime('now')`
1632
+ );
1633
+ }
1634
+ /**
1635
+ * Enforce the maximum size limit
1636
+ * Removes lowest relevance items when over limit
1637
+ */
1638
+ async enforceLimit() {
1639
+ const maxEvents = this.config.workingSet.maxEvents;
1640
+ const keepIds = await this.db.all(
1641
+ `SELECT id FROM working_set
1642
+ ORDER BY relevance_score DESC, added_at DESC
1643
+ LIMIT ?`,
1644
+ [maxEvents]
1645
+ );
1646
+ if (keepIds.length === 0)
1647
+ return;
1648
+ const keepIdList = keepIds.map((r) => r.id);
1649
+ const placeholders = keepIdList.map(() => "?").join(",");
1650
+ await this.db.run(
1651
+ `DELETE FROM working_set WHERE id NOT IN (${placeholders})`,
1652
+ keepIdList
1653
+ );
1654
+ }
1655
+ /**
1656
+ * Calculate continuity score based on recent context transitions
1657
+ */
1658
+ async calculateContinuityScore() {
1659
+ const result = await this.db.all(
1660
+ `SELECT AVG(continuity_score) as avg_score
1661
+ FROM continuity_log
1662
+ WHERE created_at > datetime('now', '-1 hour')`
1663
+ );
1664
+ return result[0]?.avg_score ?? 0.5;
1665
+ }
1666
+ /**
1667
+ * Get topics from current working set for context matching
1668
+ */
1669
+ async getActiveTopics() {
1670
+ const rows = await this.db.all(
1671
+ `SELECT topics FROM working_set WHERE topics IS NOT NULL`
1672
+ );
1673
+ const allTopics = /* @__PURE__ */ new Set();
1674
+ for (const row of rows) {
1675
+ const topics = JSON.parse(row.topics);
1676
+ topics.forEach((t) => allTopics.add(t));
1677
+ }
1678
+ return Array.from(allTopics);
1679
+ }
1680
+ };
1681
+ function createWorkingSetStore(eventStore, config) {
1682
+ return new WorkingSetStore(eventStore, config);
1683
+ }
1684
+
1685
+ // src/core/consolidated-store.ts
1686
+ import { randomUUID as randomUUID3 } from "crypto";
1687
+ var ConsolidatedStore = class {
1688
+ constructor(eventStore) {
1689
+ this.eventStore = eventStore;
1690
+ }
1691
+ get db() {
1692
+ return this.eventStore.getDatabase();
1693
+ }
1694
+ /**
1695
+ * Create a new consolidated memory
1696
+ */
1697
+ async create(input) {
1698
+ const memoryId = randomUUID3();
1699
+ await this.db.run(
1700
+ `INSERT INTO consolidated_memories
1701
+ (memory_id, summary, topics, source_events, confidence, created_at)
1702
+ VALUES (?, ?, ?, ?, ?, CURRENT_TIMESTAMP)`,
1703
+ [
1704
+ memoryId,
1705
+ input.summary,
1706
+ JSON.stringify(input.topics),
1707
+ JSON.stringify(input.sourceEvents),
1708
+ input.confidence
1709
+ ]
1710
+ );
1711
+ return memoryId;
1712
+ }
1713
+ /**
1714
+ * Get a consolidated memory by ID
1715
+ */
1716
+ async get(memoryId) {
1717
+ const rows = await this.db.all(
1718
+ `SELECT * FROM consolidated_memories WHERE memory_id = ?`,
1719
+ [memoryId]
1720
+ );
1721
+ if (rows.length === 0)
1722
+ return null;
1723
+ return this.rowToMemory(rows[0]);
1724
+ }
1725
+ /**
1726
+ * Search consolidated memories by query (simple text search)
1727
+ */
1728
+ async search(query, options) {
1729
+ const topK = options?.topK || 5;
1730
+ const rows = await this.db.all(
1731
+ `SELECT * FROM consolidated_memories
1732
+ WHERE summary LIKE ?
1733
+ ORDER BY confidence DESC
1734
+ LIMIT ?`,
1735
+ [`%${query}%`, topK]
1736
+ );
1737
+ return rows.map(this.rowToMemory);
1738
+ }
1739
+ /**
1740
+ * Search by topics
1741
+ */
1742
+ async searchByTopics(topics, options) {
1743
+ const topK = options?.topK || 5;
1744
+ const topicConditions = topics.map(() => `topics LIKE ?`).join(" OR ");
1745
+ const topicParams = topics.map((t) => `%"${t}"%`);
1746
+ const rows = await this.db.all(
1747
+ `SELECT * FROM consolidated_memories
1748
+ WHERE ${topicConditions}
1749
+ ORDER BY confidence DESC
1750
+ LIMIT ?`,
1751
+ [...topicParams, topK]
1752
+ );
1753
+ return rows.map(this.rowToMemory);
1754
+ }
1755
+ /**
1756
+ * Get all consolidated memories ordered by confidence
1757
+ */
1758
+ async getAll(options) {
1759
+ const limit = options?.limit || 100;
1760
+ const rows = await this.db.all(
1761
+ `SELECT * FROM consolidated_memories
1762
+ ORDER BY confidence DESC, created_at DESC
1763
+ LIMIT ?`,
1764
+ [limit]
1765
+ );
1766
+ return rows.map(this.rowToMemory);
1767
+ }
1768
+ /**
1769
+ * Get recently created memories
1770
+ */
1771
+ async getRecent(options) {
1772
+ const limit = options?.limit || 10;
1773
+ const hours = options?.hours || 24;
1774
+ const rows = await this.db.all(
1775
+ `SELECT * FROM consolidated_memories
1776
+ WHERE created_at > datetime('now', '-${hours} hours')
1777
+ ORDER BY created_at DESC
1778
+ LIMIT ?`,
1779
+ [limit]
1780
+ );
1781
+ return rows.map(this.rowToMemory);
1782
+ }
1783
+ /**
1784
+ * Mark a memory as accessed (tracks usage for importance scoring)
1785
+ */
1786
+ async markAccessed(memoryId) {
1787
+ await this.db.run(
1788
+ `UPDATE consolidated_memories
1789
+ SET accessed_at = CURRENT_TIMESTAMP,
1790
+ access_count = access_count + 1
1791
+ WHERE memory_id = ?`,
1792
+ [memoryId]
1793
+ );
1794
+ }
1795
+ /**
1796
+ * Update confidence score for a memory
1797
+ */
1798
+ async updateConfidence(memoryId, confidence) {
1799
+ await this.db.run(
1800
+ `UPDATE consolidated_memories
1801
+ SET confidence = ?
1802
+ WHERE memory_id = ?`,
1803
+ [confidence, memoryId]
1804
+ );
1805
+ }
1806
+ /**
1807
+ * Delete a consolidated memory
1808
+ */
1809
+ async delete(memoryId) {
1810
+ await this.db.run(
1811
+ `DELETE FROM consolidated_memories WHERE memory_id = ?`,
1812
+ [memoryId]
1813
+ );
1814
+ }
1815
+ /**
1816
+ * Get count of consolidated memories
1817
+ */
1818
+ async count() {
1819
+ const result = await this.db.all(
1820
+ `SELECT COUNT(*) as count FROM consolidated_memories`
1821
+ );
1822
+ return result[0]?.count || 0;
1823
+ }
1824
+ /**
1825
+ * Get most accessed memories (for importance scoring)
1826
+ */
1827
+ async getMostAccessed(limit = 10) {
1828
+ const rows = await this.db.all(
1829
+ `SELECT * FROM consolidated_memories
1830
+ WHERE access_count > 0
1831
+ ORDER BY access_count DESC
1832
+ LIMIT ?`,
1833
+ [limit]
1834
+ );
1835
+ return rows.map(this.rowToMemory);
1836
+ }
1837
+ /**
1838
+ * Get statistics about consolidated memories
1839
+ */
1840
+ async getStats() {
1841
+ const total = await this.count();
1842
+ const avgResult = await this.db.all(
1843
+ `SELECT AVG(confidence) as avg FROM consolidated_memories`
1844
+ );
1845
+ const averageConfidence = avgResult[0]?.avg || 0;
1846
+ const recentResult = await this.db.all(
1847
+ `SELECT COUNT(*) as count FROM consolidated_memories
1848
+ WHERE created_at > datetime('now', '-24 hours')`
1849
+ );
1850
+ const recentCount = recentResult[0]?.count || 0;
1851
+ const allMemories = await this.getAll({ limit: 1e3 });
1852
+ const topicCounts = {};
1853
+ for (const memory of allMemories) {
1854
+ for (const topic of memory.topics) {
1855
+ topicCounts[topic] = (topicCounts[topic] || 0) + 1;
1856
+ }
1857
+ }
1858
+ return {
1859
+ total,
1860
+ averageConfidence,
1861
+ topicCounts,
1862
+ recentCount
1863
+ };
1864
+ }
1865
+ /**
1866
+ * Check if source events are already consolidated
1867
+ */
1868
+ async isAlreadyConsolidated(eventIds) {
1869
+ for (const eventId of eventIds) {
1870
+ const result = await this.db.all(
1871
+ `SELECT COUNT(*) as count FROM consolidated_memories
1872
+ WHERE source_events LIKE ?`,
1873
+ [`%"${eventId}"%`]
1874
+ );
1875
+ if ((result[0]?.count || 0) > 0)
1876
+ return true;
1877
+ }
1878
+ return false;
1879
+ }
1880
+ /**
1881
+ * Get the last consolidation time
1882
+ */
1883
+ async getLastConsolidationTime() {
1884
+ const result = await this.db.all(
1885
+ `SELECT created_at FROM consolidated_memories
1886
+ ORDER BY created_at DESC
1887
+ LIMIT 1`
1888
+ );
1889
+ if (result.length === 0)
1890
+ return null;
1891
+ return new Date(result[0].created_at);
1892
+ }
1893
+ /**
1894
+ * Convert database row to ConsolidatedMemory
1895
+ */
1896
+ rowToMemory(row) {
1897
+ return {
1898
+ memoryId: row.memory_id,
1899
+ summary: row.summary,
1900
+ topics: JSON.parse(row.topics || "[]"),
1901
+ sourceEvents: JSON.parse(row.source_events || "[]"),
1902
+ confidence: row.confidence,
1903
+ createdAt: new Date(row.created_at),
1904
+ accessedAt: row.accessed_at ? new Date(row.accessed_at) : void 0,
1905
+ accessCount: row.access_count || 0
1906
+ };
1907
+ }
1908
+ };
1909
+ function createConsolidatedStore(eventStore) {
1910
+ return new ConsolidatedStore(eventStore);
1911
+ }
1912
+
1913
+ // src/core/consolidation-worker.ts
1914
+ var ConsolidationWorker = class {
1915
+ constructor(workingSetStore, consolidatedStore, config) {
1916
+ this.workingSetStore = workingSetStore;
1917
+ this.consolidatedStore = consolidatedStore;
1918
+ this.config = config;
1919
+ }
1920
+ running = false;
1921
+ timeout = null;
1922
+ lastActivity = /* @__PURE__ */ new Date();
1923
+ /**
1924
+ * Start the consolidation worker
1925
+ */
1926
+ start() {
1927
+ if (this.running)
1928
+ return;
1929
+ this.running = true;
1930
+ this.scheduleNext();
1931
+ }
1932
+ /**
1933
+ * Stop the consolidation worker
1934
+ */
1935
+ stop() {
1936
+ this.running = false;
1937
+ if (this.timeout) {
1938
+ clearTimeout(this.timeout);
1939
+ this.timeout = null;
1940
+ }
1941
+ }
1942
+ /**
1943
+ * Record activity (resets idle timer)
1944
+ */
1945
+ recordActivity() {
1946
+ this.lastActivity = /* @__PURE__ */ new Date();
1947
+ }
1948
+ /**
1949
+ * Check if currently running
1950
+ */
1951
+ isRunning() {
1952
+ return this.running;
1953
+ }
1954
+ /**
1955
+ * Force a consolidation run (manual trigger)
1956
+ */
1957
+ async forceRun() {
1958
+ return await this.consolidate();
1959
+ }
1960
+ /**
1961
+ * Schedule the next consolidation check
1962
+ */
1963
+ scheduleNext() {
1964
+ if (!this.running)
1965
+ return;
1966
+ this.timeout = setTimeout(
1967
+ () => this.run(),
1968
+ this.config.consolidation.triggerIntervalMs
1969
+ );
1970
+ }
1971
+ /**
1972
+ * Run consolidation check
1973
+ */
1974
+ async run() {
1975
+ if (!this.running)
1976
+ return;
1977
+ try {
1978
+ await this.checkAndConsolidate();
1979
+ } catch (error) {
1980
+ console.error("Consolidation error:", error);
1981
+ }
1982
+ this.scheduleNext();
1983
+ }
1984
+ /**
1985
+ * Check conditions and consolidate if needed
1986
+ */
1987
+ async checkAndConsolidate() {
1988
+ const workingSet = await this.workingSetStore.get();
1989
+ if (!this.shouldConsolidate(workingSet)) {
1990
+ return;
1991
+ }
1992
+ await this.consolidate();
1993
+ }
1994
+ /**
1995
+ * Perform consolidation
1996
+ */
1997
+ async consolidate() {
1998
+ const workingSet = await this.workingSetStore.get();
1999
+ if (workingSet.recentEvents.length < 3) {
2000
+ return 0;
2001
+ }
2002
+ const groups = this.groupByTopic(workingSet.recentEvents);
2003
+ let consolidatedCount = 0;
2004
+ for (const group of groups) {
2005
+ if (group.events.length < 3)
2006
+ continue;
2007
+ const eventIds = group.events.map((e) => e.id);
2008
+ const alreadyConsolidated = await this.consolidatedStore.isAlreadyConsolidated(eventIds);
2009
+ if (alreadyConsolidated)
2010
+ continue;
2011
+ const summary = await this.summarize(group);
2012
+ await this.consolidatedStore.create({
2013
+ summary,
2014
+ topics: group.topics,
2015
+ sourceEvents: eventIds,
2016
+ confidence: this.calculateConfidence(group)
2017
+ });
2018
+ consolidatedCount++;
2019
+ }
2020
+ if (consolidatedCount > 0) {
2021
+ const consolidatedEventIds = groups.filter((g) => g.events.length >= 3).flatMap((g) => g.events.map((e) => e.id));
2022
+ const oldEventIds = consolidatedEventIds.filter((id) => {
2023
+ const event = workingSet.recentEvents.find((e) => e.id === id);
2024
+ if (!event)
2025
+ return false;
2026
+ const ageHours = (Date.now() - event.timestamp.getTime()) / (1e3 * 60 * 60);
2027
+ return ageHours > this.config.workingSet.timeWindowHours / 2;
2028
+ });
2029
+ if (oldEventIds.length > 0) {
2030
+ await this.workingSetStore.prune(oldEventIds);
2031
+ }
2032
+ }
2033
+ return consolidatedCount;
2034
+ }
2035
+ /**
2036
+ * Check if consolidation should run
2037
+ */
2038
+ shouldConsolidate(workingSet) {
2039
+ if (workingSet.recentEvents.length >= this.config.consolidation.triggerEventCount) {
2040
+ return true;
2041
+ }
2042
+ const idleTime = Date.now() - this.lastActivity.getTime();
2043
+ if (idleTime >= this.config.consolidation.triggerIdleMs) {
2044
+ return true;
2045
+ }
2046
+ return false;
2047
+ }
2048
+ /**
2049
+ * Group events by topic using simple keyword extraction
2050
+ */
2051
+ groupByTopic(events) {
2052
+ const groups = /* @__PURE__ */ new Map();
2053
+ for (const event of events) {
2054
+ const topics = this.extractTopics(event.content);
2055
+ for (const topic of topics) {
2056
+ if (!groups.has(topic)) {
2057
+ groups.set(topic, { topics: [topic], events: [] });
2058
+ }
2059
+ const group = groups.get(topic);
2060
+ if (!group.events.find((e) => e.id === event.id)) {
2061
+ group.events.push(event);
2062
+ }
2063
+ }
2064
+ }
2065
+ const mergedGroups = this.mergeOverlappingGroups(Array.from(groups.values()));
2066
+ return mergedGroups;
2067
+ }
2068
+ /**
2069
+ * Extract topics from content using simple keyword extraction
2070
+ */
2071
+ extractTopics(content) {
2072
+ const topics = [];
2073
+ const codePatterns = [
2074
+ /\b(function|class|interface|type|const|let|var)\s+(\w+)/gi,
2075
+ /\b(import|export)\s+.*?from\s+['"]([^'"]+)['"]/gi,
2076
+ /\bfile[:\s]+([^\s,]+)/gi
2077
+ ];
2078
+ for (const pattern of codePatterns) {
2079
+ let match;
2080
+ while ((match = pattern.exec(content)) !== null) {
2081
+ const keyword = match[2] || match[1];
2082
+ if (keyword && keyword.length > 2) {
2083
+ topics.push(keyword.toLowerCase());
2084
+ }
2085
+ }
2086
+ }
2087
+ const commonTerms = [
2088
+ "bug",
2089
+ "fix",
2090
+ "error",
2091
+ "issue",
2092
+ "feature",
2093
+ "test",
2094
+ "refactor",
2095
+ "implement",
2096
+ "add",
2097
+ "remove",
2098
+ "update",
2099
+ "change",
2100
+ "modify",
2101
+ "create",
2102
+ "delete"
2103
+ ];
2104
+ const contentLower = content.toLowerCase();
2105
+ for (const term of commonTerms) {
2106
+ if (contentLower.includes(term)) {
2107
+ topics.push(term);
2108
+ }
2109
+ }
2110
+ return [...new Set(topics)].slice(0, 5);
2111
+ }
2112
+ /**
2113
+ * Merge groups that have significant event overlap
2114
+ */
2115
+ mergeOverlappingGroups(groups) {
2116
+ const merged = [];
2117
+ for (const group of groups) {
2118
+ let foundMerge = false;
2119
+ for (const existing of merged) {
2120
+ const overlap = group.events.filter(
2121
+ (e) => existing.events.some((ex) => ex.id === e.id)
2122
+ );
2123
+ if (overlap.length > group.events.length / 2) {
2124
+ existing.topics = [.../* @__PURE__ */ new Set([...existing.topics, ...group.topics])];
2125
+ for (const event of group.events) {
2126
+ if (!existing.events.find((e) => e.id === event.id)) {
2127
+ existing.events.push(event);
2128
+ }
2129
+ }
2130
+ foundMerge = true;
2131
+ break;
2132
+ }
2133
+ }
2134
+ if (!foundMerge) {
2135
+ merged.push(group);
2136
+ }
2137
+ }
2138
+ return merged;
2139
+ }
2140
+ /**
2141
+ * Generate summary for a group of events
2142
+ * Rule-based extraction (no LLM by default)
2143
+ */
2144
+ async summarize(group) {
2145
+ if (this.config.consolidation.useLLMSummarization) {
2146
+ return this.ruleBasedSummary(group);
2147
+ }
2148
+ return this.ruleBasedSummary(group);
2149
+ }
2150
+ /**
2151
+ * Rule-based summary generation
2152
+ */
2153
+ ruleBasedSummary(group) {
2154
+ const keyPoints = [];
2155
+ for (const event of group.events.slice(0, 10)) {
2156
+ const keyPoint = this.extractKeyPoint(event.content);
2157
+ if (keyPoint) {
2158
+ keyPoints.push(keyPoint);
2159
+ }
2160
+ }
2161
+ const topicsStr = group.topics.slice(0, 3).join(", ");
2162
+ const summary = [
2163
+ `Topics: ${topicsStr}`,
2164
+ "",
2165
+ "Key points:",
2166
+ ...keyPoints.map((kp) => `- ${kp}`)
2167
+ ].join("\n");
2168
+ return summary;
2169
+ }
2170
+ /**
2171
+ * Extract key point from content
2172
+ */
2173
+ extractKeyPoint(content) {
2174
+ const sentences = content.split(/[.!?\n]+/).filter((s) => s.trim().length > 10);
2175
+ if (sentences.length === 0)
2176
+ return null;
2177
+ const firstSentence = sentences[0].trim();
2178
+ if (firstSentence.length > 100) {
2179
+ return firstSentence.slice(0, 100) + "...";
2180
+ }
2181
+ return firstSentence;
2182
+ }
2183
+ /**
2184
+ * Calculate confidence score for a group
2185
+ */
2186
+ calculateConfidence(group) {
2187
+ const eventScore = Math.min(group.events.length / 10, 1);
2188
+ const timeScore = this.calculateTimeProximity(group.events);
2189
+ const topicScore = Math.min(3 / group.topics.length, 1);
2190
+ return eventScore * 0.4 + timeScore * 0.4 + topicScore * 0.2;
2191
+ }
2192
+ /**
2193
+ * Calculate time proximity score
2194
+ */
2195
+ calculateTimeProximity(events) {
2196
+ if (events.length < 2)
2197
+ return 1;
2198
+ const timestamps = events.map((e) => e.timestamp.getTime()).sort((a, b) => a - b);
2199
+ const timeSpan = timestamps[timestamps.length - 1] - timestamps[0];
2200
+ const avgGap = timeSpan / (events.length - 1);
2201
+ const hourInMs = 60 * 60 * 1e3;
2202
+ return Math.max(0, 1 - avgGap / (24 * hourInMs));
2203
+ }
2204
+ };
2205
+ function createConsolidationWorker(workingSetStore, consolidatedStore, config) {
2206
+ return new ConsolidationWorker(workingSetStore, consolidatedStore, config);
2207
+ }
2208
+
2209
+ // src/core/continuity-manager.ts
2210
+ import { randomUUID as randomUUID4 } from "crypto";
2211
+ var ContinuityManager = class {
2212
+ constructor(eventStore, config) {
2213
+ this.eventStore = eventStore;
2214
+ this.config = config;
2215
+ }
2216
+ lastContext = null;
2217
+ get db() {
2218
+ return this.eventStore.getDatabase();
2219
+ }
2220
+ /**
2221
+ * Calculate continuity score between current and previous context
2222
+ */
2223
+ async calculateScore(currentContext, previousContext) {
2224
+ const prev = previousContext || this.lastContext;
2225
+ if (!prev) {
2226
+ this.lastContext = currentContext;
2227
+ return { score: 0.5, transitionType: "break" };
2228
+ }
2229
+ let score = 0;
2230
+ const topicOverlap = this.calculateOverlap(
2231
+ currentContext.topics,
2232
+ prev.topics
2233
+ );
2234
+ score += topicOverlap * 0.3;
2235
+ const fileOverlap = this.calculateOverlap(
2236
+ currentContext.files,
2237
+ prev.files
2238
+ );
2239
+ score += fileOverlap * 0.2;
2240
+ const timeDiff = currentContext.timestamp - prev.timestamp;
2241
+ const decayHours = this.config.continuity.topicDecayHours;
2242
+ const timeScore = Math.exp(-timeDiff / (decayHours * 36e5));
2243
+ score += timeScore * 0.3;
2244
+ const entityOverlap = this.calculateOverlap(
2245
+ currentContext.entities,
2246
+ prev.entities
2247
+ );
2248
+ score += entityOverlap * 0.2;
2249
+ const transitionType = this.determineTransitionType(score);
2250
+ await this.logTransition(currentContext, prev, score, transitionType);
2251
+ this.lastContext = currentContext;
2252
+ return { score, transitionType };
2253
+ }
2254
+ /**
2255
+ * Create a context snapshot from current state
2256
+ */
2257
+ createSnapshot(id, content, metadata) {
2258
+ return {
2259
+ id,
2260
+ timestamp: Date.now(),
2261
+ topics: this.extractTopics(content),
2262
+ files: metadata?.files || this.extractFiles(content),
2263
+ entities: metadata?.entities || this.extractEntities(content)
2264
+ };
2265
+ }
2266
+ /**
2267
+ * Get recent continuity logs
2268
+ */
2269
+ async getRecentLogs(limit = 10) {
2270
+ const rows = await this.db.all(
2271
+ `SELECT * FROM continuity_log
2272
+ ORDER BY created_at DESC
2273
+ LIMIT ?`,
2274
+ [limit]
2275
+ );
2276
+ return rows.map((row) => ({
2277
+ logId: row.log_id,
2278
+ fromContextId: row.from_context_id,
2279
+ toContextId: row.to_context_id,
2280
+ continuityScore: row.continuity_score,
2281
+ transitionType: row.transition_type,
2282
+ createdAt: new Date(row.created_at)
2283
+ }));
2284
+ }
2285
+ /**
2286
+ * Get average continuity score over time period
2287
+ */
2288
+ async getAverageScore(hours = 1) {
2289
+ const result = await this.db.all(
2290
+ `SELECT AVG(continuity_score) as avg_score
2291
+ FROM continuity_log
2292
+ WHERE created_at > datetime('now', '-${hours} hours')`
2293
+ );
2294
+ return result[0]?.avg_score ?? 0.5;
2295
+ }
2296
+ /**
2297
+ * Get transition type distribution
2298
+ */
2299
+ async getTransitionStats(hours = 24) {
2300
+ const rows = await this.db.all(
2301
+ `SELECT transition_type, COUNT(*) as count
2302
+ FROM continuity_log
2303
+ WHERE created_at > datetime('now', '-${hours} hours')
2304
+ GROUP BY transition_type`
2305
+ );
2306
+ const stats = {
2307
+ seamless: 0,
2308
+ topic_shift: 0,
2309
+ break: 0
2310
+ };
2311
+ for (const row of rows) {
2312
+ stats[row.transition_type] = row.count;
2313
+ }
2314
+ return stats;
2315
+ }
2316
+ /**
2317
+ * Clear old continuity logs
2318
+ */
2319
+ async cleanup(olderThanDays = 7) {
2320
+ const result = await this.db.all(
2321
+ `DELETE FROM continuity_log
2322
+ WHERE created_at < datetime('now', '-${olderThanDays} days')
2323
+ RETURNING COUNT(*) as changes`
2324
+ );
2325
+ return result[0]?.changes || 0;
2326
+ }
2327
+ /**
2328
+ * Calculate overlap between two arrays
2329
+ */
2330
+ calculateOverlap(a, b) {
2331
+ if (a.length === 0 || b.length === 0)
2332
+ return 0;
2333
+ const setA = new Set(a.map((s) => s.toLowerCase()));
2334
+ const setB = new Set(b.map((s) => s.toLowerCase()));
2335
+ const intersection = [...setA].filter((x) => setB.has(x));
2336
+ const union = /* @__PURE__ */ new Set([...setA, ...setB]);
2337
+ return intersection.length / union.size;
2338
+ }
2339
+ /**
2340
+ * Determine transition type based on score
2341
+ */
2342
+ determineTransitionType(score) {
2343
+ if (score >= this.config.continuity.minScoreForSeamless) {
2344
+ return "seamless";
2345
+ } else if (score >= 0.4) {
2346
+ return "topic_shift";
2347
+ } else {
2348
+ return "break";
2349
+ }
2350
+ }
2351
+ /**
2352
+ * Log a context transition
2353
+ */
2354
+ async logTransition(current, previous, score, type) {
2355
+ await this.db.run(
2356
+ `INSERT INTO continuity_log
2357
+ (log_id, from_context_id, to_context_id, continuity_score, transition_type, created_at)
2358
+ VALUES (?, ?, ?, ?, ?, CURRENT_TIMESTAMP)`,
2359
+ [randomUUID4(), previous.id, current.id, score, type]
2360
+ );
2361
+ }
2362
+ /**
2363
+ * Extract topics from content
2364
+ */
2365
+ extractTopics(content) {
2366
+ const topics = [];
2367
+ const contentLower = content.toLowerCase();
2368
+ const langPatterns = [
2369
+ { pattern: /typescript|\.ts\b/i, topic: "typescript" },
2370
+ { pattern: /javascript|\.js\b/i, topic: "javascript" },
2371
+ { pattern: /python|\.py\b/i, topic: "python" },
2372
+ { pattern: /rust|\.rs\b/i, topic: "rust" },
2373
+ { pattern: /go\b|golang/i, topic: "go" }
2374
+ ];
2375
+ for (const { pattern, topic } of langPatterns) {
2376
+ if (pattern.test(content)) {
2377
+ topics.push(topic);
2378
+ }
2379
+ }
2380
+ const devTopics = [
2381
+ "api",
2382
+ "database",
2383
+ "test",
2384
+ "bug",
2385
+ "feature",
2386
+ "refactor",
2387
+ "component",
2388
+ "function",
2389
+ "class",
2390
+ "module",
2391
+ "hook",
2392
+ "deploy",
2393
+ "build",
2394
+ "config",
2395
+ "docker",
2396
+ "git"
2397
+ ];
2398
+ for (const topic of devTopics) {
2399
+ if (contentLower.includes(topic)) {
2400
+ topics.push(topic);
2401
+ }
2402
+ }
2403
+ return [...new Set(topics)].slice(0, 10);
2404
+ }
2405
+ /**
2406
+ * Extract file paths from content
2407
+ */
2408
+ extractFiles(content) {
2409
+ const filePatterns = [
2410
+ /(?:^|\s)([a-zA-Z0-9_\-./]+\.[a-zA-Z0-9]+)(?:\s|$|:)/gm,
2411
+ /['"](\.?\/[^'"]+\.[a-zA-Z0-9]+)['"]/g,
2412
+ /file[:\s]+([^\s,]+)/gi
2413
+ ];
2414
+ const files = /* @__PURE__ */ new Set();
2415
+ for (const pattern of filePatterns) {
2416
+ let match;
2417
+ while ((match = pattern.exec(content)) !== null) {
2418
+ const file = match[1];
2419
+ if (file && file.length > 3 && file.length < 100) {
2420
+ if (!file.match(/^(https?:|mailto:|ftp:)/i)) {
2421
+ files.add(file);
2422
+ }
2423
+ }
2424
+ }
2425
+ }
2426
+ return Array.from(files).slice(0, 10);
2427
+ }
2428
+ /**
2429
+ * Extract entity names from content (functions, classes, variables)
2430
+ */
2431
+ extractEntities(content) {
2432
+ const entities = /* @__PURE__ */ new Set();
2433
+ const entityPatterns = [
2434
+ /\b(function|const|let|var|class|interface|type)\s+([a-zA-Z_][a-zA-Z0-9_]*)/g,
2435
+ /\b([A-Z][a-zA-Z0-9_]*(?:Component|Service|Store|Manager|Handler|Factory|Provider))\b/g,
2436
+ /\b(use[A-Z][a-zA-Z0-9_]*)\b/g
2437
+ // React hooks
2438
+ ];
2439
+ for (const pattern of entityPatterns) {
2440
+ let match;
2441
+ while ((match = pattern.exec(content)) !== null) {
2442
+ const entity = match[2] || match[1];
2443
+ if (entity && entity.length > 2) {
2444
+ entities.add(entity);
2445
+ }
2446
+ }
2447
+ }
2448
+ return Array.from(entities).slice(0, 20);
2449
+ }
2450
+ /**
2451
+ * Reset the last context (for testing or manual reset)
2452
+ */
2453
+ resetLastContext() {
2454
+ this.lastContext = null;
2455
+ }
2456
+ /**
2457
+ * Get the last context snapshot
2458
+ */
2459
+ getLastContext() {
2460
+ return this.lastContext;
2461
+ }
2462
+ };
2463
+ function createContinuityManager(eventStore, config) {
2464
+ return new ContinuityManager(eventStore, config);
2465
+ }
2466
+
2467
+ // src/services/memory-service.ts
2468
+ var MemoryService = class {
2469
+ eventStore;
2470
+ vectorStore;
2471
+ embedder;
2472
+ matcher;
2473
+ retriever;
2474
+ graduation;
2475
+ vectorWorker = null;
2476
+ initialized = false;
2477
+ // Endless Mode components
2478
+ workingSetStore = null;
2479
+ consolidatedStore = null;
2480
+ consolidationWorker = null;
2481
+ continuityManager = null;
2482
+ endlessMode = "session";
2483
+ constructor(config) {
2484
+ const storagePath = this.expandPath(config.storagePath);
2485
+ if (!fs.existsSync(storagePath)) {
2486
+ fs.mkdirSync(storagePath, { recursive: true });
2487
+ }
2488
+ this.eventStore = new EventStore(path.join(storagePath, "events.duckdb"));
2489
+ this.vectorStore = new VectorStore(path.join(storagePath, "vectors"));
2490
+ this.embedder = config.embeddingModel ? new Embedder(config.embeddingModel) : getDefaultEmbedder();
2491
+ this.matcher = getDefaultMatcher();
2492
+ this.retriever = createRetriever(
2493
+ this.eventStore,
2494
+ this.vectorStore,
2495
+ this.embedder,
2496
+ this.matcher
2497
+ );
2498
+ this.graduation = createGraduationPipeline(this.eventStore);
2499
+ }
2500
+ /**
2501
+ * Initialize all components
2502
+ */
2503
+ async initialize() {
2504
+ if (this.initialized)
2505
+ return;
2506
+ await this.eventStore.initialize();
2507
+ await this.vectorStore.initialize();
2508
+ await this.embedder.initialize();
2509
+ this.vectorWorker = createVectorWorker(
2510
+ this.eventStore,
2511
+ this.vectorStore,
2512
+ this.embedder
2513
+ );
2514
+ this.vectorWorker.start();
2515
+ const savedMode = await this.eventStore.getEndlessConfig("mode");
2516
+ if (savedMode === "endless") {
2517
+ this.endlessMode = "endless";
2518
+ await this.initializeEndlessMode();
2519
+ }
2520
+ this.initialized = true;
2521
+ }
2522
+ /**
2523
+ * Start a new session
2524
+ */
2525
+ async startSession(sessionId, projectPath) {
2526
+ await this.initialize();
2527
+ await this.eventStore.upsertSession({
2528
+ id: sessionId,
2529
+ startedAt: /* @__PURE__ */ new Date(),
2530
+ projectPath
2531
+ });
2532
+ }
2533
+ /**
2534
+ * End a session
2535
+ */
2536
+ async endSession(sessionId, summary) {
2537
+ await this.initialize();
2538
+ await this.eventStore.upsertSession({
2539
+ id: sessionId,
2540
+ endedAt: /* @__PURE__ */ new Date(),
2541
+ summary
2542
+ });
2543
+ }
2544
+ /**
2545
+ * Store a user prompt
2546
+ */
2547
+ async storeUserPrompt(sessionId, content, metadata) {
2548
+ await this.initialize();
2549
+ const result = await this.eventStore.append({
2550
+ eventType: "user_prompt",
2551
+ sessionId,
2552
+ timestamp: /* @__PURE__ */ new Date(),
2553
+ content,
2554
+ metadata
2555
+ });
2556
+ if (result.success && !result.isDuplicate) {
2557
+ await this.eventStore.enqueueForEmbedding(result.eventId, content);
2558
+ }
2559
+ return result;
2560
+ }
2561
+ /**
2562
+ * Store an agent response
2563
+ */
2564
+ async storeAgentResponse(sessionId, content, metadata) {
2565
+ await this.initialize();
2566
+ const result = await this.eventStore.append({
2567
+ eventType: "agent_response",
2568
+ sessionId,
2569
+ timestamp: /* @__PURE__ */ new Date(),
2570
+ content,
2571
+ metadata
2572
+ });
2573
+ if (result.success && !result.isDuplicate) {
2574
+ await this.eventStore.enqueueForEmbedding(result.eventId, content);
2575
+ }
2576
+ return result;
2577
+ }
2578
+ /**
2579
+ * Store a session summary
2580
+ */
2581
+ async storeSessionSummary(sessionId, summary) {
2582
+ await this.initialize();
2583
+ const result = await this.eventStore.append({
2584
+ eventType: "session_summary",
2585
+ sessionId,
2586
+ timestamp: /* @__PURE__ */ new Date(),
2587
+ content: summary
2588
+ });
2589
+ if (result.success && !result.isDuplicate) {
2590
+ await this.eventStore.enqueueForEmbedding(result.eventId, summary);
2591
+ }
2592
+ return result;
2593
+ }
2594
+ /**
2595
+ * Store a tool observation
2596
+ */
2597
+ async storeToolObservation(sessionId, payload) {
2598
+ await this.initialize();
2599
+ const content = JSON.stringify(payload);
2600
+ const result = await this.eventStore.append({
2601
+ eventType: "tool_observation",
2602
+ sessionId,
2603
+ timestamp: /* @__PURE__ */ new Date(),
2604
+ content,
2605
+ metadata: {
2606
+ toolName: payload.toolName,
2607
+ success: payload.success
2608
+ }
2609
+ });
2610
+ if (result.success && !result.isDuplicate) {
2611
+ const embeddingContent = createToolObservationEmbedding(
2612
+ payload.toolName,
2613
+ payload.metadata || {},
2614
+ payload.success
2615
+ );
2616
+ await this.eventStore.enqueueForEmbedding(result.eventId, embeddingContent);
2617
+ }
2618
+ return result;
2619
+ }
2620
+ /**
2621
+ * Retrieve relevant memories for a query
2622
+ */
2623
+ async retrieveMemories(query, options) {
2624
+ await this.initialize();
2625
+ if (this.vectorWorker) {
2626
+ await this.vectorWorker.processAll();
2627
+ }
2628
+ return this.retriever.retrieve(query, options);
2629
+ }
2630
+ /**
2631
+ * Get session history
2632
+ */
2633
+ async getSessionHistory(sessionId) {
2634
+ await this.initialize();
2635
+ return this.eventStore.getSessionEvents(sessionId);
2636
+ }
2637
+ /**
2638
+ * Get recent events
2639
+ */
2640
+ async getRecentEvents(limit = 100) {
2641
+ await this.initialize();
2642
+ return this.eventStore.getRecentEvents(limit);
2643
+ }
2644
+ /**
2645
+ * Get memory statistics
2646
+ */
2647
+ async getStats() {
2648
+ await this.initialize();
2649
+ const recentEvents = await this.eventStore.getRecentEvents(1e4);
2650
+ const vectorCount = await this.vectorStore.count();
2651
+ const levelStats = await this.graduation.getStats();
2652
+ return {
2653
+ totalEvents: recentEvents.length,
2654
+ vectorCount,
2655
+ levelStats
2656
+ };
2657
+ }
2658
+ /**
2659
+ * Process pending embeddings
2660
+ */
2661
+ async processPendingEmbeddings() {
2662
+ if (this.vectorWorker) {
2663
+ return this.vectorWorker.processAll();
2664
+ }
2665
+ return 0;
2666
+ }
2667
+ /**
2668
+ * Format retrieval results as context for Claude
2669
+ */
2670
+ formatAsContext(result) {
2671
+ if (!result.context) {
2672
+ return "";
2673
+ }
2674
+ const confidence = result.matchResult.confidence;
2675
+ let header = "";
2676
+ if (confidence === "high") {
2677
+ header = "\u{1F3AF} **High-confidence memory match found:**\n\n";
2678
+ } else if (confidence === "suggested") {
2679
+ header = "\u{1F4A1} **Suggested memories (may be relevant):**\n\n";
2680
+ }
2681
+ return header + result.context;
2682
+ }
2683
+ // ============================================================
2684
+ // Endless Mode Methods
2685
+ // ============================================================
2686
+ /**
2687
+ * Get the default endless mode config
2688
+ */
2689
+ getDefaultEndlessConfig() {
2690
+ return {
2691
+ enabled: true,
2692
+ workingSet: {
2693
+ maxEvents: 100,
2694
+ timeWindowHours: 24,
2695
+ minRelevanceScore: 0.5
2696
+ },
2697
+ consolidation: {
2698
+ triggerIntervalMs: 36e5,
2699
+ // 1 hour
2700
+ triggerEventCount: 100,
2701
+ triggerIdleMs: 18e5,
2702
+ // 30 minutes
2703
+ useLLMSummarization: false
2704
+ },
2705
+ continuity: {
2706
+ minScoreForSeamless: 0.7,
2707
+ topicDecayHours: 48
2708
+ }
2709
+ };
2710
+ }
2711
+ /**
2712
+ * Initialize Endless Mode components
2713
+ */
2714
+ async initializeEndlessMode() {
2715
+ const config = await this.getEndlessConfig();
2716
+ this.workingSetStore = createWorkingSetStore(this.eventStore, config);
2717
+ this.consolidatedStore = createConsolidatedStore(this.eventStore);
2718
+ this.consolidationWorker = createConsolidationWorker(
2719
+ this.workingSetStore,
2720
+ this.consolidatedStore,
2721
+ config
2722
+ );
2723
+ this.continuityManager = createContinuityManager(this.eventStore, config);
2724
+ this.consolidationWorker.start();
2725
+ }
2726
+ /**
2727
+ * Get Endless Mode configuration
2728
+ */
2729
+ async getEndlessConfig() {
2730
+ const savedConfig = await this.eventStore.getEndlessConfig("config");
2731
+ return savedConfig || this.getDefaultEndlessConfig();
2732
+ }
2733
+ /**
2734
+ * Set Endless Mode configuration
2735
+ */
2736
+ async setEndlessConfig(config) {
2737
+ const current = await this.getEndlessConfig();
2738
+ const merged = { ...current, ...config };
2739
+ await this.eventStore.setEndlessConfig("config", merged);
2740
+ }
2741
+ /**
2742
+ * Set memory mode (session or endless)
2743
+ */
2744
+ async setMode(mode) {
2745
+ await this.initialize();
2746
+ if (mode === this.endlessMode)
2747
+ return;
2748
+ this.endlessMode = mode;
2749
+ await this.eventStore.setEndlessConfig("mode", mode);
2750
+ if (mode === "endless") {
2751
+ await this.initializeEndlessMode();
2752
+ } else {
2753
+ if (this.consolidationWorker) {
2754
+ this.consolidationWorker.stop();
2755
+ this.consolidationWorker = null;
2756
+ }
2757
+ this.workingSetStore = null;
2758
+ this.consolidatedStore = null;
2759
+ this.continuityManager = null;
2760
+ }
2761
+ }
2762
+ /**
2763
+ * Get current memory mode
2764
+ */
2765
+ getMode() {
2766
+ return this.endlessMode;
2767
+ }
2768
+ /**
2769
+ * Check if endless mode is active
2770
+ */
2771
+ isEndlessModeActive() {
2772
+ return this.endlessMode === "endless";
2773
+ }
2774
+ /**
2775
+ * Add event to Working Set (Endless Mode)
2776
+ */
2777
+ async addToWorkingSet(eventId, relevanceScore) {
2778
+ if (!this.workingSetStore)
2779
+ return;
2780
+ await this.workingSetStore.add(eventId, relevanceScore);
2781
+ }
2782
+ /**
2783
+ * Get the current Working Set
2784
+ */
2785
+ async getWorkingSet() {
2786
+ if (!this.workingSetStore)
2787
+ return null;
2788
+ return this.workingSetStore.get();
2789
+ }
2790
+ /**
2791
+ * Search consolidated memories
2792
+ */
2793
+ async searchConsolidated(query, options) {
2794
+ if (!this.consolidatedStore)
2795
+ return [];
2796
+ return this.consolidatedStore.search(query, options);
2797
+ }
2798
+ /**
2799
+ * Get all consolidated memories
2800
+ */
2801
+ async getConsolidatedMemories(limit) {
2802
+ if (!this.consolidatedStore)
2803
+ return [];
2804
+ return this.consolidatedStore.getAll({ limit });
2805
+ }
2806
+ /**
2807
+ * Calculate continuity score for current context
2808
+ */
2809
+ async calculateContinuity(content, metadata) {
2810
+ if (!this.continuityManager)
2811
+ return null;
2812
+ const snapshot = this.continuityManager.createSnapshot(
2813
+ crypto2.randomUUID(),
2814
+ content,
2815
+ metadata
2816
+ );
2817
+ return this.continuityManager.calculateScore(snapshot);
2818
+ }
2819
+ /**
2820
+ * Record activity (for consolidation idle trigger)
2821
+ */
2822
+ recordActivity() {
2823
+ if (this.consolidationWorker) {
2824
+ this.consolidationWorker.recordActivity();
2825
+ }
2826
+ }
2827
+ /**
2828
+ * Force a consolidation run
2829
+ */
2830
+ async forceConsolidation() {
2831
+ if (!this.consolidationWorker)
2832
+ return 0;
2833
+ return this.consolidationWorker.forceRun();
2834
+ }
2835
+ /**
2836
+ * Get Endless Mode status
2837
+ */
2838
+ async getEndlessModeStatus() {
2839
+ await this.initialize();
2840
+ let workingSetSize = 0;
2841
+ let continuityScore = 0.5;
2842
+ let consolidatedCount = 0;
2843
+ let lastConsolidation = null;
2844
+ if (this.workingSetStore) {
2845
+ workingSetSize = await this.workingSetStore.count();
2846
+ const workingSet = await this.workingSetStore.get();
2847
+ continuityScore = workingSet.continuityScore;
2848
+ }
2849
+ if (this.consolidatedStore) {
2850
+ consolidatedCount = await this.consolidatedStore.count();
2851
+ lastConsolidation = await this.consolidatedStore.getLastConsolidationTime();
2852
+ }
2853
+ return {
2854
+ mode: this.endlessMode,
2855
+ workingSetSize,
2856
+ continuityScore,
2857
+ consolidatedCount,
2858
+ lastConsolidation
2859
+ };
2860
+ }
2861
+ /**
2862
+ * Format Endless Mode context for Claude
2863
+ */
2864
+ async formatEndlessContext(query) {
2865
+ if (!this.isEndlessModeActive()) {
2866
+ return "";
2867
+ }
2868
+ const workingSet = await this.getWorkingSet();
2869
+ const consolidated = await this.searchConsolidated(query, { topK: 3 });
2870
+ const continuity = await this.calculateContinuity(query);
2871
+ const parts = [];
2872
+ if (continuity) {
2873
+ const statusEmoji = continuity.transitionType === "seamless" ? "\u{1F517}" : continuity.transitionType === "topic_shift" ? "\u21AA\uFE0F" : "\u{1F195}";
2874
+ parts.push(`${statusEmoji} Context: ${continuity.transitionType} (score: ${continuity.score.toFixed(2)})`);
2875
+ }
2876
+ if (workingSet && workingSet.recentEvents.length > 0) {
2877
+ parts.push("\n## Recent Context (Working Set)");
2878
+ const recent = workingSet.recentEvents.slice(0, 5);
2879
+ for (const event of recent) {
2880
+ const preview = event.content.slice(0, 80) + (event.content.length > 80 ? "..." : "");
2881
+ const time = event.timestamp.toLocaleTimeString();
2882
+ parts.push(`- ${time} [${event.eventType}] ${preview}`);
2883
+ }
2884
+ }
2885
+ if (consolidated.length > 0) {
2886
+ parts.push("\n## Related Knowledge (Consolidated)");
2887
+ for (const memory of consolidated) {
2888
+ parts.push(`- ${memory.topics.slice(0, 3).join(", ")}: ${memory.summary.slice(0, 100)}...`);
2889
+ }
2890
+ }
2891
+ return parts.join("\n");
2892
+ }
2893
+ /**
2894
+ * Shutdown service
2895
+ */
2896
+ async shutdown() {
2897
+ if (this.consolidationWorker) {
2898
+ this.consolidationWorker.stop();
2899
+ }
2900
+ if (this.vectorWorker) {
2901
+ this.vectorWorker.stop();
2902
+ }
2903
+ await this.eventStore.close();
2904
+ }
2905
+ /**
2906
+ * Expand ~ to home directory
2907
+ */
2908
+ expandPath(p) {
2909
+ if (p.startsWith("~")) {
2910
+ return path.join(os.homedir(), p.slice(1));
2911
+ }
2912
+ return p;
2913
+ }
2914
+ };
2915
+ var defaultService = null;
2916
+ function getDefaultMemoryService() {
2917
+ if (!defaultService) {
2918
+ defaultService = new MemoryService({
2919
+ storagePath: "~/.claude-code/memory"
2920
+ });
2921
+ }
2922
+ return defaultService;
2923
+ }
2924
+
2925
+ // src/hooks/user-prompt-submit.ts
2926
+ async function main() {
2927
+ const inputData = await readStdin();
2928
+ const input = JSON.parse(inputData);
2929
+ const memoryService = getDefaultMemoryService();
2930
+ try {
2931
+ const retrievalResult = await memoryService.retrieveMemories(input.prompt, {
2932
+ topK: 5,
2933
+ minScore: 0.7
2934
+ });
2935
+ await memoryService.storeUserPrompt(
2936
+ input.session_id,
2937
+ input.prompt
2938
+ );
2939
+ const context = memoryService.formatAsContext(retrievalResult);
2940
+ const output = { context };
2941
+ console.log(JSON.stringify(output));
2942
+ } catch (error) {
2943
+ console.error("Memory hook error:", error);
2944
+ console.log(JSON.stringify({ context: "" }));
2945
+ }
2946
+ }
2947
+ function readStdin() {
2948
+ return new Promise((resolve) => {
2949
+ let data = "";
2950
+ process.stdin.setEncoding("utf8");
2951
+ process.stdin.on("data", (chunk) => {
2952
+ data += chunk;
2953
+ });
2954
+ process.stdin.on("end", () => {
2955
+ resolve(data);
2956
+ });
2957
+ });
2958
+ }
2959
+ main().catch(console.error);
2960
+ //# sourceMappingURL=user-prompt-submit.js.map