|
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, |
@@ -418,9 +419,9 @@ def serialize_output(output: list) -> str: |
418 | 419 | ) |
419 | 420 |
|
420 | 421 | if status == "completed" or duration is not None or not is_last_item: |
421 | | - 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' |
| 422 | + 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' |
422 | 423 | else: |
423 | | - content = f'{content}<details type="reasoning" done="false">\n<summary>Thinking…</summary>\n{display}\n</details>\n' |
| 424 | + content = f'{content}<details type="reasoning" done="false" id="{item.get("id", "")}">\n<summary>Thinking…</summary>\n{display}\n</details>\n' |
424 | 425 |
|
425 | 426 | elif item_type == "open_webui:code_interpreter": |
426 | 427 | content_stripped, original_whitespace = split_content_and_whitespace( |
@@ -460,9 +461,9 @@ def serialize_output(output: list) -> str: |
460 | 461 | output_attr = f' output="{html.escape(output_json)}"' |
461 | 462 |
|
462 | 463 | if status == "completed" or duration is not None or not is_last_item: |
463 | | - content += f'<details type="code_interpreter" done="true" duration="{duration or 0}"{output_attr}>\n<summary>Analyzed</summary>\n{display}\n</details>\n' |
| 464 | + 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' |
464 | 465 | else: |
465 | | - content += f'<details type="code_interpreter" done="false"{output_attr}>\n<summary>Analyzing…</summary>\n{display}\n</details>\n' |
| 466 | + content += f'<details type="code_interpreter" done="false" id="{item.get("id", "")}"{output_attr}>\n<summary>Analyzing…</summary>\n{display}\n</details>\n' |
466 | 467 |
|
467 | 468 | return content.strip() |
468 | 469 |
|
@@ -1977,11 +1978,40 @@ def process_messages_with_output(messages: list[dict]) -> list[dict]: |
1977 | 1978 |
|
1978 | 1979 | for message in messages: |
1979 | 1980 | if message.get("role") == "assistant" and message.get("output"): |
1980 | | - # Use output items for clean OpenAI-format messages |
1981 | | - output_messages = convert_output_to_messages(message["output"], raw=True) |
| 1981 | + # Drop output items for <details> blocks removed from content |
| 1982 | + output = filter_output_by_content( |
| 1983 | + message["output"], message.get("content", "") |
| 1984 | + ) |
| 1985 | + |
| 1986 | + # Use content for text (respects edits), output for structured items |
| 1987 | + content = re.sub( |
| 1988 | + r"<details\b[^>]*>.*?</details>", "", |
| 1989 | + message.get("content", ""), flags=re.S, |
| 1990 | + ).strip() |
| 1991 | + non_message_items = [ |
| 1992 | + i for i in output if i.get("type") != "message" |
| 1993 | + ] |
| 1994 | + output_messages = convert_output_to_messages(non_message_items, raw=True) |
| 1995 | + |
1982 | 1996 | if output_messages: |
| 1997 | + # Prepend edited text to first assistant message |
| 1998 | + for om in output_messages: |
| 1999 | + if om.get("role") == "assistant": |
| 2000 | + om["content"] = ( |
| 2001 | + (content + "\n" + om["content"]).strip() |
| 2002 | + if om.get("content") |
| 2003 | + else content |
| 2004 | + ) |
| 2005 | + content = "" |
| 2006 | + break |
| 2007 | + if content: |
| 2008 | + output_messages.insert( |
| 2009 | + 0, {"role": "assistant", "content": content} |
| 2010 | + ) |
1983 | 2011 | processed.extend(output_messages) |
1984 | | - continue |
| 2012 | + elif content: |
| 2013 | + processed.append({"role": "assistant", "content": content}) |
| 2014 | + continue |
1985 | 2015 |
|
1986 | 2016 | # Strip 'output' field before adding (LLM shouldn't see it) |
1987 | 2017 | clean_message = {k: v for k, v in message.items() if k != "output"} |
|
0 commit comments