rcc-pipeline
Version:
RCC Pipeline Module - Pipeline system and workflow management based on pipeline-framework
1,477 lines (1,472 loc) • 219 kB
JavaScript
import { BaseModule } from 'rcc-basemodule';
import { ErrorHandlingCenter } from 'rcc-errorhandling';
import { DebugCenter } from 'rcc-debugcenter';
import axios from 'axios';
import * as crypto from 'crypto';
import crypto__default from 'crypto';
import open from 'open';
import * as fs from 'fs';
import fs__default from 'fs';
import * as path from 'path';
import path__default from 'path';
import * as os from 'os';
import os__default from 'os';
/**
* Pipeline Base Module - Base module for pipeline components with enhanced debug capabilities
* 流水线基础模块 - 具有增强调试功能的流水线组件基础模块
*/
/**
* Pipeline Base Module with enhanced debug capabilities
* 具有增强调试功能的流水线基础模块
*/
class PipelineBaseModule extends BaseModule {
constructor(config) {
// Create module info for BaseModule
const moduleInfo = {
id: config.id,
name: config.name,
version: config.version,
description: config.description,
type: config.type
};
super(moduleInfo);
this.debugCenter = null;
this.pipelineConfig = config;
// Initialize error handler
this.errorHandler = new ErrorHandlingCenter({
id: `${config.id}-error-handler`,
name: `${config.name} Error Handler`,
version: '1.0.0',
type: 'error-handler',
description: `Error handler for ${config.name}`
});
// Enable two-phase debug if configured
if (config.enableTwoPhaseDebug) {
const debugConfig = {
enabled: true,
level: 'debug',
recordStack: true,
maxLogEntries: 1000,
consoleOutput: true,
trackDataFlow: true,
enableFileLogging: true,
maxFileSize: 10485760, // 10MB
maxLogFiles: 5,
baseDirectory: config.debugBaseDirectory || '~/.rcc/debug-logs',
ioTracking: config.ioTrackingConfig || {
enabled: config.enableIOTracking || false,
autoRecord: false,
saveIndividualFiles: true,
saveSessionFiles: false,
ioDirectory: `${config.debugBaseDirectory || '~/.rcc/debug-logs'}/io`,
includeTimestamp: true,
includeDuration: true,
maxEntriesPerFile: 100
}
};
this.setDebugConfig(debugConfig);
if (config.enableIOTracking) {
this.enableTwoPhaseDebug(true, config.debugBaseDirectory || '~/.rcc/debug-logs', config.ioTrackingConfig);
}
}
this.logInfo('Pipeline base module initialized', { config }, 'constructor');
// Store debug center reference if available
this.debugCenter = this.getDebugCenter();
}
/**
* Enable two-phase debug system
* 启用两阶段调试系统
*/
enableTwoPhaseDebug(enabled, baseDirectory, ioTrackingConfig) {
// Method implementation would go here
this.logInfo('Two-phase debug system enabled', { enabled, baseDirectory, ioTrackingConfig }, 'enableTwoPhaseDebug');
}
/**
* Get debug center instance
* 获取调试中心实例
*/
getDebugCenter() {
return this.debugCenter;
}
/**
* Get pipeline configuration
* 获取流水线配置
*/
getPipelineConfig() {
return { ...this.pipelineConfig };
}
/**
* Update pipeline configuration
* 更新流水线配置
*/
updatePipelineConfig(newConfig) {
const oldConfig = { ...this.pipelineConfig };
this.pipelineConfig = { ...this.pipelineConfig, ...newConfig };
this.logInfo('Pipeline configuration updated', {
oldConfig,
newConfig: this.pipelineConfig
}, 'updatePipelineConfig');
}
/**
* Get provider information
* 获取提供者信息
*/
getProviderInfo() {
return {
name: this.pipelineConfig.providerName || this.pipelineConfig.name,
endpoint: this.pipelineConfig.endpoint,
supportedModels: this.pipelineConfig.supportedModels || [],
defaultModel: this.pipelineConfig.defaultModel,
type: this.pipelineConfig.type
};
}
/**
* Track pipeline operation with I/O tracking
* 跟踪流水线操作并记录I/O
*/
async trackPipelineOperation(operationId, operation, inputData, operationType = 'pipeline-operation') {
const startTime = Date.now();
try {
// Start I/O tracking if enabled
if (this.pipelineConfig.enableIOTracking && this.debugCenter) {
this.debugCenter.recordOperation(`${this.info.id}-${operationId}`, this.info.id, operationId, inputData, undefined, operationType, true, undefined, 'middle');
}
// Execute the operation
const result = await operation();
// End I/O tracking if enabled
if (this.pipelineConfig.enableIOTracking && this.debugCenter) {
this.debugCenter.recordOperation(`${this.info.id}-${operationId}`, this.info.id, operationId, undefined, result, operationType, true, undefined, 'middle');
}
this.logInfo('Pipeline operation completed successfully', {
operationId,
operationType,
duration: Date.now() - startTime,
inputData: inputData ? { type: typeof inputData } : undefined,
outputData: result ? { type: typeof result } : undefined
}, 'trackPipelineOperation');
return result;
}
catch (error) {
// End I/O tracking with error if enabled
if (this.pipelineConfig.enableIOTracking && this.debugCenter) {
this.debugCenter.recordOperation(`${this.info.id}-${operationId}`, this.info.id, operationId, undefined, undefined, operationType, false, error instanceof Error ? error.message : String(error), 'middle');
}
this.debug('error', 'Pipeline operation failed', {
operationId,
operationType,
duration: Date.now() - startTime,
inputData: inputData ? { type: typeof inputData } : undefined,
error: error instanceof Error ? { message: error.message, stack: error.stack } : String(error)
}, 'trackPipelineOperation');
throw error;
}
}
/**
* Record pipeline stage
* 记录流水线阶段
*/
recordPipelineStage(stageName, stageData, status = 'started') {
const timestamp = Date.now();
this.logInfo(`Pipeline stage ${status}`, {
stageName,
stageData: stageData ? { type: typeof stageData } : undefined,
status,
timestamp
}, 'recordPipelineStage');
}
/**
* Handle pipeline errors with enhanced error handling
* 处理流水线错误并提供增强的错误处理
*/
handlePipelineError(error, context) {
const errorContext = {
...context,
moduleId: this.info.id,
moduleName: this.info.name,
timestamp: Date.now(),
error: {
message: error.message,
stack: error.stack,
name: error.name
}
};
// Log the error
this.debug('error', 'Pipeline error occurred', errorContext, 'handlePipelineError');
// Handle error with error handling center
this.errorHandler.handleError({
error: error,
source: this.info.id,
severity: 'high',
timestamp: Date.now()
});
}
/**
* Get pipeline metrics
* 获取流水线指标
*/
getPipelineMetrics() {
if (this.debugCenter) {
return {
debugEnabled: true,
ioTrackingEnabled: this.pipelineConfig.enableIOTracking || false,
debugConfig: this.getDebugConfig(),
pipelineEntries: this.debugCenter.getPipelineEntries ? this.debugCenter.getPipelineEntries({
pipelineId: this.info.id,
limit: 100
}) : [],
stats: this.debugCenter.getStats ? this.debugCenter.getStats() : null
};
}
return {
debugEnabled: false,
ioTrackingEnabled: false,
debugConfig: this.getDebugConfig()
};
}
/**
* Override destroy method to ensure proper cleanup
* 重写destroy方法以确保正确的清理
*/
async destroy() {
try {
this.logInfo('Destroying pipeline base module', { moduleId: this.info.id }, 'destroy');
// Perform any additional cleanup specific to pipeline modules
if (this.errorHandler) {
// ErrorHandlingCenter cleanup if available
if (typeof this.errorHandler.destroy === 'function') {
await this.errorHandler.destroy();
}
}
// Call parent destroy method
await super.destroy();
}
catch (error) {
this.debug('error', 'Failed to destroy pipeline base module', {
error: error instanceof Error ? { message: error.message, stack: error.stack } : String(error),
moduleId: this.info.id
}, 'destroy');
throw error;
}
}
}
/**
* Pipeline Execution Context - Enhanced request-response tracing system
* 流水线执行上下文 - 增强的请求响应跟踪系统
*/
/**
* Pipeline stage enumeration
* 流水线阶段枚举
*/
var PipelineStage;
(function (PipelineStage) {
PipelineStage["REQUEST_INIT"] = "request_init";
PipelineStage["AUTHENTICATION"] = "authentication";
PipelineStage["SCHEDULING"] = "scheduling";
PipelineStage["PIPELINE_SELECTION"] = "pipeline_selection";
PipelineStage["PROVIDER_EXECUTION"] = "provider_execution";
PipelineStage["RESPONSE_PROCESSING"] = "response_processing";
PipelineStage["COMPLETION"] = "completion";
PipelineStage["ERROR_HANDLING"] = "error_handling";
})(PipelineStage || (PipelineStage = {}));
/**
* Error category enumeration
* 错误类别枚举
*/
var ErrorCategory$1;
(function (ErrorCategory) {
ErrorCategory["NETWORK"] = "network";
ErrorCategory["AUTHENTICATION"] = "authentication";
ErrorCategory["VALIDATION"] = "validation";
ErrorCategory["PROCESSING"] = "processing";
ErrorCategory["PROVIDER"] = "provider";
ErrorCategory["SYSTEM"] = "system";
ErrorCategory["TIMEOUT"] = "timeout";
ErrorCategory["RATE_LIMIT"] = "rate_limit";
})(ErrorCategory$1 || (ErrorCategory$1 = {}));
/**
* Error severity enumeration
* 错误严重性枚举
*/
var ErrorSeverity$1;
(function (ErrorSeverity) {
ErrorSeverity["FATAL"] = "fatal";
ErrorSeverity["ERROR"] = "error";
ErrorSeverity["WARNING"] = "warning";
ErrorSeverity["INFO"] = "info";
})(ErrorSeverity$1 || (ErrorSeverity$1 = {}));
/**
* Execution context factory
* 执行上下文工厂
*/
class ExecutionContextFactory {
constructor() { }
static getInstance() {
if (!this.instance) {
this.instance = new ExecutionContextFactory();
}
return this.instance;
}
/**
* Create execution context
* 创建执行上下文
*/
createContext(moduleInfo, stage, request, options) {
const now = Date.now();
const executionId = options?.executionId || this.generateExecutionId();
const traceId = options?.traceId || (options?.inheritFrom?.traceId || this.generateTraceId());
const sessionId = options?.sessionId || options?.inheritFrom?.sessionId;
const context = {
executionId,
requestId: this.generateRequestId(),
traceId,
sessionId,
stage,
module: moduleInfo,
timing: {
startTime: now,
stageTimings: new Map([[stage, {
startTime: now,
status: 'running'
}]])
},
request: this.sanitizeRequest(request),
metadata: options?.metadata || {},
config: this.createConfigSnapshot(options?.config),
children: [],
parent: options?.inheritFrom
};
if (options?.inheritFrom) {
options.inheritFrom.children?.push(context);
}
return context;
}
/**
* Update context stage
* 更新上下文阶段
*/
updateStage(context, newStage, data) {
const now = Date.now();
// Complete current stage
const currentStageTiming = context.timing.stageTimings.get(context.stage);
if (currentStageTiming && !currentStageTiming.endTime) {
currentStageTiming.endTime = now;
currentStageTiming.duration = now - currentStageTiming.startTime;
currentStageTiming.status = 'completed';
}
// Update to new stage
context.stage = newStage;
context.timing.stageTimings.set(newStage, {
startTime: now,
status: 'running'
});
// Update metadata if provided
if (data) {
context.metadata = { ...context.metadata, ...data };
}
}
/**
* Complete context execution
* 完成上下文执行
*/
completeContext(context, response, error) {
const now = Date.now();
context.timing.endTime = now;
context.timing.duration = now - context.timing.startTime;
// Complete current stage
const currentStageTiming = context.timing.stageTimings.get(context.stage);
if (currentStageTiming && !currentStageTiming.endTime) {
currentStageTiming.endTime = now;
currentStageTiming.duration = now - currentStageTiming.startTime;
currentStageTiming.status = error ? 'failed' : 'completed';
}
// Set response or error
if (response) {
context.response = this.sanitizeResponse(response);
}
if (error) {
context.error = error;
}
}
generateExecutionId() {
return `exec_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
}
generateTraceId() {
return `trace_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
}
generateRequestId() {
return `req_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
}
sanitizeRequest(request) {
if (!request)
return request;
// Remove sensitive data
const sanitized = { ...request };
const sensitiveKeys = ['password', 'token', 'apiKey', 'secret', 'auth'];
for (const key of sensitiveKeys) {
if (sanitized[key]) {
sanitized[key] = '[REDACTED]';
}
}
return sanitized;
}
sanitizeResponse(response) {
if (!response)
return response;
// Remove sensitive data
const sanitized = { ...response };
const sensitiveKeys = ['token', 'apiKey', 'secret', 'privateKey'];
for (const key of sensitiveKeys) {
if (sanitized[key]) {
sanitized[key] = '[REDACTED]';
}
}
return sanitized;
}
createConfigSnapshot(config) {
return {
recordingConfig: config || { enabled: false },
baseDirectory: config?.basePath || '~/.rcc/debug',
port: config?.port,
environment: process.env.NODE_ENV || 'development',
timestamp: Date.now()
};
}
}
/**
* Pipeline Tracker - Request ID and Pipeline Tracking System
* 流水线跟踪器 - 请求ID和流水线跟踪系统
*/
/**
* Request Context Implementation
* 请求上下文实现
*/
class RequestContextImpl {
constructor(data) {
this.data = { ...data };
}
getRequestId() {
return this.data.requestId;
}
getPipelineId() {
return this.data.pipelineId;
}
getSessionId() {
return this.data.sessionId;
}
getStartTime() {
return this.data.startTime;
}
getEndTime() {
return this.data.endTime;
}
getDuration() {
if (this.data.endTime) {
return this.data.endTime - this.data.startTime;
}
return undefined;
}
getProvider() {
return this.data.provider;
}
getModel() {
return this.data.model;
}
getOperation() {
return this.data.operation;
}
getStages() {
return [...this.data.stages];
}
getMetadata() {
return this.data.metadata ? { ...this.data.metadata } : undefined;
}
getStage(stageName) {
return this.data.stages.find(s => s.stage === stageName);
}
getStageStatus(stageName) {
const stage = this.getStage(stageName);
return stage?.status;
}
addStage(stage) {
this.data.stages.push({ ...stage });
}
updateStage(stageName, updates) {
const stage = this.getStage(stageName);
if (stage) {
Object.assign(stage, updates);
}
}
isCompleted() {
return this.data.endTime !== undefined;
}
isFailed() {
return this.data.stages.some(s => s.status === 'failed');
}
getFailedStages() {
return this.data.stages.filter(s => s.status === 'failed');
}
getCompletedStages() {
return this.data.stages.filter(s => s.status === 'completed');
}
getRunningStages() {
return this.data.stages.filter(s => s.status === 'running');
}
getStageDuration(stageName) {
const stage = this.getStage(stageName);
if (stage && stage.endTime) {
return stage.endTime - stage.startTime;
}
return undefined;
}
getTotalStageDuration() {
return this.data.stages
.filter(s => s.endTime)
.reduce((total, stage) => total + (stage.endTime - stage.startTime), 0);
}
getSummary() {
const completedStages = this.getCompletedStages();
const failedStages = this.getFailedStages();
let status = 'pending';
if (this.isCompleted()) {
status = failedStages.length > 0 ? 'failed' : 'completed';
}
else if (this.data.stages.length > 0) {
status = 'running';
}
return {
requestId: this.data.requestId,
pipelineId: this.data.pipelineId,
provider: this.data.provider,
operation: this.data.operation,
duration: this.getDuration(),
totalStages: this.data.stages.length,
completedStages: completedStages.length,
failedStages: failedStages.length,
status
};
}
toObject() {
return { ...this.data };
}
clone() {
return new RequestContextImpl(this.toObject());
}
setSessionId(sessionId) {
this.data.sessionId = sessionId;
}
setEndTime(endTime) {
this.data.endTime = endTime;
}
setModel(model) {
this.data.model = model;
}
setMetadata(metadata) {
this.data.metadata = { ...metadata };
}
}
/**
* Pipeline Stage Implementation
* 流水线阶段实现
*/
class PipelineStageImpl {
constructor(data) {
this.data = { ...data };
}
getStageName() {
return this.data.stage;
}
getStartTime() {
return this.data.startTime;
}
getEndTime() {
return this.data.endTime;
}
getDuration() {
if (this.data.endTime) {
return this.data.endTime - this.data.startTime;
}
return undefined;
}
getStatus() {
return this.data.status;
}
getError() {
return this.data.error;
}
getData() {
return this.data.data;
}
setStartTime(startTime) {
this.data.startTime = startTime;
}
setEndTime(endTime) {
this.data.endTime = endTime;
}
setStatus(status) {
this.data.status = status;
}
setError(error) {
this.data.error = error;
}
setData(data) {
this.data.data = data;
}
markAsStarted() {
this.data.startTime = Date.now();
this.data.status = 'running';
}
markAsCompleted(data) {
this.data.endTime = Date.now();
this.data.status = 'completed';
if (data !== undefined) {
this.data.data = data;
}
}
markAsFailed(error) {
this.data.endTime = Date.now();
this.data.status = 'failed';
this.data.error = error;
}
isCompleted() {
return this.data.status === 'completed';
}
isFailed() {
return this.data.status === 'failed';
}
isRunning() {
return this.data.status === 'running';
}
toObject() {
return { ...this.data };
}
clone() {
return new PipelineStageImpl(this.toObject());
}
}
/**
* Pipeline Stage Factory Implementation
* 流水线阶段工厂实现
*/
class PipelineStageFactoryImpl {
createStage(stageName) {
return new PipelineStageImpl({
stage: stageName,
startTime: Date.now(),
status: 'pending'
});
}
createStageWithData(stageName, data) {
return new PipelineStageImpl({
stage: stageName,
startTime: Date.now(),
status: 'pending',
data
});
}
createStageFromObject(stageObject) {
return new PipelineStageImpl(stageObject);
}
}
/**
* Pipeline Stage Manager Implementation
* 流水线阶段管理器实现
*/
class PipelineStageManagerImpl {
constructor(stageFactory) {
this.stages = new Map();
this.stageFactory = stageFactory;
}
addStage(stage) {
this.stages.set(stage.getStageName(), stage);
}
getStage(stageName) {
return this.stages.get(stageName);
}
removeStage(stageName) {
return this.stages.delete(stageName);
}
updateStage(stageName, updates) {
const stage = this.getStage(stageName);
if (stage) {
if (updates.getStageName && updates.getStageName() !== undefined) {
const newStageName = updates.getStageName();
this.stages.delete(stageName);
this.stages.set(newStageName, stage);
}
return true;
}
return false;
}
getAllStages() {
return Array.from(this.stages.values());
}
getStagesByStatus(status) {
return this.getAllStages().filter(stage => stage.getStatus() === status);
}
getCompletedStages() {
return this.getStagesByStatus('completed');
}
getFailedStages() {
return this.getStagesByStatus('failed');
}
getRunningStages() {
return this.getStagesByStatus('running');
}
clearAllStages() {
this.stages.clear();
}
getStageStatistics() {
const stages = this.getAllStages();
return {
total: stages.length,
completed: stages.filter(s => s.isCompleted()).length,
failed: stages.filter(s => s.isFailed()).length,
running: stages.filter(s => s.isRunning()).length,
pending: stages.filter(s => s.getStatus() === 'pending').length
};
}
}
/**
* Pipeline Tracker Main Class
* 流水线跟踪器主类
*/
class PipelineTracker extends PipelineBaseModule {
/**
* Set debug center for integration
* 设置调试中心用于集成
*/
setDebugCenter(debugCenter) {
this.debugCenter = debugCenter;
}
/**
* Get debug center instance
* 获取调试中心实例
*/
getDebugCenter() {
return this.debugCenter;
}
/**
* Event listener for tracking events
* 跟踪事件的事件监听器
*/
subscribe(event, callback) {
// Simple event listener implementation
// In a real implementation, this would use an event emitter
this.logInfo(`Event listener registered for: ${event}`, { event }, 'subscribe');
}
/**
* Legacy on method for backward compatibility
* @deprecated Use subscribe instead
*/
on(event, callback) {
this.subscribe(event, callback);
}
constructor() {
const config = {
id: 'pipeline-tracker',
name: 'Pipeline Tracker',
version: '1.0.0',
description: 'Pipeline request tracking and stage management system',
type: 'tracker',
enableTwoPhaseDebug: true,
enableIOTracking: true,
debugBaseDirectory: '~/.rcc/debug-logs'
};
super(config);
this.debugCenter = null;
this.activeRequests = new Map();
this.stageFactory = new PipelineStageFactoryImpl();
this.stageManager = new PipelineStageManagerImpl(this.stageFactory);
this.logInfo('Pipeline tracker initialized', {}, 'constructor');
}
/**
* Create new request context with I/O tracking
* 创建新的请求上下文并启用I/O跟踪
*/
createRequestContext(provider, operation, metadata) {
const contextData = {
requestId: this.generateRequestId(),
pipelineId: this.generatePipelineId(),
startTime: Date.now(),
provider,
operation,
stages: [],
metadata
};
const context = new RequestContextImpl(contextData);
this.activeRequests.set(context.getRequestId(), context);
// Start I/O tracking if enabled
if (this.debugCenter) {
this.debugCenter.recordPipelineStart(contextData.requestId, contextData.pipelineId, `Pipeline: ${provider} ${operation}`, { provider, operation, metadata }, { method: 'createRequestContext' });
}
this.logInfo('Request context created', {
requestId: contextData.requestId,
provider,
operation
}, 'createRequestContext');
return context;
}
/**
* Get request context by ID
* 根据ID获取请求上下文
*/
getRequestContext(requestId) {
return this.activeRequests.get(requestId);
}
/**
* Add pipeline stage with I/O tracking
* 添加流水线阶段并记录I/O
*/
addStage(requestId, stageName) {
const context = this.activeRequests.get(requestId);
if (context) {
const stage = this.stageFactory.createStage(stageName);
stage.markAsStarted();
context.addStage(stage.toObject());
this.stageManager.addStage(stage);
// Record stage operation in I/O tracking
if (this.debugCenter) {
this.debugCenter.recordOperation(context.getPipelineId(), this.info.id, `${requestId}-${stageName}`, { stageName, action: 'start' }, undefined, 'addStage', true, undefined, 'middle');
}
this.logInfo('Pipeline stage added', {
requestId,
stageName,
status: 'started'
}, 'addStage');
}
}
/**
* Complete pipeline stage with I/O tracking
* 完成流水线阶段并记录I/O
*/
completeStage(requestId, stageName, data) {
const context = this.activeRequests.get(requestId);
if (context) {
const stage = this.stageManager.getStage(stageName);
if (stage) {
stage.markAsCompleted(data);
context.updateStage(stageName, stage.toObject());
// Record stage completion in I/O tracking
if (this.debugCenter) {
this.debugCenter.recordOperation(context.getPipelineId(), this.info.id, `${requestId}-${stageName}`, { stageName, action: 'complete' }, { data }, 'completeStage', true, undefined, 'middle');
}
this.logInfo('Pipeline stage completed', {
requestId,
stageName,
duration: stage.getDuration()
}, 'completeStage');
}
}
}
/**
* Mark stage as failed with I/O tracking
* 标记阶段为失败并记录I/O
*/
failStage(requestId, stageName, error) {
const context = this.activeRequests.get(requestId);
if (context) {
const stage = this.stageManager.getStage(stageName);
if (stage) {
stage.markAsFailed(error);
context.updateStage(stageName, stage.toObject());
// Record stage failure in I/O tracking
if (this.debugCenter) {
this.debugCenter.recordOperation(context.getPipelineId(), this.info.id, `${requestId}-${stageName}`, { stageName, action: 'fail' }, undefined, 'failStage', false, error, 'middle');
}
this.logInfo('Pipeline stage failed', {
requestId,
stageName,
error
}, 'failStage');
}
}
}
/**
* Complete request context with I/O tracking
* 完成请求上下文并记录I/O
*/
completeRequest(requestId) {
const context = this.activeRequests.get(requestId);
if (context) {
context.setEndTime(Date.now());
// Record pipeline completion in I/O tracking
if (this.debugCenter) {
this.debugCenter.recordPipelineEnd(context.getRequestId(), context.getPipelineId(), `Pipeline: ${context.getPipelineId()}`, { duration: context.getDuration(), stages: context.getStages() }, !context.isFailed(), context.isFailed() ? 'Pipeline failed' : undefined, { method: 'completeRequest' });
}
this.activeRequests.delete(requestId);
this.logInfo('Request context completed', {
requestId,
duration: context.getDuration(),
success: !context.isFailed()
}, 'completeRequest');
return context;
}
return undefined;
}
/**
* Get all active requests
* 获取所有活动请求
*/
getActiveRequests() {
return Array.from(this.activeRequests.values());
}
/**
* Get request statistics
* 获取请求统计
*/
getRequestStatistics() {
const activeRequests = this.getActiveRequests();
return {
activeRequests: activeRequests.length,
totalStages: activeRequests.reduce((total, req) => total + req.getStages().length, 0),
completedStages: activeRequests.reduce((total, req) => total + req.getCompletedStages().length, 0),
failedStages: activeRequests.reduce((total, req) => total + req.getFailedStages().length, 0),
runningStages: activeRequests.reduce((total, req) => total + req.getRunningStages().length, 0)
};
}
/**
* Clear all active requests
* 清除所有活动请求
*/
clearAllRequests() {
this.activeRequests.clear();
this.stageManager.clearAllStages();
this.logInfo('All requests cleared', {}, 'clearAllRequests');
}
/**
* Generate unique request ID
* 生成唯一请求ID
*/
generateRequestId() {
return `req_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
}
/**
* Generate unique pipeline ID
* 生成唯一流水线ID
*/
generatePipelineId() {
return `pipeline_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
}
/**
* Get stage factory
* 获取阶段工厂
*/
getStageFactory() {
return this.stageFactory;
}
/**
* Get stage manager
* 获取阶段管理器
*/
getStageManager() {
return this.stageManager;
}
/**
* Create context for pipeline execution (compatibility method)
* 创建流水线执行上下文(兼容性方法)
*/
createContext(moduleInfo, stage, request, options) {
const requestId = this.generateRequestId();
const context = this.createRequestContext(moduleInfo.providerName || 'unknown', 'chat', { moduleInfo, stage, request, options });
// Add stage information
this.addStage(requestId, stage);
return {
executionId: requestId,
requestId: requestId,
traceId: context.getPipelineId(),
stage: stage,
module: moduleInfo,
timing: {
startTime: Date.now(),
stageTimings: new Map()
},
request: request,
metadata: options?.metadata || {},
parent: options?.parentContext
};
}
/**
* Record request (compatibility method)
* 记录请求(兼容性方法)
*/
async recordRequest(context, request, stage) {
if (this.debugCenter) {
this.debugCenter.recordOperation(context.traceId, context.module?.moduleId || 'unknown', `${context.executionId}-request`, { stage, request }, undefined, 'recordRequest', true, undefined, 'middle');
}
}
/**
* Record response (compatibility method)
* 记录响应(兼容性方法)
*/
async recordResponse(context, response, stage) {
if (this.debugCenter) {
this.debugCenter.recordOperation(context.traceId, context.module?.moduleId || 'unknown', `${context.executionId}-response`, { stage, response }, undefined, 'recordResponse', true, undefined, 'middle');
}
}
/**
* Complete context (compatibility method)
* 完成上下文(兼容性方法)
*/
completeContext(context, response, error) {
const requestId = context.executionId || context.requestId;
this.completeRequest(requestId);
}
/**
* Get execution statistics (compatibility method)
* 获取执行统计(兼容性方法)
*/
getStatistics() {
return this.getRequestStatistics();
}
/**
* Get active contexts (compatibility method)
* 获取活动上下文(兼容性方法)
*/
getActiveContexts() {
return this.getActiveRequests();
}
/**
* Get active trace chains (compatibility method)
* 获取活动跟踪链(兼容性方法)
*/
getActiveTraceChains() {
return this.getActiveRequests().map(req => ({
traceId: req.getPipelineId(),
requests: [req]
}));
}
/**
* Sanitize data for logging (compatibility method)
* 清理数据用于日志记录(兼容性方法)
*/
sanitizeData(data) {
if (!data)
return data;
const sanitized = { ...data };
const sensitiveKeys = ['password', 'token', 'apiKey', 'secret', 'auth'];
for (const key of sensitiveKeys) {
if (sanitized[key]) {
sanitized[key] = '[REDACTED]';
}
}
return sanitized;
}
/**
* Get configuration (compatibility method)
* 获取配置(兼容性方法)
*/
getConfig() {
return {
enabled: this.pipelineConfig.enableIOTracking || false,
maxActiveTraces: 1000,
traceRetentionTime: 300000,
enableChainTracking: true,
enableMetrics: this.pipelineConfig.enableIOTracking || false,
maxContextDepth: 10,
samplingRate: 1.0,
enableRealTimeMonitoring: true
};
}
/**
* Update configuration (compatibility method)
* 更新配置(兼容性方法)
*/
updateConfig(newConfig) {
this.updatePipelineConfig({
enableIOTracking: newConfig.enabled
});
}
}
/**
* Error Types for Pipeline Module
* 流水线模块的错误类型
*/
/**
* Error category enumeration
* 错误类别枚举
*/
var ErrorCategory;
(function (ErrorCategory) {
ErrorCategory["NETWORK"] = "network";
ErrorCategory["AUTHENTICATION"] = "authentication";
ErrorCategory["VALIDATION"] = "validation";
ErrorCategory["PROCESSING"] = "processing";
ErrorCategory["PROVIDER"] = "provider";
ErrorCategory["SYSTEM"] = "system";
ErrorCategory["TIMEOUT"] = "timeout";
ErrorCategory["RATE_LIMIT"] = "rate_limit";
ErrorCategory["FATAL"] = "fatal";
})(ErrorCategory || (ErrorCategory = {}));
/**
* Error severity enumeration
* 错误严重性枚举
*/
var ErrorSeverity;
(function (ErrorSeverity) {
ErrorSeverity["FATAL"] = "fatal";
ErrorSeverity["ERROR"] = "error";
ErrorSeverity["WARNING"] = "warning";
ErrorSeverity["INFO"] = "info";
})(ErrorSeverity || (ErrorSeverity = {}));
/**
* Trace event types
* 跟踪事件类型
*/
var TraceEventType;
(function (TraceEventType) {
TraceEventType["ERROR_OCCURRED"] = "error_occurred";
TraceEventType["CONTEXT_COMPLETED"] = "context_completed";
TraceEventType["CONTEXT_STARTED"] = "context_started";
TraceEventType["STAGE_CHANGED"] = "stage_changed";
TraceEventType["REQUEST_RECEIVED"] = "request_received";
TraceEventType["RESPONSE_SENT"] = "response_sent";
})(TraceEventType || (TraceEventType = {}));
/**
* Debuggable Pipeline Module - Enhanced pipeline module with integrated tracing
* 可调试流水线模块 - 带有集成跟踪的增强流水线模块
*/
/**
* Debuggable Pipeline Module - Main class with integrated tracing and monitoring
* 可调试流水线模块 - 主类,集成了跟踪和监控
*/
class DebuggablePipelineModule extends BaseModule {
constructor(config) {
const moduleInfo = {
id: config.id,
name: config.name,
version: config.version,
description: config.description,
type: config.type
};
super(moduleInfo);
this.config = config;
// Initialize execution context factory
this.contextFactory = ExecutionContextFactory.getInstance();
// Initialize error handler
this.errorHandler = new ErrorHandlingCenter({
id: `${config.id}-error-handler`,
name: `${config.name} Error Handler`,
version: '1.0.0',
type: 'error-handler',
description: `Error handler for ${config.name}`
});
// Initialize tracker
this.tracker = new PipelineTracker();
// Initialize debug center
this.debugCenter = new DebugCenter({
enabled: config.enableTracing !== false,
baseDirectory: config.recordingConfig?.basePath || '~/.rcc/debug-logs',
maxLogEntries: 1000,
recordStack: true
});
// Configure tracker with debug center
if (config.enableTracing !== false) {
this.tracker.setDebugCenter(this.debugCenter);
}
}
async initialize() {
await super.initialize();
// Initialize tracker
await this.tracker.initialize();
// Set up trace event listeners
this.setupTraceListeners();
// Enable recording if configured
if (this.config.recordingConfig?.enabled) {
this.setRecordingConfig(this.config.recordingConfig);
// IO tracking is enabled through recording config
}
this.logInfo('Debuggable pipeline module initialized', { config: this.config });
}
/**
* Set recording configuration
* 设置录制配置
*/
setRecordingConfig(config) {
// Use DebugCenter's updateConfig method instead of non-existent setRecordingConfig
if (this.debugCenter && typeof this.debugCenter.updateConfig === 'function') {
this.debugCenter.updateConfig({
enabled: config.enabled !== false,
level: 'debug',
recordStack: true,
maxLogEntries: 1000,
consoleOutput: true,
trackDataFlow: true,
enableFileLogging: config.enabled || false,
maxFileSize: 10 * 1024 * 1024, // 10MB
maxLogFiles: 5,
baseDirectory: config.basePath || '~/.rcc/debug-logs',
pipelineIO: {
enabled: config.enabled || false,
autoRecordPipelineStart: true,
autoRecordPipelineEnd: true,
pipelineSessionFileName: 'pipeline-session.jsonl',
pipelineDirectory: (config.basePath || '~/.rcc/debug-logs') + '/pipeline-logs',
recordAllOperations: true,
includeModuleContext: true,
includeTimestamp: true,
includeDuration: true,
maxPipelineOperationsPerFile: 2000
},
eventBus: {
enabled: true,
maxSubscribers: 100,
eventQueueSize: 10000
}
});
}
this.logInfo('Recording config set', { config }, 'setRecordingConfig');
}
/**
* Log debug message
* 记录调试信息
*/
logDebug(message, data) {
// Use BaseModule's log method instead of DebugCenter's private log method
this.logInfo(message, data, 'logDebug');
}
/**
* Log warning message
* 记录警告信息
*/
logWarn(message, data) {
// Use BaseModule's log method instead of DebugCenter's private log method
this.logInfo(message, data, 'logWarn');
}
/**
* Log error message
* 记录错误信息
*/
logError(message, data) {
// Use BaseModule's log method instead of DebugCenter's private log method
this.logInfo(message, data, 'logError');
}
/**
* Execute operation with full tracing and error handling
* 执行操作并带有完整的跟踪和错误处理
*/
async executeWithTracing(operation, stage, request, options) {
const startTime = Date.now();
const moduleInfo = {
moduleId: this.info.id,
moduleName: this.info.name,
moduleType: this.info.type,
providerName: this.config.providerName,
endpoint: this.config.endpoint
};
// Create execution context
const contextOptions = {
executionId: options?.executionId,
traceId: options?.traceId,
sessionId: options?.sessionId,
metadata: options?.metadata,
inheritFrom: options?.parentContext
};
const context = this.tracker.createContext(moduleInfo, stage, request, contextOptions);
this.logDebug('Starting execution with tracing', {
executionId: context.executionId,
requestId: context.requestId,
traceId: context.traceId,
stage: context.stage
});
try {
// Record request if provided
if (request) {
await this.tracker.recordRequest(context, request, stage);
}
// Record memory and CPU usage if metrics enabled
const metrics = this.config.enablePerformanceMetrics
? await this.collectPerformanceMetrics()
: undefined;
// Execute operation with timeout and retry logic
const result = await this.executeWithTimeoutAndRetry(() => operation(context), options?.timeout || this.config.executionTimeout || 30000, options?.enableRetry, options?.maxRetries, options?.retryDelay);
// Record successful response
await this.tracker.recordResponse(context, result, stage);
// Complete context
this.tracker.completeContext(context, result);
const endTime = Date.now();
const duration = endTime - startTime;
const executionResult = {
executionId: context.executionId,
requestId: context.requestId,
traceId: context.traceId,
status: 'success',
data: result,
timing: {
startTime,
endTime,
duration
},
context,
metrics,
traceSummary: this.generateTraceSummary(context)
};
this.logDebug('Execution completed successfully', {
executionId: context.executionId,
duration,
dataType: typeof result
});
return executionResult;
}
catch (error) {
return await this.handleExecutionError(error, context, startTime, stage, options);
}
}
/**
* Execute with timeout and retry logic
* 执行带超时和重试逻辑
*/
async executeWithTimeoutAndRetry(operation, timeout, enableRetry = false, maxRetries = 3, retryDelay = 1000) {
const executeWithTimeout = async () => {
return new Promise((resolve, reject) => {
const timer = setTimeout(() => {
reject(new Error(`Operation timed out after ${timeout}ms`));
}, timeout);
operation()
.then(resolve)
.catch(reject)
.finally(() => clearTimeout(timer));
});
};
let lastError;
for (let attempt = 0; attempt <= (enableRetry ? maxRetries : 0); attempt++) {
try {
return await executeWithTimeout();
}
catch (error) {
lastError = error;
if (attempt === (enableRetry ? maxRetries : 0)) {
throw lastError;
}
// Exponential backoff
const delay = retryDelay * Math.pow(2, attempt);
this.logWarn(`Operation failed, retrying in ${delay}ms`, {
attempt: attempt + 1,
maxRetries,
error: lastError.message
});
await new Promise(resolve => setTimeout(resolve, delay));
}
}
throw lastError;
}
/**
* Handle execution errors with comprehensive error handling
* 处理执行错误并提供全面的错误处理
*/
async handleExecutionError(error, context, startTime, stage, options) {
const endTime = Date.now();
const duration = endTime - startTime;
// Determine error category
const category = this.categorizeError(error);
const severity = this.determineSeverity(error, options?.maxRetries);
// Record error
const executionError = await this.recordError(error, category, severity, this.isErrorRecoverable(error));
// Handle with error center if enabled
if (this.config.enableEnhancedErrorHandling) {
this.errorHandler.handleError({
error,
source: this.info.id,
severity: severity.toLowerCase(),
timestamp: Date.now(),
context: {
executionId: context.executionId,
requestId: context.requestId,
traceId: context.traceId,
stage: context.stage
}
});
}
// Complete context with error
this.tracker.completeContext(context, undefined, executionError);
const executionResult = {
executionId: context.executionId,
requestId: context.requestId,
traceId: context.traceId,
status: this.getErrorStatus(error),
error: executionError,
timing: {
startTime,
endTime,
duration
},
context,
traceSummary: this.generateTraceSummary(context)
};
this.logError('Execution failed', {
executionId: context.executionId,
error: error.message,
category,
severity,
duration
});
return executionResult;
}
/**
* Collect performance metrics
* 收集性能指标
*/
async collectPerformanceMetrics() {
try {
const memUsage = process.memoryUsage();
const cpuUsage = process.cpuUsage();
return {
memoryUsage: {
start: memUsage.heapUsed,
end: memUsage.heapUsed,
peak: memUsage.heapUsed
},
cpuUsage: {
start: cpuUsage.user,
end: cpuUsage.user,
average: cpuUsage.user
}
};
}
catch (error) {
this.logWarn('Failed to collect performance metrics', { error: error });
return {};
}
}
/**
* Generate trace summary for analysis
* 生成跟踪摘要用于分析
*/
generateTraceSummary(context) {
const stageTransitions = [];
if (context.parent) {
const parentStage = context.parent.stage;
const currentStage = context.stage;
const transitionTime = context.timing.startTime - context.parent.timing.startTime;
stageTransitions.push({
from: parentStage,
to: currentStage,
duration: transitionTime
});
}
return {
totalStages: context.timing.stageTimings.size,
completedStages: Array.from(context.timing.stageTimings.values()).filter(t => t.status === 'completed').length,
failedStages: Array.from(context.timing.stageTimings.values()).filter(t => t.status === 'failed').length,
stageTransitions,
errors: context.error ? [context.error] : []
};
}
categorizeError(error) {
const message = error.message.toLowerCase();
const stack = error.stack?.toLowerCase() || '';
if (message.includes('timeout') || message.includes('timed out')) {
return ErrorCategory.TIMEOUT;
}
if (message.includes('network') || message.includes('connection') || message.includes('econnrefused')) {
return ErrorCategory.NETWORK;
}
if (message.includes('auth') || message.includes('unauthorized') || message.includes('forbidden')) {
return ErrorCategory.AUTHENTICATION;
}
if (message.includes('validation') || message.includes('invalid')) {
return ErrorCategory.VALIDATION;
}
if (message.includes('rate limit') || message.includes('too many requests')) {
return ErrorCategory.RATE_LIMIT;
}
if (message.includes(