clash-linux commited on
Commit
e0b237a
·
verified ·
1 Parent(s): 29cc386

Upload 14 files

Browse files
Files changed (1) hide show
  1. src/routes/chat.js +25 -3
src/routes/chat.js CHANGED
@@ -25,7 +25,7 @@ async function parseMessages(req, res, next) {
25
  let message = {
26
  role: (msg.role === "system" && provider === "anthropic") ? "user" : msg.role,
27
  tool_calls: [],
28
- template_format: "f-string"
29
  }
30
 
31
  if (Array.isArray(msg.content)) {
@@ -289,10 +289,18 @@ router.post('/v1/chat/completions', verify, parseMessages, async (req, res) => {
289
  ]
290
  }
291
 
 
 
292
  ws.on('open', async () => {
293
  ws.send(sendAction)
294
  RequestID = await sentRequest(req, res)
295
  setHeader()
 
 
 
 
 
 
296
  })
297
 
298
  ws.on('message', async (data) => {
@@ -405,6 +413,7 @@ router.post('/v1/chat/completions', verify, parseMessages, async (req, res) => {
405
  })
406
 
407
  ws.on('error', (err) => {
 
408
  // 标准OpenAI错误响应格式
409
  res.status(500).json({
410
  "error": {
@@ -416,7 +425,15 @@ router.post('/v1/chat/completions', verify, parseMessages, async (req, res) => {
416
  })
417
  })
418
 
419
- setTimeout(() => {
 
 
 
 
 
 
 
 
420
  if (ws.readyState === WebSocket.OPEN) {
421
  ws.close()
422
  if (!res.headersSent) {
@@ -431,7 +448,12 @@ router.post('/v1/chat/completions', verify, parseMessages, async (req, res) => {
431
  })
432
  }
433
  }
434
- }, 300 * 1000)
 
 
 
 
 
435
 
436
  } catch (error) {
437
  console.error("错误:", error)
 
25
  let message = {
26
  role: (msg.role === "system" && provider === "anthropic") ? "user" : msg.role,
27
  tool_calls: [],
28
+ template_format: "jinja2"
29
  }
30
 
31
  if (Array.isArray(msg.content)) {
 
289
  ]
290
  }
291
 
292
+ let pingInterval;
293
+
294
  ws.on('open', async () => {
295
  ws.send(sendAction)
296
  RequestID = await sentRequest(req, res)
297
  setHeader()
298
+ // Start sending pings every 30 seconds to keep the connection alive
299
+ pingInterval = setInterval(() => {
300
+ if (ws.readyState === WebSocket.OPEN) {
301
+ ws.ping(() => {}); // Empty callback, just to send the ping
302
+ }
303
+ }, 15000);
304
  })
305
 
306
  ws.on('message', async (data) => {
 
413
  })
414
 
415
  ws.on('error', (err) => {
416
+ clearInterval(pingInterval); // Stop sending pings
417
  // 标准OpenAI错误响应格式
418
  res.status(500).json({
419
  "error": {
 
425
  })
426
  })
427
 
428
+ const oSeriesModels = ["o4-mini", "o4-mini-high", "o3-mini", "o3-mini-high", "o1", "o3", "o3-2025-04-16", "o4-mini-2025-04-16"];
429
+ let timeoutDuration = 300 * 1000; // 默认5分钟
430
+
431
+ if (oSeriesModels.includes(req.body.model)) {
432
+ timeoutDuration = 1200 * 1000; // o系列模型20分钟
433
+ }
434
+
435
+ const requestTimeout = setTimeout(() => {
436
+ clearInterval(pingInterval); // Stop sending pings
437
  if (ws.readyState === WebSocket.OPEN) {
438
  ws.close()
439
  if (!res.headersSent) {
 
448
  })
449
  }
450
  }
451
+ }, timeoutDuration)
452
+
453
+ ws.on('close', () => {
454
+ clearInterval(pingInterval); // Stop sending pings
455
+ clearTimeout(requestTimeout); // Clear the main request timeout as well if ws closes first
456
+ });
457
 
458
  } catch (error) {
459
  console.error("错误:", error)