Update server.js
Browse files
server.js
CHANGED
|
@@ -5,82 +5,55 @@ const fs = require("fs");
|
|
| 5 |
|
| 6 |
const app = express();
|
| 7 |
app.use(express.json());
|
| 8 |
-
|
| 9 |
const PORT = process.env.PORT || 7860;
|
| 10 |
|
| 11 |
-
/
|
| 12 |
-
* Health endpoints (HF-friendly)
|
| 13 |
-
*/
|
| 14 |
app.get("/", (_, res) => {
|
| 15 |
-
res.send("✅ Moltbot
|
| 16 |
-
});
|
| 17 |
-
|
| 18 |
-
app.get("/health", (_, res) => {
|
| 19 |
-
res.json({ ok: true });
|
| 20 |
});
|
|
|
|
| 21 |
|
| 22 |
-
/
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
const base = path.join(process.cwd(), "node_modules", "moltbot");
|
| 27 |
-
|
| 28 |
const candidates = [
|
|
|
|
| 29 |
"dist/index.js",
|
| 30 |
-
"
|
| 31 |
-
"src/index.js",
|
| 32 |
-
"index.js"
|
| 33 |
];
|
| 34 |
-
|
| 35 |
for (const file of candidates) {
|
| 36 |
const fullPath = path.join(base, file);
|
| 37 |
-
if (fs.existsSync(fullPath))
|
| 38 |
-
return fullPath;
|
| 39 |
-
}
|
| 40 |
}
|
| 41 |
-
|
| 42 |
throw new Error("Moltbot entry file not found");
|
| 43 |
}
|
| 44 |
|
| 45 |
-
/
|
| 46 |
-
* Run Moltbot
|
| 47 |
-
*/
|
| 48 |
app.post("/run", (req, res) => {
|
| 49 |
const args = Array.isArray(req.body?.args) ? req.body.args : [];
|
| 50 |
-
|
| 51 |
let entry;
|
| 52 |
try {
|
| 53 |
-
entry =
|
| 54 |
} catch (err) {
|
| 55 |
-
return res.status(500).json({
|
| 56 |
-
error: err.message
|
| 57 |
-
});
|
| 58 |
}
|
| 59 |
|
| 60 |
const child = spawn(
|
| 61 |
"node",
|
| 62 |
[entry, ...args],
|
| 63 |
-
{
|
| 64 |
-
env: process.env,
|
| 65 |
-
cwd: process.cwd()
|
| 66 |
-
}
|
| 67 |
);
|
| 68 |
|
| 69 |
-
let stdout = "";
|
| 70 |
-
let stderr = "";
|
| 71 |
-
|
| 72 |
child.stdout.on("data", d => stdout += d.toString());
|
| 73 |
child.stderr.on("data", d => stderr += d.toString());
|
| 74 |
-
|
| 75 |
child.on("close", code => {
|
| 76 |
-
res.json({
|
| 77 |
-
exitCode: code,
|
| 78 |
-
stdout,
|
| 79 |
-
stderr
|
| 80 |
-
});
|
| 81 |
});
|
| 82 |
});
|
| 83 |
|
| 84 |
-
app.listen(PORT, "0.0.0.0", () =>
|
| 85 |
-
|
| 86 |
-
|
|
|
|
| 5 |
|
| 6 |
const app = express();
|
| 7 |
app.use(express.json());
|
|
|
|
| 8 |
const PORT = process.env.PORT || 7860;
|
| 9 |
|
| 10 |
+
// Root health check
|
|
|
|
|
|
|
| 11 |
app.get("/", (_, res) => {
|
| 12 |
+
res.send("✅ Moltbot HF Space Running");
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
});
|
| 14 |
+
app.get("/health", (_, res) => res.json({ ok: true }));
|
| 15 |
|
| 16 |
+
// Try to find the Moltbot executable inside node_modules
|
| 17 |
+
function resolveMoltbot() {
|
| 18 |
+
// Moltbot repo uses a main entry Moltbot CLI file
|
| 19 |
+
// Check common compiled or source paths
|
| 20 |
const base = path.join(process.cwd(), "node_modules", "moltbot");
|
|
|
|
| 21 |
const candidates = [
|
| 22 |
+
"moltbot.mjs",
|
| 23 |
"dist/index.js",
|
| 24 |
+
"src/index.ts"
|
|
|
|
|
|
|
| 25 |
];
|
|
|
|
| 26 |
for (const file of candidates) {
|
| 27 |
const fullPath = path.join(base, file);
|
| 28 |
+
if (fs.existsSync(fullPath)) return fullPath;
|
|
|
|
|
|
|
| 29 |
}
|
|
|
|
| 30 |
throw new Error("Moltbot entry file not found");
|
| 31 |
}
|
| 32 |
|
| 33 |
+
// POST /run { "args": ["chat", "--message", "..."] }
|
|
|
|
|
|
|
| 34 |
app.post("/run", (req, res) => {
|
| 35 |
const args = Array.isArray(req.body?.args) ? req.body.args : [];
|
|
|
|
| 36 |
let entry;
|
| 37 |
try {
|
| 38 |
+
entry = resolveMoltbot();
|
| 39 |
} catch (err) {
|
| 40 |
+
return res.status(500).json({ error: err.message });
|
|
|
|
|
|
|
| 41 |
}
|
| 42 |
|
| 43 |
const child = spawn(
|
| 44 |
"node",
|
| 45 |
[entry, ...args],
|
| 46 |
+
{ cwd: process.cwd(), env: process.env }
|
|
|
|
|
|
|
|
|
|
| 47 |
);
|
| 48 |
|
| 49 |
+
let stdout = "", stderr = "";
|
|
|
|
|
|
|
| 50 |
child.stdout.on("data", d => stdout += d.toString());
|
| 51 |
child.stderr.on("data", d => stderr += d.toString());
|
|
|
|
| 52 |
child.on("close", code => {
|
| 53 |
+
res.json({ exitCode: code, stdout, stderr });
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
});
|
| 55 |
});
|
| 56 |
|
| 57 |
+
app.listen(PORT, "0.0.0.0", () =>
|
| 58 |
+
console.log(`🚀 Moltbot server listening on ${PORT}`)
|
| 59 |
+
);
|