Upload 2 files
Browse files- src/index.js +72 -10
src/index.js
CHANGED
@@ -137,15 +137,70 @@ const makeRequest = async (session_id, requestModel, messages) => {
|
|
137 |
}
|
138 |
}
|
139 |
|
|
|
|
|
|
|
140 |
const parseStreamData = (data) => {
|
141 |
try {
|
142 |
if (!data) return null
|
143 |
-
|
144 |
-
|
145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
146 |
} catch (error) {
|
147 |
-
console.error('解析数据出错:', error, '原始数据:', data)
|
148 |
-
return
|
|
|
|
|
|
|
|
|
149 |
}
|
150 |
}
|
151 |
|
@@ -235,27 +290,34 @@ const handleChatCompletions = async (ctx) => {
|
|
235 |
if (!content) continue
|
236 |
|
237 |
if (stream == "true" || stream == true) {
|
238 |
-
|
|
|
|
|
|
|
|
|
239 |
try {
|
240 |
-
|
241 |
"id": `chatcmpl-${messageId}`,
|
242 |
"choices": [
|
243 |
{
|
244 |
"index": 0,
|
245 |
"delta": {
|
246 |
-
"content":
|
247 |
}
|
248 |
}
|
249 |
],
|
250 |
"created": Math.floor(Date.now() / 1000),
|
251 |
"model": model,
|
252 |
"object": "chat.completion.chunk"
|
253 |
-
}
|
|
|
|
|
254 |
} catch (e) {
|
255 |
-
console.error('写入流数据失败:', e)
|
256 |
}
|
257 |
}
|
258 |
} else {
|
|
|
259 |
if (content.field_value &&
|
260 |
content.field_name !== 'session_state.answer_is_finished' &&
|
261 |
content.field_name !== 'content' &&
|
|
|
137 |
}
|
138 |
}
|
139 |
|
140 |
+
// 用于存储未完成的数据块
|
141 |
+
let dataBuffer = '';
|
142 |
+
|
143 |
const parseStreamData = (data) => {
|
144 |
try {
|
145 |
if (!data) return null
|
146 |
+
|
147 |
+
// 将新数据添加到缓冲区
|
148 |
+
dataBuffer += data;
|
149 |
+
|
150 |
+
// 查找完整的JSON对象
|
151 |
+
const matches = dataBuffer.match(/data: ({[^}]+})/g);
|
152 |
+
if (!matches) return null;
|
153 |
+
|
154 |
+
// 处理找到的每个完整JSON对象
|
155 |
+
for (const match of matches) {
|
156 |
+
try {
|
157 |
+
const cleanData = match.replace("data: ", '').trim();
|
158 |
+
if (cleanData === '[DONE]') return null;
|
159 |
+
|
160 |
+
// 尝试解析JSON
|
161 |
+
const parsed = JSON.parse(cleanData);
|
162 |
+
|
163 |
+
// 从缓冲区移除已处理的数据
|
164 |
+
dataBuffer = dataBuffer.slice(dataBuffer.indexOf(match) + match.length);
|
165 |
+
|
166 |
+
// 返回解析后的对象
|
167 |
+
return parsed;
|
168 |
+
} catch (parseError) {
|
169 |
+
// 如果解析失败,尝试提取字段
|
170 |
+
const messageIdMatch = cleanData.match(/"message_id":\s*"([^"]+)"/);
|
171 |
+
const fieldNameMatch = cleanData.match(/"field_name":\s*"([^"]+)"/);
|
172 |
+
const fieldValueMatch = cleanData.match(/"field_value":\s*"([^]*?)(?:(?<!\\)",|$)/);
|
173 |
+
|
174 |
+
if (messageIdMatch && fieldNameMatch) {
|
175 |
+
const fieldValue = fieldValueMatch ? fieldValueMatch[1] : '';
|
176 |
+
return {
|
177 |
+
message_id: messageIdMatch[1],
|
178 |
+
field_name: fieldNameMatch[1],
|
179 |
+
field_value: fieldValue,
|
180 |
+
type: 'text'
|
181 |
+
};
|
182 |
+
}
|
183 |
+
}
|
184 |
+
}
|
185 |
+
|
186 |
+
// 如果缓冲区过大,清理它
|
187 |
+
if (dataBuffer.length > 10000) {
|
188 |
+
console.warn('缓冲区过大,正在清理...');
|
189 |
+
dataBuffer = dataBuffer.slice(-5000);
|
190 |
+
}
|
191 |
+
|
192 |
+
return {
|
193 |
+
field_name: 'content',
|
194 |
+
field_value: '',
|
195 |
+
type: 'text'
|
196 |
+
};
|
197 |
} catch (error) {
|
198 |
+
console.error('解析数据出错:', error, '原始数据:', data);
|
199 |
+
return {
|
200 |
+
field_name: 'content',
|
201 |
+
field_value: '',
|
202 |
+
type: 'text'
|
203 |
+
};
|
204 |
}
|
205 |
}
|
206 |
|
|
|
290 |
if (!content) continue
|
291 |
|
292 |
if (stream == "true" || stream == true) {
|
293 |
+
// 处理流式响应
|
294 |
+
const delta = content.delta ||
|
295 |
+
(content.field_name === 'content' ? content.field_value : null)
|
296 |
+
|
297 |
+
if (delta) {
|
298 |
try {
|
299 |
+
const chunk = {
|
300 |
"id": `chatcmpl-${messageId}`,
|
301 |
"choices": [
|
302 |
{
|
303 |
"index": 0,
|
304 |
"delta": {
|
305 |
+
"content": delta
|
306 |
}
|
307 |
}
|
308 |
],
|
309 |
"created": Math.floor(Date.now() / 1000),
|
310 |
"model": model,
|
311 |
"object": "chat.completion.chunk"
|
312 |
+
};
|
313 |
+
|
314 |
+
ctx.res.write(`data: ${JSON.stringify(chunk)}\n\n`)
|
315 |
} catch (e) {
|
316 |
+
console.error('写入流数据失败:', e, '数据:', delta)
|
317 |
}
|
318 |
}
|
319 |
} else {
|
320 |
+
// 处理非流式响应
|
321 |
if (content.field_value &&
|
322 |
content.field_name !== 'session_state.answer_is_finished' &&
|
323 |
content.field_name !== 'content' &&
|