import {z} from 'zod' export type ProbeArtifacts = {kind?: ('repair' | 'feature.implement.authored') | undefined, probe_md: string, meta_json: string, mutant_diff?: (string | null) | undefined, sentinel_py?: (string | null) | undefined, feature_contract?: (string | null) | undefined, oracle_test_diff?: (string | null) | undefined, source_context?: (string | null) | undefined, validation_evidence?: (string | null) | undefined}; export type ProbeStatus = 'pending-review' | 'approved' | 'rejected'; export type CreateProbeBody = {id: string, site: string, slot: string, subsystem: string, mint_run: string, artifacts: ProbeArtifacts, status?: (ProbeStatus | null) | undefined, created_at?: (string | null) | undefined}; export type CreateApiProbesArgs = {authorization?: (string | null) | undefined, body: CreateProbeBody}; export type CreateApiProbesOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const probeStatus = z.enum(["pending-review", "approved", "rejected"]); export const probeComment = z.object({author: z.string(), verdict: z.enum(["approved", "rejected"]), comment: z.string().nullable(), at: z.string()}); export const probeState = z.object({id: z.string(), site: z.string(), slot: z.string(), subsystem: z.string(), status: probeStatus, mint_run: z.string(), kind: z.enum(["repair", "feature.implement.authored"]).optional(), created_at: z.string(), reviewed_by: z.string().nullable().optional(), reviewed_at: z.string().nullable().optional(), comments: z.array(probeComment).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 Probe * * POST /probes */ export const createApiProbes = async (args: CreateApiProbesArgs, options: CreateApiProbesOptions) => { 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") if (args.authorization != null) headers.set("authorization", String(args.authorization)) const url = `${baseUrl.replace(/\/+$/, '')}/probes${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(probeState, await res.json()) };