From e870bd5496337c92bfdfd4a0c013b6d76146a6ae Mon Sep 17 00:00:00 2001 From: Agent Date: Sat, 18 Jul 2026 01:11:56 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=95=86=E6=B1=A4=E9=A2=9D=E5=BA=A6?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E5=8D=87=E7=BA=A7-=E6=AF=8F=E4=B8=AA?= =?UTF-8?q?=E6=A8=A1=E5=9E=8B=E8=BD=BB=E9=87=8F=E8=B0=83=E7=94=A8=E6=A3=80?= =?UTF-8?q?=E6=B5=8B=E5=AE=9E=E9=99=85=E9=85=8D=E9=A2=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/routes/api.js | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/backend/src/routes/api.js b/backend/src/routes/api.js index b5149bf..7937904 100644 --- a/backend/src/routes/api.js +++ b/backend/src/routes/api.js @@ -280,14 +280,39 @@ const router = (app) => { if (modelResp.status === 200) { const models = await modelResp.json(); - // 尝试轻量调用检查配额 - let quotaInfo = { available: true, models: [] }; - for (const m of ['sensenova-6.7-flash-lite', 'deepseek-v4-flash', 'sensenova-u1-fast']) { - if (models.data?.some((md) => md.id === m)) { - quotaInfo.models.push(m); + let quotaInfo = { available: true, models: [], quotas: [] }; + const modelIds = ['sensenova-6.7-flash-lite', 'sensenova-u1-fast', 'deepseek-v4-flash']; + for (const m of modelIds) { + if (!models.data?.some((md) => md.id === m)) continue; + quotaInfo.models.push(m); + + try { + const testResp = await fetch('https://token.sensenova.cn/v1/chat/completions', { + method: 'POST', + headers: { 'Authorization': `Bearer ${apiKey}`, 'Content-Type': 'application/json' }, + body: JSON.stringify({ model: m, messages: [{ role: 'user', content: 'hi' }], max_tokens: 1 }), + signal: AbortSignal.timeout(5000), + }); + + if (testResp.status === 200) { + quotaInfo.quotas.push({ model: m, status: '可用', remaining: '有配额' }); + } else if (testResp.status === 429) { + quotaInfo.quotas.push({ model: m, status: '配额已用完', remaining: '0' }); + } else { + const errBody = await testResp.json().catch(() => ({})); + const msg = errBody?.error?.message || `HTTP ${testResp.status}`; + if (msg.includes('quota') || msg.includes('limit') || msg.includes('rate')) { + quotaInfo.quotas.push({ model: m, status: '配额受限', remaining: msg }); + } else { + quotaInfo.quotas.push({ model: m, status: '正常', remaining: msg }); + } + } + } catch { + quotaInfo.quotas.push({ model: m, status: '查询超时', remaining: '-' }); } } - sensenovaCache = { data: { valid: true, models: quotaInfo.models, message: 'API Key 有效' }, time: Date.now() }; + + sensenovaCache = { data: { valid: true, models: quotaInfo.models, quotas: quotaInfo.quotas, message: 'API Key 有效' }, time: Date.now() }; } else if (modelResp.status === 401) { sensenovaCache = { data: { valid: false, message: 'API Key 无效或已过期' }, time: Date.now() }; } else {