import { useCallback, useState, type ErrorInfo, type ReactNode } from 'react'; import { ErrorBoundary as ReactErrorBoundary, type FallbackProps, } from 'react-error-boundary'; type ErrorFallbackProps = FallbackProps & { showDetails: boolean; componentStack: string | null; }; type ErrorBoundaryProps = { children: ReactNode; showDetails?: boolean; onRetry?: () => void; resetKeys?: unknown[]; }; function formatError(error: unknown): string { if (error instanceof Error) { return `${error.name}: ${error.message}`; } return String(error); } function ErrorFallback({ error, resetErrorBoundary, showDetails, componentStack, }: ErrorFallbackProps) { return (

Something went wrong

An error occurred while loading the chat interface.

{showDetails && (
Error Details
                {formatError(error)}
                {componentStack}
              
)}
); } function ErrorBoundary({ children, showDetails = false, onRetry = undefined, resetKeys = undefined, }: ErrorBoundaryProps) { const [componentStack, setComponentStack] = useState(null); const handleError = useCallback((error: Error, errorInfo: ErrorInfo) => { console.error('ErrorBoundary caught an error:', error, errorInfo); // Keep component stack for optional debug rendering in fallback UI. setComponentStack(errorInfo?.componentStack ?? null); }, []); const handleReset = useCallback(() => { setComponentStack(null); onRetry?.(); }, [onRetry]); const renderFallback = useCallback( ({ error, resetErrorBoundary }: FallbackProps) => ( ), [showDetails, componentStack] ); return ( {children} ); } export default ErrorBoundary;