Timothy Jaeryang Baek 4 月之前
父節點
當前提交
947f5600d6
共有 1 個文件被更改,包括 42 次插入37 次删除
  1. 42 37
      backend/open_webui/utils/middleware.py

+ 42 - 37
backend/open_webui/utils/middleware.py

@@ -23,7 +23,7 @@ from open_webui.models.users import Users
 from open_webui.socket.main import (
 from open_webui.socket.main import (
     get_event_call,
     get_event_call,
     get_event_emitter,
     get_event_emitter,
-    get_user_id_from_session_pool,
+    get_active_status_by_user_id,
 )
 )
 from open_webui.routers.tasks import (
 from open_webui.routers.tasks import (
     generate_queries,
     generate_queries,
@@ -896,7 +896,7 @@ async def process_chat_response(
                     )
                     )
 
 
                     # Send a webhook notification if the user is not active
                     # Send a webhook notification if the user is not active
-                    if get_user_id_from_session_pool(metadata["session_id"]) is None:
+                    if get_active_status_by_user_id(user.id) is None:
                         webhook_url = Users.get_user_webhook_url_by_id(user.id)
                         webhook_url = Users.get_user_webhook_url_by_id(user.id)
                         if webhook_url:
                         if webhook_url:
                             post_webhook(
                             post_webhook(
@@ -1002,51 +1002,56 @@ async def process_chat_response(
                                         "content": content,
                                         "content": content,
                                     }
                                     }
 
 
+                        await event_emitter(
+                            {
+                                "type": "chat:completion",
+                                "data": data,
+                            }
+                        )
+
                     except Exception as e:
                     except Exception as e:
                         done = "data: [DONE]" in line
                         done = "data: [DONE]" in line
-                        title = Chats.get_chat_title_by_id(metadata["chat_id"])
 
 
                         if done:
                         if done:
-                            data = {"done": True, "content": content, "title": title}
-
-                            if not ENABLE_REALTIME_CHAT_SAVE:
-                                # Save message in the database
-                                Chats.upsert_message_to_chat_by_id_and_message_id(
-                                    metadata["chat_id"],
-                                    metadata["message_id"],
-                                    {
-                                        "content": content,
-                                    },
-                                )
-
-                            # Send a webhook notification if the user is not active
-                            if (
-                                get_user_id_from_session_pool(metadata["session_id"])
-                                is None
-                            ):
-                                webhook_url = Users.get_user_webhook_url_by_id(user.id)
-                                if webhook_url:
-                                    post_webhook(
-                                        webhook_url,
-                                        f"{title} - {request.app.state.config.WEBUI_URL}/c/{metadata['chat_id']}\n\n{content}",
-                                        {
-                                            "action": "chat",
-                                            "message": content,
-                                            "title": title,
-                                            "url": f"{request.app.state.config.WEBUI_URL}/c/{metadata['chat_id']}",
-                                        },
-                                    )
-
+                            pass
                         else:
                         else:
                             continue
                             continue
 
 
-                    await event_emitter(
+                title = Chats.get_chat_title_by_id(metadata["chat_id"])
+                data = {"done": True, "content": content, "title": title}
+
+                if not ENABLE_REALTIME_CHAT_SAVE:
+                    # Save message in the database
+                    Chats.upsert_message_to_chat_by_id_and_message_id(
+                        metadata["chat_id"],
+                        metadata["message_id"],
                         {
                         {
-                            "type": "chat:completion",
-                            "data": data,
-                        }
+                            "content": content,
+                        },
                     )
                     )
 
 
+                # Send a webhook notification if the user is not active
+                if get_active_status_by_user_id(user.id) is None:
+                    webhook_url = Users.get_user_webhook_url_by_id(user.id)
+                    if webhook_url:
+                        post_webhook(
+                            webhook_url,
+                            f"{title} - {request.app.state.config.WEBUI_URL}/c/{metadata['chat_id']}\n\n{content}",
+                            {
+                                "action": "chat",
+                                "message": content,
+                                "title": title,
+                                "url": f"{request.app.state.config.WEBUI_URL}/c/{metadata['chat_id']}",
+                            },
+                        )
+
+                await event_emitter(
+                    {
+                        "type": "chat:completion",
+                        "data": data,
+                    }
+                )
+
                 await background_tasks_handler()
                 await background_tasks_handler()
             except asyncio.CancelledError:
             except asyncio.CancelledError:
                 print("Task was cancelled!")
                 print("Task was cancelled!")