import {z} from 'zod' export type IncidentMemorySearchRequest = {q?: (string | null) | undefined, incident_id?: (string | null) | undefined, fingerprint?: (string | null) | undefined, family_id?: (string | null) | undefined, service?: (string | null) | undefined, source?: (string | null) | undefined, incident_type?: (string | null) | undefined, severity?: (string | null) | undefined, template_id?: (string | null) | undefined, role?: (string | null) | undefined, detected?: (boolean | null) | undefined, limit?: number | undefined}; export type CreateApiV1IncidentMemorySearchArgs = {body: IncidentMemorySearchRequest}; export type CreateApiV1IncidentMemorySearchOptions = {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 incidentMemorySearchResponse = z.object({incidents: z.array(incidentMemorySummary)}); 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 Search Endpoint * * POST /v1/incident-memory/search */ export const createApiV1IncidentMemorySearch = async (args: CreateApiV1IncidentMemorySearchArgs, options: CreateApiV1IncidentMemorySearchOptions) => { 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(/\/+$/, '')}/v1/incident-memory/search${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(incidentMemorySearchResponse, await res.json()) };