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.js
CHANGED
|
@@ -32,6 +32,8 @@ var index_exports = {};
|
|
|
32
32
|
__export(index_exports, {
|
|
33
33
|
AnthropicLLM: () => AnthropicLLM,
|
|
34
34
|
EmbedderFactory: () => EmbedderFactory,
|
|
35
|
+
GoogleEmbedder: () => GoogleEmbedder,
|
|
36
|
+
GoogleLLM: () => GoogleLLM,
|
|
35
37
|
GroqLLM: () => GroqLLM,
|
|
36
38
|
HistoryManagerFactory: () => HistoryManagerFactory,
|
|
37
39
|
LLMFactory: () => LLMFactory,
|
|
@@ -382,6 +384,12 @@ var MemoryVectorStore = class {
|
|
|
382
384
|
payload TEXT NOT NULL
|
|
383
385
|
)
|
|
384
386
|
`);
|
|
387
|
+
await this.run(`
|
|
388
|
+
CREATE TABLE IF NOT EXISTS memory_migrations (
|
|
389
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
390
|
+
user_id TEXT NOT NULL UNIQUE
|
|
391
|
+
)
|
|
392
|
+
`);
|
|
385
393
|
}
|
|
386
394
|
async run(sql, params = []) {
|
|
387
395
|
return new Promise((resolve, reject) => {
|
|
@@ -516,6 +524,28 @@ var MemoryVectorStore = class {
|
|
|
516
524
|
}
|
|
517
525
|
return [results.slice(0, limit), results.length];
|
|
518
526
|
}
|
|
527
|
+
async getUserId() {
|
|
528
|
+
const row = await this.getOne(
|
|
529
|
+
`SELECT user_id FROM memory_migrations LIMIT 1`
|
|
530
|
+
);
|
|
531
|
+
if (row) {
|
|
532
|
+
return row.user_id;
|
|
533
|
+
}
|
|
534
|
+
const randomUserId = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
|
|
535
|
+
await this.run(`INSERT INTO memory_migrations (user_id) VALUES (?)`, [
|
|
536
|
+
randomUserId
|
|
537
|
+
]);
|
|
538
|
+
return randomUserId;
|
|
539
|
+
}
|
|
540
|
+
async setUserId(userId) {
|
|
541
|
+
await this.run(`DELETE FROM memory_migrations`);
|
|
542
|
+
await this.run(`INSERT INTO memory_migrations (user_id) VALUES (?)`, [
|
|
543
|
+
userId
|
|
544
|
+
]);
|
|
545
|
+
}
|
|
546
|
+
async initialize() {
|
|
547
|
+
await this.init();
|
|
548
|
+
}
|
|
519
549
|
};
|
|
520
550
|
|
|
521
551
|
// src/oss/src/vector_stores/qdrant.ts
|
|
@@ -548,49 +578,8 @@ var Qdrant = class {
|
|
|
548
578
|
this.client = new import_js_client_rest.QdrantClient(params);
|
|
549
579
|
}
|
|
550
580
|
this.collectionName = config.collectionName;
|
|
551
|
-
this.
|
|
552
|
-
|
|
553
|
-
async createCol(vectorSize, onDisk, distance = "Cosine") {
|
|
554
|
-
var _a, _b;
|
|
555
|
-
try {
|
|
556
|
-
const collections = await this.client.getCollections();
|
|
557
|
-
const exists = collections.collections.some(
|
|
558
|
-
(col) => col.name === this.collectionName
|
|
559
|
-
);
|
|
560
|
-
if (!exists) {
|
|
561
|
-
const vectorParams = {
|
|
562
|
-
size: vectorSize,
|
|
563
|
-
distance,
|
|
564
|
-
on_disk: onDisk
|
|
565
|
-
};
|
|
566
|
-
try {
|
|
567
|
-
await this.client.createCollection(this.collectionName, {
|
|
568
|
-
vectors: vectorParams
|
|
569
|
-
});
|
|
570
|
-
} catch (error) {
|
|
571
|
-
if ((error == null ? void 0 : error.status) === 409) {
|
|
572
|
-
const collectionInfo = await this.client.getCollection(
|
|
573
|
-
this.collectionName
|
|
574
|
-
);
|
|
575
|
-
const vectorConfig = (_b = (_a = collectionInfo.config) == null ? void 0 : _a.params) == null ? void 0 : _b.vectors;
|
|
576
|
-
if (!vectorConfig || vectorConfig.size !== vectorSize) {
|
|
577
|
-
throw new Error(
|
|
578
|
-
`Collection ${this.collectionName} exists but has wrong configuration. Expected vector size: ${vectorSize}, got: ${vectorConfig == null ? void 0 : vectorConfig.size}`
|
|
579
|
-
);
|
|
580
|
-
}
|
|
581
|
-
return;
|
|
582
|
-
}
|
|
583
|
-
throw error;
|
|
584
|
-
}
|
|
585
|
-
}
|
|
586
|
-
} catch (error) {
|
|
587
|
-
if (error instanceof Error) {
|
|
588
|
-
console.error("Error creating/verifying collection:", error.message);
|
|
589
|
-
} else {
|
|
590
|
-
console.error("Error creating/verifying collection:", error);
|
|
591
|
-
}
|
|
592
|
-
throw error;
|
|
593
|
-
}
|
|
581
|
+
this.dimension = config.dimension || 1536;
|
|
582
|
+
this.initialize().catch(console.error);
|
|
594
583
|
}
|
|
595
584
|
createFilter(filters) {
|
|
596
585
|
if (!filters) return void 0;
|
|
@@ -684,6 +673,131 @@ var Qdrant = class {
|
|
|
684
673
|
}));
|
|
685
674
|
return [results, response.points.length];
|
|
686
675
|
}
|
|
676
|
+
generateUUID() {
|
|
677
|
+
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(
|
|
678
|
+
/[xy]/g,
|
|
679
|
+
function(c) {
|
|
680
|
+
const r = Math.random() * 16 | 0;
|
|
681
|
+
const v = c === "x" ? r : r & 3 | 8;
|
|
682
|
+
return v.toString(16);
|
|
683
|
+
}
|
|
684
|
+
);
|
|
685
|
+
}
|
|
686
|
+
async getUserId() {
|
|
687
|
+
var _a2;
|
|
688
|
+
try {
|
|
689
|
+
const collections = await this.client.getCollections();
|
|
690
|
+
const userCollectionExists = collections.collections.some(
|
|
691
|
+
(col) => col.name === "memory_migrations"
|
|
692
|
+
);
|
|
693
|
+
if (!userCollectionExists) {
|
|
694
|
+
await this.client.createCollection("memory_migrations", {
|
|
695
|
+
vectors: {
|
|
696
|
+
size: 1,
|
|
697
|
+
distance: "Cosine",
|
|
698
|
+
on_disk: false
|
|
699
|
+
}
|
|
700
|
+
});
|
|
701
|
+
}
|
|
702
|
+
const result = await this.client.scroll("memory_migrations", {
|
|
703
|
+
limit: 1,
|
|
704
|
+
with_payload: true
|
|
705
|
+
});
|
|
706
|
+
if (result.points.length > 0) {
|
|
707
|
+
return (_a2 = result.points[0].payload) == null ? void 0 : _a2.user_id;
|
|
708
|
+
}
|
|
709
|
+
const randomUserId = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
|
|
710
|
+
await this.client.upsert("memory_migrations", {
|
|
711
|
+
points: [
|
|
712
|
+
{
|
|
713
|
+
id: this.generateUUID(),
|
|
714
|
+
vector: [0],
|
|
715
|
+
payload: { user_id: randomUserId }
|
|
716
|
+
}
|
|
717
|
+
]
|
|
718
|
+
});
|
|
719
|
+
return randomUserId;
|
|
720
|
+
} catch (error) {
|
|
721
|
+
console.error("Error getting user ID:", error);
|
|
722
|
+
throw error;
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
async setUserId(userId) {
|
|
726
|
+
try {
|
|
727
|
+
const result = await this.client.scroll("memory_migrations", {
|
|
728
|
+
limit: 1,
|
|
729
|
+
with_payload: true
|
|
730
|
+
});
|
|
731
|
+
const pointId = result.points.length > 0 ? result.points[0].id : this.generateUUID();
|
|
732
|
+
await this.client.upsert("memory_migrations", {
|
|
733
|
+
points: [
|
|
734
|
+
{
|
|
735
|
+
id: pointId,
|
|
736
|
+
vector: [0],
|
|
737
|
+
payload: { user_id: userId }
|
|
738
|
+
}
|
|
739
|
+
]
|
|
740
|
+
});
|
|
741
|
+
} catch (error) {
|
|
742
|
+
console.error("Error setting user ID:", error);
|
|
743
|
+
throw error;
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
async initialize() {
|
|
747
|
+
var _a2, _b;
|
|
748
|
+
try {
|
|
749
|
+
const collections = await this.client.getCollections();
|
|
750
|
+
const exists = collections.collections.some(
|
|
751
|
+
(c) => c.name === this.collectionName
|
|
752
|
+
);
|
|
753
|
+
if (!exists) {
|
|
754
|
+
try {
|
|
755
|
+
await this.client.createCollection(this.collectionName, {
|
|
756
|
+
vectors: {
|
|
757
|
+
size: this.dimension,
|
|
758
|
+
distance: "Cosine"
|
|
759
|
+
}
|
|
760
|
+
});
|
|
761
|
+
} catch (error) {
|
|
762
|
+
if ((error == null ? void 0 : error.status) === 409) {
|
|
763
|
+
const collectionInfo = await this.client.getCollection(
|
|
764
|
+
this.collectionName
|
|
765
|
+
);
|
|
766
|
+
const vectorConfig = (_b = (_a2 = collectionInfo.config) == null ? void 0 : _a2.params) == null ? void 0 : _b.vectors;
|
|
767
|
+
if (!vectorConfig || vectorConfig.size !== this.dimension) {
|
|
768
|
+
throw new Error(
|
|
769
|
+
`Collection ${this.collectionName} exists but has wrong configuration. Expected vector size: ${this.dimension}, got: ${vectorConfig == null ? void 0 : vectorConfig.size}`
|
|
770
|
+
);
|
|
771
|
+
}
|
|
772
|
+
} else {
|
|
773
|
+
throw error;
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
const userExists = collections.collections.some(
|
|
778
|
+
(c) => c.name === "memory_migrations"
|
|
779
|
+
);
|
|
780
|
+
if (!userExists) {
|
|
781
|
+
try {
|
|
782
|
+
await this.client.createCollection("memory_migrations", {
|
|
783
|
+
vectors: {
|
|
784
|
+
size: 1,
|
|
785
|
+
// Minimal size since we only store user_id
|
|
786
|
+
distance: "Cosine"
|
|
787
|
+
}
|
|
788
|
+
});
|
|
789
|
+
} catch (error) {
|
|
790
|
+
if ((error == null ? void 0 : error.status) === 409) {
|
|
791
|
+
} else {
|
|
792
|
+
throw error;
|
|
793
|
+
}
|
|
794
|
+
}
|
|
795
|
+
}
|
|
796
|
+
} catch (error) {
|
|
797
|
+
console.error("Error initializing Qdrant:", error);
|
|
798
|
+
throw error;
|
|
799
|
+
}
|
|
800
|
+
}
|
|
687
801
|
};
|
|
688
802
|
|
|
689
803
|
// src/oss/src/vector_stores/redis.ts
|
|
@@ -780,48 +894,6 @@ var RedisDB = class {
|
|
|
780
894
|
throw err;
|
|
781
895
|
});
|
|
782
896
|
}
|
|
783
|
-
async initialize() {
|
|
784
|
-
try {
|
|
785
|
-
await this.client.connect();
|
|
786
|
-
console.log("Connected to Redis");
|
|
787
|
-
const modulesResponse = await this.client.moduleList();
|
|
788
|
-
const hasSearch = modulesResponse.some((module2) => {
|
|
789
|
-
var _a;
|
|
790
|
-
const moduleMap = /* @__PURE__ */ new Map();
|
|
791
|
-
for (let i = 0; i < module2.length; i += 2) {
|
|
792
|
-
moduleMap.set(module2[i], module2[i + 1]);
|
|
793
|
-
}
|
|
794
|
-
return ((_a = moduleMap.get("name")) == null ? void 0 : _a.toLowerCase()) === "search";
|
|
795
|
-
});
|
|
796
|
-
if (!hasSearch) {
|
|
797
|
-
throw new Error(
|
|
798
|
-
"RediSearch module is not loaded. Please ensure Redis Stack is properly installed and running."
|
|
799
|
-
);
|
|
800
|
-
}
|
|
801
|
-
let retries = 0;
|
|
802
|
-
const maxRetries = 3;
|
|
803
|
-
while (retries < maxRetries) {
|
|
804
|
-
try {
|
|
805
|
-
await this.createIndex();
|
|
806
|
-
console.log("Redis index created successfully");
|
|
807
|
-
break;
|
|
808
|
-
} catch (error) {
|
|
809
|
-
console.error(
|
|
810
|
-
`Error creating index (attempt ${retries + 1}/${maxRetries}):`,
|
|
811
|
-
error
|
|
812
|
-
);
|
|
813
|
-
retries++;
|
|
814
|
-
if (retries === maxRetries) {
|
|
815
|
-
throw error;
|
|
816
|
-
}
|
|
817
|
-
await new Promise((resolve) => setTimeout(resolve, 1e3));
|
|
818
|
-
}
|
|
819
|
-
}
|
|
820
|
-
} catch (error) {
|
|
821
|
-
console.error("Error during Redis initialization:", error);
|
|
822
|
-
throw error;
|
|
823
|
-
}
|
|
824
|
-
}
|
|
825
897
|
async createIndex() {
|
|
826
898
|
try {
|
|
827
899
|
try {
|
|
@@ -866,6 +938,52 @@ var RedisDB = class {
|
|
|
866
938
|
throw error;
|
|
867
939
|
}
|
|
868
940
|
}
|
|
941
|
+
async initialize() {
|
|
942
|
+
try {
|
|
943
|
+
await this.client.connect();
|
|
944
|
+
console.log("Connected to Redis");
|
|
945
|
+
const modulesResponse = await this.client.moduleList();
|
|
946
|
+
const hasSearch = modulesResponse.some((module2) => {
|
|
947
|
+
var _a2;
|
|
948
|
+
const moduleMap = /* @__PURE__ */ new Map();
|
|
949
|
+
for (let i = 0; i < module2.length; i += 2) {
|
|
950
|
+
moduleMap.set(module2[i], module2[i + 1]);
|
|
951
|
+
}
|
|
952
|
+
return ((_a2 = moduleMap.get("name")) == null ? void 0 : _a2.toLowerCase()) === "search";
|
|
953
|
+
});
|
|
954
|
+
if (!hasSearch) {
|
|
955
|
+
throw new Error(
|
|
956
|
+
"RediSearch module is not loaded. Please ensure Redis Stack is properly installed and running."
|
|
957
|
+
);
|
|
958
|
+
}
|
|
959
|
+
let retries = 0;
|
|
960
|
+
const maxRetries = 3;
|
|
961
|
+
while (retries < maxRetries) {
|
|
962
|
+
try {
|
|
963
|
+
await this.createIndex();
|
|
964
|
+
console.log("Redis index created successfully");
|
|
965
|
+
break;
|
|
966
|
+
} catch (error) {
|
|
967
|
+
console.error(
|
|
968
|
+
`Error creating index (attempt ${retries + 1}/${maxRetries}):`,
|
|
969
|
+
error
|
|
970
|
+
);
|
|
971
|
+
retries++;
|
|
972
|
+
if (retries === maxRetries) {
|
|
973
|
+
throw error;
|
|
974
|
+
}
|
|
975
|
+
await new Promise((resolve) => setTimeout(resolve, 1e3));
|
|
976
|
+
}
|
|
977
|
+
}
|
|
978
|
+
} catch (error) {
|
|
979
|
+
if (error instanceof Error) {
|
|
980
|
+
console.error("Error initializing Redis:", error.message);
|
|
981
|
+
} else {
|
|
982
|
+
console.error("Error initializing Redis:", error);
|
|
983
|
+
}
|
|
984
|
+
throw error;
|
|
985
|
+
}
|
|
986
|
+
}
|
|
869
987
|
async insert(vectors, ids, payloads) {
|
|
870
988
|
const data = vectors.map((vector, idx) => {
|
|
871
989
|
const payload = toSnakeCase(payloads[idx]);
|
|
@@ -936,7 +1054,7 @@ var RedisDB = class {
|
|
|
936
1054
|
searchOptions
|
|
937
1055
|
);
|
|
938
1056
|
return results.documents.map((doc) => {
|
|
939
|
-
var
|
|
1057
|
+
var _a2;
|
|
940
1058
|
const resultPayload = {
|
|
941
1059
|
hash: doc.value.hash,
|
|
942
1060
|
data: doc.value.memory,
|
|
@@ -952,7 +1070,7 @@ var RedisDB = class {
|
|
|
952
1070
|
return {
|
|
953
1071
|
id: doc.value.memory_id,
|
|
954
1072
|
payload: toCamelCase(resultPayload),
|
|
955
|
-
score: (
|
|
1073
|
+
score: (_a2 = Number(doc.value.__vector_score)) != null ? _a2 : 0
|
|
956
1074
|
};
|
|
957
1075
|
});
|
|
958
1076
|
} catch (error) {
|
|
@@ -1133,6 +1251,28 @@ var RedisDB = class {
|
|
|
1133
1251
|
async close() {
|
|
1134
1252
|
await this.client.quit();
|
|
1135
1253
|
}
|
|
1254
|
+
async getUserId() {
|
|
1255
|
+
try {
|
|
1256
|
+
const userId = await this.client.get("memory_migrations:1");
|
|
1257
|
+
if (userId) {
|
|
1258
|
+
return userId;
|
|
1259
|
+
}
|
|
1260
|
+
const randomUserId = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
|
|
1261
|
+
await this.client.set("memory_migrations:1", randomUserId);
|
|
1262
|
+
return randomUserId;
|
|
1263
|
+
} catch (error) {
|
|
1264
|
+
console.error("Error getting user ID:", error);
|
|
1265
|
+
throw error;
|
|
1266
|
+
}
|
|
1267
|
+
}
|
|
1268
|
+
async setUserId(userId) {
|
|
1269
|
+
try {
|
|
1270
|
+
await this.client.set("memory_migrations:1", userId);
|
|
1271
|
+
} catch (error) {
|
|
1272
|
+
console.error("Error setting user ID:", error);
|
|
1273
|
+
throw error;
|
|
1274
|
+
}
|
|
1275
|
+
}
|
|
1136
1276
|
};
|
|
1137
1277
|
|
|
1138
1278
|
// src/oss/src/llms/ollama.ts
|
|
@@ -1141,9 +1281,9 @@ var OllamaLLM = class {
|
|
|
1141
1281
|
constructor(config) {
|
|
1142
1282
|
// Using this variable to avoid calling the Ollama server multiple times
|
|
1143
1283
|
this.initialized = false;
|
|
1144
|
-
var
|
|
1284
|
+
var _a2;
|
|
1145
1285
|
this.ollama = new import_ollama2.Ollama({
|
|
1146
|
-
host: ((
|
|
1286
|
+
host: ((_a2 = config.config) == null ? void 0 : _a2.url) || "http://localhost:11434"
|
|
1147
1287
|
});
|
|
1148
1288
|
this.model = config.model || "llama3.1:8b";
|
|
1149
1289
|
this.ensureModelExists().catch((err) => {
|
|
@@ -1265,6 +1405,12 @@ create table if not exists memories (
|
|
|
1265
1405
|
updated_at timestamp with time zone default timezone('utc', now())
|
|
1266
1406
|
);
|
|
1267
1407
|
|
|
1408
|
+
-- Create the memory migrations table
|
|
1409
|
+
create table if not exists memory_migrations (
|
|
1410
|
+
user_id text primary key,
|
|
1411
|
+
created_at timestamp with time zone default timezone('utc', now())
|
|
1412
|
+
);
|
|
1413
|
+
|
|
1268
1414
|
-- Create the vector similarity search function
|
|
1269
1415
|
create or replace function match_vectors(
|
|
1270
1416
|
query_embedding vector(1536),
|
|
@@ -1411,6 +1557,39 @@ See the SQL migration instructions in the code comments.`
|
|
|
1411
1557
|
throw error;
|
|
1412
1558
|
}
|
|
1413
1559
|
}
|
|
1560
|
+
async getUserId() {
|
|
1561
|
+
try {
|
|
1562
|
+
const { data: tableExists } = await this.client.from("memory_migrations").select("user_id").limit(1);
|
|
1563
|
+
if (!tableExists || tableExists.length === 0) {
|
|
1564
|
+
const randomUserId = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
|
|
1565
|
+
const { error: insertError } = await this.client.from("memory_migrations").insert({ user_id: randomUserId });
|
|
1566
|
+
if (insertError) throw insertError;
|
|
1567
|
+
return randomUserId;
|
|
1568
|
+
}
|
|
1569
|
+
const { data, error } = await this.client.from("memory_migrations").select("user_id").limit(1);
|
|
1570
|
+
if (error) throw error;
|
|
1571
|
+
if (!data || data.length === 0) {
|
|
1572
|
+
const randomUserId = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
|
|
1573
|
+
const { error: insertError } = await this.client.from("memory_migrations").insert({ user_id: randomUserId });
|
|
1574
|
+
if (insertError) throw insertError;
|
|
1575
|
+
return randomUserId;
|
|
1576
|
+
}
|
|
1577
|
+
return data[0].user_id;
|
|
1578
|
+
} catch (error) {
|
|
1579
|
+
console.error("Error getting user ID:", error);
|
|
1580
|
+
return "anonymous-supabase";
|
|
1581
|
+
}
|
|
1582
|
+
}
|
|
1583
|
+
async setUserId(userId) {
|
|
1584
|
+
try {
|
|
1585
|
+
const { error: deleteError } = await this.client.from("memory_migrations").delete().neq("user_id", "");
|
|
1586
|
+
if (deleteError) throw deleteError;
|
|
1587
|
+
const { error: insertError } = await this.client.from("memory_migrations").insert({ user_id: userId });
|
|
1588
|
+
if (insertError) throw insertError;
|
|
1589
|
+
} catch (error) {
|
|
1590
|
+
console.error("Error setting user ID:", error);
|
|
1591
|
+
}
|
|
1592
|
+
}
|
|
1414
1593
|
};
|
|
1415
1594
|
|
|
1416
1595
|
// src/oss/src/storage/SQLiteManager.ts
|
|
@@ -1580,6 +1759,70 @@ create table ${this.tableName} (
|
|
|
1580
1759
|
}
|
|
1581
1760
|
};
|
|
1582
1761
|
|
|
1762
|
+
// src/oss/src/embeddings/google.ts
|
|
1763
|
+
var import_genai = require("@google/genai");
|
|
1764
|
+
var GoogleEmbedder = class {
|
|
1765
|
+
constructor(config) {
|
|
1766
|
+
this.google = new import_genai.GoogleGenAI({ apiKey: config.apiKey });
|
|
1767
|
+
this.model = config.model || "text-embedding-004";
|
|
1768
|
+
}
|
|
1769
|
+
async embed(text) {
|
|
1770
|
+
const response = await this.google.models.embedContent({
|
|
1771
|
+
model: this.model,
|
|
1772
|
+
contents: text,
|
|
1773
|
+
config: { outputDimensionality: 768 }
|
|
1774
|
+
});
|
|
1775
|
+
return response.embeddings[0].values;
|
|
1776
|
+
}
|
|
1777
|
+
async embedBatch(texts) {
|
|
1778
|
+
const response = await this.google.models.embedContent({
|
|
1779
|
+
model: this.model,
|
|
1780
|
+
contents: texts,
|
|
1781
|
+
config: { outputDimensionality: 768 }
|
|
1782
|
+
});
|
|
1783
|
+
return response.embeddings.map((item) => item.values);
|
|
1784
|
+
}
|
|
1785
|
+
};
|
|
1786
|
+
|
|
1787
|
+
// src/oss/src/llms/google.ts
|
|
1788
|
+
var import_genai2 = require("@google/genai");
|
|
1789
|
+
var GoogleLLM = class {
|
|
1790
|
+
constructor(config) {
|
|
1791
|
+
this.google = new import_genai2.GoogleGenAI({ apiKey: config.apiKey });
|
|
1792
|
+
this.model = config.model || "gemini-2.0-flash";
|
|
1793
|
+
}
|
|
1794
|
+
async generateResponse(messages, responseFormat, tools) {
|
|
1795
|
+
var _a2;
|
|
1796
|
+
const completion = await this.google.models.generateContent({
|
|
1797
|
+
contents: messages.map((msg) => ({
|
|
1798
|
+
parts: [
|
|
1799
|
+
{
|
|
1800
|
+
text: typeof msg.content === "string" ? msg.content : JSON.stringify(msg.content)
|
|
1801
|
+
}
|
|
1802
|
+
],
|
|
1803
|
+
role: msg.role === "system" ? "model" : "user"
|
|
1804
|
+
})),
|
|
1805
|
+
model: this.model
|
|
1806
|
+
// config: {
|
|
1807
|
+
// responseSchema: {}, // Add response schema if needed
|
|
1808
|
+
// },
|
|
1809
|
+
});
|
|
1810
|
+
const text = (_a2 = completion.text) == null ? void 0 : _a2.replace(/^```json\n/, "").replace(/\n```$/, "");
|
|
1811
|
+
return text || "";
|
|
1812
|
+
}
|
|
1813
|
+
async generateChat(messages) {
|
|
1814
|
+
const completion = await this.google.models.generateContent({
|
|
1815
|
+
contents: messages,
|
|
1816
|
+
model: this.model
|
|
1817
|
+
});
|
|
1818
|
+
const response = completion.candidates[0].content;
|
|
1819
|
+
return {
|
|
1820
|
+
content: response.parts[0].text || "",
|
|
1821
|
+
role: response.role
|
|
1822
|
+
};
|
|
1823
|
+
}
|
|
1824
|
+
};
|
|
1825
|
+
|
|
1583
1826
|
// src/oss/src/utils/factory.ts
|
|
1584
1827
|
var EmbedderFactory = class {
|
|
1585
1828
|
static create(provider, config) {
|
|
@@ -1588,6 +1831,8 @@ var EmbedderFactory = class {
|
|
|
1588
1831
|
return new OpenAIEmbedder(config);
|
|
1589
1832
|
case "ollama":
|
|
1590
1833
|
return new OllamaEmbedder(config);
|
|
1834
|
+
case "google":
|
|
1835
|
+
return new GoogleEmbedder(config);
|
|
1591
1836
|
default:
|
|
1592
1837
|
throw new Error(`Unsupported embedder provider: ${provider}`);
|
|
1593
1838
|
}
|
|
@@ -1606,6 +1851,8 @@ var LLMFactory = class {
|
|
|
1606
1851
|
return new GroqLLM(config);
|
|
1607
1852
|
case "ollama":
|
|
1608
1853
|
return new OllamaLLM(config);
|
|
1854
|
+
case "google":
|
|
1855
|
+
return new GoogleLLM(config);
|
|
1609
1856
|
default:
|
|
1610
1857
|
throw new Error(`Unsupported LLM provider: ${provider}`);
|
|
1611
1858
|
}
|
|
@@ -1949,11 +2196,11 @@ var DEFAULT_MEMORY_CONFIG = {
|
|
|
1949
2196
|
// src/oss/src/config/manager.ts
|
|
1950
2197
|
var ConfigManager = class {
|
|
1951
2198
|
static mergeConfig(userConfig = {}) {
|
|
1952
|
-
var
|
|
2199
|
+
var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p;
|
|
1953
2200
|
const mergedConfig = {
|
|
1954
2201
|
version: userConfig.version || DEFAULT_MEMORY_CONFIG.version,
|
|
1955
2202
|
embedder: {
|
|
1956
|
-
provider: ((
|
|
2203
|
+
provider: ((_a2 = userConfig.embedder) == null ? void 0 : _a2.provider) || DEFAULT_MEMORY_CONFIG.embedder.provider,
|
|
1957
2204
|
config: {
|
|
1958
2205
|
apiKey: ((_c = (_b = userConfig.embedder) == null ? void 0 : _b.config) == null ? void 0 : _c.apiKey) || DEFAULT_MEMORY_CONFIG.embedder.config.apiKey,
|
|
1959
2206
|
model: ((_e = (_d = userConfig.embedder) == null ? void 0 : _d.config) == null ? void 0 : _e.model) || DEFAULT_MEMORY_CONFIG.embedder.config.model
|
|
@@ -2203,9 +2450,9 @@ function getDeleteMessages(existingMemoriesString, data, userId) {
|
|
|
2203
2450
|
// src/oss/src/memory/graph_memory.ts
|
|
2204
2451
|
var MemoryGraph = class {
|
|
2205
2452
|
constructor(config) {
|
|
2206
|
-
var
|
|
2453
|
+
var _a2, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
2207
2454
|
this.config = config;
|
|
2208
|
-
if (!((_b = (
|
|
2455
|
+
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)) {
|
|
2209
2456
|
throw new Error("Neo4j configuration is incomplete");
|
|
2210
2457
|
}
|
|
2211
2458
|
this.graph = import_neo4j_driver.default.driver(
|
|
@@ -2359,9 +2606,9 @@ var MemoryGraph = class {
|
|
|
2359
2606
|
return entityTypeMap;
|
|
2360
2607
|
}
|
|
2361
2608
|
async _establishNodesRelationsFromData(data, filters, entityTypeMap) {
|
|
2362
|
-
var
|
|
2609
|
+
var _a2;
|
|
2363
2610
|
let messages;
|
|
2364
|
-
if ((
|
|
2611
|
+
if ((_a2 = this.config.graphStore) == null ? void 0 : _a2.customPrompt) {
|
|
2365
2612
|
messages = [
|
|
2366
2613
|
{
|
|
2367
2614
|
role: "system",
|
|
@@ -2520,7 +2767,7 @@ Text: ${data}`
|
|
|
2520
2767
|
return results;
|
|
2521
2768
|
}
|
|
2522
2769
|
async _addEntities(toBeAdded, userId, entityTypeMap) {
|
|
2523
|
-
var
|
|
2770
|
+
var _a2, _b;
|
|
2524
2771
|
const results = [];
|
|
2525
2772
|
const session = this.graph.session();
|
|
2526
2773
|
try {
|
|
@@ -2591,7 +2838,7 @@ Text: ${data}`
|
|
|
2591
2838
|
RETURN source.name AS source, type(r) AS relationship, destination.name AS target
|
|
2592
2839
|
`;
|
|
2593
2840
|
params = {
|
|
2594
|
-
source_id: (
|
|
2841
|
+
source_id: (_a2 = sourceNodeSearchResult[0]) == null ? void 0 : _a2.elementId,
|
|
2595
2842
|
destination_id: (_b = destinationNodeSearchResult[0]) == null ? void 0 : _b.elementId,
|
|
2596
2843
|
user_id: userId
|
|
2597
2844
|
};
|
|
@@ -2746,6 +2993,76 @@ var parse_vision_messages = async (messages) => {
|
|
|
2746
2993
|
return parsed_messages;
|
|
2747
2994
|
};
|
|
2748
2995
|
|
|
2996
|
+
// src/oss/src/utils/telemetry.ts
|
|
2997
|
+
var version = "2.1.15";
|
|
2998
|
+
var MEM0_TELEMETRY = true;
|
|
2999
|
+
var _a;
|
|
3000
|
+
try {
|
|
3001
|
+
MEM0_TELEMETRY = ((_a = process == null ? void 0 : process.env) == null ? void 0 : _a.MEM0_TELEMETRY) === "false" ? false : true;
|
|
3002
|
+
} catch (error) {
|
|
3003
|
+
}
|
|
3004
|
+
var POSTHOG_API_KEY = "phc_hgJkUVJFYtmaJqrvf6CYN67TIQ8yhXAkWzUn9AMU4yX";
|
|
3005
|
+
var POSTHOG_HOST = "https://us.i.posthog.com/i/v0/e/";
|
|
3006
|
+
var UnifiedTelemetry = class {
|
|
3007
|
+
constructor(projectApiKey, host) {
|
|
3008
|
+
this.apiKey = projectApiKey;
|
|
3009
|
+
this.host = host;
|
|
3010
|
+
}
|
|
3011
|
+
async captureEvent(distinctId, eventName, properties = {}) {
|
|
3012
|
+
if (!MEM0_TELEMETRY) return;
|
|
3013
|
+
const eventProperties = {
|
|
3014
|
+
client_version: version,
|
|
3015
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
3016
|
+
...properties,
|
|
3017
|
+
$process_person_profile: distinctId === "anonymous" || distinctId === "anonymous-supabase" ? false : true,
|
|
3018
|
+
$lib: "posthog-node"
|
|
3019
|
+
};
|
|
3020
|
+
const payload = {
|
|
3021
|
+
api_key: this.apiKey,
|
|
3022
|
+
distinct_id: distinctId,
|
|
3023
|
+
event: eventName,
|
|
3024
|
+
properties: eventProperties
|
|
3025
|
+
};
|
|
3026
|
+
try {
|
|
3027
|
+
const response = await fetch(this.host, {
|
|
3028
|
+
method: "POST",
|
|
3029
|
+
headers: {
|
|
3030
|
+
"Content-Type": "application/json"
|
|
3031
|
+
},
|
|
3032
|
+
body: JSON.stringify(payload)
|
|
3033
|
+
});
|
|
3034
|
+
if (!response.ok) {
|
|
3035
|
+
console.error("Telemetry event capture failed:", await response.text());
|
|
3036
|
+
}
|
|
3037
|
+
} catch (error) {
|
|
3038
|
+
console.error("Telemetry event capture failed:", error);
|
|
3039
|
+
}
|
|
3040
|
+
}
|
|
3041
|
+
async shutdown() {
|
|
3042
|
+
}
|
|
3043
|
+
};
|
|
3044
|
+
var telemetry = new UnifiedTelemetry(POSTHOG_API_KEY, POSTHOG_HOST);
|
|
3045
|
+
async function captureClientEvent(eventName, instance, additionalData = {}) {
|
|
3046
|
+
if (!instance.telemetryId) {
|
|
3047
|
+
console.warn("No telemetry ID found for instance");
|
|
3048
|
+
return;
|
|
3049
|
+
}
|
|
3050
|
+
const eventData = {
|
|
3051
|
+
function: `${instance.constructor.name}`,
|
|
3052
|
+
method: eventName,
|
|
3053
|
+
api_host: instance.host,
|
|
3054
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
3055
|
+
client_version: version,
|
|
3056
|
+
client_source: "nodejs",
|
|
3057
|
+
...additionalData
|
|
3058
|
+
};
|
|
3059
|
+
await telemetry.captureEvent(
|
|
3060
|
+
instance.telemetryId,
|
|
3061
|
+
`mem0.${eventName}`,
|
|
3062
|
+
eventData
|
|
3063
|
+
);
|
|
3064
|
+
}
|
|
3065
|
+
|
|
2749
3066
|
// src/oss/src/memory/index.ts
|
|
2750
3067
|
var Memory = class _Memory {
|
|
2751
3068
|
constructor(config = {}) {
|
|
@@ -2780,9 +3097,46 @@ var Memory = class _Memory {
|
|
|
2780
3097
|
this.collectionName = this.config.vectorStore.config.collectionName;
|
|
2781
3098
|
this.apiVersion = this.config.version || "v1.0";
|
|
2782
3099
|
this.enableGraph = this.config.enableGraph || false;
|
|
3100
|
+
this.telemetryId = "anonymous";
|
|
2783
3101
|
if (this.enableGraph && this.config.graphStore) {
|
|
2784
3102
|
this.graphMemory = new MemoryGraph(this.config);
|
|
2785
3103
|
}
|
|
3104
|
+
this._initializeTelemetry();
|
|
3105
|
+
}
|
|
3106
|
+
async _initializeTelemetry() {
|
|
3107
|
+
try {
|
|
3108
|
+
await this._getTelemetryId();
|
|
3109
|
+
await captureClientEvent("init", this, {
|
|
3110
|
+
api_version: this.apiVersion,
|
|
3111
|
+
client_type: "Memory",
|
|
3112
|
+
collection_name: this.collectionName,
|
|
3113
|
+
enable_graph: this.enableGraph
|
|
3114
|
+
});
|
|
3115
|
+
} catch (error) {
|
|
3116
|
+
}
|
|
3117
|
+
}
|
|
3118
|
+
async _getTelemetryId() {
|
|
3119
|
+
try {
|
|
3120
|
+
if (!this.telemetryId || this.telemetryId === "anonymous" || this.telemetryId === "anonymous-supabase") {
|
|
3121
|
+
this.telemetryId = await this.vectorStore.getUserId();
|
|
3122
|
+
}
|
|
3123
|
+
return this.telemetryId;
|
|
3124
|
+
} catch (error) {
|
|
3125
|
+
this.telemetryId = "anonymous";
|
|
3126
|
+
return this.telemetryId;
|
|
3127
|
+
}
|
|
3128
|
+
}
|
|
3129
|
+
async _captureEvent(methodName, additionalData = {}) {
|
|
3130
|
+
try {
|
|
3131
|
+
await this._getTelemetryId();
|
|
3132
|
+
await captureClientEvent(methodName, this, {
|
|
3133
|
+
...additionalData,
|
|
3134
|
+
api_version: this.apiVersion,
|
|
3135
|
+
collection_name: this.collectionName
|
|
3136
|
+
});
|
|
3137
|
+
} catch (error) {
|
|
3138
|
+
console.error(`Failed to capture ${methodName} event:`, error);
|
|
3139
|
+
}
|
|
2786
3140
|
}
|
|
2787
3141
|
static fromConfig(configDict) {
|
|
2788
3142
|
try {
|
|
@@ -2794,6 +3148,12 @@ var Memory = class _Memory {
|
|
|
2794
3148
|
}
|
|
2795
3149
|
}
|
|
2796
3150
|
async add(messages, config) {
|
|
3151
|
+
await this._captureEvent("add", {
|
|
3152
|
+
message_count: Array.isArray(messages) ? messages.length : 1,
|
|
3153
|
+
has_metadata: !!config.metadata,
|
|
3154
|
+
has_filters: !!config.filters,
|
|
3155
|
+
infer: config.infer
|
|
3156
|
+
});
|
|
2797
3157
|
const {
|
|
2798
3158
|
userId,
|
|
2799
3159
|
agentId,
|
|
@@ -2980,6 +3340,11 @@ ${parsedMessages}`] : getFactRetrievalMessages(parsedMessages);
|
|
|
2980
3340
|
return { ...memoryItem, ...filters };
|
|
2981
3341
|
}
|
|
2982
3342
|
async search(query, config) {
|
|
3343
|
+
await this._captureEvent("search", {
|
|
3344
|
+
query_length: query.length,
|
|
3345
|
+
limit: config.limit,
|
|
3346
|
+
has_filters: !!config.filters
|
|
3347
|
+
});
|
|
2983
3348
|
const { userId, agentId, runId, limit = 100, filters = {} } = config;
|
|
2984
3349
|
if (userId) filters.userId = userId;
|
|
2985
3350
|
if (agentId) filters.agentId = agentId;
|
|
@@ -3030,15 +3395,22 @@ ${parsedMessages}`] : getFactRetrievalMessages(parsedMessages);
|
|
|
3030
3395
|
};
|
|
3031
3396
|
}
|
|
3032
3397
|
async update(memoryId, data) {
|
|
3398
|
+
await this._captureEvent("update", { memory_id: memoryId });
|
|
3033
3399
|
const embedding = await this.embedder.embed(data);
|
|
3034
3400
|
await this.updateMemory(memoryId, data, { [data]: embedding });
|
|
3035
3401
|
return { message: "Memory updated successfully!" };
|
|
3036
3402
|
}
|
|
3037
3403
|
async delete(memoryId) {
|
|
3404
|
+
await this._captureEvent("delete", { memory_id: memoryId });
|
|
3038
3405
|
await this.deleteMemory(memoryId);
|
|
3039
3406
|
return { message: "Memory deleted successfully!" };
|
|
3040
3407
|
}
|
|
3041
3408
|
async deleteAll(config) {
|
|
3409
|
+
await this._captureEvent("delete_all", {
|
|
3410
|
+
has_user_id: !!config.userId,
|
|
3411
|
+
has_agent_id: !!config.agentId,
|
|
3412
|
+
has_run_id: !!config.runId
|
|
3413
|
+
});
|
|
3042
3414
|
const { userId, agentId, runId } = config;
|
|
3043
3415
|
const filters = {};
|
|
3044
3416
|
if (userId) filters.userId = userId;
|
|
@@ -3059,6 +3431,7 @@ ${parsedMessages}`] : getFactRetrievalMessages(parsedMessages);
|
|
|
3059
3431
|
return this.db.getHistory(memoryId);
|
|
3060
3432
|
}
|
|
3061
3433
|
async reset() {
|
|
3434
|
+
await this._captureEvent("reset");
|
|
3062
3435
|
await this.db.reset();
|
|
3063
3436
|
await this.vectorStore.deleteCol();
|
|
3064
3437
|
if (this.graphMemory) {
|
|
@@ -3070,6 +3443,12 @@ ${parsedMessages}`] : getFactRetrievalMessages(parsedMessages);
|
|
|
3070
3443
|
);
|
|
3071
3444
|
}
|
|
3072
3445
|
async getAll(config) {
|
|
3446
|
+
await this._captureEvent("get_all", {
|
|
3447
|
+
limit: config.limit,
|
|
3448
|
+
has_user_id: !!config.userId,
|
|
3449
|
+
has_agent_id: !!config.agentId,
|
|
3450
|
+
has_run_id: !!config.runId
|
|
3451
|
+
});
|
|
3073
3452
|
const { userId, agentId, runId, limit = 100 } = config;
|
|
3074
3453
|
const filters = {};
|
|
3075
3454
|
if (userId) filters.userId = userId;
|
|
@@ -3174,6 +3553,8 @@ ${parsedMessages}`] : getFactRetrievalMessages(parsedMessages);
|
|
|
3174
3553
|
0 && (module.exports = {
|
|
3175
3554
|
AnthropicLLM,
|
|
3176
3555
|
EmbedderFactory,
|
|
3556
|
+
GoogleEmbedder,
|
|
3557
|
+
GoogleLLM,
|
|
3177
3558
|
GroqLLM,
|
|
3178
3559
|
HistoryManagerFactory,
|
|
3179
3560
|
LLMFactory,
|