|
97 | 97 | convert_logit_bias_input_to_json, |
98 | 98 | get_content_from_message, |
99 | 99 | convert_output_to_messages, |
| 100 | + filter_output_by_content, |
100 | 101 | strip_empty_content_blocks, |
101 | 102 | ) |
102 | 103 | from open_webui.utils.tools import ( |
@@ -480,9 +481,9 @@ def serialize_output(output: list) -> str: |
480 | 481 | ) |
481 | 482 |
|
482 | 483 | if status == 'completed' or duration is not None or not is_last_item: |
483 | | - 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' |
| 484 | + 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' |
484 | 485 | else: |
485 | | - content = f'{content}<details type="reasoning" done="false">\n<summary>Thinking…</summary>\n{display}\n</details>\n' |
| 486 | + content = f'{content}<details type="reasoning" done="false" id="{item.get("id", "")}">\n<summary>Thinking…</summary>\n{display}\n</details>\n' |
486 | 487 |
|
487 | 488 | elif item_type == 'open_webui:code_interpreter': |
488 | 489 | content_stripped, original_whitespace = split_content_and_whitespace(content) |
@@ -518,9 +519,9 @@ def serialize_output(output: list) -> str: |
518 | 519 | output_attr = f' output="{html.escape(output_json)}"' |
519 | 520 |
|
520 | 521 | if status == 'completed' or duration is not None or not is_last_item: |
521 | | - content += f'<details type="code_interpreter" done="true" duration="{duration or 0}"{output_attr}>\n<summary>Analyzed</summary>\n{display}\n</details>\n' |
| 522 | + 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' |
522 | 523 | else: |
523 | | - content += f'<details type="code_interpreter" done="false"{output_attr}>\n<summary>Analyzing…</summary>\n{display}\n</details>\n' |
| 524 | + content += f'<details type="code_interpreter" done="false" id="{item.get("id", "")}"{output_attr}>\n<summary>Analyzing…</summary>\n{display}\n</details>\n' |
524 | 525 |
|
525 | 526 | return content.strip() |
526 | 527 |
|
@@ -2078,16 +2079,47 @@ def process_messages_with_output(messages: list[dict]) -> list[dict]: |
2078 | 2079 |
|
2079 | 2080 | For assistant messages with 'output' field, produces properly formatted |
2080 | 2081 | OpenAI-style messages (tool_calls + tool results). Strips 'output' before LLM. |
| 2082 | + Respects content edits and dropped <details> blocks by filtering output items |
| 2083 | + against the stored content field before conversion. |
2081 | 2084 | """ |
2082 | 2085 | processed = [] |
2083 | 2086 |
|
2084 | 2087 | for message in messages: |
2085 | 2088 | if message.get('role') == 'assistant' and message.get('output'): |
2086 | | - # Use output items for clean OpenAI-format messages |
2087 | | - output_messages = convert_output_to_messages(message['output'], raw=True) |
| 2089 | + # Drop output items for <details> blocks removed from content |
| 2090 | + filtered_output = filter_output_by_content( |
| 2091 | + message['output'], message.get('content', '') |
| 2092 | + ) |
| 2093 | + |
| 2094 | + # Use content for text (respects edits), strip <details> blocks |
| 2095 | + edited_text = re.sub( |
| 2096 | + r'<details\b[^>]*>.*?</details>', '', |
| 2097 | + message.get('content', ''), flags=re.S, |
| 2098 | + ).strip() |
| 2099 | + |
| 2100 | + # Replace the first message item's text with the edited content, |
| 2101 | + # preserving the natural order of structured items (reasoning before |
| 2102 | + # text, tool calls in sequence) via convert_output_to_messages. |
| 2103 | + used_edited_text = False |
| 2104 | + modified_output = [] |
| 2105 | + for item in filtered_output: |
| 2106 | + if item.get('type') == 'message' and not used_edited_text: |
| 2107 | + modified_output.append({ |
| 2108 | + **item, |
| 2109 | + 'content': [{'type': 'output_text', 'text': edited_text}], |
| 2110 | + }) |
| 2111 | + used_edited_text = True |
| 2112 | + else: |
| 2113 | + modified_output.append(item) |
| 2114 | + |
| 2115 | + output_messages = convert_output_to_messages(modified_output, raw=True) |
2088 | 2116 | if output_messages: |
| 2117 | + if not used_edited_text and edited_text: |
| 2118 | + output_messages.append({'role': 'assistant', 'content': edited_text}) |
2089 | 2119 | processed.extend(output_messages) |
2090 | | - continue |
| 2120 | + elif edited_text: |
| 2121 | + processed.append({'role': 'assistant', 'content': edited_text}) |
| 2122 | + continue |
2091 | 2123 |
|
2092 | 2124 | # Strip 'output' field before adding (LLM shouldn't see it) |
2093 | 2125 | clean_message = {k: v for k, v in message.items() if k != 'output'} |
|
0 commit comments