import {z} from 'zod' export type SortOrder = 'asc' | 'desc'; export type GetApiMemoriesArgs = {user_id?: (string | null) | undefined, agent_id?: (string | null) | undefined, team_id?: (string | null) | undefined, search_content?: (string | null) | undefined, limit?: (number | null) | undefined, page?: (number | null) | undefined, sort_by?: (string | null) | undefined, sort_order?: (SortOrder | null) | undefined, db_id?: (string | null) | undefined, table?: (string | null) | undefined, topics?: (Array | null) | undefined}; export type GetApiMemoriesOptions = {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 paginationInfo = z.object({page: z.number().int().gte(0).optional(), limit: z.number().int().gte(1).optional(), total_pages: z.number().int().gte(0).optional(), total_count: z.number().int().gte(0).optional(), search_time_ms: z.number().gte(0).optional()}); export const paginatedResponseUserMemorySchema = z.object({data: z.array(userMemorySchema), meta: paginationInfo}); 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 }; /** * List Memories * * GET /memories */ export const getApiMemories = async (args: GetApiMemoriesArgs, options: GetApiMemoriesOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.user_id != null) query.append("user_id", String(args.user_id)) if (args.agent_id != null) query.append("agent_id", String(args.agent_id)) if (args.team_id != null) query.append("team_id", String(args.team_id)) if (args.search_content != null) query.append("search_content", String(args.search_content)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.page != null) query.append("page", String(args.page)) if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) if (args.sort_order != null) query.append("sort_order", String(args.sort_order)) if (args.db_id != null) query.append("db_id", String(args.db_id)) if (args.table != null) query.append("table", String(args.table)) for (const item of args.topics ?? []) query.append("topics", String(item)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/memories${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(paginatedResponseUserMemorySchema, await res.json()) };