Skip to content

armgit / AiSOC

Public Imported from GitHub
0

Open-source AI Security Operations Center: alert fusion, LLM-agent triage, MITRE ATT&CK investigation, and a replayable decision ledger for every agent step. Self-hostable, runs with no API keys, MIT licensed. Ships an MCP server for Claude, Cursor and Continue.

30.7 KB
Raw Blame
'use client';

/**
 * AI Copilot — full-page experience.
 *
 * Two-column layout:
 *   left  — recent conversations + suggested prompts
 *   right — active conversation thread + composer
 *
 * Talks to `copilotApi`. In the hosted demo an unreachable backend falls back
 * to a deterministic local reply so the UX is still useful with no LLM key;
 * everywhere else the failure is surfaced as a failure, because a fabricated
 * verdict an analyst cannot distinguish from a real one is the worse outcome.
 * Streaming is preferred when available.
 */

import { useEffect, useRef, useState } from 'react';
import useSWR from 'swr';
import { clsx } from 'clsx';
import { motion, AnimatePresence } from 'framer-motion';
import {
  copilotApi,
  type CopilotConversation,
  type CopilotMessage,
} from '@/lib/api';
import { Skeleton } from '@/components/ui/Skeleton';
import { canUseDemoData } from '@/lib/demoFallback';
import { describeApiFailure } from '@/lib/failure';

// ─── Suggested prompts ───────────────────────────────────────────────────────

const SUGGESTED_PROMPTS: Array<{ label: string; prompt: string }> = [
  {
    label: 'Top critical alerts',
    prompt: 'Summarize the top 5 critical alerts in the last 24 hours.',
  },
  {
    label: 'Investigate an entity',
    prompt:
      'Investigate host WIN-FIN-DB01: show recent alerts, related users, and ATT&CK techniques observed.',
  },
  {
    label: 'Explain an ATT&CK technique',
    prompt: 'Explain MITRE ATT&CK T1078 (Valid Accounts) and how we detect it.',
  },
  {
    label: 'Tune a noisy rule',
    prompt:
      'Show me the noisiest detection rules this week and recommend exceptions to reduce false positives.',
  },
  {
    label: 'MTTR by severity',
    prompt: 'What is our MTTR by severity over the last 30 days, and what is trending?',
  },
  {
    label: 'Threat intel match',
    prompt:
      'Has any indicator from the latest threat intel feed matched our telemetry in the last 7 days?',
  },
];

// ─── Demo fallback reply ─────────────────────────────────────────────────────

