/** * Utility functions for @compilr-dev/agents */ export { countTokens, countMessageTokens } from './tokenizer.js'; /** * Generate a unique ID for tool uses */ export declare function generateId(): string; /** * Sleep for a specified duration, respecting AbortSignal * * @param ms - Duration in milliseconds * @param signal - Optional AbortSignal to cancel sleep * @throws Error if signal is aborted */ export declare function sleep(ms: number, signal?: AbortSignal): Promise; /** * Configuration for LLM retry behavior */ export interface RetryConfig { /** * Enable/disable automatic retry. * @default true */ enabled?: boolean; /** * Maximum number of retry attempts (not including initial attempt). * Total attempts = maxAttempts + 1 * @default 10 */ maxAttempts?: number; /** * Base delay in milliseconds for exponential backoff. * Actual delay = min(baseDelayMs * 2^attempt, maxDelayMs) + jitter * @default 1000 */ baseDelayMs?: number; /** * Maximum delay in milliseconds (cap for exponential growth). * @default 30000 */ maxDelayMs?: number; } /** * Default retry configuration */ export declare const DEFAULT_RETRY_CONFIG: Required; /** * Options for the withRetry function */ export interface WithRetryOptions { /** * Maximum retry attempts (default: 10) */ maxAttempts?: number; /** * Base delay in milliseconds (default: 1000) */ baseDelayMs?: number; /** * Maximum delay cap in milliseconds (default: 30000) */ maxDelayMs?: number; /** * Function to determine if an error is retryable */ isRetryable?: (error: E) => boolean; /** * Callback invoked before each retry attempt */ onRetry?: (attempt: number, maxAttempts: number, error: E, delayMs: number) => void; /** * Callback invoked when all retries are exhausted */ onExhausted?: (attempts: number, error: E) => void; /** * AbortSignal to cancel retries */ signal?: AbortSignal; } /** * Calculate delay with exponential backoff and jitter * * @param attempt - Current attempt number (0-indexed) * @param baseDelayMs - Base delay in milliseconds * @param maxDelayMs - Maximum delay cap in milliseconds * @returns Delay in milliseconds */ export declare function calculateBackoffDelay(attempt: number, baseDelayMs?: number, maxDelayMs?: number): number; /** * Execute an async function with automatic retry on failure * * @param fn - Async function to execute * @param options - Retry options * @returns Result of the function * @throws Last error if all retries exhausted * * @example * ```typescript * const result = await withRetry( * async () => { * const response = await fetch(url); * if (!response.ok) throw new Error(`HTTP ${response.status}`); * return response.json(); * }, * { * maxAttempts: 5, * isRetryable: (error) => error.message.includes('HTTP 5'), * onRetry: (attempt, max, error, delay) => { * console.log(`Retry ${attempt}/${max} in ${delay}ms: ${error.message}`); * }, * } * ); * ``` */ export declare function withRetry(fn: () => Promise, options?: WithRetryOptions): Promise; /** * Create a retryable async generator for streaming responses * * This is specifically designed for streaming LLM responses where we need to * retry the entire stream if it fails partway through. * * @param fn - Function that returns an async iterable * @param options - Retry options * @returns Async generator that retries on failure */ export declare function withRetryGenerator(fn: () => AsyncIterable, options?: WithRetryOptions): AsyncGenerator; /** * @deprecated Use withRetry instead. This function is kept for backward compatibility. */ export declare function retry(fn: () => Promise, options?: { maxRetries?: number; baseDelay?: number; maxDelay?: number; }): Promise; /** * Truncate a string to a maximum length */ export declare function truncate(str: string, maxLength: number, suffix?: string): string;