Files
copilotkit__copilotkit/CopilotKit/demo-suggestions-improvement.md

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: suggestionsFailed prevents 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:

  1. First attempt: Suggestions load immediately (no cooldown)
  2. On failure: Retry up to 3 times with 5-second cooldowns
  3. After max retries: Suggestions disabled until context changes
  4. 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

  1. No more infinite loops: Prevents server hammering
  2. Better UX: Users see suggestions when possible
  3. Graceful degradation: App continues working even if suggestions fail
  4. Configurable retry: Easy to adjust limits and cooldowns
  5. Manual recovery: Users can reset suggestions if needed