{% from "_macros.html" import icon %}
{#
  Everything is rendered through normal Jinja escaping and never `|safe`. This
  JSON is full of model output, search results and uploaded documents -- hard
  rule 6 applies here exactly as it does to a chat bubble.
#}
<p class="inspector__note">
  Rebuilt now against the current configuration. This is not a recording of the
  request that produced the last reply — if a prompt or a setting has changed
  since, this shows what would be sent today.
</p>

<div class="btn-row">
  <button class="btn btn--sm" type="button"
          hx-get="/api/chats/{{ chat.id }}/inspect"
          hx-target="#inspector-body" hx-swap="innerHTML">
    {{ icon("refresh", "icon--sm") }} Refresh
  </button>
</div>

<h3 class="inspector__heading">This chat</h3>
<dl class="inspector__facts">
  <dt>Model</dt><dd>{{ chat.model_id or "—" }}</dd>
  <dt>Context</dt>
  <dd>
    {% if model and model.context_length %}
      {{ model.context_length }} tokens
    {% else %}
      {# Shown here rather than only beside a reply, because "why is there no
         percentage" is a question people have before the first one. #}
      not set{% if model %} — <a href="/admin/models/{{ model.id }}/edit">set it</a>{% endif %}
    {% endif %}
  </dd>
  {% if chat.temporary %}<dt>Lifetime</dt><dd>temporary</dd>{% endif %}
</dl>

<h3 class="inspector__heading">Last reply</h3>
{% if row %}
<dl class="inspector__facts">
  <dt>Tokens</dt>
  <dd>
    {% if metrics.has_anything %}
      {% if metrics.estimated %}~{% endif %}{{ metrics.prompt_tokens }} in,
      {% if metrics.estimated %}~{% endif %}{{ metrics.completion_tokens }} out
      {% if metrics.estimated %}<span class="badge">estimated</span>{% endif %}
    {% else %}
      not reported
    {% endif %}
  </dd>
  <dt>Elapsed</dt><dd>{{ metrics.elapsed_ms }} ms</dd>
  <dt>Thinking</dt><dd>{{ row.reasoning_ms }} ms</dd>
  <dt>Tool calls</dt><dd>{{ row.tool_calls_json | length }}</dd>
  <dt>Rounds</dt><dd>{{ metrics.rounds }}</dd>
  <dt>State</dt>
  <dd>
    {% if row.error %}<span class="badge badge--danger">error</span>
    {% elif row.stopped %}<span class="badge badge--warning">stopped</span>
    {% elif not row.complete %}<span class="badge">writing</span>
    {% else %}<span class="badge badge--success">complete</span>{% endif %}
  </dd>
</dl>
{% if row.error %}
<p class="inspector__note">{{ row.error }}</p>
{% endif %}
{% else %}
<p class="inspector__note">Nothing has been answered in this chat yet.</p>
{% endif %}

<h3 class="inspector__heading">System message</h3>
{% if system %}
<pre class="inspector__json">{{ system }}</pre>
{% else %}
<p class="inspector__note">None is being sent.</p>
{% endif %}

<h3 class="inspector__heading">
  Tools offered
  <span class="badge">{{ tool_names | length }}</span>
</h3>
{% if tool_names %}
<p class="inspector__note mono">{{ tool_names | join(", ") }}</p>
{% else %}
<p class="inspector__note">
  None. A model has to be marked as supporting tools, the user needs the
  permission, and the tool itself has to be turned on.
</p>
{% endif %}

<h3 class="inspector__heading">Request body</h3>
<pre class="inspector__json">{{ request_json }}</pre>
