import {z} from 'zod' export type DocumentRerunRequest = {doc_ids: Array, override_extraction?: boolean | undefined}; export type CreateApiV1RerunDocumentsArgs = {body: DocumentRerunRequest}; export type CreateApiV1RerunDocumentsOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const batchFileErrorResponse = z.object({error_code: z.string(), error_type: z.string(), id_type: z.string().nullable().optional(), id: z.union([z.number().int(), z.string()]).nullable().optional(), message: z.string()}); export const documentRerunBatchResponse = z.object({rerun: z.number().int(), not_rerun: z.number().int(), rerun_doc_ids: z.array(z.number().int()).optional(), errors: z.array(batchFileErrorResponse).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 }; /** * Rerun Documents * * POST /v1/rerun_documents/ */ export const createApiV1RerunDocuments = async (args: CreateApiV1RerunDocumentsArgs, options: CreateApiV1RerunDocumentsOptions) => { 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(/\/+$/, '')}/v1/rerun_documents/${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(documentRerunBatchResponse, await res.json()) };