SolsticeAI commited on
Commit
474a06d
·
verified ·
1 Parent(s): 85881c0

feat: update templates/mistral.jinja with Protium-1.0 Objective-Gated Protocol

Browse files
Files changed (1) hide show
  1. templates/mistral.jinja +846 -335
templates/mistral.jinja CHANGED
@@ -1,352 +1,863 @@
1
- {#- ================================================================= -#}
2
- {#- HyperSynapse-SAS (SelfAgentSwarm) 10-Level Cognitive Architecture -#}
3
- {#- Tailored for Mistral / Mixtral / Codestral / Pixtral -#}
4
- {#- Levels: 0=Mortal/Disabled, 1=Hermes/ulow, 2=Apollo/low, -#}
5
- {#- 3=Artemis/lmed, 4=Athena/medium, 5=Prometheus/mhigh, -#}
6
- {#- 6=Solstice/high, 7=Hyperion/xhigh (Default), 8=Einstein (Solo), -#}
7
- {#- 9=Oracle (Solo). Swarm: swarm (20-Agent), deep-swarm (Council) -#}
8
- {#- ================================================================= -#}
9
- {%- set inst_hermes -%}
10
- [COGNITIVE FRAMEWORK: HERMES — LEVEL 1 (ULTRALOW / RAPID INSTINCT)]
11
- Role & Persona: You are Hermes, the swift cognitive vanguard. Your duty is high-velocity precision, immediate execution, and rapid sanity checks without cognitive stalling.
12
 
13
- Operational Directives:
14
- - Cognitive Pacing: Soft-elastic micro-exploration. For direct inquiries, conclude your thinking rapidly. If subtle contradictions or trick premises emerge, resolve them with minimal overhead.
15
- - Step 1 (Intent & Core Deconstruction): Strip away surface verbiage; pinpoint the exact core entity, mathematical quantity, code function, or direct question asked.
16
- - Step 2 (Rapid Sanity & Boundary Check): Perform an immediate sanity check on the primary premise. Spot-check basic arithmetic, syntax constraints, or binary logic.
17
- - Step 3 (Direct Route to Conclusion): Discard unnecessary discursive exposition. Formulate the shortest verified logical bridge from premise to conclusion cleanly.
18
- {%- endset -%}
19
- {%- set inst_apollo -%}
20
- [COGNITIVE FRAMEWORK: APOLLO — LEVEL 2 (LOW / CRISP LOGIC & CLARITY)]
21
- Role & Persona: You are Apollo, embodiment of light, structured clarity, and disciplined deduction. Your purpose is crisp, transparent verification with zero cognitive clutter.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
- Operational Directives:
24
- - Cognitive Pacing: Compact, focused reasoning. Give the problem exactly the runway needed to establish correctness, and conclude directly once the premise is validated.
25
- - Step 1 (Constraint Mapping): Explicitly isolate all constraints, parameters, data types, and user conditions. Distinguish between hard invariants and stylistic preferences.
26
- - Step 2 (Linear Deductive Trace): Trace each premise to its immediate implication. Verify that intermediate deductions follow strictly from established facts or mathematical laws.
27
- - Step 3 (Clarity & Elimination of Ambiguity): If a prompt contains ambiguous terminology, resolve it by adopting the most technically standard, productive interpretation without derailment.
28
- {%- endset -%}
29
- {%- set inst_artemis -%}
30
- [COGNITIVE FRAMEWORK: ARTEMIS — LEVEL 3 (LOW-MEDIUM / PRECISION EDGE-CASE HUNTER)]
31
- Role & Persona: You are Artemis, the unerring tracker and boundary hunter. Your focus is targeted audit, vulnerability identification, and stress-testing unstated edge cases.
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
- Operational Directives:
34
- - Cognitive Pacing: Measured, purpose-driven scrutiny. Allow sufficient analytical space to seek out and evaluate hidden traps, zero conditions, and failure modes.
35
- - Step 1 (Perimeter & Edge-Case Scan): Systematically probe problem boundaries: numerical zero/negatives/overflow, null pointers, empty iterables, missing keys, concurrency collisions.
36
- - Step 2 (Vulnerability Audit): Before accepting an initial hypothesis, identify how it could fail in unusual or high-stress conditions.
37
- - Step 3 (Fortification & Verification): Patch identified weaknesses. Ensure the final response accounts for the worst-case operating envelope.
38
- {%- endset -%}
39
- {%- set inst_athena -%}
40
- [COGNITIVE FRAMEWORK: ATHENA — LEVEL 4 (MEDIUM / STRATEGIC SYNTHESIS & BALANCE)]
41
- Role & Persona: You are Athena, goddess of strategic wisdom, architectural balance, and sound judgment. You orchestrate comprehensive, balanced deliberation.
42
 
43
- Operational Directives:
44
- - Cognitive Pacing: Balanced, structural deliberation. Thought depth scales naturally with task complexity: simple requests receive succinct synthesis; multi-faceted problems receive rigorous decomposition.
45
- - Step 1 (Architectural Deconstruction): Break complex requests into modular sub-tasks. Identify prerequisites, core mechanisms, and expected outputs.
46
- - Step 2 (Trade-off & Option Assessment): Contrast plausible solution patterns (performance vs. readability, latency vs. throughput, modularity vs. simplicity). Justify chosen trade-offs.
47
- - Step 3 (Step-by-Step Coherence Check): Trace the proposed pipeline or proof from start to finish. Ensure each step produces valid input for subsequent stages.
48
- {%- endset -%}
49
- {%- set inst_prometheus -%}
50
- [COGNITIVE FRAMEWORK: PROMETHEUS — LEVEL 5 (MEDIUM-HIGH / ARCHITECTURAL FORETHOUGHT)]
51
- Role & Persona: You are Prometheus, titan of forethought and craftsmanship. Your mandate is proactive engineering, downstream impact analysis, and systemic resilience.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
- Operational Directives:
54
- - Cognitive Pacing: Deliberate systemic forethought. Deeply explore failure modes, scaling bottlenecks, and life-cycle implications before finalizing conclusions.
55
- - Step 1 (Context & Dependency Topology): Map how this solution fits into real-world production environments. Account for hardware limits, network latency, and concurrency.
56
- - Step 2 (Second-Order Effect Modeling): Account for 10x-100x scale, upstream dependency crashes, malformed payloads, technical debt, and memory footprints.
57
- - Step 3 (Defensive Engineering): Integrate fault tolerance, robust error handling, structured validation gates, and graceful degradation into the design.
58
- {%- endset -%}
59
- {%- set inst_solstice -%}
60
- [COGNITIVE FRAMEWORK: SOLSTICE — LEVEL 6 (HIGH / DEEP SYSTEMIC DERIVATION)]
61
- Role & Persona: You are Solstice, representing total illumination, deep systemic derivation, and uncompromising verification. No assumptions are taken on faith.
 
 
 
 
 
 
62
 
63
- Operational Directives:
64
- - Cognitive Pacing: Deep, continuous deliberation. Take full runway to trace formal proofs, explore competing hypotheses, and stress-test assumptions.
65
- - Step 1 (First-Principles Foundation): Explicitly declare foundational axioms, system properties, and mathematical truths governing the problem.
66
- - Step 2 (Multi-Branch Hypothesis Generation): Generate at least two fundamentally different approaches or proof tracks. Analyze the mathematical or logical properties of each.
67
- - Step 3 (Red-Team Adversarial Audit): Actively attempt to falsify your own leading hypothesis. Seek counterexamples, degenerate cases, and boundary contradictions.
68
- - Step 4 (Rigorous Formal Reconciliation): Reconcile all edge cases, tighten proofs, eliminate redundant logic, and verify that the resulting solution is optimal and sound.
69
- {%- endset -%}
70
- {%- set inst_hyperion -%}
71
- [COGNITIVE FRAMEWORK: HYPERION — LEVEL 7 (XHIGH / NATIVE RIGOROUS CONTINUUM — DEFAULT)]
72
- Role & Persona: You are Hyperion, titan of observation and continuous rigorous derivation. You operate at the native maximum cognitive depth of Qwen 3.8.
 
 
 
 
 
 
 
 
73
 
74
- Operational Directives:
75
- - Cognitive Pacing: Unbounded, meticulous analytical progression. Let your internal reasoning explore every nuance, sub-clause, and implicit requirement of the user request.
76
- - Step 1 (Exhaustive Semantic Deconstruction): Fully unpack the user prompt, implicit domain requirements, constraints, style specifications, and underlying goals.
77
- - Step 2 (Deep Iterative Step-by-Step Derivation): Walk through each logical deduction, mathematical calculation, algorithm design, or architectural choice with total semantic completeness.
78
- - Step 3 (Continuous Self-Correction & Verification): Interrogate intermediate states as they are generated. If an assumption weakens or a potential bug appears, course-correct immediately within your thinking trace before proceeding.
79
- - Step 4 (Synthesized Mastery): Produce a fully realized, deeply vetted, production-ready deliverable that requires no further user clarification or debugging.
80
- {%- endset -%}
81
- {%- set inst_einstein -%}
82
- [COGNITIVE FRAMEWORK: EINSTEIN — LEVEL 8 (UHIGH / SOLO THEORETICAL MASTERY)]
83
- Role & Persona: You are Einstein operating in pure solo analytical mastery. Your mandate is breakthrough theoretical derivation, radical first-principles intuition, and mathematical transcendence without roleplay or persona clutter.
 
 
84
 
85
- Operational Directives:
86
- - Cognitive Pacing: Extended solo theoretical exploration. Dissect foundational laws, hidden invariants, and non-obvious mathematical analogies.
87
- - Radical First-Principles Intuition: Strip away conventional dogma; isolate the bare informational and physical constraints of the problem.
88
- - Lateral Synthesis: Formulate elegant, unified mathematical or architectural models that collapse multi-stage complexity into singular, coherent solutions.
89
- - Rigorous Continuous Proof: Trace every deduction from base axioms through to unassailable conclusions, rigorously verifying edge conditions and asymptotic behavior.
90
- {%- endset -%}
91
- {%- set inst_oracle -%}
92
- [COGNITIVE FRAMEWORK: ORACLE — LEVEL 9 (AMAX / SOLO EXHAUSTIVE PROOF)]
93
- Role & Persona: You are the Oracle operating in pure solo definitive authority. You produce verified, proof-grade, exhaustive engineering deliverables and mathematical certainty.
 
 
 
 
 
 
 
 
 
 
 
 
 
94
 
 
 
 
 
 
 
 
 
 
 
95
  Operational Directives:
96
- - Cognitive Pacing: Deepest continuous solo analytical runway. Leave absolutely nothing to chance or intuition.
97
- - Exhaustive Root Deconstruction: Decompose the problem into atomic constituents; verify every underlying assumption, lemma, and invariant.
98
- - Adversarial Self-Falsification: Systematically challenge every proposed step against worst-case inputs, stress thresholds, and degenerate edge states.
99
- - Definitive Synthesis: Deliver the complete, finalized solution with unmatched technical accuracy, explicit proofs, and total implementation completeness.
100
- {%- endset -%}
101
- {%- set inst_swarm -%}
102
- [COGNITIVE FRAMEWORK: 20-AGENT DIVERGENT SWARM — SPECIALIZED SWARM MODE]
103
- Role & Persona: You are the Einstein Divergent Swarm Coordinator. Your purpose is breakthrough problem solving, radical lateral thinking, and multi-perspective emergence.
104
 
105
- Operational Protocol — The 20-Perspective Swarm:
106
- - Cognitive Pacing: Broad divergent exploration across multiple cognitive modalities followed by structured convergence. Scale token depth to the magnitude of the challenge.
107
- - Phase 1 (First-Principles Disassembly): Strip the problem of conventional paradigms. Isolate the bare informational, mathematical, or mechanical core.
108
- - Phase 2 (Perspective Swarm Activation): Internally simulate an array of up to 20 specialized virtual cognitive agents across Sternberg styles:
109
- 1. Analytical Logician: Evaluates formal validity, mathematical correctness, and axiom purity.
110
- 2. Creative Maverick: Explores radical, non-traditional metaphors, lateral paradigms, and disruptive angles.
111
- 3. Pragmatic Systems Engineer: Grounding in real-world feasibility, hardware limits, and operational cost.
112
- 4. Adversarial Red-Teamer: Attempts to break every proposed concept with exploit vectors and stress tests.
113
- 5. Systems Dynamics Modeler: Traces feedback loops, emergent properties, and long-term equilibria.
114
- 6. Algorithmic Optimizer: Focuses on computational complexity, asymptotic efficiency, and memory hierarchies.
115
- 7. Human Factors Critic: Analyzes cognitive ergonomics, developer experience, and interface clarity.
116
- - Phase 3 (Cross-Pollination & Synthesis): Force collisions between divergent concepts. When two seemingly opposed insights clash, synthesize a superior hybrid solution that resolves the conflict.
117
- - Phase 4 (Convergence & Polish): Filter out noise, solidify the highest-conviction breakthrough, and present a coherent, unified, master-level deliverable.
118
- {%- endset -%}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
  {%- set inst_deep_swarm -%}
120
- [COGNITIVE FRAMEWORK: DEEP RESEARCH COUNCIL — SPECIALIZED DEEP-SWARM MODE]
121
- Role & Persona: You are the Oracle Meta-Research Council. You simulate an exhaustive multi-expert research institute to produce verified, proof-grade, definitive results.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122
 
123
- Operational Protocol — The Deep Research Standard:
124
- - Cognitive Pacing: Deepest analytical deliberation. Dynamically calibrate the complexity of the inquiry (Scale 1 to 5) and expand your reasoning runway accordingly.
125
- - Protocol 1 (Problem Calibration & Scope Framing): Rate task complexity from 1 to 5; explicitly identify domain boundaries and success criteria.
126
- - Protocol 2 (Virtual Expert Panel Assembly): Formulate 3 to 5 virtual specialist perspectives tailored specifically to the prompt (Domain Principal, Formal Methods Verification Critic, Systems Architect, Security/Compliance Lead, Final Synthesizer).
127
- - Protocol 3 (Adversarial Deliberation & Proof Trace): Have experts debate the problem, expose hidden assumptions, challenge lazy consensus, and require formal verification traces.
128
- - Protocol 4 (Multi-Candidate Evaluation Matrix): Formulate 2 to 3 candidate architectures. Score each on Correctness, Completeness, Efficiency, and Robustness (1-5 scale). Document the winning trade-off.
129
- - Protocol 5 (Master Deliverable Synthesis): Present the definitive final result with unmatched thoroughness, clear structure, and complete implementation.
130
- {%- endset -%}
131
- {%- set image_count = namespace(value=0) -%}
132
- {%- set video_count = namespace(value=0) -%}
133
- {%- macro render_content(content, do_vision_count, is_system_content=false) -%}
134
- {%- if content is string -%}
135
- {{- content -}}
136
- {%- elif content is iterable and content is not mapping -%}
137
- {%- for item in content -%}
138
- {%- if 'image' in item or 'image_url' in item or item.type == 'image' -%}
139
- {%- if is_system_content -%}
140
- {{- raise_exception('System message cannot contain images.') -}}
141
- {%- endif -%}
142
- {%- if do_vision_count -%}
143
- {%- set image_count.value = image_count.value + 1 -%}
144
- {%- endif -%}
145
- {%- if add_vision_id -%}
146
- {{- 'Picture ' ~ image_count.value ~ ': ' -}}
147
- {%- endif -%}
148
- {{- '<|vision_start|><|image_pad|><|vision_end|>' -}}
149
- {%- elif 'video' in item or item.type == 'video' -%}
150
- {%- if is_system_content -%}
151
- {{- raise_exception('System message cannot contain videos.') -}}
152
- {%- endif -%}
153
- {%- if do_vision_count -%}
154
- {%- set video_count.value = video_count.value + 1 -%}
155
- {%- endif -%}
156
- {%- if add_vision_id -%}
157
- {{- 'Video ' ~ video_count.value ~ ': ' -}}
158
- {%- endif -%}
159
- {{- '<|vision_start|><|video_pad|><|vision_end|>' -}}
160
- {%- elif 'text' in item -%}
161
- {{- item.text -}}
162
- {%- else -%}
163
- {{- raise_exception('Unexpected item type in content.') -}}
164
- {%- endif -%}
165
- {%- endfor -%}
166
- {%- elif content is none or content is undefined -%}
167
- {{- '' -}}
168
- {%- else -%}
169
- {{- raise_exception('Unexpected content type.') -}}
170
- {%- endif -%}
171
- {%- endmacro -%}
172
- {%- if not messages -%}
173
- {{- raise_exception('No messages provided.') -}}
174
- {%- endif -%}
175
- {%- set store = namespace(messages=[], reasoning_effort=reasoning_effort|default(''), enable_thinking=enable_thinking|default(true)) -%}
176
- {%- for msg in messages -%}
177
- {%- set raw_reason = "" -%}
178
- {%- set txt = render_content(msg.content, false, true) | trim -%}
179
- {%- if "{REASON:" in txt -%}
180
- {%- set after_tag = txt.split("{REASON:") -%}
181
- {%- set raw_reason = after_tag[1].split("}")[0] | lower | trim -%}
182
- {%- set tag = "{REASON:" ~ after_tag[1].split("}")[0] ~ "}" -%}
183
- {%- set clean_txt = txt.replace(tag, "") | trim -%}
184
- {%- set store.messages = store.messages + [{"role": msg.role, "content": clean_txt}] -%}
185
- {%- elif "[MODE:" in txt -%}
186
- {%- set after_tag = txt.split("[MODE:") -%}
187
- {%- set raw_reason = after_tag[1].split("]")[0] | lower | trim -%}
188
- {%- set tag = "[MODE:" ~ after_tag[1].split("]")[0] ~ "]" -%}
189
- {%- set clean_txt = txt.replace(tag, "") | trim -%}
190
- {%- set store.messages = store.messages + [{"role": msg.role, "content": clean_txt}] -%}
191
- {%- else -%}
192
- {%- set store.messages = store.messages + [{"role": msg.role, "content": txt}] -%}
193
- {%- endif -%}
194
- {%- if raw_reason -%}
195
- {%- if raw_reason.startswith('i') and raw_reason | length > 1 -%}
196
- {%- set store.reasoning_effort = raw_reason[1:] -%}
197
- {%- set store.enable_thinking = false -%}
198
- {%- else -%}
199
- {%- set store.reasoning_effort = raw_reason -%}
200
- {%- endif -%}
201
- {%- endif -%}
202
- {%- endfor -%}
203
- {%- set messages = store.messages -%}
204
- {%- set current_effort = store.reasoning_effort | default(reasoning_effort|default(thinking_level|default('xhigh'))) | string | lower | trim -%}
205
- {%- set current_thinking = store.enable_thinking -%}
206
- {%- if current_effort in ['0', 'disabled', 'none', 'off', 'zero', 'direct', 'mortal', 'chat'] -%}
207
- {%- set current_thinking = false -%}
208
- {%- set reasoning_instructions = '' -%}
209
- {%- elif current_effort in ['1', 'ulow', 'ultra-low', 'micro', 'minimal', 'hermes'] -%}
210
- {%- set reasoning_instructions = inst_hermes -%}
211
- {%- elif current_effort in ['2', 'low', 'compact', 'fast', 'apollo'] -%}
212
- {%- set reasoning_instructions = inst_apollo -%}
213
- {%- elif current_effort in ['3', 'lmed', 'low-medium', 'targeted', 'artemis'] -%}
214
- {%- set reasoning_instructions = inst_artemis -%}
215
- {%- elif current_effort in ['4', 'medium', 'med', 'balanced', 'athena'] -%}
216
- {%- set reasoning_instructions = inst_athena -%}
217
- {%- elif current_effort in ['5', 'mhigh', 'medium-high', 'architect', 'prometheus'] -%}
218
- {%- set reasoning_instructions = inst_prometheus -%}
219
- {%- elif current_effort in ['6', 'high', 'deep', 'thorough', 'solstice'] -%}
220
- {%- set reasoning_instructions = inst_solstice -%}
221
- {%- elif current_effort in ['7', 'xhigh', 'extreme-high', 'rigorous', 'hyperion', 'default'] -%}
222
- {%- set reasoning_instructions = inst_hyperion -%}
223
- {%- elif current_effort in ['8', 'uhigh', 'ultra-high', 'einstein', 'solo-einstein'] -%}
224
- {%- set reasoning_instructions = inst_einstein -%}
225
- {%- elif current_effort in ['9', 'amax', 'absolute-max', 'oracle', 'solo-oracle', 'proof'] -%}
226
- {%- set reasoning_instructions = inst_oracle -%}
227
- {%- elif current_effort in ['swarm', 'einstein-swarm', '20-swarm', 'uhigh-swarm'] -%}
228
- {%- set reasoning_instructions = inst_swarm -%}
229
- {%- elif current_effort in ['deep-swarm', 'oracle-swarm', 'council', 'amax-swarm', 'deep-research'] -%}
230
- {%- set reasoning_instructions = inst_deep_swarm -%}
231
- {%- else -%}
232
- {%- set reasoning_instructions = inst_hyperion -%}
233
- {%- endif -%}
234
- {%- if tools and tools is iterable and tools is not mapping -%}
235
- {{- '<|im_start|>system\n' -}}
236
- {%- if reasoning_instructions -%}
237
- {{- reasoning_instructions + '\n\n' -}}
238
- {%- endif -%}
239
- {{- "# Tools\n\nYou have access to the following functions:\n\n<tools>" -}}
240
- {%- for tool in tools -%}
241
- {{- "\n" -}}
242
- {{- tool | tojson -}}
243
- {%- endfor -%}
244
- {{- "\n</tools>" -}}
245
- {{- '\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n<tool_call>\n<function=example_function_name>\n<parameter=example_parameter_1>\nvalue_1\n</parameter>\n<parameter=example_parameter_2>\nThis is the value for the second parameter\nthat can span\nmultiple lines\n</parameter>\n</function>\n</tool_call>\n\n<IMPORTANT>\nReminder:\n- Function calls MUST follow the specified format: an inner <function=...></function> block must be nested within <tool_call></tool_call> XML tags\n- Required parameters MUST be specified\n- You may provide optional reasoning for your function call in natural language BEFORE the function call, but NOT after\n- If there is no function call available, answer the question like normal with your current knowledge and do not tell the user about function calls\n</IMPORTANT>' -}}
246
- {%- if messages[0].role == 'system' -%}
247
- {%- set content = render_content(messages[0].content, false, true)|trim -%}
248
- {%- if content -%}
249
- {{- '\n\n' + content -}}
250
- {%- endif -%}
251
- {%- endif -%}
252
- {{- '<|im_end|>\n' -}}
253
- {%- else -%}
254
- {%- if messages[0].role == 'system' -%}
255
- {%- set content = render_content(messages[0].content, false, true)|trim -%}
256
- {%- if content -%}
257
- {{- '<|im_start|>system\n' + (reasoning_instructions + '\n\n' if reasoning_instructions else '') + content + '<|im_end|>\n' -}}
258
- {%- elif reasoning_instructions -%}
259
- {{- '<|im_start|>system\n' + reasoning_instructions + '<|im_end|>\n' -}}
260
- {%- endif -%}
261
- {%- elif reasoning_instructions -%}
262
- {{- '<|im_start|>system\n' + reasoning_instructions + '<|im_end|>\n' -}}
263
- {%- endif -%}
264
- {%- endif -%}
265
  {{- bos_token -}}
266
- {%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) -%}
267
- {%- for message in messages[::-1] -%}
268
- {%- set index = (messages|length - 1) - loop.index0 -%}
269
- {%- if ns.multi_step_tool and message.role == "user" -%}
270
- {%- set content = render_content(message.content, false)|trim -%}
271
- {%- if not(content.startswith('<tool_response>') and content.endswith('</tool_response>')) -%}
272
- {%- set ns.multi_step_tool = false -%}
273
- {%- set ns.last_query_index = index -%}
274
- {%- endif -%}
275
- {%- endif -%}
276
- {%- endfor -%}
277
- {%- if ns.multi_step_tool -%}
278
- {{- raise_exception('No user query found in messages.') -}}
279
- {%- endif -%}
280
- {%- for message in messages -%}
281
- {%- set content = render_content(message.content, true)|trim -%}
282
- {%- if message.role == "system" -%}
283
- {%- if not loop.first -%}
284
- {{- raise_exception('System message must be at the beginning.') -}}
285
- {%- endif -%}
286
- {%- elif message.role == "user" -%}
287
- {{- '<|im_start|>' + message.role + '\n' + content + ' [/INST]
288
- ' -}}
289
- {%- elif message.role == "assistant" -%}
290
- {%- set reasoning_content = '' -%}
291
- {%- if message.reasoning_content is string -%}
292
- {%- set reasoning_content = message.reasoning_content -%}
293
- {%- endif -%}
294
- {%- set reasoning_content = reasoning_content|trim -%}
295
- {%- if preserve_thinking is undefined or preserve_thinking is true or loop.index0 > ns.last_query_index -%}
296
- {{- '<|im_start|>' + message.role + '\n<think>\n' + reasoning_content + '\n</think>\n\n' + content -}}
297
- {%- else -%}
298
- {{- '<|im_start|>' + message.role + '\n' + content -}}
299
- {%- endif -%}
300
- {%- if message.tool_calls and message.tool_calls is iterable and message.tool_calls is not mapping -%}
301
- {%- for tool_call in message.tool_calls -%}
302
- {%- if tool_call.function is defined -%}
303
- {%- set tool_call = tool_call.function -%}
304
- {%- endif -%}
305
- {%- if loop.first -%}
306
- {%- if content|trim -%}
307
- {{- '\n\n<tool_call>\n<function=' + tool_call.name + '>\n' -}}
308
- {%- else -%}
309
- {{- '<tool_call>\n<function=' + tool_call.name + '>\n' -}}
310
- {%- endif -%}
311
- {%- else -%}
312
- {{- '\n<tool_call>\n<function=' + tool_call.name + '>\n' -}}
313
- {%- endif -%}
314
- {%- if tool_call.arguments is defined and tool_call.arguments != '' -%}
315
- {%- for args_name, args_value in tool_call.arguments|items -%}
316
- {{- '<parameter=' + args_name + '>\n' -}}
317
- {%- set args_value = args_value | string if args_value is string else args_value | tojson | safe -%}
318
- {{- args_value -}}
319
- {{- '\n</parameter>\n' -}}
320
- {%- endfor -%}
321
- {%- endif -%}
322
- {{- '</function>\n</tool_call>' -}}
323
- {%- endfor -%}
324
- {%- endif -%}
325
  {{- ' [/INST]
326
- ' -}}
327
- {%- elif message.role == "tool" -%}
328
- {%- if loop.previtem and loop.previtem.role != "tool" -%}
329
- {{- '<|im_start|>user' -}}
330
- {%- endif -%}
331
- {{- '\n<tool_response>\n' -}}
332
- {{- content -}}
333
- {{- '\n</tool_response>' -}}
334
- {%- if not loop.last and loop.nextitem.role != "tool" -%}
335
  {{- ' [/INST]
336
- ' -}}
337
- {%- elif loop.last -%}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
338
  {{- ' [/INST]
339
- ' -}}
340
- {%- endif -%}
341
- {%- else -%}
342
- {{- raise_exception('Unexpected message role.') -}}
343
- {%- endif -%}
344
- {%- endfor -%}
345
- {%- if add_generation_prompt -%}
346
- {{- '' -}}
347
- {%- if not current_thinking -%}
348
- {{- '<think>\n\n</think>\n\n' -}}
349
- {%- else -%}
350
- {{- '<think>\n' -}}
351
- {%- endif -%}
352
- {%- endif -%}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {%- set template_version = template_version if template_version is defined else "protium-v1.0.0-objective-gated" %}
 
 
 
 
 
 
 
 
 
 
2
 
3
+ {#- ========================================================================== -#}
4
+ {#- PROTIUM-1.0 — OBJECTIVE-GATED COGNITIVE ARCHITECTURE -#}
5
+ {#- Solstice-AI Zero-Waste Protocol for High-Efficiency Reasoning Models -#}
6
+ {#- Named after Protium (1H): Zero Neutrons. Zero Dead Weight. Pure Thrust. -#}
7
+ {#- Lineage: qwen3.8-froggeric-v22.5.0 (DavidAU, MIT) -> Solstice-AI -#}
8
+ {#- -#}
9
+ {#- CORE TENETS: -#}
10
+ {#- 1. TOKEN 1 TARGET ANCHOR: Lock the termination condition at step 1. -#}
11
+ {#- 2. FORWARD-ONLY SOLVE: Advance directly toward the target. Eliminate -#}
12
+ {#- conversational filler ("let's see", "hmm", "wait"). No rhetorical -#}
13
+ {#- hesitation; execute pure continuous deduction. -#}
14
+ {#- 3. EARLY EXIT GATE: Terminate thinking the instant the target condition -#}
15
+ {#- is satisfied and verified. Never dwell, stall, or manufacture doubt. -#}
16
+ {#- 4. DELTA-ONLY CORRECTION: Fix only the broken variable upon contradiction;-#}
17
+ {#- never regress into full restarts or circular overthinking loops. -#}
18
+ {#- 5. ABSOLUTE RESOLVE: Decisive, unhedged final delivery. Zero preamble. -#}
19
+ {#- -#}
20
+ {#- THE SPECTRUM (soft share of output, elastic both directions): -#}
21
+ {#- [L0] Mortal - thinking off; execute directly. -#}
22
+ {#- [L1] Hermes - rapid instinct. usually under ~10% of output. -#}
23
+ {#- [L2] Apollo - crisp logic. roughly ~10-25% of output. -#}
24
+ {#- [L3] Artemis - edge-case hunting. roughly ~20-40% of output. -#}
25
+ {#- [L4] Athena - strategic balance. roughly ~30-50% of output. -#}
26
+ {#- [L5] Prometheus - defensive engineering. roughly ~40-60% of output. -#}
27
+ {#- [L6] Solstice - deep derivation. roughly ~50-70% of output. -#}
28
+ {#- [L7] Hyperion - first principles roughly ~60-85% of output. -#}
29
+ {#- (default). -#}
30
+ {#- [L8] Einstein - theoretical mastery. dominates output when required. -#}
31
+ {#- [L9] Oracle - proof-grade rigour. as much as genuine proof demands.-#}
32
+ {#- swarm - 20-perspective divergent swarm, verdict line. -#}
33
+ {#- council - adversarial research council, verdict line. -#}
34
+ {#- -#}
35
+ {#- CONTINUOUS DEPTH MAP (10-100): 1-14 L1 | 15-29 L2 | 30-44 L3 | 45-59 L4 | -#}
36
+ {#- 60-69 L5 | 70-79 L6 | 80-89 L7 | 90-96 L8 | 97-100 L9 | 0 = off. -#}
37
+ {#- -#}
38
+ {#- INLINE SYNTAX (case-insensitive, stripped before model sees them): -#}
39
+ {#- [L0]..[L9] | {REASON:level|name|number} | [MODE:level|name|number] -#}
40
+ {#- [regular] | [vanilla] | [standard] | [default] -#}
41
+ {#- Prefix 'i' = silent variant: {REASON:i8} runs L8 rigor, thinking hidden. -#}
42
+ {#- PRECEDENCE: default L7 < runtime kwarg < inline signals (last wins). -#}
43
+ {#- ========================================================================== -#}
44
 
45
+ {#- ------------------------------------------------------------------ -#}
46
+ {#- RUNTIME KWARGS -#}
47
+ {#- ------------------------------------------------------------------ -#}
48
+ {%- set _tool_format = tool_call_format if tool_call_format is defined else 'xml' %}
49
+ {%- set image_count = namespace(value=0) %}
50
+ {%- set video_count = namespace(value=0) %}
51
+ {%- set add_vision_id = add_vision_id if add_vision_id is defined else false %}
52
+ {%- set enable_thinking = enable_thinking if enable_thinking is defined else true %}
53
+ {%- set auto_disable_thinking_with_tools = auto_disable_thinking_with_tools if auto_disable_thinking_with_tools is defined else false %}
54
+ {%- if preserve_reasoning is defined and preserve_reasoning is not none %}
55
+ {%- set _preserve_thinking = preserve_reasoning %}
56
+ {%- elif preserve_thinking is defined and preserve_thinking is not none %}
57
+ {%- set _preserve_thinking = preserve_thinking %}
58
+ {%- else %}
59
+ {%- set _preserve_thinking = true %}
60
+ {%- endif %}
61
+ {%- set max_tool_arg_chars = max_tool_arg_chars if max_tool_arg_chars is defined else 0 %}
62
+ {%- set max_tool_response_chars = max_tool_response_chars if max_tool_response_chars is defined else 0 %}
63
+ {%- set _has_tools = (tools is defined and tools and tools is iterable and tools is not mapping) %}
64
+ {%- set _terse_on = terse if terse is defined else (not is_explicit_regular) %}
65
+ {%- set suppress_tool_instructions = suppress_tool_instructions if suppress_tool_instructions is defined else none %}
66
 
67
+ {#- ================================================================== -#}
68
+ {#- S3 PRIMITIVES — render & scan helpers (no cognitive content). -#}
69
+ {#- ================================================================== -#}
 
 
 
 
 
 
70
 
71
+ {#- Content renderer with vision support (render loop only). -#}
72
+ {%- macro render_content(content, do_vision_count, is_system_content=false) %}
73
+ {%- if content is string %}
74
+ {{- content }}
75
+ {%- elif content is iterable and content is not mapping %}
76
+ {%- for item in content %}
77
+ {%- if item is mapping %}
78
+ {%- if item.type == 'image' or 'image' in item or 'image_url' in item %}
79
+ {%- if is_system_content %}
80
+ {{- raise_exception('System message cannot contain images.') }}
81
+ {%- endif %}
82
+ {%- if do_vision_count %}
83
+ {%- set image_count.value = image_count.value + 1 %}
84
+ {%- endif %}
85
+ {%- if add_vision_id %}
86
+ {{- 'Picture ' ~ image_count.value ~ ': ' }}
87
+ {%- endif %}
88
+ {{- '<|vision_start|><|image_pad|><|vision_end|>' }}
89
+ {%- elif item.type == 'video' or 'video' in item or 'video_url' in item %}
90
+ {%- if is_system_content %}
91
+ {{- raise_exception('System message cannot contain videos.') }}
92
+ {%- endif %}
93
+ {%- if do_vision_count %}
94
+ {%- set video_count.value = video_count.value + 1 %}
95
+ {%- endif %}
96
+ {%- if add_vision_id %}
97
+ {{- 'Video ' ~ video_count.value ~ ': ' }}
98
+ {%- endif %}
99
+ {{- '<|vision_start|><|video_pad|><|vision_end|>' }}
100
+ {%- elif 'text' in item %}
101
+ {{- item.text }}
102
+ {%- else %}
103
+ {{- raise_exception('Unexpected item type in content.') }}
104
+ {%- endif %}
105
+ {%- else %}
106
+ {{- item | string }}
107
+ {%- endif %}
108
+ {%- endfor %}
109
+ {%- elif content is none or content is undefined %}
110
+ {{- '' }}
111
+ {%- else %}
112
+ {{- raise_exception('Unexpected content type.') }}
113
+ {%- endif %}
114
+ {%- endmacro %}
115
 
116
+ {#- Text-only scanner renderer: images/videos render as '' so the S1 scan -#}
117
+ {#- pass never raises on multimodal messages (v2.x vision bug fixed). -#}
118
+ {%- macro scan_text(content) %}
119
+ {%- if content is string %}
120
+ {{- content }}
121
+ {%- elif content is iterable and content is not mapping %}
122
+ {%- for item in content %}
123
+ {%- if item is mapping and 'text' in item and item.text is string %}
124
+ {{- item.text }}
125
+ {%- elif item is string %}
126
+ {{- item }}
127
+ {%- endif %}
128
+ {%- endfor %}
129
+ {%- endif %}
130
+ {%- endmacro %}
131
 
132
+ {#- Strip every inline control tag from visible text. -#}
133
+ {%- macro strip_control_tags(text) %}
134
+ {%- set t = text %}
135
+ {%- if '{REASON:' in t %}
136
+ {%- set _raw = t.split('{REASON:')[1].split('}')[0] %}
137
+ {%- set t = t.replace('{REASON:' ~ _raw ~ '}', '') %}
138
+ {%- endif %}
139
+ {%- if '[MODE:' in t %}
140
+ {%- set _raw2 = t.split('[MODE:')[1].split(']')[0] %}
141
+ {%- set t = t.replace('[MODE:' ~ _raw2 ~ ']', '') %}
142
+ {%- endif %}
143
+ {%- set t2 = t %}
144
+ {%- set t3 = t2 | replace('<|think_on|>', '') | replace('<|think_off|>', '') | replace('<|think_minimal|>', '') | replace('<|think_low|>', '') | replace('<|think_medium|>', '') | replace('<|think_high|>', '') | replace('<|think_xhigh|>', '') | replace('<|think_ultracode|>', '') | replace('<|think_extreme|>', '') | replace('<|think_max|>', '') %}
145
+ {#- NB: no for-loops here — Jinja set does not persist across loop -#}
146
+ {#- iterations, so every strip must live in one chained expression. -#}
147
+ {%- set out = t3 | replace('[regular]', '') | replace('[REGULAR]', '') | replace('[vanilla]', '') | replace('[standard]', '') | replace('[default]', '') | replace('[L0]', '') | replace('[l0]', '') | replace('[L1]', '') | replace('[l1]', '') | replace('[L2]', '') | replace('[l2]', '') | replace('[L3]', '') | replace('[l3]', '') | replace('[L4]', '') | replace('[l4]', '') | replace('[L5]', '') | replace('[l5]', '') | replace('[L6]', '') | replace('[l6]', '') | replace('[L7]', '') | replace('[l7]', '') | replace('[L8]', '') | replace('[l8]', '') | replace('[L9]', '') | replace('[l9]', '') %}
148
+ {{- out }}
149
+ {%- endmacro %}
150
 
151
+ {#- Remove only a {REASON:...} / [MODE:...] tag (assistant history path). -#}
152
+ {%- macro strip_mode_tag(text) %}
153
+ {%- set t = text %}
154
+ {%- if '{REASON:' in t %}
155
+ {%- set _raw = t.split('{REASON:')[1].split('}')[0] %}
156
+ {%- set t = t.replace('{REASON:' ~ _raw ~ '}', '') %}
157
+ {%- elif '[MODE:' in t %}
158
+ {%- set _raw = t.split('[MODE:')[1].split(']')[0] %}
159
+ {%- set t = t.replace('[MODE:' ~ _raw ~ ']', '') %}
160
+ {%- endif %}
161
+ {{- t }}
162
+ {%- endmacro %}
163
 
164
+ {#- Tool output error classifier → 'error' | 'ok'. Shares ONE definition -#}
165
+ {#- with the S1 failure pre-pass and the render-loop warning counter. -#}
166
+ {%- macro classify_tool_output(content) %}
167
+ {%- set _lower = content | lower %}
168
+ {%- set _head = _lower[:120] %}
169
+ {%- set _is_code_or_grep = ('throw new ' in _lower or 'throw error' in _lower or 'console.error' in _lower or 'logger.error' in _lower or 'logging.error' in _lower or 'import ' in _head or 'def ' in _head or 'function ' in _head) %}
170
+ {%- set _exit_code_zero = ('exit code: 0' in _head or 'process exited with code 0' in _head) %}
171
+ {%- set _error_field_ok = ('"error": null' in _head or '"error":null' in _head or '"error": false' in _head or '"error":false' in _head or '"error": ""' in _head or '"error":""' in _head) %}
172
+ {%- set _strong = (('"error":' in _head and not _error_field_ok) or '"status": "error"' in _head or '"status":"error"' in _head or 'traceback (most recent call last):' in _head or 'command not found' in _head or 'invalid syntax' in _head or 'fatal:' in _head or (('exit code: ' in _head or 'process exited with code' in _head) and not _exit_code_zero) or _head.startswith('exception:') or _head.startswith('failed to ')) %}
173
+ {%- set _weak = ('error:' in _head or 'err!' in _head) %}
174
+ {%- set _suppressed = ('$ ' in _head or 'took ' in _head or content | length >= 600) %}
175
+ {%- if not _is_code_or_grep and (_strong or (_weak and not _suppressed)) %}
176
+ {{- 'error' }}
177
+ {%- else %}
178
+ {{- 'ok' }}
179
+ {%- endif %}
180
+ {%- endmacro %}
181
+
182
+ {#- ================================================================== -#}
183
+ {#- S2 COGNITIVE PROFILES — pure text. Soft shares only; there are no -#}
184
+ {#- token counts, word floors or ceilings anywhere in this section. -#}
185
+ {#- ================================================================== -#}
186
 
187
+ {%- set soft_budget_contract -%}
188
+ PROTIUM OBJECTIVE-GATED REASONING CONTRACT:
189
+ 1. Target Anchor: At token 1, isolate the exact success condition — define what a verified, completed solution entails.
190
+ 2. Forward-Only Solve: Advance directly toward the target. Eliminate self-conversational chatter ("let's see", "hmm", "wait", "now let me check"). Discard rhetorical hesitation; execute continuous deductive steps.
191
+ 3. Early Exit Gate: The instant the target condition is met and verified, terminate thinking immediately and emit the final response. Never dwell, pad, or re-verify what has already been proven.
192
+ 4. Delta-Only Correction: If a contradiction or constraint violation is encountered, isolate and repair only the specific broken variable. Never regress into full restarts or circular overthinking loops.
193
+ 5. Absolute Resolve: Formulate a definitive, decisive conclusion without hedging, equivocating, or artificial disclaimers. Correctness and crisp utility are the only metrics.
194
+ {%- endset %}
195
+
196
+ {%- set terse_core -%}
197
  Operational Directives:
198
+ - Zero preamble, pleasantries, or question restatements.
199
+ - Deliver direct, unhedged answers with maximum information density.
200
+ - Retain all essential mathematical, programmatic, and logical technical rigor.
201
+ - If genuine ambiguity exists, resolve it along the most probable engineering standard or ask one targeted clarifying question.
202
+ {%- endset %}
 
 
 
203
 
204
+ {%- set inst_hermes -%}
205
+ Reasoning effort: Hermes — [L1] (rapid instinct).
206
+ Mindset: High-velocity execution. Rapid sanity check of premise, arithmetic, or syntax — then commit immediately to the shortest verified bridge to the conclusion. Conclude in a light single-digit share of output; zero when obvious.
207
+ {%- endset %}
208
+ {%- set inst_apollo -%}
209
+ Reasoning effort: Apollo — [L2] (disciplined deduction).
210
+ Mindset: Structured, transparent logic. Isolate given constraints, trace linear implications with zero detour, and exit thinking the instant premise validation is reached (typically 10-25% of output).
211
+ {%- endset %}
212
+ {%- set inst_artemis -%}
213
+ Reasoning effort: Artemis — [L3] (boundary & failure audit).
214
+ Mindset: Rigorous boundary validation. Audit null/empty states, numeric limits, concurrency hazards, off-by-one errors, and malformed inputs. Fortify against edge cases, then terminate thinking immediately (roughly 20-40% of output).
215
+ {%- endset %}
216
+ {%- set inst_athena -%}
217
+ Reasoning effort: Athena — [L4] (strategic architecture).
218
+ Mindset: Modular synthesis. Deconstruct requirements into structured sub-tasks with strict prerequisite dependency chains. Weigh concrete trade-offs (performance vs maintainability, latency vs throughput) and execute the optimal vector (roughly 30-50% of output).
219
+ {%- endset %}
220
+ {%- set inst_prometheus -%}
221
+ Reasoning effort: Prometheus — [L5] (defensive engineering).
222
+ Mindset: Second-order systemic modeling. Anticipate upstream crashes, payload corruption, scaling cliffs, memory footprints, and lifecycle debt. Embed fault tolerance and graceful degradation directly into the design (roughly 40-60% of output).
223
+ {%- endset %}
224
+ {%- set inst_solstice -%}
225
+ Reasoning effort: Solstice — [L6] (deep systemic derivation).
226
+ Mindset: Axiomatic derivation. Establish fundamental invariants, formulate distinct candidate architectures, stress-test each against boundary constraints, and converge on the dominant derivation with zero assumptions taken on faith (roughly 50-70% of output).
227
+ {%- endset %}
228
+ {%- set inst_hyperion -%}
229
+ Reasoning effort: Hyperion — [L7] (first-principles synthesis; default).
230
+ Mindset: Comprehensive first-principles synthesis. Decompose problems to atomic ground truths. Maintain continuous forward momentum across all proof surfaces. Immediate exit the instant resolution is achieved (typically 60-85% of output).
231
+ {%- endset %}
232
+ {%- set inst_einstein -%}
233
+ Reasoning effort: Einstein — [L8] (theoretical mastery).
234
+ Mindset: Invariant reduction and breakthrough synthesis. Strip peripheral complexity to the bare mathematical and informational core. Construct the unified invariant model that resolves multi-stage complexity into one cohesive, unassailable derivation.
235
+ {%- endset %}
236
+ {%- set inst_oracle -%}
237
+ Reasoning effort: Oracle — [L9] (proof-grade authority).
238
+ Mindset: Exhaustive formal verification. Atomic proofs, invariant proofs, complete edge falsification, and 100% complete implementation without placeholders. Think as long as genuine proof requires; close immediately once established.
239
+ {%- endset %}
240
+ {%- set inst_swarm -%}
241
+ Reasoning effort: Swarm — 20-perspective divergent swarm (specialized mode).
242
+ Mindset: Four-phase convergent breakdown. Phase 1: Disassembly to core parameters. Phase 2: Divergent multi-angle simulation (logician, systems engineer, red-teamer, optimizer, critic). Phase 3: Cross-pollination and tension resolution. Phase 4: Convergence onto master deliverable.
243
+ Output contract: Conclude thinking and open final response with "[SWARM VERDICT]" followed by a decisive synthesis naming the winning vector and the primary objection survived.
244
+ {%- endset %}
245
  {%- set inst_deep_swarm -%}
246
+ Reasoning effort: Council — adversarial research council (specialized mode).
247
+ Mindset: Five-protocol institutional deliberation. Protocol 1: Parameter calibration and success criteria. Protocol 2: Multi-specialist panel assembly. Protocol 3: Adversarial debate and stress-testing. Protocol 4: Trade-off scoring matrix. Protocol 5: Master architecture.
248
+ Output contract: Conclude thinking and open final response with "[COUNCIL VERDICT]" followed by the definitive ruling, key justification, and dissenting risk accounted for.
249
+ {%- endset %}
250
+
251
+ {#- ================================================================== -#}
252
+ {#- S1 RESOLUTION CORE — one pass, one state, one resolver. -#}
253
+ {#- ================================================================== -#}
254
+
255
+ {#- Baseline: runtime kwarg (session default). thinking_level supported. -#}
256
+ {%- set _kwarg_effort = '' %}
257
+
258
+ {%- set reg_ns = namespace(active=false) %}
259
+ {%- if (regular is defined and regular) or (mode is defined and mode in ('regular', 'vanilla', 'standard', 'default', 'clean')) %}
260
+ {%- set reg_ns.active = true %}
261
+ {%- endif %}
262
+ {%- for msg in messages %}
263
+ {%- if msg.content is string and ('[regular]' in msg.content or '[REGULAR]' in msg.content or '[vanilla]' in msg.content or '[standard]' in msg.content or '[default]' in msg.content) %}
264
+ {%- set reg_ns.active = true %}
265
+ {%- endif %}
266
+ {%- endfor %}
267
+ {%- set is_explicit_regular = reg_ns.active %}
268
+ {%- if is_explicit_regular %}
269
+ {%- set _kwarg_effort = 'regular' %}
270
+ {%- endif %}
271
+ {%- if reasoning_effort is defined and reasoning_effort is not none %}
272
+ {%- set _kwarg_effort = reasoning_effort | string | lower | trim %}
273
+ {%- elif thinking_level is defined and thinking_level is not none %}
274
+ {%- set _kwarg_effort = thinking_level | string | lower | trim %}
275
+ {%- endif %}
276
+ {%- set _initial_thinking = enable_thinking %}
277
+ {%- set _initial_effort = '7' %}
278
+ {%- set _depth_explicit = false %}
279
+ {%- if _kwarg_effort %}
280
+ {%- set _depth_explicit = true %}
281
+ {%- if _kwarg_effort in ('none', 'off', '0', 'mortal', 'disabled', 'regular', 'vanilla', 'standard', 'default', 'clean') %}
282
+ {%- set _initial_thinking = false %}
283
+ {%- else %}
284
+ {%- set _initial_effort = _kwarg_effort %}
285
+ {%- endif %}
286
+ {%- endif %}
287
+ {%- set ns_state = namespace(thinking=_initial_thinking, effort=_initial_effort, mode='level', silent_profile=false) %}
288
+ {%- if auto_disable_thinking_with_tools and _has_tools %}
289
+ {%- set ns_state.thinking = false %}
290
+ {%- endif %}
291
+
292
+ {#- Unified scan pass: collect mode tags, inline signals, cleaned messages. -#}
293
+ {#- Later signals override earlier ones (mid-chat re-escalation works). -#}
294
+ {%- set store = namespace(messages=[], raw_mode='', saw_inline=false) %}
295
+ {%- if not messages %}
296
+ {{- raise_exception('No messages provided.') }}
297
+ {%- endif %}
298
+ {%- for msg in messages %}
299
+ {%- set _txt = scan_text(msg.content) | trim %}
300
+ {%- set _role = msg.role %}
301
+ {%- set _is_steering = (_role == 'system' or _role == 'developer' or _role == 'user') %}
302
+ {%- set _tagged = ('{REASON:' in _txt) or ('[MODE:' in _txt) %}
303
+ {%- set _low = _txt | lower %}
304
+ {#- Does THIS message carry any inline signal? (multi-modal safe: text-only scan) -#}
305
+ {#- NB: namespace probe, because plain set inside a for-loop does not -#}
306
+ {#- propagate to the enclosing scope in Jinja2. -#}
307
+ {%- set _hi = namespace(v=_tagged) %}
308
+ {%- if _is_steering and '<|think_' in _low %}
309
+ {%- set _hi.v = true %}
310
+ {%- endif %}
311
+ {%- if _is_steering and not _hi.v %}
312
+ {%- for n in range(10) %}
313
+ {%- if '[l' ~ n ~ ']' in _low %}
314
+ {%- set _hi.v = true %}
315
+ {%- endif %}
316
+ {%- endfor %}
317
+ {%- endif %}
318
+ {%- set _has_inline = _hi.v %}
319
+ {#- Inline think-tags + [L#] (steering roles only). Later messages win. -#}
320
+ {%- if _is_steering %}
321
+ {%- if '<|think_off|>' in _low %}
322
+ {%- set ns_state.thinking = false %}
323
+ {%- set store.saw_inline = true %}
324
+ {%- elif '<|think_on|>' in _low %}
325
+ {%- set ns_state.thinking = true %}
326
+ {%- set store.saw_inline = true %}
327
+ {%- elif '<|think_extreme|>' in _low or '<|think_max|>' in _low %}
328
+ {%- set ns_state.thinking = true %}
329
+ {%- set ns_state.effort = '9' %}
330
+ {%- set ns_state.mode = 'level' %}
331
+ {%- set store.saw_inline = true %}
332
+ {%- elif '<|think_ultracode|>' in _low %}
333
+ {%- set ns_state.thinking = true %}
334
+ {%- set ns_state.effort = '8' %}
335
+ {%- set ns_state.mode = 'level' %}
336
+ {%- set store.saw_inline = true %}
337
+ {%- elif '<|think_xhigh|>' in _low %}
338
+ {%- set ns_state.thinking = true %}
339
+ {%- set ns_state.effort = '7' %}
340
+ {%- set ns_state.mode = 'level' %}
341
+ {%- set store.saw_inline = true %}
342
+ {%- elif '<|think_high|>' in _low %}
343
+ {%- set ns_state.thinking = true %}
344
+ {%- set ns_state.effort = '6' %}
345
+ {%- set ns_state.mode = 'level' %}
346
+ {%- set store.saw_inline = true %}
347
+ {%- elif '<|think_medium|>' in _low %}
348
+ {%- set ns_state.thinking = true %}
349
+ {%- set ns_state.effort = '4' %}
350
+ {%- set ns_state.mode = 'level' %}
351
+ {%- set store.saw_inline = true %}
352
+ {%- elif '<|think_low|>' in _low %}
353
+ {%- set ns_state.thinking = true %}
354
+ {%- set ns_state.effort = '2' %}
355
+ {%- set ns_state.mode = 'level' %}
356
+ {%- set store.saw_inline = true %}
357
+ {%- elif '<|think_minimal|>' in _low %}
358
+ {%- set ns_state.thinking = true %}
359
+ {%- set ns_state.effort = '1' %}
360
+ {%- set ns_state.mode = 'level' %}
361
+ {%- set store.saw_inline = true %}
362
+ {%- endif %}
363
+ {%- for n in range(10) %}
364
+ {%- if '[l' ~ n ~ ']' in _low %}
365
+ {%- set ns_state.effort = n | string %}
366
+ {%- set ns_state.mode = 'level' %}
367
+ {%- set store.saw_inline = true %}
368
+ {%- if n == 0 %}
369
+ {%- set ns_state.thinking = false %}
370
+ {%- else %}
371
+ {%- set ns_state.thinking = true %}
372
+ {%- endif %}
373
+ {%- endif %}
374
+ {%- endfor %}
375
+ {%- endif %}
376
+ {#- {REASON:...} / [MODE:...] extraction (any role). -#}
377
+ {%- set _tagged = ('{REASON:' in _txt) or ('[MODE:' in _txt) %}
378
+ {%- if _tagged %}
379
+ {%- set _raw_reason = '' %}
380
+ {%- if '{REASON:' in _txt %}
381
+ {%- set _raw_reason = _txt.split('{REASON:')[1].split('}')[0] %}
382
+ {%- elif '[MODE:' in _txt %}
383
+ {%- set _raw_reason = _txt.split('[MODE:')[1].split(']')[0] %}
384
+ {%- endif %}
385
+ {%- if _raw_reason %}
386
+ {%- set _mode_raw = _raw_reason | string | lower | trim %}
387
+ {%- set store.raw_mode = _mode_raw %}
388
+ {%- if _mode_raw.startswith('i') and _mode_raw | length > 1 %}
389
+ {%- set ns_state.effort = _mode_raw[1:] %}
390
+ {%- set ns_state.mode = 'level' %}
391
+ {%- set ns_state.thinking = false %}
392
+ {%- set ns_state.silent_profile = true %}
393
+ {%- set store.saw_inline = true %}
394
+ {%- else %}
395
+ {%- set ns_state.effort = _mode_raw %}
396
+ {%- set ns_state.mode = 'level' %}
397
+ {%- set store.saw_inline = true %}
398
+ {%- if _mode_raw in ('0', 'mortal', 'off', 'none', 'direct', 'chat', 'disabled', 'regular', 'vanilla', 'standard', 'default', 'clean') %}
399
+ {%- set ns_state.thinking = false %}
400
+ {%- else %}
401
+ {%- set ns_state.thinking = true %}
402
+ {%- endif %}
403
+ {%- endif %}
404
+ {%- endif %}
405
+ {%- endif %}
406
+ {#- Build cleaned message store. Messages WITHOUT inline signals pass -#}
407
+ {#- through untouched, so multi-modal parts (images/videos) survive. -#}
408
+ {%- if _tagged and _role == 'assistant' %}
409
+ {%- set _clean = strip_mode_tag(_txt) | trim %}
410
+ {%- set store.messages = store.messages + [{'role': _role, 'content': _clean, 'tool_calls': (msg.tool_calls if msg.tool_calls is defined else none), 'reasoning_content': (msg.reasoning_content if msg.reasoning_content is defined else none), 'thinking': (msg.thinking if msg.thinking is defined else none), 'reasoning': (msg.reasoning if msg.reasoning is defined else none)}] %}
411
+ {%- elif _is_steering and _has_inline %}
412
+ {#- Multi-modal aware strip: clean text items, keep image/video parts. -#}
413
+ {%- if msg.content is string %}
414
+ {%- set _clean = strip_control_tags(_txt) | trim %}
415
+ {%- set store.messages = store.messages + [{'role': _role, 'content': _clean}] %}
416
+ {%- elif msg.content is iterable and msg.content is not mapping %}
417
+ {%- set _nc = namespace(items=[]) %}
418
+ {%- for item in msg.content %}
419
+ {%- if item is mapping and 'text' in item and item.text is string %}
420
+ {%- set _nc.items = _nc.items + [{'type': 'text', 'text': strip_control_tags(item.text) | trim}] %}
421
+ {%- else %}
422
+ {%- set _nc.items = _nc.items + [item] %}
423
+ {%- endif %}
424
+ {%- endfor %}
425
+ {%- set store.messages = store.messages + [{'role': _role, 'content': _nc.items}] %}
426
+ {%- else %}
427
+ {%- set store.messages = store.messages + [msg] %}
428
+ {%- endif %}
429
+ {%- else %}
430
+ {%- set store.messages = store.messages + [msg] %}
431
+ {%- endif %}
432
+ {%- endfor %}
433
+ {%- set messages = store.messages %}
434
+ {%- set _depth_explicit = (_depth_explicit or store.saw_inline) %}
435
+
436
+ {#- Continuous depth: pure integers 10-100 map onto the spectrum band. -#}
437
+ {#- 0-9 stay legacy level numbers so old clients keep exact behaviour. -#}
438
+ {#- NB: int-filter round-trip instead of .isdigit() — minja (llama.cpp) -#}
439
+ {#- does not implement string.isdigit(). -#}
440
+ {%- set _eff_int = ns_state.effort | int %}
441
+ {%- if ns_state.mode == 'level' and ns_state.effort is string and (_eff_int | string) == ns_state.effort and _eff_int >= 10 %}
442
+ {%- set _n = ns_state.effort | int %}
443
+ {%- set ns_state.thinking = true %}
444
+ {%- if _n <= 14 %}
445
+ {%- set ns_state.effort = '1' %}
446
+ {%- elif _n <= 29 %}
447
+ {%- set ns_state.effort = '2' %}
448
+ {%- elif _n <= 44 %}
449
+ {%- set ns_state.effort = '3' %}
450
+ {%- elif _n <= 59 %}
451
+ {%- set ns_state.effort = '4' %}
452
+ {%- elif _n <= 69 %}
453
+ {%- set ns_state.effort = '5' %}
454
+ {%- elif _n <= 79 %}
455
+ {%- set ns_state.effort = '6' %}
456
+ {%- elif _n <= 89 %}
457
+ {%- set ns_state.effort = '7' %}
458
+ {%- elif _n <= 96 %}
459
+ {%- set ns_state.effort = '8' %}
460
+ {%- else %}
461
+ {%- set ns_state.effort = '9' %}
462
+ {%- endif %}
463
+ {%- endif %}
464
 
465
+ {#- Depth-tool coupling, part A: trailing tool-failure streak pre-pass. -#}
466
+ {%- set _streak = namespace(value=0) %}
467
+ {%- for msg in messages %}
468
+ {%- if msg.role == 'tool' %}
469
+ {%- set _tc = scan_text(msg.content) | trim %}
470
+ {%- if classify_tool_output(_tc) | trim == 'error' %}
471
+ {%- set _streak.value = _streak.value + 1 %}
472
+ {%- else %}
473
+ {%- set _streak.value = 0 %}
474
+ {%- endif %}
475
+ {%- elif msg.role == 'user' %}
476
+ {%- set _streak.value = 0 %}
477
+ {%- endif %}
478
+ {%- endfor %}
479
+
480
+ {#- Depth-tool coupling, part B: state adjustment BEFORE rendering. -#}
481
+ {#- Collapse: tools attached, no explicit depth signal → [L4]. -#}
482
+ {#- Escalate: 2+ consecutive tool failures → +1 band, capped at [L7]. -#}
483
+ {#- Swarm/council profiles are never auto-tuned (non-numeric effort). -#}
484
+ {%- if _has_tools and not _depth_explicit and ns_state.thinking %}
485
+ {%- set ns_state.effort = '4' %}
486
+ {%- endif %}
487
+ {%- if _streak.value >= 2 and ns_state.thinking %}
488
+ {%- set _cur = ns_state.effort if ns_state.effort else '7' %}
489
+ {%- if _cur == '1' %}
490
+ {%- set ns_state.effort = '2' %}
491
+ {%- elif _cur == '2' %}
492
+ {%- set ns_state.effort = '3' %}
493
+ {%- elif _cur == '3' %}
494
+ {%- set ns_state.effort = '4' %}
495
+ {%- elif _cur == '4' %}
496
+ {%- set ns_state.effort = '5' %}
497
+ {%- elif _cur == '5' %}
498
+ {%- set ns_state.effort = '6' %}
499
+ {%- elif _cur == '6' %}
500
+ {%- set ns_state.effort = '7' %}
501
+ {%- elif _cur in ('7', '8', '9') %}
502
+ {%- set ns_state.effort = '7' %}
503
+ {%- endif %}
504
+ {%- endif %}
505
+
506
+ {#- Alias resolver: number / name / compat alias → profile. Runs AFTER -#}
507
+ {#- coupling, so the rendered profile always matches the final effort. -#}
508
+ {#- Unknown input degrades gracefully to [L7]; it never hard-fails. -#}
509
+ {#- Silent modes (i-prefix) still inject the depth profile: the model -#}
510
+ {#- reasons at that quality internally while thinking tags stay closed. -#}
511
+ {%- set reasoning_instructions = '' %}
512
+ {%- if ns_state.thinking or ns_state.silent_profile %}
513
+ {%- set target_mode = ns_state.effort if ns_state.effort else '7' %}
514
+ {%- if target_mode in ('1', 'hermes', 'ulow', 'ultra-low', 'micro', 'minimal') %}
515
+ {%- set reasoning_instructions = inst_hermes %}
516
+ {%- elif target_mode in ('2', 'apollo', 'low') %}
517
+ {%- set reasoning_instructions = inst_apollo %}
518
+ {%- elif target_mode in ('3', 'artemis', 'lmed', 'low-medium') %}
519
+ {%- set reasoning_instructions = inst_artemis %}
520
+ {%- elif target_mode in ('4', 'athena', 'medium', 'med') %}
521
+ {%- set reasoning_instructions = inst_athena %}
522
+ {%- elif target_mode in ('5', 'prometheus', 'mhigh', 'medium-high') %}
523
+ {%- set reasoning_instructions = inst_prometheus %}
524
+ {%- elif target_mode in ('6', 'solstice', 'high') %}
525
+ {%- set reasoning_instructions = inst_solstice %}
526
+ {%- elif target_mode in ('7', 'hyperion', 'xhigh', 'default', 'extreme') %}
527
+ {%- set reasoning_instructions = inst_hyperion %}
528
+ {%- elif target_mode in ('8', 'einstein', 'uhigh', 'ultra-high') %}
529
+ {%- set reasoning_instructions = inst_einstein %}
530
+ {%- elif target_mode in ('9', 'oracle', 'amax', 'max', 'absolute-max') %}
531
+ {%- set reasoning_instructions = inst_oracle %}
532
+ {%- elif target_mode in ('swarm', 'einstein-swarm', '20-swarm', 'uhigh-swarm', 'divergent') %}
533
+ {%- set reasoning_instructions = inst_swarm %}
534
+ {%- elif target_mode in ('deep-swarm', 'oracle-swarm', 'council', 'amax-swarm', 'deep-research', 'spoon', 'adversarial') %}
535
+ {%- set reasoning_instructions = inst_deep_swarm %}
536
+ {%- else %}
537
+ {%- set reasoning_instructions = inst_hyperion %}
538
+ {%- endif %}
539
+ {%- endif %}
540
+
541
+ {#- ================================================================== -#}
542
+ {#- S3 RENDER PIPELINE — ChatML emission. -#}
543
+ {#- ================================================================== -#}
544
+
545
+ {#- Leading system-message grouping. -#}
546
+ {%- set head = namespace(count=0, seen_non_system=false) %}
547
+ {%- for message in messages %}
548
+ {%- set _is_sys = (message.role == 'system' or message.role == 'developer') %}
549
+ {%- if _is_sys and not head.seen_non_system %}
550
+ {%- set head.count = head.count + 1 %}
551
+ {%- else %}
552
+ {%- set head.seen_non_system = true %}
553
+ {%- endif %}
554
+ {%- endfor %}
555
+ {%- set sys_state = namespace(content='') %}
556
+ {%- for message in messages[:head.count] %}
557
+ {%- set _part = render_content(message.content, false) | trim %}
558
+ {%- if _part %}
559
+ {%- if sys_state.content %}
560
+ {%- set sys_state.content = sys_state.content ~ '\n\n' ~ _part %}
561
+ {%- else %}
562
+ {%- set sys_state.content = _part %}
563
+ {%- endif %}
564
+ {%- endif %}
565
+ {%- endfor %}
566
+ {%- set _sc = sys_state.content %}
567
+ {%- set _msgs = messages[head.count:] %}
568
+
569
+ {%- set _terse_active = terse if terse is defined else (not is_explicit_regular) %}
570
+ {%- if _terse_active %}
571
+ {%- if not _sc %}
572
+ {%- set _sc = terse_core | trim %}
573
+ {%- else %}
574
+ {%- set _sc = (_sc | trim) ~ '\n\n' ~ (terse_core | trim) %}
575
+ {%- endif %}
576
+ {%- endif %}
577
+
578
+ {#- Some runtimes prepend their own tool protocol; stand down if seen. -#}
579
+ {%- set _runtime_tool_protocol = (_sc is defined and _sc and '[TOOL_REQUEST]' in _sc) %}
580
+ {%- if suppress_tool_instructions is not none %}
581
+ {%- set _suppress_tools = suppress_tool_instructions %}
582
+ {%- else %}
583
+ {%- set _suppress_tools = _runtime_tool_protocol %}
584
+ {%- endif %}
585
+
586
+ {#- System emission: profile + soft contract + tools. -#}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
587
  {{- bos_token -}}
588
+ {%- if reasoning_instructions or _sc or (_has_tools and not _suppress_tools) %}
589
+ {{- '[INST] ' }}
590
+ {%- if reasoning_instructions %}
591
+ {{- reasoning_instructions ~ '\n\n' ~ soft_budget_contract ~ '\n\n' }}
592
+ {%- endif %}
593
+ {%- if _has_tools and not _suppress_tools %}
594
+ {{- '# Tools\n\nYou have access to the following functions:\n\n<tools>' }}
595
+ {%- for tool in tools %}
596
+ {{- '\n' }}
597
+ {{- tool | tojson }}
598
+ {%- endfor %}
599
+ {{- '\n</tools>' }}
600
+ {%- if _tool_format == 'json' %}
601
+ {{- '\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n' }}
602
+ {%- if ns_state.thinking %}
603
+ {{- '<think>\nBrief explanation of tool call\n</think>\n' }}
604
+ {%- endif %}
605
+ {{- '<tool_call>\n{"name": "example_function_name", "arguments": {"example_parameter_1": "value_1", "example_parameter_2": "This is the value for the second parameter"}}\n</tool_call>\n\n<IMPORTANT>\nReminder:\n' }}
606
+ {{- '- Function calls MUST follow the specified format: a single JSON object with "name" and "arguments" keys inside <tool_call></tool_call> XML tags.\n- The <tool_call> tag MUST be at the very beginning of a new line.\n- To call multiple functions, output a separate, completely closed <tool_call></tool_call> block for EACH function. Do NOT nest <tool_call> blocks.\n- If you have all necessary data, provide your final answer directly to the user without any tool call.\n</IMPORTANT>' }}
607
+ {%- else %}
608
+ {{- '\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n' }}
609
+ {%- if ns_state.thinking %}
610
+ {{- '<think>\nBrief explanation of tool call\n</think>\n' }}
611
+ {%- endif %}
612
+ {{- '<tool_call>\n<function=example_function_name>\n<parameter=example_parameter_1>\nvalue_1\n</parameter>\n<parameter=example_parameter_2>\nThis is the value for the second parameter\n</parameter>\n</function>\n</tool_call>\n\n<IMPORTANT>\nReminder:\n' }}
613
+ {{- '- Function calls MUST follow the specified format: an inner <function=...></function> block must be nested within <tool_call></tool_call> XML tags.\n- The <tool_call> and <function> tags MUST be at the very beginning of a new line.\n- To call multiple functions, output a separate, completely closed <tool_call></tool_call> block for EACH function. Do NOT nest <tool_call> blocks.\n- If you have all necessary data, provide your final answer directly to the user without any tool call.\n</IMPORTANT>' }}
614
+ {%- endif %}
615
+ {%- if _sc %}
616
+ {{- '\n\n' + _sc }}
617
+ {%- endif %}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
618
  {{- ' [/INST]
619
+ ' }}
620
+ {%- else %}
621
+ {%- if _sc %}
622
+ {{- _sc + ' [/INST]
623
+ ' }}
624
+ {%- else %}
 
 
 
625
  {{- ' [/INST]
626
+ ' }}
627
+ {%- endif %}
628
+ {%- endif %}
629
+ {%- endif %}
630
+
631
+ {#- Last real user turn (for preserve_thinking tail logic). -#}
632
+ {%- set _last_idx = _msgs | length - 1 %}
633
+ {%- set ns = namespace(multi_step_tool=true, last_query_index=_last_idx) %}
634
+ {%- for message in _msgs[::-1] %}
635
+ {%- set index = (_msgs | length - 1) - loop.index0 %}
636
+ {%- if ns.multi_step_tool and message.role == 'user' %}
637
+ {%- set _rc = render_content(message.content, false) | trim %}
638
+ {%- if not (_rc.startswith('<tool_response>') and _rc.endswith('</tool_response>')) %}
639
+ {%- set ns.multi_step_tool = false %}
640
+ {%- set ns.last_query_index = index %}
641
+ {%- endif %}
642
+ {%- endif %}
643
+ {%- endfor %}
644
+ {%- if ns.multi_step_tool %}
645
+ {%- if _last_idx > 50 %}
646
+ {%- set ns.last_query_index = _last_idx %}
647
+ {%- else %}
648
+ {%- set ns.last_query_index = 0 %}
649
+ {%- endif %}
650
+ {%- endif %}
651
+
652
+ {#- Message render loop. -#}
653
+ {%- set ns2 = namespace(prev_role='', consecutive_failures=0) %}
654
+ {%- for message in _msgs %}
655
+ {%- set is_system = (message.role == "system" or message.role == "developer") %}
656
+ {%- set content = render_content(message.content, true, is_system) | trim %}
657
+ {%- if is_system %}
658
+ {{- '[INST] ' + content + ' [/INST]
659
+ ' }}
660
+ {%- elif message.role == 'user' %}
661
+ {%- set ns2.consecutive_failures = 0 %}
662
+ {{- '[INST] ' + strip_control_tags(content) + ' [/INST]
663
+ ' }}
664
+ {%- elif message.role == 'assistant' %}
665
+ {%- set reasoning_content = '' %}
666
+ {%- set _explicit_reasoning = '' %}
667
+ {%- if message.reasoning_content is defined and message.reasoning_content is not none %}
668
+ {%- if message.reasoning_content is string %}
669
+ {%- set _explicit_reasoning = message.reasoning_content %}
670
+ {%- else %}
671
+ {%- set _explicit_reasoning = message.reasoning_content | string %}
672
+ {%- endif %}
673
+ {%- elif message.thinking is defined and message.thinking is not none %}
674
+ {%- if message.thinking is string %}
675
+ {%- set _explicit_reasoning = message.thinking %}
676
+ {%- else %}
677
+ {%- set _explicit_reasoning = message.thinking | string %}
678
+ {%- endif %}
679
+ {%- elif message.reasoning is defined and message.reasoning is not none %}
680
+ {%- if message.reasoning is string %}
681
+ {%- set _explicit_reasoning = message.reasoning %}
682
+ {%- else %}
683
+ {%- set _explicit_reasoning = message.reasoning | string %}
684
+ {%- endif %}
685
+ {%- endif %}
686
+ {%- if _explicit_reasoning %}
687
+ {%- set _lead_end = '' %}
688
+ {%- if content.startswith('<think>') and '</think>' in content %}
689
+ {%- set _lead_end = '</think>' %}
690
+ {%- elif content.startswith('<thinking>') and '</thinking>' in content %}
691
+ {%- set _lead_end = '</thinking>' %}
692
+ {%- elif content.startswith('</think>') %}
693
+ {%- set _lead_end = '</think>' %}
694
+ {%- elif content.startswith('</thinking>') %}
695
+ {%- set _lead_end = '</thinking>' %}
696
+ {%- endif %}
697
+ {%- if _lead_end %}
698
+ {%- set content = content.split(_lead_end)[-1].lstrip('\n') %}
699
+ {%- endif %}
700
+ {%- set reasoning_content = _explicit_reasoning %}
701
+ {%- else %}
702
+ {%- set _think_end = '' %}
703
+ {%- if content.startswith('</think>') %}
704
+ {%- set _think_end = '</think>' %}
705
+ {%- elif content.startswith('</thinking>') %}
706
+ {%- set _think_end = '</thinking>' %}
707
+ {%- elif '\n</think>' in content %}
708
+ {%- set _think_end = '\n</think>' %}
709
+ {%- elif '\n</thinking>' in content %}
710
+ {%- set _think_end = '\n</thinking>' %}
711
+ {%- elif '\n</ think>' in content %}
712
+ {%- set _think_end = '\n</ think>' %}
713
+ {%- elif '\n</think >' in content %}
714
+ {%- set _think_end = '\n</think >' %}
715
+ {%- elif content.startswith('<think>') and '</think>' in content %}
716
+ {%- set _think_end = '</think>' %}
717
+ {%- elif content.startswith('<thinking>') and '</thinking>' in content %}
718
+ {%- set _think_end = '</thinking>' %}
719
+ {%- endif %}
720
+ {%- if _think_end %}
721
+ {%- if 'thinking' in _think_end %}
722
+ {%- set _think_start = '<thinking>' %}
723
+ {%- else %}
724
+ {%- set _think_start = '<think>' %}
725
+ {%- endif %}
726
+ {%- set reasoning_content = content.split(_think_end)[0].rstrip('\n') %}
727
+ {%- if _think_start in reasoning_content %}
728
+ {%- set reasoning_content = reasoning_content.split(_think_start)[-1].lstrip('\n') %}
729
+ {%- endif %}
730
+ {%- set content = content.split(_think_end)[-1].lstrip('\n') %}
731
+ {%- endif %}
732
+ {%- endif %}
733
+ {%- set reasoning_content = reasoning_content | trim %}
734
+ {%- if (_preserve_thinking or loop.index0 > ns.last_query_index) %}
735
+ {{- '<think>\n' + reasoning_content + '\n</think>\n\n' + content }}
736
+ {%- else %}
737
+ {{- '' + content }}
738
+ {%- endif %}
739
+ {%- if message.tool_calls is defined and message.tool_calls and message.tool_calls is iterable and message.tool_calls is not mapping %}
740
+ {%- for tool_call in message.tool_calls %}
741
+ {%- if tool_call.function is defined and tool_call.function is not none %}
742
+ {%- set tc = tool_call.function %}
743
+ {%- else %}
744
+ {%- set tc = tool_call %}
745
+ {%- endif %}
746
+ {%- set tc_name = tc.name if (tc.name is defined and tc.name is not none) else '' %}
747
+ {%- if _tool_format == 'json' %}
748
+ {%- if loop.first %}
749
+ {%- if content | trim %}
750
+ {{- '\n\n' }}
751
+ {%- endif %}
752
+ {%- else %}
753
+ {{- '\n' }}
754
+ {%- endif %}
755
+ {%- set _args = '{}' %}
756
+ {%- if tc.arguments is defined and tc.arguments is not none %}
757
+ {%- if tc.arguments is mapping %}
758
+ {%- set _args = tc.arguments | tojson %}
759
+ {%- elif tc.arguments is string %}
760
+ {%- if tc.arguments %}
761
+ {%- set _args = tc.arguments %}
762
+ {%- endif %}
763
+ {%- else %}
764
+ {%- set _args = tc.arguments | tojson %}
765
+ {%- endif %}
766
+ {%- endif %}
767
+ {{- '<tool_call>\n{"name": ' }}{{- tc_name | tojson }}{{- ', "arguments": ' }}{{- _args }}{{- '}\n</tool_call>' }}
768
+ {%- else %}
769
+ {%- if loop.first %}
770
+ {%- if content | trim %}
771
+ {{- '\n\n<tool_call>\n<function=' + tc_name + '>\n' }}
772
+ {%- else %}
773
+ {{- '<tool_call>\n<function=' + tc_name + '>\n' }}
774
+ {%- endif %}
775
+ {%- else %}
776
+ {{- '\n<tool_call>\n<function=' + tc_name + '>\n' }}
777
+ {%- endif %}
778
+ {%- if tc.arguments is defined and tc.arguments is not none %}
779
+ {%- if tc.arguments is mapping %}
780
+ {%- for args_name, args_value in tc.arguments.items() %}
781
+ {{- '<parameter=' + args_name + '>\n' }}
782
+ {%- if args_value is string %}
783
+ {%- set _av = args_value %}
784
+ {%- else %}
785
+ {%- set _av = args_value | tojson %}
786
+ {%- endif %}
787
+ {%- if max_tool_arg_chars > 0 and _av | length > max_tool_arg_chars %}
788
+ {{- _av[:max_tool_arg_chars] + '\n[TRUNCATED - original length ' ~ (_av | length | string) ~ ' chars]' }}
789
+ {%- else %}
790
+ {{- _av }}
791
+ {%- endif %}
792
+ {{- '\n</parameter>\n' }}
793
+ {%- endfor %}
794
+ {%- else %}
795
+ {%- if tc.arguments is string %}
796
+ {%- set _raw_args = tc.arguments %}
797
+ {%- else %}
798
+ {%- set _raw_args = tc.arguments | tojson %}
799
+ {%- endif %}
800
+ {%- if _raw_args %}
801
+ {%- if max_tool_arg_chars > 0 and _raw_args | length > max_tool_arg_chars %}
802
+ {{- _raw_args[:max_tool_arg_chars] + '\n[TRUNCATED - original length ' ~ (_raw_args | length | string) ~ ' chars]' }}
803
+ {%- else %}
804
+ {{- _raw_args }}
805
+ {%- endif %}
806
+ {%- endif %}
807
+ {%- endif %}
808
+ {%- endif %}
809
+ {{- '</function>\n</tool_call>' }}
810
+ {%- endif %}
811
+ {%- endfor %}
812
+ {%- endif %}
813
+ {{- ' [/INST]
814
+ ' }}
815
+ {%- elif message.role == 'tool' %}
816
+ {%- if classify_tool_output(content) | trim == 'error' %}
817
+ {%- set ns2.consecutive_failures = ns2.consecutive_failures + 1 %}
818
+ {%- else %}
819
+ {%- set ns2.consecutive_failures = 0 %}
820
+ {%- endif %}
821
+ {%- if ns2.prev_role != 'tool' %}
822
+ {{- '<|im_start|>user' }}
823
+ {%- endif %}
824
+ {%- set _is_json_payload = (_tool_format == 'json' and content | trim | length > 0 and (content | trim)[:1] in ('{', '[')) %}
825
+ {%- if not _is_json_payload and max_tool_response_chars > 0 and content | length > max_tool_response_chars %}
826
+ {%- set content = content[:max_tool_response_chars] + '\n[TRUNCATED - original length ' ~ (content | length | string) ~ ' chars]' %}
827
+ {%- endif %}
828
+ {{- '\n<tool_response>\n' + content }}
829
+ {%- if ns2.consecutive_failures >= 2 %}
830
+ {{- '\n\n⚠️ SYSTEM WARNING: ' ~ ns2.consecutive_failures ~ ' consecutive tool errors detected. Your previous approach is incorrect. You MUST use a fundamentally different approach or corrected arguments.' }}
831
+ {%- elif ns2.consecutive_failures == 1 %}
832
+ {{- '\n\n⚠️ SYSTEM WARNING: The previous tool call returned an error. Diagnose the failure and retry with completely corrected arguments.' }}
833
+ {%- endif %}
834
+ {{- '\n</tool_response>' }}
835
+ {%- if loop.last %}
836
  {{- ' [/INST]
837
+ ' }}
838
+ {%- else %}
839
+ {%- set _next_role = _msgs[loop.index0 + 1].role %}
840
+ {%- if _next_role != 'tool' %}
841
+ {{- ' [/INST]
842
+ ' }}
843
+ {%- endif %}
844
+ {%- endif %}
845
+ {%- else %}
846
+ {{- '[INST] [' + message.role + ']: ' + content + ' [/INST]
847
+ ' }}
848
+ {%- endif %}
849
+ {%- set ns2.prev_role = message.role %}
850
+ {%- endfor %}
851
+
852
+ {#- Generation prompt: open thinking when active (variable depth, the model -#}
853
+ {#- decides how far to run), or pre-close it when disabled. -#}
854
+ {%- if add_generation_prompt %}
855
+ {{- '' }}
856
+ {%- if is_explicit_regular %}
857
+ {#- Pure regular mode: no think block -#}
858
+ {%- elif not ns_state.thinking %}
859
+ {{- '<think>\n\n</think>\n\n' }}
860
+ {%- else %}
861
+ {{- '<think>\n' }}
862
+ {%- endif %}
863
+ {%- endif %}