diff --git a/app/server/config.js b/app/server/config.js
index 4388711..7686313 100644
--- a/app/server/config.js
+++ b/app/server/config.js
@@ -81,7 +81,7 @@ function normalizePlatforms(rawPlatforms) {
if (!p.url) throw new Error('platforms[' + pi + '] 缺少 url(OpenAI 兼容 /v1 地址)');
const url = String(p.url).replace(/\/+$/, '');
const name = String(p.name || '').trim() || (hostnameOf(url) || ('p' + (pi + 1)));
- // type 决定余额查询适配器(预留);openai/deepseek/custom 之外也允许,只是查不了余额。
+ // type 可完全自定义(不校验白名单);它决定余额查询适配器,由 routes/platforms.js 按类型分发(当前仅 deepseek 已接入,其余类型查询时返回不支持)。
const type = String(p.type || 'openai').trim();
const keys = [];
for (let ki = 0; ki < (p.keys || []).length; ki++) {
diff --git a/app/server/public/css/style.css b/app/server/public/css/style.css
index f7ab2e7..65e23fb 100644
--- a/app/server/public/css/style.css
+++ b/app/server/public/css/style.css
@@ -405,6 +405,13 @@ body {
}
.model-group-meta { margin-left: auto; flex-shrink: 0; font-size: 12px; color: #888; white-space: nowrap; }
.model-group-body { padding: 0 10px 10px; border-top: 1px dashed #eef0f5; }
+/* 余额行:展开体顶部(随折叠隐藏),首次展开自动查,右侧环形箭头手动刷新 */
+.model-group-bal { display: flex; align-items: center; gap: 6px; padding: 10px 4px 0; font-size: 12.5px; color: #556; }
+.mbal-refresh { border: none; background: none; padding: 2px; margin: 0; cursor: pointer; display: flex; color: #7f8db0; }
+.mbal-refresh svg { width: 14px; height: 14px; stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
+.mbal-refresh:active { opacity: 0.6; }
+.mbal-refresh.spin svg { animation: mbal-spin 0.8s linear infinite; }
+@keyframes mbal-spin { to { transform: rotate(360deg); } }
.model-group-body .model-card { margin: 10px 0 0; background: #fafbfd; border: 1px solid #eef0f5; box-shadow: none; }
.model-group-empty { font-size: 12px; color: #99a3b8; padding: 12px 4px 2px; text-align: center; }
diff --git a/app/server/public/index.html b/app/server/public/index.html
index d26e6cf..832907a 100644
--- a/app/server/public/index.html
+++ b/app/server/public/index.html
@@ -252,11 +252,7 @@
-
+
diff --git a/app/server/public/js/models.js b/app/server/public/js/models.js
index 9084455..7487b38 100644
--- a/app/server/public/js/models.js
+++ b/app/server/public/js/models.js
@@ -3,7 +3,8 @@
// 模型项只有 {id, display_name, vision};id 只需同平台内唯一(内部唯一键 = 平台名/模型id,
// effective.models 的 id 即该键、mid 为原始模型名)。页面 PATCH 用 platforms[i].keys[j].models[k] 嵌套下标定位;
// 新增一律走 append(只动目标数组,脱敏 key 绝不整体回写文件);已有 Key 的 api_key 修改请走配置原文编辑器。
-// 展示层:模型池按「平台名 + 脱敏 Key」分组(余额由二者决定),父级 Key、子级模型,默认折叠。
+// 展示层:模型池按「平台名 + 脱敏 Key」分组(余额由二者决定),父级 Key、子级模型,默认折叠;
+// 余额行在展开体顶部,首次展开自动查一次(每 Tab 会话一次),可点刷新图标手动重查。
'use strict';
window.tabModules = window.tabModules || {};
@@ -14,6 +15,9 @@ let runtimeById = {}; // 运行池状态,按 id 关联
let modelsPendingRestart = false;
let moeIdx = -1; // 当前编辑弹窗对应的打平下标;-1 = 新增
let expandedGroups = {}; // 模型池分组展开态,key =「pi-ki」;余额由 name+key 决定,分组即 Key 槽位,默认折叠
+let balanceByGroup = {}; // 余额查询结果缓存,key =「pi-ki」;切 Tab/写配置时清空
+let balanceRequested = {}; // 本 Tab 会话内该分组是否已自动查过(首次展开只自动查一次)
+let balanceTypes = []; // 后端已接入余额查询的平台类型(GET /api/config 的 balance_types)
// api_key 脱敏显示:sk-***1234 样式。后端 GET effective 已脱敏,这里仅兜底(幂等)。
function maskKey(k) {
@@ -45,6 +49,12 @@ function toggleModelGroup(gk) {
el.classList.toggle('open', on);
const body = el.querySelector('.model-group-body');
if (body) body.classList.toggle('hidden', !on);
+ // 首次展开自动查一次余额(mbal 存在 = 该类型已接入);标记随切 Tab 重置,
+ // 故每次切到模型 Tab 后,每个分组只自动查一次,反复折叠展开不重复请求
+ if (on && !balanceRequested[gk] && $('mbal-' + gk)) {
+ const parts = gk.split('-');
+ fetchBalance(gk, Number(parts[0]), Number(parts[1]), false);
+ }
}
// 单个模型的子卡片;余额是 Key 级概念,统一在分组头显示,模型卡不再重复。
@@ -68,20 +78,77 @@ function modelCardHtml(m) {
'';
}
+// 余额展示文案与悬停详情(查询成功后才有值,默认 --)
+function balText(gk) {
+ const b = balanceByGroup[gk];
+ if (!b) return '余额 --';
+ if (b.error) return '余额 查询失败';
+ return '余额 ' + (b.total_balance == null ? '--' : b.total_balance) + (b.currency ? ' ' + esc(b.currency) : '');
+}
+function balTitle(gk) {
+ const b = balanceByGroup[gk];
+ if (!b) return '';
+ if (b.error) return '查询失败:' + b.error;
+ const parts = [];
+ if (b.available === false) parts.push('上游报告余额不可用');
+ if (b.granted_balance != null) parts.push('赠送 ' + b.granted_balance);
+ if (b.topped_up_balance != null) parts.push('充值 ' + b.topped_up_balance);
+ return parts.join(' · ');
+}
+
+async function fetchBalance(gk, pi, ki, manual) {
+ balanceRequested[gk] = true; // 无论成败,本 Tab 会话不再自动重查(手动刷新不受限)
+ const btn = $('mbrg-' + gk);
+ if (btn) btn.classList.add('spin');
+ const el = $('mbal-' + gk);
+ if (el) el.textContent = '余额 …';
+ try {
+ const r = await req('GET', '/api/platforms/balance?pi=' + pi + '&ki=' + ki);
+ const b = r.balance || {};
+ balanceByGroup[gk] = {
+ total_balance: b.total_balance, currency: b.currency,
+ granted_balance: b.granted_balance, topped_up_balance: b.topped_up_balance,
+ available: b.is_available,
+ };
+ } catch (e) {
+ balanceByGroup[gk] = { error: e.message };
+ if (manual) toast('查余额失败:' + e.message); // 自动查询失败只落悬停详情,不打扰
+ }
+ if (btn) btn.classList.remove('spin');
+ const el2 = $('mbal-' + gk);
+ if (el2) { el2.textContent = balText(gk); el2.title = balTitle(gk); }
+}
+
+// 点刷新图标:无视「本轮已查过」标记,总是重新请求
+function refreshBalance(ev, pi, ki) {
+ if (ev) ev.stopPropagation(); // 冒泡到分组头会误触折叠/展开
+ fetchBalance(pi + '-' + ki, pi, ki, true);
+}
+
+// 余额行刷新图标(环形箭头)
+const BAL_REFRESH_SVG = '';
+
function modelGroupHtml(pi, ki) {
const p = cfgPlatforms[pi] || {};
const k = (p.keys || [])[ki] || {};
const gk = pi + '-' + ki;
const models = cfgModels.filter(m => m.platform_index === pi && m.key_index === ki);
const open = !!expandedGroups[gk];
+ // 仅已接入余额查询的类型显示余额行(未接入的显示出来也只会是报错,不如不显示)
+ const hasBal = balanceTypes.indexOf(String(p.type || '')) >= 0;
return '' +
'
' +
'
' +
'
' + esc(p.name || ('平台' + pi)) + '' +
'
' + esc(k.api_key ? maskKey(k.api_key) : '无 key') + '' +
- '
余额 -- · ' + models.length + ' 模型' +
+ '
' + models.length + ' 模型' +
'
' +
'
' +
+ (hasBal ?
+ '
' +
+ '' + balText(gk) + '' +
+ '' +
+ '
' : '') +
(models.length ? models.map(modelCardHtml).join('') : '
该 Key 下暂无模型
') +
'
' +
'
';
@@ -96,6 +163,7 @@ async function loadModelCards() {
for (const m of rt || []) runtimeById[m.id] = m;
cfgPlatforms = (cg.effective && cg.effective.platforms) || [];
cfgModels = (cg.effective && cg.effective.models) || [];
+ balanceTypes = cg.balance_types || [];
if (!cfgModels.length) {
el.innerHTML = '尚未配置模型,点右上角 + 添加。
';
return;
@@ -107,7 +175,7 @@ async function loadModelCards() {
el.innerHTML = html +
(modelsPendingRestart
? '已写入 config.yml,到「设置」页重启服务后生效。
'
- : 'token 格式为「输入 / 输出」;余额按平台名+Key 统计,需对接模型提供方 API,暂未实现;改动写入 config.yml,重启服务后生效。
');
+ : 'token 格式为「输入 / 输出」;余额在分组首次展开时自动查询,点刷新图标可重查;改动写入 config.yml,重启服务后生效。
');
} catch (e) {
el.innerHTML = '加载失败:' + esc(e.message) + '
';
}
@@ -119,6 +187,8 @@ function idxOf(id) {
}
function afterWrite() {
modelsPendingRestart = true;
+ balanceByGroup = {}; // 配置变了,旧余额不再对应现有 pi/ki 槽位
+ balanceRequested = {};
toast('已写入 config.yml,到「设置」页重启后生效');
loadModelCards();
}
@@ -211,7 +281,7 @@ async function saveModelEditor() {
body = { op: 'append', path: ['platforms'], value: {
name: $('moe-np-name').value.trim(),
url,
- type: $('moe-np-type').value || 'openai',
+ type: $('moe-np-type').value.trim() || 'openai',
keys: [{ api_key: $('moe-np-key').value.trim(), concurrency: parseInt($('moe-np-con').value, 10) || 1, models: [model] }],
} };
} else if (t === 'newkey') {
@@ -255,8 +325,11 @@ window.modelsUI = {
del: delModelFromModal,
onTargetChange: onTargetChange,
toggleGroup: toggleModelGroup,
+ refreshBalance: refreshBalance,
};
tabModules.models = {
- onShow() { loadModelCards(); },
+ // 每次切到模型 Tab:余额结果与「本 Tab 已自动查过」标记一并重置,
+ // 分组首次点开时重新自动查一次(对应需求:每次切 Tab 后只首次点开自动更新)
+ onShow() { balanceByGroup = {}; balanceRequested = {}; loadModelCards(); },
};
diff --git a/app/server/routes/config.js b/app/server/routes/config.js
index 30283d5..cc6cb2a 100644
--- a/app/server/routes/config.js
+++ b/app/server/routes/config.js
@@ -4,6 +4,7 @@
const express = require('express');
const engine = require('../engine');
const { loadConfigFromText, readConfigText, applyEditsToText, writeConfigText } = require('../config');
+const balance = require('../balance');
const log = require('../log');
const router = express.Router();
@@ -92,7 +93,8 @@ router.get('/', wrap((req, res) => {
const cfg = cfgOf();
const text = readConfigText(cfg);
const shown = text === null ? '' : maskApiKeys(text);
- res.json({ config_path: cfg.config_path || '', exists: text !== null, text: shown, effective: maskEffective(cfg) });
+ // balance_types:已接入余额查询的平台类型,前端据此决定是否展示余额行
+ res.json({ config_path: cfg.config_path || '', exists: text !== null, text: shown, effective: maskEffective(cfg), balance_types: balance.types() });
}));
// 干跑校验(编辑器保存前预检):body { text }
diff --git a/app/server/server.js b/app/server/server.js
index bf9dfe6..6ddf9de 100644
--- a/app/server/server.js
+++ b/app/server/server.js
@@ -38,6 +38,7 @@ app.use('/api/sessions', require('./routes/messages'));
app.use('/api/sessions', require('./routes/files'));
app.use('/api/skills', require('./routes/skills'));
app.use('/api/config', require('./routes/config'));
+app.use('/api/platforms', require('./routes/platforms'));
app.use('/api/system', require('./routes/system'));
// 模型池状态(前端可展示空闲/占用)+ 每模型 token 用量(今日/累计,输入/输出分开)