{# ------------------------------------------------------------------------ #}
{# EXAMPLE CHAT TEMPLATE                                                   #}
{# Copy this file to your model directory and customize as needed.         #}
{# ------------------------------------------------------------------------ #}
{# Available context variables:                                            #}
{#                                                                         #}
{# From control_tokens.json:                                               #}
{#   - begin_of_text, end_of_sequence, end_of_message                      #}
{#   - roles.system, roles.user, roles.agent, roles.tool                   #}
{#                                                                         #}
{# From capabilities.yaml:                                                 #}
{#   - capabilities.<name>.tokens.<key>                                    #}
{#   - capabilities.<name>.placeholders.<key>                              #}
{#                                                                         #}
{# Runtime variables:                                                      #}
{#   - interactions: list of {role, content} message dicts                 #}
{#   - add_generation_prompt: whether to add assistant turn prefix         #}
{#   - prefill: optional string to prefill assistant response              #}
{#   - reasoning: whether to enable reasoning/thinking mode                #}
{#   - task: optional task name for task-specific formatting               #}
{#   - tools: list of tool schemas (when tool calling is enabled)          #}
{# ------------------------------------------------------------------------ #}
{%- 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_interaction                                                #}
{# Renders a single message with role tags and content.                     #}
{# ------------------------------------------------------------------------ #}
{%- macro render_interaction(interaction) -%}
    {%- set role = roles.get(interaction['role']) -%}
    {%- if role is not none -%}
        {{- role.role_start_tag + role.role_name + role.role_end_tag -}}
    {%- endif -%}

    {# Handle string content #}
    {%- if interaction['content'] is string -%}
        {{- interaction['content'] | safe -}}
    {# Handle multimodal content (list of parts) #}
    {%- else -%}
        {%- for part in interaction['content'] -%}
            {%- if part['type'] == 'text' -%}
                {{- part['text'] | safe -}}
            {%- elif part['type'] == 'image' -%}
                {{- capabilities.vision.tokens.start }}{{ capabilities.vision.tokens.end -}}
            {%- endif -%}
        {%- endfor -%}
    {%- endif -%}

    {{- end_of_message -}}
{%- endmacro -%}

{# ------------------------------------------------------------------------ #}
{# Macro: render_tool                                                       #}
{# Renders a single tool definition as plain text.                         #}
{# ------------------------------------------------------------------------ #}
{%- macro render_tool(tool) -%}
    {{ "\n" + tool.name + ":" }}
    {%- if tool.description is defined %}
        {{ tool.description }}
    {%- endif %}
    Schema: {{ tool | tojson }}
{%- 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 -%}
    {%- if roles.system is not none -%}
        {{- roles.system.role_start_tag + roles.system.role_name + roles.system.role_end_tag -}}
    {%- endif -%}
    {%- if has_system -%}
        {%- if interactions[0]['content'] is string -%}
            {{- interactions[0]['content'] | safe -}}
    {%- endif -%}
    {%- if has_tools -%}
        {{- "\nAvailable tools:" -}}
        {%- for t in tools -%}
            {{- render_tool(t) -}}
        {%- endfor -%}
        {{- "\n\nWhen calling a tool, output valid JSON wrapped in \"" + tc_tokens.start | trim + "\" and \"" + tc_tokens.end | trim + "\" delimiters." -}}
    {%- endif -%}
    {{- end_of_message -}}
{%- endif -%}
{# Render conversation #}
{%- for interaction in interactions -%}
    {%- if has_system and loop.first -%}
        {# Already rendered system message above #}
    {%- else -%}
        {{ render_interaction(interaction) }}
    {%- endif -%}
{%- endfor -%}
{# Add generation prompt for assistant turn #}
{%- if add_generation_prompt -%}
    {%- if reasoning and capabilities.thinking is defined -%}
        {{- capabilities.thinking.tokens.start -}}
    {%- elif roles.agent is not none -%}
        {{- roles.agent.role_start_tag + roles.agent.role_name + roles.agent.role_end_tag -}}
    {%- endif -%}
{%- endif -%}
{# Optional prefill #}
{%- if prefill is not none -%}
    {{- prefill -}}
{%- endif -%}
