|
| 1 | +import { BaseAgent } from './BaseAgent' |
| 2 | +import { ProjectWorkspace } from '@/services/workspace/ProjectWorkspace' |
| 3 | +import { AgentOutput, ProjectContext } from '@/types/orchestrator' |
| 4 | + |
| 5 | +export interface BackendDeveloperContext { |
| 6 | + projectId: string |
| 7 | + userRequest: string |
| 8 | + workspace: ProjectWorkspace |
| 9 | + architecture?: any |
| 10 | +} |
| 11 | + |
| 12 | +export interface BackendDeveloperOutput { |
| 13 | + filesCreated: string[] |
| 14 | + filesModified: string[] |
| 15 | + cost: number |
| 16 | + duration: number |
| 17 | +} |
| 18 | + |
| 19 | +export class BackendDeveloper extends BaseAgent { |
| 20 | + agentType = 'BackendDeveloper' as const |
| 21 | + |
| 22 | + private workspace: ProjectWorkspace |
| 23 | + private userRequest: string |
| 24 | + private architecture?: any |
| 25 | + |
| 26 | + constructor(context: BackendDeveloperContext) { |
| 27 | + // Create a minimal ProjectContext for BaseAgent |
| 28 | + const projectContext: ProjectContext = { |
| 29 | + state: { |
| 30 | + userRequest: context.userRequest, |
| 31 | + userId: 'test-user', |
| 32 | + organizationId: 'test-org', |
| 33 | + projectId: context.projectId, |
| 34 | + projectName: context.projectId, |
| 35 | + createdAt: new Date().toISOString(), |
| 36 | + agentsSpawned: [], |
| 37 | + agentOutputs: {}, |
| 38 | + errors: [], |
| 39 | + retryCount: 0, |
| 40 | + }, |
| 41 | + organizationId: 'test-org', |
| 42 | + userId: 'test-user', |
| 43 | + costOptimizerUrl: process.env.COST_OPTIMIZER_URL || 'http://localhost:3001', |
| 44 | + costOptimizerApiKey: process.env.COST_OPTIMIZER_API_KEY || 'test-key', |
| 45 | + } |
| 46 | + |
| 47 | + super('BackendDeveloper', projectContext) |
| 48 | + this.workspace = context.workspace |
| 49 | + this.userRequest = context.userRequest |
| 50 | + this.architecture = context.architecture |
| 51 | + } |
| 52 | + |
| 53 | + async execute(): Promise<AgentOutput> { |
| 54 | + const startTime = Date.now() |
| 55 | + |
| 56 | + try { |
| 57 | + // TODO: Load skills (test-driven-development, api-design-patterns, security-best-practices) |
| 58 | + // TODO: Use MCP tools (context7, sequential-thinking, supabase) |
| 59 | + |
| 60 | + // For MVP: Generate simple API file structure |
| 61 | + const prompt = this.buildPrompt() |
| 62 | + const response = await this.think({ prompt, complexity: 'simple' }) |
| 63 | + |
| 64 | + // Parse response and generate files |
| 65 | + const files = await this.generateFiles(response) |
| 66 | + |
| 67 | + this.output.duration = Date.now() - startTime |
| 68 | + |
| 69 | + return this.getOutput() |
| 70 | + } catch (error) { |
| 71 | + console.error('[BackendDeveloper] Error:', error) |
| 72 | + this.addError(`BackendDeveloper execution failed: ${error}`) |
| 73 | + this.output.duration = Date.now() - startTime |
| 74 | + return this.getOutput() |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private buildPrompt(): string { |
| 79 | + return `You are a backend developer agent. Generate TypeScript files for the following request: |
| 80 | +
|
| 81 | +User Request: ${this.userRequest} |
| 82 | +
|
| 83 | +${this.architecture ? `Architecture Context:\n${JSON.stringify(this.architecture, null, 2)}\n` : ''} |
| 84 | +
|
| 85 | +Generate a simple API structure with: |
| 86 | +1. API route file (app/api/[resource]/route.ts) |
| 87 | +2. Service layer file (src/services/[resource]Service.ts) |
| 88 | +3. Type definitions (src/types/[resource].ts) |
| 89 | +
|
| 90 | +Return a JSON array of files: |
| 91 | +[ |
| 92 | + { |
| 93 | + "path": "app/api/todos/route.ts", |
| 94 | + "content": "import { NextRequest, NextResponse } from 'next/server'..." |
| 95 | + }, |
| 96 | + { |
| 97 | + "path": "src/services/todoService.ts", |
| 98 | + "content": "export class TodoService { ... }" |
| 99 | + } |
| 100 | +] |
| 101 | +
|
| 102 | +Keep it simple and focused. Use Next.js 15 patterns.` |
| 103 | + } |
| 104 | + |
| 105 | + private async generateFiles(response: string): Promise<string[]> { |
| 106 | + try { |
| 107 | + // Try to parse JSON response |
| 108 | + const files = JSON.parse(response) |
| 109 | + const createdFiles: string[] = [] |
| 110 | + |
| 111 | + for (const file of files) { |
| 112 | + await this.workspace.writeFile(file.path, file.content) |
| 113 | + createdFiles.push(file.path) |
| 114 | + |
| 115 | + // Track file creation using BaseAgent method |
| 116 | + this.addFileCreated(file.path) |
| 117 | + } |
| 118 | + |
| 119 | + return createdFiles |
| 120 | + } catch (error) { |
| 121 | + // If JSON parsing fails, create a single example file |
| 122 | + console.warn('[BackendDeveloper] Failed to parse response, creating example file') |
| 123 | + |
| 124 | + const exampleFile = 'src/example-generated.ts' |
| 125 | + await this.workspace.writeFile(exampleFile, `// Generated by BackendDeveloper\n// Request: ${this.userRequest}\n\nexport const placeholder = true;`) |
| 126 | + |
| 127 | + this.addFileCreated(exampleFile) |
| 128 | + return [exampleFile] |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments