@tanstack/ai-client 0.18.5 → 0.18.6

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.
Files changed (48) hide show
  1. package/dist/esm/chat-client.d.ts +264 -0
  2. package/dist/esm/chat-client.js +1097 -0
  3. package/dist/esm/chat-client.js.map +1 -0
  4. package/dist/esm/client-persistor.d.ts +86 -0
  5. package/dist/esm/client-persistor.js +243 -0
  6. package/dist/esm/client-persistor.js.map +1 -0
  7. package/dist/esm/connection-adapters.d.ts +203 -0
  8. package/dist/esm/connection-adapters.js +611 -0
  9. package/dist/esm/connection-adapters.js.map +1 -0
  10. package/dist/esm/devtools-noop.d.ts +63 -0
  11. package/dist/esm/devtools-noop.js +141 -0
  12. package/dist/esm/devtools-noop.js.map +1 -0
  13. package/dist/esm/devtools.d.ts +276 -0
  14. package/dist/esm/devtools.js +1178 -0
  15. package/dist/esm/devtools.js.map +1 -0
  16. package/dist/esm/events.d.ts +129 -0
  17. package/dist/esm/events.js +227 -0
  18. package/dist/esm/events.js.map +1 -0
  19. package/dist/esm/generation-client.d.ts +99 -0
  20. package/dist/esm/generation-client.js +325 -0
  21. package/dist/esm/generation-client.js.map +1 -0
  22. package/dist/esm/generation-types.d.ts +242 -0
  23. package/dist/esm/generation-types.js +14 -0
  24. package/dist/esm/generation-types.js.map +1 -0
  25. package/dist/esm/index.d.ts +16 -0
  26. package/dist/esm/index.js +44 -0
  27. package/dist/esm/index.js.map +1 -0
  28. package/dist/esm/realtime-client.d.ts +98 -0
  29. package/dist/esm/realtime-client.js +403 -0
  30. package/dist/esm/realtime-client.js.map +1 -0
  31. package/dist/esm/realtime-types.d.ts +78 -0
  32. package/dist/esm/response-stream.d.ts +7 -0
  33. package/dist/esm/response-stream.js +32 -0
  34. package/dist/esm/response-stream.js.map +1 -0
  35. package/dist/esm/sse-parser.d.ts +8 -0
  36. package/dist/esm/sse-parser.js +52 -0
  37. package/dist/esm/sse-parser.js.map +1 -0
  38. package/dist/esm/sse-utils.d.ts +1 -0
  39. package/dist/esm/sse-utils.js +11 -0
  40. package/dist/esm/sse-utils.js.map +1 -0
  41. package/dist/esm/tool-types.d.ts +20 -0
  42. package/dist/esm/types.d.ts +419 -0
  43. package/dist/esm/types.js +11 -0
  44. package/dist/esm/types.js.map +1 -0
  45. package/dist/esm/video-generation-client.d.ts +110 -0
  46. package/dist/esm/video-generation-client.js +382 -0
  47. package/dist/esm/video-generation-client.js.map +1 -0
  48. package/package.json +3 -3
