Fix stream error parsing when using Smooth Streaming

This commit is contained in:
Cohee 2024-04-04 21:20:10 +03:00
parent ecc638a76d
commit 813476d72a
1 changed files with 9 additions and 2 deletions

View File

@ -227,7 +227,7 @@ async function* parseStreamData(json) {
} }
} }
return null; yield null;
} }
/** /**
@ -243,6 +243,12 @@ export class SmoothEventSourceStream extends EventSourceStream {
const data = event.data; const data = event.data;
try { try {
const hasFocus = document.hasFocus(); const hasFocus = document.hasFocus();
if (data === '[DONE]') {
lastStr = '';
return controller.enqueue(event);
}
const json = JSON.parse(data); const json = JSON.parse(data);
if (!json) { if (!json) {
@ -261,7 +267,8 @@ export class SmoothEventSourceStream extends EventSourceStream {
lastStr = parsed.chunk; lastStr = parsed.chunk;
hasFocus && await eventSource.emit(event_types.SMOOTH_STREAM_TOKEN_RECEIVED, parsed.chunk); hasFocus && await eventSource.emit(event_types.SMOOTH_STREAM_TOKEN_RECEIVED, parsed.chunk);
} }
} catch { } catch (error) {
console.error('Smooth Streaming parsing error', error);
controller.enqueue(event); controller.enqueue(event);
} }
}, },