mem0ai 1.0.6 → 1.0.9

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/README.md CHANGED
@@ -200,6 +200,14 @@ client.delete("memory-id-here")
200
200
  .catch(error => console.error(error));
201
201
  ```
202
202
 
203
+ Delete all users for which you have memories:
204
+
205
+ ```javascript
206
+ client.deleteUsers()
207
+ .then(result => console.log(result))
208
+ .catch(error => console.error(error));
209
+ ```
210
+
203
211
  Delete all memories of a user:
204
212
 
205
213
  ```javascript
package/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "mem0ai",
3
- "version": "1.0.6",
3
+ "version": "1.0.9",
4
4
  "description": "The Memory layer for your AI apps",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
7
7
  "test": "echo \"Error: no test specified\" && exit 1"
8
8
  },
9
+ "types": "src/index.d.ts",
9
10
  "keywords": [
10
11
  "mem0",
11
12
  "api",
package/src/index.d.ts ADDED
@@ -0,0 +1,30 @@
1
+ declare module 'mem0ai' {
2
+ export interface MemoryOptions {
3
+ user_id?: string;
4
+ }
5
+
6
+ export interface SearchOptions extends MemoryOptions {
7
+ api_version?: 'v1' | 'v2';
8
+ }
9
+
10
+ export interface Memory {
11
+ id: string;
12
+ messages: Array<{ role: string; content: string }>;
13
+ }
14
+
15
+ export class MemoryClient {
16
+ constructor(apiKey: string, host?: string);
17
+
18
+ add(messages: string | Array<{ role: string; content: string }>, options?: MemoryOptions): Promise<Memory>;
19
+ get(memoryId: string): Promise<Memory>;
20
+ getAll(options?: MemoryOptions): Promise<{ results: Memory[] }>;
21
+ search(query: string, options?: SearchOptions): Promise<{ results: Memory[] }>;
22
+ delete(memoryId: string): Promise<any>;
23
+ deleteAll(options?: MemoryOptions): Promise<any>;
24
+ history(memoryId: string): Promise<any>;
25
+ users(): Promise<any>;
26
+ deleteUsers(): Promise<{ message: string }>;
27
+ }
28
+
29
+ export default MemoryClient;
30
+ }
package/src/index.js CHANGED
@@ -1,5 +1,18 @@
1
1
  const axios = require('axios');
2
2
 
3
+ /**
4
+ * @typedef {Object} MemoryOptions
5
+ * @property {string} [user_id]
6
+ *
7
+ * @typedef {Object} SearchOptions
8
+ * @property {string} [user_id]
9
+ * @property {('v1'|'v2')} [api_version]
10
+ *
11
+ * @typedef {Object} Memory
12
+ * @property {string} id
13
+ * @property {Array<{role: string, content: string}>} messages
14
+ */
15
+
3
16
  class APIError extends Error {
4
17
  constructor(message) {
5
18
  super(message);
@@ -7,6 +20,11 @@ class APIError extends Error {
7
20
  }
8
21
  }
9
22
 
23
+ /**
24
+ * @template T
25
+ * @param {(...args: any[]) => Promise<T>} fn
26
+ * @returns {(...args: any[]) => Promise<T>}
27
+ */
10
28
  function apiErrorHandler(fn) {
11
29
  return async function (...args) {
12
30
  try {
@@ -23,7 +41,14 @@ function apiErrorHandler(fn) {
23
41
  };
24
42
  }
25
43
 
44
+ /**
45
+ * MemoryClient for interacting with the mem0ai API
46
+ */
26
47
  class MemoryClient {
48
+ /**
49
+ * @param {string} apiKey
50
+ * @param {string} [host]
51
+ */
27
52
  constructor(apiKey, host = 'https://api.mem0.ai') {
28
53
  this.apiKey = apiKey || process.env.MEM0_API_KEY;
29
54
  this.host = host;
@@ -48,6 +73,7 @@ class MemoryClient {
48
73
  this.delete = apiErrorHandler(this.delete.bind(this));
49
74
  this.deleteAll = apiErrorHandler(this.deleteAll.bind(this));
50
75
  this.history = apiErrorHandler(this.history.bind(this));
76
+ this.deleteUsers = apiErrorHandler(this.deleteUsers.bind(this));
51
77
  }
52
78
 
53
79
  async _validateApiKey() {
@@ -58,23 +84,49 @@ class MemoryClient {
58
84
  }
59
85
  }
60
86
 
87
+ /**
88
+ * @param {string|Array<{role: string, content: string}>} messages
89
+ * @param {MemoryOptions} [options]
90
+ * @returns {Promise<Memory>}
91
+ */
61
92
  async add(messages, options = {}) {
62
93
  const payload = this._preparePayload(messages, options);
63
94
  const response = await this.client.post('/v1/memories/', payload);
64
95
  return response.data;
65
96
  }
66
97
 
98
+ /**
99
+ * @param {string} memoryId
100
+ * @returns {Promise<Memory>}
101
+ */
67
102
  async get(memoryId) {
68
103
  const response = await this.client.get(`/v1/memories/${memoryId}/`);
69
104
  return response.data;
70
105
  }
71
106
 
107
+ /**
108
+ * @param {MemoryOptions} [options]
109
+ * @returns {Promise<{results: Memory[]}>}
110
+ */
72
111
  async getAll(options = {}) {
73
- const params = this._prepareParams(options);
74
- const response = await this.client.get('/v1/memories/', { params });
75
- return response.data;
112
+ const { api_version, ...otherOptions } = options;
113
+ const endpoint = api_version === 'v2' ? '/v2/memories/' : '/v1/memories/';
114
+
115
+ if (api_version === 'v2') {
116
+ const response = await this.client.post(endpoint, otherOptions);
117
+ return response.data;
118
+ } else {
119
+ const params = this._prepareParams(otherOptions);
120
+ const response = await this.client.get(endpoint, { params });
121
+ return response.data;
122
+ }
76
123
  }
77
124
 
125
+ /**
126
+ * @param {string} query
127
+ * @param {SearchOptions} [options]
128
+ * @returns {Promise<{results: Memory[]}>}
129
+ */
78
130
  async search(query, options = {}) {
79
131
  const { api_version, ...otherOptions } = options;
80
132
  const payload = { query, ...otherOptions };
@@ -83,27 +135,58 @@ class MemoryClient {
83
135
  return response.data;
84
136
  }
85
137
 
138
+ /**
139
+ * @param {string} memoryId
140
+ * @returns {Promise<any>}
141
+ */
86
142
  async delete(memoryId) {
87
143
  const response = await this.client.delete(`/v1/memories/${memoryId}/`);
88
144
  return response.data;
89
145
  }
90
146
 
147
+ /**
148
+ * @param {MemoryOptions} [options]
149
+ * @returns {Promise<any>}
150
+ */
91
151
  async deleteAll(options = {}) {
92
152
  const params = this._prepareParams(options);
93
153
  const response = await this.client.delete('/v1/memories/', { params });
94
154
  return response.data;
95
155
  }
96
156
 
157
+ /**
158
+ * @param {string} memoryId
159
+ * @returns {Promise<any>}
160
+ */
97
161
  async history(memoryId) {
98
162
  const response = await this.client.get(`/v1/memories/${memoryId}/history/`);
99
163
  return response.data;
100
164
  }
101
165
 
166
+ /**
167
+ * @returns {Promise<any>}
168
+ */
102
169
  async users() {
103
170
  const response = await this.client.get('/v1/entities/');
104
171
  return response.data;
105
172
  }
106
173
 
174
+ /**
175
+ * @returns {Promise<{message: string}>}
176
+ */
177
+ async deleteUsers() {
178
+ const entities = await this.users();
179
+ for (const entity of entities.results) {
180
+ await this.client.delete(`/entities/${entity.type}/${entity.id}/`);
181
+ }
182
+ return { message: "All users, agents, and sessions deleted." };
183
+ }
184
+
185
+ /**
186
+ * @param {string|Array<{role: string, content: string}>} messages
187
+ * @param {MemoryOptions} options
188
+ * @returns {Object}
189
+ */
107
190
  _preparePayload(messages, options) {
108
191
  const payload = {};
109
192
  if (typeof messages === 'string') {
@@ -114,6 +197,10 @@ class MemoryClient {
114
197
  return { ...payload, ...options };
115
198
  }
116
199
 
200
+ /**
201
+ * @param {MemoryOptions} options
202
+ * @returns {Object}
203
+ */
117
204
  _prepareParams(options) {
118
205
  return Object.fromEntries(Object.entries(options).filter(([_, v]) => v != null));
119
206
  }