ai 5.0.0-canary.21 → 5.0.0-canary.23

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/dist/index.d.mts CHANGED
@@ -1,375 +1,12 @@
1
- import { ToolResultContent, Schema, ToolCall, ToolResult, IdGenerator, FetchFunction } from '@ai-sdk/provider-utils';
1
+ import { ToolResultContent, Schema, ToolCall, ToolResult, FetchFunction, IdGenerator } from '@ai-sdk/provider-utils';
2
2
  export { IdGenerator, Schema, ToolCall, ToolResult, asSchema, createIdGenerator, generateId, jsonSchema } from '@ai-sdk/provider-utils';
3
- import { z } from 'zod';
4
- import { ServerResponse } from 'node:http';
5
3
  import { AISDKError, SharedV2ProviderMetadata, SharedV2ProviderOptions, EmbeddingModelV2, EmbeddingModelV2Embedding, ImageModelV2, ImageModelV2CallWarning, ImageModelV2ProviderMetadata, JSONValue as JSONValue$1, LanguageModelV2, LanguageModelV2FinishReason, LanguageModelV2CallWarning, LanguageModelV2Source, SpeechModelV1, SpeechModelV1CallWarning, TranscriptionModelV1, TranscriptionModelV1CallWarning, LanguageModelV2Usage, JSONObject, LanguageModelV2ToolCall, JSONSchema7, LanguageModelV2CallOptions, JSONParseError, TypeValidationError, LanguageModelV2Middleware, ProviderV2, NoSuchModelError } from '@ai-sdk/provider';
6
4
  export { AISDKError, APICallError, EmptyResponseBodyError, InvalidPromptError, InvalidResponseDataError, JSONParseError, JSONSchema7, LoadAPIKeyError, NoContentGeneratedError, NoSuchModelError, TypeValidationError, UnsupportedFunctionalityError } from '@ai-sdk/provider';
5
+ import { z } from 'zod';
6
+ import { ServerResponse } from 'node:http';
7
7
  import { AttributeValue, Tracer } from '@opentelemetry/api';
8
8
  import { ServerResponse as ServerResponse$1 } from 'http';
9
9
 
10
- declare const dataStreamPartSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObject<{
11
- type: z.ZodLiteral<"text">;
12
- value: z.ZodString;
13
- }, "strip", z.ZodTypeAny, {
14
- type: "text";
15
- value: string;
16
- }, {
17
- type: "text";
18
- value: string;
19
- }>, z.ZodObject<{
20
- type: z.ZodLiteral<"error">;
21
- value: z.ZodString;
22
- }, "strip", z.ZodTypeAny, {
23
- type: "error";
24
- value: string;
25
- }, {
26
- type: "error";
27
- value: string;
28
- }>, z.ZodObject<{
29
- type: z.ZodLiteral<"tool-call">;
30
- value: z.ZodObject<{
31
- toolCallId: z.ZodString;
32
- toolName: z.ZodString;
33
- args: z.ZodUnknown;
34
- }, "strip", z.ZodTypeAny, {
35
- toolCallId: string;
36
- toolName: string;
37
- args?: unknown;
38
- }, {
39
- toolCallId: string;
40
- toolName: string;
41
- args?: unknown;
42
- }>;
43
- }, "strip", z.ZodTypeAny, {
44
- type: "tool-call";
45
- value: {
46
- toolCallId: string;
47
- toolName: string;
48
- args?: unknown;
49
- };
50
- }, {
51
- type: "tool-call";
52
- value: {
53
- toolCallId: string;
54
- toolName: string;
55
- args?: unknown;
56
- };
57
- }>, z.ZodObject<{
58
- type: z.ZodLiteral<"tool-result">;
59
- value: z.ZodObject<{
60
- toolCallId: z.ZodString;
61
- result: z.ZodUnknown;
62
- providerMetadata: z.ZodOptional<z.ZodAny>;
63
- }, "strip", z.ZodTypeAny, {
64
- toolCallId: string;
65
- result?: unknown;
66
- providerMetadata?: any;
67
- }, {
68
- toolCallId: string;
69
- result?: unknown;
70
- providerMetadata?: any;
71
- }>;
72
- }, "strip", z.ZodTypeAny, {
73
- type: "tool-result";
74
- value: {
75
- toolCallId: string;
76
- result?: unknown;
77
- providerMetadata?: any;
78
- };
79
- }, {
80
- type: "tool-result";
81
- value: {
82
- toolCallId: string;
83
- result?: unknown;
84
- providerMetadata?: any;
85
- };
86
- }>, z.ZodObject<{
87
- type: z.ZodLiteral<"tool-call-streaming-start">;
88
- value: z.ZodObject<{
89
- toolCallId: z.ZodString;
90
- toolName: z.ZodString;
91
- }, "strip", z.ZodTypeAny, {
92
- toolCallId: string;
93
- toolName: string;
94
- }, {
95
- toolCallId: string;
96
- toolName: string;
97
- }>;
98
- }, "strip", z.ZodTypeAny, {
99
- type: "tool-call-streaming-start";
100
- value: {
101
- toolCallId: string;
102
- toolName: string;
103
- };
104
- }, {
105
- type: "tool-call-streaming-start";
106
- value: {
107
- toolCallId: string;
108
- toolName: string;
109
- };
110
- }>, z.ZodObject<{
111
- type: z.ZodLiteral<"tool-call-delta">;
112
- value: z.ZodObject<{
113
- toolCallId: z.ZodString;
114
- argsTextDelta: z.ZodString;
115
- }, "strip", z.ZodTypeAny, {
116
- toolCallId: string;
117
- argsTextDelta: string;
118
- }, {
119
- toolCallId: string;
120
- argsTextDelta: string;
121
- }>;
122
- }, "strip", z.ZodTypeAny, {
123
- type: "tool-call-delta";
124
- value: {
125
- toolCallId: string;
126
- argsTextDelta: string;
127
- };
128
- }, {
129
- type: "tool-call-delta";
130
- value: {
131
- toolCallId: string;
132
- argsTextDelta: string;
133
- };
134
- }>, z.ZodObject<{
135
- type: z.ZodLiteral<"reasoning">;
136
- value: z.ZodObject<{
137
- text: z.ZodString;
138
- providerMetadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
139
- }, "strip", z.ZodTypeAny, {
140
- text: string;
141
- providerMetadata?: Record<string, any> | undefined;
142
- }, {
143
- text: string;
144
- providerMetadata?: Record<string, any> | undefined;
145
- }>;
146
- }, "strip", z.ZodTypeAny, {
147
- type: "reasoning";
148
- value: {
149
- text: string;
150
- providerMetadata?: Record<string, any> | undefined;
151
- };
152
- }, {
153
- type: "reasoning";
154
- value: {
155
- text: string;
156
- providerMetadata?: Record<string, any> | undefined;
157
- };
158
- }>, z.ZodObject<{
159
- type: z.ZodLiteral<"source">;
160
- value: z.ZodObject<{
161
- type: z.ZodLiteral<"source">;
162
- sourceType: z.ZodLiteral<"url">;
163
- id: z.ZodString;
164
- url: z.ZodString;
165
- title: z.ZodOptional<z.ZodString>;
166
- providerMetadata: z.ZodOptional<z.ZodAny>;
167
- }, "strip", z.ZodTypeAny, {
168
- type: "source";
169
- sourceType: "url";
170
- url: string;
171
- id: string;
172
- providerMetadata?: any;
173
- title?: string | undefined;
174
- }, {
175
- type: "source";
176
- sourceType: "url";
177
- url: string;
178
- id: string;
179
- providerMetadata?: any;
180
- title?: string | undefined;
181
- }>;
182
- }, "strip", z.ZodTypeAny, {
183
- type: "source";
184
- value: {
185
- type: "source";
186
- sourceType: "url";
187
- url: string;
188
- id: string;
189
- providerMetadata?: any;
190
- title?: string | undefined;
191
- };
192
- }, {
193
- type: "source";
194
- value: {
195
- type: "source";
196
- sourceType: "url";
197
- url: string;
198
- id: string;
199
- providerMetadata?: any;
200
- title?: string | undefined;
201
- };
202
- }>, z.ZodObject<{
203
- type: z.ZodLiteral<"file">;
204
- value: z.ZodObject<{
205
- url: z.ZodString;
206
- mediaType: z.ZodString;
207
- }, "strip", z.ZodTypeAny, {
208
- url: string;
209
- mediaType: string;
210
- }, {
211
- url: string;
212
- mediaType: string;
213
- }>;
214
- }, "strip", z.ZodTypeAny, {
215
- type: "file";
216
- value: {
217
- url: string;
218
- mediaType: string;
219
- };
220
- }, {
221
- type: "file";
222
- value: {
223
- url: string;
224
- mediaType: string;
225
- };
226
- }>, z.ZodObject<{
227
- type: z.ZodLiteral<"metadata">;
228
- value: z.ZodObject<{
229
- metadata: z.ZodUnknown;
230
- }, "strip", z.ZodTypeAny, {
231
- metadata?: unknown;
232
- }, {
233
- metadata?: unknown;
234
- }>;
235
- }, "strip", z.ZodTypeAny, {
236
- type: "metadata";
237
- value: {
238
- metadata?: unknown;
239
- };
240
- }, {
241
- type: "metadata";
242
- value: {
243
- metadata?: unknown;
244
- };
245
- }>, z.ZodObject<{
246
- type: z.ZodLiteral<"start-step">;
247
- value: z.ZodObject<{
248
- metadata: z.ZodUnknown;
249
- }, "strip", z.ZodTypeAny, {
250
- metadata?: unknown;
251
- }, {
252
- metadata?: unknown;
253
- }>;
254
- }, "strip", z.ZodTypeAny, {
255
- type: "start-step";
256
- value: {
257
- metadata?: unknown;
258
- };
259
- }, {
260
- type: "start-step";
261
- value: {
262
- metadata?: unknown;
263
- };
264
- }>, z.ZodObject<{
265
- type: z.ZodLiteral<"finish-step">;
266
- value: z.ZodObject<{
267
- metadata: z.ZodUnknown;
268
- }, "strip", z.ZodTypeAny, {
269
- metadata?: unknown;
270
- }, {
271
- metadata?: unknown;
272
- }>;
273
- }, "strip", z.ZodTypeAny, {
274
- type: "finish-step";
275
- value: {
276
- metadata?: unknown;
277
- };
278
- }, {
279
- type: "finish-step";
280
- value: {
281
- metadata?: unknown;
282
- };
283
- }>, z.ZodObject<{
284
- type: z.ZodLiteral<"start">;
285
- value: z.ZodObject<{
286
- messageId: z.ZodOptional<z.ZodString>;
287
- metadata: z.ZodUnknown;
288
- }, "strip", z.ZodTypeAny, {
289
- metadata?: unknown;
290
- messageId?: string | undefined;
291
- }, {
292
- metadata?: unknown;
293
- messageId?: string | undefined;
294
- }>;
295
- }, "strip", z.ZodTypeAny, {
296
- type: "start";
297
- value: {
298
- metadata?: unknown;
299
- messageId?: string | undefined;
300
- };
301
- }, {
302
- type: "start";
303
- value: {
304
- metadata?: unknown;
305
- messageId?: string | undefined;
306
- };
307
- }>, z.ZodObject<{
308
- type: z.ZodLiteral<"finish">;
309
- value: z.ZodObject<{
310
- metadata: z.ZodUnknown;
311
- }, "strip", z.ZodTypeAny, {
312
- metadata?: unknown;
313
- }, {
314
- metadata?: unknown;
315
- }>;
316
- }, "strip", z.ZodTypeAny, {
317
- type: "finish";
318
- value: {
319
- metadata?: unknown;
320
- };
321
- }, {
322
- type: "finish";
323
- value: {
324
- metadata?: unknown;
325
- };
326
- }>, z.ZodObject<{
327
- type: z.ZodLiteral<"reasoning-part-finish">;
328
- value: z.ZodNull;
329
- }, "strip", z.ZodTypeAny, {
330
- type: "reasoning-part-finish";
331
- value: null;
332
- }, {
333
- type: "reasoning-part-finish";
334
- value: null;
335
- }>]>;
336
- type DataStreamPart = z.infer<typeof dataStreamPartSchema>;
337
-
338
- interface DataStreamWriter {
339
- /**
340
- * Appends a data stream part to the stream.
341
- */
342
- write(part: DataStreamPart): void;
343
- /**
344
- * Merges the contents of another stream to this stream.
345
- */
346
- merge(stream: ReadableStream<DataStreamPart>): void;
347
- /**
348
- * Error handler that is used by the data stream writer.
349
- * This is intended for forwarding when merging streams
350
- * to prevent duplicated error masking.
351
- */
352
- onError: ((error: unknown) => string) | undefined;
353
- }
354
-
355
- declare function createDataStream({ execute, onError, }: {
356
- execute: (writer: DataStreamWriter) => Promise<void> | void;
357
- onError?: (error: unknown) => string;
358
- }): ReadableStream<DataStreamPart>;
359
-
360
- declare function createDataStreamResponse({ status, statusText, headers, dataStream, }: ResponseInit & {
361
- dataStream: ReadableStream<DataStreamPart>;
362
- }): Response;
363
-
364
- declare function pipeDataStreamToResponse({ response, status, statusText, headers, dataStream, }: {
365
- response: ServerResponse;
366
- dataStream: ReadableStream<DataStreamPart>;
367
- } & ResponseInit): void;
368
-
369
- declare class JsonToSseTransformStream extends TransformStream<unknown, string> {
370
- constructor();
371
- }
372
-
373
10
  declare const symbol$f: unique symbol;
