{# ------------------------------------------------------------------------ #}
{# 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 -%}
{# ------------------------------------------------------------------------ #}
{# 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 -%}
{# ------------------------------------------------------------------------ #}
{# 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 -%}
            {{- 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 -%}
{%- 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_images                                                     #}
{# Renders image placeholders for an interaction                            #}
{# ------------------------------------------------------------------------ #}
{%- macro render_images(interaction) -%}
    {%- if 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_content(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_content(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 -%}
