-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
136 lines (113 loc) · 3.58 KB
/
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
import express from 'express';
import axios from 'axios';
import bodyParser from 'body-parser';
import cors from 'cors';
const app = express();
const port = 3000;
app.use(cors());
app.use(bodyParser.json());
app.post('/proxy/*', async (req, res) => {
const {
max_thinking_chars,
...bodyParams
} = req.body;
const protocol = req.protocol;
const targetUrl = protocol + '://' + req.path.split('/proxy/')[1];
const headers = {
...req.headers,
'host': targetUrl.split('/')[2]
};
// Remove unnecessary headers
delete headers['content-length'];
delete headers['connection'];
let isThinking = false;
let charCount = 0;
let currentChunk = '';
const controller = new AbortController();
try {
const response = await axios({
method: 'post',
url: targetUrl,
headers: headers,
data: bodyParams,
responseType: 'stream',
signal: controller.signal
});
response.data.on('data', async chunk => {
const chunkStr = chunk.toString();
currentChunk += chunkStr;
// Check for complete data chunks
while (currentChunk.includes('\n')) {
const parts = currentChunk.split('\n');
const completeParts = parts.slice(0, -1);
currentChunk = parts[parts.length - 1];
for (const part of completeParts) {
if (part.trim() === '') continue;
if (!part.startsWith('data: ')) continue;
const data = part.slice(6);
if (data === '[DONE]') {
res.write('data: [DONE]\n\n');
return;
}
try {
const parsed = JSON.parse(data);
const content = parsed.choices[0]?.delta?.content || '';
if (content.includes('<think>')) {
isThinking = true;
charCount = 0;
}
if (isThinking) {
charCount += content.length;
if (charCount >= (parseInt(max_thinking_chars) || Infinity)) {
controller.abort();
res.write(`data: ${JSON.stringify({
...parsed,
choices: [{
...parsed.choices[0],
delta: { content: '\n</think>\n\n' }
}]
})}\n\n`);
// Make a new request with prefilled message
const newBodyParams = {
...bodyParams,
messages: [
...bodyParams.messages,
{
role: 'assistant',
content: parsed.choices[0].delta.content + '\n</think>\n\n'
}
]
};
const newResponse = await axios({
method: 'post',
url: targetUrl,
headers: headers,
data: newBodyParams,
responseType: 'stream'
});
newResponse.data.pipe(res);
return;
}
}
if (content.includes('</think>')) {
isThinking = false;
charCount = 0;
}
res.write(`data: ${JSON.stringify(parsed)}\n\n`);
} catch (e) {
console.error('Error parsing JSON:', e);
}
}
}
});
response.data.on('end', () => {
res.end();
});
} catch (error) {
console.error('Error:', error);
res.status(500).json({ error: 'Proxy request failed' });
}
});
app.listen(port, () => {
console.log(`Proxy server running on port ${port}`);
});