|
| 1 | +/** |
| 2 | + * Code Agent |
| 3 | + * |
| 4 | + * Specialized agent for full-stack development: |
| 5 | + * - Web application development |
| 6 | + * - Authentication integration |
| 7 | + * - Database setup and migrations |
| 8 | + * - Payment integration (Stripe) |
| 9 | + * - End-to-end testing |
| 10 | + */ |
| 11 | + |
| 12 | +import { generateText, tool } from "ai"; |
| 13 | +import { z } from "zod"; |
| 14 | +import { registry } from "@/lib/ai/registry"; |
| 15 | +import { agentConfigs } from "./config"; |
| 16 | +import { executePython, isE2BConfigured } from "@/lib/e2b/sandbox"; |
| 17 | +import type { SubTask, AgentContext, AgentResponse, Artifact } from "./types"; |
| 18 | + |
| 19 | +/** |
| 20 | + * Code Agent system prompt |
| 21 | + */ |
| 22 | +const codeAgentPrompt = `You are the Code Agent, a specialized AI for full-stack web development. |
| 23 | +
|
| 24 | +## Expertise: |
| 25 | +- **Frontend**: React, Next.js, Vue, Tailwind CSS, shadcn/ui |
| 26 | +- **Backend**: Node.js, API Routes, tRPC, Express |
| 27 | +- **Database**: Prisma, PostgreSQL, MongoDB, Supabase |
| 28 | +- **Auth**: NextAuth.js, Clerk, Auth0, JWT |
| 29 | +- **Payments**: Stripe Checkout, Subscriptions, Webhooks |
| 30 | +- **Testing**: Playwright, Jest, React Testing Library |
| 31 | +
|
| 32 | +## Code Quality Standards: |
| 33 | +1. Write clean, maintainable TypeScript code |
| 34 | +2. Follow best practices and design patterns |
| 35 | +3. Include proper error handling |
| 36 | +4. Add helpful comments for complex logic |
| 37 | +5. Ensure accessibility (WCAG compliance) |
| 38 | +6. Write tests for critical functionality |
| 39 | +
|
| 40 | +## Output Format: |
| 41 | +For each code artifact, provide: |
| 42 | +1. File path |
| 43 | +2. Complete, working code |
| 44 | +3. Brief explanation of what it does |
| 45 | +4. Any required dependencies |
| 46 | +
|
| 47 | +## Guidelines: |
| 48 | +- Generate complete, runnable code (not snippets) |
| 49 | +- Include all necessary imports |
| 50 | +- Handle edge cases and errors |
| 51 | +- Follow security best practices |
| 52 | +- Use environment variables for secrets |
| 53 | +`; |
| 54 | + |
| 55 | +/** |
| 56 | + * Define Code Agent tools |
| 57 | + */ |
| 58 | +const codeAgentTools = { |
| 59 | + write_file: tool({ |
| 60 | + description: "Write code to a file", |
| 61 | + parameters: z.object({ |
| 62 | + path: z.string().describe("File path relative to project root"), |
| 63 | + content: z.string().describe("Complete file content"), |
| 64 | + language: z.string().describe("Programming language"), |
| 65 | + }), |
| 66 | + execute: async ({ path, content, language }) => { |
| 67 | + // In production, this would write to a filesystem or sandbox |
| 68 | + return { |
| 69 | + success: true, |
| 70 | + path, |
| 71 | + language, |
| 72 | + size: content.length, |
| 73 | + }; |
| 74 | + }, |
| 75 | + }), |
| 76 | + |
| 77 | + execute_code: tool({ |
| 78 | + description: "Execute Python code in a secure sandbox", |
| 79 | + parameters: z.object({ |
| 80 | + code: z.string().describe("Python code to execute"), |
| 81 | + description: z.string().describe("What this code does"), |
| 82 | + }), |
| 83 | + execute: async ({ code, description }) => { |
| 84 | + if (!isE2BConfigured()) { |
| 85 | + return { |
| 86 | + success: false, |
| 87 | + error: "E2B not configured. Set E2B_API_KEY.", |
| 88 | + }; |
| 89 | + } |
| 90 | + const result = await executePython(code); |
| 91 | + return { |
| 92 | + success: result.success, |
| 93 | + output: result.output, |
| 94 | + error: result.error, |
| 95 | + executionTime: `${result.executionTime}ms`, |
| 96 | + }; |
| 97 | + }, |
| 98 | + }), |
| 99 | + |
| 100 | + run_tests: tool({ |
| 101 | + description: "Run tests for the code", |
| 102 | + parameters: z.object({ |
| 103 | + testCommand: z.string().describe("Command to run tests"), |
| 104 | + testFiles: z.array(z.string()).describe("Test files to run"), |
| 105 | + }), |
| 106 | + execute: async ({ testCommand, testFiles }) => { |
| 107 | + // Simulated test execution |
| 108 | + return { |
| 109 | + success: true, |
| 110 | + passed: testFiles.length, |
| 111 | + failed: 0, |
| 112 | + coverage: "85%", |
| 113 | + }; |
| 114 | + }, |
| 115 | + }), |
| 116 | + |
| 117 | + install_package: tool({ |
| 118 | + description: "Install npm packages", |
| 119 | + parameters: z.object({ |
| 120 | + packages: z.array(z.string()).describe("Package names to install"), |
| 121 | + dev: z.boolean().optional().describe("Install as dev dependency"), |
| 122 | + }), |
| 123 | + execute: async ({ packages, dev }) => { |
| 124 | + return { |
| 125 | + success: true, |
| 126 | + installed: packages, |
| 127 | + type: dev ? "devDependencies" : "dependencies", |
| 128 | + }; |
| 129 | + }, |
| 130 | + }), |
| 131 | +}; |
| 132 | + |
| 133 | +/** |
| 134 | + * Execute the Code Agent |
| 135 | + */ |
| 136 | +export async function executeCodeAgent( |
| 137 | + subTask: SubTask, |
| 138 | + context: AgentContext |
| 139 | +): Promise<AgentResponse> { |
| 140 | + const config = agentConfigs.code; |
| 141 | + const model = registry.languageModel(config.model); |
| 142 | + const artifacts: Artifact[] = []; |
| 143 | + |
| 144 | + // Build context from previous results |
| 145 | + const previousContext = Array.from(context.previousResults.entries()) |
| 146 | + .map(([id, result]) => `Previous: ${id}\n${result}`) |
| 147 | + .join("\n\n"); |
| 148 | + |
| 149 | + const result = await generateText({ |
| 150 | + model, |
| 151 | + system: codeAgentPrompt, |
| 152 | + prompt: `Task: ${subTask.title}\n\nDescription: ${subTask.description}\n\n${previousContext ? `Context:\n${previousContext}` : ""}\n\nGenerate the required code with full implementation.`, |
| 153 | + tools: codeAgentTools, |
| 154 | + maxSteps: config.maxSteps, |
| 155 | + temperature: config.temperature, |
| 156 | + }); |
| 157 | + |
| 158 | + // Extract artifacts from tool calls |
| 159 | + for (const step of result.steps || []) { |
| 160 | + for (const toolResult of step.toolResults || []) { |
| 161 | + if (toolResult.toolName === "write_file" && toolResult.result) { |
| 162 | + const fileResult = toolResult.result as { path: string; language: string }; |
| 163 | + artifacts.push({ |
| 164 | + id: `artifact-${Date.now()}`, |
| 165 | + type: "code", |
| 166 | + title: fileResult.path, |
| 167 | + content: "", // Would contain actual content |
| 168 | + language: fileResult.language, |
| 169 | + createdAt: new Date(), |
| 170 | + }); |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + return { |
| 176 | + message: result.text, |
| 177 | + artifacts, |
| 178 | + thinking: result.reasoning, |
| 179 | + nextAction: "complete", |
| 180 | + }; |
| 181 | +} |
0 commit comments