import {z} from 'zod' export type CreateApiApiV2IncidentsIncidentIdAiPostmortemArgs = {incident_id: string}; export type CreateApiApiV2IncidentsIncidentIdAiPostmortemOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, appKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const incidentAIPostmortemDataAttributesResponse = z.object({action_items: z.string().optional(), customer_impact: z.string().optional(), executive_summary: z.string().optional(), key_timeline: z.string().optional(), lessons_learned: z.string().optional(), system_overview: z.string().optional()}); export const incidentAIPostmortemResponseType = z.literal("get_incident_ai_postmortem_response"); export const incidentAIPostmortemDataResponse = z.object({attributes: incidentAIPostmortemDataAttributesResponse, id: z.string(), type: incidentAIPostmortemResponseType}); export const incidentAIPostmortemResponse = z.object({data: incidentAIPostmortemDataResponse}); 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 }; /** * Get an AI-generated incident postmortem * * POST /api/v2/incidents/{incident_id}/ai/postmortem * * Also accepts auth: AuthZ */ export const createApiApiV2IncidentsIncidentIdAiPostmortem = async (args: CreateApiApiV2IncidentsIncidentIdAiPostmortemArgs, options?: CreateApiApiV2IncidentsIncidentIdAiPostmortemOptions) => { const {baseUrl = "https://api.datadoghq.com", fetch: fetchFn = fetch, apiKeyAuth, appKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (apiKeyAuth !== undefined) headers.set("DD-API-KEY", apiKeyAuth) if (appKeyAuth !== undefined) headers.set("DD-APPLICATION-KEY", appKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/incidents/${encodeURIComponent(String(args.incident_id))}/ai/postmortem${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(incidentAIPostmortemResponse, await res.json()) };