{# ------------------------------------------------------------------------ #}
{# Template configuration                                                   #}
{# Designed to accept model-specific control tokens                         #}
{# ------------------------------------------------------------------------ #}
{%- 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_tools_block                                                #}
{# Renders the full tools block if tools are available.                     #}
{# ------------------------------------------------------------------------ #}
{%- macro render_tools_block() -%}
    {%- if tools is defined and tools and tc_tokens -%}
        {{- "\nAvailable tools:" -}}
        {%- for t in tools -%}
            {{- "\n" if loop.first else "\n\n" -}}
            {{- tool_macros.render_tool(t) -}}
        {%- endfor -%}
        {{- "\n\nWhen you need a tool, respond with exactly one Python-style function call using keyword arguments inside \"" + tc_tokens.start | trim + "\" and \"" + tc_tokens.end | trim + "\"." -}}
        {{- "\nUse double quotes for strings, True or False for booleans, and None for null values." -}}
    {%- endif -%}
{%- endmacro -%}
{# ------------------------------------------------------------------------ #}
{# Macro: render_content                                                    #}
{# Renders the content parts of an interaction (text only, no images)       #}
{# ------------------------------------------------------------------------ #}
{%- macro render_content(interaction) -%}
    {%- if interaction['content'] is string -%}
        {{- interaction['content'] | safe -}}
    {%- else -%}
        {%- for content in interaction['content'] -%}
            {%- if content['type'] != 'image' and content['type'] != 'capability' -%}
                {{- content | safe -}}
            {%- endif -%}
        {%- endfor -%}
    {%- endif -%}
{%- endmacro -%}
{# ------------------------------------------------------------------------ #}
{# Macro: render_tool_call                                                  #}
{# ------------------------------------------------------------------------ #}
{%- 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) -}}
    {{- end_of_message -}}
{%- endmacro -%}
{# ------------------------------------------------------------------------ #}
{# Macro: render_query_interaction                                          #}
{# ------------------------------------------------------------------------ #}
{%- macro render_query_interaction(interaction) -%}
    {%- if 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_content(interaction) -}}
    {%- endif -%}
{%- endmacro -%}
{# ------------------------------------------------------------------------ #}
{# Macro: render_images                                                     #}
{# Renders image placeholders for an interaction                            #}
{# ------------------------------------------------------------------------ #}
{%- macro render_images(interaction) -%}
    {%- if interaction['content'] is sequence and interaction['content'] is not string -%}
        {%- for content in interaction['content'] -%}
            {%- if content['type'] == 'image' -%}
                {{- capabilities.vision.placeholders.image -}}
            {%- endif -%}
        {%- endfor -%}
    {%- endif -%}
{%- endmacro -%}
{# ------------------------------------------------------------------------ #}
{# Task-specific templates for Moondream3                                   #}
{# ------------------------------------------------------------------------ #}
{{ begin_of_text }}
{#- Render images first (they come before the task prompt) -#}
{%- for interaction in interactions -%}
    {{- render_images(interaction) -}}
{%- endfor -%}
{%- if task is defined and task is not none -%}
    {#- Caption tasks: describe<sep>{length}<answer> -#}
    {%- if task == 'caption_short' -%}
        {{- roles.user.role_start_tag ~ 'describe' ~ roles.user.role_end_tag ~ 'short' ~ roles.agent.role_start_tag -}}
    {%- elif task == 'caption_normal' -%}
        {{- roles.user.role_start_tag ~ 'describe' ~ roles.user.role_end_tag ~ 'normal' ~ roles.agent.role_start_tag -}}
    {%- elif task == 'caption_long' -%}
        {{- roles.user.role_start_tag ~ 'describe' ~ roles.user.role_end_tag ~ 'long' ~ roles.agent.role_start_tag -}}
    {#- Detect task: detect<sep>{object}<answer> -#}
    {%- elif task == 'detect' -%}
        {{- roles.user.role_start_tag ~ 'detect' ~ roles.user.role_end_tag -}}
        {%- for interaction in interactions -%}
            {{- render_content(interaction) -}}
        {%- endfor -%}
        {{- roles.agent.role_start_tag -}}
    {#- Point task: point<sep>{object}<answer> -#}
    {%- elif task == 'point' -%}
        {{- roles.user.role_start_tag ~ 'point' ~ roles.user.role_end_tag -}}
        {%- for interaction in interactions -%}
            {{- render_content(interaction) -}}
        {%- endfor -%}
        {{- roles.agent.role_start_tag -}}
    {#- Detect gaze task: coordinate pair embedded inline via single coord placeholder -#}
    {#- C++ MoondreamCoordCapability takes 8-byte input (x,y) and produces 2 embedding tokens -#}
    {%- elif task == 'detect_gaze' -%}
        {{- "\n\nPoint:" ~ capabilities.gaze_detection.placeholders.coord ~ " gaze\n\n" -}}
    {#- Unknown task, fall through to default query behavior -#}
    {%- else -%}
        {{- roles.user.role_start_tag ~ roles.user.role_name ~ roles.user.role_end_tag -}}
        {{- render_tools_block() -}}
        {%- for interaction in interactions -%}
            {{- render_query_interaction(interaction) -}}
        {%- endfor -%}
        {%- if add_generation_prompt -%}
            {%- if reasoning is not none and reasoning -%}
                {{- capabilities.thinking.tokens.start -}}
            {%- else -%}
                {{- roles.agent.role_start_tag -}}
            {%- endif -%}
        {%- endif -%}
    {%- endif -%}
{%- else -%}
    {#- Default query behavior -#}
    {{- roles.user.role_start_tag ~ roles.user.role_name ~ roles.user.role_end_tag -}}
    {{- render_tools_block() -}}
    {%- for interaction in interactions -%}
        {{- render_query_interaction(interaction) -}}
    {%- endfor -%}
    {%- if add_generation_prompt -%}
        {%- if reasoning is not none and reasoning -%}
            {{- capabilities.thinking.tokens.start -}}
        {%- else -%}
            {{- roles.agent.role_start_tag -}}
        {%- endif -%}
    {%- endif -%}
{%- endif -%}
{%- if prefill is not none -%}
    {{- prefill -}}
{%- endif -%}
