From e88a11ce210b74287d36aa15bad2982df0d1c94b Mon Sep 17 00:00:00 2001 From: sam Date: Mon, 22 Sep 2025 22:55:46 +0700 Subject: [PATCH] most recent error only --- src/components/Shakespeare/ChatPane.tsx | 2 +- src/hooks/useErrorDetection.ts | 40 +++++++++++++++++++++---- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/components/Shakespeare/ChatPane.tsx b/src/components/Shakespeare/ChatPane.tsx index 9a1b372b..604679a2 100644 --- a/src/components/Shakespeare/ChatPane.tsx +++ b/src/components/Shakespeare/ChatPane.tsx @@ -276,7 +276,7 @@ export const ChatPane = forwardRef(({ // Function to suggest error fix // Clean alert hooks with proper interfaces const aiModelAlert = useAIModelErrorAlert(messages, _onNewChat, openModelSelector); - const consoleAlert = useConsoleErrorAlert(); + const consoleAlert = useConsoleErrorAlert(messages, isBuildLoading, internalIsLoading); // Scroll to bottom when alerts appear useEffect(() => { diff --git a/src/hooks/useErrorDetection.ts b/src/hooks/useErrorDetection.ts index 832472e8..9a100f85 100644 --- a/src/hooks/useErrorDetection.ts +++ b/src/hooks/useErrorDetection.ts @@ -9,26 +9,51 @@ export interface ConsoleErrorAlert { dismiss: () => void; } -export const useConsoleErrorAlert = (): ConsoleErrorAlert => { +export const useConsoleErrorAlert = ( + aiMessages: AIMessage[], + isBuilding?: boolean, + isLoading?: boolean +): ConsoleErrorAlert => { const { messages } = useConsoleMessages(); const [dismissedErrorSummary, setDismissedErrorSummary] = useState(''); const { hasError, errorSummary, errorCount } = useMemo(() => { - const errors = messages.filter(msg => msg.level === 'error'); + // Don't show alerts while building or AI is working + if (isBuilding || isLoading) { + return { hasError: false, errorSummary: null, errorCount: 0 }; + } - if (errors.length === 0) { + // Find the timestamp of the last AI assistant message + // Since AIMessage doesn't have timestamps, we'll use a different approach: + // Only show errors if there are any errors at all and no recent user activity + const lastMessage = aiMessages[aiMessages.length - 1]; + const isLastMessageFromAssistant = lastMessage?.role === 'assistant'; + + // Only show console errors if the last message was from assistant (AI just responded) + if (!isLastMessageFromAssistant) { + return { hasError: false, errorSummary: null, errorCount: 0 }; + } + + // Get recent errors (last 10 seconds worth to catch errors from recent AI actions) + const recentTimeThreshold = Date.now() - (10 * 1000); // 10 seconds ago + const recentErrors = messages.filter(msg => + msg.level === 'error' && + msg.timestamp > recentTimeThreshold + ); + + if (recentErrors.length === 0) { return { hasError: false, errorSummary: null, errorCount: 0 }; } // Just return the raw error messages, no formatting - const summary = errors.slice(-3).map(e => e.message).join('\n'); + const summary = recentErrors.slice(-3).map(e => e.message).join('\n'); return { hasError: dismissedErrorSummary !== summary, errorSummary: summary, - errorCount: errors.length + errorCount: recentErrors.length }; - }, [messages, dismissedErrorSummary]); + }, [messages, dismissedErrorSummary, aiMessages, isBuilding, isLoading]); const dismiss = useCallback(() => { if (errorSummary) { @@ -61,6 +86,9 @@ export const useAIModelErrorAlert = ( const { hasError, errorMessage, errorType, action } = useMemo(() => { const lastMessage = messages[messages.length - 1]; + + // Only show AI model alerts if the last message is from the assistant + // This ensures we don't show stale alerts when user continues the conversation if (!lastMessage || lastMessage.role !== 'assistant') { return { hasError: false, errorMessage: null, errorType: null, action: undefined }; }