-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.js
More file actions
353 lines (291 loc) · 9.67 KB
/
Copy pathproxy.js
File metadata and controls
353 lines (291 loc) · 9.67 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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
const express = require('express');
const { Readable } = require('node:stream');
const { pipeline } = require('node:stream/promises');
const { fetch: defaultFetch } = require('undici');
const DEFAULT_TARGET_URL = 'https://cn2.router.team/v1/chat/completions';
const DEFAULT_TIMEOUT_MS = 90_000;
const DEFAULT_BODY_LIMIT = '10mb';
const REQUEST_HEADERS = new Set([
'accept',
'authorization',
'content-type',
'openai-organization',
'openai-project',
]);
// Undici transparently decodes compressed bodies, and transformed SSE bodies have
// a different length. Forwarding either original header corrupts the response.
const OMITTED_RESPONSE_HEADERS = new Set([
'connection',
'content-encoding',
'content-length',
'keep-alive',
'proxy-authenticate',
'proxy-authorization',
'te',
'trailer',
'transfer-encoding',
'upgrade',
]);
function getPositiveNumber(value, fallback) {
const parsed = Number(value);
return Number.isFinite(parsed) && parsed > 0 ? parsed : fallback;
}
function getRequestHeaders(headers) {
const result = {};
for (const [name, value] of Object.entries(headers)) {
if (REQUEST_HEADERS.has(name.toLowerCase()) && value !== undefined) {
result[name] = value;
}
}
return result;
}
function copyResponseHeaders(headers, res) {
for (const [name, value] of headers) {
if (!OMITTED_RESPONSE_HEADERS.has(name.toLowerCase())) {
res.setHeader(name, value);
}
}
}
function patchCompletionPayload(data) {
if (!data || typeof data !== 'object' || Array.isArray(data)) return data;
// MiMo treats the presence of `error` as a failure and reads `error.message`.
// Remove empty sentinels from successful chunks, but preserve real errors.
if (data.error == null) delete data.error;
if (!Array.isArray(data.choices)) return data;
for (const choice of data.choices) {
const toolCalls = choice?.delta?.tool_calls;
if (!Array.isArray(toolCalls)) continue;
for (const toolCall of toolCalls) {
if (
toolCall
&& typeof toolCall === 'object'
&& toolCall.function === undefined
) {
toolCall.function = {};
}
}
}
return data;
}
function transformSseEvent(rawEvent) {
const lines = rawEvent.split(/\r\n|\r|\n/);
const dataLineIndexes = [];
const dataParts = [];
for (let index = 0; index < lines.length; index += 1) {
const line = lines[index];
const colonIndex = line.indexOf(':');
const field = colonIndex === -1 ? line : line.slice(0, colonIndex);
if (field !== 'data') continue;
let value = colonIndex === -1 ? '' : line.slice(colonIndex + 1);
if (value.startsWith(' ')) value = value.slice(1);
dataLineIndexes.push(index);
dataParts.push(value);
}
if (dataLineIndexes.length === 0) return `${lines.join('\n')}\n\n`;
const payload = dataParts.join('\n').trim();
if (payload === '[DONE]') return `${lines.join('\n')}\n\n`;
let replacement;
try {
replacement = `data: ${JSON.stringify(patchCompletionPayload(JSON.parse(payload)))}`;
} catch {
return `${lines.join('\n')}\n\n`;
}
const dataIndexes = new Set(dataLineIndexes);
const transformedLines = [];
let dataWritten = false;
for (let index = 0; index < lines.length; index += 1) {
if (!dataIndexes.has(index)) {
transformedLines.push(lines[index]);
} else if (!dataWritten) {
transformedLines.push(replacement);
dataWritten = true;
}
}
return `${transformedLines.join('\n')}\n\n`;
}
function takeNextSseEvent(buffer) {
function lineEndingLengthAt(index) {
if (buffer[index] === '\n') return 1;
if (buffer[index] !== '\r') return 0;
return buffer[index + 1] === '\n' ? 2 : 1;
}
for (let index = 0; index < buffer.length; index += 1) {
const firstLength = lineEndingLengthAt(index);
if (firstLength === 0) continue;
const secondLength = lineEndingLengthAt(index + firstLength);
if (secondLength === 0) {
index += firstLength - 1;
continue;
}
return {
event: buffer.slice(0, index),
rest: buffer.slice(index + firstLength + secondLength),
};
}
return null;
}
async function writeWithBackpressure(res, value) {
if (res.destroyed || res.writableEnded) throw new Error('Client disconnected');
if (res.write(value)) return;
await new Promise((resolve, reject) => {
const cleanup = () => {
res.off('close', onClose);
res.off('drain', onDrain);
res.off('error', onError);
};
const onClose = () => {
cleanup();
reject(new Error('Client disconnected'));
};
const onDrain = () => {
cleanup();
resolve();
};
const onError = error => {
cleanup();
reject(error);
};
res.once('close', onClose);
res.once('drain', onDrain);
res.once('error', onError);
});
}
async function pipeSseBody(body, res) {
const reader = body.getReader();
const decoder = new TextDecoder('utf-8');
let buffer = '';
while (true) {
const { done, value } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
while (true) {
const next = takeNextSseEvent(buffer);
if (!next) break;
buffer = next.rest;
await writeWithBackpressure(res, transformSseEvent(next.event));
}
}
buffer += decoder.decode();
while (true) {
const next = takeNextSseEvent(buffer);
if (!next) break;
buffer = next.rest;
await writeWithBackpressure(res, transformSseEvent(next.event));
}
// Preserve a final upstream event even if the connection closes without a blank line.
if (buffer.length > 0) {
await writeWithBackpressure(res, transformSseEvent(buffer));
}
res.end();
}
function createApp(options = {}) {
const app = express();
const fetchImpl = options.fetch ?? defaultFetch;
const logger = options.logger ?? console;
const chatTargetUrl = options.targetUrl ?? process.env.UPSTREAM_URL ?? DEFAULT_TARGET_URL;
const responsesTargetUrl = options.responsesTargetUrl
?? process.env.RESPONSES_UPSTREAM_URL
?? new URL('/v1/responses', chatTargetUrl).toString();
const timeoutMs = getPositiveNumber(
options.timeoutMs ?? process.env.UPSTREAM_TIMEOUT_MS,
DEFAULT_TIMEOUT_MS,
);
const bodyLimit = options.bodyLimit ?? process.env.REQUEST_BODY_LIMIT ?? DEFAULT_BODY_LIMIT;
// Validate configuration during startup instead of failing every request later.
new URL(chatTargetUrl);
new URL(responsesTargetUrl);
const parseBody = express.raw({ type: () => true, limit: bodyLimit });
const createProxyHandler = (upstreamUrl, { transformCompletionSse = false } = {}) => async (req, res) => {
const controller = new AbortController();
let timedOut = false;
const timeout = setTimeout(() => {
timedOut = true;
controller.abort(new Error(`Upstream timed out after ${timeoutMs}ms`));
}, timeoutMs);
timeout.unref?.();
const clearUpstreamTimeout = () => clearTimeout(timeout);
const abortUpstream = () => controller.abort(new Error('Client disconnected'));
const abortIfIncomplete = () => {
if (!res.writableEnded) abortUpstream();
};
req.once('aborted', abortUpstream);
res.once('close', abortIfIncomplete);
try {
const upstreamRes = await fetchImpl(upstreamUrl, {
method: 'POST',
headers: getRequestHeaders(req.headers),
body: req.body?.length ? req.body : undefined,
signal: controller.signal,
});
// The timeout limits how long the upstream may take to start responding.
// Streaming responses can legitimately remain open much longer.
clearUpstreamTimeout();
res.status(upstreamRes.status);
copyResponseHeaders(upstreamRes.headers, res);
if (!upstreamRes.body) {
res.end();
return;
}
const contentType = upstreamRes.headers.get('content-type')?.toLowerCase() ?? '';
const isEventStream = contentType.includes('text/event-stream');
if (isEventStream) res.flushHeaders();
if (isEventStream && transformCompletionSse) {
await pipeSseBody(upstreamRes.body, res);
} else {
await pipeline(Readable.fromWeb(upstreamRes.body), res);
}
} catch (error) {
const clientDisconnected = controller.signal.aborted && !timedOut;
if (!clientDisconnected) {
logger.error('Proxy request failed:', error.message);
}
if (!res.headersSent) {
res.status(timedOut ? 504 : 502).json({
error: {
message: timedOut ? 'Upstream request timed out' : 'Upstream request failed',
type: 'proxy_error',
},
});
} else if (!res.writableEnded) {
res.destroy();
}
} finally {
clearUpstreamTimeout();
req.off('aborted', abortUpstream);
res.off('close', abortIfIncomplete);
}
};
app.post(
['/v1/chat/completions', '/chat/completions'],
parseBody,
createProxyHandler(chatTargetUrl, { transformCompletionSse: true }),
);
app.post(
['/v1/responses', '/responses'],
parseBody,
createProxyHandler(responsesTargetUrl),
);
app.all(
['/v1/chat/completions', '/chat/completions', '/v1/responses', '/responses'],
(_req, res) => {
res.setHeader('allow', 'POST');
res.status(405).send('Method Not Allowed: Only POST is supported');
},
);
return app;
}
function startServer(options = {}) {
const app = createApp(options);
const port = getPositiveNumber(options.port ?? process.env.PORT, 3000);
const host = options.host ?? process.env.HOST ?? '0.0.0.0';
return app.listen(port, host, () => {
console.log(`Proxy listening on ${host}:${port}`);
});
}
if (require.main === module) startServer();
module.exports = {
createApp,
patchCompletionPayload,
startServer,
transformSseEvent,
};