-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver-dev.js
More file actions
84 lines (70 loc) · 2.45 KB
/
Copy pathserver-dev.js
File metadata and controls
84 lines (70 loc) · 2.45 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
// Simple dev server for local development when not using Vercel CLI
// Run with: node server-dev.js
// Then use: pnpm run dev (in another terminal)
import express from "express";
import cors from "cors";
import { GoogleGenerativeAI } from "@google/generative-ai";
import dotenv from "dotenv";
import { fileURLToPath } from "url";
import { dirname, join } from "path";
dotenv.config({ path: ".env.local" });
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const app = express();
const PORT = 3001;
app.use(cors());
app.use(express.json());
app.post("/api/chat", async (req, res) => {
try {
const { message, personaId, prompt } = req.body;
if (!message || !personaId || !prompt) {
return res.status(400).json({ error: "Missing required fields" });
}
const apiKey = process.env.GEMINI_API_KEY;
if (!apiKey) {
return res.status(500).json({ error: "GEMINI_API_KEY not configured" });
}
// Initialize Gemini
const genAI = new GoogleGenerativeAI(apiKey);
const model = genAI.getGenerativeModel({
model: "gemini-1.5-flash",
systemInstruction: prompt,
});
// Set up SSE headers
res.setHeader("Content-Type", "text/event-stream");
res.setHeader("Cache-Control", "no-cache");
res.setHeader("Connection", "keep-alive");
// Generate content stream
const result = await model.generateContentStream(message);
// Stream responses
for await (const chunk of result.stream) {
const chunkText = chunk.text();
if (chunkText) {
const data = JSON.stringify({ text: chunkText, done: false });
res.write(`data: ${data}\n\n`);
}
}
// Send completion signal
const done = JSON.stringify({ text: "", done: true });
res.write(`data: ${done}\n\n`);
res.end();
} catch (error) {
console.error("Error in chat API:", error);
if (!res.headersSent) {
return res.status(500).json({ error: error.message || "Internal server error" });
} else {
const errorData = JSON.stringify({ error: error.message, done: true });
res.write(`data: ${errorData}\n\n`);
res.end();
}
}
});
app.listen(PORT, () => {
console.log(`🚀 Dev server running on http://localhost:${PORT}`);
console.log(`✅ Proxy this from Vite dev server`);
if (!process.env.GEMINI_API_KEY) {
console.log(`❌ GEMINI_API_KEY not found in .env.local`);
} else {
console.log(`✅ GEMINI_API_KEY configured`);
}
});