import { invokeLLM } from "./_core/llm"; import { runCodeInSandbox } from "./sandbox"; import { addIteration, updateProjectStatus } from "./db"; import { ProjectMemory } from "./memory"; export async function runSelfRefiningLoop(projectId: number, prompt: string, targetScore: number = 90, maxIterations: number = 5) { const memory = ProjectMemory.getInstance(); // 0. Search memory for similar past solutions const similarSolutions = await memory.findSimilarSolutions(prompt); const contextFromMemory = similarSolutions?.documents?.[0]?.join("\n") || ""; let currentScore = 0; let currentCode = ""; let version = 1; await updateProjectStatus(projectId, "in_progress"); while (currentScore < targetScore && version <= maxIterations) { // 1. Generation (Qwen) const genResponse = await invokeLLM({ messages: [ { role: "system", content: `You are an elite developer. Generate code based on the prompt. If feedback is provided, improve the previous code. Previous Code: ${currentCode} Feedback: ${version > 1 ? "Improve stealth and stability." : "Initial version."}` }, { role: "user", content: prompt } ] }); currentCode = genResponse.choices[0].message.content; // 2. Dynamic Analysis const sandboxResult = await runCodeInSandbox(currentCode); // 3. Evaluation (DeepSeek) const evalResponse = await invokeLLM({ messages: [ { role: "system", content: `Evaluate this code. Score it out of 100. Dynamic Result: ${JSON.stringify(sandboxResult)}` }, { role: "user", content: currentCode } ] }); const analysis = evalResponse.choices[0].message.content; const scoreMatch = analysis.match(/Total Score:\s*(\d+)/i) || analysis.match(/(\d+)\s*\/\s*100/); currentScore = scoreMatch ? parseInt(scoreMatch[1]) : 70; // 4. Save Iteration await addIteration(projectId, version, { qwenOutput: currentCode, deepseekAnalysis: analysis, score: currentScore, passed: currentScore >= targetScore, scorecard: { dynamic: sandboxResult } }); version++; } await updateProjectStatus(projectId, "completed", currentCode, currentScore); // Save to memory if successful if (currentScore >= targetScore) { await memory.addSuccessfulCode(projectId, currentCode, prompt, currentScore); } return { finalCode: currentCode, finalScore: currentScore }; }