Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

37.6 KB
Raw Blame
// oxlint-disable ok/no-physical-direction-utility -- pre-rule backlog — physical margin/padding/inset utilities predate the rule; drain by swapping ml/mr → ms/me, pl/pr → ps/pe, left/right → start/end, then deleting this line. See https://github.com/inkeep/open-knowledge/blob/main/lint-plugins/ok-rules/README.md#no-physical-direction-utility

import {
  type TargetData,
  TERMINAL_CLIS,
  type TerminalCli,
} from '@inkeep/open-knowledge-core/handoff';
import { Plural, Trans, useLingui } from '@lingui/react/macro';
import { ArrowUpRight, ChevronDown, TextQuote, X } from 'lucide-react';
import { useEffect, useEffectEvent, useRef, useState, useSyncExternalStore } from 'react';
import { toast } from 'sonner';
import {
  composeCommentBatchInstruction,
  QueuedCommentsChip,
  toCommentBatchItem,
  useSelectedCommentCount,
  useSelectedCommentDocs,
} from '@/comments/comment-chips';
import { type BatchPreparedItem, dispatchComments, subscribeCommentPosted } from '@/comments/store';
import { PendingImageStrip } from '@/components/acp/PendingImageStrip';
import { RegisteredAgentIcon } from '@/components/acp/RegisteredAgentIcon';
import {
  ComposerAddMenu,
  ComposerCommentsMenuItem,
  ComposerFilesMenuItem,
  ComposerMentionMenuItem,
  ComposerTooltipProvider,
} from '@/components/ComposerAddMenu';
import { ComposerContextChips } from '@/components/ComposerContextChips';
import { isExternalFileDrag } from '@/components/file-tree-adapter';
import { AgentSplitButton } from '@/components/handoff/AgentSplitButton';
import { AskAgentNameLabel, OpenDesktopAppLabel } from '@/components/handoff/agent-launcher-labels';
import { TargetIcon } from '@/components/handoff/OpenInAgentMenuItem';
import { useTerminalLaunch } from '@/components/handoff/TerminalLaunchContext';
import { cliIconTargetId } from '@/components/handoff/terminal-cli-display';
import {
  buildComposerHandoffInput,
  openInstallUrl,
  startAgentThreadForInput,
  useHandoffDispatch,
} from '@/components/handoff/useHandoffDispatch';
import { useInstalledAgents } from '@/components/handoff/useInstalledAgents';
import { RotatingComposerPlaceholder } from '@/components/RotatingComposerPlaceholder';
import { AGENT_CONNECTIONS_SECTION_LABEL } from '@/components/settings/settings-section-labels';
import { Button } from '@/components/ui/button';
import { Spinner } from '@/components/ui/spinner';
import { publishAskComposerHeight } from '@/editor/ask-composer-inset';
import {
  type ComposerAttachmentDropPolicy,
  ComposerMentionInput,
  type ComposerMentionInputHandle,
} from '@/editor/ComposerMentionInput';
import { revealCaretAboveComposerCard } from '@/editor/caret-reveal';
import { documentScrollports, isPinnedToEnd } from '@/editor/document-scrollports';
import type { SuggestionPopupLabel } from '@/editor/extensions/suggestion-floating-ui';
import { isScrollRestoreSuppressed } from '@/editor/scroll-restore-coordination';
import {
  lightRenderMarkdownPreview,
  type SelectionSnapshot,
  selectionChipLabel,
  selectionSnapshotToCompose,
} from '@/editor/selection-context';
import type { EditorSurface } from '@/editor/selection-stats';
import { useComposerAttachments } from '@/editor/use-composer-attachments';
import { useConflictComposerPrefill } from '@/hooks/use-conflict-composer-prefill';
import { useSelectionContext } from '@/hooks/use-selection-context';
import { isDesktopTargetEnabled, isInAppAgentEnabled } from '@/lib/acp/agent-visibility';
import { useEnabledOverrides } from '@/lib/acp/enabled-agents';
import { collectAllFiles, collectImageFiles } from '@/lib/acp/image-attachment';
import {
  enabledDesktopTargets,
  enabledTerminalClis,
  resolveLauncherSelection,
  unresolvedDesktopTargets,
} from '@/lib/acp/launcher-selection';
import {
  pickEffectiveDefaultAgent,
  type RegisteredAgent,
  registerAgent,
  useDefaultRegisteredAgent,
  useRegisteredAgents,
} from '@/lib/acp/registered-agents';
import { VISIBLE_TARGETS } from '@/lib/handoff/targets';
import { matchesKeyboardShortcut } from '@/lib/keyboard-shortcuts';
import { isNoteWindow } from '@/lib/note-window-mode';
import { recordOnboardingAskedAi } from '@/lib/onboarding-signals';
import { isOverlayLayerOpen } from '@/lib/overlay-layers';
import {
  IN_APP_THREAD_ID,
  loadStickyAgent,
  saveStickyAgent,
  terminalCliId,
} from '@/lib/unified-agent-store';
import { openAgentSettings } from '@/lib/use-settings-route';
import { useWorkspace } from '@/lib/use-workspace';
import { cn } from '@/lib/utils';
import { docNameToRelativePath } from '@/lib/workspace-paths';
import { emitOpenAskAiComposer, subscribeToOpenAskAiComposer } from './ask-ai-composer-events';
import {
  clearComposerDraft,
  getComposerDraft,
  setComposerDraftDoc,
  subscribeComposerDraft,
} from './composer-draft-store';
import { nextTouchedFiles } from './composer-touched-files';
import { focusComposerInputOnCardPointer } from './focus-composer-on-card-pointer';
import { usePageList } from './PageListContext';

