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.d.mts
CHANGED
|
@@ -617,9 +617,6 @@ declare class MemoryVectorStore implements VectorStore {
|
|
|
617
617
|
private dbPath;
|
|
618
618
|
constructor(config: VectorStoreConfig);
|
|
619
619
|
private init;
|
|
620
|
-
private run;
|
|
621
|
-
private all;
|
|
622
|
-
private getOne;
|
|
623
620
|
private cosineSimilarity;
|
|
624
621
|
private filterVector;
|
|
625
622
|
insert(vectors: number[][], ids: string[], payloads: Record<string, any>[]): Promise<void>;
|
package/dist/oss/index.d.ts
CHANGED
|
@@ -617,9 +617,6 @@ declare class MemoryVectorStore implements VectorStore {
|
|
|
617
617
|
private dbPath;
|
|
618
618
|
constructor(config: VectorStoreConfig);
|
|
619
619
|
private init;
|
|
620
|
-
private run;
|
|
621
|
-
private all;
|
|
622
|
-
private getOne;
|
|
623
620
|
private cosineSimilarity;
|
|
624
621
|
private filterVector;
|
|
625
622
|
insert(vectors: number[][], ids: string[], payloads: Record<string, any>[]): Promise<void>;
|
package/dist/oss/index.js
CHANGED
|
@@ -173,9 +173,10 @@ var OllamaEmbedder = class {
|
|
|
173
173
|
} catch (err) {
|
|
174
174
|
logger.error(`Error ensuring model exists: ${err}`);
|
|
175
175
|
}
|
|
176
|
+
const prompt = typeof text === "string" ? text : JSON.stringify(text);
|
|
176
177
|
const response = await this.ollama.embeddings({
|
|
177
178
|
model: this.model,
|
|
178
|
-
prompt
|
|
179
|
+
prompt
|
|
179
180
|
});
|
|
180
181
|
return response.embedding;
|
|
181
182
|
}
|
|
@@ -469,7 +470,7 @@ var MistralLLM = class {
|
|
|
469
470
|
};
|
|
470
471
|
|
|
471
472
|
// src/oss/src/vector_stores/memory.ts
|
|
472
|
-
var
|
|
473
|
+
var import_better_sqlite3 = __toESM(require("better-sqlite3"));
|
|
473
474
|
var import_path = __toESM(require("path"));
|
|
474
475
|
var MemoryVectorStore = class {
|
|
475
476
|
constructor(config) {
|
|
@@ -478,48 +479,24 @@ var MemoryVectorStore = class {
|
|
|
478
479
|
if (config.dbPath) {
|
|
479
480
|
this.dbPath = config.dbPath;
|
|
480
481
|
}
|
|
481
|
-
this.db = new
|
|
482
|
-
this.init()
|
|
482
|
+
this.db = new import_better_sqlite3.default(this.dbPath);
|
|
483
|
+
this.init();
|
|
483
484
|
}
|
|
484
|
-
|
|
485
|
-
|
|
485
|
+
init() {
|
|
486
|
+
this.db.exec(`
|
|
486
487
|
CREATE TABLE IF NOT EXISTS vectors (
|
|
487
488
|
id TEXT PRIMARY KEY,
|
|
488
489
|
vector BLOB NOT NULL,
|
|
489
490
|
payload TEXT NOT NULL
|
|
490
491
|
)
|
|
491
492
|
`);
|
|
492
|
-
|
|
493
|
+
this.db.exec(`
|
|
493
494
|
CREATE TABLE IF NOT EXISTS memory_migrations (
|
|
494
495
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
495
496
|
user_id TEXT NOT NULL UNIQUE
|
|
496
497
|
)
|
|
497
498
|
`);
|
|
498
499
|
}
|
|
499
|
-
async run(sql, params = []) {
|
|
500
|
-
return new Promise((resolve, reject) => {
|
|
501
|
-
this.db.run(sql, params, (err) => {
|
|
502
|
-
if (err) reject(err);
|
|
503
|
-
else resolve();
|
|
504
|
-
});
|
|
505
|
-
});
|
|
506
|
-
}
|
|
507
|
-
async all(sql, params = []) {
|
|
508
|
-
return new Promise((resolve, reject) => {
|
|
509
|
-
this.db.all(sql, params, (err, rows) => {
|
|
510
|
-
if (err) reject(err);
|
|
511
|
-
else resolve(rows);
|
|
512
|
-
});
|
|
513
|
-
});
|
|
514
|
-
}
|
|
515
|
-
async getOne(sql, params = []) {
|
|
516
|
-
return new Promise((resolve, reject) => {
|
|
517
|
-
this.db.get(sql, params, (err, row) => {
|
|
518
|
-
if (err) reject(err);
|
|
519
|
-
else resolve(row);
|
|
520
|
-
});
|
|
521
|
-
});
|
|
522
|
-
}
|
|
523
500
|
cosineSimilarity(a, b) {
|
|
524
501
|
let dotProduct = 0;
|
|
525
502
|
let normA = 0;
|
|
@@ -538,18 +515,23 @@ var MemoryVectorStore = class {
|
|
|
538
515
|
);
|
|
539
516
|
}
|
|
540
517
|
async insert(vectors, ids, payloads) {
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
518
|
+
const stmt = this.db.prepare(
|
|
519
|
+
`INSERT OR REPLACE INTO vectors (id, vector, payload) VALUES (?, ?, ?)`
|
|
520
|
+
);
|
|
521
|
+
const insertMany = this.db.transaction(
|
|
522
|
+
(vecs, vIds, vPayloads) => {
|
|
523
|
+
for (let i = 0; i < vecs.length; i++) {
|
|
524
|
+
if (vecs[i].length !== this.dimension) {
|
|
525
|
+
throw new Error(
|
|
526
|
+
`Vector dimension mismatch. Expected ${this.dimension}, got ${vecs[i].length}`
|
|
527
|
+
);
|
|
528
|
+
}
|
|
529
|
+
const vectorBuffer = Buffer.from(new Float32Array(vecs[i]).buffer);
|
|
530
|
+
stmt.run(vIds[i], vectorBuffer, JSON.stringify(vPayloads[i]));
|
|
531
|
+
}
|
|
546
532
|
}
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
`INSERT OR REPLACE INTO vectors (id, vector, payload) VALUES (?, ?, ?)`,
|
|
550
|
-
[ids[i], vectorBuffer, JSON.stringify(payloads[i])]
|
|
551
|
-
);
|
|
552
|
-
}
|
|
533
|
+
);
|
|
534
|
+
insertMany(vectors, ids, payloads);
|
|
553
535
|
}
|
|
554
536
|
async search(query, limit = 10, filters) {
|
|
555
537
|
if (query.length !== this.dimension) {
|
|
@@ -557,10 +539,14 @@ var MemoryVectorStore = class {
|
|
|
557
539
|
`Query dimension mismatch. Expected ${this.dimension}, got ${query.length}`
|
|
558
540
|
);
|
|
559
541
|
}
|
|
560
|
-
const rows =
|
|
542
|
+
const rows = this.db.prepare(`SELECT * FROM vectors`).all();
|
|
561
543
|
const results = [];
|
|
562
544
|
for (const row of rows) {
|
|
563
|
-
const vector = new Float32Array(
|
|
545
|
+
const vector = new Float32Array(
|
|
546
|
+
row.vector.buffer,
|
|
547
|
+
row.vector.byteOffset,
|
|
548
|
+
row.vector.byteLength / 4
|
|
549
|
+
);
|
|
564
550
|
const payload = JSON.parse(row.payload);
|
|
565
551
|
const memoryVector = {
|
|
566
552
|
id: row.id,
|
|
@@ -580,9 +566,7 @@ var MemoryVectorStore = class {
|
|
|
580
566
|
return results.slice(0, limit);
|
|
581
567
|
}
|
|
582
568
|
async get(vectorId) {
|
|
583
|
-
const row =
|
|
584
|
-
vectorId
|
|
585
|
-
]);
|
|
569
|
+
const row = this.db.prepare(`SELECT * FROM vectors WHERE id = ?`).get(vectorId);
|
|
586
570
|
if (!row) return null;
|
|
587
571
|
const payload = JSON.parse(row.payload);
|
|
588
572
|
return {
|
|
@@ -597,27 +581,29 @@ var MemoryVectorStore = class {
|
|
|
597
581
|
);
|
|
598
582
|
}
|
|
599
583
|
const vectorBuffer = Buffer.from(new Float32Array(vector).buffer);
|
|
600
|
-
|
|
601
|
-
vectorBuffer,
|
|
602
|
-
JSON.stringify(payload),
|
|
603
|
-
vectorId
|
|
604
|
-
]);
|
|
584
|
+
this.db.prepare(`UPDATE vectors SET vector = ?, payload = ? WHERE id = ?`).run(vectorBuffer, JSON.stringify(payload), vectorId);
|
|
605
585
|
}
|
|
606
586
|
async delete(vectorId) {
|
|
607
|
-
|
|
587
|
+
this.db.prepare(`DELETE FROM vectors WHERE id = ?`).run(vectorId);
|
|
608
588
|
}
|
|
609
589
|
async deleteCol() {
|
|
610
|
-
|
|
611
|
-
|
|
590
|
+
this.db.exec(`DROP TABLE IF EXISTS vectors`);
|
|
591
|
+
this.init();
|
|
612
592
|
}
|
|
613
593
|
async list(filters, limit = 100) {
|
|
614
|
-
const rows =
|
|
594
|
+
const rows = this.db.prepare(`SELECT * FROM vectors`).all();
|
|
615
595
|
const results = [];
|
|
616
596
|
for (const row of rows) {
|
|
617
597
|
const payload = JSON.parse(row.payload);
|
|
618
598
|
const memoryVector = {
|
|
619
599
|
id: row.id,
|
|
620
|
-
vector: Array.from(
|
|
600
|
+
vector: Array.from(
|
|
601
|
+
new Float32Array(
|
|
602
|
+
row.vector.buffer,
|
|
603
|
+
row.vector.byteOffset,
|
|
604
|
+
row.vector.byteLength / 4
|
|
605
|
+
)
|
|
606
|
+
),
|
|
621
607
|
payload
|
|
622
608
|
};
|
|
623
609
|
if (this.filterVector(memoryVector, filters)) {
|
|
@@ -630,26 +616,20 @@ var MemoryVectorStore = class {
|
|
|
630
616
|
return [results.slice(0, limit), results.length];
|
|
631
617
|
}
|
|
632
618
|
async getUserId() {
|
|
633
|
-
const row =
|
|
634
|
-
`SELECT user_id FROM memory_migrations LIMIT 1`
|
|
635
|
-
);
|
|
619
|
+
const row = this.db.prepare(`SELECT user_id FROM memory_migrations LIMIT 1`).get();
|
|
636
620
|
if (row) {
|
|
637
621
|
return row.user_id;
|
|
638
622
|
}
|
|
639
623
|
const randomUserId = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
|
|
640
|
-
|
|
641
|
-
randomUserId
|
|
642
|
-
]);
|
|
624
|
+
this.db.prepare(`INSERT INTO memory_migrations (user_id) VALUES (?)`).run(randomUserId);
|
|
643
625
|
return randomUserId;
|
|
644
626
|
}
|
|
645
627
|
async setUserId(userId) {
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
userId
|
|
649
|
-
]);
|
|
628
|
+
this.db.prepare(`DELETE FROM memory_migrations`).run();
|
|
629
|
+
this.db.prepare(`INSERT INTO memory_migrations (user_id) VALUES (?)`).run(userId);
|
|
650
630
|
}
|
|
651
631
|
async initialize() {
|
|
652
|
-
|
|
632
|
+
this.init();
|
|
653
633
|
}
|
|
654
634
|
};
|
|
655
635
|
|
|
@@ -2071,14 +2051,14 @@ See the SQL migration instructions in the code comments.`
|
|
|
2071
2051
|
};
|
|
2072
2052
|
|
|
2073
2053
|
// src/oss/src/storage/SQLiteManager.ts
|
|
2074
|
-
var
|
|
2054
|
+
var import_better_sqlite32 = __toESM(require("better-sqlite3"));
|
|
2075
2055
|
var SQLiteManager = class {
|
|
2076
2056
|
constructor(dbPath) {
|
|
2077
|
-
this.db = new
|
|
2078
|
-
this.init()
|
|
2057
|
+
this.db = new import_better_sqlite32.default(dbPath);
|
|
2058
|
+
this.init();
|
|
2079
2059
|
}
|
|
2080
|
-
|
|
2081
|
-
|
|
2060
|
+
init() {
|
|
2061
|
+
this.db.exec(`
|
|
2082
2062
|
CREATE TABLE IF NOT EXISTS memory_history (
|
|
2083
2063
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
2084
2064
|
memory_id TEXT NOT NULL,
|
|
@@ -2090,48 +2070,32 @@ var SQLiteManager = class {
|
|
|
2090
2070
|
is_deleted INTEGER DEFAULT 0
|
|
2091
2071
|
)
|
|
2092
2072
|
`);
|
|
2093
|
-
|
|
2094
|
-
|
|
2095
|
-
|
|
2096
|
-
|
|
2097
|
-
|
|
2098
|
-
|
|
2099
|
-
|
|
2100
|
-
|
|
2101
|
-
}
|
|
2102
|
-
async all(sql, params = []) {
|
|
2103
|
-
return new Promise((resolve, reject) => {
|
|
2104
|
-
this.db.all(sql, params, (err, rows) => {
|
|
2105
|
-
if (err) reject(err);
|
|
2106
|
-
else resolve(rows);
|
|
2107
|
-
});
|
|
2108
|
-
});
|
|
2073
|
+
this.stmtInsert = this.db.prepare(
|
|
2074
|
+
`INSERT INTO memory_history
|
|
2075
|
+
(memory_id, previous_value, new_value, action, created_at, updated_at, is_deleted)
|
|
2076
|
+
VALUES (?, ?, ?, ?, ?, ?, ?)`
|
|
2077
|
+
);
|
|
2078
|
+
this.stmtSelect = this.db.prepare(
|
|
2079
|
+
"SELECT * FROM memory_history WHERE memory_id = ? ORDER BY id DESC"
|
|
2080
|
+
);
|
|
2109
2081
|
}
|
|
2110
2082
|
async addHistory(memoryId, previousValue, newValue, action, createdAt, updatedAt, isDeleted = 0) {
|
|
2111
|
-
|
|
2112
|
-
|
|
2113
|
-
|
|
2114
|
-
|
|
2115
|
-
|
|
2116
|
-
|
|
2117
|
-
|
|
2118
|
-
|
|
2119
|
-
action,
|
|
2120
|
-
createdAt,
|
|
2121
|
-
updatedAt,
|
|
2122
|
-
isDeleted
|
|
2123
|
-
]
|
|
2083
|
+
this.stmtInsert.run(
|
|
2084
|
+
memoryId,
|
|
2085
|
+
previousValue,
|
|
2086
|
+
newValue,
|
|
2087
|
+
action,
|
|
2088
|
+
createdAt != null ? createdAt : null,
|
|
2089
|
+
updatedAt != null ? updatedAt : null,
|
|
2090
|
+
isDeleted
|
|
2124
2091
|
);
|
|
2125
2092
|
}
|
|
2126
2093
|
async getHistory(memoryId) {
|
|
2127
|
-
return this.all(
|
|
2128
|
-
"SELECT * FROM memory_history WHERE memory_id = ? ORDER BY id DESC",
|
|
2129
|
-
[memoryId]
|
|
2130
|
-
);
|
|
2094
|
+
return this.stmtSelect.all(memoryId);
|
|
2131
2095
|
}
|
|
2132
2096
|
async reset() {
|
|
2133
|
-
|
|
2134
|
-
|
|
2097
|
+
this.db.exec("DROP TABLE IF EXISTS memory_history");
|
|
2098
|
+
this.init();
|
|
2135
2099
|
}
|
|
2136
2100
|
close() {
|
|
2137
2101
|
this.db.close();
|
|
@@ -2403,8 +2367,13 @@ var import_messages = require("@langchain/core/messages");
|
|
|
2403
2367
|
|
|
2404
2368
|
// src/oss/src/prompts/index.ts
|
|
2405
2369
|
var import_zod2 = require("zod");
|
|
2370
|
+
var factItem = import_zod2.z.union([
|
|
2371
|
+
import_zod2.z.string(),
|
|
2372
|
+
import_zod2.z.object({ fact: import_zod2.z.string() }).transform((o) => o.fact),
|
|
2373
|
+
import_zod2.z.object({ text: import_zod2.z.string() }).transform((o) => o.text)
|
|
2374
|
+
]);
|
|
2406
2375
|
var FactRetrievalSchema = import_zod2.z.object({
|
|
2407
|
-
facts: import_zod2.z.array(
|
|
2376
|
+
facts: import_zod2.z.array(factItem).transform((arr) => arr.filter((s) => s.length > 0)).describe("An array of distinct facts extracted from the conversation.")
|
|
2408
2377
|
});
|
|
2409
2378
|
var MemoryUpdateSchema = import_zod2.z.object({
|
|
2410
2379
|
memory: import_zod2.z.array(
|
|
@@ -4712,7 +4681,8 @@ ${parsedMessages}`
|
|
|
4712
4681
|
const cleanResponse = removeCodeBlocks(response);
|
|
4713
4682
|
let facts = [];
|
|
4714
4683
|
try {
|
|
4715
|
-
|
|
4684
|
+
const parsed = FactRetrievalSchema.parse(JSON.parse(cleanResponse));
|
|
4685
|
+
facts = parsed.facts;
|
|
4716
4686
|
} catch (e) {
|
|
4717
4687
|
console.error(
|
|
4718
4688
|
"Failed to parse facts from LLM response:",
|