|
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 | ) |
101 | 102 | from open_webui.utils.tools import ( |
102 | 103 | get_tools, |
@@ -474,9 +475,9 @@ def serialize_output(output: list) -> str: |
474 | 475 | ) |
475 | 476 |
|
476 | 477 | if status == 'completed' or duration is not None or not is_last_item: |
477 | | - 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' |
| 478 | + 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' |
478 | 479 | else: |
479 | | - content = f'{content}<details type="reasoning" done="false">\n<summary>Thinking…</summary>\n{display}\n</details>\n' |
| 480 | + content = f'{content}<details type="reasoning" done="false" id="{item.get("id", "")}">\n<summary>Thinking…</summary>\n{display}\n</details>\n' |
480 | 481 |
|
481 | 482 | elif item_type == 'open_webui:code_interpreter': |
482 | 483 | content_stripped, original_whitespace = split_content_and_whitespace(content) |
@@ -512,9 +513,9 @@ def serialize_output(output: list) -> str: |
512 | 513 | output_attr = f' output="{html.escape(output_json)}"' |
513 | 514 |
|
514 | 515 | if status == 'completed' or duration is not None or not is_last_item: |
515 | | - content += f'<details type="code_interpreter" done="true" duration="{duration or 0}"{output_attr}>\n<summary>Analyzed</summary>\n{display}\n</details>\n' |
| 516 | + 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' |
516 | 517 | else: |
517 | | - content += f'<details type="code_interpreter" done="false"{output_attr}>\n<summary>Analyzing…</summary>\n{display}\n</details>\n' |
| 518 | + content += f'<details type="code_interpreter" done="false" id="{item.get("id", "")}"{output_attr}>\n<summary>Analyzing…</summary>\n{display}\n</details>\n' |
518 | 519 |
|
519 | 520 | return content.strip() |
520 | 521 |
|
@@ -2039,11 +2040,40 @@ def process_messages_with_output(messages: list[dict]) -> list[dict]: |
2039 | 2040 |
|
2040 | 2041 | for message in messages: |
2041 | 2042 | if message.get('role') == 'assistant' and message.get('output'): |
2042 | | - # Use output items for clean OpenAI-format messages |
2043 | | - output_messages = convert_output_to_messages(message['output'], raw=True) |
| 2043 | + # Drop output items for <details> blocks removed from content |
| 2044 | + output = filter_output_by_content( |
| 2045 | + message['output'], message.get('content', '') |
| 2046 | + ) |
| 2047 | + |
| 2048 | + # Use content for text (respects edits), output for structured items |
| 2049 | + content = re.sub( |
| 2050 | + r'<details\b[^>]*>.*?</details>', '', |
| 2051 | + message.get('content', ''), flags=re.S, |
| 2052 | + ).strip() |
| 2053 | + non_message_items = [ |
| 2054 | + i for i in output if i.get('type') != 'message' |
| 2055 | + ] |
| 2056 | + output_messages = convert_output_to_messages(non_message_items, raw=True) |
| 2057 | + |
2044 | 2058 | if output_messages: |
| 2059 | + # Prepend edited text to first assistant message |
| 2060 | + for om in output_messages: |
| 2061 | + if om.get('role') == 'assistant': |
| 2062 | + om['content'] = ( |
| 2063 | + (content + '\n' + om['content']).strip() |
| 2064 | + if om.get('content') |
| 2065 | + else content |
| 2066 | + ) |
| 2067 | + content = '' |
| 2068 | + break |
| 2069 | + if content: |
| 2070 | + output_messages.insert( |
| 2071 | + 0, {'role': 'assistant', 'content': content} |
| 2072 | + ) |
2045 | 2073 | processed.extend(output_messages) |
2046 | | - continue |
| 2074 | + elif content: |
| 2075 | + processed.append({'role': 'assistant', 'content': content}) |
| 2076 | + continue |
2047 | 2077 |
|
2048 | 2078 | # Strip 'output' field before adding (LLM shouldn't see it) |
2049 | 2079 | clean_message = {k: v for k, v in message.items() if k != 'output'} |
|
0 commit comments