|
95 | 95 | convert_logit_bias_input_to_json, |
96 | 96 | get_content_from_message, |
97 | 97 | convert_output_to_messages, |
| 98 | + filter_output_by_content, |
98 | 99 | ) |
99 | 100 | from open_webui.utils.tools import ( |
100 | 101 | get_tools, |
@@ -424,9 +425,9 @@ def serialize_output(output: list) -> str: |
424 | 425 | ) |
425 | 426 |
|
426 | 427 | if status == "completed" or duration is not None or not is_last_item: |
427 | | - 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' |
| 428 | + 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' |
428 | 429 | else: |
429 | | - content = f'{content}<details type="reasoning" done="false">\n<summary>Thinking…</summary>\n{display}\n</details>\n' |
| 430 | + content = f'{content}<details type="reasoning" done="false" id="{item.get("id", "")}">\n<summary>Thinking…</summary>\n{display}\n</details>\n' |
430 | 431 |
|
431 | 432 | elif item_type == "open_webui:code_interpreter": |
432 | 433 | content_stripped, original_whitespace = split_content_and_whitespace( |
@@ -466,9 +467,9 @@ def serialize_output(output: list) -> str: |
466 | 467 | output_attr = f' output="{html.escape(output_json)}"' |
467 | 468 |
|
468 | 469 | if status == "completed" or duration is not None or not is_last_item: |
469 | | - content += f'<details type="code_interpreter" done="true" duration="{duration or 0}"{output_attr}>\n<summary>Analyzed</summary>\n{display}\n</details>\n' |
| 470 | + 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' |
470 | 471 | else: |
471 | | - content += f'<details type="code_interpreter" done="false"{output_attr}>\n<summary>Analyzing…</summary>\n{display}\n</details>\n' |
| 472 | + content += f'<details type="code_interpreter" done="false" id="{item.get("id", "")}"{output_attr}>\n<summary>Analyzing…</summary>\n{display}\n</details>\n' |
472 | 473 |
|
473 | 474 | return content.strip() |
474 | 475 |
|
@@ -2049,11 +2050,40 @@ def process_messages_with_output(messages: list[dict]) -> list[dict]: |
2049 | 2050 |
|
2050 | 2051 | for message in messages: |
2051 | 2052 | if message.get("role") == "assistant" and message.get("output"): |
2052 | | - # Use output items for clean OpenAI-format messages |
2053 | | - output_messages = convert_output_to_messages(message["output"], raw=True) |
| 2053 | + # Drop output items for <details> blocks removed from content |
| 2054 | + output = filter_output_by_content( |
| 2055 | + message["output"], message.get("content", "") |
| 2056 | + ) |
| 2057 | + |
| 2058 | + # Use content for text (respects edits), output for structured items |
| 2059 | + content = re.sub( |
| 2060 | + r"<details\b[^>]*>.*?</details>", "", |
| 2061 | + message.get("content", ""), flags=re.S, |
| 2062 | + ).strip() |
| 2063 | + non_message_items = [ |
| 2064 | + i for i in output if i.get("type") != "message" |
| 2065 | + ] |
| 2066 | + output_messages = convert_output_to_messages(non_message_items, raw=True) |
| 2067 | + |
2054 | 2068 | if output_messages: |
| 2069 | + # Prepend edited text to first assistant message |
| 2070 | + for om in output_messages: |
| 2071 | + if om.get("role") == "assistant": |
| 2072 | + om["content"] = ( |
| 2073 | + (content + "\n" + om["content"]).strip() |
| 2074 | + if om.get("content") |
| 2075 | + else content |
| 2076 | + ) |
| 2077 | + content = "" |
| 2078 | + break |
| 2079 | + if content: |
| 2080 | + output_messages.insert( |
| 2081 | + 0, {"role": "assistant", "content": content} |
| 2082 | + ) |
2055 | 2083 | processed.extend(output_messages) |
2056 | | - continue |
| 2084 | + elif content: |
| 2085 | + processed.append({"role": "assistant", "content": content}) |
| 2086 | + continue |
2057 | 2087 |
|
2058 | 2088 | # Strip 'output' field before adding (LLM shouldn't see it) |
2059 | 2089 | clean_message = {k: v for k, v in message.items() if k != "output"} |
|
0 commit comments