From e9a13315578e10408bdae506c67879d6f0d5b51f Mon Sep 17 00:00:00 2001 From: kicer Date: Tue, 1 Sep 2026 16:50:00 +0800 Subject: [PATCH] =?UTF-8?q?2.0=E9=98=B6=E6=AE=B52:=20=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E8=AF=BB=E5=86=99API(GET/PATCH=E4=BF=9D=E7=95=99=E6=B3=A8?= =?UTF-8?q?=E9=87=8A/PUT=E6=95=B4=E6=96=87=E4=BB=B6/validate=E5=B9=B2?= =?UTF-8?q?=E8=B7=91)+=E7=B3=BB=E7=BB=9F=E7=8A=B6=E6=80=81+=E4=BC=98?= =?UTF-8?q?=E9=9B=85=E9=87=8D=E5=90=AF(exit+Docker=E6=8B=89=E8=B5=B7,?= =?UTF-8?q?=E8=BF=90=E8=A1=8C=E4=B8=AD=E5=9B=9E=E5=90=88409=E4=BF=9D?= =?UTF-8?q?=E6=8A=A4);=E5=8E=BB=E6=8E=89logo=E9=80=80=E5=87=BA=E7=99=BB?= =?UTF-8?q?=E5=BD=95;config.yml=E6=8C=82=E8=BD=BD=E6=94=BE=E5=BC=80:ro?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/package.json | 2 +- app/server/config.js | 55 +++++++++++++++++--- app/server/engine.js | 9 ++++ app/server/public/css/style.css | 4 +- app/server/public/index.html | 12 ++--- app/server/routes/config.js | 89 +++++++++++++++++++++++++++++++++ app/server/routes/system.js | 66 ++++++++++++++++++++++++ app/server/server.js | 2 + app/test/smoke.js | 34 +++++++++++++ docker-compose.yml | 2 +- 10 files changed, 256 insertions(+), 19 deletions(-) create mode 100644 app/server/routes/config.js create mode 100644 app/server/routes/system.js diff --git a/app/package.json b/app/package.json index 156df92..0c43d73 100644 --- a/app/package.json +++ b/app/package.json @@ -1,6 +1,6 @@ { "name": "agent-app", - "version": "1.0.0", + "version": "2.0.0", "description": "自用通用 Agent 系统:多模型调度 + 任务目录 + 工具调用 + H5 Web UI", "main": "server/server.js", "scripts": { diff --git a/app/server/config.js b/app/server/config.js index e890f67..ad4ccc4 100644 --- a/app/server/config.js +++ b/app/server/config.js @@ -37,12 +37,9 @@ function deepMerge(base, extra) { return out; } -function loadConfig(configPath) { - const p = configPath || process.env.AGENT_CONFIG || path.join(__dirname, '..', 'config.yml'); - let raw = {}; - if (fs.existsSync(p)) { - raw = YAML.parse(fs.readFileSync(p, 'utf8')) || {}; - } +// 校验原始配置文本(启动读文件与网页编辑共用)。configPath 仅记录来源,可为空。 +function parseAndValidate(rawText, configPath) { + const raw = YAML.parse(rawText || '') || {}; const cfg = deepMerge(DEFAULTS, raw); if (!Array.isArray(cfg.models) || cfg.models.length === 0) { @@ -106,8 +103,50 @@ function loadConfig(configPath) { s.headers = headers; } - cfg.config_path = p; + cfg.config_path = configPath || ''; return cfg; } -module.exports = { loadConfig }; +function loadConfig(configPath) { + const p = configPath || process.env.AGENT_CONFIG || path.join(__dirname, '..', 'config.yml'); + const text = fs.existsSync(p) ? fs.readFileSync(p, 'utf8') : ''; + return parseAndValidate(text, p); +} + +// 从文本校验一份配置(页面编辑试运行 / 写盘前校验),返回完整生效 cfg。 +function loadConfigFromText(text, configPath) { + return parseAndValidate(text, configPath || ''); +} + +// ---------- 配置读写(2.0:设置页管理 config.yml;无热重载,写盘后重启生效) ---------- +// 读当前配置文件原文(保留注释);文件不存在返回 null。 +function readConfigText(cfg) { + const p = cfg && cfg.config_path; + if (!p || !fs.existsSync(p)) return null; + return fs.readFileSync(p, 'utf8'); +} + +// 单项编辑:[{path:[...], value}] 或 [{path:[...], delete:true}]。 +// 用 YAML Document 写回,保留注释与排版;路径段为纯数字时按数组下标处理(如 ["models",0,"url"])。 +function applyEditsToText(text, edits) { + const doc = YAML.parseDocument(text || ''); + for (const e of edits || []) { + if (!e || !Array.isArray(e.path) || !e.path.length) throw new Error('配置项 path 必须是非空数组'); + const docPath = e.path.map(s => (typeof s === 'string' && /^\d+$/.test(s)) ? Number(s) : s); + if (e.delete) doc.deleteIn(docPath); + else doc.setIn(docPath, e.value); + } + return doc.toString(); +} + +// 校验通过才写盘(不合法直接抛错、不动文件)。 +// 注意:不用「临时文件 + rename」原子写:容器里 config.yml 是单文件 bind mount, +// rename 替换该路径会 EBUSY;写坏风险已由“先校验后写”兑底。 +function writeConfigText(cfg, text) { + const p = cfg && cfg.config_path; + if (!p) throw Object.assign(new Error('配置文件路径未知(config.yml 可能不存在),无法写入'), { status_code: 409 }); + parseAndValidate(text, p); + fs.writeFileSync(p, text, 'utf8'); +} + +module.exports = { loadConfig, loadConfigFromText, readConfigText, applyEditsToText, writeConfigText }; diff --git a/app/server/engine.js b/app/server/engine.js index f7da323..6a786d3 100644 --- a/app/server/engine.js +++ b/app/server/engine.js @@ -256,6 +256,14 @@ function getRun(sessionId, create) { return run; } +// 正在执行 AI 回合(looping)的会话 id 列表:供重启保护判断。 +// 注意只算「回合执行中」;active 但空闲(等用户输入)与 held 都不算,重启无损失。 +function activeRuns() { + const out = []; + for (const [id, run] of runs) if (run && run.looping) out.push(id); + return out; +} + function killRunProcs(run) { for (const proc of run.procs) { try { process.kill(-proc.pid, 'SIGKILL'); } catch (_) { @@ -549,6 +557,7 @@ module.exports = { getDb: () => db, getBus: () => bus, getConfig: () => cfg, + activeRuns, createSession, postUserMessage, startExecution, diff --git a/app/server/public/css/style.css b/app/server/public/css/style.css index 7b9dbb5..6436b5d 100644 --- a/app/server/public/css/style.css +++ b/app/server/public/css/style.css @@ -35,11 +35,9 @@ body { display: inline-flex; align-items: center; gap: 8px; - cursor: pointer; - /* 可选:增加内边距扩大点击热区,但不会扩展到空白区域 */ + cursor: default; padding: 4px 8px; border-radius: 6px; - transition: background 0.2s; } .chat-title { flex: 1; text-align: center; font-size: 16px; font-weight: 600; diff --git a/app/server/public/index.html b/app/server/public/index.html index 71cd4b4..92f77b6 100644 --- a/app/server/public/index.html +++ b/app/server/public/index.html @@ -16,7 +16,7 @@