This commit is contained in:
Timothy Jaeryang Baek
2026-07-26 21:12:14 -04:00
parent f867825bf3
commit 71c4da8c06
8 changed files with 65 additions and 40 deletions
+20 -1
View File
@@ -1,8 +1,27 @@
from typing import Optional
NON_SAVED_CHAT_ID_PREFIXES = ('local:', 'channel:')
TEMPORARY_CHAT_ID_PREFIX = 'temporary:'
LEGACY_TEMPORARY_CHAT_ID_PREFIX = 'local:' # Legacy temporary chat prefix.
CHANNEL_CHAT_ID_PREFIX = 'channel:'
TEMPORARY_CHAT_ID_PREFIXES = (
TEMPORARY_CHAT_ID_PREFIX,
LEGACY_TEMPORARY_CHAT_ID_PREFIX,
)
NON_SAVED_CHAT_ID_PREFIXES = (*TEMPORARY_CHAT_ID_PREFIXES, CHANNEL_CHAT_ID_PREFIX)
def is_saved_chat_id(chat_id: Optional[str]) -> bool:
return bool(chat_id) and not chat_id.startswith(NON_SAVED_CHAT_ID_PREFIXES)
def is_temporary_chat_id(chat_id: Optional[str]) -> bool:
return bool(chat_id) and chat_id.startswith(TEMPORARY_CHAT_ID_PREFIXES)
def get_temporary_chat_session_id(chat_id: str) -> Optional[str]:
for prefix in TEMPORARY_CHAT_ID_PREFIXES:
if chat_id.startswith(prefix):
return chat_id.removeprefix(prefix)
return None
@@ -7,6 +7,7 @@ from typing import Any
from fastapi.responses import JSONResponse
from open_webui.models.chats import Chats
from open_webui.models.config import Config
from open_webui.utils.chat_id import is_saved_chat_id
from open_webui.utils.misc import get_content_from_message, get_last_user_message, get_message_list
from open_webui.utils.task import (
get_task_model_id,
@@ -110,7 +111,7 @@ async def compact_messages_for_request(
checkpoint_message_id = (
recent_messages[0].get('id') or metadata.get('user_message_id') or metadata.get('message_id')
)
if chat_id and checkpoint_message_id and not chat_id.startswith(('local:', 'channel:')):
if is_saved_chat_id(chat_id) and checkpoint_message_id:
await Chats.upsert_message_to_chat_by_id_and_message_id(
chat_id,
checkpoint_message_id,
+11 -19
View File
@@ -1553,7 +1553,7 @@ async def add_file_context(messages: list, chat_id: str, user) -> list:
"""
Add file URLs to messages for native function calling.
"""
if not chat_id or chat_id.startswith('local:') or chat_id.startswith('channel:'):
if not is_saved_chat_id(chat_id):
return messages
chat = await Chats.get_chat_by_id_and_user_id(chat_id, user.id)
@@ -1609,7 +1609,7 @@ async def chat_image_generation_handler(request: Request, form_data: dict, extra
if not chat_id or not isinstance(chat_id, str) or not __event_emitter__:
return form_data
if chat_id.startswith('local:') or chat_id.startswith('channel:'):
if not is_saved_chat_id(chat_id):
message_list = form_data.get('messages', [])
else:
chat = await Chats.get_chat_by_id_and_user_id(chat_id, user.id)
@@ -2270,7 +2270,7 @@ async def process_chat_payload(request, form_data, user, metadata, model):
chat_id = metadata.get('chat_id')
user_message_id = metadata.get('user_message_id')
if chat_id and user_message_id and not chat_id.startswith('local:') and not chat_id.startswith('channel:'):
if is_saved_chat_id(chat_id) and user_message_id:
db_messages = await load_messages_from_db(chat_id, user_message_id)
if db_messages:
# Continue: frontend sends assistant_message_id when continuing
@@ -2317,7 +2317,7 @@ async def process_chat_payload(request, form_data, user, metadata, model):
if regeneration_prompt:
form_data['messages'].append({'role': 'user', 'content': regeneration_prompt})
if chat_id and user_message_id and not chat_id.startswith('local:') and not chat_id.startswith('channel:'):
if is_saved_chat_id(chat_id) and user_message_id:
if getattr(request.state, 'direct', False) and hasattr(request.state, 'model'):
compaction_models = {
request.state.model['id']: request.state.model,
@@ -2574,7 +2574,7 @@ async def process_chat_payload(request, form_data, user, metadata, model):
available_skills = []
view_skill_ids = []
chat = None
if metadata.get('chat_id') and not metadata['chat_id'].startswith(('local:', 'channel:')):
if is_saved_chat_id(metadata.get('chat_id')):
chat = await Chats.get_chat_by_id(metadata['chat_id'])
if chat and (chat.meta or {}).get('internal') is True and (chat.meta or {}).get('type') == 'note':
@@ -3138,11 +3138,7 @@ async def background_tasks_handler(ctx):
message = None
messages = []
if (
'chat_id' in metadata
and not metadata.get('chat_id', '').startswith('local:')
and not metadata.get('chat_id', '').startswith('channel:')
):
if is_saved_chat_id(metadata.get('chat_id')):
messages_map = await Chats.get_messages_map_by_chat_id(metadata['chat_id'])
if not messages_map:
# Chat was deleted while the response was streaming — skip background tasks
@@ -3225,9 +3221,7 @@ async def background_tasks_handler(ctx):
}
)
if not metadata.get('chat_id', '').startswith('local:') and not metadata.get(
'chat_id', ''
).startswith('channel:'):
if is_saved_chat_id(metadata.get('chat_id')):
await Chats.upsert_message_to_chat_by_id_and_message_id(
metadata['chat_id'],
metadata['message_id'],
@@ -3240,9 +3234,7 @@ async def background_tasks_handler(ctx):
except Exception as e:
pass
if not metadata.get('chat_id', '').startswith('local:') and not metadata.get('chat_id', '').startswith(
'channel:'
): # Only update titles and tags for non-temp chats
if is_saved_chat_id(metadata.get('chat_id')): # Only update titles and tags for saved chats
if TASKS.TITLE_GENERATION in tasks:
user_message = get_last_user_message(messages)
if user_message and len(user_message) > 100:
@@ -3378,11 +3370,11 @@ async def outlet_filter_handler(ctx):
if not message_id:
message_id = output_id('msg')
is_temp_chat = chat_id.startswith('local:') or chat_id.startswith('channel:')
is_unsaved_chat = not is_saved_chat_id(chat_id)
try:
messages_map = None
if is_temp_chat or not chat_id:
if is_unsaved_chat:
form_messages = ctx.get('form_data', {}).get('messages', [])
assistant_message = ctx.get('assistant_message', {})
@@ -3658,7 +3650,7 @@ async def non_streaming_chat_response_handler(response, ctx):
if (
getattr(request.state, 'internal', False) is not True
and chat_id
and not chat_id.startswith(('channel:', 'local:'))
and is_saved_chat_id(chat_id)
):
webui_url = await Config.get('webui.url')
await publish_event(