function buildDemoReply(prompt: string): CopilotMessage {
  const now = new Date().toISOString();
  const lower = prompt.toLowerCase();

  if (lower.includes('mttr')) {
    return {
      id: `demo-${Date.now()}`,
      role: 'assistant',
      createdAt: now,
      content: [
        'MTTR by severity (last 30 days)',
        '',
        '| Severity | MTTR | vs prev 30d |',
        '|---|---|---|',
        '| Critical | 22 min | -18% |',
        '| High | 1h 14m | -9% |',
        '| Medium | 4h 03m | +4% |',
        '| Low | 9h 47m | flat |',
        '',
        'Critical and High are improving, driven by 3 new auto-containment playbooks. Medium is regressing because of a backlog in identity tickets; recommend re-triaging stale items over 6h.',
      ].join('\n'),
      suggestions: [
        'Show me the stale Medium tickets > 6h.',
        'Which playbooks contributed most to the Critical MTTR drop?',
      ],
    };
  }

  if (lower.includes('t1078') || lower.includes('valid accounts')) {
    return {
      id: `demo-${Date.now()}`,
      role: 'assistant',
      createdAt: now,
      content: [
        'T1078 — Valid Accounts',
        '',
        'Adversaries obtain credentials and use them to access systems through legitimate authentication flows. There are four sub-techniques:',
        '- T1078.001 Default Accounts',
        '- T1078.002 Domain Accounts',
        '- T1078.003 Local Accounts',
        '- T1078.004 Cloud Accounts',
        '',
        'How the demo data flags it',
        '1. Impossible travel and geo-velocity (`auth.geo_velocity > 800kph`).',
        '2. New ASN plus new device fingerprint within 24h of a sensitive role assignment.',
        '3. MFA fatigue patterns: 3 or more push denies followed by an approval.',
        '4. Service-account interactive logon outside its baselined hours.',
        '',
        'Coverage in the bundled rules: 74% across the 4 sub-techniques. Gap: limited Cloud Accounts coverage for non-AWS providers.',
      ].join('\n'),
      citations: [
        { label: 'rule:auth-impossible-travel', kind: 'rule' },
        { label: 'rule:mfa-fatigue', kind: 'rule' },
      ],
      suggestions: [
        'Open the rule auth-impossible-travel.',
        'Draft a rule for Azure AD MFA fatigue.',
      ],
    };
  }

  if (lower.includes('investigate')) {
    return {
      id: `demo-${Date.now()}`,
      role: 'assistant',
      createdAt: now,
      content: [
        'Investigation: WIN-FIN-DB01',
        '',
        '- Risk score 92 (top 1% of fleet).',
        '- 3 open alerts: privilege escalation, suspicious PowerShell, outbound to rare ASN.',
        '- Most likely user: `[email protected]` (4 logon events in last 2h, all from a new device).',
        '- Observed ATT&CK techniques: T1078.002, T1059.001, T1071.001.',
        '',
        'Recommended next steps',
        '1. Isolate the host (EDR action).',
        '2. Force credential reset for `j.harlow`.',
        '3. Block egress to ASN `AS204796` at the perimeter.',
      ].join('\n'),
      citations: [
        { label: 'alert:a-2031', kind: 'alert' },
        { label: 'alert:a-2032', kind: 'alert' },
        { label: 'asset:WIN-FIN-DB01', kind: 'asset' },
      ],
      suggestions: [
        'Open a case from these alerts.',
        'Run the host-isolate playbook on WIN-FIN-DB01.',
      ],
    };
  }

  return {
    id: `demo-${Date.now()}`,
    role: 'assistant',
    createdAt: now,
    content: [
      'Running in demo mode. The LLM backend is not reachable from this session, so this reply is a stub.',
      '',
      'Real prompts will be forwarded to `POST /api/v1/copilot/chat` once the API service is reachable.',
      '',
      `> You said: ${prompt}`,
    ].join('\n'),
    suggestions: [
      'Show me the top critical alerts.',
      'What is our MTTR by severity?',
    ],
  };
}

// ─── Markdown-ish renderer (lightweight) ─────────────────────────────────────

