| |
| |
| |
| |
| |
| |
| |
|
|
| const SystemDetector = require('./system-detector'); |
| const ErrorHandler = require('./error-handler'); |
| const ContextManager = require('./context-manager'); |
|
|
| class Executor { |
| constructor() { |
| this.systemDetector = new SystemDetector(); |
| this.errorHandler = new ErrorHandler(); |
| this.contextManager = new ContextManager(); |
| |
| this.executionHistory = []; |
| this.maxHistorySize = 100; |
| |
| console.log(' Executor initialized - ready for strategy execution'); |
| } |
|
|
| |
| |
| |
| async execute(request) { |
| const executionId = this.generateExecutionId(); |
| const startTime = Date.now(); |
| |
| try { |
| console.log(` Starting execution [${executionId}]`); |
| |
| |
| const validationResult = { |
| success: true, |
| sessionId: 'exec_' + Date.now(), |
| userId: request.userId || 'system', |
| permissions: request.permissions || 'admin', |
| level: 'admin' |
| }; |
| |
| |
| const executionContext = this.prepareExecutionContext(request, executionId, validationResult); |
| |
| |
| const detectionResult = await this.systemDetector.detect( |
| executionContext.projectPath, |
| executionContext |
| ); |
| |
| |
| const executionResult = await this.executeWithStrategy( |
| detectionResult.strategy, |
| executionContext |
| ); |
| |
| |
| const finalResult = this.processExecutionResult( |
| executionResult, |
| detectionResult, |
| executionContext, |
| startTime |
| ); |
| |
| this.recordExecution(finalResult); |
| console.log(` Execution completed [${executionId}] in ${finalResult.totalDuration}ms`); |
| |
| return finalResult; |
|
|
| } catch (error) { |
| console.log(` Execution failed [${executionId}]: ${error.message}`); |
| const errorResult = this.handleExecutionError(error, request, executionId, startTime); |
| this.recordExecution(errorResult); |
| return errorResult; |
| } |
| } |
|
|
| |
| |
| |
| prepareExecutionContext(request, executionId, validationResult) { |
| |
| let fullCommand; |
| if (request.args && Array.isArray(request.args) && request.args.length > 0) { |
| |
| fullCommand = `${request.command} ${request.args.join(' ')}`; |
| } else { |
| |
| fullCommand = request.command; |
| } |
|
|
| const context = this.contextManager.createContext({ |
| executionId: executionId, |
| command: fullCommand, |
| originalCommand: request.command, |
| args: request.args || [], |
| projectPath: request.projectPath || process.cwd(), |
| options: request.options || {}, |
| sessionId: validationResult.sessionId, |
| userId: validationResult.userId, |
| permissions: validationResult.permissions, |
| timestamp: Date.now(), |
| platform: process.platform, |
| nodeVersion: process.version, |
| isSecure: validationResult.success, |
| validationLevel: validationResult.level |
| }); |
|
|
| console.log(` Execution context prepared: ${context.executionId}`); |
| return context; |
| } |
|
|
| |
| |
| |
| async executeWithStrategy(strategy, executionContext) { |
| console.log(` Executing with strategy: ${strategy.name}`); |
| |
| const strategyStatus = await strategy.getStatus(); |
| if (strategyStatus.status !== 'READY') { |
| throw new Error(`Strategy not ready: ${strategy.name}`); |
| } |
| |
| const result = await strategy.execute(executionContext); |
| console.log(` Strategy execution completed: ${strategy.name}`); |
| |
| return result; |
| } |
|
|
| |
| |
| |
| processExecutionResult(executionResult, detectionResult, executionContext, startTime) { |
| return { |
| success: executionResult.success, |
| executionId: executionContext.executionId, |
| command: executionContext.command, |
| projectPath: executionContext.projectPath, |
| strategy: detectionResult.type, |
| confidence: detectionResult.confidence, |
| output: executionResult.output, |
| exitCode: executionResult.exitCode, |
| error: executionResult.error, |
| totalDuration: Date.now() - startTime, |
| strategyDuration: executionResult.duration, |
| sessionId: executionContext.sessionId, |
| userId: executionContext.userId, |
| timestamp: startTime, |
| platform: executionContext.platform |
| }; |
| } |
|
|
| |
| |
| |
| handleExecutionError(error, request, executionId, startTime) { |
| this.errorHandler.logError('EXECUTION_FAILED', error, { |
| executionId: executionId, |
| request: request |
| }); |
|
|
| return { |
| success: false, |
| executionId: executionId, |
| error: error.message, |
| command: request.command, |
| projectPath: request.projectPath, |
| totalDuration: Date.now() - startTime, |
| timestamp: startTime, |
| errorType: error.constructor.name |
| }; |
| } |
|
|
| |
| |
| |
| recordExecution(result) { |
| this.executionHistory.unshift(result); |
| if (this.executionHistory.length > this.maxHistorySize) { |
| this.executionHistory = this.executionHistory.slice(0, this.maxHistorySize); |
| } |
| console.log(` Execution recorded: ${result.executionId}`); |
| } |
|
|
| |
| |
| |
| async run(command, projectPath = process.cwd(), options = {}) { |
| return this.execute({ |
| command: command, |
| projectPath: projectPath, |
| options: options |
| }); |
| } |
|
|
| |
| |
| |
| async getStatus() { |
| const systemStatus = await this.systemDetector.getStatus(); |
| |
| return { |
| executor: 'ExecutionManager', |
| status: 'READY', |
| components: { |
| systemDetector: systemStatus |
| }, |
| executionHistory: { |
| total: this.executionHistory.length, |
| successful: this.executionHistory.filter(r => r.success).length, |
| failed: this.executionHistory.filter(r => !r.success).length |
| }, |
| timestamp: Date.now() |
| }; |
| } |
|
|
| |
| |
| |
| generateExecutionId() { |
| return `exec_${Date.now()}_${Math.random().toString(36).substring(2, 8)}`; |
| } |
|
|
| |
| |
| |
| getHistory(limit = 10) { |
| return this.executionHistory.slice(0, limit); |
| } |
|
|
| |
| |
| |
| clearHistory() { |
| this.executionHistory = []; |
| console.log(' Execution history cleared'); |
| } |
| } |
|
|
| module.exports = Executor; |