Introduce Ping in Mem0 Client (#2472)

This commit is contained in:
Saket Aryan
2025-03-30 12:49:36 +05:30
committed by GitHub
parent 5ed15c3bcd
commit 1033ab227c
3 changed files with 56 additions and 4 deletions

View File

@@ -56,6 +56,11 @@ mode: "wide"
<Tab title="TypeScript"> <Tab title="TypeScript">
<Update label="2025-03-29" description="v2.1.13">
**Improvements:**
- **Introuced `ping` method to check if API key is valid and populate org/project id**
</Update>
<Update label="2025-03-29" description="AI SDK v1.0.0"> <Update label="2025-03-29" description="AI SDK v1.0.0">
**New Features:** **New Features:**
- **Vercel AI SDK Update:** Support threshold and rerank - **Vercel AI SDK Update:** Support threshold and rerank

View File

@@ -1,6 +1,6 @@
{ {
"name": "mem0ai", "name": "mem0ai",
"version": "2.1.12", "version": "2.1.13",
"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",

View File

@@ -97,7 +97,6 @@ export default class MemoryClient {
}); });
this._validateApiKey(); this._validateApiKey();
this._validateOrgProject();
// Initialize with a temporary ID that will be updated // Initialize with a temporary ID that will be updated
this.telemetryId = ""; this.telemetryId = "";
@@ -109,12 +108,20 @@ export default class MemoryClient {
private async _initializeClient() { private async _initializeClient() {
try { try {
// Generate telemetry ID // Generate telemetry ID
this.telemetryId = generateHash(this.apiKey); await this.ping();
if (!this.telemetryId) {
this.telemetryId = generateHash(this.apiKey);
}
this._validateOrgProject();
// Capture initialization event // Capture initialization event
await captureClientEvent("init", this, { captureClientEvent("init", this, {
api_version: "v1", api_version: "v1",
client_type: "MemoryClient", client_type: "MemoryClient",
}).catch((error: any) => {
console.error("Failed to capture event:", error);
}); });
} catch (error: any) { } catch (error: any) {
console.error("Failed to initialize client:", error); console.error("Failed to initialize client:", error);
@@ -171,10 +178,31 @@ export default class MemoryClient {
); );
} }
async ping(): Promise<void> {
const response = await fetch(`${this.host}/v1/ping/`, {
headers: {
Authorization: `Token ${this.apiKey}`,
},
});
const data = await response.json();
if (data.status !== "ok") {
throw new Error("API Key is invalid");
}
const { org_id, project_id, user_email } = data;
this.organizationId = org_id || null;
this.projectId = project_id || null;
this.telemetryId = user_email || "";
}
async add( async add(
messages: string | Array<Message>, messages: string | Array<Message>,
options: MemoryOptions = {}, options: MemoryOptions = {},
): Promise<Array<Memory>> { ): Promise<Array<Memory>> {
if (this.telemetryId === "") await this.ping();
this._validateOrgProject(); this._validateOrgProject();
if (this.organizationName != null && this.projectName != null) { if (this.organizationName != null && this.projectName != null) {
options.org_name = this.organizationName; options.org_name = this.organizationName;
@@ -211,6 +239,7 @@ export default class MemoryClient {
} }
async update(memoryId: string, message: string): Promise<Array<Memory>> { async update(memoryId: string, message: string): Promise<Array<Memory>> {
if (this.telemetryId === "") await this.ping();
this._validateOrgProject(); this._validateOrgProject();
const payload = { const payload = {
text: message, text: message,
@@ -231,6 +260,7 @@ export default class MemoryClient {
} }
async get(memoryId: string): Promise<Memory> { async get(memoryId: string): Promise<Memory> {
if (this.telemetryId === "") await this.ping();
this._captureEvent("get", []); this._captureEvent("get", []);
return this._fetchWithErrorHandling( return this._fetchWithErrorHandling(
`${this.host}/v1/memories/${memoryId}/`, `${this.host}/v1/memories/${memoryId}/`,
@@ -241,6 +271,7 @@ export default class MemoryClient {
} }
async getAll(options?: SearchOptions): Promise<Array<Memory>> { async getAll(options?: SearchOptions): Promise<Array<Memory>> {
if (this.telemetryId === "") await this.ping();
this._validateOrgProject(); this._validateOrgProject();
const payloadKeys = Object.keys(options || {}); const payloadKeys = Object.keys(options || {});
this._captureEvent("get_all", [payloadKeys]); this._captureEvent("get_all", [payloadKeys]);
@@ -288,6 +319,7 @@ export default class MemoryClient {
} }
async search(query: string, options?: SearchOptions): Promise<Array<Memory>> { async search(query: string, options?: SearchOptions): Promise<Array<Memory>> {
if (this.telemetryId === "") await this.ping();
this._validateOrgProject(); this._validateOrgProject();
const payloadKeys = Object.keys(options || {}); const payloadKeys = Object.keys(options || {});
this._captureEvent("search", [payloadKeys]); this._captureEvent("search", [payloadKeys]);
@@ -319,6 +351,7 @@ export default class MemoryClient {
} }
async delete(memoryId: string): Promise<{ message: string }> { async delete(memoryId: string): Promise<{ message: string }> {
if (this.telemetryId === "") await this.ping();
this._captureEvent("delete", []); this._captureEvent("delete", []);
return this._fetchWithErrorHandling( return this._fetchWithErrorHandling(
`${this.host}/v1/memories/${memoryId}/`, `${this.host}/v1/memories/${memoryId}/`,
@@ -330,6 +363,7 @@ export default class MemoryClient {
} }
async deleteAll(options: MemoryOptions = {}): Promise<{ message: string }> { async deleteAll(options: MemoryOptions = {}): Promise<{ message: string }> {
if (this.telemetryId === "") await this.ping();
this._validateOrgProject(); this._validateOrgProject();
const payloadKeys = Object.keys(options || {}); const payloadKeys = Object.keys(options || {});
this._captureEvent("delete_all", [payloadKeys]); this._captureEvent("delete_all", [payloadKeys]);
@@ -358,6 +392,7 @@ export default class MemoryClient {
} }
async history(memoryId: string): Promise<Array<MemoryHistory>> { async history(memoryId: string): Promise<Array<MemoryHistory>> {
if (this.telemetryId === "") await this.ping();
this._captureEvent("history", []); this._captureEvent("history", []);
const response = await this._fetchWithErrorHandling( const response = await this._fetchWithErrorHandling(
`${this.host}/v1/memories/${memoryId}/history/`, `${this.host}/v1/memories/${memoryId}/history/`,
@@ -369,6 +404,7 @@ export default class MemoryClient {
} }
async users(): Promise<AllUsers> { async users(): Promise<AllUsers> {
if (this.telemetryId === "") await this.ping();
this._validateOrgProject(); this._validateOrgProject();
this._captureEvent("users", []); this._captureEvent("users", []);
const options: MemoryOptions = {}; const options: MemoryOptions = {};
@@ -399,6 +435,7 @@ export default class MemoryClient {
entityId: string, entityId: string,
entity: { type: string } = { type: "user" }, entity: { type: string } = { type: "user" },
): Promise<{ message: string }> { ): Promise<{ message: string }> {
if (this.telemetryId === "") await this.ping();
this._captureEvent("delete_user", []); this._captureEvent("delete_user", []);
const response = await this._fetchWithErrorHandling( const response = await this._fetchWithErrorHandling(
`${this.host}/v1/entities/${entity.type}/${entityId}/`, `${this.host}/v1/entities/${entity.type}/${entityId}/`,
@@ -411,6 +448,7 @@ export default class MemoryClient {
} }
async deleteUsers(): Promise<{ message: string }> { async deleteUsers(): Promise<{ message: string }> {
if (this.telemetryId === "") await this.ping();
this._validateOrgProject(); this._validateOrgProject();
this._captureEvent("delete_users", []); this._captureEvent("delete_users", []);
const entities = await this.users(); const entities = await this.users();
@@ -437,6 +475,7 @@ export default class MemoryClient {
} }
async batchUpdate(memories: Array<MemoryUpdateBody>): Promise<string> { async batchUpdate(memories: Array<MemoryUpdateBody>): Promise<string> {
if (this.telemetryId === "") await this.ping();
this._captureEvent("batch_update", []); this._captureEvent("batch_update", []);
const memoriesBody = memories.map((memory) => ({ const memoriesBody = memories.map((memory) => ({
memory_id: memory.memoryId, memory_id: memory.memoryId,
@@ -454,6 +493,7 @@ export default class MemoryClient {
} }
async batchDelete(memories: Array<string>): Promise<string> { async batchDelete(memories: Array<string>): Promise<string> {
if (this.telemetryId === "") await this.ping();
this._captureEvent("batch_delete", []); this._captureEvent("batch_delete", []);
const memoriesBody = memories.map((memory) => ({ const memoriesBody = memories.map((memory) => ({
memory_id: memory, memory_id: memory,
@@ -470,6 +510,7 @@ export default class MemoryClient {
} }
async getProject(options: ProjectOptions): Promise<ProjectResponse> { async getProject(options: ProjectOptions): Promise<ProjectResponse> {
if (this.telemetryId === "") await this.ping();
this._validateOrgProject(); this._validateOrgProject();
const payloadKeys = Object.keys(options || {}); const payloadKeys = Object.keys(options || {});
this._captureEvent("get_project", [payloadKeys]); this._captureEvent("get_project", [payloadKeys]);
@@ -496,6 +537,7 @@ export default class MemoryClient {
async updateProject( async updateProject(
prompts: PromptUpdatePayload, prompts: PromptUpdatePayload,
): Promise<Record<string, any>> { ): Promise<Record<string, any>> {
if (this.telemetryId === "") await this.ping();
this._validateOrgProject(); this._validateOrgProject();
this._captureEvent("update_project", []); this._captureEvent("update_project", []);
if (!(this.organizationId && this.projectId)) { if (!(this.organizationId && this.projectId)) {
@@ -517,6 +559,7 @@ export default class MemoryClient {
// WebHooks // WebHooks
async getWebhooks(data?: { projectId?: string }): Promise<Array<Webhook>> { async getWebhooks(data?: { projectId?: string }): Promise<Array<Webhook>> {
if (this.telemetryId === "") await this.ping();
this._captureEvent("get_webhooks", []); this._captureEvent("get_webhooks", []);
const project_id = data?.projectId || this.projectId; const project_id = data?.projectId || this.projectId;
const response = await this._fetchWithErrorHandling( const response = await this._fetchWithErrorHandling(
@@ -529,6 +572,7 @@ export default class MemoryClient {
} }
async createWebhook(webhook: WebhookPayload): Promise<Webhook> { async createWebhook(webhook: WebhookPayload): Promise<Webhook> {
if (this.telemetryId === "") await this.ping();
this._captureEvent("create_webhook", []); this._captureEvent("create_webhook", []);
const response = await this._fetchWithErrorHandling( const response = await this._fetchWithErrorHandling(
`${this.host}/api/v1/webhooks/projects/${this.projectId}/`, `${this.host}/api/v1/webhooks/projects/${this.projectId}/`,
@@ -542,6 +586,7 @@ export default class MemoryClient {
} }
async updateWebhook(webhook: WebhookPayload): Promise<{ message: string }> { async updateWebhook(webhook: WebhookPayload): Promise<{ message: string }> {
if (this.telemetryId === "") await this.ping();
this._captureEvent("update_webhook", []); this._captureEvent("update_webhook", []);
const project_id = webhook.projectId || this.projectId; const project_id = webhook.projectId || this.projectId;
const response = await this._fetchWithErrorHandling( const response = await this._fetchWithErrorHandling(
@@ -561,6 +606,7 @@ export default class MemoryClient {
async deleteWebhook(data: { async deleteWebhook(data: {
webhookId: string; webhookId: string;
}): Promise<{ message: string }> { }): Promise<{ message: string }> {
if (this.telemetryId === "") await this.ping();
this._captureEvent("delete_webhook", []); this._captureEvent("delete_webhook", []);
const webhook_id = data.webhookId || data; const webhook_id = data.webhookId || data;
const response = await this._fetchWithErrorHandling( const response = await this._fetchWithErrorHandling(
@@ -574,6 +620,7 @@ export default class MemoryClient {
} }
async feedback(data: FeedbackPayload): Promise<{ message: string }> { async feedback(data: FeedbackPayload): Promise<{ message: string }> {
if (this.telemetryId === "") await this.ping();
const payloadKeys = Object.keys(data || {}); const payloadKeys = Object.keys(data || {});
this._captureEvent("feedback", [payloadKeys]); this._captureEvent("feedback", [payloadKeys]);
const response = await this._fetchWithErrorHandling( const response = await this._fetchWithErrorHandling(