diff --git a/src/components/ModelSelector.tsx b/src/components/ModelSelector.tsx index aa4b7790..590bca08 100644 --- a/src/components/ModelSelector.tsx +++ b/src/components/ModelSelector.tsx @@ -1,4 +1,4 @@ -import { useState, useMemo, forwardRef, useImperativeHandle } from 'react'; +import { useState, useMemo } from 'react'; import { useNavigate } from 'react-router-dom'; import { useTranslation } from 'react-i18next'; import { Check, ChevronDown, Edit3, RefreshCw, AlertCircle, Settings } from 'lucide-react'; @@ -21,7 +21,7 @@ interface ModelSelectorProps { onOpenChange?: (open: boolean) => void; } -export const ModelSelector = forwardRef<{ open: () => void }, ModelSelectorProps>(({ +export function ModelSelector({ value, onChange, className, @@ -29,7 +29,7 @@ export const ModelSelector = forwardRef<{ open: () => void }, ModelSelectorProps placeholder, open: controlledOpen, onOpenChange: setControlledOpen, -}, ref) => { +}: ModelSelectorProps) { const { t } = useTranslation(); const [uncontrolledOpen, setUncontrolledOpen] = useState(false); @@ -37,10 +37,6 @@ export const ModelSelector = forwardRef<{ open: () => void }, ModelSelectorProps const open = controlledOpen !== undefined ? controlledOpen : uncontrolledOpen; const setOpen = setControlledOpen || setUncontrolledOpen; - // Expose open method via ref - useImperativeHandle(ref, () => ({ - open: () => setOpen(true) - }), [setOpen]); const [isCustomInput, setIsCustomInput] = useState(false); const [customValue, setCustomValue] = useState(''); const { settings, addRecentlyUsedModel, isConfigured } = useAISettings(); @@ -292,4 +288,4 @@ export const ModelSelector = forwardRef<{ open: () => void }, ModelSelectorProps ); -}); \ No newline at end of file +} \ No newline at end of file diff --git a/src/components/Shakespeare/ChatPane.tsx b/src/components/Shakespeare/ChatPane.tsx index bfad8b6e..51ce0225 100644 --- a/src/components/Shakespeare/ChatPane.tsx +++ b/src/components/Shakespeare/ChatPane.tsx @@ -53,6 +53,10 @@ type AlertType = typeof ALERT_TYPE[keyof typeof ALERT_TYPE]; interface ChatAlert { type: AlertType; message: string; + action?: { + label: string; + onClick: () => void; + }; } function ChatIntervention({ alert, onDismiss }: { alert: ChatAlert; onDismiss: () => void }) { @@ -61,16 +65,34 @@ function ChatIntervention({ alert, onDismiss }: { alert: ChatAlert; onDismiss: ( return ( {isError ? : } - - {alert.message} - + +
+ {alert.message} + +
+ {alert.action && ( +
+ +
+ )}
); @@ -80,41 +102,62 @@ interface ErrorPattern { test: (content: string) => boolean; type: AlertType; message: string; + action?: { + label: string; + onClick: () => void; + }; } -const ERROR_PATTERNS: ErrorPattern[] = [ - { - test: (content) => /AI service error: 422/.test(content) || /Provider returned error/.test(content), - type: ALERT_TYPE.ERROR, - message: 'The AI model encountered an issue. Try switching to a different model or starting a new chat.' - }, - { - test: (content) => /maximum context length is \d+ tokens/.test(content) || /context length/.test(content), - type: ALERT_TYPE.WARN, - message: 'Your conversation is too long for this model. Try starting a new chat or switching to a model with a larger context window.' - }, - { - test: (content) => /rate limit/i.test(content) || /too many requests/i.test(content), - type: ALERT_TYPE.WARN, - message: 'You\'re sending requests too quickly. Please wait a moment before trying again.' - } -]; -function useChatAlert(messages: AIMessage[], scrollToBottom: () => void): { alert: ChatAlert | null; dismiss: () => void } { +function useChatAlert( + messages: AIMessage[], + scrollToBottom: () => void, + onNewChat?: () => void, + openModelSelector?: () => void +): { alert: ChatAlert | null; dismiss: () => void } { const [dismissedAlert, setDismissedAlert] = useState(null); const [prevAlert, setPrevAlert] = useState(null); + const alert = useMemo(() => { const lastMessage = messages[messages.length - 1]; if (!lastMessage || lastMessage.role !== 'assistant') return null; const content = typeof lastMessage.content === 'string' ? lastMessage.content : ''; - for (const pattern of ERROR_PATTERNS) { + // Define error patterns inside the useMemo + const errorPatterns: ErrorPattern[] = [ + { + test: (content) => /AI service error: 422/.test(content) || /Provider returned error/.test(content), + type: ALERT_TYPE.ERROR, + message: 'The AI model encountered an issue. Try switching to a different model or starting a new chat.', + action: openModelSelector ? { + label: 'Change model', + onClick: openModelSelector + } : undefined + }, + { + test: (content) => /maximum context length is \d+ tokens/.test(content) || /context length/.test(content), + type: ALERT_TYPE.WARN, + message: 'Your conversation is too long for this model. Try starting a new chat or switching to a model with a larger context window.', + action: onNewChat ? { + label: 'New chat', + onClick: onNewChat + } : undefined + }, + { + test: (content) => /rate limit/i.test(content) || /too many requests/i.test(content), + type: ALERT_TYPE.WARN, + message: 'You\'re sending requests too quickly. Please wait a moment before trying again.' + } + ]; + + for (const pattern of errorPatterns) { if (pattern.test(content)) { const alert = { type: pattern.type, - message: pattern.message + message: pattern.message, + action: pattern.action }; // Only show if not dismissed @@ -123,7 +166,7 @@ function useChatAlert(messages: AIMessage[], scrollToBottom: () => void): { aler } return null; - }, [messages, dismissedAlert]); + }, [messages, dismissedAlert, onNewChat, openModelSelector]); // Handle alert changes - scroll when alert appears/disappears useEffect(() => { @@ -190,6 +233,8 @@ export const ChatPane = forwardRef(({ // Initialize with first recently used model if available, otherwise empty return settings.recentlyUsedModels?.[0] || ''; }); + // State to control model selector dropdown + const [isModelSelectorOpen, setIsModelSelectorOpen] = useState(false); useEffect(() => { if (!providerModel && settings.recentlyUsedModels?.length) { @@ -323,8 +368,18 @@ export const ChatPane = forwardRef(({ } }; + // Function to open model selector dropdown + const openModelSelector = useCallback(() => { + setIsModelSelectorOpen(true); + }, []); + // Check for chat alerts based on messages - const { alert: chatAlert, dismiss: dismissAlert } = useChatAlert(messages, scrollToBottom); + const { alert: chatAlert, dismiss: dismissAlert } = useChatAlert( + messages, + scrollToBottom, + _onNewChat, + openModelSelector + ); // Check for autostart parameter and trigger AI generation useEffect(() => { @@ -787,6 +842,8 @@ export const ChatPane = forwardRef(({ className="w-full" disabled={isLoading} placeholder={t('chooseModel')} + open={isModelSelectorOpen} + onOpenChange={setIsModelSelectorOpen} />