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);
return json;
} catch {
return '[ERROR]';
return jsonString;
}
});
return result;

View file

@ -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(