/**
 * Rate Limiter for Crewly Agent API Calls
 *
 * Provides token-bucket rate limiting, message coalescing, and 429 retry
 * logic to prevent Gemini API quota exhaustion when multiple agents are
 * actively generating messages.
 *
 * @module services/agent/crewly-agent/rate-limiter
 */
/**
 * Configuration for the rate limiter.
 */
export interface RateLimiterConfig {
    /** Maximum requests allowed per window */
    maxRequestsPerWindow: number;
    /** Time window in milliseconds */
    windowMs: number;
    /** Maximum retry attempts for 429 errors */
    maxRetries: number;
    /** Initial backoff delay in milliseconds for 429 retries */
    initialBackoffMs: number;
    /** Backoff multiplier for exponential retry */
    backoffMultiplier: number;
    /** Maximum backoff delay in milliseconds */
    maxBackoffMs: number;
    /** Coalescing window — how long to wait for additional messages before processing (ms) */
    coalesceWindowMs: number;
}
/**
 * Default rate limiter configuration.
 */
export declare const RATE_LIMITER_DEFAULTS: RateLimiterConfig;
/**
 * Token-bucket rate limiter with message coalescing and 429 retry.
 *
 * Features:
 * - **Rate limiting**: Enforces max requests per time window using a sliding window.
 * - **Message coalescing**: Groups messages arriving within a short window into one.
 * - **429 retry**: Wraps API calls with exponential backoff on quota errors.
 *
 * @example
 * ```typescript
 * const limiter = new RateLimiter<AgentRunResult>();
 * const result = await limiter.enqueue('Check status', undefined, async (msg) => {
 *   return agentRunner.run(msg);
 * });
 * ```
 */
export declare class RateLimiter<T> {
    private config;
    private requestTimestamps;
    private queue;
    private processing;
    private coalesceTimer;
    /**
     * Create a new RateLimiter instance.
     *
     * @param config - Optional partial config overrides
     */
    constructor(config?: Partial<RateLimiterConfig>);
    /**
     * Enqueue a message for rate-limited processing.
     *
     * If multiple messages arrive within the coalescing window, they are
     * merged into a single request to reduce API call count.
     *
     * @param message - Message to process
     * @param metadata - Optional metadata
     * @param handler - Async function that performs the actual API call
     * @returns Result from the handler
     */
    enqueue(message: string, metadata: Record<string, string> | undefined, handler: (message: string, metadata?: Record<string, string>) => Promise<T>): Promise<T>;
    /**
     * Get the current number of messages in the queue.
     *
     * @returns Queue length
     */
    getQueueLength(): number;
    /**
     * Check if the rate limiter is currently processing.
     *
     * @returns True if processing
     */
    isProcessing(): boolean;
    /**
     * Get the number of requests made in the current window.
     *
     * @returns Request count in current window
     */
    getRequestCountInWindow(): number;
    /**
     * Get the current configuration.
     *
     * @returns Rate limiter configuration
     */
    getConfig(): RateLimiterConfig;
    /**
     * Reset internal state (for testing).
     */
    reset(): void;
    /**
     * Process the queue: coalesce messages, enforce rate limit, execute with retry.
     *
     * @param handler - The actual API call handler
     */
    private processQueue;
    /**
     * Wait until we have capacity in the rate limit window.
     */
    private waitForCapacity;
    /**
     * Remove timestamps older than the current window.
     */
    private pruneOldTimestamps;
    /**
     * Coalesce multiple queued messages into a single message.
     *
     * If there's only one message, it passes through unchanged.
     * Multiple messages are joined with a separator and a header.
     *
     * @param batch - Array of queued messages to coalesce
     * @returns Single coalesced message and metadata from the most recent item
     */
    private coalesceMessages;
    /**
     * Execute the handler with exponential backoff retry on 429/quota errors.
     *
     * @param message - Message to send
     * @param metadata - Optional metadata
     * @param handler - The API call handler
     * @returns Handler result
     * @throws Error after max retries exhausted
     */
    private executeWithRetry;
    /**
     * Check if an error is retryable (429 or quota-related).
     *
     * @param error - The error to check
     * @returns True if the error should be retried
     */
    private isRetryableError;
}
//# sourceMappingURL=rate-limiter.d.ts.map