vercel / ai

Build AI-powered applications with React, Svelte, Vue, and Solid
https://sdk.vercel.ai/docs
Other
9.43k stars 1.38k forks source link

Unable to cancel or abort streaming UI server actions #1122

Open AaronFriel opened 6 months ago

AaronFriel commented 6 months ago

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:

fjun99 commented 5 months ago

also interested in this.

psai-terwin commented 3 months ago

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

danielpl10 commented 3 months ago

@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.

C-mbai commented 3 months ago

+1 Please

lgrammel commented 3 months ago

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

AaronFriel commented 2 months ago

@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!

alicercedigital commented 1 week ago

Does someone found any workaround?

camwest commented 1 week ago

Does someone found any workaround?

Workaround for Handling Long-Running Server Actions

Implementation Steps

  1. Assign the server action promise to a ref
  2. Create a separate async workflow:
    • Perform the await
    • Add a UI gesture for cancellation
  3. When user taps to cancel:
    • Set a piece of state
    • When the promise resolves, discard it
    • Kick off a new process

Results

Note: The server action continues running, but its result is ignored