{%- set bos = '<｜begin▁of▁sentence｜>' -%}
{%- set eos = '<｜end▁of▁sentence｜>' -%}
{%- set think_start = '<think>' -%}
{%- set think_end = '</think>' -%}
{%- set dsml = '｜DSML｜' -%}
{%- set tok_user = '<｜User｜>' -%}
{%- set tok_asst = '<｜Assistant｜>' -%}
{%- set tok_reminder = '<｜latest_reminder｜>' -%}
{%- set reasoning_effort = reasoning_effort | default(none) -%}
{#- Auto-enable thinking when reasoning_effort is 'high' or 'max' if enable_thinking is not explicitly set. -#}
{%- set _effort_implies_thinking = reasoning_effort == 'high' or reasoning_effort == 'max' -%}
{%- set thinking_mode = 'thinking' if (enable_thinking | default(_effort_implies_thinking)) else 'chat' -%}
{%- set REASONING_EFFORT_MAX -%}Reasoning Effort: Absolute maximum with no shortcuts permitted.
You MUST be very thorough in your thinking and comprehensively decompose the problem to resolve the root cause, rigorously stress-testing your logic against all potential paths, edge cases, and adversarial scenarios.
Explicitly write out your entire deliberation process, documenting every intermediate step, considered alternative, and rejected hypothesis to ensure absolutely no assumption is left unchecked.

{% endset -%}
{%- set TOOLS_PREFIX -%}## Tools

You have access to a set of tools to help answer the user's question. You can invoke tools by writing a "<｜DSML｜tool_calls>" block like the following:

<｜DSML｜tool_calls>
<｜DSML｜invoke name="$TOOL_NAME">
<｜DSML｜parameter name="$PARAMETER_NAME" string="true|false">$PARAMETER_VALUE</｜DSML｜parameter>
...
</｜DSML｜invoke>
<｜DSML｜invoke name="$TOOL_NAME2">
...
</｜DSML｜invoke>
</｜DSML｜tool_calls>

String parameters should be specified as is and set `string="true"`. For all other types (numbers, booleans, arrays, objects), pass the value in JSON format and set `string="false"`.

If thinking_mode is enabled (triggered by <think>), you MUST output your complete reasoning inside <think>...</think> BEFORE any tool calls or final response.

Otherwise, output directly after </think> with tool calls or final response.

### Available Tool Schemas

{%- endset -%}
{%- set TOOLS_SUFFIX -%}

You MUST strictly follow the above defined tool name and parameter schemas to invoke tool calls.{%- endset -%}
{%- macro render_tools(tools) -%}
{{- TOOLS_PREFIX -}}{{- '\n\n' -}}
{%- for tool in tools -%}
{%- if tool is not none and tool.function is defined and tool.function -%}
{{- tool.function | tojson -}}{{- '\n' -}}
{%- endif -%}
{%- endfor -%}
{{- '\n\n' -}}{{- TOOLS_SUFFIX -}}{{- '\n' -}}
{%- endmacro -%}
{%- macro render_tool_call(tc) -%}
{%- set fn = tc.function -%}
{#- llama.cpp normalizes tool-call arguments to objects before template rendering. -#}
{%- set args = fn.arguments -%}
<{{ dsml }}invoke name="{{ fn.name }}">{{- '\n' -}}{%- for key, val in args.items() -%}
<{{ dsml }}parameter name="{{ key }}" string="{{ 'true' if val is string else 'false' }}">{{ val if val is string else (val | tojson) }}</{{ dsml }}parameter>{{- '\n' if not loop.last else '' -}}
{%- endfor %}
{{- '\n' -}}</{{ dsml }}invoke>{%- endmacro -%}
{%- macro render_tool_calls(tool_calls) -%}
{{- '\n\n' -}}<{{ dsml }}tool_calls>{{- '\n' -}}
{%- for tc in tool_calls -%}
{{- render_tool_call(tc) -}}{{- '\n' -}}
{%- endfor -%}
</{{ dsml }}tool_calls>{%- endmacro -%}
{%- macro render_content_value(content) -%}
{%- if content is not defined or content is none -%}
{%- elif content is string -%}
  {{- content -}}
{%- elif content is sequence -%}
  {%- for part in content -%}
    {%- if not loop.first -%}{{- '\n\n' -}}{%- endif -%}
    {%- if part.type is defined and part.type == 'text' -%}
      {{- part.text if (part.text is defined and part.text is not none) else '' -}}
    {%- elif part.text is defined -%}
      {{- part.text if part.text is not none else '' -}}
    {%- elif part.type is defined -%}
      {{- '[Unsupported ' ~ part.type ~ ']' -}}
    {%- else -%}
      {{- part | tojson -}}
    {%- endif -%}
  {%- endfor -%}
{%- else -%}
  {{- content | tojson -}}
{%- endif -%}
{%- endmacro -%}
{%- set top_level_tools = tools if (tools is defined and tools) else none -%}
{%- set ns = namespace(last_user_idx=-1, any_tools=false, has_system_or_developer=false, injected_top_level_tools=false) -%}
{%- if top_level_tools -%}
  {%- set ns.any_tools = true -%}
{%- endif -%}
{%- for msg in messages -%}
  {%- if msg.role in ['user', 'developer', 'tool'] -%}
    {%- set ns.last_user_idx = loop.index0 -%}
  {%- endif -%}
  {%- if msg.role in ['system', 'developer'] -%}
    {%- set ns.has_system_or_developer = true -%}
  {%- endif -%}
  {%- if msg.tools is defined and msg.tools -%}
    {%- set ns.any_tools = true -%}
  {%- endif -%}
{%- endfor -%}
{%- set effective_drop_thinking = false if ns.any_tools else true -%}
{{- bos -}}
{#- llama.cpp passes tools as a top-level variable. Some agent harnesses send no
    system/developer message, so inject tool instructions before the first chat
    turn in that case; otherwise they would never reach the model. -#}
{%- if top_level_tools and not ns.has_system_or_developer -%}
  {{- render_tools(top_level_tools) -}}
  {%- set ns.injected_top_level_tools = true -%}
{%- endif -%}
{%- if thinking_mode == 'thinking' and reasoning_effort == 'max' -%}
{{- REASONING_EFFORT_MAX -}}
{%- endif -%}
{%- for msg in messages -%}
  {%- set idx = loop.index0 -%}
  {%- set next_role = messages[idx + 1].role if idx + 1 < (messages | length) else none -%}
  {%- set prev_role = messages[idx - 1].role if idx - 1 >= 0 else none -%}
  {%- set skip_message = effective_drop_thinking and msg.role == 'developer' and idx < ns.last_user_idx -%}
  {%- if skip_message -%}
  {%- elif msg.role == 'system' -%}
    {{- render_content_value(msg.content) -}}
    {#- Tools may be on the message (HF Python path) or at the top level (llama.cpp path) -#}
    {%- set _msg_tools = msg.tools if (msg.tools is defined and msg.tools) else none -%}
    {%- if _msg_tools -%}
      {{- '\n\n' -}}{{- render_tools(_msg_tools) -}}
    {%- elif top_level_tools and not ns.injected_top_level_tools -%}
      {{- '\n\n' -}}{{- render_tools(top_level_tools) -}}
      {%- set ns.injected_top_level_tools = true -%}
    {%- endif -%}
    {%- if msg.response_format is defined and msg.response_format -%}
      {{- '\n\n## Response Format:\n\nYou MUST strictly adhere to the following schema to reply:\n' -}}{{- msg.response_format | tojson -}}
    {%- endif -%}
  {%- elif msg.role == 'developer' -%}
    {{- tok_user -}}{{- render_content_value(msg.content) -}}
    {%- set _msg_tools = msg.tools if (msg.tools is defined and msg.tools) else none -%}
    {%- if _msg_tools -%}
      {{- '\n\n' -}}{{- render_tools(_msg_tools) -}}
    {%- elif top_level_tools and not ns.injected_top_level_tools -%}
      {{- '\n\n' -}}{{- render_tools(top_level_tools) -}}
      {%- set ns.injected_top_level_tools = true -%}
    {%- endif -%}
    {%- if msg.response_format is defined and msg.response_format -%}
      {{- '\n\n## Response Format:\n\nYou MUST strictly adhere to the following schema to reply:\n' -}}{{- msg.response_format | tojson -}}
    {%- endif -%}
  {%- elif msg.role == 'user' -%}
    {%- if prev_role == 'tool' and not (msg.task is defined and msg.task) -%}
      {{- '\n\n' -}}
    {%- else -%}
      {{- tok_user -}}
    {%- endif -%}
    {%- if msg.content_blocks is defined and msg.content_blocks -%}
      {%- for block in msg.content_blocks -%}
        {%- if not loop.first -%}{{- '\n\n' -}}{%- endif -%}
        {%- if block.type == 'text' -%}
          {{- block.text if (block.text is defined and block.text is not none) else '' -}}
        {%- elif block.type == 'tool_result' -%}
          {{- '<tool_result>' -}}
          {%- if block.content is sequence and block.content is not string -%}
            {%- for b in block.content -%}
              {%- if not loop.first -%}{{- '\n\n' -}}{%- endif -%}
              {{- b.text if b.type == 'text' else '[Unsupported ' ~ b.type ~ ']' -}}
            {%- endfor -%}
          {%- else -%}
            {{- render_content_value(block.content) -}}
          {%- endif -%}
          {{- '</tool_result>' -}}
        {%- else -%}
          {{- '[Unsupported ' ~ block.type ~ ']' -}}
        {%- endif -%}
      {%- endfor -%}
    {%- else -%}
      {{- render_content_value(msg.content) -}}
    {%- endif -%}
  {%- elif msg.role == 'tool' -%}
    {%- if prev_role == 'tool' -%}
    {%- else -%}
      {{- tok_user -}}
      {%- set tr = namespace(first=true, emitted='|') -%}
      {%- set prev = messages[idx - 1] if idx - 1 >= 0 else none -%}
      {%- if prev and prev.role == 'assistant' and prev.tool_calls is defined and prev.tool_calls -%}
        {%- for tc in prev.tool_calls -%}
          {%- set tcid = tc.id if tc.id is defined and tc.id else (tc.function.id if tc.function is defined and tc.function.id is defined else '') -%}
          {%- if tcid -%}
            {%- set scan = namespace(active=true) -%}
            {%- for tmsg in messages[idx:] -%}
              {%- if scan.active and tmsg.role == 'tool' -%}
                {%- if (tmsg.tool_call_id | default('')) == tcid -%}
                  {%- if not tr.first -%}{{- '\n\n' -}}{%- endif -%}
                  {{- '<tool_result>' -}}{{- render_content_value(tmsg.content) -}}{{- '</tool_result>' -}}
                  {%- set tr.first = false -%}
                  {%- set tr.emitted = tr.emitted ~ tcid ~ '|' -%}
                {%- endif -%}
              {%- elif scan.active -%}
                {%- set scan.active = false -%}
              {%- endif -%}
            {%- endfor -%}
          {%- endif -%}
        {%- endfor -%}
      {%- endif -%}
      {%- set scan = namespace(active=true) -%}
      {%- for tmsg in messages[idx:] -%}
        {%- if scan.active and tmsg.role == 'tool' -%}
          {%- set tid = tmsg.tool_call_id | default('') -%}
          {%- if not tid or ('|' ~ tid ~ '|') not in tr.emitted -%}
            {%- if not tr.first -%}{{- '\n\n' -}}{%- endif -%}
            {{- '<tool_result>' -}}{{- render_content_value(tmsg.content) -}}{{- '</tool_result>' -}}
            {%- set tr.first = false -%}
          {%- endif -%}
        {%- elif scan.active -%}
          {%- set scan.active = false -%}
        {%- endif -%}
      {%- endfor -%}
    {%- endif -%}
  {%- elif msg.role == 'latest_reminder' -%}
    {{- tok_reminder -}}{{- render_content_value(msg.content) -}}
  {%- elif msg.role == 'assistant' -%}
    {%- set prev_has_task = idx - 1 >= 0 and messages[idx - 1].task is defined and messages[idx - 1].task -%}
    {%- if thinking_mode == 'thinking' and not prev_has_task -%}
      {%- if (not effective_drop_thinking) or idx > ns.last_user_idx -%}
        {{- msg.reasoning_content if (msg.reasoning_content is defined and msg.reasoning_content is not none) else '' -}}{{- think_end -}}
      {%- endif -%}
    {%- endif -%}
    {{- render_content_value(msg.content) -}}
    {%- if msg.tool_calls is defined and msg.tool_calls -%}
      {{- render_tool_calls(msg.tool_calls) -}}
    {%- endif -%}
    {%- if not (msg.wo_eos is defined and msg.wo_eos) -%}
      {{- eos -}}
    {%- endif -%}
  {%- endif -%}

  {%- if skip_message -%}
  {#- Suppress the trailing <Assistant><think>/<think> generation suffix on the
     last message when the caller did not request a generation prompt. This
     allows llama.cpp's differential autoparser to detect the suffix as the
     reasoning generation prompt, which is required for `</think>` parsing. -#}
  {%- elif idx + 1 == (messages | length) and not add_generation_prompt -%}
  {%- elif idx + 1 < (messages | length) and next_role not in ['assistant', 'latest_reminder'] -%}
  {%- else -%}
    {%- if msg.task is defined and msg.task -%}
      {%- if msg.task != 'action' -%}
        {{- '<｜' ~ msg.task ~ '｜>' -}}
      {%- else -%}
        {{- tok_asst -}}{{- think_start if thinking_mode == 'thinking' else think_end -}}{{- '<｜action｜>' -}}
      {%- endif -%}
    {%- elif msg.role in ['user', 'developer', 'tool'] -%}
      {{- tok_asst -}}
      {%- if thinking_mode == 'thinking' and ((not effective_drop_thinking) or idx >= ns.last_user_idx) -%}
        {{- think_start -}}
      {%- else -%}
        {{- think_end -}}
      {%- endif -%}
    {%- endif -%}
  {%- endif -%}
{%- endfor -%}