374
11
  declare class InvalidArgumentError extends AISDKError {
375
12
  private readonly [symbol$f];
@@ -1037,29 +674,29 @@ declare const JSONRPCErrorSchema: z.ZodObject<{
1037
674
  message: z.ZodString;
1038
675
  data: z.ZodOptional<z.ZodUnknown>;
1039
676
  }, "strip", z.ZodTypeAny, {
1040
- code: number;
1041
677
  message: string;
678
+ code: number;
1042
679
  data?: unknown;
1043
680
  }, {
1044
- code: number;
1045
681
  message: string;
682
+ code: number;
1046
683
  data?: unknown;
1047
684
  }>;
1048
685
  }, "strict", z.ZodTypeAny, {
1049
- id: string | number;
1050
686
  error: {
1051
- code: number;
1052
687
  message: string;
688
+ code: number;
1053
689
  data?: unknown;
1054
690
  };
691
+ id: string | number;
1055
692
  jsonrpc: "2.0";
1056
693
  }, {
1057
- id: string | number;
1058
694
  error: {
1059
- code: number;
1060
695
  message: string;
696
+ code: number;
1061
697
  data?: unknown;
1062
698
  };
699
+ id: string | number;
1063
700
  jsonrpc: "2.0";
1064
701
  }>;
1065
702
  type JSONRPCError = z.infer<typeof JSONRPCErrorSchema>;
@@ -1171,29 +808,29 @@ declare const JSONRPCMessageSchema: z.ZodUnion<[z.ZodObject<z.objectUtil.extendS
1171
808
  message: z.ZodString;
1172
809
  data: z.ZodOptional<z.ZodUnknown>;
1173
810
  }, "strip", z.ZodTypeAny, {
1174
- code: number;
1175
811
  message: string;
812
+ code: number;
1176
813
  data?: unknown;
1177
814
  }, {
1178
- code: number;
1179
815
  message: string;
816
+ code: number;
1180
817
  data?: unknown;
1181
818
  }>;
1182
819
  }, "strict", z.ZodTypeAny, {
1183
- id: string | number;
1184
820
  error: {
1185
- code: number;
1186
821
  message: string;
822
+ code: number;
1187
823
  data?: unknown;
1188
824
  };
825
+ id: string | number;
1189
826
  jsonrpc: "2.0";
1190
827
  }, {
1191
- id: string | number;
1192
828
  error: {
1193
- code: number;
1194
829
  message: string;
830
+ code: number;
1195
831
  data?: unknown;
1196
832
  };
833
+ id: string | number;
1197
834
  jsonrpc: "2.0";
1198
835
  }>]>;
1199
836
  type JSONRPCMessage = z.infer<typeof JSONRPCMessageSchema>;
