import {z} from 'zod' export type GetApiIssuesIdResultsArgs = {'Antimetal-Version'?: string | undefined, version?: number | undefined, id: string}; export type GetApiIssuesIdResultsOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const issueResultsDto = z.object({sessionUuid: z.string().nullable(), rootCause: z.object({id: z.string(), incidentOverview: z.string(), rootCauseSummary: z.string(), relevantEvidence: z.array(z.object({title: z.string(), status: z.enum(["relevant", "irrelevant", "inconclusive"]), description: z.string(), confidence: z.enum(["unknown", "unclear", "probable", "likely", "confirmed"]), artifacts: z.array(z.object({documentId: z.string(), title: z.string(), description: z.string(), dataType: z.string()}))})), irrelevantEvidence: z.array(z.object({title: z.string(), status: z.enum(["relevant", "irrelevant", "inconclusive"]), description: z.string(), confidence: z.enum(["unknown", "unclear", "probable", "likely", "confirmed"]), artifacts: z.array(z.object({documentId: z.string(), title: z.string(), description: z.string(), dataType: z.string()}))}))}).nullable(), causalTree: z.object({nodes: z.array(z.object({id: z.string(), type: z.enum(["validated", "needs-context", "ruled-out"]), title: z.string(), description: z.string(), expandedContent: z.string().optional()})), edges: z.array(z.object({id: z.string(), source: z.string(), target: z.string()}))}), causalTreeV2: z.object({nodes: z.array(z.object({id: z.string(), title: z.string(), description: z.string(), confidence: z.enum(["unknown", "unclear", "probable", "likely", "confirmed"]), nodeType: z.enum(["outcome", "cause", "confounder", "mediator"]), evidence: z.array(z.object({documentId: z.string(), title: z.string().min(1), description: z.string().min(1), dataType: z.string()}))})), edges: z.array(z.object({from: z.string(), to: z.string(), confidence: z.number().gte(0).lte(1)}))}), remediation: z.object({actions: z.array(z.object({id: z.string(), uuid: z.string(), title: z.string(), description: z.string().optional(), category: z.enum(["immediate", "root_cause", "preventative"]).optional(), steps: z.array(z.union([z.object({id: z.string(), title: z.string(), helpMarkdown: z.string().optional(), required: z.boolean(), label: z.string().optional(), type: z.literal("info"), content: z.string()}), z.object({id: z.string(), title: z.string(), helpMarkdown: z.string().optional(), required: z.boolean(), label: z.string().optional(), type: z.literal("code"), files: z.array(z.object({filename: z.string(), language: z.string().optional(), content: z.string()})), activeFilename: z.string().optional()}), z.object({id: z.string(), title: z.string(), helpMarkdown: z.string().optional(), required: z.boolean(), label: z.string().optional(), type: z.literal("cli"), command: z.string()})])), markdownPrompt: z.string().optional()}))}), timeline: z.array(z.object({id: z.string(), type: z.enum(["outcome", "cause", "confounder", "mediator"]).nullable(), timestamp: z.string().nullable(), title: z.string(), description: z.string(), evidence: z.array(z.object({artifactId: z.string().nullable(), timestamp: z.string().nullable(), title: z.string(), description: z.string()}))}))}); 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 }; /** * Fetch investigation results by ID (UUID) * * GET /issues/{id}/results */ export const getApiIssuesIdResults = async (args: GetApiIssuesIdResultsArgs, options?: GetApiIssuesIdResultsOptions) => { const {baseUrl = "https://bff.antimetal.com/api/v2", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['Antimetal-Version'] != null) headers.set("Antimetal-Version", String(args['Antimetal-Version'])) if (args.version != null) query.append("version", String(args.version)) if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/issues/${encodeURIComponent(String(args.id))}/results${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(issueResultsDto, await res.json()) };