@tanstack/ai-client 0.22.0 → 0.23.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -1
- package/dist/esm/audio-recorder.js +190 -213
- package/dist/esm/audio-recorder.js.map +1 -1
- package/dist/esm/chat-client.d.ts +172 -3
- package/dist/esm/chat-client.js +1656 -1386
- package/dist/esm/chat-client.js.map +1 -1
- package/dist/esm/cleared-stream-tracker.d.ts +23 -0
- package/dist/esm/cleared-stream-tracker.js +97 -0
- package/dist/esm/cleared-stream-tracker.js.map +1 -0
- package/dist/esm/client-persistor.d.ts +25 -12
- package/dist/esm/client-persistor.js +260 -235
- package/dist/esm/client-persistor.js.map +1 -1
- package/dist/esm/connection-adapters.d.ts +231 -10
- package/dist/esm/connection-adapters.js +989 -574
- package/dist/esm/connection-adapters.js.map +1 -1
- package/dist/esm/devtools-noop.d.ts +1 -0
- package/dist/esm/devtools-noop.js +79 -139
- package/dist/esm/devtools-noop.js.map +1 -1
- package/dist/esm/devtools.d.ts +31 -1
- package/dist/esm/devtools.js +977 -1127
- package/dist/esm/devtools.js.map +1 -1
- package/dist/esm/events.js +224 -226
- package/dist/esm/events.js.map +1 -1
- package/dist/esm/generation-client.d.ts +145 -2
- package/dist/esm/generation-client.js +659 -321
- package/dist/esm/generation-client.js.map +1 -1
- package/dist/esm/generation-reconstruct.d.ts +21 -0
- package/dist/esm/generation-reconstruct.js +85 -0
- package/dist/esm/generation-reconstruct.js.map +1 -0
- package/dist/esm/generation-types.d.ts +289 -3
- package/dist/esm/generation-types.js +356 -13
- package/dist/esm/generation-types.js.map +1 -1
- package/dist/esm/index.d.ts +9 -4
- package/dist/esm/index.js +7 -39
- package/dist/esm/interrupt-manager.d.ts +77 -0
- package/dist/esm/interrupt-manager.js +787 -0
- package/dist/esm/interrupt-manager.js.map +1 -0
- package/dist/esm/mcp-app-bridge.js +56 -64
- package/dist/esm/mcp-app-bridge.js.map +1 -1
- package/dist/esm/realtime-client.js +366 -440
- package/dist/esm/realtime-client.js.map +1 -1
- package/dist/esm/response-stream.js +19 -26
- package/dist/esm/response-stream.js.map +1 -1
- package/dist/esm/sse-parser.js +44 -47
- package/dist/esm/sse-parser.js.map +1 -1
- package/dist/esm/sse-utils.js +8 -9
- package/dist/esm/sse-utils.js.map +1 -1
- package/dist/esm/storage-adapters.d.ts +62 -0
- package/dist/esm/storage-adapters.js +174 -0
- package/dist/esm/storage-adapters.js.map +1 -0
- package/dist/esm/types.d.ts +212 -10
- package/dist/esm/types.js +38 -7
- package/dist/esm/types.js.map +1 -1
- package/dist/esm/video-generation-client.d.ts +113 -2
- package/dist/esm/video-generation-client.js +665 -379
- package/dist/esm/video-generation-client.js.map +1 -1
- package/package.json +7 -7
- package/src/chat-client.ts +1079 -61
- package/src/cleared-stream-tracker.ts +151 -0
- package/src/client-persistor.ts +102 -33
- package/src/connection-adapters.ts +1185 -142
- package/src/devtools-noop.ts +4 -3
- package/src/devtools.ts +121 -3
- package/src/generation-client.ts +563 -13
- package/src/generation-reconstruct.ts +121 -0
- package/src/generation-types.ts +727 -3
- package/src/index.ts +56 -1
- package/src/interrupt-manager.ts +1440 -0
- package/src/storage-adapters.ts +242 -0
- package/src/types.ts +301 -9
- package/src/video-generation-client.ts +479 -13
- package/dist/esm/index.js.map +0 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ConnectConnectionAdapter } from './connection-adapters.js';
|
|
2
|
-
import { GenerationClientOptions, GenerationClientState, GenerationFetcher } from './generation-types.js';
|
|
2
|
+
import { GenerationClientOptions, GenerationClientState, GenerationFetcher, GenerationResumeSnapshot } from './generation-types.js';
|
|
3
3
|
/**
|
|
4
4
|
* A lightweight, generic client for one-shot generation tasks
|
|
5
5
|
* (image, speech, transcription, summarize).
|
|
@@ -38,10 +38,14 @@ import { GenerationClientOptions, GenerationClientState, GenerationFetcher } fro
|
|
|
38
38
|
export declare class GenerationClient<TInput extends Record<string, any>, TResult, TOutput = TResult> {
|
|
39
39
|
private readonly connection;
|
|
40
40
|
private readonly fetcher;
|
|
41
|
+
private readonly hydrateGenerationHandler;
|
|
42
|
+
private readonly joinRunHandler;
|
|
41
43
|
private readonly uniqueId;
|
|
42
44
|
private readonly devtoolsMetadata;
|
|
43
45
|
private readonly devtoolsBridge;
|
|
44
46
|
private readonly threadId;
|
|
47
|
+
private readonly persistenceScope;
|
|
48
|
+
private readonly serverDriven;
|
|
45
49
|
private body;
|
|
46
50
|
private result;
|
|
47
51
|
private input;
|
|
@@ -49,9 +53,14 @@ export declare class GenerationClient<TInput extends Record<string, any>, TResul
|
|
|
49
53
|
private isLoading;
|
|
50
54
|
private error;
|
|
51
55
|
private status;
|
|
56
|
+
private resumeSnapshot;
|
|
57
|
+
private lastEmittedResumeState;
|
|
52
58
|
private abortController;
|
|
59
|
+
private rejoinedRunId;
|
|
53
60
|
private readonly callbacksRef;
|
|
54
61
|
private devtoolsMounted;
|
|
62
|
+
private disposed;
|
|
63
|
+
private serverHydrationStarted;
|
|
55
64
|
constructor(options: GenerationClientOptions<TInput, TResult, TOutput> & ({
|
|
56
65
|
connection: ConnectConnectionAdapter;
|
|
57
66
|
fetcher?: never;
|
|
@@ -69,6 +78,15 @@ export declare class GenerationClient<TInput extends Record<string, any>, TResul
|
|
|
69
78
|
generate(input: TInput): Promise<void>;
|
|
70
79
|
/**
|
|
71
80
|
* Process a stream of AG-UI events from the streaming connection adapter.
|
|
81
|
+
*
|
|
82
|
+
* Throws {@link GENERATION_STREAM_TRUNCATED_MESSAGE} when the iteration ends
|
|
83
|
+
* without a terminal chunk. A `for await` over a stream that simply stops —
|
|
84
|
+
* proxy idle timeout, server restart, a durable log missing its terminal
|
|
85
|
+
* append — returns normally and would otherwise leave the caller's `status`
|
|
86
|
+
* on `generating` forever, with the persisted snapshot still `running` so
|
|
87
|
+
* every reload rejoins the same dead run. Throwing routes it through the
|
|
88
|
+
* caller's error path instead, which settles the status and rewrites the
|
|
89
|
+
* snapshot so nothing chases it again.
|
|
72
90
|
*/
|
|
73
91
|
private processStream;
|
|
74
92
|
/**
|
|
@@ -76,7 +94,10 @@ export declare class GenerationClient<TInput extends Record<string, any>, TResul
|
|
|
76
94
|
*/
|
|
77
95
|
stop(): void;
|
|
78
96
|
/**
|
|
79
|
-
* Clear the result, error, and return to idle state.
|
|
97
|
+
* Clear the result, error, and return to idle state. Also drops the client's
|
|
98
|
+
* in-memory resume snapshot, so a remount restores nothing. The server-side
|
|
99
|
+
* record is untouched — this client no longer writes one — so a full page
|
|
100
|
+
* reload under `persistence: true` re-hydrates the last generation again.
|
|
80
101
|
*/
|
|
81
102
|
reset(): void;
|
|
82
103
|
/**
|
|
@@ -88,6 +109,7 @@ export declare class GenerationClient<TInput extends Record<string, any>, TResul
|
|
|
88
109
|
getIsLoading(): boolean;
|
|
89
110
|
getError(): Error | undefined;
|
|
90
111
|
getStatus(): GenerationClientState;
|
|
112
|
+
getResumeSnapshot(): GenerationResumeSnapshot | undefined;
|
|
91
113
|
private setResult;
|
|
92
114
|
private setIsLoading;
|
|
93
115
|
private setError;
|
|
@@ -96,4 +118,125 @@ export declare class GenerationClient<TInput extends Record<string, any>, TResul
|
|
|
96
118
|
private createDevtoolsMetadata;
|
|
97
119
|
private generateUniqueId;
|
|
98
120
|
private createRunContext;
|
|
121
|
+
private observeResumeSnapshot;
|
|
122
|
+
/**
|
|
123
|
+
* Notify the (internal) snapshot listener AND emit the public resume state.
|
|
124
|
+
* The snapshot stays internal (persistence + devtools); the hook consumes
|
|
125
|
+
* `resumeState`, mirroring the chat client.
|
|
126
|
+
*/
|
|
127
|
+
private notifyResumeSnapshotChanged;
|
|
128
|
+
/**
|
|
129
|
+
* Derive the public `resumeState` from the internal snapshot: the in-flight
|
|
130
|
+
* run identity, with any in-flight artifact refs folded under it. `null` once
|
|
131
|
+
* no run is in flight.
|
|
132
|
+
*
|
|
133
|
+
* The snapshot is rebuilt for every chunk, so emitting unconditionally would
|
|
134
|
+
* hand each framework hook a fresh object per chunk and re-render the
|
|
135
|
+
* component on every stream event. `resumeState` only changes at run
|
|
136
|
+
* boundaries and when artifacts land, so skip the notification unless it
|
|
137
|
+
* materially changed — same gate the persistence writes use.
|
|
138
|
+
*/
|
|
139
|
+
private emitResumeState;
|
|
140
|
+
/**
|
|
141
|
+
* Repaint the normal fields from a restored snapshot (client store or server
|
|
142
|
+
* hydrate), so a reload presents the run in `result` / `status` / `error`
|
|
143
|
+
* exactly as a just-finished run would, never a bolt-on snapshot object.
|
|
144
|
+
* `isLoading` stays false: the client never auto-tails a restored run. The
|
|
145
|
+
* snapshot is not re-persisted here (it came from storage / the server).
|
|
146
|
+
*
|
|
147
|
+
* When the activity's mapper DECLINES a `complete` snapshot the repaint
|
|
148
|
+
* settles as an error instead: `success` with a `null` result is a state no
|
|
149
|
+
* consumer can render, and it hides the real cause (an output artifact
|
|
150
|
+
* persisted without a serve URL). A decline on any other status is expected —
|
|
151
|
+
* a `running` snapshot has no result yet, the rejoin will deliver it.
|
|
152
|
+
*/
|
|
153
|
+
private repaintFromSnapshot;
|
|
154
|
+
/**
|
|
155
|
+
* Report a `complete` snapshot the activity's mapper could not rebuild.
|
|
156
|
+
* Runs after the status/error repaint above, so it wins over the snapshot's
|
|
157
|
+
* own `complete` status.
|
|
158
|
+
*/
|
|
159
|
+
private reportUnrestorableResult;
|
|
160
|
+
/**
|
|
161
|
+
* Repaint a restored snapshot (client store or server hydrate) and, when it
|
|
162
|
+
* reports a run still in flight, tail that run to completion via `joinRun`
|
|
163
|
+
* (from the connection, or the `joinRun` option when the transport can't
|
|
164
|
+
* carry one).
|
|
165
|
+
*
|
|
166
|
+
* A `running` snapshot that no `joinRun` handler can tail is repainted as an
|
|
167
|
+
* interrupted error instead of a `generating` status that would never
|
|
168
|
+
* settle: an interrupted generation cannot be resumed, only re-run.
|
|
169
|
+
*/
|
|
170
|
+
private repaintRestoredSnapshot;
|
|
171
|
+
/**
|
|
172
|
+
* Build the restorable result shape from the snapshot and hand it to the
|
|
173
|
+
* per-activity `reconstructResult` mapper (injected by the specialized
|
|
174
|
+
* client/hook, which knows the concrete result type).
|
|
175
|
+
*
|
|
176
|
+
* Returns `null` both when no mapper is set (nothing to rebuild — `result`
|
|
177
|
+
* simply stays null) and when the mapper declines. The caller distinguishes
|
|
178
|
+
* the two: see {@link repaintFromSnapshot}.
|
|
179
|
+
*/
|
|
180
|
+
private reconstructRestoredResult;
|
|
181
|
+
/**
|
|
182
|
+
* The plain (non-Response) fetcher path never observes stream chunks, so
|
|
183
|
+
* the terminal snapshot is built here from the fetcher's own result. A
|
|
184
|
+
* stale `error` from a previous run is intentionally dropped — this run
|
|
185
|
+
* succeeded.
|
|
186
|
+
*/
|
|
187
|
+
private completePlainFetcherResumeSnapshot;
|
|
188
|
+
/**
|
|
189
|
+
* Records a transport-level failure (network drop, throwing callback) in
|
|
190
|
+
* the snapshot. Without this, only a server-emitted RUN_ERROR chunk would
|
|
191
|
+
* mark the snapshot `error`, leaving a persisted record that claims the
|
|
192
|
+
* run is still in flight.
|
|
193
|
+
*/
|
|
194
|
+
private recordResumeSnapshotError;
|
|
195
|
+
/**
|
|
196
|
+
* Drop the client's in-memory snapshot and re-emit. Purely local — this
|
|
197
|
+
* client writes no storage, so nothing persisted is removed.
|
|
198
|
+
*/
|
|
199
|
+
private clearResumeSnapshot;
|
|
200
|
+
/**
|
|
201
|
+
* Server-driven mount hydration entry point (`persistence: true`). Runs at
|
|
202
|
+
* most once, from the commit-phase mount path (`mountDevtools`) — never the
|
|
203
|
+
* constructor / render phase — so remounts and speculative renders can't
|
|
204
|
+
* re-fire the hydrate GET.
|
|
205
|
+
*/
|
|
206
|
+
private maybeHydrateFromServer;
|
|
207
|
+
/**
|
|
208
|
+
* Server-driven mount hydration (`persistence: true`). The client holds no
|
|
209
|
+
* local snapshot; on mount it asks the server — keyed by the stable threadId —
|
|
210
|
+
* for the last generation's resume snapshot, validates it, and repaints it. It
|
|
211
|
+
* never auto-starts a run, and never blocks: a `generate()` that starts first
|
|
212
|
+
* owns the client and hydration backs off, mirroring the chat client.
|
|
213
|
+
*
|
|
214
|
+
* A genuine **miss** (the server reports no record for the thread) is silent —
|
|
215
|
+
* a fresh thread is not an error. A genuine **failure** (transport error, a
|
|
216
|
+
* 403 from the authorize gate, a malformed body, a record the client's own
|
|
217
|
+
* validator rejects) is surfaced through `status` / `error` / `onError`, so a
|
|
218
|
+
* broken server is distinguishable from an empty one and the app can retry.
|
|
219
|
+
*/
|
|
220
|
+
private hydrateFromServer;
|
|
221
|
+
/**
|
|
222
|
+
* Surface a hydration failure on the observable fields. Skipped when a
|
|
223
|
+
* `generate()` took ownership while the hydrate GET was in flight — the live
|
|
224
|
+
* run's state must win over a stale mount-time failure.
|
|
225
|
+
*/
|
|
226
|
+
private failHydration;
|
|
227
|
+
/**
|
|
228
|
+
* Re-attach to an already-loaded `running` snapshot (the remount case). Safe
|
|
229
|
+
* to call repeatedly: `rejoinInFlight` dedupes on `rejoinedRunId` and bails
|
|
230
|
+
* when a run is already in flight, so on the first mount (where the rejoin was
|
|
231
|
+
* already started from `repaintRestoredSnapshot`) this is a no-op.
|
|
232
|
+
*/
|
|
233
|
+
private maybeResumeInFlight;
|
|
234
|
+
/**
|
|
235
|
+
* Re-attach to a run that is still generating and stream it to completion,
|
|
236
|
+
* mirroring the chat client's mount-time rejoin. Reuses `processStream`, so
|
|
237
|
+
* `result` / `progress` / `status` repaint from the replayed chunks exactly as
|
|
238
|
+
* a live run does. Best-effort: a live `generate()` owns the client and is
|
|
239
|
+
* never stomped, and the same run is only rejoined once.
|
|
240
|
+
*/
|
|
241
|
+
private rejoinInFlight;
|
|
99
242
|
}
|