File size: 2,520 Bytes
4c41b3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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 };
}