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:
@@ -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,
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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>';
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user