@memextend/claude-code 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1054 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") {
10
+ for (let key of __getOwnPropNames(from))
11
+ if (!__hasOwnProp.call(to, key) && key !== except)
12
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
13
+ }
14
+ return to;
15
+ };
16
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
17
+ // If the importer is in node compatibility mode or this is not an ESM
18
+ // file that has been converted to a CommonJS file using a Babel-
19
+ // compatible transform (i.e. "__esModule" has not been set), then set
20
+ // "default" to the CommonJS "module.exports" for node compatibility.
21
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
22
+ mod
23
+ ));
24
+
25
+ // src/hooks/session-start.ts
26
+ var import_crypto = require("crypto");
27
+ var import_fs3 = require("fs");
28
+ var import_promises3 = require("fs/promises");
29
+ var import_path3 = require("path");
30
+ var import_os2 = require("os");
31
+ var import_child_process = require("child_process");
32
+
33
+ // ../../core/dist/storage/sqlite.js
34
+ var import_better_sqlite3 = __toESM(require("better-sqlite3"), 1);
35
+ var SQLiteStorage = class {
36
+ db;
37
+ constructor(dbPath) {
38
+ this.db = new import_better_sqlite3.default(dbPath);
39
+ this.db.pragma("journal_mode = WAL");
40
+ this.initialize();
41
+ }
42
+ initialize() {
43
+ this.db.exec(`
44
+ CREATE TABLE IF NOT EXISTS memories (
45
+ id TEXT PRIMARY KEY,
46
+ project_id TEXT,
47
+ content TEXT NOT NULL,
48
+ type TEXT NOT NULL,
49
+ source_tool TEXT,
50
+ created_at TEXT NOT NULL,
51
+ session_id TEXT,
52
+ metadata TEXT
53
+ );
54
+
55
+ CREATE TABLE IF NOT EXISTS projects (
56
+ id TEXT PRIMARY KEY,
57
+ name TEXT NOT NULL,
58
+ path TEXT NOT NULL,
59
+ created_at TEXT NOT NULL
60
+ );
61
+
62
+ CREATE TABLE IF NOT EXISTS global_profile (
63
+ id TEXT PRIMARY KEY,
64
+ key TEXT NOT NULL,
65
+ content TEXT NOT NULL,
66
+ created_at TEXT NOT NULL
67
+ );
68
+
69
+ CREATE VIRTUAL TABLE IF NOT EXISTS memories_fts USING fts5(
70
+ content,
71
+ content='memories',
72
+ content_rowid='rowid'
73
+ );
74
+
75
+ CREATE TRIGGER IF NOT EXISTS memories_ai AFTER INSERT ON memories BEGIN
76
+ INSERT INTO memories_fts(rowid, content) VALUES (NEW.rowid, NEW.content);
77
+ END;
78
+
79
+ CREATE TRIGGER IF NOT EXISTS memories_ad AFTER DELETE ON memories BEGIN
80
+ INSERT INTO memories_fts(memories_fts, rowid, content) VALUES('delete', OLD.rowid, OLD.content);
81
+ END;
82
+
83
+ CREATE TRIGGER IF NOT EXISTS memories_au AFTER UPDATE ON memories BEGIN
84
+ INSERT INTO memories_fts(memories_fts, rowid, content) VALUES('delete', OLD.rowid, OLD.content);
85
+ INSERT INTO memories_fts(rowid, content) VALUES (NEW.rowid, NEW.content);
86
+ END;
87
+ `);
88
+ }
89
+ getTables() {
90
+ const rows = this.db.prepare(`
91
+ SELECT name FROM sqlite_master
92
+ WHERE type='table' OR type='virtual table'
93
+ `).all();
94
+ return rows.map((r) => r.name);
95
+ }
96
+ insertMemory(memory) {
97
+ const stmt = this.db.prepare(`
98
+ INSERT INTO memories (id, project_id, content, type, source_tool, created_at, session_id, metadata)
99
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?)
100
+ `);
101
+ stmt.run(memory.id, memory.projectId, memory.content, memory.type, memory.sourceTool, memory.createdAt, memory.sessionId, memory.metadata ? JSON.stringify(memory.metadata) : null);
102
+ }
103
+ getMemory(id) {
104
+ const row = this.db.prepare("SELECT * FROM memories WHERE id = ?").get(id);
105
+ if (!row)
106
+ return null;
107
+ return this.rowToMemory(row);
108
+ }
109
+ getAllMemories(projectId, limit = 100) {
110
+ let query = "SELECT * FROM memories";
111
+ const params = [];
112
+ if (projectId) {
113
+ query += " WHERE project_id = ?";
114
+ params.push(projectId);
115
+ }
116
+ query += " ORDER BY created_at DESC LIMIT ?";
117
+ params.push(limit);
118
+ const rows = this.db.prepare(query).all(...params);
119
+ return rows.map((row) => this.rowToMemory(row));
120
+ }
121
+ searchFTS(query, limit = 10) {
122
+ const rows = this.db.prepare(`
123
+ SELECT m.*, bm25(memories_fts) as score
124
+ FROM memories_fts fts
125
+ JOIN memories m ON m.rowid = fts.rowid
126
+ WHERE memories_fts MATCH ?
127
+ ORDER BY score
128
+ LIMIT ?
129
+ `).all(query, limit);
130
+ return rows.map((row) => ({
131
+ memory: this.rowToMemory(row),
132
+ score: Math.abs(row.score),
133
+ source: "fts"
134
+ }));
135
+ }
136
+ getRecentMemories(projectId, limit = 0, days = 0) {
137
+ let query = "SELECT * FROM memories";
138
+ const params = [];
139
+ const conditions = [];
140
+ if (days > 0) {
141
+ const cutoff = /* @__PURE__ */ new Date();
142
+ cutoff.setDate(cutoff.getDate() - days);
143
+ conditions.push("created_at > ?");
144
+ params.push(cutoff.toISOString());
145
+ }
146
+ if (projectId !== null) {
147
+ conditions.push("project_id = ?");
148
+ params.push(projectId);
149
+ }
150
+ if (conditions.length > 0) {
151
+ query += " WHERE " + conditions.join(" AND ");
152
+ }
153
+ query += " ORDER BY created_at DESC";
154
+ if (limit > 0) {
155
+ query += " LIMIT ?";
156
+ params.push(limit);
157
+ }
158
+ const rows = this.db.prepare(query).all(...params);
159
+ return rows.map((row) => this.rowToMemory(row));
160
+ }
161
+ deleteMemory(id) {
162
+ const result = this.db.prepare("DELETE FROM memories WHERE id = ?").run(id);
163
+ return result.changes > 0;
164
+ }
165
+ updateMemory(id, content) {
166
+ const result = this.db.prepare("UPDATE memories SET content = ? WHERE id = ?").run(content, id);
167
+ return result.changes > 0;
168
+ }
169
+ deleteAllMemories(projectId) {
170
+ if (projectId) {
171
+ const result = this.db.prepare("DELETE FROM memories WHERE project_id = ?").run(projectId);
172
+ return result.changes;
173
+ } else {
174
+ const result = this.db.prepare("DELETE FROM memories").run();
175
+ return result.changes;
176
+ }
177
+ }
178
+ deleteMemoriesBefore(date, projectId) {
179
+ const timestamp = date.toISOString();
180
+ if (projectId) {
181
+ const result = this.db.prepare("DELETE FROM memories WHERE created_at < ? AND project_id = ?").run(timestamp, projectId);
182
+ return result.changes;
183
+ } else {
184
+ const result = this.db.prepare("DELETE FROM memories WHERE created_at < ?").run(timestamp);
185
+ return result.changes;
186
+ }
187
+ }
188
+ // Project methods
189
+ insertProject(project) {
190
+ const stmt = this.db.prepare(`
191
+ INSERT OR REPLACE INTO projects (id, name, path, created_at)
192
+ VALUES (?, ?, ?, ?)
193
+ `);
194
+ stmt.run(project.id, project.name, project.path, project.createdAt);
195
+ }
196
+ getProject(id) {
197
+ const row = this.db.prepare("SELECT * FROM projects WHERE id = ?").get(id);
198
+ if (!row)
199
+ return null;
200
+ return {
201
+ id: row.id,
202
+ name: row.name,
203
+ path: row.path,
204
+ createdAt: row.created_at
205
+ };
206
+ }
207
+ /**
208
+ * Find a project by name (case-insensitive).
209
+ * Returns the first matching project, preferring projects with a path set.
210
+ */
211
+ getProjectByName(name) {
212
+ const rowWithPath = this.db.prepare("SELECT * FROM projects WHERE LOWER(name) = LOWER(?) AND path != '' ORDER BY created_at ASC LIMIT 1").get(name);
213
+ if (rowWithPath) {
214
+ return {
215
+ id: rowWithPath.id,
216
+ name: rowWithPath.name,
217
+ path: rowWithPath.path,
218
+ createdAt: rowWithPath.created_at
219
+ };
220
+ }
221
+ const row = this.db.prepare("SELECT * FROM projects WHERE LOWER(name) = LOWER(?) ORDER BY created_at ASC LIMIT 1").get(name);
222
+ if (!row)
223
+ return null;
224
+ return {
225
+ id: row.id,
226
+ name: row.name,
227
+ path: row.path,
228
+ createdAt: row.created_at
229
+ };
230
+ }
231
+ /**
232
+ * Delete a project and all its memories.
233
+ * @returns Object with counts of deleted project and memories
234
+ */
235
+ deleteProject(projectId) {
236
+ const memoriesResult = this.db.prepare("DELETE FROM memories WHERE project_id = ?").run(projectId);
237
+ const projectResult = this.db.prepare("DELETE FROM projects WHERE id = ?").run(projectId);
238
+ return {
239
+ projectDeleted: projectResult.changes > 0,
240
+ memoriesDeleted: memoriesResult.changes
241
+ };
242
+ }
243
+ /**
244
+ * Get all projects.
245
+ */
246
+ getAllProjects() {
247
+ const rows = this.db.prepare("SELECT * FROM projects ORDER BY name ASC").all();
248
+ return rows.map((row) => ({
249
+ id: row.id,
250
+ name: row.name,
251
+ path: row.path,
252
+ createdAt: row.created_at
253
+ }));
254
+ }
255
+ // Global profile methods
256
+ insertGlobalProfile(profile) {
257
+ const stmt = this.db.prepare(`
258
+ INSERT INTO global_profile (id, key, content, created_at)
259
+ VALUES (?, ?, ?, ?)
260
+ `);
261
+ stmt.run(profile.id, profile.key, profile.content, profile.createdAt);
262
+ }
263
+ getGlobalProfiles(limit = 10) {
264
+ const rows = this.db.prepare(`
265
+ SELECT * FROM global_profile ORDER BY created_at DESC LIMIT ?
266
+ `).all(limit);
267
+ return rows.map((row) => ({
268
+ id: row.id,
269
+ key: row.key,
270
+ content: row.content,
271
+ createdAt: row.created_at
272
+ }));
273
+ }
274
+ /**
275
+ * Delete a specific global profile by ID.
276
+ */
277
+ deleteGlobalProfile(id) {
278
+ const result = this.db.prepare("DELETE FROM global_profile WHERE id = ?").run(id);
279
+ return result.changes > 0;
280
+ }
281
+ /**
282
+ * Delete all global profiles.
283
+ * @returns Number of profiles deleted
284
+ */
285
+ deleteAllGlobalProfiles() {
286
+ const result = this.db.prepare("DELETE FROM global_profile").run();
287
+ return result.changes;
288
+ }
289
+ getMemoryCount() {
290
+ const row = this.db.prepare("SELECT COUNT(*) as count FROM memories").get();
291
+ return row.count;
292
+ }
293
+ getMemoryCountByProject(projectId) {
294
+ const row = this.db.prepare("SELECT COUNT(*) as count FROM memories WHERE project_id = ?").get(projectId);
295
+ return row.count;
296
+ }
297
+ /**
298
+ * Get IDs of oldest memories for a project, exceeding the limit
299
+ * @returns Array of memory IDs to delete (oldest first)
300
+ */
301
+ getOldestMemoryIds(projectId, limit) {
302
+ let query;
303
+ let params;
304
+ if (projectId) {
305
+ query = `
306
+ SELECT id FROM memories
307
+ WHERE project_id = ?
308
+ ORDER BY created_at ASC
309
+ LIMIT ?
310
+ `;
311
+ params = [projectId, limit];
312
+ } else {
313
+ query = `
314
+ SELECT id FROM memories
315
+ ORDER BY created_at ASC
316
+ LIMIT ?
317
+ `;
318
+ params = [limit];
319
+ }
320
+ const rows = this.db.prepare(query).all(...params);
321
+ return rows.map((row) => row.id);
322
+ }
323
+ /**
324
+ * Prune memories to stay within limits
325
+ * @returns Number of memories deleted
326
+ */
327
+ pruneMemories(options) {
328
+ const deletedIds = [];
329
+ if (options.maxPerProject && options.maxPerProject > 0 && options.projectId) {
330
+ const count = this.getMemoryCountByProject(options.projectId);
331
+ if (count > options.maxPerProject) {
332
+ const excess = count - options.maxPerProject;
333
+ const idsToDelete = this.getOldestMemoryIds(options.projectId, excess);
334
+ for (const id of idsToDelete) {
335
+ this.deleteMemory(id);
336
+ deletedIds.push(id);
337
+ }
338
+ }
339
+ }
340
+ if (options.maxTotal && options.maxTotal > 0) {
341
+ const count = this.getMemoryCount();
342
+ if (count > options.maxTotal) {
343
+ const excess = count - options.maxTotal;
344
+ const idsToDelete = this.getOldestMemoryIds(null, excess);
345
+ for (const id of idsToDelete) {
346
+ if (!deletedIds.includes(id)) {
347
+ this.deleteMemory(id);
348
+ deletedIds.push(id);
349
+ }
350
+ }
351
+ }
352
+ }
353
+ return { deleted: deletedIds.length, deletedIds };
354
+ }
355
+ rowToMemory(row) {
356
+ return {
357
+ id: row.id,
358
+ projectId: row.project_id,
359
+ content: row.content,
360
+ type: row.type,
361
+ sourceTool: row.source_tool,
362
+ createdAt: row.created_at,
363
+ sessionId: row.session_id,
364
+ metadata: row.metadata ? JSON.parse(row.metadata) : null
365
+ };
366
+ }
367
+ close() {
368
+ this.db.close();
369
+ }
370
+ };
371
+
372
+ // ../../core/dist/storage/lancedb.js
373
+ var lancedb = __toESM(require("@lancedb/lancedb"), 1);
374
+ var LanceDBStorage = class _LanceDBStorage {
375
+ db;
376
+ table = null;
377
+ tableName = "memories";
378
+ dimensions = 384;
379
+ constructor(db) {
380
+ this.db = db;
381
+ }
382
+ static async create(dbPath) {
383
+ const db = await lancedb.connect(dbPath);
384
+ const storage = new _LanceDBStorage(db);
385
+ await storage.initialize();
386
+ return storage;
387
+ }
388
+ async initialize() {
389
+ const tableNames = await this.db.tableNames();
390
+ if (tableNames.includes(this.tableName)) {
391
+ this.table = await this.db.openTable(this.tableName);
392
+ }
393
+ }
394
+ async insertVector(id, vector) {
395
+ if (vector.length !== this.dimensions) {
396
+ throw new Error(`Vector must have ${this.dimensions} dimensions, got ${vector.length}`);
397
+ }
398
+ const data = [{ id, vector }];
399
+ if (!this.table) {
400
+ this.table = await this.db.createTable(this.tableName, data);
401
+ } else {
402
+ await this.table.add(data);
403
+ }
404
+ }
405
+ async insertVectors(items) {
406
+ for (const item of items) {
407
+ if (item.vector.length !== this.dimensions) {
408
+ throw new Error(`Vector must have ${this.dimensions} dimensions, got ${item.vector.length}`);
409
+ }
410
+ }
411
+ if (!this.table) {
412
+ this.table = await this.db.createTable(this.tableName, items);
413
+ } else {
414
+ await this.table.add(items);
415
+ }
416
+ }
417
+ async search(vector, limit = 10) {
418
+ if (!this.table) {
419
+ return [];
420
+ }
421
+ const effectiveLimit = limit > 0 ? limit : 100;
422
+ const results = await this.table.vectorSearch(vector).limit(effectiveLimit).toArray();
423
+ return results.map((row) => ({
424
+ id: row.id,
425
+ score: 1 - row._distance
426
+ // Convert distance to similarity
427
+ }));
428
+ }
429
+ async deleteVector(id) {
430
+ if (!this.table)
431
+ return;
432
+ const sanitizedId = id.replace(/'/g, "''");
433
+ await this.table.delete(`id = '${sanitizedId}'`);
434
+ }
435
+ async getVectorCount() {
436
+ if (!this.table)
437
+ return 0;
438
+ return await this.table.countRows();
439
+ }
440
+ async getVectorsByIds(ids) {
441
+ const result = /* @__PURE__ */ new Map();
442
+ if (!this.table || ids.length === 0)
443
+ return result;
444
+ const BATCH_SIZE = 100;
445
+ try {
446
+ for (let i = 0; i < ids.length; i += BATCH_SIZE) {
447
+ const batch = ids.slice(i, i + BATCH_SIZE);
448
+ const sanitizedIds = batch.map((id) => id.replace(/'/g, "''"));
449
+ const filter = sanitizedIds.map((id) => `id = '${id}'`).join(" OR ");
450
+ const rows = await this.table.query().where(filter).toArray();
451
+ for (const row of rows) {
452
+ result.set(row.id, row.vector);
453
+ }
454
+ }
455
+ } catch {
456
+ }
457
+ return result;
458
+ }
459
+ async close() {
460
+ }
461
+ };
462
+
463
+ // ../../core/dist/embedding/local.js
464
+ var import_path = require("path");
465
+ var import_fs = require("fs");
466
+ var import_promises = require("fs/promises");
467
+ var import_promises2 = require("stream/promises");
468
+ var MODEL_URL = "https://huggingface.co/nomic-ai/nomic-embed-text-v1.5-GGUF/resolve/main/nomic-embed-text-v1.5.Q8_0.gguf";
469
+ var MODEL_FILENAME = "nomic-embed-text-v1.5.Q8_0.gguf";
470
+ var LocalEmbedding = class _LocalEmbedding {
471
+ // Using any to avoid ESM/CJS type issues with dynamic imports
472
+ model;
473
+ context;
474
+ dimensions;
475
+ constructor(model, context, dimensions) {
476
+ this.model = model;
477
+ this.context = context;
478
+ this.dimensions = dimensions;
479
+ }
480
+ static async create(modelsDir) {
481
+ const modelPath = (0, import_path.join)(modelsDir, MODEL_FILENAME);
482
+ if (!(0, import_fs.existsSync)(modelPath)) {
483
+ await _LocalEmbedding.downloadModel(modelsDir, modelPath);
484
+ }
485
+ const { getLlama, LlamaLogLevel } = await import("node-llama-cpp");
486
+ const llama = await getLlama({
487
+ logLevel: LlamaLogLevel.error
488
+ // Only show errors, not warnings
489
+ });
490
+ const model = await llama.loadModel({ modelPath });
491
+ const context = await model.createEmbeddingContext();
492
+ return new _LocalEmbedding(model, context, 384);
493
+ }
494
+ static async downloadModel(modelsDir, modelPath) {
495
+ await (0, import_promises.mkdir)(modelsDir, { recursive: true });
496
+ console.log(`Downloading embedding model to ${modelPath}...`);
497
+ console.log("This is a one-time download (~274MB)");
498
+ const response = await fetch(MODEL_URL);
499
+ if (!response.ok || !response.body) {
500
+ throw new Error(`Failed to download model: ${response.statusText}`);
501
+ }
502
+ const fileStream = (0, import_fs.createWriteStream)(modelPath);
503
+ await (0, import_promises2.pipeline)(response.body, fileStream);
504
+ console.log("Model downloaded successfully");
505
+ }
506
+ /**
507
+ * Generate embedding for a document/memory
508
+ */
509
+ async embed(text) {
510
+ const prefixedText = `search_document: ${text}`;
511
+ const embedding = await this.context.getEmbeddingFor(prefixedText);
512
+ const vector = Array.from(embedding.vector).slice(0, this.dimensions);
513
+ return this.normalize(vector);
514
+ }
515
+ /**
516
+ * Generate embedding for a search query
517
+ */
518
+ async embedQuery(text) {
519
+ const prefixedText = `search_query: ${text}`;
520
+ const embedding = await this.context.getEmbeddingFor(prefixedText);
521
+ const vector = Array.from(embedding.vector).slice(0, this.dimensions);
522
+ return this.normalize(vector);
523
+ }
524
+ /**
525
+ * Batch embed multiple texts (documents)
526
+ */
527
+ async embedBatch(texts) {
528
+ const results = [];
529
+ for (const text of texts) {
530
+ results.push(await this.embed(text));
531
+ }
532
+ return results;
533
+ }
534
+ /**
535
+ * Get the embedding dimensions
536
+ */
537
+ getDimensions() {
538
+ return this.dimensions;
539
+ }
540
+ /**
541
+ * Normalize vector to unit length (for cosine similarity)
542
+ */
543
+ normalize(vector) {
544
+ const norm = Math.sqrt(vector.reduce((sum, v) => sum + v * v, 0));
545
+ if (norm === 0)
546
+ return vector;
547
+ return vector.map((v) => v / norm);
548
+ }
549
+ async close() {
550
+ await this.context.dispose();
551
+ }
552
+ };
553
+ function cosineSimilarity(a, b) {
554
+ if (a.length !== b.length) {
555
+ throw new Error(`Vector dimensions must match: ${a.length} vs ${b.length}`);
556
+ }
557
+ let dot = 0;
558
+ for (let i = 0; i < a.length; i++) {
559
+ dot += a[i] * b[i];
560
+ }
561
+ return dot;
562
+ }
563
+ function isModelAvailable(modelsDir) {
564
+ const modelPath = (0, import_path.join)(modelsDir, MODEL_FILENAME);
565
+ return (0, import_fs.existsSync)(modelPath);
566
+ }
567
+ async function createEmbedFunction(modelsDir) {
568
+ if (isModelAvailable(modelsDir)) {
569
+ try {
570
+ const embedding = await LocalEmbedding.create(modelsDir);
571
+ return {
572
+ embed: (text) => embedding.embed(text),
573
+ embedQuery: (text) => embedding.embedQuery(text),
574
+ isReal: true,
575
+ close: () => embedding.close()
576
+ };
577
+ } catch (error) {
578
+ console.error("[memextend] Failed to load embedding model, using fallback:", error);
579
+ }
580
+ }
581
+ const { createHash: createHash2 } = await import("crypto");
582
+ const hashEmbed = (text, prefix) => {
583
+ const hash = createHash2("sha256").update(prefix + text).digest();
584
+ const vector = Array.from(hash).slice(0, 32);
585
+ const expanded = [];
586
+ for (let i = 0; i < 12; i++) {
587
+ const roundHash = createHash2("sha256").update(text + i.toString()).digest();
588
+ expanded.push(...Array.from(roundHash).slice(0, 32));
589
+ }
590
+ const norm = Math.sqrt(expanded.reduce((sum, v) => sum + v * v, 0));
591
+ return expanded.map((v) => v / norm / 255);
592
+ };
593
+ return {
594
+ embed: async (text) => hashEmbed(text, "doc:"),
595
+ embedQuery: async (text) => hashEmbed(text, "query:"),
596
+ isReal: false,
597
+ close: async () => {
598
+ }
599
+ };
600
+ }
601
+
602
+ // ../../core/dist/memory/retrieve.js
603
+ var MemoryRetriever = class {
604
+ sqlite;
605
+ lancedb;
606
+ embed;
607
+ options;
608
+ constructor(sqlite, lancedb2, embed, options = {}) {
609
+ this.sqlite = sqlite;
610
+ this.lancedb = lancedb2;
611
+ this.embed = embed;
612
+ this.options = {
613
+ defaultLimit: options.defaultLimit ?? 0,
614
+ // 0 = unlimited
615
+ defaultRecentDays: options.defaultRecentDays ?? 0,
616
+ // 0 = unlimited
617
+ rrfK: options.rrfK ?? 60
618
+ };
619
+ }
620
+ /**
621
+ * Full-text search using FTS5
622
+ */
623
+ async search(query, options = {}) {
624
+ const limit = options.limit ?? this.options.defaultLimit;
625
+ return this.sqlite.searchFTS(query, limit);
626
+ }
627
+ /**
628
+ * Vector similarity search using LanceDB
629
+ */
630
+ async vectorSearch(query, options = {}) {
631
+ const limit = options.limit ?? this.options.defaultLimit;
632
+ const queryVector = await this.embed(query);
633
+ const vectorResults = await this.lancedb.search(queryVector, limit * 2);
634
+ const results = [];
635
+ for (const vr of vectorResults) {
636
+ const memory = this.sqlite.getMemory(vr.id);
637
+ if (memory) {
638
+ if (options.projectId && memory.projectId !== options.projectId) {
639
+ continue;
640
+ }
641
+ results.push({
642
+ memory,
643
+ score: vr.score,
644
+ source: "vector"
645
+ });
646
+ }
647
+ }
648
+ return results.slice(0, limit);
649
+ }
650
+ /**
651
+ * Hybrid search combining FTS and vector search with RRF fusion
652
+ */
653
+ async hybridSearch(query, options = {}) {
654
+ const limit = options.limit ?? this.options.defaultLimit;
655
+ const k = this.options.rrfK;
656
+ const [ftsResults, vectorResults] = await Promise.all([
657
+ this.search(query, { ...options, limit: limit * 2 }),
658
+ this.vectorSearch(query, { ...options, limit: limit * 2 })
659
+ ]);
660
+ const scores = /* @__PURE__ */ new Map();
661
+ const memories = /* @__PURE__ */ new Map();
662
+ ftsResults.forEach((result, rank) => {
663
+ const rrfScore = 1 / (k + rank + 1);
664
+ scores.set(result.memory.id, (scores.get(result.memory.id) ?? 0) + rrfScore);
665
+ memories.set(result.memory.id, result.memory);
666
+ });
667
+ vectorResults.forEach((result, rank) => {
668
+ const rrfScore = 1 / (k + rank + 1);
669
+ scores.set(result.memory.id, (scores.get(result.memory.id) ?? 0) + rrfScore);
670
+ memories.set(result.memory.id, result.memory);
671
+ });
672
+ const combined = Array.from(scores.entries()).sort((a, b) => b[1] - a[1]).slice(0, limit).map(([id, score]) => ({
673
+ memory: memories.get(id),
674
+ score,
675
+ source: "hybrid"
676
+ }));
677
+ return combined;
678
+ }
679
+ /**
680
+ * Get recent memories for a project
681
+ */
682
+ getRecent(projectId, options = {}) {
683
+ const limit = options.limit ?? this.options.defaultLimit;
684
+ const days = options.recentDays ?? this.options.defaultRecentDays;
685
+ return this.sqlite.getRecentMemories(projectId, limit, days);
686
+ }
687
+ /**
688
+ * Get context for a new session - combines recent memories and global profile
689
+ */
690
+ async getContextForSession(projectId, options = {}) {
691
+ const limit = options.limit ?? this.options.defaultLimit;
692
+ const includeGlobal = options.includeGlobal ?? true;
693
+ const recentMemories = this.getRecent(projectId, {
694
+ limit: Math.floor(limit / 2),
695
+ recentDays: options.recentDays
696
+ });
697
+ const globalProfile = includeGlobal ? this.sqlite.getGlobalProfiles(5) : [];
698
+ const project = this.sqlite.getProject(projectId);
699
+ const projectName = project?.name ?? "project";
700
+ const relevantMemories = await this.vectorSearch(projectName, {
701
+ projectId,
702
+ limit: Math.floor(limit / 2)
703
+ });
704
+ return {
705
+ recentMemories,
706
+ globalProfile,
707
+ relevantMemories
708
+ };
709
+ }
710
+ };
711
+ function formatContextForInjection(context, options = {}) {
712
+ const maxChars = options.maxChars ?? 0;
713
+ const previewLength = options.previewLength ?? 200;
714
+ const lines = ["<memextend-context>", "## Your Memory for This Project", ""];
715
+ let currentLength = lines.join("\n").length;
716
+ const footer = [
717
+ "Use these memories naturally. Use `memextend_search` to find more details.",
718
+ "Use `memextend_save` to remember important decisions for future sessions.",
719
+ "</memextend-context>"
720
+ ].join("\n");
721
+ const footerLength = footer.length + 1;
722
+ const canAdd = (text) => {
723
+ if (maxChars === 0)
724
+ return true;
725
+ return currentLength + text.length + footerLength <= maxChars;
726
+ };
727
+ if (context.recentMemories.length > 0) {
728
+ const sectionHeader = "### Recent Work\n";
729
+ if (canAdd(sectionHeader)) {
730
+ lines.push("### Recent Work");
731
+ currentLength += sectionHeader.length;
732
+ for (const memory of context.recentMemories) {
733
+ const date = formatRelativeDate(memory.createdAt);
734
+ const preview = memory.content.split("\n")[0].slice(0, previewLength);
735
+ const line = `- [${date}] ${preview}
736
+ `;
737
+ if (!canAdd(line))
738
+ break;
739
+ lines.push(`- [${date}] ${preview}`);
740
+ currentLength += line.length;
741
+ }
742
+ lines.push("");
743
+ currentLength += 1;
744
+ }
745
+ }
746
+ if (context.globalProfile.length > 0) {
747
+ const sectionHeader = "### User Preferences (Global)\n";
748
+ if (canAdd(sectionHeader)) {
749
+ lines.push("### User Preferences (Global)");
750
+ currentLength += sectionHeader.length;
751
+ for (const profile of context.globalProfile) {
752
+ const line = `- ${profile.content}
753
+ `;
754
+ if (!canAdd(line))
755
+ break;
756
+ lines.push(`- ${profile.content}`);
757
+ currentLength += line.length;
758
+ }
759
+ lines.push("");
760
+ currentLength += 1;
761
+ }
762
+ }
763
+ if (context.relevantMemories && context.relevantMemories.length > 0) {
764
+ const sectionHeader = "### Relevant Past Work\n";
765
+ if (canAdd(sectionHeader)) {
766
+ lines.push("### Relevant Past Work");
767
+ currentLength += sectionHeader.length;
768
+ for (const result of context.relevantMemories) {
769
+ const preview = result.memory.content.split("\n")[0].slice(0, previewLength);
770
+ const line = `- ${preview}
771
+ `;
772
+ if (!canAdd(line))
773
+ break;
774
+ lines.push(`- ${preview}`);
775
+ currentLength += line.length;
776
+ }
777
+ lines.push("");
778
+ }
779
+ }
780
+ lines.push("Use these memories naturally. Use `memextend_search` to find more details.");
781
+ lines.push("Use `memextend_save` to remember important decisions for future sessions.");
782
+ lines.push("</memextend-context>");
783
+ return lines.join("\n");
784
+ }
785
+ function formatRelativeDate(isoDate) {
786
+ const date = new Date(isoDate);
787
+ const now = /* @__PURE__ */ new Date();
788
+ const diffMs = now.getTime() - date.getTime();
789
+ const diffDays = Math.floor(diffMs / (1e3 * 60 * 60 * 24));
790
+ if (diffDays === 0)
791
+ return "today";
792
+ if (diffDays === 1)
793
+ return "yesterday";
794
+ if (diffDays < 7)
795
+ return `${diffDays} days ago`;
796
+ if (diffDays < 30)
797
+ return `${Math.floor(diffDays / 7)} weeks ago`;
798
+ return `${Math.floor(diffDays / 30)} months ago`;
799
+ }
800
+
801
+ // ../../core/dist/memory/deduplicate.js
802
+ var DEFAULT_THRESHOLD = 0.85;
803
+ function deduplicateMemories(memories, vectors, options) {
804
+ if (memories.length === 0)
805
+ return [];
806
+ const threshold = options?.similarityThreshold ?? DEFAULT_THRESHOLD;
807
+ const sorted = [...memories].sort((a, b) => {
808
+ return new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime();
809
+ });
810
+ const selected = [];
811
+ const selectedVectors = [];
812
+ for (const memory of sorted) {
813
+ const vector = vectors.get(memory.id);
814
+ if (!vector) {
815
+ selected.push(memory);
816
+ continue;
817
+ }
818
+ let isDuplicate = false;
819
+ for (const selectedVector of selectedVectors) {
820
+ const similarity = cosineSimilarity(vector, selectedVector);
821
+ if (similarity > threshold) {
822
+ isDuplicate = true;
823
+ break;
824
+ }
825
+ }
826
+ if (!isDuplicate) {
827
+ selected.push(memory);
828
+ selectedVectors.push(vector);
829
+ }
830
+ }
831
+ return selected;
832
+ }
833
+ function getDeduplicationStats(originalCount, deduplicatedCount) {
834
+ const removed = originalCount - deduplicatedCount;
835
+ const percentage = originalCount > 0 ? Math.round(removed / originalCount * 100) : 0;
836
+ return { removed, percentage };
837
+ }
838
+
839
+ // src/hooks/logger.ts
840
+ var import_fs2 = require("fs");
841
+ var import_path2 = require("path");
842
+ var import_os = require("os");
843
+ var MEMEXTEND_DIR = (0, import_path2.join)((0, import_os.homedir)(), ".memextend");
844
+ var LOGS_DIR = (0, import_path2.join)(MEMEXTEND_DIR, "logs");
845
+ var CONFIG_PATH = (0, import_path2.join)(MEMEXTEND_DIR, "config.json");
846
+ var debugEnabled = null;
847
+ function isDebugEnabled() {
848
+ if (debugEnabled !== null)
849
+ return debugEnabled;
850
+ try {
851
+ if ((0, import_fs2.existsSync)(CONFIG_PATH)) {
852
+ const config = JSON.parse((0, import_fs2.readFileSync)(CONFIG_PATH, "utf-8"));
853
+ debugEnabled = config.debug === true;
854
+ } else {
855
+ debugEnabled = false;
856
+ }
857
+ } catch {
858
+ debugEnabled = false;
859
+ }
860
+ return debugEnabled;
861
+ }
862
+ function ensureLogsDir() {
863
+ if (!(0, import_fs2.existsSync)(LOGS_DIR)) {
864
+ (0, import_fs2.mkdirSync)(LOGS_DIR, { recursive: true });
865
+ }
866
+ }
867
+ function log(hook, message, data) {
868
+ if (!isDebugEnabled())
869
+ return;
870
+ try {
871
+ ensureLogsDir();
872
+ const timestamp = (/* @__PURE__ */ new Date()).toISOString();
873
+ const logFile = (0, import_path2.join)(LOGS_DIR, "hooks.log");
874
+ const entry = data ? `[${timestamp}] [${hook}] ${message} ${JSON.stringify(data)}
875
+ ` : `[${timestamp}] [${hook}] ${message}
876
+ `;
877
+ (0, import_fs2.appendFileSync)(logFile, entry);
878
+ } catch {
879
+ }
880
+ }
881
+ function logError(hook, error) {
882
+ try {
883
+ ensureLogsDir();
884
+ const timestamp = (/* @__PURE__ */ new Date()).toISOString();
885
+ const logFile = (0, import_path2.join)(LOGS_DIR, "hooks.log");
886
+ const errorMsg = error instanceof Error ? error.message : String(error);
887
+ const entry = `[${timestamp}] [${hook}] ERROR: ${errorMsg}
888
+ `;
889
+ (0, import_fs2.appendFileSync)(logFile, entry);
890
+ } catch {
891
+ }
892
+ }
893
+
894
+ // src/hooks/session-start.ts
895
+ var MEMEXTEND_DIR2 = (0, import_path3.join)((0, import_os2.homedir)(), ".memextend");
896
+ var CONFIG_PATH2 = (0, import_path3.join)(MEMEXTEND_DIR2, "config.json");
897
+ var DB_PATH = (0, import_path3.join)(MEMEXTEND_DIR2, "memextend.db");
898
+ var VECTORS_PATH = (0, import_path3.join)(MEMEXTEND_DIR2, "vectors");
899
+ var MODELS_PATH = (0, import_path3.join)(MEMEXTEND_DIR2, "models");
900
+ async function main() {
901
+ const chunks = [];
902
+ for await (const chunk of process.stdin) {
903
+ chunks.push(chunk);
904
+ }
905
+ const input = JSON.parse(Buffer.concat(chunks).toString());
906
+ log("SessionStart", "Hook fired", {
907
+ source: input.source,
908
+ session_id: input.session_id,
909
+ cwd: input.cwd
910
+ });
911
+ try {
912
+ if (!(0, import_fs3.existsSync)(DB_PATH)) {
913
+ log("SessionStart", "DB not found, skipping");
914
+ outputResult({});
915
+ return;
916
+ }
917
+ const config = await loadConfig();
918
+ if (!config.retrieval?.autoInject) {
919
+ log("SessionStart", "Auto-inject disabled in config");
920
+ outputResult({});
921
+ return;
922
+ }
923
+ const projectId = getProjectId2(input.cwd);
924
+ const sqlite = new SQLiteStorage(DB_PATH);
925
+ const lancedb2 = await LanceDBStorage.create(VECTORS_PATH);
926
+ const embedder = await createEmbedFunction(MODELS_PATH);
927
+ const retriever = new MemoryRetriever(sqlite, lancedb2, embedder.embedQuery, {
928
+ defaultLimit: config.retrieval?.maxMemories ?? 0,
929
+ defaultRecentDays: config.retrieval?.recentDays ?? 0
930
+ });
931
+ const project = sqlite.getProject(projectId);
932
+ if (!project) {
933
+ sqlite.insertProject({
934
+ id: projectId,
935
+ name: (0, import_path3.basename)(input.cwd),
936
+ path: input.cwd,
937
+ createdAt: (/* @__PURE__ */ new Date()).toISOString()
938
+ });
939
+ }
940
+ const isPostCompact = input.source === "compact";
941
+ if (isPostCompact) {
942
+ log("SessionStart", "POST-COMPACT INJECTION - restoring context after compaction");
943
+ }
944
+ const configLimit = config.retrieval?.maxMemories ?? 0;
945
+ const effectiveLimit = configLimit === 0 ? 1e3 : configLimit;
946
+ const maxMemories = isPostCompact ? Math.min(effectiveLimit * 2, configLimit === 0 ? 1e3 : 100) : effectiveLimit;
947
+ log("SessionStart", "Retrieving memories", { maxMemories, isPostCompact, configLimit });
948
+ const context = await retriever.getContextForSession(projectId, {
949
+ includeGlobal: config.retrieval?.includeGlobal ?? true,
950
+ limit: maxMemories,
951
+ recentDays: config.retrieval?.recentDays ?? 0
952
+ });
953
+ const allMemories = [
954
+ ...context.recentMemories,
955
+ ...context.relevantMemories.map((r) => r.memory)
956
+ ];
957
+ const deduplicationThreshold = config.retrieval?.deduplicationThreshold ?? 0.85;
958
+ let deduplicatedMemories = allMemories;
959
+ if (allMemories.length > 1) {
960
+ const memoryIds = allMemories.map((m) => m.id);
961
+ const vectors = await lancedb2.getVectorsByIds(memoryIds);
962
+ if (vectors.size > 0) {
963
+ deduplicatedMemories = deduplicateMemories(allMemories, vectors, {
964
+ similarityThreshold: deduplicationThreshold
965
+ });
966
+ const stats = getDeduplicationStats(allMemories.length, deduplicatedMemories.length);
967
+ if (stats.removed > 0) {
968
+ log("SessionStart", "Deduplication removed similar memories", {
969
+ original: allMemories.length,
970
+ deduplicated: deduplicatedMemories.length,
971
+ removed: stats.removed,
972
+ percentage: stats.percentage
973
+ });
974
+ }
975
+ }
976
+ }
977
+ sqlite.close();
978
+ await lancedb2.close();
979
+ await embedder.close();
980
+ if (deduplicatedMemories.length === 0 && context.globalProfile.length === 0) {
981
+ log("SessionStart", "No memories to inject");
982
+ outputResult({});
983
+ return;
984
+ }
985
+ const recentIds = new Set(context.recentMemories.map((m) => m.id));
986
+ const deduplicatedContext = {
987
+ recentMemories: deduplicatedMemories.filter((m) => recentIds.has(m.id)),
988
+ globalProfile: context.globalProfile,
989
+ relevantMemories: deduplicatedMemories.filter((m) => !recentIds.has(m.id)).map((m) => ({ memory: m, score: 0, source: "hybrid" }))
990
+ };
991
+ log("SessionStart", "Injecting memories", {
992
+ recentCount: deduplicatedContext.recentMemories.length,
993
+ globalCount: deduplicatedContext.globalProfile.length,
994
+ relevantCount: deduplicatedContext.relevantMemories.length,
995
+ totalOriginal: allMemories.length,
996
+ totalDeduplicated: deduplicatedMemories.length
997
+ });
998
+ const maxChars = isPostCompact ? config.retrieval?.compactMaxChars ?? 2e3 : config.retrieval?.sessionMaxChars ?? 1e4;
999
+ const previewLength = isPostCompact ? 100 : 200;
1000
+ let formattedContext = formatContextForInjection(deduplicatedContext, {
1001
+ maxChars,
1002
+ previewLength
1003
+ });
1004
+ log("SessionStart", "Formatted context", {
1005
+ isPostCompact,
1006
+ maxChars,
1007
+ actualChars: formattedContext.length
1008
+ });
1009
+ if (isPostCompact) {
1010
+ formattedContext = `[Context restored after compaction - the following memories were preserved from your session]
1011
+
1012
+ ${formattedContext}`;
1013
+ }
1014
+ outputResult({
1015
+ hookSpecificOutput: {
1016
+ hookEventName: "SessionStart",
1017
+ additionalContext: formattedContext
1018
+ }
1019
+ });
1020
+ } catch (error) {
1021
+ logError("SessionStart", error);
1022
+ console.error("[memextend] Session start error:", error);
1023
+ outputResult({});
1024
+ }
1025
+ }
1026
+ function getProjectId2(cwd) {
1027
+ try {
1028
+ const gitRoot = (0, import_child_process.execSync)("git rev-parse --show-toplevel", {
1029
+ cwd,
1030
+ encoding: "utf-8",
1031
+ stdio: ["pipe", "pipe", "pipe"]
1032
+ }).trim();
1033
+ return (0, import_crypto.createHash)("sha256").update(gitRoot).digest("hex").slice(0, 16);
1034
+ } catch {
1035
+ return (0, import_crypto.createHash)("sha256").update(cwd).digest("hex").slice(0, 16);
1036
+ }
1037
+ }
1038
+ async function loadConfig() {
1039
+ try {
1040
+ if ((0, import_fs3.existsSync)(CONFIG_PATH2)) {
1041
+ const content = await (0, import_promises3.readFile)(CONFIG_PATH2, "utf-8");
1042
+ return JSON.parse(content);
1043
+ }
1044
+ } catch {
1045
+ }
1046
+ return {};
1047
+ }
1048
+ function outputResult(result) {
1049
+ console.log(JSON.stringify(result));
1050
+ }
1051
+ main().catch((error) => {
1052
+ console.error("[memextend] Fatal error:", error);
1053
+ process.exit(0);
1054
+ });