Spaces:
Running
Running
Update app.js
Browse files
app.js
CHANGED
|
@@ -79,59 +79,43 @@ 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',
|
| 83 |
-
const { projectId, prompt,
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
-
|
| 90 |
-
if (imageBase64) {
|
| 91 |
-
imagePart = { inlineData: { mimeType: 'image/png', data: imageBase64 } };
|
| 92 |
-
}
|
| 93 |
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
// Auto-trigger internal PM ping
|
| 106 |
-
// We reuse the logic from /internal/ping/pm here for efficiency
|
| 107 |
-
const distressSignal = `Worker failed to solve: ${prompt}. Last response: ${response}`;
|
| 108 |
-
const pmGuidance = await AIEngine.callPM(
|
| 109 |
-
project.pmHistory,
|
| 110 |
-
sysPrompts.pm_guidance_prompt.replace('{{DISTRESS_SIGNAL}}', distressSignal)
|
| 111 |
-
);
|
| 112 |
-
|
| 113 |
-
// Send guidance back to worker
|
| 114 |
-
const fixedWorkerResponse = await AIEngine.callWorker(project.workerHistory, `PM GUIDANCE: ${pmGuidance}. Try again.`);
|
| 115 |
-
|
| 116 |
-
project.failureCount = 0; // Reset failure count
|
| 117 |
-
StateManager.queueCommand(projectId, fixedWorkerResponse);
|
| 118 |
-
await StateManager.updateProject(projectId, { pmHistory: project.pmHistory, workerHistory: project.workerHistory, failureCount: 0 });
|
| 119 |
-
|
| 120 |
-
return res.json({ success: true, message: "Escalated to PM and Resolved", type: "ESCALATION" });
|
| 121 |
-
} else {
|
| 122 |
-
// Standard Success
|
| 123 |
-
StateManager.queueCommand(projectId, response);
|
| 124 |
-
await StateManager.updateProject(projectId, { workerHistory: project.workerHistory });
|
| 125 |
-
return res.json({ success: true, message: "Processed by Worker" });
|
| 126 |
-
}
|
| 127 |
|
| 128 |
-
|
| 129 |
-
const project = await StateManager.getProject(projectId);
|
| 130 |
-
if (project) {
|
| 131 |
-
await StateManager.updateProject(projectId, { failureCount: (project.failureCount || 0) + 1 });
|
| 132 |
-
}
|
| 133 |
-
res.status(500).json({ error: err.message });
|
| 134 |
-
}
|
| 135 |
});
|
| 136 |
|
| 137 |
/**
|
|
@@ -200,6 +184,7 @@ app.post('/project/reset', validateRequest, async (req, res) => {
|
|
| 200 |
* 6. PROJECT PING (The Roblox Plugin Endpoint)
|
| 201 |
* Plugin calls this to fetch executable code.
|
| 202 |
*/
|
|
|
|
| 203 |
app.post('/project/ping', validateRequest, async (req, res) => {
|
| 204 |
const { projectId } = req.body;
|
| 205 |
const project = await StateManager.getProject(projectId);
|
|
@@ -219,6 +204,7 @@ app.post('/project/ping', validateRequest, async (req, res) => {
|
|
| 219 |
codeSnippet: command.code // The markdown snippet extracted in StateManager
|
| 220 |
});
|
| 221 |
});
|
|
|
|
| 222 |
|
| 223 |
/**
|
| 224 |
* 7. INACTIVITY CLEANUP
|
|
@@ -228,6 +214,22 @@ app.delete('/internal/cleanup', async (req, res) => {
|
|
| 228 |
res.json({ success: true, removed: count });
|
| 229 |
});
|
| 230 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
app.listen(PORT, () => {
|
| 232 |
console.log(`AI Builder Backend running on port ${PORT}`);
|
| 233 |
});
|
|
|
|
| 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 |
+
// Build Context String based on what was sent
|
| 89 |
+
let contextStr = "";
|
| 90 |
+
if (prompt) contextStr += `USER: ${prompt}\n`;
|
| 91 |
+
|
| 92 |
+
// If Plugin sent Hierarchy
|
| 93 |
+
if (hierarchyContext) {
|
| 94 |
+
contextStr += `[SYSTEM] HIERARCHY SCAN (${hierarchyContext.rootName}):\n${hierarchyContext.tree}\n`;
|
| 95 |
+
}
|
| 96 |
+
// If Plugin sent Script Source
|
| 97 |
+
if (scriptContext) {
|
| 98 |
+
contextStr += `[SYSTEM] SCRIPT SOURCE (${scriptContext.targetName}):\n${scriptContext.scriptSource}\n`;
|
| 99 |
+
}
|
| 100 |
+
// If Plugin sent Logs
|
| 101 |
+
if (logContext) {
|
| 102 |
+
contextStr += `[SYSTEM] CONSOLE LOGS:\n${logContext.logs}\n`;
|
| 103 |
+
}
|
| 104 |
|
| 105 |
+
console.log(`[${projectId}] Input Received. Processing...`);
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
+
// Call Worker
|
| 108 |
+
const response = await AIEngine.callWorker(project.workerHistory, contextStr);
|
| 109 |
+
|
| 110 |
+
// Standard History Update
|
| 111 |
+
project.workerHistory.push({ role: 'user', parts: [{ text: contextStr }] });
|
| 112 |
+
project.workerHistory.push({ role: 'model', parts: [{ text: response }] });
|
| 113 |
+
await StateManager.updateProject(projectId, { workerHistory: project.workerHistory });
|
| 114 |
+
|
| 115 |
+
// Parse and Queue the next move (Execute code or Fetch more info)
|
| 116 |
+
await StateManager.queueCommand(projectId, response);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
+
res.json({ success: true, message: "Context received. Worker processing." });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
});
|
| 120 |
|
| 121 |
/**
|
|
|
|
| 184 |
* 6. PROJECT PING (The Roblox Plugin Endpoint)
|
| 185 |
* Plugin calls this to fetch executable code.
|
| 186 |
*/
|
| 187 |
+
/*
|
| 188 |
app.post('/project/ping', validateRequest, async (req, res) => {
|
| 189 |
const { projectId } = req.body;
|
| 190 |
const project = await StateManager.getProject(projectId);
|
|
|
|
| 204 |
codeSnippet: command.code // The markdown snippet extracted in StateManager
|
| 205 |
});
|
| 206 |
});
|
| 207 |
+
*/
|
| 208 |
|
| 209 |
/**
|
| 210 |
* 7. INACTIVITY CLEANUP
|
|
|
|
| 214 |
res.json({ success: true, removed: count });
|
| 215 |
});
|
| 216 |
|
| 217 |
+
app.post('/project/ping', async (req, res) => {
|
| 218 |
+
const { projectId } = req.body;
|
| 219 |
+
const command = await StateManager.popCommand(projectId);
|
| 220 |
+
|
| 221 |
+
if (command) {
|
| 222 |
+
// Sends: { action: "EXECUTE", target: "...", code: "..." }
|
| 223 |
+
res.json({
|
| 224 |
+
action: command.type,
|
| 225 |
+
target: command.payload,
|
| 226 |
+
code: command.type === 'EXECUTE' ? command.payload : null
|
| 227 |
+
});
|
| 228 |
+
} else {
|
| 229 |
+
res.json({ action: "IDLE" });
|
| 230 |
+
}
|
| 231 |
+
});
|
| 232 |
+
|
| 233 |
app.listen(PORT, () => {
|
| 234 |
console.log(`AI Builder Backend running on port ${PORT}`);
|
| 235 |
});
|