Skip to content

Commit

Permalink
Revert "chore(internal): streaming refactors (#1261)"
Browse files Browse the repository at this point in the history
This reverts commit dd4af93.
  • Loading branch information
RobertCraigie committed Jan 13, 2025
1 parent 6a72fd7 commit c17d39e
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/streaming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ export class Stream<Item> implements AsyncIterable<Item> {
if (sse.data.startsWith('[DONE]')) {
done = true;
continue;
} else {
}

if (sse.event === null) {
let data;

try {
Expand All @@ -52,12 +54,22 @@ export class Stream<Item> implements AsyncIterable<Item> {
if (data && data.error) {
throw new APIError(undefined, data.error, undefined, undefined);
}

yield data;
} else {
let data;
try {
data = JSON.parse(sse.data);
} catch (e) {
console.error(`Could not parse message into JSON:`, sse.data);
console.error(`From chunk:`, sse.raw);
throw e;
}
// TODO: Is this where the error should be thrown?
if (sse.event == 'error') {
throw new APIError(undefined, data.error, data.message, undefined);
}

yield data;
yield { event: sse.event, data: data } as any;
}
}
done = true;
Expand Down

0 comments on commit c17d39e

Please sign in to comment.