[Feat][Js] Make ai.generateStream no longer require a double await #1602
+127
−91
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #1593
Rewrote generateStream so you longer need to call
await
to get the return value of objects that are themselves async.As a style change I named the struct for {resolve, reject, promise} as Task because that's how I've commonly seen this design called.
The former method for passing into an async generator was a bit fragile because it depended on the exact order of the callbacks to the run loop (in fact, in my refactoring I broke it). Instead I created a buffering utility class called
Channel<T>
, inspired by Go channels but implementsAsyncIterable
for receiving.I've had to change the copyright script from
ts-node
totsx
in many of my PRs because I get import vs require errors trying to load child-process. Since tsx is more officially supported I decided to change the way the script runs rather than adding module resolution annotations.Checklist (if applicable):