File size: 2,755 Bytes
9903c38
b06a41a
 
 
ab9b3de
 
 
b06a41a
ab9b3de
 
03e89dc
9903c38
ab9b3de
 
eace673
072db80
03e89dc
 
 
 
45ae1d9
eace673
b06a41a
03e89dc
 
 
 
 
 
 
 
 
ab9b3de
03e89dc
 
 
 
 
 
 
ab9b3de
03e89dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eace673
03e89dc
 
 
 
 
 
 
ab9b3de
9903c38
03e89dc
 
9903c38
 
03e89dc
 
2d34f28
ab9b3de
b06a41a
03e89dc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// 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}`));