From ea5a208487e95949789c189b03ce74b7ded241f7 Mon Sep 17 00:00:00 2001 From: mike Date: Thu, 15 Oct 2026 11:27:09 +0800 Subject: [PATCH] Update server.js via upload script - 2026-10-15 11:27:07 --- server.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/server.js b/server.js index e860ddc..6d18d33 100644 --- a/server.js +++ b/server.js @@ -271,16 +271,26 @@ async function callWithRetries(provider, messages, opts, backoffs, emptyRetries) // 主力 DeepSeek,失败(繁忙/网络问题)时自动切到备用的阿里云。 // 名字保留 callDeepSeekWithRetry,所有调用方(目录导入、词汇翻译、网站翻译)都不用改。 +// 熔断:DeepSeek 刚判定过繁忙,接下来 10 分钟内的请求直接走阿里云,不再每次先白等 DeepSeek +const DEEPSEEK_COOLDOWN_MS = 10 * 60 * 1000; +let deepseekBusyUntil = 0; async function callDeepSeekWithRetry(apiKey, messages, opts, retries = 1) { const { backup } = loadAiConfig(); const hasBackup = !!backup.apiKey; if (!apiKey && hasBackup) return callWithRetries(aliyunProvider(backup), messages, opts, [3000, 8000], retries); + if (hasBackup && Date.now() < deepseekBusyUntil) { + console.log(`[AI] DeepSeek 最近繁忙,直接使用阿里云 ${backup.model}(${Math.round((deepseekBusyUntil - Date.now()) / 60000)} 分钟后再试 DeepSeek)`); + return callWithRetries(aliyunProvider(backup), messages, opts, [3000, 8000], retries); + } + // DeepSeek 正常时几秒内就会开始输出;有备用时只给它 20 秒,没备用时才耐心等 60 秒 + const primaryOpts = { firstTokenMs: hasBackup ? 20000 : 60000, ...(opts || {}) }; const primary = { name: 'DeepSeek', base: DEEPSEEK_API_BASE, apiKey, model: (opts && opts.model) || 'deepseek-v4-flash', extraBody: { thinking: { type: 'disabled' } } }; try { // 有备用时只重试一次就切换,不让用户干等;没备用时多重试几次 - return await callWithRetries(primary, messages, opts, hasBackup ? [3000] : [3000, 8000, 15000], retries); + return await callWithRetries(primary, messages, primaryOpts, hasBackup ? [3000] : [3000, 8000, 15000], retries); } catch (e) { if (!(e.busyExhausted || (hasBackup && /empty response/i.test(e.message)))) throw e; + if (e.busyExhausted) deepseekBusyUntil = Date.now() + DEEPSEEK_COOLDOWN_MS; if (!hasBackup) { const fe = new Error(e.slow ? `${e.message}。请过几分钟再试;也可以在"设置"里配置备用的阿里云。` : `DeepSeek 服务器繁忙(${e.status || '网络错误'}),已自动重试 ${e.busyTries} 次仍失败。这是 DeepSeek 那边高峰期过载,请过几分钟再试;也可以在"设置"里配置备用的阿里云。`); fe.status = e.status;