mirror of
https://github.com/CopilotKit/CopilotKit.git
synced 2026-09-14 16:26:20 +08:00
8674da1938
Signed-off-by: Tyler Slaton <tyler@copilotkit.ai>
2.9 KiB
2.9 KiB
Suggestions Error Handling Improvements
Problem Fixed
Previously, when suggestions failed to load (due to network errors, server issues, etc.), CopilotKit would continuously retry the request, creating an infinite loop that would hammer the server.
Solution Implemented
1. Retry Logic with Limits
- Maximum retries: 3 attempts
- Cooldown period: 5 seconds between retries
- First attempt: No cooldown, immediate execution
- Failure tracking: Prevents infinite loops
2. Error Categorization
- Network errors: Caught and logged without throwing
- Abort errors: Handled gracefully when user cancels
- Other errors: Logged but don't prevent other suggestion configs from trying
3. State Management
- Failed state tracking:
suggestionsFailedprevents retries - Reset on context change: When messages change, suggestions can be retried
- Manual reset:
resetSuggestions()function for manual retry
Key Files Modified
packages/react-ui/src/components/chat/Chat.tsx
- Added retry state management
- Implemented cooldown logic
- Fixed first-attempt immediate execution
packages/react-core/src/hooks/use-copilot-chat.ts
- Added failure tracking with
suggestionsFailedRef - Improved error handling in
generateSuggestionsFunc - Added
resetSuggestions()function
packages/react-core/src/utils/suggestions.ts
- Enhanced error handling in
reloadSuggestions - Better error categorization
- Partial success support
Demonstration
The code now includes a simulated network failure (30% chance) to demonstrate the improved behavior:
- First attempt: Suggestions load immediately (no cooldown)
- On failure: Retry up to 3 times with 5-second cooldowns
- After max retries: Suggestions disabled until context changes
- Manual reset: Available via
resetSuggestions()function
Console Output Examples
Successful first attempt:
✅ Suggestions loaded successfully
Failed attempts with retries:
🔴 Simulating network failure for suggestions...
Error in generateSuggestions: Error: Network error: Failed to fetch suggestions
🔴 Simulating network failure for suggestions...
Error in generateSuggestions: Error: Network error: Failed to fetch suggestions
🔴 Simulating network failure for suggestions...
Error in generateSuggestions: Error: Network error: Failed to fetch suggestions
Skipping suggestions generation - previous attempts failed
Reset behavior:
// After messages change or manual reset
✅ Suggestions can be retried again
Benefits
- No more infinite loops: Prevents server hammering
- Better UX: Users see suggestions when possible
- Graceful degradation: App continues working even if suggestions fail
- Configurable retry: Easy to adjust limits and cooldowns
- Manual recovery: Users can reset suggestions if needed