File size: 23,077 Bytes
aded0a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
{%- macro format_argument(argument, escape_keys=True) -%}
    {%- if argument is none -%}
        {{- 'null' -}}
    {%- elif argument is string -%}
        {{- '<|"|>' + argument + '<|"|>' -}}
    {%- elif argument is boolean -%}
        {{- 'true' if argument else 'false' -}}
    {%- elif argument is mapping -%}
        {{- '{' -}}
        {%- set ns = namespace(found_first=false) -%}
        {%- for key, value in argument | dictsort -%}
            {%- if ns.found_first %},{% endif -%}
            {%- set ns.found_first = true -%}
            {%- if escape_keys -%}
                {{- '<|"|>' + key + '<|"|>' -}}
            {%- else -%}
                {{- key -}}
            {%- endif -%}
            :{{- format_argument(value, escape_keys=escape_keys) -}}
        {%- endfor -%}
        {{- '}' -}}
    {%- elif argument is iterable and argument is not string -%}
        {{- '[' -}}
        {%- for item in argument -%}
            {{- format_argument(item, escape_keys=escape_keys) -}}
            {%- if not loop.last %},{% endif -%}
        {%- endfor -%}
        {{- ']' -}}
    {%- else -%}
        {{- argument -}}
    {%- endif -%}
{%- endmacro -%}

{%- macro format_type_argument(type_value) -%}
    {%- if type_value is none -%}
        <|"|>OBJECT<|"|>
    {%- elif type_value is string -%}
        <|"|>{{ type_value | upper }}<|"|>
    {%- elif type_value is iterable and type_value is not string -%}
        [
        {%- for item in type_value -%}
            {%- if item is string -%}
                <|"|>{{ item | upper }}<|"|>
            {%- else -%}
                {{- format_argument(item) -}}
            {%- endif -%}
            {%- if not loop.last %},{% endif -%}
        {%- endfor -%}
        ]
    {%- else -%}
        {{- format_argument(type_value) -}}
    {%- endif -%}
{%- endmacro -%}

