mem0ai 2.4.5 → 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.js CHANGED
@@ -123,6 +123,37 @@ async function captureClientEvent(eventName, instance, additionalData = {}) {
123
123
  );
124
124
  }
125
125
 
126
+ // src/client/utils.ts
127
+ function camelToSnake(str) {
128
+ if (str === str.toUpperCase()) return str;
129
+ return str.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`);
130
+ }
131
+ function snakeToCamel(str) {
132
+ return str.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase());
133
+ }
134
+ function camelToSnakeKeys(obj) {
135
+ if (obj === null || obj === void 0 || typeof obj !== "object") return obj;
136
+ if (Array.isArray(obj)) return obj.map(camelToSnakeKeys);
137
+ if (obj instanceof Date) return obj;
138
+ return Object.fromEntries(
139
+ Object.entries(obj).map(([key, value]) => [
140
+ camelToSnake(key),
141
+ camelToSnakeKeys(value)
142
+ ])
143
+ );
144
+ }
145
+ function snakeToCamelKeys(obj) {
146
+ if (obj === null || obj === void 0 || typeof obj !== "object") return obj;
147
+ if (Array.isArray(obj)) return obj.map(snakeToCamelKeys);
148
+ if (obj instanceof Date) return obj;
149
+ return Object.fromEntries(
150
+ Object.entries(obj).map(([key, value]) => [
151
+ snakeToCamel(key),
152
+ snakeToCamelKeys(value)
153
+ ])
154
+ );
155
+ }
156
+
126
157
  // src/common/exceptions.ts
127
158
  var MemoryError = class extends Error {
128
159
  constructor(message, errorCode, options = {}) {
@@ -239,25 +270,11 @@ var MemoryClient = class {
239
270
  throw new Error("Mem0 API key cannot be empty");
240
271
  }
241
272
  }
242
- _validateOrgProject() {
243
- if (this.organizationName === null && this.projectName !== null || this.organizationName !== null && this.projectName === null) {
244
- console.warn(
245
- "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."
246
- );
247
- }
248
- if (this.organizationId === null && this.projectId !== null || this.organizationId !== null && this.projectId === null) {
249
- console.warn(
250
- "Warning: Both organizationId and projectId must be provided together when using either. This will be removed from version 1.0.40."
251
- );
252
- }
253
- }
254
273
  constructor(options) {
255
274
  this.apiKey = options.apiKey;
256
275
  this.host = options.host || "https://api.mem0.ai";
257
- this.organizationName = options.organizationName || null;
258
- this.projectName = options.projectName || null;
259
- this.organizationId = options.organizationId || null;
260
- this.projectId = options.projectId || null;
276
+ this.organizationId = null;
277
+ this.projectId = null;
261
278
  this.headers = {
262
279
  Authorization: `Token ${this.apiKey}`,
263
280
  "Content-Type": "application/json"
@@ -277,9 +294,7 @@ var MemoryClient = class {
277
294
  if (!this.telemetryId) {
278
295
  this.telemetryId = generateHash(this.apiKey);
279
296
  }
280
- this._validateOrgProject();
281
297
  captureClientEvent("init", this, {
282
- api_version: "v1",
283
298
  client_type: "MemoryClient"
284
299
  }).catch((error) => {
285
300
  console.error("Failed to capture event:", error);
@@ -315,12 +330,12 @@ var MemoryClient = class {
315
330
  throw createExceptionFromResponse(response.status, errorData);
316
331
  }
317
332
  const jsonResponse = await response.json();
318
- return jsonResponse;
333
+ return snakeToCamelKeys(jsonResponse);
319
334
  }
320
335
  _preparePayload(messages, options) {
321
336
  const payload = {};
322
337
  payload.messages = messages;
323
- return { ...payload, ...options };
338
+ return camelToSnakeKeys({ ...payload, ...options });
324
339
  }
325
340
  _prepareParams(options) {
326
341
  return Object.fromEntries(
@@ -344,10 +359,10 @@ var MemoryClient = class {
344
359
  if (response.status !== "ok") {
345
360
  throw new APIError(response.message || "API Key is invalid");
346
361
  }
347
- const { org_id, project_id, user_email } = response;
348
- if (org_id && !this.organizationId) this.organizationId = org_id;
349
- if (project_id && !this.projectId) this.projectId = project_id;
350
- if (user_email) this.telemetryId = user_email;
362
+ const { orgId, projectId, userEmail } = response;
363
+ if (orgId) this.organizationId = orgId;
364
+ if (projectId) this.projectId = projectId;
365
+ if (userEmail) this.telemetryId = userEmail;
351
366
  } catch (error) {
352
367
  if (error instanceof MemoryError || error instanceof APIError) {
353
368
  throw error;
@@ -360,20 +375,6 @@ var MemoryClient = class {
360
375
  }
361
376
  async add(messages, options = {}) {
362
377
  if (this.telemetryId === "") await this.ping();
363
- this._validateOrgProject();
364
- if (this.organizationName != null && this.projectName != null) {
365
- options.org_name = this.organizationName;
366
- options.project_name = this.projectName;
367
- }
368
- if (this.organizationId != null && this.projectId != null) {
369
- options.org_id = this.organizationId;
370
- options.project_id = this.projectId;
371
- if (options.org_name) delete options.org_name;
372
- if (options.project_name) delete options.project_name;
373
- }
374
- if (options.api_version) {
375
- options.version = options.api_version.toString() || "v2";
376
- }
377
378
  const payload = this._preparePayload(messages, options);
378
379
  const payloadKeys = Object.keys(payload);
379
380
  this._captureEvent("add", [payloadKeys]);
@@ -398,7 +399,6 @@ var MemoryClient = class {
398
399
  );
399
400
  }
400
401
  if (this.telemetryId === "") await this.ping();
401
- this._validateOrgProject();
402
402
  const payload = {};
403
403
  if (text !== void 0) payload.text = text;
404
404
  if (metadata !== void 0) payload.metadata = metadata;
@@ -426,69 +426,45 @@ var MemoryClient = class {
426
426
  );
427
427
  }
428
428
  async getAll(options) {
429
+ var _a2;
429
430
  if (this.telemetryId === "") await this.ping();
430
- this._validateOrgProject();
431
431
  const payloadKeys = Object.keys(options || {});
432
432
  this._captureEvent("get_all", [payloadKeys]);
433
- const { api_version, page, page_size, ...otherOptions } = options != null ? options : {};
434
- if (this.organizationName != null && this.projectName != null) {
435
- otherOptions.org_name = this.organizationName;
436
- otherOptions.project_name = this.projectName;
437
- }
438
- let appendedParams = "";
439
- let paginated_response = false;
440
- if (page && page_size) {
441
- appendedParams += `page=${page}&page_size=${page_size}`;
442
- paginated_response = true;
443
- }
444
- if (this.organizationId != null && this.projectId != null) {
445
- otherOptions.org_id = this.organizationId;
446
- otherOptions.project_id = this.projectId;
447
- if (otherOptions.org_name) delete otherOptions.org_name;
448
- if (otherOptions.project_name) delete otherOptions.project_name;
449
- }
450
- if (api_version === "v2") {
451
- let url = paginated_response ? `${this.host}/v2/memories/?${appendedParams}` : `${this.host}/v2/memories/`;
452
- return this._fetchWithErrorHandling(url, {
453
- method: "POST",
454
- headers: this.headers,
455
- body: JSON.stringify(otherOptions)
456
- });
457
- } else {
458
- const params = new URLSearchParams(this._prepareParams(otherOptions));
459
- const url = paginated_response ? `${this.host}/v1/memories/?${params}&${appendedParams}` : `${this.host}/v1/memories/?${params}`;
460
- return this._fetchWithErrorHandling(url, {
461
- headers: this.headers
462
- });
463
- }
433
+ const { page, pageSize, ...rest } = options != null ? options : {};
434
+ const body = {
435
+ output_format: "v1.1",
436
+ ...camelToSnakeKeys(rest)
437
+ };
438
+ let url = `${this.host}/v2/memories/`;
439
+ if (page && pageSize) {
440
+ url += `?page=${page}&page_size=${pageSize}`;
441
+ }
442
+ const response = await this._fetchWithErrorHandling(url, {
443
+ method: "POST",
444
+ headers: this.headers,
445
+ body: JSON.stringify(body)
446
+ });
447
+ return Array.isArray(response) ? response : (_a2 = response == null ? void 0 : response.results) != null ? _a2 : response;
464
448
  }
465
449
  async search(query, options) {
450
+ var _a2;
466
451
  if (this.telemetryId === "") await this.ping();
467
- this._validateOrgProject();
468
452
  const payloadKeys = Object.keys(options || {});
469
453
  this._captureEvent("search", [payloadKeys]);
470
- const { api_version, ...otherOptions } = options != null ? options : {};
471
- const payload = { query, ...otherOptions };
472
- if (this.organizationName != null && this.projectName != null) {
473
- payload.org_name = this.organizationName;
474
- payload.project_name = this.projectName;
475
- }
476
- if (this.organizationId != null && this.projectId != null) {
477
- payload.org_id = this.organizationId;
478
- payload.project_id = this.projectId;
479
- if (payload.org_name) delete payload.org_name;
480
- if (payload.project_name) delete payload.project_name;
481
- }
482
- const endpoint = api_version === "v2" ? "/v2/memories/search/" : "/v1/memories/search/";
454
+ const payload = {
455
+ query,
456
+ output_format: "v1.1",
457
+ ...camelToSnakeKeys(options != null ? options : {})
458
+ };
483
459
  const response = await this._fetchWithErrorHandling(
484
- `${this.host}${endpoint}`,
460
+ `${this.host}/v2/memories/search/`,
485
461
  {
486
462
  method: "POST",
487
463
  headers: this.headers,
488
464
  body: JSON.stringify(payload)
489
465
  }
490
466
  );
491
- return response;
467
+ return Array.isArray(response) ? response : (_a2 = response == null ? void 0 : response.results) != null ? _a2 : response;
492
468
  }
493
469
  async delete(memoryId) {
494
470
  if (this.telemetryId === "") await this.ping();
@@ -503,20 +479,10 @@ var MemoryClient = class {
503
479
  }
504
480
  async deleteAll(options = {}) {
505
481
  if (this.telemetryId === "") await this.ping();
506
- this._validateOrgProject();
507
482
  const payloadKeys = Object.keys(options || {});
508
483
  this._captureEvent("delete_all", [payloadKeys]);
509
- if (this.organizationName != null && this.projectName != null) {
510
- options.org_name = this.organizationName;
511
- options.project_name = this.projectName;
512
- }
513
- if (this.organizationId != null && this.projectId != null) {
514
- options.org_id = this.organizationId;
515
- options.project_id = this.projectId;
516
- if (options.org_name) delete options.org_name;
517
- if (options.project_name) delete options.project_name;
518
- }
519
- const params = new URLSearchParams(this._prepareParams(options));
484
+ const snakeOptions = camelToSnakeKeys(this._prepareParams(options));
485
+ const params = new URLSearchParams(snakeOptions);
520
486
  const response = await this._fetchWithErrorHandling(
521
487
  `${this.host}/v1/memories/?${params}`,
522
488
  {
@@ -537,28 +503,17 @@ var MemoryClient = class {
537
503
  );
538
504
  return response;
539
505
  }
540
- async users() {
506
+ async users(options) {
541
507
  if (this.telemetryId === "") await this.ping();
542
- this._validateOrgProject();
543
508
  this._captureEvent("users", []);
544
- const options = {};
545
- if (this.organizationName != null && this.projectName != null) {
546
- options.org_name = this.organizationName;
547
- options.project_name = this.projectName;
548
- }
549
- if (this.organizationId != null && this.projectId != null) {
550
- options.org_id = this.organizationId;
551
- options.project_id = this.projectId;
552
- if (options.org_name) delete options.org_name;
553
- if (options.project_name) delete options.project_name;
554
- }
555
- const params = new URLSearchParams(options);
556
- const response = await this._fetchWithErrorHandling(
557
- `${this.host}/v1/entities/?${params}`,
558
- {
559
- headers: this.headers
560
- }
561
- );
509
+ let url = `${this.host}/v1/entities/`;
510
+ const params = [];
511
+ if (options == null ? void 0 : options.page) params.push(`page=${options.page}`);
512
+ if (options == null ? void 0 : options.pageSize) params.push(`page_size=${options.pageSize}`);
513
+ if (params.length) url += `?${params.join("&")}`;
514
+ const response = await this._fetchWithErrorHandling(url, {
515
+ headers: this.headers
516
+ });
562
517
  return response;
563
518
  }
564
519
  /**
@@ -581,17 +536,16 @@ var MemoryClient = class {
581
536
  }
582
537
  async deleteUsers(params = {}) {
583
538
  if (this.telemetryId === "") await this.ping();
584
- this._validateOrgProject();
585
539
  let to_delete = [];
586
- const { user_id, agent_id, app_id, run_id } = params;
587
- if (user_id) {
588
- to_delete = [{ type: "user", name: user_id }];
589
- } else if (agent_id) {
590
- to_delete = [{ type: "agent", name: agent_id }];
591
- } else if (app_id) {
592
- to_delete = [{ type: "app", name: app_id }];
593
- } else if (run_id) {
594
- to_delete = [{ type: "run", name: run_id }];
540
+ const { userId, agentId, appId, runId } = params;
541
+ if (userId) {
542
+ to_delete = [{ type: "user", name: userId }];
543
+ } else if (agentId) {
544
+ to_delete = [{ type: "agent", name: agentId }];
545
+ } else if (appId) {
546
+ to_delete = [{ type: "app", name: appId }];
547
+ } else if (runId) {
548
+ to_delete = [{ type: "run", name: runId }];
595
549
  } else {
596
550
  const entities = await this.users();
597
551
  to_delete = entities.results.map((entity) => ({
@@ -602,25 +556,9 @@ var MemoryClient = class {
602
556
  if (to_delete.length === 0) {
603
557
  throw new Error("No entities to delete");
604
558
  }
605
- const requestOptions = {};
606
- if (this.organizationName != null && this.projectName != null) {
607
- requestOptions.org_name = this.organizationName;
608
- requestOptions.project_name = this.projectName;
609
- }
610
- if (this.organizationId != null && this.projectId != null) {
611
- requestOptions.org_id = this.organizationId;
612
- requestOptions.project_id = this.projectId;
613
- if (requestOptions.org_name) delete requestOptions.org_name;
614
- if (requestOptions.project_name) delete requestOptions.project_name;
615
- }
616
559
  for (const entity of to_delete) {
617
560
  try {
618
- await this.client.delete(
619
- `/v2/entities/${entity.type}/${entity.name}/`,
620
- {
621
- params: requestOptions
622
- }
623
- );
561
+ await this.client.delete(`/v2/entities/${entity.type}/${entity.name}/`);
624
562
  } catch (error) {
625
563
  throw new APIError(
626
564
  `Failed to delete ${entity.type} ${entity.name}: ${error.message}`
@@ -628,16 +566,10 @@ var MemoryClient = class {
628
566
  }
629
567
  }
630
568
  this._captureEvent("delete_users", [
631
- {
632
- user_id,
633
- agent_id,
634
- app_id,
635
- run_id,
636
- sync_type: "sync"
637
- }
569
+ { userId, agentId, appId, runId, sync_type: "sync" }
638
570
  ]);
639
571
  return {
640
- message: user_id || agent_id || app_id || run_id ? "Entity deleted successfully." : "All users, agents, apps and runs deleted."
572
+ message: userId || agentId || appId || runId ? "Entity deleted successfully." : "All users, agents, apps and runs deleted."
641
573
  };
642
574
  }
643
575
  async batchUpdate(memories) {
@@ -675,7 +607,6 @@ var MemoryClient = class {
675
607
  }
676
608
  async getProject(options) {
677
609
  if (this.telemetryId === "") await this.ping();
678
- this._validateOrgProject();
679
610
  const payloadKeys = Object.keys(options || {});
680
611
  this._captureEvent("get_project", [payloadKeys]);
681
612
  const { fields } = options;
@@ -685,7 +616,7 @@ var MemoryClient = class {
685
616
  );
686
617
  }
687
618
  const params = new URLSearchParams();
688
- fields == null ? void 0 : fields.forEach((field) => params.append("fields", field));
619
+ fields == null ? void 0 : fields.forEach((field) => params.append("fields", camelToSnake(field)));
689
620
  const response = await this._fetchWithErrorHandling(
690
621
  `${this.host}/api/v1/orgs/organizations/${this.organizationId}/projects/${this.projectId}/?${params.toString()}`,
691
622
  {
@@ -696,7 +627,6 @@ var MemoryClient = class {
696
627
  }
697
628
  async updateProject(prompts) {
698
629
  if (this.telemetryId === "") await this.ping();
699
- this._validateOrgProject();
700
630
  this._captureEvent("update_project", []);
701
631
  if (!(this.organizationId && this.projectId)) {
702
632
  throw new Error(
@@ -708,7 +638,7 @@ var MemoryClient = class {
708
638
  {
709
639
  method: "PATCH",
710
640
  headers: this.headers,
711
- body: JSON.stringify(prompts)
641
+ body: JSON.stringify(camelToSnakeKeys(prompts))
712
642
  }
713
643
  );
714
644
  return response;
@@ -783,45 +713,39 @@ var MemoryClient = class {
783
713
  {
784
714
  method: "POST",
785
715
  headers: this.headers,
786
- body: JSON.stringify(data)
716
+ body: JSON.stringify(camelToSnakeKeys(data))
787
717
  }
788
718
  );
789
719
  return response;
790
720
  }
791
721
  async createMemoryExport(data) {
792
- var _a2, _b;
793
722
  if (this.telemetryId === "") await this.ping();
794
723
  this._captureEvent("create_memory_export", []);
795
724
  if (!data.filters || !data.schema) {
796
725
  throw new Error("Missing filters or schema");
797
726
  }
798
- data.org_id = ((_a2 = this.organizationId) == null ? void 0 : _a2.toString()) || null;
799
- data.project_id = ((_b = this.projectId) == null ? void 0 : _b.toString()) || null;
800
727
  const response = await this._fetchWithErrorHandling(
801
728
  `${this.host}/v1/exports/`,
802
729
  {
803
730
  method: "POST",
804
731
  headers: this.headers,
805
- body: JSON.stringify(data)
732
+ body: JSON.stringify(camelToSnakeKeys(data))
806
733
  }
807
734
  );
808
735
  return response;
809
736
  }
810
737
  async getMemoryExport(data) {
811
- var _a2, _b;
812
738
  if (this.telemetryId === "") await this.ping();
813
739
  this._captureEvent("get_memory_export", []);
814
- if (!data.memory_export_id && !data.filters) {
815
- throw new Error("Missing memory_export_id or filters");
740
+ if (!data.memoryExportId && !data.filters) {
741
+ throw new Error("Missing memoryExportId or filters");
816
742
  }
817
- data.org_id = ((_a2 = this.organizationId) == null ? void 0 : _a2.toString()) || "";
818
- data.project_id = ((_b = this.projectId) == null ? void 0 : _b.toString()) || "";
819
743
  const response = await this._fetchWithErrorHandling(
820
744
  `${this.host}/v1/exports/get/`,
821
745
  {
822
746
  method: "POST",
823
747
  headers: this.headers,
824
- body: JSON.stringify(data)
748
+ body: JSON.stringify(camelToSnakeKeys(data))
825
749
  }
826
750
  );
827
751
  return response;