plugin_server / app.js
everydaycats's picture
Update app.js
9b3839b verified
// server.js
import express from "express";
import bodyParser from "body-parser";
import cors from "cors";
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" }));
// === TEST MODES ===
// 'BUILD_PART' -> Makes a part
// 'FETCH_SCRIPT' -> Finds a script by name anywhere
// 'SCAN_SERVER_STORAGE' -> Scans "ServerStorage" explicitly
// 'SCAN_SELECTION' -> Scans whatever you clicked on in Roblox
// 'FETCH_LOGS' -> Fetches the recent Output logs
const DEMO_MODE = 'FETCH_LOGS';
app.post("/api/ai-build", async (req, res) => {
try {
const { instruction, selection, scriptContext, logContext, hierarchyContext } = req.body;
console.log("------------------------------------------------");
console.log("πŸ“₯ INSTRUCTION:", instruction);
// 1. Script Found Response
if (scriptContext) {
console.log(`πŸ“˜ SCRIPT FOUND [${scriptContext.targetName}]:`);
console.log(` Parent: ${scriptContext.parentName}`);
console.log(scriptContext.scriptSource.substring(0, 150) + "...\n");
return res.json({ success: true, message: `Read ${scriptContext.targetName} successfully.` });
}
// 2. Hierarchy Scanned Response
if (hierarchyContext) {
console.log(`🌳 SCANNED HIERARCHY [${hierarchyContext.rootName}]:`);
const treePreview = hierarchyContext.tree.split('\n').slice(0, 20).join('\n');
console.log(treePreview);
return res.json({ success: true, message: `Scanned ${hierarchyContext.rootName}.` });
}
// 3. Logs Received Response (RESTORED)
if (logContext) {
console.log("πŸ“ LOGS RECEIVED:");
console.log("------------------------------------------------");
// Print last 500 chars to avoid flooding terminal
const logPreview = logContext.logs.length > 500
? "..." + logContext.logs.substring(logContext.logs.length - 500)
: logContext.logs;
console.log(logPreview);
console.log("------------------------------------------------");
return res.json({ success: true, message: "Logs received and analyzed." });
}
// 4. Demo Triggers
if (DEMO_MODE === 'BUILD_PART') {
return res.send(`\`\`\`lua
local p = Instance.new("Part", workspace)
p.Position = Vector3.new(0,20,0)
p.Anchored = true
print("Created Part")
\`\`\``);
}
else if (DEMO_MODE === 'FETCH_SCRIPT') {
console.log(" πŸ‘‰ Asking to find 'BikeLogic'...");
return res.json({
action: "read_script",
targetName: "BikeLogic"
});
}
else if (DEMO_MODE === 'SCAN_SERVER_STORAGE') {
console.log(" πŸ‘‰ Asking to scan ServerStorage...");
return res.json({
action: "read_hierarchy",
targetName: "ServerStorage"
});
}
else if (DEMO_MODE === 'SCAN_SELECTION') {
console.log(" πŸ‘‰ Asking to scan selection...");
return res.json({ action: "read_hierarchy" });
}
else if (DEMO_MODE === 'FETCH_LOGS') {
console.log(" πŸ‘‰ Asking to fetch Console Logs...");
return res.json({ action: "read_logs" });
}
return res.json({ success: true, message: "Thinking..." });
} catch (err) {
console.error("Server Error:", err);
res.status(500).send("Server Error: " + err.message);
}
});
app.listen(PORT, () => console.log(`πŸš€ Server running on port ${PORT}. Mode: ${DEMO_MODE}`));