{# ------------------------------------------------------------------------ #}
{# 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 -%}
{# ------------------------------------------------------------------------ #}
{# 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_interaction                                                #}
{# Renders individual interactions using model-specific control tokens.     #}
{# ------------------------------------------------------------------------ #}
{%- macro render_interaction(interaction) -%}
    {{- render_role_header(interaction['role']) -}}

    {%- if interaction['content'] is string -%}
        {{- interaction['content'] | safe -}}
    {%- else -%}
        {%- for content in interaction['content'] -%}
            {{- content | safe -}}
        {%- endfor -%}
    {%- endif -%}

    {{- 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_call.function | tojson(ensure_ascii=False) -}}
            {{- tc_tokens.end -}}
        {%- endfor -%}
    {%- endif -%}

    {{- end_of_message -}}
{%- 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 -%}
            {{- t | tojson(indent=2, ensure_ascii=False) -}}
        {%- endfor -%}
        {{- "\n\n" -}}
        {{- "To invoke a tool, wrap the tool's json schema in the delimiters " + tc_tokens.start + " and " + tc_tokens.end + ".\n" -}}
    {%- endif -%}
    {%- if has_system -%}
        {%- if interactions[0]['content'] is string -%}
            {{- interactions[0]['content'] | safe -}}
        {%- else -%}
            {%- for content in interactions[0]['content'] -%}
                {{- content | safe -}}
            {%- endfor -%}
        {%- endif -%}
    {%- 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) -}}
    {%- else -%}
        {{- render_interaction(interaction) -}}
    {%- endif -%}
{%- endfor -%}
{# Generation prompt #}
{%- if add_generation_prompt -%}
    {{- render_role_header('agent') -}}
{%- endif -%}
{%- if prefill is not none -%}
    {{- prefill -}}
{%- endif -%}
