diff --git a/backend/open_webui/main.py b/backend/open_webui/main.py index 86184b0b33..fd7094b099 100644 --- a/backend/open_webui/main.py +++ b/backend/open_webui/main.py @@ -1549,6 +1549,14 @@ async def chat_completion( user_message = metadata.get('user_message') or {} assistant_message_id = metadata.get('assistant_message_id') + if parent_id and not ( + assistant_message_id or metadata.get('regeneration_message_id') or metadata['internal'] + ): + from open_webui.utils.subagents import queue_user_message + + # A reply in this chat is still running: it takes this message at its next step. + if await queue_user_message(request, chat_id, user, user_message, parent_id): + return {'status': True, 'queued': True, 'chat_id': chat_id} if assistant_message_id: message = await Chats.get_message_by_id_and_message_id(chat_id, assistant_message_id) if not message or (message.get('user_id') or chat.user_id) != user.id: @@ -1804,23 +1812,10 @@ async def chat_completion( and getattr(request.state, 'internal', False) is not True and not await has_active_tasks(request.app.state.redis, chat_id) ): - from open_webui.utils.subagents import process_pending_internal_messages + from open_webui.utils.subagents import get_pending_run, process_pending_internal_messages await process_pending_internal_messages( - request, - chat_id, - user.id, - { - 'model_id': metadata.get('model_id') or form_data.get('model'), - 'session_id': metadata.get('session_id'), - 'tool_ids': metadata.get('tool_ids') or [], - 'skill_ids': metadata.get('skill_ids') or [], - 'chat_context': metadata.get('chat_context'), - 'filter_ids': metadata.get('filter_ids') or [], - 'terminal_id': metadata.get('terminal_id'), - 'features': metadata.get('features') or {}, - 'variables': metadata.get('variables') or {}, - }, + request, chat_id, user.id, get_pending_run(metadata, form_data.get('model')) ) except Exception: log.exception('Failed to process pending internal messages for chat %s', metadata.get('chat_id')) diff --git a/backend/open_webui/models/chat_messages.py b/backend/open_webui/models/chat_messages.py index dfc758cd86..149a5f1733 100644 --- a/backend/open_webui/models/chat_messages.py +++ b/backend/open_webui/models/chat_messages.py @@ -383,6 +383,20 @@ class ChatMessageTable: ) return result.scalar_one_or_none() is not None + async def has_queued_message(self, chat_id: str, message_id: str, db: Optional[AsyncSession] = None) -> bool: + """Whether the user sent a message while this reply was still running.""" + async with get_async_db_context(db) as db: + result = await db.execute( + select(ChatMessage.meta) + .where(ChatMessage.chat_id == chat_id) + .where(ChatMessage.parent_id == message_id) + .where(ChatMessage.role == 'user') + ) + return any( + (meta or {}).get('status') == 'pending' and (meta or {}).get('internal') is not True + for meta in result.scalars() + ) + async def get_messages_by_chat_id(self, chat_id: str, db: Optional[AsyncSession] = None) -> list[ChatMessageModel]: async with get_async_db_context(db) as db: result = await db.execute( diff --git a/backend/open_webui/models/chats.py b/backend/open_webui/models/chats.py index d683eaa8b1..cc4cc0af70 100644 --- a/backend/open_webui/models/chats.py +++ b/backend/open_webui/models/chats.py @@ -578,6 +578,11 @@ class ChatTable: and child.get('id') == child_id and child.get('role') and child.get('parentId') == message_id + # A queued message waits in the queue, not on the branch shown. + and not ( + (child.get('meta') or {}).get('status') == 'pending' + and (child.get('meta') or {}).get('internal') is not True + ) ), None, ) diff --git a/backend/open_webui/utils/middleware.py b/backend/open_webui/utils/middleware.py index 643a12a627..c0a403007d 100644 --- a/backend/open_webui/utils/middleware.py +++ b/backend/open_webui/utils/middleware.py @@ -47,6 +47,7 @@ from open_webui.env import ( ) from open_webui.events import EVENTS, build_event, publish_event from open_webui.models.access_grants import AccessGrants +from open_webui.models.chat_messages import ChatMessages from open_webui.models.chats import Chats from open_webui.models.config import Config from open_webui.models.folders import Folders @@ -3612,6 +3613,22 @@ async def background_tasks_handler(ctx): if not await get_verified_user_by_id(user.id): return + # Messages the user sent while this reply ran are answered now; follow-ups for it would go unused. + if ( + is_saved_chat_id(metadata.get('chat_id')) + and not metadata.get('internal') + and await ChatMessages.has_queued_message(metadata['chat_id'], metadata['message_id']) + ): + from open_webui.utils.subagents import get_pending_run, process_pending_internal_messages + + tasks = {key: value for key, value in (tasks or {}).items() if key != TASKS.FOLLOW_UP_GENERATION} + try: + await process_pending_internal_messages( + request, metadata['chat_id'], user.id, get_pending_run(metadata), reply_id=metadata['message_id'] + ) + except Exception: + log.exception('Failed to answer queued messages for chat %s', metadata['chat_id']) + message = None messages = [] @@ -6081,6 +6098,11 @@ async def streaming_chat_response_handler(response, ctx): if final_answer_requested: await emit_message_error(round_error or 'The provider requested tools after tool use stopped.') break + # The user sent a message meanwhile: stop here, and it is answered next with these results. + if is_saved_chat_id(metadata.get('chat_id')) and await ChatMessages.has_queued_message( + metadata['chat_id'], metadata['message_id'] + ): + break try: new_form_data = { diff --git a/backend/open_webui/utils/subagents.py b/backend/open_webui/utils/subagents.py index 996626a578..3baa6938c2 100644 --- a/backend/open_webui/utils/subagents.py +++ b/backend/open_webui/utils/subagents.py @@ -91,18 +91,87 @@ async def _build_request(source: Request, user_id: str, *, internal: bool) -> Re return request +async def queue_user_message( + request: Request, chat_id: str, user: UserModel, user_message: dict, parent_id: str +) -> bool: + """Hold a message sent while its reply is running. The reply ends at its next step and this is answered next.""" + if not user_message.get('id') or not await has_active_tasks(request.app.state.redis, chat_id): + return False + # Only a reply still being written holds messages. Checked before locking: the turn that answers + # held messages is started under this lock, and its own reply is already finished. + reply = await Chats.get_message_by_id_and_message_id(chat_id, parent_id) + if not reply or reply.get('role') != 'assistant' or reply.get('done') is not False: + return False + async with _parent_locks.setdefault(chat_id, asyncio.Lock()): + # Checked under the lock the reply's own cleanup takes, so the message can't be left unanswered. + if not await has_active_tasks(request.app.state.redis, chat_id): + return False + async with Chats._chat_transaction(chat_id) as (_, chat): + if not chat or chat.user_id != user.id: + return False + history = copy.deepcopy((chat.chat or {}).get('history') or {}) + messages = history.get('messages') or {} + reply = messages.get(parent_id) + if not reply: + return False + message = { + 'id': user_message['id'], + 'parentId': parent_id, + 'childrenIds': [], + 'role': 'user', + 'content': user_message.get('content', ''), + **({'files': user_message['files']} if user_message.get('files') else {}), + 'model': reply.get('model'), + 'user_id': user.id, + 'user': {'id': user.id, 'name': user.name}, + 'meta': {'status': 'pending'}, + 'timestamp': int(time.time()), + } + messages[message['id']] = message + reply.setdefault('childrenIds', []).append(message['id']) + history['messages'] = messages + chat.chat = {**(chat.chat or {}), 'history': history} + chat.updated_at = int(time.time()) + await ChatMessages.upsert_message(message['id'], chat_id, user.id, message) + return True + + +def get_pending_run(metadata: dict, model_id: str | None = None) -> dict: + """The settings a turn started for pending messages takes from the turn before it.""" + return { + 'model_id': metadata.get('model_id') or model_id, + 'session_id': metadata.get('session_id'), + 'tool_ids': metadata.get('tool_ids') or [], + 'skill_ids': metadata.get('skill_ids') or [], + 'chat_context': metadata.get('chat_context'), + 'filter_ids': metadata.get('filter_ids') or [], + 'terminal_id': metadata.get('terminal_id'), + 'features': metadata.get('features') or {}, + 'variables': metadata.get('variables') or {}, + } + + async def process_pending_internal_messages( source_request: Request, parent_chat_id: str, user_id: str, run: dict, + reply_id: str | None = None, ) -> None: + """Answer pending messages once the chat is idle. With reply_id, answer only the messages queued under that + reply as soon as it is saved, without waiting for its follow-ups, title and other background work.""" lock = _parent_locks.setdefault(parent_chat_id, asyncio.Lock()) - while await has_active_tasks(source_request.app.state.redis, parent_chat_id): - await asyncio.sleep(0.25) + if reply_id is None: + while await has_active_tasks(source_request.app.state.redis, parent_chat_id): + await asyncio.sleep(0.25) + else: + # The turn started below asks whether its parent reply is still running, without taking this lock. + reply = await Chats.get_message_by_id_and_message_id(parent_chat_id, reply_id) + if not reply or reply.get('done') is not True: + return async with lock: - if await has_active_tasks(source_request.app.state.redis, parent_chat_id): + if reply_id is None and await has_active_tasks(source_request.app.state.redis, parent_chat_id): return user = await Users.get_user_by_id(user_id) @@ -128,14 +197,28 @@ async def process_pending_internal_messages( and meta.get('status') in (None, 'pending') ) or (meta.get('internal') is True and meta.get('type') == 'timer') + # The user's own message, held while its reply was running. + or (meta.get('internal') is not True and meta.get('status') == 'pending') ) ] + if reply_id is not None: + pending = [ + message + for message in pending + if message.get('parentId') == reply_id and (message.get('meta') or {}).get('internal') is not True + ] if not pending: return first = pending[0] first_meta = first.get('meta') or {} - kind = 'timer' if first_meta.get('internal') is True and first_meta.get('type') == 'timer' else 'subagent' + kind = ( + 'timer' + if first_meta.get('internal') is True and first_meta.get('type') == 'timer' + else 'queued' + if first_meta.get('internal') is not True + else 'subagent' + ) parent_id = first.get('parentId') if kind == 'timer' and first_meta.get('timer_id'): timer = await Chats.get_chat_by_id(first_meta['timer_id']) @@ -145,6 +228,12 @@ async def process_pending_internal_messages( model_id = first.get('model') or run['model_id'] if kind == 'timer': batch = [first] + elif kind == 'queued': + batch = [ + message + for message in pending + if message.get('parentId') == parent_id and (message.get('meta') or {}).get('internal') is not True + ] else: batch = [ message @@ -168,6 +257,9 @@ async def process_pending_internal_messages( combined_meta['timer_id'] = timer_ids[0] elif timer_ids: combined_meta['timer_ids'] = timer_ids + elif kind == 'queued': + # The user's own words, answered as an ordinary message. + combined_meta = {} else: delegation_ids = [ message['meta']['delegation_id'] @@ -215,6 +307,11 @@ async def process_pending_internal_messages( 'meta': combined_meta, 'timestamp': int(time.time()), } + if kind == 'queued': + user_message.update(user_id=user.id, user={'id': user.id, 'name': user.name}) + held_files = [file for message in batch for file in message.get('files') or []] + if held_files: + user_message['files'] = held_files assistant_message = { 'id': assistant_message_id, 'parentId': user_message_id, @@ -285,7 +382,16 @@ async def process_pending_internal_messages( 'skill_ids': run.get('skill_ids') or [], 'filter_ids': run.get('filter_ids') or [], 'features': run.get('features') or {}, - 'files': run.get('files') or [], + 'files': [ + *(run.get('files') or []), + # Documents attached to held messages; their images reach the model with the message itself. + *[ + file + for file in user_message.get('files') or [] + if file.get('type') in ('doc', 'text', 'note', 'chat', 'collection', 'folder') + or (file.get('type') == 'file' and not is_raster_image_content_type(file.get('content_type'))) + ], + ], 'variables': run.get('variables') or {}, 'chat_variables': chat_context.get('chat_variables'), } diff --git a/src/lib/components/chat/Chat.svelte b/src/lib/components/chat/Chat.svelte index 23b0167aac..5682fd11ab 100644 --- a/src/lib/components/chat/Chat.svelte +++ b/src/lib/components/chat/Chat.svelte @@ -111,6 +111,7 @@ compactChatById, createNewChat, deleteChatById, + deleteChatMessageById, forkChatById, getAllTags, getChatById, @@ -3496,7 +3497,9 @@ if (!queue || queue.length === 0 || queue[0].error) return; const lastMessage = history.currentId ? history.messages[history.currentId] : null; if ( - (lastMessage && lastMessage.role === 'assistant' && !lastMessage.done) || + (lastMessage?.role === 'assistant' && + !lastMessage.done && + !usesBackendQueue(queue.flatMap((m) => m.files ?? []))) || queue.some((m) => (m.files ?? []).some((file) => ['uploading', 'error'].includes(file.status)) ) @@ -3539,7 +3542,52 @@ } }; + // The backend queues messages sent to your own saved chat while a reply runs; the reply ends at its next + // step and they are answered next. Files on a terminal stay in this browser's queue. + const usesBackendQueue = (messageFiles: any[] = []) => + ($settings?.enableMessageQueue ?? true) && + !$temporaryChat && + !!$chatId && + !isTemporaryChatId($chatId) && + (chat?.user_id ?? $user?.id) === $user?.id && + !messageFiles.some((file) => file.type === 'filesystem'); + + // Messages the backend queued under the reply shown, listed with the browser's queue. + $: backendQueue = ( + (history.currentId ? history.messages[history.currentId]?.childrenIds : null) ?? [] + ) + .map((id: string) => history.messages[id]) + .filter( + (message: any) => + message?.role === 'user' && + message.meta?.status === 'pending' && + message.meta?.internal !== true + ); + $: messageQueue = [ + ...backendQueue.map((message: any) => ({ + id: message.id, + prompt: message.content, + files: message.files ?? [] + })), + ...($chatRequestQueues[$chatId] ?? []) + ]; + + const removeFromBackendQueue = async (id: string) => { + const message = history.messages[id]; + await deleteChatMessageById(localStorage.token, $chatId, id); + const parent = history.messages[message?.parentId]; + if (parent) parent.childrenIds = parent.childrenIds.filter((childId: string) => childId !== id); + delete history.messages[id]; + history = history; + return message; + }; + const sendQueuedMessageNow = async (id) => { + if (history.messages[id]?.meta?.status === 'pending') { + // Stopping the reply answers its queued messages right away. + await stopResponse(false); + return; + } if (processingQueueChats.has($chatId)) return; const queue = $chatRequestQueues[$chatId] ?? []; @@ -3574,6 +3622,16 @@ }; const editQueuedMessage = async (id) => { + if (history.messages[id]?.meta?.status === 'pending') { + const message = await removeFromBackendQueue(id).catch((error) => { + toast.error(`${error}`); + return null; + }); + if (!message) return; + files = message.files ?? []; + messageInput?.setText(message.content); + return; + } const queue = $chatRequestQueues[$chatId] ?? []; const item = queue.find((m) => m.id === id); if (!item) return; @@ -3589,6 +3647,10 @@ }; const deleteQueuedMessage = async (id) => { + if (history.messages[id]?.meta?.status === 'pending') { + await removeFromBackendQueue(id).catch((error) => toast.error(`${error}`)); + return; + } const queue = $chatRequestQueues[$chatId] ?? []; chatRequestQueues.update((q) => ({ ...q, @@ -3987,6 +4049,13 @@ (item, index, array) => array.findIndex((i) => equal(i, item)) === index ); + const runningReply = history.currentId ? history.messages[history.currentId] : null; + const queued = + !bridgeRequest && + runningReply?.role === 'assistant' && + runningReply.done === false && + usesBackendQueue(_files); + // Create user message let userMessageId = bridgeRequest?.userMessageId ?? uuidv4(); let userMessage = { @@ -3999,7 +4068,8 @@ content: inputContent, files: _files.length > 0 ? _files : undefined, timestamp: Math.floor(Date.now() / 1000), // Unix epoch - models: selectedModels + models: selectedModels, + ...(queued ? { meta: { status: 'pending' } } : {}) }; // A transcribed Bridge message already exists in the same history. @@ -4012,7 +4082,9 @@ history.messages[history.currentId].childrenIds.push(userMessageId); } - history.currentId = userMessageId; + // A queued message waits in the queue list; the running reply stays the end of the transcript. + if (!queued) history.currentId = userMessageId; + else history = history; // focus on chat input (skip during voice call to avoid triggering mobile keyboard) if (!$voiceCall) { @@ -4549,7 +4621,7 @@ ) : lastMessage && lastMessage.role === 'assistant' && !lastMessage.done; - if (isGenerating) { + if (isGenerating && (bridgeRequest || !usesBackendQueue(files))) { if (bridgeRequest) return { status: 'deferred' }; if ($settings?.enableMessageQueue ?? true) { // Enqueue the request @@ -4633,6 +4705,10 @@ history.messages[parentId].models = [...selectedModelIds]; } + // A queued message gets no reply on screen unless the backend answers it now. + const queued = history.messages[parentId]?.meta?.status === 'pending'; + const target = queued ? structuredClone(history) : history; + // Use a message_ids array with modelIdx to preserve separate columns for duplicate models. const messageIdsList: Array<{ model_id: string; message_id: string; modelIdx: number }> = []; for (const [_modelIdx, modelId] of selectedModelIds.entries()) { @@ -4655,13 +4731,13 @@ }; // Add message to history and Set currentId to messageId - history.messages[responseMessageId] = responseMessage; - history.currentId = responseMessageId; + target.messages[responseMessageId] = responseMessage; + target.currentId = responseMessageId; // Append messageId to childrenIds of parent message - if (parentId !== null && history.messages[parentId]) { - history.messages[parentId].childrenIds = [ - ...history.messages[parentId].childrenIds, + if (parentId !== null && target.messages[parentId]) { + target.messages[parentId].childrenIds = [ + ...target.messages[parentId].childrenIds, responseMessageId ]; } @@ -4679,7 +4755,7 @@ await tick(); // Re-clone history so sendMessageSocket gets the response messages we just added - _history = structuredClone(history); + _history = structuredClone(target); // Vision capability check for (const mid of selectedModelIds) { @@ -4988,7 +5064,11 @@ id: responseMessageId, ...(messageIdsList ? { message_ids: messageIdsList } : {}), parent_id: userMessage?.parentId ?? null, - user_message: userMessage, + // Whether a message is queued is the backend's call, not a status the browser sends. + user_message: + userMessage?.meta?.status === 'pending' + ? { ...userMessage, meta: { ...userMessage.meta, status: undefined } } + : userMessage, ...(regenerationPrompt ? { regeneration_prompt: regenerationPrompt } : {}), ...(regenerationMessageId ? { regeneration_message_id: regenerationMessageId } : {}), ...(continueResponse ? { assistant_message_id: responseMessageId } : {}), @@ -5037,6 +5117,32 @@ if (filesystemAttachment && (!res || res.error)) throw new Error('File request failed. Retry from the message or queue.'); + const placeholderIds = (messageIdsList ?? [{ message_id: responseMessageId }]).map( + ({ message_id }) => message_id + ); + const sentMessage = history.messages[userMessage.id]; + if (res?.queued && sentMessage) { + // Queued for the running reply, which ends at its next step; the answer arrives with a chat reload. + for (const id of placeholderIds) delete history.messages[id]; + sentMessage.childrenIds = sentMessage.childrenIds.filter( + (id: string) => !placeholderIds.includes(id) + ); + sentMessage.meta = { ...(sentMessage.meta ?? {}), status: 'pending' }; + if (placeholderIds.includes(history.currentId)) history.currentId = sentMessage.parentId; + history = history; + return res; + } + if (res && !res.error && sentMessage?.meta?.status === 'pending') { + // The reply had already finished, so the backend answered it as a normal turn. + sentMessage.meta = { ...sentMessage.meta, status: undefined }; + for (const id of placeholderIds) { + if (!_history.messages[id]) continue; + history.messages[id] = _history.messages[id]; + if (!sentMessage.childrenIds.includes(id)) sentMessage.childrenIds.push(id); + } + history.currentId = responseMessageId; + history = history; + } if (res) { if (res.error) { await handleOpenAIError(res.error, responseMessage); @@ -6100,7 +6206,7 @@ {createMessagePair} {onUpload} {onUpdate} - messageQueue={$chatRequestQueues[$chatId] ?? []} + {messageQueue} {chatTasks} askUser={savedAskUserPrompt ?? socketAskUserPrompt} onQueueSendNow={sendQueuedMessageNow} @@ -6219,7 +6325,7 @@ {createMessagePair} {onUpload} {onUpdate} - messageQueue={$chatRequestQueues[$chatId] ?? []} + {messageQueue} {chatTasks} askUser={savedAskUserPrompt ?? socketAskUserPrompt} onQueueSendNow={sendQueuedMessageNow} @@ -6287,7 +6393,7 @@ {onSelect} {onUpload} {onUpdate} - messageQueue={$chatRequestQueues[$chatId] ?? []} + {messageQueue} askUser={savedAskUserPrompt ?? socketAskUserPrompt} onQueueSendNow={sendQueuedMessageNow} onQueueEdit={editQueuedMessage}