UNPKG

rcc-pipeline

Version:

RCC Pipeline Module - Pipeline system and workflow management based on pipeline-framework

1,887 lines (1,861 loc) 70.2 kB
import * as rcc_debugcenter from 'rcc-debugcenter'; import { DebugCenter } from 'rcc-debugcenter'; import * as rcc_basemodule from 'rcc-basemodule'; import { BaseModule, BaseModuleRecordingConfig } from 'rcc-basemodule'; import { ErrorHandlingCenter } from 'rcc-errorhandling'; /** * IO Tracking Configuration * IO跟踪配置 */ interface IOTrackingConfig { enabled?: boolean; baseDirectory?: string; maxFiles?: number; maxSize?: number; autoRecord?: boolean; saveIndividualFiles?: boolean; saveSessionFiles?: boolean; ioDirectory?: string; includeTimestamp?: boolean; includeDuration?: boolean; maxEntriesPerFile?: number; } /** * Pipeline-specific module configuration * 流水线特定模块配置 */ interface PipelineModuleConfig { id: string; name: string; version: string; description: string; type: 'provider' | 'scheduler' | 'tracker' | 'pipeline'; providerName?: string; endpoint?: string; supportedModels?: string[]; defaultModel?: string; maxConcurrentRequests?: number; requestTimeout?: number; enableTwoPhaseDebug?: boolean; debugBaseDirectory?: string; enableIOTracking?: boolean; ioTrackingConfig?: IOTrackingConfig; } /** * Pipeline Base Module with enhanced debug capabilities * 具有增强调试功能的流水线基础模块 */ declare class PipelineBaseModule extends BaseModule { protected pipelineConfig: PipelineModuleConfig; protected errorHandler: ErrorHandlingCenter; protected debugCenter: DebugCenter | null; constructor(config: PipelineModuleConfig); /** * Enable two-phase debug system * 启用两阶段调试系统 */ protected enableTwoPhaseDebug(enabled: boolean, baseDirectory?: string, ioTrackingConfig?: IOTrackingConfig): void; /** * Get debug center instance * 获取调试中心实例 */ protected getDebugCenter(): DebugCenter | null; /** * Get pipeline configuration * 获取流水线配置 */ getPipelineConfig(): PipelineModuleConfig; /** * Update pipeline configuration * 更新流水线配置 */ updatePipelineConfig(newConfig: Partial<PipelineModuleConfig>): void; /** * Get provider information * 获取提供者信息 */ getProviderInfo(): { name: string; endpoint: string | undefined; supportedModels: string[]; defaultModel: string | undefined; type: "provider" | "scheduler" | "tracker" | "pipeline"; }; /** * Track pipeline operation with I/O tracking * 跟踪流水线操作并记录I/O */ trackPipelineOperation<T>(operationId: string, operation: () => Promise<T>, inputData?: any, operationType?: string): Promise<T>; /** * Record pipeline stage * 记录流水线阶段 */ recordPipelineStage(stageName: string, stageData: any, status?: 'started' | 'completed' | 'failed'): void; /** * Handle pipeline errors with enhanced error handling * 处理流水线错误并提供增强的错误处理 */ handlePipelineError(error: Error, context: { operation?: string; stage?: string; requestId?: string; additionalData?: Record<string, any>; }): void; /** * Get pipeline metrics * 获取流水线指标 */ getPipelineMetrics(): { debugEnabled: boolean; ioTrackingEnabled: boolean; debugConfig: rcc_basemodule.DebugConfig; pipelineEntries: rcc_debugcenter.PipelineIOEntry[]; stats: rcc_debugcenter.RecordingStats | null; } | { debugEnabled: boolean; ioTrackingEnabled: boolean; debugConfig: rcc_basemodule.DebugConfig; pipelineEntries?: undefined; stats?: undefined; }; /** * Override destroy method to ensure proper cleanup * 重写destroy方法以确保正确的清理 */ destroy(): Promise<void>; } /** * Pipeline Execution Context - Enhanced request-response tracing system * 流水线执行上下文 - 增强的请求响应跟踪系统 */ /** * Pipeline execution context interface with comprehensive request-response tracing * 流水线执行上下文接口,提供完整的请求响应跟踪 */ interface PipelineExecutionContext { /** Unique execution identifier */ executionId: string; /** Request identifier for cross-module tracking */ requestId: string; /** Trace identifier for distributed tracking */ traceId: string; /** Session identifier for session tracking */ sessionId?: string; /** Pipeline stage tracking */ stage: PipelineStage$1; /** Current module information */ module: ModuleInfo; /** Execution timing information */ timing: ExecutionTiming; /** Request data with truncation support */ request?: any; /** Response data with truncation support */ response?: any; /** Error information if execution failed */ error?: ExecutionError; /** Metadata for additional context */ metadata?: Record<string, any>; /** Configuration snapshot */ config?: ConfigSnapshot; /** Child contexts for nested executions */ children?: PipelineExecutionContext[]; /** Parent context for hierarchical tracking */ parent?: PipelineExecutionContext; } /** * Pipeline stage enumeration * 流水线阶段枚举 */ declare enum PipelineStage$1 { REQUEST_INIT = "request_init", AUTHENTICATION = "authentication", SCHEDULING = "scheduling", PIPELINE_SELECTION = "pipeline_selection", PROVIDER_EXECUTION = "provider_execution", RESPONSE_PROCESSING = "response_processing", COMPLETION = "completion", ERROR_HANDLING = "error_handling" } /** * Module information for pipeline tracking * 模块信息,用于流水线跟踪 */ interface ModuleInfo { moduleId: string; moduleName: string; moduleType: string; providerName?: string; endpoint?: string; } /** * Execution timing information * 执行时间信息 */ interface ExecutionTiming { startTime: number; endTime?: number; duration?: number; stageTimings: Map<PipelineStage$1, StageTiming>; } /** * Stage-specific timing information * 阶段特定的时间信息 */ interface StageTiming { startTime: number; endTime?: number; duration?: number; status: 'pending' | 'running' | 'completed' | 'failed'; } /** * Execution error information * 执行错误信息 */ interface ExecutionError { errorId: string; message: string; stack?: string; category: ErrorCategory$1; severity: ErrorSeverity$1; recoverable: boolean; context?: Record<string, any>; } /** * Error category enumeration * 错误类别枚举 */ declare enum ErrorCategory$1 { NETWORK = "network", AUTHENTICATION = "authentication", VALIDATION = "validation", PROCESSING = "processing", PROVIDER = "provider", SYSTEM = "system", TIMEOUT = "timeout", RATE_LIMIT = "rate_limit" } /** * Error severity enumeration * 错误严重性枚举 */ declare enum ErrorSeverity$1 { FATAL = "fatal", ERROR = "error", WARNING = "warning", INFO = "info" } /** * Configuration snapshot for consistency tracking * 配置快照,用于一致性跟踪 */ interface ConfigSnapshot { recordingConfig: BaseModuleRecordingConfig; baseDirectory: string; port?: number; environment: string; timestamp: number; } /** * Execution context creation options * 执行上下文创建选项 */ interface ExecutionContextOptions { /** Create new trace or inherit existing */ createNewTrace?: boolean; /** Inherit from parent context */ inheritFrom?: PipelineExecutionContext; /** Custom execution ID */ executionId?: string; /** Custom trace ID */ traceId?: string; /** Custom session ID */ sessionId?: string; /** Additional metadata */ metadata?: Record<string, any>; /** Configuration overrides */ config?: Partial<BaseModuleRecordingConfig>; } /** * Execution statistics * 执行统计 */ interface ExecutionStatistics { totalExecutions: number; activeExecutions: number; completedExecutions: number; failedExecutions: number; averageDuration: number; stageStatistics: Map<PipelineStage$1, StageStatistics>; errorStatistics: Map<ErrorCategory$1, number>; } /** * Stage-specific statistics * 阶段特定统计 */ interface StageStatistics { total: number; completed: number; failed: number; averageDuration: number; } /** * Execution context factory * 执行上下文工厂 */ declare class ExecutionContextFactory { private static instance; private constructor(); static getInstance(): ExecutionContextFactory; /** * Create execution context * 创建执行上下文 */ createContext(moduleInfo: ModuleInfo, stage: PipelineStage$1, request?: any, options?: ExecutionContextOptions): PipelineExecutionContext; /** * Update context stage * 更新上下文阶段 */ updateStage(context: PipelineExecutionContext, newStage: PipelineStage$1, data?: any): void; /** * Complete context execution * 完成上下文执行 */ completeContext(context: PipelineExecutionContext, response?: any, error?: ExecutionError): void; private generateExecutionId; private generateTraceId; private generateRequestId; private sanitizeRequest; private sanitizeResponse; private createConfigSnapshot; } /** * Pipeline Request Context Interface * 流水线请求上下文接口 */ /** * Pipeline I/O Entry for tracking pipeline operations * 流水线I/O条目用于跟踪流水线操作 */ interface PipelineIOEntry { timestamp: number; pipelineId: string; pipelineName?: string; moduleId: string; operationId: string; operationType: 'pipeline_start' | 'pipeline_end' | 'module_operation' | 'data_transfer'; input?: any; output?: any; duration?: number; success: boolean; error?: string; method?: string; context?: { phase?: string; stage?: number; previousOperation?: string; nextOperation?: string; }; } /** * Pipeline Stage Status * 流水线阶段状态 */ type PipelineStageStatus = 'pending' | 'running' | 'completed' | 'failed'; /** * Pipeline Stage Information * 流水线阶段信息 */ interface PipelineStage { stage: string; startTime: number; endTime?: number; duration?: number; status: PipelineStageStatus; error?: string; data?: any; } /** * Pipeline Request Context * 流水线请求上下文 */ interface PipelineRequestContext { requestId: string; pipelineId: string; sessionId?: string; startTime: number; endTime?: number; provider: string; model?: string; operation: 'chat' | 'streamChat' | 'healthCheck'; stages: PipelineStage[]; metadata?: Record<string, any>; ioEntry?: PipelineIOEntry; } /** * Request Context Interface * 请求上下文接口 */ interface IRequestContext { getRequestId(): string; getPipelineId(): string; getSessionId(): string | undefined; getStartTime(): number; getEndTime(): number | undefined; getDuration(): number | undefined; getProvider(): string; getModel(): string | undefined; getOperation(): string; getStages(): PipelineStage[]; getMetadata(): Record<string, any> | undefined; getStage(stageName: string): PipelineStage | undefined; getStageStatus(stageName: string): PipelineStageStatus | undefined; addStage(stage: PipelineStage): void; updateStage(stageName: string, updates: Partial<PipelineStage>): void; isCompleted(): boolean; isFailed(): boolean; getFailedStages(): PipelineStage[]; getCompletedStages(): PipelineStage[]; getRunningStages(): PipelineStage[]; getStageDuration(stageName: string): number | undefined; getTotalStageDuration(): number; getSummary(): { requestId: string; pipelineId: string; provider: string; operation: string; duration: number | undefined; totalStages: number; completedStages: number; failedStages: number; status: 'pending' | 'running' | 'completed' | 'failed'; }; toObject(): PipelineRequestContext; clone(): IRequestContext; setSessionId(sessionId: string): void; setEndTime(endTime: number): void; setModel(model: string): void; setMetadata(metadata: Record<string, any>): void; } /** * Pipeline Stage Interfaces * 流水线阶段接口 */ /** * Pipeline Stage Interface * 流水线阶段接口 */ interface IPipelineStage { getStageName(): string; getStartTime(): number; getEndTime(): number | undefined; getDuration(): number | undefined; getStatus(): PipelineStageStatus; getError(): string | undefined; getData(): any; setStartTime(startTime: number): void; setEndTime(endTime: number): void; setStatus(status: PipelineStageStatus): void; setError(error: string): void; setData(data: any): void; markAsStarted(): void; markAsCompleted(data?: any): void; markAsFailed(error: string): void; isCompleted(): boolean; isFailed(): boolean; isRunning(): boolean; toObject(): PipelineStage; clone(): IPipelineStage; } /** * Pipeline Stage Factory Interface * 流水线阶段工厂接口 */ interface IPipelineStageFactory { createStage(stageName: string): IPipelineStage; createStageWithData(stageName: string, data: any): IPipelineStage; createStageFromObject(stageObject: PipelineStage): IPipelineStage; } /** * Pipeline Stage Manager Interface * 流水线阶段管理器接口 */ interface IPipelineStageManager { addStage(stage: IPipelineStage): void; getStage(stageName: string): IPipelineStage | undefined; removeStage(stageName: string): boolean; updateStage(stageName: string, updates: Partial<IPipelineStage>): boolean; getAllStages(): IPipelineStage[]; getStagesByStatus(status: PipelineStageStatus): IPipelineStage[]; getCompletedStages(): IPipelineStage[]; getFailedStages(): IPipelineStage[]; getRunningStages(): IPipelineStage[]; getStageStatistics(): { total: number; completed: number; failed: number; running: number; pending: number; }; clearAllStages(): void; } /** * Pipeline Tracker - Request ID and Pipeline Tracking System * 流水线跟踪器 - 请求ID和流水线跟踪系统 */ /** * Pipeline Tracker Main Class * 流水线跟踪器主类 */ declare class PipelineTracker extends PipelineBaseModule { protected debugCenter: DebugCenter | null; /** * Set debug center for integration * 设置调试中心用于集成 */ setDebugCenter(debugCenter: DebugCenter): void; /** * Get debug center instance * 获取调试中心实例 */ getDebugCenter(): DebugCenter | null; /** * Event listener for tracking events * 跟踪事件的事件监听器 */ subscribe(event: string, callback: (data: any) => void): void; /** * Legacy on method for backward compatibility * @deprecated Use subscribe instead */ on(event: string, callback: (data: any) => void): void; private activeRequests; private stageFactory; private stageManager; constructor(); /** * Create new request context with I/O tracking * 创建新的请求上下文并启用I/O跟踪 */ createRequestContext(provider: string, operation: 'chat' | 'streamChat' | 'healthCheck', metadata?: Record<string, any>): IRequestContext; /** * Get request context by ID * 根据ID获取请求上下文 */ getRequestContext(requestId: string): IRequestContext | undefined; /** * Add pipeline stage with I/O tracking * 添加流水线阶段并记录I/O */ addStage(requestId: string, stageName: string): void; /** * Complete pipeline stage with I/O tracking * 完成流水线阶段并记录I/O */ completeStage(requestId: string, stageName: string, data?: any): void; /** * Mark stage as failed with I/O tracking * 标记阶段为失败并记录I/O */ failStage(requestId: string, stageName: string, error: string): void; /** * Complete request context with I/O tracking * 完成请求上下文并记录I/O */ completeRequest(requestId: string): IRequestContext | undefined; /** * Get all active requests * 获取所有活动请求 */ getActiveRequests(): IRequestContext[]; /** * Get request statistics * 获取请求统计 */ getRequestStatistics(): { activeRequests: number; totalStages: number; completedStages: number; failedStages: number; runningStages: number; }; /** * Clear all active requests * 清除所有活动请求 */ clearAllRequests(): void; /** * Generate unique request ID * 生成唯一请求ID */ private generateRequestId; /** * Generate unique pipeline ID * 生成唯一流水线ID */ private generatePipelineId; /** * Get stage factory * 获取阶段工厂 */ getStageFactory(): IPipelineStageFactory; /** * Get stage manager * 获取阶段管理器 */ getStageManager(): IPipelineStageManager; /** * Create context for pipeline execution (compatibility method) * 创建流水线执行上下文(兼容性方法) */ createContext(moduleInfo: any, stage: string, request?: any, options?: any): any; /** * Record request (compatibility method) * 记录请求(兼容性方法) */ recordRequest(context: any, request: any, stage: string): Promise<void>; /** * Record response (compatibility method) * 记录响应(兼容性方法) */ recordResponse(context: any, response: any, stage: string): Promise<void>; /** * Complete context (compatibility method) * 完成上下文(兼容性方法) */ completeContext(context: any, response?: any, error?: any): void; /** * Get execution statistics (compatibility method) * 获取执行统计(兼容性方法) */ getStatistics(): any; /** * Get active contexts (compatibility method) * 获取活动上下文(兼容性方法) */ getActiveContexts(): any[]; /** * Get active trace chains (compatibility method) * 获取活动跟踪链(兼容性方法) */ getActiveTraceChains(): any[]; /** * Sanitize data for logging (compatibility method) * 清理数据用于日志记录(兼容性方法) */ sanitizeData(data: any): any; /** * Get configuration (compatibility method) * 获取配置(兼容性方法) */ getConfig(): any; /** * Update configuration (compatibility method) * 更新配置(兼容性方法) */ updateConfig(newConfig: any): void; } /** * Debuggable Pipeline Module - Enhanced pipeline module with integrated tracing * 可调试流水线模块 - 带有集成跟踪的增强流水线模块 */ /** * Debuggable pipeline module configuration * 可调试流水线模块配置 */ interface DebuggablePipelineModuleConfig { id: string; name: string; version: string; description: string; type: 'provider' | 'scheduler' | 'tracker' | 'pipeline' | 'debuggable-pipeline'; enableTracing?: boolean; tracerConfig?: Partial<any>; recordingConfig?: Partial<BaseModuleRecordingConfig>; maxConcurrentExecutions?: number; executionTimeout?: number; enablePerformanceMetrics?: boolean; enableEnhancedErrorHandling?: boolean; errorRecoveryAttempts?: number; providerName?: string; endpoint?: string; supportedModels?: string[]; defaultModel?: string; maxConcurrentRequests?: number; requestTimeout?: number; loadBalancingStrategy?: 'round-robin' | 'random' | 'weighted' | 'least-connections'; oauth?: { clientId: string; clientSecret: string; scopes: string[]; }; } /** * Execution operation options * 执行操作选项 */ interface ExecutionOptions { /** Custom execution ID */ executionId?: string; /** Custom trace ID */ traceId?: string; /** Custom session ID */ sessionId?: string; /** Request timeout in milliseconds */ timeout?: number; /** Enable retry on failure */ enableRetry?: boolean; /** Maximum retry attempts */ maxRetries?: number; /** Retry delay in milliseconds */ retryDelay?: number; /** Additional metadata */ metadata?: Record<string, any>; /** Parent execution context */ parentContext?: PipelineExecutionContext; /** Stage-specific configuration */ stageConfig?: Record<string, any>; } /** * Execution result with comprehensive information * 执行结果,包含完整信息 */ interface ExecutionResult<T = any> { /** Execution ID */ executionId: string; /** Request ID */ requestId: string; /** Trace ID */ traceId: string; /** Execution status */ status: 'success' | 'failed' | 'timeout' | 'cancelled'; /** Execution result data */ data?: T; /** Error information if failed */ error?: ExecutionError; /** Execution timing */ timing: { startTime: number; endTime: number; duration: number; }; /** Execution context */ context: PipelineExecutionContext; /** Performance metrics */ metrics?: ExecutionMetrics; /** Trace summary */ traceSummary?: TraceSummary; } /** * Execution metrics for performance analysis * 执行指标,用于性能分析 */ interface ExecutionMetrics { /** Memory usage */ memoryUsage?: { start: number; end: number; peak: number; }; /** CPU usage */ cpuUsage?: { start: number; end: number; average: number; }; /** Network requests */ networkRequests?: number; /** Database queries */ databaseQueries?: number; /** External service calls */ externalServiceCalls?: number; } /** * Trace summary for analysis * 跟踪摘要,用于分析 */ interface TraceSummary { totalStages: number; completedStages: number; failedStages: number; stageTransitions: Array<{ from: PipelineStage$1; to: PipelineStage$1; duration: number; }>; errors: ExecutionError[]; } /** * Debuggable Pipeline Module - Main class with integrated tracing and monitoring * 可调试流水线模块 - 主类,集成了跟踪和监控 */ declare class DebuggablePipelineModule extends BaseModule { protected config: DebuggablePipelineModuleConfig; protected tracker: PipelineTracker; protected errorHandler: ErrorHandlingCenter; protected contextFactory: ExecutionContextFactory; protected debugCenter: DebugCenter; constructor(config: DebuggablePipelineModuleConfig); initialize(): Promise<void>; /** * Set recording configuration * 设置录制配置 */ setRecordingConfig(config: Partial<BaseModuleRecordingConfig>): void; /** * Log debug message * 记录调试信息 */ protected logDebug(message: string, data?: any): void; /** * Log warning message * 记录警告信息 */ protected logWarn(message: string, data?: any): void; /** * Log error message * 记录错误信息 */ protected logError(message: string, data?: any): void; /** * Execute operation with full tracing and error handling * 执行操作并带有完整的跟踪和错误处理 */ executeWithTracing<T>(operation: (context: PipelineExecutionContext) => Promise<T>, stage: PipelineStage$1, request?: any, options?: ExecutionOptions): Promise<ExecutionResult<T>>; /** * Execute with timeout and retry logic * 执行带超时和重试逻辑 */ private executeWithTimeoutAndRetry; /** * Handle execution errors with comprehensive error handling * 处理执行错误并提供全面的错误处理 */ private handleExecutionError; /** * Collect performance metrics * 收集性能指标 */ private collectPerformanceMetrics; /** * Generate trace summary for analysis * 生成跟踪摘要用于分析 */ private generateTraceSummary; private categorizeError; private determineSeverity; private isErrorRecoverable; /** * Record error with tracker * 使用跟踪器记录错误 */ private recordError; private generateErrorId; private getErrorStatus; private setupTraceListeners; private handleTraceError; private handleTraceCompletion; private handleStageChange; /** * Get tracer instance * 获取跟踪器实例 */ getTracker(): PipelineTracker; /** * Get execution statistics * 获取执行统计 */ getExecutionStatistics(): any; /** * Get active execution contexts * 获取活动执行上下文 */ getActiveExecutionContexts(): PipelineExecutionContext[]; /** * Get trace chains * 获取跟踪链 */ getTraceChains(): any[]; /** * Update module configuration * 更新模块配置 */ updateConfig(newConfig: Partial<DebuggablePipelineModuleConfig>): void; destroy(): Promise<void>; } /** * OpenAI Standard Interface Definitions (TypeScript version) * 标准OpenAI接口定义 */ type MessageRole = 'system' | 'user' | 'assistant' | 'tool'; type ToolType = 'function'; type FinishReason = 'stop' | 'length' | 'tool_calls' | 'content_filter'; interface ChatMessageData { role: MessageRole; content: string | Array<{ type: string; text?: string; image_url?: { url: string; }; }>; name?: string | null; function_call?: FunctionCallData | null; tool_calls?: ToolCallData[] | null; tool_call_id?: string | null; } interface FunctionCallData { name: string; arguments: string; } interface ToolCallData { id: string; type: ToolType; function: FunctionCallData; } interface ChatToolData { type: ToolType; function: { name: string; description?: string; parameters: Record<string, any>; }; } interface ChatChoiceData { index: number; message: ChatMessageData; finish_reason: FinishReason; logprobs?: any | null; } interface UsageStatsData { prompt_tokens: number; completion_tokens: number; total_tokens: number; } interface OpenAIChatRequestData { model: string; messages: ChatMessageData[]; temperature?: number; max_tokens?: number; top_p?: number; n?: number; stream?: boolean; stop?: string | string[] | null; presence_penalty?: number; frequency_penalty?: number; logit_bias?: Record<string, number> | null; user?: string; tools?: ChatToolData[]; tool_choice?: string | Record<string, any> | null; } interface OpenAIChatResponseData { id: string; object: string; created: number; model: string; choices: ChatChoiceData[]; usage?: UsageStatsData; system_fingerprint?: string; } declare class OpenAIChatRequest$2 { model: string; messages: ChatMessageData[]; temperature?: number; max_tokens?: number; top_p?: number; n?: number; stream: boolean; stop?: string | string[] | null; presence_penalty?: number; frequency_penalty?: number; logit_bias?: Record<string, number> | null; user?: string; tools?: ChatToolData[]; tool_choice?: string | Record<string, any> | null; constructor(data: OpenAIChatRequestData); validate(): boolean; } declare class OpenAIChatResponse$2 { id: string; object: string; created: number; model: string; choices: ChatChoiceData[]; usage?: UsageStatsData; system_fingerprint?: string; constructor(data: OpenAIChatResponseData); toStandardFormat(): OpenAIChatResponseData; } declare class ChatMessage { role: MessageRole; content: string | Array<{ type: string; text?: string; image_url?: { url: string; }; }>; name: string | null; function_call: FunctionCallData | null; tool_calls: ToolCallData[] | null; tool_call_id: string | null; constructor(role: MessageRole, content: string | Array<{ type: string; text?: string; image_url?: { url: string; }; }>); static user(content: string | Array<{ type: string; text?: string; image_url?: { url: string; }; }>): ChatMessage; static assistant(content: string | Array<{ type: string; text?: string; image_url?: { url: string; }; }>, toolCalls?: ToolCallData[]): ChatMessage; static system(content: string | Array<{ type: string; text?: string; image_url?: { url: string; }; }>): ChatMessage; static tool(toolId: string, content: string): ChatMessage; } declare class ChatChoice { index: number; message: ChatMessageData; finish_reason: FinishReason; logprobs: any | null; constructor(index: number, message: ChatMessageData, finishReason?: FinishReason); } declare class ToolCall { id: string; type: ToolType; function: FunctionCallData; constructor(id: string, type: ToolType, function_: FunctionCallData); } declare class FunctionCall { name: string; arguments: string; constructor(name: string, arguments_: string); } declare class ChatTool { type: ToolType; function: { name: string; description?: string; parameters: Record<string, any>; }; constructor(type: ToolType, function_: { name: string; description?: string; parameters: Record<string, any>; }); } declare class UsageStats { prompt_tokens: number; completion_tokens: number; total_tokens: number; constructor(promptTokens?: number, completionTokens?: number, totalTokens?: number); } /** * Base Provider Class (TypeScript version) * Provider基类(包含标准OpenAI处理) */ interface ProviderCapabilities { streaming: boolean; tools: boolean; vision: boolean; jsonMode: boolean; } interface ProviderConfig { name: string; endpoint?: string; supportedModels?: string[]; defaultModel?: string; metadata?: Record<string, any>; enableTwoPhaseDebug?: boolean; debugBaseDirectory?: string; enableIOTracking?: boolean; maxConcurrentRequests?: number; requestTimeout?: number; } interface ProviderInfo$1 { name: string; endpoint: string | undefined; supportedModels: string[]; defaultModel: string | undefined; capabilities: ProviderCapabilities; type: 'provider' | 'scheduler' | 'tracker' | 'pipeline'; } interface HealthCheckResult { status: 'healthy' | 'unhealthy'; provider: string; error?: string; timestamp: string; } interface CompatibilityModule { mapRequest: (request: OpenAIChatRequest$2) => any; mapResponse: (response: any) => any; } declare abstract class BaseProvider extends PipelineBaseModule { protected endpoint?: string; protected supportedModels: string[]; protected defaultModel?: string; constructor(config: ProviderConfig); protected twoPhaseDebugSystem: any | null; chat(openaiRequest: any, compatibility?: CompatibilityModule): Promise<any>; streamChat(openaiRequest: any, compatibility?: CompatibilityModule): AsyncGenerator<any, void, unknown>; abstract executeChat(providerRequest: any): Promise<any>; abstract executeStreamChat(providerRequest: any): AsyncGenerator<any, void, unknown>; protected standardizeResponse(providerResponse: any): any; getInfo(): any; getConfig(): any; getProviderInfo(): ProviderInfo$1; protected getCapabilities(): ProviderCapabilities; healthCheck(): Promise<HealthCheckResult>; } /** * Pipeline Class - Represents a single pipeline with multiple targets * 流水线类 - 表示包含多个目标的单一流水线 */ type OperationType$2 = 'chat' | 'streamChat' | 'healthCheck'; interface PipelineTarget { id: string; provider: BaseProvider; weight: number; enabled: boolean; healthStatus: 'healthy' | 'unhealthy' | 'unknown'; lastHealthCheck: number; requestCount: number; errorCount: number; metadata?: Record<string, any>; } interface PipelineConfig { id: string; name: string; virtualModelId: string; description?: string; targets: PipelineTarget[]; loadBalancingStrategy: 'round-robin' | 'weighted' | 'least-connections' | 'random'; healthCheckInterval: number; maxRetries: number; timeout: number; metadata?: Record<string, any>; } interface PipelineMetrics { totalRequests: number; successfulRequests: number; failedRequests: number; averageResponseTime: number; currentTargets: number; healthyTargets: number; unhealthyTargets: number; errorRate: number; uptime: number; lastUsed: number; } interface PipelineExecutionOptions { timeout?: number; retries?: number; requestContext?: IRequestContext; healthCheck?: boolean; metadata?: Record<string, any>; } interface PipelineExecutionResult { pipelineId: string; targetId: string; success: boolean; response?: any; error?: string; duration: number; timestamp: number; targetMetrics: { requestCount: number; errorCount: number; healthStatus: string; }; stages?: IPipelineStage[]; } /** * Pipeline Class - Manages multiple targets with load balancing * 流水线类 - 管理多个目标并进行负载均衡 */ declare class Pipeline { config: PipelineConfig; private targets; private currentTargetIndex; private metrics; private healthCheckInterval?; private pipelineTracker; constructor(config: PipelineConfig, pipelineTracker: PipelineTracker); /** * Execute a request through the pipeline * 通过流水线执行请求 */ execute(request: any, operation: OperationType$2, options?: PipelineExecutionOptions): Promise<PipelineExecutionResult>; /** * Execute streaming request through the pipeline * 通过流水线执行流式请求 */ executeStreaming(request: any, operation: OperationType$2, options?: PipelineExecutionOptions): AsyncGenerator<PipelineExecutionResult, void, unknown>; /** * Select target based on load balancing strategy * 根据负载均衡策略选择目标 */ private selectTarget; /** * Round-robin target selection * 轮询目标选择 */ private selectRoundRobin; /** * Weighted target selection * 加权目标选择 */ private selectWeighted; /** * Least connections target selection * 最少连接目标选择 */ private selectLeastConnections; /** * Random target selection * 随机目标选择 */ private selectRandom; /** * Execute with timeout * 带超时执行 */ private executeWithTimeout; /** * Start health checks (simplified - no-op) * 启动健康检查(简化 - 空操作) */ private startHealthChecks; /** * Get healthy targets * 获取健康目标 */ private getHealthyTargets; /** * Get unhealthy targets * 获取不健康目标 */ private getUnhealthyTargets; /** * Update average response time * 更新平均响应时间 */ private updateAverageResponseTime; /** * Add target to pipeline * 向流水线添加目标 */ addTarget(target: PipelineTarget): void; /** * Remove target from pipeline * 从流水线移除目标 */ removeTarget(targetId: string): boolean; /** * Update target * 更新目标 */ updateTarget(targetId: string, updates: Partial<PipelineTarget>): boolean; /** * Get pipeline metrics * 获取流水线指标 */ getMetrics(): PipelineMetrics; /** * Get pipeline config * 获取流水线配置 */ getConfig(): PipelineConfig; /** * Get all targets * 获取所有目标 */ getTargets(): PipelineTarget[]; /** * Check if pipeline is healthy (simplified - always healthy) * 检查流水线是否健康(简化 - 总是健康的) */ isHealthy(): boolean; /** * Get target by ID * 根据ID获取目标 */ getTarget(targetId: string): PipelineTarget | undefined; /** * Enable/disable target * 启用/禁用目标 */ setTargetEnabled(targetId: string, enabled: boolean): boolean; /** * Destroy pipeline and cleanup resources * 销毁流水线并清理资源 */ destroy(): void; } /** * Virtual Model Configuration Types * 虚拟模型配置类型 */ interface VirtualModelTarget { providerId: string; modelId: string; weight?: number; enabled?: boolean; keyIndex?: number; } interface VirtualModelConfig { id: string; name: string; description?: string; modelId: string; provider: string; endpoint?: string; apiKey?: string; maxTokens?: number; temperature?: number; topP?: number; enabled: boolean; targets?: VirtualModelTarget[]; capabilities?: string[]; metadata?: Record<string, any>; createdAt?: string; updatedAt?: string; } /** * Module Scanner - Discovers and manages available providers * 模块扫描器 - 发现并管理可用的provider */ interface ProviderDiscoveryOptions { enabledProviders?: string[]; excludeProviders?: string[]; includeTestProviders?: boolean; } interface ProviderInfo { id: string; name: string; version: string; type: string; capabilities: string[]; enabled: boolean; className: string; modulePath?: string; } interface DiscoveredProvider { info: ProviderInfo; instance: BaseProvider | null; status: 'available' | 'unavailable' | 'error'; error?: string; } declare class ModuleScanner { private availableProviders; private providerInstances; private isInitialized; constructor(); /** * Initialize provider registry with known providers * 用已知的provider初始化注册表 */ private initializeProviderRegistry; /** * Scan and discover available providers based on options * 根据选项扫描并发现可用的provider */ scan(options?: ProviderDiscoveryOptions): Promise<DiscoveredProvider[]>; /** * Load provider instance using imported classes * 使用导入的类加载provider实例 */ private loadProvider; /** * Get discovered provider by ID * 通过ID获取发现的provider */ getProvider(providerId: string): BaseProvider | null; /** * Get all discovered providers * 获取所有发现的provider */ getAllProviders(): Map<string, BaseProvider>; /** * Get provider information * 获取provider信息 */ getProviderInfo(providerId: string): ProviderInfo | null; /** * Select providers based on criteria (provider + model + compatibility) * 根据条件选择provider(provider + 模型 + 兼容性) */ selectProviders(criteria?: { provider?: string; model?: string; compatibility?: string; capability?: string; }): BaseProvider[]; /** * Create pipeline targets from virtual model configuration and discovered providers * 从虚拟模型配置和发现的provider创建流水线目标 */ createTargetsFromVirtualModel(virtualModel: VirtualModelConfig): PipelineTarget[]; /** * Get scanner status * 获取扫描器状态 */ getStatus(): { initialized: boolean; totalProviders: number; availableProviders: number; providerIds: string[]; }; /** * Clear all cached providers * 清空所有缓存的provider */ clear(): void; } /** * Pipeline Factory - Creates pipeline instances from configuration * 流水线工厂 - 从配置创建流水线实例 */ interface PipelineFactoryConfig { defaultTimeout: number; defaultHealthCheckInterval: number; defaultMaxRetries: number; defaultLoadBalancingStrategy: 'round-robin' | 'weighted' | 'least-connections' | 'random'; enableHealthChecks: boolean; metricsEnabled: boolean; } interface VirtualModelPipelineConfig { virtualModel: VirtualModelConfig; providers: Map<string, BaseProvider>; metadata?: Record<string, any>; } interface PipelineCreationOptions { overrideTimeout?: number; overrideHealthCheckInterval?: number; overrideMaxRetries?: number; overrideLoadBalancingStrategy?: 'round-robin' | 'weighted' | 'least-connections' | 'random'; customMetadata?: Record<string, any>; enableHealthChecks?: boolean; } interface PipelineValidationResult { isValid: boolean; errors: string[]; warnings: string[]; pipelineConfig?: PipelineConfig; } /** * Pipeline Factory - Creates and configures pipeline instances * 流水线工厂 - 创建和配置流水线实例 */ declare class PipelineFactory { private config; private pipelineTracker; constructor(config: PipelineFactoryConfig, pipelineTracker: PipelineTracker); /** * Create pipeline from virtual model configuration * 从虚拟模型配置创建流水线 */ createPipelineFromVirtualModel(virtualModelConfig: VirtualModelPipelineConfig, options?: PipelineCreationOptions): Pipeline | null; /** * Create multiple pipelines from virtual model configurations * 从多个虚拟模型配置创建流水线 */ createPipelinesFromVirtualModels(virtualModelConfigs: VirtualModelPipelineConfig[], options?: PipelineCreationOptions): Map<string, Pipeline>; /** * Validate virtual model configuration * 验证虚拟模型配置 */ validateVirtualModelConfig(config: VirtualModelPipelineConfig): PipelineValidationResult; /** * Build pipeline configuration from virtual model config * 从虚拟模型配置构建流水线配置 */ private buildPipelineConfig; /** * Create pipeline from explicit configuration * 从显式配置创建流水线 */ createPipelineFromConfig(config: PipelineConfig): Pipeline; /** * Validate pipeline configuration * 验证流水线配置 */ validatePipelineConfig(config: PipelineConfig): PipelineValidationResult; /** * Clone pipeline configuration * 克隆流水线配置 */ clonePipelineConfig(config: PipelineConfig): PipelineConfig; /** * Get factory configuration * 获取工厂配置 */ getFactoryConfig(): PipelineFactoryConfig; /** * Update factory configuration * 更新工厂配置 */ updateFactoryConfig(updates: Partial<PipelineFactoryConfig>): void; /** * Create minimal pipeline for testing * 创建用于测试的最小流水线 */ createTestPipeline(virtualModelId: string, providers: BaseProvider[], options?: PipelineCreationOptions): Pipeline; } /** * Pipeline Assembler - Assembles pipelines from configuration and discovered providers * 流水线组装器 - 从配置和发现的provider组装流水线 */ interface AssemblerConfig { providerDiscoveryOptions?: ProviderDiscoveryOptions; pipelineFactoryConfig?: PipelineFactoryConfig; enableAutoDiscovery?: boolean; fallbackStrategy?: 'first-available' | 'round-robin'; } interface PipelinePool { virtualModelId: string; pipelines: Map<string, Pipeline>; activePipeline: Pipeline | null; healthStatus: 'healthy'; lastHealthCheck: number; metrics: { totalRequests: number; successfulRequests: number; failedRequests: number; averageResponseTime: number; }; } interface AssemblyResult { success: boolean; pipelinePools: Map<string, PipelinePool>; errors: Array<{ virtualModelId: string; error: string; provider?: string; }>; warnings: Array<{ virtualModelId: string; warning: string; }>; } /** * Pipeline Assembler - Core service for assembling pipelines from configuration * 流水线组装器 - 从配置组装流水线的核心服务 */ declare class PipelineAssembler { private config; private moduleScanner; private pipelineFactory; private pipelineTracker; private pipelinePools; private discoveredProviders; constructor(config: AssemblerConfig, pipelineTracker: PipelineTracker); /** * Assemble pipelines from virtual model configurations * 从虚拟模型配置组装流水线 */ assemblePipelines(virtualModelConfigs: VirtualModelConfig[]): Promise<AssemblyResult>; /** * Discover available providers using ModuleScanner * 使用ModuleScanner发现可用的provider */ private discoverProviders; /** * Assemble pipeline pool for a single virtual model * 为单个虚拟模型组装流水线池 */ private assemblePipelinePool; /** * Create fallback pipeline when no targets are configured * 当没有配置目标时创建回退流水线 */ private createFallbackPipeline; /** * Create pipeline from target configuration * 从目标配置创建流水线 */ private createPipelineFromTarget; /** * Build pipeline configuration from virtual model and target * 从虚拟模型和目标构建流水线配置 */ private buildPipelineConfig; /** * Get assembled pipeline pools * 获取已组装的流水线池 */ getPipelinePools(): Map<string, PipelinePool>; /** * Get pipeline pool for specific virtual model * 获取特定虚拟模型的流水线池 */ getPipelinePool(virtualModelId: string): PipelinePool | null; /** * Get all discovered providers * 获取所有发现的provider */ getDiscoveredProviders(): Map<string, BaseProvider>; /** * Reload providers and reassemble pipelines * 重新加载provider并重新组装流水线 */ reloadProviders(): Promise<void>; /** * Infer virtual model configuration from existing pool (fallback) * 从现有池推断虚拟模型配置(回退) */ private inferVirtualModelConfig; /** * Get assembler status * 获取组装器状态 */ getStatus(): { initialized: boolean; totalPools: number; totalPipelines: number; healthyPools: number; discoveredProviders: number; virtualModelIds: string[]; }; /** * Cleanup resources * 清理资源 */ destroy(): void; } /** * Pipeline Scheduler - Handles scheduling for one virtual model * 流水线调度器 - 处理单个虚拟模型的调度 */ type OperationType$1 = 'chat' | 'streamChat' | 'healthCheck'; interface SchedulerConfig { maxConcurrentRequests: number; requestTimeout: number; healthCheckInterval: number; retryStrategy: { maxRetries: number; baseDelay: number; maxDelay: number; backoffMultiplier: number; }; loadBalancingStrategy: 'round-robin' | 'weighted' | 'least-connections' | 'random'; enableCircuitBreaker: boolean; circuitBreakerThreshold: number; circuitBreakerTimeout: number; } interface SchedulerMetrics { totalRequests: number; successfulRequests: number; failedRequests: number; currentConcurrentRequests: number; averageResponseTime: number; errorRate: number; uptime: number; lastHealthCheck: number; circuitBreakerTripped: boolean; circuitBreakerTripCount: number; queueLength: number; averageQueueTime: number; } interface SchedulerHealth { status: 'healthy' | 'degraded' | 'unhealthy'; checks: { pipelineHealth: boolean; errorRate: boolean; responseTime: boolean; circuitBreaker: boolean; concurrency: boolean; }; details: { healthyPipelines: number; totalPipelines: number; currentErrorRate: number; averageResponseTime: number; circuitBreakerTripped: boolean; currentConcurrency: number; maxConcurrency: number; }; } interface RequestPriority { level: 'low' | 'medium' | 'high' | 'critical'; score: number; timestamp: number; } interface SchedulerOptions { timeout?: number; retries?: number; priority?: RequestPriority['level']; healthCheck?: boolean; circuitBreaker?: boolean; metadata?: Record<string, any>; } /** * Pipeline Scheduler - Manages request scheduling for a single virtual model * 流水线调度器 - 管理单个虚拟模型的请求调度 */ declare class PipelineScheduler { private virtualModelId; private config; private pipelines; private pipelineTracker; private currentPipelineIndex; private metrics; private requestQueue; private activeRequests; private healthCheckInterval?; private circuitBreakerState; constructor(virtualModelId: string, config: SchedulerConfig, pipelineTracker: PipelineTracker); /** * Add pipeline to scheduler * 向调度器添加流水线 */ addPipeline(pipeline: Pipeline): void; /** * Remove pipeline from scheduler * 从调度器移除流水线 */ removePipeline(pipelineId: string): boolean; /** * Execute request through scheduler * 通过调度器执行请求 */ execute(request: any, operation: OperationType$1, options?: SchedulerOptions): Promise<any>; /** * Execute streaming request through scheduler * 通过调度器执行流式请求 */ executeStreaming(request: any, operation: OperationType$1, options?: SchedulerOptions): AsyncGenerator<any, void, unknown>; /** * Execute with retry logic * 带重试逻辑执行 */ private executeWithRetry; /** * Queue request for later processing * 将请求排队等待后续处理 */ private queueRequest; /** * Start request processing from queue * 开始从队列处理请求 */ private startRequestProcessing; /** * Process queued requests * 处理排队请求 */ private processQueue; /** * Select pipeline using configured strategy * 使用配置策略选择流水线 */ private selectPipeline; /** * Round-robin pipeline selection * 轮询流水线选择 */ private selectRoundRobin; /** * Weighted pipeline selection * 加权流水线选择 */ private selectWeighted; /** * Least connections pipeline selection * 最少连接流水线选择 */ private selectLeastConnections; /** * Random pipeline selection * 随机流水线选择 */ private selectRandom; /** * Calculate retry delay with exponential backoff * 计算带指数退避的重试延迟 */ private calculateRetryDelay; /** * Calculate priority score * 计算优先级分数 */ private calculatePriorityScore; /** * Circuit breaker operations * 熔断器操作 */ private isCircuitBreakerTripped; private updateCircuitBreaker; private resetCircuitBreaker; /** * Health check operations * 健康检查操作 */ private startHealthChecks; private performHealthCheck; private updateHealthCheck; /** * Update average response time * 更新平均响应时间 */ private updateAverageResponseTime; /** * Utility functions * 实用函数 */ private sleep; /** * Get scheduler metrics * 获取调度器指标 */ getMetrics(): SchedulerMetrics; /** * Get scheduler health (simplified - always healthy) * 获取调度器健康状态(简化 - 总是健康的) */ getHealth(): SchedulerHealth; /** * Get scheduler configuration * 获取调度器配置 */ getConfig(): SchedulerConfig; /** * Get all pipelines * 获取所有流水线 */ getPipelines(): Pipeline[]; /** * Get virtual model ID * 获取虚拟模型ID */ getVirtualModelId(): string; /** * Destroy scheduler and cleanup resources * 销毁调度器并清理资源 */ destroy(): void; } /** * Virtual Model Scheduler Manager - Manages all scheduler instances * 虚拟模型调度器管理器 - 管理所有调度器实例 */ type OperationType = 'chat' | 'streamChat' | 'healthCheck'; interface ManagerConfig { maxSchedulers: number; defaultSchedulerConfig: SchedulerConfig; enableAutoScaling: