import {z} from 'zod' export type AlertMetric = 'turn_latency_p50' | 'turn_latency_p95' | 'api_errors' | 'function_errors' | 'call_crashes' | 'call_volume'; export type AlertState = 'ok' | 'alert' | 'no_data' | 'unknown'; export type GetApiV1AlertRulesArgs = {enabled?: boolean | undefined, metric?: AlertMetric | undefined, project_id?: string | undefined, state?: AlertState | undefined}; export type GetApiV1AlertRulesOptions = {baseUrl?: string | undefined, polyApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const alertMetric = z.enum(["turn_latency_p50", "turn_latency_p95", "api_errors", "function_errors", "call_crashes", "call_volume"]); export const alertOperator = z.enum([">", "<", ">=", "<="]); export const windowDuration = z.enum(["5m", "10m", "60m"]); export const alertState = z.enum(["ok", "alert", "no_data", "unknown"]); export const alertRuleResponse = z.object({id: z.string(), name: z.string(), project_id: z.string().nullable().optional(), metric: alertMetric, operator: alertOperator, threshold_value: z.number().int().gte(0), window_duration: windowDuration, enabled: z.boolean(), current_state: alertState.optional(), since: z.string().nullable().optional(), last_evaluated_at: z.string().nullable().optional(), current_value: z.number().int().nullable().optional(), created_at: z.string(), updated_at: z.string()}); export const alertRuleListResponse = z.object({data: z.array(alertRuleResponse)}); 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 rules * * GET /v1/alert-rules */ export const getApiV1AlertRules = async (args: GetApiV1AlertRulesArgs, options?: GetApiV1AlertRulesOptions) => { const {baseUrl = "https://api.us.poly.ai", fetch: fetchFn = fetch, polyApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.enabled != null) query.append("enabled", String(args.enabled)) if (args.metric != null) query.append("metric", String(args.metric)) if (args.project_id != null) query.append("project_id", String(args.project_id)) if (args.state != null) query.append("state", String(args.state)) if (polyApiKey !== undefined) headers.set("X-API-KEY", polyApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/alert-rules${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(alertRuleListResponse, await res.json()) };