createWebSocketRuns
Stream runs, such as an AI agent's answer, that survive reconnects and page reloads. Each run has an id and numbered events. After a reconnect the client resumes after the last event it received, and after a reload it follows the run again from the beginning.
Available from both react-websocket and react-websocket/core. Render a run with useAgentRun. The guide is Resumable runs.
Usage
Annotate start's request parameter: that's what runs.start() accepts.
Parameters
Returns
A WebSocketRuns<Req, T>:
AgentRun
Behavior
- Reconnects. A streaming run sends
resume(runId, after)on every new connection, with the offset of the last event received. Events at or below it are dropped, and an event after a gap asks for the missing ones, as feeds do: each run is a topic of its own. - Reloads. Streaming runs are remembered in
storagewith their policy, and forgotten once they end. A new page follows the rememberedkeepruns from their first event, and sendscancelfor thecancelones, 200 ms later, unless another page says it still follows them. A duplicated tab, or one opened withwindow.open, starts with a copy ofsessionStorage, and so with the runs of the page that is still streaming them. - Policies. When the connection drops,
cancelruns are aborted at once, andcancelgoes out first thing on the next connection, holding the connection open until it does. It doesn't go through the send queue, so it goes out withqueue: falsetoo.keepruns carry on.migrate()keeps the statusopen, so it never aborts a run. - The connection. While runs are streaming, they hold the client's connection, as a mounted hook would.
- Values stay. A run that ended keeps its final
valueuntildelete().