import type { Run, RunQuery, TraceStore } from '../types/tracing.js'; /** What an alert measures: error rate, latency percentile, total cost, or run count. */ export type AlertMetric = 'errorRate' | 'latencyP95' | 'latencyP50' | 'cost' | 'count'; /** A condition over recent runs that should fire an alert. */ export interface AlertRule { /** Name reported when it fires. */ name: string; /** What it measures. */ metric: AlertMetric; /** Fires when the measured value crosses this, in the direction implied by the metric. */ threshold: number; /** Window to measure, in milliseconds. Defaults to 15 minutes. */ windowMs?: number; /** Narrows what is measured: one model, one kind of run, one tag. */ filter?: RunQuery; /** Ignores a window with too few runs to mean anything. Defaults to 1. */ minRuns?: number; } /** A rule that fired. */ export interface AlertEvent { /** The rule's name. */ rule: string; /** What it measured. */ metric: AlertMetric; /** The measured value. */ value: number; /** The threshold it crossed. */ threshold: number; /** Runs measured. */ runs: number; /** ISO-8601 start of the window. */ windowStart: string; /** ISO-8601 end of the window. */ windowEnd: string; /** A few runs that contributed, so an alert points at something rather than just firing. */ samples: Array<{ id: string; traceId: string; name: string; }>; } /** Where fired alerts are sent. */ export interface AlertNotifier { /** Sends one alert. */ notify(event: AlertEvent): Promise | void; } /** * Watches traces and reports when something crosses a line. * * Metrics answer "how is it going"; alerts answer "tell me when it stops going well". Evaluating * over stored runs rather than a separate metrics pipeline means every alert carries the runs that * caused it, so the next step is reading them, not starting an investigation from scratch. */ export declare class AlertEvaluator { private readonly store; private readonly rules; private readonly options; constructor(store: TraceStore, rules: AlertRule[], options?: { notifier?: AlertNotifier; now?: () => Date; }); /** Evaluates every rule once. Call it on a timer, or from a scheduled job. */ evaluate(): Promise; } /** Computes a metric over a set of runs. */ export declare function measure(metric: AlertMetric, runs: Run[]): number; /** Options for `createWebhookNotifier()`. */ export interface WebhookNotifierOptions { /** The webhook URL. */ url: string; /** Headers added to each post. */ headers?: Record; /** Replaces the global `fetch`. */ fetch?: typeof globalThis.fetch; /** Builds the payload. Defaults to a shape chat webhooks accept: `{ text }` plus the event. */ body?: (event: AlertEvent) => unknown; } /** Posts alerts to a webhook. The default payload fits the common `{ text }` chat format. */ export declare function createWebhookNotifier(options: WebhookNotifierOptions): AlertNotifier;