工具/任务执行卡片支持删除:单删 tool 回执同步摘除父 assistant 的对应调用
- deleteToolMessage:删工具回执行后,把该 tool_call 从父 assistant 的 tool_calls 里摘除;纯工具轮(无文本)且无剩余调用则父行整删,避免「有调用无结果」被上游拒收 - tool_result 事件先落库再广播,带 msg_id;前端工具卡时间行(复制旁)挂 × 删除按钮,历史与实时均可删 - 删除走统一 .act-del 委托:confirm → DELETE 接口 → 重拉详情重建消息区
This commit is contained in:
+35
-6
@@ -540,11 +540,12 @@ async function runLoopInner(sessionId, run) {
|
||||
{ type: 'image_url', image_url: { url: imageDataUrl } },
|
||||
])
|
||||
: outText;
|
||||
// 先落库再广播:回执带上消息行 id(msg_id),前端工具卡据此挂「删除本条」按钮
|
||||
const toolMsgId = saveMessage(sessionId, { role: 'tool', tool_call_id: id, name: tc.function.name, content: toolContent });
|
||||
bus.emit(sessionId, 'tool_result', {
|
||||
tool_call_id: id, name: tc.function.name, content: outText.slice(0, 20000),
|
||||
is_error: isError, image_url: imageDataUrl || undefined,
|
||||
is_error: isError, image_url: imageDataUrl || undefined, msg_id: toolMsgId,
|
||||
});
|
||||
saveMessage(sessionId, { role: 'tool', tool_call_id: id, name: tc.function.name, content: toolContent });
|
||||
}
|
||||
iter++;
|
||||
}
|
||||
@@ -900,16 +901,16 @@ function clearSessionMessages(sessionId) {
|
||||
return { ok: true };
|
||||
}
|
||||
|
||||
// 删除单条消息:消息行直接从库里消失,下次模型请求的历史里就没有它了(buildMessages 只看库)。
|
||||
// 删除单条消息(含工具/任务消息):消息行直接从库里消失,下次模型请求的历史里就没有它了(buildMessages 只看库)。
|
||||
// assistant 带 tool_calls 时连带删掉该轮的 tool 回执:只删 assistant 会留下无 tool_call 配对的
|
||||
// 孤儿 tool 行,上游会拒收整个请求。tool 回执不允许单独删:删了它其父 assistant 的
|
||||
// tool_calls 就没有回执,同样破坏协议;要删请删对应的 assistant 消息整轮。
|
||||
// 孤儿 tool 行,上游会拒收整个请求。单删 tool 回执则反向配对:把它在父 assistant 的
|
||||
// tool_calls 里同步摘除(见 deleteToolMessage),保证「调用↔回执」始终成对。
|
||||
function deleteMessage(sessionId, messageId) {
|
||||
const session = getSession(sessionId);
|
||||
if (!session) throw Object.assign(new Error('会话不存在'), { status_code: 404 });
|
||||
const row = db.prepare('SELECT * FROM messages WHERE id = ? AND session_id = ?').get(messageId, sessionId);
|
||||
if (!row) throw Object.assign(new Error('消息不存在'), { status_code: 404 });
|
||||
if (row.role === 'tool') throw Object.assign(new Error('工具回执不能单独删除,请删它对应的 AI 消息'), { status_code: 400 });
|
||||
if (row.role === 'tool') return deleteToolMessage(sessionId, row);
|
||||
let removed = 0;
|
||||
if (row.tool_calls) {
|
||||
let calls = [];
|
||||
@@ -924,6 +925,34 @@ function deleteMessage(sessionId, messageId) {
|
||||
return { ok: true, id: messageId, removed: removed + 1 };
|
||||
}
|
||||
|
||||
// 删工具回执(任务执行卡片):同步把对应 tool_call 从父 assistant 的 tool_calls 里摘掉,
|
||||
// 避免留下「有调用无结果」被严格上游拒收。父回合若是纯工具调用轮(无文本)且已无
|
||||
// 其余调用,则整行一起删(空气泡没有意义);有文本则保留为普通文本回复。
|
||||
// tool_call_id 全局唯一,LIKE 只是初筛,命中后按 JSON 精确确认。
|
||||
function deleteToolMessage(sessionId, row) {
|
||||
db.prepare('DELETE FROM messages WHERE id = ?').run(row.id);
|
||||
let removed = 1;
|
||||
const parents = db.prepare(
|
||||
"SELECT id, content, tool_calls FROM messages WHERE session_id = ? AND role = 'assistant' AND tool_calls LIKE ? ORDER BY id DESC"
|
||||
).all(sessionId, '%' + row.tool_call_id + '%');
|
||||
for (const p of parents) {
|
||||
let calls = [];
|
||||
try { calls = JSON.parse(p.tool_calls) || []; } catch (_) { continue; }
|
||||
const left = calls.filter(c => c && c.id !== row.tool_call_id);
|
||||
if (left.length === calls.length) continue; // LIKE 误中,不是这条
|
||||
if (!left.length && !(p.content || '').trim()) {
|
||||
db.prepare('DELETE FROM messages WHERE id = ?').run(p.id);
|
||||
removed++;
|
||||
} else if (!left.length) {
|
||||
db.prepare('UPDATE messages SET tool_calls = NULL WHERE id = ?').run(p.id);
|
||||
} else {
|
||||
db.prepare('UPDATE messages SET tool_calls = ? WHERE id = ?').run(JSON.stringify(left), p.id);
|
||||
}
|
||||
break; // id 唯一,命中即止
|
||||
}
|
||||
return { ok: true, id: row.id, removed };
|
||||
}
|
||||
|
||||
function setModelPool(cfg_, db_, bus_) { cfg = cfg_; db = db_; bus = bus_; }
|
||||
|
||||
module.exports = {
|
||||
|
||||
@@ -257,7 +257,9 @@ main { flex: 1; min-height: 0; display: flex; flex-direction: column; }
|
||||
.msg-actions .act.copied { color: var(--active); }
|
||||
/* 删除本条:悬停变红点明破坏性;禁用态(请求中)半透且不响应 */
|
||||
.msg-actions .act-del:hover { color: #c0392b; }
|
||||
.msg-actions .act-del[disabled] { opacity: .4; pointer-events: none; }
|
||||
.act-del[disabled] { opacity: .4; pointer-events: none; }
|
||||
/* 工具卡时间行的删除按钮:同样红示警 */
|
||||
.tool-card .tool-time .act-del:hover { color: #c0392b; }
|
||||
|
||||
/* ================= 底部输入区 ================= */
|
||||
footer { flex: none; padding: 8px 12px calc(10px + env(safe-area-inset-bottom, 0px)); background: var(--page); }
|
||||
|
||||
@@ -287,7 +287,7 @@
|
||||
* POST /api/sessions/:id/stop | /release 状态栏「终止/断开」
|
||||
* POST /api/sessions/:id/bind {model_id} 状态栏「选择模型」(已完成/已终止会话)
|
||||
* DELETE /api/sessions/:id/messages 状态栏「清空」(会话内删除已移除,清空由这里承担)
|
||||
* DELETE /api/sessions/:id/messages/:messageId 删除单条消息(assistant 轮连带其工具回执)
|
||||
* DELETE /api/sessions/:id/messages/:messageId 删除单条消息/工具卡(删 assistant 连带工具回执;单删工具卡同步摘除父调用)
|
||||
* GET /api/models 状态栏「选择模型」弹窗模型列表
|
||||
*
|
||||
* 设置区(侧边栏用户区整行点击进入;页面搬自 index.html,未新增任何接口):
|
||||
@@ -526,6 +526,13 @@
|
||||
return '<button class="act act-copy" title="复制工具输出" data-b64="' + b64 + '">' +
|
||||
'<svg class="ic sm" 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></svg></button>';
|
||||
}
|
||||
// 工具卡「删除本条」:删的是 tool 消息行;后端会把它在父 assistant 里的调用同步摘除。
|
||||
// msgId = tool 消息的库行 id(历史渲染传 m.id;实时由 tool_result 事件的 msg_id 携带)
|
||||
function toolDelBtnHtml(msgId) {
|
||||
if (!msgId) return '';
|
||||
return '<button class="act act-del" title="删除本条" data-msg-id="' + msgId + '">' +
|
||||
'<svg class="ic sm" viewBox="0 0 24 24"><path d="M18 6 6 18"></path><path d="m6 6 12 12"></path></svg></button>';
|
||||
}
|
||||
function renderToolCard(id, name, argsStr, createdAt) {
|
||||
const div = document.createElement('div');
|
||||
div.className = 'tool-card';
|
||||
@@ -554,7 +561,7 @@
|
||||
}
|
||||
return { text: content, imageUrl: null };
|
||||
}
|
||||
function fillToolResult(id, content, isError, liveImageUrl) {
|
||||
function fillToolResult(id, content, isError, liveImageUrl, msgId) {
|
||||
const card = document.getElementById('tool-' + id);
|
||||
if (!card) return;
|
||||
if (isError) card.classList.add('error');
|
||||
@@ -563,10 +570,16 @@
|
||||
const pre = card.querySelector('.tool-result');
|
||||
if (!pre) return;
|
||||
pre.textContent = parsed.text || '(空)';
|
||||
// 结果就绪后在卡片时间行补复制按钮(复制工具输出)
|
||||
// 结果就绪后在卡片时间行补复制/删除按钮(删除走统一的 .act-del 委托)
|
||||
const timeRow = card.querySelector('.tool-time');
|
||||
if (timeRow && !timeRow.querySelector('.act-copy')) {
|
||||
timeRow.insertAdjacentHTML('beforeend', toolCopyBtnHtml(parsed.text || ''));
|
||||
if (timeRow) {
|
||||
if (msgId) card.dataset.msgId = msgId; // 记住 tool 消息行 id,多次回填不丢
|
||||
if (!timeRow.querySelector('.act-copy')) {
|
||||
timeRow.insertAdjacentHTML('beforeend', toolCopyBtnHtml(parsed.text || ''));
|
||||
}
|
||||
if (!timeRow.querySelector('.act-del')) {
|
||||
timeRow.insertAdjacentHTML('beforeend', toolDelBtnHtml(card.dataset.msgId));
|
||||
}
|
||||
}
|
||||
const img = liveImageUrl || parsed.imageUrl;
|
||||
if (img && !card.querySelector('.tool-img')) {
|
||||
@@ -613,7 +626,7 @@
|
||||
const node = msgNode(m);
|
||||
if (node) box.insertAdjacentHTML('beforeend', node);
|
||||
if (m.role === 'assistant') appendToolCardsFromAssistant(m, box);
|
||||
else if (m.role === 'tool' && m.tool_call_id) fillToolResult(m.tool_call_id, m.content, false);
|
||||
else if (m.role === 'tool' && m.tool_call_id) fillToolResult(m.tool_call_id, m.content, false, undefined, m.id);
|
||||
}
|
||||
if (!(detail.messages || []).length) {
|
||||
box.innerHTML = '<div class="drawer-empty">描述你的需求,AI 会和你讨论并直接干活。</div>';
|
||||
@@ -1136,7 +1149,7 @@
|
||||
});
|
||||
es.addEventListener('tool_result', ev => {
|
||||
const d = JSON.parse(ev.data);
|
||||
fillToolResult(d.tool_call_id, d.content, d.is_error, d.image_url);
|
||||
fillToolResult(d.tool_call_id, d.content, d.is_error, d.image_url, d.msg_id);
|
||||
maybeAutoScroll();
|
||||
});
|
||||
es.addEventListener('done', ev => {
|
||||
|
||||
@@ -70,7 +70,8 @@ router.delete('/:id/messages', wrap((req, res) => {
|
||||
res.json(r);
|
||||
}));
|
||||
|
||||
// 删除单条消息:assistant 的 tool_calls 轮连带删除其 tool 回执(见 engine.deleteMessage)。
|
||||
// 删除单条消息(含工具/任务消息):删 assistant 轮连带其 tool 回执;单删 tool 回执会把该调用
|
||||
// 从父 assistant 的 tool_calls 里同步摘除(见 engine.deleteMessage),保证调用↔回执成对。
|
||||
// 库中没了,后续发给模型的消息里自然也没有;执行中删除会在下一轮组装历史时生效。
|
||||
router.delete('/:id/messages/:messageId', wrap((req, res) => {
|
||||
const r = engine.deleteMessage(req.params.id, Number(req.params.messageId));
|
||||
|
||||
Reference in New Issue
Block a user