Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

UpsertableStreamDurability.md

211 lines5.7 KB

1---
2id: UpsertableStreamDurability
3title: UpsertableStreamDurability
4---
5​
6Defined in: [packages/ai/src/stream-durability.ts:83](https://github.com/TanStack/ai/blob/main/packages/ai/src/stream-durability.ts#L83)
7​
8A [StreamDurability](StreamDurability.md) that can re-persist an already-stored range
9idempotently.
10​
11A run driver resuming after a crash re-derives the same offsets from its
12source position, so replaying an overlapping range must be a no-op rather
13than producing duplicates. That capability is deliberately a **separate,
14optional method** instead of an optional parameter on `append`:
15​
16- Only adapters that actually support it return this type, so a consumer
17 requiring the capability asks for `UpsertableStreamDurability` and a
18 mismatch is a compile error rather than a runtime failure buried in a
19 run log.
20- Pairing each chunk with its offset structurally makes a length mismatch
21 and an unpaired chunk unrepresentable. A sparse hole is still
22 representable, so implementations must reject one explicitly.
23​
24Implementations MUST validate the entire batch before mutating any stored
25state (so a rejected call never partially applies), MUST reject an offset
26they did not mint themselves (every accepted offset is resumable by
27definition), MUST reject an offset repeated within one batch, and MUST
28reject a hole in the entries array.
29​
30## Extends
31​
32- [`StreamDurability`](StreamDurability.md)\<`TOffset`\>
33​
34## Type Parameters
35​
36### TOffset
37​
38`TOffset` *extends* `string` = `string`
39​
40## Properties
41​
42### append
43​
44```ts
45append: (chunks) => Promise<TOffset[]>;
46```
47​
48Defined in: [packages/ai/src/stream-durability.ts:17](https://github.com/TanStack/ai/blob/main/packages/ai/src/stream-durability.ts#L17)
49​
50Persist a batch before it is delivered and return exactly one resumable
51offset for each chunk, in the same order.
52​
53#### Parameters
54​
55##### chunks
56​
57[`AGUIEvent`](../type-aliases/AGUIEvent.md)[]
58​
59#### Returns
60​
61`Promise`\<`TOffset`[]\>
62​
63#### Inherited from
64​
65[`StreamDurability`](StreamDurability.md).[`append`](StreamDurability.md#append)
66​
67***
68​
69### close
70​
71```ts
72close: () => Promise<void>;
73```
74​
75Defined in: [packages/ai/src/stream-durability.ts:27](https://github.com/TanStack/ai/blob/main/packages/ai/src/stream-durability.ts#L27)
76​
77Terminalize the producer log and unblock live readers. Core awaits this
78for every producer exit, including completion, cancellation, and failure.
79​
80#### Returns
81​
82`Promise`\<`void`\>
83​
84#### Inherited from
85​
86[`StreamDurability`](StreamDurability.md).[`close`](StreamDurability.md#close)
87​
88***
89​
90### read
91​
92```ts
93read: (offset, signal?) => AsyncIterable<{
94 chunk: AGUIEvent;
95 offset: TOffset;
96}>;
97```
98​
99Defined in: [packages/ai/src/stream-durability.ts:19](https://github.com/TanStack/ai/blob/main/packages/ai/src/stream-durability.ts#L19)
100​
101Replay chunks strictly after the supplied adapter-owned offset.
102​
103#### Parameters
104​
105##### offset
106​
107`TOffset`
108​
109##### signal?
110​
111`AbortSignal`
112​
113#### Returns
114​
115`AsyncIterable`\<\{
116 `chunk`: [`AGUIEvent`](../type-aliases/AGUIEvent.md);
117 `offset`: `TOffset`;
118\}\>
119​
120#### Inherited from
121​
122[`StreamDurability`](StreamDurability.md).[`read`](StreamDurability.md#read)
123​
124***
125​
126### resumeFrom
127​
128```ts
129resumeFrom: () => TOffset | null;
130```
131​
132Defined in: [packages/ai/src/stream-durability.ts:12](https://github.com/TanStack/ai/blob/main/packages/ai/src/stream-durability.ts#L12)
133​
134Return the adapter offset captured from the request, or null for a producer.
135​
136#### Returns
137​
138`TOffset` \| `null`
139​
140#### Inherited from
141​
142[`StreamDurability`](StreamDurability.md).[`resumeFrom`](StreamDurability.md#resumefrom)
143​
144***
145​
146### snapshot
147​
148```ts
149snapshot: () => Promise<object[]>;
150```
151​
152Defined in: [packages/ai/src/stream-durability.ts:57](https://github.com/TanStack/ai/blob/main/packages/ai/src/stream-durability.ts#L57)
153​
154Everything stored for this run **at the moment of the call**, in append
155order, then resolve.
156​
157This is the bounded counterpart to [StreamDurability.read](StreamDurability.md#read). `read`
158tails: it parks until the log is terminalized or the caller aborts, so it
159cannot be used to inspect a log whose producer died without calling
160`close` — that log stays open forever and a `for await` over it never
161finishes. `snapshot` exists for exactly that case: a producer resuming a
162run needs to see the prefix a previous host already stored so it can line
163its own output up against it, and it needs that read to *return*.
164​
165Implementations MUST:
166​
167- never wait for more entries — resolve with what is stored, including
168 while the log is still open and still being appended to;
169- resolve to an empty array for a run with nothing stored, rather than
170 throwing. In particular an implementation must not reuse the
171 unknown-run failure path a from-start `read` join takes (`read('-1')` on
172 an empty log is allowed to fail; `snapshot()` is not). A backend over a
173 network may of course still reject on a transport, protocol, or
174 authorization failure — that is a failed call, not an empty run;
175- return a fresh array the caller can keep or mutate without reaching the
176 stored log through it.
177​
178The result is a point-in-time view and carries no lock: a concurrent
179`append` may land immediately after the snapshot is taken, so a caller
180must not treat the last returned offset as the permanent tail.
181​
182#### Returns
183​
184`Promise`\<`object`[]\>
185​
186#### Inherited from
187​
188[`StreamDurability`](StreamDurability.md).[`snapshot`](StreamDurability.md#snapshot)
189​
190***
191​
192### upsert
193​
194```ts
195upsert: (entries) => Promise<TOffset[]>;
196```
197​
198Defined in: [packages/ai/src/stream-durability.ts:90](https://github.com/TanStack/ai/blob/main/packages/ai/src/stream-durability.ts#L90)
199​
200Persist a batch at caller-supplied offsets, replacing any entry already
201stored at the same offset. Returns the offsets in the order supplied.
202​
203#### Parameters
204​
205##### entries
206​
207`object`[]
208​
209#### Returns
210​
211`Promise`\<`TOffset`[]\>