import {z} from 'zod' export type EdgeType = 'supports' | 'contradicts' | 'resolved_contradiction' | 'refines' | 'derives_from' | 'is_part_of' | 'extends' | 'cites' | 'related_to' | 'supersedes' | 'restates'; export type EdgeCreate = {from_id: string, to_id: string, type: EdgeType, weight: number, rationale?: string | undefined, detected_by?: string | undefined, confidence?: number | undefined}; export type CreateApiPotsPotIdEdgesArgs = {pot_id: string, body: Array}; export type CreateApiPotsPotIdEdgesOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const edgeType = z.enum(["supports", "contradicts", "resolved_contradiction", "refines", "derives_from", "is_part_of", "extends", "cites", "related_to", "supersedes", "restates"]); export const edge = z.object({id: z.string(), pot_id: z.string(), workspace_id: z.string(), from_id: z.string(), to_id: z.string(), type: edgeType, weight: z.number().gte(0).lte(1), rationale: z.string().optional(), detected_by: z.string().optional(), companion_of: z.string().nullable().optional(), episode_id: z.string().nullable().optional(), confidence: z.number().gte(0).lte(1).optional(), judge: z.string().max(200).nullable().optional(), partial: z.boolean().nullable().optional(), shared_figures: z.number().int().gte(0).nullable().optional(), similarity: z.number().gte(0).lte(1).nullable().optional(), validation_status: z.string().optional(), decided_by: z.string().nullable().optional(), decided_at: z.string().nullable().optional(), resolution_type: z.string().nullable().optional(), resolved_by_name: z.string().nullable().optional(), resolved_by_role: z.string().nullable().optional(), resolved_by_contributor_id: z.string().nullable().optional(), resolved_at: z.string().nullable().optional(), resolution_notes: z.string().nullable().optional(), resolution_score_shifts: z.record(z.string(), z.number()).nullable().optional(), kind: z.enum(["express", "tacit", "partial", "hierarchy", "suspension", "extension", "manual"]).nullable().optional(), basis: z.string().nullable().optional(), scope_section: z.string().max(300).nullable().optional(), effective_at: z.string().nullable().optional(), resume_at: z.string().nullable().optional(), applied_at: z.string().nullable().optional(), axiom_conflict: z.array(z.string()).nullable().optional(), axiom_hold: z.enum(["conflict", "unverified"]).nullable().optional(), created_at: z.string().nullable().optional(), updated_at: z.string().nullable().optional()}).and(z.record(z.string(), z.unknown())); export const blockedSupersessionRef = z.object({edge_id: z.string(), from_id: z.string(), to_id: z.string(), validation_status: z.string(), reason: z.string(), from_pot_score: z.number().nullable().optional(), to_pot_score: z.number().nullable().optional(), axiom_conflict: z.array(z.string()).optional()}); export const edgeListResponse = z.object({edges: z.array(edge), total: z.number().int(), pot_id: z.string(), blocked_supersedes: z.array(blockedSupersessionRef).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 edges between facts * * POST /pots/{pot_id}/edges */ export const createApiPotsPotIdEdges = async (args: CreateApiPotsPotIdEdgesArgs, options: CreateApiPotsPotIdEdgesOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/pots/${encodeURIComponent(String(args.pot_id))}/edges${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(edgeListResponse, await res.json()) };