|
98 | 98 | convert_logit_bias_input_to_json, |
99 | 99 | get_content_from_message, |
100 | 100 | convert_output_to_messages, |
| 101 | + filter_output_by_content, |
101 | 102 | strip_empty_content_blocks, |
102 | 103 | ) |
103 | 104 | from open_webui.utils.tools import ( |
@@ -483,9 +484,9 @@ def serialize_output(output: list) -> str: |
483 | 484 | ) |
484 | 485 |
|
485 | 486 | if status == 'completed' or duration is not None or not is_last_item: |
486 | | - content = f'{content}<details type="reasoning" done="true" duration="{duration or 0}">\n<summary>Thought for {duration or 0} seconds</summary>\n{display}\n</details>\n' |
| 487 | + content = f'{content}<details type="reasoning" done="true" id="{item.get("id", "")}" duration="{duration or 0}">\n<summary>Thought for {duration or 0} seconds</summary>\n{display}\n</details>\n' |
487 | 488 | else: |
488 | | - content = f'{content}<details type="reasoning" done="false">\n<summary>Thinking…</summary>\n{display}\n</details>\n' |
| 489 | + content = f'{content}<details type="reasoning" done="false" id="{item.get("id", "")}">\n<summary>Thinking…</summary>\n{display}\n</details>\n' |
489 | 490 |
|
490 | 491 | elif item_type == 'open_webui:code_interpreter': |
491 | 492 | content_stripped, original_whitespace = split_content_and_whitespace(content) |
@@ -521,9 +522,9 @@ def serialize_output(output: list) -> str: |
521 | 522 | output_attr = f' output="{html.escape(output_json)}"' |
522 | 523 |
|
523 | 524 | if status == 'completed' or duration is not None or not is_last_item: |
524 | | - content += f'<details type="code_interpreter" done="true" duration="{duration or 0}"{output_attr}>\n<summary>Analyzed</summary>\n{display}\n</details>\n' |
| 525 | + content += f'<details type="code_interpreter" done="true" id="{item.get("id", "")}" duration="{duration or 0}"{output_attr}>\n<summary>Analyzed</summary>\n{display}\n</details>\n' |
525 | 526 | else: |
526 | | - content += f'<details type="code_interpreter" done="false"{output_attr}>\n<summary>Analyzing…</summary>\n{display}\n</details>\n' |
| 527 | + content += f'<details type="code_interpreter" done="false" id="{item.get("id", "")}"{output_attr}>\n<summary>Analyzing…</summary>\n{display}\n</details>\n' |
527 | 528 |
|
528 | 529 | return content.strip() |
529 | 530 |
|
@@ -2097,16 +2098,47 @@ def process_messages_with_output(messages: list[dict]) -> list[dict]: |
2097 | 2098 |
|
2098 | 2099 | For assistant messages with 'output' field, produces properly formatted |
2099 | 2100 | OpenAI-style messages (tool_calls + tool results). Strips 'output' before LLM. |
| 2101 | + Respects content edits and dropped <details> blocks by filtering output items |
| 2102 | + against the stored content field before conversion. |
2100 | 2103 | """ |
2101 | 2104 | processed = [] |
2102 | 2105 |
|
2103 | 2106 | for message in messages: |
2104 | 2107 | if message.get('role') == 'assistant' and message.get('output'): |
2105 | | - # Use output items for clean OpenAI-format messages |
2106 | | - output_messages = convert_output_to_messages(message['output'], raw=True) |
| 2108 | + # Drop output items for <details> blocks removed from content |
| 2109 | + filtered_output = filter_output_by_content( |
| 2110 | + message['output'], message.get('content', '') |
| 2111 | + ) |
| 2112 | + |
| 2113 | + # Use content for text (respects edits), strip <details> blocks |
| 2114 | + edited_text = re.sub( |
| 2115 | + r'<details\b[^>]*>.*?</details>', '', |
| 2116 | + message.get('content', ''), flags=re.S, |
| 2117 | + ).strip() |
| 2118 | + |
| 2119 | + # Replace the first message item's text with the edited content, |
| 2120 | + # preserving the natural order of structured items (reasoning before |
| 2121 | + # text, tool calls in sequence) via convert_output_to_messages. |
| 2122 | + used_edited_text = False |
| 2123 | + modified_output = [] |
| 2124 | + for item in filtered_output: |
| 2125 | + if item.get('type') == 'message' and not used_edited_text: |
| 2126 | + modified_output.append({ |
| 2127 | + **item, |
| 2128 | + 'content': [{'type': 'output_text', 'text': edited_text}], |
| 2129 | + }) |
| 2130 | + used_edited_text = True |
| 2131 | + else: |
| 2132 | + modified_output.append(item) |
| 2133 | + |
| 2134 | + output_messages = convert_output_to_messages(modified_output, raw=True) |
2107 | 2135 | if output_messages: |
| 2136 | + if not used_edited_text and edited_text: |
| 2137 | + output_messages.append({'role': 'assistant', 'content': edited_text}) |
2108 | 2138 | processed.extend(output_messages) |
2109 | | - continue |
| 2139 | + elif edited_text: |
| 2140 | + processed.append({'role': 'assistant', 'content': edited_text}) |
| 2141 | + continue |
2110 | 2142 |
|
2111 | 2143 | # Strip 'output' field before adding (LLM shouldn't see it) |
2112 | 2144 | clean_message = {k: v for k, v in message.items() if k != 'output'} |
|
0 commit comments