Skip to content

Comments

feat (ui/react): support resuming an ongoing stream#6052

Merged
jeremyphilemon merged 23 commits intomainfrom
jrmy/experimental-resume-2
May 1, 2025
Merged

feat (ui/react): support resuming an ongoing stream#6052
jeremyphilemon merged 23 commits intomainfrom
jrmy/experimental-resume-2

Conversation

@jeremyphilemon
Copy link
Contributor

@jeremyphilemon jeremyphilemon commented Apr 29, 2025

Background

This pull request adds the ability for clients to resume an ongoing chat generation stream after a network disconnect.

Summary

This pull request adds support for the useChat hook to resume an ongoing chat generation stream by exposing experimental_resume() that can be called by any client, typically during the initial mount of the hook.

The experimental_resume function makes a GET request to the api endpoint you've initialized the hook with (or /api/chat by default) and streams the contents of the stream if it is active or fails silently if it has ended.

In order for experimental_resume to work as intended, it requires the usage of the resumable-stream package for stream creation and a redis instance for the package to manage the pub/sub mechanism.

Verification

An example has been added at examples/next-openai/app/use-chat-resume to test the feature. Follow the following steps to test it end-to-end:

  1. Run the development server
  2. Navigate to http://localhost:3000/use-chat-resume
  3. Send a message that will have a longer generation duration, example "Write an essay about Michael Jordan"
  4. Once the generation starts, click the chat id above to open the conversation in a new tab
  5. Verify the stream gets resumed

Tasks

  • Tests have been added / updated (for bug fixes / features)
  • Documentation has been added / updated (for bug fixes / features)
  • A patch changeset for relevant packages has been added
  • Formatting issues have been fixed (run pnpm prettier-fix in the project root)

@jeremyphilemon
Copy link
Contributor Author

v5 version: #6053

4. Returns the latest one to `streamContext.resumableStream()`
5. Falls back to an empty stream if it’s already closed

```ts filename="app/api/chat/route.ts"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The highlighting is broken - wonder if this will lead to issues with the docs rendering. Did you check it in the docs deployment?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm not sure why github isn't highlighting the code snippet, fwiw I did try building this in the docs deployment and it works!

/**
* Resume an ongoing chat generation stream. This does not resume an aborted generation.
*/
experimental_resume: () => Promise<string | null | undefined>;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the meaning of the promise return values?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made it return void

streaming is not yet complete.
</Note>

## Resuming ongoing streams
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @nicoalbanese for docs review

@lgrammel
Copy link
Collaborator

useChat reference docs need updating.

@jeremyphilemon
Copy link
Contributor Author

I added experimental_resume to the reference docs for useChat.

@jeremyphilemon jeremyphilemon merged commit 6c59ae7 into main May 1, 2025
8 checks passed
@jeremyphilemon jeremyphilemon deleted the jrmy/experimental-resume-2 branch May 1, 2025 07:36
jeremyphilemon added a commit that referenced this pull request May 1, 2025
## Background
This pull request fixes a docs formatting issue due to a missing comma
in useChat reference docs.

## Summary

Fixed formatting in useChat reference docs by adding a comma.

## Tasks
- [x] Documentation has been added / updated (for bug fixes / features)
- [x] Formatting issues have been fixed (run `pnpm prettier-fix` in the
project root)

## Related Issues
#6052
jeremyphilemon added a commit that referenced this pull request May 1, 2025
## Background
This pull request fixes a docs formatting issue due to a missing comma
in useChat reference docs.

## Summary

Fixed formatting in useChat reference docs by adding a comma.

## Tasks
- [x] Documentation has been added / updated (for bug fixes / features)
- [x] Formatting issues have been fixed (run `pnpm prettier-fix` in the
project root)

## Related Issues
#6052
jeremyphilemon added a commit that referenced this pull request May 8, 2025
…ming stream (#6224)

## Background

This pull request updates the documentation for the GET handler in the
resumable streams section to account for a race condition that may occur
when the stream ends in-flight.

## Summary

Updated the route by adding the code snippet that handles the above
condition.

## Tasks

- [x] Documentation has been added / updated (for bug fixes / features)
- [x] Formatting issues have been fixed (run `pnpm prettier-fix` in the
project root)

## Related Issues

#6052
jeremyphilemon added a commit that referenced this pull request May 8, 2025
…ming stream (#6224)

## Background

This pull request updates the documentation for the GET handler in the
resumable streams section to account for a race condition that may occur
when the stream ends in-flight.

## Summary

Updated the route by adding the code snippet that handles the above
condition.

## Tasks

- [x] Documentation has been added / updated (for bug fixes / features)
- [x] Formatting issues have been fixed (run `pnpm prettier-fix` in the
project root)

## Related Issues

#6052
lgrammel pushed a commit that referenced this pull request May 8, 2025
…ming stream (#6224)

## Background

This pull request updates the documentation for the GET handler in the
resumable streams section to account for a race condition that may occur
when the stream ends in-flight.

## Summary

Updated the route by adding the code snippet that handles the above
condition.

## Tasks

- [x] Documentation has been added / updated (for bug fixes / features)
- [x] Formatting issues have been fixed (run `pnpm prettier-fix` in the
project root)

## Related Issues

#6052
jacobkerber pushed a commit to jacobkerber/ai that referenced this pull request Jul 15, 2025
## Background

This pull request adds the ability for clients to resume an ongoing chat
generation stream after a network disconnect.

## Summary

This pull request adds support for the `useChat` hook to resume an
ongoing chat generation stream by exposing `experimental_resume()` that
can be called by any client, typically during the initial mount of the
hook.

The `experimental_resume` function makes a `GET` request to the api
endpoint you've initialized the hook with (or `/api/chat` by default)
and streams the contents of the stream if it is active or fails silently
if it has ended.

In order for `experimental_resume` to work as intended, it requires the
usage of the
[`resumable-stream`](https://www.npmjs.com/package/resumable-stream)
package for stream creation and a redis instance for the package to
manage the pub/sub mechanism.

## Verification

An example has been added at `examples/next-openai/app/use-chat-resume`
to test the feature. Follow the following steps to test it end-to-end:
1. Run the development server
2. Navigate to `http://localhost:3000/use-chat-resume`
3. Send a message that will have a longer generation duration, example
"Write an essay about Michael Jordan"
4. Once the generation starts, click the chat id above to open the
conversation in a new tab
5. Verify the stream gets resumed

