Skip to content
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

feat(ai/rsc): readStreamableValue #1114

Merged
merged 2 commits into from
Mar 6, 2024
Merged

feat(ai/rsc): readStreamableValue #1114

merged 2 commits into from
Mar 6, 2024

Conversation

shuding
Copy link
Member

@shuding shuding commented Mar 6, 2024

This PR removes the useStreamableValue API and implements readStreamableValue instead.

readStreamableValue takes a streamable value created via the createStreamableValue().value API, and returns an async iterator.

// Inside your AI action:
async function action() {
  'use server'
  const streamable = createStreamableValue();
  streamable.update(1);
  streamable.update(2);
  streamable.done(3);
  // ...
  return streamable.value;
}

And to read the value:

const streamableValue = await action()
for await (const v of readStreamableValue(streamableValue)) {
  console.log(v)
}

This logs out 1, 2, 3 on console.

This API is simpler and more imperative. It gives back retries, error handling and state tracking to the userland.

@shuding shuding marked this pull request as ready for review March 6, 2024 17:55
@shuding shuding merged commit 2367b59 into main Mar 6, 2024
6 checks passed
@shuding shuding deleted the shu/42ad branch March 6, 2024 18:02
MaxLeiter pushed a commit that referenced this pull request Mar 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants