-
Notifications
You must be signed in to change notification settings - Fork 265
Type-safe react stub streaming calls #304
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
🦋 Changeset detectedLatest commit: eea893b The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
BTW, my idea is to integrate streaming properly into stubs and simplify everything by just keeping type safety in stubs (I think |
76fa487
to
5fa2531
Compare
commit: |
5fa2531
to
5c25ce4
Compare
bcb8c81
to
eea893b
Compare
@whoiskatrin @threepointone I'm trying a different approach for streaming methods using async generators. The example above will look like this: class MyAgent extends Agent<typeof env, {}> {
@callable({ streaming: true })
performStream(response: StreamingResponse<number, string>, result: string): void {
response.send(1);
response.send(2);
response.end(result);
}
}
// in react
const { streamingStub } = useAgent<MyAgent, {}>({ agent: "my-agent" });
// we can consume chunks with for await but not return value
for await (const chunk of streamingStub.performStream('Hello')) {
console.log('streaming: ', chunk);
} For chunks (yielded values) consumption, this is a very clean approach. If we need to consume the With this approach, it would also be worth of supporting declaring streaming method implementations as async generator functions, something like: class MyAgent extends Agent<typeof env, {}> {
@callable({ streaming: true })
async * performStream(result: string) { // returns a AsyncGenerator<number, string>
yield 1;
yield await Promise.resolve(2);
return result;
}
} Edit: |
Proposal to add type safety to streaming calls.
Usage:
I'm opening this as a draft as I'm not very happy with the current solution, first argument being a
StreamOptions
doesn't seem right.I'll gladly accept suggestions on how to improve it.