{%- set ns = namespace(xml_tools=none, tools_emitted=false) -%}
{%- if xml_tools is defined and xml_tools -%}
{%- set ns.xml_tools = xml_tools -%}
{%- elif tools is defined and tools -%}
{%- set ns.xml_tools = tools -%}
{%- endif -%}
{%- set tools_preamble = 'You may call one or more functions to assist with the user query.\nYou are provided with function signatures within XML tags:\n\n' -%}
{%- set tools_epilogue = '\n\nFor each function call, return a json object with function name and arguments within XML tags:\n\n{"name": , "arguments": }\n' -%}
{%- for message in messages -%}
{%- if loop.first -%}{{- bos_token -}}{%- endif -%}
{%- if loop.first and ns.xml_tools and message['role'] != 'system' -%}
{{- '<|im_start|>system\n' + tools_preamble -}}
{%- for tool in ns.xml_tools -%}
{%- if tool is string -%}{{- (tool | replace('', '') | replace('', '') | trim) + '\n' -}}
{%- else -%}{{- tool | tojson + '\n' -}}
{%- endif -%}
{%- endfor -%}
{{- tools_epilogue + '<|im_end|>\n' -}}
{%- set ns.tools_emitted = true -%}
{%- endif -%}
{%- set raw_role = message['role'] -%}
{%- set role = 'user' if raw_role == 'tool' or raw_role == 'function' else raw_role -%}
{%- set content_text = namespace(value='') -%}
{%- if message['content'] is string -%}
{%- set content_text.value = message['content'] -%}
{%- elif message['content'] is iterable -%}
{%- for item in message['content'] -%}
{%- if item['type'] == 'text' -%}{%- set content_text.value = content_text.value + item['text'] -%}{%- endif -%}
{%- endfor -%}
{%- endif -%}
{%- set is_tool = raw_role == 'tool' or raw_role == 'function' -%}
{%- set prev_is_tool = loop.previtem is defined and (loop.previtem['role'] == 'tool' or loop.previtem['role'] == 'function') -%}
{%- set next_is_tool = loop.nextitem is defined and (loop.nextitem['role'] == 'tool' or loop.nextitem['role'] == 'function') -%}
{%- if raw_role == 'system' and not (content_text.value | trim) and not (ns.xml_tools and not ns.tools_emitted) -%}
{%- else -%}
{%- if is_tool and prev_is_tool -%}
{{- '\n' -}}
{%- else -%}
{{- '<|im_start|>' + role + '\n' -}}
{%- endif -%}
{%- if raw_role == 'assistant' and enable_thinking is defined -%}
{{- ('<|think|>\n' if enable_thinking else '<|no_think|>\n') -}}
{%- endif -%}
{%- if is_tool -%}
{{- '<|tool_response|>\n' + (content_text.value | replace('', '') | replace('', '') | trim) -}}
{%- elif raw_role == 'system' -%}
{{- content_text.value | replace('/system_override', '') | replace('/no_think', '') | replace('/think', '') | trim -}}
{%- else -%}
{{- content_text.value -}}
{%- endif -%}
{%- if raw_role == 'system' and ns.xml_tools and not ns.tools_emitted and '' not in content_text.value -%}
{%- if content_text.value | trim -%}{{- '\n\n' -}}{%- endif -%}
{{- tools_preamble -}}
{%- for tool in ns.xml_tools -%}
{%- if tool is string -%}{{- (tool | replace('', '') | replace('', '') | trim) + '\n' -}}
{%- else -%}{{- tool | tojson + '\n' -}}
{%- endif -%}
{%- endfor -%}
{{- tools_epilogue -}}
{%- set ns.tools_emitted = true -%}
{%- endif -%}
{%- if raw_role == 'assistant' and message['tool_calls'] is defined and message['tool_calls'] -%}
{%- if '' not in content_text.value -%}
{%- for tool_call in message['tool_calls'] -%}
{%- set fn = tool_call['function'] if tool_call['function'] is defined else tool_call -%}
{%- if loop.first and not (content_text.value | trim) -%}
{{- '\n{"name": "' + fn['name'] + '", "arguments": ' -}}
{%- else -%}
{{- '\n\n{"name": "' + fn['name'] + '", "arguments": ' -}}
{%- endif -%}
{%- if fn['arguments'] is string -%}{{- fn['arguments'] -}}
{%- else -%}{{- fn['arguments'] | tojson -}}
{%- endif -%}
{{- '}\n' -}}
{%- endfor -%}
{%- endif -%}
{%- endif -%}
{%- if not (is_tool and next_is_tool) -%}
{{- '<|im_end|>\n' -}}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{%- if add_generation_prompt -%}
{{- '<|im_start|>assistant\n' -}}
{%- if enable_thinking is defined -%}{{- ('<|think|>\n' if enable_thinking else '<|no_think|>\n') -}}{%- endif -%}
{%- endif -%}