@@ -2564,91 +2201,733 @@ type SourceUIPart = {
2564
2201
  type FileUIPart = {
2565
2202
  type: 'file';
2566
2203
  /**
2567
- * IANA media type of the file.
2568
- *
2569
- * @see https://www.iana.org/assignments/media-types/media-types.xhtml
2204
+ * IANA media type of the file.
2205
+ *
2206
+ * @see https://www.iana.org/assignments/media-types/media-types.xhtml
2207
+ */
2208
+ mediaType: string;
2209
+ /**
2210
+ * Optional filename of the file.
2211
+ */
2212
+ filename?: string;
2213
+ /**
2214
+ * The URL of the file.
2215
+ * It can either be a URL to a hosted file or a [Data URL](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URLs).
2216
+ */
2217
+ url: string;
2218
+ };
2219
+ /**
2220
+ * A step boundary part of a message.
2221
+ */
2222
+ type StepStartUIPart = {
2223
+ type: 'step-start';
2224
+ };
2225
+ type CreateUIMessage<METADATA = unknown> = Omit<UIMessage<METADATA>, 'id'> & {
2226
+ id?: UIMessage<METADATA>['id'];
2227
+ };
2228
+
2229
+ declare const symbol$3: unique symbol;
2230
+ declare class MessageConversionError extends AISDKError {
2231
+ private readonly [symbol$3];
2232
+ readonly originalMessage: Omit<UIMessage, 'id'>;
2233
+ constructor({ originalMessage, message, }: {
2234
+ originalMessage: Omit<UIMessage, 'id'>;
2235
+ message: string;
2236
+ });
2237
+ static isInstance(error: unknown): error is MessageConversionError;
2238
+ }
2239
+
2240
+ declare const symbol$2: unique symbol;
2241
+ declare class DownloadError extends AISDKError {
2242
+ private readonly [symbol$2];
2243
+ readonly url: string;
2244
+ readonly statusCode?: number;
2245
+ readonly statusText?: string;
2246
+ constructor({ url, statusCode, statusText, cause, message, }: {
2247
+ url: string;
2248
+ statusCode?: number;
2249
+ statusText?: string;
2250
+ message?: string;
2251
+ cause?: unknown;
2252
+ });
2253
+ static isInstance(error: unknown): error is DownloadError;
2254
+ }
2255
+
2256
+ declare const symbol$1: unique symbol;
2257
+ type RetryErrorReason = 'maxRetriesExceeded' | 'errorNotRetryable' | 'abort';
2258
+ declare class RetryError extends AISDKError {
2259
+ private readonly [symbol$1];
2260
+ readonly reason: RetryErrorReason;
2261
+ readonly lastError: unknown;
2262
+ readonly errors: Array<unknown>;
2263
+ constructor({ message, reason, errors, }: {
2264
+ message: string;
2265
+ reason: RetryErrorReason;
2266
+ errors: Array<unknown>;
2267
+ });
2268
+ static isInstance(error: unknown): error is RetryError;
2269
+ }
2270
+
2271
+ declare function createTextStreamResponse({ status, statusText, headers, textStream, }: ResponseInit & {
2272
+ textStream: ReadableStream<string>;
2273
+ }): Response;
2274
+
2275
+ declare function pipeTextStreamToResponse({ response, status, statusText, headers, textStream, }: {
2276
+ response: ServerResponse;
2277
+ textStream: ReadableStream<string>;
2278
+ } & ResponseInit): void;
2279
+
2280
+ /**
2281
+ * Appends a client message to the messages array.
2282
+ * If the last message in the array has the same id as the new message, it will be replaced.
2283
+ * Otherwise, the new message will be appended.
2284
+ */
2285
+ declare function appendClientMessage({ messages, message, }: {
2286
+ messages: UIMessage[];
2287
+ message: UIMessage;
2288
+ }): UIMessage<unknown>[];
2289
+
2290
+ declare const uiMessageStreamPartSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObject<{
2291
+ type: z.ZodLiteral<"text">;
2292
+ value: z.ZodString;
2293
+ }, "strip", z.ZodTypeAny, {
2294
+ value: string;
2295
+ type: "text";
2296
+ }, {
2297
+ value: string;
2298
+ type: "text";
2299
+ }>, z.ZodObject<{
2300
+ type: z.ZodLiteral<"error">;
2301
+ value: z.ZodString;
2302
+ }, "strip", z.ZodTypeAny, {
2303
+ value: string;
2304
+ type: "error";
2305
+ }, {
2306
+ value: string;
2307
+ type: "error";
2308
+ }>, z.ZodObject<{
2309
+ type: z.ZodLiteral<"tool-call">;
2310
+ value: z.ZodObject<{
2311
+ toolCallId: z.ZodString;
2312
+ toolName: z.ZodString;
2313
+ args: z.ZodUnknown;
2314
+ }, "strip", z.ZodTypeAny, {
2315
+ toolName: string;
2316
+ toolCallId: string;
2317
+ args?: unknown;
2318
+ }, {
2319
+ toolName: string;
2320
+ toolCallId: string;
2321
+ args?: unknown;
2322
+ }>;
2323
+ }, "strip", z.ZodTypeAny, {
2324
+ value: {
2325
+ toolName: string;
2326
+ toolCallId: string;
2327
+ args?: unknown;
2328
+ };
2329
+ type: "tool-call";
2330
+ }, {
2331
+ value: {
2332
+ toolName: string;
2333
+ toolCallId: string;
2334
+ args?: unknown;
2335
+ };
2336
+ type: "tool-call";
2337
+ }>, z.ZodObject<{
2338
+ type: z.ZodLiteral<"tool-result">;
2339
+ value: z.ZodObject<{
2340
+ toolCallId: z.ZodString;
2341
+ result: z.ZodUnknown;
2342
+ providerMetadata: z.ZodOptional<z.ZodAny>;
2343
+ }, "strip", z.ZodTypeAny, {
2344
+ toolCallId: string;
2345
+ result?: unknown;
2346
+ providerMetadata?: any;
2347
+ }, {
2348
+ toolCallId: string;
2349
+ result?: unknown;
2350
+ providerMetadata?: any;
2351
+ }>;
2352
+ }, "strip", z.ZodTypeAny, {
2353
+ value: {
2354
+ toolCallId: string;
2355
+ result?: unknown;
2356
+ providerMetadata?: any;
2357
+ };
2358
+ type: "tool-result";
2359
+ }, {
2360
+ value: {
2361
+ toolCallId: string;
2362
+ result?: unknown;
2363
+ providerMetadata?: any;
2364
+ };
2365
+ type: "tool-result";
2366
+ }>, z.ZodObject<{
2367
+ type: z.ZodLiteral<"tool-call-streaming-start">;
2368
+ value: z.ZodObject<{
2369
+ toolCallId: z.ZodString;
2370
+ toolName: z.ZodString;
2371
+ }, "strip", z.ZodTypeAny, {
2372
+ toolName: string;
2373
+ toolCallId: string;
2374
+ }, {
2375
+ toolName: string;
2376
+ toolCallId: string;
2377
+ }>;
2378
+ }, "strip", z.ZodTypeAny, {
2379
+ value: {
2380
+ toolName: string;
2381
+ toolCallId: string;
2382
+ };
2383
+ type: "tool-call-streaming-start";
2384
+ }, {
2385
+ value: {
2386
+ toolName: string;
2387
+ toolCallId: string;
2388
+ };
2389
+ type: "tool-call-streaming-start";
2390
+ }>, z.ZodObject<{
2391
+ type: z.ZodLiteral<"tool-call-delta">;
2392
+ value: z.ZodObject<{
2393
+ toolCallId: z.ZodString;
2394
+ argsTextDelta: z.ZodString;
2395
+ }, "strip", z.ZodTypeAny, {
2396
+ toolCallId: string;
2397
+ argsTextDelta: string;
2398
+ }, {
2399
+ toolCallId: string;
2400
+ argsTextDelta: string;
2401
+ }>;
2402
+ }, "strip", z.ZodTypeAny, {
2403
+ value: {
2404
+ toolCallId: string;
2405
+ argsTextDelta: string;
2406
+ };
2407
+ type: "tool-call-delta";
2408
+ }, {
2409
+ value: {
2410
+ toolCallId: string;
2411
+ argsTextDelta: string;
2412
+ };
2413
+ type: "tool-call-delta";
2414
+ }>, z.ZodObject<{
2415
+ type: z.ZodLiteral<"reasoning">;
2416
+ value: z.ZodObject<{
2417
+ text: z.ZodString;
2418
+ providerMetadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
2419
+ }, "strip", z.ZodTypeAny, {
2420
+ text: string;
2421
+ providerMetadata?: Record<string, any> | undefined;
2422
+ }, {
2423
+ text: string;
2424
+ providerMetadata?: Record<string, any> | undefined;
2425
+ }>;
2426
+ }, "strip", z.ZodTypeAny, {
2427
+ value: {
2428
+ text: string;
2429
+ providerMetadata?: Record<string, any> | undefined;
2430
+ };
2431
+ type: "reasoning";
2432
+ }, {
2433
+ value: {
2434
+ text: string;
2435
+ providerMetadata?: Record<string, any> | undefined;
2436
+ };
2437
+ type: "reasoning";
2438
+ }>, z.ZodObject<{
2439
+ type: z.ZodLiteral<"source">;
2440
+ value: z.ZodObject<{
2441
+ type: z.ZodLiteral<"source">;
2442
+ sourceType: z.ZodLiteral<"url">;
2443
+ id: z.ZodString;
2444
+ url: z.ZodString;
2445
+ title: z.ZodOptional<z.ZodString>;
2446
+ providerMetadata: z.ZodOptional<z.ZodAny>;
2447
+ }, "strip", z.ZodTypeAny, {
2448
+ type: "source";
2449
+ id: string;
2450
+ url: string;
2451
+ sourceType: "url";
2452
+ providerMetadata?: any;
2453
+ title?: string | undefined;
2454
+ }, {
2455
+ type: "source";
2456
+ id: string;
2457
+ url: string;
2458
+ sourceType: "url";
2459
+ providerMetadata?: any;
2460
+ title?: string | undefined;
2461
+ }>;
2462
+ }, "strip", z.ZodTypeAny, {
2463
+ value: {
2464
+ type: "source";
2465
+ id: string;
2466
+ url: string;
2467
+ sourceType: "url";
2468
+ providerMetadata?: any;
2469
+ title?: string | undefined;
2470
+ };
2471
+ type: "source";
2472
+ }, {
2473
+ value: {
2474
+ type: "source";
2475
+ id: string;
2476
+ url: string;
2477
+ sourceType: "url";
2478
+ providerMetadata?: any;
2479
+ title?: string | undefined;
2480
+ };
2481
+ type: "source";
2482
+ }>, z.ZodObject<{
2483
+ type: z.ZodLiteral<"file">;
2484
+ value: z.ZodObject<{
2485
+ url: z.ZodString;
2486
+ mediaType: z.ZodString;
2487
+ }, "strip", z.ZodTypeAny, {
2488
+ mediaType: string;
2489
+ url: string;
2490
+ }, {
2491
+ mediaType: string;
2492
+ url: string;
2493
+ }>;
2494
+ }, "strip", z.ZodTypeAny, {
2495
+ value: {
2496
+ mediaType: string;
2497
+ url: string;
2498
+ };
2499
+ type: "file";
2500
+ }, {
2501
+ value: {
2502
+ mediaType: string;
2503
+ url: string;
2504
+ };
2505
+ type: "file";
2506
+ }>, z.ZodObject<{
2507
+ type: z.ZodLiteral<"metadata">;
2508
+ value: z.ZodObject<{
2509
+ metadata: z.ZodUnknown;
2510
+ }, "strip", z.ZodTypeAny, {
2511
+ metadata?: unknown;
2512
+ }, {
2513
+ metadata?: unknown;
2514
+ }>;
2515
+ }, "strip", z.ZodTypeAny, {
2516
+ value: {
2517
+ metadata?: unknown;
2518
+ };
2519
+ type: "metadata";
2520
+ }, {
2521
+ value: {
2522
+ metadata?: unknown;
2523
+ };
2524
+ type: "metadata";
2525
+ }>, z.ZodObject<{
2526
+ type: z.ZodLiteral<"start-step">;
2527
+ value: z.ZodObject<{
2528
+ metadata: z.ZodUnknown;
2529
+ }, "strip", z.ZodTypeAny, {
2530
+ metadata?: unknown;
2531
+ }, {
2532
+ metadata?: unknown;
2533
+ }>;
2534
+ }, "strip", z.ZodTypeAny, {
2535
+ value: {
2536
+ metadata?: unknown;
2537
+ };
2538
+ type: "start-step";
2539
+ }, {
2540
+ value: {
2541
+ metadata?: unknown;
2542
+ };
2543
+ type: "start-step";
2544
+ }>, z.ZodObject<{
2545
+ type: z.ZodLiteral<"finish-step">;
2546
+ value: z.ZodObject<{
2547
+ metadata: z.ZodUnknown;
2548
+ }, "strip", z.ZodTypeAny, {
2549
+ metadata?: unknown;
2550
+ }, {
2551
+ metadata?: unknown;
2552
+ }>;
2553
+ }, "strip", z.ZodTypeAny, {
2554
+ value: {
2555
+ metadata?: unknown;
2556
+ };
2557
+ type: "finish-step";
2558
+ }, {
2559
+ value: {
2560
+ metadata?: unknown;
2561
+ };
2562
+ type: "finish-step";
2563
+ }>, z.ZodObject<{
2564
+ type: z.ZodLiteral<"start">;
2565
+ value: z.ZodObject<{
2566
+ messageId: z.ZodOptional<z.ZodString>;
2567
+ metadata: z.ZodUnknown;
2568
+ }, "strip", z.ZodTypeAny, {
2569
+ metadata?: unknown;
2570
+ messageId?: string | undefined;
2571
+ }, {
2572
+ metadata?: unknown;
2573
+ messageId?: string | undefined;
2574
+ }>;
2575
+ }, "strip", z.ZodTypeAny, {
2576
+ value: {
2577
+ metadata?: unknown;
2578
+ messageId?: string | undefined;
2579
+ };
2580
+ type: "start";
2581
+ }, {
2582
+ value: {
2583
+ metadata?: unknown;
2584
+ messageId?: string | undefined;
2585
+ };
2586
+ type: "start";
2587
+ }>, z.ZodObject<{
2588
+ type: z.ZodLiteral<"finish">;
2589
+ value: z.ZodObject<{
2590
+ metadata: z.ZodUnknown;
2591
+ }, "strip", z.ZodTypeAny, {
2592
+ metadata?: unknown;
2593
+ }, {
2594
+ metadata?: unknown;
2595
+ }>;
2596
+ }, "strip", z.ZodTypeAny, {
2597
+ value: {
2598
+ metadata?: unknown;
2599
+ };
2600
+ type: "finish";
2601
+ }, {
2602
+ value: {
2603
+ metadata?: unknown;
2604
+ };
2605
+ type: "finish";
2606
+ }>, z.ZodObject<{
2607
+ type: z.ZodLiteral<"reasoning-part-finish">;
2608
+ value: z.ZodNull;
2609
+ }, "strip", z.ZodTypeAny, {
2610
+ value: null;
2611
+ type: "reasoning-part-finish";
2612
+ }, {
2613
+ value: null;
2614
+ type: "reasoning-part-finish";
2615
+ }>]>;
2616
+ type UIMessageStreamPart = z.infer<typeof uiMessageStreamPartSchema>;
2617
+
2618
+ type Job = () => Promise<void>;
2619
+
2620
+ declare class SerialJobExecutor {
2621
+ private queue;
2622
+ private isProcessing;
2623
+ private processQueue;
2624
+ run(job: Job): Promise<void>;
2625
+ }
2626
+
2627
+ interface UIMessageStreamWriter {
2628
+ /**
2629
+ * Appends a data stream part to the stream.
2570
2630
  */
2571
- mediaType: string;
2631
+ write(part: UIMessageStreamPart): void;
2572
2632
  /**
2573
- * Optional filename of the file.
2633
+ * Merges the contents of another stream to this stream.
2574
2634
  */
2575
- filename?: string;
2635
+ merge(stream: ReadableStream<UIMessageStreamPart>): void;
2576
2636
  /**
2577
- * The URL of the file.
2578
- * It can either be a URL to a hosted file or a [Data URL](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URLs).
2637
+ * Error handler that is used by the data stream writer.
2638
+ * This is intended for forwarding when merging streams
2639
+ * to prevent duplicated error masking.
2579
2640
  */
2580
- url: string;
2581
- };
2582
- /**
2583
- * A step boundary part of a message.
2584
- */
2585
- type StepStartUIPart = {
2586
- type: 'step-start';
2587
- };
2588
- type CreateUIMessage<METADATA = unknown> = Omit<UIMessage<METADATA>, 'id'> & {
2589
- id?: UIMessage<METADATA>['id'];
2590
- };
2591
-
2592
- declare const symbol$3: unique symbol;
2593
- declare class MessageConversionError extends AISDKError {
2594
- private readonly [symbol$3];
2595
- readonly originalMessage: Omit<UIMessage, 'id'>;
2596
- constructor({ originalMessage, message, }: {
2597
- originalMessage: Omit<UIMessage, 'id'>;
2598
- message: string;
2599
- });
2600
- static isInstance(error: unknown): error is MessageConversionError;
2601
- }
2602
-
2603
- declare const symbol$2: unique symbol;
2604
- declare class DownloadError extends AISDKError {
2605
- private readonly [symbol$2];
2606
- readonly url: string;
2607
- readonly statusCode?: number;
2608
- readonly statusText?: string;
2609
- constructor({ url, statusCode, statusText, cause, message, }: {
2610
- url: string;
2611
- statusCode?: number;
2612
- statusText?: string;
2613
- message?: string;
2614
- cause?: unknown;
2615
- });
2616
- static isInstance(error: unknown): error is DownloadError;
2641
+ onError: ((error: unknown) => string) | undefined;
2617
2642
  }
