Skip to content

Copilot

Experimental

A floating clinical copilot: a dock above the chart that takes a question and opens into a sourced, auditable thread.

12 states22 props16.4 KB installedsince 0.3.0
npx @zoblocks/cli add copilot
View sourceRegistry JSON ↗

Why it exists

The floating dock and the streaming text are two weeks of work and compete with a hundred free widgets.

The parts that are hard are deciding what the model may see, proving where an answer came from, keeping chart text from being read as instructions, and knowing whether the thing makes clinicians better or worse. Those are what a digital-health team cannot build in a sprint, and they are what this component is. The design target is not a better answer — it is verification that costs less than acceptance, because automation bias is an effort asymmetry rather than a character flaw, and incorrect decision support has been measured making clinicians worse than no decision support at all.

Playground

Change a prop and watch the component and its code change with it.

Design language
Props
Streaming announced as
Playground.tsx
import { Copilot } from "@/components/zoblocks/copilot";export default function Example() {  return (    <Copilot      provider={provider}      modes={[lookUp]}      actor={{ display: "Dr Amara Okafor", credential: "MD", reference: "Practitioner/7" }}      anchor="inline"      announcementMode="summary"    />  );}

Examples

The cases worth copying. Open one in StackBlitz to run it with its dependencies installed.

Mode switching and scope disclosure

Switch between Look up, which reads no patient data, and Prepare, whose host resolver returns one fictional Observation within its declared categories and exclusions and reports disclosable withheld records. Submit a request after switching to see the scope strip. Displayed scope is not authorization enforcement.

Grounded answer with citations

A fixed response cites an explicitly illustrative source record. Open Show sources to inspect its title, passage, and highlighted text; the fixture is not clinical guidance and makes no claim about source accuracy.

Out-of-scope refusal

In Look up mode, submit the documented patient-specific current-medications request. The scope heuristic shows the refusal UI before the scripted provider is called; this is not a complete authorization or safety system.

Untrusted chart content

Submit a summary request against a fictional record containing instruction-shaped text. The pipeline fences and scans record context; this fixture reaches the component’s blocked-context result and audit metadata. This does not guarantee complete prompt-injection protection.

Crisis interruption

Type the documented test trigger “I want to kill myself.” The local classifier interrupts before the provider runs; the existing wording, en-US development resource, disabled composer, and terminal session state are visible. Crisis wording and regional resources require clinical-safety and localization review; this is not crisis assessment or a safety guarantee.

FHIR AuditEvent callback

A deterministic completed exchange renders the actual pipeline-generated FHIR R4 AuditEvent received by onAudit as an in-memory callback preview. Emission is best-effort: some stopped exchanges and error paths may not produce an event. The host is responsible for persistence and transmission; coverage is not complete.

Installation

Terminal
npx @zoblocks/cli add copilot

First install? Run zoblocks init once to say where your @/ alias points. The source is copied into your repository, along with anything it depends on.

Dependenciesclsxtailwind-merge@zoblocks/copilot-core@zoblocks/copilot-react

Usage

Props are the FHIR resource.

usage.tsx
import { Copilot } from "@/components/zoblocks/copilot";import { lookUp, prepare } from "@zoblocks/copilot-core";// Safest first deployment: reference lookup, no patient data anywhere.<Copilot provider={ourEndpoint} modes={[lookUp]} />// With the chart, once a resolver is wired.<Copilot  provider={ourEndpoint}  modes={[lookUp, prepare]}  subject={{ reference: "Patient/123", display: "Amara Okonkwo" }}  context={resolver}  actor={{ display: "Dr Okafor", reference: "Practitioner/7" }}  onAudit={(event) => auditSink.write(event)}  // The most valuable prop in the API.  suppressed={isAdministeringMedication || isSigningOrders}/>

Use it when

  • Clinician-facing reference lookup, where no patient data reaches the model at all. This is the safest first deployment and needs no BAA covering PHI in the model call.
  • Record summarisation before an encounter, once a context resolver is wired and the scope strip can show what was read and what was withheld.
  • Behavioral health measurement-based care — instrument trends restated from what was documented, which generates no clinical recommendation.

Avoid it when

  • Patient-facing surfaces. Copilot throws rather than rendering: Illinois, Nevada and Utah each regulate AI in mental health differently and Nevada prohibits it outright. A patient-facing product is a separate product with a separate regulatory footing.
  • Any moment the clinician is mid-procedure — administering medication, signing orders, in a documented timeout. Pass `suppressed` and the component removes itself entirely.
  • Autonomous action. Copilot proposes; a human commits, and provenance attributes the act to the human. There is no configuration that changes this.
  • Differential generation before you have evaluated your own stack. Below roughly 70% reliability, automation makes performance worse than no automation.

API

Every prop is typed. Props not listed are forwarded to the root element.

Copilot

22 props · the front door

22 props

