From c5346130875365a3be287c1447ed2db8d3147809 Mon Sep 17 00:00:00 2001 From: Baptiste Arnaud Date: Wed, 2 Aug 2023 08:10:26 +0200 Subject: [PATCH] :children_crossing: (openai) Display OpenAI initial response error --- .../openai/getChatCompletionStream.ts | 2 ++ .../src/pages/api/integrations/openai/streamer.ts | 15 ++++++++++++--- packages/embeds/js/package.json | 2 +- .../ConversationContainer.tsx | 1 + .../blocks/integrations/openai/streamChat.ts | 4 +--- packages/embeds/nextjs/package.json | 2 +- packages/embeds/react/package.json | 2 +- 7 files changed, 19 insertions(+), 9 deletions(-) diff --git a/apps/viewer/src/features/blocks/integrations/openai/getChatCompletionStream.ts b/apps/viewer/src/features/blocks/integrations/openai/getChatCompletionStream.ts index 19a96d838..fb3c5d3bb 100644 --- a/apps/viewer/src/features/blocks/integrations/openai/getChatCompletionStream.ts +++ b/apps/viewer/src/features/blocks/integrations/openai/getChatCompletionStream.ts @@ -52,5 +52,7 @@ export const getChatCompletionStream = messages, }) + if (!response.ok) return response + return OpenAIStream(response) } diff --git a/apps/viewer/src/pages/api/integrations/openai/streamer.ts b/apps/viewer/src/pages/api/integrations/openai/streamer.ts index 69ff621c4..b15a3d14d 100644 --- a/apps/viewer/src/pages/api/integrations/openai/streamer.ts +++ b/apps/viewer/src/pages/api/integrations/openai/streamer.ts @@ -60,15 +60,24 @@ const handler = async (req: Request) => { ) return new Response('Current block is not an OpenAI block', { status: 400 }) - const stream = await getChatCompletionStream(conn)( + const streamOrResponse = await getChatCompletionStream(conn)( state, block.options, messages ) - if (!stream) return new Response('Could not create stream', { status: 400 }) + if (!streamOrResponse) + return new Response('Could not create stream', { status: 400 }) - return new StreamingTextResponse(stream, { + if ('ok' in streamOrResponse) + return new Response(streamOrResponse.body, { + status: streamOrResponse.status, + headers: { + 'Access-Control-Allow-Origin': '*', + }, + }) + + return new StreamingTextResponse(streamOrResponse, { headers: { 'Access-Control-Allow-Origin': '*', }, diff --git a/packages/embeds/js/package.json b/packages/embeds/js/package.json index 5d321f59e..eceb3f9cf 100644 --- a/packages/embeds/js/package.json +++ b/packages/embeds/js/package.json @@ -1,6 +1,6 @@ { "name": "@typebot.io/js", - "version": "0.1.13", + "version": "0.1.14", "description": "Javascript library to display typebots on your website", "type": "module", "main": "dist/index.js", diff --git a/packages/embeds/js/src/components/ConversationContainer/ConversationContainer.tsx b/packages/embeds/js/src/components/ConversationContainer/ConversationContainer.tsx index abe640c66..3e13c6e13 100644 --- a/packages/embeds/js/src/components/ConversationContainer/ConversationContainer.tsx +++ b/packages/embeds/js/src/components/ConversationContainer/ConversationContainer.tsx @@ -105,6 +105,7 @@ export const ConversationContainer = (props: Props) => { }) const streamMessage = (content: string) => { + console.log('STREAM', content) setIsSending(false) const lastChunk = [...chatChunks()].pop() if (!lastChunk) return diff --git a/packages/embeds/js/src/features/blocks/integrations/openai/streamChat.ts b/packages/embeds/js/src/features/blocks/integrations/openai/streamChat.ts index b91246621..be0aecae7 100644 --- a/packages/embeds/js/src/features/blocks/integrations/openai/streamChat.ts +++ b/packages/embeds/js/src/features/blocks/integrations/openai/streamChat.ts @@ -37,9 +37,7 @@ export const streamChat = if (!res.ok) { return { - error: { - message: (await res.text()) || 'Failed to fetch the chat response.', - }, + error: (await res.json()) || 'Failed to fetch the chat response.', } } diff --git a/packages/embeds/nextjs/package.json b/packages/embeds/nextjs/package.json index 05c9b4592..cbb81877d 100644 --- a/packages/embeds/nextjs/package.json +++ b/packages/embeds/nextjs/package.json @@ -1,6 +1,6 @@ { "name": "@typebot.io/nextjs", - "version": "0.1.13", + "version": "0.1.14", "description": "Convenient library to display typebots on your Next.js website", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/packages/embeds/react/package.json b/packages/embeds/react/package.json index 2d7fc0209..c217f3947 100644 --- a/packages/embeds/react/package.json +++ b/packages/embeds/react/package.json @@ -1,6 +1,6 @@ { "name": "@typebot.io/react", - "version": "0.1.13", + "version": "0.1.14", "description": "Convenient library to display typebots on your Next.js website", "main": "dist/index.js", "types": "dist/index.d.ts",