Browse Source

enh: save status from the backend

Timothy Jaeryang Baek 4 months ago
parent
commit
55da6224b8
2 changed files with 27 additions and 0 deletions
  1. 18 0
      backend/open_webui/models/chats.py
  2. 9 0
      backend/open_webui/socket/main.py

+ 18 - 0
backend/open_webui/models/chats.py

@@ -235,6 +235,24 @@ class ChatTable:
         chat["history"] = history
         chat["history"] = history
         return self.update_chat_by_id(id, chat)
         return self.update_chat_by_id(id, chat)
 
 
+    def add_message_status_to_chat_by_id_and_message_id(
+        self, id: str, message_id: str, status: dict
+    ) -> Optional[ChatModel]:
+        chat = self.get_chat_by_id(id)
+        if chat is None:
+            return None
+
+        chat = chat.chat
+        history = chat.get("history", {})
+
+        if message_id in history.get("messages", {}):
+            status_history = history["messages"][message_id].get("statusHistory", [])
+            status_history.append(status)
+            history["messages"][message_id]["statusHistory"] = status_history
+
+        chat["history"] = history
+        return self.update_chat_by_id(id, chat)
+
     def insert_shared_chat_by_chat_id(self, chat_id: str) -> Optional[ChatModel]:
     def insert_shared_chat_by_chat_id(self, chat_id: str) -> Optional[ChatModel]:
         with get_db() as db:
         with get_db() as db:
             # Get the existing chat to share
             # Get the existing chat to share

+ 9 - 0
backend/open_webui/socket/main.py

@@ -6,6 +6,8 @@ import time
 
 
 from open_webui.models.users import Users
 from open_webui.models.users import Users
 from open_webui.models.channels import Channels
 from open_webui.models.channels import Channels
+from open_webui.models.chats import Chats
+
 from open_webui.env import (
 from open_webui.env import (
     ENABLE_WEBSOCKET_SUPPORT,
     ENABLE_WEBSOCKET_SUPPORT,
     WEBSOCKET_MANAGER,
     WEBSOCKET_MANAGER,
@@ -259,6 +261,13 @@ def get_event_emitter(request_info):
                 to=session_id,
                 to=session_id,
             )
             )
 
 
+        if "type" in event_data and event_data["type"] == "status":
+            Chats.add_message_status_to_chat_by_id_and_message_id(
+                request_info["chat_id"],
+                request_info["message_id"],
+                event_data.get("data", {}),
+            )
+
     return __event_emitter__
     return __event_emitter__