-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Expand file tree
/
Copy pathserver.js
More file actions
111 lines (94 loc) · 2.69 KB
/
server.js
File metadata and controls
111 lines (94 loc) · 2.69 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { randomBytes } from "node:crypto";
import cors from "cors";
import express from "express";
const app = express();
// Environment configuration
const PORT = process.env.PORT || 3001;
// Middleware
app.use(cors());
app.use(express.json());
// ==================== In-memory DB ====================
// sessionId -> { createdAt, updatedAt, userId, messages: [{ role, content, timestamp }] }
const sessions = new Map();
function nowMs() {
return Date.now();
}
function createSessionId() {
return `session-${nowMs()}-${randomBytes(6).toString("hex")}`;
}
function getOrCreateSession(sessionId, userId) {
if (sessionId && sessions.has(sessionId)) {
return sessions.get(sessionId);
}
const id = sessionId || createSessionId();
const session = {
id,
userId: userId || "demo-user",
createdAt: nowMs(),
updatedAt: nowMs(),
messages: [],
};
sessions.set(id, session);
return session;
}
function buildReply(message, session) {
const trimmed = String(message || "").trim();
if (!trimmed) {
return {
message: "I didn't catch that — can you rephrase?",
confidence: 0.6,
suggestions: [],
};
}
const lower = trimmed.toLowerCase();
if (lower.includes("help")) {
return {
message:
"Try: “Summarize my last message”, “Give me 3 next steps”, or “Ask me clarifying questions.”",
confidence: 0.9,
suggestions: [
"Summarize my last message",
"Give me 3 next steps",
"Ask me clarifying questions",
],
};
}
return {
message: `You said: "${trimmed}". (Session messages: ${session.messages.length})`,
confidence: 0.9,
suggestions: [],
};
}
// ==================== API ROUTES ====================
// Health check
app.get("/health", (_req, res) => {
res.json({ status: "ok", timestamp: new Date().toISOString() });
});
// Chat with Eliza (in-memory demo)
app.post("/api/chat/eliza", async (req, res) => {
try {
const { message, sessionId, userId } = req.body ?? {};
const session = getOrCreateSession(sessionId, userId);
session.messages.push({
role: "user",
content: String(message ?? ""),
timestamp: nowMs(),
});
session.updatedAt = nowMs();
const reply = buildReply(message, session);
session.messages.push({
role: "assistant",
content: reply.message,
timestamp: nowMs(),
});
session.updatedAt = nowMs();
res.json({ ...reply, sessionId: session.id });
} catch (error) {
console.error("Error chatting with Eliza:", error);
res.status(500).json({ error: error.message });
}
});
// Start server
app.listen(PORT, () => {
console.log(`🤖 Eliza Classic Chat API running on port ${PORT}`);
});