import {z} from 'zod' export type GetApiV1IncidentMemoryIncidentIdArgs = {incident_id: string}; export type GetApiV1IncidentMemoryIncidentIdOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const incidentMemorySummary = z.object({incident_id: z.string(), request_id: z.string().nullable().optional(), endpoint: z.string().nullable().optional(), family_id: z.string().nullable().optional(), fingerprint: z.string(), detected: z.boolean(), type: z.string().nullable().optional(), severity: z.string().nullable().optional(), service: z.string().nullable().optional(), source: z.string().nullable().optional(), start_ts: z.string().nullable().optional(), end_ts: z.string().nullable().optional(), line_count_in: z.number().int().optional(), token_count_in: z.number().int().optional(), token_count_out: z.number().int().optional(), compression_ratio: z.number().nullable().optional(), created_at: z.unknown(), affected_services: z.array(z.string()).optional(), root_cause_hypotheses: z.array(z.record(z.string(), z.unknown())).optional(), evidence_preview: z.array(z.record(z.string(), z.unknown())).optional()}); export const incidentMemoryGraphResponse = z.object({incident: incidentMemorySummary, capsule: z.record(z.string(), z.unknown()), compact_text: z.string().nullable().optional(), summary: z.string().nullable().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 }; /** * Incident Memory Detail Endpoint * * GET /v1/incident-memory/{incident_id} */ export const getApiV1IncidentMemoryIncidentId = async (args: GetApiV1IncidentMemoryIncidentIdArgs, options: GetApiV1IncidentMemoryIncidentIdOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/incident-memory/${encodeURIComponent(String(args.incident_id))}${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(incidentMemoryGraphResponse, await res.json()) };