// engine/llm-providers/health-checker.ts — Provider health checking.
// SPEC-083 AC-07
import { createProviderAdapter } from './provider-adapter.js';
/** Cache for health check results with TTL. */
const healthCache = new Map();
/** Default cache TTL in milliseconds (5 minutes). */
const DEFAULT_CACHE_TTL_MS = 5 * 60 * 1000;
/**
 * Check health of a single provider. Results are cached for `cacheTtlMs` milliseconds.
 */
export function checkProviderHealth(definition, cacheTtlMs = DEFAULT_CACHE_TTL_MS) {
    const now = Date.now();
    const cached = healthCache.get(definition.id);
    if (cached && cached.expiry > now) {
        return cached.result;
    }
    const adapter = createProviderAdapter(definition);
    const result = adapter.checkHealth();
    healthCache.set(definition.id, { result, expiry: now + cacheTtlMs });
    return result;
}
/**
 * Check health of all configured providers.
 */
export function checkAllProvidersHealth(definitions, cacheTtlMs) {
    return definitions.map((def) => checkProviderHealth(def, cacheTtlMs));
}
/**
 * Get providers filtered by health status.
 */
export function getHealthyProviders(definitions) {
    return definitions
        .filter((def) => {
        const health = checkProviderHealth(def);
        return health.status === 'healthy';
    })
        .map((def) => def.id);
}
/**
 * Clear the health cache (useful for testing or force-refresh).
 */
export function clearHealthCache() {
    healthCache.clear();
}
//# sourceMappingURL=health-checker.js.map