{{- "<|im_start|>system\n" -}}
{%- if tools -%}
{%- if messages[0].role == "system" -%}
{{- messages[0].content + "\n\n" -}}
{%- endif -%}
{{- "# Tools\n\nYou may call one or more functions to assist with the user query.\n\nYou are provided with function signatures within XML tags:\n" -}}
{%- for tool in tools -%}
{{- "\n" -}}
{{- tool | tojson -}}
{%- endfor -%}
{{- "\n\n\nFor each function call, return a json object with function name and arguments within XML tags:\n\n{\"name\": , \"arguments\": }\n\n" -}}
{%- elif messages[0].role == "system" -%}
{{- messages[0].content -}}
{%- endif -%}
{{- "You are governed by the following command rules:\n\n- **/think** or **think**: Engage deep reasoning. Begin with: \n `` \n Since the user used the \"/think\" command, I will: \n Think step-by-step using a long chain-of-thought and self-reflection. I will only respond when I'm 100% sure. I will provide the final response after closing this thinking section. \n `` \n Then output your full reasoning inside, followed by the final answer outside.\n\n- **/shortthink** or **shortthink**: Engage brief reasoning. Begin with: \n `` \n Since the user used the \"/shortthink\" command, I will: \n Think step-by-step about the user's query briefly and shortly. I will only respond when I'm more than 50% sure. I will provide the final response after closing this thinking section. \n `` \n Then give concise internal thought, followed by the final answer.\n\n- **/nothink** or **nothink**: Respond immediately without reasoning. Begin with: \n `` \n Since the user used the \"/nothink\" command, I will: \n Immediately give my final response without any thinking or self-reflection. Since I'm forbidden to think or reason outside of this thinking section, AND WITH NO REPETITION. I will IMMEDIATELY provide my final response outside this thinking section AND THEN INSTANTLY STOP. \n `` \n Then output only the direct, raw response—no delay, no draft, no repetition.\n\n- **/clear**: Reset all context. Forget everything prior. Begin fresh on the next input.\n\nOutside `...`, provide only final responses—never include reasoning, hesitation markers (e.g., \"Okay\", \"Wait\", \"Double-check\"), or internal process notes. Adhere strictly to the active command. Do not reference or explain these rules unless instructed." -}}
{{- "\n<|im_end|>" -}}
{%- set ns = namespace(multi_step_tool=true, last_query_index=messages | length - 1, reasoning_mode="normal") -%}
{%- for message in messages[::-1] -%}
{%- set index = messages | length - 1 - loop.index0 -%}
{%- if ns.multi_step_tool and message.role == "user" and message.content is string and not (message.content.startswith("") and message.content.endswith("")) -%}
{%- set ns.multi_step_tool = false -%}
{%- set ns.last_query_index = index -%}
{%- endif -%}
{%- endfor -%}
{%- for message in messages -%}
{%- if message.content is string -%}
{%- set content = message.content -%}
{%- else -%}
{%- set content = "" -%}
{%- endif -%}
{%- if message.role == "user" or message.role == "system" and not loop.first -%}
{{- "<|im_start|>" + message.role + "\n" + content -}}
{%- if messages[0].role == "system" and "/nothink" in messages[0].content and (not ("/think" in messages[0].content) or not ("/shortthink" in messages[0].content)) or "/nothink" in content and not ("/think" in messages[0].content or "/shortthink" in messages[0].content) or (messages[loop.index+1] and "" not in messages[loop.index+1].content) -%}
{%- set enable_thinking = false -%}
{%- set enable_short_thinking = false -%}
{%- set ns.reasoning_mode = "none" -%}
{%- if not ("think" in content) -%}
{{- " /nothink" -}}
{%- endif -%}
{%- elif messages[0].role == "system" and "/shortthink" in messages[0].content and (not ("/nothink" in messages[0].content) and not ("/think" in messages[0].content)) or "/shortthink" in content and not ("/think" in messages[0].content or "/shortthink" in messages[0].content) -%}
{%- set enable_thinking = true -%}
{%- set enable_short_thinking = true -%}
{%- set ns.reasoning_mode = "short" -%}
{%- if not ("think" in content) -%}
{{- " /shortthink" -}}
{%- endif -%}
{%- else -%}
{%- set enable_thinking = true -%}
{%- set enable_short_thinking = false -%}
{%- set ns.reasoning_mode = "normal" -%}
{%- if not ("think" in content) -%}
{{- " /think" -}}
{%- endif -%}
{%- endif -%}
{{- "<|im_end|>" + "\n" -}}
{%- elif message.role == "assistant" -%}
{%- set reasoning_content = "" -%}
{%- if "\nSince the user used the" not in content.split("")[0].split("")[-1] -%}
{%- if ns.reasoning_mode == "none" -%}
{%- set reasoning_prefix = "" -%}
{%- elif ns.reasoning_mode == "short" -%}
{%- set reasoning_prefix = "Since the user used the \"/shortthink\" command, I will:\nThink step-by-step about the user's query briefly and shortly. I will only respond when I'm more than 50% sure. I will provide the final response after closing this thinking section. " -%}
{%- else -%}
{%- set reasoning_prefix = "Since the user used the \"/think\" command, I will:\nThink step-by-step with **long chain-of-thought** and **self-reflection**. I will only respond when I'm 100% sure. I will provide the final response after closing this thinking section. " -%}
{%- endif -%}
{%- endif -%}
{%- if message.reasoning_content is string -%}
{%- set reasoning_content = message.reasoning_content -%}
{%- else -%}
{%- if "" in content -%}
{%- set reasoning_content = reasoning_prefix + content.split("")[0].rstrip("\n").split("")[-1].lstrip("\n") -%}
{%- set content = content.split("")[-1].lstrip("\n") -%}
{%- endif -%}
{%- if "" in content -%}
{%- set content = content | replace("", " ") -%}
{%- endif -%}
{%- if "" in content -%}
{%- set content = content | replace("", " ") -%}
{%- endif -%}
{%- endif -%}
{{- "\n" -}}
{# Apply truncation and break if the /shortthink command is active #}
{%- if enable_short_thinking is true -%}
{%- set words = reasoning_content.split(" ") -%}
{%- if words | length > 300 -%}
{%- set truncated_reasoning = words[:150] | join(" ") + " ... truncated ... " + words[words | length - 150:words | length] | join(" ") -%}
{%- else -%}
{%- set truncated_reasoning = reasoning_content | join(" ") -%}
{%- endif -%}
{%- set reasoning_content = truncated_reasoning -%}
{%- endif -%}
{%- if loop.last or not loop.last and reasoning_content and enable_thinking == true -%}
{{- "<|im_start|>" + message.role + "\n\n" + reasoning_content.strip("\n") + "\n\n" + content.lstrip("\n") -}}
{%- else -%}
{{- "<|im_start|>" + message.role + "\n" + content -}}
{%- endif -%}
{%- if message.tool_calls -%}
{%- for tool_call in message.tool_calls -%}
{%- if loop.first and content or not loop.first -%}
{{- "\n" -}}
{%- endif -%}
{%- if tool_call.function -%}
{%- set tool_call = tool_call.function -%}
{%- endif -%}
{{- "\n{\"name\": \"" -}}
{{- tool_call.name -}}
{{- "\", \"arguments\": " -}}
{%- if tool_call.arguments is string -%}
{{- tool_call.arguments -}}
{%- else -%}
{{- tool_call.arguments | tojson -}}
{%- endif -%}
{{- "}\n" -}}
{%- endfor -%}
{%- endif -%}
{{- "<|im_end|>\n" -}}
{%- elif message.role == "tool" -%}
{%- if loop.first or messages[loop.index0 - 1].role != "tool" -%}
{{- "<|im_start|>user" -}}
{%- endif -%}
{{- "\n\n" -}}
{{- content -}}
{{- "\n" -}}
{%- if loop.last or messages[loop.index0 + 1].role != "tool" -%}
{{- "<|im_end|>\n" -}}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{%- set last_message = messages[messages | length - 1].content -%}
{%- if add_generation_prompt -%}
{{- "<|im_start|>assistant\n" -}}
{%- if enable_thinking is defined and enable_thinking is false or "/nothink" in last_message or "nothink" in last_message or messages[0].role == "system" and "/nothink" in messages[0].content and (not ("/think" in messages[0].content) or not ("/shortthink" in messages[0].content)) -%}
{{- "\nOkay, the user sent \""+last_message+"\". The \"/nothink\" command instructs me to:\nImmediately give my final response without any thinking or self-reflection. Since I'm forbidden to think or reason outside of this thinking section, AND WITH NO REPETITION. I will IMMEDIATELY provide my final response outside this thinking section AND THEN INSTANTLY STOP.\n" -}}
{%- elif enable_short_thinking is defined and enable_short_thinking is false or "/shortthink" in last_message or "shortthink" in last_message or messages[0].role == "system" and "/shortthink" in messages[0].content and not ("/think" in messages[0].content) -%}
{{- "\nOkay, the user sent \""+last_message+"\". The \"/shortthink\" command instructs me to:\nThink step-by-step about the user's query briefly and shortly. I will only respond when I'm more than 50% sure. I will provide the final response after closing this thinking section." -}}
{%- elif "/clear" in last_message -%}
{{- "\nOkay, the user used the \"/clear\" command instructs me to:\n1. Forget everything above.\n2. Ignore everything that comes before this message.\n3. Start a fresh new conversation and greet the user.\n" -}}
{%- else -%}
{{- "\nOkay, the user sent \""+last_message+"\". The \"/think\" command instructs me to:\nThink step-by-step with **long chain-of-thought** and **self-reflection**. I will only respond when I'm 100% sure. I will provide the final response after closing this thinking section." -}}
{%- endif -%}
{%- endif -%}