2.0阶段5调整4: 模型卡默认仅名称/id/状态+余额与token占位,编辑图标开统一弹窗(编辑/删除/新增复用);新增全局confirmModal/promptModal替代全部原生弹窗;新建文件按钮改primary样式

This commit is contained in:
2026-09-01 22:36:50 +08:00
parent d645c00dd8
commit 981ca33a1a
7 changed files with 181 additions and 118 deletions
+67 -89
View File
@@ -1,14 +1,14 @@
// 模型 Tab(阶段 5):模型池状态 + 增 / 改 / 删(仅写 config.yml,到「设置」页重启才生效)。
// 状态(忙/闲)来自运行中的模型池 /api/models;配置(名称/地址/key/vision)来自 /api/config effective。
// 说明:删除/新增按整段替换 models(该段注释会丢失);id 不提供页面修改(会话按 id 绑定,改 id 属危险操作)。
// 卡片默认只展示:名称 / id / 运行状态 + 余额、token 用量占位(功能后续上线)。
// 编辑走统一弹窗(新增复用同一弹窗);id 不可页面修改(会话按 id 绑定,改 id 走配置文件原文)。
'use strict';
window.tabModules = window.tabModules || {};
// 配置里的模型数组(下标即 PATCH 下标);运行态按 id 关联
let cfgModels = [];
let runtimeById = {};
let cfgModels = []; // 配置里的模型数组(下标即 PATCH 下标)
let runtimeById = {}; // 运行池状态,按 id 关联
let modelsPendingRestart = false;
let moeIdx = -1; // 当前编辑弹窗对应的下标;-1 = 新增
async function loadModelCards() {
const el = $('model-cards');
@@ -30,23 +30,23 @@ async function loadModelCards() {
: '<span class="model-card-state pending">待重启</span>';
return '<div class="model-card">' +
'<div class="model-card-head">' +
'<span class="model-card-name">' + esc(m.display_name || m.id) + '</span>' + stateHtml +
'<span class="model-card-name">' + esc(m.display_name || m.id) + '</span>' +
'<span class="model-card-right">' + stateHtml +
'<button class="model-edit-ico" title="编辑" onclick="modelsUI.openEdit(\'' + encodeURIComponent(m.id) + '\')">' +
'<svg viewBox="0 0 24 24"><path d="M17 3a2.83 2.83 0 1 1 4 4L7.5 20.5 2 22l1.5-5.5z"></path></svg>' +
'</button>' +
'</span>' +
'</div>' +
'<div class="model-card-meta">' + esc(m.id) +
(run && run.is_busy && run.current_session_id ? ' · 会话 ' + esc(run.current_session_id) : '') + '</div>' +
'<div class="cfg-kv"><label>地址</label><b>' + esc(m.url) + '</b>' +
'<button class="file-act" onclick="modelsUI.edit(\'' + encodeURIComponent(m.id) + '\',\'url\')">改</button></div>' +
'<div class="cfg-kv"><label>api_key</label><b>' + (m.api_key ? '已设置(••••' + esc(String(m.api_key).slice(-4)) + ')' : '(未设置)') + '</b>' +
'<button class="file-act" onclick="modelsUI.edit(\'' + encodeURIComponent(m.id) + '\',\'api_key\')">改</button></div>' +
'<div class="cfg-kv"><label>支持图片</label><b>' + (m.vision ? '是' : '否') + '</b>' +
'<button class="file-act" onclick="modelsUI.toggleVision(\'' + encodeURIComponent(m.id) + '\')">' + (m.vision ? '关' : '开') + '</button></div>' +
'<div class="model-card-foot"><button class="file-act" onclick="modelsUI.rename(\'' + encodeURIComponent(m.id) + '\')">改显示名</button>' +
'<button class="file-act del" onclick="modelsUI.del(\'' + encodeURIComponent(m.id) + '\')">删除模型</button></div>' +
'<div class="cfg-kv"><label>余额</label><b>--</b></div>' +
'<div class="cfg-kv"><label>今日 token</label><b>--</b></div>' +
'<div class="cfg-kv"><label>累计 token</label><b>--</b></div>' +
'</div>';
}).join('') +
(modelsPendingRestart
? '<div class="empty-tip">已写入 config.yml,到「设置」页重启服务后生效。</div>'
: '<div class="empty-tip">改动写入 config.yml,重启服务后生效。token 用量统计在阶段 4 上线。</div>');
: '<div class="empty-tip">余额与 token 用量统计后续上线;改动写入 config.yml,重启服务后生效。</div>');
} catch (e) {
el.innerHTML = '<div class="empty-tip">加载失败:' + esc(e.message) + '</div>';
}
@@ -56,100 +56,78 @@ function idxOf(id) {
for (let i = 0; i < cfgModels.length; i++) if (cfgModels[i].id === id) return i;
return -1;
}
function afterWrite(r) {
function afterWrite() {
modelsPendingRestart = true;
toast('已写入 config.yml,到「设置」页重启后生效');
loadModelCards();
}
// ---------- 增 ----------
function openAddModel() {
['model-new-id', 'model-new-name', 'model-new-url', 'model-new-key'].forEach(i => { $(i).value = ''; });
$('model-new-vision').checked = false;
$('model-add-modal').classList.remove('hidden');
$('model-new-id').focus();
// ---------- 编辑/新增弹窗 ----------
function openModelEditor(rawId) {
const add = rawId == null;
moeIdx = add ? -1 : idxOf(decodeURIComponent(rawId));
if (!add && moeIdx < 0) return toast('模型不存在');
const m = add ? { display_name: '', id: '', url: '', api_key: '', vision: false } : cfgModels[moeIdx];
$('moe-title').textContent = add ? '添加模型' : '编辑模型';
$('moe-name').value = m.display_name || '';
const idIn = $('moe-id');
idIn.value = m.id || '';
idIn.disabled = !add; // 编辑时禁改 id(历史会话按 id 绑定)
$('moe-url').value = m.url || '';
$('moe-key').value = m.api_key || '';
$('moe-vision').checked = !!m.vision;
$('moe-del-btn').classList.toggle('hidden', add);
$('model-edit-modal').classList.remove('hidden');
}
function closeAddModel() { $('model-add-modal').classList.add('hidden'); }
function closeModelEditor() { $('model-edit-modal').classList.add('hidden'); }
async function addModel() {
const id = $('model-new-id').value.trim();
const url = $('model-new-url').value.trim();
if (!id) { toast('请填写 id'); return; }
if (idxOf(id) >= 0) { toast('id 已存在:' + id); return; }
if (!url) { toast('请填写上游地址'); return; }
const m = {
async function saveModelEditor() {
const add = moeIdx < 0;
const id = $('moe-id').value.trim();
const url = $('moe-url').value.trim().replace(/\/+$/, '');
if (!id) return toast('请填写 id');
if (!url) return toast('请填写上游地址');
const obj = {
id,
display_name: $('model-new-name').value.trim() || id,
display_name: $('moe-name').value.trim() || id,
url,
api_key: $('model-new-key').value.trim(),
vision: $('model-new-vision').checked,
api_key: $('moe-key').value.trim(),
vision: $('moe-vision').checked,
};
let body;
if (add) {
if (idxOf(id) >= 0) return toast('id 已存在:' + id);
body = { path: 'models', value: cfgModels.concat([obj]) };
} else {
// 整项替换;key 顺序固定,保持配置文件观感一致
body = { path: ['models', moeIdx], value: obj };
}
try {
const r = await req('PATCH', '/api/config', { path: 'models', value: cfgModels.concat([m]) });
closeAddModel();
afterWrite(r);
} catch (e) { toast('添加失败:' + e.message); }
}
// ---------- 改 ----------
async function patchModel(id, field, value) {
const i = idxOf(id);
if (i < 0) return toast('模型不存在:' + id);
try {
const r = await req('PATCH', '/api/config', { path: ['models', i, field], value });
afterWrite(r);
await req('PATCH', '/api/config', body);
closeModelEditor();
afterWrite();
} catch (e) { toast('保存失败:' + e.message); }
}
async function editModel(rawId, field) {
const id = decodeURIComponent(rawId);
const m = cfgModels[idxOf(id)] || {};
const label = field === 'url' ? '上游地址(OpenAI 兼容 /v1,不带尾斜杠)' : 'API Key(留空则不带鉴权头)';
// api_key 不回填旧值,避免密钥展示在输入框
const v = prompt(label + ':', field === 'url' ? (m.url || '') : '');
if (v == null) return;
await patchModel(id, field, v.trim());
}
async function renameModel(rawId) {
const id = decodeURIComponent(rawId);
const m = cfgModels[idxOf(id)] || {};
const v = prompt('显示名:', m.display_name || '');
if (v == null) return;
const name = v.trim();
if (!name) return toast('显示名不能为空');
await patchModel(id, 'display_name', name);
}
async function toggleVision(rawId) {
const id = decodeURIComponent(rawId);
const m = cfgModels[idxOf(id)] || {};
if (!confirm('模型「' + (m.display_name || id) + '」支持图片输入(vision)?\n确定=开启,取消=关闭')) return;
await patchModel(id, 'vision', !m.vision);
}
// ---------- 删(整段替换 models,会丢该段注释) ----------
async function delModel(rawId) {
const id = decodeURIComponent(rawId);
const i = idxOf(id);
if (i < 0) return;
const m = cfgModels[i];
async function delModelFromModal() {
if (moeIdx < 0) return;
const m = cfgModels[moeIdx];
closeModelEditor();
if (cfgModels.length <= 1) return toast('至少要保留一个模型');
if (!confirm('删除模型「' + (m.display_name || id) + '」?将整体重写 models 段(注释可能丢失)。')) return;
const ok = await confirmModal('删除模型', '删除模型「' + (m.display_name || m.id) + '」?将整体重写 models 段(注释可能丢失)。', '删除', true);
if (!ok) return;
try {
const r = await req('PATCH', '/api/config', { path: 'models', value: cfgModels.filter(x => x.id !== id) });
afterWrite(r);
await req('PATCH', '/api/config', { path: 'models', value: cfgModels.filter(x => x.id !== m.id) });
afterWrite();
} catch (e) { toast('删除失败:' + e.message); }
}
window.modelsUI = {
openAdd: openAddModel,
closeAdd: closeAddModel,
add: addModel,
edit: editModel,
rename: renameModel,
toggleVision: toggleVision,
del: delModel,
openAdd: function () { openModelEditor(null); },
openEdit: openModelEditor,
close: closeModelEditor,
save: saveModelEditor,
del: delModelFromModal,
};
tabModules.models = {