Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

55.1 KB
Raw Blame
import { ChatCommandPanel, useChatCommands } from "./ChatCommands";
import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
import {
  ArrowUp,
  AudioLines,
  Bot,
  Brain,
  FolderCode,
  FolderOpen,
  Gauge,
  Hammer,
  NotebookPen,
  Paperclip,
  ShieldCheck,
  Square,
} from "lucide-react";

import { Combobox, type ComboboxGroup, type ComboboxOption } from "@/components/ui/combobox";
import { AgentMark } from "@/components/agentic/AgentMark";
import { ProviderLogo } from "@/components/providers/ProviderLogo";
import { useAgentChat, useAgentChatApi } from "@/components/agentchat/AgentChatStoreContext";
import type { ProviderOption } from "@/store/agentChat";
import { useEventStore } from "@/store/events";
import { folderLeaf } from "@/lib/folderPath";
import { effortLadder, snapEffort } from "@/lib/effortLadder";
import { rankModels } from "@/lib/modelRanking";
import { isApiRunner, pickAgentChatFolder, type CuratedModel } from "@/lib/agentChatApi";
import { offeredModels, useSavedHiddenModels } from "@/lib/agentProviderPrefs";
import { runningTurn } from "@/components/agentchat/reduce";
import { permissionModeIcon } from "@/components/agentchat/permissionIcons";
import { useComposerDictation } from "@/components/agentchat/useComposerDictation";
import { releaseHeldFiles, restoreHeldFiles, useChatAttachments } from "@/components/agentchat/useChatAttachments";
import { usePasteRescue } from "@/components/agentchat/usePasteRescue";
import { useComposerTypeahead } from "@/components/agentchat/useComposerTypeahead";
import { ComposerTypeahead } from "@/components/agentchat/ComposerTypeahead";
import { ChatAttachmentStrip } from "@/components/agentchat/ChatAttachmentStrip";
import { DictationButton } from "@/components/agentchat/DictationButton";
import { ComposerAddMenu } from "@/components/agentchat/ComposerAddMenu";
import { ComposerChipField, type ComposerChipFieldHandle } from "@/components/agentchat/ComposerChipField";
import {
  COMPOSER_CONTROL_CLASS,
  ComposerBrainPicker,
  type BrainSection,
} from "@/components/agentchat/ComposerBrainPicker";
import type { ToolChoice } from "@/components/agentchat/toolChoices";
import { captureComposerDraftTarget, readComposerDraft, useComposerDraft, writeComposerDraft } from "./composerDrafts";
import { useAppshotClaim } from "@/components/agentchat/useAppshotClaim";
import { PetMark } from "@/components/pets/PetMark";
import { useVoiceModeSwitch } from "@/components/home/assistantStatus";
import { fill, useT } from "@/i18n";
import { attachFilePasteBridge } from "@/lib/editActions";
import { cn } from "@/lib/utils";

/**
 * The composer — the front page's one control for talking to Jarvis by
 * keyboard. What is typed here goes to the same assistant the microphone
 * reaches; the picks under the text box decide what Jarvis runs on for this
 * chat.
 *
 * One card (maintainer sketch, 2026-08-23): the text box on top and, under
 * it, the four picks that decide who answers and how —
 *
 *   Provider · Model · Reasoning effort │ Permission mode · Build | Plan
 *
 * — then dictation and Send on the right. The picks are the backend's
 * catalog for this surface, never a list typed here: the provider column is
 * what that surface may be seated on (connected ones pickable, the rest
 * greyed with a "connect" hint), the model column is the provider's own list
 * (live from its catalog route, or the curated list for a CLI), the effort
 * ladder and the permission ladder are whatever the catalog delivers for
 * that row (dodaios/agent_chat/effort.py, permissions.py) — on the front
 * page one unified ladder for every provider (ask / accept-edits / plan /
 * bypass), each step wearing its stance's glyph (permissionIcons.ts).
 * Build | Plan is the permission ladder's `plan` entry drawn as a switch,
 * shown only when the ladder has one.
 *
 * Which providers a surface offers is the backend's call, not a filter here
 * (dodaios/agent_chat/catalog.py — `rows_for`): the front page's chat lists
 * only providers whose own API Jarvis' harness drives, so a turn there is
 * one `TurnOverride` on one endpoint behind one key (maintainer,
 * 2026-08-26). The vendor CLIs stay where a vendor CLI is the point — the
 * IDE's chat mode, a coding agent in a folder.
 *
 * The provider list is grouped the way the Agents tab groups its cards — by
 * what stands behind a row, never by whether it is connected: a coding CLI
 * signed in with a subscription, a provider's own API behind a key, or a
 * server on this machine with no account at all (maintainer, 2026-08-23: a
 * person must SEE which rows are the CLIs and which are API keys), and it
 * lists only the providers the Agents tab has connected — a fresh install
 * with nothing connected sees every row greyed with a "connect" hint instead.
 *
 * A pick applies to the open session at once (the backend patches it and
 * the next turn runs on it); with no session open it seeds the next one.
 *
 * The front page (`jarvis` surface) draws the same controls as one quiet
 * row under the text (2026-10-05): a round "+", then three labelled picks
 * split by hairlines — the brain (provider mark and model, one menu with a
 * provider rail and favourites), the effort word, the permission stance as
 * glyph and word — and on the right dictation and one round blue button
 * that is voice mode while the box is empty, Send once there is something
 * to send, and Stop while a turn runs. The IDE keeps the labelled pills:
 * there a folder, a stance and a model are things a person checks before
 * every run.
 */
