1{# ----------‑‑‑ special token variables ‑‑‑---------- #}
  2{%- set toolcall_begin_token   = '<minimax:tool_call>'         -%}
  3{%- set toolcall_end_token     = '</minimax:tool_call>'        -%}
  4{#- Tool Rendering Functions ============================================== -#}
  5{%- macro render_tool_namespace(namespace_name, tool_list) -%}
  6{%- for tool in tool_list -%}
  7<tool>{{ tool.function | tojson(ensure_ascii=False) }}</tool>
  8{% endfor -%}
  9{%- endmacro -%}
 10{%- macro visible_text(content) -%}
 11    {%- if content is string -%}
 12        {{ content }}
 13    {%- elif content is iterable and content is not mapping -%}
 14        {%- for item in content -%}
 15            {%- if item is mapping and item.type == 'text' -%}
 16                {{- item.text }}
 17            {%- elif item is string -%}
 18                {{- item }}
 19            {%- endif -%}
 20        {%- endfor -%}
 21    {%- else -%}
 22        {{- content }}
 23    {%- endif -%}
 24{%- endmacro -%}
 25{#- System Message Construction ============================================ -#}
 26{%- macro build_system_message(system_message) -%}
 27    {%- if system_message and system_message.content -%}
 28        {{- visible_text(system_message.content) }}
 29    {%- else -%}
 30        {%- if model_identity is not defined -%}
 31            {%- set model_identity = "You are a helpful assistant." -%}
 32        {%- endif -%}
 33        {{- model_identity }}
 34    {%- endif -%}
 35    
 36    {#- Handle current_date -#}
 37    {%- if system_message and system_message.current_date -%}
 38        {{- '\n' ~ 'Current date: ' + system_message.current_date }}
 39    {%- endif -%}
 40    {#- Handle current_location -#}
 41    {%- if system_message and system_message.current_location -%}
 42        {{- '\n' ~ 'Current location: ' + system_message.current_location }}
 43    {%- endif -%}
 44{%- endmacro -%}
 45{#- Main Template Logic ================================================= -#}
 46{#- Extract system message (only first message if it's system) -#}
 47{%- set system_message = none -%}
 48{%- set conversation_messages = messages -%}
 49{%- if messages and messages[0].role == "system" -%}
 50    {%- set system_message = messages[0] -%}
 51    {%- set conversation_messages = messages[1:] -%}
 52{%- endif -%}
 53{#- Get the last user message turn, for interleved thinking -#}
 54{%- set ns = namespace(last_user_index=-1) %}
 55{% for m in conversation_messages %}
 56    {%- if m.role == 'user' %}
 57        {% set ns.last_user_index = loop.index0 -%}
 58    {%- endif %}
 59{%- endfor %}
 60{#- Render system message -#}
 61{{- ']~!b[' ~ ']~b]system' ~ '\n' }}
 62{{- build_system_message(system_message) }}
 63{#- Render tools if available -#}
 64{%- if tools -%}
 65    {{- '\n\n' ~ '# Tools' ~ '\n' ~ 'You may call one or more tools to assist with the user query.\nHere are the tools available in JSONSchema format:' ~ '\n' }}
 66    {{- '\n' ~ '<tools>' ~ '\n' }}
 67    {{- render_tool_namespace("functions", tools) }}
 68    {{- '</tools>' ~ '\n\n' }}
 69{{- 'When making tool calls, use XML format to invoke tools and pass parameters:' ~ '\n' }}
 70{{- '\n' ~ toolcall_begin_token }}
 71<invoke name="tool-name-1">
 72<parameter name="param-key-1">param-value-1</parameter>
 73<parameter name="param-key-2">param-value-2</parameter>
 74...
 75</invoke>
 76{{- '\n' ~ toolcall_end_token }}
 77{%- endif -%}
 78{{- '[e~[\n' }}
 79
 80{#- Render messages -#}
 81{%- set last_tool_call = namespace(name=none) -%}
 82{%- for message in conversation_messages -%}
 83    {%- if message.role == 'assistant' -%}
 84        {#- Only render reasoning_content if no user message follows -#}
 85        {{- ']~b]ai' ~ '\n' }}
 86
 87        {%- set reasoning_content = '' %}
 88        {%- set content = visible_text(message.content) %}
 89        {%- if message.reasoning_content is string %}
 90            {%- set reasoning_content = message.reasoning_content %}
 91        {%- else %}
 92            {%- if '</think>' in content %}
 93                {%- set reasoning_content = content.split('</think>')[0].strip('\n').split('<think>')[-1].strip('\n') %}
 94                {%- set content = content.split('</think>')[-1].strip('\n') %}
 95            {%- endif %}
 96        {%- endif %}
 97        {%- if reasoning_content and loop.index0 > ns.last_user_index -%}
 98            {{- '<think>' ~ '\n' ~ reasoning_content ~ '\n' ~ '</think>' ~ '\n\n' }}
 99        {%- endif -%}
100        {%- if content -%}
101            {{- content }}
102        {%- endif -%}
103        {%- if message.tool_calls -%}
104            {{- '\n' ~ toolcall_begin_token ~ '\n' }}
105
106            {%- for tool_call in message.tool_calls -%}
107                {%- if tool_call.function %}
108                    {%- set tool_call = tool_call.function %}
109                {%- endif %}
110                {{- '<invoke name="' + tool_call.name + '">' }}
111                {% set _args = tool_call.arguments %}
112                {%- for k, v in _args.items() %}
113                {{- '<parameter name="' + k + '">' }}
114                {{- v | tojson(ensure_ascii=False) if v is not string else v }}
115                {{- '</parameter>' }}
116                {% endfor %}
117                {{- '</invoke>' ~ '\n' }}
118            {%- endfor -%}
119            
120            {{- toolcall_end_token}}
121            {%- set last_tool_call.name = message.tool_calls[-1].function.name -%}
122        {%- else -%}
123            {%- set last_tool_call.name = none -%}
124        {%- endif -%}
125        {{- '[e~[' ~ '\n' }}
126        
127    {%- elif message.role == 'tool' -%}
128    {%- if last_tool_call.name is none -%}
129        {{- raise_exception("Message has tool role, but there was no previous assistant message with a tool call!") }}
130    {%- endif -%}
131    {%- if loop.first or (conversation_messages[loop.index0 - 1].role != 'tool') -%}
132        {{- ']~b]tool' }}
133    {%- endif -%}
134    {%- if message.content is string -%}
135        {{- '\n<response>' }}
136        {{- message.content }}
137        {{- '</response>' }}
138    {%- else -%}
139        {%- for tr in message.content -%}
140            {{- '\n<response>' }}
141            {{- tr.output if tr.output is defined else (tr.text if tr.type == 'text' and tr.text is defined else tr) }}
142            {{- '\n</response>' }}
143        {%- endfor -%}
144    {%- endif -%}
145    {%- if loop.last or (conversation_messages[loop.index0 + 1].role != 'tool') -%}
146        {{- '[e~[\n' -}}
147    {%- endif -%}
148        
149    {%- elif message.role == 'user' -%}
150        {{- ']~b]user' ~ '\n' }}
151        {{- visible_text(message.content) }}
152        {{- '[e~[' ~ '\n' }}
153    {%- endif -%}
154{%- endfor -%}
155
156{#- Generation prompt -#}
157{%- if add_generation_prompt -%}
158{{- ']~b]ai' ~ '\n' ~ '<think>' ~ '\n' }}
159{%- endif -%}