File tree 1 file changed +15
-3
lines changed
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> {
38
38
if ( sse . data . startsWith ( '[DONE]' ) ) {
39
39
done = true ;
40
40
continue ;
41
- } else {
41
+ }
42
+
43
+ if ( sse . event === null ) {
42
44
let data ;
43
45
44
46
try {
@@ -52,12 +54,22 @@ export class Stream<Item> implements AsyncIterable<Item> {
52
54
if ( data && data . error ) {
53
55
throw new APIError ( undefined , data . error , undefined , undefined ) ;
54
56
}
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
+ }
55
68
// TODO: Is this where the error should be thrown?
56
69
if ( sse . event == 'error' ) {
57
70
throw new APIError ( undefined , data . error , data . message , undefined ) ;
58
71
}
59
-
60
- yield data ;
72
+ yield { event : sse . event , data : data } as any ;
61
73
}
62
74
}
63
75
done = true ;
You can’t perform that action at this time.
0 commit comments