/**
 * 流状态管理类型定义
 * 管理JSON↔SSE双向转换过程中的流状态
 */
import type { JsonToSseOptions, ResponsesJson, ResponsesPartialJson, SseToJsonOptions } from './conversion-context.js';
import { SseEvent, SseEventType } from './sse-events.js';
export declare enum StreamState {
    IDLE = "idle",
    STARTING = "starting",
    ACTIVE = "active",
    PAUSED = "paused",
    COMPLETING = "completing",
    COMPLETED = "completed",
    ERROR = "error",
    TIMEOUT = "timeout",
    ABORTED = "aborted"
}
export declare enum StreamDirection {
    JSON_TO_SSE = "json_to_sse",
    SSE_TO_JSON = "sse_to_json"
}
export interface BaseStreamState {
    direction: StreamDirection;
    requestId: string;
    model: string;
    status: StreamState;
    startTime: number;
    endTime?: number;
    lastActivityTime: number;
    totalEventsProcessed: number;
    bytesProcessed: number;
    errorCount: number;
    timeoutCount: number;
}
export interface JsonToSseStreamState extends BaseStreamState {
    direction: StreamDirection.JSON_TO_SSE;
    inputJson: ResponsesJson;
    options: JsonToSseOptions;
    currentOutputIndex: number;
    currentSequenceNumber: number;
    outputItemsGenerated: number;
    contentPartsGenerated: number;
    deltaEventsGenerated: number;
    isHeartbeatActive: boolean;
    lastHeartbeatTime?: number;
    chunksRemaining?: number;
    estimatedTotalEvents?: number;
}
export interface SseToJsonStreamState extends BaseStreamState {
    direction: StreamDirection.SSE_TO_JSON;
    options: SseToJsonOptions;
    expectedEventTypes: Set<SseEventType>;
    receivedEventTypes: Set<SseEventType>;
    lastSequenceNumber: number;
    expectedSequenceNumbers: Set<number>;
    missingSequenceNumbers: Set<number>;
    isResponseCreated: boolean;
    isResponseCompleted: boolean;
    isRequiredActionReceived: boolean;
    aggregatedEvents: SseEvent[];
    partialJson: ResponsesPartialJson;
}
export interface StreamStatistics {
    duration: number;
    eventsPerSecond: number;
    bytesPerSecond: number;
    eventTypeCounts: Record<SseEventType, number>;
    totalDeltaEvents: number;
    totalItemEvents: number;
    totalResponseEvents: number;
    errorRate: number;
    timeoutRate: number;
    sequenceErrorCount: number;
    averageEventProcessingTime: number;
    maxEventProcessingTime: number;
    minEventProcessingTime: number;
    memoryUsage: number;
    cpuUsage: number;
    bufferUtilization: number;
}
export interface StreamMetrics {
    eventTimestamps: Array<{
        eventType: SseEventType;
        timestamp: number;
        sequenceNumber: number;
    }>;
    sequenceGaps: Array<{
        start: number;
        end: number;
        size: number;
    }>;
    backpressureEvents: number;
    maxBufferSize: number;
    averageBufferSize: number;
    retryAttempts: number;
    successfulRetries: number;
    timeoutEvents: Array<{
        eventType?: SseEventType;
        timeoutDuration: number;
        timestamp: number;
    }>;
}
export interface StreamConfig {
    idleTimeoutMs: number;
    totalTimeoutMs: number;
    heartbeatTimeoutMs: number;
    maxBufferSize: number;
    highWaterMark: number;
    lowWaterMark: number;
    enableBackpressure: boolean;
    backpressureThreshold: number;
    backpressureStrategy: 'drop' | 'buffer' | 'error';
    maxRetries: number;
    retryDelayMs: number;
    retryBackoffMultiplier: number;
    enableMetrics: boolean;
    metricsIntervalMs: number;
    enableDetailedLogging: boolean;
    maxConcurrentEvents: number;
    eventProcessingTimeoutMs: number;
    gcThreshold: number;
}
export declare const DEFAULT_STREAM_CONFIG: StreamConfig;
export interface StreamEvent<TPayload = unknown> {
    type: 'state_change' | 'error' | 'timeout' | 'metrics' | 'heartbeat' | 'completion';
    timestamp: number;
    requestId: string;
    data: TPayload;
}
export interface StateChangeEvent extends StreamEvent<{
    oldState: StreamState;
    newState: StreamState;
    reason?: string;
}> {
    type: 'state_change';
}
export interface StreamErrorEvent extends StreamEvent<{
    error: Error;
    eventType?: SseEventType;
    sequenceNumber?: number;
    recoverable: boolean;
}> {
    type: 'error';
}
export interface StreamTimeoutEvent extends StreamEvent<{
    timeoutType: 'idle' | 'total' | 'heartbeat' | 'event_processing';
    timeoutDuration: number;
    lastActivityTime: number;
}> {
    type: 'timeout';
}
export interface StreamMetricsEvent extends StreamEvent<StreamStatistics> {
    type: 'metrics';
}
export interface StreamHeartbeatEvent extends StreamEvent<{
    interval: number;
    eventsSinceLastHeartbeat: number;
}> {
    type: 'heartbeat';
}
export interface StreamCompletionEvent extends StreamEvent<{
    finalState: StreamState;
    totalEvents: number;
    totalDuration: number;
    success: boolean;
}> {
    type: 'completion';
}
export type StreamEventData = StateChangeEvent | StreamErrorEvent | StreamTimeoutEvent | StreamMetricsEvent | StreamHeartbeatEvent | StreamCompletionEvent;
export interface StreamStateListener {
    onStateChange(event: StateChangeEvent): void;
    onError(event: StreamErrorEvent): void;
    onTimeout(event: StreamTimeoutEvent): void;
    onMetrics(event: StreamMetricsEvent): void;
    onHeartbeat(event: StreamHeartbeatEvent): void;
    onCompletion(event: StreamCompletionEvent): void;
}
export interface StreamStateManager {
    getCurrentState(): StreamState;
    setState(newState: StreamState, reason?: string): void;
    transitionTo(newState: StreamState, condition?: () => boolean): boolean;
    updateActivity(): void;
    isIdle(): boolean;
    isCompleted(): boolean;
    isError(): boolean;
    processEvent(event: SseEvent): void;
    incrementEventCount(): void;
    incrementErrorCount(): void;
    getStatistics(): StreamStatistics;
    getMetrics(): StreamMetrics;
    addListener(listener: StreamStateListener): void;
    removeListener(listener: StreamStateListener): void;
    start(): void;
    pause(): void;
    resume(): void;
    complete(): void;
    abort(error?: Error): void;
    reset(): void;
}
