/** * Concurrency Pool for LLM API calls * * Provides rate limiting for parallel API calls to prevent overwhelming * the Z.AI API with too many concurrent requests. * * Uses a custom semaphore implementation to avoid external dependencies. */ /** * Configuration options for the concurrency pool */ export interface ConcurrencyPoolOptions { /** Maximum number of concurrent operations (default: 4) */ limit?: number; } /** * Result of a task execution, including success/failure status */ export type TaskResult = { success: true; value: T; index: number; } | { success: false; error: Error; index: number; }; /** * An async function that can be wrapped by the pool */ export type AsyncFunction = () => Promise; /** * Callback for reporting progress updates */ export type ProgressCallback = (completed: number, total: number, message?: string) => void; /** * Options for progress reporting during pool execution */ export interface ProgressOptions { /** Callback invoked on progress updates */ onProgress?: ProgressCallback; /** Milliseconds before emitting heartbeat if no task completes (default: 10000) */ heartbeatInterval?: number; /** Minimum milliseconds between progress notifications (default: 100) */ throttleMs?: number; } /** * A task with an optional label for progress messages */ export type LabeledTask = (() => Promise) | { label: string; fn: () => Promise; }; /** * Concurrency pool for rate-limiting parallel async operations * * Uses a semaphore to limit the number of concurrent operations, * preventing API rate limit issues when making many parallel requests. * * @example * ```typescript * const pool = new ConcurrencyPool({ limit: 4 }); * * // Wrap individual functions * const result = await pool.wrap(() => client.complete(messages, model)); * * // Run multiple tasks in parallel with controlled concurrency * const results = await pool.runAll([ * () => client.complete(messages1, model), * () => client.complete(messages2, model), * () => client.complete(messages3, model), * ]); * ``` */ export declare class ConcurrencyPool { private readonly semaphore; private readonly concurrencyLimit; /** * Create a new concurrency pool * * @param options - Pool configuration options */ constructor(options?: ConcurrencyPoolOptions); /** * Get the configured concurrency limit */ get limit(): number; /** * Get the number of currently pending operations */ get pendingCount(): number; /** * Get the number of currently active (running) operations */ get activeCount(): number; /** * Wrap an async function to run with rate limiting * * The function will wait for an available slot in the pool * before executing. * * @param fn - The async function to wrap * @returns Promise resolving to the function's result * * @example * ```typescript * const result = await pool.wrap(() => fetchData(url)); * ``` */ wrap(fn: AsyncFunction): Promise; /** * Run multiple tasks in parallel with controlled concurrency * * All tasks are started immediately, but only `limit` tasks * will run concurrently at any given time. * * @param tasks - Array of async functions to execute * @returns Promise resolving to array of results in original order * @throws Rejects with the first error encountered * * @example * ```typescript * const results = await pool.runAll([ * () => fetchData(url1), * () => fetchData(url2), * () => fetchData(url3), * ]); * // results = [data1, data2, data3] * ``` */ runAll(tasks: AsyncFunction[]): Promise; /** * Run multiple tasks in parallel, collecting all results (success or failure) * * Unlike runAll, this method does not throw on individual task failures. * Instead, it returns detailed results for each task. * * @param tasks - Array of async functions to execute * @returns Promise resolving to array of TaskResults in original order * * @example * ```typescript * const results = await pool.runAllSettled([ * () => fetchData(url1), * () => fetchData(url2), // might fail * () => fetchData(url3), * ]); * * for (const result of results) { * if (result.success) { * console.log(`Task ${result.index} succeeded:`, result.value); * } else { * console.error(`Task ${result.index} failed:`, result.error); * } * } * ``` */ runAllSettled(tasks: AsyncFunction[]): Promise[]>; /** * Run multiple tasks in parallel with progress reporting * * Like runAllSettled but with: * - Progress callbacks on task completion * - Optional task labels for semantic progress messages * - Heartbeat emissions if no task completes within interval * - Throttling to prevent notification flooding * * @param tasks - Array of tasks (functions or labeled objects) * @param progressOptions - Progress reporting configuration * @returns Promise resolving to array of TaskResults in original order */ runAllSettledWithProgress(tasks: LabeledTask[], progressOptions?: ProgressOptions): Promise[]>; /** * Clear the queue of pending operations * * Note: This only clears pending operations that haven't started yet. * Currently running operations will complete normally. */ clearQueue(): void; } /** * Create a new concurrency pool with the specified limit * * @param limit - Maximum number of concurrent operations (default: 4) * @returns A new ConcurrencyPool instance * * @example * ```typescript * const pool = createPool(4); * const result = await pool.wrap(() => client.complete(messages, model)); * ``` */ export declare function createPool(limit?: number): ConcurrencyPool; /** * Create a rate-limited wrapper function * * This is a convenience function for simple use cases where you want * to rate-limit a specific function without managing a pool instance. * * @param fn - The async function to wrap * @param limit - Maximum concurrent executions (default: 4) * @returns A rate-limited version of the function * * @example * ```typescript * const rateLimitedComplete = withLimit( * (messages: ChatMessage[]) => client.complete(messages, model), * 4 * ); * * // All calls go through the same limiter * await Promise.all([ * rateLimitedComplete(messages1), * rateLimitedComplete(messages2), * rateLimitedComplete(messages3), * ]); * ``` */ export declare function withLimit(fn: (...args: TArgs) => Promise, limit?: number): (...args: TArgs) => Promise; //# sourceMappingURL=pool.d.ts.map