Upload 2 files
Browse files- src/index.js +32 -2
src/index.js
CHANGED
@@ -53,8 +53,8 @@ app.use(async (ctx, next) => {
|
|
53 |
app.use(async (ctx, next) => {
|
54 |
try {
|
55 |
await next()
|
56 |
-
//
|
57 |
-
if (ctx.status === 404) {
|
58 |
ctx.status = 404
|
59 |
ctx.body = {
|
60 |
error: 'Not Found',
|
@@ -176,12 +176,14 @@ const handleChatCompletions = async (ctx) => {
|
|
176 |
})
|
177 |
|
178 |
if (stream == "true" || stream == true) {
|
|
|
179 |
ctx.set({
|
180 |
'Content-Type': 'text/event-stream',
|
181 |
'Cache-Control': 'no-cache',
|
182 |
'Connection': 'keep-alive',
|
183 |
})
|
184 |
} else {
|
|
|
185 |
ctx.set({
|
186 |
'Content-Type': 'application/json',
|
187 |
})
|
@@ -225,6 +227,7 @@ const handleChatCompletions = async (ctx) => {
|
|
225 |
const { done, value } = await reader.read()
|
226 |
if (done) {
|
227 |
if (stream == "true" || stream == true) {
|
|
|
228 |
ctx.res.write('data: [DONE]\n\n')
|
229 |
}
|
230 |
break
|
@@ -232,6 +235,7 @@ const handleChatCompletions = async (ctx) => {
|
|
232 |
|
233 |
if (stream == "true" || stream == true) {
|
234 |
const text = new TextDecoder().decode(value)
|
|
|
235 |
const textContent = [...text.matchAll(/data:.*"}/g)]
|
236 |
|
237 |
textContent.forEach(item => {
|
@@ -289,12 +293,38 @@ const handleChatCompletions = async (ctx) => {
|
|
289 |
}
|
290 |
ctx.body = resBody
|
291 |
} else {
|
|
|
292 |
ctx.res.end()
|
293 |
}
|
294 |
} catch (error) {
|
295 |
console.error('流式响应出错:', error)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
296 |
ctx.status = 500
|
297 |
ctx.body = { error: '流式响应处理失败', details: error.toString() }
|
|
|
298 |
}
|
299 |
} catch (error) {
|
300 |
console.error('请求处理出错:', error)
|
|
|
53 |
app.use(async (ctx, next) => {
|
54 |
try {
|
55 |
await next()
|
56 |
+
// 只有当没有任何路由处理请求时才设置 404
|
57 |
+
if (ctx.status === 404 && !ctx.body) {
|
58 |
ctx.status = 404
|
59 |
ctx.body = {
|
60 |
error: 'Not Found',
|
|
|
176 |
})
|
177 |
|
178 |
if (stream == "true" || stream == true) {
|
179 |
+
ctx.status = 200 // 明确设置状态码
|
180 |
ctx.set({
|
181 |
'Content-Type': 'text/event-stream',
|
182 |
'Cache-Control': 'no-cache',
|
183 |
'Connection': 'keep-alive',
|
184 |
})
|
185 |
} else {
|
186 |
+
ctx.status = 200 // 明确设置状态码
|
187 |
ctx.set({
|
188 |
'Content-Type': 'application/json',
|
189 |
})
|
|
|
227 |
const { done, value } = await reader.read()
|
228 |
if (done) {
|
229 |
if (stream == "true" || stream == true) {
|
230 |
+
console.log('流式响应完成')
|
231 |
ctx.res.write('data: [DONE]\n\n')
|
232 |
}
|
233 |
break
|
|
|
235 |
|
236 |
if (stream == "true" || stream == true) {
|
237 |
const text = new TextDecoder().decode(value)
|
238 |
+
console.log('处理流式数据块:', text.length, '字节')
|
239 |
const textContent = [...text.matchAll(/data:.*"}/g)]
|
240 |
|
241 |
textContent.forEach(item => {
|
|
|
293 |
}
|
294 |
ctx.body = resBody
|
295 |
} else {
|
296 |
+
console.log('正常结束流式响应')
|
297 |
ctx.res.end()
|
298 |
}
|
299 |
} catch (error) {
|
300 |
console.error('流式响应出错:', error)
|
301 |
+
if (stream == "true" || stream == true) {
|
302 |
+
try {
|
303 |
+
console.log('发送错误信息到流')
|
304 |
+
ctx.res.write(`data: ${JSON.stringify({
|
305 |
+
"id": `chatcmpl-${messageId}`,
|
306 |
+
"choices": [
|
307 |
+
{
|
308 |
+
"index": 0,
|
309 |
+
"delta": {
|
310 |
+
"content": "\n\n抱歉,处理您的请求时出现错误。"
|
311 |
+
}
|
312 |
+
}
|
313 |
+
],
|
314 |
+
"created": Math.floor(Date.now() / 1000),
|
315 |
+
"model": model,
|
316 |
+
"object": "chat.completion.chunk"
|
317 |
+
})}\n\n`)
|
318 |
+
ctx.res.write('data: [DONE]\n\n')
|
319 |
+
} catch (e) {
|
320 |
+
console.error('发送错误信息失败:', e)
|
321 |
+
} finally {
|
322 |
+
ctx.res.end()
|
323 |
+
}
|
324 |
+
} else {
|
325 |
ctx.status = 500
|
326 |
ctx.body = { error: '流式响应处理失败', details: error.toString() }
|
327 |
+
}
|
328 |
}
|
329 |
} catch (error) {
|
330 |
console.error('请求处理出错:', error)
|