From 92f09c275b47e45fe01f969aaa2432268f6f122b Mon Sep 17 00:00:00 2001 From: Jing Hua Date: Sun, 19 Mar 2023 22:22:24 +0800 Subject: [PATCH] fix: missing bytes when using api endpoint --- src/api/helper.ts | 2 +- src/hooks/useSubmit.ts | 14 +++++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/api/helper.ts b/src/api/helper.ts index 76ccb6d..f46b9c9 100644 --- a/src/api/helper.ts +++ b/src/api/helper.ts @@ -16,7 +16,7 @@ export const parseEventSource = ( const json = JSON.parse(jsonString); return json; } catch { - return '[ERROR]'; + return jsonString; } }); return result; diff --git a/src/hooks/useSubmit.ts b/src/hooks/useSubmit.ts index a798b9e..1056615 100644 --- a/src/hooks/useSubmit.ts +++ b/src/hooks/useSubmit.ts @@ -83,21 +83,25 @@ const useSubmit = () => { ); const reader = stream.getReader(); let reading = true; + let partial = ''; while (reading && useStore.getState().generating) { const { done, value } = await reader.read(); - - const result = parseEventSource(new TextDecoder().decode(value)); + const result = parseEventSource( + partial + new TextDecoder().decode(value) + ); + partial = ''; if (result === '[DONE]' || done) { reading = false; } else { const resultString = result.reduce((output: string, curr) => { - if (typeof curr === 'string') return output; - else { + if (typeof curr === 'string') { + partial += curr; + } else { const content = curr.choices[0].delta.content; if (content) output += content; - return output; } + return output; }, ''); const updatedChats: ChatInterface[] = JSON.parse(