Skip to content

Renderers

Renderers that produce HTML from the tapes used by various GUI scripts.

Modules:

Functions:

render_agent_tree(agent, show_nodes=True, indent_increment=4)

Renders an ASCII tree representation of an agent's hierarchical structure.

This function creates a visual tree diagram showing the relationships between agents, their nodes, and subagents using ASCII characters. Each level is indented to show the hierarchy.

Parameters:

  • agent (Agent) –

    The root agent object to render the tree from.

  • show_nodes (bool, default: True ) –

    Whether to display the agent's nodes in the tree. Defaults to True.

  • indent_increment (int, default: 4 ) –

    Number of spaces to indent each level. Defaults to 4.

Returns:

  • str ( str ) –

    A string containing the ASCII tree representation.

Example
> The Manager
    .node1
    .node2
    > His Assistant 1
        .node1
        .node2
    > His Helper 2
        .node1
        .node2
Source code in tapeagents/renderers/__init__.py
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
def render_agent_tree(agent: Agent, show_nodes: bool = True, indent_increment: int = 4) -> str:
    """
    Renders an ASCII tree representation of an agent's hierarchical structure.

    This function creates a visual tree diagram showing the relationships between agents,
    their nodes, and subagents using ASCII characters. Each level is indented to show
    the hierarchy.

    Args:
        agent (Agent): The root agent object to render the tree from.
        show_nodes (bool, optional): Whether to display the agent's nodes in the tree.
            Defaults to True.
        indent_increment (int, optional): Number of spaces to indent each level.
            Defaults to 4.

    Returns:
        str: A string containing the ASCII tree representation.

    Example:
        ```
        > The Manager
            .node1
            .node2
            > His Assistant 1
                .node1
                .node2
            > His Helper 2
                .node1
                .node2
        ```
    """

    def render(agent: Agent, indent: int = 0) -> str:
        lines = [f"{' ' * indent}> {agent.name}"]
        if show_nodes:
            lines.extend([f"{' ' * (indent + indent_increment)}.{node.name}" for node in agent.nodes])
        for subagent in agent.subagents:
            lines.append(render(subagent, indent + indent_increment))
        return "\n".join(lines)

    return render(agent)

render_dialog_plain_text(tape)

Renders a dialog tape into a plain text format.

Takes a DialogTape object containing conversation steps and formats them into human-readable text, with each dialog step on a new line prefixed by the speaker/action type.

Parameters:

  • tape (Union[DialogTape, None]) –

    A DialogTape object containing conversation steps, or None

Returns:

  • str ( str ) –

    A string containing the formatted dialog, with each step on a new line. Returns empty string if tape is None.

Source code in tapeagents/renderers/__init__.py
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
def render_dialog_plain_text(tape: DialogTape | None) -> str:
    """
    Renders a dialog tape into a plain text format.

    Takes a DialogTape object containing conversation steps and formats them into human-readable text,
    with each dialog step on a new line prefixed by the speaker/action type.

    Args:
        tape (Union[DialogTape, None]): A DialogTape object containing conversation steps, or None

    Returns:
        str: A string containing the formatted dialog, with each step on a new line.
            Returns empty string if tape is None.
    """
    if tape is None:
        return ""
    lines = []
    for step in tape:
        if isinstance(step, UserStep):
            lines.append(f"User: {step.content}")
        elif isinstance(step, AssistantStep):
            lines.append(f"Assistant: {step.content}")
        elif isinstance(step, ToolCalls):
            for tc in step.tool_calls:
                lines.append(f"Tool calls: {tc.function}")
        elif isinstance(step, ToolResult):
            lines.append(f"Tool result: {step.content}")
    return "\n".join(lines)

render_tape_with_prompts(tape, renderer)

Renders a tape with prompts using the specified renderer.

This function combines the tape's LLM calls with the renderer's style and rendering to produce a complete rendered output of the tape's content.

Parameters:

  • tape (Tape) –

    The tape object to be rendered

  • renderer (BasicRenderer) –

    The renderer to use for rendering the tape

Returns:

  • str

    The rendered tape content with applied styling, combining both the renderer's style and the tape's content with LLM calls

Source code in tapeagents/renderers/__init__.py
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
def render_tape_with_prompts(tape: Tape, renderer: BasicRenderer):
    """
    Renders a tape with prompts using the specified renderer.

    This function combines the tape's LLM calls with the renderer's style and rendering
    to produce a complete rendered output of the tape's content.

    Args:
        tape (Tape): The tape object to be rendered
        renderer (BasicRenderer): The renderer to use for rendering the tape

    Returns:
        (str): The rendered tape content with applied styling, combining both
             the renderer's style and the tape's content with LLM calls
    """
    llm_calls = retrieve_tape_llm_calls(tape)
    return renderer.style + renderer.render_tape(tape, llm_calls)

to_pretty_str(a, prefix='', indent=2)

Convert any Python object to a pretty formatted string representation.

This function recursively formats nested data structures (lists and dictionaries) with proper indentation and line breaks for improved readability.

Parameters:

  • a (Any) –

    The object to be formatted. Can be a list, dictionary, or any other type.

  • prefix (str, default: '' ) –

    String to prepend to each line. Used for recursive indentation. Defaults to "".

  • indent (int, default: 2 ) –

    Number of spaces to use for each level of indentation. Defaults to 2.

Returns:

  • str ( str ) –

    A formatted string representation of the input object.

Example
data = {"foo": [1, 2, {"bar": "baz"}]}
print(to_pretty_str(data))

foo: - 1 - 2 - bar: baz

Source code in tapeagents/renderers/__init__.py
 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
def to_pretty_str(a: Any, prefix: str = "", indent: int = 2) -> str:
    """Convert any Python object to a pretty formatted string representation.

    This function recursively formats nested data structures (lists and dictionaries)
    with proper indentation and line breaks for improved readability.

    Args:
        a (Any): The object to be formatted. Can be a list, dictionary, or any other type.
        prefix (str, optional): String to prepend to each line. Used for recursive indentation. Defaults to "".
        indent (int, optional): Number of spaces to use for each level of indentation. Defaults to 2.

    Returns:
        str: A formatted string representation of the input object.

    Example:
        ```python
        data = {"foo": [1, 2, {"bar": "baz"}]}
        print(to_pretty_str(data))
        ```
        foo:
          - 1
          - 2
          - bar: baz
    """
    view = ""
    if isinstance(a, list) and len(a):
        if len(str(a)) < 80:
            view = str(a)
        else:
            lines = []
            for item in a:
                value_view = to_pretty_str(item, prefix + " " * indent)
                if "\n" in value_view:
                    value_view = f"\n{value_view}"
                lines.append(f"{prefix}- " + value_view)
            view = "\n".join(lines)
    elif isinstance(a, dict) and len(a):
        lines = []
        for k, v in a.items():
            value_view = to_pretty_str(v, prefix + " " * indent)
            if "\n" in value_view:
                value_view = f"\n{value_view}"
            lines.append(f"{prefix}{k}: {value_view}")
        view = "\n".join(lines)
    else:
        view = str(a)
    return view