{%- macro format_parameters(properties, required, filter_keys=false) -%}
    {%- set standard_keys = ['description', 'type', 'properties', 'required', 'nullable'] -%}
    {%- set ns = namespace(found_first=false) -%}
    {%- for key, value in properties | dictsort -%}
        {%- if not filter_keys or key not in standard_keys -%}
            {%- if ns.found_first %},{% endif -%}
            {%- set ns.found_first = true -%}
            {{ key }}:{
            {%- set add_comma = false -%}
            {%- if value is mapping -%}
                {%- set vtype = value['type'] if 'type' in value and value['type'] else none -%}
                {%- set is_string_type = (vtype is string and vtype | upper == 'STRING') or (vtype is iterable and vtype is not string and ('string' in vtype or 'STRING' in vtype)) -%}
                {%- set is_array_type = (vtype is string and vtype | upper == 'ARRAY') or (vtype is iterable and vtype is not string and ('array' in vtype or 'ARRAY' in vtype)) -%}
                {%- set is_object_type = (vtype is string and vtype | upper == 'OBJECT') or (vtype is iterable and vtype is not string and ('object' in vtype or 'OBJECT' in vtype)) -%}
                {%- if 'description' in value and value['description'] -%}
                    description:<|"|>{{ value['description'] }}<|"|>
                    {%- set add_comma = true -%}
                {%- endif -%}
                {%- if is_string_type -%}
                    {%- if 'enum' in value and value['enum'] -%}
                        {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
                        enum:{{ format_argument(value['enum']) }}
                    {%- endif -%}
                {%- elif is_array_type -%}
                    {%- if 'items' in value and value['items'] is mapping and value['items'] -%}
                        {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
                        items:{
                        {%- set ns_items = namespace(found_first=false) -%}
                        {%- for item_key, item_value in value['items'] | dictsort -%}
                            {%- if item_value is not none -%}
                                {%- if ns_items.found_first %},{% endif -%}
                                {%- set ns_items.found_first = true -%}
                                {%- if item_key == 'properties' -%}
                                    properties:{
                                    {%- if item_value is mapping -%}
                                        {{- format_parameters(item_value, value['items']['required'] if 'required' in value['items'] else []) -}}
                                    {%- endif -%}
                                    }
                                {%- elif item_key == 'required' -%}
                                    required:[
                                    {%- for req_item in item_value -%}
                                        <|"|>{{- req_item -}}<|"|>
                                        {%- if not loop.last %},{% endif -%}
                                    {%- endfor -%}
                                    ]
                                {%- elif item_key == 'type' -%}
                                    type:{{ format_type_argument(item_value) }}
                                {%- else -%}
                                    {{ item_key }}:{{ format_argument(item_value) }}
                                {%- endif -%}
                            {%- endif -%}
                        {%- endfor -%}
                        }
                    {%- endif -%}
                {%- endif -%}
                {%- if 'nullable' in value and value['nullable'] -%}
                    {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
                    nullable:true
                {%- endif -%}
                {%- if is_object_type or ('properties' in value and value['properties'] is mapping) -%}
                    {%- if 'properties' in value and value['properties'] is mapping -%}
                        {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
                        properties:{
                        {{- format_parameters(value['properties'], value['required'] if 'required' in value else []) -}}
                        }
                    {%- elif value is mapping -%}
                        {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
                        properties:{
                        {{- format_parameters(value, value['required'] if 'required' in value else [], filter_keys=true) -}}
                        }
                    {%- endif -%}
                    {%- if 'required' in value and value['required'] -%}
                        {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
                        required:[
                        {%- for item in value['required'] -%}
                            <|"|>{{- item -}}<|"|>
                            {%- if not loop.last %},{% endif -%}
                        {%- endfor -%}
                        ]
                    {%- endif -%}
                {%- endif -%}
                {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
                type:{{ format_type_argument(vtype if vtype else 'object') }}
            {%- else -%}
                type:{{ format_type_argument('object') }}
            {%- endif -%}
            }
        {%- endif -%}
    {%- endfor -%}
{%- endmacro -%}

{%- macro format_function_declaration(tool_data) -%}
    {%- set fn = tool_data['function'] if tool_data is mapping and 'function' in tool_data else tool_data -%}
    {%- set tool_name = fn['name'] if fn is mapping and 'name' in fn and fn['name'] else 'unknown' -%}
    {%- set tool_description = fn['description'] if fn is mapping and 'description' in fn and fn['description'] else '' -%}
    declaration:{{- tool_name -}}{description:<|"|>{{- tool_description -}}<|"|>
    {%- set params = none -%}
    {%- if fn is mapping and 'parameters' in fn and fn['parameters'] -%}
        {%- set params = fn['parameters'] -%}
    {%- elif fn is mapping and 'input_schema' in fn and fn['input_schema'] -%}
        {%- set params = fn['input_schema'] -%}
    {%- elif fn is mapping and 'inputSchema' in fn and fn['inputSchema'] -%}
        {%- set params = fn['inputSchema'] -%}
    {%- endif -%}
    {%- if params and params is mapping -%}
        ,parameters:{
        {%- if 'properties' in params and params['properties'] -%}
            properties:{ {{- format_parameters(params['properties'], params['required'] if 'required' in params else []) -}} },
        {%- endif -%}
        {%- if 'required' in params and params['required'] -%}
            required:[
            {%- for item in params['required'] -%}
                <|"|>{{- item -}}<|"|>
                {{- ',' if not loop.last -}}
            {%- endfor -%}
            ],
        {%- endif -%}
        {%- if 'type' in params and params['type'] -%}
            type:{{- format_type_argument(params['type']) -}}}
        {%- else -%}
            type:{{- format_type_argument('object') -}}}
        {%- endif -%}
    {%- endif -%}
    {%- if fn is mapping and 'response' in fn and fn['response'] is mapping -%}
        {%- set response_declaration = fn['response'] -%}
        ,response:{
        {%- if 'description' in response_declaration and response_declaration['description'] -%}
            description:<|"|>{{- response_declaration['description'] -}}<|"|>,
        {%- endif -%}
        {%- if 'type' in response_declaration and response_declaration['type'] -%}
            type:{{- format_type_argument(response_declaration['type']) -}}}
        {%- else -%}
            type:{{- format_type_argument('object') -}}}
        {%- endif -%}
    {%- endif -%}
    }
{%- endmacro -%}

{%- macro strip_thinking(text) -%}
    {{- text | trim -}}
{%- endmacro -%}

{%- macro format_tool_response_block(tool_name, response) -%}
    {{- '<|tool_response>' -}}
    {%- if response is mapping -%}
        {{- 'response:' + tool_name + '{' -}}
        {%- for key, value in response | dictsort -%}
            {{- key -}}:{{- format_argument(value, escape_keys=False) -}}
            {%- if not loop.last %},{% endif -%}
        {%- endfor -%}
        {{- '}' -}}
    {%- else -%}
        {{- 'response:' + tool_name + '{value:' + format_argument(response, escape_keys=False) + '}' -}}
    {%- endif -%}
    {{- '<tool_response|>' -}}
{%- endmacro -%}

{#- ===== SETUP ===== -#}
{%- set ns = namespace(prev_message_type=None, prev_non_tool_role=None) -%}
{%- set loop_messages = messages -%}
{%- set enable_thinking = enable_thinking if enable_thinking is defined else false -%}
{%- set preserve_thinking = preserve_thinking if preserve_thinking is defined else true -%}
{{- bos_token -}}
{#- Handle System/Tool Definitions Block -#}
{%- if enable_thinking or (tools is defined and tools) or (messages and messages[0]['role'] in ['system', 'developer']) -%}
    {{- '<|turn>system\n' -}}
    {#- Inject Thinking token at the very top of the FIRST system turn -#}
    {%- if enable_thinking -%}
        {{- '<|think|>\n' -}}
        {%- set ns.prev_message_type = 'think' -%}
    {%- endif -%}
    {%- if messages and messages[0]['role'] in ['system', 'developer'] -%}
        {%- if messages[0]['content'] is string -%}
            {{- messages[0]['content'] | trim -}}
        {%- elif messages[0]['content'] is iterable and messages[0]['content'] is not string -%}
            {%- for item in messages[0]['content'] -%}
                {%- if item is mapping and 'text' in item -%}{{- item['text'] | trim + ' '-}}{%- endif -%}
            {%- endfor -%}
        {%- endif -%}
        {%- set loop_messages = messages[1:] -%}
    {%- endif -%}
    {%- if tools is defined and tools -%}
        {%- for tool in tools %}
            {{- '<|tool>' -}}
            {{- format_function_declaration(tool) | trim -}}
            {{- '<tool|>' -}}
        {%- endfor %}
        {%- set ns.prev_message_type = 'tool' -%}
    {%- endif -%}
    {{- '<turn|>\n' -}}
{%- endif %}

{#- Pre-scan: find last user message index for reasoning guard -#}
{%- set ns_turn = namespace(last_user_idx=-1) -%}
{%- for i in range(loop_messages | length) -%}
    {%- if loop_messages[i]['role'] == 'user' -%}
        {%- set ns_turn.last_user_idx = i -%}
    {%- endif -%}
{%- endfor -%}

{#- Loop through messages -#}
{%- for message in loop_messages -%}
    {%- if message['role'] != 'tool' -%}
    {%- set ns.prev_message_type = None -%}
    {%- set role = 'model' if message['role'] == 'assistant' else message['role'] -%}
    {%- set message_has_tool_calls = ('tool_calls' in message and message['tool_calls']) -%}
    {#- Detect continuation using tracked state — O(1) instead of O(n) backward scan -#}
    {%- set continue_same_model_turn = (role == 'model' and ns.prev_non_tool_role == 'assistant') -%}
    {%- if not continue_same_model_turn -%}
        {{- '<|turn>' + role + '\n' }}
    {%- endif -%}

    {#- Render reasoning/reasoning_content as thinking channel (tool-call turns only) -#}
    {%- set thinking_ns = namespace(text='') -%}
    {%- if 'reasoning' in message and message['reasoning'] -%}
        {%- set thinking_ns.text = message['reasoning'] -%}
    {%- elif 'reasoning_content' in message and message['reasoning_content'] -%}
        {%- set thinking_ns.text = message['reasoning_content'] -%}
    {%- endif -%}
    {%- set thinking_gate = (loop.index0 > ns_turn.last_user_idx) or preserve_thinking -%}
    {%- if thinking_ns.text and thinking_gate and message_has_tool_calls -%}
        {{- '<|channel>thought\n' + thinking_ns.text + '\n<channel|>' -}}
    {%- endif -%}

            {%- if message_has_tool_calls -%}
                {%- for tool_call in message['tool_calls'] -%}
                    {%- set function = tool_call['function'] if tool_call is mapping and 'function' in tool_call else tool_call -%}
                    {%- set function_name = function['name'] if function is mapping and 'name' in function and function['name'] else 'unknown' -%}
                    {{- '<|tool_call>call:' + function_name + '{' -}}
                    {%- if function is mapping and 'arguments' in function and function['arguments'] is mapping -%}
                        {%- set ns_args = namespace(found_first=false) -%}
                        {%- for key, value in function['arguments'] | dictsort -%}
                            {%- if ns_args.found_first %},{% endif -%}
                            {%- set ns_args.found_first = true -%}
                            {{- key -}}:{{- format_argument(value, escape_keys=False) -}}
                        {%- endfor -%}
                    {%- elif function is mapping and 'arguments' in function and function['arguments'] is string -%}
                        {{- function['arguments'] -}}
                    {%- elif function is mapping and 'arguments' in function and function['arguments'] is not none -%}
                        {{- format_argument(function['arguments'], escape_keys=False) -}}
                    {%- endif -%}
                    {{- '}<tool_call|>' -}}
                {%- endfor -%}
                {%- set ns.prev_message_type = 'tool_call' -%}
            {%- endif -%}

            {%- set ns_tr_out = namespace(flag=false) -%}
            {%- if 'tool_responses' in message and message['tool_responses'] -%}
                {#- Legacy: tool_responses embedded on the assistant message (Google/Gemma native) -#}
                {%- for tool_response in message['tool_responses'] -%}
                    {%- set tr_name = tool_response['name'] if tool_response is mapping and 'name' in tool_response and tool_response['name'] else 'unknown' -%}
                    {%- set tr_response = tool_response['response'] if tool_response is mapping and 'response' in tool_response else '' -%}
                    {{- format_tool_response_block(tr_name, tr_response) -}}
                    {%- set ns_tr_out.flag = true -%}
                    {%- set ns.prev_message_type = 'tool_response' -%}
                {%- endfor -%}
            {%- elif message_has_tool_calls -%}
                {#- OpenAI Chat Completions: forward-scan consecutive role:tool messages -#}
                {%- set ns_tool_scan = namespace(stopped=false) -%}
                {%- for k in range(loop.index0 + 1, loop_messages | length) -%}
                    {%- if ns_tool_scan.stopped -%}
                    {%- elif loop_messages[k]['role'] != 'tool' -%}
                        {%- set ns_tool_scan.stopped = true -%}
                    {%- else -%}
                        {%- set follow = loop_messages[k] -%}
                        {#- Resolve tool_call_id to function name -#}
                        {%- set ns_tname = namespace(name='unknown') -%}
                        {%- if 'name' in follow and follow['name'] -%}
                            {%- set ns_tname.name = follow['name'] -%}
                        {%- endif -%}
                        {%- for tc in message['tool_calls'] -%}
                            {%- if tc is mapping and 'id' in tc and 'tool_call_id' in follow and tc['id'] == follow['tool_call_id'] -%}
                                {%- if 'function' in tc and 'name' in tc['function'] and tc['function']['name'] -%}
                                    {%- set ns_tname.name = tc['function']['name'] -%}
                                {%- elif 'name' in tc and tc['name'] -%}
                                    {%- set ns_tname.name = tc['name'] -%}
                                {%- endif -%}
                            {%- endif -%}
                        {%- endfor -%}
                        {#- Handle content as string, object, or content-parts array -#}
                        {%- set tool_body = follow['content'] if 'content' in follow else '' -%}
                        {%- if tool_body is string -%}
                            {{- format_tool_response_block(ns_tname.name, tool_body) -}}
                        {%- elif tool_body is mapping -%}
                            {{- format_tool_response_block(ns_tname.name, tool_body) -}}
                        {%- elif tool_body is iterable and tool_body is not string -%}
                            {%- set ns_txt = namespace(s='') -%}
                            {%- for part in tool_body -%}
                                {%- if part is mapping and 'type' in part and part['type'] == 'text' and 'text' in part -%}
                                    {%- set ns_txt.s = ns_txt.s + part['text'] -%}
                                {%- endif -%}
                            {%- endfor -%}
                            {{- format_tool_response_block(ns_tname.name, ns_txt.s) -}}
                            {%- for part in tool_body -%}
                                {%- if part is mapping and 'type' in part and part['type'] in ['image', 'image_url'] -%}
                                    {{- '<|image|>' -}}
                                {%- elif part is mapping and 'type' in part and part['type'] in ['audio', 'input_audio'] -%}
                                    {{- '<|audio|>' -}}
                                {%- elif part is mapping and 'type' in part and part['type'] == 'video' -%}
                                    {{- '<|video|>' -}}
                                {%- endif -%}
                            {%- endfor -%}
                        {%- else -%}
                            {{- format_tool_response_block(ns_tname.name, tool_body) -}}
                        {%- endif -%}
                        {%- set ns_tr_out.flag = true -%}
                        {%- set ns.prev_message_type = 'tool_response' -%}
                    {%- endif -%}
                {%- endfor -%}
            {%- endif -%}

            {%- set captured_content -%}
            {%- if 'content' in message and message['content'] is string -%}
                {%- if role == 'model' -%}
                    {{- strip_thinking(message['content']) -}}
                {%- else -%}
                    {{- message['content'] | trim -}}
                {%- endif -%}
            {%- elif 'content' in message and message['content'] is iterable and message['content'] is not string -%}
                {%- for item in message['content'] -%}
                    {%- if item is mapping and 'type' in item and item['type'] == 'text' and 'text' in item -%}
                        {%- if role == 'model' -%}
                            {{- strip_thinking(item['text']) -}}
                        {%- else -%}
                            {{- item['text'] | trim -}}
                        {%- endif -%}
                    {%- elif item is mapping and 'type' in item and item['type'] in ['image', 'image_url'] -%}
                        {{- '<|image|>' -}}
                    {%- elif item is mapping and 'type' in item and item['type'] in ['audio', 'input_audio'] -%}
                        {{- '<|audio|>' -}}
                    {%- elif item is mapping and 'type' in item and item['type'] == 'video' -%}
                        {{- '<|video|>' -}}
                    {%- endif -%}
                {%- endfor -%}
            {%- endif -%}
            {%- endset -%}

            {{- captured_content -}}
            {%- set has_content = captured_content | trim | length > 0 -%}

        {#- Forward-scan: find next non-tool message role for continuation detection -#}
        {%- set next_nt = namespace(role=None, found=false) -%}
        {%- for j in range(loop.index0 + 1, loop_messages | length) -%}
            {%- if not next_nt.found -%}
                {%- if loop_messages[j]['role'] != 'tool' -%}
                    {%- set next_nt.role = loop_messages[j]['role'] -%}
                    {%- set next_nt.found = true -%}
                {%- endif -%}
            {%- endif -%}
        {%- endfor -%}

        {%- set continues_into_next = (
            role == 'model'
            and next_nt.role == 'assistant'
            and not message_has_tool_calls
            and not ns_tr_out.flag
        ) -%}

        {%- if ns.prev_message_type == 'tool_call' and not ns_tr_out.flag -%}
            {{- '<|tool_response>' -}}
        {%- elif continues_into_next -%}
            {{- '\n' -}}
        {%- elif not (ns_tr_out.flag and not has_content) -%}
            {{- '<turn|>\n' -}}
        {%- endif -%}

    {#- Track previous non-tool role for next iteration (avoids O(n) backward scan) -#}
    {%- set ns.prev_non_tool_role = message['role'] -%}
    {%- endif -%}
{%- endfor -%}

{%- if add_generation_prompt is defined and add_generation_prompt -%}
    {%- if ns.prev_message_type != 'tool_response' and ns.prev_message_type != 'tool_call' -%}
        {{- '<|turn>model\n' -}}
    {%- endif -%}

    {%- if not enable_thinking -%}
        {#- Suppress thinking — but not when awaiting tool responses -#}
        {%- if ns.prev_message_type != 'tool_call' -%}
            {{- '<|channel>thought\n<channel|>' -}}
        {%- endif -%}
    {%- endif -%}
{%- endif -%}