diff --git a/src/components/Quilly.test.tsx b/src/components/Quilly.test.tsx index 2df8be2a..dfad2479 100644 --- a/src/components/Quilly.test.tsx +++ b/src/components/Quilly.test.tsx @@ -6,75 +6,75 @@ import { ProjectPreviewConsoleError } from '@/lib/tools/ReadConsoleMessagesTool' // Mock all required hooks and components vi.mock('@/hooks/useAISettings', () => ({ - useAISettings: () => ({ - settings: { - providers: [] - } - }) + useAISettings: () => ({ + settings: { + providers: [] + } + }) })); vi.mock('@/hooks/useAICredits', () => ({ - useAICredits: () => ({ - data: null - }) + useAICredits: () => ({ + data: null + }) })); vi.mock('react-router-dom', () => ({ - useNavigate: () => vi.fn() + useNavigate: () => vi.fn() })); // Mock CreditsDialog to avoid NostrProvider dependency vi.mock('./CreditsDialog', () => ({ - CreditsDialog: () => null + CreditsDialog: () => null })); describe('Quilly', () => { - const defaultProps = { - error: new Error('Generic error'), - onDismiss: vi.fn(), - onNewChat: vi.fn(), - onOpenModelSelector: vi.fn(), - providerModel: 'test/model' - }; + const defaultProps = { + error: new Error('Generic error'), + onDismiss: vi.fn(), + onNewChat: vi.fn(), + onOpenModelSelector: vi.fn(), + providerModel: 'test/model' + }; - it('renders generic error message', () => { - render(); + it('renders generic error message', () => { + render(); - expect(screen.getByText(/AI service error: Generic error/)).toBeInTheDocument(); - }); + expect(screen.getByText(/AI service error: Generic error/)).toBeInTheDocument(); + }); - it('renders correctly for malformed tool call errors', () => { - const error = new MalformedToolCallError( - 'The AI provider sent an incomplete response.', - 'call_abc123', - 'test/model' - ); + it('renders correctly for malformed tool call errors', () => { + const error = new MalformedToolCallError( + 'The AI provider sent an incomplete response.', + 'call_abc123', + 'test/model' + ); - render( - - ); + render( + + ); - expect(screen.getByText(/The AI sent an incomplete response/)).toBeInTheDocument(); - expect(screen.getByText(/Change model/)).toBeInTheDocument(); - }); + expect(screen.getByText(/The AI sent an incomplete response/)).toBeInTheDocument(); + expect(screen.getByText(/Change model/)).toBeInTheDocument(); + }); - it('renders correctly for console errors', () => { - const error = new ProjectPreviewConsoleError( - 'Console error', - [] - ); + it('renders correctly for console errors', () => { + const error = new ProjectPreviewConsoleError( + 'Console error', + [] + ); - render( - - ); + render( + + ); - expect(screen.getByText(/I noticed some console errors/)).toBeInTheDocument(); - expect(screen.getByText(/Help fix errors/)).toBeInTheDocument(); - }); + expect(screen.getByText(/I noticed some console errors/)).toBeInTheDocument(); + expect(screen.getByText(/Help fix errors/)).toBeInTheDocument(); + }); }); diff --git a/src/components/Quilly.tsx b/src/components/Quilly.tsx index c3342b28..248274b0 100644 --- a/src/components/Quilly.tsx +++ b/src/components/Quilly.tsx @@ -56,92 +56,92 @@ export function Quilly({ error, onDismiss, onNewChat, onOpenModelSelector, onReq } switch (errorCode) { - // Application-specific error codes - case 'console_error': - return { - message: 'I noticed some console errors in your project preview. Would you like me to take a look and help fix them?', - action: { - label: 'Help fix errors', - onClick: () => { - onRequestConsoleErrorHelp?.(error as ProjectPreviewConsoleError); - onDismiss(); - }, + // Application-specific error codes + case 'console_error': + return { + message: 'I noticed some console errors in your project preview. Would you like me to take a look and help fix them?', + action: { + label: 'Help fix errors', + onClick: () => { + onRequestConsoleErrorHelp?.(error as ProjectPreviewConsoleError); + onDismiss(); }, - }; + }, + }; - case 'malformed_tool_call': - return { - message: 'The AI sent an incomplete response, possibly due to a network issue or provider problem. Try sending your message again, or switch to a different model if this persists.', - action: { - label: 'Change model', - onClick: onOpenModelSelector, - }, - }; + case 'malformed_tool_call': + return { + message: 'The AI sent an incomplete response, possibly due to a network issue or provider problem. Try sending your message again, or switch to a different model if this persists.', + action: { + label: 'Change model', + onClick: onOpenModelSelector, + }, + }; // OpenAI API error codes - case 'invalid_api_key': - case 'invalid_request_error': + case 'invalid_api_key': + case 'invalid_request_error': + return { + message: 'Authentication error: Please check your API key in AI settings.', + action: { + label: 'Check API key', + onClick: () => navigate('/settings/ai'), + } + }; + + case 'insufficient_quota': { + if (credits.data) { return { - message: 'Authentication error: Please check your API key in AI settings.', + message: `Your account has $${credits.data.amount.toFixed(2)} credits. Please add credits to keep creating.`, action: { - label: 'Check API key', + label: 'Add credits', + onClick: () => setShowCreditsDialog(true), + } + }; + } else { + return { + message: 'Your API key has reached its usage limit. Please check your billing or try a different provider.', + action: { + label: 'Check AI settings', onClick: () => navigate('/settings/ai'), } }; - - case 'insufficient_quota': { - if (credits.data) { - return { - message: `Your account has $${credits.data.amount.toFixed(2)} credits. Please add credits to keep creating.`, - action: { - label: 'Add credits', - onClick: () => setShowCreditsDialog(true), - } - }; - } else { - return { - message: 'Your API key has reached its usage limit. Please check your billing or try a different provider.', - action: { - label: 'Check AI settings', - onClick: () => navigate('/settings/ai'), - } - }; - } } + } - case 'rate_limit_exceeded': - return { - message: 'Rate limit exceeded. Please wait a moment before trying again.', - }; + case 'rate_limit_exceeded': + return { + message: 'Rate limit exceeded. Please wait a moment before trying again.', + }; - case 'model_not_found': - return { - message: 'The selected AI model is not available. Please choose a different model.', - action: { - label: 'Choose model', - onClick: onOpenModelSelector, - }, - }; + case 'model_not_found': + return { + message: 'The selected AI model is not available. Please choose a different model.', + action: { + label: 'Choose model', + onClick: onOpenModelSelector, + }, + }; - case 'context_length_exceeded': - return { - message: 'Your conversation is too long for this model. Try starting a new chat or switching to a model with a larger context window.', - action: { - label: 'New chat', - onClick: onNewChat, - }, - }; + case 'context_length_exceeded': + return { + message: 'Your conversation is too long for this model. Try starting a new chat or switching to a model with a larger context window.', + action: { + label: 'New chat', + onClick: onNewChat, + }, + }; - case 'server_error': - case 'service_unavailable': - return { - message: 'The AI service is temporarily unavailable. Please try again in a moment.', - }; + case 'server_error': + case 'service_unavailable': + return { + message: 'The AI service is temporarily unavailable. Please try again in a moment.', + }; - default: - return { - message: `AI service error: ${error.message}`, - }; + default: + return { + message: `AI service error: ${error.message}`, + }; } }; diff --git a/src/lib/errors/MalformedToolCallError.ts b/src/lib/errors/MalformedToolCallError.ts index d2ef93b7..1bd79d89 100644 --- a/src/lib/errors/MalformedToolCallError.ts +++ b/src/lib/errors/MalformedToolCallError.ts @@ -3,20 +3,20 @@ * (e.g., missing function name in the streaming response) */ export class MalformedToolCallError extends Error { - public readonly code = 'malformed_tool_call' as const; - public readonly toolCallId: string; - public readonly providerModel: string; + public readonly code = 'malformed_tool_call' as const; + public readonly toolCallId: string; + public readonly providerModel: string; - constructor(message: string, toolCallId: string, providerModel: string) { - super(message); - this.name = 'MalformedToolCallError'; - this.toolCallId = toolCallId; - this.providerModel = providerModel; + constructor(message: string, toolCallId: string, providerModel: string) { + super(message); + this.name = 'MalformedToolCallError'; + this.toolCallId = toolCallId; + this.providerModel = providerModel; - // Maintains proper stack trace for where our error was thrown (only available on V8) - if (Error.captureStackTrace) { - Error.captureStackTrace(this, MalformedToolCallError); - } - } + // Maintains proper stack trace for where our error was thrown (only available on V8) + if (Error.captureStackTrace) { + Error.captureStackTrace(this, MalformedToolCallError); + } + } }