Spaces:
Running
Running
Update server.js
Browse files
server.js
CHANGED
|
@@ -79,45 +79,85 @@ app.post('/new/project', validateRequest, async (req, res) => {
|
|
| 79 |
* 2. FEEDBACK (The Main Loop Endpoint)
|
| 80 |
* Accepts text + optional image. Hits Worker. Checks for escalation.
|
| 81 |
*/
|
|
|
|
| 82 |
app.post('/project/feedback', async (req, res) => {
|
| 83 |
-
const { projectId, prompt, hierarchyContext, scriptContext, logContext } = req.body;
|
| 84 |
|
| 85 |
const project = await StateManager.getProject(projectId);
|
| 86 |
if (!project) return res.status(404).json({ error: "Project not found." });
|
| 87 |
|
| 88 |
-
//
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
}
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
}
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
}
|
| 104 |
|
| 105 |
-
console.log(`[${projectId}]
|
| 106 |
|
| 107 |
-
//
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
});
|
| 120 |
|
|
|
|
| 121 |
/**
|
| 122 |
* 3. INTERNAL PING WORKER
|
| 123 |
* PM sends guidance to Worker.
|
|
|
|
| 79 |
* 2. FEEDBACK (The Main Loop Endpoint)
|
| 80 |
* Accepts text + optional image. Hits Worker. Checks for escalation.
|
| 81 |
*/
|
| 82 |
+
|
| 83 |
app.post('/project/feedback', async (req, res) => {
|
| 84 |
+
const { projectId, prompt, hierarchyContext, scriptContext, logContext, taskComplete } = req.body;
|
| 85 |
|
| 86 |
const project = await StateManager.getProject(projectId);
|
| 87 |
if (!project) return res.status(404).json({ error: "Project not found." });
|
| 88 |
|
| 89 |
+
// 1. MISSION COMPLETE: CALL PM & NUKE WORKER
|
| 90 |
+
if (taskComplete) {
|
| 91 |
+
console.log(`[${projectId}] ✅ TASK COMPLETE. Calling PM & Nuking Worker.`);
|
| 92 |
+
|
| 93 |
+
// A. Notify PM (Add to PM History)
|
| 94 |
+
const summary = `Worker has successfully completed the previous task. Logs: ${logContext?.logs || "Clean"}. Ready for next assignment.`;
|
| 95 |
+
project.pmHistory.push({ role: 'user', parts: [{ text: summary }] });
|
| 96 |
+
|
| 97 |
+
// B. Nuke Worker (Reset History)
|
| 98 |
+
project.workerHistory = [];
|
| 99 |
+
|
| 100 |
+
// C. Update State
|
| 101 |
+
await StateManager.updateProject(projectId, {
|
| 102 |
+
pmHistory: project.pmHistory,
|
| 103 |
+
workerHistory: [], // THE NUKE
|
| 104 |
+
status: "IDLE" // Waiting for user or PM to start next task
|
| 105 |
+
});
|
| 106 |
+
|
| 107 |
+
return res.json({ success: true, message: "Worker Nuked. PM Notified. Loop Stopped." });
|
| 108 |
}
|
| 109 |
+
|
| 110 |
+
// 2. ERROR DETECTION (Safety Net)
|
| 111 |
+
let isFailure = false;
|
| 112 |
+
if (logContext && logContext.logs) {
|
| 113 |
+
const errorKeywords = ["Infinite yield", "Stack Begin", "Error:", "Exception", "failed"];
|
| 114 |
+
if (errorKeywords.some(keyword => logContext.logs.includes(keyword))) {
|
| 115 |
+
isFailure = true;
|
| 116 |
+
console.log(`[${projectId}] ⚠️ Detected Error in Logs.`);
|
| 117 |
+
}
|
| 118 |
}
|
| 119 |
+
|
| 120 |
+
// 3. BUILD AI PROMPT
|
| 121 |
+
const fullInput = `USER: ${prompt || "Automatic Feedback"}` + formatContext({ hierarchyContext, scriptContext, logContext });
|
| 122 |
+
|
| 123 |
+
let finalPrompt = fullInput;
|
| 124 |
+
if (isFailure && (!prompt || prompt.length < 5)) {
|
| 125 |
+
finalPrompt += "\n[SYSTEM ALERT]: Runtime Error detected. Fix the code immediately. Do not use WaitForChild. Do not yield.";
|
| 126 |
}
|
| 127 |
|
| 128 |
+
console.log(`[${projectId}] Processing Feedback...`);
|
| 129 |
|
| 130 |
+
// 4. CALL AI
|
| 131 |
+
try {
|
| 132 |
+
const response = await AIEngine.callWorker(project.workerHistory, finalPrompt);
|
| 133 |
+
|
| 134 |
+
// Escalation Logic
|
| 135 |
+
if (response.includes("STATUS: ESCALATE_TO_PM") || (project.failureCount > 2)) {
|
| 136 |
+
console.log(`[${projectId}] Escalating to PM...`);
|
| 137 |
+
const guidance = await AIEngine.callPM(project.pmHistory, `Worker Stuck. Input: ${fullInput}`);
|
| 138 |
+
const fixed = await AIEngine.callWorker(project.workerHistory, `PM GUIDANCE: ${guidance}`);
|
| 139 |
+
|
| 140 |
+
await StateManager.updateProject(projectId, { failureCount: 0 });
|
| 141 |
+
project.workerHistory.push({ role: 'user', parts: [{ text: fullInput }] });
|
| 142 |
+
project.workerHistory.push({ role: 'model', parts: [{ text: fixed }] });
|
| 143 |
+
await StateManager.queueCommand(projectId, fixed);
|
| 144 |
+
return res.json({ success: true, message: "Escalated & Fixed." });
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
project.workerHistory.push({ role: 'user', parts: [{ text: fullInput }] });
|
| 148 |
+
project.workerHistory.push({ role: 'model', parts: [{ text: response }] });
|
| 149 |
+
await StateManager.updateProject(projectId, { workerHistory: project.workerHistory });
|
| 150 |
+
await StateManager.queueCommand(projectId, response);
|
| 151 |
|
| 152 |
+
res.json({ success: true, message: "Input Processed." });
|
| 153 |
+
|
| 154 |
+
} catch (err) {
|
| 155 |
+
console.error("AI Error:", err);
|
| 156 |
+
res.status(500).json({ error: "AI Failed" });
|
| 157 |
+
}
|
| 158 |
});
|
| 159 |
|
| 160 |
+
|
| 161 |
/**
|
| 162 |
* 3. INTERNAL PING WORKER
|
| 163 |
* PM sends guidance to Worker.
|