/** * ChatInput - Zed-style bottom input area for the AI chat panel * * Thin wrapper around the AI Elements prompt-input components. * Bordered textarea with monospace placeholder, expand toggle, * and a bottom toolbar with muted controls + subtle send button. */ import { AtSign, Check, ChevronDown, ChevronRight, Cpu, Expand, Eye, FileText, ImageIcon, MessageSquare, Package, Plus, ShieldCheck, SquareTerminal, X, Zap } from 'lucide-react'; import { filterQuickMessages, buildSlashCommandItems, filterUserSkillsForSlash, getSlashCommandItemKey, type AIQuickMessage, type SlashCommandItem, type UserSkillSlashOption } from '../../infrastructure/ai/quickMessages'; import { SlashCommandPicker } from './SlashCommandPicker'; import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'; import { useI18n } from '../../application/i18n/I18nProvider'; import { createPortal } from 'react-dom'; import type { FormEvent } from 'react'; import { PromptInput, PromptInputFooter, PromptInputSubmit, PromptInputTextarea, PromptInputTools, } from '../ai-elements/prompt-input'; import type { PromptInputStatus } from '../ai-elements/prompt-input'; import { formatThinkingLabel } from '../../infrastructure/ai/types'; import type { AgentModelPreset, AIPermissionMode, ProviderConfig, UploadedFile } from '../../infrastructure/ai/types'; import { ProviderIconBadge } from '../settings/tabs/ai/ProviderIconBadge'; import { ScrollArea } from '../ui/scroll-area'; import { Tooltip, TooltipContent, TooltipTrigger } from '../ui/tooltip'; // Keep in sync with the popover's Tailwind max-width below. const MODEL_PICKER_MAX_WIDTH = 360; // Slightly wider for the provider picker so the per-row default-model // caption doesn't truncate. const PROVIDER_PICKER_MAX_WIDTH = 320; /** * Provider picker payload used by Catty Agent. When set, the model chip * switches to a flat provider list (provider icon + name + the provider's * configured default model as caption) in place of the generic Cpu glyph * + model-preset dropdown. Each provider exposes a single model — its * `defaultModel` — so a two-level menu would be empty noise; picking a * provider implicitly picks its model. */ export interface ProviderSwitcherConfig { /** Every configured provider — Settings-level visibility, not the * `enabled` toggle, since the user expects to swap between everything * they've set up. */ providers: ProviderConfig[]; /** Currently bound provider id (falls back to providers[0] when missing). */ selectedProviderId?: string; /** Currently bound model id under the selected provider. */ selectedModelId?: string; /** Fires when the user picks a (providerId, modelId) pair. */ onSelect: (providerId: string, modelId: string) => void; } interface ChatInputProps { value: string; onChange: (value: string) => void; onSend: () => void; onStop?: () => void; isStreaming?: boolean; disabled?: boolean; providerName?: string; modelName?: string; agentName?: string; placeholder?: string; /** Available model presets for the current agent */ modelPresets?: AgentModelPreset[]; /** Currently selected model ID */ selectedModelId?: string; /** Callback when user selects a model */ onModelSelect?: (modelId: string) => void; /** Attached files (images, PDFs, etc.) */ files?: UploadedFile[]; /** Callback to add files (paste/drop) */ onAddFiles?: (files: File[]) => void; /** Callback to remove a file */ onRemoveFile?: (id: string) => void; /** Available hosts for @ mention */ hosts?: Array<{ sessionId: string; hostname: string; label: string; connected: boolean }>; /** User skills currently selected for the next send */ selectedUserSkills?: Array<{ id: string; slug: string; name: string; description: string }>; /** Available user skills for /skill-slug insertion */ userSkills?: Array<{ id: string; slug: string; name: string; description: string }>; /** Custom slash prompts configured in Settings → AI */ quickMessages?: AIQuickMessage[]; /** Callback to add a selected user skill */ onAddUserSkill?: (slug: string) => void; /** Callback to remove a selected user skill */ onRemoveUserSkill?: (slug: string) => void; /** Permission mode (only shown for Catty Agent) */ permissionMode?: AIPermissionMode; /** Callback when user changes permission mode */ onPermissionModeChange?: (mode: AIPermissionMode) => void; /** * Provider→model two-level picker payload. When provided, replaces the * single-list model dropdown with a provider-aware picker. Used for the * Catty Agent only — external SDK agents (Claude/Codex) keep the * `modelPresets` dropdown because their provider is wired inside the CLI. */ providerSwitcher?: ProviderSwitcherConfig; } const ChatInput: React.FC = ({ value, onChange, onSend, onStop, isStreaming = false, disabled = false, providerName, modelName, agentName, placeholder, modelPresets = [], selectedModelId, onModelSelect, files = [], onAddFiles, onRemoveFile, hosts = [], selectedUserSkills = [], userSkills = [], quickMessages = [], onAddUserSkill, onRemoveUserSkill, permissionMode, onPermissionModeChange, providerSwitcher, }) => { const { t } = useI18n(); const hasTerminalSelectionAttachment = files.some((file) => file.terminalSelection); const [expanded, setExpanded] = useState(false); // Consolidate menu state into a single discriminated union to prevent multiple menus open simultaneously type ActiveMenu = 'model' | 'attach' | 'atMention' | 'slashCommand' | 'perm' | null; const [activeMenu, setActiveMenu] = useState(null); const [menuPos, setMenuPos] = useState<{ left: number; bottom: number } | null>(null); const [inputPanelPos, setInputPanelPos] = useState<{ left: number; bottom: number; width: number } | null>(null); const [hoveredModelId, setHoveredModelId] = useState(null); const [slashQuery, setSlashQuery] = useState(''); const [slashRange, setSlashRange] = useState<{ start: number; end: number } | null>(null); // Active highlight index for @ mention / slash skill keyboard navigation const [activeMenuIndex, setActiveMenuIndex] = useState(0); // Derived booleans for readability const showModelPicker = activeMenu === 'model'; const showAttachMenu = activeMenu === 'attach'; const showAtMention = activeMenu === 'atMention'; const showSlashCommandPicker = activeMenu === 'slashCommand'; const showPermPicker = activeMenu === 'perm'; const closeAllMenus = useCallback(() => { setActiveMenu(null); setMenuPos(null); setInputPanelPos(null); setHoveredModelId(null); setSlashQuery(''); setSlashRange(null); }, []); const textareaRef = useRef(null); const inputShellRef = useRef(null); const modelBtnRef = useRef(null); const permBtnRef = useRef(null); const attachBtnRef = useRef(null); const quickMsgBtnRef = useRef(null); const slashPickerListRef = useRef(null); const fileInputRef = useRef(null); const findSlashTrigger = useCallback((text: string, caretPosition: number) => { const beforeCaret = text.slice(0, caretPosition); const match = /(^|\s)\/([a-z0-9-]*)$/i.exec(beforeCaret); if (!match) return null; const start = beforeCaret.length - match[0].length + match[1].length; return { start, end: beforeCaret.length, query: String(match[2] || '').toLowerCase(), }; }, []); const getInputPanelMenuPos = useCallback(() => { const rect = inputShellRef.current?.getBoundingClientRect(); if (!rect) return null; const horizontalMargin = 12; const safeRight = window.innerWidth - horizontalMargin; const width = Math.min(rect.width, safeRight - rect.left); return { left: rect.left, bottom: window.innerHeight - rect.top + 8, width, }; }, []); const handleInputChange = useCallback((newValue: string) => { onChange(newValue); const caretPosition = textareaRef.current?.selectionStart ?? newValue.length; // Detect if user just typed @ if ( hosts.length > 0 && newValue.length > value.length && newValue.endsWith('@') ) { // Position the popover near the textarea const pos = getInputPanelMenuPos(); if (pos) setInputPanelPos(pos); setActiveMenu('atMention'); return; } const slashTrigger = findSlashTrigger(newValue, caretPosition); if (slashTrigger) { const pos = getInputPanelMenuPos(); if (pos) { setMenuPos(null); setInputPanelPos(pos); } setSlashQuery(slashTrigger.query); setSlashRange({ start: slashTrigger.start, end: slashTrigger.end }); setActiveMenu('slashCommand'); return; } if (showAtMention && !newValue.includes('@')) { setActiveMenu(null); } else if (showSlashCommandPicker) { closeAllMenus(); } }, [onChange, value, hosts.length, showAtMention, findSlashTrigger, showSlashCommandPicker, closeAllMenus, getInputPanelMenuPos]); const handleSelectAtMention = useCallback((host: { label: string; hostname: string }) => { // Replace the trailing @ with @hostname const name = host.label || host.hostname; const lastAt = value.lastIndexOf('@'); const newValue = lastAt >= 0 ? value.slice(0, lastAt) + `@${name} ` : value + `@${name} `; onChange(newValue); closeAllMenus(); }, [value, onChange, closeAllMenus]); const openInputPanelMenu = useCallback((menu: 'atMention' | 'slashCommand') => { const pos = getInputPanelMenuPos(); if (!pos) return; setMenuPos(null); setInputPanelPos(pos); if (menu === 'slashCommand') { const caret = textareaRef.current?.selectionStart ?? value.length; const trigger = findSlashTrigger(value, caret); if (trigger) { setSlashQuery(trigger.query); setSlashRange({ start: trigger.start, end: trigger.end }); } else { setSlashQuery(''); setSlashRange(null); } } setActiveMenu(menu); }, [findSlashTrigger, getInputPanelMenuPos, value]); const openSlashCommandPicker = useCallback((anchor?: 'toolbar') => { if (anchor === 'toolbar') { const rect = quickMsgBtnRef.current?.getBoundingClientRect(); if (rect) { setMenuPos({ left: rect.left, bottom: window.innerHeight - rect.top + 6 }); } setInputPanelPos(null); const caret = textareaRef.current?.selectionStart ?? value.length; const trigger = findSlashTrigger(value, caret); if (trigger) { setSlashQuery(trigger.query); setSlashRange({ start: trigger.start, end: trigger.end }); } else { setSlashQuery(''); setSlashRange(null); } setActiveMenu('slashCommand'); return; } openInputPanelMenu('slashCommand'); }, [findSlashTrigger, openInputPanelMenu, value]); const userSkillOptions = useMemo( () => userSkills.map((skill) => ({ id: skill.id, slug: skill.slug, name: skill.name, description: skill.description, })), [userSkills], ); const quickMessageSlugSet = useMemo( () => new Set(quickMessages.map((message) => message.slug)), [quickMessages], ); const filteredQuickMessages = useMemo( () => filterQuickMessages(quickMessages, slashQuery), [quickMessages, slashQuery], ); const filteredUserSkills = useMemo( () => filterUserSkillsForSlash(userSkillOptions, slashQuery) .filter((skill) => !quickMessageSlugSet.has(skill.slug)), [userSkillOptions, slashQuery, quickMessageSlugSet], ); const slashCommandItems = useMemo( () => buildSlashCommandItems(quickMessages, userSkillOptions, slashQuery), [quickMessages, userSkillOptions, slashQuery], ); const isSlashCatalogEmpty = quickMessages.length === 0 && userSkills.length === 0; const slashPickerNoResultsLabel = isSlashCatalogEmpty ? t('ai.chat.slashEmptyHint') : t('ai.chat.slashNoResults'); const slashPickerListboxId = menuPos ? 'slash-command-toolbar' : 'slash-command-input'; const showSlashPickerUI = showSlashCommandPicker && (inputPanelPos != null || menuPos != null); const removeSlashQueryFromInput = useCallback(() => { if (!slashRange) return value; const before = value.slice(0, slashRange.start); const after = value.slice(slashRange.end); if (/\s$/.test(before) && /^\s/.test(after)) { return `${before}${after.slice(1)}`; } return `${before}${after}`; }, [slashRange, value]); const insertUserSkillToken = useCallback((skill: { slug: string }) => { onAddUserSkill?.(skill.slug); if (slashRange) { onChange(removeSlashQueryFromInput()); } closeAllMenus(); }, [closeAllMenus, onAddUserSkill, onChange, removeSlashQueryFromInput, slashRange]); const insertQuickMessage = useCallback((message: AIQuickMessage) => { if (slashRange) { const before = value.slice(0, slashRange.start); const after = value.slice(slashRange.end); const spacerBefore = before.length > 0 && !/\s$/.test(before) ? ' ' : ''; const spacerAfter = after.length > 0 && !/^\s/.test(after) ? ' ' : ''; onChange(`${before}${spacerBefore}${message.content}${spacerAfter}${after}`); } else { const spacer = value.length > 0 && !/\s$/.test(value) ? ' ' : ''; onChange(`${value}${spacer}${message.content}`); } closeAllMenus(); }, [closeAllMenus, onChange, slashRange, value]); const handleSelectSlashCommandItem = useCallback((item: SlashCommandItem) => { if (item.kind === 'quickMessage') { insertQuickMessage(item.message); return; } insertUserSkillToken(item.skill); }, [insertQuickMessage, insertUserSkillToken]); // Reset active highlight when a menu opens or when the *identity* of the // visible items changes. Watching only `.length` misses cases where the // filter produces a different set with the same count (e.g. user types // another character into the slash query) — Enter would then commit an // unexpected item. Derive a stable key from the visible ids instead. const atMentionKey = useMemo( () => hosts.map((h) => h.sessionId).join('|'), [hosts], ); const slashCommandKey = useMemo( () => slashCommandItems.map(getSlashCommandItemKey).join('|'), [slashCommandItems], ); useEffect(() => { if (showAtMention) setActiveMenuIndex(0); }, [showAtMention, atMentionKey]); useEffect(() => { if (showSlashCommandPicker) setActiveMenuIndex(0); }, [showSlashCommandPicker, slashCommandKey]); useEffect(() => { if (!showSlashCommandPicker || !menuPos || slashCommandItems.length === 0) return; slashPickerListRef.current?.focus(); }, [showSlashCommandPicker, menuPos, slashCommandKey, slashCommandItems.length]); const handleSlashCommandKeyDown = useCallback((e: KeyboardEvent | React.KeyboardEvent) => { if ('nativeEvent' in e && e.nativeEvent.isComposing) return; if (e.key === 'Escape') { e.preventDefault(); closeAllMenus(); return; } if (e.key === 'Enter') { if ('shiftKey' in e && e.shiftKey) { return; } if (slashCommandItems.length > 0) { e.preventDefault(); const item = slashCommandItems[Math.min(activeMenuIndex, slashCommandItems.length - 1)]; if (item) handleSelectSlashCommandItem(item); return; } // Mid-slash token with no matches: block accidental send of "/query" text. if (slashRange) { e.preventDefault(); } return; } if (slashCommandItems.length === 0) return; if (e.key === 'ArrowDown') { e.preventDefault(); setActiveMenuIndex((i) => (i + 1) % slashCommandItems.length); return; } if (e.key === 'ArrowUp') { e.preventDefault(); setActiveMenuIndex((i) => (i - 1 + slashCommandItems.length) % slashCommandItems.length); return; } }, [activeMenuIndex, closeAllMenus, handleSelectSlashCommandItem, slashCommandItems, slashRange]); useEffect(() => { if (!showSlashCommandPicker || !menuPos) return; const onKeyDown = (event: KeyboardEvent) => handleSlashCommandKeyDown(event); window.addEventListener('keydown', onKeyDown, true); return () => window.removeEventListener('keydown', onKeyDown, true); }, [handleSlashCommandKeyDown, menuPos, showSlashCommandPicker]); const handleTextareaKeyDown = useCallback((e: React.KeyboardEvent) => { if (e.nativeEvent.isComposing) return; // @ mention popover keyboard navigation if (showAtMention && hosts.length > 0) { if (e.key === 'ArrowDown') { e.preventDefault(); setActiveMenuIndex((i) => (i + 1) % hosts.length); return; } if (e.key === 'ArrowUp') { e.preventDefault(); setActiveMenuIndex((i) => (i - 1 + hosts.length) % hosts.length); return; } if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); const host = hosts[Math.min(activeMenuIndex, hosts.length - 1)]; if (host) handleSelectAtMention(host); return; } if (e.key === 'Escape') { e.preventDefault(); closeAllMenus(); return; } } // / command popover keyboard navigation (input-anchored picker) if (showSlashCommandPicker && !menuPos) { handleSlashCommandKeyDown(e); return; } }, [showAtMention, hosts, showSlashCommandPicker, menuPos, activeMenuIndex, handleSelectAtMention, handleSlashCommandKeyDown, closeAllMenus]); const handlePaste = useCallback((e: React.ClipboardEvent) => { const pastedFiles = Array.from(e.clipboardData.items) .map((item: DataTransferItem) => item.getAsFile()) .filter((f): f is File => !!f); if (pastedFiles.length > 0) { e.preventDefault(); onAddFiles?.(pastedFiles); } }, [onAddFiles]); const handleDrop = useCallback((e: React.DragEvent) => { e.preventDefault(); const droppedFiles = Array.from(e.dataTransfer.files); if (droppedFiles.length > 0) { onAddFiles?.(droppedFiles); } }, [onAddFiles]); const defaultPlaceholder = agentName ? t('ai.chat.placeholder').replace('{agent}', agentName) : t('ai.chat.placeholderDefault'); const handleSubmit = useCallback( (_text: string, _event: FormEvent) => { onSend(); }, [onSend], ); const status: PromptInputStatus = isStreaming ? 'streaming' : 'idle'; // Permission mode chip removed — agents run in autonomous mode // selectedModelId may be "/" for codex ChatGPT models // (e.g. "gpt-5.4/high"). Note: custom config.toml / OpenRouter model ids // themselves can contain '/' (e.g. "qwen/qwen3.6-plus"), so don't just // split on the first '/'. Match against the full id first; only treat the // trailing segment as a thinking level when we find a preset whose // declared thinkingLevels make the combined form equal to selectedModelId. const { selectedPreset, selectedThinking } = (() => { if (!selectedModelId) return { selectedPreset: undefined, selectedThinking: undefined }; const direct = modelPresets.find(m => m.id === selectedModelId); if (direct) return { selectedPreset: direct, selectedThinking: undefined }; const viaThinking = modelPresets.find( m => m.thinkingLevels?.some(level => `${m.id}/${level}` === selectedModelId), ); if (viaThinking) { const thinking = selectedModelId.slice(viaThinking.id.length + 1); return { selectedPreset: viaThinking, selectedThinking: thinking }; } return { selectedPreset: undefined, selectedThinking: undefined }; })(); const selectedBaseModelId = selectedPreset?.id; // Provider switcher mode (Catty Agent): two-column popover, chip carries // the provider's icon + name + model name. Falls back to the existing // single-list model dropdown for external SDK agents. const hasProviderSwitcher = !!providerSwitcher && providerSwitcher.providers.length > 0; // Resolve to the actually-bound provider only — no `?? providers[0]` // fallback, since a provider that isn't really bound will still hit the // `!sendActiveProvider` guard at send time. Faking a selection in the // chip would lie about a state the rest of the system treats as empty. const selectedSwitcherProvider = hasProviderSwitcher ? providerSwitcher!.providers.find((p) => p.id === providerSwitcher!.selectedProviderId) : undefined; const providerSwitcherChipLabel = hasProviderSwitcher ? (selectedSwitcherProvider ? (providerSwitcher!.selectedModelId ? `${selectedSwitcherProvider.name} · ${providerSwitcher!.selectedModelId}` : selectedSwitcherProvider.name) : t('ai.chat.selectProvider')) : ''; const modelLabel = hasProviderSwitcher ? providerSwitcherChipLabel : (selectedPreset ? selectedPreset.name + (selectedThinking ? ` / ${formatThinkingLabel(selectedThinking)}` : '') : modelName || providerName || t('ai.chat.noModel')); const hasModelPicker = hasProviderSwitcher || (modelPresets.length > 0 && !!onModelSelect); const popoverMaxWidth = hasProviderSwitcher ? PROVIDER_PICKER_MAX_WIDTH : MODEL_PICKER_MAX_WIDTH; const chipClassName = 'inline-flex h-6 items-center gap-1 rounded-full px-1.5 text-[10.5px] text-foreground/72'; const selectedSkillChipClassName = 'inline-flex h-7 items-center gap-1.5 rounded-full border border-primary/18 bg-primary/8 pl-2.5 pr-1.5 text-[11px] font-medium text-foreground/86 shadow-[inset_0_1px_0_rgba(255,255,255,0.06)]'; const iconButtonClassName = 'h-6 w-6 shrink-0 rounded-full bg-transparent text-foreground/62 hover:bg-muted/24 hover:text-foreground'; return (
{/* File attachment chips */} {files.length > 0 && (
{files.map((file) => (
{file.terminalSelection ? ( ) : file.mediaType.startsWith('image/') ? ( ) : ( )} {file.terminalSelection ? ( {t('ai.chat.terminalSelectionAttachment')} {file.lineCount ? ` · ${t('ai.chat.terminalSelectionLines').replace('{count}', String(file.lineCount))}` : ''} ) : ( {file.filename} )}
))}
)} {/* Hidden file input */} { if (e.target.files?.length) { onAddFiles?.(Array.from(e.target.files)); e.target.value = ''; } }} /> {/* Textarea with expand toggle */}
e.preventDefault()}> {selectedUserSkills.length > 0 && (
{selectedUserSkills.map((skill) => (
{skill.name && skill.name !== skill.slug ? skill.name : `/${skill.slug}`}
{skill.description || skill.name || skill.slug}
))}
)} handleInputChange(e.target.value)} onKeyDown={handleTextareaKeyDown} placeholder={placeholder || defaultPlaceholder} disabled={disabled} className={[ selectedUserSkills.length > 0 ? 'pt-1.5' : undefined, expanded ? 'max-h-[220px]' : undefined, ].filter(Boolean).join(' ')} maxLength={100000} /> {expanded ? t('ai.chat.collapse') : t('ai.chat.expand')}
{/* @ mention popover */} {showAtMention && hosts.length > 0 && inputPanelPos && createPortal( <>
{hosts.map((host, idx) => { const isActive = idx === activeMenuIndex; const showHostnameLine = host.label && host.hostname !== host.label && !host.label.includes(host.hostname); return ( ); })}
, document.body, )} {/* / command popover */} {showSlashPickerUI && createPortal( <>
, document.body, )} {/* Footer toolbar */} {t('ai.chat.attach')} {showAttachMenu && menuPos && createPortal( <>
{t('ai.chat.menuContext')}
, document.body, )} {showModelPicker && hasModelPicker && menuPos && createPortal( <>
setHoveredModelId(null)} > {hasProviderSwitcher ? (
{providerSwitcher!.providers.map((p) => { const isSelected = providerSwitcher!.selectedProviderId === p.id; const defaultModel = p.defaultModel?.trim() ?? ''; const hasModel = defaultModel.length > 0; // Rows without a defaultModel are inert — picking // one would save a binding with an empty model id // and produce a confusing model error at send time. // User has to set a defaultModel in Settings first. const disabled = !hasModel; const modelCaption = hasModel ? defaultModel : t('ai.chat.noProviderModel'); return ( ); })}
) : modelPresets.map(preset => { const isSelected = preset.id === selectedBaseModelId; const hasThinking = preset.thinkingLevels && preset.thinkingLevels.length > 0; return (
setHoveredModelId(hasThinking ? preset.id : null)} onFocus={() => { if (hasThinking) setHoveredModelId(preset.id); }} onBlur={(e) => { if (!e.currentTarget.contains(e.relatedTarget)) setHoveredModelId(null); }} > {/* Thinking level sub-menu */} {hasThinking && hoveredModelId === preset.id && (
{preset.thinkingLevels!.map(level => { const fullId = `${preset.id}/${level}`; const isLevelSelected = selectedModelId === fullId; return ( ); })}
)}
); })}
, document.body, )} {/* Permission mode chip — only for Catty Agent */} {permissionMode && onPermissionModeChange && ( <> {t('ai.safety.permissionMode')} {showPermPicker && menuPos && createPortal( <>
{([ { mode: 'autonomous' as const, icon: Zap, color: 'text-green-400/70', label: t('ai.chat.permAuto'), desc: t('ai.chat.permAutoDesc') }, { mode: 'confirm' as const, icon: ShieldCheck, color: 'text-yellow-400/70', label: t('ai.chat.permConfirm'), desc: t('ai.chat.permConfirmDesc') }, { mode: 'observer' as const, icon: Eye, color: 'text-blue-400/70', label: t('ai.chat.permObserver'), desc: t('ai.chat.permObserverDesc') }, ]).map(({ mode, icon: Icon, color, label, desc }) => ( ))}
, document.body, )} )}
{t('ai.chat.slashCommands')}
); }; export default React.memo(ChatInput);