/** * Workflow Engine * * Orchestrates multi-step agent pipelines in the Synesthesia Workbench. */ import { AgentRegistry } from './agent_registry'; import { TaskRouter } from './task_router'; import { MemoryBridge } from './memory_bridge'; export class WorkflowEngine { private taskRouter: TaskRouter; private memoryBridge: MemoryBridge; constructor(agentRegistry: AgentRegistry, taskRouter: TaskRouter, memoryBridge: MemoryBridge) { this.taskRouter = taskRouter; this.memoryBridge = memoryBridge; } async executePipeline( pipelineName: string, initialTaskDescription: string, initialArgs: any = {} ): Promise { console.log(`--- Executing Pipeline: ${pipelineName} ---`); const pipelines: Record string); kwargs: Record }>; successMessage: string }> = { 'Coding Pipeline': { steps: [ { agentType: 'ARCHITECT', taskDesc: initialTaskDescription, kwargs: { taskInputArgs: { taskDescription: initialTaskDescription, codebasePath: '/app/synesthesia' } }, }, { agentType: 'RESONANCE', taskDesc: (ctx: any) => ctx?.subTasks?.[0] || 'Implement coding task', kwargs: { taskInputArgs: { taskDescription: 'Implement coding task', trainingScript: 'ML_Pipeline/train.py' } }, }, { agentType: 'SENTINEL', taskDesc: 'Perform code review', kwargs: { taskInputArgs: { codebasePath: '/app/synesthesia', taskDescription: 'Code review for new feature' } }, }, ], successMessage: 'Coding pipeline completed successfully.', }, 'Debug Pipeline': { steps: [ { agentType: 'PULSE-DEBUG', taskDesc: initialTaskDescription, kwargs: { taskInputArgs: { runtimeId: 'runtime-xyz', taskDescription: initialTaskDescription } }, }, { agentType: 'SENTINEL', taskDesc: 'Validate debug fix', kwargs: { taskInputArgs: { codebasePath: '/app/synesthesia', taskDescription: 'Review debug fix' } }, }, ], successMessage: 'Debug pipeline completed successfully.', }, }; const pipeline = pipelines[pipelineName]; if (!pipeline) { console.error(`WorkflowEngine Error: Unknown pipeline: ${pipelineName}`); return { status: 'failed', message: `Unknown pipeline: ${pipelineName}` }; } const stepResultsContext: Record = {}; let currentPipelineResult: any = null; for (let i = 0; i < pipeline.steps.length; i++) { const step = pipeline.steps[i]; const agentType = step.agentType; const currentTaskDesc = typeof step.taskDesc === 'function' ? step.taskDesc(stepResultsContext) : step.taskDesc; const routingResult = await this.taskRouter.routeTask(agentType, currentTaskDesc, step.kwargs); currentPipelineResult = routingResult; stepResultsContext[agentType] = routingResult; if (routingResult?.status === 'failed') { const errorMessage = `Pipeline '${pipelineName}' failed at step ${i + 1} (${agentType}): ${routingResult?.message || 'Unknown error'}`; console.error(errorMessage); await this.memoryBridge.recordDebugConclusion(agentType, errorMessage); return { status: 'failed', message: errorMessage, failedStep: i + 1, agent: agentType }; } console.log(`Step ${i + 1} (${agentType}) completed with status: ${routingResult?.status || 'unknown'}`); } console.log(pipeline.successMessage); await this.memoryBridge.recordObservation(pipelineName, pipeline.successMessage); return { status: 'pipeline_completed', finalResult: currentPipelineResult, pipeline: pipelineName }; } async runSingleAgentTask(agentType: string, taskDescription: string, taskInputArgs: any): Promise { console.log(`--- Executing Single Agent Task: ${agentType} ---`); const result = await this.taskRouter.routeTask(agentType, taskDescription, taskInputArgs); if (result?.status === 'failed') { const errorMessage = `Single agent task failed (${agentType}): ${result?.message}`; console.error(errorMessage); await this.memoryBridge.recordDebugConclusion(agentType, errorMessage); return { status: 'failed', message: errorMessage, agent: agentType }; } console.log(`Single agent task (${agentType}) completed.`); await this.memoryBridge.recordObservation(agentType, `Single agent task completed: ${result?.message || 'Success'}`); return result; } }