mem0ai 2.1.4 → 2.1.5

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.js CHANGED
@@ -657,6 +657,24 @@ var EXCLUDED_KEYS = /* @__PURE__ */ new Set([
657
657
  "created_at",
658
658
  "updated_at"
659
659
  ]);
660
+ function toSnakeCase(obj) {
661
+ if (typeof obj !== "object" || obj === null) return obj;
662
+ return Object.fromEntries(
663
+ Object.entries(obj).map(([key, value]) => [
664
+ key.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`),
665
+ value
666
+ ])
667
+ );
668
+ }
669
+ function toCamelCase(obj) {
670
+ if (typeof obj !== "object" || obj === null) return obj;
671
+ return Object.fromEntries(
672
+ Object.entries(obj).map(([key, value]) => [
673
+ key.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase()),
674
+ value
675
+ ])
676
+ );
677
+ }
660
678
  var RedisDB = class {
661
679
  constructor(config) {
662
680
  this.indexName = config.collectionName;
@@ -788,7 +806,7 @@ var RedisDB = class {
788
806
  }
789
807
  async insert(vectors, ids, payloads) {
790
808
  const data = vectors.map((vector, idx) => {
791
- const payload = payloads[idx];
809
+ const payload = toSnakeCase(payloads[idx]);
792
810
  const id = ids[idx];
793
811
  const entry = {
794
812
  memory_id: id,
@@ -824,7 +842,8 @@ var RedisDB = class {
824
842
  }
825
843
  }
826
844
  async search(query, limit = 5, filters) {
827
- const filterExpr = filters ? Object.entries(filters).filter(([_, value]) => value !== null).map(([key, value]) => `@${key}:{${value}}`).join(" ") : "*";
845
+ const snakeFilters = filters ? toSnakeCase(filters) : void 0;
846
+ const filterExpr = snakeFilters ? Object.entries(snakeFilters).filter(([_, value]) => value !== null).map(([key, value]) => `@${key}:{${value}}`).join(" ") : "*";
828
847
  const queryVector = new Float32Array(query).buffer;
829
848
  const searchOptions = {
830
849
  PARAMS: {
@@ -838,9 +857,10 @@ var RedisDB = class {
838
857
  "user_id",
839
858
  "memory",
840
859
  "metadata",
841
- "created_at"
860
+ "created_at",
861
+ "__vector_score"
842
862
  ],
843
- SORTBY: "vector_score",
863
+ SORTBY: "__vector_score",
844
864
  DIALECT: 2,
845
865
  LIMIT: {
846
866
  from: 0,
@@ -850,11 +870,12 @@ var RedisDB = class {
850
870
  try {
851
871
  const results = await this.client.ft.search(
852
872
  this.indexName,
853
- `${filterExpr} =>[KNN ${limit} @embedding $vec AS vector_score]`,
873
+ `${filterExpr} =>[KNN ${limit} @embedding $vec AS __vector_score]`,
854
874
  searchOptions
855
875
  );
856
876
  return results.documents.map((doc) => {
857
- const payload = {
877
+ var _a;
878
+ const resultPayload = {
858
879
  hash: doc.value.hash,
859
880
  data: doc.value.memory,
860
881
  created_at: new Date(parseInt(doc.value.created_at)).toISOString(),
@@ -868,8 +889,8 @@ var RedisDB = class {
868
889
  };
869
890
  return {
870
891
  id: doc.value.memory_id,
871
- payload,
872
- score: doc.value.vector_score
892
+ payload: toCamelCase(resultPayload),
893
+ score: (_a = Number(doc.value.__vector_score)) != null ? _a : 0
873
894
  };
874
895
  });
875
896
  } catch (error) {
@@ -967,22 +988,23 @@ var RedisDB = class {
967
988
  }
968
989
  }
969
990
  async update(vectorId, vector, payload) {
991
+ const snakePayload = toSnakeCase(payload);
970
992
  const entry = {
971
993
  memory_id: vectorId,
972
- hash: payload.hash,
973
- memory: payload.data,
974
- created_at: new Date(payload.created_at).getTime(),
975
- updated_at: new Date(payload.updated_at).getTime(),
994
+ hash: snakePayload.hash,
995
+ memory: snakePayload.data,
996
+ created_at: new Date(snakePayload.created_at).getTime(),
997
+ updated_at: new Date(snakePayload.updated_at).getTime(),
976
998
  embedding: Buffer.from(new Float32Array(vector).buffer)
977
999
  };
978
1000
  ["agent_id", "run_id", "user_id"].forEach((field) => {
979
- if (field in payload) {
980
- entry[field] = payload[field];
1001
+ if (field in snakePayload) {
1002
+ entry[field] = snakePayload[field];
981
1003
  }
982
1004
  });
983
1005
  entry.metadata = JSON.stringify(
984
1006
  Object.fromEntries(
985
- Object.entries(payload).filter(([key]) => !EXCLUDED_KEYS.has(key))
1007
+ Object.entries(snakePayload).filter(([key]) => !EXCLUDED_KEYS.has(key))
986
1008
  )
987
1009
  );
988
1010
  try {
@@ -1014,7 +1036,8 @@ var RedisDB = class {
1014
1036
  await this.client.ft.dropIndex(this.indexName);
1015
1037
  }
1016
1038
  async list(filters, limit = 100) {
1017
- const filterExpr = filters ? Object.entries(filters).filter(([_, value]) => value !== null).map(([key, value]) => `@${key}:{${value}}`).join(" ") : "*";
1039
+ const snakeFilters = filters ? toSnakeCase(filters) : void 0;
1040
+ const filterExpr = snakeFilters ? Object.entries(snakeFilters).filter(([_, value]) => value !== null).map(([key, value]) => `@${key}:{${value}}`).join(" ") : "*";
1018
1041
  const searchOptions = {
1019
1042
  SORTBY: "created_at",
1020
1043
  SORTDIR: "DESC",
@@ -1030,7 +1053,7 @@ var RedisDB = class {
1030
1053
  );
1031
1054
  const items = results.documents.map((doc) => ({
1032
1055
  id: doc.value.memory_id,
1033
- payload: {
1056
+ payload: toCamelCase({
1034
1057
  hash: doc.value.hash,
1035
1058
  data: doc.value.memory,
1036
1059
  created_at: new Date(parseInt(doc.value.created_at)).toISOString(),
@@ -1041,7 +1064,7 @@ var RedisDB = class {
1041
1064
  ...doc.value.run_id && { run_id: doc.value.run_id },
1042
1065
  ...doc.value.user_id && { user_id: doc.value.user_id },
1043
1066
  ...JSON.parse(doc.value.metadata || "{}")
1044
- }
1067
+ })
1045
1068
  }));
1046
1069
  return [items, results.total];
1047
1070
  }