From ecf124d43dd5db34dc4c927dfdbfd28cc11cd3ab Mon Sep 17 00:00:00 2001 From: Richard Osborne Date: Thu, 2 May 2024 17:13:27 +0200 Subject: [PATCH] Changed AI API call error conditions --- .../src/models/AiAssistant/context/ai-api.ts | 25 +++++++------------ 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/packages/noodl-editor/src/editor/src/models/AiAssistant/context/ai-api.ts b/packages/noodl-editor/src/editor/src/models/AiAssistant/context/ai-api.ts index 3d73db3..26651dd 100644 --- a/packages/noodl-editor/src/editor/src/models/AiAssistant/context/ai-api.ts +++ b/packages/noodl-editor/src/editor/src/models/AiAssistant/context/ai-api.ts @@ -38,13 +38,14 @@ async function directChatOpenAi({ messages, provider, abortController, onEnd, on stream: true }), async onopen(response) { - if (response.ok && response.headers.get('content-type') === EventStreamContentType) { + const contentType = response.headers.get('content-type') || ''; + if (response.ok && contentType.includes('text/event-stream')) { return; // everything's good - } else if (response.status >= 400 && response.status < 500 && response.status !== 429) { - // client-side errors are usually non-retriable: - throw 'FatalError'; } else { - throw 'RetriableError'; + // If status is not OK or content type is unexpected, throw the response body + const errorMessage = await response.text(); // Await the response body text + const errorDetails = `HTTP ${response.status} - ${response.statusText}: ${errorMessage}`; + throw errorDetails; // Throw the raw error string } }, onmessage(ev) { @@ -70,18 +71,10 @@ async function directChatOpenAi({ messages, provider, abortController, onEnd, on onEnd && onEnd(); }, onerror(err) { - const errText = err.toString(); - if (['FatalError'].includes(errText)) { - throw err; // rethrow to stop the operation - } else if (['RetriableError'].includes(errText)) { - if (tries <= 0) { - throw `Apologies, OpenAI is currently facing heavy traffic, causing delays in processing requests. Please be patient and try again later.`; - } - tries--; - } else { - // do nothing to automatically retry. You can also - // return a specific retry interval here. + if (tries <= 0) { + throw err; // Just rethrow the error directly } + tries--; } });