Open History to see the last commit
1import { memoryStream, resumeServerSentEventsResponse } from '@tanstack/ai'
2
3/**
4 * Delivery durability for the generation routes — the layer that makes a run
5 * resumable, alongside the state persistence in `generation-server-store.ts`.
6 *
7 * `memoryStream` logs each chunk so a rejoining client replays it instead of
8 * re-running the model. Routes opt in by passing the adapter as `durability` on
9 * their `toServerSentEventsResponse`, and by serving
10 * {@link replayGenerationIfResuming} from a `GET`.
11 *
12 * There is no route-side "detach" helper. A durable response already survives a
13 * client disconnect: cancelling it cancels only the reader, while the run keeps
14 * draining into the log to completion, so a mount-time `joinRun` tails it to the
15 * end. The library owns the run's lifetime once `durability` is set.
16 *
17 * `memoryStream` keeps logs in a process-global map: development and
18 * single-process deployments only. Swap it for `durableStream(request, {
19 * server })` from `@tanstack/ai-durable-stream` in production; nothing else here
20 * changes.
21 */
22
23/**
24 * The GET half of `joinRun`: replay a run when the request carries a resume
25 * offset, otherwise `null` so the route can fall through to whatever else its
26 * GET serves (the generation routes also answer `reconstructGeneration` there).
27 *
28 * The run id rides the `X-Run-Id` header or `?runId`, and the offset the
29 * `Last-Event-ID` header or `?offset` — ask the adapter via `resumeFrom()`
30 * rather than sniffing query params.
31 */
32export function replayGenerationIfResuming(request: Request): Response | null {
33 const durability = memoryStream(request)
34 if (durability.resumeFrom() === null) return null
35 return resumeServerSentEventsResponse({ adapter: durability })
36}