diff --git a/AGENTS.md b/AGENTS.md index b5d2402..3389e27 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -863,7 +863,7 @@ function AIChat() { const handleSend = async () => { if (!input.trim()) return; - + const newMessages = [...messages, { role: 'user', content: input }]; setMessages(newMessages); setInput(''); @@ -880,7 +880,7 @@ function AIChat() { return (
{error &&
{error}
} - +
{messages.map((msg, i) => (
@@ -888,7 +888,7 @@ function AIChat() {
))}
- +
+ + + Dialog Title + + Optional description for screen readers + + + {/* Dialog content */} + + +``` + +**Important**: Even if you want to hide the title visually, use the `VisuallyHidden` component to maintain accessibility: + +```tsx +import { VisuallyHidden } from "@radix-ui/react-visually-hidden"; + + + + Hidden Title for Screen Readers + + +``` + +### Streaming Response Handling + +When implementing streaming chat interfaces, always accumulate streamed content in a local variable before clearing the streaming state to prevent content loss: + +```tsx +const handleStreamingResponse = async () => { + let streamedContent = ''; // ✅ Use local variable to accumulate content + + try { + await sendStreamingMessage(messages, model, (chunk) => { + streamedContent += chunk; // ✅ Accumulate in local variable + setCurrentStreamingMessage(streamedContent); // Update UI + }); + + // ✅ Save accumulated content to persistent state + if (streamedContent.trim()) { + const assistantMessage: MessageDisplay = { + id: Date.now().toString(), + role: 'assistant', + content: streamedContent, // ✅ Use accumulated content + timestamp: new Date() + }; + setMessages(prev => [...prev, assistantMessage]); + } + } finally { + setCurrentStreamingMessage(''); // ✅ Clear streaming state after saving + } +}; +``` + +### Error Boundary Patterns + +Always wrap AI components with error boundaries and provide user-friendly error messages for common failure scenarios: + +```tsx +import { ErrorBoundary } from '@/components/ErrorBoundary'; +import { Alert, AlertDescription } from '@/components/ui/alert'; + +function AIChatWithErrorBoundary() { + return ( + + + + + Something went wrong with the AI chat. Please refresh the page and try again. + + +
+ } + > + + + ); +} + +// In your AI component, handle specific error types gracefully: +function useAIWithErrorHandling() { + const { sendChatMessage, error, clearError } = useShakespeare(); + + const sendMessage = async (messages: ChatMessage[]) => { + try { + await sendChatMessage(messages, 'tybalt'); + } catch (err) { + // Handle specific error types with user-friendly messages + if (err.message.includes('401')) { + throw new Error('Authentication failed. Please log in again.'); + } else if (err.message.includes('402')) { + throw new Error('Insufficient credits. Please add credits to use premium features.'); + } else if (err.message.includes('network')) { + throw new Error('Network error. Please check your internet connection.'); + } + throw err; // Re-throw for error boundary + } + }; + + return { sendMessage, error, clearError }; +} +``` + ## Loading States **Use skeleton loading** for structured content (feeds, profiles, forms). **Use spinners** only for buttons or short operations.