feat(chat): 用户消息可分叉到新会话,复制该点之前的历史

改一个项目时经常想从某个点另起一支:在原会话继续会污染上下文,新开一个又得从头
讲一遍背景。分叉 = 新建会话 + 把分叉点之前的历史原样复制过去,模型看到的就是
「分叉点之前的全部内容 + 你新写的话」,另一支的回复完全不在。

- 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 图标(铅笔和删除之间),点击后跳新会话并把该条
  原文回填输入框、聚焦,改完直接发。
This commit is contained in:
2026-11-23 09:09:35 +08:00
parent f0eb71d9ed
commit e3d436714e
5 changed files with 76 additions and 1 deletions
+1
View File
@@ -139,6 +139,7 @@
<symbol id="i-copy" viewBox="0 0 24 24"><rect x="9" y="9" width="13" height="13" rx="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></symbol>
<symbol id="i-x-path" viewBox="0 0 24 24"><path d="M18 6 6 18"></path><path d="m6 6 12 12"></path></symbol>
<symbol id="i-pencil" viewBox="0 0 24 24"><path d="M12 20h9"></path><path d="M16.5 3.5a2.1 2.1 0 0 1 3 3L7 19l-4 1 1-4z"></path></symbol>
<symbol id="i-fork" viewBox="0 0 24 24"><line x1="6" y1="3" x2="6" y2="15"></line><circle cx="18" cy="6" r="3"></circle><circle cx="6" cy="18" r="3"></circle><path d="M18 9a9 9 0 0 1-9 9"></path></symbol>
<symbol id="i-gem" viewBox="0 0 24 24"><path d="M6 3h12l4 6-10 13L2 9z"></path><polyline points="2 9 22 9"></polyline><polyline points="10 3 8 9 12 22"></polyline><polyline points="14 3 16 9 12 22"></polyline></symbol>
<symbol id="i-image" viewBox="0 0 24 24"><rect x="3" y="3" width="18" height="18" rx="2"></rect><circle cx="8.5" cy="8.5" r="1.5"></circle><polyline points="21 15 16 10 5 21"></polyline></symbol>
<symbol id="i-file" viewBox="0 0 24 24"><path d="M6 3h8l4 4v14H6z"/><path d="M14 3v4h4"/></symbol>
+7
View File
@@ -50,6 +50,12 @@
const usageHtml = u
? '<span class="act-usage" title="' + usageTip(u) + '">' + usageBrief(u) + '</span>'
: '';
// 分叉:只给用户消息(它才是分支点),复制该点之前的历史到新会话,正文回填输入框
const fork = canEdit && msgId
? '<button class="act act-fork" title="从这里分叉:复制该点之前的历史到新会话" data-msg-id="' + msgId + '">' +
'<svg class="ic sm" viewBox="0 0 24 24"><use href="#i-fork"/></svg>' +
'</button>'
: '';
return '<div class="msg-actions">' +
'<span>' + fmtTime(ts) + '</span>' +
usageHtml +
@@ -57,6 +63,7 @@
'<svg class="ic sm" viewBox="0 0 24 24"><use href="#i-copy"/></svg>' +
'</button>' +
edit +
fork +
del +
'</div>';
}
+27 -1
View File
@@ -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) {