瀏覽代碼

fix: fillter exception handling

Timothy Jaeryang Baek 1 月之前
父節點
當前提交
f56889c5c7
共有 3 個文件被更改,包括 23 次插入9 次删除
  1. 13 6
      backend/open_webui/main.py
  2. 3 3
      backend/open_webui/utils/middleware.py
  3. 7 0
      src/lib/components/chat/Chat.svelte

+ 13 - 6
backend/open_webui/main.py

@@ -1519,7 +1519,7 @@ async def chat_completion(
             try:
                 event_emitter = get_event_emitter(metadata)
                 await event_emitter(
-                    {"type": "task-cancelled"},
+                    {"type": "chat:tasks:cancel"},
                 )
             except Exception as e:
                 pass
@@ -1535,14 +1535,21 @@ async def chat_completion(
                             "error": {"content": str(e)},
                         },
                     )
+
+                    event_emitter = get_event_emitter(metadata)
+                    await event_emitter(
+                        {
+                            "type": "chat:message:error",
+                            "data": {"error": {"content": str(e)}},
+                        }
+                    )
+                    await event_emitter(
+                        {"type": "chat:tasks:cancel"},
+                    )
+
                 except:
                     pass
 
-            raise HTTPException(
-                status_code=status.HTTP_400_BAD_REQUEST,
-                detail=str(e),
-            )
-
     if (
         metadata.get("session_id")
         and metadata.get("chat_id")

+ 3 - 3
backend/open_webui/utils/middleware.py

@@ -885,7 +885,7 @@ async def process_chat_payload(request, form_data, user, metadata, model):
             extra_params=extra_params,
         )
     except Exception as e:
-        raise Exception(f"Error: {e}")
+        raise Exception(f"{e}")
 
     features = form_data.pop("features", None)
     if features:
@@ -1316,7 +1316,7 @@ async def process_chat_response(
                             {
                                 "type": "chat:message:error",
                                 "data": {"error": {"content": error}},
-                            },
+                            }
                         )
 
                 if "selected_model_id" in response_data:
@@ -2624,7 +2624,7 @@ async def process_chat_response(
                 await background_tasks_handler()
             except asyncio.CancelledError:
                 log.warning("Task was cancelled!")
-                await event_emitter({"type": "task-cancelled"})
+                await event_emitter({"type": "chat:tasks:cancel"})
 
                 if not ENABLE_REALTIME_CHAT_SAVE:
                     # Save message in the database

+ 7 - 0
src/lib/components/chat/Chat.svelte

@@ -318,6 +318,13 @@
 					}
 				} else if (type === 'chat:completion') {
 					chatCompletionEventHandler(data, message, event.chat_id);
+				} else if (type === 'chat:tasks:cancel') {
+					taskIds = null;
+					const responseMessage = history.messages[history.currentId];
+					// Set all response messages to done
+					for (const messageId of history.messages[responseMessage.parentId].childrenIds) {
+						history.messages[messageId].done = true;
+					}
 				} else if (type === 'chat:message:delta' || type === 'message') {
 					message.content += data.content;
 				} else if (type === 'chat:message' || type === 'replace') {