/**
 * Key Pool — commercial-grade multi-key load balancing per provider.
 *
 * Supports: weighted-round-robin, least-busy, random strategies.
 * Features: RPM/TPM sliding window, failure cooldown, health detection, auto-failover.
 *
 * Runtime state (inFlight, counters, health) is in-memory only — resets on restart.
 * Persistent config (keys, weights, strategy) is managed by the caller (settings.json).
 */
export type LoadBalanceStrategy = "weighted-round-robin" | "least-busy" | "random";
export type KeyHealthStatus = "healthy" | "degraded" | "cooldown" | "disabled";
/** Persistent key config (saved to settings.json). */
export interface KeyConfig {
    id: string;
    key: string;
    label?: string;
    weight: number;
    enabled: boolean;
}
/** Persistent provider pool config (saved to settings.json). */
export interface ProviderPoolConfig {
    providerId: string;
    baseUrl?: string;
    strategy: LoadBalanceStrategy;
    keys: KeyConfig[];
    rateLimit?: {
        rpm?: number;
        tpm?: number;
    };
}
/** Acquired key handle — caller must call release() when done. */
export interface KeyHandle {
    keyId: string;
    apiKey: string;
    providerId: string;
    release: (result: KeyReleaseResult) => void;
}
export interface KeyReleaseResult {
    success: boolean;
    tokens?: number;
    errorCode?: number;
    retryAfter?: number;
}
/** Full status for a single key (config + runtime). */
export interface KeyStatus extends KeyConfig {
    inFlight: number;
    totalRequests: number;
    totalTokens: number;
    lastUsedAt: number;
    lastErrorAt: number;
    consecutiveErrors: number;
    cooldownUntil: number;
    healthStatus: KeyHealthStatus;
}
/** Full status for a provider pool. */
export interface PoolStatus {
    providerId: string;
    baseUrl?: string;
    strategy: LoadBalanceStrategy;
    rateLimit?: {
        rpm?: number;
        tpm?: number;
    };
    keys: KeyStatus[];
}
export declare class KeyPool {
    private pools;
    private runtimeStates;
    constructor(configs?: ProviderPoolConfig[]);
    /**
     * Acquire the next available key for a provider.
     * Returns a KeyHandle with release() that MUST be called when the request completes.
     * Returns null if no healthy key is available.
     */
    acquireKey(providerId: string): KeyHandle | null;
    /**
     * Release a key after request completion. Updates health/stats.
     */
    private releaseKey;
    addProvider(providerId: string, opts?: {
        baseUrl?: string;
        strategy?: LoadBalanceStrategy;
        rateLimit?: {
            rpm?: number;
            tpm?: number;
        };
    }): void;
    removeProvider(providerId: string): void;
    addKey(providerId: string, key: string, opts?: {
        id?: string;
        label?: string;
        weight?: number;
        enabled?: boolean;
    }): string;
    removeKey(keyId: string): boolean;
    getKeyById(keyId: string): string | null;
    updateKey(keyId: string, updates: Partial<Pick<KeyConfig, "label" | "weight" | "enabled">>): boolean;
    /** Explicitly set a key's health status (e.g. after background validation). */
    setKeyHealth(keyId: string, status: KeyHealthStatus): void;
    setStrategy(providerId: string, strategy: LoadBalanceStrategy): void;
    setRateLimit(providerId: string, rateLimit: {
        rpm?: number;
        tpm?: number;
    }): void;
    getPoolStatus(providerId: string): PoolStatus | null;
    getAllStatus(): PoolStatus[];
    getProviderIds(): string[];
    hasProvider(providerId: string): boolean;
    hasAvailableKey(providerId: string): boolean;
    /** Export persistent config (for saving to settings.json). */
    exportConfig(): ProviderPoolConfig[];
    /** Reload from config (e.g., after settings.json changes on disk). */
    reloadConfig(configs: ProviderPoolConfig[]): void;
    private createInitialState;
    private getHealthyCandidates;
    private selectByStrategy;
    private weightedRandom;
    private leastBusy;
    private pruneWindow;
    private pruneTokenWindow;
    private buildPoolStatus;
}