export function AgentComposer({ autoFocus = false }: { autoFocus?: boolean }) {
  const t = useT();
  const store = useAgentChatApi();
  const activeSessionId = useAgentChat((s) => s.activeSessionId);
  const messageDraft = useComposerDraft(store, activeSessionId);
  const { text: value, choices: selectedTools } = messageDraft;
  const textareaRef = useRef<HTMLTextAreaElement>(null);
  const fieldRef = useRef<ComposerChipFieldHandle>(null);
  const setSelectedTools = useCallback((choices: ToolChoice[]) => {
    writeComposerDraft(store, activeSessionId, { ...readComposerDraft(store, activeSessionId), choices });
  }, [store, activeSessionId]);
  const setValueState = useCallback((next: string | ((current: string) => string)) => {
    const current = readComposerDraft(store, activeSessionId);
    writeComposerDraft(store, activeSessionId, {
      ...current,
      text: typeof next === "function" ? next(current.text) : next,
    });
  }, [store, activeSessionId]);
  const setValue = useCallback(
    (next: string | ((current: string) => string)) => {
      const current = readComposerDraft(store, activeSessionId).text;
      const text = typeof next === "function" ? next(current) : next;
      setValueState(text);
      fieldRef.current?.setText(text);
    },
    [store, activeSessionId, setValueState],
  );
  useLayoutEffect(() => {
    const field = fieldRef.current;
    const current = field?.getDraft();
    if (current && (current.text !== messageDraft.text || JSON.stringify(current.choices) !== JSON.stringify(messageDraft.choices))) {
      field?.hydrate(messageDraft.text, messageDraft.choices);
    }
  }, [store, activeSessionId, messageDraft]);
  const connected = useEventStore((s) => s.connected);
  const wsWarming = useEventStore((s) => s.wsWarming);
  const assistantName = useEventStore((s) => s.assistantName);
  const setActiveSection = useEventStore((s) => s.setActiveSection);
  const pushToast = useEventStore((s) => s.pushToast);
  const [restarting, setRestarting] = useState(false);

  const surface = useAgentChat((s) => s.surface);
  const catalog = useAgentChat((s) => s.catalog);
  const catalogStale = useAgentChat((s) => s.catalogStale);
  const catalogError = useAgentChat((s) => s.catalogError);
  const backendOutdated = useAgentChat((s) => s.backendOutdated);
  const connections = useAgentChat((s) => s.connections);
  const liveModels = useAgentChat((s) => s.liveModels);
  const savedHidden = useSavedHiddenModels((s) => s.hidden);
  const health = useAgentChat((s) => s.health);
  const draft = useAgentChat((s) => s.draft);
  const timeline = useAgentChat((s) => s.timeline);
  const busy = useAgentChat((s) => s.busy);
  const lastError = useAgentChat((s) => s.lastError);
  // The picks this chat cannot take, with the sentence that says why (a pane's
  // CLI chose its provider when it started; a pick it has no typed command
  // for waits for a new chat). A disabled pill that says so is the honest
  // control; one that swallows the click was the reported bug (2026-08-27).
  const locks = useAgentChat((s) => s.locks);
  const setDraft = useAgentChat((s) => s.setDraft);
  const setPlan = useAgentChat((s) => s.setPlan);
  const send = useAgentChat((s) => s.send);
  const cancel = useAgentChat((s) => s.cancel);
  const loadCatalog = useAgentChat((s) => s.loadCatalog);
  const loadHealth = useAgentChat((s) => s.loadHealth);
  // Not an aliased narrowing: the IDE branch below still names the surface.
  const minimal: boolean = String(surface) === "jarvis";
  const voiceMode = useVoiceModeSwitch();

  // A copy from before paints the picks at once; the fresh read still runs.
  useEffect(() => {
    if (!catalog || catalogStale) void loadCatalog();
  }, [catalog, catalogStale, loadCatalog]);

  // A real call changed a seat's recorded health, or a key was saved or
  // removed somewhere: re-read the dots (a plain read — the server never
  // probes a provider for them). Debounced, since one turn can report
  // several providers.
  useEffect(() => {
    let timer: number | undefined;
    const reread = () => {
      window.clearTimeout(timer);
      timer = window.setTimeout(() => void loadHealth(), 400);
    };
    const events = ["jarvis:provider-health-changed", "jarvis:secret-configured"];
    events.forEach((e) => window.addEventListener(e, reread));
    return () => {
      window.clearTimeout(timer);
      events.forEach((e) => window.removeEventListener(e, reread));
    };
  }, [loadHealth]);

  const providers = useMemo<ProviderOption[]>(
    () => store.getState().providerOptions(),
    // Recompute when either half of the join changes.
    // eslint-disable-next-line react-hooks/exhaustive-deps
    [catalog, connections, store],
  );
  const provider = providers.find((p) => p.id === draft.provider) ?? null;

  const {
    dictating,
    stop: stopDictation,
    toggle: toggleDictation,
    stopAndSend: stopDictationAndSend,
  } = useComposerDictation(setValue, () => void onSend());

  // Files going in with this message. Kept per chat store and session, like
  // the typed text: they belong to the sentence being typed, a chat opened
  // elsewhere must not inherit them, and a section switch must not drop them.
  const [attachError, setAttachError] = useState("");
  const onAttachProblem = useCallback(
    (message: string, severity: "warning" | "error") => {
      setAttachError(severity === "warning" ? t("agent_chat.attach_nothing") : message);
    },
    [t],
  );
  const files = useChatAttachments(
    {
      sessionId: activeSessionId,
      cwd: draft.cwd,
      provider: draft.provider,
      surface,
    },
    onAttachProblem,
    { owner: store, key: activeSessionId ?? "" },
  );
  // A shortcut appshot parked for "the next message" joins this one.
  useAppshotClaim(files.attachFiles, surface === "jarvis", activeSessionId ?? "new");
  const fileInputRef = useRef<HTMLInputElement | null>(null);
  const pasteRescue = usePasteRescue();

  // The list over the box after "/", "@" or "$" — which of the three this
  // seat honours is the catalog row's word (`typeahead`), read from the
  // runner, so a seat that would take the slash as text never gets a list.
  const cardRef = useRef<HTMLDivElement | null>(null);
  useEffect(() => {
    const card = cardRef.current;
    if (!card) return;
    return attachFilePasteBridge(card, { pastePaths: files.attachPaths });
  }, [files.attachPaths]);
  const commands = useChatCommands({ value, setValue,
    attachments: files.attachments, attachmentsBusy: files.analyzing > 0, onAttachmentsSent: files.clear,
    onModel: () => cardRef.current?.querySelector<HTMLButtonElement>('[data-testid="composer-model"]')?.click(),
  });
  const triggers = useMemo(() => provider?.typeahead ?? [], [provider]);
  const typeahead = useComposerTypeahead(textareaRef, value, setValueState, {
    surface,
    provider: draft.provider,
    cwd: draft.cwd,
    triggers,
    model: draft.model,
    stance: draft.permissionMode,
  }, fieldRef);

  const running = runningTurn(timeline) !== null;
  const live = running || busy;
  const canQueue = surface === "jarvis" || surface === "society";

  async function onSend() {
    const sessionAtSend = activeSessionId;
    if (await commands.execute(fieldRef.current?.getDraft().text.trim() ?? value.trim())) return;
    const submitted = fieldRef.current?.getDraft() ?? readComposerDraft(store, sessionAtSend);
    const content = submitted.text.trim();
    // A message may be files alone: dropping a screenshot and pressing Enter
    // is a complete gesture, and refusing it would be the composer insisting
    // on a sentence the picture already is.
    if ((!content && files.attachments.length === 0) || (running || busy) && !commands.canSteer && !canQueue) return;
    if (files.analyzing > 0) return; // a file still being read would be sent without its contents
    if (dictating) stopDictation();
    const selected = submitted.choices;
    const held = files.take();
    if (!content && held.attachments.length === 0) return;
    const draftTarget = captureComposerDraftTarget(store, sessionAtSend);
    writeComposerDraft(store, sessionAtSend, { text: "", choices: [] });
    fieldRef.current?.clear();
    setAttachError("");
    const result = selected.length ? await send(
        content,
        held.attachments,
        [...new Set(selected.map((row) => row.id))],
      ) : await send(content, held.attachments);
    if (result === "failed" || result === undefined && store.getState().lastError) {
      if (draftTarget.superseded) {
        releaseHeldFiles(held);
        pushToast("error", `${t("society.chat.message_not_sent").replace("{0}", submitted.text)}`);
        return;
      }
      const current = readComposerDraft(store, draftTarget.sessionId);
      writeComposerDraft(store, draftTarget.sessionId, {
        text: [submitted.text, current.text].filter(Boolean).join("\n"),
        choices: [...selected, ...current.choices.filter((row) => !selected.some((sent) => sent.id === row.id))],
      });
      restoreHeldFiles(store, draftTarget.sessionId ?? "", held);
    } else {
      releaseHeldFiles(held);
    }
  }

  async function onPickFolder() {
    try {
      const path = await pickAgentChatFolder(draft.cwd || undefined);
      if (path) await setDraft({ cwd: path });
    } catch {
      /* no dialog on this install — the folder stays */
    }
  }

  // The same relauncher the top bar's Restart uses. A 409 means live
  // missions would die — say so and leave the override to the top bar.
  async function onRestart() {
    if (restarting) return;
    setRestarting(true);
    try {
      const res = await fetch("/api/settings/restart-app", { method: "POST" });
      if (res.status === 409) {
        pushToast("warning", t("topbar.restart_missions_running"));
        setRestarting(false);
        return;
      }
      if (!res.ok) throw new Error(`restart-failed:${res.status}`);
      // Success schedules the shutdown; the button stays busy until the
      // window goes away.
    } catch {
      pushToast("error", t("permissions.restart_failed"));
      setRestarting(false);
    }
  }

  // ---- option lists -------------------------------------------------

  // Only what the Agents tab has set up is offered (maintainer, 2026-08-23:
  // the picker lists the providers configured there, nothing else). The one
  // exception is an install with nothing connected yet: then every row shows
  // greyed with its "connect" hint, so the list is a map and not a void. No
  // "active" marker either — that word is the voice sub-agent's, and the
  // pick here is its own thing: what you choose here is what the chat runs.
  // A provider's mark plus its live health dot — the same face in the IDE's
  // provider list and in the front page's brain pick.
  // A coding CLI installed on this machine wears its own mark; a catalog row
  // wears its provider family's. Both are real brand files — the fallback
  // either would otherwise take is a letter in a box, and a letter is not a
  // logo.
  const providerMark = useCallback(
    (p: ProviderOption) =>
      p.agentMark ? (
        <AgentMark agent={p.agentMark} label={p.label} logoUrl={p.logoUrl} variant="plain" size="sm" />
      ) : (
        <ProviderLogo providerId={p.id} label={p.label} size="sm" />
      ),
    [],
  );
  const providerIcon = useCallback(
    (p: ProviderOption) => {
      const live = liveHealthFor(p, health[p.id]);
      const broken = p.connected && live?.status === "error";
      const working = p.connected && live?.status === "ok";
      return (
        <span className="relative inline-flex shrink-0">
          {providerMark(p)}
          {(broken || working) && (
            <span
              data-testid={`provider-health-${p.id}`}
              data-health={live?.status}
              title={live?.detail || undefined}
              aria-label={
                broken
                  ? healthReasonLabel(live?.reason ?? "", t)
                  : t("agent_chat.provider_health_ok")
              }
              className={cn(
                "absolute -bottom-0.5 -right-0.5 h-2 w-2 rounded-full ring-2 ring-popover",
                broken ? "bg-destructive" : "bg-muted-foreground",
              )}
            />
          )}
        </span>
      );
    },
    [health, providerMark, t],
  );

  const providerGroups = useMemo<ComboboxGroup[]>(() => {
    const anyConnected = providers.some((p) => p.connected);
    const shown = anyConnected ? providers.filter((p) => p.connected) : providers;
    const toOption = (p: ProviderOption): ComboboxOption => {
      // A saved key is not a working one. The live sweep says which seats
      // actually answered a real request; until it lands (or on a backend
      // that has no such route) the row looks exactly as it did before.
      // A coding CLI spends a subscription login, not a key — a leaked
      // `bad_key` (the dual Claude row shares the Anthropic API id) must
      // not paint "Key rejected" on that CLI.
      const live = liveHealthFor(p, health[p.id]);
      const broken = p.connected && live?.status === "error";
      return {
        value: p.id,
        label: p.label,
        hint: !p.connected
          ? p.cli_installed === false
            ? t("agent_chat.provider_not_installed")
            : t("agent_chat.provider_connect")
          : broken
            ? healthReasonLabel(live?.reason ?? "", t)
            : undefined,
        // Still selectable when broken: a rate limit or a flat network passes,
        // and refusing the click would strand someone whose account recovered
        // a minute ago. The dot and the reason are the honest part.
        disabled: !p.connected,
        icon: providerIcon(p),
        searchText: `${p.family} ${p.runner}`,
      };
    };
    const labels: Record<ProviderKind, string> = {
      cli: t("agent_chat.group_clis"),
      api: t("agent_chat.group_api_keys"),
      local: t("agent_chat.group_local"),
    };
    const groups: ComboboxGroup[] = [];
    for (const kind of PROVIDER_KINDS) {
      const rows = shown.filter((p) => providerKind(p) === kind);
      if (rows.length)
        groups.push({
          id: kind,
          label: labels[kind],
          options: rows.map(toOption),
        });
    }
    return groups;
  }, [providers, health, providerIcon, t]);

  // The front page's single brain pick: every offered provider as a group,
  // its default first, then its models (live when loaded, else curated).
  // Each row says whose model it is on a quiet second line; the rail on the
  // panel's left picks which provider's rows are listed.
  const brainProviders = useMemo<ProviderOption[]>(() => {
    if (!minimal) return [];
    const anyConnected = providers.some((p) => p.connected);
    return anyConnected ? providers.filter((p) => p.connected) : providers;
  }, [minimal, providers]);

  const brainSections = useMemo<BrainSection[]>(
    () => brainProviders.map((p) => ({ id: p.id, family: p.family,
      access: p.keyless ? "local" : isApiRunner(p.runner) ? "api" : "subscription",
      label: p.label, icon: providerIcon(p), muted: !p.connected })),
    [brainProviders, providerIcon],
  );

  const brainGroups = useMemo<ComboboxGroup[]>(() => {
    return brainProviders.map((p) => {
      const live = liveModels[p.id];
      const listed = p.models_source === "live" && live && live.length ? live : (p.curated_models ?? []);
      // Models switched off on the API Keys page stay out; the current pick stays.
      const models = offeredModels(p, listed, p.id === draft.provider ? draft.model : "", savedHidden);
      const disabled = !p.connected;
      const hint = disabled
        ? p.cli_installed === false
          ? t("agent_chat.provider_not_installed")
          : t("agent_chat.provider_connect")
        : undefined;
      const owner = (note?: string) => (
        <span className="inline-flex min-w-0 items-center gap-1.5">
          <span className="inline-flex shrink-0 scale-[0.86]">{providerMark(p)}</span>
          <span className="truncate">{note ? `${p.label} · ${note}` : p.label}</span>
        </span>
      );
      const toOption = (m: CuratedModel): ComboboxOption => ({
        value: brainValue(p.id, m.id),
        label: m.label || m.id,
        description: owner(m.note),
        hint,
        disabled,
        searchText: `${p.label} ${m.id}`,
      });
      // Newest of each model line first; earlier versions fold away.
      const { current, older } = rankModels(models.filter((m) => m.id));
      return {
        id: p.id,
        label: p.label,
        options: [
          {
            value: brainValue(p.id, ""),
            label: t("agent_chat.model_default"),
            triggerLabel: p.label,
            description: owner(),
            hint,
            disabled,
            searchText: `${p.label} ${p.family}`,
          },
          ...current.map(toOption),
        ],
        more: older.length ? { label: t("agent_chat.older_models"), options: older.map(toOption) } : undefined,
      };
    });
  }, [brainProviders, liveModels, providerMark, t, draft.provider, draft.model, savedHidden]);

  const modelList = useMemo(() => {
    if (!provider) return [];
    const live = liveModels[provider.id];
    return provider.models_source === "live" && live && live.length
      ? live
      : (provider.curated_models ?? []);
  }, [provider, liveModels]);

  const modelGroups = useMemo<ComboboxGroup[]>(() => {
    if (!provider) return [];
    const toOption = (m: CuratedModel): ComboboxOption => ({
      value: m.id,
      label: m.label || m.id,
      hint: m.note || (m.label && m.label !== m.id ? m.id : undefined),
      searchText: m.id,
    });
    // Models switched off on the API Keys page stay out; the current pick stays.
    const { current, older } = rankModels(offeredModels(provider, modelList, draft.model, savedHidden).filter((m) => m.id));
    const options: ComboboxOption[] = [
      { value: "", label: t("agent_chat.model_default"), hint: provider.label },
      ...current.map(toOption),
    ];
    return [{
      id: "models",
      options,
      more: older.length ? { label: t("agent_chat.older_models"), options: older.map(toOption) } : undefined,
    }];
  }, [provider, modelList, t, draft.model, savedHidden]);

  // The effort ladder is the provider's, narrowed to the chosen model's own
  // levels when the catalog knows them (agy's Pro: low/high; its Claude
  // models: none at all, so the pick disappears).
  const effortLevels = useMemo<string[]>(
    () => effortLadder(provider, modelList, draft.model),
    [provider, modelList, draft.model],
  );

  useEffect(() => {
    // A model change can leave the picked effort off that model's ladder;
    // fold it the way the backend does, so the label is the level that runs.
    if (!provider || !effortLevels.some(Boolean)) return;
    const next = snapEffort(draft.effort, effortLevels, provider.default_effort);
    if (next !== draft.effort) void setDraft({ effort: next });
  }, [provider, effortLevels, draft.effort, setDraft]);

  const showEffort = Boolean(provider) && effortLevels.length > 1;

  // The front page's effort menu: a heading over the ladder, and the
  // provider's own default marked so a person can find their way back.
  const effortMenuGroups = useMemo<ComboboxGroup[]>(
    () => [
      {
        id: "effort",
        label: t("agent_chat.effort_heading"),
        options: effortLevels.map((lvl) => ({
          value: lvl,
          label: effortLabel(lvl, t),
          badge: lvl === provider?.default_effort ? t("agent_chat.default_badge") : undefined,
        })),
      },
    ],
    [effortLevels, provider, t],
  );

  const effortGroups = useMemo<ComboboxGroup[]>(
    () => [
      {
        id: "effort",
        options: effortLevels.map((lvl) => ({
          value: lvl,
          label: effortLabel(lvl, t),
        })),
      },
    ],
    [effortLevels, t],
  );

  const permissionModes = useMemo(
    () => (provider?.permission_modes ?? []).filter((m) => m.id !== "plan"),
    [provider],
  );
  const hasPlan = Boolean((provider?.permission_modes ?? []).some((m) => m.id === "plan"));
  const planOn = draft.permissionMode === "plan";
  // The front page has no Plan switch (maintainer, 2026-10-05): a chat left
  // in Plan from before would stay read-only with nothing on screen saying
  // so, so it returns to the stance Build would use.
  useEffect(() => {
    if (minimal && planOn && !locks?.permissionMode) void setPlan(false);
  }, [minimal, planOn, locks?.permissionMode, setPlan]);
  // Every mode wears its stance's glyph (permissionIcons.ts) in the list and,
  // once picked, on the pill — the Combobox draws the selected option's icon
  // on its trigger, so the column's shield below only shows for a pick the
  // catalog no longer lists.
  const permissionGroups = useMemo<ComboboxGroup[]>(
    () => [
      {
        id: "permission",
        options: permissionModes.map((m) => {
          const Icon = permissionModeIcon(m.id);
          return {
            value: m.id,
            label: m.label,
            searchText: m.description,
            icon: <Icon className="h-3.5 w-3.5 shrink-0 text-muted-foreground" aria-hidden />,
          };
        }),
      },
    ],
    [permissionModes],
  );
  // The front page's permission menu: each stance with its glyph, its name
  // and one quiet line on what it lets through.
  const permissionMenuGroups = useMemo<ComboboxGroup[]>(
    () => [
      {
        id: "permission",
        options: permissionModes.map((m) => {
          const Icon = permissionModeIcon(m.id);
          return {
            value: m.id,
            label: m.label,
            description: m.description || undefined,
            searchText: m.description,
            icon: <Icon className="h-4 w-4 shrink-0 text-muted-foreground" aria-hidden />,
          };
        }),
      },
    ],
    [permissionModes],
  );
  // While Plan is on, the permission pill shows the mode Build would return to.
  const permissionValue = planOn ? draft.buildMode || provider?.default_permission_mode || "" : draft.permissionMode;
  const permissionDescription =
    (provider?.permission_modes ?? []).find((m) => m.id === draft.permissionMode)?.description ?? "";
  const permissionLabel = permissionModes.find((m) => m.id === permissionValue)?.label ?? "";

  const canSend =
    commands.isCommand || connected &&
    (Boolean(value.trim()) || files.attachments.length > 0) &&
    files.analyzing === 0 &&
    (!running || commands.canSteer || canQueue) &&
    (!busy || commands.canSteer || canQueue) &&
    Boolean(provider?.connected);
  // Send while the mic is open ends the dictation and sends once the words
  // land, so it is live even before the box holds any text.
  const canFinishDictation = dictating && connected && (!live || canQueue) && Boolean(provider?.connected);
  const placeholder = dictating
    ? t("chats_view.dictation_listening")
    : connected
    ? t("agent_chat.placeholder")
    : wsWarming
      ? t("voice_state.booting")
      : t("voice_state.offline");

  return (
    <div
      ref={cardRef}
      data-testid="agent-composer"
      data-tour="chat-composer"
      data-dragging={files.dragging ? "true" : undefined}
      // The whole card is the drop target, not just the text box: someone
      // dragging a screenshot aims at the composer, and a target smaller than
      // the thing it looks like is a target people miss.
      {...files.dragHandlers}
      className={cn(
        "relative flex flex-col transition-[border-color,box-shadow]",
        minimal
          ? "gap-2 rounded-3xl border border-border bg-card px-3 pb-3 pt-3.5 shadow-rim focus-within:border-border-strong"
          : "gap-2 rounded-2xl border border-border-strong bg-card p-3 shadow-rim focus-within:border-primary/40",
        files.dragging && "border-primary/60",
      )}
    >
      {files.dragging && (
        <div
          data-testid="composer-drop-overlay"
          aria-hidden
          className="pointer-events-none absolute inset-0 z-10 flex items-center justify-center rounded-[inherit] border-2 border-dashed border-primary/70 bg-card text-sm font-medium text-primary"
        >
          {t("agent_chat.attach_drop_hint")}
        </div>
      )}
      <ChatCommandPanel control={commands} anchorRef={cardRef} />
      <ChatAttachmentStrip
        attachments={files.attachments}
        analyzing={files.analyzing}
        onRemove={files.remove}
        previews={files.previews}
        // The picture alone, as in the thread composer: a card with the file
        // name and a "not described" receipt crowded a strip whose only job
        // is to show what goes in with the message (maintainer, 2026-10-06).
        look="thumbnail"
      />
      <ComposerTypeahead
        anchorRef={cardRef}
        open={typeahead.open && !commands.open}
        trigger={typeahead.token?.trigger ?? null}
        items={typeahead.items}
        loading={typeahead.loading}
        activeIndex={typeahead.activeIndex}
        onHover={typeahead.setActiveIndex}
        onPick={typeahead.pick}
      />
      <ComposerChipField
        ref={fieldRef}
        placeholder={placeholder}
        disabled={!connected && !commands.enabled}
        autoFocus={autoFocus}
        onSubmit={() => (dictating ? stopDictationAndSend() : void onSend())}
        onDraftChange={(draft) => {
          writeComposerDraft(store, activeSessionId, { text: draft.text, choices: draft.choices });
        }}
        onKeyDown={(ev) => {
          if (commands.onKeyDown(ev)) return true;
          pasteRescue.onKeyDown(ev);
          if (typeahead.onKeyDown(ev)) return true;
          return false;
        }}
        onPasteFiles={(e) => {
          pasteRescue.onPaste();
          files.onPaste(e);
        }}
        className={cn("max-h-[50vh] scrollbar-jarvis", minimal ? "min-h-[64px] px-2 text-base" : "text-reading")}
      />
      {minimal ? (
        <div className="flex min-w-0 items-center gap-0.5" data-testid="composer-row">
          <ComposerAddMenu
            key={activeSessionId ?? "new"}
            compact
            anchorRef={cardRef}
            provider={draft.provider}
            model={draft.model}
            cwd={draft.cwd}
            stance={draft.permissionMode}
            selected={selectedTools}
            onChange={setSelectedTools}
            onInsert={(row) => fieldRef.current?.insertChip(row)}
            onAttach={() => fileInputRef.current?.click()}
            onFolder={() => void onPickFolder()}
            onConnect={(row) =>
              setActiveSection(
                row.category === "mcp" ? "mcps" : row.category === "skills" ? "skills" : "plugins",
              )
            }
            disabled={!connected || busy}
          />
          {/* Who answers, how hard it thinks, what it may do — three labelled
              picks in one line, split by hairlines. Each opens its own menu:
              the models with a provider rail, the effort ladder with the
              provider's default marked, the stances with one line each. */}
          <span className="flex min-w-0 items-center" data-testid="composer-brain">
            <ComposerBrainPicker
              value={brainValue(draft.provider, draft.model)}
              groups={brainGroups}
              sections={brainSections}
              currentSection={draft.provider}
              onChange={(v) => {
                const [nextProvider, nextModel] = splitBrainValue(v);
                void setDraft(
                  nextProvider !== draft.provider ? { provider: nextProvider, model: nextModel } : { model: nextModel },
                );
              }}
              ariaLabel={t("agent_chat.pick_model")}
              fallbackLabel={
                catalogError
                  ? t("agent_chat.catalog_unavailable")
                  : provider
                    ? draft.model || provider.label
                    : t("agent_chat.pick_provider")
              }
              searchPlaceholder={t("agent_chat.search_models")}
              triggerPrefix={provider ? providerIcon(provider) : undefined}
              disabled={!catalog || (Boolean(locks?.provider) && Boolean(locks?.model))}
              title={locks?.model ?? locks?.provider}
              className="min-w-0"
            />
            {showEffort && (
              <>
                <ControlSeparator />
                <span className="inline-flex shrink-0" title={locks?.effort}>
                  <Combobox
                    value={draft.effort}
                    groups={effortMenuGroups}
                    onChange={(v) => void setDraft({ effort: v })}
                    ariaLabel={t("agent_chat.pick_effort")}
                    fallbackLabel={effortLabel(draft.effort, t)}
                    disabled={Boolean(locks?.effort)}
                    testId="composer-effort"
                    triggerHint={false}
                    panelMinWidth={220}
                    className={COMPOSER_CONTROL_CLASS}
                  />
                </span>
              </>
            )}
          </span>
          {provider && permissionModes.length > 0 && (
            <>
              <ControlSeparator />
              {/* The stance as glyph and word, in the row's one grey like
                  every other pick — no stance is singled out by colour. */}
              <span
                title={locks?.permissionMode ?? [permissionLabel, permissionDescription].filter(Boolean).join(" — ")}
                className="inline-flex min-w-0 shrink"
              >
                <Combobox
                  value={permissionValue}
                  groups={permissionMenuGroups}
                  onChange={(v) => void setDraft({ permissionMode: v })}
                  ariaLabel={permissionLabel ? `${t("agent_chat.pick_permission")}: ${permissionLabel}` : t("agent_chat.pick_permission")}
                  fallbackLabel={t("agent_chat.pick_permission")}
                  disabled={Boolean(locks?.permissionMode)}
                  testId="composer-permission"
                  triggerHint={false}
                  panelMinWidth={320}
                  className={cn(
                    COMPOSER_CONTROL_CLASS,
                    "max-w-[200px] max-md:[&>span.truncate]:sr-only",
                  )}
                />
              </span>
            </>
          )}
          <span className="min-w-2 flex-1" />
          <input
            ref={fileInputRef}
            type="file"
            multiple
            className="sr-only"
            tabIndex={-1}
            onChange={(event) => {
              const picked = Array.from(event.currentTarget.files ?? []);
              event.currentTarget.value = "";
              if (picked.length > 0) files.attachFiles(picked);
            }}
          />
          <DictationButton
            dictating={dictating}
            onToggle={toggleDictation}
            disabled={!connected}
            startLabel={t("chats_view.dictation_start")}
            stopLabel={t("chats_view.dictation_stop")}
            shape="round"
          />
          {/* One round action in the signal blue: voice mode while the box is
              empty, Send once there is something to send, Stop while a turn
              runs. */}
          {live && canQueue && (
            <button type="button" onClick={() => void cancel()} aria-label={t("agent_chat.stop")}
              title={t("agent_chat.stop")} data-testid="composer-stop"
              className={cn(ROUND_ACTION_CLASS, "bg-secondary text-foreground hover:bg-secondary/80")}>
              <Square className="h-3 w-3 fill-current" />
            </button>
          )}
          {live && !canQueue && !commands.isCommand && !(commands.canSteer && value.trim()) ? (
            <button
              type="button"
              onClick={() => void cancel()}
              aria-label={t("agent_chat.stop")}
              title={t("agent_chat.stop")}
              data-testid="composer-stop"
              className={cn(ROUND_ACTION_CLASS, "bg-accent text-accent-foreground hover:bg-accent/85")}
            >
              <Square className="h-3 w-3 fill-current" />
            </button>
          ) : value.trim() || files.attachments.length > 0 || dictating || commands.isCommand ? (
            <button
              type="button"
              onClick={() => (dictating ? stopDictationAndSend() : void onSend())}
              disabled={!canSend && !canFinishDictation}
              aria-label={t("agent_chat.send")}
              data-testid="composer-send"
              className={cn(
                ROUND_ACTION_CLASS,
                canSend || canFinishDictation
                  ? "bg-accent text-accent-foreground hover:bg-accent/85"
                  : "bg-secondary text-muted-foreground",
              )}
            >
              <ArrowUp className="h-4 w-4" strokeWidth={2.5} />
            </button>
          ) : (
            <button
              type="button"
              onClick={voiceMode.enter}
              disabled={voiceMode.busy}
              aria-label={t("assistant_chat.voice_enter")}
              title={fill(t("assistant_chat.voice_enter_hint"), { name: assistantName })}
              data-testid="composer-voice-mode"
              className={cn(ROUND_ACTION_CLASS, "bg-accent text-accent-foreground hover:bg-accent/85 disabled:opacity-60")}
            >
              <AudioLines className="h-4 w-4" />
            </button>
          )}
        </div>
      ) : (
      <div className="flex flex-wrap items-center gap-1">
        {surface === "jarvis" && (
          <ComposerAddMenu
            key={activeSessionId ?? "new"}
            anchorRef={cardRef}
            provider={draft.provider}
            model={draft.model}
            cwd={draft.cwd}
            stance={draft.permissionMode}
            selected={selectedTools}
            onChange={setSelectedTools}
            onInsert={(row) => fieldRef.current?.insertChip(row)}
            onAttach={() => fileInputRef.current?.click()}
            onFolder={() => void onPickFolder()}
            onConnect={(row) =>
              setActiveSection(
                row.category === "mcp" ? "mcps" : row.category === "skills" ? "skills" : "plugins",
              )
            }
            disabled={!connected || busy}
          />
        )}
        {/*
          Who you are talking to, before what they run on. The two chats wear
          the same face, so without this the front page and the IDE's chat are
          indistinguishable at a glance — and they are not the same thing: here
          it is Jarvis with a keyboard, there it is a coding agent in a folder
          (maintainer, 2026-08-25). Not a control: what a chat IS cannot be
          switched from inside the chat.
        */}
        <span
          data-testid="composer-surface"
          data-surface={surface}
          title={
            surface === "jarvis"
              ? t("agent_chat.surface_jarvis_hint")
              : `${t("agent_chat.surface_agent_hint")}: ${draft.cwd || folderLeaf("")}`
          }
          className="inline-flex h-7 max-w-[180px] shrink-0 items-center gap-1.5 rounded-lg bg-secondary px-2 text-xs font-medium text-foreground"
        >
          {surface === "jarvis" ? (
            <PetMark size={20} />
          ) : (
            <FolderCode className="h-3.5 w-3.5 shrink-0 text-muted-foreground" aria-hidden />
          )}
          <span className="truncate">
            {surface === "jarvis" ? assistantName : t("agent_chat.surface_agent")}
          </span>
        </span>
        <Pick
          testId="composer-provider"
          ariaLabel={t("agent_chat.pick_provider")}
          value={draft.provider}
          groups={providerGroups}
          onChange={(v) => void setDraft({ provider: v })}
          fallbackLabel={catalogError ? t("agent_chat.catalog_unavailable") : t("agent_chat.pick_provider")}
          searchPlaceholder={t("agent_chat.search_providers")}
          icon={provider ? undefined : <Bot className="h-3.5 w-3.5" aria-hidden />}
          disabled={!catalog || Boolean(locks?.provider)}
          title={locks?.provider}
        />
        <Pick
          testId="composer-model"
          ariaLabel={t("agent_chat.pick_model")}
          value={draft.model}
          groups={modelGroups}
          onChange={(v) => void setDraft({ model: v })}
          fallbackLabel={draft.model || t("agent_chat.model_default")}
          searchPlaceholder={t("agent_chat.search_models")}
          icon={<Brain className="h-3.5 w-3.5" aria-hidden />}
          disabled={!provider || Boolean(locks?.model)}
          title={locks?.model}
          className="max-w-[220px]"
        />
        {provider && effortLevels.length > 1 && (
          <Pick
            testId="composer-effort"
            ariaLabel={t("agent_chat.pick_effort")}
            value={draft.effort}
            groups={effortGroups}
            onChange={(v) => void setDraft({ effort: v })}
            fallbackLabel={effortLabel(draft.effort, t)}
            icon={<Gauge className="h-3.5 w-3.5" aria-hidden />}
            disabled={Boolean(locks?.effort)}
            title={locks?.effort}
          />
        )}
        {provider && permissionModes.length > 0 && (
          <>
            <span className="mx-1 h-4 w-px bg-border" aria-hidden />
            <Pick
              testId="composer-permission"
              ariaLabel={t("agent_chat.pick_permission")}
              value={permissionValue}
              groups={permissionGroups}
              onChange={(v) => void setDraft({ permissionMode: v })}
              fallbackLabel={t("agent_chat.pick_permission")}
              icon={<ShieldCheck className="h-3.5 w-3.5" aria-hidden />}
              disabled={Boolean(locks?.permissionMode)}
              title={locks?.permissionMode ?? permissionDescription}
              className="max-w-[220px]"
            />
          </>
        )}
        {hasPlan && (
          <button
            type="button"
            role="switch"
            aria-checked={planOn}
            data-testid="composer-plan"
            onClick={() => void setPlan(!planOn)}
            disabled={Boolean(locks?.permissionMode)}
            title={
              locks?.permissionMode ?? (planOn ? t("agent_chat.plan_hint") : t("agent_chat.build_hint"))
            }
            className={cn(
              "inline-flex h-7 items-center gap-1.5 rounded-lg px-2 text-xs font-medium transition-colors disabled:opacity-50",
              planOn
                ? "bg-primary/15 text-primary hover:bg-primary/20"
                : "text-muted-foreground hover:bg-secondary hover:text-foreground",
            )}
          >
            {planOn ? (
              <NotebookPen className="h-3.5 w-3.5" aria-hidden />
            ) : (
              <Hammer className="h-3.5 w-3.5" aria-hidden />
            )}
            {planOn ? t("agent_chat.mode_plan") : t("agent_chat.mode_build")}
          </button>
        )}
        {/*
          No folder chip on the Jarvis surface. Someone talking to Jarvis is
          not pointing it at a checkout: the chip belongs to the IDE's chat,
          and here it only ever showed the leaf of the default path — on
          Windows the account name, which reads like a setting nobody chose.
          The folder itself stays: a CLI seat needs a directory to start in,
          and the surface brings its own (`surface_kits.py`) instead of the
          home directory (maintainer, 2026-08-25).
        */}
        {surface !== "jarvis" && (
          <button
            type="button"
            onClick={() => void onPickFolder()}
            title={
              draft.cwd ? `${t("agent_chat.surface_agent_hint")}: ${draft.cwd}` : t("agent_chat.folder")
            }
            aria-label={t("agent_chat.folder")}
            data-testid="composer-folder"
            className="inline-flex h-7 max-w-[160px] items-center gap-1.5 rounded-lg px-2 text-xs text-muted-foreground transition-colors hover:bg-secondary hover:text-foreground"
          >
            <FolderOpen className="h-3.5 w-3.5 shrink-0" aria-hidden />
            <span className="hidden truncate font-mono text-micro 2xl:inline">{folderLeaf(draft.cwd)}</span>
          </button>
        )}
        <span className="flex-1" />
        {/*
          Dropping and pasting are the primary paths; this is for the people
          who do neither — and for a browser tab, where a drag out of Explorer
          may carry nothing the page can read.
        */}
        <input
          ref={fileInputRef}
          type="file"
          multiple
          className="sr-only"
          tabIndex={-1}
          onChange={(event) => {
            const picked = Array.from(event.currentTarget.files ?? []);
            event.currentTarget.value = "";
            if (picked.length > 0) files.attachFiles(picked);
          }}
        />
        <button
          type="button"
          data-testid="composer-attach"
          onClick={() => fileInputRef.current?.click()}
          disabled={!connected}
          aria-label={t("agent_chat.attach")}
          title={t("agent_chat.attach_hint")}
          className="inline-flex h-8 w-8 items-center justify-center rounded-lg border border-transparent text-muted-foreground transition-colors hover:bg-secondary hover:text-foreground disabled:opacity-50"
        >
          <Paperclip className="h-4 w-4" />
        </button>
        <DictationButton
          dictating={dictating}
          onToggle={toggleDictation}
          disabled={!connected}
          startLabel={t("chats_view.dictation_start")}
          stopLabel={t("chats_view.dictation_stop")}
        />
        {live && canQueue && (
          <button type="button" onClick={() => void cancel()} aria-label={t("agent_chat.stop")}
            title={t("agent_chat.stop")} data-testid="composer-stop"
            className="inline-flex h-8 w-8 items-center justify-center rounded-lg bg-secondary text-foreground hover:bg-secondary/80">
            <Square className="h-3.5 w-3.5" />
          </button>
        )}
        {live && !canQueue && !commands.isCommand && !(commands.canSteer && value.trim()) ? (
          <button
            type="button"
            onClick={() => void cancel()}
            aria-label={t("agent_chat.stop")}
            title={t("agent_chat.stop")}
            data-testid="composer-stop"
            className="inline-flex h-8 w-8 items-center justify-center rounded-lg bg-foreground text-background transition-colors hover:bg-foreground/90"
          >
            <Square className="h-3.5 w-3.5" />
          </button>
        ) : (
          <button
            type="button"
            onClick={() => (dictating ? stopDictationAndSend() : void onSend())}
            disabled={!canSend && !canFinishDictation}
            aria-label={t("agent_chat.send")}
            data-testid="composer-send"
            className={cn(
              "inline-flex h-8 w-8 items-center justify-center rounded-lg transition-colors",
              canSend || canFinishDictation
                ? "bg-foreground/70 text-primary-foreground hover:bg-primary/90"
                : "bg-secondary text-muted-foreground",
            )}
          >
            <ArrowUp className="h-4 w-4" />
          </button>
        )}
      </div>
      )}
      {backendOutdated && (
        <div
          className="flex items-center gap-2 px-1 text-xs text-muted-foreground"
          data-testid="composer-backend-outdated"
          role="status"
        >
          <span>{t("agent_chat.backend_outdated")}</span>
          <button
            type="button"
            onClick={() => void onRestart()}
            disabled={restarting}
            className="font-medium text-primary hover:underline disabled:opacity-60"
          >
            {t("agent_chat.restart_now")}
          </button>
        </div>
      )}
      {provider && !provider.connected && (
        <div
          className="flex items-center gap-2 px-1 text-xs text-muted-foreground"
          data-testid="composer-connect-hint"
        >
          <span>
            {fill(
              provider.cli_installed === false
                ? t("agent_chat.hint_not_installed")
                : t("agent_chat.hint_connect"),
              { provider: provider.label },
            )}
          </span>
          <button
            type="button"
            onClick={() => setActiveSection("apikeys")}
            className="font-medium text-primary hover:underline"
          >
            {t("agent_chat.open_api_keys")}
          </button>
        </div>
      )}
      {attachError && (
        <div
          className="px-1 text-xs text-destructive"
          role="alert"
          data-testid="composer-attach-error"
        >
          {attachError}
        </div>
      )}
      {lastError && (
        <div className="px-1 text-xs text-destructive" role="alert" data-testid="composer-error">
          {lastError}
        </div>
      )}
    </div>
  );
}