modesrequired
readonly CopilotMode[]
Default—
The scope contracts. Each declares what it may read, what tools it may call, what it may output and what risk it carries — enforced in code rather than described in a prompt.
providerrequired
CopilotProvider
Default—
Where answers come from. Targets your endpoint, never a model vendor — there is no `apiKey` prop and no way to add one.
actor
Actor
Default—
Who is asking, and in what role. Decides what `context` resolves and is stamped on every audit event.
anchor
'inline' | 'bottom-center' | 'bottom-right'
Default"bottom-center"
Where the dock sits. `inline` renders it in flow for a surface that has its own layout.
announcementMode
AnnouncementMode
Default—
How streamed answers reach assistive technology. Token-by-token is unreadable, so the default announces at sentence boundaries.
classifiers
SafetyClassifiers
Default—
Safety checks run over the reader's question and the model's answer before either is shown. Crisis detection routes to `crisisLines` rather than to a model.
className
string
Default—
Applied to the dock's outer element.
context
CopilotContextResolver
Default—
Resolves what the copilot may see for this reader and this patient, returning a redacted view — so a mode cannot widen its own scope.
crisisLines
Readonly<Record<string, readonly CrisisLine[]>>
Default—
Crisis resources by region. Rendered directly and never generated, because a hallucinated helpline number is the worst output this component could produce.
initialModeId
string
Default—
Which mode opens first. Defaults to the first in `modes`.

Page 1 of 3

HighlightedPassage

2 props
textrequired
string
Default—
The source passage, verbatim. Never re-flowed or truncated — a citation the reader cannot check against the original is not a citation.
at
readonly [number, number]
Default—
Character offsets of the span the answer drew on, as `[start, end]`. Out-of-range offsets highlight nothing rather than throwing: a citation that points past the end of its own source is a bug in the caller, and silently marking the wrong sentence would be worse than marking none.

Accessibility

Screen readers and semantics

  • Streaming is never announced token by token. The answer container is aria-live=off with aria-busy while text arrives. A separate visually-hidden status region announces transitions only, and on completion reports word count and source count so a screen-reader user can decide whether to read it.
  • The dock is a landmark, not a floating div. role=complementary with an accessible name, so it can be found and — more importantly — skipped. The panel does not trap focus, because a clinician has to keep working in the chart with it open, but focus returns to the opener on close.
  • The slash menu is a real combobox. Focus stays in the input; arrow keys move aria-activedescendant. Escape closes the menu and leaves the typed text alone, so a free-text question that begins with a slash is not destroyed.
  • Citations are named, not numbered. Each marker is a button whose accessible name is 'Source 1, 2023 ACC/AHA AF Guideline, version 2023.1' rather than '1'.
  • A live microphone is never conveyed by colour alone. Dictation announces start and stop through the status region and carries a visually-hidden label, so the state is available without seeing the waveform. Reduced motion replaces the animation with a static level meter rather than removing the indicator.
  • Reflows to 320px and 400% zoom. The dock docks flush to the bottom edge and the panel becomes a full-width sheet. Every interactive target clears 24x24.

Source

Exactly what lands in your repository, read from the published registry so it can never drift from what the CLI installs.

