import {z} from 'zod' export type RerankOptions = {prune?: (boolean | null) | undefined, dedup?: (boolean | null) | undefined}; export type Budget = 'low' | 'mid' | 'high'; export type EntityIncludeOptions = {max_tokens?: number | undefined}; export type ChunkIncludeOptions = {max_tokens?: number | undefined}; export type SourceFactsIncludeOptions = {max_tokens?: number | undefined, max_tokens_per_observation?: number | undefined}; export type IncludeOptions = {entities?: (EntityIncludeOptions | null) | undefined, chunks?: (ChunkIncludeOptions | null) | undefined, source_facts?: (SourceFactsIncludeOptions | null) | undefined}; export type TagGroupLeaf = {tags: Array, match?: ('any' | 'all' | 'any_strict' | 'all_strict' | 'exact') | undefined, resolve?: ('exact' | 'fuzzy') | undefined}; export type TagGroupNotInput = {not: TagGroupLeaf | TagGroupAndInput | TagGroupOrInput | TagGroupNotInput}; export type TagGroupOrInput = {or: Array}; export type TagGroupAndInput = {and: Array}; export type MinScores = {semantic?: (number | null) | undefined, keyword?: (number | null) | undefined, reranker?: (number | null) | undefined, final?: (number | null) | undefined}; export type TemporalWindow = {start: string, end: string}; export type RecallRequest = {query: string, types?: (Array | null) | undefined, prefer_observations?: (boolean | null) | undefined, rerank?: (RerankOptions | null) | undefined, budget?: Budget | undefined, max_tokens?: number | undefined, trace?: boolean | undefined, query_timestamp?: (string | null) | undefined, include?: IncludeOptions | undefined, tags?: (Array | null) | undefined, tags_match?: ('any' | 'all' | 'any_strict' | 'all_strict' | 'exact') | undefined, tag_groups?: (Array | null) | undefined, min_scores?: (MinScores | null) | undefined, temporal_window?: (TemporalWindow | null) | undefined}; export type CreateApiV1DefaultBanksBankIdMemoriesRecallArgs = {bank_id: string, authorization?: (string | null) | undefined, body: RecallRequest}; export type CreateApiV1DefaultBanksBankIdMemoriesRecallOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const recallScores = z.object({final: z.number(), reranker: z.number().nullable().optional(), semantic: z.number().nullable().optional(), keyword: z.number().nullable().optional()}); export const chunkAttachment = z.object({id: z.string(), hash: z.string(), kind: z.string(), media_type: z.string(), byte_size: z.number().int(), filename: z.string().nullable().optional(), url: z.string()}); export const recallResult = z.object({id: z.string(), text: z.string(), type: z.string().nullable().optional(), entities: z.array(z.string()).nullable().optional(), context: z.string().nullable().optional(), occurred_start: z.string().nullable().optional(), occurred_end: z.string().nullable().optional(), mentioned_at: z.string().nullable().optional(), document_id: z.string().nullable().optional(), metadata: z.record(z.string(), z.string()).nullable().optional(), chunk_id: z.string().nullable().optional(), tags: z.array(z.string()).nullable().optional(), source_fact_ids: z.array(z.string()).nullable().optional(), scores: recallScores.nullable().optional(), attachments: z.array(chunkAttachment).nullable().optional()}); export const entityObservationResponse = z.object({text: z.string(), mentioned_at: z.string().nullable().optional()}); export const entityStateResponse = z.object({entity_id: z.string(), canonical_name: z.string(), observations: z.array(entityObservationResponse)}); export const chunkData = z.object({id: z.string(), text: z.string(), chunk_index: z.number().int(), truncated: z.boolean().optional(), attachments: z.array(chunkAttachment).nullable().optional()}); export const recallResponse = z.object({results: z.array(recallResult), trace: z.record(z.string(), z.unknown()).nullable().optional(), entities: z.record(z.string(), entityStateResponse).nullable().optional(), chunks: z.record(z.string(), chunkData).nullable().optional(), source_facts: z.record(z.string(), recallResult).nullable().optional(), source_facts_truncated: z.boolean().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 }; /** * Recall memory * * POST /v1/default/banks/{bank_id}/memories/recall */ export const createApiV1DefaultBanksBankIdMemoriesRecall = async (args: CreateApiV1DefaultBanksBankIdMemoriesRecallArgs, options: CreateApiV1DefaultBanksBankIdMemoriesRecallOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.authorization != null) headers.set("authorization", String(args.authorization)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/default/banks/${encodeURIComponent(String(args.bank_id))}/memories/recall${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(recallResponse, await res.json()) };