/**
 * Streaming Module
 *
 * Comprehensive streaming utilities for handling async data streams
 * with support for transformations, backpressure, and error recovery.
 *
 * Key features:
 * - Per-chunk timeout handling for latency control
 * - Adaptive render throttling for smooth UI updates
 * - Detailed latency metrics with percentiles (p50/p95/p99)
 * - Flow hints for consumer feedback
 * - Native Node.js stream integration
 * - Backpressure handling for flow control
 */
export { StreamProcessor } from './stream-processor.js';
export * from './types.js';
export { ChunkProcessor, ChunkTimeoutError, } from './chunk-processor.js';
export { StreamHandler, } from './stream-handler.js';
export { ChunkHandler } from './chunk-handler.js';
export { StreamTransformer } from './stream-transformer.js';
export { BackpressureController } from './backpressure.js';
export { ToolPhaseEmitter, ToolPhaseManager, getToolPhaseManager, resetToolPhaseManager, } from './tool-phases.js';
export { DEFAULT_THROTTLE_CONFIG, throttle, ToolPhaseThrottler, getToolPhaseThrottler, resetToolPhaseThrottler, } from './tool-throttle.js';
export { ProgressTracker, createSimpleTracker, calculateIterationProgress, } from './progress-tracker.js';
export { DEFAULT_CHUNKER_CONFIG, createBlockState, detectFence, updateBlockState, MarkdownChunker, chunkMarkdown, hasUnclosedCodeBlock, countCodeBlocks, fixUnclosedCodeBlocks, createStreamingChunker, } from './markdown-chunker.js';
export { DEFAULT_RETRY_CONFIG, DEFAULT_CIRCUIT_BREAKER_CONFIG, DEFAULT_RETRY_MANAGER_CONFIG, calculateDelay, isRetryable, sleep, withTimeout, retry, retryOrThrow, CircuitBreaker, CircuitOpenError, RetryManager, getRetryManager, resetRetryManager, withRetry, withCircuitBreaker, withRetryAndCircuitBreaker, } from './retry-policy.js';
// ============================================================================
// Tool Streaming Convenience Functions
// ============================================================================
import { getToolPhaseManager } from './tool-phases.js';
import { getToolPhaseThrottler } from './tool-throttle.js';
/**
 * Create a streaming tool execution helper
 */
export function createToolStream(toolCallId, toolName, onPhase) {
    const manager = getToolPhaseManager();
    const throttler = getToolPhaseThrottler();
    const emitter = manager.createEmitter(toolCallId, toolName);
    // Set up throttled listener if callback provided
    if (onPhase) {
        throttler.setCallback(onPhase);
    }
    // Forward emitter events to throttler
    emitter.on('phase', (event) => {
        throttler.push(event);
    });
    return {
        emitter,
        start(message) {
            emitter.start(message);
        },
        update(progress, message) {
            emitter.update(progress, message);
        },
        success(output, metadata) {
            emitter.result({
                success: true,
                output,
                metadata,
            });
        },
        fail(error, metadata) {
            emitter.result({
                success: false,
                error,
                metadata,
            });
        },
        cleanup() {
            throttler.flushAll();
            manager.removeEmitter(toolCallId);
        },
    };
}
/**
 * Wrap an async operation with progress streaming
 */
export async function streamedOperation(toolCallId, toolName, operation, onPhase) {
    const stream = createToolStream(toolCallId, toolName, onPhase);
    stream.start();
    try {
        const result = await operation((progress, message) => {
            stream.update(progress, message);
        });
        stream.success(typeof result === 'string' ? result : JSON.stringify(result));
        return result;
    }
    catch (error) {
        const errorMessage = error instanceof Error ? error.message : String(error);
        stream.fail(errorMessage);
        throw error;
    }
    finally {
        stream.cleanup();
    }
}
/**
 * Stream progress for an array operation
 */
export async function streamedIteration(toolCallId, toolName, items, processor, onPhase) {
    const stream = createToolStream(toolCallId, toolName, onPhase);
    stream.start(`Processing ${items.length} items...`);
    const results = [];
    try {
        for (let i = 0; i < items.length; i++) {
            const progress = Math.round(((i + 1) / items.length) * 100);
            stream.update(progress, `Processing item ${i + 1}/${items.length}...`);
            const result = await processor(items[i], i);
            results.push(result);
        }
        stream.success(`Processed ${items.length} items`);
        return results;
    }
    catch (error) {
        const errorMessage = error instanceof Error ? error.message : String(error);
        stream.fail(errorMessage);
        throw error;
    }
    finally {
        stream.cleanup();
    }
}
//# sourceMappingURL=index.js.map