File size: 6,333 Bytes
3943768
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
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):
            # in case in image like structure
            content = "\n".join([x["text"] for x in content if x.get("type") == "text"])

        if not content or not content.strip():
            continue

        # Add a horizontal rule between messages (except before the first one)
        if i > 0:
            markdown += f"{markdown_mark}\n\n"

        # Add an emoji based on the role
        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 there is agent name mentioned, update the role and the emoji
        if 'name' in message and message['name']:
            # turns 'chat_agent' to 'Chat Agent'
            real_role = message['name']
            real_role = ' '.join(word.capitalize() for word in real_role.split('_'))

            if message['name'] == 'chat_agent':
                # put bubble emoji for chat agent
                emoji = "πŸ’¬"
            if message['name'] == 'human_proxy_agent':
                # put human emoji for human proxy agent
                emoji = "πŸ‘€"
            if message['name'] == 'code_writer_agent':
                # put code emoji for code writer agent
                emoji = "πŸ€–"
            if message['name'] == 'code_executor_agent':
                # put code emoji for code executor agent
                emoji = "🧠"

        # Format the role
        if cute:
            markdown += f"### {emoji} {real_role}\n\n"
        else:
            markdown += f"### {real_role}\n\n"

        # Process the content
        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:
                # If we're in a code block, add the line as is
                markdown += line + "\n"
            else:
                # For non-code block content, wrap long lines
                wrapped_lines = wrap_long_lines(line)
                markdown += wrapped_lines + "\n"

        markdown += "\n"  # Add an extra newline for spacing between messages

    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 = "# Chat History\n\n"
    markdown = ""
    for i, message in enumerate(chat_history):
        role = message["role"].capitalize()
        content = message["content"]

        if isinstance(content, list):
            # in case in image like structure
            content = "\n".join([x["text"] for x in content if x.get("type") == "text"])

        if not content or not content.strip():
            continue

        # Add a horizontal rule between messages (except before the first one)
        if i > 0:
            markdown += f"{markdown_mark}\n\n"

        # Add an emoji based on the role
        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 there is agent name mentioned, update the role and the emoji
        if 'name' in message and message['name']:
            # turns 'chat_agent' to 'Chat Agent'
            real_role = message['name']
            real_role = ' '.join(word.capitalize() for word in real_role.split('_'))

            if message['name'] == 'chat_agent':
                # put bubble emoji for chat agent
                emoji = "πŸ’¬"
            if message['name'] == 'human_proxy_agent':
                # put human emoji for human proxy agent
                emoji = "πŸ‘€"
            if message['name'] == 'code_writer_agent':
                # put code emoji for code writer agent
                emoji = "πŸ€–"
            if message['name'] == 'code_executor_agent':
                # put code emoji for code executor agent
                emoji = "🧠"

        # Format the role
        if cute:
            markdown += f"### {emoji} {real_role}\n\n"
        else:
            markdown += f"### {real_role}\n\n"

        # Split content into code blocks and non-code blocks
        parts = re.split(r"(```[\s\S]*?```)", content)

        for part in parts:
            if part.startswith("```") and part.endswith("```"):
                # This is a code block, add it as-is
                markdown += part + "\n\n"
            else:
                # This is not a code block, wrap it
                wrapped_content = textwrap.wrap(part.strip(), width=80)
                markdown += "\n".join(wrapped_content) + "\n\n"

    return markdown.strip()