@@ -0,0 +1,264 @@
1
+ import { AnyClientTool, ModelMessage, StreamChunk } from '@tanstack/ai/client';
2
+ import { ConnectionAdapter } from './connection-adapters.js';
3
+ import { ChatClientOptions, ChatClientState, ChatFetcher, ConnectionStatus, MultimodalContent, UIMessage } from './types.js';
4
+ type ChatClientUpdateOptionsWithoutContext<TTools extends ReadonlyArray<AnyClientTool>> = {
5
+ connection?: ConnectionAdapter;
6
+ fetcher?: ChatFetcher;
7
+ /** @deprecated Use `forwardedProps` instead. */
8
+ body?: Record<string, any>;
9
+ forwardedProps?: Record<string, any>;
10
+ tools?: TTools;
11
+ onResponse?: (response?: Response) => void | Promise<void>;
12
+ onChunk?: (chunk: StreamChunk) => void;
13
+ onFinish?: (message: UIMessage) => void;
14
+ onError?: (error: Error) => void;
15
+ onSubscriptionChange?: (isSubscribed: boolean) => void;
16
+ onConnectionStatusChange?: (status: ConnectionStatus) => void;
17
+ onSessionGeneratingChange?: (isGenerating: boolean) => void;
18
+ onCustomEvent?: (eventType: string, data: unknown, context: {
19
+ toolCallId?: string;
20
+ }) => void;
21
+ };
22
+ type ClientToolResult = {
23
+ toolCallId: string;
24
+ tool: string;
25
+ output: any;
26
+ state?: 'output-available' | 'output-error';
27
+ errorText?: string;
28
+ };
29
+ export declare class ChatClient<TTools extends ReadonlyArray<AnyClientTool> = any, TContext = unknown> {
30
+ private readonly processor;
31
+ private connection;
32
+ private readonly uniqueId;
33
+ private readonly threadId;
34
+ private readonly persistor?;
35
+ private currentRunId;
36
+ private bodyOption;
37
+ private forwardedPropsOption;
38
+ private context;
39
+ private pendingMessageBody;
40
+ private isLoading;
41
+ private isSubscribed;
42
+ private error;
43
+ private status;
44
+ private connectionStatus;
45
+ private abortController;
46
+ private readonly clientToolsRef;
47
+ private readonly devtoolsBridge;
48
+ /**
49
+ * Alias for `this.events`. The bridge installs an
50
+ * emitter that auto-attaches run/thread context and auto-emits a
51
+ * snapshot after every event, so chat-client only ever calls
52
+ * `this.events.X(...)` exactly like it did before devtools landed.
53
+ */
54
+ private readonly events;
55
+ private currentStreamId;
56
+ private currentMessageId;
57
+ private readonly postStreamActions;
58
+ private readonly pendingToolExecutions;
59
+ private activeClientTools;
60
+ private activeContext;
61
+ private continuationPending;
62
+ private subscriptionAbortController;
63
+ private processingResolve;
64
+ private errorReportedGeneration;
65
+ private streamGeneration;
66
+ private continuationSkipped;
67
+ private draining;
68
+ private sessionGenerating;
69
+ private readonly activeRunIds;
70
+ private devtoolsMounted;
71
+ private readonly callbacksRef;
72
+ constructor(options: ChatClientOptions<TTools, TContext>);
73
+ mountDevtools(): void;
74
+ /**
75
+ * Drain a runId-less RUN_ERROR that belongs to a cleared run the client is
76
+ * still tracking. The persistor owns the cleared-run bookkeeping; the client
77
+ * owns the active-run / session / processing state.
78
+ */
79
+ private drainIgnoredRunlessChunk;
80
+ private updateRunLifecycle;
81
+ private generateUniqueId;
82
+ private setIsLoading;
83
+ private setStatus;
84
+ private setIsSubscribed;
85
+ private setConnectionStatus;
86
+ private setSessionGenerating;
87
+ private resetSessionGenerating;
88
+ private setError;
89
+ private buildDevtoolsBridgeOptions;
90
+ private getDevtoolsSnapshot;
91
+ private findMessageIdForToolCall;
92
+ private abortSubscriptionLoop;
93
+ private resolveProcessing;
94
+ private cancelInFlightStream;
95
+ private reportStreamError;
96
+ /**
97
+ * Start the background subscription loop.
98
+ */
99
+ private startSubscription;
100
+ /**
101
+ * Consume chunks from the connection subscription.
102
+ */
103
+ private consumeSubscription;
104
+ /**
105
+ * Ensure subscription loop is running, starting it if needed.
106
+ */
107
+ private ensureSubscription;
108
+ /**
109
+ * Create a promise that resolves when onStreamEnd fires.
110
+ * Used by streamResponse to await processing completion.
111
+ */
112
+ private waitForProcessing;
113
+ /**
114
+ * Send a message and stream the response.
115
+ * Supports both simple string content and multimodal content (images, audio, video, documents).
116
+ *
117
+ * @param content - The message content. Can be:
118
+ * - A simple string for text-only messages
119
+ * - A MultimodalContent object with content array and optional custom ID
120
+ * @param body - Optional body parameters to merge with the client's base body for this request.
121
+ * Uses shallow merge with per-message body taking priority.
122
+ *
123
+ * @example
124
+ * ```ts
125
+ * // Simple text message
126
+ * await client.sendMessage('Hello!')
127
+ *
128
+ * // Text message with custom body params
129
+ * await client.sendMessage('Hello!', { temperature: 0.7 })
130
+ *
131
+ * // Multimodal message with image
132
+ * await client.sendMessage({
133
+ * content: [
134
+ * { type: 'text', content: 'What is in this image?' },
135
+ * { type: 'image', source: { type: 'url', value: 'https://example.com/photo.jpg' } }
136
+ * ]
137
+ * })
138
+ *
139
+ * // Multimodal message with custom ID and body params
140
+ * await client.sendMessage(
141
+ * {
142
+ * content: [
143
+ * { type: 'text', content: 'Describe this audio' },
144
+ * { type: 'audio', source: { type: 'data', value: 'base64...' } }
145
+ * ],
146
+ * id: 'custom-message-id'
147
+ * },
148
+ * { model: 'gpt-4-audio' }
149
+ * )
150
+ * ```
151
+ */
152
+ sendMessage(content: string | MultimodalContent, body?: Record<string, any>): Promise<void>;
153
+ /**
154
+ * Normalize the message input to extract content and optional id.
155
+ * Trims string content automatically.
156
+ */
157
+ private normalizeMessageInput;
158
+ /**
159
+ * Append a message and stream the response
160
+ */
161
+ append(message: UIMessage | ModelMessage): Promise<void>;
162
+ /**
163
+ * Stream a response from the LLM.
164
+ * Returns true if the stream completed successfully, false on abort or error.
165
+ */
166
+ private streamResponse;
167
+ /**
168
+ * Start the client subscription loop.
169
+ * This controls the connection lifecycle independently from request lifecycle.
170
+ */
171
+ subscribe(options?: {
172
+ restart?: boolean;
173
+ }): void;
174
+ /**
175
+ * Unsubscribe and fully tear down live behavior.
176
+ * This aborts an in-flight request and the subscription loop.
177
+ */
178
+ unsubscribe(): void;
179
+ /**
180
+ * Reload the last assistant message
181
+ */
182
+ reload(): Promise<void>;
183
+ /**
184
+ * Stop the current stream
185
+ */
186
+ stop(): void;
187
+ /**
188
+ * Clear all messages
189
+ */
190
+ clear(): void;
191
+ /**
192
+ * Add the result of a client-side tool execution
193
+ */
194
+ addToolResult(result: ClientToolResult): Promise<void>;
195
+ private addToolResultForClientTool;
196
+ private validateClientToolOutput;
197
+ /**
198
+ * Respond to a tool approval request
199
+ */
200
+ addToolApprovalResponse(response: {
201
+ id: string;
202
+ approved: boolean;
203
+ }): Promise<void>;
204
+ /**
205
+ * Queue an action to be executed after the current stream ends
206
+ */
207
+ private queuePostStreamAction;
208
+ /**
209
+ * Drain and execute all queued post-stream actions
210
+ */
211
+ private drainPostStreamActions;
212
+ /**
213
+ * Check if we should continue the flow and do so if needed
214
+ */
215
+ private checkForContinuation;
216
+ /**
217
+ * Check if all tool calls are complete and we should auto-send.
218
+ * Requires that there is at least one tool call in the last assistant message;
219
+ * a text-only response has nothing to auto-send.
220
+ */
221
+ private shouldAutoSend;
222
+ /**
223
+ * Get current messages
224
+ */
225
+ getMessages(): Array<UIMessage<TTools>>;
226
+ /**
227
+ * Get loading state
228
+ */
229
+ getIsLoading(): boolean;
230
+ /**
231
+ * Get current status
232
+ */
233
+ getStatus(): ChatClientState;
234
+ /**
235
+ * Get whether the subscription loop is active
236
+ */
237
+ getIsSubscribed(): boolean;
238
+ /**
239
+ * Get current connection lifecycle status
240
+ */
241
+ getConnectionStatus(): ConnectionStatus;
242
+ /**
243
+ * Whether the shared session is actively generating.
244
+ * Derived from stream run events (RUN_STARTED / RUN_FINISHED / RUN_ERROR).
245
+ * Unlike `isLoading` (request-local), this reflects shared generation
246
+ * activity visible to all subscribers (e.g. across tabs/devices).
247
+ */
248
+ getSessionGenerating(): boolean;
249
+ /**
250
+ * Get current error
251
+ */
252
+ getError(): Error | undefined;
253
+ /**
254
+ * Manually set messages
255
+ */
256
+ setMessagesManually(messages: Array<UIMessage<TTools>>): void;
257
+ /**
258
+ * Update options refs (for use in React hooks to avoid recreating client)
259
+ */
260
+ updateOptions(options: ChatClientUpdateOptionsWithoutContext<TTools>): void;
261
+ updateOptions(options: ChatClientUpdateOptionsWithoutContext<TTools> & Pick<ChatClientOptions<TTools, TContext>, 'context'>): void;
262
+ dispose(): void;
263
+ }
264
+ export {};