-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
80 lines (70 loc) · 3.09 KB
/
server.js
File metadata and controls
80 lines (70 loc) · 3.09 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
/**
* src/server.js — RENDER-OPTIMISED (stateless)
* ─────────────────────────────────────────────────
* Changes from the original:
* • POST /api/chat now expects { message, history } — the frontend
* owns and replays the conversation so nothing is stored server-side.
* • DELETE /api/chat is removed (nothing to clear).
* • GET /api/health exposes queue depth for easy monitoring.
*
* GITHUB PATH → src/server.js
*/
"use strict";
require("dotenv").config();
const express = require("express");
const cors = require("cors");
const path = require("path");
const { chat } = require("./geminiClient");
const app = express();
const PORT = process.env.PORT || 3000;
// ── Middleware ───────────────────────────────────────────────────────────────
app.use(cors());
app.use(express.json());
app.use(express.static(path.join(__dirname, "..", "public")));
// ── Health / queue-depth probe ───────────────────────────────────────────────
app.get("/api/health", (_req, res) => {
res.json({
status : "ok",
model : "gemini-1.5-flash",
approach: "long-context-stateless"
});
});
// ── Chat endpoint ────────────────────────────────────────────────────────────
/**
* POST /api/chat
*
* Body:
* {
* "message" : "How do I book?",
* "history" : [ ← can be []
* { "role": "user", "text": "Hi" },
* { "role": "assistant", "text": "Hello! …" }
* ]
* }
*
* Returns:
* { "answer": "…", "matchedSections": ["2. Customer FAQ"] }
*/
app.post("/api/chat", async (req, res) => {
const { message, history } = req.body;
if (!message) {
return res.status(400).json({ error: "Missing 'message' in request body." });
}
try {
const result = await chat(history || [], message);
res.json(result);
} catch (err) {
console.error("[/api/chat] Error:", err.message);
res.status(500).json({ error: "Something went wrong. Please try again." });
}
});
// ── Catch-all: SPA fallback ──────────────────────────────────────────────────
app.get("*", (_req, res) => {
res.sendFile(path.join(__dirname, "..", "public", "index.html"));
});
// ── Start ────────────────────────────────────────────────────────────────────
app.listen(PORT, () => {
console.log(`\n🚀 Skedulelt RAG — http://localhost:${PORT}`);
console.log(` Model : gemini-1.5-flash`);
console.log(` Approach : Long Context, stateless, queued\n`);
});