|
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 ( |
@@ -475,9 +476,9 @@ def serialize_output(output: list) -> str: |
475 | 476 | ) |
476 | 477 |
|
477 | 478 | if status == 'completed' or duration is not None or not is_last_item: |
478 | | - 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' |
| 479 | + 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' |
479 | 480 | else: |
480 | | - content = f'{content}<details type="reasoning" done="false">\n<summary>Thinking…</summary>\n{display}\n</details>\n' |
| 481 | + content = f'{content}<details type="reasoning" done="false" id="{item.get("id", "")}">\n<summary>Thinking…</summary>\n{display}\n</details>\n' |
481 | 482 |
|
482 | 483 | elif item_type == 'open_webui:code_interpreter': |
483 | 484 | content_stripped, original_whitespace = split_content_and_whitespace(content) |
@@ -513,9 +514,9 @@ def serialize_output(output: list) -> str: |
513 | 514 | output_attr = f' output="{html.escape(output_json)}"' |
514 | 515 |
|
515 | 516 | if status == 'completed' or duration is not None or not is_last_item: |
516 | | - content += f'<details type="code_interpreter" done="true" duration="{duration or 0}"{output_attr}>\n<summary>Analyzed</summary>\n{display}\n</details>\n' |
| 517 | + 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' |
517 | 518 | else: |
518 | | - content += f'<details type="code_interpreter" done="false"{output_attr}>\n<summary>Analyzing…</summary>\n{display}\n</details>\n' |
| 519 | + content += f'<details type="code_interpreter" done="false" id="{item.get("id", "")}"{output_attr}>\n<summary>Analyzing…</summary>\n{display}\n</details>\n' |
519 | 520 |
|
520 | 521 | return content.strip() |
521 | 522 |
|
@@ -2035,16 +2036,47 @@ def process_messages_with_output(messages: list[dict]) -> list[dict]: |
2035 | 2036 |
|
2036 | 2037 | For assistant messages with 'output' field, produces properly formatted |
2037 | 2038 | OpenAI-style messages (tool_calls + tool results). Strips 'output' before LLM. |
| 2039 | + Respects content edits and dropped <details> blocks by filtering output items |
| 2040 | + against the stored content field before conversion. |
2038 | 2041 | """ |
2039 | 2042 | processed = [] |
2040 | 2043 |
|
2041 | 2044 | for message in messages: |
2042 | 2045 | if message.get('role') == 'assistant' and message.get('output'): |
2043 | | - # Use output items for clean OpenAI-format messages |
2044 | | - output_messages = convert_output_to_messages(message['output'], raw=True) |
| 2046 | + # Drop output items for <details> blocks removed from content |
| 2047 | + filtered_output = filter_output_by_content( |
| 2048 | + message['output'], message.get('content', '') |
| 2049 | + ) |
| 2050 | + |
| 2051 | + # Use content for text (respects edits), strip <details> blocks |
| 2052 | + edited_text = re.sub( |
| 2053 | + r'<details\b[^>]*>.*?</details>', '', |
| 2054 | + message.get('content', ''), flags=re.S, |
| 2055 | + ).strip() |
| 2056 | + |
| 2057 | + # Replace the first message item's text with the edited content, |
| 2058 | + # preserving the natural order of structured items (reasoning before |
| 2059 | + # text, tool calls in sequence) via convert_output_to_messages. |
| 2060 | + used_edited_text = False |
| 2061 | + modified_output = [] |
| 2062 | + for item in filtered_output: |
| 2063 | + if item.get('type') == 'message' and not used_edited_text: |
| 2064 | + modified_output.append({ |
| 2065 | + **item, |
| 2066 | + 'content': [{'type': 'output_text', 'text': edited_text}], |
| 2067 | + }) |
| 2068 | + used_edited_text = True |
| 2069 | + else: |
| 2070 | + modified_output.append(item) |
| 2071 | + |
| 2072 | + output_messages = convert_output_to_messages(modified_output, raw=True) |
2045 | 2073 | if output_messages: |
| 2074 | + if not used_edited_text and edited_text: |
| 2075 | + output_messages.append({'role': 'assistant', 'content': edited_text}) |
2046 | 2076 | processed.extend(output_messages) |
2047 | | - continue |
| 2077 | + elif edited_text: |
| 2078 | + processed.append({'role': 'assistant', 'content': edited_text}) |
| 2079 | + continue |
2048 | 2080 |
|
2049 | 2081 | # Strip 'output' field before adding (LLM shouldn't see it) |
2050 | 2082 | clean_message = {k: v for k, v in message.items() if k != 'output'} |
|
0 commit comments