2.0阶段5: 设置页——服务状态/优雅重启(409确认强打+轮询health自动刷新)/config.yml表单化单项编辑(PATCH保留注释)/模型条目增删改/账号与MCP只读/YAML原文编辑器(干跑校验);GET /api/config增返effective

This commit is contained in:
2026-09-01 17:52:07 +08:00
parent e8ec284194
commit f30c503909
6 changed files with 355 additions and 18 deletions
+28 -3
View File
@@ -352,15 +352,40 @@ body {
font-size: 12px; color: #888; margin-top: 3px;
display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
/* 文件编辑器:全屏覆盖本 Tab(盖住列表/详情与顶栏,不遮底部导航) */
#skill-editor {
/* 代码/配置编辑器:全屏覆盖所在 Tab(盖住列表/详情与顶栏,不遮底部导航) */
#skill-editor, #cfg-editor {
position: absolute; inset: 0; z-index: 6; background: #f2f3f7;
display: flex; flex-direction: column;
}
#skill-editor-area {
#skill-editor-area, #cfg-editor-area {
flex: 1; margin: 10px 12px 12px; resize: none; outline: none;
border: 1px solid #dde; border-radius: 10px; background: #fff; color: #223;
padding: 10px 12px; font-size: 12.5px; line-height: 1.55; tab-size: 4;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, "Courier New", monospace;
white-space: pre; overflow: auto;
}
/* ---- 设置页(阶段 5) ---- */
.settings-card {
background: #fff; border-radius: 12px; padding: 10px 14px 12px; margin-bottom: 12px;
box-shadow: 0 1px 4px rgba(30,40,90,0.06);
}
.settings-card h4 { font-size: 14px; color: #334; margin: 4px 0 2px; display: flex; align-items: center; gap: 8px; }
.cfg-kv {
display: flex; align-items: baseline; gap: 10px;
font-size: 13px; padding: 7px 0; border-bottom: 1px dashed #eef0f5;
}
.cfg-kv:last-of-type { border-bottom: none; }
.cfg-kv label { color: #889; flex: none; max-width: 42%; }
.cfg-kv b {
font-weight: 500; flex: 1; text-align: right; color: #223;
overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
font-variant-numeric: tabular-nums;
}
.cfg-note { font-size: 11.5px; color: #99a; line-height: 1.5; margin-top: 6px; }
/* 原文编辑器保存前的校验错误条 */
.cfg-err {
margin: 8px 12px 0; padding: 8px 12px; border-radius: 8px;
background: #fdecea; color: #a3352a; font-size: 12.5px;
white-space: pre-wrap; word-break: break-all;
}
+23 -6
View File
@@ -98,7 +98,24 @@
<span>当前用户:<b id="whoami" class="hidden">-</b></span>
<button class="ghost-btn small hidden" id="logout-btn" onclick="ui.logout()">退出登录</button>
</div>
<div class="empty-tip">设置中心开发中(阶段 5):config.yml 配置管理与服务重启。</div>
<div id="cfg-sections"><div class="empty-tip">加载中…</div></div>
</div>
<!-- config.yml 原文编辑器(全屏覆盖本 Tab) -->
<div id="cfg-editor" class="hidden">
<div class="header">
<button class="icon-btn" onclick="cfgUI.close()" title="关闭">
<svg viewBox="0 0 24 24"><polyline points="15 18 9 12 15 6"></polyline></svg>
</button>
<span class="chat-title">config.yml 原文</span>
<div class="header-right">
<button class="icon-btn" onclick="cfgUI.save()" title="校验并保存">
<svg viewBox="0 0 24 24"><polyline points="20 6 9 17 4 12"></polyline></svg>
</button>
</div>
</div>
<div id="cfg-editor-err" class="cfg-err hidden"></div>
<textarea id="cfg-editor-area" spellcheck="false" autocomplete="off" autocapitalize="off"></textarea>
</div>
</div>
@@ -218,10 +235,10 @@
<div class="toast hidden" id="toast"></div>
<!-- 发版时更新 v 参数,避免移动端浏览器缓存旧 JS -->
<script src="/js/core.js?v=20260901e"></script>
<script src="/js/tasks.js?v=20260901e"></script>
<script src="/js/skills.js?v=20260901e"></script>
<script src="/js/models.js?v=20260901e"></script>
<script src="/js/settings.js?v=20260901e"></script>
<script src="/js/core.js?v=20260901f"></script>
<script src="/js/tasks.js?v=20260901f"></script>
<script src="/js/skills.js?v=20260901f"></script>
<script src="/js/models.js?v=20260901f"></script>
<script src="/js/settings.js?v=20260901f"></script>
</body>
</html>
+3 -1
View File
@@ -39,7 +39,9 @@ async function req(method, url, body, isForm) {
if (!res.ok) {
let msg = 'HTTP ' + res.status;
try { msg = (await res.json()).error || msg; } catch (_) {}
throw new Error(msg);
const err = new Error(msg);
err.status = res.status; // 供调用方区分 409(重启保护/重名)等状态
throw err;
}
return res.json();
}
+295 -6
View File
@@ -1,10 +1,162 @@
// 设置 Tab:用户信息 + config.yml 管理。
// 阶段 1 先注册模块占位并显示登录用户;阶段 5 实现配置编辑与重启。
// 设置 Tab(阶段 5):账号 / 服务状态与重启 / config.yml 表单化单项编辑 / 原文编辑器。
// 语义:所有写入 = 服务端校验 → 写盘;多数改动需「重启服务」生效(本项目不做热重载)。
// 表单值展示的是「当前生效值」;改完未重启前显示旧值属正常,卡片顶部会提示待生效段。
'use strict';
window.tabModules = window.tabModules || {};
// 显示当前登录用户(沿用 1.0 逻辑;未启用认证时静默跳过)
let effCfg = null; // 当前生效配置(GET /api/config 的 effective)
let cfgOrig = ''; // 原文编辑器打开/上次保存内容(脏检测)
const pendingKeys = new Set(); // 本次会话已写盘、但尚未通过重启生效的顶层段
const sleep = ms => new Promise(r => setTimeout(r, ms));
function getByPath(obj, p) {
return String(p).split('.').reduce((o, k) => (o == null ? undefined : o[k]), obj);
}
function fmtUptime(ms) {
const s = Math.floor((ms || 0) / 1000);
const d = Math.floor(s / 86400), h = Math.floor(s % 86400 / 3600), m = Math.floor(s % 3600 / 60);
if (d) return d + ' 天 ' + h + ' 小时';
if (h) return h + ' 小时 ' + m + ' 分';
if (m) return m + ' 分 ' + (s % 60) + ' 秒';
return s + ' 秒';
}
function dispVal(v) {
if (v === undefined || v === null || v === '') return '(未设置)';
if (typeof v === 'boolean') return v ? '是' : '否';
return String(v);
}
// ---------- 表单 schema:path 点分路径;type:str|int|float ----------
const CFG_FORM = [
{ group: '通用', items: [
{ path: 'port', label: '端口', type: 'int' },
{ path: 'workspace_root', label: '工作区目录', type: 'str' },
{ path: 'db_path', label: '数据库路径', type: 'str' },
] },
{ group: 'Agent 回合', items: [
{ path: 'agent.temperature', label: '温度 temperature', type: 'float' },
{ path: 'agent.max_tool_iterations', label: '单回合最大工具轮次', type: 'int' },
{ path: 'agent.max_retries', label: '上游失败重试次数', type: 'int' },
] },
{ group: '终端工具', items: [
{ path: 'terminal.default_timeout_ms', label: '命令默认超时(ms)', type: 'int' },
{ path: 'terminal.max_output_chars', label: '输出最大字符数', type: 'int' },
] },
{ group: '技能', items: [
{ path: 'skills.skills_root', label: '技能目录', type: 'str' },
{ path: 'skills.timeout_ms', label: '技能执行超时(ms)', type: 'int' },
] },
{ group: '模型租约(回合结束后的保持)', items: [
{ path: 'model_hold.idle_release_ms', label: '无人观看释放(ms)', type: 'int' },
{ path: 'model_hold.active_max_ms', label: '最长保持(ms)', type: 'int' },
{ path: 'model_hold.sweep_ms', label: '巡检间隔(ms)', type: 'int' },
] },
];
// ---------- 渲染 ----------
function kvPlain(label, v) {
return '<div class="cfg-kv"><label>' + esc(label) + '</label><b>' + esc(dispVal(v)) + '</b></div>';
}
function kvBtn(label, vDisp, call) {
return '<div class="cfg-kv"><label>' + esc(label) + '</label><b>' + vDisp + '</b>' +
'<button class="file-act" onclick="' + call + '">改</button></div>';
}
function svcCard(st) {
let html = '<div class="settings-card"><h4>服务状态' +
(pendingKeys.size ? ' <span class="badge held">待重启生效</span>' : '') + '</h4>';
html += kvPlain('版本 / Node', st.version + ' / ' + st.node);
html += kvPlain('已运行', fmtUptime(st.uptime_ms) + '(启动于 ' + new Date(st.started_at).toLocaleString() + ')');
html += kvPlain('端口', st.port);
html += kvPlain('运行中回合', (st.running_sessions || []).length);
html += kvPlain('配置文件', st.config_path);
html += kvPlain('工作区', st.workspace_root);
html += kvPlain('技能目录', st.skills_root);
if (pendingKeys.size) html += kvPlain('待生效改动', Array.from(pendingKeys).join(', '));
html += '<button class="primary-btn" id="restart-btn" style="width:100%;margin-top:8px" onclick="cfgUI.restart()">重启服务</button>' +
'<div class="cfg-note">配置改动重启后生效;重启约需十几秒,期间页面暂不可用。非 Docker 部署不会自动拉起。</div></div>';
return html;
}
function formCard() {
let html = '';
for (const g of CFG_FORM) {
html += '<div class="settings-card"><h4>' + esc(g.group) + '</h4>';
for (const it of g.items) {
const call = "cfgUI.edit('" + it.path + "','" + it.label + "','" + it.type + "')";
html += kvBtn(it.label, esc(dispVal(getByPath(effCfg, it.path))), call);
}
html += '</div>';
}
return html;
}
function modelsCard() {
const ms = (effCfg && effCfg.models) || [];
let html = '<div class="settings-card"><h4>模型池(' + ms.length + ')</h4>';
html += ms.map((m, i) =>
'<div class="model-card" style="margin-top:8px">' +
'<div class="model-card-head"><span class="model-card-name">' + esc(m.display_name || m.id) + '</span>' +
'<button class="file-act del" onclick="cfgUI.modelDel(' + i + ')">删除</button></div>' +
kvPlain('id(改 id 请用原文编辑)', m.id) +
kvBtn('上游地址 url', esc(m.url), "cfgUI.modelEdit(" + i + ",'url')") +
kvBtn('api_key', m.api_key ? '已设置(••••' + esc(String(m.api_key).slice(-4)) + ')' : '(未设置)', "cfgUI.modelEdit(" + i + ",'api_key')") +
kvBtn('支持图片 vision', esc(dispVal(!!m.vision)), "cfgUI.modelVision(" + i + ")") +
'</div>').join('');
html += '<button class="ghost-btn" style="width:100%;margin-top:8px" onclick="cfgUI.modelAdd()">+ 添加模型</button>' +
'<div class="cfg-note">添加/删除会整体重写 models 段(其中的注释会丢失);精细调整请用配置文件原文编辑。</div></div>';
return html;
}
function authCard() {
const a = effCfg && effCfg.auth;
let html = '<div class="settings-card"><h4>登录账号</h4>';
if (a && a.users && a.users.length) {
html += a.users.map(u => kvPlain('用户名', u.user)).join('');
html += '<div class="cfg-note">增删账号或改密码:请用下方「配置文件原文」编辑 auth.users 后重启。</div>';
} else {
html += '<div class="cfg-note">未启用认证:能访问本地址的人可直接使用。如需登录,在配置文件 auth.users 中添加 {user, password} 并重启。</div>';
}
return html + '</div>';
}
function mcpCard() {
const list = (effCfg && effCfg.mcp_servers) || [];
let html = '<div class="settings-card"><h4>MCP 服务(' + list.length + ')</h4>';
html += list.length
? list.map(s => kvPlain(s.name || 'mcp', s.url)).join('')
: '<div class="cfg-note">未配置外部 MCP 服务。</div>';
return html + '<div class="cfg-note">headers 含密钥,不在页面展示;请用配置文件原文编辑。</div></div>';
}
function fileCard(cg) {
return '<div class="settings-card"><h4>配置文件</h4>' +
kvPlain('路径', cg.config_path) +
(cg.exists ? '' : '<div class="cfg-note">配置文件当前不存在,原文保存时将创建。</div>') +
'<button class="ghost-btn" style="width:100%;margin-top:6px" onclick="cfgUI.open()">编辑原文(YAML)</button>' +
'<div class="cfg-note">保存前自动校验,不合法不落盘;注释与排版原样保留。</div></div>';
}
async function loadSettings() {
refreshUser();
const el = $('cfg-sections');
if (!el) return;
el.innerHTML = '<div class="empty-tip">加载中…</div>';
try {
const [st, cg] = await Promise.all([
req('GET', '/api/system/status'),
req('GET', '/api/config'),
]);
effCfg = cg.effective || {};
el.innerHTML = svcCard(st) + formCard() + modelsCard() + authCard() + mcpCard() + fileCard(cg);
} catch (e) {
el.innerHTML = '<div class="empty-tip">加载失败:' + esc(e.message) + '</div>';
}
}
// 显示登录用户(沿用 1.0 逻辑;未启用认证时静默跳过)
async function refreshUser() {
try {
const me = await req('GET', '/api/me');
@@ -18,8 +170,145 @@ async function refreshUser() {
} catch (_) { /* 未启用认证 */ }
}
// ---------- 写操作 ----------
function afterWrite(r) {
(r.changed || []).forEach(k => pendingKeys.add(k));
const ch = (r.changed || []).join(', ');
toast('已写入 config.yml' + (ch ? ';段 [' + ch + '] 重启后生效' : ';与当前生效值一致,无需重启'));
loadSettings();
}
async function editCfgItem(p, label, type) {
const cur = getByPath(effCfg, p);
let v = prompt(label + ' 新值:', cur == null ? '' : String(cur));
if (v == null) return;
v = v.trim();
let val = v;
if (type === 'int' || type === 'float') {
val = Number(v);
if (v === '' || !isFinite(val)) { toast('请输入数字'); return; }
}
try {
const r = await req('PATCH', '/api/config', { path: p, value: val });
afterWrite(r);
} catch (e) { toast('保存失败:' + e.message); }
}
async function modelEdit(i, field) {
const m = ((effCfg || {}).models || [])[i] || {};
const label = field === 'url' ? '上游地址(OpenAI 兼容 /v1,不带尾斜杠)' : 'API Key(留空则不带鉴权头)';
// api_key 不回填旧值,避免密钥长时间展示在输入框
const cur = field === 'url' ? (m.url || '') : '';
const v = prompt(label + ':', cur);
if (v == null) return;
try {
const r = await req('PATCH', '/api/config', { path: ['models', i, field], value: v.trim() });
afterWrite(r);
} catch (e) { toast('保存失败:' + e.message); }
}
async function modelVision(i) {
const ms = (effCfg && effCfg.models) || [];
const m = ms[i] || {};
const nv = !m.vision;
if (!confirm('模型「' + (m.display_name || m.id) + '」支持图片输入(vision)?\n确定=开启,取消=关闭')) return;
try {
const r = await req('PATCH', '/api/config', { path: ['models', i, 'vision'], value: nv });
afterWrite(r);
} catch (e) { toast('保存失败:' + e.message); }
}
async function modelAdd() {
const ms = (effCfg && effCfg.models) || [];
const id = (prompt('新模型 id(唯一,如 ds3):') || '').trim();
if (!id) return;
if (ms.some(m => m.id === id)) { toast('id 已存在:' + id); return; }
const url = (prompt('上游地址(OpenAI 兼容 /v1):') || '').trim();
if (!url) return;
try {
// 整体替换 models 数组(setIn 数组下标只适合改字段,增删只能整段重写)
const r = await req('PATCH', '/api/config', { path: 'models', value: ms.concat([{ id, display_name: id, url }]) });
afterWrite(r);
} catch (e) { toast('添加失败:' + e.message); }
}
async function modelDel(i) {
const ms = (effCfg && effCfg.models) || [];
const m = ms[i] || {};
if (ms.length <= 1) { toast('至少要保留一个模型'); return; }
if (!confirm('删除模型「' + (m.display_name || m.id) + '」?将整体重写 models 段(注释可能丢失)。')) return;
try {
const r = await req('PATCH', '/api/config', { path: 'models', value: ms.filter((_, j) => j !== i) });
afterWrite(r);
} catch (e) { toast('删除失败:' + e.message); }
}
// ---------- 重启 ----------
async function restart() {
if (!confirm('重启服务使新配置生效?正在执行的 AI 回合会被打断。')) return;
try {
await req('POST', '/api/system/restart');
} catch (e) {
if (e.status === 409 && /回合执行中/.test(e.message)) {
// 仅「有回合在跑」值得二次确认强打;配置不合法等 409 强打也无法启动,直接报给用户
if (!confirm(e.message + '\n\n仍要强制重启?')) return;
try { await req('POST', '/api/system/restart?force=true'); }
catch (e2) { toast('重启失败:' + e2.message); return; }
} else { toast('重启失败:' + e.message); return; }
}
const btn = $('restart-btn');
if (btn) { btn.disabled = true; btn.textContent = '重启中,请勿关闭页面…'; }
toast('服务重启中…');
for (let i = 0; i < 90; i++) {
await sleep(1000);
try { const r = await fetch('/api/health'); if (r.ok) { location.reload(); return; } } catch (_) {}
}
toast('90 秒内未恢复,请检查容器状态(非 Docker 部署需手动启动)');
if (btn) { btn.disabled = false; btn.textContent = '重启服务'; }
}
// ---------- config.yml 原文编辑器 ----------
async function openCfgEditor() {
try {
const cg = await req('GET', '/api/config');
cfgOrig = cg.text;
$('cfg-editor-area').value = cg.text;
$('cfg-editor-err').classList.add('hidden');
$('cfg-editor').classList.remove('hidden');
} catch (e) { toast('读取失败:' + e.message); }
}
async function saveCfgEditor() {
const text = $('cfg-editor-area').value;
const errEl = $('cfg-editor-err');
errEl.classList.add('hidden');
try {
await req('POST', '/api/config/validate', { text }); // 先干跑,错误信息更友好
const r = await req('PUT', '/api/config', { text });
cfgOrig = text;
afterWrite(r);
} catch (e) {
errEl.textContent = '校验失败:' + e.message;
errEl.classList.remove('hidden');
toast('配置不合法,未写入');
}
}
function closeCfgEditor() {
if ($('cfg-editor-area').value !== cfgOrig && !confirm('有未保存的修改,确定关闭?')) return;
$('cfg-editor').classList.add('hidden');
}
window.cfgUI = {
edit: editCfgItem,
restart: restart,
modelEdit: modelEdit,
modelVision: modelVision,
modelAdd: modelAdd,
modelDel: modelDel,
open: openCfgEditor,
save: saveCfgEditor,
close: closeCfgEditor,
};
tabModules.settings = {
onShow() {
refreshUser();
},
onShow() { loadSettings(); },
};
+2 -2
View File
@@ -34,11 +34,11 @@ function diffTop(oldCfg, newCfg) {
function cfgOf() { return engine.getConfig(); }
// 配置原文 + 路径(页面编辑器用;注释原样保留)
// 配置原文 + 路径 + 当前生效配置(设置页表单显示当前值;注释原样保留)
router.get('/', wrap((req, res) => {
const cfg = cfgOf();
const text = readConfigText(cfg);
res.json({ config_path: cfg.config_path || '', exists: text !== null, text: text === null ? '' : text });
res.json({ config_path: cfg.config_path || '', exists: text !== null, text: text === null ? '' : text, effective: cfg });
}));
// 干跑校验(编辑器保存前预检):body { text }
+4
View File
@@ -204,6 +204,7 @@ async function main() {
const origText = fs.readFileSync(cfgPath, 'utf8');
const got = await api('GET', '/api/config');
ok(got.exists && got.text.includes('models:'), 'GET /api/config 返回原文');
ok(got.effective && got.effective.agent && got.effective.agent.max_retries === 1 && got.effective.models.length === 2, 'GET 返回生效配置(effective)');
const patched = await api('PATCH', '/api/config', { path: 'agent.temperature', value: 0.5 });
ok(patched.ok && patched.changed.includes('agent'), 'PATCH 单项修改');
const afterPatch = await api('GET', '/api/config');
@@ -213,6 +214,9 @@ async function main() {
try { await api('PUT', '/api/config', { text: 'models: [unclosed' }); } catch (e) { bad400 = /400/.test(e.message); }
ok(bad400, 'PUT 非法配置被 400 拒绝');
ok(fs.readFileSync(cfgPath, 'utf8') === afterPatch.text, '非法写入未动文件');
const mpatch = await api('PATCH', '/api/config', { path: 'models.0.display_name', value: '改名模型' });
const mtext = (await api('GET', '/api/config')).text;
ok(mpatch.ok && mtext.includes('改名模型'), 'PATCH 支持 models[i] 数组下标');
const restored = await api('PUT', '/api/config', { text: origText });
ok(restored.ok && fs.readFileSync(cfgPath, 'utf8') === origText, 'PUT 整文件恢复原配置');