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.
@@ -41,7 +41,10 @@ var MemoryConfigSchema = z.object({
41
41
  modelProperties: z.record(z.string(), z.any()).optional(),
42
42
  baseURL: z.string().optional(),
43
43
  url: z.string().optional(),
44
- timeout: z.number().optional()
44
+ timeout: z.number().optional(),
45
+ temperature: z.number().optional(),
46
+ topP: z.number().optional(),
47
+ maxTokens: z.number().optional()
45
48
  })
46
49
  }),
47
50
  historyDbPath: z.string().optional(),
@@ -331,25 +334,58 @@ var OpenAIStructuredLLM = class {
331
334
  import Anthropic from "@anthropic-ai/sdk";
332
335
  var AnthropicLLM = class {
333
336
  constructor(config) {
337
+ var _a2, _b2;
334
338
  const apiKey = config.apiKey || process.env.ANTHROPIC_API_KEY;
335
339
  if (!apiKey) {
336
340
  throw new Error("Anthropic API key is required");
337
341
  }
338
- this.client = new Anthropic({ apiKey });
339
- this.model = config.model || "claude-3-sonnet-20240229";
342
+ const clientArgs = { apiKey };
343
+ if (config.baseURL) {
344
+ clientArgs.baseURL = config.baseURL;
345
+ }
346
+ this.client = new Anthropic(clientArgs);
347
+ this.model = config.model || "claude-sonnet-4-6";
348
+ this.maxTokens = (_a2 = config.maxTokens) != null ? _a2 : 2e3;
349
+ this.temperature = (_b2 = config.temperature) != null ? _b2 : 0.1;
350
+ this.topP = config.topP;
340
351
  }
341
- async generateResponse(messages, responseFormat) {
352
+ async generateResponse(messages, responseFormat, tools) {
342
353
  const systemMessage = messages.find((msg) => msg.role === "system");
343
354
  const otherMessages = messages.filter((msg) => msg.role !== "system");
344
- const response = await this.client.messages.create({
355
+ const params = {
345
356
  model: this.model,
346
357
  messages: otherMessages.map((msg) => ({
347
358
  role: msg.role,
348
359
  content: typeof msg.content === "string" ? msg.content : msg.content.image_url.url
349
360
  })),
350
361
  system: typeof (systemMessage == null ? void 0 : systemMessage.content) === "string" ? systemMessage.content : void 0,
351
- max_tokens: 4096
352
- });
362
+ max_tokens: this.maxTokens
363
+ };
364
+ if (this.temperature !== void 0) {
365
+ params.temperature = this.temperature;
366
+ } else if (this.topP !== void 0) {
367
+ params.top_p = this.topP;
368
+ }
369
+ if (tools) {
370
+ params.tools = tools;
371
+ params.tool_choice = { type: "auto" };
372
+ }
373
+ const response = await this.client.messages.create(params);
374
+ if (tools) {
375
+ let content = "";
376
+ const toolCalls = [];
377
+ for (const block of response.content) {
378
+ if (block.type === "text") {
379
+ content = block.text;
380
+ } else if (block.type === "tool_use") {
381
+ toolCalls.push({
382
+ name: block.name,
383
+ arguments: JSON.stringify(block.input)
384
+ });
385
+ }
386
+ }
387
+ return { content, role: "assistant", toolCalls };
388
+ }
353
389
  const firstBlock = response.content[0];
354
390
  if (firstBlock.type === "text") {
355
391
  return firstBlock.text;
@@ -359,10 +395,10 @@ var AnthropicLLM = class {
359
395
  }
360
396
  async generateChat(messages) {
361
397
  const response = await this.generateResponse(messages);
362
- return {
363
- content: response,
364
- role: "assistant"
365
- };
398
+ if (typeof response === "string") {
399
+ return { content: response, role: "assistant" };
400
+ }
401
+ return response;
366
402
  }
367
403
  };
368
404
 
@@ -4994,7 +5030,7 @@ var ConfigManager = class {
4994
5030
  llm: {
4995
5031
  provider: ((_c = userConfig.llm) == null ? void 0 : _c.provider) || DEFAULT_MEMORY_CONFIG.llm.provider,
4996
5032
  config: (() => {
4997
- var _a3, _b3, _c2, _d2;
5033
+ var _a3, _b3, _c2, _d2, _e2, _f2, _g2;
4998
5034
  const defaultConf = DEFAULT_MEMORY_CONFIG.llm.config;
4999
5035
  const userConf = (_a3 = userConfig.llm) == null ? void 0 : _a3.config;
5000
5036
  let finalModel = defaultConf.model;
@@ -5004,12 +5040,19 @@ var ConfigManager = class {
5004
5040
  finalModel = userConf.model;
5005
5041
  }
5006
5042
  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;
5043
+ const llmRaw = userConf;
5044
+ const temperature = (_e2 = userConf == null ? void 0 : userConf.temperature) != null ? _e2 : llmRaw == null ? void 0 : llmRaw.temperature;
5045
+ const topP = (_f2 = userConf == null ? void 0 : userConf.topP) != null ? _f2 : llmRaw == null ? void 0 : llmRaw.top_p;
5046
+ const maxTokens = (_g2 = userConf == null ? void 0 : userConf.maxTokens) != null ? _g2 : llmRaw == null ? void 0 : llmRaw.max_tokens;
5007
5047
  return {
5008
5048
  baseURL: llmBaseURL,
5009
5049
  url: userConf == null ? void 0 : userConf.url,
5010
5050
  apiKey: (userConf == null ? void 0 : userConf.apiKey) !== void 0 ? userConf.apiKey : defaultConf.apiKey,
5011
5051
  model: finalModel,
5012
- modelProperties: (userConf == null ? void 0 : userConf.modelProperties) !== void 0 ? userConf.modelProperties : defaultConf.modelProperties
5052
+ modelProperties: (userConf == null ? void 0 : userConf.modelProperties) !== void 0 ? userConf.modelProperties : defaultConf.modelProperties,
5053
+ temperature,
5054
+ topP,
5055
+ maxTokens
5013
5056
  };
5014
5057
  })()
5015
5058
  },
@@ -5055,6 +5098,7 @@ var get_image_description = async (image_url) => {
5055
5098
  return response;
5056
5099
  };
5057
5100
  var parse_vision_messages = async (messages) => {
5101
+ var _a2;
5058
5102
  const parsed_messages = [];
5059
5103
  for (const message of messages) {
5060
5104
  let new_message = {
@@ -5063,9 +5107,11 @@ var parse_vision_messages = async (messages) => {
5063
5107
  };
5064
5108
  if (message.role !== "system") {
5065
5109
  if (typeof message.content === "object" && message.content.type === "image_url") {
5066
- const description = await get_image_description(
5067
- message.content.image_url.url
5068
- );
5110
+ const imageUrl = (_a2 = message.content.image_url) == null ? void 0 : _a2.url;
5111
+ if (!imageUrl) {
5112
+ throw new Error("image_url content part is missing image_url.url");
5113
+ }
5114
+ const description = await get_image_description(imageUrl);
5069
5115
  new_message.content = typeof description === "string" ? description : JSON.stringify(description);
5070
5116
  parsed_messages.push(new_message);
5071
5117
  } else parsed_messages.push(message);
@@ -5075,7 +5121,7 @@ var parse_vision_messages = async (messages) => {
5075
5121
  };
5076
5122
 
5077
5123
  // src/oss/src/utils/telemetry.ts
5078
- var version = true ? "3.0.8" : "dev";
5124
+ var version = true ? "3.0.10" : "dev";
5079
5125
  var MEM0_TELEMETRY = true;
5080
5126
  var _a, _b;
5081
5127
  try {
@@ -7499,6 +7545,25 @@ var Memory = class _Memory {
7499
7545
  "messages is required and cannot be undefined or null. Provide a string or array of messages."
7500
7546
  );
7501
7547
  }
7548
+ if (Array.isArray(messages)) {
7549
+ if (messages.length === 0) {
7550
+ throw new Error(
7551
+ "messages array cannot be empty. Provide at least one message with non-empty content."
7552
+ );
7553
+ }
7554
+ const allBlank = messages.every(
7555
+ (m) => typeof m.content === "string" && m.content.trim() === ""
7556
+ );
7557
+ if (allBlank) {
7558
+ throw new Error(
7559
+ "messages array cannot contain only blank content. Provide at least one message with non-empty content."
7560
+ );
7561
+ }
7562
+ } else if (messages.trim() === "") {
7563
+ throw new Error(
7564
+ "messages string cannot be empty. Provide non-empty content."
7565
+ );
7566
+ }
7502
7567
  const temporalUsageNotice = detectTemporalUsageFromMetadata(
7503
7568
  config == null ? void 0 : config.metadata
7504
7569
  );
@@ -7558,7 +7623,7 @@ var Memory = class _Memory {
7558
7623
  if (!infer) {
7559
7624
  const returnedMemories = [];
7560
7625
  for (const message of messages) {
7561
- if (message.content === "system") {
7626
+ if (message.role === "system") {
7562
7627
  continue;
7563
7628
  }
7564
7629
  const memoryId = await this.createMemory(
@@ -7582,7 +7647,7 @@ var Memory = class _Memory {
7582
7647
  } catch (e) {
7583
7648
  }
7584
7649
  }
7585
- const parsedMessages = messages.map((m) => m.content).join("\n");
7650
+ const parsedMessages = messages.map((m) => `${m.role}: ${m.content}`).join("\n");
7586
7651
  const queryEmbedding = await this.embedder.embed(parsedMessages);
7587
7652
  const existingResults = await this.vectorStore.search(
7588
7653
  queryEmbedding,
@@ -7941,7 +8006,13 @@ var Memory = class _Memory {
7941
8006
  memoryItem.metadata[key] = value;
7942
8007
  }
7943
8008
  }
7944
- const result = { ...memoryItem, ...filters };
8009
+ const result = {
8010
+ ...memoryItem,
8011
+ ...filters,
8012
+ ...memory.payload.attributedTo && {
8013
+ attributedTo: memory.payload.attributedTo
8014
+ }
8015
+ };
7945
8016
  await this._displayFirstRunNotice("get");
7946
8017
  return result;
7947
8018
  }
@@ -8139,6 +8210,7 @@ var Memory = class _Memory {
8139
8210
  ...payload.user_id && { user_id: payload.user_id },
8140
8211
  ...payload.agent_id && { agent_id: payload.agent_id },
8141
8212
  ...payload.run_id && { run_id: payload.run_id },
8213
+ ...payload.attributedTo && { attributedTo: payload.attributedTo },
8142
8214
  ...scored.scoreDetails && { score_details: scored.scoreDetails }
8143
8215
  };
8144
8216
  });
@@ -8332,7 +8404,10 @@ var Memory = class _Memory {
8332
8404
  metadata: Object.entries(mem.payload).filter(([key]) => !excludedKeys.has(key)).reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {}),
8333
8405
  ...mem.payload.user_id && { user_id: mem.payload.user_id },
8334
8406
  ...mem.payload.agent_id && { agent_id: mem.payload.agent_id },
8335
- ...mem.payload.run_id && { run_id: mem.payload.run_id }
8407
+ ...mem.payload.run_id && { run_id: mem.payload.run_id },
8408
+ ...mem.payload.attributedTo && {
8409
+ attributedTo: mem.payload.attributedTo
8410
+ }
8336
8411
  }));
8337
8412
  const result = { results };
8338
8413
  const scaleThresholdNotice = detectScaleThresholdFromTopK(topK);
@@ -8374,20 +8449,13 @@ var Memory = class _Memory {
8374
8449
  const prevValue = existingMemory.payload.data;
8375
8450
  const embedding = existingEmbeddings[data] || await this.embedder.embed(data);
8376
8451
  const newMetadata = {
8452
+ ...existingMemory.payload,
8377
8453
  ...metadata,
8378
8454
  data,
8379
8455
  hash: createHash("md5").update(data).digest("hex"),
8456
+ textLemmatized: lemmatizeForBm25(data),
8380
8457
  createdAt: existingMemory.payload.createdAt,
8381
- updatedAt: (/* @__PURE__ */ new Date()).toISOString(),
8382
- ...existingMemory.payload.user_id && {
8383
- user_id: existingMemory.payload.user_id
8384
- },
8385
- ...existingMemory.payload.agent_id && {
8386
- agent_id: existingMemory.payload.agent_id
8387
- },
8388
- ...existingMemory.payload.run_id && {
8389
- run_id: existingMemory.payload.run_id
8390
- }
8458
+ updatedAt: (/* @__PURE__ */ new Date()).toISOString()
8391
8459
  };
8392
8460
  await this.vectorStore.update(memoryId, embedding, newMetadata);
8393
8461
  await this.db.addHistory(