{% from "_macros.html" import icon %}
{#
  What the model did before answering.

  Rendered both live (streamed as a whole block, like the reasoning and the
  answer) and from the stored message afterwards, so the sources behind an
  answer stay in the transcript rather than vanishing when the stream ends.

  EVERYTHING in here is third-party text and is untrusted, exactly as much as
  model output is -- a search provider's results, an administrator's HTTP tool
  relaying whatever it was pointed at, an MCP server's reply. Jinja autoescaping
  covers it. Two things are handled separately: the URL, because `is_linkable`
  is the only thing standing between a result carrying a javascript: URL and an
  anchor pointing at it, and `event.text`, which is rendered as preformatted
  text and deliberately NOT through services/markdown.py -- markdown is the one
  path allowed to emit HTML, and this is the last content that should be given
  it.

  `kind` says how to label the event. Rows written before it existed have none,
  so web_search reads as a search and everything else falls to the generic
  branch: an old notes_search event used to claim it had searched the web.
#}
{% for event in tool_events %}
{% set kind = event.kind or ('search' if event.name == 'web_search' else 'tool') %}
<details class="tool-activity {{ 'tool-activity--error' if event.status == 'error' }}">
  <summary class="tool-activity__summary">
    {% if kind == 'search' %}
      {{ icon("globe", "icon--sm tool-activity__icon") }}
    {% elif kind == 'custom' %}
      {{ icon("link", "icon--sm tool-activity__icon") }}
    {% elif kind == 'mcp' %}
      {{ icon("server", "icon--sm tool-activity__icon") }}
    {% else %}
      {{ icon("sparkle", "icon--sm tool-activity__icon") }}
    {% endif %}

    <span class="tool-activity__label">
      {% if kind == 'search' %}
        {% if event.status == "error" %}
          Web search failed
        {% elif event.query %}
          Searched the web for “{{ event.query }}”
        {% else %}
          Searched the web
        {% endif %}
      {% else %}
        {% set label = event.label or event.name %}
        {% if event.status == "error" %}
          {{ label }} failed
        {% else %}
          {{ label }}
        {% endif %}
        {% if event.query %}
          <span class="tool-activity__count">· {{ event.query }}</span>
        {% endif %}
      {% endif %}

      {% if event.results %}
        <span class="tool-activity__count">
          · {{ event.results | length }} result{{ '' if event.results | length == 1 else 's' }}
        </span>
      {% endif %}
    </span>
    {{ icon("chevron-down", "icon--sm reasoning__chevron") }}
  </summary>

  <div class="tool-activity__body">
    {% if event.detail and kind != 'search' %}
    <p class="tool-result__host">{{ event.detail }}</p>
    {% endif %}

    {% if event.error %}
    <p class="tool-activity__error">{{ event.error }}</p>
    {% elif not event.results and not event.text %}
    <p class="tool-activity__error">Nothing was found.</p>
    {% endif %}

    {% if event.text %}
    <pre class="tool-result__text">{{ event.text }}</pre>
    {% endif %}

    {% for result in event.results %}
    <div class="tool-result">
      {% set scheme = (result.url or "").split(":")[0] | lower %}
      {% if scheme in ("http", "https") %}
      <a class="tool-result__title" href="{{ result.url }}"
         target="_blank" rel="noopener noreferrer nofollow">{{ result.title }}</a>
      {% else %}
      {# Not a link. A result is third-party text and its URL is not trusted to
         be safe to click. #}
      <span class="tool-result__title">{{ result.title }}</span>
      {% endif %}
      <span class="tool-result__host">{{ result.host }}</span>
      {% if result.snippet %}
      <p class="tool-result__snippet">{{ result.snippet }}</p>
      {% endif %}
    </div>
    {% endfor %}
  </div>
</details>
{% endfor %}
