import {z} from 'zod' export type GetApiPotsPotIdGraphArgs = {pot_id: string, max_nodes?: number | undefined, include_content?: boolean | undefined}; export type GetApiPotsPotIdGraphOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const graphNode = z.object({id: z.string(), title: z.string().max(80).nullable().optional(), content: z.string().nullable().optional(), pot_score: z.number().gte(0).lte(1), original_pot_score: z.number().nullable().optional(), level: z.string(), node_type: z.string(), document_id: z.string().nullable().optional(), provenance_type: z.string().nullable().optional(), tags: z.array(z.string()).optional(), created_at: z.string().nullable().optional()}); export const graphEdge = z.object({id: z.string(), from_id: z.string(), to_id: z.string(), type: z.string(), weight: z.number().gte(0).lte(1), confidence: z.number().gte(0).lte(1).optional(), rationale: z.string().optional(), created_at: z.string().nullable().optional()}); export const graphDocument = z.object({id: z.string(), filename: z.string(), type: z.string(), facts_count: z.number().int().optional(), created_at: z.string().nullable().optional()}); export const graphStats = z.object({total_nodes: z.number().int().optional(), total_edges: z.number().int().optional(), by_level: z.record(z.string(), z.number().int()).optional(), by_edge_type: z.record(z.string(), z.number().int()).optional(), contradictions_count: z.number().int().optional(), avg_pot_score: z.number().optional(), propagation_applied: z.boolean().optional()}); export const potGraphResponse = z.object({pot_id: z.string(), pot_name: z.string(), nodes: z.array(graphNode), edges: z.array(graphEdge), documents: z.array(graphDocument), stats: graphStats}); 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 }; /** * Get full POT knowledge graph for visualization * * GET /pots/{pot_id}/graph */ export const getApiPotsPotIdGraph = async (args: GetApiPotsPotIdGraphArgs, options: GetApiPotsPotIdGraphOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.max_nodes != null) query.append("max_nodes", String(args.max_nodes)) if (args.include_content != null) query.append("include_content", String(args.include_content)) const url = `${baseUrl.replace(/\/+$/, '')}/pots/${encodeURIComponent(String(args.pot_id))}/graph${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(potGraphResponse, await res.json()) };