mem0ai 2.2.4 → 2.4.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 +33 -3
- package/dist/oss/index.d.ts +33 -3
- package/dist/oss/index.js +255 -202
- package/dist/oss/index.js.map +1 -1
- package/dist/oss/index.mjs +255 -202
- package/dist/oss/index.mjs.map +1 -1
- package/package.json +5 -5
package/dist/oss/index.js
CHANGED
|
@@ -83,6 +83,7 @@ var MemoryConfigSchema = import_zod.z.object({
|
|
|
83
83
|
config: import_zod.z.object({
|
|
84
84
|
collectionName: import_zod.z.string().optional(),
|
|
85
85
|
dimension: import_zod.z.number().optional(),
|
|
86
|
+
dbPath: import_zod.z.string().optional(),
|
|
86
87
|
client: import_zod.z.any().optional()
|
|
87
88
|
}).passthrough()
|
|
88
89
|
}),
|
|
@@ -122,7 +123,10 @@ var MemoryConfigSchema = import_zod.z.object({
|
|
|
122
123
|
var import_openai = __toESM(require("openai"));
|
|
123
124
|
var OpenAIEmbedder = class {
|
|
124
125
|
constructor(config) {
|
|
125
|
-
this.openai = new import_openai.default({
|
|
126
|
+
this.openai = new import_openai.default({
|
|
127
|
+
apiKey: config.apiKey,
|
|
128
|
+
baseURL: config.baseURL || config.url
|
|
129
|
+
});
|
|
126
130
|
this.model = config.model || "text-embedding-3-small";
|
|
127
131
|
this.embeddingDims = config.embeddingDims || 1536;
|
|
128
132
|
}
|
|
@@ -470,57 +474,56 @@ var MistralLLM = class {
|
|
|
470
474
|
};
|
|
471
475
|
|
|
472
476
|
// src/oss/src/vector_stores/memory.ts
|
|
473
|
-
var
|
|
477
|
+
var import_better_sqlite3 = __toESM(require("better-sqlite3"));
|
|
478
|
+
var import_fs2 = __toESM(require("fs"));
|
|
479
|
+
var import_path2 = __toESM(require("path"));
|
|
480
|
+
|
|
481
|
+
// src/oss/src/utils/sqlite.ts
|
|
482
|
+
var import_fs = __toESM(require("fs"));
|
|
483
|
+
var import_os = __toESM(require("os"));
|
|
474
484
|
var import_path = __toESM(require("path"));
|
|
485
|
+
function getDefaultVectorStoreDbPath() {
|
|
486
|
+
return import_path.default.join(import_os.default.homedir(), ".mem0", "vector_store.db");
|
|
487
|
+
}
|
|
488
|
+
function ensureSQLiteDirectory(dbPath) {
|
|
489
|
+
if (!dbPath || dbPath === ":memory:" || dbPath.startsWith("file:")) {
|
|
490
|
+
return;
|
|
491
|
+
}
|
|
492
|
+
import_fs.default.mkdirSync(import_path.default.dirname(dbPath), { recursive: true });
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
// src/oss/src/vector_stores/memory.ts
|
|
475
496
|
var MemoryVectorStore = class {
|
|
476
497
|
constructor(config) {
|
|
477
498
|
this.dimension = config.dimension || 1536;
|
|
478
|
-
this.dbPath =
|
|
479
|
-
if (config.dbPath) {
|
|
480
|
-
|
|
499
|
+
this.dbPath = config.dbPath || getDefaultVectorStoreDbPath();
|
|
500
|
+
if (!config.dbPath) {
|
|
501
|
+
const oldDefault = import_path2.default.join(process.cwd(), "vector_store.db");
|
|
502
|
+
if (import_fs2.default.existsSync(oldDefault) && oldDefault !== this.dbPath) {
|
|
503
|
+
console.warn(
|
|
504
|
+
`[mem0] Default vector_store.db location changed from ${oldDefault} to ${this.dbPath}. Move your existing file or set vectorStore.config.dbPath explicitly.`
|
|
505
|
+
);
|
|
506
|
+
}
|
|
481
507
|
}
|
|
482
|
-
|
|
483
|
-
this.
|
|
508
|
+
ensureSQLiteDirectory(this.dbPath);
|
|
509
|
+
this.db = new import_better_sqlite3.default(this.dbPath);
|
|
510
|
+
this.init();
|
|
484
511
|
}
|
|
485
|
-
|
|
486
|
-
|
|
512
|
+
init() {
|
|
513
|
+
this.db.exec(`
|
|
487
514
|
CREATE TABLE IF NOT EXISTS vectors (
|
|
488
515
|
id TEXT PRIMARY KEY,
|
|
489
516
|
vector BLOB NOT NULL,
|
|
490
517
|
payload TEXT NOT NULL
|
|
491
518
|
)
|
|
492
519
|
`);
|
|
493
|
-
|
|
520
|
+
this.db.exec(`
|
|
494
521
|
CREATE TABLE IF NOT EXISTS memory_migrations (
|
|
495
522
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
496
523
|
user_id TEXT NOT NULL UNIQUE
|
|
497
524
|
)
|
|
498
525
|
`);
|
|
499
526
|
}
|
|
500
|
-
async run(sql, params = []) {
|
|
501
|
-
return new Promise((resolve, reject) => {
|
|
502
|
-
this.db.run(sql, params, (err) => {
|
|
503
|
-
if (err) reject(err);
|
|
504
|
-
else resolve();
|
|
505
|
-
});
|
|
506
|
-
});
|
|
507
|
-
}
|
|
508
|
-
async all(sql, params = []) {
|
|
509
|
-
return new Promise((resolve, reject) => {
|
|
510
|
-
this.db.all(sql, params, (err, rows) => {
|
|
511
|
-
if (err) reject(err);
|
|
512
|
-
else resolve(rows);
|
|
513
|
-
});
|
|
514
|
-
});
|
|
515
|
-
}
|
|
516
|
-
async getOne(sql, params = []) {
|
|
517
|
-
return new Promise((resolve, reject) => {
|
|
518
|
-
this.db.get(sql, params, (err, row) => {
|
|
519
|
-
if (err) reject(err);
|
|
520
|
-
else resolve(row);
|
|
521
|
-
});
|
|
522
|
-
});
|
|
523
|
-
}
|
|
524
527
|
cosineSimilarity(a, b) {
|
|
525
528
|
let dotProduct = 0;
|
|
526
529
|
let normA = 0;
|
|
@@ -539,18 +542,23 @@ var MemoryVectorStore = class {
|
|
|
539
542
|
);
|
|
540
543
|
}
|
|
541
544
|
async insert(vectors, ids, payloads) {
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
545
|
+
const stmt = this.db.prepare(
|
|
546
|
+
`INSERT OR REPLACE INTO vectors (id, vector, payload) VALUES (?, ?, ?)`
|
|
547
|
+
);
|
|
548
|
+
const insertMany = this.db.transaction(
|
|
549
|
+
(vecs, vIds, vPayloads) => {
|
|
550
|
+
for (let i = 0; i < vecs.length; i++) {
|
|
551
|
+
if (vecs[i].length !== this.dimension) {
|
|
552
|
+
throw new Error(
|
|
553
|
+
`Vector dimension mismatch. Expected ${this.dimension}, got ${vecs[i].length}`
|
|
554
|
+
);
|
|
555
|
+
}
|
|
556
|
+
const vectorBuffer = Buffer.from(new Float32Array(vecs[i]).buffer);
|
|
557
|
+
stmt.run(vIds[i], vectorBuffer, JSON.stringify(vPayloads[i]));
|
|
558
|
+
}
|
|
547
559
|
}
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
`INSERT OR REPLACE INTO vectors (id, vector, payload) VALUES (?, ?, ?)`,
|
|
551
|
-
[ids[i], vectorBuffer, JSON.stringify(payloads[i])]
|
|
552
|
-
);
|
|
553
|
-
}
|
|
560
|
+
);
|
|
561
|
+
insertMany(vectors, ids, payloads);
|
|
554
562
|
}
|
|
555
563
|
async search(query, limit = 10, filters) {
|
|
556
564
|
if (query.length !== this.dimension) {
|
|
@@ -558,10 +566,14 @@ var MemoryVectorStore = class {
|
|
|
558
566
|
`Query dimension mismatch. Expected ${this.dimension}, got ${query.length}`
|
|
559
567
|
);
|
|
560
568
|
}
|
|
561
|
-
const rows =
|
|
569
|
+
const rows = this.db.prepare(`SELECT * FROM vectors`).all();
|
|
562
570
|
const results = [];
|
|
563
571
|
for (const row of rows) {
|
|
564
|
-
const vector = new Float32Array(
|
|
572
|
+
const vector = new Float32Array(
|
|
573
|
+
row.vector.buffer,
|
|
574
|
+
row.vector.byteOffset,
|
|
575
|
+
row.vector.byteLength / 4
|
|
576
|
+
);
|
|
565
577
|
const payload = JSON.parse(row.payload);
|
|
566
578
|
const memoryVector = {
|
|
567
579
|
id: row.id,
|
|
@@ -581,9 +593,7 @@ var MemoryVectorStore = class {
|
|
|
581
593
|
return results.slice(0, limit);
|
|
582
594
|
}
|
|
583
595
|
async get(vectorId) {
|
|
584
|
-
const row =
|
|
585
|
-
vectorId
|
|
586
|
-
]);
|
|
596
|
+
const row = this.db.prepare(`SELECT * FROM vectors WHERE id = ?`).get(vectorId);
|
|
587
597
|
if (!row) return null;
|
|
588
598
|
const payload = JSON.parse(row.payload);
|
|
589
599
|
return {
|
|
@@ -598,27 +608,29 @@ var MemoryVectorStore = class {
|
|
|
598
608
|
);
|
|
599
609
|
}
|
|
600
610
|
const vectorBuffer = Buffer.from(new Float32Array(vector).buffer);
|
|
601
|
-
|
|
602
|
-
vectorBuffer,
|
|
603
|
-
JSON.stringify(payload),
|
|
604
|
-
vectorId
|
|
605
|
-
]);
|
|
611
|
+
this.db.prepare(`UPDATE vectors SET vector = ?, payload = ? WHERE id = ?`).run(vectorBuffer, JSON.stringify(payload), vectorId);
|
|
606
612
|
}
|
|
607
613
|
async delete(vectorId) {
|
|
608
|
-
|
|
614
|
+
this.db.prepare(`DELETE FROM vectors WHERE id = ?`).run(vectorId);
|
|
609
615
|
}
|
|
610
616
|
async deleteCol() {
|
|
611
|
-
|
|
612
|
-
|
|
617
|
+
this.db.exec(`DROP TABLE IF EXISTS vectors`);
|
|
618
|
+
this.init();
|
|
613
619
|
}
|
|
614
620
|
async list(filters, limit = 100) {
|
|
615
|
-
const rows =
|
|
621
|
+
const rows = this.db.prepare(`SELECT * FROM vectors`).all();
|
|
616
622
|
const results = [];
|
|
617
623
|
for (const row of rows) {
|
|
618
624
|
const payload = JSON.parse(row.payload);
|
|
619
625
|
const memoryVector = {
|
|
620
626
|
id: row.id,
|
|
621
|
-
vector: Array.from(
|
|
627
|
+
vector: Array.from(
|
|
628
|
+
new Float32Array(
|
|
629
|
+
row.vector.buffer,
|
|
630
|
+
row.vector.byteOffset,
|
|
631
|
+
row.vector.byteLength / 4
|
|
632
|
+
)
|
|
633
|
+
),
|
|
622
634
|
payload
|
|
623
635
|
};
|
|
624
636
|
if (this.filterVector(memoryVector, filters)) {
|
|
@@ -631,32 +643,26 @@ var MemoryVectorStore = class {
|
|
|
631
643
|
return [results.slice(0, limit), results.length];
|
|
632
644
|
}
|
|
633
645
|
async getUserId() {
|
|
634
|
-
const row =
|
|
635
|
-
`SELECT user_id FROM memory_migrations LIMIT 1`
|
|
636
|
-
);
|
|
646
|
+
const row = this.db.prepare(`SELECT user_id FROM memory_migrations LIMIT 1`).get();
|
|
637
647
|
if (row) {
|
|
638
648
|
return row.user_id;
|
|
639
649
|
}
|
|
640
650
|
const randomUserId = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
|
|
641
|
-
|
|
642
|
-
randomUserId
|
|
643
|
-
]);
|
|
651
|
+
this.db.prepare(`INSERT INTO memory_migrations (user_id) VALUES (?)`).run(randomUserId);
|
|
644
652
|
return randomUserId;
|
|
645
653
|
}
|
|
646
654
|
async setUserId(userId) {
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
userId
|
|
650
|
-
]);
|
|
655
|
+
this.db.prepare(`DELETE FROM memory_migrations`).run();
|
|
656
|
+
this.db.prepare(`INSERT INTO memory_migrations (user_id) VALUES (?)`).run(userId);
|
|
651
657
|
}
|
|
652
658
|
async initialize() {
|
|
653
|
-
|
|
659
|
+
this.init();
|
|
654
660
|
}
|
|
655
661
|
};
|
|
656
662
|
|
|
657
663
|
// src/oss/src/vector_stores/qdrant.ts
|
|
658
664
|
var import_js_client_rest = require("@qdrant/js-client-rest");
|
|
659
|
-
var
|
|
665
|
+
var fs3 = __toESM(require("fs"));
|
|
660
666
|
var Qdrant = class {
|
|
661
667
|
constructor(config) {
|
|
662
668
|
if (config.client) {
|
|
@@ -676,8 +682,8 @@ var Qdrant = class {
|
|
|
676
682
|
if (!Object.keys(params).length) {
|
|
677
683
|
params.path = config.path;
|
|
678
684
|
if (!config.onDisk && config.path) {
|
|
679
|
-
if (
|
|
680
|
-
|
|
685
|
+
if (fs3.existsSync(config.path) && fs3.statSync(config.path).isDirectory()) {
|
|
686
|
+
fs3.rmSync(config.path, { recursive: true });
|
|
681
687
|
}
|
|
682
688
|
}
|
|
683
689
|
}
|
|
@@ -792,19 +798,7 @@ var Qdrant = class {
|
|
|
792
798
|
async getUserId() {
|
|
793
799
|
var _a2;
|
|
794
800
|
try {
|
|
795
|
-
|
|
796
|
-
const userCollectionExists = collections.collections.some(
|
|
797
|
-
(col) => col.name === "memory_migrations"
|
|
798
|
-
);
|
|
799
|
-
if (!userCollectionExists) {
|
|
800
|
-
await this.client.createCollection("memory_migrations", {
|
|
801
|
-
vectors: {
|
|
802
|
-
size: 1,
|
|
803
|
-
distance: "Cosine",
|
|
804
|
-
on_disk: false
|
|
805
|
-
}
|
|
806
|
-
});
|
|
807
|
-
}
|
|
801
|
+
await this.ensureCollection("memory_migrations", 1);
|
|
808
802
|
const result = await this.client.scroll("memory_migrations", {
|
|
809
803
|
limit: 1,
|
|
810
804
|
with_payload: true
|
|
@@ -849,56 +843,50 @@ var Qdrant = class {
|
|
|
849
843
|
throw error;
|
|
850
844
|
}
|
|
851
845
|
}
|
|
852
|
-
async
|
|
853
|
-
var _a2, _b;
|
|
846
|
+
async ensureCollection(name, size) {
|
|
847
|
+
var _a2, _b, _c;
|
|
854
848
|
try {
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
});
|
|
867
|
-
} catch (error) {
|
|
868
|
-
if ((error == null ? void 0 : error.status) === 409) {
|
|
869
|
-
const collectionInfo = await this.client.getCollection(
|
|
870
|
-
this.collectionName
|
|
871
|
-
);
|
|
849
|
+
await this.client.createCollection(name, {
|
|
850
|
+
vectors: {
|
|
851
|
+
size,
|
|
852
|
+
distance: "Cosine"
|
|
853
|
+
}
|
|
854
|
+
});
|
|
855
|
+
} catch (error) {
|
|
856
|
+
if ((error == null ? void 0 : error.status) === 409) {
|
|
857
|
+
if (name === this.collectionName) {
|
|
858
|
+
try {
|
|
859
|
+
const collectionInfo = await this.client.getCollection(name);
|
|
872
860
|
const vectorConfig = (_b = (_a2 = collectionInfo.config) == null ? void 0 : _a2.params) == null ? void 0 : _b.vectors;
|
|
873
|
-
if (
|
|
861
|
+
if (vectorConfig && vectorConfig.size !== size) {
|
|
874
862
|
throw new Error(
|
|
875
|
-
`Collection ${
|
|
863
|
+
`Collection ${name} exists but has wrong vector size. Expected: ${size}, got: ${vectorConfig.size}`
|
|
876
864
|
);
|
|
877
865
|
}
|
|
878
|
-
}
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
}
|
|
882
|
-
}
|
|
883
|
-
const userExists = collections.collections.some(
|
|
884
|
-
(c) => c.name === "memory_migrations"
|
|
885
|
-
);
|
|
886
|
-
if (!userExists) {
|
|
887
|
-
try {
|
|
888
|
-
await this.client.createCollection("memory_migrations", {
|
|
889
|
-
vectors: {
|
|
890
|
-
size: 1,
|
|
891
|
-
// Minimal size since we only store user_id
|
|
892
|
-
distance: "Cosine"
|
|
866
|
+
} catch (verifyError) {
|
|
867
|
+
if ((_c = verifyError == null ? void 0 : verifyError.message) == null ? void 0 : _c.includes("wrong vector size")) {
|
|
868
|
+
throw verifyError;
|
|
893
869
|
}
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
} else {
|
|
898
|
-
throw error;
|
|
870
|
+
console.warn(
|
|
871
|
+
`Collection '${name}' exists (409) but dimension verification failed: ${(verifyError == null ? void 0 : verifyError.message) || verifyError}. Proceeding anyway.`
|
|
872
|
+
);
|
|
899
873
|
}
|
|
900
874
|
}
|
|
875
|
+
} else {
|
|
876
|
+
throw error;
|
|
901
877
|
}
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
async initialize() {
|
|
881
|
+
if (!this._initPromise) {
|
|
882
|
+
this._initPromise = this._doInitialize();
|
|
883
|
+
}
|
|
884
|
+
return this._initPromise;
|
|
885
|
+
}
|
|
886
|
+
async _doInitialize() {
|
|
887
|
+
try {
|
|
888
|
+
await this.ensureCollection(this.collectionName, this.dimension);
|
|
889
|
+
await this.ensureCollection("memory_migrations", 1);
|
|
902
890
|
} catch (error) {
|
|
903
891
|
console.error("Error initializing Qdrant:", error);
|
|
904
892
|
throw error;
|
|
@@ -1193,6 +1181,12 @@ var VectorizeDB = class {
|
|
|
1193
1181
|
}
|
|
1194
1182
|
}
|
|
1195
1183
|
async initialize() {
|
|
1184
|
+
if (!this._initPromise) {
|
|
1185
|
+
this._initPromise = this._doInitialize();
|
|
1186
|
+
}
|
|
1187
|
+
return this._initPromise;
|
|
1188
|
+
}
|
|
1189
|
+
async _doInitialize() {
|
|
1196
1190
|
var _a2, _b, _c, _d, _e;
|
|
1197
1191
|
try {
|
|
1198
1192
|
let indexFound = false;
|
|
@@ -1416,6 +1410,12 @@ var RedisDB = class {
|
|
|
1416
1410
|
}
|
|
1417
1411
|
}
|
|
1418
1412
|
async initialize() {
|
|
1413
|
+
if (!this._initPromise) {
|
|
1414
|
+
this._initPromise = this._doInitialize();
|
|
1415
|
+
}
|
|
1416
|
+
return this._initPromise;
|
|
1417
|
+
}
|
|
1418
|
+
async _doInitialize() {
|
|
1419
1419
|
try {
|
|
1420
1420
|
await this.client.connect();
|
|
1421
1421
|
console.log("Connected to Redis");
|
|
@@ -1848,6 +1848,12 @@ var SupabaseDB = class {
|
|
|
1848
1848
|
});
|
|
1849
1849
|
}
|
|
1850
1850
|
async initialize() {
|
|
1851
|
+
if (!this._initPromise) {
|
|
1852
|
+
this._initPromise = this._doInitialize();
|
|
1853
|
+
}
|
|
1854
|
+
return this._initPromise;
|
|
1855
|
+
}
|
|
1856
|
+
async _doInitialize() {
|
|
1851
1857
|
try {
|
|
1852
1858
|
const testVector = Array(1536).fill(0);
|
|
1853
1859
|
try {
|
|
@@ -2072,14 +2078,15 @@ See the SQL migration instructions in the code comments.`
|
|
|
2072
2078
|
};
|
|
2073
2079
|
|
|
2074
2080
|
// src/oss/src/storage/SQLiteManager.ts
|
|
2075
|
-
var
|
|
2081
|
+
var import_better_sqlite32 = __toESM(require("better-sqlite3"));
|
|
2076
2082
|
var SQLiteManager = class {
|
|
2077
2083
|
constructor(dbPath) {
|
|
2078
|
-
|
|
2079
|
-
this.
|
|
2084
|
+
ensureSQLiteDirectory(dbPath);
|
|
2085
|
+
this.db = new import_better_sqlite32.default(dbPath);
|
|
2086
|
+
this.init();
|
|
2080
2087
|
}
|
|
2081
|
-
|
|
2082
|
-
|
|
2088
|
+
init() {
|
|
2089
|
+
this.db.exec(`
|
|
2083
2090
|
CREATE TABLE IF NOT EXISTS memory_history (
|
|
2084
2091
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
2085
2092
|
memory_id TEXT NOT NULL,
|
|
@@ -2091,48 +2098,32 @@ var SQLiteManager = class {
|
|
|
2091
2098
|
is_deleted INTEGER DEFAULT 0
|
|
2092
2099
|
)
|
|
2093
2100
|
`);
|
|
2094
|
-
|
|
2095
|
-
|
|
2096
|
-
|
|
2097
|
-
|
|
2098
|
-
|
|
2099
|
-
|
|
2100
|
-
|
|
2101
|
-
|
|
2102
|
-
}
|
|
2103
|
-
async all(sql, params = []) {
|
|
2104
|
-
return new Promise((resolve, reject) => {
|
|
2105
|
-
this.db.all(sql, params, (err, rows) => {
|
|
2106
|
-
if (err) reject(err);
|
|
2107
|
-
else resolve(rows);
|
|
2108
|
-
});
|
|
2109
|
-
});
|
|
2101
|
+
this.stmtInsert = this.db.prepare(
|
|
2102
|
+
`INSERT INTO memory_history
|
|
2103
|
+
(memory_id, previous_value, new_value, action, created_at, updated_at, is_deleted)
|
|
2104
|
+
VALUES (?, ?, ?, ?, ?, ?, ?)`
|
|
2105
|
+
);
|
|
2106
|
+
this.stmtSelect = this.db.prepare(
|
|
2107
|
+
"SELECT * FROM memory_history WHERE memory_id = ? ORDER BY id DESC"
|
|
2108
|
+
);
|
|
2110
2109
|
}
|
|
2111
2110
|
async addHistory(memoryId, previousValue, newValue, action, createdAt, updatedAt, isDeleted = 0) {
|
|
2112
|
-
|
|
2113
|
-
|
|
2114
|
-
|
|
2115
|
-
|
|
2116
|
-
|
|
2117
|
-
|
|
2118
|
-
|
|
2119
|
-
|
|
2120
|
-
action,
|
|
2121
|
-
createdAt,
|
|
2122
|
-
updatedAt,
|
|
2123
|
-
isDeleted
|
|
2124
|
-
]
|
|
2111
|
+
this.stmtInsert.run(
|
|
2112
|
+
memoryId,
|
|
2113
|
+
previousValue,
|
|
2114
|
+
newValue,
|
|
2115
|
+
action,
|
|
2116
|
+
createdAt != null ? createdAt : null,
|
|
2117
|
+
updatedAt != null ? updatedAt : null,
|
|
2118
|
+
isDeleted
|
|
2125
2119
|
);
|
|
2126
2120
|
}
|
|
2127
2121
|
async getHistory(memoryId) {
|
|
2128
|
-
return this.all(
|
|
2129
|
-
"SELECT * FROM memory_history WHERE memory_id = ? ORDER BY id DESC",
|
|
2130
|
-
[memoryId]
|
|
2131
|
-
);
|
|
2122
|
+
return this.stmtSelect.all(memoryId);
|
|
2132
2123
|
}
|
|
2133
2124
|
async reset() {
|
|
2134
|
-
|
|
2135
|
-
|
|
2125
|
+
this.db.exec("DROP TABLE IF EXISTS memory_history");
|
|
2126
|
+
this.init();
|
|
2136
2127
|
}
|
|
2137
2128
|
close() {
|
|
2138
2129
|
this.db.close();
|
|
@@ -2260,7 +2251,7 @@ var GoogleEmbedder = class {
|
|
|
2260
2251
|
const response = await this.google.models.embedContent({
|
|
2261
2252
|
model: this.model,
|
|
2262
2253
|
contents: texts,
|
|
2263
|
-
config: { outputDimensionality:
|
|
2254
|
+
config: { outputDimensionality: this.embeddingDims }
|
|
2264
2255
|
});
|
|
2265
2256
|
return response.embeddings.map((item) => item.values);
|
|
2266
2257
|
}
|
|
@@ -2655,7 +2646,7 @@ function getUpdateMemoryMessages(retrievedOldMemory, newRetrievedFacts) {
|
|
|
2655
2646
|
Do not return anything except the JSON format.`;
|
|
2656
2647
|
}
|
|
2657
2648
|
function removeCodeBlocks(text) {
|
|
2658
|
-
return text.replace(/```[
|
|
2649
|
+
return text.replace(/```(?:\w+)?\n?([\s\S]*?)```/g, "$1").trim();
|
|
2659
2650
|
}
|
|
2660
2651
|
|
|
2661
2652
|
// src/oss/src/graphs/tools.ts
|
|
@@ -3160,6 +3151,12 @@ var AzureAISearch = class {
|
|
|
3160
3151
|
* Initialize the Azure AI Search index if it doesn't exist
|
|
3161
3152
|
*/
|
|
3162
3153
|
async initialize() {
|
|
3154
|
+
if (!this._initPromise) {
|
|
3155
|
+
this._initPromise = this._doInitialize();
|
|
3156
|
+
}
|
|
3157
|
+
return this._initPromise;
|
|
3158
|
+
}
|
|
3159
|
+
async _doInitialize() {
|
|
3163
3160
|
try {
|
|
3164
3161
|
const collections = await this.listCols();
|
|
3165
3162
|
if (!collections.includes(this.indexName)) {
|
|
@@ -3731,7 +3728,7 @@ var DEFAULT_MEMORY_CONFIG = {
|
|
|
3731
3728
|
// src/oss/src/config/manager.ts
|
|
3732
3729
|
var ConfigManager = class {
|
|
3733
3730
|
static mergeConfig(userConfig = {}) {
|
|
3734
|
-
var _a2, _b, _c;
|
|
3731
|
+
var _a2, _b, _c, _d, _e, _f, _g;
|
|
3735
3732
|
const mergedConfig = {
|
|
3736
3733
|
version: userConfig.version || DEFAULT_MEMORY_CONFIG.version,
|
|
3737
3734
|
embedder: {
|
|
@@ -3749,6 +3746,7 @@ var ConfigManager = class {
|
|
|
3749
3746
|
return {
|
|
3750
3747
|
apiKey: (userConf == null ? void 0 : userConf.apiKey) !== void 0 ? userConf.apiKey : defaultConf.apiKey,
|
|
3751
3748
|
model: finalModel,
|
|
3749
|
+
baseURL: userConf == null ? void 0 : userConf.baseURL,
|
|
3752
3750
|
url: userConf == null ? void 0 : userConf.url,
|
|
3753
3751
|
embeddingDims: userConf == null ? void 0 : userConf.embeddingDims,
|
|
3754
3752
|
modelProperties: (userConf == null ? void 0 : userConf.modelProperties) !== void 0 ? userConf.modelProperties : defaultConf.modelProperties
|
|
@@ -3758,24 +3756,22 @@ var ConfigManager = class {
|
|
|
3758
3756
|
vectorStore: {
|
|
3759
3757
|
provider: ((_b = userConfig.vectorStore) == null ? void 0 : _b.provider) || DEFAULT_MEMORY_CONFIG.vectorStore.provider,
|
|
3760
3758
|
config: (() => {
|
|
3761
|
-
var _a3;
|
|
3759
|
+
var _a3, _b2, _c2;
|
|
3762
3760
|
const defaultConf = DEFAULT_MEMORY_CONFIG.vectorStore.config;
|
|
3763
3761
|
const userConf = (_a3 = userConfig.vectorStore) == null ? void 0 : _a3.config;
|
|
3762
|
+
const explicitDimension = (userConf == null ? void 0 : userConf.dimension) || ((_c2 = (_b2 = userConfig.embedder) == null ? void 0 : _b2.config) == null ? void 0 : _c2.embeddingDims) || void 0;
|
|
3764
3763
|
if ((userConf == null ? void 0 : userConf.client) && typeof userConf.client === "object") {
|
|
3765
3764
|
return {
|
|
3766
3765
|
client: userConf.client,
|
|
3767
|
-
// Include other fields from userConf if necessary, or omit defaults
|
|
3768
3766
|
collectionName: userConf.collectionName,
|
|
3769
|
-
|
|
3770
|
-
dimension: userConf.dimension || defaultConf.dimension,
|
|
3771
|
-
// Merge dimension
|
|
3767
|
+
dimension: explicitDimension,
|
|
3772
3768
|
...userConf
|
|
3773
3769
|
// Include any other passthrough fields from user
|
|
3774
3770
|
};
|
|
3775
3771
|
} else {
|
|
3776
3772
|
return {
|
|
3777
3773
|
collectionName: (userConf == null ? void 0 : userConf.collectionName) || defaultConf.collectionName,
|
|
3778
|
-
dimension:
|
|
3774
|
+
dimension: explicitDimension,
|
|
3779
3775
|
// Ensure client is not carried over from defaults if not provided by user
|
|
3780
3776
|
client: void 0,
|
|
3781
3777
|
// Include other passthrough fields from userConf even if no client
|
|
@@ -3804,16 +3800,28 @@ var ConfigManager = class {
|
|
|
3804
3800
|
};
|
|
3805
3801
|
})()
|
|
3806
3802
|
},
|
|
3807
|
-
historyDbPath: userConfig.historyDbPath || DEFAULT_MEMORY_CONFIG.historyDbPath,
|
|
3803
|
+
historyDbPath: userConfig.historyDbPath || ((_e = (_d = userConfig.historyStore) == null ? void 0 : _d.config) == null ? void 0 : _e.historyDbPath) || ((_g = (_f = DEFAULT_MEMORY_CONFIG.historyStore) == null ? void 0 : _f.config) == null ? void 0 : _g.historyDbPath),
|
|
3808
3804
|
customPrompt: userConfig.customPrompt,
|
|
3809
3805
|
graphStore: {
|
|
3810
3806
|
...DEFAULT_MEMORY_CONFIG.graphStore,
|
|
3811
3807
|
...userConfig.graphStore
|
|
3812
3808
|
},
|
|
3813
|
-
historyStore: {
|
|
3814
|
-
|
|
3815
|
-
|
|
3816
|
-
|
|
3809
|
+
historyStore: (() => {
|
|
3810
|
+
var _a3, _b2;
|
|
3811
|
+
const defaultHistoryStore = DEFAULT_MEMORY_CONFIG.historyStore;
|
|
3812
|
+
const historyProvider = ((_a3 = userConfig.historyStore) == null ? void 0 : _a3.provider) || defaultHistoryStore.provider;
|
|
3813
|
+
const isSqlite = historyProvider.toLowerCase() === "sqlite";
|
|
3814
|
+
return {
|
|
3815
|
+
...defaultHistoryStore,
|
|
3816
|
+
...userConfig.historyStore,
|
|
3817
|
+
provider: historyProvider,
|
|
3818
|
+
config: {
|
|
3819
|
+
...isSqlite ? defaultHistoryStore.config : {},
|
|
3820
|
+
...isSqlite && userConfig.historyDbPath ? { historyDbPath: userConfig.historyDbPath } : {},
|
|
3821
|
+
...(_b2 = userConfig.historyStore) == null ? void 0 : _b2.config
|
|
3822
|
+
}
|
|
3823
|
+
};
|
|
3824
|
+
})(),
|
|
3817
3825
|
disableHistory: userConfig.disableHistory || DEFAULT_MEMORY_CONFIG.disableHistory,
|
|
3818
3826
|
enableGraph: userConfig.enableGraph || DEFAULT_MEMORY_CONFIG.enableGraph
|
|
3819
3827
|
};
|
|
@@ -3923,6 +3931,8 @@ Memory Format:
|
|
|
3923
3931
|
source -- relationship -- destination
|
|
3924
3932
|
|
|
3925
3933
|
Provide a list of deletion instructions, each specifying the relationship to be deleted.
|
|
3934
|
+
|
|
3935
|
+
Respond in JSON format.
|
|
3926
3936
|
`;
|
|
3927
3937
|
function getDeleteMessages(existingMemoriesString, data, userId) {
|
|
3928
3938
|
return [
|
|
@@ -4060,7 +4070,7 @@ var MemoryGraph = class {
|
|
|
4060
4070
|
[
|
|
4061
4071
|
{
|
|
4062
4072
|
role: "system",
|
|
4063
|
-
content: `You are a smart assistant who understands entities and their types in a given text. If user message contains self reference such as 'I', 'me', 'my' etc. then use ${filters["userId"]} as the source entity. Extract all the entities from the text. ***DO NOT*** answer the question itself if the given text is a question.`
|
|
4073
|
+
content: `You are a smart assistant who understands entities and their types in a given text. If user message contains self reference such as 'I', 'me', 'my' etc. then use ${filters["userId"]} as the source entity. Extract all the entities from the text. ***DO NOT*** answer the question itself if the given text is a question. Respond in JSON format.`
|
|
4064
4074
|
},
|
|
4065
4075
|
{ role: "user", content: data }
|
|
4066
4076
|
],
|
|
@@ -4558,10 +4568,6 @@ var Memory = class _Memory {
|
|
|
4558
4568
|
this.config.embedder.provider,
|
|
4559
4569
|
this.config.embedder.config
|
|
4560
4570
|
);
|
|
4561
|
-
this.vectorStore = VectorStoreFactory.create(
|
|
4562
|
-
this.config.vectorStore.provider,
|
|
4563
|
-
this.config.vectorStore.config
|
|
4564
|
-
);
|
|
4565
4571
|
this.llm = LLMFactory.create(
|
|
4566
4572
|
this.config.llm.provider,
|
|
4567
4573
|
this.config.llm.config
|
|
@@ -4569,16 +4575,10 @@ var Memory = class _Memory {
|
|
|
4569
4575
|
if (this.config.disableHistory) {
|
|
4570
4576
|
this.db = new DummyHistoryManager();
|
|
4571
4577
|
} else {
|
|
4572
|
-
|
|
4573
|
-
provider: "sqlite",
|
|
4574
|
-
config: {
|
|
4575
|
-
historyDbPath: this.config.historyDbPath || ":memory:"
|
|
4576
|
-
}
|
|
4577
|
-
};
|
|
4578
|
-
this.db = this.config.historyStore && !this.config.disableHistory ? HistoryManagerFactory.create(
|
|
4578
|
+
this.db = HistoryManagerFactory.create(
|
|
4579
4579
|
this.config.historyStore.provider,
|
|
4580
4580
|
this.config.historyStore
|
|
4581
|
-
)
|
|
4581
|
+
);
|
|
4582
4582
|
}
|
|
4583
4583
|
this.collectionName = this.config.vectorStore.config.collectionName;
|
|
4584
4584
|
this.apiVersion = this.config.version || "v1.0";
|
|
@@ -4587,7 +4587,51 @@ var Memory = class _Memory {
|
|
|
4587
4587
|
if (this.enableGraph && this.config.graphStore) {
|
|
4588
4588
|
this.graphMemory = new MemoryGraph(this.config);
|
|
4589
4589
|
}
|
|
4590
|
-
this.
|
|
4590
|
+
this._initPromise = this._autoInitialize().catch((error) => {
|
|
4591
|
+
this._initError = error instanceof Error ? error : new Error(String(error));
|
|
4592
|
+
console.error(this._initError);
|
|
4593
|
+
});
|
|
4594
|
+
}
|
|
4595
|
+
/**
|
|
4596
|
+
* If no explicit dimension was provided, runs a probe embedding to
|
|
4597
|
+
* detect it. Then creates and initializes the vector store.
|
|
4598
|
+
*/
|
|
4599
|
+
async _autoInitialize() {
|
|
4600
|
+
if (!this.config.vectorStore.config.dimension) {
|
|
4601
|
+
try {
|
|
4602
|
+
const probe = await this.embedder.embed("dimension probe");
|
|
4603
|
+
this.config.vectorStore.config.dimension = probe.length;
|
|
4604
|
+
} catch (error) {
|
|
4605
|
+
throw new Error(
|
|
4606
|
+
`Failed to auto-detect embedding dimension from provider '${this.config.embedder.provider}': ${error.message}. Please set 'dimension' in vectorStore.config or 'embeddingDims' in embedder.config explicitly.`
|
|
4607
|
+
);
|
|
4608
|
+
}
|
|
4609
|
+
}
|
|
4610
|
+
this.vectorStore = VectorStoreFactory.create(
|
|
4611
|
+
this.config.vectorStore.provider,
|
|
4612
|
+
this.config.vectorStore.config
|
|
4613
|
+
);
|
|
4614
|
+
await this.vectorStore.initialize();
|
|
4615
|
+
await this._initializeTelemetry();
|
|
4616
|
+
}
|
|
4617
|
+
/**
|
|
4618
|
+
* Ensures that auto-initialization (dimension detection + vector store
|
|
4619
|
+
* creation) has completed before any public method proceeds.
|
|
4620
|
+
* If a previous init attempt failed, retries automatically.
|
|
4621
|
+
*/
|
|
4622
|
+
async _ensureInitialized() {
|
|
4623
|
+
await this._initPromise;
|
|
4624
|
+
if (this._initError) {
|
|
4625
|
+
this._initError = void 0;
|
|
4626
|
+
this._initPromise = this._autoInitialize().catch((error) => {
|
|
4627
|
+
this._initError = error instanceof Error ? error : new Error(String(error));
|
|
4628
|
+
console.error(this._initError);
|
|
4629
|
+
});
|
|
4630
|
+
await this._initPromise;
|
|
4631
|
+
if (this._initError) {
|
|
4632
|
+
throw this._initError;
|
|
4633
|
+
}
|
|
4634
|
+
}
|
|
4591
4635
|
}
|
|
4592
4636
|
async _initializeTelemetry() {
|
|
4593
4637
|
try {
|
|
@@ -4634,6 +4678,7 @@ var Memory = class _Memory {
|
|
|
4634
4678
|
}
|
|
4635
4679
|
}
|
|
4636
4680
|
async add(messages, config) {
|
|
4681
|
+
await this._ensureInitialized();
|
|
4637
4682
|
await this._captureEvent("add", {
|
|
4638
4683
|
message_count: Array.isArray(messages) ? messages.length : 1,
|
|
4639
4684
|
has_metadata: !!config.metadata,
|
|
@@ -4820,6 +4865,7 @@ ${parsedMessages}`
|
|
|
4820
4865
|
return results;
|
|
4821
4866
|
}
|
|
4822
4867
|
async get(memoryId) {
|
|
4868
|
+
await this._ensureInitialized();
|
|
4823
4869
|
const memory = await this.vectorStore.get(memoryId);
|
|
4824
4870
|
if (!memory) return null;
|
|
4825
4871
|
const filters = {
|
|
@@ -4852,6 +4898,7 @@ ${parsedMessages}`
|
|
|
4852
4898
|
return { ...memoryItem, ...filters };
|
|
4853
4899
|
}
|
|
4854
4900
|
async search(query, config) {
|
|
4901
|
+
await this._ensureInitialized();
|
|
4855
4902
|
await this._captureEvent("search", {
|
|
4856
4903
|
query_length: query.length,
|
|
4857
4904
|
limit: config.limit,
|
|
@@ -4907,17 +4954,20 @@ ${parsedMessages}`
|
|
|
4907
4954
|
};
|
|
4908
4955
|
}
|
|
4909
4956
|
async update(memoryId, data) {
|
|
4957
|
+
await this._ensureInitialized();
|
|
4910
4958
|
await this._captureEvent("update", { memory_id: memoryId });
|
|
4911
4959
|
const embedding = await this.embedder.embed(data);
|
|
4912
4960
|
await this.updateMemory(memoryId, data, { [data]: embedding });
|
|
4913
4961
|
return { message: "Memory updated successfully!" };
|
|
4914
4962
|
}
|
|
4915
4963
|
async delete(memoryId) {
|
|
4964
|
+
await this._ensureInitialized();
|
|
4916
4965
|
await this._captureEvent("delete", { memory_id: memoryId });
|
|
4917
4966
|
await this.deleteMemory(memoryId);
|
|
4918
4967
|
return { message: "Memory deleted successfully!" };
|
|
4919
4968
|
}
|
|
4920
4969
|
async deleteAll(config) {
|
|
4970
|
+
await this._ensureInitialized();
|
|
4921
4971
|
await this._captureEvent("delete_all", {
|
|
4922
4972
|
has_user_id: !!config.userId,
|
|
4923
4973
|
has_agent_id: !!config.agentId,
|
|
@@ -4940,9 +4990,11 @@ ${parsedMessages}`
|
|
|
4940
4990
|
return { message: "Memories deleted successfully!" };
|
|
4941
4991
|
}
|
|
4942
4992
|
async history(memoryId) {
|
|
4993
|
+
await this._ensureInitialized();
|
|
4943
4994
|
return this.db.getHistory(memoryId);
|
|
4944
4995
|
}
|
|
4945
4996
|
async reset() {
|
|
4997
|
+
await this._ensureInitialized();
|
|
4946
4998
|
await this._captureEvent("reset");
|
|
4947
4999
|
await this.db.reset();
|
|
4948
5000
|
if (this.config.vectorStore.provider.toLowerCase() !== "langchain") {
|
|
@@ -4966,18 +5018,19 @@ ${parsedMessages}`
|
|
|
4966
5018
|
this.config.embedder.provider,
|
|
4967
5019
|
this.config.embedder.config
|
|
4968
5020
|
);
|
|
4969
|
-
this.vectorStore = VectorStoreFactory.create(
|
|
4970
|
-
this.config.vectorStore.provider,
|
|
4971
|
-
this.config.vectorStore.config
|
|
4972
|
-
// This will pass the original client instance back
|
|
4973
|
-
);
|
|
4974
5021
|
this.llm = LLMFactory.create(
|
|
4975
5022
|
this.config.llm.provider,
|
|
4976
5023
|
this.config.llm.config
|
|
4977
5024
|
);
|
|
4978
|
-
this.
|
|
5025
|
+
this._initError = void 0;
|
|
5026
|
+
this._initPromise = this._autoInitialize().catch((error) => {
|
|
5027
|
+
this._initError = error instanceof Error ? error : new Error(String(error));
|
|
5028
|
+
console.error(this._initError);
|
|
5029
|
+
});
|
|
5030
|
+
await this._initPromise;
|
|
4979
5031
|
}
|
|
4980
5032
|
async getAll(config) {
|
|
5033
|
+
await this._ensureInitialized();
|
|
4981
5034
|
await this._captureEvent("get_all", {
|
|
4982
5035
|
limit: config.limit,
|
|
4983
5036
|
has_user_id: !!config.userId,
|