import {z} from 'zod' export type FileChunk = {type?: 'file' | undefined, file_id: string}; export type DocumentURLChunk = {type?: 'document_url' | undefined, document_url: string, document_name?: (string | null) | undefined}; export type ImageDetail = 'low' | 'auto' | 'high'; export type ImageURL = {url: string, detail?: (ImageDetail | null) | undefined}; export type ImageURLChunk = {type?: 'image_url' | undefined, image_url: ImageURL | string}; export type ResponseFormats = 'text' | 'json_object' | 'json_schema'; export type JsonSchema = {name: string, description?: (string | null) | undefined, schema: {[key: string]: unknown}, strict?: boolean | undefined}; export type ResponseFormat = {type?: ResponseFormats | undefined, json_schema?: (JsonSchema | null) | undefined}; export type OCRRequest = {model: string | null, document: FileChunk | DocumentURLChunk | ImageURLChunk, pages?: (string | Array | null) | undefined, include_image_base64?: (boolean | null) | undefined, image_limit?: (number | null) | undefined, image_min_size?: (number | null) | undefined, bbox_annotation_format?: (ResponseFormat | null) | undefined, document_annotation_format?: (ResponseFormat | null) | undefined, document_annotation_prompt?: (string | null) | undefined, table_format?: ('markdown' | 'html' | null) | undefined, extract_header?: boolean | undefined, extract_footer?: boolean | undefined, include_blocks?: boolean | undefined, confidence_scores_granularity?: ('word' | 'page' | 'block' | null) | undefined}; export type CreateApiV1OcrArgs = {body: OCRRequest}; export type CreateApiV1OcrOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const oCRImageObject = z.object({id: z.string(), top_left_x: z.number().int().gte(0).nullable(), top_left_y: z.number().int().gte(0).nullable(), bottom_right_x: z.number().int().gte(0).nullable(), bottom_right_y: z.number().int().gte(0).nullable(), image_base64: z.string().nullable().optional(), image_annotation: z.string().nullable().optional()}); export const oCRConfidenceScore = z.object({text: z.string(), confidence: z.number().gte(0).lte(1), start_index: z.number().int().gte(0)}); export const oCRTableObject = z.object({id: z.string(), content: z.string(), format: z.enum(["markdown", "html"]), word_confidence_scores: z.array(oCRConfidenceScore).nullable().optional()}); export const oCRPageDimensions = z.object({dpi: z.number().int().gte(0), height: z.number().int().gte(0), width: z.number().int().gte(0)}); export const oCRPageConfidenceScores = z.object({word_confidence_scores: z.array(oCRConfidenceScore).optional(), average_page_confidence_score: z.number().gte(0).lte(1), minimum_page_confidence_score: z.number().gte(0).lte(1)}); export const oCRBlockConfidenceScores = z.object({average_content_confidence_score: z.number().gte(0).lte(1).nullable().optional(), minimum_content_confidence_score: z.number().gte(0).lte(1).nullable().optional(), block_type_confidence_score: z.number().gte(0).lte(1).nullable().optional()}); export const oCRTextBlock = z.object({top_left_x: z.number().int().gte(0), top_left_y: z.number().int().gte(0), bottom_right_x: z.number().int().gte(0), bottom_right_y: z.number().int().gte(0), content: z.string(), confidence_scores: oCRBlockConfidenceScores.nullable().optional(), type: z.literal("text").optional()}); export const oCRListBlock = z.object({top_left_x: z.number().int().gte(0), top_left_y: z.number().int().gte(0), bottom_right_x: z.number().int().gte(0), bottom_right_y: z.number().int().gte(0), content: z.string(), confidence_scores: oCRBlockConfidenceScores.nullable().optional(), type: z.literal("list").optional()}); export const oCRImageBlock = z.object({top_left_x: z.number().int().gte(0), top_left_y: z.number().int().gte(0), bottom_right_x: z.number().int().gte(0), bottom_right_y: z.number().int().gte(0), content: z.string(), confidence_scores: oCRBlockConfidenceScores.nullable().optional(), type: z.literal("image").optional(), image_id: z.string()}); export const oCRTableBlock = z.object({top_left_x: z.number().int().gte(0), top_left_y: z.number().int().gte(0), bottom_right_x: z.number().int().gte(0), bottom_right_y: z.number().int().gte(0), content: z.string(), confidence_scores: oCRBlockConfidenceScores.nullable().optional(), type: z.literal("table").optional(), table_id: z.string().nullable().optional()}); export const oCRTitleBlock = z.object({top_left_x: z.number().int().gte(0), top_left_y: z.number().int().gte(0), bottom_right_x: z.number().int().gte(0), bottom_right_y: z.number().int().gte(0), content: z.string(), confidence_scores: oCRBlockConfidenceScores.nullable().optional(), type: z.literal("title").optional()}); export const oCREquationBlock = z.object({top_left_x: z.number().int().gte(0), top_left_y: z.number().int().gte(0), bottom_right_x: z.number().int().gte(0), bottom_right_y: z.number().int().gte(0), content: z.string(), confidence_scores: oCRBlockConfidenceScores.nullable().optional(), type: z.literal("equation").optional()}); export const oCRCaptionBlock = z.object({top_left_x: z.number().int().gte(0), top_left_y: z.number().int().gte(0), bottom_right_x: z.number().int().gte(0), bottom_right_y: z.number().int().gte(0), content: z.string(), confidence_scores: oCRBlockConfidenceScores.nullable().optional(), type: z.literal("caption").optional()}); export const oCRCodeBlock = z.object({top_left_x: z.number().int().gte(0), top_left_y: z.number().int().gte(0), bottom_right_x: z.number().int().gte(0), bottom_right_y: z.number().int().gte(0), content: z.string(), confidence_scores: oCRBlockConfidenceScores.nullable().optional(), type: z.literal("code").optional()}); export const oCRReferencesBlock = z.object({top_left_x: z.number().int().gte(0), top_left_y: z.number().int().gte(0), bottom_right_x: z.number().int().gte(0), bottom_right_y: z.number().int().gte(0), content: z.string(), confidence_scores: oCRBlockConfidenceScores.nullable().optional(), type: z.literal("references").optional()}); export const oCRAsideTextBlock = z.object({top_left_x: z.number().int().gte(0), top_left_y: z.number().int().gte(0), bottom_right_x: z.number().int().gte(0), bottom_right_y: z.number().int().gte(0), content: z.string(), confidence_scores: oCRBlockConfidenceScores.nullable().optional(), type: z.literal("aside_text").optional()}); export const oCRHeaderBlock = z.object({top_left_x: z.number().int().gte(0), top_left_y: z.number().int().gte(0), bottom_right_x: z.number().int().gte(0), bottom_right_y: z.number().int().gte(0), content: z.string(), confidence_scores: oCRBlockConfidenceScores.nullable().optional(), type: z.literal("header").optional()}); export const oCRFooterBlock = z.object({top_left_x: z.number().int().gte(0), top_left_y: z.number().int().gte(0), bottom_right_x: z.number().int().gte(0), bottom_right_y: z.number().int().gte(0), content: z.string(), confidence_scores: oCRBlockConfidenceScores.nullable().optional(), type: z.literal("footer").optional()}); export const oCRSignatureBlock = z.object({top_left_x: z.number().int().gte(0), top_left_y: z.number().int().gte(0), bottom_right_x: z.number().int().gte(0), bottom_right_y: z.number().int().gte(0), content: z.string(), confidence_scores: oCRBlockConfidenceScores.nullable().optional(), type: z.literal("signature").optional()}); export const oCRPageObject = z.object({index: z.number().int().gte(0), markdown: z.string(), images: z.array(oCRImageObject), tables: z.array(oCRTableObject).optional(), hyperlinks: z.array(z.string()).optional(), header: z.string().nullable().optional(), footer: z.string().nullable().optional(), dimensions: oCRPageDimensions.nullable(), confidence_scores: oCRPageConfidenceScores.nullable().optional(), blocks: z.array(z.discriminatedUnion("type", [oCRTextBlock, oCRListBlock, oCRImageBlock, oCRTableBlock, oCRTitleBlock, oCREquationBlock, oCRCaptionBlock, oCRCodeBlock, oCRReferencesBlock, oCRAsideTextBlock, oCRHeaderBlock, oCRFooterBlock, oCRSignatureBlock])).nullable().optional()}); export const oCRUsageInfo = z.object({pages_processed: z.number().int().gte(0), doc_size_bytes: z.number().int().nullable().optional()}); export const oCRResponse = z.object({pages: z.array(oCRPageObject), model: z.string(), document_annotation: z.string().nullable().optional(), usage_info: oCRUsageInfo}); 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 }; /** * OCR * * POST /v1/ocr */ export const createApiV1Ocr = async (args: CreateApiV1OcrArgs, options?: CreateApiV1OcrOptions) => { const {baseUrl = "https://api.mistral.ai", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKey !== undefined) headers.set('authorization', `Bearer ${ApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/ocr${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(oCRResponse, await res.json()) };