import {z} from 'zod' export type GetApiApiV2OrgIdAlertsIncidentsArgs = {org_id: string, status?: (string | null) | undefined, limit?: number | undefined, offset?: number | undefined}; export type GetApiApiV2OrgIdAlertsIncidentsOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const incidentAlertGroup = z.object({alert_id: z.string(), alert_name: z.string(), count: z.number().int()}); export const keyType = z.enum(["alert_id", "secondary", "primary"]); export const runError = z.object({code: z.string(), details: z.string().nullable().optional(), reason: z.string(), trigger_type: z.string().nullable().optional()}); export const runMode = z.enum(["fresh", "build_on_previous"]); export const reportFormat = z.enum(["v1", "v2"]); export const runState = z.enum(["running", "waiting_input", "completed", "failed", "cancelled"]); export const runStats = z.object({credits: z.number().nullable().optional(), duration_ms: z.number().int().gte(0).nullable().optional(), ignored_acks: z.number().int().gte(0).nullable().optional(), steered: z.number().int().gte(0).nullable().optional(), steps: z.number().int().gte(0).nullable().optional(), tokens: z.unknown(), tool_calls: z.number().int().gte(0).nullable().optional()}); export const runSubjectType = z.enum(["incident", "alert"]); export const runTrigger = z.enum(["auto_new_incident", "auto_new_alert_type", "auto_reopen", "manual", "reanalyze", "oncall_page", "resume", "legacy"]); export const rcaRun = z.object({cancelled_by: z.string().nullable().optional(), confidence: z.string().nullable().optional(), context_used: z.unknown(), continuations: z.number().int().optional(), deadline_closed_at: z.number().int().nullable().optional(), ended_at: z.number().int().nullable().optional(), error: runError.nullable().optional(), guidance: z.string().nullable().optional(), incident_id: z.string().nullable().optional(), input_deadline_at: z.number().int().nullable().optional(), mode: runMode, oncall_subjects: z.unknown(), outcome: z.string().nullable().optional(), pending_question_id: z.string().nullable().optional(), questions_asked: z.number().int().optional(), report: z.string().nullable().optional(), report_format: reportFormat, result: z.unknown(), run_id: z.string(), session_id: z.string().nullable().optional(), stage: z.string().nullable().optional(), stage_index: z.number().int().nullable().optional(), started_at: z.number().int(), started_by: z.string(), state: runState, stats: runStats.nullable().optional(), subject_id: z.string(), subject_type: runSubjectType, trigger: runTrigger, updated_at: z.number().int()}); export const rcaRunItem = z.object({cancelled_by: z.string().nullable().optional(), confidence: z.string().nullable().optional(), ended_at: z.number().int().nullable().optional(), error_reason: z.string().nullable().optional(), has_record: z.boolean().optional(), mode: runMode, outcome: z.string().nullable().optional(), report_format: reportFormat, run_id: z.string(), started_at: z.number().int(), started_by: z.string(), state: runState, subject_type: runSubjectType, trigger: runTrigger}); export const previousResult = z.object({confidence: z.string().nullable().optional(), ended_at: z.number().int(), outcome: z.string().nullable().optional(), run_id: z.string()}); export const runSummaryFields = z.object({cancelled_by: z.string().nullable().optional(), deadline_at: z.number().int().nullable().optional(), detail: z.string().nullable().optional(), previous: previousResult.nullable().optional(), reusable_steps: z.number().int().gte(0).nullable().optional(), stage: z.string().nullable().optional(), stage_count: z.number().int().gte(0).optional(), stage_index: z.number().int().gte(0).nullable().optional(), steered_by: z.array(z.string()).optional(), steps: z.number().int().gte(0).optional(), tool_calls: z.number().int().gte(0).optional(), unblocked_by: z.string().nullable().optional()}); export const rcaSummary = z.object({current_run: rcaRun.nullable().optional(), history: z.array(rcaRunItem), report_run: rcaRunItem.nullable().optional(), resume_run_id: z.string().nullable().optional(), confidence: z.string().nullable().optional(), outcome: z.string().nullable().optional(), report_format: reportFormat, run_id: z.string(), stale: z.boolean(), started_at: z.number().int(), state: runState, summary: runSummaryFields, updated_at: z.number().int()}).nullable(); export const incidentSeverity = z.enum(["P1", "P2", "P3", "P4"]); export const incidentStatus = z.enum(["open", "acknowledged", "resolved"]); export const edgeType = z.enum(["temporal", "service_dependency"]); export const alertEdge = z.object({edge_type: edgeType, from_node_index: z.number().int().gte(0), to_node_index: z.number().int().gte(0)}); export const alertNode = z.object({alert_count: z.number().int().gte(0), alert_id: z.string(), alert_name: z.string(), first_fired_at: z.number().int(), last_fired_at: z.number().int(), service_name: z.string()}); export const incidentTopology = z.object({edges: z.array(alertEdge), nodes: z.array(alertNode), related_incident_ids: z.array(z.string())}); export const incident = z.object({acknowledged_at: z.number().int().nullable().optional(), acknowledged_by: z.string().nullable().optional(), alert_count: z.number().int(), alert_groups: z.array(incidentAlertGroup).nullable().optional(), assigned_to: z.string().nullable().optional(), created_at: z.number().int(), first_alert_at: z.number().int(), group_values: z.unknown(), id: z.string(), key_type: keyType.optional(), last_alert_at: z.number().int(), muted_by_downtime_id: z.string().nullable().optional(), org_id: z.string(), rca: rcaSummary.nullable().optional(), rca_off_reason: z.string().nullable().optional(), resolved_at: z.number().int().nullable().optional(), severity: incidentSeverity, status: incidentStatus, title: z.string().nullable().optional(), topology_context: incidentTopology.nullable().optional(), updated_at: z.number().int()}); export const listIncidentsResponse = z.object({incidents: z.array(incident), total: z.number().int().gte(0)}); 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 }; /** * List alert incidents * * GET /api/v2/{org_id}/alerts/incidents */ export const getApiApiV2OrgIdAlertsIncidents = async (args: GetApiApiV2OrgIdAlertsIncidentsArgs, options: GetApiApiV2OrgIdAlertsIncidentsOptions) => { const {baseUrl, fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.status != null) query.append("status", String(args.status)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/${encodeURIComponent(String(args.org_id))}/alerts/incidents${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(listIncidentsResponse, await res.json()) };