Update app.js
Browse files
app.js
CHANGED
|
@@ -14,8 +14,8 @@ const PORT = process.env.PORT || 7860;
|
|
| 14 |
app.use(cors());
|
| 15 |
app.use(bodyParser.json({ limit: '50mb' }));
|
| 16 |
|
| 17 |
-
const WORKER_PHASES = ["Worker: Analyzing
|
| 18 |
-
const PM_PHASES = ["Manager: Reviewing...", "Manager: Formulating Strategy...", "Manager:
|
| 19 |
|
| 20 |
function startStatusLoop(projectId, type = 'worker') {
|
| 21 |
const phases = type === 'pm' ? PM_PHASES : WORKER_PHASES;
|
|
@@ -31,8 +31,7 @@ function startStatusLoop(projectId, type = 'worker') {
|
|
| 31 |
|
| 32 |
async function checkMinimumCredits(userId, type = 'basic') {
|
| 33 |
const { data } = await supabase.from('users').select('credits').eq('id', userId).single();
|
| 34 |
-
|
| 35 |
-
if (credits < 50) throw new Error(`Insufficient ${type} credits.`);
|
| 36 |
}
|
| 37 |
|
| 38 |
async function deductUserCredits(userId, amount, type = 'basic') {
|
|
@@ -45,9 +44,9 @@ async function deductUserCredits(userId, amount, type = 'basic') {
|
|
| 45 |
|
| 46 |
const formatContext = ({ hierarchyContext, scriptContext, logContext }) => {
|
| 47 |
let out = "";
|
| 48 |
-
if (scriptContext) out += `\n[SCRIPT]: ${scriptContext.targetName}\n[SOURCE]: ${scriptContext.scriptSource}`;
|
| 49 |
-
if (logContext) out += `\n[LOGS]: ${logContext.logs}`;
|
| 50 |
-
if (hierarchyContext) out += `\n[
|
| 51 |
return out;
|
| 52 |
};
|
| 53 |
|
|
@@ -62,7 +61,7 @@ async function runAsyncFeedback(projectId, userId, fullInput, images = []) {
|
|
| 62 |
const project = await StateManager.getProject(projectId);
|
| 63 |
StateManager.clearSnapshot(projectId);
|
| 64 |
|
| 65 |
-
// ---
|
| 66 |
let stopStatus = startStatusLoop(projectId, 'worker');
|
| 67 |
let workerPass1 = "";
|
| 68 |
const result1 = await AIEngine.callWorkerStream(
|
|
@@ -75,12 +74,12 @@ async function runAsyncFeedback(projectId, userId, fullInput, images = []) {
|
|
| 75 |
|
| 76 |
const tags = extractTags(workerPass1);
|
| 77 |
if (tags.pmQuestion || tags.pmRoute) {
|
| 78 |
-
//
|
| 79 |
await StateManager.addHistory(projectId, 'worker', 'user', fullInput);
|
| 80 |
await StateManager.addHistory(projectId, 'worker', 'model', workerPass1);
|
| 81 |
await StateManager.queueCommand(projectId, workerPass1);
|
| 82 |
|
| 83 |
-
// ---
|
| 84 |
StateManager.clearSnapshot(projectId);
|
| 85 |
stopStatus = startStatusLoop(projectId, 'pm');
|
| 86 |
const pmInput = tags.pmRoute ? `[ROUTED TASK]: ${tags.pmRoute}` : `[QUESTION]: ${tags.pmQuestion}`;
|
|
@@ -96,18 +95,18 @@ async function runAsyncFeedback(projectId, userId, fullInput, images = []) {
|
|
| 96 |
await StateManager.addHistory(projectId, 'pm', 'model', pmText);
|
| 97 |
await StateManager.queueCommand(projectId, pmText);
|
| 98 |
|
| 99 |
-
//
|
| 100 |
const pmAnswerVisible = `[PM RESPONSE]:\n${pmText}`;
|
| 101 |
await StateManager.addHistory(projectId, 'worker', 'model', pmAnswerVisible);
|
| 102 |
|
| 103 |
-
// ---
|
| 104 |
-
const nextTask = extractTags(pmText).workerPrompt || "Proceed
|
| 105 |
StateManager.clearSnapshot(projectId);
|
| 106 |
stopStatus = startStatusLoop(projectId, 'worker');
|
| 107 |
let workerPass2 = "";
|
| 108 |
-
const
|
| 109 |
await AIEngine.callWorkerStream(
|
| 110 |
-
|
| 111 |
(thought) => { stopStatus(); StateManager.appendSnapshotOnly(projectId, thought); },
|
| 112 |
(chunk) => { stopStatus(); workerPass2 += chunk; StateManager.appendStream(projectId, chunk); }
|
| 113 |
);
|
|
@@ -121,9 +120,9 @@ async function runAsyncFeedback(projectId, userId, fullInput, images = []) {
|
|
| 121 |
}
|
| 122 |
|
| 123 |
await StateManager.updateProject(projectId, { status: "idle" });
|
| 124 |
-
await deductUserCredits(userId, 10);
|
| 125 |
} catch (err) {
|
| 126 |
-
console.error("Feedback Loop
|
| 127 |
StateManager.setStatus(projectId, "Error: " + err.message);
|
| 128 |
await StateManager.updateProject(projectId, { status: "error" });
|
| 129 |
}
|
|
@@ -138,7 +137,7 @@ app.post('/project/feedback', async (req, res) => {
|
|
| 138 |
await StateManager.updateProject(projectId, { status: "working" });
|
| 139 |
res.json({ success: true });
|
| 140 |
const context = formatContext({ hierarchyContext, scriptContext, logContext });
|
| 141 |
-
runAsyncFeedback(projectId, userId, `USER: ${prompt || "Auto
|
| 142 |
} catch (err) { res.status(500).json({ error: err.message }); }
|
| 143 |
});
|
| 144 |
|
|
@@ -154,12 +153,11 @@ app.post('/project/ping', async (req, res) => {
|
|
| 154 |
res.json(resData);
|
| 155 |
});
|
| 156 |
|
| 157 |
-
// Standard Handlers
|
| 158 |
app.post('/onboarding/create', async (req, res) => {
|
| 159 |
const { userId, description, answers } = req.body;
|
| 160 |
const projectId = `proj_${Date.now()}_${crypto.randomBytes(2).toString("hex")}`;
|
| 161 |
const grade = await AIEngine.gradeProject(description, answers);
|
| 162 |
-
await supabase.from('projects').insert({ id: projectId, user_id: userId, info: { title: grade.title,
|
| 163 |
res.json({ success: true, projectId, grade });
|
| 164 |
});
|
| 165 |
|
|
|
|
| 14 |
app.use(cors());
|
| 15 |
app.use(bodyParser.json({ limit: '50mb' }));
|
| 16 |
|
| 17 |
+
const WORKER_PHASES = ["Worker: Analyzing Context...", "Worker: Triage Task...", "Worker: Planning Logic...", "Worker: Thinking..."];
|
| 18 |
+
const PM_PHASES = ["Manager: Reviewing Request...", "Manager: Formulating Strategy...", "Manager: Preparing Delegation...", "Manager: Thinking..."];
|
| 19 |
|
| 20 |
function startStatusLoop(projectId, type = 'worker') {
|
| 21 |
const phases = type === 'pm' ? PM_PHASES : WORKER_PHASES;
|
|
|
|
| 31 |
|
| 32 |
async function checkMinimumCredits(userId, type = 'basic') {
|
| 33 |
const { data } = await supabase.from('users').select('credits').eq('id', userId).single();
|
| 34 |
+
if (!data || (data.credits?.[type] || 0) < 50) throw new Error(`Insufficient credits.`);
|
|
|
|
| 35 |
}
|
| 36 |
|
| 37 |
async function deductUserCredits(userId, amount, type = 'basic') {
|
|
|
|
| 44 |
|
| 45 |
const formatContext = ({ hierarchyContext, scriptContext, logContext }) => {
|
| 46 |
let out = "";
|
| 47 |
+
if (scriptContext) out += `\n[TARGET SCRIPT]: ${scriptContext.targetName}\n[SOURCE PREVIEW]: ${scriptContext.scriptSource}`;
|
| 48 |
+
if (logContext) out += `\n[LAST LOGS]: ${logContext.logs}`;
|
| 49 |
+
if (hierarchyContext) out += `\n[Hierarchy Context]: ${hierarchyContext}`;
|
| 50 |
return out;
|
| 51 |
};
|
| 52 |
|
|
|
|
| 61 |
const project = await StateManager.getProject(projectId);
|
| 62 |
StateManager.clearSnapshot(projectId);
|
| 63 |
|
| 64 |
+
// --- PASS 1: WORKER ---
|
| 65 |
let stopStatus = startStatusLoop(projectId, 'worker');
|
| 66 |
let workerPass1 = "";
|
| 67 |
const result1 = await AIEngine.callWorkerStream(
|
|
|
|
| 74 |
|
| 75 |
const tags = extractTags(workerPass1);
|
| 76 |
if (tags.pmQuestion || tags.pmRoute) {
|
| 77 |
+
// Save Pass 1 immediately
|
| 78 |
await StateManager.addHistory(projectId, 'worker', 'user', fullInput);
|
| 79 |
await StateManager.addHistory(projectId, 'worker', 'model', workerPass1);
|
| 80 |
await StateManager.queueCommand(projectId, workerPass1);
|
| 81 |
|
| 82 |
+
// --- PASS 2: PROJECT MANAGER ---
|
| 83 |
StateManager.clearSnapshot(projectId);
|
| 84 |
stopStatus = startStatusLoop(projectId, 'pm');
|
| 85 |
const pmInput = tags.pmRoute ? `[ROUTED TASK]: ${tags.pmRoute}` : `[QUESTION]: ${tags.pmQuestion}`;
|
|
|
|
| 95 |
await StateManager.addHistory(projectId, 'pm', 'model', pmText);
|
| 96 |
await StateManager.queueCommand(projectId, pmText);
|
| 97 |
|
| 98 |
+
// Save PM answer into Worker's persistent history
|
| 99 |
const pmAnswerVisible = `[PM RESPONSE]:\n${pmText}`;
|
| 100 |
await StateManager.addHistory(projectId, 'worker', 'model', pmAnswerVisible);
|
| 101 |
|
| 102 |
+
// --- PASS 3: WORKER FINAL PASS ---
|
| 103 |
+
const nextTask = extractTags(pmText).workerPrompt || "Proceed based on PM answer.";
|
| 104 |
StateManager.clearSnapshot(projectId);
|
| 105 |
stopStatus = startStatusLoop(projectId, 'worker');
|
| 106 |
let workerPass2 = "";
|
| 107 |
+
const currentProj = await StateManager.getProject(projectId); // Refresh for Pass 2 context
|
| 108 |
await AIEngine.callWorkerStream(
|
| 109 |
+
currentProj.workerHistory, nextTask,
|
| 110 |
(thought) => { stopStatus(); StateManager.appendSnapshotOnly(projectId, thought); },
|
| 111 |
(chunk) => { stopStatus(); workerPass2 += chunk; StateManager.appendStream(projectId, chunk); }
|
| 112 |
);
|
|
|
|
| 120 |
}
|
| 121 |
|
| 122 |
await StateManager.updateProject(projectId, { status: "idle" });
|
| 123 |
+
await deductUserCredits(userId, 10);
|
| 124 |
} catch (err) {
|
| 125 |
+
console.error("Feedback Loop Crashed:", err.message);
|
| 126 |
StateManager.setStatus(projectId, "Error: " + err.message);
|
| 127 |
await StateManager.updateProject(projectId, { status: "error" });
|
| 128 |
}
|
|
|
|
| 137 |
await StateManager.updateProject(projectId, { status: "working" });
|
| 138 |
res.json({ success: true });
|
| 139 |
const context = formatContext({ hierarchyContext, scriptContext, logContext });
|
| 140 |
+
runAsyncFeedback(projectId, userId, `USER: ${prompt || "Auto-Correction Loop"}${context}`, images || []);
|
| 141 |
} catch (err) { res.status(500).json({ error: err.message }); }
|
| 142 |
});
|
| 143 |
|
|
|
|
| 153 |
res.json(resData);
|
| 154 |
});
|
| 155 |
|
|
|
|
| 156 |
app.post('/onboarding/create', async (req, res) => {
|
| 157 |
const { userId, description, answers } = req.body;
|
| 158 |
const projectId = `proj_${Date.now()}_${crypto.randomBytes(2).toString("hex")}`;
|
| 159 |
const grade = await AIEngine.gradeProject(description, answers);
|
| 160 |
+
await supabase.from('projects').insert({ id: projectId, user_id: userId, info: { title: grade.title, status: "idle" } });
|
| 161 |
res.json({ success: true, projectId, grade });
|
| 162 |
});
|
| 163 |
|