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 ClassifyLegendRequest = {id: string, name: string, class: string, subtype?: (string | null) | undefined, source_bbox?: (BoundingBox | null) | undefined, source_node_id?: (string | null) | undefined, source_crop_url?: (string | null) | undefined, confidence?: number | undefined, cfihos_tag_class_code?: (string | null) | undefined, cfihos_equipment_class_code?: (string | null) | undefined, cfihos_version?: (string | null) | undefined, cfihos_source?: ('deterministic_map' | 'name_match' | 'isa_letter_code' | 'llm' | 'template_applied' | 'user_pinned' | null) | undefined, cfihos_confidence?: (number | null) | undefined, transcription?: (string | null) | undefined}; export type CreateApiApiV0SemanticCatalogClassifyLegendEntryArgs = {body: ClassifyLegendRequest}; export type CreateApiApiV0SemanticCatalogClassifyLegendEntryOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; 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 legendEntry = z.object({id: z.string(), name: z.string(), class: z.string(), subtype: z.string().nullable().optional(), source_bbox: boundingBox.nullable().optional(), source_node_id: z.string().nullable().optional(), source_crop_url: z.string().nullable().optional(), confidence: z.number().optional(), cfihos_tag_class_code: z.string().nullable().optional(), cfihos_equipment_class_code: z.string().nullable().optional(), cfihos_version: z.string().nullable().optional(), cfihos_source: z.enum(["deterministic_map", "name_match", "isa_letter_code", "llm", "template_applied", "user_pinned"]).nullable().optional(), cfihos_confidence: z.number().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 }; /** * Deterministic CFIHOS classification of a single legend entry * * POST /api/v0/semantic-catalog/classify-legend-entry */ export const createApiApiV0SemanticCatalogClassifyLegendEntry = async (args: CreateApiApiV0SemanticCatalogClassifyLegendEntryArgs, options: CreateApiApiV0SemanticCatalogClassifyLegendEntryOptions) => { 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/semantic-catalog/classify-legend-entry${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(legendEntry, await res.json()) };