/**
 * What stands behind a provider row: a coding CLI (a subscription login, no
 * key), a provider's own API behind a key, or a server on this machine with
 * no account. Decided from the catalog's own facts (runner, keyless) — the
 * same split the Agents tab draws, so the two never disagree. Claude's dual
 * row lands where its resolved runner says: the CLI group when Claude Code
 * answers, the API-key group when the Anthropic endpoint does.
 *
 * `brain` counts as an API seat, not a CLI one: it is Jarvis' own harness
 * calling that provider's endpoint with that provider's key, which is what
 * every row on the front page's chat resolves to. Reading it as a CLI put
 * the whole list under the wrong heading there.
 */
/**
 * A health reason in the person's own words. The backend's `reason` is a
 * machine token ("bad_key", "no_credits", …) from the same vocabulary the
 * API-Keys screen uses; an unfamiliar one falls back to the general "not
 * answering" rather than showing a word out of a log.
 */
export function healthReasonLabel(reason: string, t: (key: string) => string): string {
  switch (reason) {
    case "bad_key":
      return t("agent_chat.provider_health_bad_key");
    case "no_credits":
    case "rate_limited":
      return t("agent_chat.provider_health_no_credits");
    case "unreachable":
      return t("agent_chat.provider_health_unreachable");
    case "timeout":
      return t("agent_chat.provider_health_slow");
    default:
      return t("agent_chat.provider_health_error");
  }
}

