mem0ai 2.2.3 → 2.3.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/oss/index.d.mts +0 -3
- package/dist/oss/index.d.ts +0 -3
- package/dist/oss/index.js +80 -110
- package/dist/oss/index.js.map +1 -1
- package/dist/oss/index.mjs +80 -110
- package/dist/oss/index.mjs.map +1 -1
- package/package.json +6 -6
package/dist/oss/index.mjs
CHANGED
|
@@ -112,9 +112,10 @@ var OllamaEmbedder = class {
|
|
|
112
112
|
} catch (err) {
|
|
113
113
|
logger.error(`Error ensuring model exists: ${err}`);
|
|
114
114
|
}
|
|
115
|
+
const prompt = typeof text === "string" ? text : JSON.stringify(text);
|
|
115
116
|
const response = await this.ollama.embeddings({
|
|
116
117
|
model: this.model,
|
|
117
|
-
prompt
|
|
118
|
+
prompt
|
|
118
119
|
});
|
|
119
120
|
return response.embedding;
|
|
120
121
|
}
|
|
@@ -408,7 +409,7 @@ var MistralLLM = class {
|
|
|
408
409
|
};
|
|
409
410
|
|
|
410
411
|
// src/oss/src/vector_stores/memory.ts
|
|
411
|
-
import
|
|
412
|
+
import Database from "better-sqlite3";
|
|
412
413
|
import path from "path";
|
|
413
414
|
var MemoryVectorStore = class {
|
|
414
415
|
constructor(config) {
|
|
@@ -417,48 +418,24 @@ var MemoryVectorStore = class {
|
|
|
417
418
|
if (config.dbPath) {
|
|
418
419
|
this.dbPath = config.dbPath;
|
|
419
420
|
}
|
|
420
|
-
this.db = new
|
|
421
|
-
this.init()
|
|
421
|
+
this.db = new Database(this.dbPath);
|
|
422
|
+
this.init();
|
|
422
423
|
}
|
|
423
|
-
|
|
424
|
-
|
|
424
|
+
init() {
|
|
425
|
+
this.db.exec(`
|
|
425
426
|
CREATE TABLE IF NOT EXISTS vectors (
|
|
426
427
|
id TEXT PRIMARY KEY,
|
|
427
428
|
vector BLOB NOT NULL,
|
|
428
429
|
payload TEXT NOT NULL
|
|
429
430
|
)
|
|
430
431
|
`);
|
|
431
|
-
|
|
432
|
+
this.db.exec(`
|
|
432
433
|
CREATE TABLE IF NOT EXISTS memory_migrations (
|
|
433
434
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
434
435
|
user_id TEXT NOT NULL UNIQUE
|
|
435
436
|
)
|
|
436
437
|
`);
|
|
437
438
|
}
|
|
438
|
-
async run(sql, params = []) {
|
|
439
|
-
return new Promise((resolve, reject) => {
|
|
440
|
-
this.db.run(sql, params, (err) => {
|
|
441
|
-
if (err) reject(err);
|
|
442
|
-
else resolve();
|
|
443
|
-
});
|
|
444
|
-
});
|
|
445
|
-
}
|
|
446
|
-
async all(sql, params = []) {
|
|
447
|
-
return new Promise((resolve, reject) => {
|
|
448
|
-
this.db.all(sql, params, (err, rows) => {
|
|
449
|
-
if (err) reject(err);
|
|
450
|
-
else resolve(rows);
|
|
451
|
-
});
|
|
452
|
-
});
|
|
453
|
-
}
|
|
454
|
-
async getOne(sql, params = []) {
|
|
455
|
-
return new Promise((resolve, reject) => {
|
|
456
|
-
this.db.get(sql, params, (err, row) => {
|
|
457
|
-
if (err) reject(err);
|
|
458
|
-
else resolve(row);
|
|
459
|
-
});
|
|
460
|
-
});
|
|
461
|
-
}
|
|
462
439
|
cosineSimilarity(a, b) {
|
|
463
440
|
let dotProduct = 0;
|
|
464
441
|
let normA = 0;
|
|
@@ -477,18 +454,23 @@ var MemoryVectorStore = class {
|
|
|
477
454
|
);
|
|
478
455
|
}
|
|
479
456
|
async insert(vectors, ids, payloads) {
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
457
|
+
const stmt = this.db.prepare(
|
|
458
|
+
`INSERT OR REPLACE INTO vectors (id, vector, payload) VALUES (?, ?, ?)`
|
|
459
|
+
);
|
|
460
|
+
const insertMany = this.db.transaction(
|
|
461
|
+
(vecs, vIds, vPayloads) => {
|
|
462
|
+
for (let i = 0; i < vecs.length; i++) {
|
|
463
|
+
if (vecs[i].length !== this.dimension) {
|
|
464
|
+
throw new Error(
|
|
465
|
+
`Vector dimension mismatch. Expected ${this.dimension}, got ${vecs[i].length}`
|
|
466
|
+
);
|
|
467
|
+
}
|
|
468
|
+
const vectorBuffer = Buffer.from(new Float32Array(vecs[i]).buffer);
|
|
469
|
+
stmt.run(vIds[i], vectorBuffer, JSON.stringify(vPayloads[i]));
|
|
470
|
+
}
|
|
485
471
|
}
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
`INSERT OR REPLACE INTO vectors (id, vector, payload) VALUES (?, ?, ?)`,
|
|
489
|
-
[ids[i], vectorBuffer, JSON.stringify(payloads[i])]
|
|
490
|
-
);
|
|
491
|
-
}
|
|
472
|
+
);
|
|
473
|
+
insertMany(vectors, ids, payloads);
|
|
492
474
|
}
|
|
493
475
|
async search(query, limit = 10, filters) {
|
|
494
476
|
if (query.length !== this.dimension) {
|
|
@@ -496,10 +478,14 @@ var MemoryVectorStore = class {
|
|
|
496
478
|
`Query dimension mismatch. Expected ${this.dimension}, got ${query.length}`
|
|
497
479
|
);
|
|
498
480
|
}
|
|
499
|
-
const rows =
|
|
481
|
+
const rows = this.db.prepare(`SELECT * FROM vectors`).all();
|
|
500
482
|
const results = [];
|
|
501
483
|
for (const row of rows) {
|
|
502
|
-
const vector = new Float32Array(
|
|
484
|
+
const vector = new Float32Array(
|
|
485
|
+
row.vector.buffer,
|
|
486
|
+
row.vector.byteOffset,
|
|
487
|
+
row.vector.byteLength / 4
|
|
488
|
+
);
|
|
503
489
|
const payload = JSON.parse(row.payload);
|
|
504
490
|
const memoryVector = {
|
|
505
491
|
id: row.id,
|
|
@@ -519,9 +505,7 @@ var MemoryVectorStore = class {
|
|
|
519
505
|
return results.slice(0, limit);
|
|
520
506
|
}
|
|
521
507
|
async get(vectorId) {
|
|
522
|
-
const row =
|
|
523
|
-
vectorId
|
|
524
|
-
]);
|
|
508
|
+
const row = this.db.prepare(`SELECT * FROM vectors WHERE id = ?`).get(vectorId);
|
|
525
509
|
if (!row) return null;
|
|
526
510
|
const payload = JSON.parse(row.payload);
|
|
527
511
|
return {
|
|
@@ -536,27 +520,29 @@ var MemoryVectorStore = class {
|
|
|
536
520
|
);
|
|
537
521
|
}
|
|
538
522
|
const vectorBuffer = Buffer.from(new Float32Array(vector).buffer);
|
|
539
|
-
|
|
540
|
-
vectorBuffer,
|
|
541
|
-
JSON.stringify(payload),
|
|
542
|
-
vectorId
|
|
543
|
-
]);
|
|
523
|
+
this.db.prepare(`UPDATE vectors SET vector = ?, payload = ? WHERE id = ?`).run(vectorBuffer, JSON.stringify(payload), vectorId);
|
|
544
524
|
}
|
|
545
525
|
async delete(vectorId) {
|
|
546
|
-
|
|
526
|
+
this.db.prepare(`DELETE FROM vectors WHERE id = ?`).run(vectorId);
|
|
547
527
|
}
|
|
548
528
|
async deleteCol() {
|
|
549
|
-
|
|
550
|
-
|
|
529
|
+
this.db.exec(`DROP TABLE IF EXISTS vectors`);
|
|
530
|
+
this.init();
|
|
551
531
|
}
|
|
552
532
|
async list(filters, limit = 100) {
|
|
553
|
-
const rows =
|
|
533
|
+
const rows = this.db.prepare(`SELECT * FROM vectors`).all();
|
|
554
534
|
const results = [];
|
|
555
535
|
for (const row of rows) {
|
|
556
536
|
const payload = JSON.parse(row.payload);
|
|
557
537
|
const memoryVector = {
|
|
558
538
|
id: row.id,
|
|
559
|
-
vector: Array.from(
|
|
539
|
+
vector: Array.from(
|
|
540
|
+
new Float32Array(
|
|
541
|
+
row.vector.buffer,
|
|
542
|
+
row.vector.byteOffset,
|
|
543
|
+
row.vector.byteLength / 4
|
|
544
|
+
)
|
|
545
|
+
),
|
|
560
546
|
payload
|
|
561
547
|
};
|
|
562
548
|
if (this.filterVector(memoryVector, filters)) {
|
|
@@ -569,26 +555,20 @@ var MemoryVectorStore = class {
|
|
|
569
555
|
return [results.slice(0, limit), results.length];
|
|
570
556
|
}
|
|
571
557
|
async getUserId() {
|
|
572
|
-
const row =
|
|
573
|
-
`SELECT user_id FROM memory_migrations LIMIT 1`
|
|
574
|
-
);
|
|
558
|
+
const row = this.db.prepare(`SELECT user_id FROM memory_migrations LIMIT 1`).get();
|
|
575
559
|
if (row) {
|
|
576
560
|
return row.user_id;
|
|
577
561
|
}
|
|
578
562
|
const randomUserId = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
|
|
579
|
-
|
|
580
|
-
randomUserId
|
|
581
|
-
]);
|
|
563
|
+
this.db.prepare(`INSERT INTO memory_migrations (user_id) VALUES (?)`).run(randomUserId);
|
|
582
564
|
return randomUserId;
|
|
583
565
|
}
|
|
584
566
|
async setUserId(userId) {
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
userId
|
|
588
|
-
]);
|
|
567
|
+
this.db.prepare(`DELETE FROM memory_migrations`).run();
|
|
568
|
+
this.db.prepare(`INSERT INTO memory_migrations (user_id) VALUES (?)`).run(userId);
|
|
589
569
|
}
|
|
590
570
|
async initialize() {
|
|
591
|
-
|
|
571
|
+
this.init();
|
|
592
572
|
}
|
|
593
573
|
};
|
|
594
574
|
|
|
@@ -2010,14 +1990,14 @@ See the SQL migration instructions in the code comments.`
|
|
|
2010
1990
|
};
|
|
2011
1991
|
|
|
2012
1992
|
// src/oss/src/storage/SQLiteManager.ts
|
|
2013
|
-
import
|
|
1993
|
+
import Database2 from "better-sqlite3";
|
|
2014
1994
|
var SQLiteManager = class {
|
|
2015
1995
|
constructor(dbPath) {
|
|
2016
|
-
this.db = new
|
|
2017
|
-
this.init()
|
|
1996
|
+
this.db = new Database2(dbPath);
|
|
1997
|
+
this.init();
|
|
2018
1998
|
}
|
|
2019
|
-
|
|
2020
|
-
|
|
1999
|
+
init() {
|
|
2000
|
+
this.db.exec(`
|
|
2021
2001
|
CREATE TABLE IF NOT EXISTS memory_history (
|
|
2022
2002
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
2023
2003
|
memory_id TEXT NOT NULL,
|
|
@@ -2029,48 +2009,32 @@ var SQLiteManager = class {
|
|
|
2029
2009
|
is_deleted INTEGER DEFAULT 0
|
|
2030
2010
|
)
|
|
2031
2011
|
`);
|
|
2032
|
-
|
|
2033
|
-
|
|
2034
|
-
|
|
2035
|
-
|
|
2036
|
-
|
|
2037
|
-
|
|
2038
|
-
|
|
2039
|
-
|
|
2040
|
-
}
|
|
2041
|
-
async all(sql, params = []) {
|
|
2042
|
-
return new Promise((resolve, reject) => {
|
|
2043
|
-
this.db.all(sql, params, (err, rows) => {
|
|
2044
|
-
if (err) reject(err);
|
|
2045
|
-
else resolve(rows);
|
|
2046
|
-
});
|
|
2047
|
-
});
|
|
2012
|
+
this.stmtInsert = this.db.prepare(
|
|
2013
|
+
`INSERT INTO memory_history
|
|
2014
|
+
(memory_id, previous_value, new_value, action, created_at, updated_at, is_deleted)
|
|
2015
|
+
VALUES (?, ?, ?, ?, ?, ?, ?)`
|
|
2016
|
+
);
|
|
2017
|
+
this.stmtSelect = this.db.prepare(
|
|
2018
|
+
"SELECT * FROM memory_history WHERE memory_id = ? ORDER BY id DESC"
|
|
2019
|
+
);
|
|
2048
2020
|
}
|
|
2049
2021
|
async addHistory(memoryId, previousValue, newValue, action, createdAt, updatedAt, isDeleted = 0) {
|
|
2050
|
-
|
|
2051
|
-
|
|
2052
|
-
|
|
2053
|
-
|
|
2054
|
-
|
|
2055
|
-
|
|
2056
|
-
|
|
2057
|
-
|
|
2058
|
-
action,
|
|
2059
|
-
createdAt,
|
|
2060
|
-
updatedAt,
|
|
2061
|
-
isDeleted
|
|
2062
|
-
]
|
|
2022
|
+
this.stmtInsert.run(
|
|
2023
|
+
memoryId,
|
|
2024
|
+
previousValue,
|
|
2025
|
+
newValue,
|
|
2026
|
+
action,
|
|
2027
|
+
createdAt != null ? createdAt : null,
|
|
2028
|
+
updatedAt != null ? updatedAt : null,
|
|
2029
|
+
isDeleted
|
|
2063
2030
|
);
|
|
2064
2031
|
}
|
|
2065
2032
|
async getHistory(memoryId) {
|
|
2066
|
-
return this.all(
|
|
2067
|
-
"SELECT * FROM memory_history WHERE memory_id = ? ORDER BY id DESC",
|
|
2068
|
-
[memoryId]
|
|
2069
|
-
);
|
|
2033
|
+
return this.stmtSelect.all(memoryId);
|
|
2070
2034
|
}
|
|
2071
2035
|
async reset() {
|
|
2072
|
-
|
|
2073
|
-
|
|
2036
|
+
this.db.exec("DROP TABLE IF EXISTS memory_history");
|
|
2037
|
+
this.init();
|
|
2074
2038
|
}
|
|
2075
2039
|
close() {
|
|
2076
2040
|
this.db.close();
|
|
@@ -2346,8 +2310,13 @@ import {
|
|
|
2346
2310
|
|
|
2347
2311
|
// src/oss/src/prompts/index.ts
|
|
2348
2312
|
import { z as z2 } from "zod";
|
|
2313
|
+
var factItem = z2.union([
|
|
2314
|
+
z2.string(),
|
|
2315
|
+
z2.object({ fact: z2.string() }).transform((o) => o.fact),
|
|
2316
|
+
z2.object({ text: z2.string() }).transform((o) => o.text)
|
|
2317
|
+
]);
|
|
2349
2318
|
var FactRetrievalSchema = z2.object({
|
|
2350
|
-
facts: z2.array(
|
|
2319
|
+
facts: z2.array(factItem).transform((arr) => arr.filter((s) => s.length > 0)).describe("An array of distinct facts extracted from the conversation.")
|
|
2351
2320
|
});
|
|
2352
2321
|
var MemoryUpdateSchema = z2.object({
|
|
2353
2322
|
memory: z2.array(
|
|
@@ -4659,7 +4628,8 @@ ${parsedMessages}`
|
|
|
4659
4628
|
const cleanResponse = removeCodeBlocks(response);
|
|
4660
4629
|
let facts = [];
|
|
4661
4630
|
try {
|
|
4662
|
-
|
|
4631
|
+
const parsed = FactRetrievalSchema.parse(JSON.parse(cleanResponse));
|
|
4632
|
+
facts = parsed.facts;
|
|
4663
4633
|
} catch (e) {
|
|
4664
4634
|
console.error(
|
|
4665
4635
|
"Failed to parse facts from LLM response:",
|