/**
 * In-memory per-endpoint health/latency cache.
 *
 * The Model Connection Layer probes endpoints when users add or test them in
 * Admin, and later wants to display "this endpoint was healthy 30 seconds ago,
 * latency 120ms" without re-probing on every render. This cache stores the
 * most recent sample per endpoint URL and expires entries after a TTL.
 *
 * Scope is intentionally narrow: single process, no persistence, no aggregation
 * across history. Persistence and trend metrics belong to a separate observer.
 */
const DEFAULT_TTL_MS = 60_000;
export class EndpointHealthCache {
    samples = new Map();
    ttlMs;
    constructor(options = {}) {
        this.ttlMs = options.ttlMs ?? DEFAULT_TTL_MS;
    }
    record(sample) {
        if (!sample.endpoint)
            return;
        this.samples.set(sample.endpoint, sample);
    }
    get(endpoint, now = Date.now()) {
        const sample = this.samples.get(endpoint);
        if (!sample)
            return undefined;
        if (now - sample.observedAt > this.ttlMs) {
            this.samples.delete(endpoint);
            return undefined;
        }
        return sample;
    }
    list(now = Date.now()) {
        const fresh = [];
        for (const sample of this.samples.values()) {
            if (now - sample.observedAt <= this.ttlMs)
                fresh.push(sample);
        }
        return fresh;
    }
    size(now = Date.now()) {
        return this.list(now).length;
    }
    clear() {
        this.samples.clear();
    }
}
/**
 * Build a cache sample from a ProviderProbeResult-shaped object. Probe callers
 * use this to push results into the cache without coupling to the cache type.
 */
export function sampleFromProbeResult(endpoint, result, now = Date.now()) {
    return {
        endpoint,
        ok: result.ok,
        latencyMs: result.latencyMs,
        status: result.status,
        detail: result.detail,
        credentialSource: result.credentialSource,
        observedAt: now,
    };
}
//# sourceMappingURL=endpoint-health-cache.js.map