2618
2643
 
2619
- declare const symbol$1: unique symbol;
2620
- type RetryErrorReason = 'maxRetriesExceeded' | 'errorNotRetryable' | 'abort';
2621
- declare class RetryError extends AISDKError {
2622
- private readonly [symbol$1];
2623
- readonly reason: RetryErrorReason;
2624
- readonly lastError: unknown;
2625
- readonly errors: Array<unknown>;
2626
- constructor({ message, reason, errors, }: {
2627
- message: string;
2628
- reason: RetryErrorReason;
2629
- errors: Array<unknown>;
2630
- });
2631
- static isInstance(error: unknown): error is RetryError;
2632
- }
2644
+ declare function createUIMessageStream({ execute, onError, }: {
2645
+ execute: (writer: UIMessageStreamWriter) => Promise<void> | void;
2646
+ onError?: (error: unknown) => string;
2647
+ }): ReadableStream<UIMessageStreamPart>;
2633
2648
 
2634
- declare function createTextStreamResponse({ status, statusText, headers, textStream, }: ResponseInit & {
2635
- textStream: ReadableStream<string>;
2649
+ declare function createUIMessageStreamResponse({ status, statusText, headers, stream, }: ResponseInit & {
2650
+ stream: ReadableStream<UIMessageStreamPart>;
2636
2651
  }): Response;
2637
2652
 
2638
- declare function pipeTextStreamToResponse({ response, status, statusText, headers, textStream, }: {
2653
+ declare function pipeUIMessageStreamToResponse({ response, status, statusText, headers, stream, }: {
2639
2654
  response: ServerResponse;
2640
- textStream: ReadableStream<string>;
2655
+ stream: ReadableStream<UIMessageStreamPart>;
2641
2656
  } & ResponseInit): void;
2642
2657
 
2643
- /**
2644
- * Appends a client message to the messages array.
2645
- * If the last message in the array has the same id as the new message, it will be replaced.
2646
- * Otherwise, the new message will be appended.
2647
- */
2648
- declare function appendClientMessage({ messages, message, }: {
2649
- messages: UIMessage[];
2650
- message: UIMessage;
2651
- }): UIMessage<unknown>[];
2658
+ declare class JsonToSseTransformStream extends TransformStream<unknown, string> {
2659
+ constructor();
2660
+ }
2661
+
2662
+ interface ChatTransport<MESSAGE_METADATA> {
2663
+ submitMessages: (options: {
2664
+ chatId: string;
2665
+ messages: UIMessage<MESSAGE_METADATA>[];
2666
+ abortController: AbortController;
2667
+ body?: object;
2668
+ headers?: Record<string, string> | Headers;
2669
+ requestType: 'generate' | 'resume';
2670
+ }) => Promise<ReadableStream<UIMessageStreamPart>>;
2671
+ }
2672
+ declare class DefaultChatTransport<MESSAGE_METADATA> implements ChatTransport<MESSAGE_METADATA> {
2673
+ private api;
2674
+ private credentials?;
2675
+ private headers?;
2676
+ private body?;
2677
+ private streamProtocol?;
2678
+ private fetch?;
2679
+ private prepareRequestBody?;
2680
+ constructor({ api, credentials, headers, body, streamProtocol, fetch, prepareRequestBody, }: {
2681
+ api: string;
2682
+ /**
2683
+ * The credentials mode to be used for the fetch request.
2684
+ * Possible values are: 'omit', 'same-origin', 'include'.
2685
+ * Defaults to 'same-origin'.
2686
+ */
2687
+ credentials?: RequestCredentials;
2688
+ /**
2689
+ * HTTP headers to be sent with the API request.
2690
+ */
2691
+ headers?: Record<string, string> | Headers;
2692
+ /**
2693
+ * Extra body object to be sent with the API request.
2694
+ * @example
2695
+ * Send a `sessionId` to the API along with the messages.
2696
+ * ```js
2697
+ * useChat({
2698
+ * body: {
2699
+ * sessionId: '123',
2700
+ * }
2701
+ * })
2702
+ * ```
2703
+ */
2704
+ body?: object;
2705
+ /**
2706
+ Streaming protocol that is used. Defaults to `ui-message`.
2707
+ */
2708
+ streamProtocol?: 'ui-message' | 'text';
2709
+ /**
2710
+ Custom fetch implementation. You can use it as a middleware to intercept requests,
2711
+ or to provide a custom fetch implementation for e.g. testing.
2712
+ */
2713
+ fetch?: FetchFunction;
2714
+ /**
2715
+ * When a function is provided, it will be used
2716
+ * to prepare the request body for the chat API. This can be useful for
2717
+ * customizing the request body based on the messages and data in the chat.
2718
+ *
2719
+ * @param id The id of the chat.
2720
+ * @param messages The current messages in the chat.
2721
+ * @param requestBody The request body object passed in the chat request.
2722
+ */
2723
+ prepareRequestBody?: (options: {
2724
+ id: string;
2725
+ messages: UIMessage<MESSAGE_METADATA>[];
2726
+ requestBody?: object;
2727
+ }) => unknown;
2728
+ });
2729
+ submitMessages({ chatId, messages, abortController, body, headers, requestType, }: Parameters<ChatTransport<MESSAGE_METADATA>['submitMessages']>[0]): Promise<ReadableStream<{
2730
+ value: string;
2731
+ type: "text";
2732
+ } | {
2733
+ value: string;
2734
+ type: "error";
2735
+ } | {
2736
+ value: {
2737
+ toolName: string;
2738
+ toolCallId: string;
2739
+ args?: unknown;
2740
+ };
2741
+ type: "tool-call";
2742
+ } | {
2743
+ value: {
2744
+ toolCallId: string;
2745
+ result?: unknown;
2746
+ providerMetadata?: any;
2747
+ };
2748
+ type: "tool-result";
2749
+ } | {
2750
+ value: {
2751
+ toolName: string;
2752
+ toolCallId: string;
2753
+ };
2754
+ type: "tool-call-streaming-start";
2755
+ } | {
2756
+ value: {
2757
+ toolCallId: string;
2758
+ argsTextDelta: string;
2759
+ };
2760
+ type: "tool-call-delta";
2761
+ } | {
2762
+ value: {
2763
+ text: string;
2764
+ providerMetadata?: Record<string, any> | undefined;
2765
+ };
2766
+ type: "reasoning";
2767
+ } | {
2768
+ value: {
2769
+ type: "source";
2770
+ id: string;
2771
+ url: string;
2772
+ sourceType: "url";
2773
+ providerMetadata?: any;
2774
+ title?: string | undefined;
2775
+ };
2776
+ type: "source";
2777
+ } | {
2778
+ value: {
2779
+ mediaType: string;
2780
+ url: string;
2781
+ };
2782
+ type: "file";
2783
+ } | {
2784
+ value: {
2785
+ metadata?: unknown;
2786
+ };
2787
+ type: "metadata";
2788
+ } | {
2789
+ value: {
2790
+ metadata?: unknown;
2791
+ };
2792
+ type: "start-step";
2793
+ } | {
2794
+ value: {
2795
+ metadata?: unknown;
2796
+ };
2797
+ type: "finish-step";
2798
+ } | {
2799
+ value: {
2800
+ metadata?: unknown;
2801
+ messageId?: string | undefined;
2802
+ };
2803
+ type: "start";
2804
+ } | {
2805
+ value: {
2806
+ metadata?: unknown;
2807
+ };
2808
+ type: "finish";
2809
+ } | {
2810
+ value: null;
2811
+ type: "reasoning-part-finish";
2812
+ }>>;
2813
+ }
2814
+
2815
+ type StreamingUIMessageState<MESSAGE_METADATA = unknown> = {
2816
+ message: UIMessage<MESSAGE_METADATA>;
2817
+ activeTextPart: TextUIPart | undefined;
2818
+ activeReasoningPart: ReasoningUIPart | undefined;
2819
+ partialToolCalls: Record<string, {
2820
+ text: string;
2821
+ step: number;
2822
+ index: number;
2823
+ toolName: string;
2824
+ }>;
2825
+ step: number;
2826
+ };
2827
+
2828
+ interface ChatStoreSubscriber {
2829
+ onChatChanged: (event: ChatStoreEvent) => void;
2830
+ }
2831
+ interface ChatStoreEvent {
2832
+ type: 'chat-messages-changed' | 'chat-status-changed';
2833
+ chatId: number | string;
2834
+ error?: Error;
2835
+ }
2836
+ type ChatStatus = 'submitted' | 'streaming' | 'ready' | 'error';
2837
+ interface Chat<MESSAGE_METADATA> {
2838
+ status: ChatStatus;
2839
+ messages: UIMessage<MESSAGE_METADATA>[];
2840
+ error?: Error;
2841
+ activeResponse?: {
2842
+ state: StreamingUIMessageState<MESSAGE_METADATA>;
2843
+ abortController?: AbortController;
2844
+ };
2845
+ jobExecutor: SerialJobExecutor;
2846
+ }
2847
+ type ExtendedCallOptions<MESSAGE_METADATA> = ChatRequestOptions & {
2848
+ onError?: (error: Error) => void;
2849
+ /**
2850
+ Optional callback function that is invoked when a tool call is received.
2851
+ Intended for automatic client-side tool execution.
2852
+
2853
+ You can optionally return a result for the tool call,
2854
+ either synchronously or asynchronously.
2855
+ */
2856
+ onToolCall?: ({ toolCall, }: {
2857
+ toolCall: ToolCall<string, unknown>;
2858
+ }) => void | Promise<unknown> | unknown;
2859
+ /**
2860
+ * Optional callback function that is called when the assistant message is finished streaming.
2861
+ *
2862
+ * @param message The message that was streamed.
2863
+ */
2864
+ onFinish?: (options: {
2865
+ message: UIMessage<NoInfer<MESSAGE_METADATA>>;
2866
+ }) => void;
2867
+ };
2868
+ declare class ChatStore<MESSAGE_METADATA> {
2869
+ private chats;
2870
+ private subscribers;
2871
+ private generateId;
2872
+ private messageMetadataSchema;
2873
+ private transport;
2874
+ private maxSteps;
2875
+ constructor({ chats, generateId, messageMetadataSchema, transport, maxSteps, }: {
2876
+ chats?: {
2877
+ [id: string]: {
2878
+ messages: UIMessage<MESSAGE_METADATA>[];
2879
+ };
2880
+ };
2881
+ generateId?: UseChatOptions['generateId'];
2882
+ messageMetadataSchema?: Schema<MESSAGE_METADATA>;
2883
+ transport: ChatTransport<MESSAGE_METADATA>;
2884
+ maxSteps?: number;
2885
+ });
2886
+ hasChat(id: string): boolean;
2887
+ addChat(id: string, messages: UIMessage<MESSAGE_METADATA>[]): void;
2888
+ getChats(): [string, Chat<MESSAGE_METADATA>][];
2889
+ get chatCount(): number;
2890
+ getStatus(id: string): ChatStatus;
2891
+ setStatus({ id, status, error, }: {
2892
+ id: string;
2893
+ status: Chat<MESSAGE_METADATA>['status'];
2894
+ error?: Error;
2895
+ }): void;
2896
+ getError(id: string): Error | undefined;
2897
+ getMessages(id: string): UIMessage<MESSAGE_METADATA>[];
2898
+ getLastMessage(id: string): UIMessage<MESSAGE_METADATA>;
2899
+ subscribe(subscriber: ChatStoreSubscriber): () => void;
2900
+ setMessages({ id, messages, }: {
2901
+ id: string;
2902
+ messages: UIMessage<MESSAGE_METADATA>[];
2903
+ }): void;
2904
+ appendMessage({ id, message, }: {
2905
+ id: string;
2906
+ message: UIMessage<MESSAGE_METADATA>;
2907
+ }): void;
2908
+ removeAssistantResponse(id: string): void;
2909
+ submitMessage({ chatId, message, headers, body, onError, onToolCall, onFinish, }: ExtendedCallOptions<MESSAGE_METADATA> & {
2910
+ chatId: string;
2911
+ message: CreateUIMessage<MESSAGE_METADATA>;
2912
+ }): Promise<void>;
2913
+ resubmitLastUserMessage({ chatId, headers, body, onError, onToolCall, onFinish, }: ExtendedCallOptions<MESSAGE_METADATA> & {
2914
+ chatId: string;
2915
+ }): Promise<null | undefined>;
2916
+ resumeStream({ chatId, headers, body, onError, onToolCall, onFinish, }: ExtendedCallOptions<MESSAGE_METADATA> & {
2917
+ chatId: string;
2918
+ }): Promise<null | undefined>;
2919
+ addToolResult({ chatId, toolCallId, result, }: {
2920
+ chatId: string;
2921
+ toolCallId: string;
2922
+ result: unknown;
2923
+ }): Promise<void>;
2924
+ stopStream({ chatId }: {
2925
+ chatId: string;
2926
+ }): Promise<void>;
2927
+ private emit;
2928
+ private getChat;
2929
+ private triggerRequest;
2930
+ }
2652
2931
 
2653
2932
  type ChatRequestOptions = {
2654
2933
  /**
@@ -2659,16 +2938,51 @@ type ChatRequestOptions = {
2659
2938
  Additional body JSON properties that should be sent to the API endpoint.
2660
2939
  */
2661
2940
  body?: object;
2941
+ };
2942
+ type UseChatOptions<MESSAGE_METADATA = unknown> = {
2943
+ /**
2944
+ * A unique identifier for the chat. If not provided, a random one will be
2945
+ * generated. When provided, the `useChat` hook with the same `id` will
2946
+ * have shared states across components.
2947
+ */
2948
+ id?: string;
2949
+ /**
2950
+ * Initial input of the chat.
2951
+ */
2952
+ initialInput?: string;
2662
2953
  /**
2663
- Additional data to be sent to the API endpoint.
2954
+ Optional callback function that is invoked when a tool call is received.
2955
+ Intended for automatic client-side tool execution.
2956
+
2957
+ You can optionally return a result for the tool call,
2958
+ either synchronously or asynchronously.
2664
2959
  */
2665
- data?: JSONValue$1;
2960
+ onToolCall?: ({ toolCall, }: {
2961
+ toolCall: ToolCall<string, unknown>;
2962
+ }) => void | Promise<unknown> | unknown;
2963
+ /**
2964
+ * Optional callback function that is called when the assistant message is finished streaming.
2965
+ *
2966
+ * @param message The message that was streamed.
2967
+ */
2968
+ onFinish?: (options: {
2969
+ message: UIMessage<NoInfer<MESSAGE_METADATA>>;
2970
+ }) => void;
2971
+ /**
2972
+ * Callback function to be called when an error is encountered.
2973
+ */
2974
+ onError?: (error: Error) => void;
2975
+ /**
2976
+ * A way to provide a function that is going to be used for ids for messages and the chat.
2977
+ * If not provided the default AI SDK `generateId` is used.
2978
+ */
2979
+ generateId?: IdGenerator;
2666
2980
  /**
2667
- * Allow submitting an empty message. Defaults to `false`.
2981
+ * Optional chat store. Default is used when not provided.
2668
2982
  */
2669
- allowEmptySubmit?: boolean;
2983
+ chatStore?: ChatStore<MESSAGE_METADATA>;
2670
2984
  };
2671
- type UseChatOptions<MESSAGE_METADATA = unknown> = {
2985
+ type OriginalUseChatOptions<MESSAGE_METADATA = unknown> = {
2672
2986
  /**
2673
2987
  * Schema for the message metadata. Validates the message metadata.
2674
2988
  * Message metadata can be undefined or must match the schema.
@@ -2703,10 +3017,6 @@ type UseChatOptions<MESSAGE_METADATA = unknown> = {
2703
3017
  onToolCall?: ({ toolCall, }: {
2704
3018
  toolCall: ToolCall<string, unknown>;
2705
3019
  }) => void | Promise<unknown> | unknown;
2706
- /**
2707
- * Callback function to be called when the API response is received.
2708
- */
2709
- onResponse?: (response: Response) => void | Promise<void>;
2710
3020
  /**
2711
3021
  * Optional callback function that is called when the assistant message is finished streaming.
2712
3022
  *
@@ -2748,9 +3058,9 @@ type UseChatOptions<MESSAGE_METADATA = unknown> = {
2748
3058
  */
2749
3059
  body?: object;
2750
3060
  /**
2751
- Streaming protocol that is used. Defaults to `data`.
3061
+ Streaming protocol that is used. Defaults to `ui-message`.
2752
3062
  */
2753
- streamProtocol?: 'data' | 'text';
3063
+ streamProtocol?: 'ui-message' | 'text';
2754
3064
  /**
2755
3065
  Custom fetch implementation. You can use it as a middleware to intercept requests,
2756
3066
  or to provide a custom fetch implementation for e.g. testing.
@@ -2768,14 +3078,13 @@ type UseChatOptions<MESSAGE_METADATA = unknown> = {
2768
3078
  };
2769
3079
 
2770
3080
  declare const getOriginalFetch$1: () => typeof fetch;
2771
- declare function callChatApi<MESSAGE_METADATA>({ api, body, streamProtocol, credentials, headers, abortController, onResponse, onUpdate, onFinish, onToolCall, generateId, fetch, lastMessage, requestType, messageMetadataSchema, }: {
3081
+ declare function callChatApi<MESSAGE_METADATA>({ api, body, streamProtocol, credentials, headers, abortController, onUpdate, onFinish, onToolCall, generateId, fetch, lastMessage, requestType, messageMetadataSchema, }: {
2772
3082
  api: string;
2773
3083
  body: Record<string, any>;
2774
- streamProtocol: 'data' | 'text' | undefined;
3084
+ streamProtocol: 'ui-message' | 'text' | undefined;
2775
3085
  credentials: RequestCredentials | undefined;
2776
3086
  headers: HeadersInit | undefined;
2777
3087
  abortController: (() => AbortController | null) | undefined;
2778
- onResponse: ((response: Response) => void | Promise<void>) | undefined;
2779
3088
  onUpdate: (options: {
2780
3089
  message: UIMessage<MESSAGE_METADATA>;
2781
3090
  }) => void;
@@ -2789,7 +3098,7 @@ declare function callChatApi<MESSAGE_METADATA>({ api, body, streamProtocol, cred
2789
3098
  }): Promise<void>;
2790
3099
 
2791
3100
  declare const getOriginalFetch: () => typeof fetch;
2792
- declare function callCompletionApi({ api, prompt, credentials, headers, body, streamProtocol, setCompletion, setLoading, setError, setAbortController, onResponse, onFinish, onError, fetch, }: {
3101
+ declare function callCompletionApi({ api, prompt, credentials, headers, body, streamProtocol, setCompletion, setLoading, setError, setAbortController, onFinish, onError, fetch, }: {
2793
3102
  api: string;
2794
3103
  prompt: string;
2795
3104
  credentials: RequestCredentials | undefined;
@@ -2800,7 +3109,6 @@ declare function callCompletionApi({ api, prompt, credentials, headers, body, st
2800
3109
  setLoading: (loading: boolean) => void;
2801
3110
  setError: (error: Error | undefined) => void;
2802
3111
  setAbortController: (abortController: AbortController | null) => void;
2803
- onResponse: ((response: Response) => void | Promise<void>) | undefined;
2804
3112
  onFinish: ((prompt: string, completion: string) => void) | undefined;
2805
3113
  onError: ((error: Error) => void) | undefined;
2806
3114
  fetch: ReturnType<typeof getOriginalFetch> | undefined;
@@ -2820,15 +3128,88 @@ declare function convertToModelMessages<TOOLS extends ToolSet = never>(messages:
2820
3128
  */
2821
3129
  declare const convertToCoreMessages: typeof convertToModelMessages;
2822
3130
 
3131
+ declare function defaultChatStore<MESSAGE_METADATA>({ api, fetch, streamProtocol, credentials, headers, body, prepareRequestBody, generateId, messageMetadataSchema, maxSteps, chats, }: {
3132
+ /**
3133
+ * Schema for the message metadata. Validates the message metadata.
3134
+ * Message metadata can be undefined or must match the schema.
3135
+ */
3136
+ messageMetadataSchema?: Schema<MESSAGE_METADATA>;
3137
+ /**
3138
+ * The API endpoint that accepts a `{ messages: Message[] }` object and returns
3139
+ * a stream of tokens of the AI chat response.
3140
+ */
3141
+ api: string;
3142
+ /**
3143
+ * A way to provide a function that is going to be used for ids for messages and the chat.
3144
+ * If not provided the default AI SDK `generateId` is used.
3145
+ */
3146
+ generateId?: IdGenerator;
3147
+ /**
3148
+ * The credentials mode to be used for the fetch request.
3149
+ * Possible values are: 'omit', 'same-origin', 'include'.
3150
+ * Defaults to 'same-origin'.
3151
+ */
3152
+ credentials?: RequestCredentials;
3153
+ /**
3154
+ * HTTP headers to be sent with the API request.
3155
+ */
3156
+ headers?: Record<string, string> | Headers;
3157
+ /**
3158
+ * Extra body object to be sent with the API request.
3159
+ * @example
3160
+ * Send a `sessionId` to the API along with the messages.
3161
+ * ```js
3162
+ * useChat({
3163
+ * body: {
3164
+ * sessionId: '123',
3165
+ * }
3166
+ * })
3167
+ * ```
3168
+ */
3169
+ body?: object;
3170
+ /**
3171
+ Streaming protocol that is used. Defaults to `ui-message`.
3172
+ */
3173
+ streamProtocol?: 'ui-message' | 'text';
3174
+ /**
3175
+ Custom fetch implementation. You can use it as a middleware to intercept requests,
3176
+ or to provide a custom fetch implementation for e.g. testing.
3177
+ */
3178
+ fetch?: FetchFunction;
3179
+ /**
3180
+ Maximum number of sequential LLM calls (steps), e.g. when you use tool calls.
3181
+ Must be at least 1.
3182
+
3183
+ A maximum number is required to prevent infinite loops in the case of misconfigured tools.
3184
+
3185
+ By default, it's set to 1, which means that only a single LLM call is made.
3186
+ */
3187
+ maxSteps?: number;
3188
+ /**
3189
+ * When a function is provided, it will be used
3190
+ * to prepare the request body for the chat API. This can be useful for
3191
+ * customizing the request body based on the messages and data in the chat.
3192
+ *
3193
+ * @param id The id of the chat.
3194
+ * @param messages The current messages in the chat.
3195
+ * @param requestBody The request body object passed in the chat request.
3196
+ */
3197
+ prepareRequestBody?: (options: {
3198
+ id: string;
3199
+ messages: UIMessage<MESSAGE_METADATA>[];
3200
+ requestBody?: object;
3201
+ }) => unknown;
3202
+ chats?: {
3203
+ [id: string]: {
3204
+ messages: UIMessage<MESSAGE_METADATA>[];
3205
+ };
3206
+ };
3207
+ }): ChatStore<MESSAGE_METADATA>;
3208
+
2823
3209
  declare function extractMaxToolInvocationStep(toolInvocations: ToolInvocation[] | undefined): number | undefined;
2824
3210
 
2825
3211
  declare function getToolInvocations(message: UIMessage): ToolInvocation[];
2826
3212
 
2827
- declare function processTextStream({ stream, onTextPart, }: {
2828
- stream: ReadableStream<Uint8Array>;
2829
- onTextPart: (chunk: string) => Promise<void> | void;
2830
- }): Promise<void>;
2831
-
2832
3213
  declare function shouldResubmitMessages({ originalMaxToolInvocationStep, originalMessageCount, maxSteps, messages, }: {
2833
3214
  originalMaxToolInvocationStep: number | undefined;
2834
3215
  originalMessageCount: number;
@@ -2889,10 +3270,6 @@ type UseCompletionOptions = {
2889
3270
  * Initial completion result. Useful to load an existing history.
2890
3271
  */
2891
3272
  initialCompletion?: string;
2892
- /**
2893
- * Callback function to be called when the API response is received.
2894
- */
2895
- onResponse?: (response: Response) => void | Promise<void>;
2896
3273
  /**
2897
3274
  * Callback function to be called when the completion is finished streaming.
2898
3275
  */
@@ -3515,7 +3892,7 @@ A function that attempts to repair a tool call that failed to parse.
3515
3892
 
3516
3893
  type AsyncIterableStream<T> = AsyncIterable<T> & ReadableStream<T>;
3517
3894
 
3518
- type DataStreamOptions = {
3895
+ type UIMessageStreamOptions = {
3519
3896
  /**
3520
3897
  * Message ID that is sent to the client if a new message is created.
3521
3898
  * This is intended to be used for the UI message,
@@ -3724,7 +4101,7 @@ interface StreamTextResult<TOOLS extends ToolSet, PARTIAL_OUTPUT> {
3724
4101
  */
3725
4102
  consumeStream(options?: ConsumeStreamOptions): Promise<void>;
3726
4103
  /**
3727
- Converts the result to a data stream.
4104
+ Converts the result to a UI message stream.
3728
4105
 
3729
4106
  @param options.getErrorMessage an optional function that converts an error to an error message.
3730
4107
  @param options.sendUsage whether to send the usage information to the client. Defaults to true.
@@ -3733,11 +4110,11 @@ interface StreamTextResult<TOOLS extends ToolSet, PARTIAL_OUTPUT> {
3733
4110
  @param options.experimental_sendFinish whether to send the finish information to the client. Defaults to true.
3734
4111
  @param options.experimental_sendStart whether to send the start information to the client. Defaults to true.
3735
4112
 
3736
- @return A data stream.
4113
+ @return A UI message stream.
3737
4114
  */
3738
- toDataStream(options?: DataStreamOptions): ReadableStream<DataStreamPart>;
4115
+ toUIMessageStream(options?: UIMessageStreamOptions): ReadableStream<UIMessageStreamPart>;
3739
4116
  /**
3740
- Writes data stream output to a Node.js response-like object.
4117
+ Writes UI message stream output to a Node.js response-like object.
3741
4118
  @param response A Node.js response-like object (ServerResponse).
3742
4119
  @param options.status The status code.
3743
4120
  @param options.statusText The status text.
@@ -3746,7 +4123,7 @@ interface StreamTextResult<TOOLS extends ToolSet, PARTIAL_OUTPUT> {
3746
4123
  @param options.sendUsage Whether to send the usage information to the client. Defaults to true.
3747
4124
  @param options.sendReasoning Whether to send the reasoning information to the client. Defaults to false.
3748
4125
  */
3749
- pipeDataStreamToResponse(response: ServerResponse, options?: ResponseInit & DataStreamOptions): void;
4126
+ pipeUIMessageStreamToResponse(response: ServerResponse, options?: ResponseInit & UIMessageStreamOptions): void;
3750
4127
  /**
3751
4128
  Writes text delta output to a Node.js response-like object.
3752
4129
  It sets a `Content-Type` header to `text/plain; charset=utf-8` and
@@ -3757,17 +4134,16 @@ interface StreamTextResult<TOOLS extends ToolSet, PARTIAL_OUTPUT> {
3757
4134
  pipeTextStreamToResponse(response: ServerResponse, init?: ResponseInit): void;
3758
4135
  /**
3759
4136
  Converts the result to a streamed response object with a stream data part stream.
3760
- It can be used with the `useChat` and `useCompletion` hooks.
4137
+
3761
4138
  @param options.status The status code.
3762
4139
  @param options.statusText The status text.
3763
4140
  @param options.headers The headers.
3764
- @param options.data The stream data.
3765
4141
  @param options.getErrorMessage An optional function that converts an error to an error message.
3766
4142
  @param options.sendUsage Whether to send the usage information to the client. Defaults to true.
3767
4143
  @param options.sendReasoning Whether to send the reasoning information to the client. Defaults to false.
3768
4144
  @return A response object.
3769
4145
  */
3770
- toDataStreamResponse(options?: ResponseInit & DataStreamOptions): Response;
4146
+ toUIMessageStreamResponse(options?: ResponseInit & UIMessageStreamOptions): Response;
3771
4147
  /**
3772
4148
  Creates a simple text stream response.
3773
4149
  Each text delta is encoded as UTF-8 and sent as a separate chunk.
@@ -4913,4 +5289,4 @@ declare function transcribe({ model, audio, providerOptions, maxRetries: maxRetr
4913
5289
  headers?: Record<string, string>;
4914
5290
  }): Promise<TranscriptionResult>;
4915
5291
 
4916
- export { AssistantContent, AssistantModelMessage, CallSettings, CallWarning, ChatRequestOptions, ChunkDetector, CompletionRequestOptions, CoreAssistantMessage, CoreMessage, CoreSystemMessage, CoreToolMessage, CoreUserMessage, CreateUIMessage, DataContent, DataStreamOptions, DataStreamPart, DataStreamWriter, DeepPartial, DownloadError, EmbedManyResult, EmbedResult, Embedding, EmbeddingModel, EmbeddingModelUsage, GenerateImageResult as Experimental_GenerateImageResult, GeneratedFile as Experimental_GeneratedImage, SpeechResult as Experimental_SpeechResult, TranscriptionResult as Experimental_TranscriptionResult, FilePart, FileUIPart, FinishReason, GenerateObjectResult, GenerateTextOnStepFinishCallback, GenerateTextResult, GeneratedAudioFile, GeneratedFile, ImageModel, ImageGenerationWarning as ImageModelCallWarning, ImageModelProviderMetadata, ImageModelResponseMetadata, ImagePart, InvalidArgumentError, InvalidDataContentError, InvalidMessageRoleError, InvalidStreamPartError, InvalidToolArgumentsError, JSONRPCError, JSONRPCMessage, JSONRPCNotification, JSONRPCRequest, JSONRPCResponse, JSONValue, JsonToSseTransformStream, LanguageModel, LanguageModelRequestMetadata, LanguageModelResponseMetadata, LanguageModelUsage, MCPClientError, MCPTransport, MessageConversionError, ModelMessage, NoImageGeneratedError, NoObjectGeneratedError, NoOutputSpecifiedError, NoSuchProviderError, NoSuchToolError, ObjectStreamPart, output as Output, Prompt, Provider, ProviderMetadata, ProviderOptions, ProviderRegistryProvider, ReasoningUIPart, RepairTextFunction, RetryError, SourceUIPart, SpeechModel, SpeechModelResponseMetadata, SpeechWarning, StepResult, StepStartUIPart, StreamObjectOnFinishCallback, StreamObjectResult, StreamTextOnChunkCallback, StreamTextOnErrorCallback, StreamTextOnFinishCallback, StreamTextOnStepFinishCallback, StreamTextResult, StreamTextTransform, SystemModelMessage, TelemetrySettings, TextPart, TextStreamPart, TextUIPart, Tool, ToolCallPart, ToolCallRepairError, ToolCallRepairFunction, ToolCallUnion, ToolChoice, ToolContent, ToolExecutionError, ToolExecutionOptions, ToolInvocation, ToolInvocationUIPart, ToolModelMessage, ToolResultPart, ToolResultUnion, ToolSet, TranscriptionModel, TranscriptionModelResponseMetadata, TranscriptionWarning, UIMessage, UIMessagePart, UseChatOptions, UseCompletionOptions, UserContent, UserModelMessage, appendClientMessage, assistantModelMessageSchema, callChatApi, callCompletionApi, convertFileListToFileUIParts, convertToCoreMessages, convertToModelMessages, coreAssistantMessageSchema, coreMessageSchema, coreSystemMessageSchema, coreToolMessageSchema, coreUserMessageSchema, cosineSimilarity, createDataStream, createDataStreamResponse, createProviderRegistry, createTextStreamResponse, customProvider, defaultSettingsMiddleware, embed, embedMany, createMCPClient as experimental_createMCPClient, experimental_createProviderRegistry, experimental_customProvider, generateImage as experimental_generateImage, generateSpeech as experimental_generateSpeech, transcribe as experimental_transcribe, extractMaxToolInvocationStep, extractReasoningMiddleware, generateObject, generateText, getTextFromDataUrl, getToolInvocations, isAssistantMessageWithCompletedToolCalls, isDeepEqualData, modelMessageSchema, parsePartialJson, pipeDataStreamToResponse, pipeTextStreamToResponse, processTextStream, shouldResubmitMessages, simulateReadableStream, simulateStreamingMiddleware, smoothStream, streamObject, streamText, systemModelMessageSchema, tool, toolModelMessageSchema, updateToolCallResult, userModelMessageSchema, wrapLanguageModel };
5292
+ export { AssistantContent, AssistantModelMessage, CallSettings, CallWarning, ChatRequestOptions, ChatStatus, ChatStore, ChatStoreEvent, ChatTransport, ChunkDetector, CompletionRequestOptions, CoreAssistantMessage, CoreMessage, CoreSystemMessage, CoreToolMessage, CoreUserMessage, CreateUIMessage, DataContent, DeepPartial, DefaultChatTransport, DownloadError, EmbedManyResult, EmbedResult, Embedding, EmbeddingModel, EmbeddingModelUsage, GenerateImageResult as Experimental_GenerateImageResult, GeneratedFile as Experimental_GeneratedImage, SpeechResult as Experimental_SpeechResult, TranscriptionResult as Experimental_TranscriptionResult, FilePart, FileUIPart, FinishReason, GenerateObjectResult, GenerateTextOnStepFinishCallback, GenerateTextResult, GeneratedAudioFile, GeneratedFile, ImageModel, ImageGenerationWarning as ImageModelCallWarning, ImageModelProviderMetadata, ImageModelResponseMetadata, ImagePart, InvalidArgumentError, InvalidDataContentError, InvalidMessageRoleError, InvalidStreamPartError, InvalidToolArgumentsError, JSONRPCError, JSONRPCMessage, JSONRPCNotification, JSONRPCRequest, JSONRPCResponse, JSONValue, JsonToSseTransformStream, LanguageModel, LanguageModelRequestMetadata, LanguageModelResponseMetadata, LanguageModelUsage, MCPClientError, MCPTransport, MessageConversionError, ModelMessage, NoImageGeneratedError, NoObjectGeneratedError, NoOutputSpecifiedError, NoSuchProviderError, NoSuchToolError, ObjectStreamPart, OriginalUseChatOptions, output as Output, Prompt, Provider, ProviderMetadata, ProviderOptions, ProviderRegistryProvider, ReasoningUIPart, RepairTextFunction, RetryError, SourceUIPart, SpeechModel, SpeechModelResponseMetadata, SpeechWarning, StepResult, StepStartUIPart, StreamObjectOnFinishCallback, StreamObjectResult, StreamTextOnChunkCallback, StreamTextOnErrorCallback, StreamTextOnFinishCallback, StreamTextOnStepFinishCallback, StreamTextResult, StreamTextTransform, SystemModelMessage, TelemetrySettings, TextPart, TextStreamPart, TextUIPart, Tool, ToolCallPart, ToolCallRepairError, ToolCallRepairFunction, ToolCallUnion, ToolChoice, ToolContent, ToolExecutionError, ToolExecutionOptions, ToolInvocation, ToolInvocationUIPart, ToolModelMessage, ToolResultPart, ToolResultUnion, ToolSet, TranscriptionModel, TranscriptionModelResponseMetadata, TranscriptionWarning, UIMessage, UIMessagePart, UIMessageStreamOptions, UIMessageStreamPart, UIMessageStreamWriter, UseChatOptions, UseCompletionOptions, UserContent, UserModelMessage, appendClientMessage, assistantModelMessageSchema, callChatApi, callCompletionApi, convertFileListToFileUIParts, convertToCoreMessages, convertToModelMessages, coreAssistantMessageSchema, coreMessageSchema, coreSystemMessageSchema, coreToolMessageSchema, coreUserMessageSchema, cosineSimilarity, createProviderRegistry, createTextStreamResponse, createUIMessageStream, createUIMessageStreamResponse, customProvider, defaultChatStore, defaultSettingsMiddleware, embed, embedMany, createMCPClient as experimental_createMCPClient, experimental_createProviderRegistry, experimental_customProvider, generateImage as experimental_generateImage, generateSpeech as experimental_generateSpeech, transcribe as experimental_transcribe, extractMaxToolInvocationStep, extractReasoningMiddleware, generateObject, generateText, getTextFromDataUrl, getToolInvocations, isAssistantMessageWithCompletedToolCalls, isDeepEqualData, modelMessageSchema, parsePartialJson, pipeTextStreamToResponse, pipeUIMessageStreamToResponse, shouldResubmitMessages, simulateReadableStream, simulateStreamingMiddleware, smoothStream, streamObject, streamText, systemModelMessageSchema, tool, toolModelMessageSchema, updateToolCallResult, userModelMessageSchema, wrapLanguageModel };