mem0ai 3.0.8 → 3.0.10

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
@@ -99,7 +99,10 @@ var MemoryConfigSchema = import_zod.z.object({
99
99
  modelProperties: import_zod.z.record(import_zod.z.string(), import_zod.z.any()).optional(),
100
100
  baseURL: import_zod.z.string().optional(),
101
101
  url: import_zod.z.string().optional(),
102
- timeout: import_zod.z.number().optional()
102
+ timeout: import_zod.z.number().optional(),
103
+ temperature: import_zod.z.number().optional(),
104
+ topP: import_zod.z.number().optional(),
105
+ maxTokens: import_zod.z.number().optional()
103
106
  })
104
107
  }),
105
108
  historyDbPath: import_zod.z.string().optional(),
@@ -389,25 +392,58 @@ var OpenAIStructuredLLM = class {
389
392
  var import_sdk = __toESM(require("@anthropic-ai/sdk"));
390
393
  var AnthropicLLM = class {
391
394
  constructor(config) {
395
+ var _a2, _b2;
392
396
  const apiKey = config.apiKey || process.env.ANTHROPIC_API_KEY;
393
397
  if (!apiKey) {
394
398
  throw new Error("Anthropic API key is required");
395
399
  }
396
- this.client = new import_sdk.default({ apiKey });
397
- this.model = config.model || "claude-3-sonnet-20240229";
400
+ const clientArgs = { apiKey };
401
+ if (config.baseURL) {
402
+ clientArgs.baseURL = config.baseURL;
403
+ }
404
+ this.client = new import_sdk.default(clientArgs);
405
+ this.model = config.model || "claude-sonnet-4-6";
406
+ this.maxTokens = (_a2 = config.maxTokens) != null ? _a2 : 2e3;
407
+ this.temperature = (_b2 = config.temperature) != null ? _b2 : 0.1;
408
+ this.topP = config.topP;
398
409
  }
399
- async generateResponse(messages, responseFormat) {
410
+ async generateResponse(messages, responseFormat, tools) {
400
411
  const systemMessage = messages.find((msg) => msg.role === "system");
401
412
  const otherMessages = messages.filter((msg) => msg.role !== "system");
402
- const response = await this.client.messages.create({
413
+ const params = {
403
414
  model: this.model,
404
415
  messages: otherMessages.map((msg) => ({
405
416
  role: msg.role,
406
417
  content: typeof msg.content === "string" ? msg.content : msg.content.image_url.url
407
418
  })),
408
419
  system: typeof (systemMessage == null ? void 0 : systemMessage.content) === "string" ? systemMessage.content : void 0,
409
- max_tokens: 4096
410
- });
420
+ max_tokens: this.maxTokens
421
+ };
422
+ if (this.temperature !== void 0) {
423
+ params.temperature = this.temperature;
424
+ } else if (this.topP !== void 0) {
425
+ params.top_p = this.topP;
426
+ }
427
+ if (tools) {
428
+ params.tools = tools;
429
+ params.tool_choice = { type: "auto" };
430
+ }
431
+ const response = await this.client.messages.create(params);
432
+ if (tools) {
433
+ let content = "";
434
+ const toolCalls = [];
435
+ for (const block of response.content) {
436
+ if (block.type === "text") {
437
+ content = block.text;
438
+ } else if (block.type === "tool_use") {
439
+ toolCalls.push({
440
+ name: block.name,
441
+ arguments: JSON.stringify(block.input)
442
+ });
443
+ }
444
+ }
445
+ return { content, role: "assistant", toolCalls };
446
+ }
411
447
  const firstBlock = response.content[0];
412
448
  if (firstBlock.type === "text") {
413
449
  return firstBlock.text;
@@ -417,10 +453,10 @@ var AnthropicLLM = class {
417
453
  }
418
454
  async generateChat(messages) {
419
455
  const response = await this.generateResponse(messages);
420
- return {
421
- content: response,
422
- role: "assistant"
423
- };
456
+ if (typeof response === "string") {
457
+ return { content: response, role: "assistant" };
458
+ }
459
+ return response;
424
460
  }
425
461
  };
426
462
 
@@ -5044,7 +5080,7 @@ var ConfigManager = class {
5044
5080
  llm: {
5045
5081
  provider: ((_c = userConfig.llm) == null ? void 0 : _c.provider) || DEFAULT_MEMORY_CONFIG.llm.provider,
5046
5082
  config: (() => {
5047
- var _a3, _b3, _c2, _d2;
5083
+ var _a3, _b3, _c2, _d2, _e2, _f2, _g2;
5048
5084
  const defaultConf = DEFAULT_MEMORY_CONFIG.llm.config;
5049
5085
  const userConf = (_a3 = userConfig.llm) == null ? void 0 : _a3.config;
5050
5086
  let finalModel = defaultConf.model;
@@ -5054,12 +5090,19 @@ var ConfigManager = class {
5054
5090
  finalModel = userConf.model;
5055
5091
  }
5056
5092
  const llmBaseURL = (_d2 = (_c2 = (_b3 = userConf == null ? void 0 : userConf.baseURL) != null ? _b3 : userConf == null ? void 0 : userConf.lmstudio_base_url) != null ? _c2 : userConf == null ? void 0 : userConf.url) != null ? _d2 : defaultConf.baseURL;
5093
+ const llmRaw = userConf;
5094
+ const temperature = (_e2 = userConf == null ? void 0 : userConf.temperature) != null ? _e2 : llmRaw == null ? void 0 : llmRaw.temperature;
5095
+ const topP = (_f2 = userConf == null ? void 0 : userConf.topP) != null ? _f2 : llmRaw == null ? void 0 : llmRaw.top_p;
5096
+ const maxTokens = (_g2 = userConf == null ? void 0 : userConf.maxTokens) != null ? _g2 : llmRaw == null ? void 0 : llmRaw.max_tokens;
5057
5097
  return {
5058
5098
  baseURL: llmBaseURL,
5059
5099
  url: userConf == null ? void 0 : userConf.url,
5060
5100
  apiKey: (userConf == null ? void 0 : userConf.apiKey) !== void 0 ? userConf.apiKey : defaultConf.apiKey,
5061
5101
  model: finalModel,
5062
- modelProperties: (userConf == null ? void 0 : userConf.modelProperties) !== void 0 ? userConf.modelProperties : defaultConf.modelProperties
5102
+ modelProperties: (userConf == null ? void 0 : userConf.modelProperties) !== void 0 ? userConf.modelProperties : defaultConf.modelProperties,
5103
+ temperature,
5104
+ topP,
5105
+ maxTokens
5063
5106
  };
5064
5107
  })()
5065
5108
  },
@@ -5105,6 +5148,7 @@ var get_image_description = async (image_url) => {
5105
5148
  return response;
5106
5149
  };
5107
5150
  var parse_vision_messages = async (messages) => {
5151
+ var _a2;
5108
5152
  const parsed_messages = [];
5109
5153
  for (const message of messages) {
5110
5154
  let new_message = {
@@ -5113,9 +5157,11 @@ var parse_vision_messages = async (messages) => {
5113
5157
  };
5114
5158
  if (message.role !== "system") {
5115
5159
  if (typeof message.content === "object" && message.content.type === "image_url") {
5116
- const description = await get_image_description(
5117
- message.content.image_url.url
5118
- );
5160
+ const imageUrl = (_a2 = message.content.image_url) == null ? void 0 : _a2.url;
5161
+ if (!imageUrl) {
5162
+ throw new Error("image_url content part is missing image_url.url");
5163
+ }
5164
+ const description = await get_image_description(imageUrl);
5119
5165
  new_message.content = typeof description === "string" ? description : JSON.stringify(description);
5120
5166
  parsed_messages.push(new_message);
5121
5167
  } else parsed_messages.push(message);
@@ -5125,7 +5171,7 @@ var parse_vision_messages = async (messages) => {
5125
5171
  };
5126
5172
 
5127
5173
  // src/oss/src/utils/telemetry.ts
5128
- var version = true ? "3.0.8" : "dev";
5174
+ var version = true ? "3.0.10" : "dev";
5129
5175
  var MEM0_TELEMETRY = true;
5130
5176
  var _a, _b;
5131
5177
  try {
@@ -7549,6 +7595,25 @@ var Memory = class _Memory {
7549
7595
  "messages is required and cannot be undefined or null. Provide a string or array of messages."
7550
7596
  );
7551
7597
  }
7598
+ if (Array.isArray(messages)) {
7599
+ if (messages.length === 0) {
7600
+ throw new Error(
7601
+ "messages array cannot be empty. Provide at least one message with non-empty content."
7602
+ );
7603
+ }
7604
+ const allBlank = messages.every(
7605
+ (m) => typeof m.content === "string" && m.content.trim() === ""
7606
+ );
7607
+ if (allBlank) {
7608
+ throw new Error(
7609
+ "messages array cannot contain only blank content. Provide at least one message with non-empty content."
7610
+ );
7611
+ }
7612
+ } else if (messages.trim() === "") {
7613
+ throw new Error(
7614
+ "messages string cannot be empty. Provide non-empty content."
7615
+ );
7616
+ }
7552
7617
  const temporalUsageNotice = detectTemporalUsageFromMetadata(
7553
7618
  config == null ? void 0 : config.metadata
7554
7619
  );
@@ -7608,7 +7673,7 @@ var Memory = class _Memory {
7608
7673
  if (!infer) {
7609
7674
  const returnedMemories = [];
7610
7675
  for (const message of messages) {
7611
- if (message.content === "system") {
7676
+ if (message.role === "system") {
7612
7677
  continue;
7613
7678
  }
7614
7679
  const memoryId = await this.createMemory(
@@ -7632,7 +7697,7 @@ var Memory = class _Memory {
7632
7697
  } catch (e) {
7633
7698
  }
7634
7699
  }
7635
- const parsedMessages = messages.map((m) => m.content).join("\n");
7700
+ const parsedMessages = messages.map((m) => `${m.role}: ${m.content}`).join("\n");
7636
7701
  const queryEmbedding = await this.embedder.embed(parsedMessages);
7637
7702
  const existingResults = await this.vectorStore.search(
7638
7703
  queryEmbedding,
@@ -7991,7 +8056,13 @@ var Memory = class _Memory {
7991
8056
  memoryItem.metadata[key] = value;
7992
8057
  }
7993
8058
  }
7994
- const result = { ...memoryItem, ...filters };
8059
+ const result = {
8060
+ ...memoryItem,
8061
+ ...filters,
8062
+ ...memory.payload.attributedTo && {
8063
+ attributedTo: memory.payload.attributedTo
8064
+ }
8065
+ };
7995
8066
  await this._displayFirstRunNotice("get");
7996
8067
  return result;
7997
8068
  }
@@ -8189,6 +8260,7 @@ var Memory = class _Memory {
8189
8260
  ...payload.user_id && { user_id: payload.user_id },
8190
8261
  ...payload.agent_id && { agent_id: payload.agent_id },
8191
8262
  ...payload.run_id && { run_id: payload.run_id },
8263
+ ...payload.attributedTo && { attributedTo: payload.attributedTo },
8192
8264
  ...scored.scoreDetails && { score_details: scored.scoreDetails }
8193
8265
  };
8194
8266
  });
@@ -8382,7 +8454,10 @@ var Memory = class _Memory {
8382
8454
  metadata: Object.entries(mem.payload).filter(([key]) => !excludedKeys.has(key)).reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {}),
8383
8455
  ...mem.payload.user_id && { user_id: mem.payload.user_id },
8384
8456
  ...mem.payload.agent_id && { agent_id: mem.payload.agent_id },
8385
- ...mem.payload.run_id && { run_id: mem.payload.run_id }
8457
+ ...mem.payload.run_id && { run_id: mem.payload.run_id },
8458
+ ...mem.payload.attributedTo && {
8459
+ attributedTo: mem.payload.attributedTo
8460
+ }
8386
8461
  }));
8387
8462
  const result = { results };
8388
8463
  const scaleThresholdNotice = detectScaleThresholdFromTopK(topK);
@@ -8424,20 +8499,13 @@ var Memory = class _Memory {
8424
8499
  const prevValue = existingMemory.payload.data;
8425
8500
  const embedding = existingEmbeddings[data] || await this.embedder.embed(data);
8426
8501
  const newMetadata = {
8502
+ ...existingMemory.payload,
8427
8503
  ...metadata,
8428
8504
  data,
8429
8505
  hash: (0, import_crypto2.createHash)("md5").update(data).digest("hex"),
8506
+ textLemmatized: lemmatizeForBm25(data),
8430
8507
  createdAt: existingMemory.payload.createdAt,
8431
- updatedAt: (/* @__PURE__ */ new Date()).toISOString(),
8432
- ...existingMemory.payload.user_id && {
8433
- user_id: existingMemory.payload.user_id
8434
- },
8435
- ...existingMemory.payload.agent_id && {
8436
- agent_id: existingMemory.payload.agent_id
8437
- },
8438
- ...existingMemory.payload.run_id && {
8439
- run_id: existingMemory.payload.run_id
8440
- }
8508
+ updatedAt: (/* @__PURE__ */ new Date()).toISOString()
8441
8509
  };
8442
8510
  await this.vectorStore.update(memoryId, embedding, newMetadata);
8443
8511
  await this.db.addHistory(