mem0ai 2.4.6 → 3.0.0-beta.0

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.mjs CHANGED
@@ -75,6 +75,37 @@ async function captureClientEvent(eventName, instance, additionalData = {}) {
75
75
  );
76
76
  }
77
77
 
78
+ // src/client/utils.ts
79
+ function camelToSnake(str) {
80
+ if (str === str.toUpperCase()) return str;
81
+ return str.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`);
82
+ }
83
+ function snakeToCamel(str) {
84
+ return str.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase());
85
+ }
86
+ function camelToSnakeKeys(obj) {
87
+ if (obj === null || obj === void 0 || typeof obj !== "object") return obj;
88
+ if (Array.isArray(obj)) return obj.map(camelToSnakeKeys);
89
+ if (obj instanceof Date) return obj;
90
+ return Object.fromEntries(
91
+ Object.entries(obj).map(([key, value]) => [
92
+ camelToSnake(key),
93
+ camelToSnakeKeys(value)
94
+ ])
95
+ );
96
+ }
97
+ function snakeToCamelKeys(obj) {
98
+ if (obj === null || obj === void 0 || typeof obj !== "object") return obj;
99
+ if (Array.isArray(obj)) return obj.map(snakeToCamelKeys);
100
+ if (obj instanceof Date) return obj;
101
+ return Object.fromEntries(
102
+ Object.entries(obj).map(([key, value]) => [
103
+ snakeToCamel(key),
104
+ snakeToCamelKeys(value)
105
+ ])
106
+ );
107
+ }
108
+
78
109
  // src/common/exceptions.ts
79
110
  var MemoryError = class extends Error {
80
111
  constructor(message, errorCode, options = {}) {
@@ -191,25 +222,11 @@ var MemoryClient = class {
191
222
  throw new Error("Mem0 API key cannot be empty");
192
223
  }
193
224
  }
194
- _validateOrgProject() {
195
- if (this.organizationName === null && this.projectName !== null || this.organizationName !== null && this.projectName === null) {
196
- console.warn(
197
- "Warning: Both organizationName and projectName must be provided together when using either. This will be removed from version 1.0.40. Note that organizationName/projectName are being deprecated in favor of organizationId/projectId."
198
- );
199
- }
200
- if (this.organizationId === null && this.projectId !== null || this.organizationId !== null && this.projectId === null) {
201
- console.warn(
202
- "Warning: Both organizationId and projectId must be provided together when using either. This will be removed from version 1.0.40."
203
- );
204
- }
205
- }
206
225
  constructor(options) {
207
226
  this.apiKey = options.apiKey;
208
227
  this.host = options.host || "https://api.mem0.ai";
209
- this.organizationName = options.organizationName || null;
210
- this.projectName = options.projectName || null;
211
- this.organizationId = options.organizationId || null;
212
- this.projectId = options.projectId || null;
228
+ this.organizationId = null;
229
+ this.projectId = null;
213
230
  this.headers = {
214
231
  Authorization: `Token ${this.apiKey}`,
215
232
  "Content-Type": "application/json"
@@ -229,9 +246,7 @@ var MemoryClient = class {
229
246
  if (!this.telemetryId) {
230
247
  this.telemetryId = generateHash(this.apiKey);
231
248
  }
232
- this._validateOrgProject();
233
249
  captureClientEvent("init", this, {
234
- api_version: "v1",
235
250
  client_type: "MemoryClient"
236
251
  }).catch((error) => {
237
252
  console.error("Failed to capture event:", error);
@@ -267,12 +282,12 @@ var MemoryClient = class {
267
282
  throw createExceptionFromResponse(response.status, errorData);
268
283
  }
269
284
  const jsonResponse = await response.json();
270
- return jsonResponse;
285
+ return snakeToCamelKeys(jsonResponse);
271
286
  }
272
287
  _preparePayload(messages, options) {
273
288
  const payload = {};
274
289
  payload.messages = messages;
275
- return { ...payload, ...options };
290
+ return camelToSnakeKeys({ ...payload, ...options });
276
291
  }
277
292
  _prepareParams(options) {
278
293
  return Object.fromEntries(
@@ -296,10 +311,10 @@ var MemoryClient = class {
296
311
  if (response.status !== "ok") {
297
312
  throw new APIError(response.message || "API Key is invalid");
298
313
  }
299
- const { org_id, project_id, user_email } = response;
300
- if (org_id && !this.organizationId) this.organizationId = org_id;
301
- if (project_id && !this.projectId) this.projectId = project_id;
302
- if (user_email) this.telemetryId = user_email;
314
+ const { orgId, projectId, userEmail } = response;
315
+ if (orgId) this.organizationId = orgId;
316
+ if (projectId) this.projectId = projectId;
317
+ if (userEmail) this.telemetryId = userEmail;
303
318
  } catch (error) {
304
319
  if (error instanceof MemoryError || error instanceof APIError) {
305
320
  throw error;
@@ -312,20 +327,6 @@ var MemoryClient = class {
312
327
  }
313
328
  async add(messages, options = {}) {
314
329
  if (this.telemetryId === "") await this.ping();
315
- this._validateOrgProject();
316
- if (this.organizationName != null && this.projectName != null) {
317
- options.org_name = this.organizationName;
318
- options.project_name = this.projectName;
319
- }
320
- if (this.organizationId != null && this.projectId != null) {
321
- options.org_id = this.organizationId;
322
- options.project_id = this.projectId;
323
- if (options.org_name) delete options.org_name;
324
- if (options.project_name) delete options.project_name;
325
- }
326
- if (options.api_version) {
327
- options.version = options.api_version.toString() || "v2";
328
- }
329
330
  const payload = this._preparePayload(messages, options);
330
331
  const payloadKeys = Object.keys(payload);
331
332
  this._captureEvent("add", [payloadKeys]);
@@ -350,7 +351,6 @@ var MemoryClient = class {
350
351
  );
351
352
  }
352
353
  if (this.telemetryId === "") await this.ping();
353
- this._validateOrgProject();
354
354
  const payload = {};
355
355
  if (text !== void 0) payload.text = text;
356
356
  if (metadata !== void 0) payload.metadata = metadata;
@@ -378,69 +378,45 @@ var MemoryClient = class {
378
378
  );
379
379
  }
380
380
  async getAll(options) {
381
+ var _a2;
381
382
  if (this.telemetryId === "") await this.ping();
382
- this._validateOrgProject();
383
383
  const payloadKeys = Object.keys(options || {});
384
384
  this._captureEvent("get_all", [payloadKeys]);
385
- const { api_version, page, page_size, ...otherOptions } = options != null ? options : {};
386
- if (this.organizationName != null && this.projectName != null) {
387
- otherOptions.org_name = this.organizationName;
388
- otherOptions.project_name = this.projectName;
389
- }
390
- let appendedParams = "";
391
- let paginated_response = false;
392
- if (page && page_size) {
393
- appendedParams += `page=${page}&page_size=${page_size}`;
394
- paginated_response = true;
395
- }
396
- if (this.organizationId != null && this.projectId != null) {
397
- otherOptions.org_id = this.organizationId;
398
- otherOptions.project_id = this.projectId;
399
- if (otherOptions.org_name) delete otherOptions.org_name;
400
- if (otherOptions.project_name) delete otherOptions.project_name;
401
- }
402
- if (api_version === "v2") {
403
- let url = paginated_response ? `${this.host}/v2/memories/?${appendedParams}` : `${this.host}/v2/memories/`;
404
- return this._fetchWithErrorHandling(url, {
405
- method: "POST",
406
- headers: this.headers,
407
- body: JSON.stringify(otherOptions)
408
- });
409
- } else {
410
- const params = new URLSearchParams(this._prepareParams(otherOptions));
411
- const url = paginated_response ? `${this.host}/v1/memories/?${params}&${appendedParams}` : `${this.host}/v1/memories/?${params}`;
412
- return this._fetchWithErrorHandling(url, {
413
- headers: this.headers
414
- });
415
- }
385
+ const { page, pageSize, ...rest } = options != null ? options : {};
386
+ const body = {
387
+ output_format: "v1.1",
388
+ ...camelToSnakeKeys(rest)
389
+ };
390
+ let url = `${this.host}/v2/memories/`;
391
+ if (page && pageSize) {
392
+ url += `?page=${page}&page_size=${pageSize}`;
393
+ }
394
+ const response = await this._fetchWithErrorHandling(url, {
395
+ method: "POST",
396
+ headers: this.headers,
397
+ body: JSON.stringify(body)
398
+ });
399
+ return Array.isArray(response) ? response : (_a2 = response == null ? void 0 : response.results) != null ? _a2 : response;
416
400
  }
417
401
  async search(query, options) {
402
+ var _a2;
418
403
  if (this.telemetryId === "") await this.ping();
419
- this._validateOrgProject();
420
404
  const payloadKeys = Object.keys(options || {});
421
405
  this._captureEvent("search", [payloadKeys]);
422
- const { api_version, ...otherOptions } = options != null ? options : {};
423
- const payload = { query, ...otherOptions };
424
- if (this.organizationName != null && this.projectName != null) {
425
- payload.org_name = this.organizationName;
426
- payload.project_name = this.projectName;
427
- }
428
- if (this.organizationId != null && this.projectId != null) {
429
- payload.org_id = this.organizationId;
430
- payload.project_id = this.projectId;
431
- if (payload.org_name) delete payload.org_name;
432
- if (payload.project_name) delete payload.project_name;
433
- }
434
- const endpoint = api_version === "v2" ? "/v2/memories/search/" : "/v1/memories/search/";
406
+ const payload = {
407
+ query,
408
+ output_format: "v1.1",
409
+ ...camelToSnakeKeys(options != null ? options : {})
410
+ };
435
411
  const response = await this._fetchWithErrorHandling(
436
- `${this.host}${endpoint}`,
412
+ `${this.host}/v2/memories/search/`,
437
413
  {
438
414
  method: "POST",
439
415
  headers: this.headers,
440
416
  body: JSON.stringify(payload)
441
417
  }
442
418
  );
443
- return response;
419
+ return Array.isArray(response) ? response : (_a2 = response == null ? void 0 : response.results) != null ? _a2 : response;
444
420
  }
445
421
  async delete(memoryId) {
446
422
  if (this.telemetryId === "") await this.ping();
@@ -455,20 +431,10 @@ var MemoryClient = class {
455
431
  }
456
432
  async deleteAll(options = {}) {
457
433
  if (this.telemetryId === "") await this.ping();
458
- this._validateOrgProject();
459
434
  const payloadKeys = Object.keys(options || {});
460
435
  this._captureEvent("delete_all", [payloadKeys]);
461
- if (this.organizationName != null && this.projectName != null) {
462
- options.org_name = this.organizationName;
463
- options.project_name = this.projectName;
464
- }
465
- if (this.organizationId != null && this.projectId != null) {
466
- options.org_id = this.organizationId;
467
- options.project_id = this.projectId;
468
- if (options.org_name) delete options.org_name;
469
- if (options.project_name) delete options.project_name;
470
- }
471
- const params = new URLSearchParams(this._prepareParams(options));
436
+ const snakeOptions = camelToSnakeKeys(this._prepareParams(options));
437
+ const params = new URLSearchParams(snakeOptions);
472
438
  const response = await this._fetchWithErrorHandling(
473
439
  `${this.host}/v1/memories/?${params}`,
474
440
  {
@@ -489,28 +455,17 @@ var MemoryClient = class {
489
455
  );
490
456
  return response;
491
457
  }
492
- async users() {
458
+ async users(options) {
493
459
  if (this.telemetryId === "") await this.ping();
494
- this._validateOrgProject();
495
460
  this._captureEvent("users", []);
496
- const options = {};
497
- if (this.organizationName != null && this.projectName != null) {
498
- options.org_name = this.organizationName;
499
- options.project_name = this.projectName;
500
- }
501
- if (this.organizationId != null && this.projectId != null) {
502
- options.org_id = this.organizationId;
503
- options.project_id = this.projectId;
504
- if (options.org_name) delete options.org_name;
505
- if (options.project_name) delete options.project_name;
506
- }
507
- const params = new URLSearchParams(options);
508
- const response = await this._fetchWithErrorHandling(
509
- `${this.host}/v1/entities/?${params}`,
510
- {
511
- headers: this.headers
512
- }
513
- );
461
+ let url = `${this.host}/v1/entities/`;
462
+ const params = [];
463
+ if (options == null ? void 0 : options.page) params.push(`page=${options.page}`);
464
+ if (options == null ? void 0 : options.pageSize) params.push(`page_size=${options.pageSize}`);
465
+ if (params.length) url += `?${params.join("&")}`;
466
+ const response = await this._fetchWithErrorHandling(url, {
467
+ headers: this.headers
468
+ });
514
469
  return response;
515
470
  }
516
471
  /**
@@ -533,17 +488,16 @@ var MemoryClient = class {
533
488
  }
534
489
  async deleteUsers(params = {}) {
535
490
  if (this.telemetryId === "") await this.ping();
536
- this._validateOrgProject();
537
491
  let to_delete = [];
538
- const { user_id, agent_id, app_id, run_id } = params;
539
- if (user_id) {
540
- to_delete = [{ type: "user", name: user_id }];
541
- } else if (agent_id) {
542
- to_delete = [{ type: "agent", name: agent_id }];
543
- } else if (app_id) {
544
- to_delete = [{ type: "app", name: app_id }];
545
- } else if (run_id) {
546
- to_delete = [{ type: "run", name: run_id }];
492
+ const { userId, agentId, appId, runId } = params;
493
+ if (userId) {
494
+ to_delete = [{ type: "user", name: userId }];
495
+ } else if (agentId) {
496
+ to_delete = [{ type: "agent", name: agentId }];
497
+ } else if (appId) {
498
+ to_delete = [{ type: "app", name: appId }];
499
+ } else if (runId) {
500
+ to_delete = [{ type: "run", name: runId }];
547
501
  } else {
548
502
  const entities = await this.users();
549
503
  to_delete = entities.results.map((entity) => ({
@@ -554,25 +508,9 @@ var MemoryClient = class {
554
508
  if (to_delete.length === 0) {
555
509
  throw new Error("No entities to delete");
556
510
  }
557
- const requestOptions = {};
558
- if (this.organizationName != null && this.projectName != null) {
559
- requestOptions.org_name = this.organizationName;
560
- requestOptions.project_name = this.projectName;
561
- }
562
- if (this.organizationId != null && this.projectId != null) {
563
- requestOptions.org_id = this.organizationId;
564
- requestOptions.project_id = this.projectId;
565
- if (requestOptions.org_name) delete requestOptions.org_name;
566
- if (requestOptions.project_name) delete requestOptions.project_name;
567
- }
568
511
  for (const entity of to_delete) {
569
512
  try {
570
- await this.client.delete(
571
- `/v2/entities/${entity.type}/${entity.name}/`,
572
- {
573
- params: requestOptions
574
- }
575
- );
513
+ await this.client.delete(`/v2/entities/${entity.type}/${entity.name}/`);
576
514
  } catch (error) {
577
515
  throw new APIError(
578
516
  `Failed to delete ${entity.type} ${entity.name}: ${error.message}`
@@ -580,16 +518,10 @@ var MemoryClient = class {
580
518
  }
581
519
  }
582
520
  this._captureEvent("delete_users", [
583
- {
584
- user_id,
585
- agent_id,
586
- app_id,
587
- run_id,
588
- sync_type: "sync"
589
- }
521
+ { userId, agentId, appId, runId, sync_type: "sync" }
590
522
  ]);
591
523
  return {
592
- message: user_id || agent_id || app_id || run_id ? "Entity deleted successfully." : "All users, agents, apps and runs deleted."
524
+ message: userId || agentId || appId || runId ? "Entity deleted successfully." : "All users, agents, apps and runs deleted."
593
525
  };
594
526
  }
595
527
  async batchUpdate(memories) {
@@ -627,7 +559,6 @@ var MemoryClient = class {
627
559
  }
628
560
  async getProject(options) {
629
561
  if (this.telemetryId === "") await this.ping();
630
- this._validateOrgProject();
631
562
  const payloadKeys = Object.keys(options || {});
632
563
  this._captureEvent("get_project", [payloadKeys]);
633
564
  const { fields } = options;
@@ -637,7 +568,7 @@ var MemoryClient = class {
637
568
  );
638
569
  }
639
570
  const params = new URLSearchParams();
640
- fields == null ? void 0 : fields.forEach((field) => params.append("fields", field));
571
+ fields == null ? void 0 : fields.forEach((field) => params.append("fields", camelToSnake(field)));
641
572
  const response = await this._fetchWithErrorHandling(
642
573
  `${this.host}/api/v1/orgs/organizations/${this.organizationId}/projects/${this.projectId}/?${params.toString()}`,
643
574
  {
@@ -648,7 +579,6 @@ var MemoryClient = class {
648
579
  }
649
580
  async updateProject(prompts) {
650
581
  if (this.telemetryId === "") await this.ping();
651
- this._validateOrgProject();
652
582
  this._captureEvent("update_project", []);
653
583
  if (!(this.organizationId && this.projectId)) {
654
584
  throw new Error(
@@ -660,7 +590,7 @@ var MemoryClient = class {
660
590
  {
661
591
  method: "PATCH",
662
592
  headers: this.headers,
663
- body: JSON.stringify(prompts)
593
+ body: JSON.stringify(camelToSnakeKeys(prompts))
664
594
  }
665
595
  );
666
596
  return response;
@@ -735,45 +665,39 @@ var MemoryClient = class {
735
665
  {
736
666
  method: "POST",
737
667
  headers: this.headers,
738
- body: JSON.stringify(data)
668
+ body: JSON.stringify(camelToSnakeKeys(data))
739
669
  }
740
670
  );
741
671
  return response;
742
672
  }
743
673
  async createMemoryExport(data) {
744
- var _a2, _b;
745
674
  if (this.telemetryId === "") await this.ping();
746
675
  this._captureEvent("create_memory_export", []);
747
676
  if (!data.filters || !data.schema) {
748
677
  throw new Error("Missing filters or schema");
749
678
  }
750
- data.org_id = ((_a2 = this.organizationId) == null ? void 0 : _a2.toString()) || null;
751
- data.project_id = ((_b = this.projectId) == null ? void 0 : _b.toString()) || null;
752
679
  const response = await this._fetchWithErrorHandling(
753
680
  `${this.host}/v1/exports/`,
754
681
  {
755
682
  method: "POST",
756
683
  headers: this.headers,
757
- body: JSON.stringify(data)
684
+ body: JSON.stringify(camelToSnakeKeys(data))
758
685
  }
759
686
  );
760
687
  return response;
761
688
  }
762
689
  async getMemoryExport(data) {
763
- var _a2, _b;
764
690
  if (this.telemetryId === "") await this.ping();
765
691
  this._captureEvent("get_memory_export", []);
766
- if (!data.memory_export_id && !data.filters) {
767
- throw new Error("Missing memory_export_id or filters");
692
+ if (!data.memoryExportId && !data.filters) {
693
+ throw new Error("Missing memoryExportId or filters");
768
694
  }
769
- data.org_id = ((_a2 = this.organizationId) == null ? void 0 : _a2.toString()) || "";
770
- data.project_id = ((_b = this.projectId) == null ? void 0 : _b.toString()) || "";
771
695
  const response = await this._fetchWithErrorHandling(
772
696
  `${this.host}/v1/exports/get/`,
773
697
  {
774
698
  method: "POST",
775
699
  headers: this.headers,
776
- body: JSON.stringify(data)
700
+ body: JSON.stringify(camelToSnakeKeys(data))
777
701
  }
778
702
  );
779
703
  return response;