/**
 * 转换上下文类型定义
 * 定义JSON<>SSE双向转换过程中的上下文信息
 */
import type { JsonObject, JsonValue } from './core-interfaces.js';
import type { SseEvent, SseEventStats, SseEventType } from './sse-events.js';
export interface BaseConversionContext {
    requestId: string;
    model: string;
    createdAt: number;
    outputIndexCounter: number;
    sequenceCounter: number;
    timeoutMs?: number;
    heartbeatTimer?: NodeJS.Timeout;
    isCompleted: boolean;
    lastEventTime: number;
    logger: ConversionLogger;
}
export interface JsonToSseContext extends BaseConversionContext {
    jsonResponse: ResponsesJson;
    options: JsonToSseOptions;
    eventStats: SseEventStats;
    startTime: number;
    currentOutputIndex: number;
    contentIndexCounter: Map<string, number>;
    itemIds: Map<string, string>;
    activeOutputItems: Map<number, OutputItemState>;
}
export interface SseToJsonContext extends BaseConversionContext {
    options: SseToJsonOptions;
    aggregatedEvents: SseEvent[];
    partialResponse: ResponsesPartialJson;
    completionTimeout?: NodeJS.Timeout;
    outputItems: Map<number, OutputItemBuilder>;
    lastSequenceNumber: number;
    expectedSequenceNumbers: Set<number>;
    isResponseCreated: boolean;
    isInProgress: boolean;
}
type ConversionContextUnion = JsonToSseContext | SseToJsonContext;
export interface JsonToSseOptions {
    requestId: string;
    model: string;
    timeoutMs?: number;
    heartbeatIntervalMs?: number;
    chunkSize?: number;
    delayMs?: number;
    includeSequenceNumbers?: boolean;
    enableHeartbeat?: boolean;
    validationMode?: 'none' | 'basic' | 'strict';
    onEvent?: (event: SseEvent) => void;
    onError?: (error: Error) => void;
    onComplete?: () => void;
}
export interface SseToJsonOptions {
    requestId: string;
    timeoutMs?: number;
    validateEvents?: boolean;
    enableSequenceValidation?: boolean;
    onPartialResult?: (partial: ResponsesPartialJson) => void;
    onEvent?: (event: SseEvent) => void;
    onCompletion?: (final: ResponsesJson) => void;
    onError?: (error: Error) => void;
    onTimeout?: () => void;
}
export interface ResponsesJson {
    id: string;
    object: 'response';
    created_at: number;
    model: string;
    status: 'in_progress' | 'requires_action' | 'completed';
    output: OutputItem[];
    usage?: UsageInfo;
    previous_response_id?: string | null;
    required_action?: RequiredAction;
    output_text?: string;
}
export interface ResponsesPartialJson {
    id: string;
    object: 'response';
    created_at: number;
    model: string;
    status: 'in_progress' | 'requires_action' | 'completed';
    output: OutputItem[];
    usage?: UsageInfo;
    required_action?: RequiredAction;
    output_text?: string;
}
export interface OutputItemState {
    id: string;
    type: string;
    status: 'in_progress' | 'completed';
    contentIndex: number;
    isContentPartAdded: boolean;
    isTextStarted: boolean;
    isTextCompleted: boolean;
    accumulatedText: string;
    accumulatedDelta: string;
}
export interface OutputItemBuilder {
    item: OutputItem;
    isCompleted: boolean;
    contentParts: ContentPart[];
    currentContentIndex: number;
    accumulatedText: string;
    hasContentPartAdded: boolean;
    isTextInProgress: boolean;
}
type LoggerMetadata = Record<string, unknown>;
export interface ConversionLogger {
    debug(message: string, meta?: LoggerMetadata): void;
    info(message: string, meta?: LoggerMetadata): void;
    warn(message: string, meta?: LoggerMetadata): void;
    error(message: string, error?: Error, meta?: LoggerMetadata): void;
    event(eventType: string, data?: LoggerMetadata): void;
    metric(name: string, value: number, tags?: Record<string, string>): void;
}
export interface ConversionError extends Error {
    code: string;
    requestId: string;
    eventType?: string;
    sequenceNumber?: number;
    context?: JsonObject;
}
export interface StreamState {
    isStarted: boolean;
    isCompleted: boolean;
    isAborted: boolean;
    lastActivityTime: number;
    totalEventsProcessed: number;
    bytesProcessed: number;
    errorCount: number;
}
export interface SseEventStream extends AsyncIterable<SseEvent> {
    getStats(): SseEventStats;
    getState(): StreamState;
    complete(): void;
    abort(error?: Error): void;
    on?(event: 'error', handler: (error: Error) => void): this;
    on?(event: 'complete', handler: () => void): this;
    on?(event: 'timeout', handler: () => void): this;
    on?(event: 'stats', handler: (stats: SseEventStats) => void): this;
}
export interface ConversionConfig {
    defaultTimeoutMs: number;
    heartbeatIntervalMs: number;
    inactivityTimeoutMs: number;
    defaultChunkSize: number;
    defaultDelayMs: number;
    enableEventValidation: boolean;
    enableSequenceValidation: boolean;
    strictMode: boolean;
    maxConcurrentEvents: number;
    eventBufferSize: number;
    debugMode: boolean;
    logLevel: 'debug' | 'info' | 'warn' | 'error';
    enableMetrics: boolean;
}
export declare const DEFAULT_CONVERSION_CONFIG: ConversionConfig;
export interface EventHandler {
    canHandle(eventType: SseEventType): boolean;
    handle(event: SseEvent, context: ConversionContextUnion): Promise<void>;
}
export interface ConversionMiddleware {
    name: string;
    beforeConversion?: (context: ConversionContextUnion) => Promise<void>;
    afterConversion?: (context: ConversionContextUnion, result: unknown) => Promise<void>;
    onError?: (context: ConversionContextUnion, error: Error) => Promise<void>;
}
interface OutputItem {
    id: string;
    type: 'reasoning' | 'message' | 'function_call' | 'system_message';
    status?: 'in_progress' | 'completed';
    content?: ContentPart[];
    summary?: JsonValue[];
    role?: string;
    arguments?: string;
    call_id?: string;
    name?: string;
    message?: MessageContent;
}
interface ContentPart {
    type: 'reasoning_text' | 'output_text' | 'input_text' | 'commentary';
    text: string;
}
interface MessageContent {
    id?: string;
    role: string;
    status?: string;
    content: ContentPart[];
}
interface UsageInfo {
    input_tokens: number;
    output_tokens: number;
    total_tokens: number;
    output_tokens_details?: {
        reasoning_tokens: number;
    };
}
interface RequiredAction {
    type: 'submit_tool_outputs';
    submit_tool_outputs: {
        tool_calls: ToolCall[];
    };
}
interface ToolCall {
    id: string;
    type: 'function';
    function: {
        name: string;
        arguments: string;
    };
}
export {};
