-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy-server.js
235 lines (207 loc) · 6.66 KB
/
proxy-server.js
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import express from 'express';
import cors from 'cors';
import fetch from 'node-fetch';
import dotenv from 'dotenv';
// Load environment variables
dotenv.config();
const app = express();
const GLHF_URL = process.env.VITE_API_URL_GLHF;
const LMSTUDIO_URL = process.env.VITE_API_URL_LMSTUDIO
const LOCAL_LM_URL = process.env.VITE_API_URL_LOCAL_LM
if (!GLHF_URL) {
console.error('Error: VITE_GLHF_CHAT_BASE_URL is not set in environment variables');
process.exit(1);
}
// Add body parsing middleware
app.use(express.json());
// Enable CORS for all routes
app.use(cors({
origin: '*',
methods: ['GET', 'POST', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization']
}));
// Handle GLHF chat completions endpoint
app.post('/glhf', async (req, res) => {
try {
console.log('[GLHF] Received request:', {
method: req.method,
headers: {
...req.headers,
authorization: req.headers.authorization ? '(present)' : '(missing)'
},
body: req.body
});
// Get the API key from the Authorization header
const authHeader = req.headers.authorization;
console.log('[GLHF] Auth header:', authHeader);
if (!authHeader || !authHeader.startsWith('Bearer ')) {
console.error('[GLHF] Missing or invalid Authorization header');
res.status(401).json({
error: 'Authorization Error',
message: 'Missing or invalid Authorization header'
});
return;
}
const requestBody = {
model: req.body.model,
messages: req.body.messages,
temperature: req.body.temperature || 0.1
};
// Log the curl command
console.log('\n[GLHF] Equivalent curl command:');
console.log(`curl --location '${GLHF_URL}/chat/completions' \\
--header 'Content-Type: application/json' \\
--header 'Authorization: ${authHeader}' \\
--data '${JSON.stringify(requestBody, null, 2)}'`);
// Create AbortController for timeout
const controller = new AbortController();
const timeout = setTimeout(() => {
controller.abort();
}, 30000); // 30 second timeout
try {
console.log('[GLHF] Sending request to:', `${GLHF_URL}`);
console.log('[GLHF] Request body:', JSON.stringify(requestBody, null, 2));
const response = await fetch(`${GLHF_URL}/chat/completions`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': authHeader,
'Accept': 'application/json'
},
body: JSON.stringify(requestBody),
signal: controller.signal
});
clearTimeout(timeout);
if (!response.ok) {
const errorText = await response.text();
console.error('[GLHF] API Error Details:', {
status: response.status,
statusText: response.statusText,
headers: Object.fromEntries(response.headers.entries()),
error: errorText,
requestBody: JSON.stringify(requestBody, null, 2)
});
res.status(response.status).json({
error: 'GLHF API Error',
message: errorText || response.statusText,
details: {
status: response.status,
statusText: response.statusText
}
});
return;
}
const data = await response.json();
console.log('[GLHF] Response:', JSON.stringify(data, null, 2));
res.json(data);
} catch (error) {
console.error('[GLHF] Request Error:', {
name: error.name,
message: error.message,
stack: error.stack,
requestBody: JSON.stringify(requestBody, null, 2)
});
res.status(500).json({
error: 'Proxy Error',
message: error.message,
details: {
name: error.name,
requestBody: requestBody
}
});
}
} catch (error) {
console.error('[GLHF] Error:', error);
res.status(500).json({
error: 'Proxy Error',
message: error.message
});
}
});
// Handle LMStudio chat completions endpoint
app.post('/lmstudio', async (req, res) => {
try {
console.log('[LMStudio] Received request:', {
method: req.method,
body: req.body
});
const authHeader = req.headers.authorization;
// Log the curl command
console.log('\n[LMStudio] Equivalent curl command:');
console.log(`curl --location '${LMSTUDIO_URL}/chat/completions' \\
--header 'Content-Type: application/json' \\
--header 'Authorization: ${authHeader}' \\
--data '${JSON.stringify(req.body, null, 2)}'`);
const response = await fetch(`${LMSTUDIO_URL}/chat/completions`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(req.body)
});
if (!response.ok) {
const errorText = await response.text();
console.error('[LMStudio] Error response:', {
status: response.status,
statusText: response.statusText,
body: errorText
});
res.status(response.status).send(errorText);
return;
}
const data = await response.json();
res.json(data);
} catch (error) {
console.error('[LMStudio] Error:', error);
res.status(500).json({
error: 'Internal Server Error',
message: error.message
});
}
});
// Handle LMStudio chat completions endpoint
app.post('/local-ai', async (req, res) => {
try {
console.log('[LOCAL_AI] Received request:', {
method: req.method,
body: req.body
});
const authHeader = req.headers.authorization;
// Log the curl command
console.log('\n[LOCAL_AI] Equivalent curl command:');
console.log(`curl --location '${LOCAL_LM_URL}/chat/completions' \\
--header 'Content-Type: application/json' \\
--header 'Authorization: ${authHeader}' \\
--data '${JSON.stringify(req.body, null, 2)}'`);
const response = await fetch(`${LOCAL_LM_URL}/chat/completions`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(req.body)
});
if (!response.ok) {
const errorText = await response.text();
console.error('[LOCAL_AI] Error response:', {
status: response.status,
statusText: response.statusText,
body: errorText
});
res.status(response.status).send(errorText);
return;
}
const data = await response.json();
res.json(data);
} catch (error) {
console.error('[LOCAL_AI] Error:', error);
res.status(500).json({
error: 'Internal Server Error',
message: error.message
});
}
});
const port = process.env.PORT || 3002;
app.listen(port, '0.0.0.0', () => {
console.log(`Proxy server running on http://localhost:${port}`);
console.log('GLHF endpoint:', `${GLHF_URL}/chat/completions`);
});