| |
| |
| |
| |
| |
| |
| |
| |
|
|
| const crypto = require('crypto'); |
|
|
| class ContextManager { |
| constructor() { |
| this.contexts = new Map(); |
| console.log(' Context Manager initialized'); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| create(pipelineType = 'execution', initialData = {}) { |
| const pipelineId = this.generatePipelineId(); |
| const timestamp = new Date().toISOString(); |
|
|
| const context = { |
| |
| pipelineId: pipelineId, |
| pipelineType: pipelineType, |
| timestamp: timestamp, |
| |
| |
| request: { |
| command: initialData.command || '', |
| originalData: initialData || {}, |
| parameters: this.parseParameters(initialData), |
| source: initialData.source || 'unknown' |
| }, |
|
|
| |
| systemInfo: { |
| detectedType: null, |
| projectPath: null, |
| strategy: null, |
| dependencies: [], |
| environment: process.env.NODE_ENV || 'development' |
| }, |
|
|
| |
| security: { |
| keyHash: null, |
| permissionLevel: 'basic', |
| userSession: null, |
| securityChecks: [], |
| validatedAt: null |
| }, |
|
|
| |
| execution: { |
| strategy: null, |
| startTime: null, |
| endTime: null, |
| status: 'pending', |
| exitCode: null, |
| output: null |
| }, |
|
|
| |
| steps: [], |
|
|
| |
| performance: { |
| totalTime: null, |
| memoryUsage: process.memoryUsage(), |
| cacheHit: false, |
| stepTimings: {} |
| }, |
|
|
| |
| error: null, |
|
|
| |
| metadata: { |
| createdAt: timestamp, |
| updatedAt: timestamp, |
| version: '1.0.0', |
| framework: 'chahuadev' |
| } |
| }; |
|
|
| |
| this.contexts.set(pipelineId, context); |
| console.log(` Context created: ${pipelineId} (${pipelineType})`); |
| |
| return context; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| addStep(context, stepName, status, result = null) { |
| if (!context || !context.pipelineId) { |
| throw new Error('Invalid context provided'); |
| } |
|
|
| const step = { |
| stepName: stepName, |
| timestamp: new Date().toISOString(), |
| status: status, |
| result: result, |
| duration: null |
| }; |
|
|
| |
| if (status === 'completed' || status === 'failed') { |
| const startStep = context.steps.find(s => |
| s.stepName === stepName && s.status === 'started' |
| ); |
| if (startStep) { |
| step.duration = Date.now() - new Date(startStep.timestamp).getTime(); |
| context.performance.stepTimings[stepName] = step.duration; |
| } |
| } |
|
|
| context.steps.push(step); |
| context.metadata.updatedAt = new Date().toISOString(); |
|
|
| console.log(` Step added: ${stepName} (${status}) to ${context.pipelineId}`); |
| return context; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| updateSecurity(context, securityData) { |
| if (!context || !context.pipelineId) { |
| throw new Error('Invalid context provided'); |
| } |
|
|
| context.security = { |
| ...context.security, |
| ...securityData, |
| validatedAt: new Date().toISOString() |
| }; |
|
|
| context.metadata.updatedAt = new Date().toISOString(); |
| console.log(` Security updated for: ${context.pipelineId}`); |
| |
| return context; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| updateSystemInfo(context, systemData) { |
| if (!context || !context.pipelineId) { |
| throw new Error('Invalid context provided'); |
| } |
|
|
| context.systemInfo = { |
| ...context.systemInfo, |
| ...systemData |
| }; |
|
|
| context.metadata.updatedAt = new Date().toISOString(); |
| console.log(` System info updated for: ${context.pipelineId}`); |
| |
| return context; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| updateExecution(context, status, output = null, exitCode = null) { |
| if (!context || !context.pipelineId) { |
| throw new Error('Invalid context provided'); |
| } |
|
|
| |
| if (status === 'running' && !context.execution.startTime) { |
| context.execution.startTime = new Date().toISOString(); |
| } |
|
|
| |
| if ((status === 'completed' || status === 'failed') && !context.execution.endTime) { |
| context.execution.endTime = new Date().toISOString(); |
| |
| |
| if (context.execution.startTime) { |
| const startTime = new Date(context.execution.startTime).getTime(); |
| const endTime = new Date(context.execution.endTime).getTime(); |
| context.performance.totalTime = endTime - startTime; |
| } |
| } |
|
|
| context.execution.status = status; |
| if (output !== null) context.execution.output = output; |
| if (exitCode !== null) context.execution.exitCode = exitCode; |
| |
| context.metadata.updatedAt = new Date().toISOString(); |
| console.log(` Execution updated: ${status} for ${context.pipelineId}`); |
| |
| return context; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| setError(context, error) { |
| if (!context || !context.pipelineId) { |
| throw new Error('Invalid context provided'); |
| } |
|
|
| const errorInfo = { |
| message: error.message || error, |
| stack: error.stack || null, |
| timestamp: new Date().toISOString(), |
| step: context.steps.length > 0 ? context.steps[context.steps.length - 1].stepName : 'unknown' |
| }; |
|
|
| context.error = errorInfo; |
| context.execution.status = 'failed'; |
| context.metadata.updatedAt = new Date().toISOString(); |
| |
| console.error(` Error set for ${context.pipelineId}:`, errorInfo.message); |
| return context; |
| } |
|
|
| |
| |
| |
| |
| |
| get(pipelineId) { |
| return this.contexts.get(pipelineId) || null; |
| } |
|
|
| |
| |
| |
| |
| |
| remove(pipelineId) { |
| const removed = this.contexts.delete(pipelineId); |
| if (removed) { |
| console.log(` Context removed: ${pipelineId}`); |
| } |
| return removed; |
| } |
|
|
| |
| |
| |
| |
| |
| clearContext(executionId) { |
| return this.remove(executionId); |
| } |
|
|
| |
| |
| |
| |
| |
| createContext(data) { |
| return data; |
| } |
|
|
| |
| |
| |
| |
| |
| generatePipelineId() { |
| const timestamp = Date.now(); |
| const random = crypto.randomBytes(4).toString('hex'); |
| return `pipe_${timestamp}_${random}`; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| parseParameters(data) { |
| if (!data || typeof data !== 'object') { |
| return {}; |
| } |
|
|
| |
| return { |
| timeout: data.timeout || 30000, |
| retry: data.retry || false, |
| maxRetries: data.maxRetries || 3, |
| silent: data.silent || false, |
| workingDir: data.workingDir || process.cwd() |
| }; |
| } |
|
|
| |
| |
| |
| |
| getAll() { |
| return Array.from(this.contexts.values()); |
| } |
|
|
| |
| |
| |
| |
| |
| cleanup(maxAgeMinutes = 60) { |
| const cutoffTime = Date.now() - (maxAgeMinutes * 60 * 1000); |
| let cleaned = 0; |
|
|
| for (const [pipelineId, context] of this.contexts.entries()) { |
| const contextTime = new Date(context.metadata.createdAt).getTime(); |
| if (contextTime < cutoffTime) { |
| this.contexts.delete(pipelineId); |
| cleaned++; |
| } |
| } |
|
|
| if (cleaned > 0) { |
| console.log(` Cleaned up ${cleaned} old contexts`); |
| } |
|
|
| return cleaned; |
| } |
|
|
| |
| |
| |
| |
| getStatus() { |
| return { |
| activeContexts: this.contexts.size, |
| memoryUsage: process.memoryUsage(), |
| ready: true |
| }; |
| } |
| } |
|
|
| |
| module.exports = ContextManager; |