File tree Expand file tree Collapse file tree 1 file changed +15
-3
lines changed Expand file tree Collapse file tree 1 file changed +15
-3
lines changed Original file line number Diff line number Diff line change @@ -38,7 +38,9 @@ export class Stream<Item> implements AsyncIterable<Item> {
3838 if ( sse . data . startsWith ( '[DONE]' ) ) {
3939 done = true ;
4040 continue ;
41- } else {
41+ }
42+
43+ if ( sse . event === null ) {
4244 let data ;
4345
4446 try {
@@ -52,12 +54,22 @@ export class Stream<Item> implements AsyncIterable<Item> {
5254 if ( data && data . error ) {
5355 throw new APIError ( undefined , data . error , undefined , undefined ) ;
5456 }
57+
58+ yield data ;
59+ } else {
60+ let data ;
61+ try {
62+ data = JSON . parse ( sse . data ) ;
63+ } catch ( e ) {
64+ console . error ( `Could not parse message into JSON:` , sse . data ) ;
65+ console . error ( `From chunk:` , sse . raw ) ;
66+ throw e ;
67+ }
5568 // TODO: Is this where the error should be thrown?
5669 if ( sse . event == 'error' ) {
5770 throw new APIError ( undefined , data . error , data . message , undefined ) ;
5871 }
59-
60- yield data ;
72+ yield { event : sse . event , data : data } as any ;
6173 }
6274 }
6375 done = true ;
You can’t perform that action at this time.
0 commit comments