/**
 * 工具类型定义
 * 提供JSON↔SSE双向转换中常用的工具类型和辅助函数
 */
import type { JsonObject, JsonValue } from './core-interfaces.js';
export type ProtocolType = 'chat' | 'responses';
export type ConversionDirection = 'json_to_sse' | 'sse_to_json';
export interface EventTypeMap {
    chat: {
        SSE_EVENT: 'chat_chunk' | 'chat.done' | 'error' | 'ping';
        JSON_TYPE: 'ChatCompletionChunk' | 'ChatCompletionResponse';
    };
    responses: {
        SSE_EVENT: 'response.created' | 'response.in_progress' | 'response.completed' | 'response.required_action' | 'response.done' | 'response.output_item.added' | 'response.output_item.done' | 'response.content_part.added' | 'response.content_part.done' | 'response.output_text.delta' | 'response.output_text.done' | 'response.reasoning_text.delta' | 'response.reasoning_text.done' | 'response.function_call_arguments.delta' | 'response.function_call_arguments.done';
        JSON_TYPE: 'ResponsesResponse' | 'ResponsesRequest';
    };
}
export interface GenericConverter<TProtocol extends ProtocolType, TDirection extends ConversionDirection> {
    protocol: TProtocol;
    direction: TDirection;
    convert(input: unknown, options: JsonObject): Promise<unknown>;
    validate?(input: unknown): boolean;
    getStats?(): JsonObject;
}
export interface UnifiedConversionOptions {
    requestId: string;
    model: string;
    protocol: ProtocolType;
    direction: ConversionDirection;
    timeoutMs?: number;
    enableHeartbeat?: boolean;
    heartbeatIntervalMs?: number;
    validationMode?: 'none' | 'basic' | 'strict';
    debugMode?: boolean;
    onError?: (error: Error) => void;
    onComplete?: () => void;
}
export interface UnifiedEventStream extends AsyncIterable<unknown> {
    protocol: ProtocolType;
    direction: ConversionDirection;
    requestId: string;
    getStats(): JsonObject;
    getConfig(): JsonObject;
    pause?(): void;
    resume?(): void;
    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: JsonObject) => void): this;
}
export type Optional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
export type RequireKeys<T, K extends keyof T> = T & Required<Pick<T, K>>;
export type JsonType<TProtocol extends ProtocolType> = TProtocol extends 'chat' ? ChatJsonType : TProtocol extends 'responses' ? ResponsesJsonType : never;
export type SseEventType<TProtocol extends ProtocolType> = TProtocol extends 'chat' ? ChatSseEventType : TProtocol extends 'responses' ? ResponsesSseEventType : never;
export interface ChatJsonType {
    id: string;
    object: 'chat.completion' | 'chat.completion.chunk';
    created: number;
    model: string;
    choices: unknown[];
    usage?: JsonObject;
}
export type ChatSseEventType = 'chat_chunk' | 'chat.done' | 'error' | 'ping';
export interface ResponsesJsonType {
    id: string;
    object: 'response';
    created_at: number;
    model: string;
    status: string;
    output: unknown[];
    usage?: JsonObject;
}
export type ResponsesSseEventType = 'response.created' | 'response.in_progress' | 'response.completed' | 'response.required_action' | 'response.done' | 'response.output_item.added' | 'response.output_item.done' | 'response.content_part.added' | 'response.content_part.done' | 'response.output_text.delta' | 'response.output_text.done' | 'response.reasoning_text.delta' | 'response.reasoning_text.done' | 'response.function_call_arguments.delta' | 'response.function_call_arguments.done';
export interface ConversionResult<TSuccess = unknown, TError = Error> {
    success: boolean;
    data?: TSuccess;
    error?: TError;
    metadata?: {
        duration: number;
        eventsProcessed: number;
        bytesProcessed: number;
        protocol: ProtocolType;
        direction: ConversionDirection;
    };
}
export type AsyncConversionResult<TSuccess = unknown, TError = Error> = Promise<ConversionResult<TSuccess, TError>>;
export interface ConverterFactory {
    createJsonToSseConverter<TProtocol extends ProtocolType>(protocol: TProtocol, options: UnifiedConversionOptions): GenericConverter<TProtocol, 'json_to_sse'>;
    createSseToJsonConverter<TProtocol extends ProtocolType>(protocol: TProtocol, options: UnifiedConversionOptions): GenericConverter<TProtocol, 'sse_to_json'>;
}
export interface ConverterRegistry {
    register<TProtocol extends ProtocolType, TDirection extends ConversionDirection>(protocol: TProtocol, direction: TDirection, converter: GenericConverter<TProtocol, TDirection>): void;
    get<TProtocol extends ProtocolType, TDirection extends ConversionDirection>(protocol: TProtocol, direction: TDirection): GenericConverter<TProtocol, TDirection> | undefined;
    list(): Array<{
        protocol: ProtocolType;
        direction: ConversionDirection;
        converter: GenericConverter<ProtocolType, ConversionDirection>;
    }>;
}
export interface Validator<T> {
    validate(input: T): ValidationResult;
    isValid(input: T): boolean;
    getErrors(input: T): string[];
}
export interface ValidationResult {
    isValid: boolean;
    errors: ValidationError[];
    warnings: ValidationWarning[];
}
export interface ValidationError {
    code: string;
    message: string;
    path: string;
    value?: JsonValue;
}
export interface ValidationWarning {
    code: string;
    message: string;
    path: string;
    value?: JsonValue;
}
export interface Serializer<T> {
    serialize(input: T): string;
    deserialize(input: string): T;
    isValid(input: string): boolean;
}
export interface Cache<K, V> {
    get(key: K): V | undefined;
    set(key: K, value: V): void;
    has(key: K): boolean;
    delete(key: K): boolean;
    clear(): void;
    size: number;
}
export interface MetricsCollector {
    incrementCounter(name: string, tags?: Record<string, string>): void;
    recordHistogram(name: string, value: number, tags?: Record<string, string>): void;
    setGauge(name: string, value: number, tags?: Record<string, string>): void;
    recordTimer(name: string, duration: number, tags?: Record<string, string>): void;
    getMetrics(): Record<string, unknown>;
}
type LoggerMetadata = Record<string, unknown>;
export interface Logger {
    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;
    child(meta: LoggerMetadata): Logger;
}
export interface RetryConfig {
    maxAttempts: number;
    initialDelayMs: number;
    maxDelayMs: number;
    backoffMultiplier: number;
    jitter: boolean;
    retryableErrors: string[];
}
export interface CircuitBreakerConfig {
    failureThreshold: number;
    recoveryTimeoutMs: number;
    monitoringPeriodMs: number;
    expectedRecoveryTimeMs: number;
}
export interface RateLimitConfig {
    windowMs: number;
    maxRequests: number;
    burstSize?: number;
    strategy: 'sliding_window' | 'fixed_window' | 'token_bucket';
}
export interface HealthCheck {
    isHealthy(): Promise<boolean>;
    getStatus(): HealthStatus;
    onHealthChange(callback: (status: HealthStatus) => void): void;
}
export interface HealthStatus {
    healthy: boolean;
    timestamp: number;
    details?: Record<string, unknown>;
}
type LifecycleContext = Record<string, unknown>;
export interface LifecycleHooks {
    beforeConversion?: (context: LifecycleContext) => Promise<void>;
    afterConversion?: (context: LifecycleContext, result: unknown) => Promise<void>;
    onError?: (context: LifecycleContext, error: Error) => Promise<void>;
    onTimeout?: (context: LifecycleContext) => Promise<void>;
    onRetry?: (context: LifecycleContext, attempt: number) => Promise<void>;
}
export interface Middleware {
    name: string;
    priority: number;
    beforeConversion?: (context: LifecycleContext) => Promise<void>;
    afterConversion?: (context: LifecycleContext, result: unknown) => Promise<void>;
    onError?: (context: LifecycleContext, error: Error) => Promise<void>;
}
export interface Plugin {
    name: string;
    version: string;
    initialize(context: LifecycleContext): Promise<void>;
    destroy(): Promise<void>;
    getMiddlewares(): Middleware[];
    getHooks(): LifecycleHooks;
}
export interface ConfigMerger {
    merge<T>(base: T, override: Partial<T>): T;
    validate<T>(config: T): ValidationResult;
    normalize<T>(config: T): T;
}
export interface EnvironmentParser {
    getString(key: string, defaultValue?: string): string;
    getNumber(key: string, defaultValue?: number): number;
    getBoolean(key: string, defaultValue?: boolean): boolean;
    getArray(key: string, delimiter?: string): string[];
    getObject(key: string): JsonObject;
}
export declare const TypeGuards: {
    isString: (value: unknown) => value is string;
    isNumber: (value: unknown) => value is number;
    isBoolean: (value: unknown) => value is boolean;
    isObject: (value: unknown) => value is JsonObject;
    isArray: (value: unknown) => value is JsonValue[];
    isFunction: (value: unknown) => value is (...args: unknown[]) => unknown;
    isDate: (value: unknown) => value is Date;
    isNull: (value: unknown) => value is null;
    isUndefined: (value: unknown) => value is undefined;
    isNullOrUndefined: (value: unknown) => value is null | undefined;
};
export declare const AsyncUtils: {
    delay: (ms: number) => Promise<void>;
    timeout: <T>(promise: Promise<T>, ms: number) => Promise<T>;
    retry: <T>(fn: () => Promise<T>, config: RetryConfig) => Promise<T>;
};
export {};
