{# ------------------------------------------------------------------------ #}
{# Template configuration                                                   #}
{# ------------------------------------------------------------------------ #}
{%- set add_generation_prompt = add_generation_prompt or true -%}
{%- set prefill = prefill or none -%}

{# Tool calling delimiters from capabilities.yaml #}
{%- if capabilities is defined and capabilities.tool_calling is defined -%}
    {%- set tc_tokens = capabilities.tool_calling.formats[0].tokens -%}
{%- else -%}
    {%- set tc_tokens = none -%}
{%- endif -%}
{%- import "tool_macros.jinja" as tool_macros -%}
{# ------------------------------------------------------------------------ #}
{# Macro: render_role_header                                                #}
{# ------------------------------------------------------------------------ #}
{%- macro render_role_header(role_key) -%}
    {%- set role = roles.get(role_key) -%}
    {%- if role is not none -%}
        {{- role.role_start_tag + role.role_name + role.role_end_tag -}}
    {%- endif -%}
{%- endmacro -%}
{# ------------------------------------------------------------------------ #}
{# Macro: render_content                                                    #}
{# ------------------------------------------------------------------------ #}
{%- macro render_content(content) -%}
    {%- if content is string -%}
        {{- content | safe -}}
    {%- else -%}
        {%- for item in content -%}
            {{- item | safe -}}
        {%- endfor -%}
    {%- endif -%}
{%- endmacro -%}
{# ------------------------------------------------------------------------ #}
{# Macro: render_interaction                                                #}
{# Renders individual interactions using model-specific control tokens.     #}
{# ------------------------------------------------------------------------ #}
{%- macro render_interaction(interaction) -%}
    {{- render_role_header(interaction['role']) -}}
    {{- render_content(interaction['content']) -}}
    {{- end_of_sequence -}}
{%- endmacro -%}
{# ------------------------------------------------------------------------ #}
{# Macro: render_tool_call                                                  #}
{# Renders an assistant turn containing a tool call in history.             #}
{# ------------------------------------------------------------------------ #}
{%- macro render_tool_call(interaction) -%}
    {{- render_role_header('agent') -}}

    {%- if interaction['content'] is string and interaction['content'] -%}
        {{- interaction['content'] | safe + "\n" -}}
    {%- endif -%}

    {%- if tc_tokens -%}
        {%- for tool_call in interaction['tool_calls'] -%}
            {{- tc_tokens.start -}}
            {{- tool_macros.render_python_call(tool_call) -}}
            {{- tc_tokens.end -}}
        {%- endfor -%}
    {%- endif -%}

    {{- end_of_message -}}
{%- endmacro -%}
{# ------------------------------------------------------------------------ #}
{# Macro: render_tool_result                                                #}
{# ------------------------------------------------------------------------ #}
{%- macro render_tool_result(interaction) -%}
    {{- render_role_header('tool') -}}
    {{- render_content(interaction['content']) -}}
    {{- end_of_sequence -}}
{%- endmacro -%}
{# ------------------------------------------------------------------------ #}
{# Main template                                                            #}
{# ------------------------------------------------------------------------ #}
{{- begin_of_text -}}
{# System turn + tools - present if there is a system message or tools #}
{%- set has_system = interactions and interactions[0]['role'] == 'system' -%}
{%- set has_tools = tools is defined and tools and tc_tokens -%}
{%- if has_system or has_tools -%}
    {{- render_role_header('system') -}}
    {%- if has_tools -%}
        {{- "Environment: ipython\nYou have the following tools available to you:\n" }}
        {%- for t in tools -%}
            {{- "\n" if loop.first else "\n\n" -}}
            {{- tool_macros.render_tool(t) -}}
        {%- endfor -%}
        {{- "\n\n" -}}
        {{- "To invoke a tool, emit exactly one Python-style function call using keyword arguments inside the delimiters " + tc_tokens.start + " and " + tc_tokens.end + ".\n" -}}
        {{- "Use double quotes for strings, True or False for booleans, and None for null values.\n" -}}
    {%- endif -%}
    {%- if has_system -%}
        {{- render_content(interactions[0]['content']) -}}
    {%- endif -%}
    {{- end_of_sequence -}}
{%- endif -%}
{# Render conversation #}
{%- for interaction in interactions -%}
    {%- if has_system and loop.first -%}
        {# Already rendered system message above #}
    {%- elif interaction.tool_calls is defined and interaction.tool_calls -%}
        {{- render_tool_call(interaction) -}}
    {%- elif interaction['role'] == 'tool' or (interaction.tool_call_id is defined and interaction.tool_call_id) -%}
        {{- render_tool_result(interaction) -}}
    {%- else -%}
        {{- render_interaction(interaction) -}}
    {%- endif -%}
{%- endfor -%}
{# Generation prompt #}
{%- if add_generation_prompt -%}
    {{- render_role_header('agent') -}}
{%- endif -%}
{%- if prefill is not none -%}
    {{- prefill -}}
{%- endif -%}
