|
import re |
|
import textwrap |
|
from typing import List, Dict |
|
|
|
markdown_mark = "---" |
|
|
|
|
|
def chat_to_pretty_markdown( |
|
chat_history: List[Dict[str, str]], |
|
cute=False, |
|
assistant_name="Executor Agent", |
|
user_name="Coder Agent", |
|
dummy_name="Agent", |
|
) -> str: |
|
markdown = "" |
|
for i, message in enumerate(chat_history): |
|
role = message["role"].capitalize() |
|
content = message["content"] |
|
|
|
if isinstance(content, list): |
|
|
|
content = "\n".join([x["text"] for x in content if x.get("type") == "text"]) |
|
|
|
if not content or not content.strip(): |
|
continue |
|
|
|
|
|
if i > 0: |
|
markdown += f"{markdown_mark}\n\n" |
|
|
|
|
|
emoji = ( |
|
"π§ " |
|
if role.lower() == "assistant" |
|
else "π€" |
|
if role.lower() == "user" |
|
else "βΉοΈ" |
|
) |
|
real_role = ( |
|
assistant_name |
|
if role.lower() == "assistant" |
|
else user_name |
|
if role.lower() == "user" |
|
else dummy_name |
|
) |
|
|
|
if 'name' in message and message['name']: |
|
|
|
real_role = message['name'] |
|
real_role = ' '.join(word.capitalize() for word in real_role.split('_')) |
|
|
|
if message['name'] == 'chat_agent': |
|
|
|
emoji = "π¬" |
|
if message['name'] == 'human_proxy_agent': |
|
|
|
emoji = "π€" |
|
if message['name'] == 'code_writer_agent': |
|
|
|
emoji = "π€" |
|
if message['name'] == 'code_executor_agent': |
|
|
|
emoji = "π§ " |
|
|
|
|
|
if cute: |
|
markdown += f"### {emoji} {real_role}\n\n" |
|
else: |
|
markdown += f"### {real_role}\n\n" |
|
|
|
|
|
lines = content.split("\n") |
|
in_code_block = False |
|
for line in lines: |
|
if line.strip().startswith("```"): |
|
in_code_block = not in_code_block |
|
markdown += line + "\n" |
|
elif in_code_block: |
|
|
|
markdown += line + "\n" |
|
else: |
|
|
|
wrapped_lines = wrap_long_lines(line) |
|
markdown += wrapped_lines + "\n" |
|
|
|
markdown += "\n" |
|
|
|
return markdown.strip() |
|
|
|
|
|
def wrap_long_lines(line: str, max_width: int = 80) -> str: |
|
"""Wrap long lines while preserving existing line breaks and indentation.""" |
|
if len(line) <= max_width: |
|
return line |
|
|
|
words = line.split() |
|
wrapped_lines = [] |
|
current_line = words[0] |
|
current_indent = len(line) - len(line.lstrip()) |
|
indent = " " * current_indent |
|
|
|
for word in words[1:]: |
|
if len(current_line) + len(word) + 1 <= max_width: |
|
current_line += " " + word |
|
else: |
|
wrapped_lines.append(current_line) |
|
current_line = indent + word |
|
|
|
wrapped_lines.append(current_line) |
|
return "\n".join(wrapped_lines) |
|
|
|
|
|
def chat_to_pretty_markdown_simple( |
|
chat_history, |
|
cute=False, |
|
assistant_name="Executor Agent", |
|
user_name="Coder Agent", |
|
dummy_name="Agent", |
|
) -> str: |
|
|
|
markdown = "" |
|
for i, message in enumerate(chat_history): |
|
role = message["role"].capitalize() |
|
content = message["content"] |
|
|
|
if isinstance(content, list): |
|
|
|
content = "\n".join([x["text"] for x in content if x.get("type") == "text"]) |
|
|
|
if not content or not content.strip(): |
|
continue |
|
|
|
|
|
if i > 0: |
|
markdown += f"{markdown_mark}\n\n" |
|
|
|
|
|
emoji = ( |
|
"π§ " |
|
if role.lower() == "assistant" |
|
else "π€" |
|
if role.lower() == "user" |
|
else "βΉοΈ" |
|
) |
|
real_role = ( |
|
assistant_name |
|
if role.lower() == "assistant" |
|
else user_name |
|
if role.lower() == "user" |
|
else dummy_name |
|
) |
|
|
|
if 'name' in message and message['name']: |
|
|
|
real_role = message['name'] |
|
real_role = ' '.join(word.capitalize() for word in real_role.split('_')) |
|
|
|
if message['name'] == 'chat_agent': |
|
|
|
emoji = "π¬" |
|
if message['name'] == 'human_proxy_agent': |
|
|
|
emoji = "π€" |
|
if message['name'] == 'code_writer_agent': |
|
|
|
emoji = "π€" |
|
if message['name'] == 'code_executor_agent': |
|
|
|
emoji = "π§ " |
|
|
|
|
|
if cute: |
|
markdown += f"### {emoji} {real_role}\n\n" |
|
else: |
|
markdown += f"### {real_role}\n\n" |
|
|
|
|
|
parts = re.split(r"(```[\s\S]*?```)", content) |
|
|
|
for part in parts: |
|
if part.startswith("```") and part.endswith("```"): |
|
|
|
markdown += part + "\n\n" |
|
else: |
|
|
|
wrapped_content = textwrap.wrap(part.strip(), width=80) |
|
markdown += "\n".join(wrapped_content) + "\n\n" |
|
|
|
return markdown.strip() |
|
|