From 242bcf7c2996f1ad36db2050784de7b4f6a9b1e1 Mon Sep 17 00:00:00 2001 From: Gaofeng Date: Fri, 28 Nov 2025 02:42:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=B9=E5=90=8E=E7=AB=AF=20backend/open=5Fwe?= =?UTF-8?q?bui/routers/chats.py:delete=5Fchat=5Fby=5Fid=20=E8=BF=9B?= =?UTF-8?q?=E8=A1=8C=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/open_webui/routers/chats.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/backend/open_webui/routers/chats.py b/backend/open_webui/routers/chats.py index 628fab693c..dd6900fd90 100644 --- a/backend/open_webui/routers/chats.py +++ b/backend/open_webui/routers/chats.py @@ -631,18 +631,41 @@ async def send_chat_message_event_by_id( ############################ + @router.delete("/{id}", response_model=bool) async def delete_chat_by_id(request: Request, id: str, user=Depends(get_verified_user)): + """ + 删除聊天记录 - 支持管理员和用户删除,自动清理关联资源 + + 功能: + 1. 权限验证(管理员可删除任意聊天,普通用户仅限自己的) + 2. 清理 Mem0 记忆条目(删除该聊天窗口的所有记忆) + 3. 清理孤立标签(仅被该聊天使用的标签) + 4. 删除聊天记录 + + Args: + request: FastAPI 请求对象 + id: 聊天记录 ID + user: 当前用户 + + Returns: + bool: 删除成功返回 True + """ + # === 管理员分支 === if user.role == "admin": chat = Chats.get_chat_by_id(id) + + # 清理孤立标签(仅被该聊天使用的标签) for tag in chat.meta.get("tags", []): if Chats.count_chats_by_tag_name_and_user_id(tag, user.id) == 1: Tags.delete_tag_by_name_and_user_id(tag, user.id) result = Chats.delete_chat_by_id(id) - return result + + # === 普通用户分支 === else: + # 权限检查 if not has_permission( user.id, "chat.delete", request.app.state.config.USER_PERMISSIONS ): @@ -652,14 +675,18 @@ async def delete_chat_by_id(request: Request, id: str, user=Depends(get_verified ) chat = Chats.get_chat_by_id(id) + + # 清理孤立标签 for tag in chat.meta.get("tags", []): if Chats.count_chats_by_tag_name_and_user_id(tag, user.id) == 1: Tags.delete_tag_by_name_and_user_id(tag, user.id) + # 删除聊天(带用户 ID 校验) result = Chats.delete_chat_by_id_and_user_id(id, user.id) return result + ############################ # GetPinnedStatusById ############################