diff --git a/src/pages/playground/hooks/use-chat-completion.ts b/src/pages/playground/hooks/use-chat-completion.ts index 6dd06e6f..fba3ee50 100644 --- a/src/pages/playground/hooks/use-chat-completion.ts +++ b/src/pages/playground/hooks/use-chat-completion.ts @@ -168,11 +168,11 @@ export default function useChatCompletion( if (chunk?.error) { setTokenResult({ error: true, - errorMessage: extractErrorMessage(chunk) + errorMessage: extractErrorMessage(chunk.error) }); - return; + } else { + joinMessage(chunk); } - joinMessage(chunk); }); } catch (error) { console.log('error:', error); diff --git a/src/utils/fetch-chunk-data.ts b/src/utils/fetch-chunk-data.ts index 6b2b5871..c8d394f2 100644 --- a/src/utils/fetch-chunk-data.ts +++ b/src/utils/fetch-chunk-data.ts @@ -166,12 +166,6 @@ export const readStreamData = async ( while (isReading) { const { done, value } = await reader.read(); - if (done) { - isReading = false; - bufferManager.flush(); - break; - } - try { const chunk = decoder.decode(value, { stream: true }); @@ -189,6 +183,12 @@ export const readStreamData = async ( } catch (error) { bufferManager.add({ error }); } + + if (done) { + isReading = false; + bufferManager.flush(); + break; + } } };