Updated deleteUsers to use V2 API Endpoints (#2624)
This commit is contained in:
@@ -209,6 +209,12 @@ mode: "wide"
|
|||||||
|
|
||||||
<Tab title="TypeScript">
|
<Tab title="TypeScript">
|
||||||
|
|
||||||
|
<Update label="2025-05-05" description="v2.1.23">
|
||||||
|
**New Features:**
|
||||||
|
- **Client:** Updated `deleteUsers` to use `v2` API.
|
||||||
|
- **Client:** Deprecated `deleteUser` and added deprecation warning.
|
||||||
|
</Update>
|
||||||
|
|
||||||
<Update label="2025-05-02" description="v2.1.22">
|
<Update label="2025-05-02" description="v2.1.22">
|
||||||
**New Features:**
|
**New Features:**
|
||||||
- **Client:** Updated `deleteUser` to use `entity_id` and `entity_type`
|
- **Client:** Updated `deleteUser` to use `entity_id` and `entity_type`
|
||||||
|
|||||||
@@ -1513,7 +1513,7 @@ client.delete_users({ user_id: "alex" })
|
|||||||
```
|
```
|
||||||
|
|
||||||
```bash cURL
|
```bash cURL
|
||||||
curl -X DELETE "https://api.mem0.ai/v1/entities/?user_id=alex" \
|
curl -X DELETE "https://api.mem0.ai/v2/entities/user/alex" \
|
||||||
-H "Authorization: Token your-api-key"
|
-H "Authorization: Token your-api-key"
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -1531,12 +1531,6 @@ curl -X DELETE "https://api.mem0.ai/v1/entities/?user_id=alex" \
|
|||||||
client.reset()
|
client.reset()
|
||||||
```
|
```
|
||||||
|
|
||||||
```javascript JavaScript
|
|
||||||
client.reset()
|
|
||||||
.then(result => console.log(result))
|
|
||||||
.catch(error => console.error(error));
|
|
||||||
```
|
|
||||||
|
|
||||||
```json Output
|
```json Output
|
||||||
{'message': 'Client reset successful. All users and memories deleted.'}
|
{'message': 'Client reset successful. All users and memories deleted.'}
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "mem0ai",
|
"name": "mem0ai",
|
||||||
"version": "2.1.22",
|
"version": "2.1.23",
|
||||||
"description": "The Memory Layer For Your AI Apps",
|
"description": "The Memory Layer For Your AI Apps",
|
||||||
"main": "./dist/index.js",
|
"main": "./dist/index.js",
|
||||||
"module": "./dist/index.mjs",
|
"module": "./dist/index.mjs",
|
||||||
|
|||||||
@@ -431,6 +431,9 @@ export default class MemoryClient {
|
|||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated The method should not be used, use `deleteUsers` instead. This will be removed in version 2.2.0.
|
||||||
|
*/
|
||||||
async deleteUser(data: {
|
async deleteUser(data: {
|
||||||
entity_id: number;
|
entity_id: number;
|
||||||
entity_type: string;
|
entity_type: string;
|
||||||
@@ -450,31 +453,86 @@ export default class MemoryClient {
|
|||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
async deleteUsers(): Promise<{ message: string }> {
|
async deleteUsers(
|
||||||
|
params: {
|
||||||
|
user_id?: string;
|
||||||
|
agent_id?: string;
|
||||||
|
app_id?: string;
|
||||||
|
run_id?: string;
|
||||||
|
} = {},
|
||||||
|
): Promise<{ message: string }> {
|
||||||
if (this.telemetryId === "") await this.ping();
|
if (this.telemetryId === "") await this.ping();
|
||||||
this._validateOrgProject();
|
this._validateOrgProject();
|
||||||
this._captureEvent("delete_users", []);
|
|
||||||
const entities = await this.users();
|
|
||||||
|
|
||||||
for (const entity of entities.results) {
|
let to_delete: Array<{ type: string; name: string }> = [];
|
||||||
let options: MemoryOptions = {};
|
const { user_id, agent_id, app_id, run_id } = params;
|
||||||
if (this.organizationName != null && this.projectName != null) {
|
|
||||||
options.org_name = this.organizationName;
|
|
||||||
options.project_name = this.projectName;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.organizationId != null && this.projectId != null) {
|
if (user_id) {
|
||||||
options.org_id = this.organizationId;
|
to_delete = [{ type: "user", name: user_id }];
|
||||||
options.project_id = this.projectId;
|
} else if (agent_id) {
|
||||||
|
to_delete = [{ type: "agent", name: agent_id }];
|
||||||
if (options.org_name) delete options.org_name;
|
} else if (app_id) {
|
||||||
if (options.project_name) delete options.project_name;
|
to_delete = [{ type: "app", name: app_id }];
|
||||||
}
|
} else if (run_id) {
|
||||||
await this.client.delete(`/v1/entities/${entity.type}/${entity.id}/`, {
|
to_delete = [{ type: "run", name: run_id }];
|
||||||
params: options,
|
} else {
|
||||||
});
|
const entities = await this.users();
|
||||||
|
to_delete = entities.results.map((entity) => ({
|
||||||
|
type: entity.type,
|
||||||
|
name: entity.name,
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
return { message: "All users, agents, and sessions deleted." };
|
|
||||||
|
if (to_delete.length === 0) {
|
||||||
|
throw new Error("No entities to delete");
|
||||||
|
}
|
||||||
|
|
||||||
|
const requestOptions: MemoryOptions = {};
|
||||||
|
if (this.organizationName != null && this.projectName != null) {
|
||||||
|
requestOptions.org_name = this.organizationName;
|
||||||
|
requestOptions.project_name = this.projectName;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.organizationId != null && this.projectId != null) {
|
||||||
|
requestOptions.org_id = this.organizationId;
|
||||||
|
requestOptions.project_id = this.projectId;
|
||||||
|
|
||||||
|
if (requestOptions.org_name) delete requestOptions.org_name;
|
||||||
|
if (requestOptions.project_name) delete requestOptions.project_name;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete each entity and handle errors
|
||||||
|
for (const entity of to_delete) {
|
||||||
|
try {
|
||||||
|
await this.client.delete(
|
||||||
|
`/v2/entities/${entity.type}/${entity.name}/`,
|
||||||
|
{
|
||||||
|
params: requestOptions,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
} catch (error: any) {
|
||||||
|
throw new APIError(
|
||||||
|
`Failed to delete ${entity.type} ${entity.name}: ${error.message}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this._captureEvent("delete_users", [
|
||||||
|
{
|
||||||
|
user_id: user_id,
|
||||||
|
agent_id: agent_id,
|
||||||
|
app_id: app_id,
|
||||||
|
run_id: run_id,
|
||||||
|
sync_type: "sync",
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
|
return {
|
||||||
|
message:
|
||||||
|
user_id || agent_id || app_id || run_id
|
||||||
|
? "Entity deleted successfully."
|
||||||
|
: "All users, agents, apps and runs deleted.",
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async batchUpdate(memories: Array<MemoryUpdateBody>): Promise<string> {
|
async batchUpdate(memories: Array<MemoryUpdateBody>): Promise<string> {
|
||||||
|
|||||||
Reference in New Issue
Block a user