From 18f7633afa78d2d8f3fac7cd8d9840163a87d298 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Fri, 6 Nov 2026 13:40:53 +0900 Subject: [PATCH] refac --- backend/open_webui/main.py | 4 ++- backend/open_webui/models/chats.py | 6 +++- backend/open_webui/routers/tasks.py | 4 ++- backend/open_webui/utils/middleware.py | 38 +++++++++++++++++++------ backend/open_webui/utils/task.py | 10 +++++++ src/lib/components/chat/ChatTabs.svelte | 1 + 6 files changed, 51 insertions(+), 12 deletions(-) diff --git a/backend/open_webui/main.py b/backend/open_webui/main.py index d7b4787a03..4c35703c18 100644 --- a/backend/open_webui/main.py +++ b/backend/open_webui/main.py @@ -1345,8 +1345,10 @@ async def chat_completion( raise HTTPException(status_code=404, detail='Temporary chat expired or unavailable') initial_title_generation = None - if is_new_chat and not agent_model and tasks and TASKS.TITLE_GENERATION in tasks: + if is_new_chat and tasks and TASKS.TITLE_GENERATION in tasks: initial_title_generation = tasks.pop(TASKS.TITLE_GENERATION) + elif is_new_chat and agent_model: + initial_title_generation = False # Still give agent chats a first-message title. if metadata.get('chat_id') and user: chat_id = metadata['chat_id'] diff --git a/backend/open_webui/models/chats.py b/backend/open_webui/models/chats.py index acaff1acb6..b820235241 100644 --- a/backend/open_webui/models/chats.py +++ b/backend/open_webui/models/chats.py @@ -1015,11 +1015,15 @@ class ChatTable: await session.commit() return result.rowcount or 0 - async def update_chat_title_by_id(self, id: str, title: str) -> ChatModel | None: + async def update_chat_title_by_id( + self, id: str, title: str, *, expected_title: str | None = None + ) -> ChatModel | None: try: async with self._chat_transaction(id) as (session, chat_item): if chat_item is None: return None + if expected_title is not None and chat_item.title != expected_title: + return None clean_title = self._clean_null_bytes(title) chat_item.title = clean_title chat_item.chat = {**(chat_item.chat or {}), 'title': clean_title} diff --git a/backend/open_webui/routers/tasks.py b/backend/open_webui/routers/tasks.py index 145feb8bb5..e781e727b8 100644 --- a/backend/open_webui/routers/tasks.py +++ b/backend/open_webui/routers/tasks.py @@ -160,13 +160,15 @@ async def generate_title(request: Request, form_data: dict, user=Depends(get_ver status_code=status.HTTP_400_BAD_REQUEST, detail='No model specified for title generation. Please ensure a model is selected for this chat.', ) - if model_id not in models: + if model_id not in models and not model_id.startswith('agent:'): raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail=ERROR_MESSAGES.MODEL_NOT_FOUND(), ) task_model_id, task_model_params = await get_task_model_generation_config(model_id, models) + if task_model_id not in models or task_model_id.startswith('agent:'): + return JSONResponse(content={'detail': 'No task model configured for agent chat titles'}) log.debug('generating chat title using model %s for user %s ', task_model_id, user.email) diff --git a/backend/open_webui/utils/middleware.py b/backend/open_webui/utils/middleware.py index 7b1e3ea7f0..a46d4a548b 100644 --- a/backend/open_webui/utils/middleware.py +++ b/backend/open_webui/utils/middleware.py @@ -3733,6 +3733,19 @@ async def background_tasks_handler(ctx): user_message = user_message[:100] + '...' title = None + expected_title = None + if str(message['model']).startswith('agent:'): + first_user = next((m for m in messages if m.get('role') == 'user'), {}) + title = ( + replace_skill_mentions_with_labels(first_user.get('content', '')).strip()[:50] or 'New Chat' + ) + updated = await Chats.update_chat_title_by_id( + metadata['chat_id'], title, expected_title='New Chat' + ) + if not updated: + return # The chat was deleted or already renamed. + await event_emitter({'type': 'chat:title', 'data': title}) + expected_title = title if tasks[TASKS.TITLE_GENERATION]: res = await generate_title( request, @@ -3761,22 +3774,29 @@ async def background_tasks_handler(ctx): title_string = title_string[title_string.find('{') : title_string.rfind('}') + 1] try: - title = JSONCodec.loads(title_string).get('title', user_message) + title = JSONCodec.loads(title_string).get('title', expected_title or user_message) except Exception as e: title = '' if not title: - title = replace_skill_mentions_with_labels(messages[0].get('content', user_message)) + title = expected_title or replace_skill_mentions_with_labels( + messages[0].get('content', user_message) + ) - await Chats.update_chat_title_by_id(metadata['chat_id'], title) - - await event_emitter( - { - 'type': 'chat:title', - 'data': title, - } + if expected_title is not None and (not isinstance(title, str) or not title.strip()): + title = expected_title + updated = await Chats.update_chat_title_by_id( + metadata['chat_id'], title, expected_title=expected_title ) + if updated: + await event_emitter( + { + 'type': 'chat:title', + 'data': title, + } + ) + if title == None and len(messages) == 2 and (not messages_map or len(messages_map) <= 2): title = replace_skill_mentions_with_labels(messages[0].get('content', user_message)) diff --git a/backend/open_webui/utils/task.py b/backend/open_webui/utils/task.py index 3c6bfae69f..280f19c898 100644 --- a/backend/open_webui/utils/task.py +++ b/backend/open_webui/utils/task.py @@ -15,6 +15,16 @@ log = logging.getLogger(__name__) # Let the right tool be given for the work at hand, # not the one that flatters, but the one that serves. def get_task_model_id(default_model_id: str, task_model: str, task_model_external: str, models) -> str: + if default_model_id.startswith('agent:'): + # Agent sessions cannot serve ordinary completion requests for background tasks. + return next( + ( + model + for model in (task_model_external, task_model) + if model and model in models and not model.startswith('agent:') + ), + default_model_id, + ) task_model_id = default_model_id # Use an available task model override for the local or external connection. if models.get(task_model_id, {}).get('connection_type') == 'local': diff --git a/src/lib/components/chat/ChatTabs.svelte b/src/lib/components/chat/ChatTabs.svelte index 4b073fb08b..3e14e2e251 100644 --- a/src/lib/components/chat/ChatTabs.svelte +++ b/src/lib/components/chat/ChatTabs.svelte @@ -1282,6 +1282,7 @@