/** Reasons that only make sense for a stored API key. */
export function isApiKeyHealthReason(reason: string): boolean {
  return reason === "bad_key";
}

export type ProviderKind = "cli" | "api" | "local";
export const PROVIDER_KINDS: readonly ProviderKind[] = ["cli", "api", "local"];
export function providerKind(p: { runner: string; keyless: boolean }): ProviderKind {
  if (p.keyless) return "local";
  return isApiRunner(p.runner) ? "api" : "cli";
}

function liveHealthFor(
  p: { runner: string; keyless: boolean },
  live: { status: string; reason: string; detail: string } | undefined,
): { status: string; reason: string; detail: string } | undefined {
  if (!live) return undefined;
  if (providerKind(p) === "cli" && isApiKeyHealthReason(live.reason)) return undefined;
  return live;
}

/** One pick on the IDE composer's bottom row — a pill-sized Combobox. */
function Pick({
  testId,
  ariaLabel,
  value,
  groups,
  onChange,
  fallbackLabel,
  searchPlaceholder,
  icon,
  disabled,
  title,
  className,
}: {
  testId: string;
  ariaLabel: string;
  value: string;
  groups: ComboboxGroup[];
  onChange: (value: string) => void;
  fallbackLabel?: string;
  searchPlaceholder?: string;
  icon?: React.ReactNode;
  disabled?: boolean;
  title?: string;
  className?: string;
}) {
  // The Combobox draws the selected option's own icon; the leading glyph here
  // is the column's, shown when the option has none (model, effort, permission).
  const selectedHasIcon = groups.some((g) => g.options.some((o) => o.value === value && o.icon));
  return (
    <span className={cn("inline-flex min-w-0 items-center", className)} title={title}>
      {icon && !selectedHasIcon && (
        <span className="-mr-6 ml-2 shrink-0 text-muted-foreground" aria-hidden>
          {icon}
        </span>
      )}
      <Combobox
        value={value}
        groups={groups}
        onChange={onChange}
        ariaLabel={ariaLabel}
        fallbackLabel={fallbackLabel}
        searchPlaceholder={searchPlaceholder}
        disabled={disabled}
        testId={testId}
        triggerHint={false}
        className={cn(
          "h-7 w-auto max-w-[200px] gap-1.5 rounded-lg border-transparent bg-transparent py-0 pr-1.5 text-xs font-medium text-foreground shadow-none",
          "hover:border-transparent hover:bg-secondary focus-visible:ring-1",
          icon && !selectedHasIcon ? "pl-7" : "pl-1.5",
        )}
      />
    </span>
  );
}

