UNPKG

mem0ai

Version:

The Memory Layer For Your AI Apps

1 lines 66.2 kB
{"version":3,"sources":["../src/client/mem0.ts","../src/client/telemetry.ts","../src/client/config.ts","../src/client/utils.ts","../src/common/exceptions.ts","../src/client/mem0.types.ts","../src/client/index.ts"],"sourcesContent":["import axios from \"axios\";\nimport {\n AllUsers,\n PaginatedMemories,\n ProjectOptions,\n Memory,\n MemoryHistory,\n AddMemoryOptions,\n SearchMemoryOptions,\n GetAllMemoryOptions,\n DeleteAllMemoryOptions,\n MemoryUpdateBody,\n ProjectResponse,\n PromptUpdatePayload,\n Webhook,\n WebhookCreatePayload,\n WebhookUpdatePayload,\n Message,\n FeedbackPayload,\n CreateMemoryExportPayload,\n GetMemoryExportPayload,\n} from \"./mem0.types\";\nimport {\n captureClientEvent,\n generateHash,\n isTelemetryEnabled,\n telemetry,\n} from \"./telemetry\";\nimport {\n getOrCreateMem0UserId,\n isMem0Aliased,\n markMem0Aliased,\n readMem0AnonIds,\n} from \"./config\";\nimport { camelToSnake, camelToSnakeKeys, snakeToCamelKeys } from \"./utils\";\nimport { createExceptionFromResponse, MemoryError } from \"../common/exceptions\";\n\n// Entity params that must be passed via filters - check both snake_case and camelCase\nconst ENTITY_PARAMS = [\n \"user_id\",\n \"agent_id\",\n \"app_id\",\n \"run_id\",\n \"userId\",\n \"agentId\",\n \"appId\",\n \"runId\",\n];\n\n/**\n * Validates that no top-level entity parameters are passed.\n * @throws Error if entity params are found at top level\n */\nfunction rejectTopLevelEntityParams(\n options: Record<string, any> | undefined,\n methodName: string,\n): void {\n const invalidKeys = Object.keys(options ?? {}).filter((k) =>\n ENTITY_PARAMS.includes(k),\n );\n if (invalidKeys.length > 0) {\n throw new Error(\n `Top-level entity parameters [${invalidKeys.join(\", \")}] are not supported in ${methodName}(). ` +\n `Use filters: { user_id: \"...\" } instead.`,\n );\n }\n}\n\nclass APIError extends Error {\n constructor(message: string) {\n super(message);\n this.name = \"APIError\";\n }\n}\n\ninterface ClientOptions {\n apiKey: string;\n host?: string;\n}\n\nexport default class MemoryClient {\n apiKey: string;\n host: string;\n private organizationId: string | number | null;\n private projectId: string | number | null;\n headers: Record<string, string>;\n client: any;\n telemetryId: string;\n\n _validateApiKey(): any {\n if (!this.apiKey) {\n throw new Error(\"Mem0 API key is required\");\n }\n if (typeof this.apiKey !== \"string\") {\n throw new Error(\"Mem0 API key must be a string\");\n }\n if (this.apiKey.trim() === \"\") {\n throw new Error(\"Mem0 API key cannot be empty\");\n }\n }\n\n constructor(options: ClientOptions) {\n this.apiKey = options.apiKey;\n this.host = options.host || \"https://api.mem0.ai\";\n this.organizationId = null;\n this.projectId = null;\n\n this.headers = {\n Authorization: `Token ${this.apiKey}`,\n \"Content-Type\": \"application/json\",\n };\n\n this.client = axios.create({\n baseURL: this.host,\n headers: { Authorization: `Token ${this.apiKey}` },\n timeout: 60000,\n });\n\n this._validateApiKey();\n this.telemetryId = \"\";\n this._initializeClient();\n }\n\n private async _initializeClient() {\n try {\n await this.ping();\n\n if (!this.telemetryId) {\n this.telemetryId = generateHash(this.apiKey);\n }\n\n await this._maybeAliasAnonToEmail();\n\n captureClientEvent(\"init\", this, {\n client_type: \"MemoryClient\",\n }).catch((error: any) => {\n console.error(\"Failed to capture event:\", error);\n });\n } catch (error: any) {\n console.error(\"Failed to initialize client:\", error);\n await captureClientEvent(\"init_error\", this, {\n error: error?.message || \"Unknown error\",\n stack: error?.stack || \"No stack trace\",\n });\n }\n }\n\n private async _maybeAliasAnonToEmail(): Promise<void> {\n if (!isTelemetryEnabled()) return;\n try {\n const email = this.telemetryId;\n if (!email || !email.includes(\"@\")) return;\n const sharedAnonId = await getOrCreateMem0UserId();\n const anonIds = await readMem0AnonIds();\n if (!anonIds && !sharedAnonId) return;\n const candidates = [anonIds?.oss || sharedAnonId, anonIds?.cli].filter(\n (id): id is string => !!id && id !== email,\n );\n const seen = new Set<string>();\n for (const anonId of candidates) {\n if (seen.has(anonId) || (await isMem0Aliased(anonId, email))) continue;\n seen.add(anonId);\n if (await telemetry.captureIdentify(anonId, email)) {\n await markMem0Aliased(anonId, email);\n }\n }\n } catch (error: any) {\n console.error(\"Failed to alias telemetry identity:\", error);\n }\n }\n\n private _captureEvent(methodName: string, args: any[]) {\n captureClientEvent(methodName, this, {\n success: true,\n args_count: args.length,\n keys: args.length > 0 ? args[0] : [],\n }).catch((error: any) => {\n console.error(\"Failed to capture event:\", error);\n });\n }\n\n async _fetchWithErrorHandling(url: string, options: any): Promise<any> {\n const response = await fetch(url, {\n ...options,\n headers: {\n ...options.headers,\n Authorization: `Token ${this.apiKey}`,\n \"Mem0-User-ID\": this.telemetryId,\n },\n });\n if (!response.ok) {\n const errorData = await response.text();\n throw createExceptionFromResponse(response.status, errorData);\n }\n const jsonResponse = await response.json();\n return snakeToCamelKeys(jsonResponse);\n }\n\n _preparePayload(\n messages: Array<Message>,\n options: Record<string, any>,\n ): object {\n const payload: any = {};\n payload.messages = messages;\n return camelToSnakeKeys({ ...payload, ...options });\n }\n\n _prepareParams(options: Record<string, any>): object {\n return Object.fromEntries(\n Object.entries(options).filter(([_, v]) => v != null),\n );\n }\n\n async ping(): Promise<void> {\n try {\n const response = await this._fetchWithErrorHandling(\n `${this.host}/v1/ping/`,\n {\n method: \"GET\",\n headers: {\n Authorization: `Token ${this.apiKey}`,\n },\n },\n );\n\n if (!response || typeof response !== \"object\") {\n throw new APIError(\"Invalid response format from ping endpoint\");\n }\n\n if (response.status !== \"ok\") {\n throw new APIError(response.message || \"API Key is invalid\");\n }\n\n const { orgId, projectId, userEmail } = response;\n\n if (orgId) this.organizationId = orgId;\n if (projectId) this.projectId = projectId;\n if (userEmail) this.telemetryId = userEmail;\n } catch (error: any) {\n // Pass through structured exceptions and APIError\n if (error instanceof MemoryError || error instanceof APIError) {\n throw error;\n } else {\n throw new APIError(\n `Failed to ping server: ${error.message || \"Unknown error\"}`,\n );\n }\n }\n }\n\n async add(\n messages: Array<Message>,\n options: AddMemoryOptions & Record<string, any> = {},\n ): Promise<Array<Memory>> {\n if (this.telemetryId === \"\") await this.ping();\n\n const payload = this._preparePayload(messages, options);\n const payloadKeys = Object.keys(payload);\n this._captureEvent(\"add\", [payloadKeys]);\n\n const response = await this._fetchWithErrorHandling(\n `${this.host}/v3/memories/add/`,\n {\n method: \"POST\",\n headers: this.headers,\n body: JSON.stringify(payload),\n },\n );\n return response;\n }\n\n async update(\n memoryId: string,\n {\n text,\n metadata,\n timestamp,\n }: {\n text?: string;\n metadata?: Record<string, any>;\n timestamp?: number | string;\n },\n ): Promise<Array<Memory>> {\n if (\n text === undefined &&\n metadata === undefined &&\n timestamp === undefined\n ) {\n throw new Error(\n \"At least one of text, metadata, or timestamp must be provided for update.\",\n );\n }\n\n if (this.telemetryId === \"\") await this.ping();\n const payload: Record<string, any> = {};\n if (text !== undefined) payload.text = text;\n if (metadata !== undefined) payload.metadata = metadata;\n if (timestamp !== undefined) payload.timestamp = timestamp;\n\n const payloadKeys = Object.keys(payload);\n this._captureEvent(\"update\", [payloadKeys]);\n\n const response = await this._fetchWithErrorHandling(\n `${this.host}/v1/memories/${memoryId}/`,\n {\n method: \"PUT\",\n headers: this.headers,\n body: JSON.stringify(payload),\n },\n );\n return response;\n }\n\n async get(memoryId: string): Promise<Memory> {\n if (this.telemetryId === \"\") await this.ping();\n this._captureEvent(\"get\", []);\n return this._fetchWithErrorHandling(\n `${this.host}/v1/memories/${memoryId}/`,\n {\n headers: this.headers,\n },\n );\n }\n\n async getAll(options?: GetAllMemoryOptions): Promise<PaginatedMemories> {\n // Reject top-level entity params - must use filters instead\n rejectTopLevelEntityParams(options as Record<string, any>, \"getAll\");\n\n if (this.telemetryId === \"\") await this.ping();\n const payloadKeys = Object.keys(options || {});\n this._captureEvent(\"get_all\", [payloadKeys]);\n const { page, pageSize, filters, ...rest } = options ?? {};\n const body: Record<string, any> = {\n ...camelToSnakeKeys(rest),\n ...(filters && { filters }),\n };\n\n let url = `${this.host}/v3/memories/`;\n if (page && pageSize) {\n url += `?page=${page}&page_size=${pageSize}`;\n }\n\n const response = await this._fetchWithErrorHandling(url, {\n method: \"POST\",\n headers: this.headers,\n body: JSON.stringify(body),\n });\n return response;\n }\n\n async search(\n query: string,\n options?: SearchMemoryOptions,\n ): Promise<{ results: Array<Memory> }> {\n // Reject top-level entity params - must use filters instead\n rejectTopLevelEntityParams(options as Record<string, any>, \"search\");\n\n if (this.telemetryId === \"\") await this.ping();\n const payloadKeys = Object.keys(options || {});\n this._captureEvent(\"search\", [payloadKeys]);\n const { filters, ...rest } = options ?? {};\n const payload: Record<string, any> = {\n query,\n output_format: \"v1.1\",\n ...camelToSnakeKeys(rest),\n ...(filters && { filters }),\n };\n\n const response = await this._fetchWithErrorHandling(\n `${this.host}/v3/memories/search/`,\n {\n method: \"POST\",\n headers: this.headers,\n body: JSON.stringify(payload),\n },\n );\n return response;\n }\n\n async delete(memoryId: string): Promise<{ message: string }> {\n if (this.telemetryId === \"\") await this.ping();\n this._captureEvent(\"delete\", []);\n return this._fetchWithErrorHandling(\n `${this.host}/v1/memories/${memoryId}/`,\n {\n method: \"DELETE\",\n headers: this.headers,\n },\n );\n }\n\n async deleteAll(\n options: DeleteAllMemoryOptions = {},\n ): Promise<{ message: string }> {\n if (this.telemetryId === \"\") await this.ping();\n const payloadKeys = Object.keys(options || {});\n this._captureEvent(\"delete_all\", [payloadKeys]);\n const snakeOptions = camelToSnakeKeys(this._prepareParams(options));\n // @ts-ignore\n const params = new URLSearchParams(snakeOptions);\n const response = await this._fetchWithErrorHandling(\n `${this.host}/v1/memories/?${params}`,\n {\n method: \"DELETE\",\n headers: this.headers,\n },\n );\n return response;\n }\n\n async history(memoryId: string): Promise<Array<MemoryHistory>> {\n if (this.telemetryId === \"\") await this.ping();\n this._captureEvent(\"history\", []);\n const response = await this._fetchWithErrorHandling(\n `${this.host}/v1/memories/${memoryId}/history/`,\n {\n headers: this.headers,\n },\n );\n return response;\n }\n\n async users(options?: {\n page?: number;\n pageSize?: number;\n }): Promise<AllUsers> {\n if (this.telemetryId === \"\") await this.ping();\n this._captureEvent(\"users\", []);\n let url = `${this.host}/v1/entities/`;\n const params: string[] = [];\n if (options?.page) params.push(`page=${options.page}`);\n if (options?.pageSize) params.push(`page_size=${options.pageSize}`);\n if (params.length) url += `?${params.join(\"&\")}`;\n const response = await this._fetchWithErrorHandling(url, {\n headers: this.headers,\n });\n return response;\n }\n\n /**\n * @deprecated The method should not be used, use `deleteUsers` instead. This will be removed in version 2.2.0.\n */\n async deleteUser(data: {\n entity_id: number;\n entity_type: string;\n }): Promise<{ message: string }> {\n if (this.telemetryId === \"\") await this.ping();\n this._captureEvent(\"delete_user\", []);\n if (!data.entity_type) {\n data.entity_type = \"user\";\n }\n const response = await this._fetchWithErrorHandling(\n `${this.host}/v1/entities/${data.entity_type}/${data.entity_id}/`,\n {\n method: \"DELETE\",\n headers: this.headers,\n },\n );\n return response;\n }\n\n async deleteUsers(\n params: {\n userId?: string;\n agentId?: string;\n appId?: string;\n runId?: string;\n } = {},\n ): Promise<{ message: string }> {\n if (this.telemetryId === \"\") await this.ping();\n\n let to_delete: Array<{ type: string; name: string }> = [];\n const { userId, agentId, appId, runId } = params;\n\n if (userId) {\n to_delete = [{ type: \"user\", name: userId }];\n } else if (agentId) {\n to_delete = [{ type: \"agent\", name: agentId }];\n } else if (appId) {\n to_delete = [{ type: \"app\", name: appId }];\n } else if (runId) {\n to_delete = [{ type: \"run\", name: runId }];\n } else {\n const entities = await this.users();\n to_delete = entities.results.map((entity) => ({\n type: entity.type,\n name: entity.name,\n }));\n }\n\n if (to_delete.length === 0) {\n throw new Error(\"No entities to delete\");\n }\n\n for (const entity of to_delete) {\n try {\n await this.client.delete(`/v2/entities/${entity.type}/${entity.name}/`);\n } catch (error: any) {\n throw new APIError(\n `Failed to delete ${entity.type} ${entity.name}: ${error.message}`,\n );\n }\n }\n\n this._captureEvent(\"delete_users\", [\n { userId, agentId, appId, runId, sync_type: \"sync\" },\n ]);\n\n return {\n message:\n userId || agentId || appId || runId\n ? \"Entity deleted successfully.\"\n : \"All users, agents, apps and runs deleted.\",\n };\n }\n\n async batchUpdate(memories: Array<MemoryUpdateBody>): Promise<string> {\n if (this.telemetryId === \"\") await this.ping();\n this._captureEvent(\"batch_update\", []);\n const memoriesBody = memories.map((memory) => ({\n memory_id: memory.memoryId,\n text: memory.text,\n }));\n const response = await this._fetchWithErrorHandling(\n `${this.host}/v1/batch/`,\n {\n method: \"PUT\",\n headers: this.headers,\n body: JSON.stringify({ memories: memoriesBody }),\n },\n );\n return response;\n }\n\n async batchDelete(memories: Array<string>): Promise<string> {\n if (this.telemetryId === \"\") await this.ping();\n this._captureEvent(\"batch_delete\", []);\n const memoriesBody = memories.map((memory) => ({\n memory_id: memory,\n }));\n const response = await this._fetchWithErrorHandling(\n `${this.host}/v1/batch/`,\n {\n method: \"DELETE\",\n headers: this.headers,\n body: JSON.stringify({ memories: memoriesBody }),\n },\n );\n return response;\n }\n\n async getProject(options: ProjectOptions): Promise<ProjectResponse> {\n if (this.telemetryId === \"\") await this.ping();\n const payloadKeys = Object.keys(options || {});\n this._captureEvent(\"get_project\", [payloadKeys]);\n const { fields } = options;\n\n if (!(this.organizationId && this.projectId)) {\n throw new Error(\n \"organizationId and projectId must be set to access instructions or categories\",\n );\n }\n\n const params = new URLSearchParams();\n fields?.forEach((field) => params.append(\"fields\", camelToSnake(field)));\n\n const response = await this._fetchWithErrorHandling(\n `${this.host}/api/v1/orgs/organizations/${this.organizationId}/projects/${this.projectId}/?${params.toString()}`,\n {\n headers: this.headers,\n },\n );\n return response;\n }\n\n async updateProject(\n prompts: PromptUpdatePayload,\n ): Promise<Record<string, any>> {\n if (this.telemetryId === \"\") await this.ping();\n this._captureEvent(\"update_project\", []);\n if (!(this.organizationId && this.projectId)) {\n throw new Error(\n \"organizationId and projectId must be set to update instructions or categories\",\n );\n }\n\n const response = await this._fetchWithErrorHandling(\n `${this.host}/api/v1/orgs/organizations/${this.organizationId}/projects/${this.projectId}/`,\n {\n method: \"PATCH\",\n headers: this.headers,\n body: JSON.stringify(camelToSnakeKeys(prompts)),\n },\n );\n return response;\n }\n\n // WebHooks\n async getWebhooks(data?: { projectId?: string }): Promise<Array<Webhook>> {\n if (this.telemetryId === \"\") await this.ping();\n this._captureEvent(\"get_webhooks\", []);\n const project_id = data?.projectId || this.projectId;\n const response = await this._fetchWithErrorHandling(\n `${this.host}/api/v1/webhooks/projects/${project_id}/`,\n {\n headers: this.headers,\n },\n );\n return response;\n }\n\n async createWebhook(webhook: WebhookCreatePayload): Promise<Webhook> {\n if (this.telemetryId === \"\") await this.ping();\n this._captureEvent(\"create_webhook\", []);\n const body = {\n name: webhook.name,\n url: webhook.url,\n event_types: webhook.eventTypes,\n };\n const response = await this._fetchWithErrorHandling(\n `${this.host}/api/v1/webhooks/projects/${this.projectId}/`,\n {\n method: \"POST\",\n headers: this.headers,\n body: JSON.stringify(body),\n },\n );\n return response;\n }\n\n async updateWebhook(\n webhook: WebhookUpdatePayload,\n ): Promise<{ message: string }> {\n if (this.telemetryId === \"\") await this.ping();\n this._captureEvent(\"update_webhook\", []);\n const body: Record<string, any> = {};\n if (webhook.name != null) body.name = webhook.name;\n if (webhook.url != null) body.url = webhook.url;\n if (webhook.eventTypes != null) body.event_types = webhook.eventTypes;\n const response = await this._fetchWithErrorHandling(\n `${this.host}/api/v1/webhooks/${webhook.webhookId}/`,\n {\n method: \"PUT\",\n headers: this.headers,\n body: JSON.stringify(body),\n },\n );\n return response;\n }\n\n async deleteWebhook(data: {\n webhookId: string;\n }): Promise<{ message: string }> {\n if (this.telemetryId === \"\") await this.ping();\n this._captureEvent(\"delete_webhook\", []);\n const webhook_id = data.webhookId || data;\n const response = await this._fetchWithErrorHandling(\n `${this.host}/api/v1/webhooks/${webhook_id}/`,\n {\n method: \"DELETE\",\n headers: this.headers,\n },\n );\n return response;\n }\n\n async feedback(data: FeedbackPayload): Promise<{ message: string }> {\n if (this.telemetryId === \"\") await this.ping();\n const payloadKeys = Object.keys(data || {});\n this._captureEvent(\"feedback\", [payloadKeys]);\n const response = await this._fetchWithErrorHandling(\n `${this.host}/v1/feedback/`,\n {\n method: \"POST\",\n headers: this.headers,\n body: JSON.stringify(camelToSnakeKeys(data)),\n },\n );\n return response;\n }\n\n async createMemoryExport(\n data: CreateMemoryExportPayload,\n ): Promise<{ message: string; id: string }> {\n if (this.telemetryId === \"\") await this.ping();\n this._captureEvent(\"create_memory_export\", []);\n\n if (!data.filters || !data.schema) {\n throw new Error(\"Missing filters or schema\");\n }\n\n const { filters, ...rest } = data;\n const response = await this._fetchWithErrorHandling(\n `${this.host}/v1/exports/`,\n {\n method: \"POST\",\n headers: this.headers,\n body: JSON.stringify({\n ...camelToSnakeKeys(rest),\n filters,\n }),\n },\n );\n\n return response;\n }\n\n async getMemoryExport(\n data: GetMemoryExportPayload,\n ): Promise<{ message: string; id: string }> {\n if (this.telemetryId === \"\") await this.ping();\n this._captureEvent(\"get_memory_export\", []);\n\n if (!data.memoryExportId && !data.filters) {\n throw new Error(\"Missing memoryExportId or filters\");\n }\n\n const { filters, ...rest } = data;\n const response = await this._fetchWithErrorHandling(\n `${this.host}/v1/exports/get/`,\n {\n method: \"POST\",\n headers: this.headers,\n body: JSON.stringify({\n ...camelToSnakeKeys(rest),\n ...(filters && { filters }),\n }),\n },\n );\n return response;\n }\n}\n\nexport { MemoryClient };\n","// @ts-nocheck\nimport type { TelemetryClient, TelemetryOptions } from \"./telemetry.types\";\n\n// __MEM0_SDK_VERSION__ is inlined by tsup/esbuild's `define` at build time from\n// package.json. In unbundled environments (ts-jest, jest globalSetup) the\n// identifier is not defined, so guard with typeof to fall back safely.\nlet version =\n typeof __MEM0_SDK_VERSION__ !== \"undefined\" ? __MEM0_SDK_VERSION__ : \"dev\";\n\n// Safely check for process.env in different environments\nlet MEM0_TELEMETRY = true;\ntry {\n MEM0_TELEMETRY = process?.env?.MEM0_TELEMETRY === \"false\" ? false : true;\n} catch (error) {}\nconst POSTHOG_API_KEY = \"phc_hgJkUVJFYtmaJqrvf6CYN67TIQ8yhXAkWzUn9AMU4yX\";\nconst POSTHOG_HOST = \"https://us.i.posthog.com/i/v0/e/\";\n\n// Simple hash function using random strings\nfunction generateHash(input: string): string {\n const randomStr =\n Math.random().toString(36).substring(2, 15) +\n Math.random().toString(36).substring(2, 15);\n return randomStr;\n}\n\nclass UnifiedTelemetry implements TelemetryClient {\n private apiKey: string;\n private host: string;\n\n constructor(projectApiKey: string, host: string) {\n this.apiKey = projectApiKey;\n this.host = host;\n }\n\n async captureEvent(\n distinctId: string,\n eventName: string,\n properties = {},\n ): Promise<boolean> {\n if (!MEM0_TELEMETRY) return false;\n\n const eventProperties = {\n client_version: version,\n timestamp: new Date().toISOString(),\n ...properties,\n $process_person_profile: false,\n $lib: \"posthog-node\",\n };\n\n const payload = {\n api_key: this.apiKey,\n distinct_id: distinctId,\n event: eventName,\n properties: eventProperties,\n };\n\n try {\n const response = await fetch(this.host, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n },\n body: JSON.stringify(payload),\n });\n\n if (!response.ok) {\n console.error(\"Telemetry event capture failed:\", await response.text());\n return false;\n }\n return true;\n } catch (error) {\n console.error(\"Telemetry event capture failed:\", error);\n return false;\n }\n }\n\n async captureIdentify(anonId: string, email: string): Promise<boolean> {\n if (!MEM0_TELEMETRY) return false;\n if (!anonId || !email || anonId === email) return false;\n\n const payload = {\n api_key: this.apiKey,\n distinct_id: email,\n event: \"$identify\",\n properties: {\n $anon_distinct_id: anonId,\n client_source: \"typescript\",\n $lib: \"posthog-node\",\n },\n };\n\n try {\n const response = await fetch(this.host, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n },\n body: JSON.stringify(payload),\n });\n\n if (!response.ok) {\n console.error(\n \"Telemetry identify capture failed:\",\n await response.text(),\n );\n return false;\n }\n return true;\n } catch (error) {\n console.error(\"Telemetry identify capture failed:\", error);\n return false;\n }\n }\n\n async shutdown() {\n // No shutdown needed for direct API calls\n }\n}\n\nfunction isTelemetryEnabled(): boolean {\n return MEM0_TELEMETRY;\n}\n\nconst telemetry = new UnifiedTelemetry(POSTHOG_API_KEY, POSTHOG_HOST);\n\nasync function captureClientEvent(\n eventName: string,\n instance: any,\n additionalData = {},\n) {\n if (!instance.telemetryId) {\n console.warn(\"No telemetry ID found for instance\");\n return;\n }\n\n const eventData = {\n function: `${instance.constructor.name}`,\n method: eventName,\n api_host: instance.host,\n timestamp: new Date().toISOString(),\n client_version: version,\n keys: additionalData?.keys || [],\n ...additionalData,\n };\n\n await telemetry.captureEvent(\n instance.telemetryId,\n `client.${eventName}`,\n eventData,\n );\n}\n\nexport { telemetry, captureClientEvent, generateHash, isTelemetryEnabled };\n","/**\n * Best-effort read/write of ~/.mem0/config.json from the TS SDK.\n *\n * Used to stitch PostHog identities: SDKs and CLIs persist anonymous\n * distinct_id values here, and the TS MemoryClient reads those on init to\n * fire $identify and merge them into the email identity.\n *\n * Node-only. Browsers (no `process.versions.node`) no-op.\n */\n\nexport interface Mem0AnonIds {\n oss?: string;\n cli?: string;\n aliasedPairs: string[];\n}\n\ninterface NodeFs {\n fs: typeof import(\"fs\");\n path: typeof import(\"path\");\n crypto: typeof import(\"crypto\");\n configPath: string;\n}\n\nasync function getNodeFs(): Promise<NodeFs | null> {\n if (typeof process === \"undefined\" || !process.versions?.node) return null;\n try {\n const [fs, path, os, crypto] = await Promise.all([\n import(\"fs\"),\n import(\"path\"),\n import(\"os\"),\n import(\"crypto\"),\n ]);\n const fsMod = (fs as any).default ?? fs;\n const pathMod = (path as any).default ?? path;\n const osMod = (os as any).default ?? os;\n const cryptoMod = (crypto as any).default ?? crypto;\n const dir = process.env.MEM0_DIR || pathMod.join(osMod.homedir(), \".mem0\");\n return {\n fs: fsMod,\n path: pathMod,\n crypto: cryptoMod,\n configPath: pathMod.join(dir, \"config.json\"),\n };\n } catch {\n return null;\n }\n}\n\nfunction loadConfig(node: NodeFs): Record<string, any> | null {\n try {\n if (!node.fs.existsSync(node.configPath)) return null;\n const parsed = JSON.parse(node.fs.readFileSync(node.configPath, \"utf8\"));\n return parsed && typeof parsed === \"object\" ? parsed : null;\n } catch {\n return null;\n }\n}\n\nfunction writeConfig(node: NodeFs, config: Record<string, any>): void {\n node.fs.mkdirSync(node.path.dirname(node.configPath), { recursive: true });\n node.fs.writeFileSync(node.configPath, JSON.stringify(config, null, 4));\n}\n\nfunction aliasPairMarker(node: NodeFs, anonId: string, email: string): string {\n return node.crypto\n .createHash(\"sha256\")\n .update(`${anonId}\\0${email}`, \"utf8\")\n .digest(\"hex\");\n}\n\nfunction randomUserId(node: NodeFs): string {\n if (typeof node.crypto.randomUUID === \"function\") {\n return node.crypto.randomUUID();\n }\n return (\n Math.random().toString(36).substring(2, 15) +\n Math.random().toString(36).substring(2, 15)\n );\n}\n\nexport async function getOrCreateMem0UserId(): Promise<string | null> {\n const node = await getNodeFs();\n if (!node) return null;\n try {\n const config = loadConfig(node) ?? {};\n if (typeof config.user_id === \"string\" && config.user_id) {\n return config.user_id;\n }\n const userId = randomUserId(node);\n config.user_id = userId;\n writeConfig(node, config);\n return userId;\n } catch {\n return null;\n }\n}\n\nexport async function readMem0AnonIds(): Promise<Mem0AnonIds | null> {\n const node = await getNodeFs();\n if (!node) return null;\n const config = loadConfig(node);\n if (!config) return null;\n const telemetry =\n config.telemetry && typeof config.telemetry === \"object\"\n ? config.telemetry\n : {};\n return {\n oss: typeof config.user_id === \"string\" ? config.user_id : undefined,\n cli:\n typeof telemetry.anonymous_id === \"string\"\n ? telemetry.anonymous_id\n : undefined,\n aliasedPairs: Array.isArray(telemetry.aliased_pairs)\n ? telemetry.aliased_pairs.filter(\n (item: unknown) => typeof item === \"string\",\n )\n : [],\n };\n}\n\nexport async function isMem0Aliased(\n anonId: string,\n email: string,\n): Promise<boolean> {\n if (!anonId || !email) return false;\n const node = await getNodeFs();\n if (!node) return false;\n const config = loadConfig(node);\n if (!config) return false;\n const telemetry =\n config.telemetry && typeof config.telemetry === \"object\"\n ? config.telemetry\n : {};\n const aliasedPairs = Array.isArray(telemetry.aliased_pairs)\n ? telemetry.aliased_pairs\n : [];\n return aliasedPairs.includes(aliasPairMarker(node, anonId, email));\n}\n\nexport async function markMem0Aliased(\n anonId: string,\n email: string,\n): Promise<void> {\n const node = await getNodeFs();\n if (!node) return;\n try {\n const config = loadConfig(node) ?? {};\n const telemetry =\n config.telemetry && typeof config.telemetry === \"object\"\n ? config.telemetry\n : {};\n const aliasedPairs = Array.isArray(telemetry.aliased_pairs)\n ? telemetry.aliased_pairs\n : [];\n const marker = aliasPairMarker(node, anonId, email);\n if (!aliasedPairs.includes(marker)) {\n aliasedPairs.push(marker);\n }\n telemetry.aliased_pairs = aliasedPairs;\n config.telemetry = telemetry;\n writeConfig(node, config);\n } catch {\n // Best-effort: read-only filesystems and unwritable paths just skip.\n }\n}\n","/**\n * Converts a camelCase string to snake_case.\n */\nexport function camelToSnake(str: string): string {\n // Skip all-uppercase keys (e.g. OR, AND, NOT — logical operators)\n if (str === str.toUpperCase()) return str;\n return str.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`);\n}\n\n/**\n * Converts a snake_case string to camelCase.\n */\nfunction snakeToCamel(str: string): string {\n return str.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase());\n}\n\n/**\n * Recursively converts all keys of an object from camelCase to snake_case.\n * Used for converting user-facing camelCase params to API snake_case payloads.\n */\nexport function camelToSnakeKeys(obj: any): any {\n if (obj === null || obj === undefined || typeof obj !== \"object\") return obj;\n if (Array.isArray(obj)) return obj.map(camelToSnakeKeys);\n if (obj instanceof Date) return obj;\n\n return Object.fromEntries(\n Object.entries(obj).map(([key, value]) => [\n camelToSnake(key),\n camelToSnakeKeys(value),\n ]),\n );\n}\n\n/**\n * Recursively converts all keys of an object from snake_case to camelCase.\n * Used for converting API snake_case responses to user-facing camelCase.\n */\nexport function snakeToCamelKeys(obj: any): any {\n if (obj === null || obj === undefined || typeof obj !== \"object\") return obj;\n if (Array.isArray(obj)) return obj.map(snakeToCamelKeys);\n if (obj instanceof Date) return obj;\n\n return Object.fromEntries(\n Object.entries(obj).map(([key, value]) => [\n snakeToCamel(key),\n snakeToCamelKeys(value),\n ]),\n );\n}\n","/**\n * Structured exception classes for mem0 TypeScript SDK.\n *\n * Provides specific, actionable exceptions with error codes, suggestions,\n * and debug information. Maps HTTP status codes to appropriate exception types.\n *\n * @example\n * ```typescript\n * import { RateLimitError, MemoryNotFoundError } from 'mem0ai'\n *\n * try {\n * await client.get(memoryId)\n * } catch (e) {\n * if (e instanceof MemoryNotFoundError) {\n * console.log(e.suggestion) // \"The requested resource was not found\"\n * } else if (e instanceof RateLimitError) {\n * await sleep(e.debugInfo.retryAfter ?? 60)\n * }\n * }\n * ```\n */\n\nexport interface MemoryErrorOptions {\n details?: Record<string, unknown>;\n suggestion?: string;\n debugInfo?: Record<string, unknown>;\n}\n\n/**\n * Base exception for all memory-related errors.\n *\n * Every mem0 exception includes an error code for programmatic handling,\n * optional details, a user-friendly suggestion, and debug information.\n */\nexport class MemoryError extends Error {\n readonly errorCode: string;\n readonly details: Record<string, unknown>;\n readonly suggestion?: string;\n readonly debugInfo: Record<string, unknown>;\n\n constructor(\n message: string,\n errorCode: string,\n options: MemoryErrorOptions = {},\n ) {\n super(message);\n this.name = \"MemoryError\";\n this.errorCode = errorCode;\n this.details = options.details ?? {};\n this.suggestion = options.suggestion;\n this.debugInfo = options.debugInfo ?? {};\n\n // Fix prototype chain for instanceof checks\n Object.setPrototypeOf(this, new.target.prototype);\n }\n}\n\n/** Raised when authentication fails (401, 403). */\nexport class AuthenticationError extends MemoryError {\n constructor(\n message: string,\n errorCode: string,\n options?: MemoryErrorOptions,\n ) {\n super(message, errorCode, options);\n this.name = \"AuthenticationError\";\n }\n}\n\n/** Raised when rate limits are exceeded (429). */\nexport class RateLimitError extends MemoryError {\n constructor(\n message: string,\n errorCode: string,\n options?: MemoryErrorOptions,\n ) {\n super(message, errorCode, options);\n this.name = \"RateLimitError\";\n }\n}\n\n/** Raised when input validation fails (400, 409, 422). */\nexport class ValidationError extends MemoryError {\n constructor(\n message: string,\n errorCode: string,\n options?: MemoryErrorOptions,\n ) {\n super(message, errorCode, options);\n this.name = \"ValidationError\";\n }\n}\n\n/** Raised when a memory is not found (404). */\nexport class MemoryNotFoundError extends MemoryError {\n constructor(\n message: string,\n errorCode: string,\n options?: MemoryErrorOptions,\n ) {\n super(message, errorCode, options);\n this.name = \"MemoryNotFoundError\";\n }\n}\n\n/** Raised when network connectivity issues occur (408, 502, 503, 504). */\nexport class NetworkError extends MemoryError {\n constructor(\n message: string,\n errorCode: string,\n options?: MemoryErrorOptions,\n ) {\n super(message, errorCode, options);\n this.name = \"NetworkError\";\n }\n}\n\n/** Raised when client configuration is invalid. */\nexport class ConfigurationError extends MemoryError {\n constructor(\n message: string,\n errorCode: string,\n options?: MemoryErrorOptions,\n ) {\n super(message, errorCode, options);\n this.name = \"ConfigurationError\";\n }\n}\n\n/** Raised when memory quota is exceeded (413). */\nexport class MemoryQuotaExceededError extends MemoryError {\n constructor(\n message: string,\n errorCode: string,\n options?: MemoryErrorOptions,\n ) {\n super(message, errorCode, options);\n this.name = \"MemoryQuotaExceededError\";\n }\n}\n\n// ─── HTTP Status → Exception Mapping ─────────────────────\n\ntype MemoryErrorConstructor = new (\n message: string,\n errorCode: string,\n options?: MemoryErrorOptions,\n) => MemoryError;\n\nexport const HTTP_STATUS_TO_EXCEPTION: Record<number, MemoryErrorConstructor> =\n {\n 400: ValidationError,\n 401: AuthenticationError,\n 403: AuthenticationError,\n 404: MemoryNotFoundError,\n 408: NetworkError,\n 409: ValidationError,\n 413: MemoryQuotaExceededError,\n 422: ValidationError,\n 429: RateLimitError,\n 500: MemoryError,\n 502: NetworkError,\n 503: NetworkError,\n 504: NetworkError,\n };\n\nconst HTTP_SUGGESTIONS: Record<number, string> = {\n 400: \"Please check your request parameters and try again\",\n 401: \"Please check your API key and authentication credentials\",\n 403: \"You don't have permission to perform this operation\",\n 404: \"The requested resource was not found\",\n 408: \"Request timed out. Please try again\",\n 409: \"Resource conflict. Please check your request\",\n 413: \"Request too large. Please reduce the size of your request\",\n 422: \"Invalid request data. Please check your input\",\n 429: \"Rate limit exceeded. Please wait before making more requests\",\n 500: \"Internal server error. Please try again later\",\n 502: \"Service temporarily unavailable. Please try again later\",\n 503: \"Service unavailable. Please try again later\",\n 504: \"Gateway timeout. Please try again later\",\n};\n\n/**\n * Create an appropriate exception based on HTTP response status code.\n *\n * @param statusCode - HTTP status code from the response\n * @param responseText - Response body text\n * @param options - Additional error context (details, debugInfo)\n * @returns An instance of the appropriate MemoryError subclass\n */\nexport function createExceptionFromResponse(\n statusCode: number,\n responseText: string,\n options: Omit<MemoryErrorOptions, \"suggestion\"> = {},\n): MemoryError {\n const ExceptionClass = HTTP_STATUS_TO_EXCEPTION[statusCode] ?? MemoryError;\n const errorCode = `HTTP_${statusCode}`;\n const suggestion = HTTP_SUGGESTIONS[statusCode] ?? \"Please try again later\";\n\n return new ExceptionClass(\n responseText || `HTTP ${statusCode} error`,\n errorCode,\n { ...options, suggestion },\n );\n}\n","// ─── Entity Options (for add/delete — top-level identity) ───\nexport interface EntityOptions {\n userId?: string;\n agentId?: string;\n appId?: string;\n runId?: string;\n}\n\n// ─── Per-Method Options ─────────────────────────────────────\nexport interface AddMemoryOptions extends EntityOptions {\n metadata?: Record<string, any>;\n infer?: boolean;\n customCategories?: custom_categories[];\n customInstructions?: string;\n timestamp?: number;\n structuredDataSchema?: Record<string, any>;\n}\n\nexport interface SearchMemoryOptions {\n filters?: Record<string, any>;\n metadata?: Record<string, any>;\n topK?: number;\n threshold?: number;\n rerank?: boolean;\n fields?: string[];\n categories?: string[];\n}\n\nexport interface GetAllMemoryOptions {\n filters?: Record<string, any>;\n page?: number;\n pageSize?: number;\n startDate?: string;\n endDate?: string;\n categories?: string[];\n}\n\nexport interface DeleteAllMemoryOptions extends EntityOptions {}\n\n// ─── Project Options ────────────────────────────────────────\nexport interface ProjectOptions {\n fields?: string[];\n}\n\nexport interface PromptUpdatePayload {\n customInstructions?: string;\n customCategories?: custom_categories[];\n retrievalCriteria?: any[];\n version?: string;\n memoryDepth?: string | null;\n usecaseSetting?: string | number;\n multilingual?: boolean;\n /**\n * Toggle Memory Decay for this project. When `true`, search-time ranking\n * boosts recently-used memories and gently dampens stale ones; when `false`,\n * ranking is restored to the pre-decay behaviour. Off by default.\n * See https://docs.mem0.ai/platform/features/memory-decay\n */\n decay?: boolean;\n [key: string]: any;\n}\n\n// ─── Enums ──────────────────────────────────────────────────\nexport enum Feedback {\n POSITIVE = \"POSITIVE\",\n NEGATIVE = \"NEGATIVE\",\n VERY_NEGATIVE = \"VERY_NEGATIVE\",\n}\n\n// ─── Message Types ──────────────────────────────────────────\nexport interface MultiModalMessages {\n type: \"image_url\";\n image_url: {\n url: string;\n };\n}\n\nexport interface Messages {\n role: \"user\" | \"assistant\";\n content: string | MultiModalMessages;\n}\n\nexport interface Message extends Messages {}\n\n// ─── Response Types (camelCase — converted from API snake_case) ─────\nexport interface MemoryData {\n memory: string;\n}\n\nenum Event {\n ADD = \"ADD\",\n UPDATE = \"UPDATE\",\n DELETE = \"DELETE\",\n NOOP = \"NOOP\",\n}\n\nexport interface Memory {\n id: string;\n messages?: Array<Messages>;\n event?: Event | string;\n data?: MemoryData | null;\n memory?: string;\n userId?: string;\n hash?: string;\n categories?: Array<string>;\n createdAt?: Date;\n updatedAt?: Date;\n memoryType?: string;\n score?: number;\n metadata?: any | null;\n owner?: string | null;\n agentId?: string | null;\n appId?: string | null;\n runId?: string | null;\n}\n\nexport interface MemoryHistory {\n id: string;\n memoryId: string;\n input: Array<Messages>;\n oldMemory: string | null;\n newMemory: string | null;\n userId: string;\n categories: Array<string>;\n event: Event | string;\n createdAt: Date;\n updatedAt: Date;\n}\n\nexport interface MemoryUpdateBody {\n memoryId: string;\n text: string;\n}\n\nexport interface User {\n id: string;\n name: string;\n createdAt: Date;\n updatedAt: Date;\n totalMemories: number;\n owner: string;\n type: string;\n}\n\nexport interface AllUsers {\n count: number;\n results: Array<User>;\n next: any;\n previous: any;\n}\n\nexport interface PaginatedMemories {\n count: number;\n next: string | null;\n previous: string | null;\n results: Array<Memory>;\n}\n\nexport interface ProjectResponse {\n customInstructions?: string;\n customCategories?: string[];\n [key: string]: any;\n}\n\ninterface custom_categories {\n [key: string]: any;\n}\n\n// ─── Webhook Types ──────────────────────────────────────────\nexport enum WebhookEvent {\n MEMORY_ADDED = \"memory_add\",\n MEMORY_UPDATED = \"memory_update\",\n MEMORY_DELETED = \"memory_delete\",\n MEMORY_CATEGORIZED = \"memory_categorize\",\n}\n\nexport interface Webhook {\n webhookId?: string;\n name: string;\n url: string;\n project?: string;\n createdAt?: Date;\n updatedAt?: Date;\n isActive?: boolean;\n eventTypes?: WebhookEvent[];\n}\n\nexport interface WebhookCreatePayload {\n name: string;\n url: string;\n eventTypes: WebhookEvent[];\n}\n\nexport interface WebhookUpdatePayload {\n webhookId: string;\n name?: string;\n url?: string;\n eventTypes?: WebhookEvent[];\n}\n\n// ─── Feedback & Export Types ────────────────────────────────\nexport interface FeedbackPayload {\n memoryId: string;\n feedback?: Feedback | null;\n feedbackReason?: string | null;\n}\n\nexport interface CreateMemoryExportPayload {\n schema: Record<string, any>;\n filters: Record<string, any>;\n exportInstructions?: string;\n}\n\nexport interface GetMemoryExportPayload {\n filters?: Record<string, any>;\n memoryExportId?: string;\n}\n","import { MemoryClient } from \"./mem0\";\nimport type * as MemoryTypes from \"./mem0.types\";\n\n// Re-export all types from mem0.types\nexport type {\n AddMemoryOptions,\n SearchMemoryOptions,\n GetAllMemoryOptions,\n DeleteAllMemoryOptions,\n ProjectOptions,\n Memory,\n MemoryHistory,\n MemoryUpdateBody,\n ProjectResponse,\n PromptUpdatePayload,\n Webhook,\n WebhookCreatePayload,\n WebhookUpdatePayload,\n Messages,\n Message,\n AllUsers,\n User,\n FeedbackPayload,\n CreateMemoryExportPayload,\n GetMemoryExportPayload,\n} from \"./mem0.types\";\n\n// Re-export enums as values (not type-only)\nexport { Feedback, WebhookEvent } from \"./mem0.types\";\n\n// Export the main client\nexport { MemoryClient };\nexport default MemoryClient;\n\n// Export structured exceptions\nexport {\n MemoryError,\n AuthenticationError,\n RateLimitError,\n ValidationError,\n MemoryNotFoundError,\n NetworkError,\n ConfigurationError,\n MemoryQuotaExceededError,\n createExceptionFromResponse,\n} from \"../common/exceptions\";\n\nexport type { MemoryErrorOptions } from \"../common/exceptions\";\n"],"mappings":";AAAA,OAAO,WAAW;;;ACMlB,IAAI,UACF,OAA8C,UAAuB;AAGvE,IAAI,iBAAiB;AAVrB;AAWA,IAAI;AACF,qBAAiB,wCAAS,QAAT,mBAAc,oBAAmB,UAAU,QAAQ;AACtE,SAAS,OAAO;AAAC;AACjB,IAAM,kBAAkB;AACxB,IAAM,eAAe;AAGrB,SAAS,aAAa,OAAuB;AAC3C,QAAM,YACJ,KAAK,OAAO,EAAE,SAAS,EAAE,EAAE,UAAU,GAAG,EAAE,IAC1C,KAAK,OAAO,EAAE,SAAS,EAAE,EAAE,UAAU,GAAG,EAAE;AAC5C,SAAO;AACT;AAEA,IAAM,mBAAN,MAAkD;AAAA,EAIhD,YAAY,eAAuB,MAAc;AAC/C,SAAK,SAAS;AACd,SAAK,OAAO;AAAA,EACd;AAAA,EAEA,MAAM,aACJ,YACA,WACA,aAAa,CAAC,GACI;AAClB,QAAI,CAAC,eAAgB,QAAO;AAE5B,UAAM,kBAAkB;AAAA,MACtB,gBAAgB;AAAA,MAChB,YAAW,oBAAI,KAAK,GAAE,YAAY;AAAA,MAClC,GAAG;AAAA,MACH,yBAAyB;AAAA,MACzB,MAAM;AAAA,IACR;AAEA,UAAM,UAAU;AAAA,MACd,SAAS,KAAK;AAAA,MACd,aAAa;AAAA,MACb,OAAO;AAAA,MACP,YAAY;AAAA,IACd;AAEA,QAAI;AACF,YAAM,WAAW,MAAM,MAAM,KAAK,MAAM;AAAA,QACtC,QAAQ;AAAA,QACR,SAAS;AAAA,UACP,gBAAgB;AAAA,QAClB;AAAA,QACA,MAAM,KAAK,UAAU,O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