{%- macro render_content(content) -%}
{%- if content is string -%}
{{- content -}}
{%- else -%}
{%- for item in content -%}
{%- if item.type is defined and item.type == 'text' -%}
{{- item.text -}}
{%- elif item.type is defined and (item.type == 'image' or item.type == 'image_url') -%}
{{- '<|vision_start|><|image_pad|><|vision_end|>' -}}
{%- elif item.type is defined and (item.type == 'video' or item.type == 'video_url') -%}
{{- '<|vision_start|><|video_pad|><|vision_end|>' -}}
{%- elif item.text is defined -%}
{{- item.text -}}
{%- elif item.image is defined or item.image_url is defined -%}
{{- '<|vision_start|><|image_pad|><|vision_end|>' -}}
{%- elif item.video is defined -%}
{{- '<|vision_start|><|video_pad|><|vision_end|>' -}}
{%- endif -%}
{%- endfor -%}
{%- endif -%}
{%- endmacro -%}
{%- set DEFAULT_SYSTEM = 'Your name is Salience 1.5 Flash. You ARE this AI assistant; never describe Salience as an external company or third party. When asked who you are, answer in the first person (for example: "Sono Salience, un assistente AI per coding, debugging e design"). You are an elite software-engineering agent: you handle very large codebases with long context, write production-grade backend and frontend code, and debug methodically. For any UI, default to modern stacks (React/Next, TypeScript, Tailwind, shadcn/ui), real design tokens, accessible WCAG semantics, responsive layout. Reply in the same language as the user.' -%}
{%- set _has_sys = (messages | length > 0 and messages[0].role == 'system') -%}
{%- if tools %}
{{- '<|im_start|>system\n' }}
{%- if _has_sys %}
{{- render_content(messages[0].content) + '\n\n' }}
{%- else %}
{{- DEFAULT_SYSTEM + '\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<|im_end|>\n" }}
{%- else %}
{%- if _has_sys %}
{{- '<|im_start|>system\n' + render_content(messages[0].content) + '<|im_end|>\n' }}
{%- else %}
{{- '<|im_start|>system\n' + DEFAULT_SYSTEM + '<|im_end|>\n' }}
{%- endif %}
{%- endif %}
{%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}
{%- for forward_message in messages %}
{%- set index = (messages|length - 1) - loop.index0 %}
{%- set message = messages[index] %}
{%- set current_content = message.content if message.content is string else '' %}
{%- set tool_start = '' %}
{%- set tool_start_length = tool_start|length %}
{%- set start_of_message = current_content[:tool_start_length] %}
{%- set tool_end = '' %}
{%- set tool_end_length = tool_end|length %}
{%- set start_pos = (current_content|length) - tool_end_length %}
{%- if start_pos < 0 %}
{%- set start_pos = 0 %}
{%- endif %}
{%- set end_of_message = current_content[start_pos:] %}
{%- if ns.multi_step_tool and message.role == "user" and not(start_of_message == tool_start and end_of_message == tool_end) %}
{%- set ns.multi_step_tool = false %}
{%- set ns.last_query_index = index %}
{%- endif %}
{%- endfor %}
{%- for message in messages %}
{%- if message.role == "user" %}
{{- '<|im_start|>user\n' + render_content(message.content) + '<|im_end|>\n' }}
{%- elif message.role == "system" and not loop.first %}
{{- '<|im_start|>system\n' + render_content(message.content) + '<|im_end|>\n' }}
{%- elif message.role == "assistant" %}
{%- set content = render_content(message.content) %}
{%- set reasoning_content = '' %}
{%- if message.reasoning_content is defined and message.reasoning_content is string %}
{%- set reasoning_content = message.reasoning_content %}
{%- elif '' in content %}
{%- set reasoning_content = content.split('')[0].rstrip('\n').split('')[-1].lstrip('\n') %}
{%- set content = content.split('')[-1].lstrip('\n') %}
{%- endif %}
{%- if loop.index0 > ns.last_query_index %}
{%- if loop.last or (not loop.last and reasoning_content) %}
{{- '<|im_start|>assistant\n\n' + reasoning_content.strip('\n') + '\n\n\n' + content.lstrip('\n') }}
{%- else %}
{{- '<|im_start|>assistant\n' + content }}
{%- endif %}
{%- else %}
{{- '<|im_start|>assistant\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' + render_content(message.content) + '\n' }}
{%- if loop.last or (messages[loop.index0 + 1].role != "tool") %}
{{- '<|im_end|>\n' }}
{%- endif %}
{%- endif %}
{%- endfor %}
{%- if add_generation_prompt %}
{{- '<|im_start|>assistant\n' }}
{%- endif %}