From 185cdc51fb148a33566542ee93dabcb20b9efb55 Mon Sep 17 00:00:00 2001 From: sam Date: Sat, 27 Sep 2025 18:08:17 +0700 Subject: [PATCH 01/14] handle 422s and surface to quilly --- src/components/Quilly.tsx | 9 +++++++++ src/lib/SessionManager.ts | 11 +++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/components/Quilly.tsx b/src/components/Quilly.tsx index b0b97f05..c4185076 100644 --- a/src/components/Quilly.tsx +++ b/src/components/Quilly.tsx @@ -114,6 +114,15 @@ export function Quilly({ error, onDismiss, onNewChat, onOpenModelSelector, onReq }, }; + case 'unprocessable_entity': + return { + message: 'Request rejected by AI provider. Try a different model.', + action: { + label: 'Choose model', + onClick: onOpenModelSelector, + }, + }; + case 'server_error': case 'service_unavailable': return { diff --git a/src/lib/SessionManager.ts b/src/lib/SessionManager.ts index fde41dd3..923c69bb 100644 --- a/src/lib/SessionManager.ts +++ b/src/lib/SessionManager.ts @@ -408,6 +408,17 @@ export class SessionManager { return; // User cancelled } + // Convert UnprocessableEntityError to standard OpenAI APIError with code + if (error?.constructor?.name === 'UnprocessableEntityError') { + const apiError = new OpenAI.APIError({ + status: 422, + code: 'unprocessable_entity', + message: '422 Provider returned error' + }); + apiError.code = 'unprocessable_entity'; + throw apiError; + } + // Re-throw service errors to be handled at the UI level throw error; } finally { From 52198a0a3ca4eae14352faf7b907f5855d6444a7 Mon Sep 17 00:00:00 2001 From: sam Date: Sat, 27 Sep 2025 18:34:54 +0700 Subject: [PATCH 02/14] handle 400s too --- src/components/Quilly.tsx | 9 +++++++++ src/lib/SessionManager.ts | 23 +++++++++++++++++------ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/components/Quilly.tsx b/src/components/Quilly.tsx index c4185076..9ac094f0 100644 --- a/src/components/Quilly.tsx +++ b/src/components/Quilly.tsx @@ -114,6 +114,15 @@ export function Quilly({ error, onDismiss, onNewChat, onOpenModelSelector, onReq }, }; + case 'bad_request': + return { + message: 'Invalid request parameters. Try a different model.', + action: { + label: 'Choose model', + onClick: onOpenModelSelector, + }, + }; + case 'unprocessable_entity': return { message: 'Request rejected by AI provider. Try a different model.', diff --git a/src/lib/SessionManager.ts b/src/lib/SessionManager.ts index 923c69bb..2d30daef 100644 --- a/src/lib/SessionManager.ts +++ b/src/lib/SessionManager.ts @@ -408,14 +408,25 @@ export class SessionManager { return; // User cancelled } - // Convert UnprocessableEntityError to standard OpenAI APIError with code - if (error?.constructor?.name === 'UnprocessableEntityError') { + // Convert provider errors to standard OpenAI APIError with appropriate code + if (error?.status === 400) { + const code = 'bad_request'; const apiError = new OpenAI.APIError({ - status: 422, - code: 'unprocessable_entity', - message: '422 Provider returned error' + status: error.status, + code, + message: `${error.status} Provider returned error` }); - apiError.code = 'unprocessable_entity'; + apiError.code = code; + throw apiError; + } + if (error?.status === 422) { + const code = 'unprocessable_entity' + const apiError = new OpenAI.APIError({ + status: error.status, + code, + message: `${error.status} Provider returned error` + }); + apiError.code = code; throw apiError; } From ecdb33460d0031a3e7446a508ee94b78b7706c93 Mon Sep 17 00:00:00 2001 From: sam Date: Sat, 27 Sep 2025 18:39:48 +0700 Subject: [PATCH 03/14] broader error desc --- src/components/Quilly.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/Quilly.tsx b/src/components/Quilly.tsx index 9ac094f0..f88c9990 100644 --- a/src/components/Quilly.tsx +++ b/src/components/Quilly.tsx @@ -116,7 +116,7 @@ export function Quilly({ error, onDismiss, onNewChat, onOpenModelSelector, onReq case 'bad_request': return { - message: 'Invalid request parameters. Try a different model.', + message: 'The AI provider did not understand the message / data it got. If it persists, try stating a new conversation or using a different model.', action: { label: 'Choose model', onClick: onOpenModelSelector, From cf29632261edbb57a2db6f6fbfd7b3a7d7eb3f10 Mon Sep 17 00:00:00 2001 From: sam Date: Sat, 27 Sep 2025 18:48:30 +0700 Subject: [PATCH 04/14] support presenting two options in quilly and for 400s suggest either a new convo or a model change --- src/components/Quilly.tsx | 83 +++++++++++++++++++++++---------------- 1 file changed, 50 insertions(+), 33 deletions(-) diff --git a/src/components/Quilly.tsx b/src/components/Quilly.tsx index f88c9990..7a8b20e3 100644 --- a/src/components/Quilly.tsx +++ b/src/components/Quilly.tsx @@ -21,10 +21,10 @@ export interface QuillyProps { interface ErrorBody { message: string; - action?: { + actions?: Array<{ label: string; onClick: () => void; - }; + }>; } export function Quilly({ error, onDismiss, onNewChat, onOpenModelSelector, onRequestConsoleErrorHelp, providerModel }: QuillyProps) { @@ -46,16 +46,16 @@ export function Quilly({ error, onDismiss, onNewChat, onOpenModelSelector, onReq const renderBody = (error: Error): ErrorBody => { // Handle Project Preview Console Errors if (error instanceof ProjectPreviewConsoleError) { - 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); - onDismiss(); - }, - }, - }; + return { + message: 'I noticed some console errors in your project preview. Would you like me to take a look and help fix them?', + actions: [{ + label: 'Help fix errors', + onClick: () => { + onRequestConsoleErrorHelp?.(error); + onDismiss(); + }, + }], + }; } // Handle OpenAI API errors with specific error codes @@ -65,28 +65,28 @@ export function Quilly({ error, onDismiss, onNewChat, onOpenModelSelector, onReq case 'invalid_request_error': return { message: 'Authentication error: Please check your API key in AI settings.', - action: { + actions: [{ label: 'Check API key', 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: { + actions: [{ 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: { + actions: [{ label: 'Check AI settings', onClick: () => navigate('/settings/ai'), - } + }], }; } } @@ -94,53 +94,62 @@ export function Quilly({ error, onDismiss, onNewChat, onOpenModelSelector, onReq case 'rate_limit_exceeded': return { message: 'Rate limit exceeded. Please wait a moment before trying again.', + actions: [], }; case 'model_not_found': return { message: 'The selected AI model is not available. Please choose a different model.', - action: { + actions: [{ 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: { + actions: [{ label: 'New chat', onClick: onNewChat, - }, + }, { + label: 'Change model', + onClick: onOpenModelSelector, + }], }; case 'bad_request': return { message: 'The AI provider did not understand the message / data it got. If it persists, try stating a new conversation or using a different model.', - action: { - label: 'Choose model', + actions: [{ + label: 'New chat', + onClick: onNewChat, + }, { + label: 'Change model', onClick: onOpenModelSelector, - }, + }], }; case 'unprocessable_entity': return { message: 'Request rejected by AI provider. Try a different model.', - action: { + actions: [{ label: 'Choose model', onClick: onOpenModelSelector, - }, + }], }; case 'server_error': case 'service_unavailable': return { message: 'The AI service is temporarily unavailable. Please try again in a moment.', + actions: [], }; default: return { message: `AI service error: ${error.message}`, + actions: [], }; } } @@ -150,10 +159,11 @@ export function Quilly({ error, onDismiss, onNewChat, onOpenModelSelector, onReq message: error.message ? `AI service error: ${error.message}` : 'Sorry, I encountered an unexpected error. Please try again.', + actions: [], }; }; - const { message, action } = renderBody(error); + const { message, actions = [] } = renderBody(error); return (
@@ -166,11 +176,18 @@ export function Quilly({ error, onDismiss, onNewChat, onOpenModelSelector, onReq

{message} - {' '} - {action && ( - + {actions.length > 0 && ( + <> + {' '} + {actions.map((action, i) => ( + + + {i < actions.length - 1 && <> or } + + ))} + )}

From 4ce51b3659ac34fe3f8ad8fd88a692624ab955bf Mon Sep 17 00:00:00 2001 From: sam Date: Sat, 27 Sep 2025 19:13:10 +0700 Subject: [PATCH 05/14] handle 403s --- src/components/Quilly.tsx | 6 ++++++ src/lib/SessionManager.ts | 10 ++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/components/Quilly.tsx b/src/components/Quilly.tsx index 7a8b20e3..88db0970 100644 --- a/src/components/Quilly.tsx +++ b/src/components/Quilly.tsx @@ -118,6 +118,12 @@ export function Quilly({ error, onDismiss, onNewChat, onOpenModelSelector, onReq }], }; + case 'key_limit_exceeded': + return { + message: `It seems your API key has been used up. Check your API provider's settings, or try a different API key or different provider/model combo`, + actions: [], + }; + case 'bad_request': return { message: 'The AI provider did not understand the message / data it got. If it persists, try stating a new conversation or using a different model.', diff --git a/src/lib/SessionManager.ts b/src/lib/SessionManager.ts index 2d30daef..b375e1a4 100644 --- a/src/lib/SessionManager.ts +++ b/src/lib/SessionManager.ts @@ -419,6 +419,16 @@ export class SessionManager { apiError.code = code; throw apiError; } + if (error?.status === 403) { + const code = 'key_limit_exceeded'; + const apiError = new OpenAI.APIError({ + status: error.status, + code, + message: `${error.status} Provider returned error` + }); + apiError.code = code; + throw apiError; + } if (error?.status === 422) { const code = 'unprocessable_entity' const apiError = new OpenAI.APIError({ From 5a5bec28c43ba45ef4d9d329fed748a43614abc2 Mon Sep 17 00:00:00 2001 From: sam Date: Sat, 27 Sep 2025 19:17:17 +0700 Subject: [PATCH 06/14] offer the user two routes forward when their keys run dry --- src/components/Quilly.tsx | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/components/Quilly.tsx b/src/components/Quilly.tsx index 88db0970..d17f63d4 100644 --- a/src/components/Quilly.tsx +++ b/src/components/Quilly.tsx @@ -118,11 +118,17 @@ export function Quilly({ error, onDismiss, onNewChat, onOpenModelSelector, onReq }], }; - case 'key_limit_exceeded': - return { - message: `It seems your API key has been used up. Check your API provider's settings, or try a different API key or different provider/model combo`, - actions: [], - }; + case 'key_limit_exceeded': + return { + message: `It seems your API key has been used up. Check your API provider's settings, or try a different API key or different provider/model combo:`, + actions: [{ + label: 'Check API settings', + onClick: () => navigate('/settings/ai'), + }, { + label: 'Change model', + onClick: onOpenModelSelector, + }], + }; case 'bad_request': return { From d02d8959f6c06a7676858c46885d58a3e0401fd6 Mon Sep 17 00:00:00 2001 From: sam Date: Sat, 27 Sep 2025 21:08:25 +0700 Subject: [PATCH 07/14] lint --- src/lib/SessionManager.ts | 41 ++++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/src/lib/SessionManager.ts b/src/lib/SessionManager.ts index b375e1a4..3de7da47 100644 --- a/src/lib/SessionManager.ts +++ b/src/lib/SessionManager.ts @@ -411,33 +411,30 @@ export class SessionManager { // Convert provider errors to standard OpenAI APIError with appropriate code if (error?.status === 400) { const code = 'bad_request'; - const apiError = new OpenAI.APIError({ - status: error.status, - code, - message: `${error.status} Provider returned error` - }); - apiError.code = code; - throw apiError; + throw new OpenAI.APIError( + { status: error.status, code, message: `${error.status} Provider returned error` }, + { method: 'POST', url: '', body: {}, status: error.status }, + error.name, + error.cause + ); } if (error?.status === 403) { const code = 'key_limit_exceeded'; - const apiError = new OpenAI.APIError({ - status: error.status, - code, - message: `${error.status} Provider returned error` - }); - apiError.code = code; - throw apiError; + throw new OpenAI.APIError( + { status: error.status, code, message: `${error.status} Provider returned error` }, + { method: 'POST', url: '', body: {}, status: error.status }, + error.name, + error.cause + ); } if (error?.status === 422) { - const code = 'unprocessable_entity' - const apiError = new OpenAI.APIError({ - status: error.status, - code, - message: `${error.status} Provider returned error` - }); - apiError.code = code; - throw apiError; + const code = 'unprocessable_entity'; + throw new OpenAI.APIError( + { status: error.status, code, message: `${error.status} Provider returned error` }, + { method: 'POST', url: '', body: {}, status: error.status }, + error.name, + error.cause + ); } // Re-throw service errors to be handled at the UI level From 00d9d4d005489cc4361ce3598821c8fdda305729 Mon Sep 17 00:00:00 2001 From: sam Date: Sat, 27 Sep 2025 21:28:41 +0700 Subject: [PATCH 08/14] Revert "lint" This reverts commit d02d8959f6c06a7676858c46885d58a3e0401fd6. --- src/lib/SessionManager.ts | 41 +++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/src/lib/SessionManager.ts b/src/lib/SessionManager.ts index 3de7da47..b375e1a4 100644 --- a/src/lib/SessionManager.ts +++ b/src/lib/SessionManager.ts @@ -411,30 +411,33 @@ export class SessionManager { // Convert provider errors to standard OpenAI APIError with appropriate code if (error?.status === 400) { const code = 'bad_request'; - throw new OpenAI.APIError( - { status: error.status, code, message: `${error.status} Provider returned error` }, - { method: 'POST', url: '', body: {}, status: error.status }, - error.name, - error.cause - ); + const apiError = new OpenAI.APIError({ + status: error.status, + code, + message: `${error.status} Provider returned error` + }); + apiError.code = code; + throw apiError; } if (error?.status === 403) { const code = 'key_limit_exceeded'; - throw new OpenAI.APIError( - { status: error.status, code, message: `${error.status} Provider returned error` }, - { method: 'POST', url: '', body: {}, status: error.status }, - error.name, - error.cause - ); + const apiError = new OpenAI.APIError({ + status: error.status, + code, + message: `${error.status} Provider returned error` + }); + apiError.code = code; + throw apiError; } if (error?.status === 422) { - const code = 'unprocessable_entity'; - throw new OpenAI.APIError( - { status: error.status, code, message: `${error.status} Provider returned error` }, - { method: 'POST', url: '', body: {}, status: error.status }, - error.name, - error.cause - ); + const code = 'unprocessable_entity' + const apiError = new OpenAI.APIError({ + status: error.status, + code, + message: `${error.status} Provider returned error` + }); + apiError.code = code; + throw apiError; } // Re-throw service errors to be handled at the UI level From 10ea68547c9d152c22f741f5db46845decd654ce Mon Sep 17 00:00:00 2001 From: sam Date: Sat, 27 Sep 2025 21:38:48 +0700 Subject: [PATCH 09/14] use specific openai errors --- src/lib/SessionManager.ts | 27 +++------------------------ 1 file changed, 3 insertions(+), 24 deletions(-) diff --git a/src/lib/SessionManager.ts b/src/lib/SessionManager.ts index b375e1a4..110f9844 100644 --- a/src/lib/SessionManager.ts +++ b/src/lib/SessionManager.ts @@ -410,34 +410,13 @@ export class SessionManager { // Convert provider errors to standard OpenAI APIError with appropriate code if (error?.status === 400) { - const code = 'bad_request'; - const apiError = new OpenAI.APIError({ - status: error.status, - code, - message: `${error.status} Provider returned error` - }); - apiError.code = code; - throw apiError; + throw new OpenAI.BadRequestError(400, { code: 'bad_request' }, error.message, new Headers()); } if (error?.status === 403) { - const code = 'key_limit_exceeded'; - const apiError = new OpenAI.APIError({ - status: error.status, - code, - message: `${error.status} Provider returned error` - }); - apiError.code = code; - throw apiError; + throw new OpenAI.PermissionDeniedError(403, { code: 'key_limit_exceeded' }, error.message, new Headers()); } if (error?.status === 422) { - const code = 'unprocessable_entity' - const apiError = new OpenAI.APIError({ - status: error.status, - code, - message: `${error.status} Provider returned error` - }); - apiError.code = code; - throw apiError; + throw new OpenAI.UnprocessableEntityError(422, { code: 'unprocessable_entity' }, error.message, new Headers()); } // Re-throw service errors to be handled at the UI level From a474cd8cee137fb22df5d1476c7ec9da4555b965 Mon Sep 17 00:00:00 2001 From: sam Date: Sat, 27 Sep 2025 21:39:42 +0700 Subject: [PATCH 10/14] use generic openai error types but with specific codes --- src/lib/SessionManager.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib/SessionManager.ts b/src/lib/SessionManager.ts index 110f9844..31bdff21 100644 --- a/src/lib/SessionManager.ts +++ b/src/lib/SessionManager.ts @@ -410,13 +410,13 @@ export class SessionManager { // Convert provider errors to standard OpenAI APIError with appropriate code if (error?.status === 400) { - throw new OpenAI.BadRequestError(400, { code: 'bad_request' }, error.message, new Headers()); + throw new OpenAI.APIError(400, { code: 'bad_request' }, error.message, new Headers()); } if (error?.status === 403) { - throw new OpenAI.PermissionDeniedError(403, { code: 'key_limit_exceeded' }, error.message, new Headers()); + throw new OpenAI.APIError(403, { code: 'key_limit_exceeded' }, error.message, new Headers()); } if (error?.status === 422) { - throw new OpenAI.UnprocessableEntityError(422, { code: 'unprocessable_entity' }, error.message, new Headers()); + throw new OpenAI.APIError(422, { code: 'unprocessable_entity' }, error.message, new Headers()); } // Re-throw service errors to be handled at the UI level From f8350e1d629e4c88b159c9efe331e2a904b5759d Mon Sep 17 00:00:00 2001 From: sam Date: Sun, 28 Sep 2025 14:13:33 +0700 Subject: [PATCH 11/14] fix junk indentation --- src/components/Quilly.tsx | 42 +++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/src/components/Quilly.tsx b/src/components/Quilly.tsx index d17f63d4..445bc87a 100644 --- a/src/components/Quilly.tsx +++ b/src/components/Quilly.tsx @@ -46,16 +46,16 @@ export function Quilly({ error, onDismiss, onNewChat, onOpenModelSelector, onReq const renderBody = (error: Error): ErrorBody => { // Handle Project Preview Console Errors if (error instanceof ProjectPreviewConsoleError) { - return { - message: 'I noticed some console errors in your project preview. Would you like me to take a look and help fix them?', - actions: [{ - label: 'Help fix errors', - onClick: () => { - onRequestConsoleErrorHelp?.(error); - onDismiss(); - }, - }], - }; + return { + message: 'I noticed some console errors in your project preview. Would you like me to take a look and help fix them?', + actions: [{ + label: 'Help fix errors', + onClick: () => { + onRequestConsoleErrorHelp?.(error); + onDismiss(); + }, + }], + }; } // Handle OpenAI API errors with specific error codes @@ -118,17 +118,17 @@ export function Quilly({ error, onDismiss, onNewChat, onOpenModelSelector, onReq }], }; - case 'key_limit_exceeded': - return { - message: `It seems your API key has been used up. Check your API provider's settings, or try a different API key or different provider/model combo:`, - actions: [{ - label: 'Check API settings', - onClick: () => navigate('/settings/ai'), - }, { - label: 'Change model', - onClick: onOpenModelSelector, - }], - }; + case 'key_limit_exceeded': + return { + message: `It seems your API key has been used up. Check your API provider's settings, or try a different API key or different provider/model combo:`, + actions: [{ + label: 'Check API settings', + onClick: () => navigate('/settings/ai'), + }, { + label: 'Change model', + onClick: onOpenModelSelector, + }], + }; case 'bad_request': return { From 8ce69023af7014152a74299b7c33c85a8dfb7e8f Mon Sep 17 00:00:00 2001 From: sam Date: Sun, 28 Sep 2025 14:57:34 +0700 Subject: [PATCH 12/14] functional -> imperative :| --- src/lib/SessionManager.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/lib/SessionManager.ts b/src/lib/SessionManager.ts index 31bdff21..90d89634 100644 --- a/src/lib/SessionManager.ts +++ b/src/lib/SessionManager.ts @@ -287,7 +287,7 @@ export class SessionManager { // LiteLLM, Z.ai, etc. use 'reasoning_content' if (delta && 'reasoning_content' in delta && typeof delta.reasoning_content === 'string') { reasoningContent = delta.reasoning_content; - // ollama uses 'reasoning' + // ollama uses 'reasoning' } else if (delta && 'reasoning' in delta && typeof delta.reasoning === 'string') { reasoningContent = delta.reasoning; } @@ -410,13 +410,13 @@ export class SessionManager { // Convert provider errors to standard OpenAI APIError with appropriate code if (error?.status === 400) { - throw new OpenAI.APIError(400, { code: 'bad_request' }, error.message, new Headers()); + error.code = 'bad_request'; } if (error?.status === 403) { - throw new OpenAI.APIError(403, { code: 'key_limit_exceeded' }, error.message, new Headers()); + error.code = 'key_limit_exceeded'; } if (error?.status === 422) { - throw new OpenAI.APIError(422, { code: 'unprocessable_entity' }, error.message, new Headers()); + error.code = 'unprocessable_entity'; } // Re-throw service errors to be handled at the UI level From 77d37f071a031c86d1bd68a2bf29ba83ee54517f Mon Sep 17 00:00:00 2001 From: sam Date: Sun, 5 Oct 2025 07:26:54 +0700 Subject: [PATCH 13/14] no error normalisation in session manager, let quilly look more closely at the open ai error to deduce what to do --- src/components/Quilly.tsx | 32 ++++++++++++++++---------------- src/lib/SessionManager.ts | 11 ----------- 2 files changed, 16 insertions(+), 27 deletions(-) diff --git a/src/components/Quilly.tsx b/src/components/Quilly.tsx index d5598e23..3aa6fa67 100644 --- a/src/components/Quilly.tsx +++ b/src/components/Quilly.tsx @@ -44,7 +44,7 @@ export function Quilly({ error, onDismiss, onNewChat, onOpenModelSelector, onReq const credits = useAICredits(provider || { id: '', name: '', apiKey: '', nostr: false }); const [showCreditsDialog, setShowCreditsDialog] = useState(false); - const renderBody = (error: Error): ErrorBody => { + const renderBody = (error: Error | OpenAI.APIError | MalformedToolCallError | ProjectPreviewConsoleError): ErrorBody => { // Handle Project Preview Console Errors if (error instanceof ProjectPreviewConsoleError) { return { @@ -62,18 +62,18 @@ export function Quilly({ error, onDismiss, onNewChat, onOpenModelSelector, onReq if (error instanceof MalformedToolCallError) { 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: { + actions: [{ label: 'Change model', onClick: onOpenModelSelector, - }, + }], }; - } + } // Handle OpenAI API errors with specific error codes if (error instanceof OpenAI.APIError) { - switch (error.code) { - case 'invalid_api_key': - case 'invalid_request_error': + switch (true) { + case error.code ==='invalid_api_key': + case error.code === 'invalid_request_error': return { message: 'Authentication error: Please check your API key in AI settings.', actions: [{ @@ -82,7 +82,7 @@ export function Quilly({ error, onDismiss, onNewChat, onOpenModelSelector, onReq }], }; - case 'insufficient_quota': { + case error.code === 'insufficient_quota': { if (credits.data) { return { message: `Your account has $${credits.data.amount.toFixed(2)} credits. Please add credits to keep creating.`, @@ -102,13 +102,13 @@ export function Quilly({ error, onDismiss, onNewChat, onOpenModelSelector, onReq } } - case 'rate_limit_exceeded': + case error.code === 'rate_limit_exceeded': return { message: 'Rate limit exceeded. Please wait a moment before trying again.', actions: [], }; - case 'model_not_found': + case error.code === 'model_not_found': return { message: 'The selected AI model is not available. Please choose a different model.', actions: [{ @@ -117,7 +117,7 @@ export function Quilly({ error, onDismiss, onNewChat, onOpenModelSelector, onReq }], }; - case 'context_length_exceeded': + case error.code === '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.', actions: [{ @@ -129,7 +129,7 @@ export function Quilly({ error, onDismiss, onNewChat, onOpenModelSelector, onReq }], }; - case 'key_limit_exceeded': + case error.status === 403: return { message: `It seems your API key has been used up. Check your API provider's settings, or try a different API key or different provider/model combo:`, actions: [{ @@ -141,7 +141,7 @@ export function Quilly({ error, onDismiss, onNewChat, onOpenModelSelector, onReq }], }; - case 'bad_request': + case error.status === 400: return { message: 'The AI provider did not understand the message / data it got. If it persists, try stating a new conversation or using a different model.', actions: [{ @@ -153,7 +153,7 @@ export function Quilly({ error, onDismiss, onNewChat, onOpenModelSelector, onReq }], }; - case 'unprocessable_entity': + case error.status === 422: return { message: 'Request rejected by AI provider. Try a different model.', actions: [{ @@ -162,8 +162,8 @@ export function Quilly({ error, onDismiss, onNewChat, onOpenModelSelector, onReq }], }; - case 'server_error': - case 'service_unavailable': + case error.code === 'server_error': + case error.code === 'service_unavailable': return { message: 'The AI service is temporarily unavailable. Please try again in a moment.', actions: [], diff --git a/src/lib/SessionManager.ts b/src/lib/SessionManager.ts index 1f99c1c7..7f9431ee 100644 --- a/src/lib/SessionManager.ts +++ b/src/lib/SessionManager.ts @@ -433,17 +433,6 @@ export class SessionManager { return; // User cancelled } - // Convert provider errors to standard OpenAI APIError with appropriate code - if (error?.status === 400) { - error.code = 'bad_request'; - } - if (error?.status === 403) { - error.code = 'key_limit_exceeded'; - } - if (error?.status === 422) { - error.code = 'unprocessable_entity'; - } - // Re-throw service errors to be handled at the UI level throw error; } finally { From 462bbb429837cbdd82d3f650e64e0a6696c9a3f6 Mon Sep 17 00:00:00 2001 From: sam Date: Sun, 5 Oct 2025 11:15:53 +0700 Subject: [PATCH 14/14] lint fix --- src/components/Quilly.tsx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/components/Quilly.tsx b/src/components/Quilly.tsx index 3aa6fa67..04f20d15 100644 --- a/src/components/Quilly.tsx +++ b/src/components/Quilly.tsx @@ -1,4 +1,4 @@ -import OpenAI from 'openai'; +import { APIError } from 'openai'; import { useNavigate } from 'react-router-dom'; import { X } from 'lucide-react'; import { Button } from '@/components/ui/button'; @@ -44,7 +44,7 @@ export function Quilly({ error, onDismiss, onNewChat, onOpenModelSelector, onReq const credits = useAICredits(provider || { id: '', name: '', apiKey: '', nostr: false }); const [showCreditsDialog, setShowCreditsDialog] = useState(false); - const renderBody = (error: Error | OpenAI.APIError | MalformedToolCallError | ProjectPreviewConsoleError): ErrorBody => { + const renderBody = (error: Error | APIError | MalformedToolCallError | ProjectPreviewConsoleError): ErrorBody => { // Handle Project Preview Console Errors if (error instanceof ProjectPreviewConsoleError) { return { @@ -70,7 +70,7 @@ export function Quilly({ error, onDismiss, onNewChat, onOpenModelSelector, onReq } // Handle OpenAI API errors with specific error codes - if (error instanceof OpenAI.APIError) { + if (error instanceof APIError) { switch (true) { case error.code ==='invalid_api_key': case error.code === 'invalid_request_error':