Spaces:
Sleeping
Sleeping
Update app.js
Browse files
app.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
// server.js
|
| 2 |
import express from "express";
|
| 3 |
import bodyParser from "body-parser";
|
| 4 |
import cors from "cors";
|
|
@@ -12,13 +12,11 @@ const PORT = process.env.PORT || 7860;
|
|
| 12 |
|
| 13 |
const app = express();
|
| 14 |
app.use(cors());
|
| 15 |
-
// Increased limit to 50mb to handle large complex workspaces
|
| 16 |
app.use(bodyParser.json({ limit: "50mb" }));
|
| 17 |
|
| 18 |
// === DEMO CONTROLLER ===
|
| 19 |
-
//
|
| 20 |
-
|
| 21 |
-
const DEMO_MODE = "FETCH_SCRIPT"; //'BUILD_PART';
|
| 22 |
|
| 23 |
app.post("/api/ai-build", async (req, res) => {
|
| 24 |
try {
|
|
@@ -27,40 +25,38 @@ app.post("/api/ai-build", async (req, res) => {
|
|
| 27 |
console.log("📥 Received Request");
|
| 28 |
console.log(" Instruction:", instruction);
|
| 29 |
|
| 30 |
-
// 1. Log Payload Size (Check token efficiency)
|
| 31 |
if(state && state.flatList) {
|
| 32 |
console.log(` Hierarchy Size: ${state.flatList.length} items (Flat Format)`);
|
| 33 |
-
|
| 34 |
-
|
| 35 |
}
|
| 36 |
|
| 37 |
-
//
|
| 38 |
if (scriptContext) {
|
| 39 |
console.log(" 📘 Received Script Source for:", scriptContext.targetName);
|
| 40 |
console.log(" --- START SOURCE ---\n", scriptContext.scriptSource.substring(0, 100) + "...", "\n --- END SOURCE ---");
|
| 41 |
-
// In a real app, you would now feed this to the AI to edit.
|
| 42 |
-
// For demo purposes, we can acknowledge and then send code back:
|
| 43 |
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
\`\`\`lua
|
| 46 |
-
-- AI
|
| 47 |
-
|
| 48 |
-
p.Name = "AI_SCRIPT_ACKNOWLEDGED"
|
| 49 |
-
p.Color = Color3.fromRGB(255, 255, 0)
|
| 50 |
-
p.Size = Vector3.new(2, 2, 2)
|
| 51 |
-
p.Position = Vector3.new(5, 5, 0)
|
| 52 |
-
p.Anchored = true
|
| 53 |
-
p.Parent = workspace
|
| 54 |
-
print("Script analysis complete. Confirmation part created.")
|
| 55 |
\`\`\`
|
| 56 |
`;
|
| 57 |
-
return res.send(
|
|
|
|
| 58 |
}
|
| 59 |
|
| 60 |
-
//
|
| 61 |
if (DEMO_MODE === 'BUILD_PART') {
|
| 62 |
-
|
| 63 |
-
// FIX: Using template literals (backticks `) for multi-line string
|
| 64 |
const luaCode = `
|
| 65 |
\`\`\`lua
|
| 66 |
-- AI Generated Demo: Part Builder
|
|
@@ -74,19 +70,19 @@ app.post("/api/ai-build", async (req, res) => {
|
|
| 74 |
print("Hello from the Server! I made a part.")
|
| 75 |
\`\`\`
|
| 76 |
`;
|
| 77 |
-
return res.send(luaCode);
|
| 78 |
}
|
| 79 |
|
| 80 |
else if (DEMO_MODE === 'FETCH_SCRIPT') {
|
| 81 |
-
console.log(" 👉 Sending Fetch Request Demo");
|
| 82 |
// This tells the plugin to find a script named "BikeLogic" and send it back
|
| 83 |
return res.json({
|
| 84 |
action: "read_script",
|
| 85 |
-
targetName: "BikeLogic"
|
| 86 |
});
|
| 87 |
}
|
| 88 |
|
| 89 |
-
// Default
|
| 90 |
return res.json({ success: true, assetId: "req_" + Date.now(), message: "Snapshot received. AI is thinking..." });
|
| 91 |
|
| 92 |
} catch (err) {
|
|
|
|
| 1 |
+
// server.js (Only the app.post function and surrounding context needed)
|
| 2 |
import express from "express";
|
| 3 |
import bodyParser from "body-parser";
|
| 4 |
import cors from "cors";
|
|
|
|
| 12 |
|
| 13 |
const app = express();
|
| 14 |
app.use(cors());
|
|
|
|
| 15 |
app.use(bodyParser.json({ limit: "50mb" }));
|
| 16 |
|
| 17 |
// === DEMO CONTROLLER ===
|
| 18 |
+
// Options: 'NONE', 'BUILD_PART', 'FETCH_SCRIPT'
|
| 19 |
+
const DEMO_MODE = 'FETCH_SCRIPT'; // Keeping this set for the test
|
|
|
|
| 20 |
|
| 21 |
app.post("/api/ai-build", async (req, res) => {
|
| 22 |
try {
|
|
|
|
| 25 |
console.log("📥 Received Request");
|
| 26 |
console.log(" Instruction:", instruction);
|
| 27 |
|
|
|
|
| 28 |
if(state && state.flatList) {
|
| 29 |
console.log(` Hierarchy Size: ${state.flatList.length} items (Flat Format)`);
|
| 30 |
+
//console.log(`Flatlist : ${JSON.stringify(state.flatList).length}`);
|
|
|
|
| 31 |
}
|
| 32 |
|
| 33 |
+
// 1. **FIXED LOOP LOGIC:** IF WE RECEIVED SCRIPT SOURCE (Step 2 of the loop)
|
| 34 |
if (scriptContext) {
|
| 35 |
console.log(" 📘 Received Script Source for:", scriptContext.targetName);
|
| 36 |
console.log(" --- START SOURCE ---\n", scriptContext.scriptSource.substring(0, 100) + "...", "\n --- END SOURCE ---");
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
// **STOP THE LOOP:** Send back a simple JSON message instead of executable code.
|
| 39 |
+
// In a real application, the AI would generate the final Lua code here.
|
| 40 |
+
return res.json({
|
| 41 |
+
success: true,
|
| 42 |
+
message: `Source for ${scriptContext.targetName} received and analyzed. Loop stopped in demo mode.`
|
| 43 |
+
});
|
| 44 |
+
|
| 45 |
+
// If you were testing the final code output, you would send the code here:
|
| 46 |
+
/*
|
| 47 |
+
const finalCode = `
|
| 48 |
\`\`\`lua
|
| 49 |
+
-- Final AI-modified code for ${scriptContext.targetName}
|
| 50 |
+
print("Final Code Executed!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
\`\`\`
|
| 52 |
`;
|
| 53 |
+
return res.send(finalCode);
|
| 54 |
+
*/
|
| 55 |
}
|
| 56 |
|
| 57 |
+
// 2. DEMO RESPONSES (Step 1 of the loop/initial request)
|
| 58 |
if (DEMO_MODE === 'BUILD_PART') {
|
| 59 |
+
// ... (BUILD_PART logic remains the same)
|
|
|
|
| 60 |
const luaCode = `
|
| 61 |
\`\`\`lua
|
| 62 |
-- AI Generated Demo: Part Builder
|
|
|
|
| 70 |
print("Hello from the Server! I made a part.")
|
| 71 |
\`\`\`
|
| 72 |
`;
|
| 73 |
+
return res.send(luaCode);
|
| 74 |
}
|
| 75 |
|
| 76 |
else if (DEMO_MODE === 'FETCH_SCRIPT') {
|
| 77 |
+
console.log(" 👉 Sending Fetch Request Demo (Step 1)");
|
| 78 |
// This tells the plugin to find a script named "BikeLogic" and send it back
|
| 79 |
return res.json({
|
| 80 |
action: "read_script",
|
| 81 |
+
targetName: "BikeLogic"
|
| 82 |
});
|
| 83 |
}
|
| 84 |
|
| 85 |
+
// Default
|
| 86 |
return res.json({ success: true, assetId: "req_" + Date.now(), message: "Snapshot received. AI is thinking..." });
|
| 87 |
|
| 88 |
} catch (err) {
|