File size: 1,292 Bytes
34367da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Agent-driven code generation service
import { GoogleGenerativeAI } from '@google/generative-ai';

const genAI = new GoogleGenerativeAI(process.env.GOOGLE_API_KEY || '');
const model = genAI.getGenerativeModel({ model: 'gemini-pro' });

export async function generateCode(spec: string, testPlan: string, kpiThreshold = 80): Promise<string> {
  const prompt = `You are Evolution Agent. Generate TypeScript code for: ${spec}.

  Follow testplan: ${testPlan}.

  Ensure: TS types, integration with MCP/personal_entities/4-layer.

  Output ONLY code - no explanations.

  KPI: Coverage >${kpiThreshold}%, no errors.`;

  const result = await model.generateContent(prompt);
  const code = await result.response.text();

  // Post-process: Validate syntax (simple check)
  if (!code.includes('export') || code.includes('error')) {
    throw new Error('Generated code invalid - retrying...');
  }

  // Log run for Evolution
  // await reportRun('code-gen', { kpi: coverage });  // Placeholder

  return code;
}

export async function refineCode(code: string, errorLog: string): Promise<string> {
  const refinePrompt = `Refine this code: ${code}. Fix errors: ${errorLog}. Output ONLY fixed code.`;
  // Similar genAI call...
  return 'refined code';  // Placeholder
}