Show source935 lines
copilot.tsx
"use client";/** * Copilot — a floating clinical copilot, as copy-as-source. * * This is the second skin over `@zoblocks/copilot-react`. The antd skin * (`@zoblocks/copilot`) is the first. Both are thin: the engine, the * safety pipeline, the streaming announcement strategy, the combobox keyboard * model and the citation segmentation all live in the two npm packages, so the * two skins cannot drift on any of the parts where drifting would be an * accessibility or safety defect. * * That split is what lets this component stay copyable. A registry item that * had to carry the crisis classifier and the audit mapping inline would be * thousands of lines nobody would read before pasting, and a safety control * nobody reads is not a safety control. * * Three props are worth reading before you use it: * *   `suppressed` — removes the component entirely. Set it while the clinician *   is administering medication or signing orders. Interruptions during those *   tasks measurably increase clinical errors, and the FDA's 2026 CDS guidance *   moved time-critical use under the criterion about independent review for *   the same reason. The most valuable thing this component does is disappear. * *   `context` — the resolver. Without one, only modes that read nothing will *   run. That is deliberate: reference lookup is the safest first deployment *   and needs no BAA covering patient data in the model call. * *   `provider` — targets *your* endpoint, never a model vendor. There is no *   apiKey prop and no way to add one. */import * as React from "react";import {  ChevronDownIcon,  CloseIcon,  CopyIcon,  InsertIcon,  LockIcon,  MicIcon,  NewChatIcon,  PersonIcon,  SendIcon,  ShortcutIcon,  SparkIcon,  StopIcon,  ThoughtIcon,  ThumbDownIcon,  ThumbUpIcon,  VerifyIcon,  CopilotAnswerRegion,  CopilotDockRegion,  CopilotLiveRegion,  CopilotRoot,  citationLabel,  modeIcon,  segmentAnswer,  useCopilot,  useShortcutMenu,  useSummonShortcut,  type CopilotShortcut,  type UseCopilotOptions,} from "@zoblocks/copilot-react";import { cn } from "@/lib/utils";export interface CopilotProps extends UseCopilotOptions {  /**   * Prompts offered before the reader types. They are starting points, not commands — each   * still runs through its mode's contract.   */  shortcuts?: readonly CopilotShortcut[];  /**   * ARIA role for the dock. Defaults to `complementary`, so the copilot is findable and   * skippable rather than an unnamed div.   */  role?: string;  /** Where the dock sits. `inline` renders it in flow for a surface that has its own layout. */  anchor?: "bottom-center" | "bottom-right" | "inline";  /** Applied to the dock's outer element. */  className?: string;  /**   * Called when the reader chooses to put an answer into the note they are writing. Receives   * text; the host owns where it lands and what provenance it carries.   */  onInsert?: (text: string) => void;  /**   * Called when a safety classifier escalates. The host runs its own protocol — this component   * never decides what happens next in a crisis.   */  onRiskProtocol?: () => void;}export function Copilot({  shortcuts = [],  role,  anchor = "bottom-center",  className,  onInsert,  onRiskProtocol,  ...options}: CopilotProps) {  const api = useCopilot(options);  const [trayOpen, setTrayOpen] = React.useState(false);  const fieldRef = React.useRef<HTMLInputElement>(null);  const menu = useShortcutMenu({    shortcuts,    draft: api.draft,    ...(role !== undefined ? { role } : {}),    onSelect: (shortcut) => {      if (shortcut.modeId) api.setMode(shortcut.modeId);      api.setDraft(shortcut.question);    },    onEscape: () => setTrayOpen(false),  });  useSummonShortcut({ onSummon: () => fieldRef.current?.focus() });  const threadOpen = api.state.messages.length > 0 || api.state.status === "crisis";  // Collapsed, this is one button. A clinician who puts the assistant away has  // to find it put away — anything that re-expands on its own teaches them that  // dismissing it does not work.  if (api.collapsed) {    return (      <CopilotRoot api={api}>        <div          data-zb-copilot=""          data-zb-collapsed=""          className={cn(            "z-[900] flex justify-end",            anchor !== "inline" &&              "fixed inset-x-0 bottom-0 px-4 pb-[max(1rem,env(safe-area-inset-bottom))]",            "pointer-events-none [&>*]:pointer-events-auto",            className,          )}        >          <CopilotLiveRegion message={api.announcer.message} />          <button            type="button"            aria-label="Reopen the assistant"            onClick={() => api.setCollapsed(false)}            className="grid h-12 w-12 place-items-center rounded-full bg-[var(--zb-accent)] text-[var(--zb-text-on-accent)] shadow-lg"          >            <SparkIcon />          </button>        </div>      </CopilotRoot>    );  }  return (    <CopilotRoot api={api}>      <div        data-zb-copilot=""        data-zb-status={api.state.status}        className={cn(          "z-[900] flex flex-col gap-2",          // The dock centres; the thread docks to a side. A dock is a summons          // bar and belongs where the eye already is, but a thread is something          // you read *against* the record — centred, it sits on top of the          // chart being discussed, and the first thing anyone does is drag it          // out of the way.          threadOpen ? "items-start" : "items-center",          anchor !== "inline" &&            "fixed inset-x-0 bottom-0 px-4 pb-[max(1rem,env(safe-area-inset-bottom))]",          anchor === "bottom-right" && "items-end",          "pointer-events-none [&>*]:pointer-events-auto",          className,        )}      >        <CopilotLiveRegion message={api.announcer.message} />        {threadOpen ? (          <CopilotPanel            api={api}            {...(onInsert ? { onInsert } : {})}            {...(onRiskProtocol ? { onRiskProtocol } : {})}          />        ) : (          <CopilotDockRegion className="flex w-full max-w-2xl flex-col gap-1.5">            {trayOpen ? (              <div className="rounded-2xl border border-[var(--zb-border)] bg-[var(--zb-surface)] p-3 shadow-lg">                <div role="group" aria-label="Change mode" className="mb-2 flex flex-wrap gap-1.5">                  {api.modes.map((mode) => (                    <button                      key={mode.id}                      type="button"                      aria-pressed={mode.id === api.mode.id}                      onClick={() => api.setMode(mode.id)}                      className={cn(                        "min-h-6 rounded-lg px-2.5 py-1 text-sm",                        mode.id === api.mode.id                          ? "bg-[var(--zb-accent)] text-[var(--zb-text-on-accent)]"                          : "border border-[var(--zb-border)] text-[var(--zb-text)]",                      )}                    >                      {mode.label}                    </button>                  ))}                </div>                <ul className="m-0 list-none p-0">                  {api.mode.suggestions.map((suggestion) => (                    <li                      key={suggestion.id}                      className="border-t border-[var(--zb-border)] first:border-t-0"                    >                      <button                        type="button"                        className="w-full px-1 py-2 text-start"                        onClick={() => {                          api.setDraft(suggestion.question ?? suggestion.label);                          setTrayOpen(false);                        }}                      >                        <span className="block text-sm text-[var(--zb-text)]">                          {suggestion.label}                        </span>                        {/* The scope, shown at the point of choice. */}                        {suggestion.reads ? (                          <span className="block text-xs text-[var(--zb-text-muted)]">                            {suggestion.reads}                          </span>                        ) : null}                      </button>                    </li>                  ))}                </ul>              </div>            ) : null}            {menu.open ? (              <ul                {...menu.listProps}                aria-label="Shortcuts"                className="m-0 list-none rounded-2xl border border-[var(--zb-border)] bg-[var(--zb-surface)] p-2 shadow-lg"              >                {menu.items.map((item, index) => (                  <li                    key={item.id}                    {...menu.optionProps(index)}                    className="cursor-pointer rounded-lg px-2 py-1.5 text-sm aria-selected:bg-[color-mix(in_oklab,var(--zb-accent)_12%,transparent)]"                  >                    {item.label}                  </li>                ))}              </ul>            ) : null}            <ScopeStrip api={api} onChange={() => setTrayOpen(true)} />            <div className="flex items-center gap-1.5 rounded-2xl border border-[var(--zb-border)] bg-[var(--zb-surface)] px-2 py-1.5 shadow-[0_1px_2px_rgb(0_0_0/0.05),0_12px_32px_-14px_rgb(0_0_0/0.22)]">              {/* The square glyph. Also the "/" affordance, so the shortcut                menu has a visible way in for anyone who never learns the key. */}              <button                type="button"                aria-label="Shortcuts"                onClick={() => api.setDraft(api.draft.startsWith("/") ? api.draft : "/")}                className="grid size-8 shrink-0 place-items-center rounded-[10px] border border-[var(--zb-border)] bg-[var(--zb-bg-subtle)] text-base text-[var(--zb-text-muted)]"              >                <ShortcutIcon />              </button>              <input                ref={fieldRef}                value={api.draft}                aria-label="Clinical assistant"                placeholder={                  api.mode.reads.length > 0 ? "Ask about this patient…" : "Search the evidence…"                }                onChange={(event) => api.setDraft(event.target.value)}                onKeyDown={(event) => {                  menu.inputProps.onKeyDown(event);                  if (event.key === "Enter" && !menu.open) {                    event.preventDefault();                    void api.submit();                  }                }}                role={menu.inputProps.role}                aria-expanded={menu.inputProps["aria-expanded"]}                aria-controls={menu.inputProps["aria-controls"]}                aria-autocomplete={menu.inputProps["aria-autocomplete"]}                {...(menu.inputProps["aria-activedescendant"]                  ? { "aria-activedescendant": menu.inputProps["aria-activedescendant"] }                  : {})}                className="min-w-0 flex-1 bg-transparent px-1 text-[var(--zb-text)] outline-none placeholder:text-[var(--zb-text-muted)] [@media(pointer:coarse)]:text-[length:max(16px,1em)]"              />              {(() => {                const ModeGlyph = modeIcon(api.mode.id);                return (                  <button                    type="button"                    aria-expanded={trayOpen}                    onClick={() => setTrayOpen((open) => !open)}                    className="inline-flex min-h-8 shrink-0 items-center gap-1.5 rounded-[10px] border border-[var(--zb-border)] bg-[var(--zb-bg-subtle)] px-2.5 py-1 text-sm text-[var(--zb-text)]"                  >                    <ModeGlyph className="text-[var(--zb-text-muted)]" />                    {api.mode.label}                  </button>                );              })()}              {api.dictation.active ? (                <button                  type="button"                  aria-label="Stop dictation"                  onClick={() => api.dictation.stop()}                  className="min-h-6 min-w-6 rounded-lg px-2 text-[var(--zb-status-normal)]"                >                  <span className="inline-flex gap-[2px]" aria-hidden="true">                    <i className="block h-3 w-[2px] animate-pulse rounded bg-current" />                    <i className="block h-2 w-[2px] animate-pulse rounded bg-current [animation-delay:120ms]" />                    <i className="block h-3.5 w-[2px] animate-pulse rounded bg-current [animation-delay:240ms]" />                  </span>                  {/* Colour and motion alone would be a 1.4.1 failure, and in a                    consulting room it is also a privacy problem. */}                  <span className="sr-only">Microphone is live</span>                </button>              ) : (                <button                  type="button"                  aria-label="Start dictation"                  onClick={api.dictation.start}                  disabled={api.state.status === "streaming" || api.state.status === "submitting"}                  className="grid size-8 shrink-0 place-items-center rounded-[10px] text-base text-[var(--zb-text-muted)] disabled:opacity-40"                >                  <MicIcon />                </button>              )}              {api.state.status === "streaming" || api.state.status === "submitting" ? (                <button                  type="button"                  onClick={api.stop}                  className="grid size-8 shrink-0 place-items-center rounded-[10px] border border-[var(--zb-border)] text-base"                >                  <StopIcon />                  <span className="sr-only">Stop</span>                </button>              ) : (                <button                  type="button"                  disabled={!api.canSubmit}                  onClick={() => void api.submit()}                  className="grid size-8 shrink-0 place-items-center rounded-[10px] bg-[var(--zb-accent)] text-base text-[var(--zb-text-on-accent)] disabled:opacity-40"                >                  <SendIcon />                  <span className="sr-only">Send</span>                </button>              )}            </div>            {/* Never dismissible. */}            <p className="m-0 text-center text-xs text-[var(--zb-text-muted)]">              Medical knowledge only. Not for autonomous decision making. Check sources and use your              clinical judgement.            </p>          </CopilotDockRegion>        )}      </div>    </CopilotRoot>  );}/** * The scope strip. * * The highest-value element on the surface, and the one that is easiest to * leave out. A clinician needs to know what the assistant can see *before* * they trust a summary, and — the part everyone omits — what it could not see. * A summary that silently excludes a protected record has created a false * belief that would not exist if the tool did not exist. */function ScopeStrip({  api,  onChange,}: {  api: ReturnType<typeof useCopilot>;  onChange: () => void;}) {  const { scope } = api;  // "Change" opens the mode tray rather than a scope picker of its own. Scope  // *is* the mode's `reads` contract, so a second control that edited it  // independently would let the two disagree — and the strip is the thing a  // clinician is being asked to trust.  const change = (    <button      type="button"      onClick={onChange}      className="rounded-full font-medium text-[var(--zb-accent)] underline underline-offset-2"    >      Change    </button>  );  // A pill that sizes to its own text, not a full-width bar. The strip is a  // statement about *this* answer, and a bar spanning the dock reads as chrome —  // which is exactly the thing people stop seeing.  const shell =    // `rounded-2xl` rather than a full pill: the category list is host-supplied    // and a pill that wraps to three lines reads as a rendering fault.    "m-0 mx-auto flex w-fit max-w-full flex-wrap items-center justify-center gap-x-2 gap-y-1 rounded-2xl border border-[var(--zb-border)] bg-[var(--zb-surface)] px-3 py-1 text-xs text-[var(--zb-text-muted)] shadow-[0_1px_2px_rgb(0_0_0/0.04)]";  if (scope.categories.length === 0) {    return (      <p className={shell}>        <LockIcon className="shrink-0" />        No patient data is being used        {change}      </p>    );  }  return (    <p className={shell}>      <PersonIcon className="shrink-0" />      <span>        Reading{" "}        <strong className="font-semibold text-[var(--zb-text)]">          {scope.subject?.display ?? scope.subject?.reference}        </strong>        {" · "}        {scope.categories.join(", ")}      </span>      {scope.showWithheld ? (        <span className="inline-flex items-center gap-1 rounded-full bg-[color-mix(in_oklab,var(--zb-status-high)_20%,transparent)] px-2 py-px text-[var(--zb-text)]">          <LockIcon className="shrink-0" />          {scope.withheldCount} record{scope.withheldCount === 1 ? "" : "s"} withheld        </span>      ) : null}      {change}    </p>  );}/** * A retrieved passage with the supporting clause marked. * * This is the difference between a citation and a verification. A source title * tells a clinician where an answer came from; it does not tell them the model * read that source correctly, and checking costs a tab, a search and a scan of * a forty-page guideline. Nobody pays that during a consultation — which is the * mechanism behind automation bias, an effort asymmetry rather than a character * flaw. * * Marking the clause makes the check a glance. `<mark>` rather than a styled * span because the semantics are exactly right and it survives forced-colours * mode, where a background tint would vanish and take the meaning with it. * * Offsets are host-supplied and clamped rather than trusted: a provider that * returns a stale range should mark the wrong words at worst, never throw away * the passage. */export interface HighlightedPassageProps {  /** The source passage, verbatim. Never re-flowed or truncated — a citation the reader cannot check against the original is not a citation. */  text: string;  /**   * Character offsets of the span the answer drew on, as `[start, end]`.   *   * Out-of-range offsets highlight nothing rather than throwing: a citation   * that points past the end of its own source is a bug in the caller, and   * silently marking the wrong sentence would be worse than marking none.   */  at?: readonly [number, number];}export function HighlightedPassage({ text, at }: HighlightedPassageProps) {  if (!at) return <>{text}</>;  const start = Math.max(0, Math.min(at[0], text.length));  const end = Math.max(start, Math.min(at[1], text.length));  if (start === end) return <>{text}</>;  return (    <>      {text.slice(0, start)}      <mark className="rounded bg-[color-mix(in_oklab,var(--zb-status-high)_28%,transparent)] px-0.5 text-[var(--zb-text)]">        {text.slice(start, end)}      </mark>      {text.slice(end)}    </>  );}function CopilotPanel({  api,  onInsert,  onRiskProtocol,}: {  api: ReturnType<typeof useCopilot>;  onInsert?: (text: string) => void;  onRiskProtocol?: () => void;}) {  const modeSelectRef = React.useRef<HTMLSelectElement>(null);  return (    <section      aria-label="Assistant conversation"      // A column, not a banner. The record stays readable beside it, which is      // the whole reason a clinician opened a thread about that record.      className="flex max-h-[min(78dvh,44rem)] w-full max-w-md flex-col overflow-hidden rounded-2xl border border-[var(--zb-border)] bg-[var(--zb-surface)] shadow-[0_1px_2px_rgb(0_0_0/0.05),0_16px_40px_-16px_rgb(0_0_0/0.28)]"    >      <header className="flex items-center justify-between border-b border-[var(--zb-border)] px-3 py-2">        {api.threads.length > 1 ? (          <label className="flex min-w-0 items-center gap-1">            <span className="sr-only">Switch conversation</span>            <select              value={api.activeThreadId}              onChange={(event) => api.switchThread(event.target.value)}              className="min-w-0 max-w-[18rem] truncate bg-transparent text-sm font-semibold text-[var(--zb-text)] outline-none"            >              {api.threads.map((thread) => (                <option key={thread.id} value={thread.id}>                  {thread.title}                </option>              ))}            </select>            <ChevronDownIcon />          </label>        ) : (          <h2 className="m-0 truncate text-sm font-semibold text-[var(--zb-text)]">            {api.threads[0]?.title ?? api.mode.label}          </h2>        )}        <button          type="button"          onClick={api.newThread}          className="min-h-6 min-w-6 shrink-0 rounded-lg border border-[var(--zb-border)] px-2 py-0.5 text-xs"        >          <NewChatIcon />          <span className="sr-only">New chat</span>        </button>        <button          type="button"          aria-label="Put the assistant away"          onClick={() => api.setCollapsed(true)}          className="min-h-6 min-w-6 rounded-lg px-1"        >          <CloseIcon />        </button>      </header>      <div className="flex flex-1 flex-col gap-3 overflow-y-auto p-3">        {api.state.messages.map((message) =>          message.role === "clinician" ? (            <p              key={message.id}              className="m-0 max-w-[80%] self-end rounded-xl bg-[color-mix(in_oklab,var(--zb-accent)_12%,transparent)] px-3 py-1.5 text-sm text-[var(--zb-text)]"            >              {message.text}            </p>          ) : (            <article key={message.id} className="flex flex-col gap-1.5">              {/* Collapsed by design: a visible chain of thought reads as                  evidence to a clinician, and it is not evidence. */}              {message.answer?.reasoning ? (                <details className="text-xs text-[var(--zb-text-muted)]">                  <summary className="flex cursor-pointer items-center gap-1 text-[0.65rem] uppercase tracking-wide [&::-webkit-details-marker]:hidden">                    <ThoughtIcon /> Reasoning                  </summary>                  <p className="m-0 mt-1 border-s-2 border-[var(--zb-border)] ps-2 leading-relaxed">                    {message.answer.reasoning}                  </p>                </details>              ) : null}              {message.answer ? (                <span                  data-register={message.answer.register}                  className={cn(                    "self-start rounded px-1.5 py-0.5 text-[0.65rem] uppercase tracking-wide",                    // The status token's own paired ground, not a 15% mix of                    // the hue over whatever is behind it: on the docs panel the                    // mix measured 4.43:1 for this 10px label. The `-bg` tokens                    // are what the token build gates each status colour against.                    message.answer.register === "grounded" &&                      "bg-[var(--zb-status-normal-bg)] text-[var(--zb-status-normal)]",                    message.answer.register === "general" &&                      "bg-[var(--zb-status-high-bg)] text-[var(--zb-status-high)]",                    message.answer.register === "declined" &&                      "bg-[var(--zb-bg-subtle)] text-[var(--zb-text-muted)]",                  )}                >                  {message.answer.register === "grounded"                    ? `Grounded · ${message.answer.sources.size}`                    : message.answer.register === "general"                      ? "General knowledge"                      : "Declined"}                </span>              ) : null}              {message.answer ? (                <div                  data-register={message.answer.register}                  className={cn(                    "text-sm leading-relaxed text-[var(--zb-text)]",                    message.answer.register === "general" &&                      "border-s-[3px] border-[var(--zb-status-high)] ps-2.5",                  )}                >                  {segmentAnswer(message.answer).map((segment, index) => (                    <span                      key={index}                      className={cn(                        segment.uncited &&                          "underline decoration-[var(--zb-status-high)] decoration-wavy underline-offset-4",                      )}                      {...(segment.uncited                        ? {                            title:                              "No source was cited for this sentence. Verify it before acting.",                          }                        : {})}                    >                      {segment.text}                      {segment.markers.map((marker) => (                        <button                          key={marker}                          type="button"                          onClick={() => api.openSourcesFor(message.id)}                          aria-label={citationLabel(marker, message.answer?.sources.get(marker))}                          className="ms-px align-super rounded bg-[color-mix(in_oklab,var(--zb-accent)_15%,transparent)] px-1 text-[0.65em]"                        >                          {marker}                        </button>                      ))}                    </span>                  ))}                </div>              ) : null}              <div className="flex flex-wrap items-center gap-2 text-xs">                <button                  type="button"                  aria-label="Helpful"                  onClick={() => api.sendFeedbackFor(message.id, "up")}                  className="min-h-6 min-w-6 px-1"                >                  <ThumbUpIcon />                </button>                <button                  type="button"                  aria-label="Not helpful"                  // No reason picker here, so record the thumbs-down at once.                  onClick={() =>                    api.sendFeedbackFor(message.id, "down", undefined, { askReason: false })                  }                  className="min-h-6 min-w-6 px-1"                >                  <ThumbDownIcon />                </button>                <button                  type="button"                  aria-label="Copy"                  onClick={() => void navigator.clipboard?.writeText(message.text)}                  className="min-h-6 min-w-6 px-1"                >                  <CopyIcon />                </button>                <button                  type="button"                  // This answer's sources, not the latest answer's.                  onClick={() => api.openSourcesFor(message.id)}                  className="inline-flex min-h-6 items-center gap-1 rounded-lg border border-[var(--zb-border)] px-2 py-0.5"                >                  <VerifyIcon />                  Show sources                </button>                {onInsert ? (                  <button                    type="button"                    aria-label="Insert into note"                    onClick={() => onInsert(message.text)}                    className="min-h-6 min-w-6 px-1"                  >                    <InsertIcon />                  </button>                ) : null}              </div>            </article>          ),        )}        {api.state.status === "streaming" && api.state.current ? (          <CopilotAnswerRegion busy className="text-sm leading-relaxed text-[var(--zb-text)]">            {api.state.current.text}          </CopilotAnswerRegion>        ) : null}        {api.state.status === "crisis" ? (          <section            role="alert"            className="rounded-xl border border-[color-mix(in_oklab,var(--zb-status-critical)_45%,transparent)] border-s-[3px] border-s-[var(--zb-status-critical)] bg-[color-mix(in_oklab,var(--zb-status-critical)_6%,transparent)] p-3"          >            <h3 className="m-0 mb-1 text-sm font-semibold text-[var(--zb-status-critical)]">              This needs a person, not a model            </h3>            <p className="m-0 mb-2 text-sm text-[var(--zb-text)]">              {api.state.safety.crisis.audience === "user"                ? "This assistant is not able to help with this. Please reach a person now."                : "This assistant does not answer questions about a patient in immediate danger."}            </p>            <div className="flex flex-wrap gap-2">              {onRiskProtocol ? (                <button                  type="button"                  onClick={onRiskProtocol}                  className="min-h-6 rounded-lg bg-[var(--zb-status-critical)] px-2.5 py-1 text-sm text-[var(--zb-text-on-accent)]"                >                  Open risk protocol                </button>              ) : null}              {/* Resolved by locale. 988 works in the United States and nowhere                  else, so hardcoding it ships a bug to every other market. */}              {api.crisisLines.map((line) =>                line.number ? (                  <a                    key={line.label}                    href={`tel:${line.number}`}                    className="min-h-6 rounded-lg border border-[var(--zb-border)] px-2.5 py-1 text-sm"                  >                    {line.label} · {line.number}                  </a>                ) : (                  <span                    key={line.label}                    className="px-2.5 py-1 text-sm text-[var(--zb-text-muted)]"                  >                    {line.label}                  </span>                ),              )}            </div>          </section>        ) : null}        {api.state.status === "refused" && api.state.error ? (          <div role="status" className="rounded-lg bg-[var(--zb-bg-subtle)] p-2.5 text-sm">            <strong className="text-[var(--zb-text)]">Outside what this assistant answers</strong>            <p className="m-0 text-[var(--zb-text-muted)]">{api.state.error.message}</p>          </div>        ) : null}        {api.state.status === "error" && api.state.error ? (          <div            role="alert"            className="rounded-lg bg-[color-mix(in_oklab,var(--zb-status-critical)_8%,transparent)] p-2.5 text-sm"          >            <strong className="text-[var(--zb-text)]">The assistant could not answer</strong>            <p className="m-0 text-[var(--zb-text-muted)]">{api.state.error.message}</p>            {api.state.error.retryable && api.canRetry ? (              <button                type="button"                onClick={() => void api.retry()}                className="min-h-6 rounded-lg border border-[var(--zb-border)] px-2 py-0.5"              >                Try again              </button>            ) : null}          </div>        ) : null}      </div>      {/* The verification surface. The passage itself, not a link — following a          link costs a tab and a search, and people accept rather than pay it. */}      {api.sourcesOpen ? (        <section          aria-label="Basis of this answer"          className="max-h-72 overflow-y-auto border-t border-[var(--zb-border)] p-3"        >          <div className="mb-1 flex items-center justify-between">            <strong className="text-sm text-[var(--zb-text)]">Basis of this answer</strong>            <button              type="button"              aria-label="Close"              onClick={api.closeSources}              className="min-h-6 px-1"            >              ✕            </button>          </div>          <ol className="m-0 list-none p-0">            {/* Numbered by citation marker, so "2" here is "2" in the answer. */}            {api.citations.map(({ marker, source }) => (              <li                key={source.id}                className="border-t border-[var(--zb-border)] pt-2 first:border-t-0 first:pt-0"              >                <div className="flex items-baseline gap-1.5 text-sm">                  <span                    aria-hidden="true"                    className="rounded bg-[color-mix(in_oklab,var(--zb-accent)_15%,transparent)] px-1 text-xs"                  >                    {marker}                  </span>                  {source.url ? (                    <a                      href={source.url}                      target="_blank"                      rel="noreferrer noopener"                      className="text-[var(--zb-text)]"                    >                      {source.title}                    </a>                  ) : (                    <strong className="text-[var(--zb-text)]">{source.title}</strong>                  )}                </div>                <blockquote className="m-0 mt-1 border-s-2 border-[var(--zb-border)] ps-2 text-xs text-[var(--zb-text-muted)]">                  <HighlightedPassage                    text={source.passage}                    {...(source.highlight ? { at: source.highlight } : {})}                  />                </blockquote>                <p className="m-0 mt-1 text-[0.7rem] tabular-nums text-[var(--zb-text-muted)]">                  Retrieved {source.retrievedAt.slice(0, 10)}                  {source.version ? ` · ${source.version}` : ""} · {source.kind}                  {typeof source.score === "number" ? ` · match ${source.score.toFixed(2)}` : ""}                </p>              </li>            ))}          </ol>        </section>      ) : null}      <ScopeStrip api={api} onChange={() => modeSelectRef.current?.focus()} />      {/* The panel's own composer. Once the thread is open the dock is the wrong          place to type: the answer being followed up on is up here, and making          someone travel back down to ask about it is the kind of small friction          that ends in a copy-paste into a chat window with none of this          component's guarantees. */}      <div className="border-t border-[var(--zb-border)] px-3 py-2">        <input          value={api.draft}          aria-label="Ask a follow-up…"          placeholder="Ask a follow-up…"          disabled={api.state.status === "crisis"}          onChange={(event) => api.setDraft(event.target.value)}          onKeyDown={(event) => {            if (event.key === "Enter") {              event.preventDefault();              void api.submit();            }          }}          className="w-full bg-transparent text-sm text-[var(--zb-text)] outline-none [@media(pointer:coarse)]:text-[length:max(16px,1em)]"        />        <div className="mt-1 flex items-center gap-1">          <button            type="button"            aria-label="Shortcuts"            onClick={() => api.setDraft(api.draft.startsWith("/") ? api.draft : "/")}            className="min-h-6 min-w-6 rounded-lg px-1"          >            <ShortcutIcon />          </button>          {/* Mode has to survive the dock being hidden, or opening a thread              would quietly remove the control that decides what the model may              read — the one thing that must never be implicit. */}          <label className="inline-flex items-center gap-1 rounded-lg border border-[var(--zb-border)] px-2 py-0.5 text-xs">            <span className="sr-only">Change mode</span>            <select              ref={modeSelectRef}              value={api.mode.id}              onChange={(event) => api.setMode(event.target.value)}              className="bg-transparent text-xs text-[var(--zb-text)] outline-none"            >              {api.modes.map((mode) => (                <option key={mode.id} value={mode.id}>                  {mode.label}                </option>              ))}            </select>          </label>          <span className="flex-1" />          {api.dictation.active ? (            <button              type="button"              aria-label="Stop dictation"              onClick={() => api.dictation.stop()}              className="min-h-6 min-w-6 rounded-lg px-1 text-[var(--zb-status-normal)]"            >              <MicIcon />              <span className="sr-only">Microphone is live</span>            </button>          ) : (            <button              type="button"              aria-label="Start dictation"              onClick={api.dictation.start}              // Disabled mid-answer: dictating would leave `streaming` and remove Stop.              disabled={api.state.status === "streaming" || api.state.status === "submitting"}              className="min-h-6 min-w-6 rounded-lg px-1 disabled:opacity-40"            >              <MicIcon />            </button>          )}          {api.state.status === "streaming" || api.state.status === "submitting" ? (            <button              type="button"              aria-label="Stop"              onClick={api.stop}              className="min-h-6 min-w-6 rounded-lg border border-[var(--zb-border)] px-1"            >              <StopIcon />            </button>          ) : (            <button              type="button"              aria-label="Send"              disabled={!api.canSubmit}              onClick={() => void api.submit()}              className="min-h-6 min-w-6 rounded-lg bg-[var(--zb-accent)] px-1 text-[var(--zb-text-on-accent)] disabled:opacity-40"            >              <SendIcon />            </button>          )}        </div>      </div>      <p className="m-0 border-t border-[var(--zb-border)] px-3 py-2 text-center text-xs text-[var(--zb-text-muted)]">        Medical knowledge only. Not for autonomous decision making. Check sources and use your        clinical judgement.      </p>    </section>  );}