import {z} from 'zod' export type CloudFile = {provider: string, fileId: string, fileName: string}; export type ParseMethod = 'default' | 'pypdf' | 'pdfplumber' | 'unstructured' | 'pymupdf' | 'deepdoc'; export type ChunkStrategy = 'recursive' | 'fixed_size' | 'markdown'; export type CloudIngestRequest = {files: Array, collection: string, parse_method?: (ParseMethod | null) | undefined, chunk_strategy?: (ChunkStrategy | null) | undefined, chunk_size?: (number | null) | undefined, chunk_overlap?: (number | null) | undefined, separators?: (Array | null) | undefined, embedding_model_id?: string | undefined, embedding_batch_size?: (number | null) | undefined, max_retries?: (number | null) | undefined, retry_delay?: (number | null) | undefined}; export type CreateApiApiKbIngestCloudArgs = {body: CloudIngestRequest}; export type CreateApiApiKbIngestCloudOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const ingestionStepResult = z.object({name: z.string(), metadata: z.record(z.string(), z.unknown()).optional()}); export const ingestionResult = z.object({status: z.string(), doc_id: z.string().nullable().optional(), parse_hash: z.string().nullable().optional(), chunk_count: z.number().int().gte(0).optional(), embedding_count: z.number().int().gte(0).optional(), vector_count: z.number().int().gte(0).optional(), completed_steps: z.array(ingestionStepResult).optional(), failed_step: z.string().nullable().optional(), message: z.string(), warnings: z.array(z.string()).optional(), file_id: z.string().nullable().optional()}); export const createApiApiKbIngestCloudResponse = z.array(ingestionResult); 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 }; /** * Ingest Cloud * * POST /api/kb/ingest-cloud */ export const createApiApiKbIngestCloud = async (args: CreateApiApiKbIngestCloudArgs, options: CreateApiApiKbIngestCloudOptions) => { 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 (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/kb/ingest-cloud${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(createApiApiKbIngestCloudResponse, await res.json()) };