From e3d436714e975bdc06f23a0e55f6e719696dde8b Mon Sep 17 00:00:00 2001 From: kicer Date: Mon, 23 Nov 2026 09:09:35 +0800 Subject: [PATCH] =?UTF-8?q?feat(chat):=20=E7=94=A8=E6=88=B7=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E5=8F=AF=E5=88=86=E5=8F=89=E5=88=B0=E6=96=B0=E4=BC=9A?= =?UTF-8?q?=E8=AF=9D=EF=BC=8C=E5=A4=8D=E5=88=B6=E8=AF=A5=E7=82=B9=E4=B9=8B?= =?UTF-8?q?=E5=89=8D=E7=9A=84=E5=8E=86=E5=8F=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 改一个项目时经常想从某个点另起一支:在原会话继续会污染上下文,新开一个又得从头 讲一遍背景。分叉 = 新建会话 + 把分叉点之前的历史原样复制过去,模型看到的就是 「分叉点之前的全部内容 + 你新写的话」,另一支的回复完全不在。 - engine.forkSession:只从 role='user' 分叉,落在 archived 段里的拒绝(复制过去模型 也看不到);新会话 status='completed' 不占模型槽,继承 work_dir / model_id / last_model_id / assistant_type / user / deep_think,标题加「(分叉)」后缀。 消息按 INSERT ... SELECT 复制,tool_calls 与 tool_call_id 原样带走,配对不变; created_at 保留原值,时间线不乱。profile.freezeSnapshot 也做一份,否则新会话 首轮没有画像注入。 - POST /api/sessions/:id/fork 返回 { id, copied, prefill }。 - 前端用户消息操作行加 git-branch 图标(铅笔和删除之间),点击后跳新会话并把该条 原文回填输入框、聚焦,改完直接发。 --- app/server/engine.js | 35 ++++++++++++++++++++++++++++++++++ app/server/public/index.html | 1 + app/server/public/js/chat.js | 7 +++++++ app/server/public/js/stream.js | 28 ++++++++++++++++++++++++++- app/server/routes/sessions.js | 6 ++++++ 5 files changed, 76 insertions(+), 1 deletion(-) diff --git a/app/server/engine.js b/app/server/engine.js index 7754d52..041e85d 100644 --- a/app/server/engine.js +++ b/app/server/engine.js @@ -1348,6 +1348,40 @@ function sleep(ms) { return new Promise(r => setTimeout(r, ms)); } // ---------- 对外 API ---------- // 新建即绑模(无排队):指定了优先模型(welcome 预选)就取其平台「最不 active」的 key; // 模型无效或平台无槽位退回全局优先级选。池完全为空直接报错(配置问题),不建吊儿郎当的会话。 +// 从某条用户消息分叉:新建会话,把该消息之前的历史原样复制过去,正文回填给前端。 +// 只复制不重跑——新会话拿到的是分叉点之前的上下文,分叉点及之后的东西模型一点看不到。 +// 标题保持源会话不变(用户要求);work_dir/模型/assistant_type 一并继承。 +// 新会话 status='completed' 不占模型槽,用户发第一条消息时再走正常的激活/分配流程。 +function forkSession(sessionId, messageId, user) { + const src = getSession(sessionId); + if (!src) throw Object.assign(new Error('会话不存在'), { status_code: 404 }); + const point = db.prepare('SELECT id, role, content, type FROM messages WHERE id = ? AND session_id = ?') + .get(messageId, sessionId); + if (!point) throw Object.assign(new Error('消息不存在'), { status_code: 404 }); + if (point.role !== 'user') throw Object.assign(new Error('只能从用户消息分叉'), { status_code: 400 }); + if (point.type === 'archived') { + throw Object.assign(new Error('这条消息在存档段里,分叉过去模型也看不到'), { status_code: 400 }); + } + const id = genTaskId(); + const ts = now(); + // 标题带「(分叉)」后缀:列表里两个同名会话分不清;源标题还没生成(空)时保持空,后续照常自动生成 + const title = src.title ? src.title + '(分叉)' : ''; + db.prepare( + "INSERT INTO sessions (id, title, status, model_id, model_slot, last_model_id, held_at, assistant_type, user, work_dir, created_at, updated_at, deep_think) VALUES (?,?,'completed',?,?,?,?,?,?,?,?,?,?)" + ).run(id, title, src.model_id || '', null, src.last_model_id || src.model_id || '', + null, src.assistant_type || 'secretary', src.user || user || '', src.work_dir || '', ts, ts, + src.deep_think ? 1 : 0); // 思考开关随会话走:分叉后新会话保持源会话的思考状态 + profile.freezeSnapshot(db, id); // 画像快照随会话冻结:分叉也要一份,否则新会话首轮没有画像 + // tool_calls / tool_call_id 原样复制,配对关系不变;tool_call_msg_ids 由 GET /sessions/:id 现算 + const r = db.prepare( + 'INSERT INTO messages (session_id, role, content, reasoning, tool_calls, tool_call_id, name, type, is_error, usage, created_at) ' + + 'SELECT ?, role, content, reasoning, tool_calls, tool_call_id, name, type, is_error, usage, created_at ' + + 'FROM messages WHERE session_id = ? AND id < ? ORDER BY id' + ).run(id, sessionId, point.id); + log.info('task', sessionId + ' 分叉 → ' + id + '(复制 ' + r.changes + ' 条,分叉点 #' + point.id + ')'); + return { id, copied: r.changes, prefill: point.content || '' }; +} + function createSession(title, preferModelId, assistantType, user, workDir) { // 用户锁定模型(auth.users[].model)优先于前端预选;锁定值无效/被禁用时退回原逻辑(用前端预选或全局自动选择)。 const lock = lockedModelOf(user); @@ -2061,6 +2095,7 @@ module.exports = { getConfig: () => cfg, activeRuns, createSession, + forkSession, postUserMessage, enqueueMessage, promotePending, diff --git a/app/server/public/index.html b/app/server/public/index.html index 3730a7c..d5e1b8a 100644 --- a/app/server/public/index.html +++ b/app/server/public/index.html @@ -139,6 +139,7 @@ + diff --git a/app/server/public/js/chat.js b/app/server/public/js/chat.js index 0190569..0b624ce 100644 --- a/app/server/public/js/chat.js +++ b/app/server/public/js/chat.js @@ -50,6 +50,12 @@ const usageHtml = u ? '' + usageBrief(u) + '' : ''; + // 分叉:只给用户消息(它才是分支点),复制该点之前的历史到新会话,正文回填输入框 + const fork = canEdit && msgId + ? '' + : ''; return '
' + '' + fmtTime(ts) + '' + usageHtml + @@ -57,6 +63,7 @@ '' + '' + edit + + fork + del + '
'; } diff --git a/app/server/public/js/stream.js b/app/server/public/js/stream.js index f3b779e..e88f0dc 100644 --- a/app/server/public/js/stream.js +++ b/app/server/public/js/stream.js @@ -682,7 +682,33 @@ const pre = card.querySelector('.tool-result'); if (pre) pre.textContent = content; } - $('chat-view').addEventListener('click', e => { + $('chat-view').addEventListener('click', async e => { + // 分叉:从这条用户消息新建会话,复制该点之前的历史,正文回填输入框 + const forkBtn = e.target.closest('.act-fork'); + if (forkBtn) { + const msgId = Number(forkBtn.dataset.msgId); + if (!msgId || !state.sessionId || forkBtn.disabled) return; + forkBtn.disabled = true; + try { + const r = await req('POST', '/api/sessions/' + state.sessionId + '/fork', { message_id: msgId }); + if (!r || !r.id) return; + toast('已分叉到新会话(复制 ' + r.copied + ' 条)'); + await openSession(r.id); + const ta = $('msg-input'); + if (ta && r.prefill) { + ta.value = r.prefill; + autosize(); + updateSendBtn(); + ta.focus(); + } + loadSessionList(); + } catch (err) { + toast('分叉失败:' + err.message); + } finally { + forkBtn.disabled = false; + } + return; + } // 工具卡:改的是 tool 消息行的 content(调试上下文用) const toolBtn = e.target.closest('.act-edit-tool'); if (toolBtn) { diff --git a/app/server/routes/sessions.js b/app/server/routes/sessions.js index be66b5c..65b69e0 100644 --- a/app/server/routes/sessions.js +++ b/app/server/routes/sessions.js @@ -231,6 +231,12 @@ router.post('/:id/bind', wrap((req, res) => { res.json(engine.bindToSession(req.params.id, mid)); })); +router.post('/:id/fork', wrap((req, res) => { + const r = engine.forkSession(req.params.id, Number(req.body && req.body.message_id), req.user); + log.info('task', (req.user || '-') + ' ' + req.params.id + ' 分叉 → ' + r.id + '(复制 ' + r.copied + ' 条)'); + res.json(r); +})); + router.patch('/:id', wrap((req, res) => { const b = req.body || {}; // work_dir 只改指向,不搬目录、不重跑;改名照旧走 title