## Tasks

- [x] Tests have been added / updated (for bug fixes / features)
- [x] Documentation has been added / updated (for bug fixes / features)
- [x] A _patch_ changeset for relevant packages has been added
- [x] Formatting issues have been fixed (run `pnpm prettier-fix` in the
project root)

---------

Co-authored-by: Lars Grammel <lars.grammel@gmail.com>
jacobkerber pushed a commit to jacobkerber/ai that referenced this pull request Jul 15, 2025
This pull request adds the ability for clients to resume an ongoing chat
generation stream after a network disconnect.

This pull request adds support for the `useChat` hook to resume an
ongoing chat generation stream by exposing `experimental_resume()` that
can be called by any client, typically during the initial mount of the
hook.

The `experimental_resume` function makes a `GET` request to the api
endpoint you've initialized the hook with (or `/api/chat` by default)
and streams the contents of the stream if it is active or fails silently
if it has ended.

In order for `experimental_resume` to work as intended, it requires the
usage of the
[`resumable-stream`](https://www.npmjs.com/package/resumable-stream)
package for stream creation and a redis instance for the package to
manage the pub/sub mechanism.

An example has been added at `examples/next-openai/app/use-chat-resume`
to test the feature. Follow the following steps to test it end-to-end:
1. Run the development server
2. Navigate to `http://localhost:3000/use-chat-resume`
3. Send a message that will have a longer generation duration, example
"Write an essay about Michael Jordan"
4. Once the generation starts, click the chat id above to open the
conversation in a new tab
5. Verify the stream gets resumed

- [x] Tests have been added / updated (for bug fixes / features)
- [x] Documentation has been added / updated (for bug fixes / features)
- [x] A _patch_ changeset for relevant packages has been added
- [x] Formatting issues have been fixed (run `pnpm prettier-fix` in the
project root)

---------

Co-authored-by: Lars Grammel <lars.grammel@gmail.com>
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.

2 participants