function renderRichText(content: string): React.ReactNode {
  // Very small subset: **bold**, `code`, # headings, > blockquote, - lists,
  // and pipe tables. Good enough for assistant replies without pulling in
  // a full markdown library.
  const lines = content.split('\n');
  const blocks: React.ReactNode[] = [];
  let i = 0;

  const inline = (text: string): React.ReactNode => {
    const parts: React.ReactNode[] = [];
    const re = /(\*\*[^*]+\*\*|`[^`]+`)/g;
    let lastIndex = 0;
    let m: RegExpExecArray | null;
    while ((m = re.exec(text)) !== null) {
      if (m.index > lastIndex) parts.push(text.slice(lastIndex, m.index));
      const token = m[0];
      if (token.startsWith('**')) {
        parts.push(
          <strong key={`b-${m.index}`} className="font-semibold text-white">
            {token.slice(2, -2)}
          </strong>,
        );
      } else if (token.startsWith('`')) {
        parts.push(
          <code
            key={`c-${m.index}`}
            className="rounded bg-slate-800/80 px-1.5 py-0.5 font-mono text-[12px] text-emerald-300"
          >
            {token.slice(1, -1)}
          </code>,
        );
      }
      lastIndex = m.index + token.length;
    }
    if (lastIndex < text.length) parts.push(text.slice(lastIndex));
    return parts;
  };

  while (i < lines.length) {
    const line = lines[i];

    // Table: header | --- | rows
    if (line.includes('|') && lines[i + 1]?.match(/^\s*\|?\s*-/)) {
      const headerCells = line
        .split('|')
        .map((c) => c.trim())
        .filter(Boolean);
      i += 2;
      const rows: string[][] = [];
      while (i < lines.length && lines[i].includes('|')) {
        rows.push(
          lines[i]
            .split('|')
            .map((c) => c.trim())
            .filter(Boolean),
        );
        i++;
      }
      blocks.push(
        <div key={`tbl-${blocks.length}`} className="my-3 overflow-x-auto">
          <table className="w-full text-sm">
            <thead>
              <tr className="border-b border-slate-700/60">
                {headerCells.map((h, idx) => (
                  <th
                    key={idx}
                    className="px-3 py-2 text-left font-medium text-slate-300"
                  >
                    {inline(h)}
                  </th>
                ))}
              </tr>
            </thead>
            <tbody>
              {rows.map((r, rIdx) => (
                <tr key={rIdx} className="border-b border-slate-800/60 last:border-0">
                  {r.map((c, cIdx) => (
                    <td key={cIdx} className="px-3 py-2 text-slate-300">
                      {inline(c)}
                    </td>
                  ))}
                </tr>
              ))}
            </tbody>
          </table>
        </div>,
      );
      continue;
    }

    if (line.startsWith('# ')) {
      blocks.push(
        <h2 key={i} className="mt-3 text-lg font-semibold text-white">
          {inline(line.slice(2))}
        </h2>,
      );
      i++;
      continue;
    }

    if (line.startsWith('> ')) {
      blocks.push(
        <blockquote
          key={i}
          className="my-2 border-l-2 border-slate-600 pl-3 text-sm italic text-slate-400"
        >
          {inline(line.slice(2))}
        </blockquote>,
      );
      i++;
      continue;
    }

    if (line.match(/^\s*[-*]\s+/)) {
      const items: string[] = [];
      while (i < lines.length && lines[i].match(/^\s*[-*]\s+/)) {
        items.push(lines[i].replace(/^\s*[-*]\s+/, ''));
        i++;
      }
      blocks.push(
        <ul
          key={`ul-${blocks.length}`}
          className="my-2 list-disc space-y-1 pl-5 text-sm text-slate-300"
        >
          {items.map((it, idx) => (
            <li key={idx}>{inline(it)}</li>
          ))}
        </ul>,
      );
      continue;
    }

    if (line.match(/^\s*\d+\.\s+/)) {
      const items: string[] = [];
      while (i < lines.length && lines[i].match(/^\s*\d+\.\s+/)) {
        items.push(lines[i].replace(/^\s*\d+\.\s+/, ''));
        i++;
      }
      blocks.push(
        <ol
          key={`ol-${blocks.length}`}
          className="my-2 list-decimal space-y-1 pl-5 text-sm text-slate-300"
        >
          {items.map((it, idx) => (
            <li key={idx}>{inline(it)}</li>
          ))}
        </ol>,
      );
      continue;
    }

    if (line.trim() === '') {
      blocks.push(<div key={`sp-${i}`} className="h-2" />);
      i++;
      continue;
    }

    blocks.push(
      <p key={i} className="my-1 text-sm leading-relaxed text-slate-200">
        {inline(line)}
      </p>,
    );
    i++;
  }

  return <>{blocks}</>;
}

// ─── Connection status ───────────────────────────────────────────────────────

export type CopilotStatusTone = 'unknown' | 'ok' | 'failed';

export interface CopilotStatus {
  tone: CopilotStatusTone;
  label: string;
  /** Long-form explanation for the `title` attribute; empty when there is none. */
  detail: string;
}

/**
 * What the header pill is entitled to claim about the backend.
 *
 * It had two states and both were wrong. A green dot reading "Connected"
 * rendered before any request had been made, so it asserted connectivity it
 * had not tested. And **any** error — a 500, an expired session, a dropped
 * connection — flipped it to "Demo mode" on deployments that are not the demo,
 * which tells an operator their platform is in a mode it does not have while
 * the real cause goes unnamed.
 *
 * Three states now, and the middle one is the honest default: nothing has come
 * back yet, so nothing is claimed.
 */
