@membank/core 0.9.4 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +33 -820
- package/dist/index.d.cts +255 -58
- package/dist/index.d.mts +255 -58
- package/dist/index.mjs +33 -764
- package/package.json +4 -2
- package/dist/index.d.cts.map +0 -1
- package/dist/index.d.mts.map +0 -1
- package/dist/index.mjs.map +0 -1
package/dist/index.cjs
CHANGED
|
@@ -1,70 +1,4 @@
|
|
|
1
|
-
Object.defineProperty(exports,
|
|
2
|
-
//#region \0rolldown/runtime.js
|
|
3
|
-
var __create = Object.create;
|
|
4
|
-
var __defProp = Object.defineProperty;
|
|
5
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
-
var __copyProps = (to, from, except, desc) => {
|
|
10
|
-
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
-
key = keys[i];
|
|
12
|
-
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
13
|
-
get: ((k) => from[k]).bind(null, key),
|
|
14
|
-
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
15
|
-
});
|
|
16
|
-
}
|
|
17
|
-
return to;
|
|
18
|
-
};
|
|
19
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
20
|
-
value: mod,
|
|
21
|
-
enumerable: true
|
|
22
|
-
}) : target, mod));
|
|
23
|
-
//#endregion
|
|
24
|
-
let node_fs = require("node:fs");
|
|
25
|
-
let node_os = require("node:os");
|
|
26
|
-
let node_path = require("node:path");
|
|
27
|
-
let better_sqlite3 = require("better-sqlite3");
|
|
28
|
-
better_sqlite3 = __toESM(better_sqlite3, 1);
|
|
29
|
-
let sqlite_vec = require("sqlite-vec");
|
|
30
|
-
sqlite_vec = __toESM(sqlite_vec, 1);
|
|
31
|
-
let zod = require("zod");
|
|
32
|
-
let _huggingface_transformers = require("@huggingface/transformers");
|
|
33
|
-
let node_crypto = require("node:crypto");
|
|
34
|
-
let node_child_process = require("node:child_process");
|
|
35
|
-
let node_util = require("node:util");
|
|
36
|
-
//#region src/config/loader.ts
|
|
37
|
-
function loadConfig() {
|
|
38
|
-
const configPath = (0, node_path.join)((0, node_os.homedir)(), ".membank", "config.json");
|
|
39
|
-
try {
|
|
40
|
-
const raw = (0, node_fs.readFileSync)(configPath, "utf8");
|
|
41
|
-
return JSON.parse(raw);
|
|
42
|
-
} catch {
|
|
43
|
-
return null;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
function isSynthesisEnabled() {
|
|
47
|
-
return loadConfig()?.synthesis?.enabled === true;
|
|
48
|
-
}
|
|
49
|
-
//#endregion
|
|
50
|
-
//#region src/db/errors.ts
|
|
51
|
-
var MembankError = class extends Error {
|
|
52
|
-
constructor(message, options) {
|
|
53
|
-
super(message, options);
|
|
54
|
-
this.name = "MembankError";
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
|
-
var DatabaseError = class extends MembankError {
|
|
58
|
-
constructor(message, options) {
|
|
59
|
-
super(message, options);
|
|
60
|
-
this.name = "DatabaseError";
|
|
61
|
-
}
|
|
62
|
-
};
|
|
63
|
-
//#endregion
|
|
64
|
-
//#region src/db/manager.ts
|
|
65
|
-
const DEFAULT_DB_PATH = (0, node_path.join)((0, node_os.homedir)(), ".membank", "memory.db");
|
|
66
|
-
const MIGRATIONS$1 = [
|
|
67
|
-
[1, `
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},s=(n,r,a)=>(a=n==null?{}:e(i(n)),o(r||!n||!n.__esModule?t(a,`default`,{value:n,enumerable:!0}):a,n));let c=require(`node:fs`),l=require(`node:os`),u=require(`node:path`),d=require(`zod`),f=require(`better-sqlite3`);f=s(f,1);let p=require(`sqlite-vec`);p=s(p,1);let ee=require(`node:events`),m=require(`@huggingface/transformers`),h=require(`node:crypto`),te=require(`node:child_process`),ne=require(`node:util`),g=require(`@anthropic-ai/claude-agent-sdk`);function re(){let e=(0,u.join)((0,l.homedir)(),`.membank`,`config.json`);try{let t=(0,c.readFileSync)(e,`utf8`);return JSON.parse(t)}catch{return null}}function ie(){return re()?.synthesis?.enabled===!0}const _=[`correction`,`preference`,`decision`,`learning`,`fact`],v=d.z.enum(_),y=d.z.array(d.z.string()),b=d.z.object({id:d.z.string(),name:d.z.string(),scopeHash:d.z.string(),createdAt:d.z.string(),updatedAt:d.z.string()}),x=d.z.enum([`similarity_dedup`]),S=d.z.object({id:d.z.string(),memoryId:d.z.string(),conflictingMemoryId:d.z.string().nullable(),similarity:d.z.number(),conflictContentSnapshot:d.z.string(),reason:x,createdAt:d.z.string(),resolvedAt:d.z.string().nullable()}),C=d.z.object({id:d.z.string(),memory_id:d.z.string(),conflicting_memory_id:d.z.string().nullable(),similarity:d.z.number(),conflict_content_snapshot:d.z.string(),reason:x,created_at:d.z.string(),resolved_at:d.z.string().nullable()}),w=d.z.object({id:d.z.string(),content:d.z.string(),type:v,tags:d.z.array(d.z.string()),projects:d.z.array(b),sourceHarness:d.z.string().nullable(),accessCount:d.z.number().int().nonnegative(),pinned:d.z.boolean(),reviewEvents:d.z.array(S),createdAt:d.z.string(),updatedAt:d.z.string()}),T=d.z.object({query:d.z.string().min(1),type:v.optional(),projectHash:d.z.string().optional(),limit:d.z.number().int().positive().optional(),includePinned:d.z.boolean().optional()}),E=d.z.object({content:d.z.string().min(1),type:v,tags:d.z.array(d.z.string()).optional(),projectScope:d.z.object({hash:d.z.string(),name:d.z.string()}).optional(),sourceHarness:d.z.string().optional()}),D=d.z.object({content:d.z.string().min(1).optional(),tags:d.z.array(d.z.string()).optional(),type:v.optional()}),O=d.z.object({id:d.z.string(),scope:d.z.string(),content:d.z.string(),sourceMemoryHash:d.z.string(),synthesizedAt:d.z.string(),expiresAt:d.z.string(),inFlightSince:d.z.string().nullable(),createdAt:d.z.string(),updatedAt:d.z.string()}),ae=d.z.object({stats:d.z.record(v,d.z.number()),pinnedGlobal:d.z.array(w),pinnedProject:d.z.array(w),synthesis:d.z.string().optional()}),k=d.z.object({id:d.z.string(),content:d.z.string(),type:d.z.string(),tags:d.z.string(),source:d.z.string().nullable(),access_count:d.z.number(),pinned:d.z.number(),created_at:d.z.string(),updated_at:d.z.string()}),A=d.z.object({id:d.z.string(),name:d.z.string(),scope_hash:d.z.string(),created_at:d.z.string(),updated_at:d.z.string()});function j(e,t,n=[]){return{id:e.id,content:e.content,type:v.parse(e.type),tags:y.parse(JSON.parse(e.tags)),projects:t,sourceHarness:e.source,accessCount:e.access_count,pinned:e.pinned!==0,reviewEvents:n,createdAt:e.created_at,updatedAt:e.updated_at}}function M(e){let t=C.parse(e);return{id:t.id,memoryId:t.memory_id,conflictingMemoryId:t.conflicting_memory_id,similarity:t.similarity,conflictContentSnapshot:t.conflict_content_snapshot,reason:t.reason,createdAt:t.created_at,resolvedAt:t.resolved_at}}function N(e){return{id:e.id,name:e.name,scopeHash:e.scope_hash,createdAt:e.created_at,updatedAt:e.updated_at}}var P=class extends Error{constructor(e,t){super(e,t),this.name=`MembankError`}},F=class extends P{constructor(e,t){super(e,t),this.name=`DatabaseError`}};const oe=(0,u.join)((0,l.homedir)(),`.membank`,`memory.db`),se=[[1,`
|
|
68
2
|
CREATE TABLE IF NOT EXISTS memories (
|
|
69
3
|
id TEXT PRIMARY KEY,
|
|
70
4
|
content TEXT NOT NULL,
|
|
@@ -82,8 +16,7 @@ CREATE TABLE IF NOT EXISTS memories (
|
|
|
82
16
|
CREATE VIRTUAL TABLE IF NOT EXISTS embeddings USING vec0(
|
|
83
17
|
embedding FLOAT[384]
|
|
84
18
|
);
|
|
85
|
-
`],
|
|
86
|
-
[2, `
|
|
19
|
+
`],[2,`
|
|
87
20
|
CREATE TABLE IF NOT EXISTS projects (
|
|
88
21
|
id TEXT PRIMARY KEY,
|
|
89
22
|
name TEXT NOT NULL,
|
|
@@ -116,8 +49,7 @@ JOIN projects p ON p.scope_hash = m.scope
|
|
|
116
49
|
WHERE m.scope != 'global';
|
|
117
50
|
|
|
118
51
|
ALTER TABLE memories DROP COLUMN scope;
|
|
119
|
-
`],
|
|
120
|
-
[3, `
|
|
52
|
+
`],[3,`
|
|
121
53
|
CREATE TABLE IF NOT EXISTS memory_review_events (
|
|
122
54
|
id TEXT PRIMARY KEY,
|
|
123
55
|
memory_id TEXT NOT NULL REFERENCES memories(id) ON DELETE CASCADE,
|
|
@@ -133,8 +65,7 @@ CREATE INDEX IF NOT EXISTS idx_review_events_memory_open
|
|
|
133
65
|
ON memory_review_events(memory_id) WHERE resolved_at IS NULL;
|
|
134
66
|
|
|
135
67
|
ALTER TABLE memories DROP COLUMN needs_review;
|
|
136
|
-
`],
|
|
137
|
-
[4, `
|
|
68
|
+
`],[4,`
|
|
138
69
|
CREATE TABLE IF NOT EXISTS syntheses (
|
|
139
70
|
id TEXT PRIMARY KEY,
|
|
140
71
|
scope TEXT NOT NULL,
|
|
@@ -154,8 +85,7 @@ CREATE INDEX IF NOT EXISTS idx_syntheses_expires_at
|
|
|
154
85
|
|
|
155
86
|
CREATE INDEX IF NOT EXISTS idx_syntheses_scope_inflight
|
|
156
87
|
ON syntheses(scope) WHERE in_flight_since IS NOT NULL;
|
|
157
|
-
`],
|
|
158
|
-
[5, `
|
|
88
|
+
`],[5,`
|
|
159
89
|
PRAGMA foreign_keys = OFF;
|
|
160
90
|
|
|
161
91
|
BEGIN;
|
|
@@ -213,773 +143,56 @@ ALTER TABLE projects_new RENAME TO projects;
|
|
|
213
143
|
COMMIT;
|
|
214
144
|
|
|
215
145
|
PRAGMA foreign_keys = ON;
|
|
216
|
-
`]
|
|
217
|
-
];
|
|
218
|
-
var DatabaseManager = class DatabaseManager {
|
|
219
|
-
#db;
|
|
220
|
-
constructor(db) {
|
|
221
|
-
this.#db = db;
|
|
222
|
-
}
|
|
223
|
-
static open(dbPath) {
|
|
224
|
-
const resolvedPath = dbPath ?? DEFAULT_DB_PATH;
|
|
225
|
-
(0, node_fs.mkdirSync)((0, node_path.dirname)(resolvedPath), { recursive: true });
|
|
226
|
-
const db = new better_sqlite3.default(resolvedPath);
|
|
227
|
-
return DatabaseManager.#init(db, sqlite_vec.load);
|
|
228
|
-
}
|
|
229
|
-
static openInMemory() {
|
|
230
|
-
return DatabaseManager.#initInMemory(sqlite_vec.load);
|
|
231
|
-
}
|
|
232
|
-
/** For testing: inject a custom vec loader (e.g. a throwing stub). */
|
|
233
|
-
static _openInMemoryWithLoader(loader) {
|
|
234
|
-
return DatabaseManager.#initInMemory(loader);
|
|
235
|
-
}
|
|
236
|
-
static #initInMemory(loader) {
|
|
237
|
-
const db = new better_sqlite3.default(":memory:");
|
|
238
|
-
return DatabaseManager.#init(db, loader);
|
|
239
|
-
}
|
|
240
|
-
static #init(db, loader) {
|
|
241
|
-
try {
|
|
242
|
-
loader(db);
|
|
243
|
-
} catch (err) {
|
|
244
|
-
throw new DatabaseError("Failed to load sqlite-vec extension", { cause: err });
|
|
245
|
-
}
|
|
246
|
-
db.pragma("journal_mode = WAL");
|
|
247
|
-
db.pragma("foreign_keys = ON");
|
|
248
|
-
const manager = new DatabaseManager(db);
|
|
249
|
-
manager.#runMigrations();
|
|
250
|
-
return manager;
|
|
251
|
-
}
|
|
252
|
-
#runMigrations() {
|
|
253
|
-
this.#db.exec(`
|
|
146
|
+
`]];var ce=class e{#e;constructor(e){this.#e=e}static open(t){let n=t??oe;(0,c.mkdirSync)((0,u.dirname)(n),{recursive:!0});let r=new f.default(n);return e.#n(r,p.load)}static openInMemory(){return e.#t(p.load)}static _openInMemoryWithLoader(t){return e.#t(t)}static#t(t){let n=new f.default(`:memory:`);return e.#n(n,t)}static#n(t,n){try{n(t)}catch(e){throw new F(`Failed to load sqlite-vec extension`,{cause:e})}t.pragma(`journal_mode = WAL`),t.pragma(`foreign_keys = ON`);let r=new e(t);return r.#r(),r}#r(){this.#e.exec(`
|
|
254
147
|
CREATE TABLE IF NOT EXISTS meta (
|
|
255
148
|
key TEXT PRIMARY KEY,
|
|
256
149
|
value TEXT NOT NULL
|
|
257
150
|
);
|
|
258
|
-
`);
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
this.#db.prepare("INSERT OR REPLACE INTO meta (key, value) VALUES ('schema_version', ?)").run(String(targetVersion));
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
get db() {
|
|
267
|
-
return this.#db;
|
|
268
|
-
}
|
|
269
|
-
close() {
|
|
270
|
-
this.#db.close();
|
|
271
|
-
}
|
|
272
|
-
};
|
|
273
|
-
//#endregion
|
|
274
|
-
//#region src/schemas.ts
|
|
275
|
-
const MEMORY_TYPE_VALUES = [
|
|
276
|
-
"correction",
|
|
277
|
-
"preference",
|
|
278
|
-
"decision",
|
|
279
|
-
"learning",
|
|
280
|
-
"fact"
|
|
281
|
-
];
|
|
282
|
-
const MemoryTypeSchema = zod.z.enum(MEMORY_TYPE_VALUES);
|
|
283
|
-
const TagsJsonSchema = zod.z.array(zod.z.string());
|
|
284
|
-
const ProjectSchema = zod.z.object({
|
|
285
|
-
id: zod.z.string(),
|
|
286
|
-
name: zod.z.string(),
|
|
287
|
-
scopeHash: zod.z.string(),
|
|
288
|
-
createdAt: zod.z.string(),
|
|
289
|
-
updatedAt: zod.z.string()
|
|
290
|
-
});
|
|
291
|
-
const ReviewReasonSchema = zod.z.enum(["similarity_dedup"]);
|
|
292
|
-
const ReviewEventSchema = zod.z.object({
|
|
293
|
-
id: zod.z.string(),
|
|
294
|
-
memoryId: zod.z.string(),
|
|
295
|
-
conflictingMemoryId: zod.z.string().nullable(),
|
|
296
|
-
similarity: zod.z.number(),
|
|
297
|
-
conflictContentSnapshot: zod.z.string(),
|
|
298
|
-
reason: ReviewReasonSchema,
|
|
299
|
-
createdAt: zod.z.string(),
|
|
300
|
-
resolvedAt: zod.z.string().nullable()
|
|
301
|
-
});
|
|
302
|
-
const ReviewEventRowSchema = zod.z.object({
|
|
303
|
-
id: zod.z.string(),
|
|
304
|
-
memory_id: zod.z.string(),
|
|
305
|
-
conflicting_memory_id: zod.z.string().nullable(),
|
|
306
|
-
similarity: zod.z.number(),
|
|
307
|
-
conflict_content_snapshot: zod.z.string(),
|
|
308
|
-
reason: ReviewReasonSchema,
|
|
309
|
-
created_at: zod.z.string(),
|
|
310
|
-
resolved_at: zod.z.string().nullable()
|
|
311
|
-
});
|
|
312
|
-
const MemorySchema = zod.z.object({
|
|
313
|
-
id: zod.z.string(),
|
|
314
|
-
content: zod.z.string(),
|
|
315
|
-
type: MemoryTypeSchema,
|
|
316
|
-
tags: zod.z.array(zod.z.string()),
|
|
317
|
-
projects: zod.z.array(ProjectSchema),
|
|
318
|
-
sourceHarness: zod.z.string().nullable(),
|
|
319
|
-
accessCount: zod.z.number().int().nonnegative(),
|
|
320
|
-
pinned: zod.z.boolean(),
|
|
321
|
-
reviewEvents: zod.z.array(ReviewEventSchema),
|
|
322
|
-
createdAt: zod.z.string(),
|
|
323
|
-
updatedAt: zod.z.string()
|
|
324
|
-
});
|
|
325
|
-
const QueryOptionsSchema = zod.z.object({
|
|
326
|
-
query: zod.z.string().min(1),
|
|
327
|
-
type: MemoryTypeSchema.optional(),
|
|
328
|
-
projectHash: zod.z.string().optional(),
|
|
329
|
-
limit: zod.z.number().int().positive().optional(),
|
|
330
|
-
includePinned: zod.z.boolean().optional()
|
|
331
|
-
});
|
|
332
|
-
const SaveOptionsSchema = zod.z.object({
|
|
333
|
-
content: zod.z.string().min(1),
|
|
334
|
-
type: MemoryTypeSchema,
|
|
335
|
-
tags: zod.z.array(zod.z.string()).optional(),
|
|
336
|
-
projectScope: zod.z.object({
|
|
337
|
-
hash: zod.z.string(),
|
|
338
|
-
name: zod.z.string()
|
|
339
|
-
}).optional(),
|
|
340
|
-
sourceHarness: zod.z.string().optional()
|
|
341
|
-
});
|
|
342
|
-
const MemoryPatchSchema = zod.z.object({
|
|
343
|
-
content: zod.z.string().min(1).optional(),
|
|
344
|
-
tags: zod.z.array(zod.z.string()).optional(),
|
|
345
|
-
type: MemoryTypeSchema.optional()
|
|
346
|
-
});
|
|
347
|
-
const SynthesisSchema = zod.z.object({
|
|
348
|
-
id: zod.z.string(),
|
|
349
|
-
scope: zod.z.string(),
|
|
350
|
-
content: zod.z.string(),
|
|
351
|
-
sourceMemoryHash: zod.z.string(),
|
|
352
|
-
synthesizedAt: zod.z.string(),
|
|
353
|
-
expiresAt: zod.z.string(),
|
|
354
|
-
inFlightSince: zod.z.string().nullable(),
|
|
355
|
-
createdAt: zod.z.string(),
|
|
356
|
-
updatedAt: zod.z.string()
|
|
357
|
-
});
|
|
358
|
-
const SessionContextSchema = zod.z.object({
|
|
359
|
-
stats: zod.z.record(MemoryTypeSchema, zod.z.number()),
|
|
360
|
-
pinnedGlobal: zod.z.array(MemorySchema),
|
|
361
|
-
pinnedProject: zod.z.array(MemorySchema),
|
|
362
|
-
synthesis: zod.z.string().optional()
|
|
363
|
-
});
|
|
364
|
-
const MemoryRowSchema = zod.z.object({
|
|
365
|
-
id: zod.z.string(),
|
|
366
|
-
content: zod.z.string(),
|
|
367
|
-
type: zod.z.string(),
|
|
368
|
-
tags: zod.z.string(),
|
|
369
|
-
source: zod.z.string().nullable(),
|
|
370
|
-
access_count: zod.z.number(),
|
|
371
|
-
pinned: zod.z.number(),
|
|
372
|
-
created_at: zod.z.string(),
|
|
373
|
-
updated_at: zod.z.string()
|
|
374
|
-
});
|
|
375
|
-
const ProjectRowSchema = zod.z.object({
|
|
376
|
-
id: zod.z.string(),
|
|
377
|
-
name: zod.z.string(),
|
|
378
|
-
scope_hash: zod.z.string(),
|
|
379
|
-
created_at: zod.z.string(),
|
|
380
|
-
updated_at: zod.z.string()
|
|
381
|
-
});
|
|
382
|
-
//#endregion
|
|
383
|
-
//#region src/db/row-types.ts
|
|
384
|
-
function rowToMemory(row, projects, reviewEvents = []) {
|
|
385
|
-
return {
|
|
386
|
-
id: row.id,
|
|
387
|
-
content: row.content,
|
|
388
|
-
type: MemoryTypeSchema.parse(row.type),
|
|
389
|
-
tags: TagsJsonSchema.parse(JSON.parse(row.tags)),
|
|
390
|
-
projects,
|
|
391
|
-
sourceHarness: row.source,
|
|
392
|
-
accessCount: row.access_count,
|
|
393
|
-
pinned: row.pinned !== 0,
|
|
394
|
-
reviewEvents,
|
|
395
|
-
createdAt: row.created_at,
|
|
396
|
-
updatedAt: row.updated_at
|
|
397
|
-
};
|
|
398
|
-
}
|
|
399
|
-
function rowToReviewEvent(row) {
|
|
400
|
-
const parsed = ReviewEventRowSchema.parse(row);
|
|
401
|
-
return {
|
|
402
|
-
id: parsed.id,
|
|
403
|
-
memoryId: parsed.memory_id,
|
|
404
|
-
conflictingMemoryId: parsed.conflicting_memory_id,
|
|
405
|
-
similarity: parsed.similarity,
|
|
406
|
-
conflictContentSnapshot: parsed.conflict_content_snapshot,
|
|
407
|
-
reason: parsed.reason,
|
|
408
|
-
createdAt: parsed.created_at,
|
|
409
|
-
resolvedAt: parsed.resolved_at
|
|
410
|
-
};
|
|
411
|
-
}
|
|
412
|
-
function rowToProject(row) {
|
|
413
|
-
return {
|
|
414
|
-
id: row.id,
|
|
415
|
-
name: row.name,
|
|
416
|
-
scopeHash: row.scope_hash,
|
|
417
|
-
createdAt: row.created_at,
|
|
418
|
-
updatedAt: row.updated_at
|
|
419
|
-
};
|
|
420
|
-
}
|
|
421
|
-
//#endregion
|
|
422
|
-
//#region src/embedding/service.ts
|
|
423
|
-
var EmbeddingService = class {
|
|
424
|
-
modelCachePath;
|
|
425
|
-
onProgress;
|
|
426
|
-
pipelineInstance = null;
|
|
427
|
-
constructor(modelCachePath, onProgress) {
|
|
428
|
-
this.modelCachePath = modelCachePath ?? (0, node_path.join)((0, node_os.homedir)(), ".membank", "models");
|
|
429
|
-
this.onProgress = onProgress;
|
|
430
|
-
}
|
|
431
|
-
async getPipeline() {
|
|
432
|
-
if (this.pipelineInstance === null) this.pipelineInstance = await (0, _huggingface_transformers.pipeline)("feature-extraction", "Xenova/bge-small-en-v1.5", {
|
|
433
|
-
cache_dir: this.modelCachePath,
|
|
434
|
-
progress_callback: this.onProgress
|
|
435
|
-
});
|
|
436
|
-
return this.pipelineInstance;
|
|
437
|
-
}
|
|
438
|
-
async embed(text) {
|
|
439
|
-
const flat = (await (await this.getPipeline())(text, {
|
|
440
|
-
pooling: "mean",
|
|
441
|
-
normalize: true
|
|
442
|
-
})).data;
|
|
443
|
-
return flat instanceof Float32Array ? flat : new Float32Array(flat);
|
|
444
|
-
}
|
|
445
|
-
};
|
|
446
|
-
//#endregion
|
|
447
|
-
//#region src/memory/repository.ts
|
|
448
|
-
const PIN_BUDGET_THRESHOLD = 8e3;
|
|
449
|
-
var MemoryRepository = class {
|
|
450
|
-
#db;
|
|
451
|
-
#embedding;
|
|
452
|
-
#projects;
|
|
453
|
-
constructor(db, embeddingService, projects) {
|
|
454
|
-
this.#db = db;
|
|
455
|
-
this.#embedding = embeddingService;
|
|
456
|
-
this.#projects = projects;
|
|
457
|
-
}
|
|
458
|
-
async save(options) {
|
|
459
|
-
const { content, type, tags = [], projectScope, sourceHarness } = SaveOptionsSchema.parse(options);
|
|
460
|
-
const embedding = await this.#embedding.embed(content);
|
|
461
|
-
const embeddingBlob = Buffer.from(embedding.buffer);
|
|
462
|
-
let top;
|
|
463
|
-
if (projectScope !== void 0) top = this.#db.db.prepare(`SELECT m.rowid, m.*, (1 - vec_distance_cosine(e.embedding, ?)) AS similarity
|
|
151
|
+
`);let e=this.#e.prepare(`SELECT value FROM meta WHERE key = 'schema_version'`).get(),t=e?Number.parseInt(e.value,10):0;for(let[e,n]of se)t<e&&(this.#e.exec(n),this.#e.prepare(`INSERT OR REPLACE INTO meta (key, value) VALUES ('schema_version', ?)`).run(String(e)))}get db(){return this.#e}close(){this.#e.close()}};const I=`Xenova/bge-small-en-v1.5`;var L=class extends Error{constructor(e,t){super(e,t),this.name=`ModelDownloadError`}};function le(){return(0,u.join)((0,l.homedir)(),`.membank`,`models`)}function R(e){if(!(0,c.existsSync)(e))return!1;try{return(0,c.readdirSync)(e).length>0}catch{return!1}}var z=class extends ee.EventEmitter{modelPath;constructor(e){super(),this.modelPath=e??le()}isAlreadyCached(){return R(this.modelPath)}get cachePath(){return this.modelPath}async download(){if(R(this.modelPath))return{skipped:!0};let e=Date.now(),t=0,n=e;try{await(0,m.pipeline)(`feature-extraction`,I,{cache_dir:this.modelPath,progress_callback:e=>{if(e.status!==`progress`||e.total==null||e.loaded==null)return;let r=e.total,i=e.loaded,a=r>0?i/r*100:0,o=Date.now(),s=o-n,c=i-t,l=0;if(s>0&&c>0){let e=c/s;l=(r-i)/e/1e3}t=i,n=o;let u={totalBytes:r,downloadedBytes:i,percentage:a,estimatedSecondsRemaining:l};this.emit(`progress`,u)}})}catch(e){throw new L(`Failed to download model`,{cause:e})}return{skipped:!1}}},B=class{modelCachePath;onProgress;pipelineInstance=null;constructor(e,t){this.modelCachePath=e??(0,u.join)((0,l.homedir)(),`.membank`,`models`),this.onProgress=t}async getPipeline(){return this.pipelineInstance===null&&(this.pipelineInstance=await(0,m.pipeline)(`feature-extraction`,`Xenova/bge-small-en-v1.5`,{cache_dir:this.modelCachePath,progress_callback:this.onProgress})),this.pipelineInstance}async embed(e){let t=(await(await this.getPipeline())(e,{pooling:`mean`,normalize:!0})).data;return t instanceof Float32Array?t:new Float32Array(t)}};function V(e,t){return t.delete(e),Promise.resolve()}function H(e,t){t.resolveReviewEvents(e)}function U(e){return e>.92?`overwrite`:e>=.75?`flag`:`none`}async function W(e,t){let{content:n,type:r,tags:i=[],projectScope:a,sourceHarness:o}=E.parse(e),{repo:s,embedder:c}=t,l=await c.embed(n),[u]=s.findSimilar(l,r,a?.hash);if(u!==void 0){let e=U(u.similarity);if(e===`overwrite`)return s.overwrite(u.id,n,l);if(e===`flag`){let e=s.create({id:(0,h.randomUUID)(),content:n,type:r,tags:i,sourceHarness:o??null,embedding:l,projectScope:a});return s.createReviewEvent({memoryId:u.id,conflictingMemoryId:e.id,similarity:u.similarity,conflictContentSnapshot:n}),e}}return s.create({id:(0,h.randomUUID)(),content:n,type:r,tags:i,sourceHarness:o??null,embedding:l,projectScope:a})}async function ue(e,t,n){let{repo:r,embedder:i}=n,a=D.parse(t),o=a.content===void 0?void 0:await i.embed(a.content);return r.update(e,a,o)}const G=8e3;function de(e){return e>=G}var K=class{#e;#t;constructor(e,t){this.#e=e,this.#t=t}findSimilar(e,t,n){let r=Buffer.from(e.buffer),i;return i=n===void 0?this.#e.db.prepare(`SELECT m.rowid, m.*, (1 - vec_distance_cosine(e.embedding, ?)) AS similarity
|
|
152
|
+
FROM memories m JOIN embeddings e ON e.rowid = m.rowid
|
|
153
|
+
WHERE m.type = ?
|
|
154
|
+
AND m.id NOT IN (SELECT memory_id FROM memory_projects)
|
|
155
|
+
ORDER BY similarity DESC LIMIT 1`).get(r,t):this.#e.db.prepare(`SELECT m.rowid, m.*, (1 - vec_distance_cosine(e.embedding, ?)) AS similarity
|
|
464
156
|
FROM memories m JOIN embeddings e ON e.rowid = m.rowid
|
|
465
157
|
JOIN memory_projects mp ON mp.memory_id = m.id
|
|
466
158
|
JOIN projects p ON p.id = mp.project_id
|
|
467
159
|
WHERE m.type = ? AND p.scope_hash = ?
|
|
468
|
-
ORDER BY similarity DESC LIMIT 1`).get(
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
if (top !== void 0 && top.similarity > .92) {
|
|
476
|
-
this.#db.db.prepare(`UPDATE memories SET content = ?, updated_at = ? WHERE id = ?`).run(content, now, top.id);
|
|
477
|
-
this.#db.db.prepare(`UPDATE embeddings SET embedding = ? WHERE rowid = ?`).run(embeddingBlob, top.rowid);
|
|
478
|
-
const updated = MemoryRowSchema.parse(this.#db.db.prepare(`SELECT * FROM memories WHERE id = ?`).get(top.id));
|
|
479
|
-
const projectMap = this.#projects.getProjectsForMemories([top.id]);
|
|
480
|
-
const events = this.#getEventsForMemories([top.id]);
|
|
481
|
-
return rowToMemory(updated, projectMap.get(top.id) ?? [], events.get(top.id) ?? []);
|
|
482
|
-
}
|
|
483
|
-
const id = (0, node_crypto.randomUUID)();
|
|
484
|
-
this.#db.db.prepare(`INSERT INTO memories (id, content, type, tags, source, access_count, pinned, created_at, updated_at)
|
|
485
|
-
VALUES (?, ?, ?, ?, ?, 0, 0, ?, ?)`).run(id, content, type, JSON.stringify(tags), sourceHarness ?? null, now, now);
|
|
486
|
-
if (top !== void 0 && top.similarity >= .75) this.#db.db.prepare(`INSERT INTO memory_review_events
|
|
487
|
-
(id, memory_id, conflicting_memory_id, similarity, conflict_content_snapshot, reason, created_at)
|
|
488
|
-
VALUES (?, ?, ?, ?, ?, 'similarity_dedup', ?)`).run((0, node_crypto.randomUUID)(), top.id, id, top.similarity, content, now);
|
|
489
|
-
this.#db.db.prepare(`INSERT INTO embeddings (rowid, embedding) SELECT m.rowid, ? FROM memories m WHERE m.id = ?`).run(embeddingBlob, id);
|
|
490
|
-
if (projectScope !== void 0) {
|
|
491
|
-
const project = this.#projects.upsertByHash(projectScope.hash, projectScope.name);
|
|
492
|
-
this.#projects.addAssociation(id, project.id);
|
|
493
|
-
}
|
|
494
|
-
return rowToMemory(MemoryRowSchema.parse(this.#db.db.prepare(`SELECT * FROM memories WHERE id = ?`).get(id)), this.#projects.getProjectsForMemories([id]).get(id) ?? [], []);
|
|
495
|
-
}
|
|
496
|
-
async update(id, patch) {
|
|
497
|
-
const { content, tags, type } = MemoryPatchSchema.parse(patch);
|
|
498
|
-
const existing = this.#db.db.prepare(`SELECT m.rowid, m.* FROM memories m WHERE m.id = ?`).get(id);
|
|
499
|
-
if (existing === void 0) throw new Error(`Memory not found: ${id}`);
|
|
500
|
-
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
501
|
-
const sets = ["updated_at = ?"];
|
|
502
|
-
const values = [now];
|
|
503
|
-
if (content !== void 0) {
|
|
504
|
-
sets.push("content = ?");
|
|
505
|
-
values.push(content);
|
|
506
|
-
}
|
|
507
|
-
if (tags !== void 0) {
|
|
508
|
-
sets.push("tags = ?");
|
|
509
|
-
values.push(JSON.stringify(tags));
|
|
510
|
-
}
|
|
511
|
-
if (type !== void 0) {
|
|
512
|
-
sets.push("type = ?");
|
|
513
|
-
values.push(type);
|
|
514
|
-
}
|
|
515
|
-
values.push(id);
|
|
516
|
-
this.#db.db.prepare(`UPDATE memories SET ${sets.join(", ")} WHERE id = ?`).run(...values);
|
|
517
|
-
if (content !== void 0) {
|
|
518
|
-
const embedding = await this.#embedding.embed(content);
|
|
519
|
-
const embeddingBlob = Buffer.from(embedding.buffer);
|
|
520
|
-
this.#db.db.prepare(`UPDATE embeddings SET embedding = ? WHERE rowid = ?`).run(embeddingBlob, existing.rowid);
|
|
521
|
-
}
|
|
522
|
-
const updated = MemoryRowSchema.parse(this.#db.db.prepare(`SELECT * FROM memories WHERE id = ?`).get(id));
|
|
523
|
-
const projectMap = this.#projects.getProjectsForMemories([id]);
|
|
524
|
-
const events = this.#getEventsForMemories([id]);
|
|
525
|
-
return rowToMemory(updated, projectMap.get(id) ?? [], events.get(id) ?? []);
|
|
526
|
-
}
|
|
527
|
-
delete(id) {
|
|
528
|
-
const row = this.#db.db.prepare(`SELECT rowid FROM memories WHERE id = ?`).get(id);
|
|
529
|
-
if (row !== void 0) this.#db.db.prepare(`DELETE FROM embeddings WHERE rowid = ?`).run(row.rowid);
|
|
530
|
-
this.#db.db.prepare(`DELETE FROM memory_projects WHERE memory_id = ?`).run(id);
|
|
531
|
-
this.#db.db.prepare(`DELETE FROM memories WHERE id = ?`).run(id);
|
|
532
|
-
return Promise.resolve();
|
|
533
|
-
}
|
|
534
|
-
list(opts) {
|
|
535
|
-
const conditions = [];
|
|
536
|
-
const params = [];
|
|
537
|
-
if (opts?.type !== void 0) {
|
|
538
|
-
conditions.push("type = ?");
|
|
539
|
-
params.push(opts.type);
|
|
540
|
-
}
|
|
541
|
-
if (opts?.pinned === true) conditions.push("pinned = 1");
|
|
542
|
-
const where = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
|
|
543
|
-
const rows = this.#db.db.prepare(`SELECT * FROM memories ${where} ORDER BY created_at DESC`).all(...params);
|
|
544
|
-
if (rows.length === 0) return [];
|
|
545
|
-
const ids = rows.map((r) => r.id);
|
|
546
|
-
const projectMap = this.#projects.getProjectsForMemories(ids);
|
|
547
|
-
const eventMap = this.#getEventsForMemories(ids);
|
|
548
|
-
return rows.map((row) => rowToMemory(row, projectMap.get(row.id) ?? [], eventMap.get(row.id) ?? []));
|
|
549
|
-
}
|
|
550
|
-
listFlagged() {
|
|
551
|
-
const rows = this.#db.db.prepare(`SELECT * FROM memories
|
|
160
|
+
ORDER BY similarity DESC LIMIT 1`).get(r,t,n),i===void 0?[]:[{id:i.id,similarity:i.similarity}]}create(e){let{id:t,content:n,type:r,tags:i,sourceHarness:a,embedding:o,projectScope:s}=e,c=new Date().toISOString(),l=Buffer.from(o.buffer);if(this.#e.db.prepare(`INSERT INTO memories (id, content, type, tags, source, access_count, pinned, created_at, updated_at)
|
|
161
|
+
VALUES (?, ?, ?, ?, ?, 0, 0, ?, ?)`).run(t,n,r,JSON.stringify(i),a,c,c),this.#e.db.prepare(`INSERT INTO embeddings (rowid, embedding) SELECT m.rowid, ? FROM memories m WHERE m.id = ?`).run(l,t),s!==void 0){let e=this.#t.upsertByHash(s.hash,s.name);this.#t.addAssociation(t,e.id)}return j(k.parse(this.#e.db.prepare(`SELECT * FROM memories WHERE id = ?`).get(t)),this.#t.getProjectsForMemories([t]).get(t)??[])}overwrite(e,t,n){let r=new Date().toISOString(),i=Buffer.from(n.buffer);this.#e.db.prepare(`UPDATE memories SET content = ?, updated_at = ? WHERE id = ?`).run(t,r,e);let a=this.#e.db.prepare(`SELECT rowid FROM memories WHERE id = ?`).get(e)?.rowid;a!==void 0&&this.#e.db.prepare(`UPDATE embeddings SET embedding = ? WHERE rowid = ?`).run(i,a);let o=k.parse(this.#e.db.prepare(`SELECT * FROM memories WHERE id = ?`).get(e)),s=this.#t.getProjectsForMemories([e]),c=this.#n([e]);return j(o,s.get(e)??[],c.get(e)??[])}findById(e){let t=this.#e.db.prepare(`SELECT * FROM memories WHERE id = ?`).get(e);if(t===void 0)return;let n=this.#t.getProjectsForMemories([e]),r=this.#n([e]);return j(t,n.get(e)??[],r.get(e)??[])}update(e,t,n){let{content:r,tags:i,type:a}=D.parse(t),o=this.#e.db.prepare(`SELECT m.rowid, m.* FROM memories m WHERE m.id = ?`).get(e);if(o===void 0)throw Error(`Memory not found: ${e}`);let s=new Date().toISOString(),c=[`updated_at = ?`],l=[s];if(r!==void 0&&(c.push(`content = ?`),l.push(r)),i!==void 0&&(c.push(`tags = ?`),l.push(JSON.stringify(i))),a!==void 0&&(c.push(`type = ?`),l.push(a)),l.push(e),this.#e.db.prepare(`UPDATE memories SET ${c.join(`, `)} WHERE id = ?`).run(...l),n!==void 0){let e=Buffer.from(n.buffer);this.#e.db.prepare(`UPDATE embeddings SET embedding = ? WHERE rowid = ?`).run(e,o.rowid)}let u=k.parse(this.#e.db.prepare(`SELECT * FROM memories WHERE id = ?`).get(e)),d=this.#t.getProjectsForMemories([e]),f=this.#n([e]);return j(u,d.get(e)??[],f.get(e)??[])}delete(e){let t=this.#e.db.prepare(`SELECT rowid FROM memories WHERE id = ?`).get(e);t!==void 0&&this.#e.db.prepare(`DELETE FROM embeddings WHERE rowid = ?`).run(t.rowid),this.#e.db.prepare(`DELETE FROM memory_projects WHERE memory_id = ?`).run(e),this.#e.db.prepare(`DELETE FROM memories WHERE id = ?`).run(e)}list(e){let t=[],n=[];e?.type!==void 0&&(t.push(`m.type = ?`),n.push(e.type)),e?.pinned===!0&&t.push(`m.pinned = 1`),e?.needsReview===!0&&t.push(`EXISTS (SELECT 1 FROM memory_review_events e WHERE e.memory_id = m.id AND e.resolved_at IS NULL)`),e?.projectId===`global`?t.push(`m.id NOT IN (SELECT memory_id FROM memory_projects)`):e?.projectId!==void 0&&(t.push(`m.id IN (SELECT memory_id FROM memory_projects WHERE project_id = ?)`),n.push(e.projectId));let r=t.length>0?`WHERE ${t.join(` AND `)}`:``,i=this.#e.db.prepare(`SELECT m.* FROM memories m ${r} ORDER BY m.created_at DESC`).all(...n);if(i.length===0)return[];let a=i.map(e=>e.id),o=this.#t.getProjectsForMemories(a),s=this.#n(a);return i.map(e=>j(e,o.get(e.id)??[],s.get(e.id)??[]))}listPinnedGlobal(){return this.#e.db.prepare(`SELECT * FROM memories
|
|
162
|
+
WHERE id NOT IN (SELECT memory_id FROM memory_projects)
|
|
163
|
+
AND pinned = 1`).all().map(e=>j(e,[]))}listPinnedForProject(e){return this.#e.db.prepare(`SELECT m.* FROM memories m
|
|
164
|
+
JOIN memory_projects mp ON mp.memory_id = m.id
|
|
165
|
+
JOIN projects p ON p.id = mp.project_id
|
|
166
|
+
WHERE p.scope_hash = ? AND m.pinned = 1`).all(e).map(e=>j(e,[]))}listFlagged(){let e=this.#e.db.prepare(`SELECT * FROM memories
|
|
552
167
|
WHERE EXISTS (
|
|
553
168
|
SELECT 1 FROM memory_review_events e
|
|
554
169
|
WHERE e.memory_id = memories.id AND e.resolved_at IS NULL
|
|
555
170
|
)
|
|
556
|
-
ORDER BY created_at DESC`).all();
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
}
|
|
563
|
-
listReviewEvents(memoryId, opts) {
|
|
564
|
-
const where = opts?.unresolvedOnly === true ? "WHERE memory_id = ? AND resolved_at IS NULL" : "WHERE memory_id = ?";
|
|
565
|
-
return this.#db.db.prepare(`SELECT * FROM memory_review_events ${where} ORDER BY created_at DESC`).all(memoryId).map((r) => rowToReviewEvent(ReviewEventRowSchema.parse(r)));
|
|
566
|
-
}
|
|
567
|
-
resolveReviewEvents(memoryId) {
|
|
568
|
-
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
569
|
-
this.#db.db.prepare(`UPDATE memory_review_events SET resolved_at = ? WHERE memory_id = ? AND resolved_at IS NULL`).run(now, memoryId);
|
|
570
|
-
}
|
|
571
|
-
#getEventsForMemories(ids, opts) {
|
|
572
|
-
if (ids.length === 0) return /* @__PURE__ */ new Map();
|
|
573
|
-
const placeholders = ids.map(() => "?").join(", ");
|
|
574
|
-
const unresolvedClause = opts?.unresolvedOnly === true ? "AND resolved_at IS NULL" : "";
|
|
575
|
-
const rows = this.#db.db.prepare(`SELECT * FROM memory_review_events
|
|
576
|
-
WHERE memory_id IN (${placeholders}) ${unresolvedClause}
|
|
577
|
-
ORDER BY created_at DESC`).all(...ids);
|
|
578
|
-
const map = /* @__PURE__ */ new Map();
|
|
579
|
-
for (const row of rows) {
|
|
580
|
-
const event = rowToReviewEvent(ReviewEventRowSchema.parse(row));
|
|
581
|
-
const existing = map.get(event.memoryId) ?? [];
|
|
582
|
-
existing.push(event);
|
|
583
|
-
map.set(event.memoryId, existing);
|
|
584
|
-
}
|
|
585
|
-
return map;
|
|
586
|
-
}
|
|
587
|
-
getPinnedCharCount() {
|
|
588
|
-
return (this.#db.db.prepare(`SELECT COALESCE(SUM(LENGTH(content)), 0) as total FROM memories WHERE pinned = 1`).get() ?? { total: 0 }).total;
|
|
589
|
-
}
|
|
590
|
-
stats() {
|
|
591
|
-
const byType = Object.fromEntries(MEMORY_TYPE_VALUES.map((t) => [t, 0]));
|
|
592
|
-
const typeRows = this.#db.db.prepare(`SELECT type, COUNT(*) as count FROM memories GROUP BY type`).all();
|
|
593
|
-
for (const row of typeRows) {
|
|
594
|
-
const parsed = MemoryTypeSchema.safeParse(row.type);
|
|
595
|
-
if (parsed.success) byType[parsed.data] = row.count;
|
|
596
|
-
}
|
|
597
|
-
const aggregates = this.#db.db.prepare(`SELECT COUNT(*) as total, SUM(pinned) as pinned FROM memories`).get() ?? {
|
|
598
|
-
total: 0,
|
|
599
|
-
pinned: 0
|
|
600
|
-
};
|
|
601
|
-
const reviewRow = this.#db.db.prepare(`SELECT COUNT(DISTINCT memory_id) as needsReview FROM memory_review_events WHERE resolved_at IS NULL`).get() ?? { needsReview: 0 };
|
|
602
|
-
return {
|
|
603
|
-
byType,
|
|
604
|
-
total: aggregates.total,
|
|
605
|
-
pinned: aggregates.pinned ?? 0,
|
|
606
|
-
needsReview: reviewRow.needsReview,
|
|
607
|
-
pinBudgetChars: this.getPinnedCharCount()
|
|
608
|
-
};
|
|
609
|
-
}
|
|
610
|
-
setPin(id, pinned) {
|
|
611
|
-
if (this.#db.db.prepare(`SELECT * FROM memories WHERE id = ?`).get(id) === void 0) throw new Error(`Memory not found: ${id}`);
|
|
612
|
-
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
613
|
-
this.#db.db.prepare(`UPDATE memories SET pinned = ?, updated_at = ? WHERE id = ?`).run(pinned ? 1 : 0, now, id);
|
|
614
|
-
const updated = MemoryRowSchema.parse(this.#db.db.prepare(`SELECT * FROM memories WHERE id = ?`).get(id));
|
|
615
|
-
const projectMap = this.#projects.getProjectsForMemories([id]);
|
|
616
|
-
const events = this.#getEventsForMemories([id]);
|
|
617
|
-
return rowToMemory(updated, projectMap.get(id) ?? [], events.get(id) ?? []);
|
|
618
|
-
}
|
|
619
|
-
incrementAccessCount(id) {
|
|
620
|
-
this.#db.db.prepare(`UPDATE memories SET access_count = access_count + 1 WHERE id = ?`).run(id);
|
|
621
|
-
}
|
|
622
|
-
};
|
|
623
|
-
//#endregion
|
|
624
|
-
//#region src/scope/resolver.ts
|
|
625
|
-
const execFileAsync = (0, node_util.promisify)(node_child_process.execFile);
|
|
626
|
-
function sha256Truncated(input) {
|
|
627
|
-
return (0, node_crypto.createHash)("sha256").update(input).digest("hex").slice(0, 16);
|
|
628
|
-
}
|
|
629
|
-
async function resolveProject() {
|
|
630
|
-
try {
|
|
631
|
-
const { stdout } = await execFileAsync("git", [
|
|
632
|
-
"remote",
|
|
633
|
-
"get-url",
|
|
634
|
-
"origin"
|
|
635
|
-
]);
|
|
636
|
-
const url = stdout.trim();
|
|
637
|
-
if (url) {
|
|
638
|
-
const hash = sha256Truncated(url);
|
|
639
|
-
return {
|
|
640
|
-
hash,
|
|
641
|
-
name: url.split("/").pop()?.replace(/\.git$/, "") ?? hash.slice(0, 8)
|
|
642
|
-
};
|
|
643
|
-
}
|
|
644
|
-
} catch {}
|
|
645
|
-
const cwd = process.cwd();
|
|
646
|
-
const hash = sha256Truncated(cwd);
|
|
647
|
-
return {
|
|
648
|
-
hash,
|
|
649
|
-
name: cwd.split(/[/\\]/).filter(Boolean).pop() ?? hash.slice(0, 8)
|
|
650
|
-
};
|
|
651
|
-
}
|
|
652
|
-
async function resolveScope() {
|
|
653
|
-
try {
|
|
654
|
-
const { stdout } = await execFileAsync("git", [
|
|
655
|
-
"remote",
|
|
656
|
-
"get-url",
|
|
657
|
-
"origin"
|
|
658
|
-
]);
|
|
659
|
-
const url = stdout.trim();
|
|
660
|
-
if (url) return sha256Truncated(url);
|
|
661
|
-
} catch {}
|
|
662
|
-
return sha256Truncated(process.cwd());
|
|
663
|
-
}
|
|
664
|
-
//#endregion
|
|
665
|
-
//#region src/migrations/index.ts
|
|
666
|
-
const MIGRATIONS = [{
|
|
667
|
-
name: "scope-to-projects",
|
|
668
|
-
description: "Rename the auto-migrated project for the current directory from its generic hash-derived name to the resolved repo/directory name."
|
|
669
|
-
}];
|
|
670
|
-
async function runScopeToProjectsMigration(projects) {
|
|
671
|
-
const resolved = await resolveProject();
|
|
672
|
-
const project = projects.getByHash(resolved.hash);
|
|
673
|
-
if (project === void 0) return null;
|
|
674
|
-
const oldName = project.name;
|
|
675
|
-
const memoryCount = projects.countMemories(project.id);
|
|
676
|
-
projects.rename(project.id, resolved.name);
|
|
677
|
-
return {
|
|
678
|
-
migration: "scope-to-projects",
|
|
679
|
-
oldName,
|
|
680
|
-
newName: resolved.name,
|
|
681
|
-
memoryCount
|
|
682
|
-
};
|
|
683
|
-
}
|
|
684
|
-
//#endregion
|
|
685
|
-
//#region src/project/repository.ts
|
|
686
|
-
var ProjectRepository = class {
|
|
687
|
-
#db;
|
|
688
|
-
constructor(db) {
|
|
689
|
-
this.#db = db;
|
|
690
|
-
}
|
|
691
|
-
upsertByHash(hash, name) {
|
|
692
|
-
if (!/^[0-9a-f]{16}$/.test(hash)) throw new Error(`Invalid scope hash "${hash}": expected 16 lowercase hex characters`);
|
|
693
|
-
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
694
|
-
const id = (0, node_crypto.randomUUID)();
|
|
695
|
-
this.#db.db.prepare(`INSERT OR IGNORE INTO projects (id, name, scope_hash, created_at, updated_at) VALUES (?, ?, ?, ?, ?)`).run(id, name, hash, now, now);
|
|
696
|
-
return rowToProject(ProjectRowSchema.parse(this.#db.db.prepare(`SELECT * FROM projects WHERE scope_hash = ?`).get(hash)));
|
|
697
|
-
}
|
|
698
|
-
rename(id, name) {
|
|
699
|
-
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
700
|
-
this.#db.db.prepare(`UPDATE projects SET name = ?, updated_at = ? WHERE id = ?`).run(name, now, id);
|
|
701
|
-
const row = this.#db.db.prepare(`SELECT * FROM projects WHERE id = ?`).get(id);
|
|
702
|
-
if (row === void 0) throw new Error(`Project not found: ${id}`);
|
|
703
|
-
return rowToProject(row);
|
|
704
|
-
}
|
|
705
|
-
list() {
|
|
706
|
-
return this.#db.db.prepare(`SELECT * FROM projects ORDER BY name ASC`).all().map(rowToProject);
|
|
707
|
-
}
|
|
708
|
-
getByHash(hash) {
|
|
709
|
-
const row = this.#db.db.prepare(`SELECT * FROM projects WHERE scope_hash = ?`).get(hash);
|
|
710
|
-
return row !== void 0 ? rowToProject(row) : void 0;
|
|
711
|
-
}
|
|
712
|
-
getByName(name) {
|
|
713
|
-
const row = this.#db.db.prepare(`SELECT * FROM projects WHERE name = ? LIMIT 1`).get(name);
|
|
714
|
-
return row !== void 0 ? rowToProject(row) : void 0;
|
|
715
|
-
}
|
|
716
|
-
addAssociation(memoryId, projectId) {
|
|
717
|
-
this.#db.db.prepare(`INSERT OR IGNORE INTO memory_projects (memory_id, project_id) VALUES (?, ?)`).run(memoryId, projectId);
|
|
718
|
-
}
|
|
719
|
-
removeAssociation(memoryId, projectId) {
|
|
720
|
-
this.#db.db.prepare(`DELETE FROM memory_projects WHERE memory_id = ? AND project_id = ?`).run(memoryId, projectId);
|
|
721
|
-
}
|
|
722
|
-
countMemories(projectId) {
|
|
723
|
-
return this.#db.db.prepare(`SELECT COUNT(*) AS count FROM memory_projects WHERE project_id = ?`).get(projectId)?.count ?? 0;
|
|
724
|
-
}
|
|
725
|
-
getProjectsForMemories(memoryIds) {
|
|
726
|
-
if (memoryIds.length === 0) return /* @__PURE__ */ new Map();
|
|
727
|
-
const placeholders = memoryIds.map(() => "?").join(",");
|
|
728
|
-
const rows = this.#db.db.prepare(`SELECT p.*, mp.memory_id FROM projects p
|
|
171
|
+
ORDER BY created_at DESC`).all();if(e.length===0)return[];let t=e.map(e=>e.id),n=this.#t.getProjectsForMemories(t),r=this.#n(t,{unresolvedOnly:!0});return e.map(e=>j(e,n.get(e.id)??[],r.get(e.id)??[]))}listReviewEvents(e,t){let n=t?.unresolvedOnly===!0?`WHERE memory_id = ? AND resolved_at IS NULL`:`WHERE memory_id = ?`;return this.#e.db.prepare(`SELECT * FROM memory_review_events ${n} ORDER BY created_at DESC`).all(e).map(e=>M(C.parse(e)))}createReviewEvent(e){let t=new Date().toISOString();this.#e.db.prepare(`INSERT INTO memory_review_events
|
|
172
|
+
(id, memory_id, conflicting_memory_id, similarity, conflict_content_snapshot, reason, created_at)
|
|
173
|
+
VALUES (?, ?, ?, ?, ?, 'similarity_dedup', ?)`).run((0,h.randomUUID)(),e.memoryId,e.conflictingMemoryId,e.similarity,e.conflictContentSnapshot,t)}resolveReviewEvents(e){let t=new Date().toISOString();this.#e.db.prepare(`UPDATE memory_review_events SET resolved_at = ? WHERE memory_id = ? AND resolved_at IS NULL`).run(t,e)}getPinnedCharCount(){return(this.#e.db.prepare(`SELECT COALESCE(SUM(LENGTH(content)), 0) as total FROM memories WHERE pinned = 1`).get()??{total:0}).total}stats(){let e=Object.fromEntries(_.map(e=>[e,0])),t=this.#e.db.prepare(`SELECT type, COUNT(*) as count FROM memories GROUP BY type`).all();for(let n of t){let t=v.safeParse(n.type);t.success&&(e[t.data]=n.count)}let n=this.#e.db.prepare(`SELECT COUNT(*) as total, SUM(pinned) as pinned FROM memories`).get()??{total:0,pinned:0},r=this.#e.db.prepare(`SELECT COUNT(DISTINCT memory_id) as needsReview FROM memory_review_events WHERE resolved_at IS NULL`).get()??{needsReview:0};return{byType:e,total:n.total,pinned:n.pinned??0,needsReview:r.needsReview,pinBudgetChars:this.getPinnedCharCount()}}setPin(e,t){if(this.#e.db.prepare(`SELECT * FROM memories WHERE id = ?`).get(e)===void 0)throw Error(`Memory not found: ${e}`);let n=new Date().toISOString();this.#e.db.prepare(`UPDATE memories SET pinned = ?, updated_at = ? WHERE id = ?`).run(+!!t,n,e);let r=k.parse(this.#e.db.prepare(`SELECT * FROM memories WHERE id = ?`).get(e)),i=this.#t.getProjectsForMemories([e]),a=this.#n([e]);return j(r,i.get(e)??[],a.get(e)??[])}incrementAccessCount(e){this.#e.db.prepare(`UPDATE memories SET access_count = access_count + 1 WHERE id = ?`).run(e)}exportAll(){return this.#e.db.prepare(`SELECT m.*, e.embedding FROM memories m LEFT JOIN embeddings e ON e.rowid = m.rowid ORDER BY m.created_at DESC`).all().map(e=>({id:e.id,content:e.content,type:v.parse(e.type),tags:y.parse(JSON.parse(e.tags)),sourceHarness:e.source,accessCount:e.access_count,pinned:e.pinned!==0,createdAt:e.created_at,updatedAt:e.updated_at,embedding:e.embedding===null?null:new Float32Array(e.embedding.buffer,e.embedding.byteOffset,e.embedding.byteLength/4)}))}importAll(e){let t=this.#e.db.prepare(`INSERT OR REPLACE INTO memories (id, content, type, tags, source, access_count, pinned, created_at, updated_at)
|
|
174
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`),n=this.#e.db.prepare(`INSERT OR REPLACE INTO embeddings (rowid, embedding) SELECT m.rowid, ? FROM memories m WHERE m.id = ?`);this.#e.db.transaction(()=>{for(let r of e)t.run(r.id,r.content,r.type,JSON.stringify(r.tags),r.sourceHarness,r.accessCount,+!!r.pinned,r.createdAt,r.updatedAt),r.embedding!==null&&n.run(Buffer.from(r.embedding.buffer,r.embedding.byteOffset,r.embedding.byteLength),r.id)})()}#n(e,t){if(e.length===0)return new Map;let n=e.map(()=>`?`).join(`, `),r=t?.unresolvedOnly===!0?`AND resolved_at IS NULL`:``,i=this.#e.db.prepare(`SELECT * FROM memory_review_events
|
|
175
|
+
WHERE memory_id IN (${n}) ${r}
|
|
176
|
+
ORDER BY created_at DESC`).all(...e),a=new Map;for(let e of i){let t=M(C.parse(e)),n=a.get(t.memoryId)??[];n.push(t),a.set(t.memoryId,n)}return a}};function fe(e,t){return new K(e,t)}const q=(0,ne.promisify)(te.execFile);function J(e){return(0,h.createHash)(`sha256`).update(e).digest(`hex`).slice(0,16)}async function Y(){try{let{stdout:e}=await q(`git`,[`remote`,`get-url`,`origin`]),t=e.trim();if(t){let e=J(t);return{hash:e,name:t.split(`/`).pop()?.replace(/\.git$/,``)??e.slice(0,8)}}}catch{}let e=process.cwd(),t=J(e);return{hash:t,name:e.split(/[/\\]/).filter(Boolean).pop()??t.slice(0,8)}}async function pe(){try{let{stdout:e}=await q(`git`,[`remote`,`get-url`,`origin`]),t=e.trim();if(t)return J(t)}catch{}return J(process.cwd())}const me=[{name:`scope-to-projects`,description:`Rename the auto-migrated project for the current directory from its generic hash-derived name to the resolved repo/directory name.`}];async function he(e){let t=await Y(),n=e.getByHash(t.hash);if(n===void 0)return null;let r=n.name,i=e.countMemories(n.id);return e.rename(n.id,t.name),{migration:`scope-to-projects`,oldName:r,newName:t.name,memoryCount:i}}var ge=class{#e;constructor(e){this.#e=e}upsertByHash(e,t){if(!/^[0-9a-f]{16}$/.test(e))throw Error(`Invalid scope hash "${e}": expected 16 lowercase hex characters`);let n=new Date().toISOString(),r=(0,h.randomUUID)();return this.#e.db.prepare(`INSERT OR IGNORE INTO projects (id, name, scope_hash, created_at, updated_at) VALUES (?, ?, ?, ?, ?)`).run(r,t,e,n,n),N(A.parse(this.#e.db.prepare(`SELECT * FROM projects WHERE scope_hash = ?`).get(e)))}rename(e,t){let n=new Date().toISOString();this.#e.db.prepare(`UPDATE projects SET name = ?, updated_at = ? WHERE id = ?`).run(t,n,e);let r=this.#e.db.prepare(`SELECT * FROM projects WHERE id = ?`).get(e);if(r===void 0)throw Error(`Project not found: ${e}`);return N(r)}list(){return this.#e.db.prepare(`SELECT * FROM projects ORDER BY name ASC`).all().map(N)}getByHash(e){let t=this.#e.db.prepare(`SELECT * FROM projects WHERE scope_hash = ?`).get(e);return t===void 0?void 0:N(t)}getByName(e){let t=this.#e.db.prepare(`SELECT * FROM projects WHERE name = ? LIMIT 1`).get(e);return t===void 0?void 0:N(t)}addAssociation(e,t){this.#e.db.prepare(`INSERT OR IGNORE INTO memory_projects (memory_id, project_id) VALUES (?, ?)`).run(e,t)}removeAssociation(e,t){this.#e.db.prepare(`DELETE FROM memory_projects WHERE memory_id = ? AND project_id = ?`).run(e,t)}countMemories(e){return this.#e.db.prepare(`SELECT COUNT(*) AS count FROM memory_projects WHERE project_id = ?`).get(e)?.count??0}getProjectsForMemories(e){if(e.length===0)return new Map;let t=e.map(()=>`?`).join(`,`),n=this.#e.db.prepare(`SELECT p.*, mp.memory_id FROM projects p
|
|
729
177
|
JOIN memory_projects mp ON mp.project_id = p.id
|
|
730
|
-
WHERE mp.memory_id IN (${
|
|
731
|
-
const result = /* @__PURE__ */ new Map();
|
|
732
|
-
for (const row of rows) {
|
|
733
|
-
const list = result.get(row.memory_id) ?? [];
|
|
734
|
-
list.push(rowToProject(row));
|
|
735
|
-
result.set(row.memory_id, list);
|
|
736
|
-
}
|
|
737
|
-
return result;
|
|
738
|
-
}
|
|
739
|
-
};
|
|
740
|
-
//#endregion
|
|
741
|
-
//#region src/query/engine.ts
|
|
742
|
-
const TYPE_WEIGHTS = {
|
|
743
|
-
correction: 1,
|
|
744
|
-
preference: .8,
|
|
745
|
-
decision: .6,
|
|
746
|
-
learning: .4,
|
|
747
|
-
fact: .2
|
|
748
|
-
};
|
|
749
|
-
var QueryEngine = class {
|
|
750
|
-
#db;
|
|
751
|
-
#embedding;
|
|
752
|
-
#repo;
|
|
753
|
-
constructor(db, embeddingService, repo) {
|
|
754
|
-
this.#db = db;
|
|
755
|
-
this.#embedding = embeddingService;
|
|
756
|
-
this.#repo = repo;
|
|
757
|
-
}
|
|
758
|
-
async query(options) {
|
|
759
|
-
const { query, type, projectHash, limit = 10, includePinned } = QueryOptionsSchema.parse(options);
|
|
760
|
-
const queryEmbedding = await this.#embedding.embed(query);
|
|
761
|
-
const queryBlob = Buffer.from(queryEmbedding.buffer);
|
|
762
|
-
const whereClauses = [];
|
|
763
|
-
const params = [queryBlob];
|
|
764
|
-
let joinClause = "";
|
|
765
|
-
if (!includePinned) whereClauses.push("m.pinned = 0");
|
|
766
|
-
if (type !== void 0) {
|
|
767
|
-
whereClauses.push("m.type = ?");
|
|
768
|
-
params.push(type);
|
|
769
|
-
}
|
|
770
|
-
if (projectHash !== void 0) {
|
|
771
|
-
joinClause = "LEFT JOIN memory_projects mp ON mp.memory_id = m.id LEFT JOIN projects p ON p.id = mp.project_id";
|
|
772
|
-
whereClauses.push("p.scope_hash = ?");
|
|
773
|
-
params.push(projectHash);
|
|
774
|
-
}
|
|
775
|
-
const whereSQL = whereClauses.length > 0 ? `WHERE ${whereClauses.join(" AND ")}` : "";
|
|
776
|
-
const sql = `
|
|
178
|
+
WHERE mp.memory_id IN (${t})`).all(...e),r=new Map;for(let e of n){let t=r.get(e.memory_id)??[];t.push(N(e)),r.set(e.memory_id,t)}return r}};function _e(e){return new ge(e)}const ve={correction:1,preference:.8,decision:.6,learning:.4,fact:.2};function ye(e,t,n){let r=ve[e.type],i=e.accessCount/(e.accessCount+10),a=1/(1+(n-new Date(e.updatedAt).getTime())/864e5),o=+!!e.pinned;return t*.4+r*.25+i*.2+a*.1+o*.05}async function X(e,t){let{query:n,type:r,projectHash:i,limit:a=10,includePinned:o}=T.parse(e),s=await t.embedder.embed(n),c=Buffer.from(s.buffer),l=t.adapter.findByEmbedding(c,{type:r,projectHash:i,includePinned:o}),u=Date.now(),d=l.filter(e=>e.cosineSim>0).map(e=>{let{cosineSim:t,...n}=e;return{...n,score:ye(n,t,u)}});d.sort((e,t)=>t.score-e.score);let f=d.slice(0,a);for(let e of f)t.repo.incrementAccessCount(e.id);return f}var be=class{#e;constructor(e){this.#e=e}findByEmbedding(e,t){let{type:n,projectHash:r,includePinned:i}=t,a=[],o=[e],s=``;i||a.push(`m.pinned = 0`),n!==void 0&&(a.push(`m.type = ?`),o.push(n)),r!==void 0&&(s=`LEFT JOIN memory_projects mp ON mp.memory_id = m.id LEFT JOIN projects p ON p.id = mp.project_id`,a.push(`p.scope_hash = ?`),o.push(r));let c=a.length>0?`WHERE ${a.join(` AND `)}`:``,l=`
|
|
777
179
|
SELECT m.*, (1 - vec_distance_cosine(e.embedding, ?)) AS cosine_sim
|
|
778
180
|
FROM memories m JOIN embeddings e ON e.rowid = m.rowid
|
|
779
|
-
${
|
|
780
|
-
${
|
|
781
|
-
`;
|
|
782
|
-
const rows = this.#db.db.prepare(sql).all(...params);
|
|
783
|
-
const now = Date.now();
|
|
784
|
-
const scored = rows.filter((row) => row.cosine_sim > 0).map((row) => {
|
|
785
|
-
const memory = rowToMemory(row, []);
|
|
786
|
-
const score = this.#computeScore(memory, row.cosine_sim, now);
|
|
787
|
-
return {
|
|
788
|
-
...memory,
|
|
789
|
-
score
|
|
790
|
-
};
|
|
791
|
-
});
|
|
792
|
-
scored.sort((a, b) => b.score - a.score);
|
|
793
|
-
const results = scored.slice(0, limit);
|
|
794
|
-
for (const result of results) this.#repo.incrementAccessCount(result.id);
|
|
795
|
-
return results;
|
|
796
|
-
}
|
|
797
|
-
#computeScore(memory, cosine_sim, now) {
|
|
798
|
-
const typeWeight = TYPE_WEIGHTS[memory.type];
|
|
799
|
-
const accessCountNorm = memory.accessCount / (memory.accessCount + 10);
|
|
800
|
-
const recencyNorm = 1 / (1 + (now - new Date(memory.updatedAt).getTime()) / 864e5);
|
|
801
|
-
const pinned = memory.pinned ? 1 : 0;
|
|
802
|
-
return cosine_sim * .4 + typeWeight * .25 + accessCountNorm * .2 + recencyNorm * .1 + pinned * .05;
|
|
803
|
-
}
|
|
804
|
-
};
|
|
805
|
-
//#endregion
|
|
806
|
-
//#region src/session/builder.ts
|
|
807
|
-
function listMemoryTypes() {
|
|
808
|
-
return [...MEMORY_TYPE_VALUES];
|
|
809
|
-
}
|
|
810
|
-
var SessionContextBuilder = class {
|
|
811
|
-
#db;
|
|
812
|
-
constructor(db) {
|
|
813
|
-
this.#db = db;
|
|
814
|
-
}
|
|
815
|
-
getSessionContext(projectHash, synthesis) {
|
|
816
|
-
const typeCounts = this.#db.db.prepare("SELECT type, COUNT(*) as count FROM memories GROUP BY type").all();
|
|
817
|
-
const stats = Object.fromEntries(MEMORY_TYPE_VALUES.map((t) => [t, 0]));
|
|
818
|
-
for (const row of typeCounts) {
|
|
819
|
-
const parsed = MemoryTypeSchema.safeParse(row.type);
|
|
820
|
-
if (parsed.success) stats[parsed.data] = row.count;
|
|
821
|
-
}
|
|
822
|
-
if (synthesis !== void 0 && synthesis.length > 0) return {
|
|
823
|
-
stats,
|
|
824
|
-
pinnedGlobal: [],
|
|
825
|
-
pinnedProject: [],
|
|
826
|
-
synthesis
|
|
827
|
-
};
|
|
828
|
-
return {
|
|
829
|
-
stats,
|
|
830
|
-
pinnedGlobal: this.#db.db.prepare(`SELECT * FROM memories
|
|
831
|
-
WHERE id NOT IN (SELECT memory_id FROM memory_projects)
|
|
832
|
-
AND pinned = 1`).all().map((row) => rowToMemory(row, [])),
|
|
833
|
-
pinnedProject: this.#db.db.prepare(`SELECT m.* FROM memories m
|
|
834
|
-
JOIN memory_projects mp ON mp.memory_id = m.id
|
|
835
|
-
JOIN projects p ON p.id = mp.project_id
|
|
836
|
-
WHERE p.scope_hash = ? AND m.pinned = 1`).all(projectHash).map((row) => rowToMemory(row, []))
|
|
837
|
-
};
|
|
838
|
-
}
|
|
839
|
-
};
|
|
840
|
-
//#endregion
|
|
841
|
-
//#region src/synthesis/repository.ts
|
|
842
|
-
function rowToSynthesis(row) {
|
|
843
|
-
return SynthesisSchema.parse({
|
|
844
|
-
id: row.id,
|
|
845
|
-
scope: row.scope,
|
|
846
|
-
content: row.content,
|
|
847
|
-
sourceMemoryHash: row.source_memory_hash,
|
|
848
|
-
synthesizedAt: row.synthesized_at,
|
|
849
|
-
expiresAt: row.expires_at,
|
|
850
|
-
inFlightSince: row.in_flight_since,
|
|
851
|
-
createdAt: row.created_at,
|
|
852
|
-
updatedAt: row.updated_at
|
|
853
|
-
});
|
|
854
|
-
}
|
|
855
|
-
const STALENESS_DAYS = 30;
|
|
856
|
-
var SynthesisRepository = class {
|
|
857
|
-
#db;
|
|
858
|
-
constructor(db) {
|
|
859
|
-
this.#db = db;
|
|
860
|
-
}
|
|
861
|
-
saveSynthesis(scope, content, sourceHash) {
|
|
862
|
-
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
863
|
-
const expiresAt = new Date(Date.now() + STALENESS_DAYS * 24 * 60 * 60 * 1e3).toISOString();
|
|
864
|
-
if (this.#db.db.prepare("SELECT id FROM syntheses WHERE scope = ?").get(scope) !== void 0) this.#db.db.prepare(`UPDATE syntheses
|
|
181
|
+
${s}
|
|
182
|
+
${c}
|
|
183
|
+
`;return this.#e.db.prepare(l).all(...o).map(e=>({...j(e,[]),cosineSim:e.cosine_sim}))}},xe=class{#e;#t;#n;constructor(e,t,n){this.#e=e,this.#t=t,this.#n=n}async query(e){return X(e,{adapter:new be(this.#e),repo:this.#n,embedder:this.#t})}};function Z(e,t){let n=t.repo.stats();return e.synthesis!==void 0&&e.synthesis.length>0?{stats:n.byType,pinnedGlobal:[],pinnedProject:[],synthesis:e.synthesis}:{stats:n.byType,pinnedGlobal:t.repo.listPinnedGlobal(),pinnedProject:t.repo.listPinnedForProject(e.projectHash)}}function Se(){return[..._]}var Ce=class{#e;constructor(e){this.#e=e}getSessionContext(e,t){return Z({projectHash:e,synthesis:t},{repo:this.#e})}},we=class{#e;#t;#n;#r=new Set;#i=new Map;#a=!1;#o;#s=new Map;constructor(e,t,n){this.#e=e,this.#t=t,this.#n=n}async init(){this.#e.clearStaleInFlight(this.#t.inFlightTimeoutMs??12e4),this.#e.expireStale();let e=this.#e.getExpiredOrDirtyScopes();for(let{scope:t}of e)this.#r.add(t);this.#a=!0,await this.#l()}shutdown(){this.#a=!1,this.#o!==void 0&&(clearTimeout(this.#o),this.#o=void 0);let e=[...this.#s.values()];return e.length===0?Promise.resolve():Promise.race([Promise.allSettled(e).then(()=>void 0),new Promise(e=>setTimeout(e,5e3))])}markDirty(e){this.#r.add(e)}#c(){if(!this.#a)return;let e=this.#t.debounceMs??45e3;this.#o=setTimeout(()=>{this.#l()},e)}async#l(){let e=[...this.#r];for(let t of e){let e=this.#t.inFlightTimeoutMs??12e4,n=this.#e.getSynthesis(t);if(n?.inFlightSince!==null&&n?.inFlightSince!==void 0){if(Date.now()-new Date(n.inFlightSince).getTime()<e)continue;this.#e.clearInFlight(t)}this.#r.delete(t);let r=this.#u(t).finally(()=>{this.#s.delete(t)});this.#s.set(t,r)}this.#c()}async#u(e){this.#e.markInFlight(e);try{let t=e===`global`?void 0:e,n=await this.#n.run(e,t),r=this.#e.computeSourceMemoryHash(e);this.#e.saveSynthesis(e,n,r),this.#i.delete(e)}catch(t){let n=(this.#i.get(e)??0)+1;this.#i.set(e,n);let r=Math.min(n,5),i=(this.#t.debounceMs??45e3)*r;process.stderr.write(`membank synthesis: error for scope=${e} failures=${n} backoff=${i}ms: ${t instanceof Error?t.message:String(t)}\n`),setTimeout(()=>{this.#r.add(e)},i),this.#e.clearInFlight(e)}}};async function Q(e,t){let n=e===`global`?void 0:e;t.synthRepo.markInFlight(e);try{let[r,i]=await Promise.all([t.agentRunner.run(e,n),Promise.resolve(t.synthRepo.computeSourceMemoryHash(e))]);return t.synthRepo.saveSynthesis(e,r,i),r}catch(n){throw t.synthRepo.clearInFlight(e),n}}var Te=class{#e;constructor(e,t){this.#e=e}async run(e,t){let n=(0,g.createSdkMcpServer)({name:`membank-synthesis-tools`,version:`1.0.0`,tools:[(0,g.tool)(`query_memory`,`Search memories by semantic similarity`,{query:d.z.string().describe(`Search text`),limit:d.z.number().optional().describe(`Maximum results to return`),global:d.z.boolean().optional().describe(`Query global memories only when true, otherwise current project scope`)},async({query:e,limit:n,global:r})=>({content:[{type:`text`,text:await this.#e.queryMemory({query:e,limit:n,global:r,projectHash:t})}]}),{annotations:{readOnlyHint:!0}}),(0,g.tool)(`get_memory_summary`,`Returns aggregate stats: total memories, counts by type, pinned count, review queue size`,{},async()=>({content:[{type:`text`,text:await this.#e.getMemorySummary()}]}),{annotations:{readOnlyHint:!0}})]}),r=`Synthesize the memories for ${e===`global`?`global (across all projects)`:`project scope: ${e}`}. Use get_memory_summary first to understand the overall state, then use query_memory to retrieve relevant memories (query with broad terms like "preferences", "corrections", "decisions", "key facts"). After gathering information, produce a concise synthesis of the most important things to remember about this user. Output only the synthesis text — no preamble, no metadata.`,i=Date.now(),a=Object.fromEntries(Object.entries(process.env).filter(e=>e[1]!==void 0)),o=(0,g.query)({prompt:r,options:{model:`claude-haiku-4-5-20251001`,systemPrompt:`You are a memory synthesizer. Your job is to read the user's stored memories and produce a concise, well-structured summary of what's most important to remember about this user — their preferences, corrections, decisions, and key facts. Pinned memories are higher fidelity and should be weighted more heavily. Exclude transient or ephemeral details. Output plain text suitable for injection into an LLM context window. Be concise — target 200-400 words.`,mcpServers:{"membank-synthesis-tools":n},allowedTools:[`query_memory`,`get_memory_summary`],permissionMode:`bypassPermissions`,env:a}}),s=``;for await(let e of o)if(e.type===`result`)if(e.subtype===`success`)s=e.result;else{let t=`errors`in e&&Array.isArray(e.errors)?`: ${e.errors.join(`; `)}`:``;throw Error(`Synthesis agent failed: ${e.subtype}${t}`)}let c=Date.now()-i;if(process.stderr.write(`membank synthesis: scope=${e} duration=${c}ms\n`),s===``)throw Error(`Synthesis agent returned empty result`);return s}};function Ee(e,t){return new Te(e,t)}function $(e){return O.parse({id:e.id,scope:e.scope,content:e.content,sourceMemoryHash:e.source_memory_hash,synthesizedAt:e.synthesized_at,expiresAt:e.expires_at,inFlightSince:e.in_flight_since,createdAt:e.created_at,updatedAt:e.updated_at})}var De=class{#e;constructor(e){this.#e=e}saveSynthesis(e,t,n){let r=new Date().toISOString(),i=new Date(Date.now()+720*60*60*1e3).toISOString();if(this.#e.db.prepare(`SELECT id FROM syntheses WHERE scope = ?`).get(e)!==void 0)this.#e.db.prepare(`UPDATE syntheses
|
|
865
184
|
SET content = ?, source_memory_hash = ?, synthesized_at = ?, expires_at = ?,
|
|
866
185
|
in_flight_since = NULL, updated_at = ?
|
|
867
|
-
WHERE scope = ?`).run(
|
|
868
|
-
else {
|
|
869
|
-
const id = (0, node_crypto.randomUUID)();
|
|
870
|
-
this.#db.db.prepare(`INSERT INTO syntheses
|
|
186
|
+
WHERE scope = ?`).run(t,n,r,i,r,e);else{let a=(0,h.randomUUID)();this.#e.db.prepare(`INSERT INTO syntheses
|
|
871
187
|
(id, scope, content, source_memory_hash, synthesized_at, expires_at,
|
|
872
188
|
in_flight_since, created_at, updated_at)
|
|
873
|
-
VALUES (?, ?, ?, ?, ?, ?, NULL, ?, ?)`).run(
|
|
874
|
-
}
|
|
875
|
-
const row = this.#db.db.prepare("SELECT * FROM syntheses WHERE scope = ?").get(scope);
|
|
876
|
-
if (row === void 0) throw new Error(`Failed to save synthesis for scope: ${scope}`);
|
|
877
|
-
return rowToSynthesis(row);
|
|
878
|
-
}
|
|
879
|
-
getSynthesis(scope) {
|
|
880
|
-
const row = this.#db.db.prepare("SELECT * FROM syntheses WHERE scope = ?").get(scope);
|
|
881
|
-
return row !== void 0 ? rowToSynthesis(row) : void 0;
|
|
882
|
-
}
|
|
883
|
-
markInFlight(scope) {
|
|
884
|
-
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
885
|
-
if (this.#db.db.prepare("SELECT id FROM syntheses WHERE scope = ?").get(scope) !== void 0) this.#db.db.prepare("UPDATE syntheses SET in_flight_since = ?, updated_at = ? WHERE scope = ?").run(now, now, scope);
|
|
886
|
-
else {
|
|
887
|
-
const id = (0, node_crypto.randomUUID)();
|
|
888
|
-
const placeholder = "pending";
|
|
889
|
-
const future = new Date(Date.now() + STALENESS_DAYS * 24 * 60 * 60 * 1e3).toISOString();
|
|
890
|
-
this.#db.db.prepare(`INSERT INTO syntheses
|
|
189
|
+
VALUES (?, ?, ?, ?, ?, ?, NULL, ?, ?)`).run(a,e,t,n,r,i,r,r)}let a=this.#e.db.prepare(`SELECT * FROM syntheses WHERE scope = ?`).get(e);if(a===void 0)throw Error(`Failed to save synthesis for scope: ${e}`);return $(a)}getSynthesis(e){let t=this.#e.db.prepare(`SELECT * FROM syntheses WHERE scope = ?`).get(e);return t===void 0?void 0:$(t)}listAll(){return this.#e.db.prepare(`SELECT * FROM syntheses ORDER BY scope`).all().map($)}markInFlight(e){let t=new Date().toISOString();if(this.#e.db.prepare(`SELECT id FROM syntheses WHERE scope = ?`).get(e)!==void 0)this.#e.db.prepare(`UPDATE syntheses SET in_flight_since = ?, updated_at = ? WHERE scope = ?`).run(t,t,e);else{let n=(0,h.randomUUID)(),r=new Date(Date.now()+720*60*60*1e3).toISOString();this.#e.db.prepare(`INSERT INTO syntheses
|
|
891
190
|
(id, scope, content, source_memory_hash, synthesized_at, expires_at,
|
|
892
191
|
in_flight_since, created_at, updated_at)
|
|
893
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`).run(
|
|
894
|
-
}
|
|
895
|
-
}
|
|
896
|
-
clearInFlight(scope) {
|
|
897
|
-
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
898
|
-
this.#db.db.prepare("UPDATE syntheses SET in_flight_since = NULL, updated_at = ? WHERE scope = ?").run(now, scope);
|
|
899
|
-
}
|
|
900
|
-
clearStaleInFlight(thresholdMs) {
|
|
901
|
-
const cutoff = new Date(Date.now() - thresholdMs).toISOString();
|
|
902
|
-
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
903
|
-
this.#db.db.prepare("UPDATE syntheses SET in_flight_since = NULL, updated_at = ? WHERE in_flight_since IS NOT NULL AND in_flight_since < ?").run(now, cutoff);
|
|
904
|
-
}
|
|
905
|
-
computeSourceMemoryHash(scope) {
|
|
906
|
-
let contents;
|
|
907
|
-
if (scope === "global") contents = this.#db.db.prepare(`SELECT content FROM memories
|
|
192
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`).run(n,e,`pending`,``,t,r,t,t,t)}}clearInFlight(e){let t=new Date().toISOString();this.#e.db.prepare(`UPDATE syntheses SET in_flight_since = NULL, updated_at = ? WHERE scope = ?`).run(t,e)}clearStaleInFlight(e){let t=new Date(Date.now()-e).toISOString(),n=new Date().toISOString();this.#e.db.prepare(`UPDATE syntheses SET in_flight_since = NULL, updated_at = ? WHERE in_flight_since IS NOT NULL AND in_flight_since < ?`).run(n,t)}computeSourceMemoryHash(e){let t;return t=e===`global`?this.#e.db.prepare(`SELECT content FROM memories
|
|
908
193
|
WHERE id NOT IN (SELECT memory_id FROM memory_projects)
|
|
909
|
-
ORDER BY id`).all()
|
|
910
|
-
else contents = this.#db.db.prepare(`SELECT m.content FROM memories m
|
|
194
|
+
ORDER BY id`).all():this.#e.db.prepare(`SELECT m.content FROM memories m
|
|
911
195
|
JOIN memory_projects mp ON mp.memory_id = m.id
|
|
912
196
|
JOIN projects p ON p.id = mp.project_id
|
|
913
197
|
WHERE p.scope_hash = ?
|
|
914
|
-
ORDER BY m.id`).all(scope);
|
|
915
|
-
return (0, node_crypto.createHash)("sha256").update(JSON.stringify(contents.map((r) => r.content))).digest("hex");
|
|
916
|
-
}
|
|
917
|
-
getExpiredOrDirtyScopes() {
|
|
918
|
-
const allScopes = this.getAllActiveScopes();
|
|
919
|
-
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
920
|
-
const results = [];
|
|
921
|
-
for (const scope of allScopes) {
|
|
922
|
-
const row = this.#db.db.prepare("SELECT * FROM syntheses WHERE scope = ?").get(scope);
|
|
923
|
-
if (row === void 0 || row.content === "pending" && row.source_memory_hash === "") {
|
|
924
|
-
results.push({
|
|
925
|
-
scope,
|
|
926
|
-
reason: "missing"
|
|
927
|
-
});
|
|
928
|
-
continue;
|
|
929
|
-
}
|
|
930
|
-
if (row.expires_at <= now) {
|
|
931
|
-
results.push({
|
|
932
|
-
scope,
|
|
933
|
-
reason: "expired"
|
|
934
|
-
});
|
|
935
|
-
continue;
|
|
936
|
-
}
|
|
937
|
-
if (this.computeSourceMemoryHash(scope) !== row.source_memory_hash) results.push({
|
|
938
|
-
scope,
|
|
939
|
-
reason: "dirty"
|
|
940
|
-
});
|
|
941
|
-
}
|
|
942
|
-
return results;
|
|
943
|
-
}
|
|
944
|
-
getAllActiveScopes() {
|
|
945
|
-
return ["global", ...this.#db.db.prepare("SELECT DISTINCT scope_hash FROM projects").all().map((r) => r.scope_hash)];
|
|
946
|
-
}
|
|
947
|
-
expireStale() {
|
|
948
|
-
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
949
|
-
this.#db.db.prepare("DELETE FROM syntheses WHERE expires_at < ?").run(now);
|
|
950
|
-
}
|
|
951
|
-
};
|
|
952
|
-
//#endregion
|
|
953
|
-
exports.DatabaseError = DatabaseError;
|
|
954
|
-
exports.DatabaseManager = DatabaseManager;
|
|
955
|
-
exports.EmbeddingService = EmbeddingService;
|
|
956
|
-
exports.MEMORY_TYPE_VALUES = MEMORY_TYPE_VALUES;
|
|
957
|
-
exports.MIGRATIONS = MIGRATIONS;
|
|
958
|
-
exports.MembankError = MembankError;
|
|
959
|
-
exports.MemoryPatchSchema = MemoryPatchSchema;
|
|
960
|
-
exports.MemoryRepository = MemoryRepository;
|
|
961
|
-
exports.MemoryRowSchema = MemoryRowSchema;
|
|
962
|
-
exports.MemorySchema = MemorySchema;
|
|
963
|
-
exports.MemoryTypeSchema = MemoryTypeSchema;
|
|
964
|
-
exports.PIN_BUDGET_THRESHOLD = PIN_BUDGET_THRESHOLD;
|
|
965
|
-
exports.ProjectRepository = ProjectRepository;
|
|
966
|
-
exports.ProjectRowSchema = ProjectRowSchema;
|
|
967
|
-
exports.ProjectSchema = ProjectSchema;
|
|
968
|
-
exports.QueryEngine = QueryEngine;
|
|
969
|
-
exports.QueryOptionsSchema = QueryOptionsSchema;
|
|
970
|
-
exports.ReviewEventRowSchema = ReviewEventRowSchema;
|
|
971
|
-
exports.ReviewEventSchema = ReviewEventSchema;
|
|
972
|
-
exports.ReviewReasonSchema = ReviewReasonSchema;
|
|
973
|
-
exports.SaveOptionsSchema = SaveOptionsSchema;
|
|
974
|
-
exports.SessionContextBuilder = SessionContextBuilder;
|
|
975
|
-
exports.SessionContextSchema = SessionContextSchema;
|
|
976
|
-
exports.SynthesisRepository = SynthesisRepository;
|
|
977
|
-
exports.SynthesisSchema = SynthesisSchema;
|
|
978
|
-
exports.TagsJsonSchema = TagsJsonSchema;
|
|
979
|
-
exports.isSynthesisEnabled = isSynthesisEnabled;
|
|
980
|
-
exports.listMemoryTypes = listMemoryTypes;
|
|
981
|
-
exports.resolveProject = resolveProject;
|
|
982
|
-
exports.resolveScope = resolveScope;
|
|
983
|
-
exports.rowToMemory = rowToMemory;
|
|
984
|
-
exports.rowToProject = rowToProject;
|
|
985
|
-
exports.runScopeToProjectsMigration = runScopeToProjectsMigration;
|
|
198
|
+
ORDER BY m.id`).all(e),(0,h.createHash)(`sha256`).update(JSON.stringify(t.map(e=>e.content))).digest(`hex`)}getExpiredOrDirtyScopes(){let e=this.getAllActiveScopes(),t=new Date().toISOString(),n=[];for(let r of e){let e=this.#e.db.prepare(`SELECT * FROM syntheses WHERE scope = ?`).get(r);if(e===void 0||e.content===`pending`&&e.source_memory_hash===``){n.push({scope:r,reason:`missing`});continue}if(e.expires_at<=t){n.push({scope:r,reason:`expired`});continue}this.computeSourceMemoryHash(r)!==e.source_memory_hash&&n.push({scope:r,reason:`dirty`})}return n}getAllActiveScopes(){return[`global`,...this.#e.db.prepare(`SELECT DISTINCT scope_hash FROM projects`).all().map(e=>e.scope_hash)]}expireStale(){let e=new Date().toISOString();this.#e.db.prepare(`DELETE FROM syntheses WHERE expires_at < ?`).run(e)}};function Oe(e){return new De(e)}exports.DatabaseError=F,exports.DatabaseManager=ce,exports.EmbeddingService=B,exports.MEMORY_TYPE_VALUES=_,exports.MIGRATIONS=me,exports.MODEL_NAME=I,exports.MembankError=P,exports.MemoryPatchSchema=D,exports.MemoryRowSchema=k,exports.MemorySchema=w,exports.MemoryTypeSchema=v,exports.ModelDownloadError=L,exports.ModelDownloader=z,exports.PIN_BUDGET_THRESHOLD=G,exports.ProjectRowSchema=A,exports.ProjectSchema=b,exports.QueryEngine=xe,exports.QueryOptionsSchema=T,exports.ReviewEventRowSchema=C,exports.ReviewEventSchema=S,exports.ReviewReasonSchema=x,exports.SaveOptionsSchema=E,exports.SessionContextBuilder=Ce,exports.SessionContextSchema=ae,exports.SqliteMemoryRepository=K,exports.SynthesisEngine=we,exports.SynthesisSchema=O,exports.TagsJsonSchema=y,exports.createMemoryRepository=fe,exports.createProjectRepository=_e,exports.createSynthesisAgentRunner=Ee,exports.createSynthesisRepository=Oe,exports.deleteMemory=V,exports.getSessionContext=Z,exports.isOverBudget=de,exports.isSynthesisEnabled=ie,exports.listMemoryTypes=Se,exports.queryMemories=X,exports.resolveProject=Y,exports.resolveReview=H,exports.resolveScope=pe,exports.rowToMemory=j,exports.rowToProject=N,exports.runScopeToProjectsMigration=he,exports.runSynthesis=Q,exports.saveMemory=W,exports.updateMemory=ue;
|