Spaces:
Sleeping
Sleeping
| // server.js | |
| import express from "express"; | |
| import bodyParser from "body-parser"; | |
| import cors from "cors"; | |
| import fs from "fs"; | |
| import path from "path"; | |
| import { fileURLToPath } from "url"; | |
| const __filename = fileURLToPath(import.meta.url); | |
| const __dirname = path.dirname(__filename); | |
| const PORT = process.env.PORT || 7860; | |
| const app = express(); | |
| app.use(cors()); | |
| app.use(bodyParser.json({ limit: "50mb" })); | |
| // === DEMO CONTROLLER === | |
| // CHANGE THIS TO TEST DIFFERENT INTERACTIONS | |
| // Options: 'NONE' (Just save), 'BUILD_PART' (Send Lua Code), 'FETCH_SCRIPT' (Ask for Source) | |
| const DEMO_MODE = 'BUILD_PART'; | |
| app.post("/api/ai-build", async (req, res) => { | |
| try { | |
| const { instruction, state, scriptContext } = req.body; | |
| console.log("π₯ Received Request"); | |
| console.log(" Instruction:", instruction); | |
| // 1. Log Payload Size (Check token efficiency) | |
| if(state && state.flatList) { | |
| console.log(` Hierarchy Size: ${state.flatList.length} items (Flat Format)`); | |
| } | |
| // 2. IF WE RECEIVED SCRIPT SOURCE (From a previous fetch request) | |
| if (scriptContext) { | |
| console.log(" π Received Script Source for:", scriptContext.name); | |
| console.log(" --- START SOURCE ---\n", scriptContext.source.substring(0, 100) + "...", "\n --- END SOURCE ---"); | |
| // In a real app, you would now feed this to the AI to edit. | |
| return res.json({ success: true, message: "Source received, analyzing..." }); | |
| } | |
| // 3. DEMO RESPONSES | |
| if (DEMO_MODE === 'BUILD_PART') { | |
| console.log(" π Sending Code Demo"); | |
| const luaCode = ` | |
| ```lua | |
| -- AI Generated Demo | |
| local p = Instance.new("Part") | |
| p.Name = "AI_Generated_Cube" | |
| p.Color = Color3.fromRGB(0, 255, 100) | |
| p.Size = Vector3.new(4, 4, 4) | |
| p.Position = Vector3.new(0, 10, 0) | |
| p.Anchored = true | |
| p.Parent = workspace | |
| print("Hello from the Server! I made a part.") | |
| ``` | |
| `; | |
| return res.send(luaCode); // Sending raw markdown/text | |
| } | |
| else if (DEMO_MODE === 'FETCH_SCRIPT') { | |
| console.log(" π Sending Fetch Request Demo"); | |
| // This tells the plugin to find a script named "BikeLogic" and send it back | |
| return res.json({ | |
| action: "read_script", | |
| targetName: "BikeLogic" // CHANGE THIS to a script name that actually exists in your studio | |
| }); | |
| } | |
| // Default: Just acknowledge | |
| return res.json({ success: true, assetId: "req_" + Date.now() }); | |
| } catch (err) { | |
| console.error("Server Error:", err); | |
| res.status(500).send("Server Error"); | |
| } | |
| }); | |
| app.listen(PORT, () => console.log(`π Server running on port ${PORT}. Demo Mode: ${DEMO_MODE}`)); |