const MARKDOWN_RELATIVE_PATH_EXTENSION = /\.(md|mdx)$/i;

function docNameToComposerRelativePath(docName: string, docExt?: string): string {
  if (MARKDOWN_RELATIVE_PATH_EXTENSION.test(docName)) return docName;
  return docExt ? `${docName}${docExt}` : docNameToRelativePath(docName);
}

function isNativeTextControl(target: EventTarget | null): boolean {
  if (!(target instanceof HTMLElement)) return false;
  const tagName = target.tagName.toUpperCase();
  return tagName === 'INPUT' || tagName === 'TEXTAREA' || tagName === 'SELECT';
}

const COMPOSER_PORTAL_ATTRIBUTE = 'data-composer-portal';
const COMPOSER_PORTAL_ATTRIBUTES = { [COMPOSER_PORTAL_ATTRIBUTE]: '' } as const;
export const COMPOSER_SUGGESTION_POPUP_LABELS = [
  'composer-mention',
  'composer-slash',
] as const satisfies readonly SuggestionPopupLabel[];
const ASK_COMPOSER_HEIGHT_RESERVE_PX = 56;

const COMPOSER_PORTAL_SELECTOR = [
  ...COMPOSER_SUGGESTION_POPUP_LABELS.map((label) => `[data-suggestion-popup="${label}"]`),
  `[${COMPOSER_PORTAL_ATTRIBUTE}]`,
].join(',');

