fix: missing bytes when using api endpoint

This commit is contained in:
Jing Hua 2023-03-19 22:22:24 +08:00
parent 1799c40ba3
commit 92f09c275b
2 changed files with 10 additions and 6 deletions

View file

@ -16,7 +16,7 @@ export const parseEventSource = (
const json = JSON.parse(jsonString); const json = JSON.parse(jsonString);
return json; return json;
} catch { } catch {
return '[ERROR]'; return jsonString;
} }
}); });
return result; return result;

View file

@ -83,21 +83,25 @@ const useSubmit = () => {
); );
const reader = stream.getReader(); const reader = stream.getReader();
let reading = true; let reading = true;
let partial = '';
while (reading && useStore.getState().generating) { while (reading && useStore.getState().generating) {
const { done, value } = await reader.read(); const { done, value } = await reader.read();
const result = parseEventSource(
const result = parseEventSource(new TextDecoder().decode(value)); partial + new TextDecoder().decode(value)
);
partial = '';
if (result === '[DONE]' || done) { if (result === '[DONE]' || done) {
reading = false; reading = false;
} else { } else {
const resultString = result.reduce((output: string, curr) => { const resultString = result.reduce((output: string, curr) => {
if (typeof curr === 'string') return output; if (typeof curr === 'string') {
else { partial += curr;
} else {
const content = curr.choices[0].delta.content; const content = curr.choices[0].delta.content;
if (content) output += content; if (content) output += content;
return output;
} }
return output;
}, ''); }, '');
const updatedChats: ChatInterface[] = JSON.parse( const updatedChats: ChatInterface[] = JSON.parse(