/** * Chat Input Component * Textarea with send button and universal file upload support. * Enter to send, Shift+Enter for new line. * Supports: native file picker, clipboard paste, drag & drop. * Files are staged to disk via IPC — only lightweight path references * are sent with the message (no base64 over WebSocket). */ import { useState, useRef, useEffect, useCallback, useMemo } from 'react'; import { Check, ChevronDown, SendHorizontal, Square, X, Paperclip, FileText, Film, Music, FileArchive, File, Loader2, BookOpen } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Textarea } from '@/components/ui/textarea'; import { hostApiFetch } from '@/lib/host-api'; import { invokeIpc } from '@/lib/api-client'; import { buildQuickTaskPrompt } from '@/lib/quick-task-prompt'; import { cn } from '@/lib/utils'; import { useGatewayStore } from '@/stores/gateway'; import { useQuickTasksStore } from '@/stores/quick-tasks'; import { useTranslation } from 'react-i18next'; // ── Types ──────────────────────────────────────────────────────── export interface FileAttachment { id: string; fileName: string; mimeType: string; fileSize: number; stagedPath: string; // disk path for gateway preview: string | null; // data URL for images, null for others status: 'staging' | 'ready' | 'error'; error?: string; } export interface KnowledgeContextDocument { id: string; name: string; mimeType: string; storedPath: string; textPath?: string; } const EMPTY_KNOWLEDGE_DOCUMENTS: KnowledgeContextDocument[] = []; interface ChatInputProps { onSend: ( text: string, attachments?: FileAttachment[], targetAgentId?: string | null, options?: { useKnowledgeBase?: boolean; workspaceId?: string; selectedKnowledgeDocumentIds?: string[]; quickTaskContext?: { taskNames: string[]; skillIds: string[]; prompt: string }; }, ) => void; onStop?: () => void; disabled?: boolean; sending?: boolean; isEmpty?: boolean; knowledgeDocuments?: KnowledgeContextDocument[]; workspaceId?: string; } // ── Helpers ────────────────────────────────────────────────────── function formatFileSize(bytes: number): string { if (bytes < 1024) return `${bytes} B`; if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`; if (bytes < 1024 * 1024 * 1024) return `${(bytes / (1024 * 1024)).toFixed(2)} MB`; return `${(bytes / (1024 * 1024 * 1024)).toFixed(2)} GB`; } function FileIcon({ mimeType, className }: { mimeType: string; className?: string }) { if (mimeType.startsWith('video/')) return ; if (mimeType.startsWith('audio/')) return ; if (mimeType.startsWith('text/') || mimeType === 'application/json' || mimeType === 'application/xml') return ; if (mimeType.includes('zip') || mimeType.includes('compressed') || mimeType.includes('archive') || mimeType.includes('tar') || mimeType.includes('rar') || mimeType.includes('7z')) return ; if (mimeType === 'application/pdf') return ; return ; } /** * Read a browser File object as base64 string (without the data URL prefix). */ function readFileAsBase64(file: globalThis.File): Promise { return new Promise((resolve, reject) => { const reader = new FileReader(); reader.onload = () => { const dataUrl = reader.result as string; if (!dataUrl || !dataUrl.includes(',')) { reject(new Error(`Invalid data URL from FileReader for ${file.name}`)); return; } const base64 = dataUrl.split(',')[1]; if (!base64) { reject(new Error(`Empty base64 data for ${file.name}`)); return; } resolve(base64); }; reader.onerror = () => reject(new Error(`Failed to read file: ${file.name}`)); reader.readAsDataURL(file); }); } // ── Component ──────────────────────────────────────────────────── export function ChatInput({ onSend, onStop, disabled = false, sending = false, isEmpty = false, knowledgeDocuments = EMPTY_KNOWLEDGE_DOCUMENTS, workspaceId, }: ChatInputProps) { const { t } = useTranslation('chat'); const [input, setInput] = useState(''); const [attachments, setAttachments] = useState([]); const [knowledgePickerOpen, setKnowledgePickerOpen] = useState(false); const [selectedKnowledgeIds, setSelectedKnowledgeIds] = useState([]); const [selectedQuickTaskIds, setSelectedQuickTaskIds] = useState([]); const textareaRef = useRef(null); const knowledgePickerRef = useRef(null); const isComposingRef = useRef(false); const quickTaskPromptRef = useRef(''); const gatewayStatus = useGatewayStore((s) => s.status); const quickTasks = useQuickTasksStore((state) => state.tasks); const knowledgeBaseAvailable = knowledgeDocuments.length > 0; const composerQuickTasks = useMemo( () => quickTasks.filter((task) => task.enabled && task.showInComposer), [quickTasks], ); const selectedQuickTasks = useMemo( () => composerQuickTasks.filter((task) => selectedQuickTaskIds.includes(task.id)), [composerQuickTasks, selectedQuickTaskIds], ); const selectedQuickTaskPrompt = useMemo( () => buildQuickTaskPrompt(selectedQuickTasks), [selectedQuickTasks], ); const selectedKnowledgeDocuments = knowledgeDocuments.filter((doc) => selectedKnowledgeIds.includes(doc.id)); // Auto-resize textarea useEffect(() => { if (textareaRef.current) { textareaRef.current.style.height = 'auto'; textareaRef.current.style.height = `${Math.min(textareaRef.current.scrollHeight, 240)}px`; } }, [input]); // Focus textarea on mount (avoids Windows focus loss after session delete + native dialog) useEffect(() => { if (!disabled && textareaRef.current) { textareaRef.current.focus(); } }, [disabled]); useEffect(() => { setSelectedKnowledgeIds((current) => { const next = current.filter((id) => knowledgeDocuments.some((doc) => doc.id === id)); return next.length === current.length ? current : next; }); }, [knowledgeDocuments]); useEffect(() => { setSelectedQuickTaskIds((current) => { const next = current.filter((id) => composerQuickTasks.some((task) => task.id === id)); return next.length === current.length ? current : next; }); }, [composerQuickTasks]); useEffect(() => { const previousPrompt = quickTaskPromptRef.current; const nextPrompt = selectedQuickTaskPrompt.trim(); if (previousPrompt === nextPrompt) return; setInput((current) => { let body = current; if (previousPrompt) { if (body === previousPrompt) { body = ''; } else if (body.startsWith(`${previousPrompt} `)) { body = body.slice(previousPrompt.length + 1); } else if (body.startsWith(`${previousPrompt}\n\n`)) { body = body.slice(previousPrompt.length + 2); } } return body.trimStart(); }); quickTaskPromptRef.current = nextPrompt; }, [selectedQuickTaskPrompt]); useEffect(() => { if (!knowledgePickerOpen) return; const handlePointerDown = (event: MouseEvent) => { if (!knowledgePickerRef.current?.contains(event.target as Node)) { setKnowledgePickerOpen(false); } }; document.addEventListener('mousedown', handlePointerDown); return () => { document.removeEventListener('mousedown', handlePointerDown); }; }, [knowledgePickerOpen]); // ── File staging via native dialog ───────────────────────────── const pickFiles = useCallback(async () => { try { const result = await invokeIpc('dialog:open', { properties: ['openFile', 'multiSelections'], }) as { canceled: boolean; filePaths?: string[] }; if (result.canceled || !result.filePaths?.length) return; // Add placeholder entries immediately const tempIds: string[] = []; for (const filePath of result.filePaths) { const tempId = crypto.randomUUID(); tempIds.push(tempId); // Handle both Unix (/) and Windows (\) path separators const fileName = filePath.split(/[\\/]/).pop() || 'file'; setAttachments(prev => [...prev, { id: tempId, fileName, mimeType: '', fileSize: 0, stagedPath: '', preview: null, status: 'staging' as const, }]); } // Stage all files via IPC console.log('[pickFiles] Staging files:', result.filePaths); const staged = await hostApiFetch>('/api/files/stage-paths', { method: 'POST', body: JSON.stringify({ filePaths: result.filePaths }), }); console.log('[pickFiles] Stage result:', staged?.map(s => ({ id: s?.id, fileName: s?.fileName, mimeType: s?.mimeType, fileSize: s?.fileSize, stagedPath: s?.stagedPath, hasPreview: !!s?.preview }))); // Update each placeholder with real data setAttachments(prev => { let updated = [...prev]; for (let i = 0; i < tempIds.length; i++) { const tempId = tempIds[i]; const data = staged[i]; if (data) { updated = updated.map(a => a.id === tempId ? { ...data, status: 'ready' as const } : a, ); } else { console.warn(`[pickFiles] No staged data for tempId=${tempId} at index ${i}`); updated = updated.map(a => a.id === tempId ? { ...a, status: 'error' as const, error: 'Staging failed' } : a, ); } } return updated; }); } catch (err) { console.error('[pickFiles] Failed to stage files:', err); // Mark any stuck 'staging' attachments as 'error' so the user can remove them // and the send button isn't permanently blocked setAttachments(prev => prev.map(a => a.status === 'staging' ? { ...a, status: 'error' as const, error: String(err) } : a, )); } }, []); // ── Stage browser File objects (paste / drag-drop) ───────────── const stageBufferFiles = useCallback(async (files: globalThis.File[]) => { for (const file of files) { const tempId = crypto.randomUUID(); setAttachments(prev => [...prev, { id: tempId, fileName: file.name, mimeType: file.type || 'application/octet-stream', fileSize: file.size, stagedPath: '', preview: null, status: 'staging' as const, }]); try { console.log(`[stageBuffer] Reading file: ${file.name} (${file.type}, ${file.size} bytes)`); const base64 = await readFileAsBase64(file); console.log(`[stageBuffer] Base64 length: ${base64?.length ?? 'null'}`); const staged = await hostApiFetch<{ id: string; fileName: string; mimeType: string; fileSize: number; stagedPath: string; preview: string | null; }>('/api/files/stage-buffer', { method: 'POST', body: JSON.stringify({ base64, fileName: file.name, mimeType: file.type || 'application/octet-stream', }), }); console.log(`[stageBuffer] Staged: id=${staged?.id}, path=${staged?.stagedPath}, size=${staged?.fileSize}`); setAttachments(prev => prev.map(a => a.id === tempId ? { ...staged, status: 'ready' as const } : a, )); } catch (err) { console.error(`[stageBuffer] Error staging ${file.name}:`, err); setAttachments(prev => prev.map(a => a.id === tempId ? { ...a, status: 'error' as const, error: String(err) } : a, )); } } }, []); // ── Attachment management ────────────────────────────────────── const removeAttachment = useCallback((id: string) => { setAttachments(prev => prev.filter(a => a.id !== id)); }, []); const allReady = attachments.length === 0 || attachments.every(a => a.status === 'ready'); const hasFailedAttachments = attachments.some((a) => a.status === 'error'); const canSend = (input.trim() || attachments.length > 0 || selectedQuickTasks.length > 0) && allReady && !disabled && !sending; const canStop = sending && !disabled && !!onStop; const handleSend = useCallback(() => { if (!canSend) return; const readyAttachments = attachments.filter(a => a.status === 'ready'); // Capture values before clearing — clear input immediately for snappy UX, // but keep attachments available for the async send const textToSend = [selectedQuickTaskPrompt, input.trim()].filter(Boolean).join(' '); const attachmentsToSend = readyAttachments.length > 0 ? readyAttachments : undefined; console.log(`[handleSend] text="${textToSend.substring(0, 50)}", attachments=${attachments.length}, ready=${readyAttachments.length}, sending=${!!attachmentsToSend}`); if (attachmentsToSend) { console.log('[handleSend] Attachment details:', attachmentsToSend.map(a => ({ id: a.id, fileName: a.fileName, mimeType: a.mimeType, fileSize: a.fileSize, stagedPath: a.stagedPath, status: a.status, hasPreview: !!a.preview, }))); } if (selectedQuickTasks.length > 0) { console.log('[handleSend] Quick task prompt:', selectedQuickTaskPrompt); } setInput(''); setAttachments([]); if (textareaRef.current) { textareaRef.current.style.height = 'auto'; } onSend(textToSend, attachmentsToSend, null, { useKnowledgeBase: selectedKnowledgeIds.length > 0, workspaceId, selectedKnowledgeDocumentIds: selectedKnowledgeIds, }); setKnowledgePickerOpen(false); setSelectedQuickTaskIds([]); quickTaskPromptRef.current = ''; }, [input, attachments, canSend, onSend, selectedKnowledgeIds, workspaceId, selectedQuickTasks, selectedQuickTaskPrompt]); const handleStop = useCallback(() => { if (!canStop) return; onStop?.(); }, [canStop, onStop]); const handleKeyDown = useCallback( (e: React.KeyboardEvent) => { if (e.key === 'Enter' && !e.shiftKey) { const nativeEvent = e.nativeEvent as KeyboardEvent; if (isComposingRef.current || nativeEvent.isComposing || nativeEvent.keyCode === 229) { return; } e.preventDefault(); handleSend(); } }, [handleSend], ); const toggleKnowledgeDocument = useCallback((id: string) => { setSelectedKnowledgeIds((current) => ( current.includes(id) ? current.filter((item) => item !== id) : [...current, id] )); }, []); const toggleQuickTask = useCallback((id: string) => { setSelectedQuickTaskIds((current) => ( current.includes(id) ? [] : [id] )); }, []); // Handle paste (Ctrl/Cmd+V with files) const handlePaste = useCallback( (e: React.ClipboardEvent) => { const items = e.clipboardData?.items; if (!items) return; const pastedFiles: globalThis.File[] = []; for (const item of Array.from(items)) { if (item.kind === 'file') { const file = item.getAsFile(); if (file) pastedFiles.push(file); } } if (pastedFiles.length > 0) { e.preventDefault(); stageBufferFiles(pastedFiles); } }, [stageBufferFiles], ); // Handle drag & drop const [dragOver, setDragOver] = useState(false); const handleDragOver = useCallback((e: React.DragEvent) => { e.preventDefault(); e.stopPropagation(); setDragOver(true); }, []); const handleDragLeave = useCallback((e: React.DragEvent) => { e.preventDefault(); e.stopPropagation(); setDragOver(false); }, []); const handleDrop = useCallback( (e: React.DragEvent) => { e.preventDefault(); e.stopPropagation(); setDragOver(false); if (e.dataTransfer?.files?.length) { stageBufferFiles(Array.from(e.dataTransfer.files)); } }, [stageBufferFiles], ); return (
{/* Attachment Previews */} {attachments.length > 0 && (
{attachments.map((att) => ( removeAttachment(att.id)} /> ))}
)} {composerQuickTasks.length > 0 && (
{composerQuickTasks.map((task) => { const selected = selectedQuickTaskIds.includes(task.id); const skillNames = task.skills.map((skill) => skill.name).filter(Boolean).join('、') || task.name; return (
{task.name}
{task.description || t('composer.quickTaskHelp')}
{skillNames && (
{skillNames}
)}
); })}
)} {/* Input Container */}
{selectedKnowledgeDocuments.length > 0 && (
{selectedKnowledgeDocuments.map((doc) => ( ))}
)} {/* Text Row — flush-left */}
{selectedQuickTaskPrompt && ( textareaRef.current?.focus()} className="mt-0 select-none whitespace-nowrap pr-1.5 font-sans text-[14px] font-normal leading-6 text-muted-foreground/60" > {selectedQuickTaskPrompt} )}