export function copilotStatus(args: {
  sendError: unknown;
  conversationsError: unknown;
  conversationsLoaded: boolean;
  everSucceeded: boolean;
  demo: boolean;
}): CopilotStatus {
  const { sendError, conversationsError, conversationsLoaded, everSucceeded, demo } = args;

  if (sendError) {
    return {
      tone: 'failed',
      label: demo ? 'Demo reply shown' : 'Last message failed',
      detail: demo
        ? `The backend did not answer, so the reply below came from a built-in demo script. ${describeApiFailure(
            sendError,
            { subject: 'copilot reply' },
          )}`
        : describeApiFailure(sendError, { subject: 'copilot reply' }),
    };
  }
  if (conversationsError) {
    // Listing conversations and answering a question are different endpoints.
    // One failing does not prove the other is down, so this says what it knows.
    return {
      tone: 'failed',
      label: 'History unavailable',
      detail: describeApiFailure(conversationsError, { subject: 'conversation history' }),
    };
  }
  if (everSucceeded || conversationsLoaded) {
    return { tone: 'ok', label: 'Connected', detail: 'The last request to the copilot succeeded.' };
  }
  return {
    tone: 'unknown',
    label: 'Not yet contacted',
    detail: 'No request has completed yet, so connectivity is untested.',
  };
}

const STATUS_DOT: Record<CopilotStatusTone, string> = {
  unknown: 'bg-slate-500',
  ok: 'bg-emerald-400',
  failed: 'bg-amber-400',
};

// ─── Component ───────────────────────────────────────────────────────────────

