everydaytok commited on
Commit
c146b75
·
verified ·
1 Parent(s): 89ca1d8

Update app.js

Browse files
Files changed (1) hide show
  1. app.js +92 -0
app.js CHANGED
@@ -122,6 +122,7 @@ function formatContext({ hierarchyContext, scriptContext, logContext }) {
122
 
123
  // --- CORE BACKGROUND LOGIC ---
124
 
 
125
  async function runBackgroundInitialization(projectId, userId, description) {
126
  console.log(`[Background] Starting initialization for ${projectId}`);
127
  let diamondUsage = 0;
@@ -201,6 +202,97 @@ async function runBackgroundInitialization(projectId, userId, description) {
201
  await StateManager.updateProject(projectId, { status: "error" });
202
  }
203
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
204
 
205
  // --- ONBOARDING ENDPOINTS ---
206
 
 
122
 
123
  // --- CORE BACKGROUND LOGIC ---
124
 
125
+ /*
126
  async function runBackgroundInitialization(projectId, userId, description) {
127
  console.log(`[Background] Starting initialization for ${projectId}`);
128
  let diamondUsage = 0;
 
202
  await StateManager.updateProject(projectId, { status: "error" });
203
  }
204
  }
205
+ */
206
+
207
+ // app.js - runBackgroundInitialization update
208
+
209
+ async function runBackgroundInitialization(projectId, userId, description) {
210
+ // GUARD: Prevent double execution
211
+ if (StateManager.isLocked(projectId)) {
212
+ console.log(`[Init Guard] Project ${projectId} is already initializing. Skipping.`);
213
+ return;
214
+ }
215
+
216
+ StateManager.lock(projectId);
217
+ console.log(`[Background] Starting initialization for ${projectId}`);
218
+
219
+ let diamondUsage = 0;
220
+ let basicUsage = 0;
221
+
222
+ try {
223
+ // --- 1. SET STATUS TO WORKING IMMEDIATELY ---
224
+ await StateManager.updateProject(projectId, {
225
+ status: "WORKING",
226
+ gdd: "",
227
+ failureCount: 0
228
+ });
229
+
230
+ const pmHistory = [];
231
+
232
+ // --- 2. GENERATE GDD ---
233
+ const gddPrompt = `Create a comprehensive Game Design Document (GDD) for: ${description}`;
234
+ const gddResult = await AIEngine.callPM(pmHistory, gddPrompt);
235
+
236
+ if (!gddResult?.text) throw new Error("PM failed to generate GDD");
237
+
238
+ const gddText = gddResult.text;
239
+ diamondUsage += (gddResult.usage?.totalTokenCount || 0);
240
+
241
+ // Always back up to DB immediately
242
+ await StateManager.addHistory(projectId, 'pm', 'user', gddPrompt);
243
+ await StateManager.addHistory(projectId, 'pm', 'model', gddText);
244
+
245
+ // Update PM History for the next step context
246
+ pmHistory.push({ role: 'user', parts: [{ text: gddPrompt }] });
247
+ pmHistory.push({ role: 'model', parts: [{ text: gddText }] });
248
+
249
+ // --- 3. GENERATE FIRST TASK ---
250
+ const taskPrompt = "Based on the GDD, generate the first technical milestone.\nOutput format:\nTASK_NAME: <Name>\nWORKER_PROMPT: <Specific, isolated instructions for the worker>";
251
+ const taskResult = await AIEngine.callPM(pmHistory, taskPrompt);
252
+
253
+ if (!taskResult?.text) throw new Error("PM failed to generate Task");
254
+
255
+ const taskText = taskResult.text;
256
+ diamondUsage += (taskResult.usage?.totalTokenCount || 0);
257
+
258
+ await StateManager.addHistory(projectId, 'pm', 'user', taskPrompt);
259
+ await StateManager.addHistory(projectId, 'pm', 'model', taskText);
260
+
261
+ // --- 4. INITIALIZE WORKER ---
262
+ const initialWorkerInstruction = extractWorkerPrompt(taskText) || `Initialize structure for: ${description}`;
263
+ const workerHistory = [];
264
+ const initialWorkerPrompt = `CONTEXT: New Project. \nINSTRUCTION: ${initialWorkerInstruction}`;
265
+
266
+ const workerResult = await AIEngine.callWorker(workerHistory, initialWorkerPrompt, []);
267
+ if (!workerResult?.text) throw new Error("Worker failed to initialize");
268
+
269
+ basicUsage += (workerResult.usage?.totalTokenCount || 0);
270
+ const workerText = workerResult.text;
271
+
272
+ await StateManager.addHistory(projectId, 'worker', 'user', initialWorkerPrompt);
273
+ await StateManager.addHistory(projectId, 'worker', 'model', workerText);
274
+
275
+ // --- 5. FINISH & CLEANUP ---
276
+ await StateManager.updateProject(projectId, {
277
+ gdd: gddText,
278
+ status: "IDLE" // Set back to IDLE when done
279
+ });
280
+
281
+ await processAndQueueResponse(projectId, workerText, userId);
282
+
283
+ if (diamondUsage > 0) await deductUserCredits(userId, diamondUsage, 'diamond');
284
+ if (basicUsage > 0) await deductUserCredits(userId, basicUsage, 'basic');
285
+
286
+ console.log(`[Background] Init complete for ${projectId}`);
287
+
288
+ } catch (err) {
289
+ console.error(`[Background] Init Error for ${projectId}:`, err.message);
290
+ await StateManager.updateProject(projectId, { status: "ERROR" });
291
+ } finally {
292
+ // ALWAYS unlock even if it fails
293
+ StateManager.unlock(projectId);
294
+ }
295
+ }
296
 
297
  // --- ONBOARDING ENDPOINTS ---
298