feat(models): 模型可禁用 — models.enable(默认启用),禁用不进模型池;模型池页加启停开关

This commit is contained in:
2026-09-03 22:09:10 +08:00
parent 3ba16402a8
commit 4d11317baa
5 changed files with 61 additions and 14 deletions
+12 -1
View File
@@ -759,7 +759,7 @@ label.icon-btn { cursor: pointer; }
.model-group-empty { font-size: 12px; color: var(--gray-light); padding: 12px 4px 2px; text-align: center; }
.model-card { background: #fafbfd; border: 1px solid var(--border); border-radius: 12px; padding: 12px 14px; margin: 10px 0 0; }
.model-card:last-child { margin-bottom: 2px; }
.model-card-head { display: flex; align-items: baseline; gap: 8px; }
.model-card-head { display: flex; align-items: center; gap: 8px; }
.model-card-name { font-weight: 600; font-size: 15px; flex-shrink: 0; cursor: pointer; }
.model-card-name:active { opacity: 0.55; }
.model-card-meta { font-size: 12px; color: var(--gray); flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
@@ -767,6 +767,17 @@ label.icon-btn { cursor: pointer; }
.model-card-state.idle { background: #e3f2e6; color: #217a34; }
.model-card-state.busy { background: #fdecea; color: #b3382c; }
.model-card-state.pending { background: #fff4dc; color: #a06a00; }
.model-card-state.off { background: var(--surface); color: var(--gray); }
/* 禁用态卡片:背景转灰、名称/token 区淡化,开关保持正常亮度可点 */
.model-card.off { background: #f5f6f8; }
.model-card.off .model-card-name, .model-card.off .cfg-kv { opacity: 0.5; }
/* 启用/禁用小开关:卡片头部最右侧;勾选=启用(蓝),取消=禁用(灰) */
.model-en-sw { position: relative; flex-shrink: 0; width: 34px; height: 20px; display: inline-block; }
.model-en-sw input { position: absolute; left: 0; top: 0; width: 100%; height: 100%; margin: 0; opacity: 0; cursor: pointer; z-index: 1; }
.model-en-sw .sw-track { position: absolute; left: 0; top: 0; right: 0; bottom: 0; background: var(--gray-light); border-radius: 999px; transition: background .15s; }
.model-en-sw .sw-track::after { content: ''; position: absolute; left: 2px; top: 2px; width: 16px; height: 16px; background: #fff; border-radius: 50%; box-shadow: 0 1px 2px rgba(0, 0, 0, .25); transition: transform .15s; }
.model-en-sw input:checked + .sw-track { background: var(--active); }
.model-en-sw input:checked + .sw-track::after { transform: translateX(14px); }
/* ---------- 编辑器页(config.yml 原文 / 技能文件) ---------- */
.set-editor {
+37 -6
View File
@@ -1605,6 +1605,7 @@
editorPath: null, editorOrig: '', editorText: '', editorLabel: '',
// 模型池(搬自 models.js)
cfgPlatforms: [], cfgModels: [], runtimeById: {}, modelsPendingRestart: false,
pendingEnable: {}, // 已写盘但重启前 effective 仍是旧值的 enable 覆盖(id → bool),防卡片回弹
moeIdx: -1, expandedGroups: {}, balanceByGroup: {}, balanceRequested: {}, balanceTypes: [],
};
let setTok = 0; // 异步渲染防竞态令牌
@@ -2281,19 +2282,44 @@
fetchBalance(gk, Number(parts[0]), Number(parts[1]), false);
}
}
// 启停切换:写 platforms[pi].keys[ki].models[mi].enable(缺省启用),重启后生效。
// 重启前后端 effective 仍返旧值,成功后本地记入 SET.pendingEnable 保持卡片显示不回弹。
async function toggleModelEnable(rawId) {
const m = SET.cfgModels[idxOf(decodeURIComponent(rawId))];
if (!m) return toast('模型不存在');
const next = m.enable === false; // 当前禁用 → 目标启用
try {
await req('PATCH', '/api/config', { path: ['platforms', m.platform_index, 'keys', m.key_index, 'models', m.model_index, 'enable'], value: next });
m.enable = next;
SET.pendingEnable[m.id] = next;
afterWrite();
} catch (e) {
toast('切换失败:' + e.message);
loadModelCards(); // 失败时重拉,回滚开关显示
}
}
// 单个模型的子卡片;余额是 Key 级概念,统一在分组头显示
function modelCardHtml(m) {
const run = SET.runtimeById[m.id];
// 运行池里没有 = 配置新增但还没重启生效
const stateHtml = run
? '<span class="model-card-state ' + (run.is_busy ? 'busy' : 'idle') + '">' + (run.is_busy ? '占用中' : '空闲') + '</span>'
: '<span class="model-card-state pending">待重启</span>';
return '<div class="model-card">' +
const disabled = m.enable === false;
// 运行池里没有 = 配置新增但还没重启生效;禁用后若仍在池里(未重启)也提示待重启
const stateHtml = disabled
? (run
? '<span class="model-card-state pending">待重启</span>'
: '<span class="model-card-state off">已禁用</span>')
: run
? '<span class="model-card-state ' + (run.is_busy ? 'busy' : 'idle') + '">' + (run.is_busy ? '占用中' : '空闲') + '</span>'
: '<span class="model-card-state pending">待重启</span>';
return '<div class="model-card' + (disabled ? ' off' : '') + '">' +
'<div class="model-card-head">' +
'<span class="model-card-name" title="点击编辑" onclick="sUI.mEdit(\'' + encodeURIComponent(m.id) + '\')">' + esc(m.display_name || m.mid || m.id) + '</span>' +
'<span class="model-card-meta">' + esc(m.mid || m.id) +
(run && run.is_busy && run.current_session_id ? ' · 会话 ' + esc(run.current_session_id) : '') + '</span>' +
stateHtml +
'<label class="model-en-sw" title="' + (disabled ? '已禁用,点击启用' : '启用中,点击禁用') + '">' +
'<input type="checkbox"' + (disabled ? '' : ' checked') + ' onchange="sUI.mToggleEnable(\'' + encodeURIComponent(m.id) + '\')">' +
'<span class="sw-track"></span>' +
'</label>' +
'</div>' +
'<div class="cfg-kv"><label>今日 token</label><b title="输入 / 输出">' + (run ? fmtTok(run.today_prompt) + ' / ' + fmtTok(run.today_completion) : '--') + '</b></div>' +
'<div class="cfg-kv"><label>累计 token</label><b title="输入 / 输出">' + (run ? fmtTok(run.total_prompt) + ' / ' + fmtTok(run.total_completion) : '--') + '</b></div>' +
@@ -2335,6 +2361,8 @@
for (const m of rt || []) SET.runtimeById[m.id] = m;
SET.cfgPlatforms = (cg.effective && cg.effective.platforms) || [];
SET.cfgModels = (cg.effective && cg.effective.models) || [];
// 重启前 effective 仍是旧配置:叠加已写盘的启停结果,保证卡片显示与已保存配置一致
for (const m of SET.cfgModels) if (m.id in SET.pendingEnable) m.enable = SET.pendingEnable[m.id];
SET.balanceTypes = cg.balance_types || [];
const box = $('model-cards');
if (!SET.cfgModels.length) {
@@ -2431,11 +2459,13 @@
async function saveModelEditor() {
const id = $('moe-id').value.trim();
if (!id) return toast('请填写 id');
// 模型项本身只含 id/display_name/vision;url/key/concurrency 在平台/Key 层,这里不动。
// 模型项本身只含 id/display_name/vision/enable;url/key/concurrency 在平台/Key 层,这里不动。
// 编辑是整体覆盖写,须携带当前 enable,否则编辑一次会把禁用态洗回启用。
const model = {
id,
display_name: $('moe-name').value.trim() || id,
vision: $('moe-vision').checked,
enable: SET.moeIdx < 0 ? true : SET.cfgModels[SET.moeIdx].enable !== false,
};
const add = SET.moeIdx < 0;
let body;
@@ -2524,6 +2554,7 @@
mDel: delModelFromModal,
toggleGroup: toggleModelGroup,
refreshBalance: refreshBalance,
mToggleEnable: toggleModelEnable,
};
// ---------- 初始化 ----------