-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
72 lines (62 loc) · 2 KB
/
server.js
File metadata and controls
72 lines (62 loc) · 2 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
import express from 'express';
import cors from 'cors';
import { handler as aiChatHandler } from './api/ai-chat.js';
import { handler as groqHandler } from './api/groq.js';
const app = express();
const PORT = process.env.PORT || 3000;
app.use(cors());
app.use(express.json());
// Handle the new AI chat endpoint with fallback support
app.post('/api/ai-chat', async (req, res) => {
try {
// Create a mock Vercel request/response object for the handler
const mockReq = {
method: 'POST',
body: req.body,
headers: req.headers
};
const mockRes = {
status: (code) => ({
json: (data) => res.status(code).json(data),
end: () => res.status(code).end()
}),
json: (data) => res.json(data),
setHeader: (name, value) => res.setHeader(name, value)
};
await aiChatHandler(mockReq, mockRes);
} catch (error) {
console.error('AI Chat API Error:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
// Handle the legacy groq API endpoint
app.post('/api/groq', async (req, res) => {
try {
// Create a mock Vercel request/response object for the handler
const mockReq = {
method: 'POST',
body: req.body,
headers: req.headers
};
const mockRes = {
status: (code) => ({
json: (data) => res.status(code).json(data),
end: () => res.status(code).end()
}),
json: (data) => res.json(data),
setHeader: (name, value) => res.setHeader(name, value)
};
await groqHandler(mockReq, mockRes);
} catch (error) {
console.error('Groq API Error:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
app.get('/', (req, res) => {
res.send('TaskTrail Backend server is running with AI fallback support.');
});
app.listen(PORT, () => {
console.log(`Backend server listening on port ${PORT}`);
console.log('AI Chat endpoint: /api/ai-chat (with Groq + DeepSeek fallback)');
console.log('Legacy Groq endpoint: /api/groq');
});