import React, { forwardRef, useEffect, useState } from 'react'; import { Textarea } from '@/components/ui/textarea'; import { Button } from '@/components/ui/button'; import { Square, Loader2, ArrowUp } from 'lucide-react'; import { cn } from '@/lib/utils'; import { UploadedFile } from './chat-input'; import { FileUploadHandler } from './file-upload-handler'; import { VoiceRecorder } from './voice-recorder'; import { ModelSelector } from './model-selector'; import { AgentSelector } from './agent-selector'; import { canAccessModel, SubscriptionStatus } from './_use-model-selection'; import { isLocalMode } from '@/lib/config'; import { useFeatureFlag } from '@/lib/feature-flags'; import { TooltipContent } from '@/components/ui/tooltip'; import { Tooltip } from '@/components/ui/tooltip'; import { TooltipProvider, TooltipTrigger } from '@radix-ui/react-tooltip'; import { BillingModal } from '@/components/billing/billing-modal'; import ChatDropdown from './chat-dropdown'; import { handleFiles } from './file-upload-handler'; interface MessageInputProps { value: string; onChange: (e: React.ChangeEvent) => void; onSubmit: (e: React.FormEvent) => void; onTranscription: (text: string) => void; placeholder: string; loading: boolean; disabled: boolean; isAgentRunning: boolean; onStopAgent?: () => void; isDraggingOver: boolean; uploadedFiles: UploadedFile[]; fileInputRef: React.RefObject; isUploading: boolean; sandboxId?: string; setPendingFiles: React.Dispatch>; setUploadedFiles: React.Dispatch>; setIsUploading: React.Dispatch>; hideAttachments?: boolean; messages?: any[]; // Add messages prop isLoggedIn?: boolean; selectedModel: string; onModelChange: (model: string) => void; modelOptions: any[]; subscriptionStatus: SubscriptionStatus; canAccessModel: (modelId: string) => boolean; refreshCustomModels?: () => void; selectedAgentId?: string; onAgentSelect?: (agentId: string | undefined) => void; enableAdvancedConfig?: boolean; hideAgentSelection?: boolean; } export const MessageInput = forwardRef( ( { value, onChange, onSubmit, onTranscription, placeholder, loading, disabled, isAgentRunning, onStopAgent, isDraggingOver, uploadedFiles, fileInputRef, isUploading, sandboxId, setPendingFiles, setUploadedFiles, setIsUploading, hideAttachments = false, messages = [], isLoggedIn = true, selectedModel, onModelChange, modelOptions, subscriptionStatus, canAccessModel, refreshCustomModels, selectedAgentId, onAgentSelect, enableAdvancedConfig = false, hideAgentSelection = false, }, ref, ) => { const [billingModalOpen, setBillingModalOpen] = useState(false); const { enabled: customAgentsEnabled, loading: flagsLoading } = useFeatureFlag('custom_agents'); useEffect(() => { const textarea = ref as React.RefObject; if (!textarea.current) return; const adjustHeight = () => { textarea.current!.style.height = 'auto'; const newHeight = Math.min( Math.max(textarea.current!.scrollHeight, 24), 200, ); textarea.current!.style.height = `${newHeight}px`; }; adjustHeight(); // Call it twice to ensure proper height calculation adjustHeight(); window.addEventListener('resize', adjustHeight); return () => window.removeEventListener('resize', adjustHeight); }, [value, ref]); const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === 'Enter' && !e.shiftKey && !e.nativeEvent.isComposing) { e.preventDefault(); if ( (value.trim() || uploadedFiles.length > 0) && !loading && (!disabled || isAgentRunning) ) { onSubmit(e as unknown as React.FormEvent); } } }; const handlePaste = (e: React.ClipboardEvent) => { if (!e.clipboardData) return; const items = Array.from(e.clipboardData.items); const imageFiles: File[] = []; for (const item of items) { if (item.kind === 'file' && item.type.startsWith('image/')) { const file = item.getAsFile(); if (file) imageFiles.push(file); } } if (imageFiles.length > 0) { e.preventDefault(); handleFiles( imageFiles, sandboxId, setPendingFiles, setUploadedFiles, setIsUploading, messages, ); } }; const renderDropdown = () => { if (isLoggedIn) { const showAdvancedFeatures = enableAdvancedConfig || (customAgentsEnabled && !flagsLoading); return (
{showAdvancedFeatures && !hideAgentSelection && ( )}
); } return ; } return (