@membank/core 0.5.1 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +213 -44
- package/dist/index.d.cts +49 -8
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +49 -8
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +210 -45
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -48,7 +48,7 @@ var DatabaseError = class extends MembankError {
|
|
|
48
48
|
//#endregion
|
|
49
49
|
//#region src/db/manager.ts
|
|
50
50
|
const DEFAULT_DB_PATH = (0, node_path.join)((0, node_os.homedir)(), ".membank", "memory.db");
|
|
51
|
-
const MIGRATIONS = [[1, `
|
|
51
|
+
const MIGRATIONS$1 = [[1, `
|
|
52
52
|
CREATE TABLE IF NOT EXISTS memories (
|
|
53
53
|
id TEXT PRIMARY KEY,
|
|
54
54
|
content TEXT NOT NULL,
|
|
@@ -66,6 +66,39 @@ CREATE TABLE IF NOT EXISTS memories (
|
|
|
66
66
|
CREATE VIRTUAL TABLE IF NOT EXISTS embeddings USING vec0(
|
|
67
67
|
embedding FLOAT[384]
|
|
68
68
|
);
|
|
69
|
+
`], [2, `
|
|
70
|
+
CREATE TABLE IF NOT EXISTS projects (
|
|
71
|
+
id TEXT PRIMARY KEY,
|
|
72
|
+
name TEXT NOT NULL,
|
|
73
|
+
scope_hash TEXT NOT NULL UNIQUE,
|
|
74
|
+
created_at TEXT NOT NULL,
|
|
75
|
+
updated_at TEXT NOT NULL
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
CREATE TABLE IF NOT EXISTS memory_projects (
|
|
79
|
+
memory_id TEXT NOT NULL REFERENCES memories(id) ON DELETE CASCADE,
|
|
80
|
+
project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
|
|
81
|
+
PRIMARY KEY (memory_id, project_id)
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
INSERT OR IGNORE INTO projects (id, name, scope_hash, created_at, updated_at)
|
|
85
|
+
SELECT
|
|
86
|
+
lower(hex(randomblob(16))),
|
|
87
|
+
'project-' || substr(scope, 1, 8),
|
|
88
|
+
scope,
|
|
89
|
+
datetime('now'),
|
|
90
|
+
datetime('now')
|
|
91
|
+
FROM memories
|
|
92
|
+
WHERE scope != 'global'
|
|
93
|
+
GROUP BY scope;
|
|
94
|
+
|
|
95
|
+
INSERT OR IGNORE INTO memory_projects (memory_id, project_id)
|
|
96
|
+
SELECT m.id, p.id
|
|
97
|
+
FROM memories m
|
|
98
|
+
JOIN projects p ON p.scope_hash = m.scope
|
|
99
|
+
WHERE m.scope != 'global';
|
|
100
|
+
|
|
101
|
+
ALTER TABLE memories DROP COLUMN scope;
|
|
69
102
|
`]];
|
|
70
103
|
var DatabaseManager = class DatabaseManager {
|
|
71
104
|
#db;
|
|
@@ -96,6 +129,7 @@ var DatabaseManager = class DatabaseManager {
|
|
|
96
129
|
throw new DatabaseError("Failed to load sqlite-vec extension", { cause: err });
|
|
97
130
|
}
|
|
98
131
|
db.pragma("journal_mode = WAL");
|
|
132
|
+
db.pragma("foreign_keys = ON");
|
|
99
133
|
const manager = new DatabaseManager(db);
|
|
100
134
|
manager.#runMigrations();
|
|
101
135
|
return manager;
|
|
@@ -109,7 +143,7 @@ var DatabaseManager = class DatabaseManager {
|
|
|
109
143
|
`);
|
|
110
144
|
const row = this.#db.prepare("SELECT value FROM meta WHERE key = 'schema_version'").get();
|
|
111
145
|
const currentVersion = row ? Number.parseInt(row.value, 10) : 0;
|
|
112
|
-
for (const [targetVersion, sql] of MIGRATIONS) if (currentVersion < targetVersion) {
|
|
146
|
+
for (const [targetVersion, sql] of MIGRATIONS$1) if (currentVersion < targetVersion) {
|
|
113
147
|
this.#db.exec(sql);
|
|
114
148
|
this.#db.prepare("INSERT OR REPLACE INTO meta (key, value) VALUES ('schema_version', ?)").run(String(targetVersion));
|
|
115
149
|
}
|
|
@@ -123,13 +157,13 @@ var DatabaseManager = class DatabaseManager {
|
|
|
123
157
|
};
|
|
124
158
|
//#endregion
|
|
125
159
|
//#region src/db/row-types.ts
|
|
126
|
-
function rowToMemory(row) {
|
|
160
|
+
function rowToMemory(row, projects) {
|
|
127
161
|
return {
|
|
128
162
|
id: row.id,
|
|
129
163
|
content: row.content,
|
|
130
164
|
type: row.type,
|
|
131
165
|
tags: JSON.parse(row.tags),
|
|
132
|
-
|
|
166
|
+
projects,
|
|
133
167
|
sourceHarness: row.source,
|
|
134
168
|
accessCount: row.access_count,
|
|
135
169
|
pinned: row.pinned !== 0,
|
|
@@ -138,6 +172,15 @@ function rowToMemory(row) {
|
|
|
138
172
|
updatedAt: row.updated_at
|
|
139
173
|
};
|
|
140
174
|
}
|
|
175
|
+
function rowToProject(row) {
|
|
176
|
+
return {
|
|
177
|
+
id: row.id,
|
|
178
|
+
name: row.name,
|
|
179
|
+
scopeHash: row.scope_hash,
|
|
180
|
+
createdAt: row.created_at,
|
|
181
|
+
updatedAt: row.updated_at
|
|
182
|
+
};
|
|
183
|
+
}
|
|
141
184
|
//#endregion
|
|
142
185
|
//#region src/embedding/service.ts
|
|
143
186
|
var EmbeddingService = class {
|
|
@@ -177,30 +220,44 @@ const MEMORY_TYPE_VALUES = [
|
|
|
177
220
|
var MemoryRepository = class {
|
|
178
221
|
#db;
|
|
179
222
|
#embedding;
|
|
180
|
-
|
|
223
|
+
#projects;
|
|
224
|
+
constructor(db, embeddingService, projects) {
|
|
181
225
|
this.#db = db;
|
|
182
226
|
this.#embedding = embeddingService;
|
|
227
|
+
this.#projects = projects;
|
|
183
228
|
}
|
|
184
229
|
async save(options) {
|
|
185
|
-
const { content, type, tags = [],
|
|
230
|
+
const { content, type, tags = [], projectScope, sourceHarness } = options;
|
|
186
231
|
const embedding = await this.#embedding.embed(content);
|
|
187
232
|
const embeddingBlob = Buffer.from(embedding.buffer);
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
233
|
+
let top;
|
|
234
|
+
if (projectScope !== void 0) top = this.#db.db.prepare(`SELECT m.rowid, m.*, (1 - vec_distance_cosine(e.embedding, ?)) AS similarity
|
|
235
|
+
FROM memories m JOIN embeddings e ON e.rowid = m.rowid
|
|
236
|
+
JOIN memory_projects mp ON mp.memory_id = m.id
|
|
237
|
+
JOIN projects p ON p.id = mp.project_id
|
|
238
|
+
WHERE m.type = ? AND p.scope_hash = ?
|
|
239
|
+
ORDER BY similarity DESC LIMIT 1`).get(embeddingBlob, type, projectScope.hash);
|
|
240
|
+
else top = this.#db.db.prepare(`SELECT m.rowid, m.*, (1 - vec_distance_cosine(e.embedding, ?)) AS similarity
|
|
241
|
+
FROM memories m JOIN embeddings e ON e.rowid = m.rowid
|
|
242
|
+
WHERE m.type = ?
|
|
243
|
+
AND m.id NOT IN (SELECT memory_id FROM memory_projects)
|
|
244
|
+
ORDER BY similarity DESC LIMIT 1`).get(embeddingBlob, type);
|
|
192
245
|
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
193
246
|
if (top !== void 0 && top.similarity > .92) {
|
|
194
247
|
this.#db.db.prepare(`UPDATE memories SET content = ?, updated_at = ? WHERE id = ?`).run(content, now, top.id);
|
|
195
248
|
this.#db.db.prepare(`UPDATE embeddings SET embedding = ? WHERE rowid = ?`).run(embeddingBlob, top.rowid);
|
|
196
|
-
return rowToMemory(this.#db.db.prepare(`SELECT * FROM memories WHERE id = ?`).get(top.id));
|
|
249
|
+
return rowToMemory(this.#db.db.prepare(`SELECT * FROM memories WHERE id = ?`).get(top.id), this.#projects.getProjectsForMemories([top.id]).get(top.id) ?? []);
|
|
197
250
|
}
|
|
198
251
|
if (top !== void 0 && top.similarity >= .75) this.#db.db.prepare(`UPDATE memories SET needs_review = 1 WHERE id = ?`).run(top.id);
|
|
199
252
|
const id = (0, node_crypto.randomUUID)();
|
|
200
|
-
this.#db.db.prepare(`INSERT INTO memories (id, content, type, tags,
|
|
201
|
-
VALUES (?, ?, ?, ?, ?,
|
|
253
|
+
this.#db.db.prepare(`INSERT INTO memories (id, content, type, tags, source, access_count, pinned, needs_review, created_at, updated_at)
|
|
254
|
+
VALUES (?, ?, ?, ?, ?, 0, 0, 0, ?, ?)`).run(id, content, type, JSON.stringify(tags), sourceHarness ?? null, now, now);
|
|
202
255
|
this.#db.db.prepare(`INSERT INTO embeddings (rowid, embedding) SELECT m.rowid, ? FROM memories m WHERE m.id = ?`).run(embeddingBlob, id);
|
|
203
|
-
|
|
256
|
+
if (projectScope !== void 0) {
|
|
257
|
+
const project = this.#projects.upsertByHash(projectScope.hash, projectScope.name);
|
|
258
|
+
this.#projects.addAssociation(id, project.id);
|
|
259
|
+
}
|
|
260
|
+
return rowToMemory(this.#db.db.prepare(`SELECT * FROM memories WHERE id = ?`).get(id), this.#projects.getProjectsForMemories([id]).get(id) ?? []);
|
|
204
261
|
}
|
|
205
262
|
async update(id, patch) {
|
|
206
263
|
const existing = this.#db.db.prepare(`SELECT m.rowid, m.* FROM memories m WHERE m.id = ?`).get(id);
|
|
@@ -223,11 +280,12 @@ var MemoryRepository = class {
|
|
|
223
280
|
const embeddingBlob = Buffer.from(embedding.buffer);
|
|
224
281
|
this.#db.db.prepare(`UPDATE embeddings SET embedding = ? WHERE rowid = ?`).run(embeddingBlob, existing.rowid);
|
|
225
282
|
}
|
|
226
|
-
return rowToMemory(this.#db.db.prepare(`SELECT * FROM memories WHERE id = ?`).get(id));
|
|
283
|
+
return rowToMemory(this.#db.db.prepare(`SELECT * FROM memories WHERE id = ?`).get(id), this.#projects.getProjectsForMemories([id]).get(id) ?? []);
|
|
227
284
|
}
|
|
228
285
|
delete(id) {
|
|
229
286
|
const row = this.#db.db.prepare(`SELECT rowid FROM memories WHERE id = ?`).get(id);
|
|
230
287
|
if (row !== void 0) this.#db.db.prepare(`DELETE FROM embeddings WHERE rowid = ?`).run(row.rowid);
|
|
288
|
+
this.#db.db.prepare(`DELETE FROM memory_projects WHERE memory_id = ?`).run(id);
|
|
231
289
|
this.#db.db.prepare(`DELETE FROM memories WHERE id = ?`).run(id);
|
|
232
290
|
return Promise.resolve();
|
|
233
291
|
}
|
|
@@ -240,7 +298,11 @@ var MemoryRepository = class {
|
|
|
240
298
|
}
|
|
241
299
|
if (opts?.pinned === true) conditions.push("pinned = 1");
|
|
242
300
|
const where = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
|
|
243
|
-
|
|
301
|
+
const rows = this.#db.db.prepare(`SELECT * FROM memories ${where} ORDER BY created_at DESC`).all(...params);
|
|
302
|
+
if (rows.length === 0) return [];
|
|
303
|
+
const ids = rows.map((r) => r.id);
|
|
304
|
+
const projectMap = this.#projects.getProjectsForMemories(ids);
|
|
305
|
+
return rows.map((row) => rowToMemory(row, projectMap.get(row.id) ?? []));
|
|
244
306
|
}
|
|
245
307
|
stats() {
|
|
246
308
|
const byType = Object.fromEntries(MEMORY_TYPE_VALUES.map((t) => [t, 0]));
|
|
@@ -260,13 +322,125 @@ var MemoryRepository = class {
|
|
|
260
322
|
if (this.#db.db.prepare(`SELECT * FROM memories WHERE id = ?`).get(id) === void 0) throw new Error(`Memory not found: ${id}`);
|
|
261
323
|
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
262
324
|
this.#db.db.prepare(`UPDATE memories SET pinned = ?, updated_at = ? WHERE id = ?`).run(pinned ? 1 : 0, now, id);
|
|
263
|
-
return rowToMemory(this.#db.db.prepare(`SELECT * FROM memories WHERE id = ?`).get(id));
|
|
325
|
+
return rowToMemory(this.#db.db.prepare(`SELECT * FROM memories WHERE id = ?`).get(id), this.#projects.getProjectsForMemories([id]).get(id) ?? []);
|
|
264
326
|
}
|
|
265
327
|
incrementAccessCount(id) {
|
|
266
328
|
this.#db.db.prepare(`UPDATE memories SET access_count = access_count + 1 WHERE id = ?`).run(id);
|
|
267
329
|
}
|
|
268
330
|
};
|
|
269
331
|
//#endregion
|
|
332
|
+
//#region src/scope/resolver.ts
|
|
333
|
+
const execFileAsync = (0, node_util.promisify)(node_child_process.execFile);
|
|
334
|
+
function sha256Truncated(input) {
|
|
335
|
+
return (0, node_crypto.createHash)("sha256").update(input).digest("hex").slice(0, 16);
|
|
336
|
+
}
|
|
337
|
+
async function resolveProject() {
|
|
338
|
+
try {
|
|
339
|
+
const { stdout } = await execFileAsync("git", [
|
|
340
|
+
"remote",
|
|
341
|
+
"get-url",
|
|
342
|
+
"origin"
|
|
343
|
+
]);
|
|
344
|
+
const url = stdout.trim();
|
|
345
|
+
if (url) {
|
|
346
|
+
const hash = sha256Truncated(url);
|
|
347
|
+
return {
|
|
348
|
+
hash,
|
|
349
|
+
name: url.split("/").pop()?.replace(/\.git$/, "") ?? hash.slice(0, 8)
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
} catch {}
|
|
353
|
+
const cwd = process.cwd();
|
|
354
|
+
const hash = sha256Truncated(cwd);
|
|
355
|
+
return {
|
|
356
|
+
hash,
|
|
357
|
+
name: cwd.split(/[/\\]/).filter(Boolean).pop() ?? hash.slice(0, 8)
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
async function resolveScope() {
|
|
361
|
+
try {
|
|
362
|
+
const { stdout } = await execFileAsync("git", [
|
|
363
|
+
"remote",
|
|
364
|
+
"get-url",
|
|
365
|
+
"origin"
|
|
366
|
+
]);
|
|
367
|
+
const url = stdout.trim();
|
|
368
|
+
if (url) return sha256Truncated(url);
|
|
369
|
+
} catch {}
|
|
370
|
+
return sha256Truncated(process.cwd());
|
|
371
|
+
}
|
|
372
|
+
//#endregion
|
|
373
|
+
//#region src/migrations/index.ts
|
|
374
|
+
const MIGRATIONS = [{
|
|
375
|
+
name: "scope-to-projects",
|
|
376
|
+
description: "Rename the auto-migrated project for the current directory from its generic hash-derived name to the resolved repo/directory name."
|
|
377
|
+
}];
|
|
378
|
+
async function runScopeToProjectsMigration(projects) {
|
|
379
|
+
const resolved = await resolveProject();
|
|
380
|
+
const project = projects.getByHash(resolved.hash);
|
|
381
|
+
if (project === void 0) return null;
|
|
382
|
+
const oldName = project.name;
|
|
383
|
+
const memoryCount = projects.countMemories(project.id);
|
|
384
|
+
projects.rename(project.id, resolved.name);
|
|
385
|
+
return {
|
|
386
|
+
migration: "scope-to-projects",
|
|
387
|
+
oldName,
|
|
388
|
+
newName: resolved.name,
|
|
389
|
+
memoryCount
|
|
390
|
+
};
|
|
391
|
+
}
|
|
392
|
+
//#endregion
|
|
393
|
+
//#region src/project/repository.ts
|
|
394
|
+
var ProjectRepository = class {
|
|
395
|
+
#db;
|
|
396
|
+
constructor(db) {
|
|
397
|
+
this.#db = db;
|
|
398
|
+
}
|
|
399
|
+
upsertByHash(hash, name) {
|
|
400
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
401
|
+
const id = (0, node_crypto.randomUUID)();
|
|
402
|
+
this.#db.db.prepare(`INSERT OR IGNORE INTO projects (id, name, scope_hash, created_at, updated_at) VALUES (?, ?, ?, ?, ?)`).run(id, name, hash, now, now);
|
|
403
|
+
return rowToProject(this.#db.db.prepare(`SELECT * FROM projects WHERE scope_hash = ?`).get(hash));
|
|
404
|
+
}
|
|
405
|
+
rename(id, name) {
|
|
406
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
407
|
+
this.#db.db.prepare(`UPDATE projects SET name = ?, updated_at = ? WHERE id = ?`).run(name, now, id);
|
|
408
|
+
const row = this.#db.db.prepare(`SELECT * FROM projects WHERE id = ?`).get(id);
|
|
409
|
+
if (row === void 0) throw new Error(`Project not found: ${id}`);
|
|
410
|
+
return rowToProject(row);
|
|
411
|
+
}
|
|
412
|
+
list() {
|
|
413
|
+
return this.#db.db.prepare(`SELECT * FROM projects ORDER BY name ASC`).all().map(rowToProject);
|
|
414
|
+
}
|
|
415
|
+
getByHash(hash) {
|
|
416
|
+
const row = this.#db.db.prepare(`SELECT * FROM projects WHERE scope_hash = ?`).get(hash);
|
|
417
|
+
return row !== void 0 ? rowToProject(row) : void 0;
|
|
418
|
+
}
|
|
419
|
+
addAssociation(memoryId, projectId) {
|
|
420
|
+
this.#db.db.prepare(`INSERT OR IGNORE INTO memory_projects (memory_id, project_id) VALUES (?, ?)`).run(memoryId, projectId);
|
|
421
|
+
}
|
|
422
|
+
removeAssociation(memoryId, projectId) {
|
|
423
|
+
this.#db.db.prepare(`DELETE FROM memory_projects WHERE memory_id = ? AND project_id = ?`).run(memoryId, projectId);
|
|
424
|
+
}
|
|
425
|
+
countMemories(projectId) {
|
|
426
|
+
return this.#db.db.prepare(`SELECT COUNT(*) AS count FROM memory_projects WHERE project_id = ?`).get(projectId)?.count ?? 0;
|
|
427
|
+
}
|
|
428
|
+
getProjectsForMemories(memoryIds) {
|
|
429
|
+
if (memoryIds.length === 0) return /* @__PURE__ */ new Map();
|
|
430
|
+
const placeholders = memoryIds.map(() => "?").join(",");
|
|
431
|
+
const rows = this.#db.db.prepare(`SELECT p.*, mp.memory_id FROM projects p
|
|
432
|
+
JOIN memory_projects mp ON mp.project_id = p.id
|
|
433
|
+
WHERE mp.memory_id IN (${placeholders})`).all(...memoryIds);
|
|
434
|
+
const result = /* @__PURE__ */ new Map();
|
|
435
|
+
for (const row of rows) {
|
|
436
|
+
const list = result.get(row.memory_id) ?? [];
|
|
437
|
+
list.push(rowToProject(row));
|
|
438
|
+
result.set(row.memory_id, list);
|
|
439
|
+
}
|
|
440
|
+
return result;
|
|
441
|
+
}
|
|
442
|
+
};
|
|
443
|
+
//#endregion
|
|
270
444
|
//#region src/query/engine.ts
|
|
271
445
|
const TYPE_WEIGHTS = {
|
|
272
446
|
correction: 1,
|
|
@@ -285,28 +459,32 @@ var QueryEngine = class {
|
|
|
285
459
|
this.#repo = repo;
|
|
286
460
|
}
|
|
287
461
|
async query(options) {
|
|
288
|
-
const { query, type,
|
|
462
|
+
const { query, type, projectHash, limit = 10 } = options;
|
|
289
463
|
const queryEmbedding = await this.#embedding.embed(query);
|
|
290
464
|
const queryBlob = Buffer.from(queryEmbedding.buffer);
|
|
291
465
|
const whereClauses = [];
|
|
292
466
|
const params = [queryBlob];
|
|
467
|
+
let joinClause = "";
|
|
293
468
|
if (type !== void 0) {
|
|
294
469
|
whereClauses.push("m.type = ?");
|
|
295
470
|
params.push(type);
|
|
296
471
|
}
|
|
297
|
-
if (
|
|
298
|
-
|
|
299
|
-
|
|
472
|
+
if (projectHash !== void 0) {
|
|
473
|
+
joinClause = "LEFT JOIN memory_projects mp ON mp.memory_id = m.id LEFT JOIN projects p ON p.id = mp.project_id";
|
|
474
|
+
whereClauses.push("p.scope_hash = ?");
|
|
475
|
+
params.push(projectHash);
|
|
300
476
|
}
|
|
477
|
+
const whereSQL = whereClauses.length > 0 ? `WHERE ${whereClauses.join(" AND ")}` : "";
|
|
301
478
|
const sql = `
|
|
302
479
|
SELECT m.*, (1 - vec_distance_cosine(e.embedding, ?)) AS cosine_sim
|
|
303
480
|
FROM memories m JOIN embeddings e ON e.rowid = m.rowid
|
|
304
|
-
${
|
|
481
|
+
${joinClause}
|
|
482
|
+
${whereSQL}
|
|
305
483
|
`;
|
|
306
484
|
const rows = this.#db.db.prepare(sql).all(...params);
|
|
307
485
|
const now = Date.now();
|
|
308
486
|
const scored = rows.filter((row) => row.cosine_sim > 0).map((row) => {
|
|
309
|
-
const memory = rowToMemory(row);
|
|
487
|
+
const memory = rowToMemory(row, []);
|
|
310
488
|
const score = this.#computeScore(memory, now);
|
|
311
489
|
return {
|
|
312
490
|
...memory,
|
|
@@ -327,24 +505,6 @@ var QueryEngine = class {
|
|
|
327
505
|
}
|
|
328
506
|
};
|
|
329
507
|
//#endregion
|
|
330
|
-
//#region src/scope/resolver.ts
|
|
331
|
-
const execFileAsync = (0, node_util.promisify)(node_child_process.execFile);
|
|
332
|
-
function sha256Truncated(input) {
|
|
333
|
-
return (0, node_crypto.createHash)("sha256").update(input).digest("hex").slice(0, 16);
|
|
334
|
-
}
|
|
335
|
-
async function resolveScope() {
|
|
336
|
-
try {
|
|
337
|
-
const { stdout } = await execFileAsync("git", [
|
|
338
|
-
"remote",
|
|
339
|
-
"get-url",
|
|
340
|
-
"origin"
|
|
341
|
-
]);
|
|
342
|
-
const url = stdout.trim();
|
|
343
|
-
if (url) return sha256Truncated(url);
|
|
344
|
-
} catch {}
|
|
345
|
-
return sha256Truncated(process.cwd());
|
|
346
|
-
}
|
|
347
|
-
//#endregion
|
|
348
508
|
//#region src/session/builder.ts
|
|
349
509
|
function listMemoryTypes() {
|
|
350
510
|
return [...MEMORY_TYPE_VALUES];
|
|
@@ -354,9 +514,14 @@ var SessionContextBuilder = class {
|
|
|
354
514
|
constructor(db) {
|
|
355
515
|
this.#db = db;
|
|
356
516
|
}
|
|
357
|
-
getSessionContext(
|
|
358
|
-
const pinnedGlobal = this.#db.db.prepare(
|
|
359
|
-
|
|
517
|
+
getSessionContext(projectHash) {
|
|
518
|
+
const pinnedGlobal = this.#db.db.prepare(`SELECT * FROM memories
|
|
519
|
+
WHERE id NOT IN (SELECT memory_id FROM memory_projects)
|
|
520
|
+
AND pinned = 1`).all().map((row) => rowToMemory(row, []));
|
|
521
|
+
const pinnedProject = this.#db.db.prepare(`SELECT m.* FROM memories m
|
|
522
|
+
JOIN memory_projects mp ON mp.memory_id = m.id
|
|
523
|
+
JOIN projects p ON p.id = mp.project_id
|
|
524
|
+
WHERE p.scope_hash = ? AND m.pinned = 1`).all(projectHash).map((row) => rowToMemory(row, []));
|
|
360
525
|
const typeCounts = this.#db.db.prepare("SELECT type, COUNT(*) as count FROM memories GROUP BY type").all();
|
|
361
526
|
const stats = Object.fromEntries(MEMORY_TYPE_VALUES.map((t) => [t, 0]));
|
|
362
527
|
for (const row of typeCounts) if (stats[row.type] !== void 0) stats[row.type] = row.count;
|
|
@@ -372,10 +537,14 @@ exports.DatabaseError = DatabaseError;
|
|
|
372
537
|
exports.DatabaseManager = DatabaseManager;
|
|
373
538
|
exports.EmbeddingService = EmbeddingService;
|
|
374
539
|
exports.MEMORY_TYPE_VALUES = MEMORY_TYPE_VALUES;
|
|
540
|
+
exports.MIGRATIONS = MIGRATIONS;
|
|
375
541
|
exports.MembankError = MembankError;
|
|
376
542
|
exports.MemoryRepository = MemoryRepository;
|
|
543
|
+
exports.ProjectRepository = ProjectRepository;
|
|
377
544
|
exports.QueryEngine = QueryEngine;
|
|
378
545
|
exports.SessionContextBuilder = SessionContextBuilder;
|
|
379
546
|
exports.listMemoryTypes = listMemoryTypes;
|
|
547
|
+
exports.resolveProject = resolveProject;
|
|
380
548
|
exports.resolveScope = resolveScope;
|
|
381
549
|
exports.rowToMemory = rowToMemory;
|
|
550
|
+
exports.runScopeToProjectsMigration = runScopeToProjectsMigration;
|
package/dist/index.d.cts
CHANGED
|
@@ -24,12 +24,19 @@ declare class DatabaseManager {
|
|
|
24
24
|
//#region src/types.d.ts
|
|
25
25
|
type MemoryType = "correction" | "preference" | "decision" | "learning" | "fact";
|
|
26
26
|
declare const MEMORY_TYPE_VALUES: readonly ["correction", "preference", "decision", "learning", "fact"];
|
|
27
|
+
interface Project {
|
|
28
|
+
id: string;
|
|
29
|
+
name: string;
|
|
30
|
+
scopeHash: string;
|
|
31
|
+
createdAt: string;
|
|
32
|
+
updatedAt: string;
|
|
33
|
+
}
|
|
27
34
|
interface Memory {
|
|
28
35
|
id: string;
|
|
29
36
|
content: string;
|
|
30
37
|
type: MemoryType;
|
|
31
38
|
tags: string[];
|
|
32
|
-
|
|
39
|
+
projects: Project[];
|
|
33
40
|
sourceHarness: string | null;
|
|
34
41
|
accessCount: number;
|
|
35
42
|
pinned: boolean;
|
|
@@ -40,14 +47,17 @@ interface Memory {
|
|
|
40
47
|
interface QueryOptions {
|
|
41
48
|
query: string;
|
|
42
49
|
type?: MemoryType;
|
|
43
|
-
|
|
50
|
+
projectHash?: string;
|
|
44
51
|
limit?: number;
|
|
45
52
|
}
|
|
46
53
|
interface SaveOptions {
|
|
47
54
|
content: string;
|
|
48
55
|
type: MemoryType;
|
|
49
56
|
tags?: string[];
|
|
50
|
-
|
|
57
|
+
projectScope?: {
|
|
58
|
+
hash: string;
|
|
59
|
+
name: string;
|
|
60
|
+
};
|
|
51
61
|
sourceHarness?: string;
|
|
52
62
|
}
|
|
53
63
|
interface SessionContext {
|
|
@@ -62,7 +72,6 @@ interface MemoryRow {
|
|
|
62
72
|
content: string;
|
|
63
73
|
type: string;
|
|
64
74
|
tags: string;
|
|
65
|
-
scope: string;
|
|
66
75
|
source: string | null;
|
|
67
76
|
access_count: number;
|
|
68
77
|
pinned: number;
|
|
@@ -70,7 +79,7 @@ interface MemoryRow {
|
|
|
70
79
|
created_at: string;
|
|
71
80
|
updated_at: string;
|
|
72
81
|
}
|
|
73
|
-
declare function rowToMemory(row: MemoryRow): Memory;
|
|
82
|
+
declare function rowToMemory(row: MemoryRow, projects: Project[]): Memory;
|
|
74
83
|
//#endregion
|
|
75
84
|
//#region src/embedding/service.d.ts
|
|
76
85
|
type ProgressCallback = (progress: {
|
|
@@ -86,10 +95,24 @@ declare class EmbeddingService {
|
|
|
86
95
|
embed(text: string): Promise<Float32Array>;
|
|
87
96
|
}
|
|
88
97
|
//#endregion
|
|
98
|
+
//#region src/project/repository.d.ts
|
|
99
|
+
declare class ProjectRepository {
|
|
100
|
+
#private;
|
|
101
|
+
constructor(db: DatabaseManager);
|
|
102
|
+
upsertByHash(hash: string, name: string): Project;
|
|
103
|
+
rename(id: string, name: string): Project;
|
|
104
|
+
list(): Project[];
|
|
105
|
+
getByHash(hash: string): Project | undefined;
|
|
106
|
+
addAssociation(memoryId: string, projectId: string): void;
|
|
107
|
+
removeAssociation(memoryId: string, projectId: string): void;
|
|
108
|
+
countMemories(projectId: string): number;
|
|
109
|
+
getProjectsForMemories(memoryIds: string[]): Map<string, Project[]>;
|
|
110
|
+
}
|
|
111
|
+
//#endregion
|
|
89
112
|
//#region src/memory/repository.d.ts
|
|
90
113
|
declare class MemoryRepository {
|
|
91
114
|
#private;
|
|
92
|
-
constructor(db: DatabaseManager, embeddingService: EmbeddingService);
|
|
115
|
+
constructor(db: DatabaseManager, embeddingService: EmbeddingService, projects: ProjectRepository);
|
|
93
116
|
save(options: SaveOptions): Promise<Memory>;
|
|
94
117
|
update(id: string, patch: {
|
|
95
118
|
content?: string;
|
|
@@ -109,6 +132,20 @@ declare class MemoryRepository {
|
|
|
109
132
|
incrementAccessCount(id: string): void;
|
|
110
133
|
}
|
|
111
134
|
//#endregion
|
|
135
|
+
//#region src/migrations/index.d.ts
|
|
136
|
+
interface MigrationMeta {
|
|
137
|
+
name: string;
|
|
138
|
+
description: string;
|
|
139
|
+
}
|
|
140
|
+
interface ScopeToProjectsResult {
|
|
141
|
+
migration: "scope-to-projects";
|
|
142
|
+
oldName: string;
|
|
143
|
+
newName: string;
|
|
144
|
+
memoryCount: number;
|
|
145
|
+
}
|
|
146
|
+
declare const MIGRATIONS: MigrationMeta[];
|
|
147
|
+
declare function runScopeToProjectsMigration(projects: ProjectRepository): Promise<ScopeToProjectsResult | null>;
|
|
148
|
+
//#endregion
|
|
112
149
|
//#region src/query/engine.d.ts
|
|
113
150
|
declare class QueryEngine {
|
|
114
151
|
#private;
|
|
@@ -119,6 +156,10 @@ declare class QueryEngine {
|
|
|
119
156
|
}
|
|
120
157
|
//#endregion
|
|
121
158
|
//#region src/scope/resolver.d.ts
|
|
159
|
+
declare function resolveProject(): Promise<{
|
|
160
|
+
hash: string;
|
|
161
|
+
name: string;
|
|
162
|
+
}>;
|
|
122
163
|
declare function resolveScope(): Promise<string>;
|
|
123
164
|
//#endregion
|
|
124
165
|
//#region src/session/builder.d.ts
|
|
@@ -126,8 +167,8 @@ declare function listMemoryTypes(): MemoryType[];
|
|
|
126
167
|
declare class SessionContextBuilder {
|
|
127
168
|
#private;
|
|
128
169
|
constructor(db: DatabaseManager);
|
|
129
|
-
getSessionContext(
|
|
170
|
+
getSessionContext(projectHash: string): SessionContext;
|
|
130
171
|
}
|
|
131
172
|
//#endregion
|
|
132
|
-
export { DatabaseError, DatabaseManager, EmbeddingService, MEMORY_TYPE_VALUES, MembankError, Memory, MemoryRepository, type MemoryRow, MemoryType, ProgressCallback, QueryEngine, QueryOptions, SaveOptions, SessionContext, SessionContextBuilder, listMemoryTypes, resolveScope, rowToMemory };
|
|
173
|
+
export { DatabaseError, DatabaseManager, EmbeddingService, MEMORY_TYPE_VALUES, MIGRATIONS, MembankError, Memory, MemoryRepository, type MemoryRow, MemoryType, MigrationMeta, ProgressCallback, Project, ProjectRepository, QueryEngine, QueryOptions, SaveOptions, ScopeToProjectsResult, SessionContext, SessionContextBuilder, listMemoryTypes, resolveProject, resolveScope, rowToMemory, runScopeToProjectsMigration };
|
|
133
174
|
//# sourceMappingURL=index.d.cts.map
|
package/dist/index.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/db/errors.ts","../src/db/manager.ts","../src/types.ts","../src/db/row-types.ts","../src/embedding/service.ts","../src/memory/repository.ts","../src/query/engine.ts","../src/scope/resolver.ts","../src/session/builder.ts"],"mappings":";;;cAAa,YAAA,SAAqB,KAAA;cACpB,OAAA,UAAiB,OAAA,GAAU,YAAA;AAAA;AAAA,cAM5B,aAAA,SAAsB,YAAA;cACrB,OAAA,UAAiB,OAAA,GAAU,YAAA;AAAA;;;KCCpC,SAAA,IAAa,EAAA,EAAI,aAAA,CAAc,QAAA;AAAA,
|
|
1
|
+
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/db/errors.ts","../src/db/manager.ts","../src/types.ts","../src/db/row-types.ts","../src/embedding/service.ts","../src/project/repository.ts","../src/memory/repository.ts","../src/migrations/index.ts","../src/query/engine.ts","../src/scope/resolver.ts","../src/session/builder.ts"],"mappings":";;;cAAa,YAAA,SAAqB,KAAA;cACpB,OAAA,UAAiB,OAAA,GAAU,YAAA;AAAA;AAAA,cAM5B,aAAA,SAAsB,YAAA;cACrB,OAAA,UAAiB,OAAA,GAAU,YAAA;AAAA;;;KCCpC,SAAA,IAAa,EAAA,EAAI,aAAA,CAAc,QAAA;AAAA,cAgEvB,eAAA;EAAA;UAGJ,WAAA,CAAA;EAAA,OAIA,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,gBAAA,EAAkB,IAAA,EAAM,gBAAA;EAMrE,KAAA,CAAM,OAAA,EAAS,YAAA,GAAe,OAAA,CAAQ,KAAA,CAAM,MAAA;IAAW,KAAA;EAAA;AAAA;;;iBCpBzC,cAAA,CAAA,GAAkB,OAAA;EAAU,IAAA;EAAc,IAAA;AAAA;AAAA,iBAwB1C,YAAA,CAAA,GAAgB,OAAA;;;iBCvBtB,eAAA,CAAA,GAAmB,UAAA;AAAA,cAItB,qBAAA;EAAA;cAGC,EAAA,EAAI,eAAA;EAIhB,iBAAA,CAAkB,WAAA,WAAsB,cAAA;AAAA"}
|
package/dist/index.d.mts
CHANGED
|
@@ -24,12 +24,19 @@ declare class DatabaseManager {
|
|
|
24
24
|
//#region src/types.d.ts
|
|
25
25
|
type MemoryType = "correction" | "preference" | "decision" | "learning" | "fact";
|
|
26
26
|
declare const MEMORY_TYPE_VALUES: readonly ["correction", "preference", "decision", "learning", "fact"];
|
|
27
|
+
interface Project {
|
|
28
|
+
id: string;
|
|
29
|
+
name: string;
|
|
30
|
+
scopeHash: string;
|
|
31
|
+
createdAt: string;
|
|
32
|
+
updatedAt: string;
|
|
33
|
+
}
|
|
27
34
|
interface Memory {
|
|
28
35
|
id: string;
|
|
29
36
|
content: string;
|
|
30
37
|
type: MemoryType;
|
|
31
38
|
tags: string[];
|
|
32
|
-
|
|
39
|
+
projects: Project[];
|
|
33
40
|
sourceHarness: string | null;
|
|
34
41
|
accessCount: number;
|
|
35
42
|
pinned: boolean;
|
|
@@ -40,14 +47,17 @@ interface Memory {
|
|
|
40
47
|
interface QueryOptions {
|
|
41
48
|
query: string;
|
|
42
49
|
type?: MemoryType;
|
|
43
|
-
|
|
50
|
+
projectHash?: string;
|
|
44
51
|
limit?: number;
|
|
45
52
|
}
|
|
46
53
|
interface SaveOptions {
|
|
47
54
|
content: string;
|
|
48
55
|
type: MemoryType;
|
|
49
56
|
tags?: string[];
|
|
50
|
-
|
|
57
|
+
projectScope?: {
|
|
58
|
+
hash: string;
|
|
59
|
+
name: string;
|
|
60
|
+
};
|
|
51
61
|
sourceHarness?: string;
|
|
52
62
|
}
|
|
53
63
|
interface SessionContext {
|
|
@@ -62,7 +72,6 @@ interface MemoryRow {
|
|
|
62
72
|
content: string;
|
|
63
73
|
type: string;
|
|
64
74
|
tags: string;
|
|
65
|
-
scope: string;
|
|
66
75
|
source: string | null;
|
|
67
76
|
access_count: number;
|
|
68
77
|
pinned: number;
|
|
@@ -70,7 +79,7 @@ interface MemoryRow {
|
|
|
70
79
|
created_at: string;
|
|
71
80
|
updated_at: string;
|
|
72
81
|
}
|
|
73
|
-
declare function rowToMemory(row: MemoryRow): Memory;
|
|
82
|
+
declare function rowToMemory(row: MemoryRow, projects: Project[]): Memory;
|
|
74
83
|
//#endregion
|
|
75
84
|
//#region src/embedding/service.d.ts
|
|
76
85
|
type ProgressCallback = (progress: {
|
|
@@ -86,10 +95,24 @@ declare class EmbeddingService {
|
|
|
86
95
|
embed(text: string): Promise<Float32Array>;
|
|
87
96
|
}
|
|
88
97
|
//#endregion
|
|
98
|
+
//#region src/project/repository.d.ts
|
|
99
|
+
declare class ProjectRepository {
|
|
100
|
+
#private;
|
|
101
|
+
constructor(db: DatabaseManager);
|
|
102
|
+
upsertByHash(hash: string, name: string): Project;
|
|
103
|
+
rename(id: string, name: string): Project;
|
|
104
|
+
list(): Project[];
|
|
105
|
+
getByHash(hash: string): Project | undefined;
|
|
106
|
+
addAssociation(memoryId: string, projectId: string): void;
|
|
107
|
+
removeAssociation(memoryId: string, projectId: string): void;
|
|
108
|
+
countMemories(projectId: string): number;
|
|
109
|
+
getProjectsForMemories(memoryIds: string[]): Map<string, Project[]>;
|
|
110
|
+
}
|
|
111
|
+
//#endregion
|
|
89
112
|
//#region src/memory/repository.d.ts
|
|
90
113
|
declare class MemoryRepository {
|
|
91
114
|
#private;
|
|
92
|
-
constructor(db: DatabaseManager, embeddingService: EmbeddingService);
|
|
115
|
+
constructor(db: DatabaseManager, embeddingService: EmbeddingService, projects: ProjectRepository);
|
|
93
116
|
save(options: SaveOptions): Promise<Memory>;
|
|
94
117
|
update(id: string, patch: {
|
|
95
118
|
content?: string;
|
|
@@ -109,6 +132,20 @@ declare class MemoryRepository {
|
|
|
109
132
|
incrementAccessCount(id: string): void;
|
|
110
133
|
}
|
|
111
134
|
//#endregion
|
|
135
|
+
//#region src/migrations/index.d.ts
|
|
136
|
+
interface MigrationMeta {
|
|
137
|
+
name: string;
|
|
138
|
+
description: string;
|
|
139
|
+
}
|
|
140
|
+
interface ScopeToProjectsResult {
|
|
141
|
+
migration: "scope-to-projects";
|
|
142
|
+
oldName: string;
|
|
143
|
+
newName: string;
|
|
144
|
+
memoryCount: number;
|
|
145
|
+
}
|
|
146
|
+
declare const MIGRATIONS: MigrationMeta[];
|
|
147
|
+
declare function runScopeToProjectsMigration(projects: ProjectRepository): Promise<ScopeToProjectsResult | null>;
|
|
148
|
+
//#endregion
|
|
112
149
|
//#region src/query/engine.d.ts
|
|
113
150
|
declare class QueryEngine {
|
|
114
151
|
#private;
|
|
@@ -119,6 +156,10 @@ declare class QueryEngine {
|
|
|
119
156
|
}
|
|
120
157
|
//#endregion
|
|
121
158
|
//#region src/scope/resolver.d.ts
|
|
159
|
+
declare function resolveProject(): Promise<{
|
|
160
|
+
hash: string;
|
|
161
|
+
name: string;
|
|
162
|
+
}>;
|
|
122
163
|
declare function resolveScope(): Promise<string>;
|
|
123
164
|
//#endregion
|
|
124
165
|
//#region src/session/builder.d.ts
|
|
@@ -126,8 +167,8 @@ declare function listMemoryTypes(): MemoryType[];
|
|
|
126
167
|
declare class SessionContextBuilder {
|
|
127
168
|
#private;
|
|
128
169
|
constructor(db: DatabaseManager);
|
|
129
|
-
getSessionContext(
|
|
170
|
+
getSessionContext(projectHash: string): SessionContext;
|
|
130
171
|
}
|
|
131
172
|
//#endregion
|
|
132
|
-
export { DatabaseError, DatabaseManager, EmbeddingService, MEMORY_TYPE_VALUES, MembankError, Memory, MemoryRepository, type MemoryRow, MemoryType, ProgressCallback, QueryEngine, QueryOptions, SaveOptions, SessionContext, SessionContextBuilder, listMemoryTypes, resolveScope, rowToMemory };
|
|
173
|
+
export { DatabaseError, DatabaseManager, EmbeddingService, MEMORY_TYPE_VALUES, MIGRATIONS, MembankError, Memory, MemoryRepository, type MemoryRow, MemoryType, MigrationMeta, ProgressCallback, Project, ProjectRepository, QueryEngine, QueryOptions, SaveOptions, ScopeToProjectsResult, SessionContext, SessionContextBuilder, listMemoryTypes, resolveProject, resolveScope, rowToMemory, runScopeToProjectsMigration };
|
|
133
174
|
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/db/errors.ts","../src/db/manager.ts","../src/types.ts","../src/db/row-types.ts","../src/embedding/service.ts","../src/memory/repository.ts","../src/query/engine.ts","../src/scope/resolver.ts","../src/session/builder.ts"],"mappings":";;;cAAa,YAAA,SAAqB,KAAA;cACpB,OAAA,UAAiB,OAAA,GAAU,YAAA;AAAA;AAAA,cAM5B,aAAA,SAAsB,YAAA;cACrB,OAAA,UAAiB,OAAA,GAAU,YAAA;AAAA;;;KCCpC,SAAA,IAAa,EAAA,EAAI,aAAA,CAAc,QAAA;AAAA,
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/db/errors.ts","../src/db/manager.ts","../src/types.ts","../src/db/row-types.ts","../src/embedding/service.ts","../src/project/repository.ts","../src/memory/repository.ts","../src/migrations/index.ts","../src/query/engine.ts","../src/scope/resolver.ts","../src/session/builder.ts"],"mappings":";;;cAAa,YAAA,SAAqB,KAAA;cACpB,OAAA,UAAiB,OAAA,GAAU,YAAA;AAAA;AAAA,cAM5B,aAAA,SAAsB,YAAA;cACrB,OAAA,UAAiB,OAAA,GAAU,YAAA;AAAA;;;KCCpC,SAAA,IAAa,EAAA,EAAI,aAAA,CAAc,QAAA;AAAA,cAgEvB,eAAA;EAAA;UAGJ,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AA,EAAiB,gBAAA,EAAkB,gBAAA,EAAkB,IAAA,EAAM,gBAAA;EAMrE,KAAA,CAAM,OAAA,EAAS,YAAA,GAAe,OAAA,CAAQ,KAAA,CAAM,MAAA;IAAW,KAAA;EAAA;AAAA;;;iBCpBzC,cAAA,CAAA,GAAkB,OAAA;EAAU,IAAA;EAAc,IAAA;AAAA;AAAA,iBAwB1C,YAAA,CAAA,GAAgB,OAAA;;;iBCvBtB,eAAA,CAAA,GAAmB,UAAA;AAAA,cAItB,qBAAA;EAAA;cAGC,EAAA,EAAI,eAAA;EAIhB,iBAAA,CAAkB,WAAA,WAAsB,cAAA;AAAA"}
|
package/dist/index.mjs
CHANGED
|
@@ -23,7 +23,7 @@ var DatabaseError = class extends MembankError {
|
|
|
23
23
|
//#endregion
|
|
24
24
|
//#region src/db/manager.ts
|
|
25
25
|
const DEFAULT_DB_PATH = join(homedir(), ".membank", "memory.db");
|
|
26
|
-
const MIGRATIONS = [[1, `
|
|
26
|
+
const MIGRATIONS$1 = [[1, `
|
|
27
27
|
CREATE TABLE IF NOT EXISTS memories (
|
|
28
28
|
id TEXT PRIMARY KEY,
|
|
29
29
|
content TEXT NOT NULL,
|
|
@@ -41,6 +41,39 @@ CREATE TABLE IF NOT EXISTS memories (
|
|
|
41
41
|
CREATE VIRTUAL TABLE IF NOT EXISTS embeddings USING vec0(
|
|
42
42
|
embedding FLOAT[384]
|
|
43
43
|
);
|
|
44
|
+
`], [2, `
|
|
45
|
+
CREATE TABLE IF NOT EXISTS projects (
|
|
46
|
+
id TEXT PRIMARY KEY,
|
|
47
|
+
name TEXT NOT NULL,
|
|
48
|
+
scope_hash TEXT NOT NULL UNIQUE,
|
|
49
|
+
created_at TEXT NOT NULL,
|
|
50
|
+
updated_at TEXT NOT NULL
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
CREATE TABLE IF NOT EXISTS memory_projects (
|
|
54
|
+
memory_id TEXT NOT NULL REFERENCES memories(id) ON DELETE CASCADE,
|
|
55
|
+
project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
|
|
56
|
+
PRIMARY KEY (memory_id, project_id)
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
INSERT OR IGNORE INTO projects (id, name, scope_hash, created_at, updated_at)
|
|
60
|
+
SELECT
|
|
61
|
+
lower(hex(randomblob(16))),
|
|
62
|
+
'project-' || substr(scope, 1, 8),
|
|
63
|
+
scope,
|
|
64
|
+
datetime('now'),
|
|
65
|
+
datetime('now')
|
|
66
|
+
FROM memories
|
|
67
|
+
WHERE scope != 'global'
|
|
68
|
+
GROUP BY scope;
|
|
69
|
+
|
|
70
|
+
INSERT OR IGNORE INTO memory_projects (memory_id, project_id)
|
|
71
|
+
SELECT m.id, p.id
|
|
72
|
+
FROM memories m
|
|
73
|
+
JOIN projects p ON p.scope_hash = m.scope
|
|
74
|
+
WHERE m.scope != 'global';
|
|
75
|
+
|
|
76
|
+
ALTER TABLE memories DROP COLUMN scope;
|
|
44
77
|
`]];
|
|
45
78
|
var DatabaseManager = class DatabaseManager {
|
|
46
79
|
#db;
|
|
@@ -71,6 +104,7 @@ var DatabaseManager = class DatabaseManager {
|
|
|
71
104
|
throw new DatabaseError("Failed to load sqlite-vec extension", { cause: err });
|
|
72
105
|
}
|
|
73
106
|
db.pragma("journal_mode = WAL");
|
|
107
|
+
db.pragma("foreign_keys = ON");
|
|
74
108
|
const manager = new DatabaseManager(db);
|
|
75
109
|
manager.#runMigrations();
|
|
76
110
|
return manager;
|
|
@@ -84,7 +118,7 @@ var DatabaseManager = class DatabaseManager {
|
|
|
84
118
|
`);
|
|
85
119
|
const row = this.#db.prepare("SELECT value FROM meta WHERE key = 'schema_version'").get();
|
|
86
120
|
const currentVersion = row ? Number.parseInt(row.value, 10) : 0;
|
|
87
|
-
for (const [targetVersion, sql] of MIGRATIONS) if (currentVersion < targetVersion) {
|
|
121
|
+
for (const [targetVersion, sql] of MIGRATIONS$1) if (currentVersion < targetVersion) {
|
|
88
122
|
this.#db.exec(sql);
|
|
89
123
|
this.#db.prepare("INSERT OR REPLACE INTO meta (key, value) VALUES ('schema_version', ?)").run(String(targetVersion));
|
|
90
124
|
}
|
|
@@ -98,13 +132,13 @@ var DatabaseManager = class DatabaseManager {
|
|
|
98
132
|
};
|
|
99
133
|
//#endregion
|
|
100
134
|
//#region src/db/row-types.ts
|
|
101
|
-
function rowToMemory(row) {
|
|
135
|
+
function rowToMemory(row, projects) {
|
|
102
136
|
return {
|
|
103
137
|
id: row.id,
|
|
104
138
|
content: row.content,
|
|
105
139
|
type: row.type,
|
|
106
140
|
tags: JSON.parse(row.tags),
|
|
107
|
-
|
|
141
|
+
projects,
|
|
108
142
|
sourceHarness: row.source,
|
|
109
143
|
accessCount: row.access_count,
|
|
110
144
|
pinned: row.pinned !== 0,
|
|
@@ -113,6 +147,15 @@ function rowToMemory(row) {
|
|
|
113
147
|
updatedAt: row.updated_at
|
|
114
148
|
};
|
|
115
149
|
}
|
|
150
|
+
function rowToProject(row) {
|
|
151
|
+
return {
|
|
152
|
+
id: row.id,
|
|
153
|
+
name: row.name,
|
|
154
|
+
scopeHash: row.scope_hash,
|
|
155
|
+
createdAt: row.created_at,
|
|
156
|
+
updatedAt: row.updated_at
|
|
157
|
+
};
|
|
158
|
+
}
|
|
116
159
|
//#endregion
|
|
117
160
|
//#region src/embedding/service.ts
|
|
118
161
|
var EmbeddingService = class {
|
|
@@ -152,30 +195,44 @@ const MEMORY_TYPE_VALUES = [
|
|
|
152
195
|
var MemoryRepository = class {
|
|
153
196
|
#db;
|
|
154
197
|
#embedding;
|
|
155
|
-
|
|
198
|
+
#projects;
|
|
199
|
+
constructor(db, embeddingService, projects) {
|
|
156
200
|
this.#db = db;
|
|
157
201
|
this.#embedding = embeddingService;
|
|
202
|
+
this.#projects = projects;
|
|
158
203
|
}
|
|
159
204
|
async save(options) {
|
|
160
|
-
const { content, type, tags = [],
|
|
205
|
+
const { content, type, tags = [], projectScope, sourceHarness } = options;
|
|
161
206
|
const embedding = await this.#embedding.embed(content);
|
|
162
207
|
const embeddingBlob = Buffer.from(embedding.buffer);
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
208
|
+
let top;
|
|
209
|
+
if (projectScope !== void 0) top = this.#db.db.prepare(`SELECT m.rowid, m.*, (1 - vec_distance_cosine(e.embedding, ?)) AS similarity
|
|
210
|
+
FROM memories m JOIN embeddings e ON e.rowid = m.rowid
|
|
211
|
+
JOIN memory_projects mp ON mp.memory_id = m.id
|
|
212
|
+
JOIN projects p ON p.id = mp.project_id
|
|
213
|
+
WHERE m.type = ? AND p.scope_hash = ?
|
|
214
|
+
ORDER BY similarity DESC LIMIT 1`).get(embeddingBlob, type, projectScope.hash);
|
|
215
|
+
else top = this.#db.db.prepare(`SELECT m.rowid, m.*, (1 - vec_distance_cosine(e.embedding, ?)) AS similarity
|
|
216
|
+
FROM memories m JOIN embeddings e ON e.rowid = m.rowid
|
|
217
|
+
WHERE m.type = ?
|
|
218
|
+
AND m.id NOT IN (SELECT memory_id FROM memory_projects)
|
|
219
|
+
ORDER BY similarity DESC LIMIT 1`).get(embeddingBlob, type);
|
|
167
220
|
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
168
221
|
if (top !== void 0 && top.similarity > .92) {
|
|
169
222
|
this.#db.db.prepare(`UPDATE memories SET content = ?, updated_at = ? WHERE id = ?`).run(content, now, top.id);
|
|
170
223
|
this.#db.db.prepare(`UPDATE embeddings SET embedding = ? WHERE rowid = ?`).run(embeddingBlob, top.rowid);
|
|
171
|
-
return rowToMemory(this.#db.db.prepare(`SELECT * FROM memories WHERE id = ?`).get(top.id));
|
|
224
|
+
return rowToMemory(this.#db.db.prepare(`SELECT * FROM memories WHERE id = ?`).get(top.id), this.#projects.getProjectsForMemories([top.id]).get(top.id) ?? []);
|
|
172
225
|
}
|
|
173
226
|
if (top !== void 0 && top.similarity >= .75) this.#db.db.prepare(`UPDATE memories SET needs_review = 1 WHERE id = ?`).run(top.id);
|
|
174
227
|
const id = randomUUID();
|
|
175
|
-
this.#db.db.prepare(`INSERT INTO memories (id, content, type, tags,
|
|
176
|
-
VALUES (?, ?, ?, ?, ?,
|
|
228
|
+
this.#db.db.prepare(`INSERT INTO memories (id, content, type, tags, source, access_count, pinned, needs_review, created_at, updated_at)
|
|
229
|
+
VALUES (?, ?, ?, ?, ?, 0, 0, 0, ?, ?)`).run(id, content, type, JSON.stringify(tags), sourceHarness ?? null, now, now);
|
|
177
230
|
this.#db.db.prepare(`INSERT INTO embeddings (rowid, embedding) SELECT m.rowid, ? FROM memories m WHERE m.id = ?`).run(embeddingBlob, id);
|
|
178
|
-
|
|
231
|
+
if (projectScope !== void 0) {
|
|
232
|
+
const project = this.#projects.upsertByHash(projectScope.hash, projectScope.name);
|
|
233
|
+
this.#projects.addAssociation(id, project.id);
|
|
234
|
+
}
|
|
235
|
+
return rowToMemory(this.#db.db.prepare(`SELECT * FROM memories WHERE id = ?`).get(id), this.#projects.getProjectsForMemories([id]).get(id) ?? []);
|
|
179
236
|
}
|
|
180
237
|
async update(id, patch) {
|
|
181
238
|
const existing = this.#db.db.prepare(`SELECT m.rowid, m.* FROM memories m WHERE m.id = ?`).get(id);
|
|
@@ -198,11 +255,12 @@ var MemoryRepository = class {
|
|
|
198
255
|
const embeddingBlob = Buffer.from(embedding.buffer);
|
|
199
256
|
this.#db.db.prepare(`UPDATE embeddings SET embedding = ? WHERE rowid = ?`).run(embeddingBlob, existing.rowid);
|
|
200
257
|
}
|
|
201
|
-
return rowToMemory(this.#db.db.prepare(`SELECT * FROM memories WHERE id = ?`).get(id));
|
|
258
|
+
return rowToMemory(this.#db.db.prepare(`SELECT * FROM memories WHERE id = ?`).get(id), this.#projects.getProjectsForMemories([id]).get(id) ?? []);
|
|
202
259
|
}
|
|
203
260
|
delete(id) {
|
|
204
261
|
const row = this.#db.db.prepare(`SELECT rowid FROM memories WHERE id = ?`).get(id);
|
|
205
262
|
if (row !== void 0) this.#db.db.prepare(`DELETE FROM embeddings WHERE rowid = ?`).run(row.rowid);
|
|
263
|
+
this.#db.db.prepare(`DELETE FROM memory_projects WHERE memory_id = ?`).run(id);
|
|
206
264
|
this.#db.db.prepare(`DELETE FROM memories WHERE id = ?`).run(id);
|
|
207
265
|
return Promise.resolve();
|
|
208
266
|
}
|
|
@@ -215,7 +273,11 @@ var MemoryRepository = class {
|
|
|
215
273
|
}
|
|
216
274
|
if (opts?.pinned === true) conditions.push("pinned = 1");
|
|
217
275
|
const where = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
|
|
218
|
-
|
|
276
|
+
const rows = this.#db.db.prepare(`SELECT * FROM memories ${where} ORDER BY created_at DESC`).all(...params);
|
|
277
|
+
if (rows.length === 0) return [];
|
|
278
|
+
const ids = rows.map((r) => r.id);
|
|
279
|
+
const projectMap = this.#projects.getProjectsForMemories(ids);
|
|
280
|
+
return rows.map((row) => rowToMemory(row, projectMap.get(row.id) ?? []));
|
|
219
281
|
}
|
|
220
282
|
stats() {
|
|
221
283
|
const byType = Object.fromEntries(MEMORY_TYPE_VALUES.map((t) => [t, 0]));
|
|
@@ -235,13 +297,125 @@ var MemoryRepository = class {
|
|
|
235
297
|
if (this.#db.db.prepare(`SELECT * FROM memories WHERE id = ?`).get(id) === void 0) throw new Error(`Memory not found: ${id}`);
|
|
236
298
|
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
237
299
|
this.#db.db.prepare(`UPDATE memories SET pinned = ?, updated_at = ? WHERE id = ?`).run(pinned ? 1 : 0, now, id);
|
|
238
|
-
return rowToMemory(this.#db.db.prepare(`SELECT * FROM memories WHERE id = ?`).get(id));
|
|
300
|
+
return rowToMemory(this.#db.db.prepare(`SELECT * FROM memories WHERE id = ?`).get(id), this.#projects.getProjectsForMemories([id]).get(id) ?? []);
|
|
239
301
|
}
|
|
240
302
|
incrementAccessCount(id) {
|
|
241
303
|
this.#db.db.prepare(`UPDATE memories SET access_count = access_count + 1 WHERE id = ?`).run(id);
|
|
242
304
|
}
|
|
243
305
|
};
|
|
244
306
|
//#endregion
|
|
307
|
+
//#region src/scope/resolver.ts
|
|
308
|
+
const execFileAsync = promisify(execFile);
|
|
309
|
+
function sha256Truncated(input) {
|
|
310
|
+
return createHash("sha256").update(input).digest("hex").slice(0, 16);
|
|
311
|
+
}
|
|
312
|
+
async function resolveProject() {
|
|
313
|
+
try {
|
|
314
|
+
const { stdout } = await execFileAsync("git", [
|
|
315
|
+
"remote",
|
|
316
|
+
"get-url",
|
|
317
|
+
"origin"
|
|
318
|
+
]);
|
|
319
|
+
const url = stdout.trim();
|
|
320
|
+
if (url) {
|
|
321
|
+
const hash = sha256Truncated(url);
|
|
322
|
+
return {
|
|
323
|
+
hash,
|
|
324
|
+
name: url.split("/").pop()?.replace(/\.git$/, "") ?? hash.slice(0, 8)
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
} catch {}
|
|
328
|
+
const cwd = process.cwd();
|
|
329
|
+
const hash = sha256Truncated(cwd);
|
|
330
|
+
return {
|
|
331
|
+
hash,
|
|
332
|
+
name: cwd.split(/[/\\]/).filter(Boolean).pop() ?? hash.slice(0, 8)
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
async function resolveScope() {
|
|
336
|
+
try {
|
|
337
|
+
const { stdout } = await execFileAsync("git", [
|
|
338
|
+
"remote",
|
|
339
|
+
"get-url",
|
|
340
|
+
"origin"
|
|
341
|
+
]);
|
|
342
|
+
const url = stdout.trim();
|
|
343
|
+
if (url) return sha256Truncated(url);
|
|
344
|
+
} catch {}
|
|
345
|
+
return sha256Truncated(process.cwd());
|
|
346
|
+
}
|
|
347
|
+
//#endregion
|
|
348
|
+
//#region src/migrations/index.ts
|
|
349
|
+
const MIGRATIONS = [{
|
|
350
|
+
name: "scope-to-projects",
|
|
351
|
+
description: "Rename the auto-migrated project for the current directory from its generic hash-derived name to the resolved repo/directory name."
|
|
352
|
+
}];
|
|
353
|
+
async function runScopeToProjectsMigration(projects) {
|
|
354
|
+
const resolved = await resolveProject();
|
|
355
|
+
const project = projects.getByHash(resolved.hash);
|
|
356
|
+
if (project === void 0) return null;
|
|
357
|
+
const oldName = project.name;
|
|
358
|
+
const memoryCount = projects.countMemories(project.id);
|
|
359
|
+
projects.rename(project.id, resolved.name);
|
|
360
|
+
return {
|
|
361
|
+
migration: "scope-to-projects",
|
|
362
|
+
oldName,
|
|
363
|
+
newName: resolved.name,
|
|
364
|
+
memoryCount
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
//#endregion
|
|
368
|
+
//#region src/project/repository.ts
|
|
369
|
+
var ProjectRepository = class {
|
|
370
|
+
#db;
|
|
371
|
+
constructor(db) {
|
|
372
|
+
this.#db = db;
|
|
373
|
+
}
|
|
374
|
+
upsertByHash(hash, name) {
|
|
375
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
376
|
+
const id = randomUUID();
|
|
377
|
+
this.#db.db.prepare(`INSERT OR IGNORE INTO projects (id, name, scope_hash, created_at, updated_at) VALUES (?, ?, ?, ?, ?)`).run(id, name, hash, now, now);
|
|
378
|
+
return rowToProject(this.#db.db.prepare(`SELECT * FROM projects WHERE scope_hash = ?`).get(hash));
|
|
379
|
+
}
|
|
380
|
+
rename(id, name) {
|
|
381
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
382
|
+
this.#db.db.prepare(`UPDATE projects SET name = ?, updated_at = ? WHERE id = ?`).run(name, now, id);
|
|
383
|
+
const row = this.#db.db.prepare(`SELECT * FROM projects WHERE id = ?`).get(id);
|
|
384
|
+
if (row === void 0) throw new Error(`Project not found: ${id}`);
|
|
385
|
+
return rowToProject(row);
|
|
386
|
+
}
|
|
387
|
+
list() {
|
|
388
|
+
return this.#db.db.prepare(`SELECT * FROM projects ORDER BY name ASC`).all().map(rowToProject);
|
|
389
|
+
}
|
|
390
|
+
getByHash(hash) {
|
|
391
|
+
const row = this.#db.db.prepare(`SELECT * FROM projects WHERE scope_hash = ?`).get(hash);
|
|
392
|
+
return row !== void 0 ? rowToProject(row) : void 0;
|
|
393
|
+
}
|
|
394
|
+
addAssociation(memoryId, projectId) {
|
|
395
|
+
this.#db.db.prepare(`INSERT OR IGNORE INTO memory_projects (memory_id, project_id) VALUES (?, ?)`).run(memoryId, projectId);
|
|
396
|
+
}
|
|
397
|
+
removeAssociation(memoryId, projectId) {
|
|
398
|
+
this.#db.db.prepare(`DELETE FROM memory_projects WHERE memory_id = ? AND project_id = ?`).run(memoryId, projectId);
|
|
399
|
+
}
|
|
400
|
+
countMemories(projectId) {
|
|
401
|
+
return this.#db.db.prepare(`SELECT COUNT(*) AS count FROM memory_projects WHERE project_id = ?`).get(projectId)?.count ?? 0;
|
|
402
|
+
}
|
|
403
|
+
getProjectsForMemories(memoryIds) {
|
|
404
|
+
if (memoryIds.length === 0) return /* @__PURE__ */ new Map();
|
|
405
|
+
const placeholders = memoryIds.map(() => "?").join(",");
|
|
406
|
+
const rows = this.#db.db.prepare(`SELECT p.*, mp.memory_id FROM projects p
|
|
407
|
+
JOIN memory_projects mp ON mp.project_id = p.id
|
|
408
|
+
WHERE mp.memory_id IN (${placeholders})`).all(...memoryIds);
|
|
409
|
+
const result = /* @__PURE__ */ new Map();
|
|
410
|
+
for (const row of rows) {
|
|
411
|
+
const list = result.get(row.memory_id) ?? [];
|
|
412
|
+
list.push(rowToProject(row));
|
|
413
|
+
result.set(row.memory_id, list);
|
|
414
|
+
}
|
|
415
|
+
return result;
|
|
416
|
+
}
|
|
417
|
+
};
|
|
418
|
+
//#endregion
|
|
245
419
|
//#region src/query/engine.ts
|
|
246
420
|
const TYPE_WEIGHTS = {
|
|
247
421
|
correction: 1,
|
|
@@ -260,28 +434,32 @@ var QueryEngine = class {
|
|
|
260
434
|
this.#repo = repo;
|
|
261
435
|
}
|
|
262
436
|
async query(options) {
|
|
263
|
-
const { query, type,
|
|
437
|
+
const { query, type, projectHash, limit = 10 } = options;
|
|
264
438
|
const queryEmbedding = await this.#embedding.embed(query);
|
|
265
439
|
const queryBlob = Buffer.from(queryEmbedding.buffer);
|
|
266
440
|
const whereClauses = [];
|
|
267
441
|
const params = [queryBlob];
|
|
442
|
+
let joinClause = "";
|
|
268
443
|
if (type !== void 0) {
|
|
269
444
|
whereClauses.push("m.type = ?");
|
|
270
445
|
params.push(type);
|
|
271
446
|
}
|
|
272
|
-
if (
|
|
273
|
-
|
|
274
|
-
|
|
447
|
+
if (projectHash !== void 0) {
|
|
448
|
+
joinClause = "LEFT JOIN memory_projects mp ON mp.memory_id = m.id LEFT JOIN projects p ON p.id = mp.project_id";
|
|
449
|
+
whereClauses.push("p.scope_hash = ?");
|
|
450
|
+
params.push(projectHash);
|
|
275
451
|
}
|
|
452
|
+
const whereSQL = whereClauses.length > 0 ? `WHERE ${whereClauses.join(" AND ")}` : "";
|
|
276
453
|
const sql = `
|
|
277
454
|
SELECT m.*, (1 - vec_distance_cosine(e.embedding, ?)) AS cosine_sim
|
|
278
455
|
FROM memories m JOIN embeddings e ON e.rowid = m.rowid
|
|
279
|
-
${
|
|
456
|
+
${joinClause}
|
|
457
|
+
${whereSQL}
|
|
280
458
|
`;
|
|
281
459
|
const rows = this.#db.db.prepare(sql).all(...params);
|
|
282
460
|
const now = Date.now();
|
|
283
461
|
const scored = rows.filter((row) => row.cosine_sim > 0).map((row) => {
|
|
284
|
-
const memory = rowToMemory(row);
|
|
462
|
+
const memory = rowToMemory(row, []);
|
|
285
463
|
const score = this.#computeScore(memory, now);
|
|
286
464
|
return {
|
|
287
465
|
...memory,
|
|
@@ -302,24 +480,6 @@ var QueryEngine = class {
|
|
|
302
480
|
}
|
|
303
481
|
};
|
|
304
482
|
//#endregion
|
|
305
|
-
//#region src/scope/resolver.ts
|
|
306
|
-
const execFileAsync = promisify(execFile);
|
|
307
|
-
function sha256Truncated(input) {
|
|
308
|
-
return createHash("sha256").update(input).digest("hex").slice(0, 16);
|
|
309
|
-
}
|
|
310
|
-
async function resolveScope() {
|
|
311
|
-
try {
|
|
312
|
-
const { stdout } = await execFileAsync("git", [
|
|
313
|
-
"remote",
|
|
314
|
-
"get-url",
|
|
315
|
-
"origin"
|
|
316
|
-
]);
|
|
317
|
-
const url = stdout.trim();
|
|
318
|
-
if (url) return sha256Truncated(url);
|
|
319
|
-
} catch {}
|
|
320
|
-
return sha256Truncated(process.cwd());
|
|
321
|
-
}
|
|
322
|
-
//#endregion
|
|
323
483
|
//#region src/session/builder.ts
|
|
324
484
|
function listMemoryTypes() {
|
|
325
485
|
return [...MEMORY_TYPE_VALUES];
|
|
@@ -329,9 +489,14 @@ var SessionContextBuilder = class {
|
|
|
329
489
|
constructor(db) {
|
|
330
490
|
this.#db = db;
|
|
331
491
|
}
|
|
332
|
-
getSessionContext(
|
|
333
|
-
const pinnedGlobal = this.#db.db.prepare(
|
|
334
|
-
|
|
492
|
+
getSessionContext(projectHash) {
|
|
493
|
+
const pinnedGlobal = this.#db.db.prepare(`SELECT * FROM memories
|
|
494
|
+
WHERE id NOT IN (SELECT memory_id FROM memory_projects)
|
|
495
|
+
AND pinned = 1`).all().map((row) => rowToMemory(row, []));
|
|
496
|
+
const pinnedProject = this.#db.db.prepare(`SELECT m.* FROM memories m
|
|
497
|
+
JOIN memory_projects mp ON mp.memory_id = m.id
|
|
498
|
+
JOIN projects p ON p.id = mp.project_id
|
|
499
|
+
WHERE p.scope_hash = ? AND m.pinned = 1`).all(projectHash).map((row) => rowToMemory(row, []));
|
|
335
500
|
const typeCounts = this.#db.db.prepare("SELECT type, COUNT(*) as count FROM memories GROUP BY type").all();
|
|
336
501
|
const stats = Object.fromEntries(MEMORY_TYPE_VALUES.map((t) => [t, 0]));
|
|
337
502
|
for (const row of typeCounts) if (stats[row.type] !== void 0) stats[row.type] = row.count;
|
|
@@ -343,6 +508,6 @@ var SessionContextBuilder = class {
|
|
|
343
508
|
}
|
|
344
509
|
};
|
|
345
510
|
//#endregion
|
|
346
|
-
export { DatabaseError, DatabaseManager, EmbeddingService, MEMORY_TYPE_VALUES, MembankError, MemoryRepository, QueryEngine, SessionContextBuilder, listMemoryTypes, resolveScope, rowToMemory };
|
|
511
|
+
export { DatabaseError, DatabaseManager, EmbeddingService, MEMORY_TYPE_VALUES, MIGRATIONS, MembankError, MemoryRepository, ProjectRepository, QueryEngine, SessionContextBuilder, listMemoryTypes, resolveProject, resolveScope, rowToMemory, runScopeToProjectsMigration };
|
|
347
512
|
|
|
348
513
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":["#db","#init","#initInMemory","#runMigrations","#db","#embedding","#db","#embedding","#repo","#computeScore","#db"],"sources":["../src/db/errors.ts","../src/db/manager.ts","../src/db/row-types.ts","../src/embedding/service.ts","../src/types.ts","../src/memory/repository.ts","../src/query/engine.ts","../src/scope/resolver.ts","../src/session/builder.ts"],"sourcesContent":["export class MembankError extends Error {\n constructor(message: string, options?: ErrorOptions) {\n super(message, options);\n this.name = \"MembankError\";\n }\n}\n\nexport class DatabaseError extends MembankError {\n constructor(message: string, options?: ErrorOptions) {\n super(message, options);\n this.name = \"DatabaseError\";\n }\n}\n","import { mkdirSync } from \"node:fs\";\nimport { homedir } from \"node:os\";\nimport { dirname, join } from \"node:path\";\nimport BetterSqlite3 from \"better-sqlite3\";\nimport * as sqliteVec from \"sqlite-vec\";\nimport { DatabaseError } from \"./errors.js\";\n\nconst DEFAULT_DB_PATH = join(homedir(), \".membank\", \"memory.db\");\n\ntype VecLoader = (db: BetterSqlite3.Database) => void;\n\nconst MIGRATIONS: [number, string][] = [\n [\n 1,\n `\nCREATE TABLE IF NOT EXISTS memories (\n id TEXT PRIMARY KEY,\n content TEXT NOT NULL,\n type TEXT NOT NULL,\n tags TEXT NOT NULL DEFAULT '[]',\n scope TEXT NOT NULL,\n source TEXT,\n access_count INTEGER NOT NULL DEFAULT 0,\n pinned INTEGER NOT NULL DEFAULT 0,\n needs_review INTEGER NOT NULL DEFAULT 0,\n created_at TEXT NOT NULL,\n updated_at TEXT NOT NULL\n);\n\nCREATE VIRTUAL TABLE IF NOT EXISTS embeddings USING vec0(\n embedding FLOAT[384]\n);\n`,\n ],\n];\n\nexport class DatabaseManager {\n readonly #db: BetterSqlite3.Database;\n\n private constructor(db: BetterSqlite3.Database) {\n this.#db = db;\n }\n\n static open(dbPath?: string): DatabaseManager {\n const resolvedPath = dbPath ?? DEFAULT_DB_PATH;\n mkdirSync(dirname(resolvedPath), { recursive: true });\n const db = new BetterSqlite3(resolvedPath);\n return DatabaseManager.#init(db, sqliteVec.load);\n }\n\n static openInMemory(): DatabaseManager {\n return DatabaseManager.#initInMemory(sqliteVec.load);\n }\n\n /** For testing: inject a custom vec loader (e.g. a throwing stub). */\n static _openInMemoryWithLoader(loader: VecLoader): DatabaseManager {\n return DatabaseManager.#initInMemory(loader);\n }\n\n static #initInMemory(loader: VecLoader): DatabaseManager {\n const db = new BetterSqlite3(\":memory:\");\n return DatabaseManager.#init(db, loader);\n }\n\n static #init(db: BetterSqlite3.Database, loader: VecLoader): DatabaseManager {\n try {\n loader(db);\n } catch (err) {\n throw new DatabaseError(\"Failed to load sqlite-vec extension\", {\n cause: err,\n });\n }\n\n db.pragma(\"journal_mode = WAL\");\n\n const manager = new DatabaseManager(db);\n manager.#runMigrations();\n return manager;\n }\n\n #runMigrations(): void {\n // Bootstrap the meta table before reading schema_version from it\n this.#db.exec(`\n CREATE TABLE IF NOT EXISTS meta (\n key TEXT PRIMARY KEY,\n value TEXT NOT NULL\n );\n `);\n\n const row = this.#db\n .prepare<[], { value: string }>(\"SELECT value FROM meta WHERE key = 'schema_version'\")\n .get();\n\n const currentVersion = row ? Number.parseInt(row.value, 10) : 0;\n\n for (const [targetVersion, sql] of MIGRATIONS) {\n if (currentVersion < targetVersion) {\n this.#db.exec(sql);\n this.#db\n .prepare(\"INSERT OR REPLACE INTO meta (key, value) VALUES ('schema_version', ?)\")\n .run(String(targetVersion));\n }\n }\n }\n\n get db(): BetterSqlite3.Database {\n return this.#db;\n }\n\n close(): void {\n this.#db.close();\n }\n}\n","import type { Memory, MemoryType } from \"../types.js\";\n\nexport interface MemoryRow {\n id: string;\n content: string;\n type: string;\n tags: string;\n scope: string;\n source: string | null;\n access_count: number;\n pinned: number;\n needs_review: number;\n created_at: string;\n updated_at: string;\n}\n\nexport function rowToMemory(row: MemoryRow): Memory {\n return {\n id: row.id,\n content: row.content,\n type: row.type as MemoryType,\n tags: JSON.parse(row.tags) as string[],\n scope: row.scope,\n sourceHarness: row.source,\n accessCount: row.access_count,\n pinned: row.pinned !== 0,\n needsReview: row.needs_review !== 0,\n createdAt: row.created_at,\n updatedAt: row.updated_at,\n };\n}\n","import { homedir } from \"node:os\";\nimport { join } from \"node:path\";\nimport { pipeline } from \"@huggingface/transformers\";\n\nexport type ProgressCallback = (progress: { status: string; progress?: number }) => void;\n\nexport class EmbeddingService {\n private readonly modelCachePath: string;\n private readonly onProgress: ProgressCallback | undefined;\n private pipelineInstance: Awaited<ReturnType<typeof pipeline>> | null = null;\n\n constructor(modelCachePath?: string, onProgress?: ProgressCallback) {\n this.modelCachePath = modelCachePath ?? join(homedir(), \".membank\", \"models\");\n this.onProgress = onProgress;\n }\n\n private async getPipeline(): Promise<Awaited<ReturnType<typeof pipeline>>> {\n if (this.pipelineInstance === null) {\n this.pipelineInstance = await pipeline(\"feature-extraction\", \"Xenova/bge-small-en-v1.5\", {\n cache_dir: this.modelCachePath,\n progress_callback: this.onProgress,\n });\n }\n return this.pipelineInstance;\n }\n\n async embed(text: string): Promise<Float32Array> {\n const pipe = await this.getPipeline();\n // Shape: [1, seq_len, 384]. Cast to any to bypass the non-unified pipeline union signature.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const output = await (\n pipe as (input: string, opts: Record<string, unknown>) => Promise<unknown>\n )(text, { pooling: \"mean\", normalize: true });\n\n // @huggingface/transformers Tensor has a .data property with the flat array\n const tensor = output as { data: Float32Array | number[] };\n const flat = tensor.data;\n\n return flat instanceof Float32Array ? flat : new Float32Array(flat);\n }\n}\n","export type MemoryType = \"correction\" | \"preference\" | \"decision\" | \"learning\" | \"fact\";\n\nexport const MEMORY_TYPE_VALUES = [\n \"correction\",\n \"preference\",\n \"decision\",\n \"learning\",\n \"fact\",\n] as const satisfies readonly MemoryType[];\n\nexport interface Memory {\n id: string;\n content: string;\n type: MemoryType;\n tags: string[];\n scope: string;\n sourceHarness: string | null;\n accessCount: number;\n pinned: boolean;\n needsReview: boolean;\n createdAt: string;\n updatedAt: string;\n}\n\nexport interface QueryOptions {\n query: string;\n type?: MemoryType;\n scope?: string;\n limit?: number;\n}\n\nexport interface SaveOptions {\n content: string;\n type: MemoryType;\n tags?: string[];\n scope?: string;\n sourceHarness?: string;\n}\n\nexport interface SessionContext {\n stats: Record<MemoryType, number>;\n pinnedGlobal: Memory[];\n pinnedProject: Memory[];\n}\n","import { randomUUID } from \"node:crypto\";\nimport type { DatabaseManager } from \"../db/manager.js\";\nimport type { MemoryRow } from \"../db/row-types.js\";\nimport { rowToMemory } from \"../db/row-types.js\";\nimport type { EmbeddingService } from \"../embedding/service.js\";\nimport type { Memory, MemoryType, SaveOptions } from \"../types.js\";\nimport { MEMORY_TYPE_VALUES } from \"../types.js\";\n\ninterface SimilarityRow extends MemoryRow {\n rowid: number;\n similarity: number;\n}\n\nexport class MemoryRepository {\n readonly #db: DatabaseManager;\n readonly #embedding: EmbeddingService;\n\n constructor(db: DatabaseManager, embeddingService: EmbeddingService) {\n this.#db = db;\n this.#embedding = embeddingService;\n }\n\n async save(options: SaveOptions): Promise<Memory> {\n const { content, type, tags = [], scope = \"global\", sourceHarness } = options;\n\n const embedding = await this.#embedding.embed(content);\n const embeddingBlob = Buffer.from(embedding.buffer);\n\n const top = this.#db.db\n .prepare<[Buffer, string, string], SimilarityRow>(\n `SELECT m.rowid, m.*, (1 - vec_distance_cosine(e.embedding, ?)) AS similarity\n FROM memories m JOIN embeddings e ON e.rowid = m.rowid\n WHERE m.type = ? AND m.scope = ?\n ORDER BY similarity DESC LIMIT 1`\n )\n .get(embeddingBlob, type, scope);\n\n const now = new Date().toISOString();\n\n if (top !== undefined && top.similarity > 0.92) {\n this.#db.db\n .prepare(`UPDATE memories SET content = ?, updated_at = ? WHERE id = ?`)\n .run(content, now, top.id);\n\n this.#db.db\n .prepare(`UPDATE embeddings SET embedding = ? WHERE rowid = ?`)\n .run(embeddingBlob, top.rowid);\n\n const updated = this.#db.db\n .prepare<[string], MemoryRow>(`SELECT * FROM memories WHERE id = ?`)\n .get(top.id);\n\n // updated is guaranteed to exist since we just updated it\n return rowToMemory(updated as MemoryRow);\n }\n\n if (top !== undefined && top.similarity >= 0.75) {\n this.#db.db.prepare(`UPDATE memories SET needs_review = 1 WHERE id = ?`).run(top.id);\n }\n\n const id = randomUUID();\n this.#db.db\n .prepare(\n `INSERT INTO memories (id, content, type, tags, scope, source, access_count, pinned, needs_review, created_at, updated_at)\n VALUES (?, ?, ?, ?, ?, ?, 0, 0, 0, ?, ?)`\n )\n .run(id, content, type, JSON.stringify(tags), scope, sourceHarness ?? null, now, now);\n\n // sqlite-vec v0.1.9 does not accept parameterized rowid on INSERT into vec0 tables.\n // Use a SELECT subquery to copy the rowid from the memories row we just inserted.\n this.#db.db\n .prepare(\n `INSERT INTO embeddings (rowid, embedding) SELECT m.rowid, ? FROM memories m WHERE m.id = ?`\n )\n .run(embeddingBlob, id);\n\n const row = this.#db.db\n .prepare<[string], MemoryRow>(`SELECT * FROM memories WHERE id = ?`)\n .get(id) as MemoryRow;\n\n return rowToMemory(row);\n }\n\n async update(id: string, patch: { content?: string; tags?: string[] }): Promise<Memory> {\n const existing = this.#db.db\n .prepare<[string], MemoryRow & { rowid: number }>(\n `SELECT m.rowid, m.* FROM memories m WHERE m.id = ?`\n )\n .get(id);\n\n if (existing === undefined) {\n throw new Error(`Memory not found: ${id}`);\n }\n\n const now = new Date().toISOString();\n const sets: string[] = [\"updated_at = ?\"];\n const values: string[] = [now];\n\n if (patch.content !== undefined) {\n sets.push(\"content = ?\");\n values.push(patch.content);\n }\n\n if (patch.tags !== undefined) {\n sets.push(\"tags = ?\");\n values.push(JSON.stringify(patch.tags));\n }\n\n values.push(id);\n this.#db.db.prepare(`UPDATE memories SET ${sets.join(\", \")} WHERE id = ?`).run(...values);\n\n if (patch.content !== undefined) {\n const embedding = await this.#embedding.embed(patch.content);\n const embeddingBlob = Buffer.from(embedding.buffer);\n this.#db.db\n .prepare(`UPDATE embeddings SET embedding = ? WHERE rowid = ?`)\n .run(embeddingBlob, existing.rowid);\n }\n\n const updated = this.#db.db\n .prepare<[string], MemoryRow>(`SELECT * FROM memories WHERE id = ?`)\n .get(id) as MemoryRow;\n\n return rowToMemory(updated);\n }\n\n delete(id: string): Promise<void> {\n const row = this.#db.db\n .prepare<[string], { rowid: number }>(`SELECT rowid FROM memories WHERE id = ?`)\n .get(id);\n\n if (row !== undefined) {\n this.#db.db.prepare(`DELETE FROM embeddings WHERE rowid = ?`).run(row.rowid);\n }\n\n this.#db.db.prepare(`DELETE FROM memories WHERE id = ?`).run(id);\n\n return Promise.resolve();\n }\n\n list(opts?: { type?: MemoryType; pinned?: boolean }): Memory[] {\n const conditions: string[] = [];\n const params: (string | number)[] = [];\n\n if (opts?.type !== undefined) {\n conditions.push(\"type = ?\");\n params.push(opts.type);\n }\n\n if (opts?.pinned === true) {\n conditions.push(\"pinned = 1\");\n }\n\n const where = conditions.length > 0 ? `WHERE ${conditions.join(\" AND \")}` : \"\";\n const rows = this.#db.db\n .prepare<(string | number)[], MemoryRow>(\n `SELECT * FROM memories ${where} ORDER BY created_at DESC`\n )\n .all(...params);\n\n return rows.map(rowToMemory);\n }\n\n stats(): { byType: Record<MemoryType, number>; total: number; needsReview: number } {\n const byType = Object.fromEntries(MEMORY_TYPE_VALUES.map((t) => [t, 0])) as Record<\n MemoryType,\n number\n >;\n\n const typeRows = this.#db.db\n .prepare<[], { type: string; count: number }>(\n `SELECT type, COUNT(*) as count FROM memories GROUP BY type`\n )\n .all();\n\n for (const row of typeRows) {\n if (row.type in byType) {\n byType[row.type as MemoryType] = row.count;\n }\n }\n\n const totals = this.#db.db\n .prepare<[], { total: number; needsReview: number }>(\n `SELECT COUNT(*) as total, SUM(needs_review) as needsReview FROM memories`\n )\n .get() ?? { total: 0, needsReview: 0 };\n\n return {\n byType,\n total: totals.total,\n needsReview: totals.needsReview ?? 0,\n };\n }\n\n setPin(id: string, pinned: boolean): Memory {\n const existing = this.#db.db\n .prepare<[string], MemoryRow>(`SELECT * FROM memories WHERE id = ?`)\n .get(id);\n\n if (existing === undefined) {\n throw new Error(`Memory not found: ${id}`);\n }\n\n const now = new Date().toISOString();\n this.#db.db\n .prepare(`UPDATE memories SET pinned = ?, updated_at = ? WHERE id = ?`)\n .run(pinned ? 1 : 0, now, id);\n\n const updated = this.#db.db\n .prepare<[string], MemoryRow>(`SELECT * FROM memories WHERE id = ?`)\n .get(id) as MemoryRow;\n\n return rowToMemory(updated);\n }\n\n incrementAccessCount(id: string): void {\n this.#db.db.prepare(`UPDATE memories SET access_count = access_count + 1 WHERE id = ?`).run(id);\n }\n}\n","import type { DatabaseManager } from \"../db/manager.js\";\nimport type { MemoryRow } from \"../db/row-types.js\";\nimport { rowToMemory } from \"../db/row-types.js\";\nimport type { EmbeddingService } from \"../embedding/service.js\";\nimport type { MemoryRepository } from \"../memory/repository.js\";\nimport type { Memory, MemoryType, QueryOptions } from \"../types.js\";\n\ninterface QueryMemoryRow extends MemoryRow {\n cosine_sim: number;\n}\n\nconst TYPE_WEIGHTS = {\n correction: 1.0,\n preference: 0.8,\n decision: 0.6,\n learning: 0.4,\n fact: 0.2,\n} satisfies Record<MemoryType, number>;\n\nexport class QueryEngine {\n readonly #db: DatabaseManager;\n readonly #embedding: EmbeddingService;\n readonly #repo: MemoryRepository;\n\n constructor(db: DatabaseManager, embeddingService: EmbeddingService, repo: MemoryRepository) {\n this.#db = db;\n this.#embedding = embeddingService;\n this.#repo = repo;\n }\n\n async query(options: QueryOptions): Promise<Array<Memory & { score: number }>> {\n const { query, type, scope, limit = 10 } = options;\n\n const queryEmbedding = await this.#embedding.embed(query);\n const queryBlob = Buffer.from(queryEmbedding.buffer);\n\n const whereClauses: string[] = [];\n const params: unknown[] = [queryBlob];\n\n if (type !== undefined) {\n whereClauses.push(\"m.type = ?\");\n params.push(type);\n }\n\n if (scope !== undefined) {\n whereClauses.push(\"m.scope = ?\");\n params.push(scope);\n }\n\n const whereSQL = whereClauses.length > 0 ? `WHERE ${whereClauses.join(\" AND \")}` : \"\";\n\n const sql = `\n SELECT m.*, (1 - vec_distance_cosine(e.embedding, ?)) AS cosine_sim\n FROM memories m JOIN embeddings e ON e.rowid = m.rowid\n ${whereSQL}\n `;\n\n const rows = this.#db.db.prepare<unknown[], QueryMemoryRow>(sql).all(...params);\n\n const now = Date.now();\n\n const scored = rows\n .filter((row) => row.cosine_sim > 0)\n .map((row) => {\n const memory = rowToMemory(row);\n const score = this.#computeScore(memory, now);\n return { ...memory, score };\n });\n\n scored.sort((a, b) => b.score - a.score);\n\n const results = scored.slice(0, limit);\n\n for (const result of results) {\n this.#repo.incrementAccessCount(result.id);\n }\n\n return results;\n }\n\n #computeScore(memory: Memory, now: number): number {\n const typeWeight = TYPE_WEIGHTS[memory.type];\n const accessCountNorm = memory.accessCount / (memory.accessCount + 10);\n const daysSinceUpdate = (now - new Date(memory.updatedAt).getTime()) / 86400000;\n const recencyNorm = 1 / (1 + daysSinceUpdate);\n const pinned = memory.pinned ? 1.0 : 0.0;\n\n return typeWeight * 0.4 + accessCountNorm * 0.3 + recencyNorm * 0.2 + pinned * 0.1;\n }\n}\n","import { execFile } from \"node:child_process\";\nimport { createHash } from \"node:crypto\";\nimport { promisify } from \"node:util\";\n\nconst execFileAsync = promisify(execFile);\n\nfunction sha256Truncated(input: string): string {\n return createHash(\"sha256\").update(input).digest(\"hex\").slice(0, 16);\n}\n\nexport async function resolveScope(): Promise<string> {\n try {\n const { stdout } = await execFileAsync(\"git\", [\"remote\", \"get-url\", \"origin\"]);\n const url = stdout.trim();\n if (url) {\n return sha256Truncated(url);\n }\n } catch {\n // git not available, not a repo, or no remote — fall through to cwd fallback\n }\n\n return sha256Truncated(process.cwd());\n}\n","import type { DatabaseManager } from \"../db/manager.js\";\nimport type { MemoryRow } from \"../db/row-types.js\";\nimport { rowToMemory } from \"../db/row-types.js\";\nimport type { MemoryType, SessionContext } from \"../types.js\";\nimport { MEMORY_TYPE_VALUES } from \"../types.js\";\n\ninterface TypeCountRow {\n type: string;\n count: number;\n}\n\nexport function listMemoryTypes(): MemoryType[] {\n return [...MEMORY_TYPE_VALUES];\n}\n\nexport class SessionContextBuilder {\n readonly #db: DatabaseManager;\n\n constructor(db: DatabaseManager) {\n this.#db = db;\n }\n\n getSessionContext(projectScope: string): SessionContext {\n const pinnedGlobal = this.#db.db\n .prepare<[], MemoryRow>(\"SELECT * FROM memories WHERE scope = 'global' AND pinned = 1\")\n .all()\n .map(rowToMemory);\n\n const pinnedProject = this.#db.db\n .prepare<[string], MemoryRow>(\"SELECT * FROM memories WHERE scope = ? AND pinned = 1\")\n .all(projectScope)\n .map(rowToMemory);\n\n const typeCounts = this.#db.db\n .prepare<[], TypeCountRow>(\"SELECT type, COUNT(*) as count FROM memories GROUP BY type\")\n .all();\n\n const stats = Object.fromEntries(MEMORY_TYPE_VALUES.map((t) => [t, 0])) as Record<\n MemoryType,\n number\n >;\n for (const row of typeCounts) {\n if (stats[row.type as MemoryType] !== undefined) {\n stats[row.type as MemoryType] = row.count;\n }\n }\n\n return { stats, pinnedGlobal, pinnedProject };\n }\n}\n"],"mappings":";;;;;;;;;;AAAA,IAAa,eAAb,cAAkC,MAAM;CACtC,YAAY,SAAiB,SAAwB;AACnD,QAAM,SAAS,QAAQ;AACvB,OAAK,OAAO;;;AAIhB,IAAa,gBAAb,cAAmC,aAAa;CAC9C,YAAY,SAAiB,SAAwB;AACnD,QAAM,SAAS,QAAQ;AACvB,OAAK,OAAO;;;;;ACHhB,MAAM,kBAAkB,KAAK,SAAS,EAAE,YAAY,YAAY;AAIhE,MAAM,aAAiC,C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AEA,YAAY,IAAqB;AAC/B,QAAA,KAAW;;CAGb,kBAAkB,cAAsC;EACtD,MAAM,eAAe,MAAA,GAAS,GAC3B,QAAuB,+DAA+D,CACtF,KAAK,CACL,IAAI,YAAY;EAEnB,MAAM,gBAAgB,MAAA,GAAS,GAC5B,QAA6B,wDAAwD,CACrF,IAAI,aAAa,CACjB,IAAI,YAAY;EAEnB,MAAM,aAAa,MAAA,GAAS,GACzB,QAA0B,6DAA6D,CACvF,KAAK;EAER,MAAM,QAAQ,OAAO,YAAY,mBAAmB,KAAK,MAAM,CAAC,GAAG,EAAE,CAAC,CAAC;AAIvE,OAAK,MAAM,OAAO,WAChB,KAAI,MAAM,IAAI,UAAwB,KAAA,EACpC,OAAM,IAAI,QAAsB,IAAI;AAIxC,SAAO;GAAE;GAAO;GAAc;GAAe"}
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["MIGRATIONS","#db","#init","#initInMemory","#runMigrations","#db","#embedding","#projects","#db","#db","#embedding","#repo","#computeScore","#db"],"sources":["../src/db/errors.ts","../src/db/manager.ts","../src/db/row-types.ts","../src/embedding/service.ts","../src/types.ts","../src/memory/repository.ts","../src/scope/resolver.ts","../src/migrations/index.ts","../src/project/repository.ts","../src/query/engine.ts","../src/session/builder.ts"],"sourcesContent":["export class MembankError extends Error {\n constructor(message: string, options?: ErrorOptions) {\n super(message, options);\n this.name = \"MembankError\";\n }\n}\n\nexport class DatabaseError extends MembankError {\n constructor(message: string, options?: ErrorOptions) {\n super(message, options);\n this.name = \"DatabaseError\";\n }\n}\n","import { mkdirSync } from \"node:fs\";\nimport { homedir } from \"node:os\";\nimport { dirname, join } from \"node:path\";\nimport BetterSqlite3 from \"better-sqlite3\";\nimport * as sqliteVec from \"sqlite-vec\";\nimport { DatabaseError } from \"./errors.js\";\n\nconst DEFAULT_DB_PATH = join(homedir(), \".membank\", \"memory.db\");\n\ntype VecLoader = (db: BetterSqlite3.Database) => void;\n\nconst MIGRATIONS: [number, string][] = [\n [\n 1,\n `\nCREATE TABLE IF NOT EXISTS memories (\n id TEXT PRIMARY KEY,\n content TEXT NOT NULL,\n type TEXT NOT NULL,\n tags TEXT NOT NULL DEFAULT '[]',\n scope TEXT NOT NULL,\n source TEXT,\n access_count INTEGER NOT NULL DEFAULT 0,\n pinned INTEGER NOT NULL DEFAULT 0,\n needs_review INTEGER NOT NULL DEFAULT 0,\n created_at TEXT NOT NULL,\n updated_at TEXT NOT NULL\n);\n\nCREATE VIRTUAL TABLE IF NOT EXISTS embeddings USING vec0(\n embedding FLOAT[384]\n);\n`,\n ],\n [\n 2,\n `\nCREATE TABLE IF NOT EXISTS projects (\n id TEXT PRIMARY KEY,\n name TEXT NOT NULL,\n scope_hash TEXT NOT NULL UNIQUE,\n created_at TEXT NOT NULL,\n updated_at TEXT NOT NULL\n);\n\nCREATE TABLE IF NOT EXISTS memory_projects (\n memory_id TEXT NOT NULL REFERENCES memories(id) ON DELETE CASCADE,\n project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,\n PRIMARY KEY (memory_id, project_id)\n);\n\nINSERT OR IGNORE INTO projects (id, name, scope_hash, created_at, updated_at)\nSELECT\n lower(hex(randomblob(16))),\n 'project-' || substr(scope, 1, 8),\n scope,\n datetime('now'),\n datetime('now')\nFROM memories\nWHERE scope != 'global'\nGROUP BY scope;\n\nINSERT OR IGNORE INTO memory_projects (memory_id, project_id)\nSELECT m.id, p.id\nFROM memories m\nJOIN projects p ON p.scope_hash = m.scope\nWHERE m.scope != 'global';\n\nALTER TABLE memories DROP COLUMN scope;\n`,\n ],\n];\n\nexport class DatabaseManager {\n readonly #db: BetterSqlite3.Database;\n\n private constructor(db: BetterSqlite3.Database) {\n this.#db = db;\n }\n\n static open(dbPath?: string): DatabaseManager {\n const resolvedPath = dbPath ?? DEFAULT_DB_PATH;\n mkdirSync(dirname(resolvedPath), { recursive: true });\n const db = new BetterSqlite3(resolvedPath);\n return DatabaseManager.#init(db, sqliteVec.load);\n }\n\n static openInMemory(): DatabaseManager {\n return DatabaseManager.#initInMemory(sqliteVec.load);\n }\n\n /** For testing: inject a custom vec loader (e.g. a throwing stub). */\n static _openInMemoryWithLoader(loader: VecLoader): DatabaseManager {\n return DatabaseManager.#initInMemory(loader);\n }\n\n static #initInMemory(loader: VecLoader): DatabaseManager {\n const db = new BetterSqlite3(\":memory:\");\n return DatabaseManager.#init(db, loader);\n }\n\n static #init(db: BetterSqlite3.Database, loader: VecLoader): DatabaseManager {\n try {\n loader(db);\n } catch (err) {\n throw new DatabaseError(\"Failed to load sqlite-vec extension\", {\n cause: err,\n });\n }\n\n db.pragma(\"journal_mode = WAL\");\n db.pragma(\"foreign_keys = ON\");\n\n const manager = new DatabaseManager(db);\n manager.#runMigrations();\n return manager;\n }\n\n #runMigrations(): void {\n // Bootstrap the meta table before reading schema_version from it\n this.#db.exec(`\n CREATE TABLE IF NOT EXISTS meta (\n key TEXT PRIMARY KEY,\n value TEXT NOT NULL\n );\n `);\n\n const row = this.#db\n .prepare<[], { value: string }>(\"SELECT value FROM meta WHERE key = 'schema_version'\")\n .get();\n\n const currentVersion = row ? Number.parseInt(row.value, 10) : 0;\n\n for (const [targetVersion, sql] of MIGRATIONS) {\n if (currentVersion < targetVersion) {\n this.#db.exec(sql);\n this.#db\n .prepare(\"INSERT OR REPLACE INTO meta (key, value) VALUES ('schema_version', ?)\")\n .run(String(targetVersion));\n }\n }\n }\n\n get db(): BetterSqlite3.Database {\n return this.#db;\n }\n\n close(): void {\n this.#db.close();\n }\n}\n","import type { Memory, MemoryType, Project } from \"../types.js\";\n\nexport interface MemoryRow {\n id: string;\n content: string;\n type: string;\n tags: string;\n source: string | null;\n access_count: number;\n pinned: number;\n needs_review: number;\n created_at: string;\n updated_at: string;\n}\n\nexport interface ProjectRow {\n id: string;\n name: string;\n scope_hash: string;\n created_at: string;\n updated_at: string;\n}\n\nexport function rowToMemory(row: MemoryRow, projects: Project[]): Memory {\n return {\n id: row.id,\n content: row.content,\n type: row.type as MemoryType,\n tags: JSON.parse(row.tags) as string[],\n projects,\n sourceHarness: row.source,\n accessCount: row.access_count,\n pinned: row.pinned !== 0,\n needsReview: row.needs_review !== 0,\n createdAt: row.created_at,\n updatedAt: row.updated_at,\n };\n}\n\nexport function rowToProject(row: ProjectRow): Project {\n return {\n id: row.id,\n name: row.name,\n scopeHash: row.scope_hash,\n createdAt: row.created_at,\n updatedAt: row.updated_at,\n };\n}\n","import { homedir } from \"node:os\";\nimport { join } from \"node:path\";\nimport { pipeline } from \"@huggingface/transformers\";\n\nexport type ProgressCallback = (progress: { status: string; progress?: number }) => void;\n\nexport class EmbeddingService {\n private readonly modelCachePath: string;\n private readonly onProgress: ProgressCallback | undefined;\n private pipelineInstance: Awaited<ReturnType<typeof pipeline>> | null = null;\n\n constructor(modelCachePath?: string, onProgress?: ProgressCallback) {\n this.modelCachePath = modelCachePath ?? join(homedir(), \".membank\", \"models\");\n this.onProgress = onProgress;\n }\n\n private async getPipeline(): Promise<Awaited<ReturnType<typeof pipeline>>> {\n if (this.pipelineInstance === null) {\n this.pipelineInstance = await pipeline(\"feature-extraction\", \"Xenova/bge-small-en-v1.5\", {\n cache_dir: this.modelCachePath,\n progress_callback: this.onProgress,\n });\n }\n return this.pipelineInstance;\n }\n\n async embed(text: string): Promise<Float32Array> {\n const pipe = await this.getPipeline();\n // Shape: [1, seq_len, 384]. Cast to any to bypass the non-unified pipeline union signature.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const output = await (\n pipe as (input: string, opts: Record<string, unknown>) => Promise<unknown>\n )(text, { pooling: \"mean\", normalize: true });\n\n // @huggingface/transformers Tensor has a .data property with the flat array\n const tensor = output as { data: Float32Array | number[] };\n const flat = tensor.data;\n\n return flat instanceof Float32Array ? flat : new Float32Array(flat);\n }\n}\n","export type MemoryType = \"correction\" | \"preference\" | \"decision\" | \"learning\" | \"fact\";\n\nexport const MEMORY_TYPE_VALUES = [\n \"correction\",\n \"preference\",\n \"decision\",\n \"learning\",\n \"fact\",\n] as const satisfies readonly MemoryType[];\n\nexport interface Project {\n id: string;\n name: string;\n scopeHash: string;\n createdAt: string;\n updatedAt: string;\n}\n\nexport interface Memory {\n id: string;\n content: string;\n type: MemoryType;\n tags: string[];\n projects: Project[];\n sourceHarness: string | null;\n accessCount: number;\n pinned: boolean;\n needsReview: boolean;\n createdAt: string;\n updatedAt: string;\n}\n\nexport interface QueryOptions {\n query: string;\n type?: MemoryType;\n projectHash?: string;\n limit?: number;\n}\n\nexport interface SaveOptions {\n content: string;\n type: MemoryType;\n tags?: string[];\n projectScope?: { hash: string; name: string };\n sourceHarness?: string;\n}\n\nexport interface SessionContext {\n stats: Record<MemoryType, number>;\n pinnedGlobal: Memory[];\n pinnedProject: Memory[];\n}\n","import { randomUUID } from \"node:crypto\";\nimport type { DatabaseManager } from \"../db/manager.js\";\nimport type { MemoryRow } from \"../db/row-types.js\";\nimport { rowToMemory } from \"../db/row-types.js\";\nimport type { EmbeddingService } from \"../embedding/service.js\";\nimport type { ProjectRepository } from \"../project/repository.js\";\nimport type { Memory, MemoryType, SaveOptions } from \"../types.js\";\nimport { MEMORY_TYPE_VALUES } from \"../types.js\";\n\ninterface SimilarityRow extends MemoryRow {\n rowid: number;\n similarity: number;\n}\n\nexport class MemoryRepository {\n readonly #db: DatabaseManager;\n readonly #embedding: EmbeddingService;\n readonly #projects: ProjectRepository;\n\n constructor(\n db: DatabaseManager,\n embeddingService: EmbeddingService,\n projects: ProjectRepository\n ) {\n this.#db = db;\n this.#embedding = embeddingService;\n this.#projects = projects;\n }\n\n async save(options: SaveOptions): Promise<Memory> {\n const { content, type, tags = [], projectScope, sourceHarness } = options;\n\n const embedding = await this.#embedding.embed(content);\n const embeddingBlob = Buffer.from(embedding.buffer);\n\n // Dedup: find similar memory in same context\n let top: SimilarityRow | undefined;\n if (projectScope !== undefined) {\n top = this.#db.db\n .prepare<[Buffer, string, string], SimilarityRow>(\n `SELECT m.rowid, m.*, (1 - vec_distance_cosine(e.embedding, ?)) AS similarity\n FROM memories m JOIN embeddings e ON e.rowid = m.rowid\n JOIN memory_projects mp ON mp.memory_id = m.id\n JOIN projects p ON p.id = mp.project_id\n WHERE m.type = ? AND p.scope_hash = ?\n ORDER BY similarity DESC LIMIT 1`\n )\n .get(embeddingBlob, type, projectScope.hash);\n } else {\n top = this.#db.db\n .prepare<[Buffer, string], SimilarityRow>(\n `SELECT m.rowid, m.*, (1 - vec_distance_cosine(e.embedding, ?)) AS similarity\n FROM memories m JOIN embeddings e ON e.rowid = m.rowid\n WHERE m.type = ?\n AND m.id NOT IN (SELECT memory_id FROM memory_projects)\n ORDER BY similarity DESC LIMIT 1`\n )\n .get(embeddingBlob, type);\n }\n\n const now = new Date().toISOString();\n\n if (top !== undefined && top.similarity > 0.92) {\n this.#db.db\n .prepare(`UPDATE memories SET content = ?, updated_at = ? WHERE id = ?`)\n .run(content, now, top.id);\n this.#db.db\n .prepare(`UPDATE embeddings SET embedding = ? WHERE rowid = ?`)\n .run(embeddingBlob, top.rowid);\n\n const updated = this.#db.db\n .prepare<[string], MemoryRow>(`SELECT * FROM memories WHERE id = ?`)\n .get(top.id) as MemoryRow;\n\n const projectMap = this.#projects.getProjectsForMemories([top.id]);\n return rowToMemory(updated, projectMap.get(top.id) ?? []);\n }\n\n if (top !== undefined && top.similarity >= 0.75) {\n this.#db.db.prepare(`UPDATE memories SET needs_review = 1 WHERE id = ?`).run(top.id);\n }\n\n const id = randomUUID();\n this.#db.db\n .prepare(\n `INSERT INTO memories (id, content, type, tags, source, access_count, pinned, needs_review, created_at, updated_at)\n VALUES (?, ?, ?, ?, ?, 0, 0, 0, ?, ?)`\n )\n .run(id, content, type, JSON.stringify(tags), sourceHarness ?? null, now, now);\n\n this.#db.db\n .prepare(\n `INSERT INTO embeddings (rowid, embedding) SELECT m.rowid, ? FROM memories m WHERE m.id = ?`\n )\n .run(embeddingBlob, id);\n\n if (projectScope !== undefined) {\n const project = this.#projects.upsertByHash(projectScope.hash, projectScope.name);\n this.#projects.addAssociation(id, project.id);\n }\n\n const row = this.#db.db\n .prepare<[string], MemoryRow>(`SELECT * FROM memories WHERE id = ?`)\n .get(id) as MemoryRow;\n\n const projectMap = this.#projects.getProjectsForMemories([id]);\n return rowToMemory(row, projectMap.get(id) ?? []);\n }\n\n async update(id: string, patch: { content?: string; tags?: string[] }): Promise<Memory> {\n const existing = this.#db.db\n .prepare<[string], MemoryRow & { rowid: number }>(\n `SELECT m.rowid, m.* FROM memories m WHERE m.id = ?`\n )\n .get(id);\n\n if (existing === undefined) {\n throw new Error(`Memory not found: ${id}`);\n }\n\n const now = new Date().toISOString();\n const sets: string[] = [\"updated_at = ?\"];\n const values: string[] = [now];\n\n if (patch.content !== undefined) {\n sets.push(\"content = ?\");\n values.push(patch.content);\n }\n\n if (patch.tags !== undefined) {\n sets.push(\"tags = ?\");\n values.push(JSON.stringify(patch.tags));\n }\n\n values.push(id);\n this.#db.db.prepare(`UPDATE memories SET ${sets.join(\", \")} WHERE id = ?`).run(...values);\n\n if (patch.content !== undefined) {\n const embedding = await this.#embedding.embed(patch.content);\n const embeddingBlob = Buffer.from(embedding.buffer);\n this.#db.db\n .prepare(`UPDATE embeddings SET embedding = ? WHERE rowid = ?`)\n .run(embeddingBlob, existing.rowid);\n }\n\n const updated = this.#db.db\n .prepare<[string], MemoryRow>(`SELECT * FROM memories WHERE id = ?`)\n .get(id) as MemoryRow;\n\n const projectMap = this.#projects.getProjectsForMemories([id]);\n return rowToMemory(updated, projectMap.get(id) ?? []);\n }\n\n delete(id: string): Promise<void> {\n const row = this.#db.db\n .prepare<[string], { rowid: number }>(`SELECT rowid FROM memories WHERE id = ?`)\n .get(id);\n\n if (row !== undefined) {\n this.#db.db.prepare(`DELETE FROM embeddings WHERE rowid = ?`).run(row.rowid);\n }\n\n this.#db.db.prepare(`DELETE FROM memory_projects WHERE memory_id = ?`).run(id);\n this.#db.db.prepare(`DELETE FROM memories WHERE id = ?`).run(id);\n\n return Promise.resolve();\n }\n\n list(opts?: { type?: MemoryType; pinned?: boolean }): Memory[] {\n const conditions: string[] = [];\n const params: (string | number)[] = [];\n\n if (opts?.type !== undefined) {\n conditions.push(\"type = ?\");\n params.push(opts.type);\n }\n\n if (opts?.pinned === true) {\n conditions.push(\"pinned = 1\");\n }\n\n const where = conditions.length > 0 ? `WHERE ${conditions.join(\" AND \")}` : \"\";\n const rows = this.#db.db\n .prepare<(string | number)[], MemoryRow>(\n `SELECT * FROM memories ${where} ORDER BY created_at DESC`\n )\n .all(...params);\n\n if (rows.length === 0) return [];\n\n const ids = rows.map((r) => r.id);\n const projectMap = this.#projects.getProjectsForMemories(ids);\n return rows.map((row) => rowToMemory(row, projectMap.get(row.id) ?? []));\n }\n\n stats(): { byType: Record<MemoryType, number>; total: number; needsReview: number } {\n const byType = Object.fromEntries(MEMORY_TYPE_VALUES.map((t) => [t, 0])) as Record<\n MemoryType,\n number\n >;\n\n const typeRows = this.#db.db\n .prepare<[], { type: string; count: number }>(\n `SELECT type, COUNT(*) as count FROM memories GROUP BY type`\n )\n .all();\n\n for (const row of typeRows) {\n if (row.type in byType) {\n byType[row.type as MemoryType] = row.count;\n }\n }\n\n const totals = this.#db.db\n .prepare<[], { total: number; needsReview: number }>(\n `SELECT COUNT(*) as total, SUM(needs_review) as needsReview FROM memories`\n )\n .get() ?? { total: 0, needsReview: 0 };\n\n return {\n byType,\n total: totals.total,\n needsReview: totals.needsReview ?? 0,\n };\n }\n\n setPin(id: string, pinned: boolean): Memory {\n const existing = this.#db.db\n .prepare<[string], MemoryRow>(`SELECT * FROM memories WHERE id = ?`)\n .get(id);\n\n if (existing === undefined) {\n throw new Error(`Memory not found: ${id}`);\n }\n\n const now = new Date().toISOString();\n this.#db.db\n .prepare(`UPDATE memories SET pinned = ?, updated_at = ? WHERE id = ?`)\n .run(pinned ? 1 : 0, now, id);\n\n const updated = this.#db.db\n .prepare<[string], MemoryRow>(`SELECT * FROM memories WHERE id = ?`)\n .get(id) as MemoryRow;\n\n const projectMap = this.#projects.getProjectsForMemories([id]);\n return rowToMemory(updated, projectMap.get(id) ?? []);\n }\n\n incrementAccessCount(id: string): void {\n this.#db.db.prepare(`UPDATE memories SET access_count = access_count + 1 WHERE id = ?`).run(id);\n }\n}\n","import { execFile } from \"node:child_process\";\nimport { createHash } from \"node:crypto\";\nimport { promisify } from \"node:util\";\n\nconst execFileAsync = promisify(execFile);\n\nfunction sha256Truncated(input: string): string {\n return createHash(\"sha256\").update(input).digest(\"hex\").slice(0, 16);\n}\n\nexport async function resolveProject(): Promise<{ hash: string; name: string }> {\n try {\n const { stdout } = await execFileAsync(\"git\", [\"remote\", \"get-url\", \"origin\"]);\n const url = stdout.trim();\n if (url) {\n const hash = sha256Truncated(url);\n // parse last path segment, strip .git suffix\n const name =\n url\n .split(\"/\")\n .pop()\n ?.replace(/\\.git$/, \"\") ?? hash.slice(0, 8);\n return { hash, name };\n }\n } catch {\n // fall through\n }\n\n const cwd = process.cwd();\n const hash = sha256Truncated(cwd);\n const name = cwd.split(/[/\\\\]/).filter(Boolean).pop() ?? hash.slice(0, 8);\n return { hash, name };\n}\n\nexport async function resolveScope(): Promise<string> {\n try {\n const { stdout } = await execFileAsync(\"git\", [\"remote\", \"get-url\", \"origin\"]);\n const url = stdout.trim();\n if (url) {\n return sha256Truncated(url);\n }\n } catch {\n // git not available, not a repo, or no remote — fall through to cwd fallback\n }\n\n return sha256Truncated(process.cwd());\n}\n","import type { ProjectRepository } from \"../project/index.js\";\nimport { resolveProject } from \"../scope/index.js\";\n\nexport interface MigrationMeta {\n name: string;\n description: string;\n}\n\nexport interface ScopeToProjectsResult {\n migration: \"scope-to-projects\";\n oldName: string;\n newName: string;\n memoryCount: number;\n}\n\nexport const MIGRATIONS: MigrationMeta[] = [\n {\n name: \"scope-to-projects\",\n description:\n \"Rename the auto-migrated project for the current directory from its generic hash-derived name to the resolved repo/directory name.\",\n },\n];\n\nexport async function runScopeToProjectsMigration(\n projects: ProjectRepository\n): Promise<ScopeToProjectsResult | null> {\n const resolved = await resolveProject();\n const project = projects.getByHash(resolved.hash);\n\n if (project === undefined) {\n return null;\n }\n\n const oldName = project.name;\n const memoryCount = projects.countMemories(project.id);\n projects.rename(project.id, resolved.name);\n\n return {\n migration: \"scope-to-projects\",\n oldName,\n newName: resolved.name,\n memoryCount,\n };\n}\n","import { randomUUID } from \"node:crypto\";\nimport type { DatabaseManager } from \"../db/manager.js\";\nimport type { ProjectRow } from \"../db/row-types.js\";\nimport { rowToProject } from \"../db/row-types.js\";\nimport type { Project } from \"../types.js\";\n\ninterface ProjectMemoryRow extends ProjectRow {\n memory_id: string;\n}\n\nexport class ProjectRepository {\n readonly #db: DatabaseManager;\n\n constructor(db: DatabaseManager) {\n this.#db = db;\n }\n\n upsertByHash(hash: string, name: string): Project {\n const now = new Date().toISOString();\n const id = randomUUID();\n this.#db.db\n .prepare(\n `INSERT OR IGNORE INTO projects (id, name, scope_hash, created_at, updated_at) VALUES (?, ?, ?, ?, ?)`\n )\n .run(id, name, hash, now, now);\n\n const row = this.#db.db\n .prepare<[string], ProjectRow>(`SELECT * FROM projects WHERE scope_hash = ?`)\n .get(hash) as ProjectRow;\n\n return rowToProject(row);\n }\n\n rename(id: string, name: string): Project {\n const now = new Date().toISOString();\n this.#db.db\n .prepare(`UPDATE projects SET name = ?, updated_at = ? WHERE id = ?`)\n .run(name, now, id);\n\n const row = this.#db.db\n .prepare<[string], ProjectRow>(`SELECT * FROM projects WHERE id = ?`)\n .get(id);\n\n if (row === undefined) throw new Error(`Project not found: ${id}`);\n return rowToProject(row);\n }\n\n list(): Project[] {\n return this.#db.db\n .prepare<[], ProjectRow>(`SELECT * FROM projects ORDER BY name ASC`)\n .all()\n .map(rowToProject);\n }\n\n getByHash(hash: string): Project | undefined {\n const row = this.#db.db\n .prepare<[string], ProjectRow>(`SELECT * FROM projects WHERE scope_hash = ?`)\n .get(hash);\n return row !== undefined ? rowToProject(row) : undefined;\n }\n\n addAssociation(memoryId: string, projectId: string): void {\n this.#db.db\n .prepare(`INSERT OR IGNORE INTO memory_projects (memory_id, project_id) VALUES (?, ?)`)\n .run(memoryId, projectId);\n }\n\n removeAssociation(memoryId: string, projectId: string): void {\n this.#db.db\n .prepare(`DELETE FROM memory_projects WHERE memory_id = ? AND project_id = ?`)\n .run(memoryId, projectId);\n }\n\n countMemories(projectId: string): number {\n const row = this.#db.db\n .prepare<[string], { count: number }>(\n `SELECT COUNT(*) AS count FROM memory_projects WHERE project_id = ?`\n )\n .get(projectId);\n return row?.count ?? 0;\n }\n\n getProjectsForMemories(memoryIds: string[]): Map<string, Project[]> {\n if (memoryIds.length === 0) return new Map();\n const placeholders = memoryIds.map(() => \"?\").join(\",\");\n const rows = this.#db.db\n .prepare<string[], ProjectMemoryRow>(\n `SELECT p.*, mp.memory_id FROM projects p\n JOIN memory_projects mp ON mp.project_id = p.id\n WHERE mp.memory_id IN (${placeholders})`\n )\n .all(...memoryIds);\n\n const result = new Map<string, Project[]>();\n for (const row of rows) {\n const list = result.get(row.memory_id) ?? [];\n list.push(rowToProject(row));\n result.set(row.memory_id, list);\n }\n return result;\n }\n}\n","import type { DatabaseManager } from \"../db/manager.js\";\nimport type { MemoryRow } from \"../db/row-types.js\";\nimport { rowToMemory } from \"../db/row-types.js\";\nimport type { EmbeddingService } from \"../embedding/service.js\";\nimport type { MemoryRepository } from \"../memory/repository.js\";\nimport type { Memory, MemoryType, QueryOptions } from \"../types.js\";\n\ninterface QueryMemoryRow extends MemoryRow {\n cosine_sim: number;\n}\n\nconst TYPE_WEIGHTS = {\n correction: 1.0,\n preference: 0.8,\n decision: 0.6,\n learning: 0.4,\n fact: 0.2,\n} satisfies Record<MemoryType, number>;\n\nexport class QueryEngine {\n readonly #db: DatabaseManager;\n readonly #embedding: EmbeddingService;\n readonly #repo: MemoryRepository;\n\n constructor(db: DatabaseManager, embeddingService: EmbeddingService, repo: MemoryRepository) {\n this.#db = db;\n this.#embedding = embeddingService;\n this.#repo = repo;\n }\n\n async query(options: QueryOptions): Promise<Array<Memory & { score: number }>> {\n const { query, type, projectHash, limit = 10 } = options;\n\n const queryEmbedding = await this.#embedding.embed(query);\n const queryBlob = Buffer.from(queryEmbedding.buffer);\n\n const whereClauses: string[] = [];\n const params: unknown[] = [queryBlob];\n let joinClause = \"\";\n\n if (type !== undefined) {\n whereClauses.push(\"m.type = ?\");\n params.push(type);\n }\n\n if (projectHash !== undefined) {\n joinClause =\n \"LEFT JOIN memory_projects mp ON mp.memory_id = m.id LEFT JOIN projects p ON p.id = mp.project_id\";\n whereClauses.push(\"p.scope_hash = ?\");\n params.push(projectHash);\n }\n\n const whereSQL = whereClauses.length > 0 ? `WHERE ${whereClauses.join(\" AND \")}` : \"\";\n\n const sql = `\n SELECT m.*, (1 - vec_distance_cosine(e.embedding, ?)) AS cosine_sim\n FROM memories m JOIN embeddings e ON e.rowid = m.rowid\n ${joinClause}\n ${whereSQL}\n `;\n\n const rows = this.#db.db.prepare<unknown[], QueryMemoryRow>(sql).all(...params);\n\n const now = Date.now();\n\n const scored = rows\n .filter((row) => row.cosine_sim > 0)\n .map((row) => {\n const memory = rowToMemory(row, []);\n const score = this.#computeScore(memory, now);\n return { ...memory, score };\n });\n\n scored.sort((a, b) => b.score - a.score);\n\n const results = scored.slice(0, limit);\n\n for (const result of results) {\n this.#repo.incrementAccessCount(result.id);\n }\n\n return results;\n }\n\n #computeScore(memory: Memory, now: number): number {\n const typeWeight = TYPE_WEIGHTS[memory.type];\n const accessCountNorm = memory.accessCount / (memory.accessCount + 10);\n const daysSinceUpdate = (now - new Date(memory.updatedAt).getTime()) / 86400000;\n const recencyNorm = 1 / (1 + daysSinceUpdate);\n const pinned = memory.pinned ? 1.0 : 0.0;\n\n return typeWeight * 0.4 + accessCountNorm * 0.3 + recencyNorm * 0.2 + pinned * 0.1;\n }\n}\n","import type { DatabaseManager } from \"../db/manager.js\";\nimport type { MemoryRow } from \"../db/row-types.js\";\nimport { rowToMemory } from \"../db/row-types.js\";\nimport type { MemoryType, SessionContext } from \"../types.js\";\nimport { MEMORY_TYPE_VALUES } from \"../types.js\";\n\ninterface TypeCountRow {\n type: string;\n count: number;\n}\n\nexport function listMemoryTypes(): MemoryType[] {\n return [...MEMORY_TYPE_VALUES];\n}\n\nexport class SessionContextBuilder {\n readonly #db: DatabaseManager;\n\n constructor(db: DatabaseManager) {\n this.#db = db;\n }\n\n getSessionContext(projectHash: string): SessionContext {\n const pinnedGlobal = this.#db.db\n .prepare<[], MemoryRow>(\n `SELECT * FROM memories\n WHERE id NOT IN (SELECT memory_id FROM memory_projects)\n AND pinned = 1`\n )\n .all()\n .map((row) => rowToMemory(row, []));\n\n const pinnedProject = this.#db.db\n .prepare<[string], MemoryRow>(\n `SELECT m.* FROM memories m\n JOIN memory_projects mp ON mp.memory_id = m.id\n JOIN projects p ON p.id = mp.project_id\n WHERE p.scope_hash = ? AND m.pinned = 1`\n )\n .all(projectHash)\n .map((row) => rowToMemory(row, []));\n\n const typeCounts = this.#db.db\n .prepare<[], TypeCountRow>(\"SELECT type, COUNT(*) as count FROM memories GROUP BY type\")\n .all();\n\n const stats = Object.fromEntries(MEMORY_TYPE_VALUES.map((t) => [t, 0])) as Record<\n MemoryType,\n number\n >;\n for (const row of typeCounts) {\n if (stats[row.type as MemoryType] !== undefined) {\n stats[row.type as MemoryType] = row.count;\n }\n }\n\n return { stats, pinnedGlobal, pinnedProject };\n }\n}\n"],"mappings":";;;;;;;;;;AAAA,IAAa,eAAb,cAAkC,MAAM;CACtC,YAAY,SAAiB,SAAwB;AACnD,QAAM,SAAS,QAAQ;AACvB,OAAK,OAAO;;;AAIhB,IAAa,gBAAb,cAAmC,aAAa;CAC9C,YAAY,SAAiB,SAAwB;AACnD,QAAM,SAAS,QAAQ;AACvB,OAAK,OAAO;;;;;ACHhB,MAAM,kBAAkB,KAAK,SAAS,EAAE,YAAY,YAAY;AAIhE,MAAMA,eAAiC,CACrC,CACE,GACA;;;;;;;;;;;;;;;;;;EAmBD,EACD,CACE,GACA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAkCD,CACF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