{% from "_macros.html" import icon %} {% 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) %}
{#- No avatar and no name. The two kinds of turn already look nothing alike -- a prompt block and a reply -- so a picture and "Frodo" / "Bonsai" beside every one said nothing and took a column off every line. The model that wrote a reply is in its metrics' tooltip. #}
{% set badged = machine or (message.role == "user" and message.origin == "terminal") %} {% if badged %}
{#- Which machine wrote it: a schedule firing into its task chat, the main agent's task or message in a helper's chat, or -- everywhere else -- a background job's or a helper's note, the harness's own turn. One badge used to say "Background job" for all three. #} {% if machine %}{{ icon("terminal", "icon--sm") }} {% if chat.kind == "task" %}{{ t("Scheduled run") }}{% elif chat.parent_chat_id %}{{ t("From the main agent") }}{% else %}{{ t("Background job") }}{% endif %}{% endif %} {% if message.role == "user" and message.origin == "terminal" %} {# Typed into the LLeMbas CLI's terminal on the linked device: the same session, shown here as it happens. #} {{ icon("terminal", "icon--sm") }} {{ t("in the terminal") }} {% endif %}
{% 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 %} · {{ t("%(n)s page", n=attachment.pages) if attachment.pages == 1 else t("%(n)s pages", n=attachment.pages) }}{% endif %} {%- if attachment.truncated %} · {{ t("truncated") }}{% endif %} {%- if attachment.extracted_text %} · {{ t("view extracted text") }} {%- endif %} {% if attachment.extraction_error %} {{ attachment.extraction_error }} {% endif %}
{% endif %} {% endfor %}
{% endif %} {% if message.terminal_attachments_json %} {# What a turn typed in the device's terminal had attached: names only -- the files are on the device, and nothing here can open them. #}
{% for chip in message.terminal_attachments_json %}
{{ icon("attach", "icon--sm") }} {{ chip.name }} {%- if chip.size %}{{ t("%(kb)s KB", kb=(chip.size / 1024) | round(1)) }} · {% endif -%} {{ t("on the device") }}
{% endfor %}
{% endif %} {% if streaming %} {# One column with one rhythm for the closed steps and the step still being written, so a block moving from the tail into a step above keeps the spacing it had and the transcript does not shift by the difference. A plain wrapper, not a swap target: the rule below is about one `sse-swap` element inside another, and this contains several siblings. The finished bubble has no need of it -- the `done` frame draws every step inside `.msg__steps`, which carries the same rhythm. #}
{# 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. Each frame is sent when its content moves, an empty one included. When a round closes the `steps` frame itself carries the tail out of band (`_tail_oob` in api/chats.py), aimed at the ids below, so the text moving into a step above and leaving the tail is one swap and never on screen twice. The ids are therefore part of the wire protocol. These carry the open tail only, so an empty one means the tail is genuinely empty; `steps` is the one that must never blank. #} {# 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 -- which is what keeps it open across a round: steps.js reads whether it is open and opens the closed block the thinking became. 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") }} {{ t("Thinking") }} {{ icon("chevron-down", "icon--sm reasoning__chevron") }}
{# Where a question from the model, or a command waiting to be allowed, lands. An empty frame is sent the moment it empties, 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. The row keeps one height whether the dots or the status are showing (chat.css), so neither appearing moves the transcript. #}
{# 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. `--live` reserves the row's height before the first chips arrive. #}
{% 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. #} {% set fell_back = (message.usage_json or {}).get("fallback") if message.role == "assistant" else none %} {% if fell_back and fell_back.failed %} {# Another model answered (services/fallback.py): said from the reply's metadata, never written into its content, which goes back to the model. #}

{{ icon("refresh", "icon--sm") }} {{ t("%(failed)s did not answer (%(reason)s), so %(answered)s answered instead.", failed=fell_back.failed, answered=fell_back.label, reason=(fell_back.reason or "").rstrip(".")) }}

{% endif %} {% 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") }} {{ t("Stopped. This reply is cut short.") }}

{% endif %} {% elif message.content == continue_prompt() %} {# Continue: the harness's own sentence, sent as this turn. Shown as what was pressed, not as a paragraph nobody typed -- the LLeMbas CLI's TUI draws the same turn as "↻ continue". #}

↻ {{ t("Continue") }}

{% 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, 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 %} {# The plan as this reply left it, with the way to carry it out. #} {% with plan = message.plan %} {% include "chat/_plan.html" %} {% endwith %} {% endif %} {% if not streaming and message.role == "assistant" and (message.usage_json or message.model_id) %} {# Above the buttons, not among them: the actions row is things you press. #}
{% if message.usage_json %} {% with metrics = message.usage_json | metrics %} {% include "chat/_metrics.html" %} {% endwith %} {% endif %} {#- Which model wrote it: the one place it is said now that a reply carries no name. #} {% if message.model_id %}{{ speaking_model.label if speaking_model else message.model_id }}{% endif %}
{% 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") }} {{ t("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. #} {# A device keeps its own history: the pencil is there only while the device is linked and offers `rewind`, and a turn typed in its terminal has no turn id here to rewind by. The files the device changed are not undone, and the control says so. #} {% set device_rewinds = chat.device_token_id and device_offers(chat, "rewind") %} {% if message.role == "user" and not machine and (not chat.device_token_id or (device_rewinds and message.origin != "terminal")) %} {% endif %} {% if message.role == "assistant" %} {# Continue: carry on after a stop, or after an answer that ended too soon -- the LLeMbas CLI's ctrl+g. Shown on the newest reply only (ui.js marks it); `/continue` does the same. #} {# A device keeps its own history: regenerate only while it is linked and offers `rewind` (the same rule as the pencil above). #} {% if not chat.device_token_id or device_rewinds %} {% endif %} {% 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