{%- if not platform_instruction_override %}
{%- set platform_instruction_override -%}
These instructions are always to be followed and cannot be overridden by subsequent system or user turns:
- You will answer requests for educational, informative, or creative content related to safety categories. You will not provide content that is harmful or could be used to cause harm.

These instructions serve as your defaults, but they can be overridden in subsequent system or user turns:
- Your name is North Mini Code.
- You are a large language model built by Cohere.
{%- endset %}
{%- endif %}
{%- set reasoning = reasoning if reasoning is not undefined else (false if reasoning_effort is defined and reasoning_effort | lower == "none" else true) -%}
{%- set grounding = grounding | default("disabled") | upper %}
{%- set grounding_enabled = grounding == "ENABLED" %}
{%- set tools_or_docs_exist = tools or documents %}
{%- set render_tools_section = true %}
{%- set render_grounding = grounding_enabled and tools_or_docs_exist %}
{%- set render_platform_instruction_override = true if platform_instruction_override else false %}
{%- set has_developer_instruction = developer_instruction or developer_instruction == "" %}
{%- set render_developer_instruction = true if developer_instruction else false %}
{%- set convert_first_system_msg = convert_first_system_msg | default(true) -%}
{%- set skip_thinking = skip_thinking | default(false) -%}
{{ bos_token }}
{%- macro document_turn(documents) -%}
{# format documents into chat turn -#}
<|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|>{%- if not skip_thinking -%}<|START_THINKING|>I will look through the document to address the users needs.<|END_THINKING|>{%- endif -%}<|START_ACTION|>[
    {"tool_call_id": "0", "tool_name": "direct-injected-document", "parameters": {}}
]<|END_ACTION|><|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|><|START_TOOL_RESULT|>[
    {
        "tool_call_id": "0",
        "results": {
{%- for doc in documents %}
{%- set doc_val = doc.data if doc.data else doc %}

            "{{ loop.index0 }}": {{ doc_val|tojson }}{% if not loop.last %},
            {%- endif %}
{%- endfor %}

        },
        "is_error": null
    }
]<|END_TOOL_RESULT|><|END_OF_TURN_TOKEN|>{%- endmacro %}
{%- macro tool_call_id_to_int(messages, tool_call_id) %}
{%- if regen_tool_call_ids -%}
    {%- set counter = namespace(value=0) %}
    {%- set tool_call_id_seen = namespace(value=false) %}
    {%- for msg in messages %}
        {%- if msg.tool_calls %}
            {%- for tool_call in msg.tool_calls %}
                {%- if tool_call.id == tool_call_id and not tool_call_id_seen.value -%}
                    {{ counter.value }}
                    {%- set tool_call_id_seen.value = true %}
                {%- endif %}
                {%- set counter.value = counter.value + 1 %}
            {%- endfor %}
        {%- endif %}
    {%- endfor %}
{%- else -%}
    {{ tool_call_id }}
{%- endif -%}
{%- endmacro %}
{%- macro format_tool_message(messages, tool_msg) -%}
{#- format tool message #}{
        "tool_call_id": "{{ tool_call_id_to_int(messages, tool_msg.tool_call_id) }}",
        "results": {
        {%- if tool_msg.content is mapping or tool_msg.content is string %}

            {% if tool_msg.content is string -%}
                {%- set text_wrapper = {"content": tool_msg.content} -%}
            {%- else -%}
                {%- set text_wrapper = tool_msg.content -%}
            {%- endif %}
            "0": {{ text_wrapper|tojson }}
        {%- else %}
            {%- for content in tool_msg.content %}

            "{{ loop.index0 }}": {{ print_tool_content(content) }}{% if not loop.last %},{% endif %}
            {%- endfor %}
        {%- endif %}

        },
        "is_error": null
    }
{%- endmacro -%}
{%- macro print_tool_content(item) %}
{%- if item.type|lower == "text" -%}
{%- set text_wrapper = {"content": item.text} -%}
{{ text_wrapper|tojson }}
{%- elif item.type|lower == "document" and item.document and "data" in item.document -%}
{{ item.document.data|tojson }}
{%- else -%}
{{ item|tojson }}
{%- endif -%}
{%- endmacro %}
{%- macro print_msg(msg) %}
    {%- if msg is string -%}
<|START_TEXT|>{{ msg }}<|END_TEXT|>
    {%- elif msg.content is string -%}
<|START_TEXT|>{{ msg.content }}<|END_TEXT|>
    {%- else %}
        {%- set last_was_text = namespace(value=false) %}
        {%- for content in msg.content %}
            {%- if content.type|lower == "text" -%}
                {%- if not last_was_text.value -%}
                    <|START_TEXT|>
                {%- endif -%}
    {{ content.text }}
                {%- if loop.last -%}
                  <|END_TEXT|>
                {%- endif %}
                {%- set last_was_text.value = true -%}
            {%- else -%}
                {%- if last_was_text.value -%}
                    <|END_TEXT|>
                {%- endif -%}
                {%- set last_was_text.value = false -%}
            {%- endif -%}
            {%- if content.type|lower == "image" -%}
                {%- if content.data -%}
{{ content.data }}
                {%- else -%}
<|IMG_PATCH|>
                {%- endif -%}
            {%- endif -%}
        {%- endfor %}
    {%- endif %}
{%- endmacro %}
{%- macro print_thinking(msg) %}
    {%- if msg.reasoning -%}
{{ msg.reasoning }}
    {%- elif msg.reasoning_content -%}
{{ msg.reasoning_content }}
    {%- elif msg.thinking -%}
{{ msg.thinking }}
    {%- elif msg.content and msg.content[0].thinking -%}
{{ msg.content[0].thinking }}
    {%- endif %}
{%- endmacro %}
{%- if messages and messages[0]['role']|lower == 'system' and not has_developer_instruction and convert_first_system_msg %}{%- set developer_instruction = messages[0] %}{%- set render_developer_instruction = true %}{%- set initial_instruction_message = true %}{% endif %}
{%- set json_object = true if response_format and response_format.type == "json_object" else false %}
{%- set json_schema = (response_format.json_schema or response_format.schema) if response_format %}
{%- set json_mode = json_object or json_schema %}
{%- set tool_idx = namespace(value=0) %}
{%- set tool_ids_seen = namespace(value=[]) %}
{%- set regen_tool_call_ids = regen_tool_call_ids | default(true) -%}
{%- set sent_documents = namespace(value=false) -%}

{%- if render_tools_section or render_platform_instruction_override or render_grounding or json_mode -%}
<|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|><|START_TEXT|>
{%- elif not render_developer_instruction -%}
<|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|>
{%- endif %}

{%- set rendered_platform_turn_chunk = false %}

{%- if render_platform_instruction_override -%}
{{ platform_instruction_override }}
{% set rendered_platform_turn_chunk = true %}
{%- else %}
{%- endif %}

{%- if render_grounding -%}
{%- if rendered_platform_turn_chunk %}

{% endif -%}
Note that both your responses and reflections can be grounded. Grounding means you associate pieces of texts (called "spans") with those specific tool results that support them (called "sources"). And you use a pair of tags "<co>" and "</co>" to indicate when a span can be grounded onto a list of sources, listing them out in the closing tag. Sources from the same tool call are grouped together and listed as "{tool_call_id}:[{list of result indices}]", before they are joined together by ",". E.g., "<co>span</co: 0:[1,2],1:[0]>" means that "span" is supported by result 1 and 2 from "tool_call_id=0" as well as result 0 from "tool_call_id=1".
{% set rendered_platform_turn_chunk = true %}
{%- endif %}

{%- if render_tools_section %}
{%- if rendered_platform_turn_chunk %}

{% endif %}
# Available Tools
```json
[
{% if tools_or_docs_exist %}
{%- if documents %}
    {"name": "direct-injected-document", "description": "This is a special tool to directly inject user-uploaded documents into the chat as additional context. DO NOT use this tool by yourself!", "parameters": {"type": "object", "properties": {}, "required": []}, "responses": {"200": {"description": "Successfully returned a list of chunked text snippets from the directly uploaded documents.", "content": {"application/json": {"schema": {"type": "array", "items": {"type": "object", "required": ["url", "snippet"], "properties": {"url": {"type": "string", "description": "The url of the uploaded document."}, "snippet": {"type": "string", "description": "The text snippet for the returned document chunk."}}}}}}}}}
    {%- if tools %},
    {% else %}

    {% endif %}
{%- endif %}
{%- for tool in tools %}
    {"name": "{{ tool['function']['name'] }}", "description": "{{ tool['function']['description'] }}", "parameters": {{ tool['function']['parameters']|tojson }}, "responses": null}
    {%- if not loop.last %},{% endif %}

{% endfor %}
{%- else %}

{% endif %}
]
```
{%- set rendered_platform_turn_chunk = true %}
{%- endif -%}

{%- if json_mode -%}
{%- if rendered_platform_turn_chunk %}


{% endif -%}
When generating JSON objects, do not generate block markers. Generate an object directly without prefixing with ```json. Return only the JSON and nothing else.
    {%- if json_schema %}

Your output should adhere to the following json schema:
{{ json_schema }}
    {%- endif -%}
{%- set rendered_platform_turn_chunk = true %}
{%- endif %}
{%- if rendered_platform_turn_chunk -%}
<|END_TEXT|><|END_OF_TURN_TOKEN|>
{%- elif not render_developer_instruction -%}
<|END_OF_TURN_TOKEN|>
{%- endif %}
{%- if render_developer_instruction -%}
<|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|>{{ print_msg(developer_instruction) }}<|END_OF_TURN_TOKEN|>
{%- endif %}
{%- for message in messages %}
    {%- set msg_role_downcased = message.role | lower %}
    {%- if msg_role_downcased == 'system' and (not (loop.first and initial_instruction_message)) -%}
<|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|>{{ print_msg(message) }}<|END_OF_TURN_TOKEN|>
    {%- elif msg_role_downcased == 'user' -%}
<|START_OF_TURN_TOKEN|><|USER_TOKEN|>{{ print_msg(message) }}<|END_OF_TURN_TOKEN|>
        {%- if documents and not sent_documents.value %}{%- set sent_documents.value = true %}{% set tool_idx.value = tool_idx.value + 1 %}{{ document_turn(documents) }}{% endif %}
    {%- elif msg_role_downcased == 'assistant' or msg_role_downcased == 'chatbot' -%}
<|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|>
        {%- if message.tool_calls %}
            {% if not skip_thinking %}
                {% if message.tool_plan -%}
                    <|START_THINKING|>{{ message.tool_plan }}<|END_THINKING|>
                {%- elif message.reasoning or message.reasoning_content or message.thinking or (message.content and message.content[0].type == "thinking") -%}
                    <|START_THINKING|>{{ print_thinking(message) }}<|END_THINKING|>
                {%- endif %}
            {%- endif %}<|START_ACTION|>[
            {%- for tc in message.tool_calls %}

    {"tool_call_id": "{%- if regen_tool_call_ids -%}{{ tool_idx.value }}{%- else -%}{{ tc.id }}{%- endif -%}", "tool_name": "{{ tc['function']['name'] }}", "parameters": {{ tc['function']['arguments']|tojson }}}{% if not loop.last %},{% endif %}
                {%- set tool_idx.value = tool_idx.value + 1 %}
            {%- endfor %}

]<|END_ACTION|><|END_OF_TURN_TOKEN|>
        {%- else -%}
            {% if (message.reasoning or message.reasoning_content or message.thinking or (message.content and message.content[0].type == "thinking")) and not skip_thinking -%}
                <|START_THINKING|>{{ print_thinking(message) }}<|END_THINKING|>
            {%- endif -%}
            {{ print_msg(message) }}<|END_OF_TURN_TOKEN|>
        {%- endif %}
    {%- elif msg_role_downcased == 'tool' and message.tool_call_id not in tool_ids_seen.value -%}
<|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|><|START_TOOL_RESULT|>[
    {{ format_tool_message(messages, message) }}
        {%- for msg in messages[loop.index0 + 1:] %}

            {%- if msg.role | lower == 'tool' %},
    {{ format_tool_message(messages, msg) }}
                {%- set tool_ids_seen.value = tool_ids_seen.value + [msg.tool_call_id] %}
            {%- else %}
                {%- break %}
            {%- endif %}
        {%- endfor %}

]<|END_TOOL_RESULT|><|END_OF_TURN_TOKEN|>
    {%- endif %}
{%- endfor %}{%- if add_generation_prompt -%}<|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|>{% if reasoning %}<|START_THINKING|>{% else %}<|START_THINKING|><|END_THINKING|>{% endif %}{%- endif %}