mem0ai 1.0.15 → 1.0.16
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/package.json +6 -1
- package/src/index.cjs +181 -0
- package/src/index.d.ts +1 -1
- package/src/index.js +0 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mem0ai",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.16",
|
|
4
4
|
"description": "The Memory Layer For Your AI Apps",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -23,5 +23,10 @@
|
|
|
23
23
|
"package.json",
|
|
24
24
|
"README.md"
|
|
25
25
|
],
|
|
26
|
+
"type": "module",
|
|
27
|
+
"exports": {
|
|
28
|
+
"require": "./src/index.cjs",
|
|
29
|
+
"import": "./dist/index.js"
|
|
30
|
+
},
|
|
26
31
|
"module": "dist/index.mjs"
|
|
27
32
|
}
|
package/src/index.cjs
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
class APIError extends Error {
|
|
4
|
+
constructor(message) {
|
|
5
|
+
super(message);
|
|
6
|
+
this.name = 'APIError';
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* MemoryClient for interacting with the mem0ai API
|
|
12
|
+
*/
|
|
13
|
+
class MemoryClient {
|
|
14
|
+
/**
|
|
15
|
+
* @param {string} apiKey
|
|
16
|
+
* @param {string} [host]
|
|
17
|
+
* @param {string} [organizationName]
|
|
18
|
+
* @param {string} [projectName]
|
|
19
|
+
*/
|
|
20
|
+
constructor(apiKey, host = 'https://api.mem0.ai', organizationName = null, projectName = "default-project") {
|
|
21
|
+
this.apiKey = apiKey;
|
|
22
|
+
this.host = host;
|
|
23
|
+
this.organizationName = organizationName;
|
|
24
|
+
this.projectName = projectName;
|
|
25
|
+
|
|
26
|
+
this.headers = {
|
|
27
|
+
'Authorization': `Token ${this.apiKey}`,
|
|
28
|
+
'Content-Type': 'application/json'
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
this._validateApiKey();
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
_validateApiKey() {
|
|
35
|
+
if (!this.apiKey) {
|
|
36
|
+
throw new Error('Mem0 API key is required');
|
|
37
|
+
}
|
|
38
|
+
if (typeof this.apiKey !== 'string') {
|
|
39
|
+
throw new Error('Mem0 API key must be a string');
|
|
40
|
+
}
|
|
41
|
+
if (this.apiKey.trim() === '') {
|
|
42
|
+
throw new Error('Mem0 API key cannot be empty');
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async _fetchWithErrorHandling(url, options) {
|
|
47
|
+
const response = await fetch(url, options);
|
|
48
|
+
if (!response.ok) {
|
|
49
|
+
const errorData = await response.text();
|
|
50
|
+
throw new APIError(`API request failed: ${errorData}`);
|
|
51
|
+
}
|
|
52
|
+
const jsonResponse = await response.json();
|
|
53
|
+
return jsonResponse;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async add(messages, options = {}) {
|
|
57
|
+
options.organization_name = this.organizationName;
|
|
58
|
+
options.project_name = this.projectName;
|
|
59
|
+
const payload = this._preparePayload(messages, options);
|
|
60
|
+
const response = await this._fetchWithErrorHandling(`${this.host}/v1/memories/`, {
|
|
61
|
+
method: 'POST',
|
|
62
|
+
headers: this.headers,
|
|
63
|
+
body: JSON.stringify(payload)
|
|
64
|
+
});
|
|
65
|
+
return response;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async get(memoryId) {
|
|
69
|
+
return this._fetchWithErrorHandling(`${this.host}/v1/memories/${memoryId}/`, {
|
|
70
|
+
headers: this.headers
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async getAll(options = {}) {
|
|
75
|
+
const { api_version, ...otherOptions } = options;
|
|
76
|
+
|
|
77
|
+
otherOptions.organization_name = this.organizationName;
|
|
78
|
+
otherOptions.project_name = this.projectName;
|
|
79
|
+
|
|
80
|
+
if (api_version === 'v2') {
|
|
81
|
+
return this._fetchWithErrorHandling(`${this.host}/v2/memories/`, {
|
|
82
|
+
method: 'POST',
|
|
83
|
+
headers: this.headers,
|
|
84
|
+
body: JSON.stringify(otherOptions)
|
|
85
|
+
});
|
|
86
|
+
} else {
|
|
87
|
+
const params = new URLSearchParams(this._prepareParams(otherOptions));
|
|
88
|
+
return this._fetchWithErrorHandling(`${this.host}/v1/memories/?${params}`, {
|
|
89
|
+
headers: this.headers
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async search(query, options = {}) {
|
|
95
|
+
const { api_version, ...otherOptions } = options;
|
|
96
|
+
const payload = { query, ...otherOptions };
|
|
97
|
+
payload.organization_name = this.organizationName;
|
|
98
|
+
payload.project_name = this.projectName;
|
|
99
|
+
const endpoint = api_version === 'v2' ? '/v2/memories/search/' : '/v1/memories/search/';
|
|
100
|
+
const response = await this._fetchWithErrorHandling(`${this.host}${endpoint}`, {
|
|
101
|
+
method: 'POST',
|
|
102
|
+
headers: this.headers,
|
|
103
|
+
body: JSON.stringify(payload)
|
|
104
|
+
});
|
|
105
|
+
return response;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
async delete(memoryId) {
|
|
109
|
+
return this._fetchWithErrorHandling(`${this.host}/v1/memories/${memoryId}/`, {
|
|
110
|
+
method: 'DELETE',
|
|
111
|
+
headers: this.headers
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
async deleteAll(options = {}) {
|
|
116
|
+
options.organization_name = this.organizationName;
|
|
117
|
+
options.project_name = this.projectName;
|
|
118
|
+
const params = new URLSearchParams(this._prepareParams(options));
|
|
119
|
+
const response = await this._fetchWithErrorHandling(`${this.host}/v1/memories/?${params}`, {
|
|
120
|
+
method: 'DELETE',
|
|
121
|
+
headers: this.headers
|
|
122
|
+
});
|
|
123
|
+
return response;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async history(memoryId) {
|
|
127
|
+
const response = await this._fetchWithErrorHandling(`${this.host}/v1/memories/${memoryId}/history/`, {
|
|
128
|
+
headers: this.headers
|
|
129
|
+
});
|
|
130
|
+
return response;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
async users() {
|
|
134
|
+
const params = new URLSearchParams({
|
|
135
|
+
organization_name: this.organizationName,
|
|
136
|
+
project_name: this.projectName
|
|
137
|
+
});
|
|
138
|
+
const response = await this._fetchWithErrorHandling(`${this.host}/v1/entities/?${params}`, {
|
|
139
|
+
headers: this.headers
|
|
140
|
+
});
|
|
141
|
+
return response;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
async deleteUsers() {
|
|
145
|
+
const entities = await this.users();
|
|
146
|
+
for (const entity of entities.results) {
|
|
147
|
+
const params = {
|
|
148
|
+
organization_name: this.organizationName,
|
|
149
|
+
project_name: this.projectName
|
|
150
|
+
};
|
|
151
|
+
await this.client.delete(`/v1/entities/${entity.type}/${entity.id}/`, { params });
|
|
152
|
+
}
|
|
153
|
+
return { message: "All users, agents, and sessions deleted." };
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* @param {string|Array<{role: string, content: string}>} messages
|
|
158
|
+
* @param {MemoryOptions} options
|
|
159
|
+
* @returns {Object}
|
|
160
|
+
*/
|
|
161
|
+
_preparePayload(messages, options) {
|
|
162
|
+
const payload = {};
|
|
163
|
+
if (typeof messages === 'string') {
|
|
164
|
+
payload.messages = [{ role: 'user', content: messages }];
|
|
165
|
+
} else if (Array.isArray(messages)) {
|
|
166
|
+
payload.messages = messages;
|
|
167
|
+
}
|
|
168
|
+
return { ...payload, ...options };
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* @param {MemoryOptions} options
|
|
173
|
+
* @returns {Object}
|
|
174
|
+
*/
|
|
175
|
+
_prepareParams(options) {
|
|
176
|
+
return Object.fromEntries(Object.entries(options).filter(([_, v]) => v != null));
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
module.exports = MemoryClient;
|
package/src/index.d.ts
CHANGED