Vercel AI SDK
useChat answers that survive a refresh, a dropped connection and a second tab, with Stop from any of them.
PulseChatTransport is a transport for the AI SDK's useChat. Your route starts the answer and returns at once, and the answer streams through a Pulse channel instead of the HTTP response. A refresh, a dropped connection or another tab picks it up where it is, and Stop works from any of them.
The route
Keep your streamText call. Write its UI message stream to the chat's channel with pipe, and answer the request with a 202.
afterkeeps the function running until the answer ends. Elsewhere, use your platform'swaitUntil; on a long-running server, don't awaitpipe.- Save messages in
onFinishas usual and pass them touseChatasmessages. The channel holds the answer while it streams, not your chat history. - Everything
useChatstreams passes through as is: text, reasoning, tool calls, approvals and data parts.
The chat
Give useChat the transport. It sends the same request as the default transport (id, messages, trigger, messageId and your body), then reads the answer from the channel.
With PulseProvider, pass its client (usePulseClient()) as client instead, in a useMemo, to share its connections.
Stop
transport.stop(id) stops the answer for everyone: the route's s.signal aborts, which cancels the model call, and every tab's stream ends as aborted. useChat's own stop() only stops this tab listening, and useChat also calls it when the component unmounts, so leaving the page never stops an answer.
The token
The browser's token must allow the chat's channel, and publishing to it for Stop. Scope it to the user:
Each UI message chunk is an ai-sdk event with the chunk as its data, between the run's RUN_STARTED and RUN_FINISHED, and text deltas are merged into frames. See AI streams for how long a stream is kept and what else a run can hold.