From 55af39d5489ebb2b69624589a40280d575d5bedb Mon Sep 17 00:00:00 2001 From: kicer Date: Wed, 23 Sep 2026 11:00:17 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=9A=E8=AF=9D=E7=BA=A7profile=E7=BB=91?= =?UTF-8?q?=E5=AE=9A:sessions=E8=A1=A8=E5=8A=A0profile=E5=88=97(PRAGMA?= =?UTF-8?q?=E8=BF=81=E7=A7=BB,=E6=97=A7=E8=A1=8C=E7=A9=BA=E4=B8=B2=3D?= =?UTF-8?q?=E9=9A=8F=E6=A8=A1=E5=9E=8B);resolveProfileName=E4=BC=98?= =?UTF-8?q?=E5=85=88=E7=BA=A7=E6=8F=92=E5=B1=82=E2=80=94=E2=80=94=E6=AF=8F?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E5=8F=82=E6=95=B0>=E4=BC=9A=E8=AF=9D?= =?UTF-8?q?=E7=BB=91=E5=AE=9A>=E6=A8=A1=E5=9E=8B=E9=BB=98=E8=AE=A4>?= =?UTF-8?q?=E5=85=A8=E9=87=8F;bind=E8=B7=AF=E7=94=B1=E5=8F=8C=E8=AF=AD?= =?UTF-8?q?=E4=B9=89:profile=3D=E6=A8=A1=E5=BC=8F=E7=BB=91=E5=AE=9A?= =?UTF-8?q?=E4=BB=BB=E6=84=8F=E7=8A=B6=E6=80=81=E5=8D=B3=E5=88=87(''?= =?UTF-8?q?=E8=A7=A3=E7=BB=91/default=E5=BD=92=E4=B8=80,=E5=AD=98=E5=9C=A8?= =?UTF-8?q?=E6=80=A7400=E5=90=8Cmessages=E8=B7=AF=E7=94=B1),model=5Fid=3D?= =?UTF-8?q?=E5=8E=9F=E6=89=8B=E5=8A=A8=E5=8D=A0=E6=A8=A1=E5=9E=8B=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=E9=9B=B6=E6=94=B9=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/server/db.js | 6 ++++++ app/server/engine.js | 21 ++++++++++++++++++--- app/server/routes/sessions.js | 20 +++++++++++++++++++- 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/app/server/db.js b/app/server/db.js index f2e8b3b..3554403 100644 --- a/app/server/db.js +++ b/app/server/db.js @@ -19,6 +19,7 @@ function initDb(dbPath) { last_model_id TEXT, assistant_type TEXT DEFAULT 'secretary', user TEXT DEFAULT '', + profile TEXT DEFAULT '', created_at INTEGER NOT NULL, updated_at INTEGER NOT NULL, completed_at INTEGER, @@ -142,6 +143,11 @@ function initDb(dbPath) { if (!cols.some(c => c.name === 'user')) { db.exec("ALTER TABLE sessions ADD COLUMN user TEXT DEFAULT ''"); } + // 迁移:profile 记会话绑定的 prompt+tools 组合名(config.yml 顶层 model_profiles;''=随模型配置)。 + // 选取优先级见 engine.resolveProfileName,切换入口见 routes/sessions.js bind 路由(弹窗「模式」区)。 + if (!cols.some(c => c.name === 'profile')) { + db.exec("ALTER TABLE sessions ADD COLUMN profile TEXT DEFAULT ''"); + } return db; } diff --git a/app/server/engine.js b/app/server/engine.js index e115b4b..5b9b95f 100644 --- a/app/server/engine.js +++ b/app/server/engine.js @@ -241,12 +241,14 @@ function registerProfilePrompts() { setProfilePrompts(map); } -// 本轮生效的 profile 名:前端显式选择(run.profile,与 deepThink 同约定: -// 每轮迭代取最新值,回合中途切换下一个请求即生效)> 模型配置默认(cfg.models[].profile)> ''(默认全量)。 -// 保留值 'default' = 前端显式切回全量(覆盖配了 profile 的模型的默认组合)。 +// 本轮生效的 profile 名:每消息参数(run.profile,与 deepThink 同约定:每轮迭代取最新值) +// > 会话绑定(sessions.profile,弹窗「模式」区写入,跨回合/刷新持久)> 模型配置默认(cfg.models[].profile) +// > ''(默认全量)。保留值 'default' 归一为 ''(显式摆脱模型默认组合;无 profile 模型下二者等效)。 function resolveProfileName(session, run) { const manual = run && String(run.profile || '').trim(); if (manual) return manual === 'default' ? '' : manual; + const bound = String((session && session.profile) || '').trim(); + if (bound) return bound === 'default' ? '' : bound; const m = session.model_id && (cfg.models || []).find(x => x.id === session.model_id); return m && m.profile ? String(m.profile) : ''; } @@ -328,6 +330,18 @@ function bindToSession(sessionId, modelId) { return { ok: true, model_id: want }; } +// 会话级 profile 绑定(bind 弹窗「模式」区):''=解绑回随模型配置;保留值 'default' 归一为 ''。 +// 不设会话状态门槛——模式是每轮选择,切换下一轮迭代/下条消息即生效;不碰模型槽位占用。 +// 名字存在性由路由校验(hasModelProfile,与 messages 路由同款 400),这里只管落库。 +function setSessionProfile(sessionId, profile) { + const s = getSession(sessionId); + if (!s) throw Object.assign(new Error('会话不存在'), { status_code: 404 }); + const name = profile === 'default' ? '' : String(profile || '').trim(); + db.prepare('UPDATE sessions SET profile = ?, updated_at = ? WHERE id = ?').run(name, now(), sessionId); + log.info('model', sessionId + ' 会话模式绑定 → ' + (name || '默认(随模型配置)')); + return name; +} + // 是否还有待回应的新用户消息(最后一条非 tool 消息是 user)。 function hasPendingUser(sessionId) { const rows = loadHistory(sessionId); @@ -1235,6 +1249,7 @@ module.exports = { modelUsageStats, dispatchQueued, bindToSession, + setSessionProfile, TOOL_DEFINITIONS, effectiveTools, hasModelProfile, diff --git a/app/server/routes/sessions.js b/app/server/routes/sessions.js index b9e6fae..1b6c623 100644 --- a/app/server/routes/sessions.js +++ b/app/server/routes/sessions.js @@ -226,8 +226,26 @@ router.get('/:id', wrap((req, res) => { // 按需加载单条消息的完整内容(用于 tool 消息的懒加载) router.get('/:id/messages/:mid', wrap(getMessage)); +// 一路由双语义(弹窗内模型/模式两套选择共用):model_id=手动占模型(仅已结版/已终止,见 bindToSession); +// profile=会话模式绑定(任意状态,''解绑,'default' 归一为 '')。各传各的,同传先模式后模型。 router.post('/:id/bind', wrap((req, res) => { - res.json(engine.bindToSession(req.params.id, req.body && req.body.model_id)); + const body = req.body || {}; + let prof; + if (body.profile !== undefined && body.profile !== null) { + const p = String(body.profile).trim(); + if (p && p !== 'default' && !engine.hasModelProfile(p)) { + throw Object.assign(new Error('profile 不存在:' + p + '(可选:' + Object.keys(engine.getConfig().model_profiles || {}).join(', ') + ')'), { status_code: 400 }); + } + prof = engine.setSessionProfile(req.params.id, p); + } + const mid = body.model_id; + if (mid === undefined || mid === null || String(mid).trim() === '') { + if (prof === undefined) throw Object.assign(new Error('model_id / profile 至少传一项'), { status_code: 400 }); + return res.json({ ok: true, profile: prof }); + } + const r = engine.bindToSession(req.params.id, mid); + if (prof !== undefined) r.profile = prof; + res.json(r); })); router.patch('/:id', wrap((req, res) => {