ai icon indicating copy to clipboard operation
ai copied to clipboard

Unable to cancel or abort streaming UI server actions

Open AaronFriel opened this issue 1 year ago • 8 comments

Feature Description

Provide an ambient AbortController via an API to server actions and a method for cancelling a request.

Use Case

Users may wish to cancel and retry long-running AI requests, especially in streaming and langchain-esque scenarios where completion times are on the order of tens of seconds or longer.

Additional context

Attempting to work around the issue by, e.g., passing a Promise to the server action does not work, as Next.js delays calling the server action while the promise resolves. This can be demonstrated by calling a server action with new Promise<void>((r) => setTimeout(r, 10_000)) and logging the un-awaited value of the promise immediately in the action.

Related issues:

  • https://github.com/facebook/react/issues/28510
  • https://github.com/facebook/react/issues/28511

AaronFriel avatar Mar 07 '24 01:03 AaronFriel

also interested in this.

fjun99 avatar Mar 24 '24 14:03 fjun99

this is crazy that there seems to be no way to do basic abort functionality on next server action or vercel ai rsc streams

psai-terwin avatar Jun 02 '24 22:06 psai-terwin

@AaronFriel Hope you are doing well, did you find any work arround for this. I know that useChat hook has a stop function but in my case I moved away from vercel ai ui in favor of vercel ai rsc, so now that I'm using server actions with streamUI is impossible to send a stop signal.

danielpl10 avatar Jun 06 '24 04:06 danielpl10

+1 Please

C-mbai avatar Jun 06 '24 23:06 C-mbai

This is (current) limitation of React. Aborting server actions is not supported.

lgrammel avatar Jun 07 '24 07:06 lgrammel

@lgrammel hopefully this can be solved, I would hope that some folks from Vercel could chime in on the issues on facebook/react, as it's forcing us to abandon server actions in favor of API routes for AI use cases. I'm sure it's also keeping the AI SDK from supporting server actions or recommending them. Cancellation is important!

AaronFriel avatar Jun 20 '24 17:06 AaronFriel