From eb2cae7a77582371f0437068d2bb188ed9f455c3 Mon Sep 17 00:00:00 2001 From: jialin Date: Fri, 15 Aug 2025 19:10:06 +0800 Subject: [PATCH] fix: break early while reading stream data --- src/pages/playground/hooks/use-chat-completion.ts | 6 +++--- src/utils/fetch-chunk-data.ts | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) 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; + } } };