/**
 * OpenClaw-inspired Retry Loop with Provider Fallback
 *
 * Implements intelligent retry mechanism with:
 * - Error classification (context overflow, rate limit, auth failure)
 * - Automatic fallback to next provider/profile
 * - Auth profile rotation with cooldown
 * - Context auto-compaction on overflow
 * - Thinking level fallback for unsupported features
 */
import { EventEmitter } from 'events';
import { CompactionConfig } from '../../context/smart-compaction.js';
export interface AuthProfile {
    id: string;
    name: string;
    provider: string;
    apiKey: string;
    baseUrl?: string;
    model?: string;
    priority: number;
    /** Whether this profile is currently locked (rate limited) */
    locked?: boolean;
    /** Timestamp when profile can be used again */
    cooldownUntil?: number;
    /** Number of consecutive failures */
    failureCount?: number;
    /** Last successful usage timestamp */
    lastSuccess?: number;
}
export interface ExecutionConfig {
    /** Maximum retry attempts per profile */
    maxRetries: number;
    /** Base delay between retries in ms */
    baseDelayMs: number;
    /** Maximum delay between retries in ms */
    maxDelayMs: number;
    /** Cooldown duration after rate limit in ms */
    rateLimitCooldownMs: number;
    /** Whether to enable auto-compaction on overflow */
    autoCompact: boolean;
    /** Compaction configuration */
    compactionConfig?: Partial<CompactionConfig>;
    /** Thinking levels to try in order */
    thinkingLevels?: string[];
}
export type ErrorType = 'CONTEXT_OVERFLOW' | 'RATE_LIMIT' | 'AUTH_FAILURE' | 'THINKING_LEVEL' | 'MODEL_UNAVAILABLE' | 'NETWORK_ERROR' | 'TIMEOUT' | 'UNKNOWN';
export interface ClassifiedError {
    type: ErrorType;
    message: string;
    retryable: boolean;
    requiresProfileRotation: boolean;
    requiresCompaction: boolean;
    originalError: Error;
}
export interface ExecutionAttempt<T> {
    profileId: string;
    attempt: number;
    thinkingLevel?: string;
    startTime: number;
    endTime?: number;
    success: boolean;
    result?: T;
    error?: ClassifiedError;
}
export interface ExecutionResult<T> {
    success: boolean;
    result?: T;
    attempts: ExecutionAttempt<T>[];
    finalProfile?: AuthProfile;
    totalDurationMs: number;
    error?: string;
}
export declare function classifyError(error: Error): ClassifiedError;
export declare class RetryFallbackEngine extends EventEmitter {
    private profiles;
    private config;
    private compactionEngine;
    private currentThinkingLevelIndex;
    constructor(profiles: AuthProfile[], config?: Partial<ExecutionConfig>);
    /**
     * Execute a function with retry and fallback logic
     */
    execute<T>(fn: (profile: AuthProfile, thinkingLevel?: string) => Promise<T>, options?: {
        messages?: Array<{
            role: string;
            content: string | null;
        }>;
        onCompact?: (messages: any[]) => void;
    }): Promise<ExecutionResult<T>>;
    /**
     * Get profiles that are available (not locked or past cooldown)
     */
    private getAvailableProfiles;
    /**
     * Lock a profile after failure
     */
    private lockProfile;
    /**
     * Mark a profile as successful
     */
    private markProfileSuccess;
    /**
     * Calculate delay with exponential backoff
     */
    private calculateDelay;
    /**
     * Sleep for specified milliseconds
     */
    private sleep;
    /**
     * Add a new profile
     */
    addProfile(profile: AuthProfile): void;
    /**
     * Remove a profile
     */
    removeProfile(profileId: string): boolean;
    /**
     * Unlock all profiles
     */
    unlockAllProfiles(): void;
    /**
     * Get profile status
     */
    getProfileStatus(): Array<{
        id: string;
        name: string;
        provider: string;
        available: boolean;
        locked: boolean;
        cooldownRemaining?: number;
        failureCount: number;
    }>;
    /**
     * Update configuration
     */
    updateConfig(config: Partial<ExecutionConfig>): void;
}
export declare function getRetryFallbackEngine(profiles?: AuthProfile[], config?: Partial<ExecutionConfig>): RetryFallbackEngine;
export declare function resetRetryFallbackEngine(): void;
/**
 * Create a simple retry wrapper for a function
 */
export declare function withRetry<T>(fn: () => Promise<T>, options?: {
    maxRetries?: number;
    baseDelayMs?: number;
    onRetry?: (attempt: number, error: Error) => void;
}): Promise<T>;
