Update app.js
Browse files
app.js
CHANGED
|
@@ -180,7 +180,7 @@ app.post('/onboarding/analyze', validateRequest, async (req, res) => {
|
|
| 180 |
|
| 181 |
try {
|
| 182 |
// Analyst uses BASIC models (Flash)
|
| 183 |
-
await checkMinimumCredits(userId, 'basic');
|
| 184 |
|
| 185 |
console.log(`[Onboarding] Analyzing idea...`);
|
| 186 |
|
|
@@ -189,7 +189,7 @@ app.post('/onboarding/analyze', validateRequest, async (req, res) => {
|
|
| 189 |
const usage = result.usage?.totalTokenCount || 0;
|
| 190 |
|
| 191 |
// Bill Basic
|
| 192 |
-
if (usage > 0) await deductUserCredits(userId, usage, 'basic');
|
| 193 |
|
| 194 |
if (result.status === "REJECTED") {
|
| 195 |
return res.json({
|
|
@@ -214,7 +214,7 @@ app.post('/onboarding/create', validateRequest, async (req, res) => {
|
|
| 214 |
|
| 215 |
try {
|
| 216 |
// Pre-flight check
|
| 217 |
-
await checkMinimumCredits(userId, 'basic');
|
| 218 |
|
| 219 |
const randomHex = (n = 6) => crypto.randomBytes(Math.ceil(n/2)).toString("hex").slice(0, n);
|
| 220 |
const projectId = `proj_${Date.now()}_${randomHex(7)}`;
|
|
@@ -314,8 +314,9 @@ app.post('/onboarding/create', validateRequest, async (req, res) => {
|
|
| 314 |
}
|
| 315 |
|
| 316 |
// Deduct Basic Credits
|
| 317 |
-
if (basicTokens > 0) await deductUserCredits(userId, basicTokens, 'basic');
|
| 318 |
-
|
|
|
|
| 319 |
res.json({
|
| 320 |
status: 200,
|
| 321 |
success: !isFailure,
|
|
@@ -399,8 +400,8 @@ async function runBackgroundInitialization(projectId, userId, description) {
|
|
| 399 |
});
|
| 400 |
|
| 401 |
// 7. Deduct Accumulated Credits
|
| 402 |
-
if (diamondUsage > 0) await deductUserCredits(userId, diamondUsage, 'diamond');
|
| 403 |
-
if (basicUsage > 0) await deductUserCredits(userId, basicUsage, 'basic');
|
| 404 |
|
| 405 |
console.log(`[Background] Init complete. Diamond: ${diamondUsage}, Basic: ${basicUsage}`);
|
| 406 |
|
|
@@ -415,8 +416,8 @@ app.post('/new/project', validateRequest, async (req, res) => {
|
|
| 415 |
|
| 416 |
try {
|
| 417 |
// Init requires BOTH types to be safe
|
| 418 |
-
await checkMinimumCredits(userId, 'diamond');
|
| 419 |
-
await checkMinimumCredits(userId, 'basic');
|
| 420 |
|
| 421 |
if(db) db.ref(`projects/${projectId}/info/status`).set("initializing");
|
| 422 |
|
|
@@ -447,7 +448,7 @@ app.post('/project/feedback', async (req, res) => {
|
|
| 447 |
if (project.userId !== userId) return res.status(403).json({ error: "Unauthorized" });
|
| 448 |
|
| 449 |
// Basic Check (most interaction is worker)
|
| 450 |
-
await checkMinimumCredits(userId, 'basic');
|
| 451 |
|
| 452 |
if(db) await db.ref(`projects/${projectId}/info/status`).set("working");
|
| 453 |
|
|
@@ -497,9 +498,11 @@ app.post('/project/feedback', async (req, res) => {
|
|
| 497 |
if(db) await db.ref(`projects/${projectId}/info`).update({ status: "working", lastUpdated: Date.now() });
|
| 498 |
|
| 499 |
// Final Bill
|
|
|
|
| 500 |
if (diamondUsage > 0) await deductUserCredits(userId, diamondUsage, 'diamond');
|
| 501 |
if (basicUsage > 0) await deductUserCredits(userId, basicUsage, 'basic');
|
| 502 |
-
|
|
|
|
| 503 |
return res.json({ success: true, message: "Next Task Assigned" });
|
| 504 |
}
|
| 505 |
|
|
@@ -528,9 +531,9 @@ app.post('/project/feedback', async (req, res) => {
|
|
| 528 |
StateManager.queueCommand(projectId, { type: "EXECUTE", payload: "print('SYSTEM: Worker Reset')" });
|
| 529 |
await processAndQueueResponse(projectId, workerResult.text, userId);
|
| 530 |
|
| 531 |
-
|
| 532 |
if (basicUsage > 0) await deductUserCredits(userId, basicUsage, 'basic');
|
| 533 |
-
return res.json({ success: true, message: "Worker Terminated." });
|
| 534 |
} else {
|
| 535 |
const injection = `[PM GUIDANCE]: ${pmVerdict} \n\nApply this fix now.`;
|
| 536 |
|
|
@@ -544,9 +547,9 @@ app.post('/project/feedback', async (req, res) => {
|
|
| 544 |
await StateManager.updateProject(projectId, { workerHistory: project.workerHistory, failureCount: 1 });
|
| 545 |
await processAndQueueResponse(projectId, workerResult.text, userId);
|
| 546 |
|
| 547 |
-
|
| 548 |
if (basicUsage > 0) await deductUserCredits(userId, basicUsage, 'basic');
|
| 549 |
-
|
| 550 |
}
|
| 551 |
}
|
| 552 |
|
|
@@ -596,9 +599,9 @@ app.post('/project/feedback', async (req, res) => {
|
|
| 596 |
if(db) await db.ref(`projects/${projectId}/info`).update({ status: "idle", lastUpdated: Date.now() });
|
| 597 |
|
| 598 |
// Final Billing
|
| 599 |
-
if (diamondUsage > 0) await deductUserCredits(userId, diamondUsage, 'diamond');
|
| 600 |
if (basicUsage > 0) await deductUserCredits(userId, basicUsage, 'basic');
|
| 601 |
-
|
| 602 |
res.json({ success: true });
|
| 603 |
|
| 604 |
} catch (err) {
|
|
@@ -640,7 +643,7 @@ app.post('/human/override', validateRequest, async (req, res) => {
|
|
| 640 |
let basicUsage = 0;
|
| 641 |
|
| 642 |
try {
|
| 643 |
-
await checkMinimumCredits(userId, 'basic');
|
| 644 |
|
| 645 |
const project = await StateManager.getProject(projectId);
|
| 646 |
const overrideMsg = `[SYSTEM OVERRIDE]: ${instruction}`;
|
|
@@ -660,7 +663,7 @@ app.post('/human/override', validateRequest, async (req, res) => {
|
|
| 660 |
await StateManager.updateProject(projectId, { workerHistory: project.workerHistory });
|
| 661 |
await processAndQueueResponse(projectId, workerResult.text, userId);
|
| 662 |
|
| 663 |
-
if (basicUsage > 0) await deductUserCredits(userId, basicUsage, 'basic');
|
| 664 |
|
| 665 |
res.json({ success: true });
|
| 666 |
} catch (err) {
|
|
@@ -685,9 +688,9 @@ async function processAndQueueResponse(projectId, rawResponse, userId) {
|
|
| 685 |
const imgTokens = imgResult.usage?.totalTokenCount || 0;
|
| 686 |
const totalCost = imgTokens; // imgTokens + IMAGE_COST_BASIC;
|
| 687 |
|
| 688 |
-
if (userId && totalCost > 0) {
|
| 689 |
await deductUserCredits(userId, totalCost, 'basic');
|
| 690 |
-
}
|
| 691 |
|
| 692 |
// 3. Queue creation command
|
| 693 |
await StateManager.queueCommand(projectId, { type: "CREATE_ASSET", payload: imgResult.image });
|
|
|
|
| 180 |
|
| 181 |
try {
|
| 182 |
// Analyst uses BASIC models (Flash)
|
| 183 |
+
// await checkMinimumCredits(userId, 'basic');
|
| 184 |
|
| 185 |
console.log(`[Onboarding] Analyzing idea...`);
|
| 186 |
|
|
|
|
| 189 |
const usage = result.usage?.totalTokenCount || 0;
|
| 190 |
|
| 191 |
// Bill Basic
|
| 192 |
+
// if (usage > 0) await deductUserCredits(userId, usage, 'basic');
|
| 193 |
|
| 194 |
if (result.status === "REJECTED") {
|
| 195 |
return res.json({
|
|
|
|
| 214 |
|
| 215 |
try {
|
| 216 |
// Pre-flight check
|
| 217 |
+
// await checkMinimumCredits(userId, 'basic');
|
| 218 |
|
| 219 |
const randomHex = (n = 6) => crypto.randomBytes(Math.ceil(n/2)).toString("hex").slice(0, n);
|
| 220 |
const projectId = `proj_${Date.now()}_${randomHex(7)}`;
|
|
|
|
| 314 |
}
|
| 315 |
|
| 316 |
// Deduct Basic Credits
|
| 317 |
+
//if (basicTokens > 0) await deductUserCredits(userId, basicTokens, 'basic');
|
| 318 |
+
|
| 319 |
+
console.log("sending");
|
| 320 |
res.json({
|
| 321 |
status: 200,
|
| 322 |
success: !isFailure,
|
|
|
|
| 400 |
});
|
| 401 |
|
| 402 |
// 7. Deduct Accumulated Credits
|
| 403 |
+
// if (diamondUsage > 0) await deductUserCredits(userId, diamondUsage, 'diamond');
|
| 404 |
+
// if (basicUsage > 0) await deductUserCredits(userId, basicUsage, 'basic');
|
| 405 |
|
| 406 |
console.log(`[Background] Init complete. Diamond: ${diamondUsage}, Basic: ${basicUsage}`);
|
| 407 |
|
|
|
|
| 416 |
|
| 417 |
try {
|
| 418 |
// Init requires BOTH types to be safe
|
| 419 |
+
// await checkMinimumCredits(userId, 'diamond');
|
| 420 |
+
// await checkMinimumCredits(userId, 'basic');
|
| 421 |
|
| 422 |
if(db) db.ref(`projects/${projectId}/info/status`).set("initializing");
|
| 423 |
|
|
|
|
| 448 |
if (project.userId !== userId) return res.status(403).json({ error: "Unauthorized" });
|
| 449 |
|
| 450 |
// Basic Check (most interaction is worker)
|
| 451 |
+
// await checkMinimumCredits(userId, 'basic');
|
| 452 |
|
| 453 |
if(db) await db.ref(`projects/${projectId}/info/status`).set("working");
|
| 454 |
|
|
|
|
| 498 |
if(db) await db.ref(`projects/${projectId}/info`).update({ status: "working", lastUpdated: Date.now() });
|
| 499 |
|
| 500 |
// Final Bill
|
| 501 |
+
/*
|
| 502 |
if (diamondUsage > 0) await deductUserCredits(userId, diamondUsage, 'diamond');
|
| 503 |
if (basicUsage > 0) await deductUserCredits(userId, basicUsage, 'basic');
|
| 504 |
+
*/
|
| 505 |
+
|
| 506 |
return res.json({ success: true, message: "Next Task Assigned" });
|
| 507 |
}
|
| 508 |
|
|
|
|
| 531 |
StateManager.queueCommand(projectId, { type: "EXECUTE", payload: "print('SYSTEM: Worker Reset')" });
|
| 532 |
await processAndQueueResponse(projectId, workerResult.text, userId);
|
| 533 |
|
| 534 |
+
/* if (diamondUsage > 0) await deductUserCredits(userId, diamondUsage, 'diamond');
|
| 535 |
if (basicUsage > 0) await deductUserCredits(userId, basicUsage, 'basic');
|
| 536 |
+
*/ return res.json({ success: true, message: "Worker Terminated." });
|
| 537 |
} else {
|
| 538 |
const injection = `[PM GUIDANCE]: ${pmVerdict} \n\nApply this fix now.`;
|
| 539 |
|
|
|
|
| 547 |
await StateManager.updateProject(projectId, { workerHistory: project.workerHistory, failureCount: 1 });
|
| 548 |
await processAndQueueResponse(projectId, workerResult.text, userId);
|
| 549 |
|
| 550 |
+
/* if (diamondUsage > 0) await deductUserCredits(userId, diamondUsage, 'diamond');
|
| 551 |
if (basicUsage > 0) await deductUserCredits(userId, basicUsage, 'basic');
|
| 552 |
+
*/ return res.json({ success: true, message: "PM Guidance Applied." });
|
| 553 |
}
|
| 554 |
}
|
| 555 |
|
|
|
|
| 599 |
if(db) await db.ref(`projects/${projectId}/info`).update({ status: "idle", lastUpdated: Date.now() });
|
| 600 |
|
| 601 |
// Final Billing
|
| 602 |
+
/* if (diamondUsage > 0) await deductUserCredits(userId, diamondUsage, 'diamond');
|
| 603 |
if (basicUsage > 0) await deductUserCredits(userId, basicUsage, 'basic');
|
| 604 |
+
*/
|
| 605 |
res.json({ success: true });
|
| 606 |
|
| 607 |
} catch (err) {
|
|
|
|
| 643 |
let basicUsage = 0;
|
| 644 |
|
| 645 |
try {
|
| 646 |
+
// await checkMinimumCredits(userId, 'basic');
|
| 647 |
|
| 648 |
const project = await StateManager.getProject(projectId);
|
| 649 |
const overrideMsg = `[SYSTEM OVERRIDE]: ${instruction}`;
|
|
|
|
| 663 |
await StateManager.updateProject(projectId, { workerHistory: project.workerHistory });
|
| 664 |
await processAndQueueResponse(projectId, workerResult.text, userId);
|
| 665 |
|
| 666 |
+
// if (basicUsage > 0) await deductUserCredits(userId, basicUsage, 'basic');
|
| 667 |
|
| 668 |
res.json({ success: true });
|
| 669 |
} catch (err) {
|
|
|
|
| 688 |
const imgTokens = imgResult.usage?.totalTokenCount || 0;
|
| 689 |
const totalCost = imgTokens; // imgTokens + IMAGE_COST_BASIC;
|
| 690 |
|
| 691 |
+
/* if (userId && totalCost > 0) {
|
| 692 |
await deductUserCredits(userId, totalCost, 'basic');
|
| 693 |
+
} */
|
| 694 |
|
| 695 |
// 3. Queue creation command
|
| 696 |
await StateManager.queueCommand(projectId, { type: "CREATE_ASSET", payload: imgResult.image });
|