import {z} from 'zod' export type BoundingBox = {x0: number, y0: number, x1: number, y1: number, dpi?: number | undefined, class_name?: (string | null) | undefined, text?: (string | null) | undefined, confidence?: (number | null) | undefined}; export type VisualComponentCreateRequest = {type: string, page_number: number, bbox: BoundingBox, bbox_dpi?: number | undefined, label?: (string | null) | undefined}; export type CreateApiApiV0DocumentsDocumentIdVisualComponentsArgs = {document_id: string, body: VisualComponentCreateRequest}; export type CreateApiApiV0DocumentsDocumentIdVisualComponentsOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const visualComponentType = z.enum(["text", "image", "chart", "table", "flowchart", "schematic", "untagged", "merged_table_part", "title_block", "zone_grid", "viewbox", "dimension", "callout", "legend", "footnote"]); export const contentFormat = z.enum(["table_html", "mermaid", "json", "program", "rich_text"]); export const boundingBox = z.object({x0: z.number().int(), y0: z.number().int(), x1: z.number().int(), y1: z.number().int(), dpi: z.number().int().optional(), class_name: z.string().nullable().optional(), text: z.string().nullable().optional(), confidence: z.number().nullable().optional()}); export const componentProcessingGap = z.object({stage: z.string(), cause: z.enum(["out_of_memory", "timed_out", "failed"])}); export const documentVisualComponentGetResponse = z.object({id: z.string(), type: z.string(), kind: visualComponentType, content_format: contentFormat, render_code: z.string().nullable().optional(), confidence: z.number().nullable().optional(), document_id: z.string(), page_number: z.number().int(), sequence_number: z.number().int().nullable().optional(), label: z.string(), bbox: boundingBox, description: z.string(), content: z.string().nullable().optional(), created_at: z.string(), updated_at: z.string(), mask_url: z.string().nullable(), metadata: z.record(z.string(), z.unknown()).nullable(), processing_gaps: z.array(componentProcessingGap).optional(), render_svg: z.string().nullable().optional(), grounding: z.record(z.string(), z.unknown()).nullable().optional(), job_status_id: z.string().nullable().optional(), job_status: z.string().nullable().optional(), needs_reprocessing: z.boolean().optional(), parent_id: z.string().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 }; /** * Create a visual component * * POST /api/v0/documents/{document_id}/visual-components */ export const createApiApiV0DocumentsDocumentIdVisualComponents = async (args: CreateApiApiV0DocumentsDocumentIdVisualComponentsArgs, options: CreateApiApiV0DocumentsDocumentIdVisualComponentsOptions) => { 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/v0/documents/${encodeURIComponent(String(args.document_id))}/visual-components${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(documentVisualComponentGetResponse, await res.json()) };