UNPKG

mem0ai

Version:

The Memory Layer For Your AI Apps

353 lines (349 loc) 11 kB
interface EntityOptions { userId?: string; agentId?: string; appId?: string; runId?: string; } interface AddMemoryOptions extends EntityOptions { metadata?: Record<string, any>; infer?: boolean; customCategories?: custom_categories[]; customInstructions?: string; timestamp?: number; structuredDataSchema?: Record<string, any>; } interface SearchMemoryOptions { filters?: Record<string, any>; metadata?: Record<string, any>; topK?: number; threshold?: number; rerank?: boolean; fields?: string[]; categories?: string[]; } interface GetAllMemoryOptions { filters?: Record<string, any>; page?: number; pageSize?: number; startDate?: string; endDate?: string; categories?: string[]; } interface DeleteAllMemoryOptions extends EntityOptions { } interface ProjectOptions { fields?: string[]; } interface PromptUpdatePayload { customInstructions?: string; customCategories?: custom_categories[]; retrievalCriteria?: any[]; version?: string; memoryDepth?: string | null; usecaseSetting?: string | number; multilingual?: boolean; /** * Toggle Memory Decay for this project. When `true`, search-time ranking * boosts recently-used memories and gently dampens stale ones; when `false`, * ranking is restored to the pre-decay behaviour. Off by default. * See https://docs.mem0.ai/platform/features/memory-decay */ decay?: boolean; [key: string]: any; } declare enum Feedback { POSITIVE = "POSITIVE", NEGATIVE = "NEGATIVE", VERY_NEGATIVE = "VERY_NEGATIVE" } interface MultiModalMessages { type: "image_url"; image_url: { url: string; }; } interface Messages { role: "user" | "assistant"; content: string | MultiModalMessages; } interface Message extends Messages { } interface MemoryData { memory: string; } declare enum Event { ADD = "ADD", UPDATE = "UPDATE", DELETE = "DELETE", NOOP = "NOOP" } interface Memory { id: string; messages?: Array<Messages>; event?: Event | string; data?: MemoryData | null; memory?: string; userId?: string; hash?: string; categories?: Array<string>; createdAt?: Date; updatedAt?: Date; memoryType?: string; score?: number; metadata?: any | null; owner?: string | null; agentId?: string | null; appId?: string | null; runId?: string | null; } interface MemoryHistory { id: string; memoryId: string; input: Array<Messages>; oldMemory: string | null; newMemory: string | null; userId: string; categories: Array<string>; event: Event | string; createdAt: Date; updatedAt: Date; } interface MemoryUpdateBody { memoryId: string; text: string; } interface User { id: string; name: string; createdAt: Date; updatedAt: Date; totalMemories: number; owner: string; type: string; } interface AllUsers { count: number; results: Array<User>; next: any; previous: any; } interface PaginatedMemories { count: number; next: string | null; previous: string | null; results: Array<Memory>; } interface ProjectResponse { customInstructions?: string; customCategories?: string[]; [key: string]: any; } interface custom_categories { [key: string]: any; } declare enum WebhookEvent { MEMORY_ADDED = "memory_add", MEMORY_UPDATED = "memory_update", MEMORY_DELETED = "memory_delete", MEMORY_CATEGORIZED = "memory_categorize" } interface Webhook { webhookId?: string; name: string; url: string; project?: string; createdAt?: Date; updatedAt?: Date; isActive?: boolean; eventTypes?: WebhookEvent[]; } interface WebhookCreatePayload { name: string; url: string; eventTypes: WebhookEvent[]; } interface WebhookUpdatePayload { webhookId: string; name?: string; url?: string; eventTypes?: WebhookEvent[]; } interface FeedbackPayload { memoryId: string; feedback?: Feedback | null; feedbackReason?: string | null; } interface CreateMemoryExportPayload { schema: Record<string, any>; filters: Record<string, any>; exportInstructions?: string; } interface GetMemoryExportPayload { filters?: Record<string, any>; memoryExportId?: string; } interface ClientOptions { apiKey: string; host?: string; } declare class MemoryClient { apiKey: string; host: string; private organizationId; private projectId; headers: Record<string, string>; client: any; telemetryId: string; _validateApiKey(): any; constructor(options: ClientOptions); private _initializeClient; private _maybeAliasAnonToEmail; private _captureEvent; _fetchWithErrorHandling(url: string, options: any): Promise<any>; _preparePayload(messages: Array<Message>, options: Record<string, any>): object; _prepareParams(options: Record<string, any>): object; ping(): Promise<void>; add(messages: Array<Message>, options?: AddMemoryOptions & Record<string, any>): Promise<Array<Memory>>; update(memoryId: string, { text, metadata, timestamp, }: { text?: string; metadata?: Record<string, any>; timestamp?: number | string; }): Promise<Array<Memory>>; get(memoryId: string): Promise<Memory>; getAll(options?: GetAllMemoryOptions): Promise<PaginatedMemories>; search(query: string, options?: SearchMemoryOptions): Promise<{ results: Array<Memory>; }>; delete(memoryId: string): Promise<{ message: string; }>; deleteAll(options?: DeleteAllMemoryOptions): Promise<{ message: string; }>; history(memoryId: string): Promise<Array<MemoryHistory>>; users(options?: { page?: number; pageSize?: number; }): Promise<AllUsers>; /** * @deprecated The method should not be used, use `deleteUsers` instead. This will be removed in version 2.2.0. */ deleteUser(data: { entity_id: number; entity_type: string; }): Promise<{ message: string; }>; deleteUsers(params?: { userId?: string; agentId?: string; appId?: string; runId?: string; }): Promise<{ message: string; }>; batchUpdate(memories: Array<MemoryUpdateBody>): Promise<string>; batchDelete(memories: Array<string>): Promise<string>; getProject(options: ProjectOptions): Promise<ProjectResponse>; updateProject(prompts: PromptUpdatePayload): Promise<Record<string, any>>; getWebhooks(data?: { projectId?: string; }): Promise<Array<Webhook>>; createWebhook(webhook: WebhookCreatePayload): Promise<Webhook>; updateWebhook(webhook: WebhookUpdatePayload): Promise<{ message: string; }>; deleteWebhook(data: { webhookId: string; }): Promise<{ message: string; }>; feedback(data: FeedbackPayload): Promise<{ message: string; }>; createMemoryExport(data: CreateMemoryExportPayload): Promise<{ message: string; id: string; }>; getMemoryExport(data: GetMemoryExportPayload): Promise<{ message: string; id: string; }>; } /** * Structured exception classes for mem0 TypeScript SDK. * * Provides specific, actionable exceptions with error codes, suggestions, * and debug information. Maps HTTP status codes to appropriate exception types. * * @example * ```typescript * import { RateLimitError, MemoryNotFoundError } from 'mem0ai' * * try { * await client.get(memoryId) * } catch (e) { * if (e instanceof MemoryNotFoundError) { * console.log(e.suggestion) // "The requested resource was not found" * } else if (e instanceof RateLimitError) { * await sleep(e.debugInfo.retryAfter ?? 60) * } * } * ``` */ interface MemoryErrorOptions { details?: Record<string, unknown>; suggestion?: string; debugInfo?: Record<string, unknown>; } /** * Base exception for all memory-related errors. * * Every mem0 exception includes an error code for programmatic handling, * optional details, a user-friendly suggestion, and debug information. */ declare class MemoryError extends Error { readonly errorCode: string; readonly details: Record<string, unknown>; readonly suggestion?: string; readonly debugInfo: Record<string, unknown>; constructor(message: string, errorCode: string, options?: MemoryErrorOptions); } /** Raised when authentication fails (401, 403). */ declare class AuthenticationError extends MemoryError { constructor(message: string, errorCode: string, options?: MemoryErrorOptions); } /** Raised when rate limits are exceeded (429). */ declare class RateLimitError extends MemoryError { constructor(message: string, errorCode: string, options?: MemoryErrorOptions); } /** Raised when input validation fails (400, 409, 422). */ declare class ValidationError extends MemoryError { constructor(message: string, errorCode: string, options?: MemoryErrorOptions); } /** Raised when a memory is not found (404). */ declare class MemoryNotFoundError extends MemoryError { constructor(message: string, errorCode: string, options?: MemoryErrorOptions); } /** Raised when network connectivity issues occur (408, 502, 503, 504). */ declare class NetworkError extends MemoryError { constructor(message: string, errorCode: string, options?: MemoryErrorOptions); } /** Raised when client configuration is invalid. */ declare class ConfigurationError extends MemoryError { constructor(message: string, errorCode: string, options?: MemoryErrorOptions); } /** Raised when memory quota is exceeded (413). */ declare class MemoryQuotaExceededError extends MemoryError { constructor(message: string, errorCode: string, options?: MemoryErrorOptions); } /** * Create an appropriate exception based on HTTP response status code. * * @param statusCode - HTTP status code from the response * @param responseText - Response body text * @param options - Additional error context (details, debugInfo) * @returns An instance of the appropriate MemoryError subclass */ declare function createExceptionFromResponse(statusCode: number, responseText: string, options?: Omit<MemoryErrorOptions, "suggestion">): MemoryError; export { type AddMemoryOptions, type AllUsers, AuthenticationError, ConfigurationError, type CreateMemoryExportPayload, type DeleteAllMemoryOptions, Feedback, type FeedbackPayload, type GetAllMemoryOptions, type GetMemoryExportPayload, type Memory, MemoryClient, MemoryError, type MemoryErrorOptions, type MemoryHistory, MemoryNotFoundError, MemoryQuotaExceededError, type MemoryUpdateBody, type Message, type Messages, NetworkError, type ProjectOptions, type ProjectResponse, type PromptUpdatePayload, RateLimitError, type SearchMemoryOptions, type User, ValidationError, type Webhook, type WebhookCreatePayload, WebhookEvent, type WebhookUpdatePayload, createExceptionFromResponse, MemoryClient as default };