import {z} from 'zod' export type SearchQuery = {query: string, limit?: (number | null) | undefined, filter_tags?: (Array | null) | undefined, filter_source?: (string | null) | undefined, min_importance?: (number | null) | undefined, space_id?: (string | null) | undefined}; export type CreateApiMemoriesSearchArgs = {space_id?: (string | null) | undefined, body: SearchQuery}; export type CreateApiMemoriesSearchOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const memory = z.object({content: z.string().max(1048576), tags: z.array(z.string()).nullable().optional(), source: z.string().nullable().optional(), importance: z.number().int().gte(1).lte(10).nullable().optional(), category: z.string().nullable().optional(), metadata: z.record(z.string(), z.unknown()).nullable().optional(), id: z.string(), created_at: z.string(), updated_at: z.string().nullable().optional(), created_by_user: z.string(), space_id: z.string().nullable().optional(), space_name: z.string().nullable().optional(), tenant_id: z.string().nullable().optional(), full_content: z.string().nullable().optional(), is_chunk: z.boolean().optional(), parent_memory_id: z.string().nullable().optional(), chunk_index: z.number().int().nullable().optional(), total_chunks: z.number().int().nullable().optional(), chunk_metadata: z.record(z.string(), z.unknown()).nullable().optional()}); export const createApiMemoriesSearchResponse = z.array(memory); 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 }; /** * Search memories * * POST /memories/search */ export const createApiMemoriesSearch = async (args: CreateApiMemoriesSearchArgs, options: CreateApiMemoriesSearchOptions) => { 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.space_id != null) query.append("space_id", String(args.space_id)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/memories/search${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(createApiMemoriesSearchResponse, await res.json()) };