From 561e5614c5e06b63e44959863a99784b09d75b9c Mon Sep 17 00:00:00 2001 From: sam Date: Mon, 22 Sep 2025 17:21:59 +0700 Subject: [PATCH] chat intervention component and flow --- src/components/Shakespeare/ChatPane.tsx | 142 +++++++++++++++++++++--- 1 file changed, 129 insertions(+), 13 deletions(-) diff --git a/src/components/Shakespeare/ChatPane.tsx b/src/components/Shakespeare/ChatPane.tsx index e6dc2218..40e32994 100644 --- a/src/components/Shakespeare/ChatPane.tsx +++ b/src/components/Shakespeare/ChatPane.tsx @@ -40,6 +40,111 @@ import { makeSystemPrompt } from '@/lib/system'; import { assistantContentEmpty } from '@/lib/ai-messages'; import { saveFileToTmp } from '@/lib/fileUtils'; import { useGit } from '@/hooks/useGit'; +import { Alert, AlertDescription } from '@/components/ui/alert'; +import { AlertTriangle, AlertCircle } from 'lucide-react'; + +const ALERT_TYPE = { + WARN: 'warn', + ERROR: 'error' +} as const; + +type AlertType = typeof ALERT_TYPE[keyof typeof ALERT_TYPE]; + +interface ChatAlert { + type: AlertType; + message: string; +} + +function ChatIntervention({ alert, onDismiss }: { alert: ChatAlert; onDismiss: () => void }) { + const isError = alert.type === ALERT_TYPE.ERROR; + + return ( + + {isError ? : } + + {alert.message} + + + + ); +} + +interface ErrorPattern { + test: (content: string) => boolean; + type: AlertType; + message: string; +} + +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 } { + 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) { + if (pattern.test(content)) { + const alert = { + type: pattern.type, + message: pattern.message + }; + + // Only show if not dismissed + return dismissedAlert !== alert.message ? alert : null; + } + } + + return null; + }, [messages, dismissedAlert]); + + // Handle alert changes - scroll when alert appears/disappears + useEffect(() => { + const alertChanged = prevAlert !== alert; + if (alertChanged && alert) { + // Small delay to ensure alert is rendered + setTimeout(() => { + scrollToBottom(); + }, 50); + } + setPrevAlert(alert); + }, [alert, prevAlert, scrollToBottom]); + + const dismiss = useCallback(() => { + if (alert) { + setDismissedAlert(alert.message); + } + }, [alert]); + + return { alert, dismiss }; +} interface ChatPaneProps { projectId: string; @@ -211,6 +316,16 @@ export const ChatPane = forwardRef(({ } }, [internalIsLoading, onLoadingChange]); + // Function to scroll to bottom + const scrollToBottom = () => { + if (scrollAreaRef.current) { + scrollAreaRef.current.scrollTop = scrollAreaRef.current.scrollHeight; + } + }; + + // Check for chat alerts based on messages + const { alert: chatAlert, dismiss: dismissAlert } = useChatAlert(messages, scrollToBottom); + // Check for autostart parameter and trigger AI generation useEffect(() => { const autostart = searchParams.get('autostart'); @@ -239,14 +354,6 @@ export const ChatPane = forwardRef(({ } }, [addRecentlyUsedModel, isConfigured, providerModel, searchParams, setSearchParams, startGeneration]); - - // Function to scroll to bottom - const scrollToBottom = () => { - if (scrollAreaRef.current) { - scrollAreaRef.current.scrollTop = scrollAreaRef.current.scrollHeight; - } - }; - // Simple scroll event listener useEffect(() => { const container = scrollAreaRef.current; @@ -281,7 +388,6 @@ export const ChatPane = forwardRef(({ if (isNearBottom) { container.scrollTop = container.scrollHeight; } - } }, [messages, streamingMessage, isLoading]); @@ -400,7 +506,7 @@ export const ChatPane = forwardRef(({ await sendMessage(messageContent, modelToUse); } catch (error) { console.error('AI chat error:', error); - // Error handling is done in the useAIChat hook + // Error handling is done in the useAIChat hook and SessionManager } }; @@ -564,6 +670,17 @@ export const ChatPane = forwardRef(({ )} + + {/* Chat Alert */} + {chatAlert && ( +
+ +
+ )} + @@ -584,9 +701,8 @@ export const ChatPane = forwardRef(({
{/* Chat Input Container */}