-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.ts
More file actions
57 lines (47 loc) · 1.41 KB
/
server.ts
File metadata and controls
57 lines (47 loc) · 1.41 KB
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
import express from "express";
import { setupAgent, setupProcessHandlers } from "./agent";
import dotenv from "dotenv";
dotenv.config();
async function startServer() {
const app = express();
app.use(express.json());
let lastInitTime = Date.now();
const TIMEOUT = 5 * 60 * 1000; // 5 minutes
let agentInterface = await setupAgent();
setupProcessHandlers(agentInterface);
await agentInterface.start();
app.get("/heartbeat", async (req, res) => {
const currentTime = Date.now();
if (currentTime - lastInitTime > TIMEOUT) {
agentInterface = await setupAgent();
await agentInterface.start();
lastInitTime = currentTime;
}
res.json({ status: "ok" });
});
// Single message endpoint for all interactions
app.post("/message", async (req, res) => {
try {
const { message } = req.body;
if (!message) {
return res.status(400).json({
error: "Message is required",
});
}
const response = await agentInterface.processMessage(message);
res.json({
text: response,
});
} catch (error: any) {
console.error("Error processing message:", error);
res.status(500).json({
text: error.message || "Internal server error",
});
}
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
}
startServer().catch(console.error);