import {z} from 'zod' export type GetApiV2AlertsAlertIdArgs = {alert_id: string}; export type GetApiV2AlertsAlertIdOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const monitorFilter = z.object({field: z.string(), op: z.enum(["eq", "neq", "in", "not_in", "gt", "gte", "lt", "lte", "between", "contains", "exists", "not_exists"]), values: z.array(z.string())}); export const alertTelemetrySource = z.enum(["ALERT_TELEMETRY_SOURCE_TRACES", "ALERT_TELEMETRY_SOURCE_METRICS"]); export const alertQuery = z.object({metric: z.string(), filters: z.array(monitorFilter).optional(), source: alertTelemetrySource.optional(), operation: z.string().optional()}); export const alertCondition = z.object({comparator: z.enum(["gt", "gte", "lt", "lte", "eq"]), threshold: z.number(), degraded_threshold: z.number().optional(), resolve_threshold: z.number().optional(), window: z.enum(["5m", "15m", "30m", "1h", "4h", "1d"]), interval: z.enum(["5m", "15m", "1h", "1d"]), delay: z.enum(["30s", "60s", "120s", "300s", "600s"]).optional()}); export const alertRun = z.object({at: z.string(), value: z.number(), breached: z.boolean(), has_data: z.boolean(), severity: z.enum(["critical", "degraded"]).optional()}); export const alertDisplay = z.object({visualization: z.enum(["lines", "bars"]).optional(), preview_range: z.enum(["1h", "4h", "1d", "7d"]).optional()}); export const alertScope = z.enum(["ALERT_SCOPE_PROJECT", "ALERT_SCOPE_WORKSPACE"]); export const alert = z.object({alert_id: z.string(), display_name: z.string(), description: z.string(), project_id: z.string(), signal: z.enum(["cost", "latency", "errors", "evals", "custom"]), query: alertQuery, condition: alertCondition, notifier_ids: z.array(z.string()), enabled: z.boolean(), status: z.enum(["ok", "degraded", "triggered"]), last_triggered_at: z.string().nullable().optional(), recent_runs: z.array(alertRun).optional(), created_at: z.string(), updated_at: z.string(), created_by_id: z.string(), updated_by_id: z.string(), display: alertDisplay.nullable().optional(), scope: alertScope}); export const getAlertResponse = z.object({alert: alert}); 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 }; /** * Retrieve an alert * * GET /v2/alerts/{alert_id} */ export const getApiV2AlertsAlertId = async (args: GetApiV2AlertsAlertIdArgs, options?: GetApiV2AlertsAlertIdOptions) => { const {baseUrl = "https://my.orq.ai", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (ApiKey !== undefined) headers.set('authorization', `Bearer ${ApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/alerts/${encodeURIComponent(String(args.alert_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(getAlertResponse, await res.json()) };