import {z} from 'zod' export type OptimizeMemoriesRequest = {user_id: string, model?: (string | null) | undefined, apply?: boolean | undefined}; export type CreateApiOptimizeMemoriesArgs = {db_id?: (string | null) | undefined, table?: (string | null) | undefined, body: OptimizeMemoriesRequest}; export type CreateApiOptimizeMemoriesOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const userMemorySchema = z.object({memory_id: z.string(), memory: z.string(), topics: z.array(z.string()).nullable().optional(), agent_id: z.string().nullable().optional(), team_id: z.string().nullable().optional(), user_id: z.string().nullable().optional(), updated_at: z.string().nullable().optional()}); export const optimizeMemoriesResponse = z.object({memories: z.array(userMemorySchema), memories_before: z.number().int().gte(0), memories_after: z.number().int().gte(0), tokens_before: z.number().int().gte(0), tokens_after: z.number().int().gte(0), tokens_saved: z.number().int().gte(0), reduction_percentage: z.number().gte(0).lte(100)}); 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 }; /** * Optimize User Memories * * POST /optimize-memories */ export const createApiOptimizeMemories = async (args: CreateApiOptimizeMemoriesArgs, options: CreateApiOptimizeMemoriesOptions) => { const {baseUrl, 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.db_id != null) query.append("db_id", String(args.db_id)) if (args.table != null) query.append("table", String(args.table)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/optimize-memories${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(optimizeMemoriesResponse, await res.json()) };