import {z} from 'zod' export type UserMemoryCreateSchema = {memory: string, user_id?: (string | null) | undefined, topics?: (Array | null) | undefined}; export type PatchApiMemoriesMemoryIdArgs = {memory_id: string, db_id?: (string | null) | undefined, table?: (string | null) | undefined, body: UserMemoryCreateSchema}; export type PatchApiMemoriesMemoryIdOptions = {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 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 }; /** * Update Memory * * PATCH /memories/{memory_id} */ export const patchApiMemoriesMemoryId = async (args: PatchApiMemoriesMemoryIdArgs, options: PatchApiMemoriesMemoryIdOptions) => { 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(/\/+$/, '')}/memories/${encodeURIComponent(String(args.memory_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(userMemorySchema, await res.json()) };