import {z} from 'zod' export type Mem0Message = {role: string, content: string}; export type Mem0Memory = {id?: (string | null) | undefined, content_text?: (string | null) | undefined, message_list?: (Array | null) | undefined, agent_id?: (string | null) | undefined, user_id?: (string | null) | undefined, app_id?: (string | null) | undefined, run_id?: (string | null) | undefined, metadata?: ({[key: string]: unknown} | null) | undefined, include_preferences?: (string | null) | undefined, exclude_preferences?: (string | null) | undefined, auto_infer?: boolean | undefined, response_format?: (string | null) | undefined, category_definitions?: (Array<{[key: string]: string}> | null) | undefined, processing_instructions?: (string | null) | undefined, read_only?: boolean | undefined, background_processing?: boolean | undefined, timestamp?: (number | null) | undefined, created_at?: (string | null) | undefined, updated_at?: (string | null) | undefined}; export type Mem0DataInput = {memory_list?: Array | undefined}; export type CreateApiSandboxMem0SandboxIdInitializeArgs = {sandbox_id: string, init_default_data?: boolean | undefined, body: Mem0DataInput | null}; export type CreateApiSandboxMem0SandboxIdInitializeOptions = {baseUrl?: string | undefined, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const sandboxStatus = z.enum(["idle", "occupied", "error"]); export const initializeSandboxResponse = z.object({sandbox_id: z.string(), status: sandboxStatus, message: z.string()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Initialize mem0 sandbox with data * * POST /sandbox/mem0/{sandbox_id}/initialize */ export const createApiSandboxMem0SandboxIdInitialize = async (args: CreateApiSandboxMem0SandboxIdInitializeArgs, options?: CreateApiSandboxMem0SandboxIdInitializeOptions) => { const {baseUrl = "https://api.klavis.ai", fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.init_default_data != null) query.append("init_default_data", String(args.init_default_data)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/sandbox/mem0/${encodeURIComponent(String(args.sandbox_id))}/initialize${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(initializeSandboxResponse, await res.json()) };