/** * Abstract backend interface and factory. */ import type { Mem0Config } from "../config.js"; import { PlatformBackend } from "./platform.js"; export interface AddOptions { userId?: string; agentId?: string; appId?: string; runId?: string; metadata?: Record; immutable?: boolean; infer?: boolean; expires?: string; categories?: string[]; enableGraph?: boolean; } export interface SearchOptions { userId?: string; agentId?: string; appId?: string; runId?: string; topK?: number; threshold?: number; rerank?: boolean; keyword?: boolean; filters?: Record; fields?: string[]; enableGraph?: boolean; } export interface ListOptions { userId?: string; agentId?: string; appId?: string; runId?: string; page?: number; pageSize?: number; category?: string; after?: string; before?: string; enableGraph?: boolean; } export interface DeleteOptions { all?: boolean; userId?: string; agentId?: string; appId?: string; runId?: string; } export interface EntityIds { userId?: string; agentId?: string; appId?: string; runId?: string; } export interface Backend { add( content?: string, messages?: Record[], opts?: AddOptions, ): Promise>; search( query: string, opts?: SearchOptions, ): Promise[]>; get(memoryId: string): Promise>; listMemories(opts?: ListOptions): Promise[]>; update( memoryId: string, content?: string, metadata?: Record, ): Promise>; delete( memoryId?: string, opts?: DeleteOptions, ): Promise>; deleteEntities(opts: EntityIds): Promise>; ping(): Promise>; status(opts?: { userId?: string; agentId?: string }): Promise< Record >; entities(entityType: string): Promise[]>; listEvents(): Promise[]>; getEvent(eventId: string): Promise>; } export class AuthError extends Error { constructor( message = "Authentication failed. Your API key may be invalid or expired.", ) { super(message); this.name = "AuthError"; } } export class NotFoundError extends Error { constructor(path: string) { super(`Resource not found: ${path}`); this.name = "NotFoundError"; } } export class APIError extends Error { constructor(path: string, detail: string) { super(`Bad request to ${path}: ${detail}`); this.name = "APIError"; } } export function getBackend(config: Mem0Config): Backend { return new PlatformBackend(config.platform); }