{% from "_macros.html" import icon, mark, model_avatar %} {% set speaking_model = (models_by_id | default({})).get(message.model_id) %} {# One message bubble, in either of two states. An incomplete assistant message renders the streaming shell: it carries the sse-connect that opens the reply stream. This is deliberately the ONLY thing that starts a generation, which means a page load showing an unfinished reply picks it up again -- reloading after a dropped connection retries rather than leaving a permanently half-written answer. A complete message renders its finished body: Markdown for the assistant, escaped plain text for everyone else. #} {% set streaming = (message.role == "assistant" and not message.complete) %} {# Typed while the previous reply was still being written, and not yet handed to a model. It is in the transcript and it is not in the request. It must never carry `sse-connect` -- a queued turn with a streaming shell on it would be the second concurrent reply the queue exists to prevent. #} {% set queued = (message.role == "user" and message.queued) %}
{% if message.role == "assistant" %} {{ speaking_model.label if speaking_model else "LLeMbas" }} {% else %} {{ user.name or "You" }} {% endif %} {% if message.model_id and (not speaking_model or speaking_model.display_name) %} {# Only worth showing when it adds something the author line does not. #} {{ message.model_id }} {% endif %}
{% if message.attachments %} {# Above the text, matching the order they were added and the order the model receives them. #}
{% for attachment in message.attachments %} {% if attachment.is_image %} {{ attachment.filename }} {% else %}
{{ icon("attach", "icon--sm") }} {{ attachment.filename }} {{ attachment.human_size }} {%- if attachment.pages %} · {{ attachment.pages }} page{{ '' if attachment.pages == 1 else 's' }}{% endif %} {%- if attachment.truncated %} · truncated{% endif %} {%- if attachment.extracted_text %} · view extracted text {%- endif %} {% if attachment.extraction_error %} {{ attachment.extraction_error }} {% endif %}
{% endif %} {% endfor %}
{% endif %} {% if streaming %} {# The reply as a sequence of steps, in the order they happened. The closed ones are re-sent only when a round ends; the two live containers come with them, inside `_steps.html`, which is how the tail blanks itself. See services/steps.py. #}
{% with steps = [], live = true %} {% include "chat/_steps.html" %} {% endwith %}
{# Where a question from the model, or a command waiting to be allowed, lands. Unlike the blocks above it this frame is sent on every version bump including when it is empty, because the card has to disappear the moment it is answered. The buttons inside are hx-post and they work: the SSE extension processes what it swaps in. #}
{# No stop button here: the composer's send button becomes Stop while a reply is being written, which is where the hand already is. #}
{# What the reply is doing when it is not producing tokens. A silent multi-second pause before the first token is what a hang looks like. #}
{# Counts as the reply is written. Everything is an estimate until the usage chunk lands at the very end, and the chips say so. #}
{% else %} {# Finished, and rendered from the same partial the live view uses so the bubble cannot rearrange itself the moment the stream ends. `message_steps` is a Jinja global rather than something each handler passes, for the reason `tool_label` is: this template is rendered from four different places and a fifth thing to remember is a fifth thing one of them forgets. A reply written before the marks existed has none, and `steps.py` answers that with the old layout exactly -- thinking, then every tool block, then the whole answer. Kept with the message rather than discarded with the stream, so the sources behind an answer are still there tomorrow. #} {% if message.role == "assistant" %} {# The same id and the same marker as the live container. That is what carries an opened block across the `done` frame, which replaces the whole bubble: steps.js keys what it remembers on this id, and the ids inside come from the mark index either way. #}
{% with steps = message_steps(message), live = false %} {% include "chat/_steps.html" %} {% endwith %}
{% endif %} {% if message.error %} {% endif %} {% if message.role == "assistant" %} {% if message.stopped %}

{{ icon("x", "icon--sm") }} Stopped. This reply is cut short.

{% endif %} {% elif message.content %} {# `tokens` escapes and then marks up: @mentions read as references rather than as punctuation. It must stay `pre-wrap` -- the newlines are still carried by CSS, not by markup. #}
{{ message.content|tokens|safe }}
{% endif %} {# An attachment-only turn has no text; rendering the bubble anyway would leave an empty box under the file. #} {% endif %} {% if not streaming and message.plan_json %} {# What the model proposed in Plan mode, with the way to carry it out. #} {% include "chat/_plan.html" %} {% endif %} {% if not streaming and message.role == "assistant" and message.usage_json %} {# Above the buttons, not among them: the actions row is things you press. #}
{% with metrics = message.usage_json | metrics %} {% include "chat/_metrics.html" %} {% endwith %}
{% endif %} {% if queued %} {# Nothing has been sent to any model. Both buttons sit on the bubble they act on rather than in a toast somewhere, and Edit is deliberately absent: editing rewinds and then starts a reply, which on a row you can press while another reply is streaming is a second concurrent generation behind a pencil. Discard and retype is the honest affordance. #}
{{ icon("clock", "icon--sm") }} Waiting to be sent
{% elif not streaming %}
{% if message.role == "user" %} {% endif %} {% if message.role == "assistant" %} {% if can_listen | default(false) and message.content and not message.error %} {# Speech is synthesised on demand rather than stored: the voice can change under the reader between plays, and a reply can be regenerated at the same address. #} {# data-speak-auto is set only on the frame that ends a live stream, never on a page load: reopening a chat must not start reading its last reply out loud again. #} {% endif %} {% endif %}
{# The raw source, so the copy button yields Markdown rather than rendered text. A hidden div and not a