getzero11 commited on
Commit
16e0a07
·
verified ·
1 Parent(s): a0f823c

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +35 -29
server.js CHANGED
@@ -5,55 +5,61 @@ const fs = require("fs");
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
- );
 
5
 
6
  const app = express();
7
  app.use(express.json());
8
+
9
  const PORT = process.env.PORT || 7860;
10
 
 
11
  app.get("/", (_, res) => {
12
+ res.send("✅ Moltbot is running on Hugging Face Space");
13
+ });
14
+
15
+ app.get("/health", (_, res) => {
16
+ res.json({ status: "ok" });
17
  });
 
18
 
19
+ function findMoltbotEntry() {
 
 
 
20
  const base = path.join(process.cwd(), "node_modules", "moltbot");
21
+
22
  const candidates = [
 
23
  "dist/index.js",
24
+ "dist/cli.js",
25
+ "src/index.js",
26
+ "index.js"
27
  ];
28
+
29
+ for (const rel of candidates) {
30
+ const full = path.join(base, rel);
31
+ if (fs.existsSync(full)) return full;
32
  }
33
+
34
+ throw new Error("Could not locate Moltbot entry file");
35
  }
36
 
 
37
  app.post("/run", (req, res) => {
 
38
  let entry;
39
  try {
40
+ entry = findMoltbotEntry();
41
+ } catch (e) {
42
+ return res.status(500).json({ error: e.message });
43
  }
44
 
45
+ const args = Array.isArray(req.body?.args) ? req.body.args : [];
46
+
47
+ const child = spawn("node", [entry, ...args], {
48
+ env: process.env,
49
+ cwd: process.cwd()
50
+ });
51
+
52
+ let stdout = "";
53
+ let stderr = "";
54
+
55
+ child.stdout.on("data", d => (stdout += d.toString()));
56
+ child.stderr.on("data", d => (stderr += d.toString()));
57
 
 
 
 
58
  child.on("close", code => {
59
  res.json({ exitCode: code, stdout, stderr });
60
  });
61
  });
62
 
63
+ app.listen(PORT, "0.0.0.0", () => {
64
+ console.log(`🚀 Moltbot listening on port ${PORT}`);
65
+ });