From d484a2a99e3a0c21fdcad007a50ebc412fffbb2e Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Sun, 26 Jul 2026 21:07:20 -0400 Subject: [PATCH] refac --- backend/open_webui/main.py | 9 +++++++-- backend/open_webui/socket/main.py | 4 +++- backend/open_webui/tools/builtin.py | 13 +++++++------ backend/open_webui/utils/chat_id.py | 8 ++++++++ 4 files changed, 25 insertions(+), 9 deletions(-) create mode 100644 backend/open_webui/utils/chat_id.py diff --git a/backend/open_webui/main.py b/backend/open_webui/main.py index d364660b02..a45500a4e5 100644 --- a/backend/open_webui/main.py +++ b/backend/open_webui/main.py @@ -220,6 +220,7 @@ from open_webui.utils.chat import ( from open_webui.utils.chat import ( generate_chat_completion as chat_completion_handler, ) +from open_webui.utils.chat_id import is_saved_chat_id from open_webui.utils.chat_variables import ( normalize_chat_variables, ) @@ -264,7 +265,7 @@ log = logging.getLogger(__name__) async def emit_chat_list_event(metadata: dict, chat_id: str): - if not chat_id or chat_id.startswith(('channel:', 'local:')): + if not is_saved_chat_id(chat_id): return event_emitter = await get_event_emitter(metadata, update_db=False) @@ -1141,7 +1142,11 @@ async def chat_completion( chat_id = form_data.get('chat_id') or '' chat_variables = form_data.pop('chat_variables', None) if chat_variables is None: - existing_chat = await Chats.get_chat_by_id(chat_id) if chat_id else None + existing_chat = ( + await Chats.get_chat_by_id(chat_id) + if is_saved_chat_id(chat_id) + else None + ) chat_variables = existing_chat.variables if existing_chat else {} chat_variables = normalize_chat_variables(chat_variables) diff --git a/backend/open_webui/socket/main.py b/backend/open_webui/socket/main.py index c16ee91566..fc209853fb 100644 --- a/backend/open_webui/socket/main.py +++ b/backend/open_webui/socket/main.py @@ -37,6 +37,7 @@ from open_webui.socket.utils import RedisDict, RedisLock, YdocManager from open_webui.tasks import create_task, stop_item_tasks from open_webui.utils.access_control import has_permission from open_webui.utils.auth import get_verified_user_by_token +from open_webui.utils.chat_id import is_saved_chat_id from open_webui.utils.redis import ( build_sentinel_url, get_redis_connection, @@ -945,6 +946,7 @@ async def get_event_emitter(request_info, update_db=True): chat_id = request_info['chat_id'] message_id = request_info['message_id'] internal = request_info.get('internal') is True + save_to_chat = update_db and message_id and is_saved_chat_id(chat_id) if internal and event_data.get('type') == 'notification': return @@ -963,7 +965,7 @@ async def get_event_emitter(request_info, update_db=True): room=room, ) - if update_db and message_id and not (request_info.get('chat_id') or '').startswith('local:'): + if save_to_chat: event_type = event_data.get('type') if event_type == 'status': diff --git a/backend/open_webui/tools/builtin.py b/backend/open_webui/tools/builtin.py index bf4ab51f71..5be66b602d 100644 --- a/backend/open_webui/tools/builtin.py +++ b/backend/open_webui/tools/builtin.py @@ -52,6 +52,7 @@ from open_webui.routers.retrieval import search_web as _search_web from open_webui.tasks import stop_item_tasks from open_webui.events import EVENTS, publish_event from open_webui.socket.main import sio +from open_webui.utils.chat_id import is_saved_chat_id from open_webui.utils.notifications import notify_target from open_webui.utils.sanitize import sanitize_code @@ -378,7 +379,7 @@ async def generate_image( image_files = [{'type': 'image', 'url': img['url']} for img in images] # Persist files to DB if chat context is available - if __chat_id__ and __message_id__ and images: + if is_saved_chat_id(__chat_id__) and __message_id__ and images: db_files = await Chats.add_message_files_by_id_and_message_id( __chat_id__, __message_id__, @@ -446,7 +447,7 @@ async def edit_image( image_files = [{'type': 'image', 'url': img['url']} for img in images] # Persist files to DB if chat context is available - if __chat_id__ and __message_id__ and images: + if is_saved_chat_id(__chat_id__) and __message_id__ and images: db_files = await Chats.add_message_files_by_id_and_message_id( __chat_id__, __message_id__, @@ -3164,8 +3165,8 @@ async def create_tasks( :param tasks: List of task items. Each item: content (string, required), status (pending|in_progress|completed|cancelled, default pending), id (optional, auto-generated). :return: JSON with the full task list and summary counts """ - if __chat_id__ is None: - return json.dumps({'error': 'Chat context not available'}) + if not is_saved_chat_id(__chat_id__): + return json.dumps({'error': 'Saved chat context not available'}) try: all_tasks = [] @@ -3216,8 +3217,8 @@ async def update_task( :param status: New status: completed, in_progress, pending, or cancelled (default: completed) :return: JSON with the updated task list and summary counts """ - if __chat_id__ is None: - return json.dumps({'error': 'Chat context not available'}) + if not is_saved_chat_id(__chat_id__): + return json.dumps({'error': 'Saved chat context not available'}) try: status = status.strip().lower() diff --git a/backend/open_webui/utils/chat_id.py b/backend/open_webui/utils/chat_id.py new file mode 100644 index 0000000000..549a6bc5bd --- /dev/null +++ b/backend/open_webui/utils/chat_id.py @@ -0,0 +1,8 @@ +from typing import Optional + + +NON_SAVED_CHAT_ID_PREFIXES = ('local:', 'channel:') + + +def is_saved_chat_id(chat_id: Optional[str]) -> bool: + return bool(chat_id) and not chat_id.startswith(NON_SAVED_CHAT_ID_PREFIXES)