diff --git a/backend/open_webui/main.py b/backend/open_webui/main.py index 8706f439c8..9bdc804b45 100644 --- a/backend/open_webui/main.py +++ b/backend/open_webui/main.py @@ -1763,6 +1763,7 @@ async def chat_completion( 'chat_id': form_data.pop('chat_id', None), 'user_message': user_message, 'user_message_id': user_message.get('id') if user_message else None, + 'assistant_message_id': form_data.pop('assistant_message_id', None), 'session_id': form_data.pop('session_id', None), 'folder_id': form_data.pop('folder_id', None), 'filter_ids': form_data.pop('filter_ids', []), diff --git a/backend/open_webui/utils/middleware.py b/backend/open_webui/utils/middleware.py index 154dc9e8a6..7d67e60ade 100644 --- a/backend/open_webui/utils/middleware.py +++ b/backend/open_webui/utils/middleware.py @@ -2274,6 +2274,16 @@ async def process_chat_payload(request, form_data, user, metadata, model): if chat_id and user_message_id and not chat_id.startswith('local:'): db_messages = await load_messages_from_db(chat_id, user_message_id) if db_messages: + # Continue: frontend sends assistant_message_id when continuing + # an existing response. Load its content so the LLM sees prior output. + assistant_message_id = metadata.get('assistant_message_id') + if assistant_message_id: + assistant_message = await Chats.get_message_by_id_and_message_id(chat_id, assistant_message_id) + if assistant_message and (assistant_message.get('content') or assistant_message.get('output')): + db_messages.append( + {k: v for k, v in assistant_message.items() if k in ('role', 'content', 'output', 'files')} + ) + system_message = get_system_message(form_data.get('messages', [])) form_data['messages'] = [system_message, *db_messages] if system_message else db_messages @@ -3015,7 +3025,6 @@ async def background_tasks_handler(ctx): tasks = ctx['tasks'] event_emitter = ctx['event_emitter'] - message = None messages = [] @@ -4076,9 +4085,9 @@ async def streaming_chat_response_handler(response, ctx): current_response_tool_call['function']['name'] = delta_name if delta_arguments: - current_response_tool_call['function']['arguments'] += ( - delta_arguments - ) + current_response_tool_call['function'][ + 'arguments' + ] += delta_arguments # Emit pending tool calls in real-time if response_tool_calls: @@ -4832,8 +4841,7 @@ async def streaming_chat_response_handler(response, ctx): code = sanitize_code(code) if CODE_INTERPRETER_BLOCKED_MODULES: - blocking_code = textwrap.dedent( - f""" + blocking_code = textwrap.dedent(f""" import builtins BLOCKED_MODULES = {CODE_INTERPRETER_BLOCKED_MODULES} @@ -4849,8 +4857,7 @@ async def streaming_chat_response_handler(response, ctx): return _real_import(name, globals, locals, fromlist, level) builtins.__import__ = restricted_import - """ - ) + """) code = blocking_code + '\n' + code if request.app.state.config.CODE_INTERPRETER_ENGINE == 'pyodide': diff --git a/src/lib/components/chat/Chat.svelte b/src/lib/components/chat/Chat.svelte index d444ca450d..f13404ea07 100644 --- a/src/lib/components/chat/Chat.svelte +++ b/src/lib/components/chat/Chat.svelte @@ -2128,8 +2128,10 @@ _history, primaryResponseMessageId, _chatId, - selectedModelIds.length > 1 ? messageIdsMap : undefined, - regenerationPrompt + { + messageIdsMap: selectedModelIds.length > 1 ? messageIdsMap : undefined, + regenerationPrompt + } ); if (chatEventEmitter) clearInterval(chatEventEmitter); @@ -2194,8 +2196,15 @@ _history, responseMessageId, _chatId, - messageIdsMap?: Record, - regenerationPrompt?: string | null + { + messageIdsMap, + regenerationPrompt, + continueResponse = false + }: { + messageIdsMap?: Record; + regenerationPrompt?: string | null; + continueResponse?: boolean; + } = {} ) => { const responseMessage = _history.messages[responseMessageId]; const userMessage = _history.messages[responseMessage.parentId]; @@ -2396,6 +2405,7 @@ parent_id: userMessage?.parentId ?? null, user_message: userMessage, ...(regenerationPrompt ? { regeneration_prompt: regenerationPrompt } : {}), + ...(continueResponse ? { assistant_message_id: responseMessageId } : {}), background_tasks: { ...(!$temporaryChatEnabled && !_chatId && (userMessage?.parentId ?? null) === null @@ -2655,7 +2665,8 @@ createMessagesList(history, responseMessage.id), history, responseMessage.id, - _chatId + _chatId, + { continueResponse: true } ); } }