diff --git a/backend/open_webui/utils/middleware.py b/backend/open_webui/utils/middleware.py index ef77a1d303..643a12a627 100644 --- a/backend/open_webui/utils/middleware.py +++ b/backend/open_webui/utils/middleware.py @@ -112,6 +112,7 @@ from open_webui.utils.misc import ( extract_urls, get_content_from_message, get_last_assistant_message, + get_last_tool_result, get_last_user_message, get_last_user_message_item, get_message_list, @@ -2150,6 +2151,9 @@ async def process_chat_payload(request, form_data, user, metadata, model): ) event_emitter = await get_event_emitter(metadata) initial_output = [] + # What the model is told is available, by the hidden tool that reports it; added after compaction, and only + # when the history the model sees does not already end with the same manifest. + manifests: dict[str, str] = {} async def emit_initial_output(): await event_emitter({'type': 'chat:completion', 'data': {'output': initial_output}}) @@ -2624,11 +2628,7 @@ async def process_chat_payload(request, form_data, user, metadata, model): if skill_tools_enabled and skill_manifest: if not metadata.get('assistant_message_id'): - initial_output.extend( - hidden_tool_result( - 'list_available_skills', f'\n{skill_manifest}' - ) - ) + manifests['list_available_skills'] = f'\n{skill_manifest}' elif skill_manifest: form_data['messages'] = add_or_update_system_message( f'\n{skill_manifest}', @@ -2832,11 +2832,8 @@ async def process_chat_payload(request, form_data, user, metadata, model): knowledge_tags.append(f'') if knowledge_tags and not metadata.get('assistant_message_id'): - initial_output.extend( - hidden_tool_result( - 'list_attached_knowledge', - '\n' + '\n'.join(knowledge_tags) + '\n', - ) + manifests['list_attached_knowledge'] = ( + '\n' + '\n'.join(knowledge_tags) + '\n' ) builtin_tools = await get_builtin_tools( @@ -2975,21 +2972,6 @@ async def process_chat_payload(request, form_data, user, metadata, model): raise Exception(f'{e}') metadata.pop('initial_output', None) - if skill_tools_enabled and not metadata.get('assistant_message_id'): - initial_output.extend( - await preload_skills( - form_data['messages'], - activated_skill_ids & resolved_skill_ids, - metadata.get('tools', {}).get('view_skill'), - ) - ) - if initial_output: - form_data['messages'].extend( - convert_output_to_messages(initial_output, raw=True, reasoning_format=get_reasoning_format(model)) - ) - # Compaction below can still add its item to the output this response starts with. - metadata['initial_output'] = initial_output - form_data = normalize_messages_for_model(form_data) if compaction_messages is not None: @@ -3025,6 +3007,25 @@ async def process_chat_payload(request, form_data, user, metadata, model): form_data['messages'] = add_or_update_system_message(summary_content, form_data['messages'], append=True) metadata['context_summary'] = context_summary + + # Added after compaction: it can summarize away the copy the model had, and it should not summarize this turn's. + for name, manifest in manifests.items(): + if get_last_tool_result(form_data['messages'], name) != manifest: + initial_output.extend(hidden_tool_result(name, manifest)) + if skill_tools_enabled and not metadata.get('assistant_message_id'): + initial_output.extend( + await preload_skills( + form_data['messages'], + activated_skill_ids & resolved_skill_ids, + metadata.get('tools', {}).get('view_skill'), + ) + ) + if initial_output: + form_data['messages'].extend( + convert_output_to_messages(initial_output, raw=True, reasoning_format=get_reasoning_format(model)) + ) + metadata['initial_output'] = initial_output + return form_data, metadata, events, False diff --git a/backend/open_webui/utils/misc.py b/backend/open_webui/utils/misc.py index 7204a5ced2..0a8c210905 100644 --- a/backend/open_webui/utils/misc.py +++ b/backend/open_webui/utils/misc.py @@ -294,6 +294,20 @@ def get_paired_tool_call_ids(messages: list[dict]) -> list[set[str]]: return paired_ids_by_message +def get_last_tool_result(messages: list[dict], name: str) -> str | None: + """What the named tool last returned in these messages, or None if it never did.""" + call_ids = { + tool_call.get('id') + for message in messages + for tool_call in message.get('tool_calls') or [] + if (tool_call.get('function') or {}).get('name') == name + } + for message in reversed(messages): + if message.get('role') == 'tool' and message.get('tool_call_id') in call_ids: + return get_content_from_message(message) or '' + return None + + def reconcile_tool_pairs(messages: list[dict]) -> list[dict]: """Drop unpaired tool_use / tool_result from a reconstructed conversation.