{% 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) %} {# Written by the application, in the user role, and not by the person whose bubble this would otherwise be -- a background job reporting that it finished. The wire role is deliberately unchanged (see Message.machine), so this is the only place in the whole request the difference exists. `msg--user` stays on the article as well, so the bubble keeps the layout it already had and `msg--machine` only overrides what should differ. #} {% set machine = (message.role == "user" and message.machine) %}
{% if message.role == "assistant" %} {{ speaking_model.label if speaking_model else brand.name }} {% elif machine %} Background job {% 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. Closed steps only, re-sent when a round ends. See services/steps.py. #}
{# The step still being written. SIBLINGS of the container above, never inside it: that one is swapped whole every time a round closes, and an `sse-swap` element nested in another is torn out and rebuilt at each boundary with the frames aimed at it arriving in the same pass. That is what made an agent reply render nothing from its first tool call on. Both frames are sent on every version bump *including empty*, which is how the tail clears when a round closes and its contents move into a step above. That is safe here and was not before: these carry the open tail only, so an empty one means the tail is genuinely empty, whereas the version that carried the whole reply would have wiped it. `steps` is the one that must never blank now. #} {# The word animates its own ellipsis in CSS -- `.` `..` `...` -- so there is no timer to start or clean up, and it stops when the element does. The numbers beside it come from the `think` frame. That span is a SIBLING of the body, inside a `
` that is never itself swapped. Two swap targets in one static container is fine; a swap target inside another is what blanked every agent chat. #}
{{ icon("sparkle", "icon--sm reasoning__icon") }} Thinking {{ icon("chevron-down", "icon--sm reasoning__chevron") }}
{# 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 machine and message.content %} {# Jinja's own escaping, not `tokens`: that filter marks `@name` as a reference to this reader's files and people, and an `@` inside a command line or a log is neither. Nothing here goes through services/markdown.py either -- the fence in the content stays a fence on screen, which is the rule `_jobs_panel.html` states about the log it shows for the same reason: it came off somebody else's machine. #}
{{ message.content }}
{% 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 {# Copy is here as well as on a delivered turn: a queued machine event holds a job's output, which is the one waiting bubble somebody actually wants to paste somewhere. The hidden source div is already below. #}
{% elif not streaming %}
{# Not on a machine event: editing rewinds the transcript and starts a reply from the edited words, so a pencil here offers to rewrite what a machine reported and re-send it under the reader's own authority. The route refuses it too -- this only removes the button. #} {% if message.role == "user" and not machine %} {% 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