pragmaticcs commited on
Commit
50db790
·
verified ·
1 Parent(s): 6a3cbca

Upload chat_template.jinja with huggingface_hub

Browse files
Files changed (1) hide show
  1. chat_template.jinja +534 -0
chat_template.jinja ADDED
@@ -0,0 +1,534 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {%- set template_version = "qwen3.5-3.6-kat-qwopus-agentworld-ornith-merged-v3-agentic" %}
2
+ {#- =========================================================================================
3
+ QWEN HYBRID AGENTIC CHAT TEMPLATE (KAT-Coder + Qwopus + AgentWorld + Ornith)
4
+ Supported tool formats:
5
+ - 'qwen3_coder' (default): Official XML syntax (<function=...><parameter=...>)
6
+ - 'json': Official Qwen / Qwopus JSON syntax (<tool_call>{"name":...,"arguments":...}</tool_call>)
7
+ Agentic Features:
8
+ - Runtime protocol collision suppression (_suppress_tools / [TOOL_REQUEST])
9
+ - 3-tier consecutive failure warning escalation (anti-loop circuit breaker)
10
+ - MCP / ToolCall argument normalization (.arguments, .parameters, .input)
11
+ - Non-destructive JSON response truncation protection
12
+ - Dynamic thinking pruning on multi-turn trajectories
13
+ ========================================================================================= -#}
14
+
15
+ {%- set _tool_format = tool_call_format if tool_call_format is defined else 'qwen3_coder' %}
16
+ {%- set enable_thinking = (enable_thinking not in (false, 'false', 'False', 0, '0', 'off', 'none')) if enable_thinking is defined else true %}
17
+ {%- set auto_disable_thinking_with_tools = (auto_disable_thinking_with_tools in (true, 'true', 'True', 1, '1', 'on')) if auto_disable_thinking_with_tools is defined else false %}
18
+
19
+ {%- if preserve_reasoning is defined and preserve_reasoning is not none %}
20
+ {%- set _preserve_thinking = (preserve_reasoning not in (false, 'false', 'False', 0, '0', 'off', 'none')) %}
21
+ {%- elif preserve_thinking is defined and preserve_thinking is not none %}
22
+ {%- set _preserve_thinking = (preserve_thinking not in (false, 'false', 'False', 0, '0', 'off', 'none')) %}
23
+ {%- else %}
24
+ {%- set _preserve_thinking = true %}
25
+ {%- endif %}
26
+
27
+ {%- set max_tool_arg_chars = (max_tool_arg_chars | int) if (max_tool_arg_chars is defined and max_tool_arg_chars is not none) else 0 %}
28
+ {%- set max_tool_response_chars = (max_tool_response_chars | int) if (max_tool_response_chars is defined and max_tool_response_chars is not none) else 0 %}
29
+ {%- set _has_tools = (tools is defined and tools and tools is iterable and tools is not mapping) %}
30
+
31
+ {# --- Dynamic Reasoning Effort Mapping --- #}
32
+ {%- set _effort_raw = (reasoning_effort | string | lower) if reasoning_effort is defined and reasoning_effort is not none else 'medium' %}
33
+ {%- set _initial_thinking = enable_thinking %}
34
+ {%- set _initial_effort = 'medium' %}
35
+ {%- if _effort_raw in ('none', 'off') %}
36
+ {%- set _initial_thinking = false %}
37
+ {%- set _initial_effort = 'medium' %}
38
+ {%- elif _effort_raw in ('minimal', 'low') %}
39
+ {%- set _initial_effort = 'low' %}
40
+ {%- elif _effort_raw in ('high', 'xhigh', 'max', 'ultracode', 'extreme', 'deep', 'long') %}
41
+ {%- set _initial_thinking = true %}
42
+ {%- set _initial_effort = 'xhigh' %}
43
+ {%- else %}
44
+ {%- set _initial_effort = 'medium' %}
45
+ {%- endif %}
46
+
47
+ {%- set ns_state = namespace(thinking=_initial_thinking, effort=_initial_effort) %}
48
+ {%- if auto_disable_thinking_with_tools and _has_tools %}
49
+ {%- set ns_state.thinking = false %}
50
+ {%- endif %}
51
+
52
+ {# --- In-Prompt Thinking Directive Scanning --- #}
53
+ {%- for msg in messages %}
54
+ {%- if msg.role == 'system' or msg.role == 'developer' or msg.role == 'user' %}
55
+ {%- if msg.content is string %}
56
+ {%- if '<|think_off|>' in msg.content %}
57
+ {%- set ns_state.thinking = false %}
58
+ {%- elif '<|think_on|>' in msg.content %}
59
+ {%- set ns_state.thinking = true %}
60
+ {%- elif '<|think_xhigh|>' in msg.content or '<|think_high|>' in msg.content or '<|think_ultracode|>' in msg.content or '<|think_extreme|>' in msg.content or '<|think_max|>' in msg.content or '<|think_deep|>' in msg.content or '<|think_long|>' in msg.content %}
61
+ {%- set ns_state.thinking = true %}
62
+ {%- set ns_state.effort = 'xhigh' %}
63
+ {%- elif '<|think_low|>' in msg.content or '<|think_minimal|>' in msg.content %}
64
+ {%- set ns_state.thinking = true %}
65
+ {%- set ns_state.effort = 'low' %}
66
+ {%- elif '<|think_medium|>' in msg.content %}
67
+ {%- set ns_state.thinking = true %}
68
+ {%- set ns_state.effort = 'medium' %}
69
+ {%- endif %}
70
+ {%- elif msg.content is iterable and msg.content is not mapping %}
71
+ {%- for item in msg.content %}
72
+ {%- if item is string %}
73
+ {%- set _item_text = item %}
74
+ {%- elif item is mapping and 'text' in item and item.text is string %}
75
+ {%- set _item_text = item.text %}
76
+ {%- elif item is mapping and 'content' in item and item.content is string %}
77
+ {%- set _item_text = item.content %}
78
+ {%- else %}
79
+ {%- set _item_text = '' %}
80
+ {%- endif %}
81
+ {%- if _item_text %}
82
+ {%- if '<|think_off|>' in _item_text %}
83
+ {%- set ns_state.thinking = false %}
84
+ {%- elif '<|think_on|>' in _item_text %}
85
+ {%- set ns_state.thinking = true %}
86
+ {%- elif '<|think_xhigh|>' in _item_text or '<|think_high|>' in _item_text or '<|think_ultracode|>' in _item_text or '<|think_extreme|>' in _item_text or '<|think_max|>' in _item_text or '<|think_deep|>' in _item_text or '<|think_long|>' in _item_text %}
87
+ {%- set ns_state.thinking = true %}
88
+ {%- set ns_state.effort = 'xhigh' %}
89
+ {%- elif '<|think_low|>' in _item_text or '<|think_minimal|>' in _item_text %}
90
+ {%- set ns_state.thinking = true %}
91
+ {%- set ns_state.effort = 'low' %}
92
+ {%- elif '<|think_medium|>' in _item_text %}
93
+ {%- set ns_state.thinking = true %}
94
+ {%- set ns_state.effort = 'medium' %}
95
+ {%- endif %}
96
+ {%- endif %}
97
+ {%- endfor %}
98
+ {%- endif %}
99
+ {%- endif %}
100
+ {%- endfor %}
101
+
102
+ {%- set reasoning_instructions = '' %}
103
+ {%- if ns_state.thinking %}
104
+ {%- if ns_state.effort == 'xhigh' %}
105
+ {%- set reasoning_instructions = 'Reasoning effort is set to xhigh. Please think rigorously through the task, explore alternative hypotheses, validate edge cases, and prioritize architectural soundness, consistency, and correctness in the final answer.' %}
106
+ {%- elif ns_state.effort == 'low' %}
107
+ {%- set reasoning_instructions = 'Reasoning effort is set to low. Keep your thinking concise, focused, and move directly to execution or tool calls without unnecessary elaboration.' %}
108
+ {%- endif %}
109
+ {%- endif %}
110
+
111
+ {# --- Content Render Macro (Enforces Pure-Text CausalLM) --- #}
112
+ {%- macro render_content(content, is_system_content=false) %}
113
+ {%- if content is string %}
114
+ {{- content }}
115
+ {%- elif content is iterable and content is not mapping %}
116
+ {%- for item in content %}
117
+ {%- if item is mapping %}
118
+ {%- if item.type == 'image' or 'image' in item or 'image_url' in item %}
119
+ {{- raise_exception('This model is a pure text CausalLM (vision tower removed). Image inputs are not supported.') }}
120
+ {%- elif item.type == 'video' or 'video' in item %}
121
+ {{- raise_exception('This model is a pure text CausalLM (vision tower removed). Video inputs are not supported.') }}
122
+ {%- elif 'text' in item %}
123
+ {{- (item.text | string) if item.text is not none else '' }}
124
+ {%- elif 'content' in item and item.content is string %}
125
+ {{- item.content }}
126
+ {%- else %}
127
+ {{- item | tojson }}
128
+ {%- endif %}
129
+ {%- else %}
130
+ {{- item | string }}
131
+ {%- endif %}
132
+ {%- endfor %}
133
+ {%- elif content is none or content is undefined %}
134
+ {{- '' }}
135
+ {%- else %}
136
+ {{- raise_exception('Unexpected content type.') }}
137
+ {%- endif %}
138
+ {%- endmacro %}
139
+
140
+ {%- if not messages %}
141
+ {{- raise_exception('No messages provided.') }}
142
+ {%- endif %}
143
+
144
+ {# --- Head Extraction (System/Developer Messages) --- #}
145
+ {%- set head = namespace(count=0, seen_non_system=false) %}
146
+ {%- for message in messages %}
147
+ {%- set _is_sys = (message.role == 'system' or message.role == 'developer') %}
148
+ {%- if _is_sys and not head.seen_non_system %}
149
+ {%- set head.count = head.count + 1 %}
150
+ {%- else %}
151
+ {%- set head.seen_non_system = true %}
152
+ {%- endif %}
153
+ {%- endfor %}
154
+
155
+ {%- set sys_state = namespace(content='') %}
156
+ {%- for message in messages[:head.count] %}
157
+ {%- set _part = render_content(message.content, true) | trim %}
158
+ {%- if '<|think_off|>' in _part %}{%- set _part = _part.split('<|think_off|>') | join('') | trim %}{%- endif %}
159
+ {%- if '<|think_on|>' in _part %}{%- set _part = _part.split('<|think_on|>') | join('') | trim %}{%- endif %}
160
+ {%- if '<|think_xhigh|>' in _part %}{%- set _part = _part.split('<|think_xhigh|>') | join('') | trim %}{%- endif %}
161
+ {%- if '<|think_high|>' in _part %}{%- set _part = _part.split('<|think_high|>') | join('') | trim %}{%- endif %}
162
+ {%- if '<|think_ultracode|>' in _part %}{%- set _part = _part.split('<|think_ultracode|>') | join('') | trim %}{%- endif %}
163
+ {%- if '<|think_extreme|>' in _part %}{%- set _part = _part.split('<|think_extreme|>') | join('') | trim %}{%- endif %}
164
+ {%- if '<|think_max|>' in _part %}{%- set _part = _part.split('<|think_max|>') | join('') | trim %}{%- endif %}
165
+ {%- if '<|think_deep|>' in _part %}{%- set _part = _part.split('<|think_deep|>') | join('') | trim %}{%- endif %}
166
+ {%- if '<|think_long|>' in _part %}{%- set _part = _part.split('<|think_long|>') | join('') | trim %}{%- endif %}
167
+ {%- if '<|think_medium|>' in _part %}{%- set _part = _part.split('<|think_medium|>') | join('') | trim %}{%- endif %}
168
+ {%- if '<|think_low|>' in _part %}{%- set _part = _part.split('<|think_low|>') | join('') | trim %}{%- endif %}
169
+ {%- if '<|think_minimal|>' in _part %}{%- set _part = _part.split('<|think_minimal|>') | join('') | trim %}{%- endif %}
170
+ {%- if _part %}
171
+ {%- if sys_state.content %}
172
+ {%- set sys_state.content = sys_state.content ~ '\n\n' ~ _part %}
173
+ {%- else %}
174
+ {%- set sys_state.content = _part %}
175
+ {%- endif %}
176
+ {%- endif %}
177
+ {%- endfor %}
178
+
179
+ {%- set _sc = sys_state.content %}
180
+ {%- set _msgs = messages[head.count:] %}
181
+
182
+ {# --- Optional Terse Directive --- #}
183
+ {%- set _terse_on = (terse in (true, 'true', 'True', 1, '1', 'on')) if terse is defined else false %}
184
+ {%- if _terse_on %}
185
+ {%- if ns_state.thinking %}
186
+ {%- set _terse_lead = 'Answer directly, after thinking. Lead with the answer, then only what it needs to be correct and usable.' %}
187
+ {%- else %}
188
+ {%- set _terse_lead = 'Answer directly and concisely. Give the answer with only what it needs to be correct and usable.' %}
189
+ {%- endif %}
190
+ {%- set _terse_core %}
191
+ Never: open with preamble or pleasantries; restate the question; add filler transitions; hedge with niceties; or repeat a point you have already made.
192
+ Always: keep essential steps, caveats, uncertainties, and specifics — never drop correctness or a needed warning for brevity. Keep the final answer lean. Use the least structure that conveys it (plain prose when short; lists or code only when they earn their place). If genuinely uncertain, say so and explain why — never omit uncertainty for the sake of brevity.
193
+ If a user request is genuinely ambiguous, ask a sharp question, do not guess.
194
+ {%- endset %}
195
+ {%- set _terse = _terse_lead ~ '\n' ~ (_terse_core | trim) %}
196
+ {%- if not _sc %}
197
+ {%- set _sc = _terse | trim %}
198
+ {%- else %}
199
+ {%- set _sc = (_sc | trim) ~ '\n\n' ~ (_terse | trim) %}
200
+ {%- endif %}
201
+ {%- endif %}
202
+
203
+ {# --- Frontend Runtime Protocol Collision Detection (Borrowed from Donor 1) --- #}
204
+ {%- set _runtime_tool_protocol = (_sc is defined and _sc and ('[TOOL_REQUEST]' in _sc or '[AVAILABLE_TOOLS]' in _sc or '<tool_protocol>' in _sc)) %}
205
+ {%- set _suppress_tools = (suppress_tool_instructions in (true, 'true', 'True', 1, '1', 'on')) if suppress_tool_instructions is defined else _runtime_tool_protocol %}
206
+
207
+ {# --- Tool System Prompt Injection --- #}
208
+ {%- if _has_tools and not _suppress_tools %}
209
+ {{- '<|im_start|>system\n' }}
210
+ {%- if reasoning_instructions %}
211
+ {{- reasoning_instructions + '\n\n' }}
212
+ {%- endif %}
213
+ {{- '# Tools\n\nYou have access to the following functions:\n\n<tools>' }}
214
+ {%- for tool in tools %}
215
+ {{- '\n' }}
216
+ {{- tool | tojson }}
217
+ {%- endfor %}
218
+ {{- '\n</tools>' }}
219
+ {%- if _tool_format == 'json' %}
220
+ {#- Canonical Qwen / Qwopus JSON invocation format (Borrowed from Donor 2) -#}
221
+ {{- '\n\nFor each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:\n' }}
222
+ {%- if ns_state.thinking %}
223
+ {{- '<think>\nBrief explanation of next step or parameter rationale\n</think>\n' }}
224
+ {%- endif %}
225
+ {{- '<tool_call>\n{"name": "example_function_name", "arguments": {"example_parameter_1": "value_1", "example_parameter_2": "value_2"}}\n</tool_call>\n\n<IMPORTANT>\n' }}
226
+ {%- if ns_state.thinking %}
227
+ {{- '- All planning, hypothesis testing, and error reflection MUST be placed strictly inside the <think></think> block.\n' }}
228
+ {{- '- Output the <tool_call> block IMMEDIATELY after thinking with NO conversational text before it.\n' }}
229
+ {%- else %}
230
+ {{- '- Output the <tool_call> block IMMEDIATELY with NO conversational text before it.\n' }}
231
+ {%- endif %}
232
+ {{- '- The <tool_call> tag MUST be at the very beginning of a new line, with NO indentation.\n' }}
233
+ {{- '- For multiple calls in parallel, output a separate <tool_call></tool_call> block for EACH function. Do NOT nest calls.\n' }}
234
+ {{- '- Keep tool calls purposeful: issue only the calls required for the immediate next step.\n' }}
235
+ {{- '- Once all necessary data is gathered, provide your final answer directly without calling further tools.\n</IMPORTANT>' }}
236
+ {%- else %}
237
+ {#- Official Qwen3-Coder XML syntax (<function=...><parameter=...>) -#}
238
+ {{- '\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n' }}
239
+ {%- if ns_state.thinking %}
240
+ {{- '<think>\nBrief explanation of next step or parameter rationale\n</think>\n' }}
241
+ {%- endif %}
242
+ {{- '<tool_call>\n<function=example_function_name>\n<parameter=example_parameter_1>\nvalue_1\n</parameter>\n<parameter=example_parameter_2>\nvalue_2\n</parameter>\n</function>\n</tool_call>\n\n<IMPORTANT>\n' }}
243
+ {%- if ns_state.thinking %}
244
+ {{- '- All planning, hypothesis testing, and error reflection MUST be placed strictly inside the <think></think> block.\n' }}
245
+ {{- '- Output the <tool_call> block IMMEDIATELY after thinking with NO conversational text before it.\n' }}
246
+ {%- else %}
247
+ {{- '- Output the <tool_call> block IMMEDIATELY with NO conversational text before it.\n' }}
248
+ {%- endif %}
249
+ {{- '- Function calls MUST follow the specified format: an inner <function=...></function> block nested within <tool_call></tool_call> tags.\n' }}
250
+ {{- '- The <tool_call> and <function> tags MUST be at the very beginning of a new line, with NO spaces or indentation before them.\n' }}
251
+ {{- '- For multiple calls, output a separate, completely closed <tool_call></tool_call> block for EACH function. Do NOT nest blocks.\n' }}
252
+ {{- '- Keep tool calls purposeful: issue only the calls required for the immediate next step.\n' }}
253
+ {{- '- Once all necessary data is gathered, provide your final answer directly without calling further tools.\n</IMPORTANT>' }}
254
+ {%- endif %}
255
+ {%- if _sc %}
256
+ {{- '\n\n' + _sc }}
257
+ {%- endif %}
258
+ {{- '<|im_end|>\n' }}
259
+ {%- else %}
260
+ {%- if _sc %}
261
+ {{- '<|im_start|>system\n' + (reasoning_instructions + '\n\n' if reasoning_instructions else '') + _sc + '<|im_end|>\n' }}
262
+ {%- elif reasoning_instructions %}
263
+ {{- '<|im_start|>system\n' + reasoning_instructions + '<|im_end|>\n' }}
264
+ {%- endif %}
265
+ {%- endif %}
266
+
267
+ {# --- Multi-Turn Trajectory Analysis (Scanning for Root User Query) --- #}
268
+ {%- set ns = namespace(found_query=false, last_query_index=0) %}
269
+ {%- for message in _msgs[::-1] %}
270
+ {%- set index = (_msgs | length - 1) - loop.index0 %}
271
+ {%- if not ns.found_query and message.role == 'user' %}
272
+ {%- set _rc = render_content(message.content) | trim %}
273
+ {%- if not (_rc.startswith('<tool_response>') and _rc.endswith('</tool_response>')) %}
274
+ {%- set ns.found_query = true %}
275
+ {%- set ns.last_query_index = index %}
276
+ {%- endif %}
277
+ {%- endif %}
278
+ {%- endfor %}
279
+
280
+ {# Fallback when entire context consists of autonomous tool loops (Borrowed from Donor 1) #}
281
+ {%- if not ns.found_query and _msgs | length > 0 %}
282
+ {%- set ns.last_query_index = (_msgs | length - 1) %}
283
+ {%- endif %}
284
+
285
+ {# --- Conversation Render Loop --- #}
286
+ {%- set ns2 = namespace(prev_role='', consecutive_failures=0) %}
287
+ {%- for message in _msgs %}
288
+ {%- set is_system = (message.role == "system" or message.role == "developer") %}
289
+ {%- set content = render_content(message.content, is_system) | trim %}
290
+ {%- if is_system or message.role == 'user' %}
291
+ {%- if '<|think_off|>' in content %}{%- set content = content.split('<|think_off|>') | join('') | trim %}{%- endif %}
292
+ {%- if '<|think_on|>' in content %}{%- set content = content.split('<|think_on|>') | join('') | trim %}{%- endif %}
293
+ {%- if '<|think_xhigh|>' in content %}{%- set content = content.split('<|think_xhigh|>') | join('') | trim %}{%- endif %}
294
+ {%- if '<|think_high|>' in content %}{%- set content = content.split('<|think_high|>') | join('') | trim %}{%- endif %}
295
+ {%- if '<|think_ultracode|>' in content %}{%- set content = content.split('<|think_ultracode|>') | join('') | trim %}{%- endif %}
296
+ {%- if '<|think_extreme|>' in content %}{%- set content = content.split('<|think_extreme|>') | join('') | trim %}{%- endif %}
297
+ {%- if '<|think_max|>' in content %}{%- set content = content.split('<|think_max|>') | join('') | trim %}{%- endif %}
298
+ {%- if '<|think_deep|>' in content %}{%- set content = content.split('<|think_deep|>') | join('') | trim %}{%- endif %}
299
+ {%- if '<|think_long|>' in content %}{%- set content = content.split('<|think_long|>') | join('') | trim %}{%- endif %}
300
+ {%- if '<|think_medium|>' in content %}{%- set content = content.split('<|think_medium|>') | join('') | trim %}{%- endif %}
301
+ {%- if '<|think_low|>' in content %}{%- set content = content.split('<|think_low|>') | join('') | trim %}{%- endif %}
302
+ {%- if '<|think_minimal|>' in content %}{%- set content = content.split('<|think_minimal|>') | join('') | trim %}{%- endif %}
303
+ {%- endif %}
304
+ {%- if is_system %}
305
+ {{- '<|im_start|>system\n' + content + '<|im_end|>\n' }}
306
+ {%- elif message.role == 'user' %}
307
+ {%- set ns2.consecutive_failures = 0 %}
308
+ {{- '<|im_start|>user\n' + content + '<|im_end|>\n' }}
309
+ {%- elif message.role == 'assistant' %}
310
+ {%- set reasoning_content = '' %}
311
+ {%- set _explicit_reasoning = '' %}
312
+ {%- if message.reasoning_content is defined and message.reasoning_content is not none %}
313
+ {%- if message.reasoning_content is string %}
314
+ {%- set _explicit_reasoning = message.reasoning_content %}
315
+ {%- elif message.reasoning_content is mapping and 'text' in message.reasoning_content %}
316
+ {%- set _explicit_reasoning = message.reasoning_content.text | string %}
317
+ {%- else %}
318
+ {%- set _explicit_reasoning = message.reasoning_content | string %}
319
+ {%- endif %}
320
+ {%- elif message.thinking is defined and message.thinking is not none %}
321
+ {%- if message.thinking is string %}
322
+ {%- set _explicit_reasoning = message.thinking %}
323
+ {%- elif message.thinking is mapping and 'text' in message.thinking %}
324
+ {%- set _explicit_reasoning = message.thinking.text | string %}
325
+ {%- else %}
326
+ {%- set _explicit_reasoning = message.thinking | string %}
327
+ {%- endif %}
328
+ {%- elif message.reasoning is defined and message.reasoning is not none %}
329
+ {%- if message.reasoning is string %}
330
+ {%- set _explicit_reasoning = message.reasoning %}
331
+ {%- elif message.reasoning is mapping and 'text' in message.reasoning %}
332
+ {%- set _explicit_reasoning = message.reasoning.text | string %}
333
+ {%- else %}
334
+ {%- set _explicit_reasoning = message.reasoning | string %}
335
+ {%- endif %}
336
+ {%- endif %}
337
+ {%- if _explicit_reasoning %}
338
+ {%- set _lead_end = '' %}
339
+ {%- if '</think>' in content %}
340
+ {%- set _lead_end = '</think>' %}
341
+ {%- elif '</thinking>' in content %}
342
+ {%- set _lead_end = '</thinking>' %}
343
+ {%- endif %}
344
+ {%- if _lead_end %}
345
+ {%- set _parts = content.split(_lead_end) %}
346
+ {%- set content = (_parts[1:] | join(_lead_end)).lstrip('\n') %}
347
+ {%- endif %}
348
+ {%- set reasoning_content = _explicit_reasoning %}
349
+ {%- else %}
350
+ {%- set _think_end = '' %}
351
+ {%- if '</think>' in content %}
352
+ {%- set _think_end = '</think>' %}
353
+ {%- elif '</thinking>' in content %}
354
+ {%- set _think_end = '</thinking>' %}
355
+ {%- endif %}
356
+ {%- if _think_end %}
357
+ {%- set _think_start = '<thinking>' if 'thinking' in _think_end else '<think>' %}
358
+ {%- set _parts = content.split(_think_end) %}
359
+ {%- set reasoning_content = _parts[0].rstrip('\n') %}
360
+ {%- if _think_start in reasoning_content %}
361
+ {%- set reasoning_content = reasoning_content.split(_think_start)[-1].lstrip('\n') %}
362
+ {%- endif %}
363
+ {%- set content = (_parts[1:] | join(_think_end)).lstrip('\n') %}
364
+ {%- endif %}
365
+ {%- endif %}
366
+ {%- set reasoning_content = reasoning_content | trim %}
367
+
368
+ {# Emit Assistant Thought and Content #}
369
+ {%- if reasoning_content and (_preserve_thinking or loop.index0 >= ns.last_query_index) %}
370
+ {{- '<|im_start|>assistant\n<think>\n' + reasoning_content + '\n</think>\n\n' + content }}
371
+ {%- else %}
372
+ {{- '<|im_start|>assistant\n' + content }}
373
+ {%- endif %}
374
+
375
+ {# Emit Tool Calls (Guarded against double emission if <tool_call> is already in content) #}
376
+ {%- if message.tool_calls is defined and message.tool_calls and message.tool_calls is iterable and message.tool_calls is not mapping and '<tool_call>' not in content %}
377
+ {%- for tool_call in message.tool_calls %}
378
+ {%- if tool_call.function is defined and tool_call.function is not none %}
379
+ {%- set tc = tool_call.function %}
380
+ {%- else %}
381
+ {%- set tc = tool_call %}
382
+ {%- endif %}
383
+ {%- set tc_name = tc.name if (tc.name is defined and tc.name is not none) else '' %}
384
+
385
+ {# Resolve Arguments across OpenAI, Anthropic/Claude, and MCP schemas (.arguments, .parameters, .input) #}
386
+ {%- set tc_args_raw = none %}
387
+ {%- if tc.arguments is defined and tc.arguments is not none %}
388
+ {%- set tc_args_raw = tc.arguments %}
389
+ {%- elif tc.parameters is defined and tc.parameters is not none %}
390
+ {%- set tc_args_raw = tc.parameters %}
391
+ {%- elif tc.input is defined and tc.input is not none %}
392
+ {%- set tc_args_raw = tc.input %}
393
+ {%- endif %}
394
+
395
+ {%- if _tool_format == 'json' %}
396
+ {%- if loop.first %}
397
+ {%- if content | trim %}
398
+ {{- '\n\n' }}
399
+ {%- endif %}
400
+ {%- else %}
401
+ {{- '\n' }}
402
+ {%- endif %}
403
+ {%- set _args = '{}' %}
404
+ {%- if tc_args_raw is not none %}
405
+ {%- if tc_args_raw is mapping %}
406
+ {%- set _args = tc_args_raw | tojson %}
407
+ {%- elif tc_args_raw is string %}
408
+ {%- set _trimmed_args = tc_args_raw | trim %}
409
+ {%- if _trimmed_args | length > 0 %}
410
+ {%- if _trimmed_args | first == '{' and _trimmed_args | last == '}' %}
411
+ {%- set _args = _trimmed_args %}
412
+ {%- else %}
413
+ {%- set _args = tc_args_raw | tojson %}
414
+ {%- endif %}
415
+ {%- endif %}
416
+ {%- else %}
417
+ {%- set _args = tc_args_raw | tojson %}
418
+ {%- endif %}
419
+ {%- endif %}
420
+ {{- '<tool_call>\n{"name": ' }}{{- tc_name | tojson }}{{- ', "arguments": ' }}{{- _args }}{{- '}\n</tool_call>' }}
421
+ {%- else %}
422
+ {%- if loop.first %}
423
+ {%- if content | trim %}
424
+ {{- '\n\n<tool_call>\n<function=' + tc_name + '>\n' }}
425
+ {%- else %}
426
+ {{- '<tool_call>\n<function=' + tc_name + '>\n' }}
427
+ {%- endif %}
428
+ {%- else %}
429
+ {{- '\n<tool_call>\n<function=' + tc_name + '>\n' }}
430
+ {%- endif %}
431
+ {%- if tc_args_raw is not none %}
432
+ {%- if tc_args_raw is mapping %}
433
+ {%- for args_name, args_value in tc_args_raw.items() %}
434
+ {{- '<parameter=' + args_name + '>\n' }}
435
+ {%- if args_value is string %}
436
+ {%- set _av = args_value %}
437
+ {%- else %}
438
+ {%- set _av = args_value | tojson %}
439
+ {%- endif %}
440
+ {%- if max_tool_arg_chars > 0 and _av | length > max_tool_arg_chars %}
441
+ {{- _av[:max_tool_arg_chars] + '\n[TRUNCATED - original length ' ~ (_av | length | string) ~ ' chars]' }}
442
+ {%- else %}
443
+ {{- _av }}
444
+ {%- endif %}
445
+ {{- '\n</parameter>\n' }}
446
+ {%- endfor %}
447
+ {%- else %}
448
+ {%- if tc_args_raw is string %}
449
+ {%- set _raw_args = tc_args_raw %}
450
+ {%- else %}
451
+ {%- set _raw_args = tc_args_raw | tojson %}
452
+ {%- endif %}
453
+ {%- if _raw_args %}
454
+ {%- if max_tool_arg_chars > 0 and _raw_args | length > max_tool_arg_chars %}
455
+ {{- _raw_args[:max_tool_arg_chars] + '\n[TRUNCATED - original length ' ~ (_raw_args | length | string) ~ ' chars]' }}
456
+ {%- else %}
457
+ {{- _raw_args }}
458
+ {%- endif %}
459
+ {%- endif %}
460
+ {%- endif %}
461
+ {%- endif %}
462
+ {{- '</function>\n</tool_call>' }}
463
+ {%- endif %}
464
+ {%- endfor %}
465
+ {%- endif %}
466
+ {{- '<|im_end|>\n' }}
467
+ {%- elif message.role == 'tool' %}
468
+ {# --- Multi-Turn Tool Response & Escalating Circuit Breaker --- #}
469
+ {%- set _content_lower = content | lower %}
470
+ {%- set _content_head = _content_lower[:150] %}
471
+ {%- set _is_code_or_grep = ('throw new ' in _content_lower or 'throw error' in _content_lower or 'console.error' in _content_lower or 'logger.error' in _content_lower or 'logging.error' in _content_lower or 'import ' in _content_head or 'def ' in _content_head or 'function ' in _content_head) %}
472
+ {%- set _exit_code_zero = ('exit code: 0' in _content_head or 'process exited with code 0' in _content_head) %}
473
+ {%- set _error_field_ok = ('"error": null' in _content_head or '"error":null' in _content_head or '"error": false' in _content_head or '"error":false' in _content_head or '"error": ""' in _content_head or '"error":""' in _content_head) %}
474
+ {%- set _strong_error = (('"error":' in _content_head and not _error_field_ok) or '"status": "error"' in _content_head or '"status":"error"' in _content_head or '"success": false' in _content_head or '"success":false' in _content_head or 'traceback (most recent call last):' in _content_head or 'command not found' in _content_head or 'invalid syntax' in _content_head or 'fatal:' in _content_head or 'permission denied' in _content_head or 'no such file or directory' in _content_head or '404 not found' in _content_head or 'rate limit' in _content_head or 'connection refused' in _content_head or 'timed out' in _content_head or (('exit code: ' in _content_head or 'process exited with code' in _content_head) and not _exit_code_zero) or _content_head.startswith('exception:') or _content_head.startswith('failed to ')) %}
475
+ {%- set _weak_error = ('error:' in _content_head or 'err!' in _content_head) %}
476
+ {%- set _weak_suppressed = ('$ ' in _content_head or 'took ' in _content_head or content | length >= 600) %}
477
+
478
+ {%- if not _is_code_or_grep and (_strong_error or (_weak_error and not _weak_suppressed)) %}
479
+ {%- set ns2.consecutive_failures = ns2.consecutive_failures + 1 %}
480
+ {%- else %}
481
+ {%- set ns2.consecutive_failures = 0 %}
482
+ {%- endif %}
483
+
484
+ {%- if ns2.prev_role != 'tool' %}
485
+ {{- '<|im_start|>user' }}
486
+ {%- endif %}
487
+
488
+ {# Truncate non-JSON outputs safely, alert if JSON was clipped (Borrowed from Donor 1) #}
489
+ {%- set _is_json_payload = (content | trim | length > 0 and (content | trim)[:1] in ('{', '[')) %}
490
+ {%- if max_tool_response_chars > 0 and content | length > max_tool_response_chars %}
491
+ {%- if _is_json_payload %}
492
+ {%- set content = content[:max_tool_response_chars] + '\n[TRUNCATED: Output exceeded ' ~ max_tool_response_chars ~ ' chars. Remaining JSON payload clipped]' %}
493
+ {%- else %}
494
+ {%- set content = content[:max_tool_response_chars] + '\n[TRUNCATED - original length ' ~ (content | length | string) ~ ' chars]' %}
495
+ {%- endif %}
496
+ {%- endif %}
497
+
498
+ {{- '\n<tool_response>\n' + content }}
499
+
500
+ {# 3-Tier Failure Escalation (Anti-Loop Circuit Breaker) #}
501
+ {%- if ns2.consecutive_failures >= 3 %}
502
+ {{- '\n\n⚠️ SYSTEM WARNING: ' ~ ns2.consecutive_failures ~ ' consecutive tool errors detected. CRITICAL AGENTIC LOOP. Do NOT repeat the failed command or variations of it. If a dependency, tool, or path is missing, immediately PIVOT to an alternative diagnostic tool or report the blocker directly.' }}
503
+ {%- elif ns2.consecutive_failures == 2 %}
504
+ {{- '\n\n⚠️ SYSTEM WARNING: 2 consecutive tool errors detected. Your previous technical approach is fundamentally incorrect. You MUST diagnose root cause and use a completely different approach or command.' }}
505
+ {%- elif ns2.consecutive_failures == 1 %}
506
+ {{- '\n\n⚠️ SYSTEM WARNING: The previous tool call returned an error. Diagnose the failure and retry with corrected arguments.' }}
507
+ {%- endif %}
508
+
509
+ {{- '\n</tool_response>' }}
510
+
511
+ {%- if loop.last %}
512
+ {{- '<|im_end|>\n' }}
513
+ {%- else %}
514
+ {%- set _next_role = _msgs[loop.index0 + 1].role %}
515
+ {%- if _next_role != 'tool' %}
516
+ {{- '<|im_end|>\n' }}
517
+ {%- endif %}
518
+ {%- endif %}
519
+ {%- else %}
520
+ {{- '<|im_start|>user\n[' + message.role + ']: ' + content + '<|im_end|>\n' }}
521
+ {%- endif %}
522
+ {%- set ns2.prev_role = message.role %}
523
+ {%- endfor %}
524
+
525
+ {# --- Generation Prompt --- #}
526
+ {%- set _add_gen = (add_generation_prompt in (true, 'true', 'True', 1, '1', 'on')) if add_generation_prompt is defined else false %}
527
+ {%- if _add_gen %}
528
+ {{- '<|im_start|>assistant\n' }}
529
+ {%- if not ns_state.thinking %}
530
+ {{- '<think>\n\n</think>\n\n' }}
531
+ {%- else %}
532
+ {{- '<think>\n' }}
533
+ {%- endif %}
534
+ {%- endif %}