export function CopilotView() {
  const [messages, setMessages] = useState<CopilotMessage[]>([]);
  const [conversationId, setConversationId] = useState<string | undefined>();
  const [input, setInput] = useState('');
  const [sending, setSending] = useState(false);
  const [error, setError] = useState<unknown>(null);
  // The prompt whose request failed, so the retry re-issues *that* request
  // rather than making the operator retype it.
  const [failedPrompt, setFailedPrompt] = useState<string | null>(null);
  const [everSucceeded, setEverSucceeded] = useState(false);
  const scrollerRef = useRef<HTMLDivElement>(null);

  const conversationsState = useSWR(
    'copilot.conversations',
    () => copilotApi.listConversations(),
    { revalidateOnFocus: false, shouldRetryOnError: false },
  );
  const conversations: CopilotConversation[] =
    conversationsState.data?.conversations ?? [];

  // Auto-scroll thread to bottom on new messages.
  useEffect(() => {
    const el = scrollerRef.current;
    if (el) el.scrollTop = el.scrollHeight;
  }, [messages.length, sending]);

  /**
   * `replay` re-issues a prompt already in the thread (the retry after a
   * failure) without appending a second identical user bubble.
   */
  const send = async (prompt: string, { replay = false }: { replay?: boolean } = {}) => {
    const trimmed = prompt.trim();
    if (!trimmed || sending) return;

    if (!replay) {
      const userMsg: CopilotMessage = {
        id: `u-${Date.now()}`,
        role: 'user',
        content: trimmed,
        createdAt: new Date().toISOString(),
      };
      setMessages((prev) => [...prev, userMsg]);
    }
    setInput('');
    setSending(true);
    setError(null);
    setFailedPrompt(null);

    try {
      const res = await copilotApi.chat({
        conversationId,
        message: trimmed,
        context: { page: 'copilot' },
      });
      setConversationId(res.conversationId);
      // A 200 does not mean a model answered. When no LLM key is configured,
      // or the call fails, the backend falls back to a canned paragraph and
      // now says so with `source: "template"`. Without this the analyst reads
      // generic claims — "this IP was seen in 3 other alerts" — as real
      // analysis of their own environment, and the honest fallback below never
      // fires because the request technically succeeded.
      const source = (res as { source?: string }).source;
      const notice = (res as { notice?: string }).notice;
      // Parity 3.6: a model answer that names an IP, a hash or a CVE is
      // making a claim about this estate. The backend grades each such
      // claim against the records it was given; an analyst has no other
      // way to tell one drawn from the evidence from one the model
      // produced because it sounded right.
      //
      // The warning is appended rather than used to suppress or edit the
      // answer. Hiding the unsupported half would show the analyst a
      // different answer than the model gave, edited by something that
      // cannot reliably tell which half was wrong.
      const grounding = (res as { grounding?: { label?: string; uncited?: string[] } }).grounding;
      const uncited = grounding?.uncited ?? [];
      const groundingWarning =
        uncited.length > 0
          ? `Not supported by anything this answer was given: ${uncited.join(', ')}. Verify before acting.`
          : null;

      const reply =
        source === 'template'
          ? {
              ...res.reply,
              content: [
                notice ??
                  'This reply came from a built-in template, not a language model, and is not analysis of your environment.',
                '',
                '---',
                '',
                res.reply.content,
              ].join('\n'),
            }
          : groundingWarning
            ? { ...res.reply, content: [groundingWarning, '', '---', '', res.reply.content].join('\n') }
            : res.reply;
      setMessages((prev) => [...prev, reply]);
      setEverSucceeded(true);
    } catch (err) {
      // This used to substitute `buildDemoReply(trimmed)` unconditionally, so
      // a backend outage produced an invented investigation — a named host, a
      // named user, ATT&CK techniques and three alert "citations" — rendered
      // in the same style as a real answer. An analyst had no way to tell.
      //
      // Outside demo mode the error is now surfaced as an error. The dock
      // feeling alive is not worth a fabricated verdict.
      if (canUseDemoData()) {
        setMessages((prev) => [...prev, buildDemoReply(trimmed)]);
      }
      setError(err);
      setFailedPrompt(trimmed);
    } finally {
      setSending(false);
    }
  };

  const onKeyDown = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
    if (e.key === 'Enter' && !e.shiftKey) {
      e.preventDefault();
      void send(input);
    }
  };

  const startNew = () => {
    setMessages([]);
    setConversationId(undefined);
    setError(null);
    setFailedPrompt(null);
  };

  const empty = messages.length === 0;
  const demo = canUseDemoData();
  const status = copilotStatus({
    sendError: error,
    conversationsError: conversationsState.error,
    conversationsLoaded: conversationsState.data !== undefined,
    everSucceeded,
    demo,
  });

  return (
    <div className="grid h-[calc(100vh-7rem)] grid-cols-1 gap-4 lg:grid-cols-[18rem_1fr]">
      {/* Sidebar */}
      <aside className="hidden flex-col gap-3 rounded-xl border border-slate-800/80 bg-slate-900/40 p-3 lg:flex">
        <button
          onClick={startNew}
          className="flex items-center justify-center gap-2 rounded-lg bg-emerald-500 px-3 py-2 text-sm font-semibold text-emerald-950 shadow-sm transition-colors hover:bg-emerald-400"
        >
          New chat
        </button>

        <div>
          <h3 className="mb-2 px-1 text-[11px] font-semibold uppercase tracking-wider text-slate-500">
            Suggested
          </h3>
          <ul className="space-y-1">
            {SUGGESTED_PROMPTS.slice(0, 4).map((p) => (
              <li key={p.label}>
                <button
                  onClick={() => void send(p.prompt)}
                  className="group flex w-full items-start gap-2 rounded-md px-2 py-2 text-left text-sm text-slate-300 transition-colors hover:bg-slate-800/60 hover:text-white"
                >
                  <span className="leading-tight">{p.label}</span>
                </button>
              </li>
            ))}
          </ul>
        </div>

        <div className="mt-2 flex-1 overflow-y-auto">
          <h3 className="mb-2 px-1 text-[11px] font-semibold uppercase tracking-wider text-slate-500">
            Recent
          </h3>
          {conversationsState.isLoading ? (
            <div className="space-y-2 px-1">
              <Skeleton className="h-8" />
              <Skeleton className="h-8" />
              <Skeleton className="h-8" />
            </div>
          ) : conversations.length === 0 ? (
            <p className="px-2 text-xs text-slate-500">
              Past conversations will appear here.
            </p>
          ) : (
            <ul className="space-y-1">
              {conversations.slice(0, 8).map((c) => (
                <li key={c.id}>
                  <button
                    className={clsx(
                      'block w-full truncate rounded-md px-2 py-1.5 text-left text-sm transition-colors',
                      c.id === conversationId
                        ? 'bg-slate-800 text-white'
                        : 'text-slate-300 hover:bg-slate-800/60',
                    )}
                    onClick={() => setConversationId(c.id)}
                    title={c.title}
                  >
                    {c.title}
                  </button>
                </li>
              ))}
            </ul>
          )}
        </div>
      </aside>

      {/* Chat panel */}
      <section className="flex min-h-0 flex-col rounded-xl border border-slate-800/80 bg-slate-900/40">
        {/* Header */}
        <div className="flex items-center justify-between border-b border-slate-800/80 px-4 py-3">
          <div>
            <h1 className="text-base font-semibold text-white">AI Copilot</h1>
            <p className="text-xs text-slate-400">
              Ask about alerts, hosts, MITRE techniques, or your detection
              coverage.
            </p>
          </div>
          <div className="flex items-center gap-2 text-xs text-slate-400" title={status.detail}>
            <span className={clsx('inline-block h-2 w-2 rounded-full', STATUS_DOT[status.tone])} />
            {status.label}
          </div>
        </div>

        {/* Thread */}
        <div ref={scrollerRef} className="min-h-0 flex-1 overflow-y-auto p-4">
          {empty ? (
            <div className="mx-auto flex h-full max-w-2xl flex-col items-center justify-center text-center">
              <h2 className="text-lg font-semibold text-white">
                Copilot
              </h2>
              <p className="mt-1 max-w-md text-sm text-slate-400">
                Ask about alerts, hosts, MITRE techniques, or detection
                coverage. Cite an alert ID, hostname, or technique to scope
                the answer.
              </p>
              <div className="mt-6 grid w-full max-w-2xl grid-cols-1 gap-2 sm:grid-cols-2">
                {SUGGESTED_PROMPTS.map((p) => (
                  <button
                    key={p.label}
                    onClick={() => void send(p.prompt)}
                    className="flex items-start gap-3 rounded-lg border border-slate-800/80 bg-slate-900/60 p-3 text-left transition-colors hover:border-emerald-500/40 hover:bg-slate-800/60"
                  >
                    <span>
                      <span className="block text-sm font-medium text-white">
                        {p.label}
                      </span>
                      <span className="block text-xs text-slate-400">
                        {p.prompt}
                      </span>
                    </span>
                  </button>
                ))}
              </div>
            </div>
          ) : (
            <ul className="mx-auto max-w-3xl space-y-4">
              <AnimatePresence initial={false}>
                {messages.map((m) => (
                  <motion.li
                    key={m.id}
                    initial={{ opacity: 0, y: 8 }}
                    animate={{ opacity: 1, y: 0 }}
                    exit={{ opacity: 0 }}
                    className={clsx(
                      'flex gap-3',
                      m.role === 'user' ? 'justify-end' : 'justify-start',
                    )}
                  >
                    {m.role === 'assistant' && (
                      <div className="flex h-8 w-8 flex-none items-center justify-center rounded-full bg-emerald-500/15 text-[10px] font-semibold uppercase tracking-wider text-emerald-300">
                        ai
                      </div>
                    )}
                    <div
                      className={clsx(
                        'max-w-[85%] rounded-2xl px-4 py-3 shadow-sm',
                        m.role === 'user'
                          ? 'bg-emerald-500/15 text-white ring-1 ring-emerald-500/30'
                          : 'bg-slate-800/70 text-slate-100 ring-1 ring-slate-700/60',
                      )}
                    >
                      {m.role === 'assistant' ? (
                        renderRichText(m.content)
                      ) : (
                        <p className="whitespace-pre-wrap text-sm text-slate-100">
                          {m.content}
                        </p>
                      )}

                      {m.citations && m.citations.length > 0 && (
                        <div className="mt-3 flex flex-wrap gap-1.5">
                          {m.citations.map((c, idx) => (
                            <span
                              key={idx}
                              className="inline-flex items-center gap-1 rounded-full border border-slate-700/70 bg-slate-900/60 px-2 py-0.5 text-[11px] text-slate-300"
                            >
                              <span className="text-slate-500">
                                {c.kind ?? 'ref'}:
                              </span>
                              {c.label}
                            </span>
                          ))}
                        </div>
                      )}

                      {m.suggestions && m.suggestions.length > 0 && (
                        <div className="mt-3 flex flex-wrap gap-2">
                          {m.suggestions.map((s, idx) => (
                            <button
                              key={idx}
                              onClick={() => void send(s)}
                              className="rounded-full border border-emerald-500/30 bg-emerald-500/10 px-3 py-1 text-xs text-emerald-200 transition-colors hover:bg-emerald-500/20"
                            >
                              {s}
                            </button>
                          ))}
                        </div>
                      )}
                    </div>
                    {m.role === 'user' && (
                      <div className="flex h-8 w-8 flex-none items-center justify-center rounded-full bg-slate-700 text-xs font-semibold text-slate-200">
                        You
                      </div>
                    )}
                  </motion.li>
                ))}
              </AnimatePresence>
              {sending && (
                <li className="flex justify-start gap-3">
                  <div className="flex h-8 w-8 flex-none items-center justify-center rounded-full bg-emerald-500/15 text-[10px] font-semibold uppercase tracking-wider text-emerald-300">
                    ai
                  </div>
                  <div className="rounded-2xl bg-slate-800/70 px-4 py-3 ring-1 ring-slate-700/60">
                    <TypingDots />
                  </div>
                </li>
              )}
              {/* Outside the hosted demo a failed send produced no reply at
                  all: the analyst's question sat in the thread unanswered,
                  and the only signal anywhere was a header pill reading
                  "Demo mode". In the demo a scripted reply *was* substituted,
                  so "No answer" would be its own small lie — the pill
                  discloses that case instead. */}
              {error != null && !sending && !demo && (
                <li role="status" className="flex justify-start">
                  <div className="w-full rounded-2xl border border-amber-500/30 bg-amber-500/5 px-4 py-3 text-sm text-amber-200">
                    <p>
                      <span className="font-semibold">No answer:</span>{' '}
                      {describeApiFailure(error, { subject: 'copilot reply' })}
                    </p>
                    {failedPrompt && (
                      <button
                        type="button"
                        onClick={() => void send(failedPrompt, { replay: true })}
                        className="mt-2 rounded border border-amber-400/40 px-2 py-1 text-xs font-medium text-amber-100 transition-colors hover:bg-amber-400/10"
                      >
                        Retry that question
                      </button>
                    )}
                  </div>
                </li>
              )}
            </ul>
          )}
        </div>

        {/* Composer */}
        <div className="border-t border-slate-800/80 p-3">
          <div className="mx-auto flex max-w-3xl items-end gap-2 rounded-xl border border-slate-700/70 bg-slate-950/40 p-2 focus-within:border-emerald-500/50">
            <textarea
              value={input}
              onChange={(e) => setInput(e.target.value)}
              onKeyDown={onKeyDown}
              rows={1}
              placeholder="Ask anything — try 'investigate WIN-FIN-DB01'…"
              className="flex-1 resize-none bg-transparent px-2 py-2 text-sm text-slate-100 placeholder-slate-500 outline-none"
            />
            <button
              onClick={() => void send(input)}
              disabled={!input.trim() || sending}
              className={clsx(
                'flex-none rounded-lg px-3 py-2 text-sm font-semibold transition-colors',
                input.trim() && !sending
                  ? 'bg-emerald-500 text-emerald-950 hover:bg-emerald-400'
                  : 'bg-slate-800 text-slate-500',
              )}
            >
              {sending ? '…' : 'Send'}
            </button>
          </div>
          <p className="mx-auto mt-1.5 max-w-3xl px-1 text-[11px] text-slate-500">
            Press <kbd className="rounded bg-slate-800 px-1">Enter</kbd> to
            send,{' '}
            <kbd className="rounded bg-slate-800 px-1">Shift</kbd>+
            <kbd className="rounded bg-slate-800 px-1">Enter</kbd> for newline.
            Responses can include actions you can run from inside the chat.
          </p>
        </div>
      </section>
    </div>
  );
}

function TypingDots() {
  return (
    <span className="inline-flex items-center gap-1" aria-label="Assistant is typing">
      {[0, 1, 2].map((i) => (
        <motion.span
          key={i}
          className="block h-1.5 w-1.5 rounded-full bg-slate-400"
          animate={{ opacity: [0.2, 1, 0.2] }}
          transition={{ duration: 1.1, repeat: Infinity, delay: i * 0.18 }}
        />
      ))}
    </span>
  );
}