|
96 | 96 | convert_logit_bias_input_to_json, |
97 | 97 | get_content_from_message, |
98 | 98 | convert_output_to_messages, |
| 99 | + filter_output_by_content, |
99 | 100 | ) |
100 | 101 | from open_webui.utils.tools import ( |
101 | 102 | get_tools, |
@@ -434,9 +435,9 @@ def serialize_output(output: list) -> str: |
434 | 435 | ) |
435 | 436 |
|
436 | 437 | if status == "completed" or duration is not None or not is_last_item: |
437 | | - 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' |
| 438 | + 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' |
438 | 439 | else: |
439 | | - content = f'{content}<details type="reasoning" done="false">\n<summary>Thinking…</summary>\n{display}\n</details>\n' |
| 440 | + content = f'{content}<details type="reasoning" done="false" id="{item.get("id", "")}">\n<summary>Thinking…</summary>\n{display}\n</details>\n' |
440 | 441 |
|
441 | 442 | elif item_type == "open_webui:code_interpreter": |
442 | 443 | content_stripped, original_whitespace = split_content_and_whitespace( |
@@ -476,9 +477,9 @@ def serialize_output(output: list) -> str: |
476 | 477 | output_attr = f' output="{html.escape(output_json)}"' |
477 | 478 |
|
478 | 479 | if status == "completed" or duration is not None or not is_last_item: |
479 | | - content += f'<details type="code_interpreter" done="true" duration="{duration or 0}"{output_attr}>\n<summary>Analyzed</summary>\n{display}\n</details>\n' |
| 480 | + 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' |
480 | 481 | else: |
481 | | - content += f'<details type="code_interpreter" done="false"{output_attr}>\n<summary>Analyzing…</summary>\n{display}\n</details>\n' |
| 482 | + content += f'<details type="code_interpreter" done="false" id="{item.get("id", "")}"{output_attr}>\n<summary>Analyzing…</summary>\n{display}\n</details>\n' |
482 | 483 |
|
483 | 484 | return content.strip() |
484 | 485 |
|
@@ -2069,11 +2070,40 @@ def process_messages_with_output(messages: list[dict]) -> list[dict]: |
2069 | 2070 |
|
2070 | 2071 | for message in messages: |
2071 | 2072 | if message.get("role") == "assistant" and message.get("output"): |
2072 | | - # Use output items for clean OpenAI-format messages |
2073 | | - output_messages = convert_output_to_messages(message["output"], raw=True) |
| 2073 | + # Drop output items for <details> blocks removed from content |
| 2074 | + output = filter_output_by_content( |
| 2075 | + message["output"], message.get("content", "") |
| 2076 | + ) |
| 2077 | + |
| 2078 | + # Use content for text (respects edits), output for structured items |
| 2079 | + content = re.sub( |
| 2080 | + r"<details\b[^>]*>.*?</details>", "", |
| 2081 | + message.get("content", ""), flags=re.S, |
| 2082 | + ).strip() |
| 2083 | + non_message_items = [ |
| 2084 | + i for i in output if i.get("type") != "message" |
| 2085 | + ] |
| 2086 | + output_messages = convert_output_to_messages(non_message_items, raw=True) |
| 2087 | + |
2074 | 2088 | if output_messages: |
| 2089 | + # Prepend edited text to first assistant message |
| 2090 | + for om in output_messages: |
| 2091 | + if om.get("role") == "assistant": |
| 2092 | + om["content"] = ( |
| 2093 | + (content + "\n" + om["content"]).strip() |
| 2094 | + if om.get("content") |
| 2095 | + else content |
| 2096 | + ) |
| 2097 | + content = "" |
| 2098 | + break |
| 2099 | + if content: |
| 2100 | + output_messages.insert( |
| 2101 | + 0, {"role": "assistant", "content": content} |
| 2102 | + ) |
2075 | 2103 | processed.extend(output_messages) |
2076 | | - continue |
| 2104 | + elif content: |
| 2105 | + processed.append({"role": "assistant", "content": content}) |
| 2106 | + continue |
2077 | 2107 |
|
2078 | 2108 | # Strip 'output' field before adding (LLM shouldn't see it) |
2079 | 2109 | clean_message = {k: v for k, v in message.items() if k != "output"} |
|
0 commit comments