diff --git a/apps/demos/Demos/Chat/AIAndChatbotIntegration/Angular/app/ai/ai.service.ts b/apps/demos/Demos/Chat/AIAndChatbotIntegration/Angular/app/ai/ai.service.ts index c90a0efe9aba..fe312c947559 100644 --- a/apps/demos/Demos/Chat/AIAndChatbotIntegration/Angular/app/ai/ai.service.ts +++ b/apps/demos/Demos/Chat/AIAndChatbotIntegration/Angular/app/ai/ai.service.ts @@ -33,7 +33,7 @@ export class AiService { temperature: 0.7, }; - const response = await this.chatService.chat.completions.create(params); + const response = await this.chatService.chat.completions.create(params, { maxRetries: 0 }); const data = { choices: response.choices }; return data.choices[0].message?.content; diff --git a/apps/demos/Demos/Chat/AIAndChatbotIntegration/Angular/app/app.service.ts b/apps/demos/Demos/Chat/AIAndChatbotIntegration/Angular/app/app.service.ts index 20c638097558..87d2c84a13d7 100644 --- a/apps/demos/Demos/Chat/AIAndChatbotIntegration/Angular/app/app.service.ts +++ b/apps/demos/Demos/Chat/AIAndChatbotIntegration/Angular/app/app.service.ts @@ -106,13 +106,23 @@ export class AppService { this.messages.push({ role: 'assistant', content: aiResponse }); this.renderAssistantMessage(aiResponse); }, 200); - } catch { + } catch (err: unknown) { this.typingUsersSubject.next([]); this.messages.pop(); - this.alertLimitReached(); + this.alertError(this.getErrorMessage(err)); } } + private getErrorMessage(err: unknown): string { + if (typeof err === 'object' && err !== null) { + const e = err as { error?: { message?: unknown }; message?: unknown }; + if (typeof e.error?.message === 'string') return e.error.message; + if (typeof e.message === 'string') return e.message; + } + if (typeof err === 'string') return err; + return 'Unknown error'; + } + updateLastMessage(text = this.REGENERATION_TEXT) { const items = this.dataSource.items(); const lastMessage = items.at(-1); @@ -135,9 +145,9 @@ export class AppService { this.dataSource.store().push([{ type: 'insert', data: message }]); } - alertLimitReached() { + alertError(message: string) { this.setAlerts([{ - message: 'Request limit reached, try again in a minute.', + message, }]); setTimeout(() => { @@ -156,9 +166,9 @@ export class AppService { this.updateLastMessage(aiResponse); this.messages.at(-1).content = aiResponse; - } catch { + } catch (err: unknown) { this.updateLastMessage(this.messages.at(-1).content); - this.alertLimitReached(); + this.alertError(this.getErrorMessage(err)); } } diff --git a/apps/demos/Demos/Chat/AIAndChatbotIntegration/React/service.ts b/apps/demos/Demos/Chat/AIAndChatbotIntegration/React/service.ts index 37aee0842731..7d1698e6736d 100644 --- a/apps/demos/Demos/Chat/AIAndChatbotIntegration/React/service.ts +++ b/apps/demos/Demos/Chat/AIAndChatbotIntegration/React/service.ts @@ -31,7 +31,7 @@ export async function getAIResponse(messages: AIMessage[], delay?: number): Prom temperature: 0.7, }; - const response = await chatService.chat.completions.create(params); + const response = await chatService.chat.completions.create(params, { maxRetries: 0 }); const data = { choices: response.choices }; if (delay) { diff --git a/apps/demos/Demos/Chat/AIAndChatbotIntegration/React/useApi.ts b/apps/demos/Demos/Chat/AIAndChatbotIntegration/React/useApi.ts index 03a20011b0db..23b764ce3a1d 100644 --- a/apps/demos/Demos/Chat/AIAndChatbotIntegration/React/useApi.ts +++ b/apps/demos/Demos/Chat/AIAndChatbotIntegration/React/useApi.ts @@ -38,6 +38,16 @@ const dataItemToMessage = (item: ChatTypes.Message): AIMessage => ({ const getMessageHistory = (): AIMessage[] => [...dataSource.items()].map(dataItemToMessage); +const getErrorMessage = (err: unknown): string => { + if (typeof err === 'object' && err !== null) { + const e = err as { error?: { message?: unknown }; message?: unknown }; + if (typeof e.error?.message === 'string') return e.error.message; + if (typeof e.message === 'string') return e.message; + } + if (typeof err === 'string') return err; + return 'Unknown error'; +}; + export const useApi = () => { const [alerts, setAlerts] = useState([]); @@ -55,9 +65,9 @@ export const useApi = () => { }]); }, []); - const alertLimitReached = useCallback((): void => { + const alertError = useCallback((message: string): void => { setAlerts([{ - message: 'Request limit reached, try again in a minute.', + message, }]); setTimeout(() => { @@ -77,10 +87,10 @@ export const useApi = () => { author: assistant, text: aiResponse, }); - } catch { - alertLimitReached(); + } catch (err: unknown) { + alertError(getErrorMessage(err)); } - }, [alertLimitReached, insertMessage]); + }, [alertError, insertMessage]); const regenerateLastAIResponse = useCallback(async (): Promise => { const messageHistory = getMessageHistory(); @@ -92,11 +102,11 @@ export const useApi = () => { if (typeof aiResponse === 'string') { updateLastMessageContent(aiResponse); } - } catch { + } catch (err: unknown) { updateLastMessageContent(messageHistory.at(-1)?.content as string); - alertLimitReached(); + alertError(getErrorMessage(err)); } - }, [alertLimitReached, updateLastMessageContent]); + }, [alertError, updateLastMessageContent]); return { alerts, diff --git a/apps/demos/Demos/Chat/AIAndChatbotIntegration/ReactJs/service.js b/apps/demos/Demos/Chat/AIAndChatbotIntegration/ReactJs/service.js index e1eeaa9fc9d1..89d12a3bcde0 100644 --- a/apps/demos/Demos/Chat/AIAndChatbotIntegration/ReactJs/service.js +++ b/apps/demos/Demos/Chat/AIAndChatbotIntegration/ReactJs/service.js @@ -19,7 +19,7 @@ export async function getAIResponse(messages, delay) { max_completion_tokens: 1000, temperature: 0.7, }; - const response = await chatService.chat.completions.create(params); + const response = await chatService.chat.completions.create(params, { maxRetries: 0 }); const data = { choices: response.choices }; if (delay) { await wait(delay); diff --git a/apps/demos/Demos/Chat/AIAndChatbotIntegration/ReactJs/useApi.js b/apps/demos/Demos/Chat/AIAndChatbotIntegration/ReactJs/useApi.js index 7f88abd8f651..0dc8fbd82f11 100644 --- a/apps/demos/Demos/Chat/AIAndChatbotIntegration/ReactJs/useApi.js +++ b/apps/demos/Demos/Chat/AIAndChatbotIntegration/ReactJs/useApi.js @@ -29,6 +29,15 @@ const dataItemToMessage = (item) => ({ content: item.text, }); const getMessageHistory = () => [...dataSource.items()].map(dataItemToMessage); +const getErrorMessage = (err) => { + if (typeof err === 'object' && err !== null) { + const e = err; + if (typeof e.error?.message === 'string') return e.error.message; + if (typeof e.message === 'string') return e.message; + } + if (typeof err === 'string') return err; + return 'Unknown error'; +}; export const useApi = () => { const [alerts, setAlerts] = useState([]); const insertMessage = useCallback((data) => { @@ -44,10 +53,10 @@ export const useApi = () => { }, ]); }, []); - const alertLimitReached = useCallback(() => { + const alertError = useCallback((message) => { setAlerts([ { - message: 'Request limit reached, try again in a minute.', + message, }, ]); setTimeout(() => { @@ -65,11 +74,11 @@ export const useApi = () => { author: assistant, text: aiResponse, }); - } catch { - alertLimitReached(); + } catch (err) { + alertError(getErrorMessage(err)); } }, - [alertLimitReached, insertMessage], + [alertError, insertMessage], ); const regenerateLastAIResponse = useCallback(async () => { const messageHistory = getMessageHistory(); @@ -79,11 +88,11 @@ export const useApi = () => { if (typeof aiResponse === 'string') { updateLastMessageContent(aiResponse); } - } catch { + } catch (err) { updateLastMessageContent(messageHistory.at(-1)?.content); - alertLimitReached(); + alertError(getErrorMessage(err)); } - }, [alertLimitReached, updateLastMessageContent]); + }, [alertError, updateLastMessageContent]); return { alerts, insertMessage, diff --git a/apps/demos/Demos/Chat/AIAndChatbotIntegration/Vue/App.vue b/apps/demos/Demos/Chat/AIAndChatbotIntegration/Vue/App.vue index ae2196fc72eb..6dfc70fe096d 100644 --- a/apps/demos/Demos/Chat/AIAndChatbotIntegration/Vue/App.vue +++ b/apps/demos/Demos/Chat/AIAndChatbotIntegration/Vue/App.vue @@ -108,6 +108,16 @@ function renderAssistantMessage(text: string): void { dataSource.store().push([{ type: 'insert', data: message }]); } +function getErrorMessage(err: unknown): string { + if (typeof err === 'object' && err !== null) { + const e = err as { error?: { message?: unknown }; message?: unknown }; + if (typeof e.error?.message === 'string') return e.error.message; + if (typeof e.message === 'string') return e.message; + } + if (typeof err === 'string') return err; + return 'Unknown error'; +} + async function processMessageSending( message: DxChatTypes.TextMessage, event: Events.EventObject | undefined, @@ -125,18 +135,18 @@ async function processMessageSending( messages.push({ role: 'assistant', content: aiResponse }); renderAssistantMessage(aiResponse); }, 200); - } catch { + } catch (err: unknown) { typingUsers.value = []; messages.pop(); - alertLimitReached(); + alertError(getErrorMessage(err)); } finally { toggleDisabledState(false, event); } } -function alertLimitReached(): void { +function alertError(message: string): void { alerts.value = [{ - message: 'Request limit reached, try again in a minute.', + message, }]; setTimeout(() => { @@ -156,12 +166,11 @@ async function regenerate(): Promise { if (lastMessage?.content) { lastMessage.content = aiResponse; } - } catch { + } catch (err: unknown) { if (lastMessage?.content) { updateLastMessage(lastMessage.content); } - - alertLimitReached(); + alertError(getErrorMessage(err)); } finally { toggleDisabledState(false); } diff --git a/apps/demos/Demos/Chat/AIAndChatbotIntegration/Vue/service.ts b/apps/demos/Demos/Chat/AIAndChatbotIntegration/Vue/service.ts index 89c54de11d95..269218f9ef7d 100644 --- a/apps/demos/Demos/Chat/AIAndChatbotIntegration/Vue/service.ts +++ b/apps/demos/Demos/Chat/AIAndChatbotIntegration/Vue/service.ts @@ -26,7 +26,7 @@ export async function getAIResponse(messages: AIMessage[]): Promise temperature: 0.7, }; - const response = await chatService.chat.completions.create(params as any); + const response = await chatService.chat.completions.create(params as any, { maxRetries: 0 }); const data = { choices: response.choices }; return data.choices[0].message?.content || ''; diff --git a/apps/demos/Demos/Chat/AIAndChatbotIntegration/jQuery/index.js b/apps/demos/Demos/Chat/AIAndChatbotIntegration/jQuery/index.js index 4b0fde216add..04ad7dd40dd3 100644 --- a/apps/demos/Demos/Chat/AIAndChatbotIntegration/jQuery/index.js +++ b/apps/demos/Demos/Chat/AIAndChatbotIntegration/jQuery/index.js @@ -26,16 +26,16 @@ $(() => { temperature: 0.7, }; - const response = await chatService.chat.completions.create(params); + const response = await chatService.chat.completions.create(params, { maxRetries: 0 }); const data = { choices: response.choices }; return data.choices[0].message?.content; } - function alertLimitReached() { + function alertError(message) { instance.option({ alerts: [{ - message: 'Request limit reached, try again in a minute.', + message, }], }); @@ -70,10 +70,14 @@ $(() => { renderAssistantMessage(aiResponse); }, 200); - } catch { + } catch (err) { instance.option({ typingUsers: [] }); messages.pop(); - alertLimitReached(); + const errorMessage = + err.error?.message ?? + err.message ?? + 'Unknown error'; + alertError(errorMessage); } finally { toggleDisabledState(false, event); } @@ -87,9 +91,13 @@ $(() => { updateLastMessage(aiResponse); messages.at(-1).content = aiResponse; - } catch { + } catch (err) { updateLastMessage(messages.at(-1).content); - alertLimitReached(); + const errorMessage = + err.error?.message ?? + err.message ?? + 'Unknown error'; + alertError(errorMessage); } finally { toggleDisabledState(false); }