mem0ai 2.1.13 → 2.1.15
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.d.mts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +14 -6
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +14 -6
- package/dist/index.mjs.map +1 -1
- package/dist/oss/index.d.mts +38 -3
- package/dist/oss/index.d.ts +38 -3
- package/dist/oss/index.js +478 -97
- package/dist/oss/index.js.map +1 -1
- package/dist/oss/index.mjs +476 -97
- package/dist/oss/index.mjs.map +1 -1
- package/package.json +3 -1
package/dist/oss/index.mjs
CHANGED
|
@@ -331,6 +331,12 @@ var MemoryVectorStore = class {
|
|
|
331
331
|
payload TEXT NOT NULL
|
|
332
332
|
)
|
|
333
333
|
`);
|
|
334
|
+
await this.run(`
|
|
335
|
+
CREATE TABLE IF NOT EXISTS memory_migrations (
|
|
336
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
337
|
+
user_id TEXT NOT NULL UNIQUE
|
|
338
|
+
)
|
|
339
|
+
`);
|
|
334
340
|
}
|
|
335
341
|
async run(sql, params = []) {
|
|
336
342
|
return new Promise((resolve, reject) => {
|
|
@@ -465,6 +471,28 @@ var MemoryVectorStore = class {
|
|
|
465
471
|
}
|
|
466
472
|
return [results.slice(0, limit), results.length];
|
|
467
473
|
}
|
|
474
|
+
async getUserId() {
|
|
475
|
+
const row = await this.getOne(
|
|
476
|
+
`SELECT user_id FROM memory_migrations LIMIT 1`
|
|
477
|
+
);
|
|
478
|
+
if (row) {
|
|
479
|
+
return row.user_id;
|
|
480
|
+
}
|
|
481
|
+
const randomUserId = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
|
|
482
|
+
await this.run(`INSERT INTO memory_migrations (user_id) VALUES (?)`, [
|
|
483
|
+
randomUserId
|
|
484
|
+
]);
|
|
485
|
+
return randomUserId;
|
|
486
|
+
}
|
|
487
|
+
async setUserId(userId) {
|
|
488
|
+
await this.run(`DELETE FROM memory_migrations`);
|
|
489
|
+
await this.run(`INSERT INTO memory_migrations (user_id) VALUES (?)`, [
|
|
490
|
+
userId
|
|
491
|
+
]);
|
|
492
|
+
}
|
|
493
|
+
async initialize() {
|
|
494
|
+
await this.init();
|
|
495
|
+
}
|
|
468
496
|
};
|
|
469
497
|
|
|
470
498
|
// src/oss/src/vector_stores/qdrant.ts
|
|
@@ -497,49 +525,8 @@ var Qdrant = class {
|
|
|
497
525
|
this.client = new QdrantClient(params);
|
|
498
526
|
}
|
|
499
527
|
this.collectionName = config.collectionName;
|
|
500
|
-
this.
|
|
501
|
-
|
|
502
|
-
async createCol(vectorSize, onDisk, distance = "Cosine") {
|
|
503
|
-
var _a, _b;
|
|
504
|
-
try {
|
|
505
|
-
const collections = await this.client.getCollections();
|
|
506
|
-
const exists = collections.collections.some(
|
|
507
|
-
(col) => col.name === this.collectionName
|
|
508
|
-
);
|
|
509
|
-
if (!exists) {
|
|
510
|
-
const vectorParams = {
|
|
511
|
-
size: vectorSize,
|
|
512
|
-
distance,
|
|
513
|
-
on_disk: onDisk
|
|
514
|
-
};
|
|
515
|
-
try {
|
|
516
|
-
await this.client.createCollection(this.collectionName, {
|
|
517
|
-
vectors: vectorParams
|
|
518
|
-
});
|
|
519
|
-
} catch (error) {
|
|
520
|
-
if ((error == null ? void 0 : error.status) === 409) {
|
|
521
|
-
const collectionInfo = await this.client.getCollection(
|
|
522
|
-
this.collectionName
|
|
523
|
-
);
|
|
524
|
-
const vectorConfig = (_b = (_a = collectionInfo.config) == null ? void 0 : _a.params) == null ? void 0 : _b.vectors;
|
|
525
|
-
if (!vectorConfig || vectorConfig.size !== vectorSize) {
|
|
526
|
-
throw new Error(
|
|
527
|
-
`Collection ${this.collectionName} exists but has wrong configuration. Expected vector size: ${vectorSize}, got: ${vectorConfig == null ? void 0 : vectorConfig.size}`
|
|
528
|
-
);
|
|
529
|
-
}
|
|
530
|
-
return;
|
|
531
|
-
}
|
|
532
|
-
throw error;
|
|
533
|
-
}
|
|
534
|
-
}
|
|
535
|
-
} catch (error) {
|
|
536
|
-
if (error instanceof Error) {
|
|
537
|
-
console.error("Error creating/verifying collection:", error.message);
|
|
538
|
-
} else {
|
|
539
|
-
console.error("Error creating/verifying collection:", error);
|
|
540
|
-
}
|
|
541
|
-
throw error;
|
|
542
|
-
}
|
|
528
|
+
this.dimension = config.dimension || 1536;
|
|
529
|
+
this.initialize().catch(console.error);
|
|
543
530
|
}
|
|
544
531
|
createFilter(filters) {
|
|
545
532
|
if (!filters) return void 0;
|
|
@@ -633,6 +620,131 @@ var Qdrant = class {
|
|
|
633
620
|
}));
|
|
634
621
|
return [results, response.points.length];
|
|
635
622
|
}
|
|
623
|
+
generateUUID() {
|
|
624
|
+
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(
|
|
625
|
+
/[xy]/g,
|
|
626
|
+
function(c) {
|
|
627
|
+
const r = Math.random() * 16 | 0;
|
|
628
|
+
const v = c === "x" ? r : r & 3 | 8;
|
|
629
|
+
return v.toString(16);
|
|
630
|
+
}
|
|
631
|
+
);
|
|
632
|
+
}
|
|
633
|
+
async getUserId() {
|
|
634
|
+
var _a2;
|
|
635
|
+
try {
|
|
636
|
+
const collections = await this.client.getCollections();
|
|
637
|
+
const userCollectionExists = collections.collections.some(
|
|
638
|
+
(col) => col.name === "memory_migrations"
|
|
639
|
+
);
|
|
640
|
+
if (!userCollectionExists) {
|
|
641
|
+
await this.client.createCollection("memory_migrations", {
|
|
642
|
+
vectors: {
|
|
643
|
+
size: 1,
|
|
644
|
+
distance: "Cosine",
|
|
645
|
+
on_disk: false
|
|
646
|
+
}
|
|
647
|
+
});
|
|
648
|
+
}
|
|
649
|
+
const result = await this.client.scroll("memory_migrations", {
|
|
650
|
+
limit: 1,
|
|
651
|
+
with_payload: true
|
|
652
|
+
});
|
|
653
|
+
if (result.points.length > 0) {
|
|
654
|
+
return (_a2 = result.points[0].payload) == null ? void 0 : _a2.user_id;
|
|
655
|
+
}
|
|
656
|
+
const randomUserId = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
|
|
657
|
+
await this.client.upsert("memory_migrations", {
|
|
658
|
+
points: [
|
|
659
|
+
{
|
|
660
|
+
id: this.generateUUID(),
|
|
661
|
+
vector: [0],
|
|
662
|
+
payload: { user_id: randomUserId }
|
|
663
|
+
}
|
|
664
|
+
]
|
|
665
|
+
});
|
|
666
|
+
return randomUserId;
|
|
667
|
+
} catch (error) {
|
|
668
|
+
console.error("Error getting user ID:", error);
|
|
669
|
+
throw error;
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
async setUserId(userId) {
|
|
673
|
+
try {
|
|
674
|
+
const result = await this.client.scroll("memory_migrations", {
|
|
675
|
+
limit: 1,
|
|
676
|
+
with_payload: true
|
|
677
|
+
});
|
|
678
|
+
const pointId = result.points.length > 0 ? result.points[0].id : this.generateUUID();
|
|
679
|
+
await this.client.upsert("memory_migrations", {
|
|
680
|
+
points: [
|
|
681
|
+
{
|
|
682
|
+
id: pointId,
|
|
683
|
+
vector: [0],
|
|
684
|
+
payload: { user_id: userId }
|
|
685
|
+
}
|
|
686
|
+
]
|
|
687
|
+
});
|
|
688
|
+
} catch (error) {
|
|
689
|
+
console.error("Error setting user ID:", error);
|
|
690
|
+
throw error;
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
async initialize() {
|
|
694
|
+
var _a2, _b;
|
|
695
|
+
try {
|
|
696
|
+
const collections = await this.client.getCollections();
|
|
697
|
+
const exists = collections.collections.some(
|
|
698
|
+
(c) => c.name === this.collectionName
|
|
699
|
+
);
|
|
700
|
+
if (!exists) {
|
|
701
|
+
try {
|
|
702
|
+
await this.client.createCollection(this.collectionName, {
|
|
703
|
+
vectors: {
|
|
704
|
+
size: this.dimension,
|
|
705
|
+
distance: "Cosine"
|
|
706
|
+
}
|
|
707
|
+
});
|
|
708
|
+
} catch (error) {
|
|
709
|
+
if ((error == null ? void 0 : error.status) === 409) {
|
|
710
|
+
const collectionInfo = await this.client.getCollection(
|
|
711
|
+
this.collectionName
|
|
712
|
+
);
|
|
713
|
+
const vectorConfig = (_b = (_a2 = collectionInfo.config) == null ? void 0 : _a2.params) == null ? void 0 : _b.vectors;
|
|
714
|
+
if (!vectorConfig || vectorConfig.size !== this.dimension) {
|
|
715
|
+
throw new Error(
|
|
716
|
+
`Collection ${this.collectionName} exists but has wrong configuration. Expected vector size: ${this.dimension}, got: ${vectorConfig == null ? void 0 : vectorConfig.size}`
|
|
717
|
+
);
|
|
718
|
+
}
|
|
719
|
+
} else {
|
|
720
|
+
throw error;
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
const userExists = collections.collections.some(
|
|
725
|
+
(c) => c.name === "memory_migrations"
|
|
726
|
+
);
|
|
727
|
+
if (!userExists) {
|
|
728
|
+
try {
|
|
729
|
+
await this.client.createCollection("memory_migrations", {
|
|
730
|
+
vectors: {
|
|
731
|
+
size: 1,
|
|
732
|
+
// Minimal size since we only store user_id
|
|
733
|
+
distance: "Cosine"
|
|
734
|
+
}
|
|
735
|
+
});
|
|
736
|
+
} catch (error) {
|
|
737
|
+
if ((error == null ? void 0 : error.status) === 409) {
|
|
738
|
+
} else {
|
|
739
|
+
throw error;
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
} catch (error) {
|
|
744
|
+
console.error("Error initializing Qdrant:", error);
|
|
745
|
+
throw error;
|
|
746
|
+
}
|
|
747
|
+
}
|
|
636
748
|
};
|
|
637
749
|
|
|
638
750
|
// src/oss/src/vector_stores/redis.ts
|
|
@@ -729,48 +841,6 @@ var RedisDB = class {
|
|
|
729
841
|
throw err;
|
|
730
842
|
});
|
|
731
843
|
}
|
|
732
|
-
async initialize() {
|
|
733
|
-
try {
|
|
734
|
-
await this.client.connect();
|
|
735
|
-
console.log("Connected to Redis");
|
|
736
|
-
const modulesResponse = await this.client.moduleList();
|
|
737
|
-
const hasSearch = modulesResponse.some((module) => {
|
|
738
|
-
var _a;
|
|
739
|
-
const moduleMap = /* @__PURE__ */ new Map();
|
|
740
|
-
for (let i = 0; i < module.length; i += 2) {
|
|
741
|
-
moduleMap.set(module[i], module[i + 1]);
|
|
742
|
-
}
|
|
743
|
-
return ((_a = moduleMap.get("name")) == null ? void 0 : _a.toLowerCase()) === "search";
|
|
744
|
-
});
|
|
745
|
-
if (!hasSearch) {
|
|
746
|
-
throw new Error(
|
|
747
|
-
"RediSearch module is not loaded. Please ensure Redis Stack is properly installed and running."
|
|
748
|
-
);
|
|
749
|
-
}
|
|
750
|
-
let retries = 0;
|
|
751
|
-
const maxRetries = 3;
|
|
752
|
-
while (retries < maxRetries) {
|
|
753
|
-
try {
|
|
754
|
-
await this.createIndex();
|
|
755
|
-
console.log("Redis index created successfully");
|
|
756
|
-
break;
|
|
757
|
-
} catch (error) {
|
|
758
|
-
console.error(
|
|
759
|
-
`Error creating index (attempt ${retries + 1}/${maxRetries}):`,
|
|
760
|
-
error
|
|
761
|
-
);
|
|
762
|
-
retries++;
|
|
763
|
-
if (retries === maxRetries) {
|
|
764
|
-
throw error;
|
|
765
|
-
}
|
|
766
|
-
await new Promise((resolve) => setTimeout(resolve, 1e3));
|
|
767
|
-
}
|
|
768
|
-
}
|
|
769
|
-
} catch (error) {
|
|
770
|
-
console.error("Error during Redis initialization:", error);
|
|
771
|
-
throw error;
|
|
772
|
-
}
|
|
773
|
-
}
|
|
774
844
|
async createIndex() {
|
|
775
845
|
try {
|
|
776
846
|
try {
|
|
@@ -815,6 +885,52 @@ var RedisDB = class {
|
|
|
815
885
|
throw error;
|
|
816
886
|
}
|
|
817
887
|
}
|
|
888
|
+
async initialize() {
|
|
889
|
+
try {
|
|
890
|
+
await this.client.connect();
|
|
891
|
+
console.log("Connected to Redis");
|
|
892
|
+
const modulesResponse = await this.client.moduleList();
|
|
893
|
+
const hasSearch = modulesResponse.some((module) => {
|
|
894
|
+
var _a2;
|
|
895
|
+
const moduleMap = /* @__PURE__ */ new Map();
|
|
896
|
+
for (let i = 0; i < module.length; i += 2) {
|
|
897
|
+
moduleMap.set(module[i], module[i + 1]);
|
|
898
|
+
}
|
|
899
|
+
return ((_a2 = moduleMap.get("name")) == null ? void 0 : _a2.toLowerCase()) === "search";
|
|
900
|
+
});
|
|
901
|
+
if (!hasSearch) {
|
|
902
|
+
throw new Error(
|
|
903
|
+
"RediSearch module is not loaded. Please ensure Redis Stack is properly installed and running."
|
|
904
|
+
);
|
|
905
|
+
}
|
|
906
|
+
let retries = 0;
|
|
907
|
+
const maxRetries = 3;
|
|
908
|
+
while (retries < maxRetries) {
|
|
909
|
+
try {
|
|
910
|
+
await this.createIndex();
|
|
911
|
+
console.log("Redis index created successfully");
|
|
912
|
+
break;
|
|
913
|
+
} catch (error) {
|
|
914
|
+
console.error(
|
|
915
|
+
`Error creating index (attempt ${retries + 1}/${maxRetries}):`,
|
|
916
|
+
error
|
|
917
|
+
);
|
|
918
|
+
retries++;
|
|
919
|
+
if (retries === maxRetries) {
|
|
920
|
+
throw error;
|
|
921
|
+
}
|
|
922
|
+
await new Promise((resolve) => setTimeout(resolve, 1e3));
|
|
923
|
+
}
|
|
924
|
+
}
|
|
925
|
+
} catch (error) {
|
|
926
|
+
if (error instanceof Error) {
|
|
927
|
+
console.error("Error initializing Redis:", error.message);
|
|
928
|
+
} else {
|
|
929
|
+
console.error("Error initializing Redis:", error);
|
|
930
|
+
}
|
|
931
|
+
throw error;
|
|
932
|
+
}
|
|
933
|
+
}
|
|
818
934
|
async insert(vectors, ids, payloads) {
|
|
819
935
|
const data = vectors.map((vector, idx) => {
|
|
820
936
|
const payload = toSnakeCase(payloads[idx]);
|
|
@@ -885,7 +1001,7 @@ var RedisDB = class {
|
|
|
885
1001
|
searchOptions
|
|
886
1002
|
);
|
|
887
1003
|
return results.documents.map((doc) => {
|
|
888
|
-
var
|
|
1004
|
+
var _a2;
|
|
889
1005
|
const resultPayload = {
|
|
890
1006
|
hash: doc.value.hash,
|
|
891
1007
|
data: doc.value.memory,
|
|
@@ -901,7 +1017,7 @@ var RedisDB = class {
|
|
|
901
1017
|
return {
|
|
902
1018
|
id: doc.value.memory_id,
|
|
903
1019
|
payload: toCamelCase(resultPayload),
|
|
904
|
-
score: (
|
|
1020
|
+
score: (_a2 = Number(doc.value.__vector_score)) != null ? _a2 : 0
|
|
905
1021
|
};
|
|
906
1022
|
});
|
|
907
1023
|
} catch (error) {
|
|
@@ -1082,6 +1198,28 @@ var RedisDB = class {
|
|
|
1082
1198
|
async close() {
|
|
1083
1199
|
await this.client.quit();
|
|
1084
1200
|
}
|
|
1201
|
+
async getUserId() {
|
|
1202
|
+
try {
|
|
1203
|
+
const userId = await this.client.get("memory_migrations:1");
|
|
1204
|
+
if (userId) {
|
|
1205
|
+
return userId;
|
|
1206
|
+
}
|
|
1207
|
+
const randomUserId = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
|
|
1208
|
+
await this.client.set("memory_migrations:1", randomUserId);
|
|
1209
|
+
return randomUserId;
|
|
1210
|
+
} catch (error) {
|
|
1211
|
+
console.error("Error getting user ID:", error);
|
|
1212
|
+
throw error;
|
|
1213
|
+
}
|
|
1214
|
+
}
|
|
1215
|
+
async setUserId(userId) {
|
|
1216
|
+
try {
|
|
1217
|
+
await this.client.set("memory_migrations:1", userId);
|
|
1218
|
+
} catch (error) {
|
|
1219
|
+
console.error("Error setting user ID:", error);
|
|
1220
|
+
throw error;
|
|
1221
|
+
}
|
|
1222
|
+
}
|
|
1085
1223
|
};
|
|
1086
1224
|
|
|
1087
1225
|
// src/oss/src/llms/ollama.ts
|
|
@@ -1090,9 +1228,9 @@ var OllamaLLM = class {
|
|
|
1090
1228
|
constructor(config) {
|
|
1091
1229
|
// Using this variable to avoid calling the Ollama server multiple times
|
|
1092
1230
|
this.initialized = false;
|
|
1093
|
-
var
|
|
1231
|
+
var _a2;
|
|
1094
1232
|
this.ollama = new Ollama2({
|
|
1095
|
-
host: ((
|
|
1233
|
+
host: ((_a2 = config.config) == null ? void 0 : _a2.url) || "http://localhost:11434"
|
|
1096
1234
|
});
|
|
1097
1235
|
this.model = config.model || "llama3.1:8b";
|
|
1098
1236
|
this.ensureModelExists().catch((err) => {
|
|
@@ -1214,6 +1352,12 @@ create table if not exists memories (
|
|
|
1214
1352
|
updated_at timestamp with time zone default timezone('utc', now())
|
|
1215
1353
|
);
|
|
1216
1354
|
|
|
1355
|
+
-- Create the memory migrations table
|
|
1356
|
+
create table if not exists memory_migrations (
|
|
1357
|
+
user_id text primary key,
|
|
1358
|
+
created_at timestamp with time zone default timezone('utc', now())
|
|
1359
|
+
);
|
|
1360
|
+
|
|
1217
1361
|
-- Create the vector similarity search function
|
|
1218
1362
|
create or replace function match_vectors(
|
|
1219
1363
|
query_embedding vector(1536),
|
|
@@ -1360,6 +1504,39 @@ See the SQL migration instructions in the code comments.`
|
|
|
1360
1504
|
throw error;
|
|
1361
1505
|
}
|
|
1362
1506
|
}
|
|
1507
|
+
async getUserId() {
|
|
1508
|
+
try {
|
|
1509
|
+
const { data: tableExists } = await this.client.from("memory_migrations").select("user_id").limit(1);
|
|
1510
|
+
if (!tableExists || tableExists.length === 0) {
|
|
1511
|
+
const randomUserId = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
|
|
1512
|
+
const { error: insertError } = await this.client.from("memory_migrations").insert({ user_id: randomUserId });
|
|
1513
|
+
if (insertError) throw insertError;
|
|
1514
|
+
return randomUserId;
|
|
1515
|
+
}
|
|
1516
|
+
const { data, error } = await this.client.from("memory_migrations").select("user_id").limit(1);
|
|
1517
|
+
if (error) throw error;
|
|
1518
|
+
if (!data || data.length === 0) {
|
|
1519
|
+
const randomUserId = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
|
|
1520
|
+
const { error: insertError } = await this.client.from("memory_migrations").insert({ user_id: randomUserId });
|
|
1521
|
+
if (insertError) throw insertError;
|
|
1522
|
+
return randomUserId;
|
|
1523
|
+
}
|
|
1524
|
+
return data[0].user_id;
|
|
1525
|
+
} catch (error) {
|
|
1526
|
+
console.error("Error getting user ID:", error);
|
|
1527
|
+
return "anonymous-supabase";
|
|
1528
|
+
}
|
|
1529
|
+
}
|
|
1530
|
+
async setUserId(userId) {
|
|
1531
|
+
try {
|
|
1532
|
+
const { error: deleteError } = await this.client.from("memory_migrations").delete().neq("user_id", "");
|
|
1533
|
+
if (deleteError) throw deleteError;
|
|
1534
|
+
const { error: insertError } = await this.client.from("memory_migrations").insert({ user_id: userId });
|
|
1535
|
+
if (insertError) throw insertError;
|
|
1536
|
+
} catch (error) {
|
|
1537
|
+
console.error("Error setting user ID:", error);
|
|
1538
|
+
}
|
|
1539
|
+
}
|
|
1363
1540
|
};
|
|
1364
1541
|
|
|
1365
1542
|
// src/oss/src/storage/SQLiteManager.ts
|
|
@@ -1529,6 +1706,70 @@ create table ${this.tableName} (
|
|
|
1529
1706
|
}
|
|
1530
1707
|
};
|
|
1531
1708
|
|
|
1709
|
+
// src/oss/src/embeddings/google.ts
|
|
1710
|
+
import { GoogleGenAI } from "@google/genai";
|
|
1711
|
+
var GoogleEmbedder = class {
|
|
1712
|
+
constructor(config) {
|
|
1713
|
+
this.google = new GoogleGenAI({ apiKey: config.apiKey });
|
|
1714
|
+
this.model = config.model || "text-embedding-004";
|
|
1715
|
+
}
|
|
1716
|
+
async embed(text) {
|
|
1717
|
+
const response = await this.google.models.embedContent({
|
|
1718
|
+
model: this.model,
|
|
1719
|
+
contents: text,
|
|
1720
|
+
config: { outputDimensionality: 768 }
|
|
1721
|
+
});
|
|
1722
|
+
return response.embeddings[0].values;
|
|
1723
|
+
}
|
|
1724
|
+
async embedBatch(texts) {
|
|
1725
|
+
const response = await this.google.models.embedContent({
|
|
1726
|
+
model: this.model,
|
|
1727
|
+
contents: texts,
|
|
1728
|
+
config: { outputDimensionality: 768 }
|
|
1729
|
+
});
|
|
1730
|
+
return response.embeddings.map((item) => item.values);
|
|
1731
|
+
}
|
|
1732
|
+
};
|
|
1733
|
+
|
|
1734
|
+
// src/oss/src/llms/google.ts
|
|
1735
|
+
import { GoogleGenAI as GoogleGenAI2 } from "@google/genai";
|
|
1736
|
+
var GoogleLLM = class {
|
|
1737
|
+
constructor(config) {
|
|
1738
|
+
this.google = new GoogleGenAI2({ apiKey: config.apiKey });
|
|
1739
|
+
this.model = config.model || "gemini-2.0-flash";
|
|
1740
|
+
}
|
|
1741
|
+
async generateResponse(messages, responseFormat, tools) {
|
|
1742
|
+
var _a2;
|
|
1743
|
+
const completion = await this.google.models.generateContent({
|
|
1744
|
+
contents: messages.map((msg) => ({
|
|
1745
|
+
parts: [
|
|
1746
|
+
{
|
|
1747
|
+
text: typeof msg.content === "string" ? msg.content : JSON.stringify(msg.content)
|
|
1748
|
+
}
|
|
1749
|
+
],
|
|
1750
|
+
role: msg.role === "system" ? "model" : "user"
|
|
1751
|
+
})),
|
|
1752
|
+
model: this.model
|
|
1753
|
+
// config: {
|
|
1754
|
+
// responseSchema: {}, // Add response schema if needed
|
|
1755
|
+
// },
|
|
1756
|
+
});
|
|
1757
|
+
const text = (_a2 = completion.text) == null ? void 0 : _a2.replace(/^```json\n/, "").replace(/\n```$/, "");
|
|
1758
|
+
return text || "";
|
|
1759
|
+
}
|
|
1760
|
+
async generateChat(messages) {
|
|
1761
|
+
const completion = await this.google.models.generateContent({
|
|
1762
|
+
contents: messages,
|
|
1763
|
+
model: this.model
|
|
1764
|
+
});
|
|
1765
|
+
const response = completion.candidates[0].content;
|
|
1766
|
+
return {
|
|
1767
|
+
content: response.parts[0].text || "",
|
|
1768
|
+
role: response.role
|
|
1769
|
+
};
|
|
1770
|
+
}
|
|
1771
|
+
};
|
|
1772
|
+
|
|
1532
1773
|
// src/oss/src/utils/factory.ts
|
|
1533
1774
|
var EmbedderFactory = class {
|
|
1534
1775
|
static create(provider, config) {
|
|
@@ -1537,6 +1778,8 @@ var EmbedderFactory = class {
|
|
|
1537
1778
|
return new OpenAIEmbedder(config);
|
|
1538
1779
|
case "ollama":
|
|
1539
1780
|
return new OllamaEmbedder(config);
|
|
1781
|
+
case "google":
|
|
1782
|
+
return new GoogleEmbedder(config);
|
|
1540
1783
|
default:
|
|
1541
1784
|
throw new Error(`Unsupported embedder provider: ${provider}`);
|
|
1542
1785
|
}
|
|
@@ -1555,6 +1798,8 @@ var LLMFactory = class {
|
|
|
1555
1798
|
return new GroqLLM(config);
|
|
1556
1799
|
case "ollama":
|
|
1557
1800
|
return new OllamaLLM(config);
|
|
1801
|
+
case "google":
|
|
1802
|
+
return new GoogleLLM(config);
|
|
1558
1803
|
default:
|
|
1559
1804
|
throw new Error(`Unsupported LLM provider: ${provider}`);
|
|
1560
1805
|
}
|
|
@@ -1898,11 +2143,11 @@ var DEFAULT_MEMORY_CONFIG = {
|
|
|
1898
2143
|
// src/oss/src/config/manager.ts
|
|
1899
2144
|
var ConfigManager = class {
|
|
1900
2145
|
static mergeConfig(userConfig = {}) {
|
|
1901
|
-
var
|
|
2146
|
+
var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p;
|
|
1902
2147
|
const mergedConfig = {
|
|
1903
2148
|
version: userConfig.version || DEFAULT_MEMORY_CONFIG.version,
|
|
1904
2149
|
embedder: {
|
|
1905
|
-
provider: ((
|
|
2150
|
+
provider: ((_a2 = userConfig.embedder) == null ? void 0 : _a2.provider) || DEFAULT_MEMORY_CONFIG.embedder.provider,
|
|
1906
2151
|
config: {
|
|
1907
2152
|
apiKey: ((_c = (_b = userConfig.embedder) == null ? void 0 : _b.config) == null ? void 0 : _c.apiKey) || DEFAULT_MEMORY_CONFIG.embedder.config.apiKey,
|
|
1908
2153
|
model: ((_e = (_d = userConfig.embedder) == null ? void 0 : _d.config) == null ? void 0 : _e.model) || DEFAULT_MEMORY_CONFIG.embedder.config.model
|
|
@@ -2152,9 +2397,9 @@ function getDeleteMessages(existingMemoriesString, data, userId) {
|
|
|
2152
2397
|
// src/oss/src/memory/graph_memory.ts
|
|
2153
2398
|
var MemoryGraph = class {
|
|
2154
2399
|
constructor(config) {
|
|
2155
|
-
var
|
|
2400
|
+
var _a2, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
2156
2401
|
this.config = config;
|
|
2157
|
-
if (!((_b = (
|
|
2402
|
+
if (!((_b = (_a2 = config.graphStore) == null ? void 0 : _a2.config) == null ? void 0 : _b.url) || !((_d = (_c = config.graphStore) == null ? void 0 : _c.config) == null ? void 0 : _d.username) || !((_f = (_e = config.graphStore) == null ? void 0 : _e.config) == null ? void 0 : _f.password)) {
|
|
2158
2403
|
throw new Error("Neo4j configuration is incomplete");
|
|
2159
2404
|
}
|
|
2160
2405
|
this.graph = neo4j.driver(
|
|
@@ -2308,9 +2553,9 @@ var MemoryGraph = class {
|
|
|
2308
2553
|
return entityTypeMap;
|
|
2309
2554
|
}
|
|
2310
2555
|
async _establishNodesRelationsFromData(data, filters, entityTypeMap) {
|
|
2311
|
-
var
|
|
2556
|
+
var _a2;
|
|
2312
2557
|
let messages;
|
|
2313
|
-
if ((
|
|
2558
|
+
if ((_a2 = this.config.graphStore) == null ? void 0 : _a2.customPrompt) {
|
|
2314
2559
|
messages = [
|
|
2315
2560
|
{
|
|
2316
2561
|
role: "system",
|
|
@@ -2469,7 +2714,7 @@ Text: ${data}`
|
|
|
2469
2714
|
return results;
|
|
2470
2715
|
}
|
|
2471
2716
|
async _addEntities(toBeAdded, userId, entityTypeMap) {
|
|
2472
|
-
var
|
|
2717
|
+
var _a2, _b;
|
|
2473
2718
|
const results = [];
|
|
2474
2719
|
const session = this.graph.session();
|
|
2475
2720
|
try {
|
|
@@ -2540,7 +2785,7 @@ Text: ${data}`
|
|
|
2540
2785
|
RETURN source.name AS source, type(r) AS relationship, destination.name AS target
|
|
2541
2786
|
`;
|
|
2542
2787
|
params = {
|
|
2543
|
-
source_id: (
|
|
2788
|
+
source_id: (_a2 = sourceNodeSearchResult[0]) == null ? void 0 : _a2.elementId,
|
|
2544
2789
|
destination_id: (_b = destinationNodeSearchResult[0]) == null ? void 0 : _b.elementId,
|
|
2545
2790
|
user_id: userId
|
|
2546
2791
|
};
|
|
@@ -2695,6 +2940,76 @@ var parse_vision_messages = async (messages) => {
|
|
|
2695
2940
|
return parsed_messages;
|
|
2696
2941
|
};
|
|
2697
2942
|
|
|
2943
|
+
// src/oss/src/utils/telemetry.ts
|
|
2944
|
+
var version = "2.1.15";
|
|
2945
|
+
var MEM0_TELEMETRY = true;
|
|
2946
|
+
var _a;
|
|
2947
|
+
try {
|
|
2948
|
+
MEM0_TELEMETRY = ((_a = process == null ? void 0 : process.env) == null ? void 0 : _a.MEM0_TELEMETRY) === "false" ? false : true;
|
|
2949
|
+
} catch (error) {
|
|
2950
|
+
}
|
|
2951
|
+
var POSTHOG_API_KEY = "phc_hgJkUVJFYtmaJqrvf6CYN67TIQ8yhXAkWzUn9AMU4yX";
|
|
2952
|
+
var POSTHOG_HOST = "https://us.i.posthog.com/i/v0/e/";
|
|
2953
|
+
var UnifiedTelemetry = class {
|
|
2954
|
+
constructor(projectApiKey, host) {
|
|
2955
|
+
this.apiKey = projectApiKey;
|
|
2956
|
+
this.host = host;
|
|
2957
|
+
}
|
|
2958
|
+
async captureEvent(distinctId, eventName, properties = {}) {
|
|
2959
|
+
if (!MEM0_TELEMETRY) return;
|
|
2960
|
+
const eventProperties = {
|
|
2961
|
+
client_version: version,
|
|
2962
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
2963
|
+
...properties,
|
|
2964
|
+
$process_person_profile: distinctId === "anonymous" || distinctId === "anonymous-supabase" ? false : true,
|
|
2965
|
+
$lib: "posthog-node"
|
|
2966
|
+
};
|
|
2967
|
+
const payload = {
|
|
2968
|
+
api_key: this.apiKey,
|
|
2969
|
+
distinct_id: distinctId,
|
|
2970
|
+
event: eventName,
|
|
2971
|
+
properties: eventProperties
|
|
2972
|
+
};
|
|
2973
|
+
try {
|
|
2974
|
+
const response = await fetch(this.host, {
|
|
2975
|
+
method: "POST",
|
|
2976
|
+
headers: {
|
|
2977
|
+
"Content-Type": "application/json"
|
|
2978
|
+
},
|
|
2979
|
+
body: JSON.stringify(payload)
|
|
2980
|
+
});
|
|
2981
|
+
if (!response.ok) {
|
|
2982
|
+
console.error("Telemetry event capture failed:", await response.text());
|
|
2983
|
+
}
|
|
2984
|
+
} catch (error) {
|
|
2985
|
+
console.error("Telemetry event capture failed:", error);
|
|
2986
|
+
}
|
|
2987
|
+
}
|
|
2988
|
+
async shutdown() {
|
|
2989
|
+
}
|
|
2990
|
+
};
|
|
2991
|
+
var telemetry = new UnifiedTelemetry(POSTHOG_API_KEY, POSTHOG_HOST);
|
|
2992
|
+
async function captureClientEvent(eventName, instance, additionalData = {}) {
|
|
2993
|
+
if (!instance.telemetryId) {
|
|
2994
|
+
console.warn("No telemetry ID found for instance");
|
|
2995
|
+
return;
|
|
2996
|
+
}
|
|
2997
|
+
const eventData = {
|
|
2998
|
+
function: `${instance.constructor.name}`,
|
|
2999
|
+
method: eventName,
|
|
3000
|
+
api_host: instance.host,
|
|
3001
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
3002
|
+
client_version: version,
|
|
3003
|
+
client_source: "nodejs",
|
|
3004
|
+
...additionalData
|
|
3005
|
+
};
|
|
3006
|
+
await telemetry.captureEvent(
|
|
3007
|
+
instance.telemetryId,
|
|
3008
|
+
`mem0.${eventName}`,
|
|
3009
|
+
eventData
|
|
3010
|
+
);
|
|
3011
|
+
}
|
|
3012
|
+
|
|
2698
3013
|
// src/oss/src/memory/index.ts
|
|
2699
3014
|
var Memory = class _Memory {
|
|
2700
3015
|
constructor(config = {}) {
|
|
@@ -2729,9 +3044,46 @@ var Memory = class _Memory {
|
|
|
2729
3044
|
this.collectionName = this.config.vectorStore.config.collectionName;
|
|
2730
3045
|
this.apiVersion = this.config.version || "v1.0";
|
|
2731
3046
|
this.enableGraph = this.config.enableGraph || false;
|
|
3047
|
+
this.telemetryId = "anonymous";
|
|
2732
3048
|
if (this.enableGraph && this.config.graphStore) {
|
|
2733
3049
|
this.graphMemory = new MemoryGraph(this.config);
|
|
2734
3050
|
}
|
|
3051
|
+
this._initializeTelemetry();
|
|
3052
|
+
}
|
|
3053
|
+
async _initializeTelemetry() {
|
|
3054
|
+
try {
|
|
3055
|
+
await this._getTelemetryId();
|
|
3056
|
+
await captureClientEvent("init", this, {
|
|
3057
|
+
api_version: this.apiVersion,
|
|
3058
|
+
client_type: "Memory",
|
|
3059
|
+
collection_name: this.collectionName,
|
|
3060
|
+
enable_graph: this.enableGraph
|
|
3061
|
+
});
|
|
3062
|
+
} catch (error) {
|
|
3063
|
+
}
|
|
3064
|
+
}
|
|
3065
|
+
async _getTelemetryId() {
|
|
3066
|
+
try {
|
|
3067
|
+
if (!this.telemetryId || this.telemetryId === "anonymous" || this.telemetryId === "anonymous-supabase") {
|
|
3068
|
+
this.telemetryId = await this.vectorStore.getUserId();
|
|
3069
|
+
}
|
|
3070
|
+
return this.telemetryId;
|
|
3071
|
+
} catch (error) {
|
|
3072
|
+
this.telemetryId = "anonymous";
|
|
3073
|
+
return this.telemetryId;
|
|
3074
|
+
}
|
|
3075
|
+
}
|
|
3076
|
+
async _captureEvent(methodName, additionalData = {}) {
|
|
3077
|
+
try {
|
|
3078
|
+
await this._getTelemetryId();
|
|
3079
|
+
await captureClientEvent(methodName, this, {
|
|
3080
|
+
...additionalData,
|
|
3081
|
+
api_version: this.apiVersion,
|
|
3082
|
+
collection_name: this.collectionName
|
|
3083
|
+
});
|
|
3084
|
+
} catch (error) {
|
|
3085
|
+
console.error(`Failed to capture ${methodName} event:`, error);
|
|
3086
|
+
}
|
|
2735
3087
|
}
|
|
2736
3088
|
static fromConfig(configDict) {
|
|
2737
3089
|
try {
|
|
@@ -2743,6 +3095,12 @@ var Memory = class _Memory {
|
|
|
2743
3095
|
}
|
|
2744
3096
|
}
|
|
2745
3097
|
async add(messages, config) {
|
|
3098
|
+
await this._captureEvent("add", {
|
|
3099
|
+
message_count: Array.isArray(messages) ? messages.length : 1,
|
|
3100
|
+
has_metadata: !!config.metadata,
|
|
3101
|
+
has_filters: !!config.filters,
|
|
3102
|
+
infer: config.infer
|
|
3103
|
+
});
|
|
2746
3104
|
const {
|
|
2747
3105
|
userId,
|
|
2748
3106
|
agentId,
|
|
@@ -2929,6 +3287,11 @@ ${parsedMessages}`] : getFactRetrievalMessages(parsedMessages);
|
|
|
2929
3287
|
return { ...memoryItem, ...filters };
|
|
2930
3288
|
}
|
|
2931
3289
|
async search(query, config) {
|
|
3290
|
+
await this._captureEvent("search", {
|
|
3291
|
+
query_length: query.length,
|
|
3292
|
+
limit: config.limit,
|
|
3293
|
+
has_filters: !!config.filters
|
|
3294
|
+
});
|
|
2932
3295
|
const { userId, agentId, runId, limit = 100, filters = {} } = config;
|
|
2933
3296
|
if (userId) filters.userId = userId;
|
|
2934
3297
|
if (agentId) filters.agentId = agentId;
|
|
@@ -2979,15 +3342,22 @@ ${parsedMessages}`] : getFactRetrievalMessages(parsedMessages);
|
|
|
2979
3342
|
};
|
|
2980
3343
|
}
|
|
2981
3344
|
async update(memoryId, data) {
|
|
3345
|
+
await this._captureEvent("update", { memory_id: memoryId });
|
|
2982
3346
|
const embedding = await this.embedder.embed(data);
|
|
2983
3347
|
await this.updateMemory(memoryId, data, { [data]: embedding });
|
|
2984
3348
|
return { message: "Memory updated successfully!" };
|
|
2985
3349
|
}
|
|
2986
3350
|
async delete(memoryId) {
|
|
3351
|
+
await this._captureEvent("delete", { memory_id: memoryId });
|
|
2987
3352
|
await this.deleteMemory(memoryId);
|
|
2988
3353
|
return { message: "Memory deleted successfully!" };
|
|
2989
3354
|
}
|
|
2990
3355
|
async deleteAll(config) {
|
|
3356
|
+
await this._captureEvent("delete_all", {
|
|
3357
|
+
has_user_id: !!config.userId,
|
|
3358
|
+
has_agent_id: !!config.agentId,
|
|
3359
|
+
has_run_id: !!config.runId
|
|
3360
|
+
});
|
|
2991
3361
|
const { userId, agentId, runId } = config;
|
|
2992
3362
|
const filters = {};
|
|
2993
3363
|
if (userId) filters.userId = userId;
|
|
@@ -3008,6 +3378,7 @@ ${parsedMessages}`] : getFactRetrievalMessages(parsedMessages);
|
|
|
3008
3378
|
return this.db.getHistory(memoryId);
|
|
3009
3379
|
}
|
|
3010
3380
|
async reset() {
|
|
3381
|
+
await this._captureEvent("reset");
|
|
3011
3382
|
await this.db.reset();
|
|
3012
3383
|
await this.vectorStore.deleteCol();
|
|
3013
3384
|
if (this.graphMemory) {
|
|
@@ -3019,6 +3390,12 @@ ${parsedMessages}`] : getFactRetrievalMessages(parsedMessages);
|
|
|
3019
3390
|
);
|
|
3020
3391
|
}
|
|
3021
3392
|
async getAll(config) {
|
|
3393
|
+
await this._captureEvent("get_all", {
|
|
3394
|
+
limit: config.limit,
|
|
3395
|
+
has_user_id: !!config.userId,
|
|
3396
|
+
has_agent_id: !!config.agentId,
|
|
3397
|
+
has_run_id: !!config.runId
|
|
3398
|
+
});
|
|
3022
3399
|
const { userId, agentId, runId, limit = 100 } = config;
|
|
3023
3400
|
const filters = {};
|
|
3024
3401
|
if (userId) filters.userId = userId;
|
|
@@ -3122,6 +3499,8 @@ ${parsedMessages}`] : getFactRetrievalMessages(parsedMessages);
|
|
|
3122
3499
|
export {
|
|
3123
3500
|
AnthropicLLM,
|
|
3124
3501
|
EmbedderFactory,
|
|
3502
|
+
GoogleEmbedder,
|
|
3503
|
+
GoogleLLM,
|
|
3125
3504
|
GroqLLM,
|
|
3126
3505
|
HistoryManagerFactory,
|
|
3127
3506
|
LLMFactory,
|