{# EXTRA CONTEXT CONTENT #}
{% set rendered_context_text -%}
<|SECTION:CHARACTERS|>
{% for character in characters %}
### {{ character.name }}
{{ character.sheet }}

{{ character.description }}
{% endfor %}
<|CLOSE_SECTION|>
{% if scene.description %}
<|SECTION:SCENE DESCRIPTION|>
{{ scene.description }}
<|CLOSE_SECTION|>
{% endif -%}
{% include "extra-context.jinja2" %}
{% endset -%}
{# END EXTRA CONTEXT CONTENT #}

{# TASK #}
{% set task_main_text %}{% block task_main_text %}{% endblock %}{% endset -%}
{# END TASK #}

{# PREPARE SCENE HISTORY #}
{% set rendered_context_tokens = count_tokens(rendered_context_text) + count_tokens(task_main_text) -%}
{% set director_guidance = agent_context_state["director__actor_guidance"] -%}
{% if director_guidance %}{% set keep_director=False %}{% else %}{% set keep_director=talking_character.name %}{% endif -%}
{% set scene_history = scene.context_history(budget=max_tokens-200-rendered_context_tokens, min_dialogue=15, sections=False, keep_director=keep_director) -%}
{% if not director_guidance -%}
    {# INSERT ACTING INSTRUCTIONS VIA OFFSET #}
    {%- if actor_instructions_offset > 0 and talking_character.dialogue_instructions and scene.count_messages() > actor_instructions_offset -%}
        {%- set _ = scene_history.insert(-actor_instructions_offset, "(Broad character guidance for "+talking_character.name+": "+talking_character.dialogue_instructions+" "+actor_instructions+")") -%}
    {% endif -%}
    {% endif -%}
    {# END INSERT ACTING INSTRUCTIONS VIA OFFSET #}
{# END PREPARE SCENE HISTORY #}

{# RAG CONTEXT #}
{% set rag_context_text %}
{% with memory_prompt = scene_history %}{% include "memory-context.jinja2" %}{% endwith %}
{% endset -%}
{# END RAG CONTEXT #}

{# BUILD SCENE HISTORY #}
{% set scene_context_text %}
<|SECTION:SCENE|>
{% for scene_line in scene_history -%}
{{ scene_line }}

{% endfor %}
<|CLOSE_SECTION|>
{% endset -%}
{# END BUILD SCENE HISTORY #}

{# FINAL ACTING INSTRUCTIONS #}
{% set acting_instructions -%}
{% if scene.count_messages() < actor_instructions_offset or actor_instructions_offset == 0 %}
{% if not director_guidance -%}
{% if not talking_character.dialogue_instructions %}({% if actor_instructions %} {{ actor_instructions }}{% else %}Use an informal and colloquial register with a conversational tone. Overall, {{ talking_character.name }}'s dialog is informal, conversational, natural, and spontaneous, with a sense of immediacy.{% endif -%}){% else %}(Internal acting instructions for {{ talking_character.name }}: {{ talking_character.dialogue_instructions }}{% if actor_instructions %} {{ actor_instructions }}{% endif %}){% endif -%}
{% endif -%}
{% endif -%}

{% with direct=direct_instruction, character=talking_character %}{% include "regenerate-context.jinja2" %}{% endwith %}

{% if director_guidance %}(Instructions for {{ talking_character.name }}'s next part in the scene: {{ director_guidance }}){% endif %}
{% if direct_instruction -%}
(Instructions for {{ talking_character.name }}'s next part in the scene: {{ direct_instruction }})
{% endif -%}
{% endset %}
{# END FINAL ACTING INSTRUCTIONS #}

{# RENDER PROMPT #}
{% set scene_context_length = count_tokens(scene_context_text) %}
{% set large_context_threshold = 1024 %}
{{ rendered_context_text }}

{{ rag_context_text }}

{{ task_main_text }}

<|RESPONSE_LENGTH_INSTRUCTIONS|>

{% if scene_context_length < large_context_threshold %}{{ acting_instructions }}{% endif %}{# if scene context is relatively short, its beneficial to move the acting instructions into the task #}

{% with _text=scene_context_text %}{% include "internal-note-help.jinja2" %}{% endwith %}

{{ scene_context_text }}

{% if scene_context_length >= large_context_threshold %}{{ acting_instructions }}{% endif %}{# if scene context is relatively long, acting instrctions should be close to the end of the prompt so they get more importance attributed to them #}
{# END RENDER PROMPT #}

{% block response_scaffolding %}{% endblock -%}