import {z} from 'zod' export type CompositeRetrievalMode = 'routing' | 'full'; export type ReRankerType = 'system_default' | 'llm' | 'cohere' | 'bedrock' | 'score' | 'disabled'; export type ReRankConfig = {top_n?: number | undefined, type?: ReRankerType | undefined}; export type CompositeRetrievalParams = {mode?: CompositeRetrievalMode | undefined, rerank_top_n?: (number | null) | undefined, rerank_config?: ReRankConfig | undefined, query: string}; export type CreateApiApiV1RetrieversRetrieverIdRetrieveArgs = {retriever_id: string, project_id?: (string | null) | undefined, organization_id?: (string | null) | undefined, session?: (string | null) | undefined, body: CompositeRetrievalParams}; export type CreateApiApiV1RetrieversRetrieverIdRetrieveOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const compositeRetrievedTextNode = z.object({id: z.string(), retriever_id: z.string(), retriever_pipeline_name: z.string(), pipeline_id: z.string(), metadata: z.record(z.string(), z.unknown()).optional(), text: z.string(), start_char_idx: z.number().int().nullable(), end_char_idx: z.number().int().nullable()}); export const compositeRetrievedTextNodeWithScore = z.object({node: compositeRetrievedTextNode, score: z.number().nullable().optional(), class_name: z.string().optional()}); export const pageScreenshotMetadata = z.object({page_index: z.number().int().gte(0), file_id: z.string(), image_size: z.number().int().gte(0), metadata: z.record(z.string(), z.unknown()).nullable().optional()}); export const pageScreenshotNodeWithScore = z.object({node: pageScreenshotMetadata, score: z.number(), class_name: z.string().optional()}); export const pageFigureMetadata = z.object({figure_name: z.string(), file_id: z.string(), page_index: z.number().int().gte(0), figure_size: z.number().int().gte(0), is_likely_noise: z.boolean().optional(), confidence: z.number().gte(0).lte(1), metadata: z.record(z.string(), z.unknown()).nullable().optional()}); export const pageFigureNodeWithScore = z.object({node: pageFigureMetadata, score: z.number(), class_name: z.string().optional()}); export const compositeRetrievalResult = z.object({nodes: z.array(compositeRetrievedTextNodeWithScore).optional(), image_nodes: z.array(pageScreenshotNodeWithScore).optional(), page_figure_nodes: z.array(pageFigureNodeWithScore).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 }; /** * Retrieve * * POST /api/v1/retrievers/{retriever_id}/retrieve */ export const createApiApiV1RetrieversRetrieverIdRetrieve = async (args: CreateApiApiV1RetrieversRetrieverIdRetrieveArgs, options: CreateApiApiV1RetrieversRetrieverIdRetrieveOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() const cookies: string[] = [] headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.project_id != null) query.append("project_id", String(args.project_id)) if (args.organization_id != null) query.append("organization_id", String(args.organization_id)) if (args.session != null) cookies.push("session=" + encodeURIComponent(String(args.session))) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) if (cookies.length > 0) headers.set('cookie', cookies.join('; ')) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/retrievers/${encodeURIComponent(String(args.retriever_id))}/retrieve${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(compositeRetrievalResult, await res.json()) };