Spaces:
Running
Running
| const AGENTS = [ | |
| { id: 'pm', name: 'Product Manager', icon: 'π', color: 'blue' }, | |
| { id: 'architect', name: 'Architect', icon: 'ποΈ', color: 'purple' }, | |
| { id: 'planner', name: 'Task Planner', icon: 'π', color: 'indigo' }, | |
| { id: 'backend', name: 'Backend Dev', icon: 'βοΈ', color: 'green' }, | |
| { id: 'frontend', name: 'Frontend Dev', icon: 'π¨', color: 'pink' }, | |
| { id: 'devops', name: 'DevOps', icon: 'π', color: 'orange' }, | |
| { id: 'qa', name: 'QA Agent', icon: 'π§ͺ', color: 'yellow' }, | |
| { id: 'reviewer', name: 'Code Review', icon: 'π', color: 'cyan' }, | |
| { id: 'security', name: 'Security', icon: 'π', color: 'red' }, | |
| { id: 'docs', name: 'Documentation', icon: 'π', color: 'teal' }, | |
| { id: 'knowledge', name: 'Knowledge Mgr', icon: 'π§ ', color: 'violet' }, | |
| ]; | |
| const WORKFLOW_STEPS = [ | |
| { phase: 'Planning', agents: ['pm', 'architect', 'planner'], description: 'Define requirements and architecture' }, | |
| { phase: 'Execution', agents: ['backend', 'frontend', 'devops'], description: 'Implement and deploy' }, | |
| { phase: 'Validation', agents: ['qa', 'security', 'reviewer'], description: 'Test, secure, and review' }, | |
| { phase: 'Documentation', agents: ['docs'], description: 'Update all documentation' }, | |
| { phase: 'Learning', agents: ['knowledge'], description: 'Extract and store learnings' }, | |
| ]; | |
| const SAMPLE_TASKS = [ | |
| 'Build a REST API for user management', | |
| 'Create a real-time chat feature', | |
| 'Implement authentication with OAuth2', | |
| 'Build a dashboard analytics module', | |
| 'Create a file upload service', | |
| 'Implement search functionality with Elasticsearch', | |
| ]; | |
| const KNOWLEDGE_ENTRIES = [ | |
| { | |
| task: 'JWT Authentication Implementation', | |
| error: 'Token expiration not validated on server side', | |
| root_cause: 'Missing expiration check in middleware', | |
| fix: 'Added exp claim validation in auth middleware', | |
| prevention_rule: 'IF using JWT THEN validate expiration config BECAUSE misconfiguration causes auth issues', | |
| success_score: 0.85, | |
| tags: ['auth', 'jwt', 'security'], | |
| }, | |
| { | |
| task: 'Database Migration', | |
| error: 'Data loss during column rename', | |
| root_cause: 'Direct column rename without backup', | |
| fix: 'Added backup step and incremental migration', | |
| prevention_rule: 'IF modifying schema THEN create backup first BECAUSE direct changes can cause data loss', | |
| success_score: 0.72, | |
| tags: ['database', 'migration', 'data'], | |
| }, | |
| { | |
| task: 'API Rate Limiting', | |
| error: 'Legitimate users blocked', | |
| root_cause: 'Rate limit too aggressive for normal usage', | |
| fix: 'Adjusted limits based on traffic analysis', | |
| prevention_rule: 'IF implementing rate limits THEN analyze traffic patterns first BECAUSE aggressive limits block valid users', | |
| success_score: 0.91, | |
| tags: ['api', 'rate-limiting', 'performance'], | |
| }, | |
| ]; | |
| function generateCommunication(agentId, step, task) { | |
| const agent = AGENTS.find(a => a.id === agentId); | |
| const statuses = ['pending', 'in_progress', 'done']; | |
| const status = statuses[Math.floor(Math.random() * 2) + 1]; | |
| const outputs = { | |
| pm: `Requirements defined for: ${task}`, | |
| architect: `System design completed for: ${task}`, | |
| planner: `Task decomposed into 5 subtasks`, | |
| backend: `API endpoints implemented`, | |
| frontend: `UI components built and integrated`, | |
| devops: `CI/CD pipeline configured`, | |
| qa: `12 tests written, all passing`, | |
| reviewer: `Code reviewed, 3 improvements suggested`, | |
| security: `No critical vulnerabilities found`, | |
| docs: `Documentation updated`, | |
| knowledge: `Experience logged to knowledge base`, | |
| }; | |
| return { | |
| agent: agent.name, | |
| task: step.description, | |
| input: task, | |
| output: outputs[agentId] || 'Task completed', | |
| issues: status === 'in_progress' ? ['Awaiting dependency'] : [], | |
| status, | |
| next_step: status === 'done' ? 'Proceed to next phase' : 'Continue current work', | |
| timestamp: new Date().toISOString(), | |
| }; | |
| } | |
| export default function handler(req, res) { | |
| if (req.method !== 'POST') { | |
| return res.status(405).json({ error: 'Method not allowed' }); | |
| } | |
| const { task, mode = 'safe' } = req.body; | |
| const selectedTask = task || SAMPLE_TASKS[Math.floor(Math.random() * SAMPLE_TASKS.length)]; | |
| const simulation = { | |
| task: selectedTask, | |
| mode, | |
| startedAt: new Date().toISOString(), | |
| phases: WORKFLOW_STEPS.map((step, phaseIndex) => { | |
| const communications = step.agents.map(agentId => | |
| generateCommunication(agentId, step, selectedTask) | |
| ); | |
| return { | |
| phase: step.phase, | |
| description: step.description, | |
| order: phaseIndex + 1, | |
| communications, | |
| status: 'done', | |
| }; | |
| }), | |
| knowledge: KNOWLEDGE_ENTRIES, | |
| definitionOfDone: { | |
| code_compiles: true, | |
| tests_pass: true, | |
| security_checks: mode !== 'fast', | |
| code_reviewed: mode !== 'fast', | |
| docs_updated: mode === 'safe', | |
| }, | |
| completedAt: new Date().toISOString(), | |
| }; | |
| res.status(200).json(simulation); | |
| } |