/** The thin upright line between two picks on the front page's row. */
function ControlSeparator() {
  return <span aria-hidden className="mx-0.5 hidden h-4 w-px shrink-0 bg-border-strong sm:block" />;
}

/** The front page's one round action — voice mode, Send or Stop. */
const ROUND_ACTION_CLASS =
  "ml-1 inline-flex h-9 w-9 shrink-0 items-center justify-center rounded-full transition-colors";

const BRAIN_SEP ="\u0001";

/** One value for a provider + model pair in the front page's brain pick. */
export function brainValue(provider: string, model: string): string {
  return `${provider}${BRAIN_SEP}${model}`;
}

export function splitBrainValue(value: string): [string, string] {
  const at = value.indexOf(BRAIN_SEP);
  return at < 0 ? [value, ""] : [value.slice(0, at), value.slice(at + 1)];
}

export function effortLabel(level: string, t: (key: string) => string): string {
  switch (level) {
    case "":
      return t("agent_chat.effort_default");
    case "none":
      return t("agent_chat.effort_none");
    case "minimal":
      return t("agent_chat.effort_minimal");
    case "low":
      return t("agent_chat.effort_low");
    case "medium":
      return t("agent_chat.effort_medium");
    case "high":
      return t("agent_chat.effort_high");
    case "xhigh":
      return t("agent_chat.effort_xhigh");
    case "max":
      return t("agent_chat.effort_max");
    case "ultra":
      return t("agent_chat.effort_ultra");
    default:
      return level;
  }
}