feat: 删除提问时级联删除其回答,并移除回答上的删除按钮

- 删除按钮只保留在用户提问气泡上;AI 回答不再单独提供删除
- 后端 DELETE /messages/{id} 改为按整轮删除:从提问删到下一条提问之前,
  返回实际删除的 deleted_ids,保持 checkpoint 合法(无孤立工具结果/回答)
- 前端隐藏后端返回的整批 id,删除后即时消失
- 更新删除相关中英文案

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
吉鑫升andClaude Opus 4.8 committed 2026-06-04 14:34:52 +08:00
1 parent 5d12616211
commit 1f2b1e3b8a
7 files changed
+73 -46

No files matched your search

@@ -1012,13 +1012,16 @@ async def update_thread_state(thread_id: str, body: ThreadStateUpdateRequest, re
@router.delete("/{thread_id}/messages/{message_id}")
@require_permission("threads", "write", owner_check=True, require_existing=True)
async def delete_thread_message(thread_id: str, message_id: str, request: Request) -> dict[str, Any]:
"""Delete a single message (a user question or an assistant answer) from a
thread's latest checkpoint.
"""Delete a conversation turn from a thread's latest checkpoint.
Filters the message whose ``id`` matches ``message_id`` out of the
``messages`` channel and writes a fresh checkpoint. The remaining messages
keep their original ``BaseMessage`` objects (mirroring
:func:`update_thread_state`), so the graph can still resume the thread.
``message_id`` identifies a user question. The whole turn is removed: the
question plus every following message (its answer and any tool/intermediate
messages) up to — but not including — the next user question. Removing a
complete turn keeps the remaining checkpoint valid (no orphaned tool
results / dangling answers) and lets the graph still resume the thread.
If ``message_id`` points at a non-human message, only that single message is
removed.
"""
checkpointer = get_checkpointer(request)
@@ -1045,10 +1048,28 @@ async def delete_thread_message(thread_id: str, message_id: str, request: Reques
if not isinstance(messages, list):
raise HTTPException(status_code=404, detail="Thread has no messages")
filtered = [m for m in messages if _message_id(m) != message_id]
if len(filtered) == len(messages):
target_index = next(
(i for i, m in enumerate(messages) if _message_id(m) == message_id),
None,
)
if target_index is None:
raise HTTPException(status_code=404, detail=f"Message {message_id} not found")
# Delete the whole turn: from the question to just before the next question.
end = len(messages)
if _message_type(messages[target_index]) == "human":
for j in range(target_index + 1, len(messages)):
if _message_type(messages[j]) == "human":
end = j
break
else:
end = target_index + 1
deleted_ids = [
mid for m in messages[target_index:end] if (mid := _message_id(m))
]
filtered = messages[:target_index] + messages[end:]
channel_values["messages"] = filtered
checkpoint["channel_values"] = channel_values
metadata["updated_at"] = now_iso()
@@ -1070,11 +1091,12 @@ async def delete_thread_message(thread_id: str, message_id: str, request: Reques
raise HTTPException(status_code=500, detail="Failed to delete message")
logger.info(
"Deleted message %s from thread %s",
"Deleted turn starting at message %s (%d message(s)) from thread %s",
sanitize_log_param(message_id),
len(messages) - len(filtered),
sanitize_log_param(thread_id),
)
return {"ok": True, "deleted": message_id, "remaining": len(filtered)}
return {"ok": True, "deleted_ids": deleted_ids, "remaining": len(filtered)}
@router.post("/{thread_id}/history", response_model=list[HistoryEntry])