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 {