Spaces:
Sleeping
Sleeping
Update app.js
Browse files
app.js
CHANGED
|
@@ -94,41 +94,76 @@ async function startBackgroundInit(projectId, description) {
|
|
| 94 |
// =======================================================
|
| 95 |
// 2. FEEDBACK
|
| 96 |
// =======================================================
|
|
|
|
| 97 |
app.post('/project/feedback', async (req, res) => {
|
| 98 |
-
|
|
|
|
| 99 |
|
| 100 |
const project = await StateManager.getProject(projectId);
|
| 101 |
-
if (!project) return res.status(404).json({ error: "Project not found.
|
| 102 |
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
-
//
|
| 108 |
-
const
|
| 109 |
|
| 110 |
-
|
| 111 |
-
if (
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
project.workerHistory.push({ role: 'user', parts: [{ text: fullInput }] });
|
| 118 |
-
project.workerHistory.push({ role: 'model', parts: [{ text:
|
| 119 |
-
await StateManager.
|
| 120 |
-
|
| 121 |
-
}
|
| 122 |
|
| 123 |
-
|
| 124 |
-
project.workerHistory.push({ role: 'user', parts: [{ text: fullInput }] });
|
| 125 |
-
project.workerHistory.push({ role: 'model', parts: [{ text: response }] });
|
| 126 |
-
await StateManager.updateProject(projectId, { workerHistory: project.workerHistory });
|
| 127 |
-
await StateManager.queueCommand(projectId, response);
|
| 128 |
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
| 130 |
});
|
| 131 |
-
|
|
|
|
| 132 |
// =======================================================
|
| 133 |
// 3. INTERNAL / PING
|
| 134 |
// =======================================================
|
|
|
|
| 94 |
// =======================================================
|
| 95 |
// 2. FEEDBACK
|
| 96 |
// =======================================================
|
| 97 |
+
|
| 98 |
app.post('/project/feedback', async (req, res) => {
|
| 99 |
+
// Added 'taskComplete' to the destructured body
|
| 100 |
+
const { projectId, prompt, hierarchyContext, scriptContext, logContext, taskComplete } = req.body;
|
| 101 |
|
| 102 |
const project = await StateManager.getProject(projectId);
|
| 103 |
+
if (!project) return res.status(404).json({ error: "Project not found." });
|
| 104 |
|
| 105 |
+
// 1. CHECK FOR LOOP STOPPER
|
| 106 |
+
if (taskComplete) {
|
| 107 |
+
console.log(`[${projectId}] ✅ TASK COMPLETE. Stopping Feedback Loop.`);
|
| 108 |
+
|
| 109 |
+
// We log it to history but DO NOT call the Worker AI
|
| 110 |
+
project.workerHistory.push({ role: 'user', parts: [{ text: "USER: Task Execution Confirmed. Logs: " + (logContext?.logs || "OK") }] });
|
| 111 |
+
await StateManager.updateProject(projectId, { workerHistory: project.workerHistory });
|
| 112 |
+
|
| 113 |
+
return res.json({ success: true, message: "Loop Stopped. Waiting for new instructions." });
|
| 114 |
+
}
|
| 115 |
|
| 116 |
+
// 2. ERROR DETECTION (Safety Net)
|
| 117 |
+
let isFailure = false;
|
| 118 |
+
if (logContext && logContext.logs) {
|
| 119 |
+
const errorKeywords = ["Infinite yield", "Stack Begin", "Error:", "Exception", "failed"];
|
| 120 |
+
if (errorKeywords.some(keyword => logContext.logs.includes(keyword))) {
|
| 121 |
+
isFailure = true;
|
| 122 |
+
console.log(`[${projectId}] ⚠️ Detected Error in Logs.`);
|
| 123 |
+
}
|
| 124 |
+
}
|
| 125 |
|
| 126 |
+
// 3. BUILD AI PROMPT
|
| 127 |
+
const fullInput = `USER: ${prompt || "Automatic Feedback"}` + formatContext({ hierarchyContext, scriptContext, logContext });
|
| 128 |
|
| 129 |
+
let finalPrompt = fullInput;
|
| 130 |
+
if (isFailure && (!prompt || prompt.length < 5)) {
|
| 131 |
+
finalPrompt += "\n[SYSTEM ALERT]: The logs indicate a Runtime Error or Infinite Yield. Fix the code immediately. Do not use WaitForChild.";
|
| 132 |
+
}
|
| 133 |
+
|
| 134 |
+
console.log(`[${projectId}] Processing Feedback...`);
|
| 135 |
+
|
| 136 |
+
// 4. CALL AI (Continue Loop)
|
| 137 |
+
try {
|
| 138 |
+
const response = await AIEngine.callWorker(project.workerHistory, finalPrompt);
|
| 139 |
|
| 140 |
+
// Check Escalation
|
| 141 |
+
if (response.includes("STATUS: ESCALATE_TO_PM") || (project.failureCount > 2)) {
|
| 142 |
+
console.log(`[${projectId}] Escalating to PM...`);
|
| 143 |
+
const guidance = await AIEngine.callPM(project.pmHistory, `Worker Stuck. Input: ${fullInput}`);
|
| 144 |
+
const fixed = await AIEngine.callWorker(project.workerHistory, `PM GUIDANCE: ${guidance}`);
|
| 145 |
+
|
| 146 |
+
await StateManager.updateProject(projectId, { failureCount: 0 });
|
| 147 |
+
project.workerHistory.push({ role: 'user', parts: [{ text: fullInput }] });
|
| 148 |
+
project.workerHistory.push({ role: 'model', parts: [{ text: fixed }] });
|
| 149 |
+
await StateManager.queueCommand(projectId, fixed);
|
| 150 |
+
return res.json({ success: true, message: "Escalated & Fixed." });
|
| 151 |
+
}
|
| 152 |
+
|
| 153 |
project.workerHistory.push({ role: 'user', parts: [{ text: fullInput }] });
|
| 154 |
+
project.workerHistory.push({ role: 'model', parts: [{ text: response }] });
|
| 155 |
+
await StateManager.updateProject(projectId, { workerHistory: project.workerHistory });
|
| 156 |
+
await StateManager.queueCommand(projectId, response);
|
|
|
|
| 157 |
|
| 158 |
+
res.json({ success: true, message: "Input Processed." });
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
+
} catch (err) {
|
| 161 |
+
console.error("AI Error:", err);
|
| 162 |
+
res.status(500).json({ error: "AI Failed" });
|
| 163 |
+
}
|
| 164 |
});
|
| 165 |
+
;
|
| 166 |
+
|
| 167 |
// =======================================================
|
| 168 |
// 3. INTERNAL / PING
|
| 169 |
// =======================================================
|