import { z } from "zod"; const confidenceEnum = z.enum(["low", "medium", "high"]); const itemSchema = z.object({ id: z.string().min(1), description: z.string().min(1), confidence: confidenceEnum, }); export const reconstructionSchema = z.object({ observations: z.array(itemSchema), reportedClaims: z.array( itemSchema.extend({ attributedTo: z.union([z.string().min(1), z.null()]).optional().nullable(), }) ), assumptions: z.array(itemSchema), entities: z.array(itemSchema), transitions: z.array( itemSchema.extend({ entity: z.string().min(1), previousState: z.string().min(1), currentState: z.string().min(1), explanationStatus: z.string().min(1), }) ), expectedButMissing: z.array(itemSchema), presentButUnexpected: z.array(itemSchema), contradictions: z.array(itemSchema), openUncertainties: z.array(itemSchema), }); export const analyseResponseSchema = z.object({ reconstruction: reconstructionSchema, modelName: z.string(), responseDurationMs: z.number(), validationStatus: z.enum(["valid", "partial", "invalid"]), rawResponse: z.string().optional(), errors: z.array(z.string()).optional(), }); export const healthResponseSchema = z.object({ configPresent: z.boolean(), baseUrl: z.string().nullable(), model: z.string().nullable(), reachable: z.boolean(), error: z.string().nullable(), }); export function parseReconstruction(raw) { if (typeof raw === "string") { try { raw = JSON.parse(raw); } catch { throw new SyntaxError("Model response is not valid JSON"); } } return reconstructionSchema.parse(raw); }