everydaycats commited on
Commit
ca47b28
·
verified ·
1 Parent(s): f34d293

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +66 -26
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
- // 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
  /**
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.