Update server.js via upload script - 2026-10-15 11:27:07

This commit is contained in:
mike 2026-10-15 11:27:09 +08:00
parent e97a96530c
commit ea5a208487
1 changed files with 11 additions and 1 deletions

View File

@ -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;