export function BottomComposer({
  docName,
  surface,
  folderPath,
  editorVisible = true,
  dismissed = false,
  onDismiss,
  onReopen,
}: {
  docName?: string | null;
  surface?: EditorSurface;
  folderPath?: string;
  editorVisible?: boolean;
  dismissed?: boolean;
  onDismiss?: () => void;
  onReopen?: () => void;
}) {
  const { t } = useLingui();
  const folderMode = folderPath !== undefined;
  const activeDocOrNull = folderMode ? null : (docName ?? null);
  const effectiveSurface: EditorSurface = surface ?? 'wysiwyg';
  const workspace = useWorkspace();
  const { pageMeta } = usePageList();
  const { states, refresh: refreshInstalledAgents } = useInstalledAgents();
  const overrides = useEnabledOverrides();
  const { dispatch } = useHandoffDispatch();
  const terminalLaunch = useTerminalLaunch();
  const [stickyId] = useState(() => loadStickyAgent());
  const [selectedId, setSelectedId] = useState<string | null>(null);
  const [isEmpty, setIsEmpty] = useState(true);
  const [pending, setPending] = useState(false);
  const [dragActive, setDragActive] = useState(false);
  const inputRef = useRef<ComposerMentionInputHandle>(null);
  const { isSeedIntact, onContentChanged: onPrefillContentChanged } = useConflictComposerPrefill(
    activeDocOrNull,
    inputRef,
  );
  const cardRef = useRef<HTMLDivElement>(null);
  const clampRef = useRef<(() => void) | null>(null);

  const { doc: savedDraftDoc } = useSyncExternalStore(subscribeComposerDraft, getComposerDraft);

  const {
    pendingAttachments,
    pendingUploads,
    ingestFiles,
    removeAt: removePendingAttachment,
    clear: clearPendingAttachments,
  } = useComposerAttachments({
    absPathOf:
      typeof window !== 'undefined' && window.okDesktop
        ? window.okDesktop.getPathForFile
        : undefined,
    workspaceContentDir: workspace?.contentDir,
    pathSeparator: workspace?.pathSeparator,
    onError: (message) => toast.error(message),
  });

  useEffect(() => {
    if (folderMode || docName == null) return;
    const followBottom = () => {
      clampRef.current?.();
      if (isScrollRestoreSuppressed(docName)) return;
      const pinned = documentScrollports().filter(isPinnedToEnd);
      if (pinned.length === 0) return;
      let cancelled = false;
      let frame: number | null = null;
      let backstop: ReturnType<typeof setTimeout> | null = null;
      const cancelIfOutsideCard = (event: Event) => {
        const target = event.target as Node | null;
        if (target === null) return;
        if (cardRef.current?.contains(target)) return;
        if (target instanceof Element && target.closest(COMPOSER_PORTAL_SELECTOR) !== null) return;
        cancelled = true;
      };
      const dispose = () => {
        cancelled = true;
        if (frame !== null) cancelAnimationFrame(frame);
        frame = null;
        if (backstop !== null) clearTimeout(backstop);
        backstop = null;
        window.removeEventListener('wheel', cancelIfOutsideCard);
        window.removeEventListener('touchstart', cancelIfOutsideCard);
        window.removeEventListener('mousedown', cancelIfOutsideCard);
        window.removeEventListener('keydown', cancelIfOutsideCard);
        if (clampRef.current === dispose) clampRef.current = null;
      };
      clampRef.current = dispose;
      window.addEventListener('wheel', cancelIfOutsideCard, { passive: true });
      window.addEventListener('touchstart', cancelIfOutsideCard, { passive: true });
      window.addEventListener('mousedown', cancelIfOutsideCard);
      window.addEventListener('keydown', cancelIfOutsideCard);
      backstop = setTimeout(dispose, 400);
      const start = performance.now();
      const step = () => {
        frame = null;
        if (isScrollRestoreSuppressed(docName)) cancelled = true;
        if (cancelled || performance.now() - start >= 300) {
          dispose();
          return;
        }
        for (const el of pinned) el.scrollTop = el.scrollHeight - el.clientHeight;
        frame = requestAnimationFrame(step);
      };
      frame = requestAnimationFrame(step);
    };
    const card = cardRef.current;
    if (dismissed || !card) {
      followBottom();
      return () => {
        clampRef.current?.();
      };
    }
    const apply = () => {
      followBottom();
      publishAskComposerHeight(card.offsetHeight + ASK_COMPOSER_HEIGHT_RESERVE_PX);
    };
    apply();
    const observer = new ResizeObserver(apply);
    observer.observe(card);
    return () => {
      observer.disconnect();
      followBottom();
      publishAskComposerHeight(null);
    };
  }, [dismissed, docName, folderMode]);

  const openAndFocus = useEffectEvent(() => {
    if (!editorVisible) return;
    if (dismissed) onReopen?.();
    else inputRef.current?.focus();
  });

  const readPaintedOver = useEffectEvent(() => ({
    docName: activeDocOrNull,
    effectiveSurface,
    editorVisible,
  }));

  const arrivalRevealOwedRef = useRef(true);
  useEffect(() => {
    if (dismissed) {
      arrivalRevealOwedRef.current = true;
      return;
    }
    if (!editorVisible || !arrivalRevealOwedRef.current) return;
    const arrivedOver = readPaintedOver();
    const arrivedOverDoc = arrivedOver.docName;
    if (arrivedOverDoc == null) return;
    const arrivedOverSurface = arrivedOver.effectiveSurface;
    const frame = requestAnimationFrame(() => {
      arrivalRevealOwedRef.current = false;
      const paintedOver = readPaintedOver();
      if (
        !paintedOver.editorVisible ||
        paintedOver.docName !== arrivedOverDoc ||
        paintedOver.effectiveSurface !== arrivedOverSurface
      ) {
        return;
      }
      if (isScrollRestoreSuppressed(arrivedOverDoc)) return;
      const card = cardRef.current;
      if (!card) return;
      revealCaretAboveComposerCard({
        docName: arrivedOverDoc,
        surface: arrivedOverSurface,
        card,
      });
    });
    return () => cancelAnimationFrame(frame);
  }, [dismissed, editorVisible]);

  useEffect(() => {
    return subscribeToOpenAskAiComposer(openAndFocus);
  }, []);

  useEffect(() => {
    const onKeyDown = (event: KeyboardEvent) => {
      if (!readPaintedOver().editorVisible) return;
      if (!matchesKeyboardShortcut(event, 'open-ask-ai')) return;
      if (isOverlayLayerOpen()) return;
      if (isNativeTextControl(event.target)) return;
      event.preventDefault();
      emitOpenAskAiComposer();
    };
    window.addEventListener('keydown', onKeyDown, { capture: true });
    return () => window.removeEventListener('keydown', onKeyDown, { capture: true });
  }, []);

  const prevDismissedRef = useRef(dismissed);
  useEffect(() => {
    const wasDismissed = prevDismissedRef.current;
    prevDismissedRef.current = dismissed;
    if (wasDismissed && !dismissed) inputRef.current?.focus();
  }, [dismissed]);

  const [touchedFiles, setTouchedFiles] = useState<readonly string[]>([]);
  const [dismissedFiles, setDismissedFiles] = useState<ReadonlySet<string>>(() => new Set());
  const selectedCommentCount = useSelectedCommentCount();
  const selectedCommentDocs = useSelectedCommentDocs();
  const [commentsAttached, setCommentsAttached] = useState(true);
  const hasQueuedComments = selectedCommentCount > 0 && commentsAttached;
  const [inlineMentions, setInlineMentions] = useState<readonly string[]>([]);

  const activeFilePath =
    folderMode || docName == null
      ? ''
      : docNameToComposerRelativePath(docName, pageMeta.get(docName)?.docExt);
  useEffect(() => {
    if (folderMode || isEmpty) return;
    setTouchedFiles((prev) => nextTouchedFiles(prev, activeFilePath, dismissedFiles, isSeedIntact));
  }, [folderMode, isEmpty, isSeedIntact, activeFilePath, dismissedFiles]);

  const fileChips = folderMode
    ? folderPath && !dismissedFiles.has(folderPath) && !inlineMentions.includes(folderPath)
      ? [folderPath]
      : []
    : touchedFiles.filter((path) => !dismissedFiles.has(path) && !inlineMentions.includes(path));

  const liveSelection = useSelectionContext(activeDocOrNull, effectiveSurface);
  const liveFrontmatterSelection = useSelectionContext(activeDocOrNull, 'frontmatter');
  const [pinnedSelection, setPinnedSelection] = useState<SelectionSnapshot | null>(null);
  const [selectionExpanded, setSelectionExpanded] = useState(false);
  useEffect(() => {
    if (liveSelection) setPinnedSelection(liveSelection);
  }, [liveSelection]);
  useEffect(() => {
    if (liveFrontmatterSelection) setPinnedSelection(liveFrontmatterSelection);
  }, [liveFrontmatterSelection]);
  useEffect(
    () =>
      subscribeCommentPosted(() => {
        setPinnedSelection(null);
        setSelectionExpanded(false);
        setCommentsAttached(true);
      }),
    [],
  );

  const defaultRegisteredAgent = useDefaultRegisteredAgent();
  const registeredThreadAgents = useRegisteredAgents();
  const enabledThreadAgents = registeredThreadAgents.filter((agent) =>
    isInAppAgentEnabled(overrides, agent.source, agent.id, true, agent.supported),
  );
  const defaultThreadAgent = pickEffectiveDefaultAgent(enabledThreadAgents, defaultRegisteredAgent);

  const selection = resolveLauncherSelection({
    sticky: selectedId ?? stickyId,
    effectiveThreadAgent: defaultThreadAgent,
    enabledClis:
      terminalLaunch !== null ? enabledTerminalClis(overrides, terminalLaunch.installedClis) : [],
    enabledDesktopTargets: enabledDesktopTargets(overrides, states),
    unresolvedDesktopTargets: unresolvedDesktopTargets(overrides, states),
    installedClis: terminalLaunch?.installedClis ?? {},
    terminalAvailable: terminalLaunch !== null,
    threadsAvailable: true,
    desktopSelectable: true,
  });
  const isThreadSelected = selection.kind === 'thread';
  const selectedCli: TerminalCli | null = selection.kind === 'cli' ? selection.cli : null;
  const isTerminalSelected = selectedCli !== null;
  const resolvedTarget: TargetData | null =
    selection.kind === 'desktop'
      ? (VISIBLE_TARGETS.find((target) => target.id === selection.target) ?? null)
      : null;

  const canSend =
    !pending &&
    pendingUploads.length === 0 &&
    (!isEmpty || pinnedSelection !== null || hasQueuedComments || pendingAttachments.length > 0) &&
    (isTerminalSelected || resolvedTarget !== null || isThreadSelected);

  const inNoteWindow = isNoteWindow();
  const dropRefusalReason = ((): string | null => {
    if (inNoteWindow) {
      return t`Attachments aren't available in note windows yet — use the main window to attach files.`;
    }
    switch (selection.kind) {
      case 'thread':
        return null;
      case 'cli':
        return t`${TERMINAL_CLIS[selection.cli].displayName} runs in a terminal and doesn't accept attachments — choose an in-app agent instead.`;
      case 'desktop':
        return resolvedTarget !== null
          ? t`${resolvedTarget.displayName} opens via a link and doesn't accept attachments — choose an in-app agent instead.`
          : t`This composer doesn't accept attachments.`;
      case 'terminal':
        return t`This composer doesn't accept attachments.`;
      case 'none': {
        const agentConnectionsLabel = t(AGENT_CONNECTIONS_SECTION_LABEL);
        return t({
          message: `No agents are set up yet — add an in-app agent in ${agentConnectionsLabel} to attach files.`,
          comment:
            'Composer hint when no agent is set up; agentConnectionsLabel is the Settings sidebar pane name',
        });
      }
      default: {
        const _exhaustive: never = selection;
        throw new Error(`Unhandled launcher selection: ${String(_exhaustive)}`);
      }
    }
  })();
  const attachmentsAccepted = isThreadSelected && !inNoteWindow;
  const attachmentDrop: ComposerAttachmentDropPolicy = attachmentsAccepted
    ? {
        kind: 'accept',
        onFiles: (files) => {
          void ingestFiles(files);
        },
      }
    : {
        kind: 'refuse',
        ...(dropRefusalReason !== null ? { reason: dropRefusalReason } : {}),
      };

  const desktopAgents = VISIBLE_TARGETS.filter((target) =>
    isDesktopTargetEnabled(overrides, target.id, states[target.id]?.installed),
  );
  const agentProbePending = VISIBLE_TARGETS.some((target) => states[target.id]?.installed == null);

  const cliRows =
    terminalLaunch !== null
      ? enabledTerminalClis(overrides, terminalLaunch.installedClis).map((cli) => {
          const { displayName } = TERMINAL_CLIS[cli];
          return {
            cli,
            label: displayName,
            ariaLabel: t`${displayName} CLI`,
            selected: selectedCli === cli,
            onSelect: () => handleSelectCli(cli),
          };
        })
      : undefined;

  const suggestions = hasQueuedComments
    ? [t`Work through these comments`]
    : [
        t`Research the extinction of flightless birds`,
        t`Condense my AGENTS.md file to less than 40k characters`,
        t`Create a new spec file for my user story`,
        t`Summarize everything I changed this week`,
      ];

  const handleSelectAgent = (target: TargetData) => {
    setSelectedId(target.id);
    saveStickyAgent(target.id);
  };

  const handleSelectCli = (cli: TerminalCli) => {
    const id = terminalCliId(cli);
    setSelectedId(id);
    saveStickyAgent(id);
  };

  const handleSelectThread = () => {
    setSelectedId(IN_APP_THREAD_ID);
    saveStickyAgent(IN_APP_THREAD_ID);
  };

  const handleSelectThreadAgent = (agent: RegisteredAgent) => {
    registerAgent(agent);
    handleSelectThread();
  };

  const clearComposer = () => {
    inputRef.current?.clear();
    setPinnedSelection(null);
    setSelectionExpanded(false);
    setTouchedFiles([]);
    setDismissedFiles(new Set());
    setCommentsAttached(true);
    clearPendingAttachments();
    clearComposerDraft();
  };

  const dispatchComposed = async (
    input: ReturnType<typeof buildComposerHandoffInput>,
    clearOnSuccess = true,
  ): Promise<boolean> => {
    const clearIfOwned = () => {
      if (clearOnSuccess) clearComposer();
    };
    if (input === null) {
      toast.error(t`Couldn't send your prompt — please try again.`);
      return false;
    }
    if (pendingAttachments.length > 0 && !isThreadSelected) {
      toast.error(
        t`This agent doesn't accept attachments — remove them or choose an in-app agent.`,
      );
      return false;
    }
    if (isThreadSelected) {
      startAgentThreadForInput(
        input,
        defaultThreadAgent !== null
          ? { agent: { source: defaultThreadAgent.source, id: defaultThreadAgent.id } }
          : undefined,
      );
      recordOnboardingAskedAi();
      clearIfOwned();
      return true;
    }
    if (selectedCli !== null && terminalLaunch !== null) {
      try {
        terminalLaunch.launchInTerminal(input, selectedCli);
      } catch {
        toast.error(t`Couldn't open the terminal — please try again.`);
        return false;
      }
      recordOnboardingAskedAi();
      clearIfOwned();
      return true;
    }
    if (resolvedTarget === null) {
      toast.error(t`No agent is set up yet — pick one from the send menu.`);
      return false;
    }
    if (states[resolvedTarget.id]?.installed !== true) {
      void openInstallUrl(resolvedTarget);
      toast.info(t`${resolvedTarget.displayName} isn't installed yet — opening its download page.`);
      clearIfOwned();
      return false;
    }
    setPending(true);
    const settle = (clear: boolean) => {
      setPending(false);
      if (clear) clearIfOwned();
    };
    return dispatch(resolvedTarget.id, input, {
      installState: states[resolvedTarget.id],
    }).then(
      (outcome) => {
        settle(
          outcome.ok || (outcome.reason !== 'setup-canceled' && outcome.reason !== 'superseded'),
        );
        if (outcome.ok) recordOnboardingAskedAi();
        return outcome.ok;
      },
      (error: unknown) => {
        settle(true);
        throw error;
      },
    );
  };

  const composeCurrentInput = () => {
    const { instruction, mentions } = inputRef.current?.getContent() ?? {
      instruction: '',
      mentions: [],
    };

    if (folderMode) {
      const dispatchMentions = [...new Set([...fileChips, ...mentions])].filter(
        (path) => path !== folderPath,
      );
      return buildComposerHandoffInput({
        docName: null,
        folderRelativePath: folderPath,
        workspace,
        instruction,
        mentions: dispatchMentions,
        attachments: pendingAttachments,
      });
    }

    const selection = pinnedSelection ? selectionSnapshotToCompose(pinnedSelection) : undefined;
    const selectionDoc = pinnedSelection?.docName ?? null;
    const leadDocName = pinnedSelection
      ? selectionDoc
      : fileChips.includes(activeFilePath)
        ? (docName ?? null)
        : null;
    const leadPath =
      leadDocName !== null
        ? docNameToComposerRelativePath(leadDocName, pageMeta.get(leadDocName)?.docExt)
        : null;
    const dispatchMentions = [...new Set([...fileChips, ...mentions])].filter(
      (path) => path !== leadPath,
    );
    return buildComposerHandoffInput({
      docName: leadDocName,
      ...(leadPath !== null ? { docRelativePath: leadPath } : {}),
      workspace,
      instruction,
      mentions: dispatchMentions,
      selection,
      attachments: pendingAttachments,
    });
  };

  const submit = () => {
    if (!canSend) return;
    if (hasQueuedComments) {
      submitQueuedComments().catch((err) => {
        console.warn('[comments] queued-comment send rejected unexpectedly', err);
      });
      return;
    }
    void dispatchComposed(composeCurrentInput());
  };

  const submitQueuedComments = async () => {
    const { instruction, mentions } = inputRef.current?.getContent() ?? {
      instruction: '',
      mentions: [],
    };
    const shipped = await dispatchComments({
      compose: async (items: readonly BatchPreparedItem[]) => {
        const input = buildComposerHandoffInput({
          docName: null,
          workspace,
          instruction: composeCommentBatchInstruction(
            items.map((item) => toCommentBatchItem(item.payload)),
            instruction,
            pinnedSelection
              ? { docName: pinnedSelection.docName, markdown: pinnedSelection.markdown }
              : undefined,
          ),
          mentions: [
            ...new Set([
              ...items.map((item) => docNameToRelativePath(item.payload.docName)),
              ...fileChips,
              ...mentions,
            ]),
          ],
          attachments: pendingAttachments,
        });
        if (input === null) {
          toast.error(t`Couldn't send your comments — please try again.`);
          return false;
        }
        return dispatchComposed(input, false);
      },
    });
    if (shipped.length > 0) clearComposer();
  };

  if (dismissed) return null;

  let pinnedLabel = '';
  let pinnedPreview = '';
  if (pinnedSelection) {
    const basename =
      docNameToComposerRelativePath(
        pinnedSelection.docName,
        pageMeta.get(pinnedSelection.docName)?.docExt,
      )
        .split('/')
        .pop() ?? '';
    pinnedLabel = selectionChipLabel(pinnedSelection, basename);
    pinnedPreview = lightRenderMarkdownPreview(pinnedSelection.markdown);
  }

  const cardContent = (
    // oxlint-disable-next-line jsx-a11y/no-static-element-interactions -- pointer clicks only delegate focus to the composer's editable; keyboard users focus it directly (Tab / ⇧⌘L).
    <div
      ref={cardRef}
      onMouseDown={(event) => focusComposerInputOnCardPointer(event, inputRef)}
      onPaste={(event) => {
        const files = collectImageFiles(event.clipboardData);
        if (files.length === 0) return;
        event.preventDefault();
        if (!attachmentsAccepted) {
          inputRef.current?.refuseDrop(
            dropRefusalReason ?? t`This composer doesn't accept attachments.`,
          );
          return;
        }
        void ingestFiles(files);
      }}
      onDragEnter={(event) => {
        if (isExternalFileDrag(event)) {
          event.preventDefault();
          setDragActive(true);
        }
      }}
      onDragOver={(event) => {
        if (isExternalFileDrag(event)) {
          event.preventDefault();
          event.dataTransfer.dropEffect = 'copy';
          setDragActive(true);
        }
      }}
      onDragLeave={(event) => {
        const next = event.relatedTarget;
        if (next instanceof Node && event.currentTarget.contains(next)) return;
        setDragActive(false);
      }}
      onDropCapture={() => setDragActive(false)}
      onDrop={(event) => {
        if (!isExternalFileDrag(event)) return;
        event.preventDefault();
        const files = collectAllFiles(event.dataTransfer);
        if (files.length === 0) {
          inputRef.current?.refuseDrop(
            t`Folders and empty files can't be attached — drop the files themselves.`,
          );
          return;
        }
        if (!attachmentsAccepted) {
          inputRef.current?.refuseDrop(
            dropRefusalReason ?? t`This composer doesn't accept attachments.`,
          );
          return;
        }
        void ingestFiles(files);
      }}
      data-testid="ask-ai-composer-card"
      data-drag-active={dragActive ? (attachmentsAccepted ? 'accept' : 'refuse') : undefined}
      className={cn(
        'pointer-events-auto group relative flex cursor-text flex-col gap-1.5 rounded-2xl border border-border/60 bg-card px-3 py-2 shadow-sm transition-colors focus-within:border-ring focus-within:ring-3 focus-within:ring-ring/50',
        dragActive &&
          attachmentsAccepted &&
          'bg-primary/5 outline-2 outline-dashed outline-offset-2 outline-primary',
        dragActive &&
          !attachmentsAccepted &&
          'bg-destructive/5 outline-2 outline-dashed outline-offset-2 outline-destructive/60',
      )}
    >
      {!folderMode ? (
        <Button
          type="button"
          variant="outline"
          aria-label={t`Collapse Ask AI`}
          onClick={() => onDismiss?.()}
          data-testid="ask-ai-collapse"
          className="-top-2.5 -translate-x-1/2 absolute left-1/2 z-10 h-5 w-10 rounded-md p-0 text-muted-foreground opacity-0 shadow-sm transition-opacity hover:text-foreground focus-visible:opacity-100 group-focus-within:opacity-100 group-hover:opacity-100 dark:bg-background dark:hover:bg-muted"
        >
          <ChevronDown className="size-3.5" aria-hidden />
        </Button>
      ) : null}
      <ComposerContextChips
        files={fileChips}
        onRemoveFile={(path) =>
          setDismissedFiles((prev) => {
            const next = new Set(prev);
            next.add(path);
            return next;
          })
        }
      >
        {pinnedSelection ? (
          <>
            <span
              data-testid="composer-selection-pill"
              title={pinnedLabel}
              className="group/chip inline-flex max-w-[16rem] items-center gap-1 rounded-md border bg-muted/40 py-0.5 pr-1.5 pl-1 text-muted-foreground text-xs"
            >
              <Button
                type="button"
                variant="ghost"
                size="icon"
                aria-label={t`Remove selection`}
                onClick={() => {
                  setPinnedSelection(null);
                  setSelectionExpanded(false);
                }}
                className="group/remove relative size-3.5 shrink-0 rounded-sm text-muted-foreground/80 hover:text-foreground"
              >
                <TextQuote
                  className="absolute top-1/2 left-1/2 size-3 -translate-x-1/2 -translate-y-1/2 opacity-100 transition-opacity duration-150 ease-out group-hover/chip:opacity-0 group-focus-within/chip:opacity-0 motion-reduce:transition-none"
                  aria-hidden
                />
                <X
                  className="absolute top-1/2 left-1/2 size-3 -translate-x-1/2 -translate-y-1/2 opacity-0 transition-opacity duration-150 ease-out group-hover/chip:opacity-100 group-focus-within/chip:opacity-100 motion-reduce:transition-none"
                  aria-hidden
                />
              </Button>
              <Button
                type="button"
                variant="ghost"
                aria-expanded={selectionExpanded}
                aria-label={
                  selectionExpanded ? t`Hide selection preview` : t`Show selection preview`
                }
                onClick={() => setSelectionExpanded((open) => !open)}
                data-testid="composer-selection-peek"
                className="h-auto min-h-0 min-w-0 shrink justify-start px-0 py-0 text-left font-normal text-muted-foreground text-xs hover:bg-transparent hover:text-foreground"
              >
                <span className="min-w-0 truncate">{pinnedLabel}</span>
              </Button>
            </span>
            {selectionExpanded && pinnedPreview !== '' ? (
              <p
                className="max-h-24 w-full basis-full overflow-y-auto overscroll-contain whitespace-pre-wrap text-2xs text-muted-foreground/80 subtle-scrollbar"
                data-testid="composer-selection-preview"
              >
                {pinnedPreview}
              </p>
            ) : null}
          </>
        ) : null}
        {hasQueuedComments && (
          <QueuedCommentsChip
            count={selectedCommentCount}
            docs={selectedCommentDocs}
            onDismiss={() => setCommentsAttached(false)}
          />
        )}
      </ComposerContextChips>
      {pendingAttachments.length > 0 || pendingUploads.length > 0 ? (
        <PendingImageStrip
          testIdPrefix="ask-ai"
          images={pendingAttachments}
          uploads={pendingUploads}
          onRemove={removePendingAttachment}
        />
      ) : null}
      {pendingAttachments.length > 0 && dropRefusalReason !== null ? (
        <p aria-hidden="true" className="px-1 pb-1 text-muted-foreground text-xs">
          {dropRefusalReason}
        </p>
      ) : null}
      <div
        role="status"
        aria-live="polite"
        className="sr-only"
        data-testid="composer-attachment-status"
      >
        {pendingAttachments.length > 0 && dropRefusalReason !== null ? (
          dropRefusalReason
        ) : pendingUploads.length > 0 ? (
          <Plural
            value={pendingUploads.length}
            one="Uploading # attachment"
            other="Uploading # attachments"
          />
        ) : pendingAttachments.length > 0 ? (
          <Plural
            value={pendingAttachments.length}
            one="# attachment is ready to send"
            other="# attachments are ready to send"
          />
        ) : null}
      </div>
      <div className="flex flex-wrap items-end gap-2">
        <ComposerAddMenu composerRef={inputRef} testId="ask-ai-add-to-prompt" size="icon">
          {attachmentsAccepted ? <ComposerFilesMenuItem onFiles={ingestFiles} /> : null}
          {selectedCommentCount > 0 && !hasQueuedComments ? (
            <ComposerCommentsMenuItem
              count={selectedCommentCount}
              onSelect={() => setCommentsAttached(true)}
            />
          ) : null}
          <ComposerMentionMenuItem onSelect={() => inputRef.current?.openMentionPicker()} />
        </ComposerAddMenu>
        <div className="relative flex min-h-8 min-w-24 flex-[1_1_6rem] flex-col justify-center">
          <ComposerMentionInput
            ref={inputRef}
            ariaLabel={t`Ask AI`}
            mentionRecency={{ currentDocName: activeDocOrNull, recentPaths: [] }}
            attachmentDrop={attachmentDrop}
            onEmptyChange={setIsEmpty}
            onContentChange={(doc) => {
              setComposerDraftDoc(doc);
              onPrefillContentChanged();
            }}
            onMentionsChange={setInlineMentions}
            onSubmit={submit}
            initialDoc={savedDraftDoc ?? undefined}
            className="max-h-[200px] overflow-y-auto overscroll-contain text-base md:text-sm"
          />
          {isEmpty ? (
            <RotatingComposerPlaceholder
              phrases={suggestions}
              rotating={!hasQueuedComments}
              className="flex -translate-y-px items-center px-0 py-0"
              testId="ask-ai-composer-placeholder"
            />
          ) : null}
        </div>
        <AgentSplitButton
          className="ms-auto max-w-full min-w-0"
          primaryClassName="h-auto min-h-8 min-w-0 shrink whitespace-normal"
          primary={
            <>
              {isThreadSelected ? (
                <RegisteredAgentIcon
                  agentId={defaultThreadAgent?.id ?? ''}
                  iconUrl={defaultThreadAgent?.iconUrl}
                  className="size-4"
                />
              ) : selectedCli !== null ? (
                <TargetIcon id={cliIconTargetId(selectedCli)} className="size-4" aria-hidden />
              ) : resolvedTarget ? (
                <TargetIcon id={resolvedTarget.id} className="size-4" aria-hidden />
              ) : null}
              <span className="min-w-0 break-words">
                {isThreadSelected ? (
                  defaultThreadAgent !== null ? (
                    <AskAgentNameLabel agentName={defaultThreadAgent.name} />
                  ) : (
                    <Trans>Ask an agent</Trans>
                  )
                ) : selectedCli !== null ? (
                  <Trans>Ask {TERMINAL_CLIS[selectedCli].displayName} CLI</Trans>
                ) : resolvedTarget ? (
                  <OpenDesktopAppLabel displayName={resolvedTarget.displayName} />
                ) : (
                  <Trans>Ask</Trans>
                )}
              </span>
              {resolvedTarget ? <ArrowUpRight aria-hidden className="size-3.5" /> : null}
              {pending ? <Spinner className="size-3.5" aria-hidden /> : null}
            </>
          }
          onPrimary={submit}
          primaryDisabled={!canSend}
          enabledTargets={desktopAgents}
          selectedTargetId={isTerminalSelected ? null : (resolvedTarget?.id ?? null)}
          onSelectTarget={handleSelectAgent}
          threadAgents={enabledThreadAgents.map((agent) => ({
            key: `${agent.source}:${agent.id}`,
            id: agent.id,
            name: agent.name,
            ...(agent.iconUrl !== undefined ? { iconUrl: agent.iconUrl } : {}),
            selected:
              isThreadSelected &&
              defaultThreadAgent !== null &&
              defaultThreadAgent.source === agent.source &&
              defaultThreadAgent.id === agent.id,
            onSelect: () => handleSelectThreadAgent(agent),
          }))}
          onOpenSettings={openAgentSettings}
          onMenuOpenChange={(open) => {
            if (open) void refreshInstalledAgents();
          }}
          terminals={cliRows}
          menuEmptyState={
            <p className="px-2 py-1.5 text-sm text-muted-foreground" aria-live="polite">
              {agentProbePending ? (
                <Trans>Checking for agents</Trans>
              ) : (
                <Trans>No agents enabled</Trans>
              )}
            </p>
          }
          menuAttributes={COMPOSER_PORTAL_ATTRIBUTES}
          triggerAriaLabel={t`Choose agent`}
          testIds={{
            group: 'ask-ai-agent-group',
            primary: 'ask-ai-send',
            trigger: 'ask-ai-agent-trigger',
            menu: 'ask-ai-agent-menu',
            option: (id) => `ask-ai-agent-option-${id}`,
            threadAgent: (key) => `ask-ai-agent-option-thread-${key}`,
            settings: 'ask-ai-agent-option-settings',
            terminal: (cli) =>
              cli === 'claude'
                ? 'ask-ai-agent-option-terminal'
                : `ask-ai-agent-option-terminal-${cli}`,
          }}
        />
      </div>
    </div>
  );
  const card = <ComposerTooltipProvider>{cardContent}</ComposerTooltipProvider>;

  if (folderMode) {
    return (
      <div className="shrink-0 pt-2 pb-3" data-testid="bottom-composer">
        <div className="mx-auto w-full max-w-4xl px-6">{card}</div>
      </div>
    );
  }

  return (
    <div
      className="pointer-events-none absolute inset-x-0 bottom-[var(--conflict-footer-height,0px)] z-20 editor-content-aligned bg-gradient-to-t from-background from-65% via-background to-transparent pt-10 pb-2"
      data-testid="bottom-composer"
    >
      {card}
    </div>
  );
}