{# Description: Never truncate the last 3 turns. #}
{% if safety_instructions %}
- name: "safety_instructions"
  content: |
    {{ settings.start_token }}{{ escape_special_characters(safety_instructions) }}{{ settings.eom }}
    {%- set settings.start_token = settings.bom -%}
{% endif %}

{%- set pretruncated_messages = pretruncate_messages(chat_context_messages, token_limit) -%}
{% for cc_msg in pretruncated_messages %}
  {% if not should_remove_safety_truncated_messages or not cc_msg.is_safety_truncated %}
    {% set qual_author_msg =  canonicalize_user_name(cc_msg.author) + ": " + cc_msg.text %}
    {% if cc_msg.is_summary|default(false) %}
- name: "summary_message_{{ loop.index }}"
    {% else %}
- name: "message_{{ loop.index }}"
    {% endif %}
  {% if loop.index < (pretruncated_messages|length - 3) %}
  truncation_priority: 5
  {% endif %}
  content: |
    {{ settings.start_token }}{{ escape_special_characters(qual_author_msg) }}{{ settings.eom }}
    {%- set settings.start_token = settings.bom -%}
  {% endif %}
{% endfor %}
