ai 5.0.0-alpha.10 → 5.0.0-alpha.12

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,4 +1,4 @@
1
- import { ToolResultContent, Schema, ToolCall, ToolResult, Validator, StandardSchemaV1, IdGenerator, InferSchema, FetchFunction } from '@ai-sdk/provider-utils';
1
+ import { ToolResultContent, Schema, ToolCall, ToolResult, Validator, StandardSchemaV1, IdGenerator, FetchFunction, InferSchema } from '@ai-sdk/provider-utils';
2
2
  export { IdGenerator, Schema, ToolCall, ToolResult, asSchema, createIdGenerator, generateId, jsonSchema } from '@ai-sdk/provider-utils';
3
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';
4
4
  export { AISDKError, APICallError, EmptyResponseBodyError, InvalidPromptError, InvalidResponseDataError, JSONParseError, JSONSchema7, LoadAPIKeyError, NoContentGeneratedError, NoSuchModelError, TypeValidationError, UnsupportedFunctionalityError } from '@ai-sdk/provider';
@@ -881,7 +881,7 @@ type MCPTransportConfig = {
881
881
  };
882
882
 
883
883
  type ToolParameters<T = JSONObject> = z4.$ZodType<T> | z3.Schema<T> | Schema<T>;
884
- interface ToolExecutionOptions {
884
+ interface ToolCallOptions {
885
885
  /**
886
886
  * The ID of the tool call. You can use it e.g. when sending tool-call related information with stream data.
887
887
  */
@@ -925,11 +925,30 @@ If not provided, the tool will not be executed automatically.
925
925
  @args is the input of the tool call.
926
926
  @options.abortSignal is a signal that can be used to abort the tool call.
927
927
  */
928
- execute: (args: [PARAMETERS] extends [never] ? undefined : PARAMETERS, options: ToolExecutionOptions) => PromiseLike<RESULT>;
928
+ execute: (args: [PARAMETERS] extends [never] ? undefined : PARAMETERS, options: ToolCallOptions) => PromiseLike<RESULT>;
929
929
  /**
930
930
  Optional conversion function that maps the tool result to multi-part tool content for LLMs.
931
931
  */
932
932
  experimental_toToolResultContent?: (result: RESULT) => ToolResultContent;
933
+ /**
934
+ * Optional function that is called when the argument streaming starts.
935
+ * Only called when the tool is used in a streaming context.
936
+ */
937
+ onArgsStreamingStart?: (options: ToolCallOptions) => void | PromiseLike<void>;
938
+ /**
939
+ * Optional function that is called when an argument streaming delta is available.
940
+ * Only called when the tool is used in a streaming context.
941
+ */
942
+ onArgsStreamingDelta?: (options: {
943
+ argsTextDelta: string;
944
+ } & ToolCallOptions) => void | PromiseLike<void>;
945
+ /**
946
+ * Optional function that is called when a tool call can be started,
947
+ * even if the execute function is not provided.
948
+ */
949
+ onArgsAvailable?: (options: {
950
+ args: [PARAMETERS] extends [never] ? undefined : PARAMETERS;
951
+ } & ToolCallOptions) => void | PromiseLike<void>;
933
952
  }> & ({
934
953
  /**
935
954
  Function tool.
@@ -952,10 +971,10 @@ The arguments for configuring the tool. Must match the expected arguments define
952
971
  /**
953
972
  Helper function for inferring the execute args of a tool.
954
973
  */
955
- declare function tool(tool: Tool<never, never>): Tool<never, never>;
974
+ declare function tool<PARAMETERS, RESULT>(tool: Tool<PARAMETERS, RESULT>): Tool<PARAMETERS, RESULT>;
956
975
  declare function tool<PARAMETERS>(tool: Tool<PARAMETERS, never>): Tool<PARAMETERS, never>;
957
976
  declare function tool<RESULT>(tool: Tool<never, RESULT>): Tool<never, RESULT>;
958
- declare function tool<PARAMETERS, RESULT>(tool: Tool<PARAMETERS, RESULT>): Tool<PARAMETERS, RESULT>;
977
+ declare function tool(tool: Tool<never, never>): Tool<never, never>;
959
978
  type MappedTool<T extends Tool | JSONObject, RESULT extends any> = T extends Tool<infer P> ? Tool<P, RESULT> : T extends JSONObject ? Tool<T, RESULT> : never;
960
979
 
961
980
  type ToolSchemas = Record<string, {
@@ -1736,7 +1755,7 @@ declare class MCPClient {
1736
1755
  private onResponse;
1737
1756
  }
1738
1757
 
1739
- type ToolSet = Record<string, (Tool<never, never> | Tool<any, any> | Tool<any, never> | Tool<never, any>) & Pick<Tool<any, any>, 'execute'>>;
1758
+ type ToolSet = Record<string, (Tool<never, never> | Tool<any, any> | Tool<any, never> | Tool<never, any>) & Pick<Tool<any, any>, 'execute' | 'onArgsAvailable' | 'onArgsStreamingStart' | 'onArgsStreamingDelta'>>;
1740
1759
 
1741
1760
  type ToolCallUnion<TOOLS extends ToolSet> = ValueOf<{
1742
1761
  [NAME in keyof TOOLS]: {
@@ -1955,6 +1974,9 @@ type SingleRequestTextStreamPart<TOOLS extends ToolSet> = ContentPart<TOOLS> | {
1955
1974
  } | {
1956
1975
  type: 'error';
1957
1976
  error: unknown;
1977
+ } | {
1978
+ type: 'raw';
1979
+ rawValue: unknown;
1958
1980
  };
1959
1981
 
1960
1982
  declare const symbol$c: unique symbol;
@@ -2155,7 +2177,7 @@ interface UIMessage<METADATA = unknown, DATA_PARTS extends UIDataTypes = UIDataT
2155
2177
  */
2156
2178
  parts: Array<UIMessagePart<DATA_PARTS>>;
2157
2179
  }
2158
- type UIMessagePart<DATA_TYPES extends UIDataTypes> = TextUIPart | ReasoningUIPart | ToolInvocationUIPart | SourceUrlUIPart | FileUIPart | DataUIPart<DATA_TYPES> | StepStartUIPart;
2180
+ type UIMessagePart<DATA_TYPES extends UIDataTypes> = TextUIPart | ReasoningUIPart | ToolInvocationUIPart | SourceUrlUIPart | SourceDocumentUIPart | FileUIPart | DataUIPart<DATA_TYPES> | StepStartUIPart;
2159
2181
  type DataUIPart<DATA_TYPES extends UIDataTypes> = ValueOf<{
2160
2182
  [NAME in keyof DATA_TYPES & string]: {
2161
2183
  type: `data-${NAME}`;
@@ -2167,6 +2189,8 @@ type UIDataPartSchemas = Record<string, Validator<any> | StandardSchemaV1<any>>;
2167
2189
  type InferUIDataParts<T extends UIDataPartSchemas> = {
2168
2190
  [K in keyof T]: T[K] extends Validator<infer U> ? U : T[K] extends StandardSchemaV1<infer U> ? U : unknown;
2169
2191
  };
2192
+ type InferUIMessageData<T extends UIMessage> = T extends UIMessage<unknown, infer DATA_TYPES> ? DATA_TYPES : UIDataTypes;
2193
+ type InferUIMessageMetadata<T extends UIMessage> = T extends UIMessage<infer METADATA, UIDataTypes> ? METADATA : unknown;
2170
2194
  /**
2171
2195
  * A text part of a message.
2172
2196
  */
@@ -2211,6 +2235,17 @@ type SourceUrlUIPart = {
2211
2235
  title?: string;
2212
2236
  providerMetadata?: Record<string, any>;
2213
2237
  };
2238
+ /**
2239
+ * A document source part of a message.
2240
+ */
2241
+ type SourceDocumentUIPart = {
2242
+ type: 'source-document';
2243
+ sourceId: string;
2244
+ mediaType: string;
2245
+ title: string;
2246
+ filename?: string;
2247
+ providerMetadata?: Record<string, any>;
2248
+ };
2214
2249
  /**
2215
2250
  * A file part of a message.
2216
2251
  */
@@ -2311,352 +2346,497 @@ declare function callCompletionApi({ api, prompt, credentials, headers, body, st
2311
2346
  fetch: ReturnType<typeof getOriginalFetch> | undefined;
2312
2347
  }): Promise<string | null | undefined>;
2313
2348
 
2314
- /**
2315
- The result of an `embed` call.
2316
- It contains the embedding, the value, and additional information.
2317
- */
2318
- interface EmbedResult<VALUE> {
2319
- /**
2320
- The value that was embedded.
2321
- */
2322
- readonly value: VALUE;
2323
- /**
2324
- The embedding of the value.
2325
- */
2326
- readonly embedding: Embedding;
2327
- /**
2328
- The embedding token usage.
2329
- */
2330
- readonly usage: EmbeddingModelUsage;
2331
- /**
2332
- Optional response data.
2333
- */
2334
- readonly response?: {
2335
- /**
2336
- Response headers.
2337
- */
2338
- headers?: Record<string, string>;
2339
- /**
2340
- The response body.
2341
- */
2342
- body?: unknown;
2349
+ type DataUIMessageStreamPart<DATA_TYPES extends UIDataTypes> = ValueOf<{
2350
+ [NAME in keyof DATA_TYPES & string]: {
2351
+ type: `data-${NAME}`;
2352
+ id?: string;
2353
+ data: DATA_TYPES[NAME];
2343
2354
  };
2344
- }
2345
-
2346
- /**
2347
- Embed a value using an embedding model. The type of the value is defined by the embedding model.
2348
-
2349
- @param model - The embedding model to use.
2350
- @param value - The value that should be embedded.
2351
-
2352
- @param maxRetries - Maximum number of retries. Set to 0 to disable retries. Default: 2.
2353
- @param abortSignal - An optional abort signal that can be used to cancel the call.
2354
- @param headers - Additional HTTP headers to be sent with the request. Only applicable for HTTP-based providers.
2355
+ }>;
2356
+ type UIMessageStreamPart<METADATA = unknown, DATA_TYPES extends UIDataTypes = UIDataTypes> = {
2357
+ type: 'text';
2358
+ text: string;
2359
+ } | {
2360
+ type: 'error';
2361
+ errorText: string;
2362
+ } | {
2363
+ type: 'tool-call';
2364
+ toolCallId: string;
2365
+ toolName: string;
2366
+ args: unknown;
2367
+ } | {
2368
+ type: 'tool-result';
2369
+ toolCallId: string;
2370
+ result: unknown;
2371
+ providerMetadata?: ProviderMetadata;
2372
+ } | {
2373
+ type: 'tool-call-streaming-start';
2374
+ toolCallId: string;
2375
+ toolName: string;
2376
+ } | {
2377
+ type: 'tool-call-delta';
2378
+ toolCallId: string;
2379
+ argsTextDelta: string;
2380
+ } | {
2381
+ type: 'reasoning';
2382
+ text: string;
2383
+ providerMetadata?: ProviderMetadata;
2384
+ } | {
2385
+ type: 'source-url';
2386
+ sourceId: string;
2387
+ url: string;
2388
+ title?: string;
2389
+ providerMetadata?: ProviderMetadata;
2390
+ } | {
2391
+ type: 'source-document';
2392
+ sourceId: string;
2393
+ mediaType: string;
2394
+ title: string;
2395
+ filename?: string;
2396
+ providerMetadata?: ProviderMetadata;
2397
+ } | {
2398
+ type: 'file';
2399
+ url: string;
2400
+ mediaType: string;
2401
+ } | DataUIMessageStreamPart<DATA_TYPES> | {
2402
+ type: 'metadata';
2403
+ metadata: METADATA;
2404
+ } | {
2405
+ type: 'start-step';
2406
+ metadata?: METADATA;
2407
+ } | {
2408
+ type: 'finish-step';
2409
+ metadata?: METADATA;
2410
+ } | {
2411
+ type: 'start';
2412
+ messageId?: string;
2413
+ metadata?: METADATA;
2414
+ } | {
2415
+ type: 'finish';
2416
+ metadata?: METADATA;
2417
+ } | {
2418
+ type: 'reasoning-part-finish';
2419
+ };
2420
+ type InferUIMessageStreamPart<T extends UIMessage> = UIMessageStreamPart<InferUIMessageMetadata<T>, InferUIMessageData<T>>;
2355
2421
 
2356
- @returns A result object that contains the embedding, the value, and additional information.
2357
- */
2358
- declare function embed<VALUE>({ model, value, providerOptions, maxRetries: maxRetriesArg, abortSignal, headers, experimental_telemetry: telemetry, }: {
2359
- /**
2360
- The embedding model to use.
2361
- */
2362
- model: EmbeddingModel<VALUE>;
2422
+ interface UIMessageStreamWriter<UI_MESSAGE extends UIMessage = UIMessage> {
2363
2423
  /**
2364
- The value that should be embedded.
2424
+ * Appends a data stream part to the stream.
2365
2425
  */
2366
- value: VALUE;
2426
+ write(part: InferUIMessageStreamPart<UI_MESSAGE>): void;
2367
2427
  /**
2368
- Maximum number of retries per embedding model call. Set to 0 to disable retries.
2369
-
2370
- @default 2
2428
+ * Merges the contents of another stream to this stream.
2371
2429
  */
2372
- maxRetries?: number;
2373
- /**
2374
- Abort signal.
2375
- */
2376
- abortSignal?: AbortSignal;
2377
- /**
2378
- Additional headers to include in the request.
2379
- Only applicable for HTTP-based providers.
2380
- */
2381
- headers?: Record<string, string>;
2382
- /**
2383
- Additional provider-specific options. They are passed through
2384
- to the provider from the AI SDK and enable provider-specific
2385
- functionality that can be fully encapsulated in the provider.
2386
- */
2387
- providerOptions?: ProviderOptions;
2430
+ merge(stream: ReadableStream<InferUIMessageStreamPart<UI_MESSAGE>>): void;
2388
2431
  /**
2389
- * Optional telemetry configuration (experimental).
2432
+ * Error handler that is used by the data stream writer.
2433
+ * This is intended for forwarding when merging streams
2434
+ * to prevent duplicated error masking.
2390
2435
  */
2391
- experimental_telemetry?: TelemetrySettings;
2392
- }): Promise<EmbedResult<VALUE>>;
2436
+ onError: ((error: unknown) => string) | undefined;
2437
+ }
2393
2438
 
2394
- /**
2395
- The result of a `embedMany` call.
2396
- It contains the embeddings, the values, and additional information.
2397
- */
2398
- interface EmbedManyResult<VALUE> {
2399
- /**
2400
- The values that were embedded.
2401
- */
2402
- readonly values: Array<VALUE>;
2403
- /**
2404
- The embeddings. They are in the same order as the values.
2405
- */
2406
- readonly embeddings: Array<Embedding>;
2407
- /**
2408
- The embedding token usage.
2409
- */
2410
- readonly usage: EmbeddingModelUsage;
2439
+ declare function createUIMessageStream<UI_MESSAGE extends UIMessage>({ execute, onError, // mask error messages for safety by default
2440
+ originalMessages, onFinish, }: {
2441
+ execute: (options: {
2442
+ writer: UIMessageStreamWriter<UI_MESSAGE>;
2443
+ }) => Promise<void> | void;
2444
+ onError?: (error: unknown) => string;
2411
2445
  /**
2412
- Optional raw response data.
2413
- */
2414
- readonly responses?: Array<{
2446
+ * The original messages.
2447
+ */
2448
+ originalMessages?: UI_MESSAGE[];
2449
+ onFinish?: (options: {
2415
2450
  /**
2416
- Response headers.
2417
- */
2418
- headers?: Record<string, string>;
2451
+ * The updates list of UI messages.
2452
+ */
2453
+ messages: UI_MESSAGE[];
2419
2454
  /**
2420
- The response body.
2421
- */
2422
- body?: unknown;
2423
- } | undefined>;
2424
- }
2455
+ * Indicates whether the response message is a continuation of the last original message,
2456
+ * or if a new message was created.
2457
+ */
2458
+ isContinuation: boolean;
2459
+ /**
2460
+ * The message that was sent to the client as a response
2461
+ * (including the original message if it was extended).
2462
+ */
2463
+ responseMessage: UI_MESSAGE;
2464
+ }) => void;
2465
+ }): ReadableStream<InferUIMessageStreamPart<UI_MESSAGE>>;
2425
2466
 
2426
- /**
2427
- Embed several values using an embedding model. The type of the value is defined
2428
- by the embedding model.
2467
+ declare function createUIMessageStreamResponse({ status, statusText, headers, stream, }: ResponseInit & {
2468
+ stream: ReadableStream<UIMessageStreamPart>;
2469
+ }): Response;
2429
2470
 
2430
- `embedMany` automatically splits large requests into smaller chunks if the model
2431
- has a limit on how many embeddings can be generated in a single call.
2471
+ declare function pipeUIMessageStreamToResponse({ response, status, statusText, headers, stream, }: {
2472
+ response: ServerResponse;
2473
+ stream: ReadableStream<UIMessageStreamPart>;
2474
+ } & ResponseInit): void;
2432
2475
 
2433
- @param model - The embedding model to use.
2434
- @param values - The values that should be embedded.
2476
+ declare class JsonToSseTransformStream extends TransformStream<unknown, string> {
2477
+ constructor();
2478
+ }
2435
2479
 
2436
- @param maxRetries - Maximum number of retries. Set to 0 to disable retries. Default: 2.
2437
- @param abortSignal - An optional abort signal that can be used to cancel the call.
2438
- @param headers - Additional HTTP headers to be sent with the request. Only applicable for HTTP-based providers.
2480
+ interface ChatTransport<MESSAGE_METADATA, DATA_TYPES extends UIDataTypes> {
2481
+ submitMessages: (options: {
2482
+ chatId: string;
2483
+ messages: UIMessage<MESSAGE_METADATA, DATA_TYPES>[];
2484
+ abortSignal: AbortSignal | undefined;
2485
+ requestType: 'generate' | 'resume';
2486
+ } & ChatRequestOptions) => Promise<ReadableStream<UIMessageStreamPart>>;
2487
+ }
2439
2488
 
2440
- @returns A result object that contains the embeddings, the value, and additional information.
2441
- */
2442
- declare function embedMany<VALUE>({ model, values, maxParallelCalls, maxRetries: maxRetriesArg, abortSignal, headers, providerOptions, experimental_telemetry: telemetry, }: {
2443
- /**
2444
- The embedding model to use.
2445
- */
2446
- model: EmbeddingModel<VALUE>;
2489
+ type ChatRequestOptions = {
2447
2490
  /**
2448
- The values that should be embedded.
2491
+ Additional headers that should be to be passed to the API endpoint.
2449
2492
  */
2450
- values: Array<VALUE>;
2493
+ headers?: Record<string, string> | Headers;
2451
2494
  /**
2452
- Maximum number of retries per embedding model call. Set to 0 to disable retries.
2453
-
2454
- @default 2
2495
+ Additional body JSON properties that should be sent to the API endpoint.
2455
2496
  */
2456
- maxRetries?: number;
2457
- /**
2458
- Abort signal.
2459
- */
2460
- abortSignal?: AbortSignal;
2461
- /**
2462
- Additional headers to include in the request.
2463
- Only applicable for HTTP-based providers.
2464
- */
2465
- headers?: Record<string, string>;
2466
- /**
2467
- * Optional telemetry configuration (experimental).
2468
- */
2469
- experimental_telemetry?: TelemetrySettings;
2470
- /**
2471
- Additional provider-specific options. They are passed through
2472
- to the provider from the AI SDK and enable provider-specific
2473
- functionality that can be fully encapsulated in the provider.
2474
- */
2475
- providerOptions?: ProviderOptions;
2476
- /**
2477
- * Maximum number of concurrent requests.
2478
- *
2479
- * @default Infinity
2480
- */
2481
- maxParallelCalls?: number;
2482
- }): Promise<EmbedManyResult<VALUE>>;
2483
-
2484
- /**
2485
- A message that was generated during the generation process.
2486
- It can be either an assistant message or a tool message.
2487
- */
2488
- type ResponseMessage = AssistantModelMessage | ToolModelMessage;
2489
-
2490
- /**
2491
- * The result of a single step in the generation process.
2492
- */
2493
- type StepResult<TOOLS extends ToolSet> = {
2494
- /**
2495
- The content that was generated in the last step.
2496
- */
2497
- readonly content: Array<ContentPart<TOOLS>>;
2498
- /**
2499
- The generated text.
2500
- */
2501
- readonly text: string;
2502
- /**
2503
- The reasoning that was generated during the generation.
2504
- */
2505
- readonly reasoning: Array<ReasoningPart>;
2506
- /**
2507
- The reasoning text that was generated during the generation.
2508
- */
2509
- readonly reasoningText: string | undefined;
2510
- /**
2511
- The files that were generated during the generation.
2512
- */
2513
- readonly files: Array<GeneratedFile>;
2514
- /**
2515
- The sources that were used to generate the text.
2516
- */
2517
- readonly sources: Array<Source>;
2518
- /**
2519
- The tool calls that were made during the generation.
2520
- */
2521
- readonly toolCalls: ToolCallArray<TOOLS>;
2522
- /**
2523
- The results of the tool calls.
2524
- */
2525
- readonly toolResults: ToolResultArray<TOOLS>;
2526
- /**
2527
- The reason why the generation finished.
2528
- */
2529
- readonly finishReason: FinishReason;
2530
- /**
2531
- The token usage of the generated text.
2532
- */
2533
- readonly usage: LanguageModelUsage;
2534
- /**
2535
- Warnings from the model provider (e.g. unsupported settings).
2536
- */
2537
- readonly warnings: CallWarning[] | undefined;
2538
- /**
2539
- Additional request information.
2540
- */
2541
- readonly request: LanguageModelRequestMetadata;
2542
- /**
2543
- Additional response information.
2544
- */
2545
- readonly response: LanguageModelResponseMetadata & {
2546
- /**
2547
- The response messages that were generated during the call.
2548
- Response messages can be either assistant messages or tool messages.
2549
- They contain a generated id.
2550
- */
2551
- readonly messages: Array<ResponseMessage>;
2552
- /**
2553
- Response body (available only for providers that use HTTP requests).
2554
- */
2555
- body?: unknown;
2556
- };
2557
- /**
2558
- Additional provider-specific metadata. They are passed through
2559
- from the provider to the AI SDK and enable provider-specific
2560
- results that can be fully encapsulated in the provider.
2561
- */
2562
- readonly providerMetadata: ProviderMetadata | undefined;
2497
+ body?: object;
2498
+ metadata?: unknown;
2563
2499
  };
2564
-
2565
- /**
2566
- The result of a `generateText` call.
2567
- It contains the generated text, the tool calls that were made during the generation, and the results of the tool calls.
2568
- */
2569
- interface GenerateTextResult<TOOLS extends ToolSet, OUTPUT> {
2500
+ type ChatStatus = 'submitted' | 'streaming' | 'ready' | 'error';
2501
+ interface ChatState<MESSAGE_METADATA, DATA_TYPES extends UIDataTypes> {
2502
+ status: ChatStatus;
2503
+ error: Error | undefined;
2504
+ messages: UIMessage<MESSAGE_METADATA, DATA_TYPES>[];
2505
+ pushMessage: (message: UIMessage<MESSAGE_METADATA, DATA_TYPES>) => void;
2506
+ popMessage: () => void;
2507
+ replaceMessage: (index: number, message: UIMessage<MESSAGE_METADATA, DATA_TYPES>) => void;
2508
+ snapshot: <T>(thing: T) => T;
2509
+ }
2510
+ interface ChatInit<MESSAGE_METADATA = unknown, UI_DATA_PART_SCHEMAS extends UIDataPartSchemas = UIDataPartSchemas> {
2570
2511
  /**
2571
- The content that was generated in the last step.
2512
+ * A unique identifier for the chat. If not provided, a random one will be
2513
+ * generated.
2572
2514
  */
2573
- readonly content: Array<ContentPart<TOOLS>>;
2574
- /**
2575
- The text that was generated in the last step.
2576
- */
2577
- readonly text: string;
2515
+ id?: string;
2516
+ messageMetadataSchema?: Validator<MESSAGE_METADATA> | StandardSchemaV1<MESSAGE_METADATA>;
2517
+ dataPartSchemas?: UI_DATA_PART_SCHEMAS;
2518
+ messages?: UIMessage<MESSAGE_METADATA, InferUIDataParts<UI_DATA_PART_SCHEMAS>>[];
2578
2519
  /**
2579
- The full reasoning that the model has generated in the last step.
2520
+ * A way to provide a function that is going to be used for ids for messages and the chat.
2521
+ * If not provided the default AI SDK `generateId` is used.
2580
2522
  */
2581
- readonly reasoning: Array<ReasoningPart>;
2523
+ generateId?: IdGenerator;
2524
+ transport?: ChatTransport<NoInfer<MESSAGE_METADATA>, NoInfer<InferUIDataParts<UI_DATA_PART_SCHEMAS>>>;
2525
+ maxSteps?: number;
2582
2526
  /**
2583
- The reasoning text that the model has generated in the last step. Can be undefined if the model
2584
- has only generated text.
2527
+ * Callback function to be called when an error is encountered.
2585
2528
  */
2586
- readonly reasoningText: string | undefined;
2529
+ onError?: (error: Error) => void;
2587
2530
  /**
2588
- The files that were generated in the last step.
2589
- Empty array if no files were generated.
2531
+ Optional callback function that is invoked when a tool call is received.
2532
+ Intended for automatic client-side tool execution.
2533
+
2534
+ You can optionally return a result for the tool call,
2535
+ either synchronously or asynchronously.
2590
2536
  */
2591
- readonly files: Array<GeneratedFile>;
2592
- /**
2593
- Sources that have been used as references in the last step.
2594
- */
2595
- readonly sources: Array<Source>;
2596
- /**
2597
- The tool calls that were made in the last step.
2598
- */
2599
- readonly toolCalls: ToolCallArray<TOOLS>;
2600
- /**
2601
- The results of the tool calls from the last step.
2602
- */
2603
- readonly toolResults: ToolResultArray<TOOLS>;
2537
+ onToolCall?: ({ toolCall, }: {
2538
+ toolCall: ToolCall<string, unknown>;
2539
+ }) => void | Promise<unknown> | unknown;
2604
2540
  /**
2605
- The reason why the generation finished.
2541
+ * Optional callback function that is called when the assistant message is finished streaming.
2542
+ *
2543
+ * @param message The message that was streamed.
2606
2544
  */
2607
- readonly finishReason: FinishReason;
2545
+ onFinish?: (options: {
2546
+ message: UIMessage<NoInfer<MESSAGE_METADATA>, NoInfer<InferUIDataParts<UI_DATA_PART_SCHEMAS>>>;
2547
+ }) => void;
2548
+ }
2549
+ declare abstract class AbstractChat<MESSAGE_METADATA = unknown, UI_DATA_PART_SCHEMAS extends UIDataPartSchemas = UIDataPartSchemas> {
2550
+ readonly id: string;
2551
+ readonly generateId: IdGenerator;
2552
+ protected state: ChatState<MESSAGE_METADATA, InferUIDataParts<UI_DATA_PART_SCHEMAS>>;
2553
+ private messageMetadataSchema;
2554
+ private dataPartSchemas;
2555
+ private readonly transport;
2556
+ private maxSteps;
2557
+ private onError?;
2558
+ private onToolCall?;
2559
+ private onFinish?;
2560
+ private activeResponse;
2561
+ private jobExecutor;
2562
+ constructor({ generateId, id, transport, maxSteps, messageMetadataSchema, dataPartSchemas, state, onError, onToolCall, onFinish, }: Omit<ChatInit<MESSAGE_METADATA, UI_DATA_PART_SCHEMAS>, 'messages'> & {
2563
+ state: ChatState<MESSAGE_METADATA, InferUIDataParts<UI_DATA_PART_SCHEMAS>>;
2564
+ });
2608
2565
  /**
2609
- The token usage of the last step.
2566
+ * Hook status:
2567
+ *
2568
+ * - `submitted`: The message has been sent to the API and we're awaiting the start of the response stream.
2569
+ * - `streaming`: The response is actively streaming in from the API, receiving chunks of data.
2570
+ * - `ready`: The full response has been received and processed; a new user message can be submitted.
2571
+ * - `error`: An error occurred during the API request, preventing successful completion.
2610
2572
  */
2611
- readonly usage: LanguageModelUsage;
2573
+ get status(): ChatStatus;
2574
+ protected setStatus({ status, error, }: {
2575
+ status: ChatStatus;
2576
+ error?: Error;
2577
+ }): void;
2578
+ get error(): Error | undefined;
2579
+ get messages(): UIMessage<MESSAGE_METADATA, InferUIDataParts<UI_DATA_PART_SCHEMAS>>[];
2580
+ get lastMessage(): UIMessage<MESSAGE_METADATA, InferUIDataParts<UI_DATA_PART_SCHEMAS>> | undefined;
2581
+ set messages(messages: UIMessage<MESSAGE_METADATA, InferUIDataParts<UI_DATA_PART_SCHEMAS>>[]);
2582
+ removeAssistantResponse: () => void;
2612
2583
  /**
2613
- The total token usage of all steps.
2614
- When there are multiple steps, the usage is the sum of all step usages.
2584
+ * Append a user message to the chat list. This triggers the API call to fetch
2585
+ * the assistant's response.
2615
2586
  */
2616
- readonly totalUsage: LanguageModelUsage;
2587
+ sendMessage: (message: (CreateUIMessage<MESSAGE_METADATA, InferUIDataParts<UI_DATA_PART_SCHEMAS>> & {
2588
+ text?: never;
2589
+ files?: never;
2590
+ }) | {
2591
+ text: string;
2592
+ files?: FileList | FileUIPart[];
2593
+ metadata?: MESSAGE_METADATA;
2594
+ parts?: never;
2595
+ } | {
2596
+ files: FileList | FileUIPart[];
2597
+ metadata?: MESSAGE_METADATA;
2598
+ parts?: never;
2599
+ }, options?: ChatRequestOptions) => Promise<void>;
2617
2600
  /**
2618
- Warnings from the model provider (e.g. unsupported settings)
2601
+ * Regenerate the last assistant message.
2619
2602
  */
2620
- readonly warnings: CallWarning[] | undefined;
2603
+ reload: (options?: ChatRequestOptions) => Promise<void>;
2621
2604
  /**
2622
- Additional request information.
2605
+ * Resume an ongoing chat generation stream. This does not resume an aborted generation.
2623
2606
  */
2624
- readonly request: LanguageModelRequestMetadata;
2607
+ experimental_resume: (options?: ChatRequestOptions) => Promise<void>;
2608
+ addToolResult: ({ toolCallId, result, }: {
2609
+ toolCallId: string;
2610
+ result: unknown;
2611
+ }) => Promise<void>;
2625
2612
  /**
2626
- Additional response information.
2613
+ * Abort the current request immediately, keep the generated tokens if any.
2627
2614
  */
2628
- readonly response: LanguageModelResponseMetadata & {
2629
- /**
2630
- The response messages that were generated during the call. It consists of an assistant message,
2631
- potentially containing tool calls.
2632
-
2633
- When there are tool results, there is an additional tool message with the tool results that are available.
2634
- If there are tools that do not have execute functions, they are not included in the tool results and
2635
- need to be added separately.
2636
- */
2637
- messages: Array<ResponseMessage>;
2638
- /**
2639
- Response body (available only for providers that use HTTP requests).
2640
- */
2641
- body?: unknown;
2642
- };
2615
+ stop: () => Promise<void>;
2616
+ private triggerRequest;
2617
+ }
2618
+
2619
+ declare function convertFileListToFileUIParts(files: FileList | undefined): Promise<Array<FileUIPart>>;
2620
+
2621
+ /**
2622
+ Converts an array of messages from useChat into an array of CoreMessages that can be used
2623
+ with the AI core functions (e.g. `streamText`).
2624
+ */
2625
+ declare function convertToModelMessages<TOOLS extends ToolSet = never>(messages: Array<Omit<UIMessage, 'id'>>, options?: {
2626
+ tools?: TOOLS;
2627
+ }): ModelMessage[];
2628
+ /**
2629
+ @deprecated Use `convertToModelMessages` instead.
2630
+ */
2631
+ declare const convertToCoreMessages: typeof convertToModelMessages;
2632
+
2633
+ type PrepareRequest<MESSAGE_METADATA, DATA_TYPES extends UIDataTypes> = (options: {
2634
+ id: string;
2635
+ messages: UIMessage<MESSAGE_METADATA, DATA_TYPES>[];
2636
+ requestMetadata: unknown;
2637
+ body: Record<string, any> | undefined;
2638
+ credentials: RequestCredentials | undefined;
2639
+ headers: HeadersInit | undefined;
2640
+ }) => {
2641
+ body: object;
2642
+ headers?: HeadersInit;
2643
+ credentials?: RequestCredentials;
2644
+ };
2645
+
2646
+ declare class DefaultChatTransport<MESSAGE_METADATA, DATA_TYPES extends UIDataTypes> implements ChatTransport<MESSAGE_METADATA, DATA_TYPES> {
2647
+ private api;
2648
+ private credentials?;
2649
+ private headers?;
2650
+ private body?;
2651
+ private fetch?;
2652
+ private prepareRequest?;
2653
+ constructor({ api, credentials, headers, body, fetch, prepareRequest, }?: {
2654
+ api?: string;
2655
+ /**
2656
+ * The credentials mode to be used for the fetch request.
2657
+ * Possible values are: 'omit', 'same-origin', 'include'.
2658
+ * Defaults to 'same-origin'.
2659
+ */
2660
+ credentials?: RequestCredentials;
2661
+ /**
2662
+ * HTTP headers to be sent with the API request.
2663
+ */
2664
+ headers?: Record<string, string> | Headers;
2665
+ /**
2666
+ * Extra body object to be sent with the API request.
2667
+ * @example
2668
+ * Send a `sessionId` to the API along with the messages.
2669
+ * ```js
2670
+ * useChat({
2671
+ * body: {
2672
+ * sessionId: '123',
2673
+ * }
2674
+ * })
2675
+ * ```
2676
+ */
2677
+ body?: object;
2678
+ /**
2679
+ Custom fetch implementation. You can use it as a middleware to intercept requests,
2680
+ or to provide a custom fetch implementation for e.g. testing.
2681
+ */
2682
+ fetch?: FetchFunction;
2683
+ /**
2684
+ * When a function is provided, it will be used
2685
+ * to prepare the request body for the chat API. This can be useful for
2686
+ * customizing the request body based on the messages and data in the chat.
2687
+ *
2688
+ * @param id The id of the chat.
2689
+ * @param messages The current messages in the chat.
2690
+ * @param requestBody The request body object passed in the chat request.
2691
+ */
2692
+ prepareRequest?: PrepareRequest<MESSAGE_METADATA, DATA_TYPES>;
2693
+ });
2694
+ submitMessages({ chatId, messages, abortSignal, metadata, headers, body, requestType, }: Parameters<ChatTransport<MESSAGE_METADATA, DATA_TYPES>['submitMessages']>[0]): Promise<ReadableStream<UIMessageStreamPart>>;
2695
+ }
2696
+
2697
+ declare function getToolInvocations(message: UIMessage): ToolInvocation[];
2698
+
2699
+ declare class TextStreamChatTransport<MESSAGE_METADATA, DATA_TYPES extends UIDataTypes> implements ChatTransport<MESSAGE_METADATA, DATA_TYPES> {
2700
+ private api;
2701
+ private credentials?;
2702
+ private headers?;
2703
+ private body?;
2704
+ private fetch?;
2705
+ private prepareRequest?;
2706
+ constructor({ api, credentials, headers, body, fetch, prepareRequest, }: {
2707
+ api: string;
2708
+ /**
2709
+ * The credentials mode to be used for the fetch request.
2710
+ * Possible values are: 'omit', 'same-origin', 'include'.
2711
+ * Defaults to 'same-origin'.
2712
+ */
2713
+ credentials?: RequestCredentials;
2714
+ /**
2715
+ * HTTP headers to be sent with the API request.
2716
+ */
2717
+ headers?: Record<string, string> | Headers;
2718
+ /**
2719
+ * Extra body object to be sent with the API request.
2720
+ * @example
2721
+ * Send a `sessionId` to the API along with the messages.
2722
+ * ```js
2723
+ * useChat({
2724
+ * body: {
2725
+ * sessionId: '123',
2726
+ * }
2727
+ * })
2728
+ * ```
2729
+ */
2730
+ body?: object;
2731
+ /**
2732
+ Custom fetch implementation. You can use it as a middleware to intercept requests,
2733
+ or to provide a custom fetch implementation for e.g. testing.
2734
+ */
2735
+ fetch?: FetchFunction;
2736
+ /**
2737
+ * When a function is provided, it will be used
2738
+ * to prepare the request body for the chat API. This can be useful for
2739
+ * customizing the request body based on the messages and data in the chat.
2740
+ *
2741
+ * @param id The id of the chat.
2742
+ * @param messages The current messages in the chat.
2743
+ * @param requestBody The request body object passed in the chat request.
2744
+ */
2745
+ prepareRequest?: NoInfer<PrepareRequest<MESSAGE_METADATA, DATA_TYPES>>;
2746
+ });
2747
+ submitMessages({ chatId, messages, abortSignal, metadata, headers, body, requestType, }: Parameters<ChatTransport<MESSAGE_METADATA, DATA_TYPES>['submitMessages']>[0]): Promise<ReadableStream<UIMessageStreamPart<never, never>>>;
2748
+ }
2749
+
2750
+ type CompletionRequestOptions = {
2643
2751
  /**
2644
- Additional provider-specific metadata. They are passed through
2645
- from the provider to the AI SDK and enable provider-specific
2646
- results that can be fully encapsulated in the provider.
2752
+ An optional object of headers to be passed to the API endpoint.
2647
2753
  */
2648
- readonly providerMetadata: ProviderMetadata | undefined;
2754
+ headers?: Record<string, string> | Headers;
2649
2755
  /**
2650
- Details for all steps.
2651
- You can use this to get information about intermediate steps,
2652
- such as the tool calls or the response headers.
2756
+ An optional object to be passed to the API endpoint.
2757
+ */
2758
+ body?: object;
2759
+ };
2760
+ type UseCompletionOptions = {
2761
+ /**
2762
+ * The API endpoint that accepts a `{ prompt: string }` object and returns
2763
+ * a stream of tokens of the AI completion response. Defaults to `/api/completion`.
2653
2764
  */
2654
- readonly steps: Array<StepResult<TOOLS>>;
2765
+ api?: string;
2655
2766
  /**
2656
- The generated structured output. It uses the `experimental_output` specification.
2767
+ * An unique identifier for the chat. If not provided, a random one will be
2768
+ * generated. When provided, the `useChat` hook with the same `id` will
2769
+ * have shared states across components.
2657
2770
  */
2658
- readonly experimental_output: OUTPUT;
2659
- }
2771
+ id?: string;
2772
+ /**
2773
+ * Initial prompt input of the completion.
2774
+ */
2775
+ initialInput?: string;
2776
+ /**
2777
+ * Initial completion result. Useful to load an existing history.
2778
+ */
2779
+ initialCompletion?: string;
2780
+ /**
2781
+ * Callback function to be called when the completion is finished streaming.
2782
+ */
2783
+ onFinish?: (prompt: string, completion: string) => void;
2784
+ /**
2785
+ * Callback function to be called when an error is encountered.
2786
+ */
2787
+ onError?: (error: Error) => void;
2788
+ /**
2789
+ * The credentials mode to be used for the fetch request.
2790
+ * Possible values are: 'omit', 'same-origin', 'include'.
2791
+ * Defaults to 'same-origin'.
2792
+ */
2793
+ credentials?: RequestCredentials;
2794
+ /**
2795
+ * HTTP headers to be sent with the API request.
2796
+ */
2797
+ headers?: Record<string, string> | Headers;
2798
+ /**
2799
+ * Extra body object to be sent with the API request.
2800
+ * @example
2801
+ * Send a `sessionId` to the API along with the prompt.
2802
+ * ```js
2803
+ * useChat({
2804
+ * body: {
2805
+ * sessionId: '123',
2806
+ * }
2807
+ * })
2808
+ * ```
2809
+ */
2810
+ body?: object;
2811
+ /**
2812
+ Streaming protocol that is used. Defaults to `data`.
2813
+ */
2814
+ streamProtocol?: 'data' | 'text';
2815
+ /**
2816
+ Custom fetch implementation. You can use it as a middleware to intercept requests,
2817
+ or to provide a custom fetch implementation for e.g. testing.
2818
+ */
2819
+ fetch?: FetchFunction;
2820
+ };
2821
+
2822
+ /**
2823
+ * Calculates the cosine similarity between two vectors. This is a useful metric for
2824
+ * comparing the similarity of two vectors such as embeddings.
2825
+ *
2826
+ * @param vector1 - The first vector.
2827
+ * @param vector2 - The second vector.
2828
+ *
2829
+ * @returns The cosine similarity between vector1 and vector2.
2830
+ * @returns 0 if either vector is the zero vector.
2831
+ *
2832
+ * @throws {InvalidArgumentError} If the vectors do not have the same length.
2833
+ */
2834
+ declare function cosineSimilarity(vector1: number[], vector2: number[]): number;
2835
+
2836
+ /**
2837
+ * Converts a data URL of type text/* to a text string.
2838
+ */
2839
+ declare function getTextFromDataUrl(dataUrl: string): string;
2660
2840
 
2661
2841
  /**
2662
2842
  Create a type from an object with all keys and nested keys set to optional.
@@ -2675,566 +2855,470 @@ type PartialObject<ObjectType extends object> = {
2675
2855
  [KeyType in keyof ObjectType]?: DeepPartialInternal<ObjectType[KeyType]>;
2676
2856
  };
2677
2857
 
2678
- interface Output<OUTPUT, PARTIAL> {
2679
- readonly type: 'object' | 'text';
2680
- responseFormat: LanguageModelV2CallOptions['responseFormat'];
2681
- parsePartial(options: {
2682
- text: string;
2683
- }): Promise<{
2684
- partial: PARTIAL;
2685
- } | undefined>;
2686
- parseOutput(options: {
2687
- text: string;
2688
- }, context: {
2689
- response: LanguageModelResponseMetadata;
2690
- usage: LanguageModelUsage;
2691
- finishReason: FinishReason;
2692
- }): Promise<OUTPUT>;
2693
- }
2694
- declare const text: () => Output<string, string>;
2695
- declare const object: <OUTPUT>({ schema: inputSchema, }: {
2696
- schema: z4.$ZodType<OUTPUT, any> | z3.Schema<OUTPUT, z3.ZodTypeDef, any> | Schema<OUTPUT>;
2697
- }) => Output<OUTPUT, DeepPartial<OUTPUT>>;
2698
-
2699
- type output_Output<OUTPUT, PARTIAL> = Output<OUTPUT, PARTIAL>;
2700
- declare const output_object: typeof object;
2701
- declare const output_text: typeof text;
2702
- declare namespace output {
2703
- export {
2704
- output_Output as Output,
2705
- output_object as object,
2706
- output_text as text,
2707
- };
2708
- }
2709
-
2710
2858
  /**
2711
- Function that you can use to provide different settings for a step.
2859
+ * Performs a deep-equal comparison of two parsed JSON objects.
2860
+ *
2861
+ * @param {any} obj1 - The first object to compare.
2862
+ * @param {any} obj2 - The second object to compare.
2863
+ * @returns {boolean} - Returns true if the two objects are deeply equal, false otherwise.
2864
+ */
2865
+ declare function isDeepEqualData(obj1: any, obj2: any): boolean;
2712
2866
 
2713
- @param options - The options for the step.
2714
- @param options.steps - The steps that have been executed so far.
2715
- @param options.stepNumber - The number of the step that is being executed.
2716
- @param options.model - The model that is being used.
2867
+ declare function parsePartialJson(jsonText: string | undefined): Promise<{
2868
+ value: JSONValue$1 | undefined;
2869
+ state: 'undefined-input' | 'successful-parse' | 'repaired-parse' | 'failed-parse';
2870
+ }>;
2717
2871
 
2718
- @returns An object that contains the settings for the step.
2719
- If you return undefined (or for undefined settings), the settings from the outer level will be used.
2720
- */
2721
- type PrepareStepFunction<TOOLS extends Record<string, Tool> = Record<string, Tool>> = (options: {
2722
- steps: Array<StepResult<NoInfer<TOOLS>>>;
2723
- stepNumber: number;
2724
- model: LanguageModel;
2725
- }) => PromiseLike<PrepareStepResult<TOOLS>> | PrepareStepResult<TOOLS>;
2726
- type PrepareStepResult<TOOLS extends Record<string, Tool> = Record<string, Tool>> = {
2727
- model?: LanguageModel;
2728
- toolChoice?: ToolChoice<NoInfer<TOOLS>>;
2729
- activeTools?: Array<keyof NoInfer<TOOLS>>;
2730
- system?: string;
2731
- } | undefined;
2872
+ type Job = () => Promise<void>;
2732
2873
 
2733
- type StopCondition<TOOLS extends ToolSet> = (options: {
2734
- steps: Array<StepResult<TOOLS>>;
2735
- }) => PromiseLike<boolean> | boolean;
2736
- declare function stepCountIs(stepCount: number): StopCondition<any>;
2737
- declare function hasToolCall(toolName: string): StopCondition<any>;
2874
+ declare class SerialJobExecutor {
2875
+ private queue;
2876
+ private isProcessing;
2877
+ private processQueue;
2878
+ run(job: Job): Promise<void>;
2879
+ }
2738
2880
 
2739
2881
  /**
2740
- Callback that is set using the `onStepFinish` option.
2741
-
2742
- @param stepResult - The result of the step.
2882
+ * Creates a ReadableStream that emits the provided values with an optional delay between each value.
2883
+ *
2884
+ * @param options - The configuration options
2885
+ * @param options.chunks - Array of values to be emitted by the stream
2886
+ * @param options.initialDelayInMs - Optional initial delay in milliseconds before emitting the first value (default: 0). Can be set to `null` to skip the initial delay. The difference between `initialDelayInMs: null` and `initialDelayInMs: 0` is that `initialDelayInMs: null` will emit the values without any delay, while `initialDelayInMs: 0` will emit the values with a delay of 0 milliseconds.
2887
+ * @param options.chunkDelayInMs - Optional delay in milliseconds between emitting each value (default: 0). Can be set to `null` to skip the delay. The difference between `chunkDelayInMs: null` and `chunkDelayInMs: 0` is that `chunkDelayInMs: null` will emit the values without any delay, while `chunkDelayInMs: 0` will emit the values with a delay of 0 milliseconds.
2888
+ * @returns A ReadableStream that emits the provided values
2743
2889
  */
2744
- type GenerateTextOnStepFinishCallback<TOOLS extends ToolSet> = (stepResult: StepResult<TOOLS>) => Promise<void> | void;
2745
- /**
2746
- Generate a text and call tools for a given prompt using a language model.
2747
-
2748
- This function does not stream the output. If you want to stream the output, use `streamText` instead.
2749
-
2750
- @param model - The language model to use.
2890
+ declare function simulateReadableStream<T>({ chunks, initialDelayInMs, chunkDelayInMs, _internal, }: {
2891
+ chunks: T[];
2892
+ initialDelayInMs?: number | null;
2893
+ chunkDelayInMs?: number | null;
2894
+ _internal?: {
2895
+ delay?: (ms: number | null) => Promise<void>;
2896
+ };
2897
+ }): ReadableStream<T>;
2751
2898
 
2752
- @param tools - Tools that are accessible to and can be called by the model. The model needs to support calling tools.
2753
- @param toolChoice - The tool choice strategy. Default: 'auto'.
2899
+ /**
2900
+ The result of an `embed` call.
2901
+ It contains the embedding, the value, and additional information.
2902
+ */
2903
+ interface EmbedResult<VALUE> {
2904
+ /**
2905
+ The value that was embedded.
2906
+ */
2907
+ readonly value: VALUE;
2908
+ /**
2909
+ The embedding of the value.
2910
+ */
2911
+ readonly embedding: Embedding;
2912
+ /**
2913
+ The embedding token usage.
2914
+ */
2915
+ readonly usage: EmbeddingModelUsage;
2916
+ /**
2917
+ Optional response data.
2918
+ */
2919
+ readonly response?: {
2920
+ /**
2921
+ Response headers.
2922
+ */
2923
+ headers?: Record<string, string>;
2924
+ /**
2925
+ The response body.
2926
+ */
2927
+ body?: unknown;
2928
+ };
2929
+ }
2754
2930
 
2755
- @param system - A system message that will be part of the prompt.
2756
- @param prompt - A simple text prompt. You can either use `prompt` or `messages` but not both.
2757
- @param messages - A list of messages. You can either use `prompt` or `messages` but not both.
2931
+ /**
2932
+ Embed a value using an embedding model. The type of the value is defined by the embedding model.
2758
2933
 
2759
- @param maxOutputTokens - Maximum number of tokens to generate.
2760
- @param temperature - Temperature setting.
2761
- The value is passed through to the provider. The range depends on the provider and model.
2762
- It is recommended to set either `temperature` or `topP`, but not both.
2763
- @param topP - Nucleus sampling.
2764
- The value is passed through to the provider. The range depends on the provider and model.
2765
- It is recommended to set either `temperature` or `topP`, but not both.
2766
- @param topK - Only sample from the top K options for each subsequent token.
2767
- Used to remove "long tail" low probability responses.
2768
- Recommended for advanced use cases only. You usually only need to use temperature.
2769
- @param presencePenalty - Presence penalty setting.
2770
- It affects the likelihood of the model to repeat information that is already in the prompt.
2771
- The value is passed through to the provider. The range depends on the provider and model.
2772
- @param frequencyPenalty - Frequency penalty setting.
2773
- It affects the likelihood of the model to repeatedly use the same words or phrases.
2774
- The value is passed through to the provider. The range depends on the provider and model.
2775
- @param stopSequences - Stop sequences.
2776
- If set, the model will stop generating text when one of the stop sequences is generated.
2777
- @param seed - The seed (integer) to use for random sampling.
2778
- If set and supported by the model, calls will generate deterministic results.
2934
+ @param model - The embedding model to use.
2935
+ @param value - The value that should be embedded.
2779
2936
 
2780
2937
  @param maxRetries - Maximum number of retries. Set to 0 to disable retries. Default: 2.
2781
2938
  @param abortSignal - An optional abort signal that can be used to cancel the call.
2782
2939
  @param headers - Additional HTTP headers to be sent with the request. Only applicable for HTTP-based providers.
2783
2940
 
2784
- @param experimental_generateMessageId - Generate a unique ID for each message.
2785
-
2786
- @param onStepFinish - Callback that is called when each step (LLM call) is finished, including intermediate steps.
2787
-
2788
- @returns
2789
- A result object that contains the generated text, the results of the tool calls, and additional information.
2941
+ @returns A result object that contains the embedding, the value, and additional information.
2790
2942
  */
2791
- declare function generateText<TOOLS extends ToolSet, OUTPUT = never, OUTPUT_PARTIAL = never>({ model: modelArg, tools, toolChoice, system, prompt, messages, maxRetries: maxRetriesArg, abortSignal, headers, stopWhen, experimental_output: output, experimental_telemetry: telemetry, providerOptions, experimental_activeTools, activeTools, experimental_prepareStep, prepareStep, experimental_repairToolCall: repairToolCall, _internal: { generateId, currentDate, }, onStepFinish, ...settings }: CallSettings & Prompt & {
2792
- /**
2793
- The language model to use.
2794
- */
2795
- model: LanguageModel;
2943
+ declare function embed<VALUE>({ model, value, providerOptions, maxRetries: maxRetriesArg, abortSignal, headers, experimental_telemetry: telemetry, }: {
2796
2944
  /**
2797
- The tools that the model can call. The model needs to support calling tools.
2798
- */
2799
- tools?: TOOLS;
2945
+ The embedding model to use.
2946
+ */
2947
+ model: EmbeddingModel<VALUE>;
2800
2948
  /**
2801
- The tool choice strategy. Default: 'auto'.
2949
+ The value that should be embedded.
2802
2950
  */
2803
- toolChoice?: ToolChoice<NoInfer<TOOLS>>;
2951
+ value: VALUE;
2804
2952
  /**
2805
- Condition for stopping the generation when there are tool results in the last step.
2806
- When the condition is an array, any of the conditions can be met to stop the generation.
2807
-
2808
- @default stepCountIs(1)
2953
+ Maximum number of retries per embedding model call. Set to 0 to disable retries.
2954
+
2955
+ @default 2
2809
2956
  */
2810
- stopWhen?: StopCondition<NoInfer<TOOLS>> | Array<StopCondition<NoInfer<TOOLS>>>;
2957
+ maxRetries?: number;
2811
2958
  /**
2812
- Optional telemetry configuration (experimental).
2813
- */
2814
- experimental_telemetry?: TelemetrySettings;
2959
+ Abort signal.
2960
+ */
2961
+ abortSignal?: AbortSignal;
2815
2962
  /**
2816
- Additional provider-specific options. They are passed through
2817
- to the provider from the AI SDK and enable provider-specific
2818
- functionality that can be fully encapsulated in the provider.
2819
- */
2963
+ Additional headers to include in the request.
2964
+ Only applicable for HTTP-based providers.
2965
+ */
2966
+ headers?: Record<string, string>;
2967
+ /**
2968
+ Additional provider-specific options. They are passed through
2969
+ to the provider from the AI SDK and enable provider-specific
2970
+ functionality that can be fully encapsulated in the provider.
2971
+ */
2820
2972
  providerOptions?: ProviderOptions;
2821
2973
  /**
2822
- * @deprecated Use `activeTools` instead.
2974
+ * Optional telemetry configuration (experimental).
2823
2975
  */
2824
- experimental_activeTools?: Array<keyof NoInfer<TOOLS>>;
2976
+ experimental_telemetry?: TelemetrySettings;
2977
+ }): Promise<EmbedResult<VALUE>>;
2978
+
2979
+ /**
2980
+ The result of a `embedMany` call.
2981
+ It contains the embeddings, the values, and additional information.
2982
+ */
2983
+ interface EmbedManyResult<VALUE> {
2825
2984
  /**
2826
- Limits the tools that are available for the model to call without
2827
- changing the tool call and result types in the result.
2828
- */
2829
- activeTools?: Array<keyof NoInfer<TOOLS>>;
2985
+ The values that were embedded.
2986
+ */
2987
+ readonly values: Array<VALUE>;
2830
2988
  /**
2831
- Optional specification for parsing structured outputs from the LLM response.
2832
- */
2833
- experimental_output?: Output<OUTPUT, OUTPUT_PARTIAL>;
2989
+ The embeddings. They are in the same order as the values.
2990
+ */
2991
+ readonly embeddings: Array<Embedding>;
2834
2992
  /**
2835
- * @deprecated Use `prepareStep` instead.
2836
- */
2837
- experimental_prepareStep?: PrepareStepFunction<NoInfer<TOOLS>>;
2993
+ The embedding token usage.
2994
+ */
2995
+ readonly usage: EmbeddingModelUsage;
2838
2996
  /**
2839
- Optional function that you can use to provide different settings for a step.
2997
+ Optional raw response data.
2998
+ */
2999
+ readonly responses?: Array<{
3000
+ /**
3001
+ Response headers.
3002
+ */
3003
+ headers?: Record<string, string>;
3004
+ /**
3005
+ The response body.
2840
3006
  */
2841
- prepareStep?: PrepareStepFunction<NoInfer<TOOLS>>;
3007
+ body?: unknown;
3008
+ } | undefined>;
3009
+ }
3010
+
3011
+ /**
3012
+ Embed several values using an embedding model. The type of the value is defined
3013
+ by the embedding model.
3014
+
3015
+ `embedMany` automatically splits large requests into smaller chunks if the model
3016
+ has a limit on how many embeddings can be generated in a single call.
3017
+
3018
+ @param model - The embedding model to use.
3019
+ @param values - The values that should be embedded.
3020
+
3021
+ @param maxRetries - Maximum number of retries. Set to 0 to disable retries. Default: 2.
3022
+ @param abortSignal - An optional abort signal that can be used to cancel the call.
3023
+ @param headers - Additional HTTP headers to be sent with the request. Only applicable for HTTP-based providers.
3024
+
3025
+ @returns A result object that contains the embeddings, the value, and additional information.
3026
+ */
3027
+ declare function embedMany<VALUE>({ model, values, maxParallelCalls, maxRetries: maxRetriesArg, abortSignal, headers, providerOptions, experimental_telemetry: telemetry, }: {
2842
3028
  /**
2843
- A function that attempts to repair a tool call that failed to parse.
3029
+ The embedding model to use.
3030
+ */
3031
+ model: EmbeddingModel<VALUE>;
3032
+ /**
3033
+ The values that should be embedded.
2844
3034
  */
2845
- experimental_repairToolCall?: ToolCallRepairFunction<NoInfer<TOOLS>>;
3035
+ values: Array<VALUE>;
2846
3036
  /**
2847
- Callback that is called when each step (LLM call) is finished, including intermediate steps.
2848
- */
2849
- onStepFinish?: GenerateTextOnStepFinishCallback<NoInfer<TOOLS>>;
2850
- /**
2851
- * Internal. For test use only. May change without notice.
3037
+ Maximum number of retries per embedding model call. Set to 0 to disable retries.
3038
+
3039
+ @default 2
2852
3040
  */
2853
- _internal?: {
2854
- generateId?: IdGenerator;
2855
- currentDate?: () => Date;
2856
- };
2857
- }): Promise<GenerateTextResult<TOOLS, OUTPUT>>;
2858
-
2859
- type AsyncIterableStream<T> = AsyncIterable<T> & ReadableStream<T>;
2860
-
2861
- type UIMessageStreamOptions = {
3041
+ maxRetries?: number;
2862
3042
  /**
2863
- * Message ID that is sent to the client if a new message is created.
2864
- * This is intended to be used for the UI message,
2865
- * if the last original message is not an assistant message
2866
- * (in which case that message ID is used).
2867
- */
2868
- newMessageId?: string;
3043
+ Abort signal.
3044
+ */
3045
+ abortSignal?: AbortSignal;
2869
3046
  /**
2870
- * The original messages.
3047
+ Additional headers to include in the request.
3048
+ Only applicable for HTTP-based providers.
3049
+ */
3050
+ headers?: Record<string, string>;
3051
+ /**
3052
+ * Optional telemetry configuration (experimental).
2871
3053
  */
2872
- originalMessages?: UIMessage[];
2873
- onFinish?: (options: {
2874
- /**
2875
- * The updates list of UI messages.
2876
- */
2877
- messages: UIMessage[];
2878
- /**
2879
- * Indicates whether the response message is a continuation of the last original message,
2880
- * or if a new message was created.
2881
- */
2882
- isContinuation: boolean;
2883
- /**
2884
- * The message that was sent to the client as a response
2885
- * (including the original message if it was extended).
2886
- */
2887
- responseMessage: UIMessage;
2888
- }) => void;
3054
+ experimental_telemetry?: TelemetrySettings;
2889
3055
  /**
2890
- * Extracts message metadata that will be send to the client.
3056
+ Additional provider-specific options. They are passed through
3057
+ to the provider from the AI SDK and enable provider-specific
3058
+ functionality that can be fully encapsulated in the provider.
3059
+ */
3060
+ providerOptions?: ProviderOptions;
3061
+ /**
3062
+ * Maximum number of concurrent requests.
2891
3063
  *
2892
- * Called on `start` and `finish` events.
3064
+ * @default Infinity
2893
3065
  */
2894
- messageMetadata?: (options: {
2895
- part: TextStreamPart<ToolSet> & {
2896
- type: 'start' | 'finish' | 'start-step' | 'finish-step';
2897
- };
2898
- }) => unknown;
3066
+ maxParallelCalls?: number;
3067
+ }): Promise<EmbedManyResult<VALUE>>;
3068
+
3069
+ /**
3070
+ A message that was generated during the generation process.
3071
+ It can be either an assistant message or a tool message.
3072
+ */
3073
+ type ResponseMessage = AssistantModelMessage | ToolModelMessage;
3074
+
3075
+ /**
3076
+ * The result of a single step in the generation process.
3077
+ */
3078
+ type StepResult<TOOLS extends ToolSet> = {
2899
3079
  /**
2900
- * Send reasoning parts to the client.
2901
- * Default to false.
3080
+ The content that was generated in the last step.
2902
3081
  */
2903
- sendReasoning?: boolean;
3082
+ readonly content: Array<ContentPart<TOOLS>>;
2904
3083
  /**
2905
- * Send source parts to the client.
2906
- * Default to false.
2907
- */
2908
- sendSources?: boolean;
3084
+ The generated text.
3085
+ */
3086
+ readonly text: string;
2909
3087
  /**
2910
- * Send the finish event to the client.
2911
- * Set to false if you are using additional streamText calls
2912
- * that send additional data.
2913
- * Default to true.
2914
- */
2915
- sendFinish?: boolean;
3088
+ The reasoning that was generated during the generation.
3089
+ */
3090
+ readonly reasoning: Array<ReasoningPart>;
2916
3091
  /**
2917
- * Send the message start event to the client.
2918
- * Set to false if you are using additional streamText calls
2919
- * and the message start event has already been sent.
2920
- * Default to true.
2921
- *
2922
- * Note: this setting is currently not used, but you should
2923
- * already set it to false if you are using additional
2924
- * streamText calls that send additional data to prevent
2925
- * the message start event from being sent multiple times.
3092
+ The reasoning text that was generated during the generation.
3093
+ */
3094
+ readonly reasoningText: string | undefined;
3095
+ /**
3096
+ The files that were generated during the generation.
3097
+ */
3098
+ readonly files: Array<GeneratedFile>;
3099
+ /**
3100
+ The sources that were used to generate the text.
3101
+ */
3102
+ readonly sources: Array<Source>;
3103
+ /**
3104
+ The tool calls that were made during the generation.
3105
+ */
3106
+ readonly toolCalls: ToolCallArray<TOOLS>;
3107
+ /**
3108
+ The results of the tool calls.
3109
+ */
3110
+ readonly toolResults: ToolResultArray<TOOLS>;
3111
+ /**
3112
+ The reason why the generation finished.
3113
+ */
3114
+ readonly finishReason: FinishReason;
3115
+ /**
3116
+ The token usage of the generated text.
3117
+ */
3118
+ readonly usage: LanguageModelUsage;
3119
+ /**
3120
+ Warnings from the model provider (e.g. unsupported settings).
3121
+ */
3122
+ readonly warnings: CallWarning[] | undefined;
3123
+ /**
3124
+ Additional request information.
2926
3125
  */
2927
- sendStart?: boolean;
3126
+ readonly request: LanguageModelRequestMetadata;
2928
3127
  /**
2929
- * Process an error, e.g. to log it. Default to `() => 'An error occurred.'`.
2930
- *
2931
- * @return error message to include in the data stream.
3128
+ Additional response information.
3129
+ */
3130
+ readonly response: LanguageModelResponseMetadata & {
3131
+ /**
3132
+ The response messages that were generated during the call.
3133
+ Response messages can be either assistant messages or tool messages.
3134
+ They contain a generated id.
3135
+ */
3136
+ readonly messages: Array<ResponseMessage>;
3137
+ /**
3138
+ Response body (available only for providers that use HTTP requests).
3139
+ */
3140
+ body?: unknown;
3141
+ };
3142
+ /**
3143
+ Additional provider-specific metadata. They are passed through
3144
+ from the provider to the AI SDK and enable provider-specific
3145
+ results that can be fully encapsulated in the provider.
2932
3146
  */
2933
- onError?: (error: unknown) => string;
2934
- };
2935
- type ConsumeStreamOptions = {
2936
- onError?: (error: unknown) => void;
3147
+ readonly providerMetadata: ProviderMetadata | undefined;
2937
3148
  };
3149
+
2938
3150
  /**
2939
- A result object for accessing different stream types and additional information.
3151
+ The result of a `generateText` call.
3152
+ It contains the generated text, the tool calls that were made during the generation, and the results of the tool calls.
2940
3153
  */
2941
- interface StreamTextResult<TOOLS extends ToolSet, PARTIAL_OUTPUT> {
3154
+ interface GenerateTextResult<TOOLS extends ToolSet, OUTPUT> {
2942
3155
  /**
2943
3156
  The content that was generated in the last step.
2944
-
2945
- Resolved when the response is finished.
2946
3157
  */
2947
- readonly content: Promise<Array<ContentPart<TOOLS>>>;
3158
+ readonly content: Array<ContentPart<TOOLS>>;
2948
3159
  /**
2949
- The full text that has been generated by the last step.
2950
-
2951
- Resolved when the response is finished.
3160
+ The text that was generated in the last step.
2952
3161
  */
2953
- readonly text: Promise<string>;
3162
+ readonly text: string;
2954
3163
  /**
2955
- The full reasoning that the model has generated.
2956
-
2957
- Resolved when the response is finished.
3164
+ The full reasoning that the model has generated in the last step.
2958
3165
  */
2959
- readonly reasoning: Promise<Array<ReasoningPart>>;
2960
- /**
2961
- The reasoning that has been generated by the last step.
2962
-
2963
- Resolved when the response is finished.
2964
- */
2965
- readonly reasoningText: Promise<string | undefined>;
3166
+ readonly reasoning: Array<ReasoningPart>;
2966
3167
  /**
2967
- Files that have been generated by the model in the last step.
2968
-
2969
- Resolved when the response is finished.
3168
+ The reasoning text that the model has generated in the last step. Can be undefined if the model
3169
+ has only generated text.
2970
3170
  */
2971
- readonly files: Promise<GeneratedFile[]>;
3171
+ readonly reasoningText: string | undefined;
3172
+ /**
3173
+ The files that were generated in the last step.
3174
+ Empty array if no files were generated.
3175
+ */
3176
+ readonly files: Array<GeneratedFile>;
2972
3177
  /**
2973
3178
  Sources that have been used as references in the last step.
2974
-
2975
- Resolved when the response is finished.
2976
3179
  */
2977
- readonly sources: Promise<Source[]>;
3180
+ readonly sources: Array<Source>;
2978
3181
  /**
2979
- The tool calls that have been executed in the last step.
2980
-
2981
- Resolved when the response is finished.
2982
- */
2983
- readonly toolCalls: Promise<ToolCallUnion<TOOLS>[]>;
3182
+ The tool calls that were made in the last step.
3183
+ */
3184
+ readonly toolCalls: ToolCallArray<TOOLS>;
2984
3185
  /**
2985
- The tool results that have been generated in the last step.
2986
-
2987
- Resolved when the all tool executions are finished.
3186
+ The results of the tool calls from the last step.
2988
3187
  */
2989
- readonly toolResults: Promise<ToolResultUnion<TOOLS>[]>;
3188
+ readonly toolResults: ToolResultArray<TOOLS>;
2990
3189
  /**
2991
- The reason why the generation finished. Taken from the last step.
2992
-
2993
- Resolved when the response is finished.
2994
- */
2995
- readonly finishReason: Promise<FinishReason>;
3190
+ The reason why the generation finished.
3191
+ */
3192
+ readonly finishReason: FinishReason;
2996
3193
  /**
2997
3194
  The token usage of the last step.
2998
-
2999
- Resolved when the response is finished.
3000
3195
  */
3001
- readonly usage: Promise<LanguageModelUsage>;
3196
+ readonly usage: LanguageModelUsage;
3002
3197
  /**
3003
- The total token usage of the generated response.
3198
+ The total token usage of all steps.
3004
3199
  When there are multiple steps, the usage is the sum of all step usages.
3005
-
3006
- Resolved when the response is finished.
3007
- */
3008
- readonly totalUsage: Promise<LanguageModelUsage>;
3200
+ */
3201
+ readonly totalUsage: LanguageModelUsage;
3009
3202
  /**
3010
- Warnings from the model provider (e.g. unsupported settings) for the first step.
3011
- */
3012
- readonly warnings: Promise<CallWarning[] | undefined>;
3203
+ Warnings from the model provider (e.g. unsupported settings)
3204
+ */
3205
+ readonly warnings: CallWarning[] | undefined;
3013
3206
  /**
3014
- Details for all steps.
3015
- You can use this to get information about intermediate steps,
3016
- such as the tool calls or the response headers.
3207
+ Additional request information.
3017
3208
  */
3018
- readonly steps: Promise<Array<StepResult<TOOLS>>>;
3209
+ readonly request: LanguageModelRequestMetadata;
3019
3210
  /**
3020
- Additional request information from the last step.
3021
- */
3022
- readonly request: Promise<LanguageModelRequestMetadata>;
3023
- /**
3024
- Additional response information from the last step.
3025
- */
3026
- readonly response: Promise<LanguageModelResponseMetadata & {
3211
+ Additional response information.
3212
+ */
3213
+ readonly response: LanguageModelResponseMetadata & {
3027
3214
  /**
3028
- The response messages that were generated during the call. It consists of an assistant message,
3029
- potentially containing tool calls.
3030
-
3031
- When there are tool results, there is an additional tool message with the tool results that are available.
3032
- If there are tools that do not have execute functions, they are not included in the tool results and
3033
- need to be added separately.
3034
- */
3215
+ The response messages that were generated during the call. It consists of an assistant message,
3216
+ potentially containing tool calls.
3217
+
3218
+ When there are tool results, there is an additional tool message with the tool results that are available.
3219
+ If there are tools that do not have execute functions, they are not included in the tool results and
3220
+ need to be added separately.
3221
+ */
3035
3222
  messages: Array<ResponseMessage>;
3036
- }>;
3223
+ /**
3224
+ Response body (available only for providers that use HTTP requests).
3225
+ */
3226
+ body?: unknown;
3227
+ };
3037
3228
  /**
3038
- Additional provider-specific metadata from the last step.
3039
- Metadata is passed through from the provider to the AI SDK and
3040
- enables provider-specific results that can be fully encapsulated in the provider.
3229
+ Additional provider-specific metadata. They are passed through
3230
+ from the provider to the AI SDK and enable provider-specific
3231
+ results that can be fully encapsulated in the provider.
3041
3232
  */
3042
- readonly providerMetadata: Promise<ProviderMetadata | undefined>;
3043
- /**
3044
- A text stream that returns only the generated text deltas. You can use it
3045
- as either an AsyncIterable or a ReadableStream. When an error occurs, the
3046
- stream will throw the error.
3047
- */
3048
- readonly textStream: AsyncIterableStream<string>;
3049
- /**
3050
- A stream with all events, including text deltas, tool calls, tool results, and
3051
- errors.
3052
- You can use it as either an AsyncIterable or a ReadableStream.
3053
- Only errors that stop the stream, such as network errors, are thrown.
3054
- */
3055
- readonly fullStream: AsyncIterableStream<TextStreamPart<TOOLS>>;
3233
+ readonly providerMetadata: ProviderMetadata | undefined;
3056
3234
  /**
3057
- A stream of partial outputs. It uses the `experimental_output` specification.
3235
+ Details for all steps.
3236
+ You can use this to get information about intermediate steps,
3237
+ such as the tool calls or the response headers.
3058
3238
  */
3059
- readonly experimental_partialOutputStream: AsyncIterableStream<PARTIAL_OUTPUT>;
3060
- /**
3061
- Consumes the stream without processing the parts.
3062
- This is useful to force the stream to finish.
3063
- It effectively removes the backpressure and allows the stream to finish,
3064
- triggering the `onFinish` callback and the promise resolution.
3065
-
3066
- If an error occurs, it is passed to the optional `onError` callback.
3067
- */
3068
- consumeStream(options?: ConsumeStreamOptions): Promise<void>;
3069
- /**
3070
- Converts the result to a UI message stream.
3071
-
3072
- @param options.getErrorMessage an optional function that converts an error to an error message.
3073
- @param options.sendUsage whether to send the usage information to the client. Defaults to true.
3074
- @param options.sendReasoning whether to send the reasoning information to the client. Defaults to false.
3075
- @param options.sendSources whether to send the sources information to the client. Defaults to false.
3076
- @param options.experimental_sendFinish whether to send the finish information to the client. Defaults to true.
3077
- @param options.experimental_sendStart whether to send the start information to the client. Defaults to true.
3078
-
3079
- @return A UI message stream.
3080
- */
3081
- toUIMessageStream(options?: UIMessageStreamOptions): ReadableStream<UIMessageStreamPart>;
3082
- /**
3083
- Writes UI message stream output to a Node.js response-like object.
3084
- @param response A Node.js response-like object (ServerResponse).
3085
- @param options.status The status code.
3086
- @param options.statusText The status text.
3087
- @param options.headers The headers.
3088
- @param options.getErrorMessage An optional function that converts an error to an error message.
3089
- @param options.sendUsage Whether to send the usage information to the client. Defaults to true.
3090
- @param options.sendReasoning Whether to send the reasoning information to the client. Defaults to false.
3091
- */
3092
- pipeUIMessageStreamToResponse(response: ServerResponse, options?: ResponseInit & UIMessageStreamOptions): void;
3093
- /**
3094
- Writes text delta output to a Node.js response-like object.
3095
- It sets a `Content-Type` header to `text/plain; charset=utf-8` and
3096
- writes each text delta as a separate chunk.
3097
- @param response A Node.js response-like object (ServerResponse).
3098
- @param init Optional headers, status code, and status text.
3099
- */
3100
- pipeTextStreamToResponse(response: ServerResponse, init?: ResponseInit): void;
3101
- /**
3102
- Converts the result to a streamed response object with a stream data part stream.
3103
-
3104
- @param options.status The status code.
3105
- @param options.statusText The status text.
3106
- @param options.headers The headers.
3107
- @param options.getErrorMessage An optional function that converts an error to an error message.
3108
- @param options.sendUsage Whether to send the usage information to the client. Defaults to true.
3109
- @param options.sendReasoning Whether to send the reasoning information to the client. Defaults to false.
3110
- @return A response object.
3111
- */
3112
- toUIMessageStreamResponse(options?: ResponseInit & UIMessageStreamOptions): Response;
3239
+ readonly steps: Array<StepResult<TOOLS>>;
3113
3240
  /**
3114
- Creates a simple text stream response.
3115
- Each text delta is encoded as UTF-8 and sent as a separate chunk.
3116
- Non-text-delta events are ignored.
3117
- @param init Optional headers, status code, and status text.
3118
- */
3119
- toTextStreamResponse(init?: ResponseInit): Response;
3241
+ The generated structured output. It uses the `experimental_output` specification.
3242
+ */
3243
+ readonly experimental_output: OUTPUT;
3120
3244
  }
3121
- type TextStreamPart<TOOLS extends ToolSet> = ContentPart<TOOLS> | {
3122
- type: 'reasoning-part-finish';
3123
- } | {
3124
- type: 'tool-call-streaming-start';
3125
- toolCallId: string;
3126
- toolName: string;
3127
- } | {
3128
- type: 'tool-call-delta';
3129
- toolCallId: string;
3130
- toolName: string;
3131
- argsTextDelta: string;
3132
- } | {
3133
- type: 'start-step';
3134
- request: LanguageModelRequestMetadata;
3135
- warnings: CallWarning[];
3136
- } | {
3137
- type: 'finish-step';
3138
- response: LanguageModelResponseMetadata;
3139
- usage: LanguageModelUsage;
3140
- finishReason: FinishReason;
3141
- providerMetadata: ProviderMetadata | undefined;
3142
- } | {
3143
- type: 'start';
3144
- } | {
3145
- type: 'finish';
3146
- finishReason: FinishReason;
3147
- totalUsage: LanguageModelUsage;
3148
- } | {
3149
- type: 'error';
3150
- error: unknown;
3151
- };
3152
3245
 
3153
- /**
3154
- * Detects the first chunk in a buffer.
3155
- *
3156
- * @param buffer - The buffer to detect the first chunk in.
3157
- *
3158
- * @returns The first detected chunk, or `undefined` if no chunk was detected.
3159
- */
3160
- type ChunkDetector = (buffer: string) => string | undefined | null;
3161
- /**
3162
- * Smooths text streaming output.
3163
- *
3164
- * @param delayInMs - The delay in milliseconds between each chunk. Defaults to 10ms. Can be set to `null` to skip the delay.
3165
- * @param chunking - Controls how the text is chunked for streaming. Use "word" to stream word by word (default), "line" to stream line by line, or provide a custom RegExp pattern for custom chunking.
3166
- *
3167
- * @returns A transform stream that smooths text streaming output.
3168
- */
3169
- declare function smoothStream<TOOLS extends ToolSet>({ delayInMs, chunking, _internal: { delay }, }?: {
3170
- delayInMs?: number | null;
3171
- chunking?: 'word' | 'line' | RegExp | ChunkDetector;
3172
- /**
3173
- * Internal. For test use only. May change without notice.
3174
- */
3175
- _internal?: {
3176
- delay?: (delayInMs: number | null) => Promise<void>;
3177
- };
3178
- }): (options: {
3179
- tools: TOOLS;
3180
- }) => TransformStream<TextStreamPart<TOOLS>, TextStreamPart<TOOLS>>;
3246
+ interface Output<OUTPUT, PARTIAL> {
3247
+ readonly type: 'object' | 'text';
3248
+ responseFormat: LanguageModelV2CallOptions['responseFormat'];
3249
+ parsePartial(options: {
3250
+ text: string;
3251
+ }): Promise<{
3252
+ partial: PARTIAL;
3253
+ } | undefined>;
3254
+ parseOutput(options: {
3255
+ text: string;
3256
+ }, context: {
3257
+ response: LanguageModelResponseMetadata;
3258
+ usage: LanguageModelUsage;
3259
+ finishReason: FinishReason;
3260
+ }): Promise<OUTPUT>;
3261
+ }
3262
+ declare const text: () => Output<string, string>;
3263
+ declare const object: <OUTPUT>({ schema: inputSchema, }: {
3264
+ schema: z4.$ZodType<OUTPUT, any> | z3.Schema<OUTPUT, z3.ZodTypeDef, any> | Schema<OUTPUT>;
3265
+ }) => Output<OUTPUT, DeepPartial<OUTPUT>>;
3181
3266
 
3182
- /**
3183
- A transformation that is applied to the stream.
3267
+ type output_Output<OUTPUT, PARTIAL> = Output<OUTPUT, PARTIAL>;
3268
+ declare const output_object: typeof object;
3269
+ declare const output_text: typeof text;
3270
+ declare namespace output {
3271
+ export {
3272
+ output_Output as Output,
3273
+ output_object as object,
3274
+ output_text as text,
3275
+ };
3276
+ }
3184
3277
 
3185
- @param stopStream - A function that stops the source stream.
3186
- @param tools - The tools that are accessible to and can be called by the model. The model needs to support calling tools.
3187
- */
3188
- type StreamTextTransform<TOOLS extends ToolSet> = (options: {
3189
- tools: TOOLS;
3190
- stopStream: () => void;
3191
- }) => TransformStream<TextStreamPart<TOOLS>, TextStreamPart<TOOLS>>;
3192
3278
  /**
3193
- Callback that is set using the `onError` option.
3279
+ Function that you can use to provide different settings for a step.
3280
+
3281
+ @param options - The options for the step.
3282
+ @param options.steps - The steps that have been executed so far.
3283
+ @param options.stepNumber - The number of the step that is being executed.
3284
+ @param options.model - The model that is being used.
3285
+
3286
+ @returns An object that contains the settings for the step.
3287
+ If you return undefined (or for undefined settings), the settings from the outer level will be used.
3288
+ */
3289
+ type PrepareStepFunction<TOOLS extends Record<string, Tool> = Record<string, Tool>> = (options: {
3290
+ steps: Array<StepResult<NoInfer<TOOLS>>>;
3291
+ stepNumber: number;
3292
+ model: LanguageModel;
3293
+ }) => PromiseLike<PrepareStepResult<TOOLS>> | PrepareStepResult<TOOLS>;
3294
+ type PrepareStepResult<TOOLS extends Record<string, Tool> = Record<string, Tool>> = {
3295
+ model?: LanguageModel;
3296
+ toolChoice?: ToolChoice<NoInfer<TOOLS>>;
3297
+ activeTools?: Array<keyof NoInfer<TOOLS>>;
3298
+ system?: string;
3299
+ } | undefined;
3300
+
3301
+ type StopCondition<TOOLS extends ToolSet> = (options: {
3302
+ steps: Array<StepResult<TOOLS>>;
3303
+ }) => PromiseLike<boolean> | boolean;
3304
+ declare function stepCountIs(stepCount: number): StopCondition<any>;
3305
+ declare function hasToolCall(toolName: string): StopCondition<any>;
3194
3306
 
3195
- @param event - The event that is passed to the callback.
3196
- */
3197
- type StreamTextOnErrorCallback = (event: {
3198
- error: unknown;
3199
- }) => Promise<void> | void;
3200
3307
  /**
3201
3308
  Callback that is set using the `onStepFinish` option.
3202
3309
 
3203
3310
  @param stepResult - The result of the step.
3204
3311
  */
3205
- type StreamTextOnStepFinishCallback<TOOLS extends ToolSet> = (stepResult: StepResult<TOOLS>) => Promise<void> | void;
3312
+ type GenerateTextOnStepFinishCallback<TOOLS extends ToolSet> = (stepResult: StepResult<TOOLS>) => Promise<void> | void;
3206
3313
  /**
3207
- Callback that is set using the `onChunk` option.
3314
+ Generate a text and call tools for a given prompt using a language model.
3208
3315
 
3209
- @param event - The event that is passed to the callback.
3210
- */
3211
- type StreamTextOnChunkCallback<TOOLS extends ToolSet> = (event: {
3212
- chunk: Extract<TextStreamPart<TOOLS>, {
3213
- type: 'text' | 'reasoning' | 'source' | 'tool-call' | 'tool-call-streaming-start' | 'tool-call-delta' | 'tool-result';
3214
- }>;
3215
- }) => Promise<void> | void;
3216
- /**
3217
- Callback that is set using the `onFinish` option.
3218
-
3219
- @param event - The event that is passed to the callback.
3220
- */
3221
- type StreamTextOnFinishCallback<TOOLS extends ToolSet> = (event: StepResult<TOOLS> & {
3222
- /**
3223
- Details for all steps.
3224
- */
3225
- readonly steps: StepResult<TOOLS>[];
3226
- /**
3227
- Total usage for all steps. This is the sum of the usage of all steps.
3228
- */
3229
- readonly totalUsage: LanguageModelUsage;
3230
- }) => Promise<void> | void;
3231
- /**
3232
- Generate a text and call tools for a given prompt using a language model.
3233
-
3234
- This function streams the output. If you do not want to stream the output, use `generateText` instead.
3316
+ This function does not stream the output. If you want to stream the output, use `streamText` instead.
3235
3317
 
3236
3318
  @param model - The language model to use.
3319
+
3237
3320
  @param tools - Tools that are accessible to and can be called by the model. The model needs to support calling tools.
3321
+ @param toolChoice - The tool choice strategy. Default: 'auto'.
3238
3322
 
3239
3323
  @param system - A system message that will be part of the prompt.
3240
3324
  @param prompt - A simple text prompt. You can either use `prompt` or `messages` but not both.
@@ -3265,30 +3349,26 @@ If set and supported by the model, calls will generate deterministic results.
3265
3349
  @param abortSignal - An optional abort signal that can be used to cancel the call.
3266
3350
  @param headers - Additional HTTP headers to be sent with the request. Only applicable for HTTP-based providers.
3267
3351
 
3268
- @param maxSteps - Maximum number of sequential LLM calls (steps), e.g. when you use tool calls.
3352
+ @param experimental_generateMessageId - Generate a unique ID for each message.
3269
3353
 
3270
- @param onChunk - Callback that is called for each chunk of the stream. The stream processing will pause until the callback promise is resolved.
3271
- @param onError - Callback that is called when an error occurs during streaming. You can use it to log errors.
3272
3354
  @param onStepFinish - Callback that is called when each step (LLM call) is finished, including intermediate steps.
3273
- @param onFinish - Callback that is called when the LLM response and all request tool executions
3274
- (for tools that have an `execute` function) are finished.
3275
3355
 
3276
- @return
3277
- A result object for accessing different stream types and additional information.
3356
+ @returns
3357
+ A result object that contains the generated text, the results of the tool calls, and additional information.
3278
3358
  */
3279
- declare function streamText<TOOLS extends ToolSet, OUTPUT = never, PARTIAL_OUTPUT = never>({ model, tools, toolChoice, system, prompt, messages, maxRetries, abortSignal, headers, stopWhen, experimental_output: output, experimental_telemetry: telemetry, prepareStep, providerOptions, experimental_toolCallStreaming, toolCallStreaming, experimental_activeTools, activeTools, experimental_repairToolCall: repairToolCall, experimental_transform: transform, onChunk, onError, onFinish, onStepFinish, _internal: { now, generateId, currentDate, }, ...settings }: CallSettings & Prompt & {
3359
+ declare function generateText<TOOLS extends ToolSet, OUTPUT = never, OUTPUT_PARTIAL = never>({ model: modelArg, tools, toolChoice, system, prompt, messages, maxRetries: maxRetriesArg, abortSignal, headers, stopWhen, experimental_output: output, experimental_telemetry: telemetry, providerOptions, experimental_activeTools, activeTools, experimental_prepareStep, prepareStep, experimental_repairToolCall: repairToolCall, _internal: { generateId, currentDate, }, onStepFinish, ...settings }: CallSettings & Prompt & {
3280
3360
  /**
3281
3361
  The language model to use.
3282
3362
  */
3283
3363
  model: LanguageModel;
3284
3364
  /**
3285
3365
  The tools that the model can call. The model needs to support calling tools.
3286
- */
3366
+ */
3287
3367
  tools?: TOOLS;
3288
3368
  /**
3289
3369
  The tool choice strategy. Default: 'auto'.
3290
3370
  */
3291
- toolChoice?: ToolChoice<TOOLS>;
3371
+ toolChoice?: ToolChoice<NoInfer<TOOLS>>;
3292
3372
  /**
3293
3373
  Condition for stopping the generation when there are tool results in the last step.
3294
3374
  When the condition is an array, any of the conditions can be met to stop the generation.
@@ -3311,261 +3391,437 @@ functionality that can be fully encapsulated in the provider.
3311
3391
  */
3312
3392
  experimental_activeTools?: Array<keyof NoInfer<TOOLS>>;
3313
3393
  /**
3314
- Limits the tools that are available for the model to call without
3315
- changing the tool call and result types in the result.
3316
- */
3394
+ Limits the tools that are available for the model to call without
3395
+ changing the tool call and result types in the result.
3396
+ */
3317
3397
  activeTools?: Array<keyof NoInfer<TOOLS>>;
3318
3398
  /**
3319
3399
  Optional specification for parsing structured outputs from the LLM response.
3320
3400
  */
3321
- experimental_output?: Output<OUTPUT, PARTIAL_OUTPUT>;
3401
+ experimental_output?: Output<OUTPUT, OUTPUT_PARTIAL>;
3402
+ /**
3403
+ * @deprecated Use `prepareStep` instead.
3404
+ */
3405
+ experimental_prepareStep?: PrepareStepFunction<NoInfer<TOOLS>>;
3322
3406
  /**
3323
3407
  Optional function that you can use to provide different settings for a step.
3324
-
3325
- @param options - The options for the step.
3326
- @param options.steps - The steps that have been executed so far.
3327
- @param options.stepNumber - The number of the step that is being executed.
3328
- @param options.model - The model that is being used.
3329
-
3330
- @returns An object that contains the settings for the step.
3331
- If you return undefined (or for undefined settings), the settings from the outer level will be used.
3332
3408
  */
3333
3409
  prepareStep?: PrepareStepFunction<NoInfer<TOOLS>>;
3334
3410
  /**
3335
3411
  A function that attempts to repair a tool call that failed to parse.
3336
3412
  */
3337
- experimental_repairToolCall?: ToolCallRepairFunction<TOOLS>;
3413
+ experimental_repairToolCall?: ToolCallRepairFunction<NoInfer<TOOLS>>;
3414
+ /**
3415
+ Callback that is called when each step (LLM call) is finished, including intermediate steps.
3416
+ */
3417
+ onStepFinish?: GenerateTextOnStepFinishCallback<NoInfer<TOOLS>>;
3338
3418
  /**
3339
- Enable streaming of tool call deltas as they are generated. Disabled by default.
3419
+ * Internal. For test use only. May change without notice.
3340
3420
  */
3341
- toolCallStreaming?: boolean;
3421
+ _internal?: {
3422
+ generateId?: IdGenerator;
3423
+ currentDate?: () => Date;
3424
+ };
3425
+ }): Promise<GenerateTextResult<TOOLS, OUTPUT>>;
3426
+
3427
+ type AsyncIterableStream<T> = AsyncIterable<T> & ReadableStream<T>;
3428
+
3429
+ type UIMessageStreamOptions<UI_MESSAGE extends UIMessage> = {
3342
3430
  /**
3343
- @deprecated Use `toolCallStreaming` instead.
3431
+ * Message ID that is sent to the client if a new message is created.
3432
+ * This is intended to be used for the UI message,
3433
+ * if the last original message is not an assistant message
3434
+ * (in which case that message ID is used).
3344
3435
  */
3345
- experimental_toolCallStreaming?: boolean;
3436
+ newMessageId?: string;
3346
3437
  /**
3347
- Optional stream transformations.
3348
- They are applied in the order they are provided.
3349
- The stream transformations must maintain the stream structure for streamText to work correctly.
3438
+ * The original messages.
3350
3439
  */
3351
- experimental_transform?: StreamTextTransform<TOOLS> | Array<StreamTextTransform<TOOLS>>;
3440
+ originalMessages?: UI_MESSAGE[];
3441
+ onFinish?: (options: {
3442
+ /**
3443
+ * The updates list of UI messages.
3444
+ */
3445
+ messages: UI_MESSAGE[];
3446
+ /**
3447
+ * Indicates whether the response message is a continuation of the last original message,
3448
+ * or if a new message was created.
3449
+ */
3450
+ isContinuation: boolean;
3451
+ /**
3452
+ * The message that was sent to the client as a response
3453
+ * (including the original message if it was extended).
3454
+ */
3455
+ responseMessage: UI_MESSAGE;
3456
+ }) => void;
3352
3457
  /**
3353
- Callback that is called for each chunk of the stream.
3354
- The stream processing will pause until the callback promise is resolved.
3458
+ * Extracts message metadata that will be send to the client.
3459
+ *
3460
+ * Called on `start` and `finish` events.
3355
3461
  */
3356
- onChunk?: StreamTextOnChunkCallback<TOOLS>;
3462
+ messageMetadata?: (options: {
3463
+ part: TextStreamPart<ToolSet> & {
3464
+ type: 'start' | 'finish' | 'start-step' | 'finish-step';
3465
+ };
3466
+ }) => InferUIMessageMetadata<UI_MESSAGE> | undefined;
3357
3467
  /**
3358
- Callback that is invoked when an error occurs during streaming.
3359
- You can use it to log errors.
3360
- The stream processing will pause until the callback promise is resolved.
3468
+ * Send reasoning parts to the client.
3469
+ * Default to false.
3361
3470
  */
3362
- onError?: StreamTextOnErrorCallback;
3471
+ sendReasoning?: boolean;
3363
3472
  /**
3364
- Callback that is called when the LLM response and all request tool executions
3365
- (for tools that have an `execute` function) are finished.
3366
-
3367
- The usage is the combined usage of all steps.
3473
+ * Send source parts to the client.
3474
+ * Default to false.
3368
3475
  */
3369
- onFinish?: StreamTextOnFinishCallback<TOOLS>;
3476
+ sendSources?: boolean;
3370
3477
  /**
3371
- Callback that is called when each step (LLM call) is finished, including intermediate steps.
3372
- */
3373
- onStepFinish?: StreamTextOnStepFinishCallback<TOOLS>;
3478
+ * Send the finish event to the client.
3479
+ * Set to false if you are using additional streamText calls
3480
+ * that send additional data.
3481
+ * Default to true.
3482
+ */
3483
+ sendFinish?: boolean;
3374
3484
  /**
3375
- Internal. For test use only. May change without notice.
3485
+ * Send the message start event to the client.
3486
+ * Set to false if you are using additional streamText calls
3487
+ * and the message start event has already been sent.
3488
+ * Default to true.
3489
+ *
3490
+ * Note: this setting is currently not used, but you should
3491
+ * already set it to false if you are using additional
3492
+ * streamText calls that send additional data to prevent
3493
+ * the message start event from being sent multiple times.
3376
3494
  */
3377
- _internal?: {
3378
- now?: () => number;
3379
- generateId?: IdGenerator;
3380
- currentDate?: () => Date;
3381
- };
3382
- }): StreamTextResult<TOOLS, PARTIAL_OUTPUT>;
3383
-
3495
+ sendStart?: boolean;
3496
+ /**
3497
+ * Process an error, e.g. to log it. Default to `() => 'An error occurred.'`.
3498
+ *
3499
+ * @return error message to include in the data stream.
3500
+ */
3501
+ onError?: (error: unknown) => string;
3502
+ };
3503
+ type ConsumeStreamOptions = {
3504
+ onError?: (error: unknown) => void;
3505
+ };
3384
3506
  /**
3385
- The result of a `generateImage` call.
3386
- It contains the images and additional information.
3507
+ A result object for accessing different stream types and additional information.
3387
3508
  */
3388
- interface GenerateImageResult {
3509
+ interface StreamTextResult<TOOLS extends ToolSet, PARTIAL_OUTPUT> {
3389
3510
  /**
3390
- The first image that was generated.
3511
+ The content that was generated in the last step.
3512
+
3513
+ Resolved when the response is finished.
3391
3514
  */
3392
- readonly image: GeneratedFile;
3393
- /**
3394
- The images that were generated.
3395
- */
3396
- readonly images: Array<GeneratedFile>;
3515
+ readonly content: Promise<Array<ContentPart<TOOLS>>>;
3397
3516
  /**
3398
- Warnings for the call, e.g. unsupported settings.
3517
+ The full text that has been generated by the last step.
3518
+
3519
+ Resolved when the response is finished.
3399
3520
  */
3400
- readonly warnings: Array<ImageGenerationWarning>;
3401
- /**
3402
- Response metadata from the provider. There may be multiple responses if we made multiple calls to the model.
3403
- */
3404
- readonly responses: Array<ImageModelResponseMetadata>;
3521
+ readonly text: Promise<string>;
3405
3522
  /**
3406
- * Provider-specific metadata. They are passed through from the provider to the AI SDK and enable provider-specific
3407
- * results that can be fully encapsulated in the provider.
3523
+ The full reasoning that the model has generated.
3524
+
3525
+ Resolved when the response is finished.
3408
3526
  */
3409
- readonly providerMetadata: ImageModelProviderMetadata;
3410
- }
3411
-
3412
- /**
3413
- Generates images using an image model.
3414
-
3415
- @param model - The image model to use.
3416
- @param prompt - The prompt that should be used to generate the image.
3417
- @param n - Number of images to generate. Default: 1.
3418
- @param size - Size of the images to generate. Must have the format `{width}x{height}`.
3419
- @param aspectRatio - Aspect ratio of the images to generate. Must have the format `{width}:{height}`.
3420
- @param seed - Seed for the image generation.
3421
- @param providerOptions - Additional provider-specific options that are passed through to the provider
3422
- as body parameters.
3423
- @param maxRetries - Maximum number of retries. Set to 0 to disable retries. Default: 2.
3424
- @param abortSignal - An optional abort signal that can be used to cancel the call.
3425
- @param headers - Additional HTTP headers to be sent with the request. Only applicable for HTTP-based providers.
3426
-
3427
- @returns A result object that contains the generated images.
3428
- */
3429
- declare function generateImage({ model, prompt, n, maxImagesPerCall, size, aspectRatio, seed, providerOptions, maxRetries: maxRetriesArg, abortSignal, headers, }: {
3527
+ readonly reasoning: Promise<Array<ReasoningPart>>;
3430
3528
  /**
3431
- The image model to use.
3529
+ The reasoning that has been generated by the last step.
3530
+
3531
+ Resolved when the response is finished.
3432
3532
  */
3433
- model: ImageModelV2;
3533
+ readonly reasoningText: Promise<string | undefined>;
3434
3534
  /**
3435
- The prompt that should be used to generate the image.
3535
+ Files that have been generated by the model in the last step.
3536
+
3537
+ Resolved when the response is finished.
3436
3538
  */
3437
- prompt: string;
3539
+ readonly files: Promise<GeneratedFile[]>;
3438
3540
  /**
3439
- Number of images to generate.
3541
+ Sources that have been used as references in the last step.
3542
+
3543
+ Resolved when the response is finished.
3440
3544
  */
3441
- n?: number;
3545
+ readonly sources: Promise<Source[]>;
3442
3546
  /**
3443
- Number of images to generate.
3444
- */
3445
- maxImagesPerCall?: number;
3547
+ The tool calls that have been executed in the last step.
3548
+
3549
+ Resolved when the response is finished.
3550
+ */
3551
+ readonly toolCalls: Promise<ToolCallUnion<TOOLS>[]>;
3446
3552
  /**
3447
- Size of the images to generate. Must have the format `{width}x{height}`. If not provided, the default size will be used.
3553
+ The tool results that have been generated in the last step.
3554
+
3555
+ Resolved when the all tool executions are finished.
3448
3556
  */
3449
- size?: `${number}x${number}`;
3557
+ readonly toolResults: Promise<ToolResultUnion<TOOLS>[]>;
3450
3558
  /**
3451
- Aspect ratio of the images to generate. Must have the format `{width}:{height}`. If not provided, the default aspect ratio will be used.
3452
- */
3453
- aspectRatio?: `${number}:${number}`;
3559
+ The reason why the generation finished. Taken from the last step.
3560
+
3561
+ Resolved when the response is finished.
3562
+ */
3563
+ readonly finishReason: Promise<FinishReason>;
3454
3564
  /**
3455
- Seed for the image generation. If not provided, the default seed will be used.
3565
+ The token usage of the last step.
3566
+
3567
+ Resolved when the response is finished.
3456
3568
  */
3457
- seed?: number;
3569
+ readonly usage: Promise<LanguageModelUsage>;
3458
3570
  /**
3459
- Additional provider-specific options that are passed through to the provider
3460
- as body parameters.
3571
+ The total token usage of the generated response.
3572
+ When there are multiple steps, the usage is the sum of all step usages.
3461
3573
 
3462
- The outer record is keyed by the provider name, and the inner
3463
- record is keyed by the provider-specific metadata key.
3464
- ```ts
3465
- {
3466
- "openai": {
3467
- "style": "vivid"
3468
- }
3469
- }
3470
- ```
3574
+ Resolved when the response is finished.
3471
3575
  */
3472
- providerOptions?: ProviderOptions;
3576
+ readonly totalUsage: Promise<LanguageModelUsage>;
3473
3577
  /**
3474
- Maximum number of retries per embedding model call. Set to 0 to disable retries.
3475
-
3476
- @default 2
3578
+ Warnings from the model provider (e.g. unsupported settings) for the first step.
3579
+ */
3580
+ readonly warnings: Promise<CallWarning[] | undefined>;
3581
+ /**
3582
+ Details for all steps.
3583
+ You can use this to get information about intermediate steps,
3584
+ such as the tool calls or the response headers.
3477
3585
  */
3478
- maxRetries?: number;
3586
+ readonly steps: Promise<Array<StepResult<TOOLS>>>;
3479
3587
  /**
3480
- Abort signal.
3588
+ Additional request information from the last step.
3481
3589
  */
3482
- abortSignal?: AbortSignal;
3590
+ readonly request: Promise<LanguageModelRequestMetadata>;
3483
3591
  /**
3484
- Additional headers to include in the request.
3485
- Only applicable for HTTP-based providers.
3592
+ Additional response information from the last step.
3486
3593
  */
3487
- headers?: Record<string, string>;
3488
- }): Promise<GenerateImageResult>;
3489
-
3490
- /**
3491
- The result of a `generateObject` call.
3492
- */
3493
- interface GenerateObjectResult<OBJECT> {
3594
+ readonly response: Promise<LanguageModelResponseMetadata & {
3595
+ /**
3596
+ The response messages that were generated during the call. It consists of an assistant message,
3597
+ potentially containing tool calls.
3598
+
3599
+ When there are tool results, there is an additional tool message with the tool results that are available.
3600
+ If there are tools that do not have execute functions, they are not included in the tool results and
3601
+ need to be added separately.
3602
+ */
3603
+ messages: Array<ResponseMessage>;
3604
+ }>;
3494
3605
  /**
3495
- The generated object (typed according to the schema).
3496
- */
3497
- readonly object: OBJECT;
3606
+ Additional provider-specific metadata from the last step.
3607
+ Metadata is passed through from the provider to the AI SDK and
3608
+ enables provider-specific results that can be fully encapsulated in the provider.
3609
+ */
3610
+ readonly providerMetadata: Promise<ProviderMetadata | undefined>;
3498
3611
  /**
3499
- The reason why the generation finished.
3612
+ A text stream that returns only the generated text deltas. You can use it
3613
+ as either an AsyncIterable or a ReadableStream. When an error occurs, the
3614
+ stream will throw the error.
3500
3615
  */
3501
- readonly finishReason: FinishReason;
3616
+ readonly textStream: AsyncIterableStream<string>;
3502
3617
  /**
3503
- The token usage of the generated text.
3618
+ A stream with all events, including text deltas, tool calls, tool results, and
3619
+ errors.
3620
+ You can use it as either an AsyncIterable or a ReadableStream.
3621
+ Only errors that stop the stream, such as network errors, are thrown.
3504
3622
  */
3505
- readonly usage: LanguageModelUsage;
3623
+ readonly fullStream: AsyncIterableStream<TextStreamPart<TOOLS>>;
3506
3624
  /**
3507
- Warnings from the model provider (e.g. unsupported settings).
3625
+ A stream of partial outputs. It uses the `experimental_output` specification.
3626
+ */
3627
+ readonly experimental_partialOutputStream: AsyncIterableStream<PARTIAL_OUTPUT>;
3628
+ /**
3629
+ Consumes the stream without processing the parts.
3630
+ This is useful to force the stream to finish.
3631
+ It effectively removes the backpressure and allows the stream to finish,
3632
+ triggering the `onFinish` callback and the promise resolution.
3633
+
3634
+ If an error occurs, it is passed to the optional `onError` callback.
3635
+ */
3636
+ consumeStream(options?: ConsumeStreamOptions): Promise<void>;
3637
+ /**
3638
+ Converts the result to a UI message stream.
3639
+
3640
+ @param options.getErrorMessage an optional function that converts an error to an error message.
3641
+ @param options.sendUsage whether to send the usage information to the client. Defaults to true.
3642
+ @param options.sendReasoning whether to send the reasoning information to the client. Defaults to false.
3643
+ @param options.sendSources whether to send the sources information to the client. Defaults to false.
3644
+ @param options.experimental_sendFinish whether to send the finish information to the client. Defaults to true.
3645
+ @param options.experimental_sendStart whether to send the start information to the client. Defaults to true.
3646
+
3647
+ @return A UI message stream.
3508
3648
  */
3509
- readonly warnings: CallWarning[] | undefined;
3649
+ toUIMessageStream<UI_MESSAGE extends UIMessage>(options?: UIMessageStreamOptions<UI_MESSAGE>): ReadableStream<InferUIMessageStreamPart<UI_MESSAGE>>;
3510
3650
  /**
3511
- Additional request information.
3512
- */
3513
- readonly request: LanguageModelRequestMetadata;
3651
+ Writes UI message stream output to a Node.js response-like object.
3652
+ @param response A Node.js response-like object (ServerResponse).
3653
+ @param options.status The status code.
3654
+ @param options.statusText The status text.
3655
+ @param options.headers The headers.
3656
+ @param options.getErrorMessage An optional function that converts an error to an error message.
3657
+ @param options.sendUsage Whether to send the usage information to the client. Defaults to true.
3658
+ @param options.sendReasoning Whether to send the reasoning information to the client. Defaults to false.
3659
+ */
3660
+ pipeUIMessageStreamToResponse<UI_MESSAGE extends UIMessage>(response: ServerResponse, options?: ResponseInit & UIMessageStreamOptions<UI_MESSAGE>): void;
3514
3661
  /**
3515
- Additional response information.
3516
- */
3517
- readonly response: LanguageModelResponseMetadata & {
3518
- /**
3519
- Response body (available only for providers that use HTTP requests).
3520
- */
3521
- body?: unknown;
3522
- };
3662
+ Writes text delta output to a Node.js response-like object.
3663
+ It sets a `Content-Type` header to `text/plain; charset=utf-8` and
3664
+ writes each text delta as a separate chunk.
3665
+ @param response A Node.js response-like object (ServerResponse).
3666
+ @param init Optional headers, status code, and status text.
3667
+ */
3668
+ pipeTextStreamToResponse(response: ServerResponse, init?: ResponseInit): void;
3523
3669
  /**
3524
- Additional provider-specific metadata. They are passed through
3525
- from the provider to the AI SDK and enable provider-specific
3526
- results that can be fully encapsulated in the provider.
3527
- */
3528
- readonly providerMetadata: ProviderMetadata | undefined;
3670
+ Converts the result to a streamed response object with a stream data part stream.
3671
+
3672
+ @param options.status The status code.
3673
+ @param options.statusText The status text.
3674
+ @param options.headers The headers.
3675
+ @param options.getErrorMessage An optional function that converts an error to an error message.
3676
+ @param options.sendUsage Whether to send the usage information to the client. Defaults to true.
3677
+ @param options.sendReasoning Whether to send the reasoning information to the client. Defaults to false.
3678
+ @return A response object.
3679
+ */
3680
+ toUIMessageStreamResponse<UI_MESSAGE extends UIMessage>(options?: ResponseInit & UIMessageStreamOptions<UI_MESSAGE>): Response;
3529
3681
  /**
3530
- Converts the object to a JSON response.
3531
- The response will have a status code of 200 and a content type of `application/json; charset=utf-8`.
3682
+ Creates a simple text stream response.
3683
+ Each text delta is encoded as UTF-8 and sent as a separate chunk.
3684
+ Non-text-delta events are ignored.
3685
+ @param init Optional headers, status code, and status text.
3532
3686
  */
3533
- toJsonResponse(init?: ResponseInit): Response;
3687
+ toTextStreamResponse(init?: ResponseInit): Response;
3534
3688
  }
3689
+ type TextStreamPart<TOOLS extends ToolSet> = ContentPart<TOOLS> | {
3690
+ type: 'reasoning-part-finish';
3691
+ } | {
3692
+ type: 'tool-call-streaming-start';
3693
+ toolCallId: string;
3694
+ toolName: string;
3695
+ } | {
3696
+ type: 'tool-call-delta';
3697
+ toolCallId: string;
3698
+ toolName: string;
3699
+ argsTextDelta: string;
3700
+ } | {
3701
+ type: 'start-step';
3702
+ request: LanguageModelRequestMetadata;
3703
+ warnings: CallWarning[];
3704
+ } | {
3705
+ type: 'finish-step';
3706
+ response: LanguageModelResponseMetadata;
3707
+ usage: LanguageModelUsage;
3708
+ finishReason: FinishReason;
3709
+ providerMetadata: ProviderMetadata | undefined;
3710
+ } | {
3711
+ type: 'start';
3712
+ } | {
3713
+ type: 'finish';
3714
+ finishReason: FinishReason;
3715
+ totalUsage: LanguageModelUsage;
3716
+ } | {
3717
+ type: 'error';
3718
+ error: unknown;
3719
+ } | {
3720
+ type: 'raw';
3721
+ rawValue: unknown;
3722
+ };
3535
3723
 
3536
3724
  /**
3537
- A function that attempts to repair the raw output of the mode
3538
- to enable JSON parsing.
3539
-
3540
- Should return the repaired text or null if the text cannot be repaired.
3725
+ * Detects the first chunk in a buffer.
3726
+ *
3727
+ * @param buffer - The buffer to detect the first chunk in.
3728
+ *
3729
+ * @returns The first detected chunk, or `undefined` if no chunk was detected.
3730
+ */
3731
+ type ChunkDetector = (buffer: string) => string | undefined | null;
3732
+ /**
3733
+ * Smooths text streaming output.
3734
+ *
3735
+ * @param delayInMs - The delay in milliseconds between each chunk. Defaults to 10ms. Can be set to `null` to skip the delay.
3736
+ * @param chunking - Controls how the text is chunked for streaming. Use "word" to stream word by word (default), "line" to stream line by line, or provide a custom RegExp pattern for custom chunking.
3737
+ *
3738
+ * @returns A transform stream that smooths text streaming output.
3739
+ */
3740
+ declare function smoothStream<TOOLS extends ToolSet>({ delayInMs, chunking, _internal: { delay }, }?: {
3741
+ delayInMs?: number | null;
3742
+ chunking?: 'word' | 'line' | RegExp | ChunkDetector;
3743
+ /**
3744
+ * Internal. For test use only. May change without notice.
3541
3745
  */
3542
- type RepairTextFunction = (options: {
3543
- text: string;
3544
- error: JSONParseError | TypeValidationError;
3545
- }) => Promise<string | null>;
3746
+ _internal?: {
3747
+ delay?: (delayInMs: number | null) => Promise<void>;
3748
+ };
3749
+ }): (options: {
3750
+ tools: TOOLS;
3751
+ }) => TransformStream<TextStreamPart<TOOLS>, TextStreamPart<TOOLS>>;
3752
+
3546
3753
  /**
3547
- Generate a structured, typed object for a given prompt and schema using a language model.
3754
+ A transformation that is applied to the stream.
3548
3755
 
3549
- This function does not stream the output. If you want to stream the output, use `streamObject` instead.
3756
+ @param stopStream - A function that stops the source stream.
3757
+ @param tools - The tools that are accessible to and can be called by the model. The model needs to support calling tools.
3758
+ */
3759
+ type StreamTextTransform<TOOLS extends ToolSet> = (options: {
3760
+ tools: TOOLS;
3761
+ stopStream: () => void;
3762
+ }) => TransformStream<TextStreamPart<TOOLS>, TextStreamPart<TOOLS>>;
3763
+ /**
3764
+ Callback that is set using the `onError` option.
3550
3765
 
3551
- @param model - The language model to use.
3552
- @param tools - Tools that are accessible to and can be called by the model. The model needs to support calling tools.
3766
+ @param event - The event that is passed to the callback.
3767
+ */
3768
+ type StreamTextOnErrorCallback = (event: {
3769
+ error: unknown;
3770
+ }) => Promise<void> | void;
3771
+ /**
3772
+ Callback that is set using the `onStepFinish` option.
3553
3773
 
3554
- @param system - A system message that will be part of the prompt.
3555
- @param prompt - A simple text prompt. You can either use `prompt` or `messages` but not both.
3556
- @param messages - A list of messages. You can either use `prompt` or `messages` but not both.
3774
+ @param stepResult - The result of the step.
3775
+ */
3776
+ type StreamTextOnStepFinishCallback<TOOLS extends ToolSet> = (stepResult: StepResult<TOOLS>) => Promise<void> | void;
3777
+ /**
3778
+ Callback that is set using the `onChunk` option.
3557
3779
 
3558
- @param maxOutputTokens - Maximum number of tokens to generate.
3559
- @param temperature - Temperature setting.
3560
- The value is passed through to the provider. The range depends on the provider and model.
3561
- It is recommended to set either `temperature` or `topP`, but not both.
3562
- @param topP - Nucleus sampling.
3563
- The value is passed through to the provider. The range depends on the provider and model.
3564
- It is recommended to set either `temperature` or `topP`, but not both.
3565
- @param topK - Only sample from the top K options for each subsequent token.
3566
- Used to remove "long tail" low probability responses.
3567
- Recommended for advanced use cases only. You usually only need to use temperature.
3568
- @param presencePenalty - Presence penalty setting.
3780
+ @param event - The event that is passed to the callback.
3781
+ */
3782
+ type StreamTextOnChunkCallback<TOOLS extends ToolSet> = (event: {
3783
+ chunk: Extract<TextStreamPart<TOOLS>, {
3784
+ type: 'text' | 'reasoning' | 'source' | 'tool-call' | 'tool-call-streaming-start' | 'tool-call-delta' | 'tool-result';
3785
+ }>;
3786
+ }) => Promise<void> | void;
3787
+ /**
3788
+ Callback that is set using the `onFinish` option.
3789
+
3790
+ @param event - The event that is passed to the callback.
3791
+ */
3792
+ type StreamTextOnFinishCallback<TOOLS extends ToolSet> = (event: StepResult<TOOLS> & {
3793
+ /**
3794
+ Details for all steps.
3795
+ */
3796
+ readonly steps: StepResult<TOOLS>[];
3797
+ /**
3798
+ Total usage for all steps. This is the sum of the usage of all steps.
3799
+ */
3800
+ readonly totalUsage: LanguageModelUsage;
3801
+ }) => Promise<void> | void;
3802
+ /**
3803
+ Generate a text and call tools for a given prompt using a language model.
3804
+
3805
+ This function streams the output. If you do not want to stream the output, use `generateText` instead.
3806
+
3807
+ @param model - The language model to use.
3808
+ @param tools - Tools that are accessible to and can be called by the model. The model needs to support calling tools.
3809
+
3810
+ @param system - A system message that will be part of the prompt.
3811
+ @param prompt - A simple text prompt. You can either use `prompt` or `messages` but not both.
3812
+ @param messages - A list of messages. You can either use `prompt` or `messages` but not both.
3813
+
3814
+ @param maxOutputTokens - Maximum number of tokens to generate.
3815
+ @param temperature - Temperature setting.
3816
+ The value is passed through to the provider. The range depends on the provider and model.
3817
+ It is recommended to set either `temperature` or `topP`, but not both.
3818
+ @param topP - Nucleus sampling.
3819
+ The value is passed through to the provider. The range depends on the provider and model.
3820
+ It is recommended to set either `temperature` or `topP`, but not both.
3821
+ @param topK - Only sample from the top K options for each subsequent token.
3822
+ Used to remove "long tail" low probability responses.
3823
+ Recommended for advanced use cases only. You usually only need to use temperature.
3824
+ @param presencePenalty - Presence penalty setting.
3569
3825
  It affects the likelihood of the model to repeat information that is already in the prompt.
3570
3826
  The value is passed through to the provider. The range depends on the provider and model.
3571
3827
  @param frequencyPenalty - Frequency penalty setting.
@@ -3580,85 +3836,40 @@ If set and supported by the model, calls will generate deterministic results.
3580
3836
  @param abortSignal - An optional abort signal that can be used to cancel the call.
3581
3837
  @param headers - Additional HTTP headers to be sent with the request. Only applicable for HTTP-based providers.
3582
3838
 
3583
- @param schema - The schema of the object that the model should generate.
3584
- @param schemaName - Optional name of the output that should be generated.
3585
- Used by some providers for additional LLM guidance, e.g.
3586
- via tool or schema name.
3587
- @param schemaDescription - Optional description of the output that should be generated.
3588
- Used by some providers for additional LLM guidance, e.g.
3589
- via tool or schema description.
3590
-
3591
- @param output - The type of the output.
3592
-
3593
- - 'object': The output is an object.
3594
- - 'array': The output is an array.
3595
- - 'enum': The output is an enum.
3596
- - 'no-schema': The output is not a schema.
3597
-
3598
- @param experimental_repairText - A function that attempts to repair the raw output of the mode
3599
- to enable JSON parsing.
3600
-
3601
- @param experimental_telemetry - Optional telemetry configuration (experimental).
3839
+ @param maxSteps - Maximum number of sequential LLM calls (steps), e.g. when you use tool calls.
3602
3840
 
3603
- @param providerOptions - Additional provider-specific options. They are passed through
3604
- to the provider from the AI SDK and enable provider-specific
3605
- functionality that can be fully encapsulated in the provider.
3841
+ @param onChunk - Callback that is called for each chunk of the stream. The stream processing will pause until the callback promise is resolved.
3842
+ @param onError - Callback that is called when an error occurs during streaming. You can use it to log errors.
3843
+ @param onStepFinish - Callback that is called when each step (LLM call) is finished, including intermediate steps.
3844
+ @param onFinish - Callback that is called when the LLM response and all request tool executions
3845
+ (for tools that have an `execute` function) are finished.
3606
3846
 
3607
- @returns
3608
- A result object that contains the generated object, the finish reason, the token usage, and additional information.
3847
+ @return
3848
+ A result object for accessing different stream types and additional information.
3609
3849
  */
3610
- declare function generateObject<SCHEMA extends z3.Schema | z4.$ZodType | Schema = z4.$ZodType<JSONValue$1>, OUTPUT extends 'object' | 'array' | 'enum' | 'no-schema' = InferSchema<SCHEMA> extends string ? 'enum' : 'object', RESULT = OUTPUT extends 'array' ? Array<InferSchema<SCHEMA>> : InferSchema<SCHEMA>>(options: Omit<CallSettings, 'stopSequences'> & Prompt & (OUTPUT extends 'enum' ? {
3611
- /**
3612
- The enum values that the model should use.
3613
- */
3614
- enum: Array<RESULT>;
3615
- mode?: 'json';
3616
- output: 'enum';
3617
- } : OUTPUT extends 'no-schema' ? {} : {
3618
- /**
3619
- The schema of the object that the model should generate.
3620
- */
3621
- schema: SCHEMA;
3622
- /**
3623
- Optional name of the output that should be generated.
3624
- Used by some providers for additional LLM guidance, e.g.
3625
- via tool or schema name.
3626
- */
3627
- schemaName?: string;
3628
- /**
3629
- Optional description of the output that should be generated.
3630
- Used by some providers for additional LLM guidance, e.g.
3631
- via tool or schema description.
3632
- */
3633
- schemaDescription?: string;
3634
- /**
3635
- The mode to use for object generation.
3636
-
3637
- The schema is converted into a JSON schema and used in one of the following ways
3638
-
3639
- - 'auto': The provider will choose the best mode for the model.
3640
- - 'tool': A tool with the JSON schema as parameters is provided and the provider is instructed to use it.
3641
- - 'json': The JSON schema and an instruction are injected into the prompt. If the provider supports JSON mode, it is enabled. If the provider supports JSON grammars, the grammar is used.
3642
-
3643
- Please note that most providers do not support all modes.
3644
-
3645
- Default and recommended: 'auto' (best mode for the model).
3646
- */
3647
- mode?: 'auto' | 'json' | 'tool';
3648
- }) & {
3649
- output?: OUTPUT;
3850
+ declare function streamText<TOOLS extends ToolSet, OUTPUT = never, PARTIAL_OUTPUT = never>({ model, tools, toolChoice, system, prompt, messages, maxRetries, abortSignal, headers, stopWhen, experimental_output: output, experimental_telemetry: telemetry, prepareStep, providerOptions, experimental_activeTools, activeTools, experimental_repairToolCall: repairToolCall, experimental_transform: transform, includeRawChunks, onChunk, onError, onFinish, onStepFinish, _internal: { now, generateId, currentDate, }, ...settings }: CallSettings & Prompt & {
3650
3851
  /**
3651
3852
  The language model to use.
3652
3853
  */
3653
3854
  model: LanguageModel;
3654
3855
  /**
3655
- A function that attempts to repair the raw output of the mode
3656
- to enable JSON parsing.
3856
+ The tools that the model can call. The model needs to support calling tools.
3857
+ */
3858
+ tools?: TOOLS;
3859
+ /**
3860
+ The tool choice strategy. Default: 'auto'.
3657
3861
  */
3658
- experimental_repairText?: RepairTextFunction;
3862
+ toolChoice?: ToolChoice<TOOLS>;
3863
+ /**
3864
+ Condition for stopping the generation when there are tool results in the last step.
3865
+ When the condition is an array, any of the conditions can be met to stop the generation.
3866
+
3867
+ @default stepCountIs(1)
3868
+ */
3869
+ stopWhen?: StopCondition<NoInfer<TOOLS>> | Array<StopCondition<NoInfer<TOOLS>>>;
3659
3870
  /**
3660
3871
  Optional telemetry configuration (experimental).
3661
- */
3872
+ */
3662
3873
  experimental_telemetry?: TelemetrySettings;
3663
3874
  /**
3664
3875
  Additional provider-specific options. They are passed through
@@ -3667,206 +3878,245 @@ functionality that can be fully encapsulated in the provider.
3667
3878
  */
3668
3879
  providerOptions?: ProviderOptions;
3669
3880
  /**
3670
- * Internal. For test use only. May change without notice.
3881
+ * @deprecated Use `activeTools` instead.
3882
+ */
3883
+ experimental_activeTools?: Array<keyof NoInfer<TOOLS>>;
3884
+ /**
3885
+ Limits the tools that are available for the model to call without
3886
+ changing the tool call and result types in the result.
3887
+ */
3888
+ activeTools?: Array<keyof NoInfer<TOOLS>>;
3889
+ /**
3890
+ Optional specification for parsing structured outputs from the LLM response.
3891
+ */
3892
+ experimental_output?: Output<OUTPUT, PARTIAL_OUTPUT>;
3893
+ /**
3894
+ Optional function that you can use to provide different settings for a step.
3895
+
3896
+ @param options - The options for the step.
3897
+ @param options.steps - The steps that have been executed so far.
3898
+ @param options.stepNumber - The number of the step that is being executed.
3899
+ @param options.model - The model that is being used.
3900
+
3901
+ @returns An object that contains the settings for the step.
3902
+ If you return undefined (or for undefined settings), the settings from the outer level will be used.
3903
+ */
3904
+ prepareStep?: PrepareStepFunction<NoInfer<TOOLS>>;
3905
+ /**
3906
+ A function that attempts to repair a tool call that failed to parse.
3907
+ */
3908
+ experimental_repairToolCall?: ToolCallRepairFunction<TOOLS>;
3909
+ /**
3910
+ Optional stream transformations.
3911
+ They are applied in the order they are provided.
3912
+ The stream transformations must maintain the stream structure for streamText to work correctly.
3913
+ */
3914
+ experimental_transform?: StreamTextTransform<TOOLS> | Array<StreamTextTransform<TOOLS>>;
3915
+ /**
3916
+ Whether to include raw chunks from the provider in the stream.
3917
+ When enabled, you will receive raw chunks with type 'raw' that contain the unprocessed data from the provider.
3918
+ This allows access to cutting-edge provider features not yet wrapped by the AI SDK.
3919
+ Defaults to false.
3920
+ */
3921
+ includeRawChunks?: boolean;
3922
+ /**
3923
+ Callback that is called for each chunk of the stream.
3924
+ The stream processing will pause until the callback promise is resolved.
3925
+ */
3926
+ onChunk?: StreamTextOnChunkCallback<TOOLS>;
3927
+ /**
3928
+ Callback that is invoked when an error occurs during streaming.
3929
+ You can use it to log errors.
3930
+ The stream processing will pause until the callback promise is resolved.
3931
+ */
3932
+ onError?: StreamTextOnErrorCallback;
3933
+ /**
3934
+ Callback that is called when the LLM response and all request tool executions
3935
+ (for tools that have an `execute` function) are finished.
3936
+
3937
+ The usage is the combined usage of all steps.
3938
+ */
3939
+ onFinish?: StreamTextOnFinishCallback<TOOLS>;
3940
+ /**
3941
+ Callback that is called when each step (LLM call) is finished, including intermediate steps.
3942
+ */
3943
+ onStepFinish?: StreamTextOnStepFinishCallback<TOOLS>;
3944
+ /**
3945
+ Internal. For test use only. May change without notice.
3671
3946
  */
3672
3947
  _internal?: {
3673
- generateId?: () => string;
3948
+ now?: () => number;
3949
+ generateId?: IdGenerator;
3674
3950
  currentDate?: () => Date;
3675
3951
  };
3676
- }): Promise<GenerateObjectResult<RESULT>>;
3677
-
3678
- /**
3679
- * Calculates the cosine similarity between two vectors. This is a useful metric for
3680
- * comparing the similarity of two vectors such as embeddings.
3681
- *
3682
- * @param vector1 - The first vector.
3683
- * @param vector2 - The second vector.
3684
- *
3685
- * @returns The cosine similarity between vector1 and vector2.
3686
- * @returns 0 if either vector is the zero vector.
3687
- *
3688
- * @throws {InvalidArgumentError} If the vectors do not have the same length.
3689
- */
3690
- declare function cosineSimilarity(vector1: number[], vector2: number[]): number;
3952
+ }): StreamTextResult<TOOLS, PARTIAL_OUTPUT>;
3691
3953
 
3692
3954
  /**
3693
- * Converts a data URL of type text/* to a text string.
3955
+ The result of a `generateImage` call.
3956
+ It contains the images and additional information.
3694
3957
  */
3695
- declare function getTextFromDataUrl(dataUrl: string): string;
3958
+ interface GenerateImageResult {
3959
+ /**
3960
+ The first image that was generated.
3961
+ */
3962
+ readonly image: GeneratedFile;
3963
+ /**
3964
+ The images that were generated.
3965
+ */
3966
+ readonly images: Array<GeneratedFile>;
3967
+ /**
3968
+ Warnings for the call, e.g. unsupported settings.
3969
+ */
3970
+ readonly warnings: Array<ImageGenerationWarning>;
3971
+ /**
3972
+ Response metadata from the provider. There may be multiple responses if we made multiple calls to the model.
3973
+ */
3974
+ readonly responses: Array<ImageModelResponseMetadata>;
3975
+ /**
3976
+ * Provider-specific metadata. They are passed through from the provider to the AI SDK and enable provider-specific
3977
+ * results that can be fully encapsulated in the provider.
3978
+ */
3979
+ readonly providerMetadata: ImageModelProviderMetadata;
3980
+ }
3696
3981
 
3697
3982
  /**
3698
- * Performs a deep-equal comparison of two parsed JSON objects.
3699
- *
3700
- * @param {any} obj1 - The first object to compare.
3701
- * @param {any} obj2 - The second object to compare.
3702
- * @returns {boolean} - Returns true if the two objects are deeply equal, false otherwise.
3703
- */
3704
- declare function isDeepEqualData(obj1: any, obj2: any): boolean;
3705
-
3706
- declare function parsePartialJson(jsonText: string | undefined): Promise<{
3707
- value: JSONValue$1 | undefined;
3708
- state: 'undefined-input' | 'successful-parse' | 'repaired-parse' | 'failed-parse';
3709
- }>;
3983
+ Generates images using an image model.
3710
3984
 
3711
- type Job = () => Promise<void>;
3712
-
3713
- declare class SerialJobExecutor {
3714
- private queue;
3715
- private isProcessing;
3716
- private processQueue;
3717
- run(job: Job): Promise<void>;
3718
- }
3719
-
3720
- /**
3721
- * Creates a ReadableStream that emits the provided values with an optional delay between each value.
3722
- *
3723
- * @param options - The configuration options
3724
- * @param options.chunks - Array of values to be emitted by the stream
3725
- * @param options.initialDelayInMs - Optional initial delay in milliseconds before emitting the first value (default: 0). Can be set to `null` to skip the initial delay. The difference between `initialDelayInMs: null` and `initialDelayInMs: 0` is that `initialDelayInMs: null` will emit the values without any delay, while `initialDelayInMs: 0` will emit the values with a delay of 0 milliseconds.
3726
- * @param options.chunkDelayInMs - Optional delay in milliseconds between emitting each value (default: 0). Can be set to `null` to skip the delay. The difference between `chunkDelayInMs: null` and `chunkDelayInMs: 0` is that `chunkDelayInMs: null` will emit the values without any delay, while `chunkDelayInMs: 0` will emit the values with a delay of 0 milliseconds.
3727
- * @returns A ReadableStream that emits the provided values
3728
- */
3729
- declare function simulateReadableStream<T>({ chunks, initialDelayInMs, chunkDelayInMs, _internal, }: {
3730
- chunks: T[];
3731
- initialDelayInMs?: number | null;
3732
- chunkDelayInMs?: number | null;
3733
- _internal?: {
3734
- delay?: (ms: number | null) => Promise<void>;
3735
- };
3736
- }): ReadableStream<T>;
3985
+ @param model - The image model to use.
3986
+ @param prompt - The prompt that should be used to generate the image.
3987
+ @param n - Number of images to generate. Default: 1.
3988
+ @param size - Size of the images to generate. Must have the format `{width}x{height}`.
3989
+ @param aspectRatio - Aspect ratio of the images to generate. Must have the format `{width}:{height}`.
3990
+ @param seed - Seed for the image generation.
3991
+ @param providerOptions - Additional provider-specific options that are passed through to the provider
3992
+ as body parameters.
3993
+ @param maxRetries - Maximum number of retries. Set to 0 to disable retries. Default: 2.
3994
+ @param abortSignal - An optional abort signal that can be used to cancel the call.
3995
+ @param headers - Additional HTTP headers to be sent with the request. Only applicable for HTTP-based providers.
3737
3996
 
3738
- /**
3739
- The result of a `streamObject` call that contains the partial object stream and additional information.
3997
+ @returns A result object that contains the generated images.
3740
3998
  */
3741
- interface StreamObjectResult<PARTIAL, RESULT, ELEMENT_STREAM> {
3742
- /**
3743
- Warnings from the model provider (e.g. unsupported settings)
3744
- */
3745
- readonly warnings: Promise<CallWarning[] | undefined>;
3999
+ declare function generateImage({ model, prompt, n, maxImagesPerCall, size, aspectRatio, seed, providerOptions, maxRetries: maxRetriesArg, abortSignal, headers, }: {
3746
4000
  /**
3747
- The token usage of the generated response. Resolved when the response is finished.
4001
+ The image model to use.
3748
4002
  */
3749
- readonly usage: Promise<LanguageModelUsage>;
4003
+ model: ImageModelV2;
3750
4004
  /**
3751
- Additional provider-specific metadata. They are passed through
3752
- from the provider to the AI SDK and enable provider-specific
3753
- results that can be fully encapsulated in the provider.
4005
+ The prompt that should be used to generate the image.
3754
4006
  */
3755
- readonly providerMetadata: Promise<ProviderMetadata | undefined>;
3756
- /**
3757
- Additional request information from the last step.
3758
- */
3759
- readonly request: Promise<LanguageModelRequestMetadata>;
3760
- /**
3761
- Additional response information.
3762
- */
3763
- readonly response: Promise<LanguageModelResponseMetadata>;
4007
+ prompt: string;
3764
4008
  /**
3765
- The generated object (typed according to the schema). Resolved when the response is finished.
3766
- */
3767
- readonly object: Promise<RESULT>;
4009
+ Number of images to generate.
4010
+ */
4011
+ n?: number;
3768
4012
  /**
3769
- Stream of partial objects. It gets more complete as the stream progresses.
3770
-
3771
- Note that the partial object is not validated.
3772
- If you want to be certain that the actual content matches your schema, you need to implement your own validation for partial results.
3773
- */
3774
- readonly partialObjectStream: AsyncIterableStream<PARTIAL>;
4013
+ Number of images to generate.
4014
+ */
4015
+ maxImagesPerCall?: number;
3775
4016
  /**
3776
- * Stream over complete array elements. Only available if the output strategy is set to `array`.
4017
+ Size of the images to generate. Must have the format `{width}x{height}`. If not provided, the default size will be used.
3777
4018
  */
3778
- readonly elementStream: ELEMENT_STREAM;
4019
+ size?: `${number}x${number}`;
3779
4020
  /**
3780
- Text stream of the JSON representation of the generated object. It contains text chunks.
3781
- When the stream is finished, the object is valid JSON that can be parsed.
3782
- */
3783
- readonly textStream: AsyncIterableStream<string>;
4021
+ Aspect ratio of the images to generate. Must have the format `{width}:{height}`. If not provided, the default aspect ratio will be used.
4022
+ */
4023
+ aspectRatio?: `${number}:${number}`;
3784
4024
  /**
3785
- Stream of different types of events, including partial objects, errors, and finish events.
3786
- Only errors that stop the stream, such as network errors, are thrown.
3787
- */
3788
- readonly fullStream: AsyncIterableStream<ObjectStreamPart<PARTIAL>>;
4025
+ Seed for the image generation. If not provided, the default seed will be used.
4026
+ */
4027
+ seed?: number;
3789
4028
  /**
3790
- Writes text delta output to a Node.js response-like object.
3791
- It sets a `Content-Type` header to `text/plain; charset=utf-8` and
3792
- writes each text delta as a separate chunk.
4029
+ Additional provider-specific options that are passed through to the provider
4030
+ as body parameters.
3793
4031
 
3794
- @param response A Node.js response-like object (ServerResponse).
3795
- @param init Optional headers, status code, and status text.
4032
+ The outer record is keyed by the provider name, and the inner
4033
+ record is keyed by the provider-specific metadata key.
4034
+ ```ts
4035
+ {
4036
+ "openai": {
4037
+ "style": "vivid"
4038
+ }
4039
+ }
4040
+ ```
3796
4041
  */
3797
- pipeTextStreamToResponse(response: ServerResponse$1, init?: ResponseInit): void;
4042
+ providerOptions?: ProviderOptions;
3798
4043
  /**
3799
- Creates a simple text stream response.
3800
- The response has a `Content-Type` header set to `text/plain; charset=utf-8`.
3801
- Each text delta is encoded as UTF-8 and sent as a separate chunk.
3802
- Non-text-delta events are ignored.
4044
+ Maximum number of retries per embedding model call. Set to 0 to disable retries.
3803
4045
 
3804
- @param init Optional headers, status code, and status text.
3805
- */
3806
- toTextStreamResponse(init?: ResponseInit): Response;
3807
- }
3808
- type ObjectStreamPart<PARTIAL> = {
3809
- type: 'object';
3810
- object: PARTIAL;
3811
- } | {
3812
- type: 'text-delta';
3813
- textDelta: string;
3814
- } | {
3815
- type: 'error';
3816
- error: unknown;
3817
- } | {
3818
- type: 'finish';
3819
- finishReason: FinishReason;
3820
- usage: LanguageModelUsage;
3821
- response: LanguageModelResponseMetadata;
3822
- providerMetadata?: ProviderMetadata;
3823
- };
3824
-
3825
- /**
3826
- Callback that is set using the `onError` option.
4046
+ @default 2
4047
+ */
4048
+ maxRetries?: number;
4049
+ /**
4050
+ Abort signal.
4051
+ */
4052
+ abortSignal?: AbortSignal;
4053
+ /**
4054
+ Additional headers to include in the request.
4055
+ Only applicable for HTTP-based providers.
4056
+ */
4057
+ headers?: Record<string, string>;
4058
+ }): Promise<GenerateImageResult>;
3827
4059
 
3828
- @param event - The event that is passed to the callback.
3829
- */
3830
- type StreamObjectOnErrorCallback = (event: {
3831
- error: unknown;
3832
- }) => Promise<void> | void;
3833
4060
  /**
3834
- Callback that is set using the `onFinish` option.
3835
-
3836
- @param event - The event that is passed to the callback.
4061
+ The result of a `generateObject` call.
3837
4062
  */
3838
- type StreamObjectOnFinishCallback<RESULT> = (event: {
4063
+ interface GenerateObjectResult<OBJECT> {
3839
4064
  /**
3840
- The token usage of the generated response.
3841
- */
3842
- usage: LanguageModelUsage;
4065
+ The generated object (typed according to the schema).
4066
+ */
4067
+ readonly object: OBJECT;
3843
4068
  /**
3844
- The generated object. Can be undefined if the final object does not match the schema.
3845
- */
3846
- object: RESULT | undefined;
4069
+ The reason why the generation finished.
4070
+ */
4071
+ readonly finishReason: FinishReason;
3847
4072
  /**
3848
- Optional error object. This is e.g. a TypeValidationError when the final object does not match the schema.
3849
- */
3850
- error: unknown | undefined;
4073
+ The token usage of the generated text.
4074
+ */
4075
+ readonly usage: LanguageModelUsage;
3851
4076
  /**
3852
- Response metadata.
3853
- */
3854
- response: LanguageModelResponseMetadata;
4077
+ Warnings from the model provider (e.g. unsupported settings).
4078
+ */
4079
+ readonly warnings: CallWarning[] | undefined;
3855
4080
  /**
3856
- Warnings from the model provider (e.g. unsupported settings).
3857
- */
3858
- warnings?: CallWarning[];
4081
+ Additional request information.
4082
+ */
4083
+ readonly request: LanguageModelRequestMetadata;
4084
+ /**
4085
+ Additional response information.
4086
+ */
4087
+ readonly response: LanguageModelResponseMetadata & {
4088
+ /**
4089
+ Response body (available only for providers that use HTTP requests).
4090
+ */
4091
+ body?: unknown;
4092
+ };
3859
4093
  /**
3860
4094
  Additional provider-specific metadata. They are passed through
3861
- to the provider from the AI SDK and enable provider-specific
3862
- functionality that can be fully encapsulated in the provider.
3863
- */
3864
- providerMetadata: ProviderMetadata | undefined;
3865
- }) => Promise<void> | void;
4095
+ from the provider to the AI SDK and enable provider-specific
4096
+ results that can be fully encapsulated in the provider.
4097
+ */
4098
+ readonly providerMetadata: ProviderMetadata | undefined;
4099
+ /**
4100
+ Converts the object to a JSON response.
4101
+ The response will have a status code of 200 and a content type of `application/json; charset=utf-8`.
4102
+ */
4103
+ toJsonResponse(init?: ResponseInit): Response;
4104
+ }
4105
+
4106
+ /**
4107
+ A function that attempts to repair the raw output of the mode
4108
+ to enable JSON parsing.
4109
+
4110
+ Should return the repaired text or null if the text cannot be repaired.
4111
+ */
4112
+ type RepairTextFunction = (options: {
4113
+ text: string;
4114
+ error: JSONParseError | TypeValidationError;
4115
+ }) => Promise<string | null>;
3866
4116
  /**
3867
4117
  Generate a structured, typed object for a given prompt and schema using a language model.
3868
4118
 
3869
- This function streams the output. If you do not want to stream the output, use `generateObject` instead.
4119
+ This function does not stream the output. If you want to stream the output, use `streamObject` instead.
3870
4120
 
3871
4121
  @param model - The language model to use.
3872
4122
  @param tools - Tools that are accessible to and can be called by the model. The model needs to support calling tools.
@@ -3915,6 +4165,9 @@ via tool or schema description.
3915
4165
  - 'enum': The output is an enum.
3916
4166
  - 'no-schema': The output is not a schema.
3917
4167
 
4168
+ @param experimental_repairText - A function that attempts to repair the raw output of the mode
4169
+ to enable JSON parsing.
4170
+
3918
4171
  @param experimental_telemetry - Optional telemetry configuration (experimental).
3919
4172
 
3920
4173
  @param providerOptions - Additional provider-specific options. They are passed through
@@ -3922,9 +4175,9 @@ to the provider from the AI SDK and enable provider-specific
3922
4175
  functionality that can be fully encapsulated in the provider.
3923
4176
 
3924
4177
  @returns
3925
- A result object for accessing the partial object stream and additional information.
4178
+ A result object that contains the generated object, the finish reason, the token usage, and additional information.
3926
4179
  */
3927
- declare function streamObject<SCHEMA extends z3.Schema | z4.$ZodType | Schema = z4.$ZodType<JSONValue$1>, OUTPUT extends 'object' | 'array' | 'enum' | 'no-schema' = InferSchema<SCHEMA> extends string ? 'enum' : 'object', RESULT = OUTPUT extends 'array' ? Array<InferSchema<SCHEMA>> : InferSchema<SCHEMA>>(options: Omit<CallSettings, 'stopSequences'> & Prompt & (OUTPUT extends 'enum' ? {
4180
+ declare function generateObject<SCHEMA extends z3.Schema | z4.$ZodType | Schema = z4.$ZodType<JSONValue$1>, OUTPUT extends 'object' | 'array' | 'enum' | 'no-schema' = InferSchema<SCHEMA> extends string ? 'enum' : 'object', RESULT = OUTPUT extends 'array' ? Array<InferSchema<SCHEMA>> : InferSchema<SCHEMA>>(options: Omit<CallSettings, 'stopSequences'> & Prompt & (OUTPUT extends 'enum' ? {
3928
4181
  /**
3929
4182
  The enum values that the model should use.
3930
4183
  */
@@ -3966,840 +4219,625 @@ Default and recommended: 'auto' (best mode for the model).
3966
4219
  output?: OUTPUT;
3967
4220
  /**
3968
4221
  The language model to use.
3969
- */
4222
+ */
3970
4223
  model: LanguageModel;
3971
4224
  /**
3972
- Optional telemetry configuration (experimental).
4225
+ A function that attempts to repair the raw output of the mode
4226
+ to enable JSON parsing.
3973
4227
  */
4228
+ experimental_repairText?: RepairTextFunction;
4229
+ /**
4230
+ Optional telemetry configuration (experimental).
4231
+ */
3974
4232
  experimental_telemetry?: TelemetrySettings;
3975
4233
  /**
3976
4234
  Additional provider-specific options. They are passed through
3977
4235
  to the provider from the AI SDK and enable provider-specific
3978
4236
  functionality that can be fully encapsulated in the provider.
3979
- */
4237
+ */
3980
4238
  providerOptions?: ProviderOptions;
3981
4239
  /**
3982
- Callback that is invoked when an error occurs during streaming.
3983
- You can use it to log errors.
3984
- The stream processing will pause until the callback promise is resolved.
3985
- */
3986
- onError?: StreamObjectOnErrorCallback;
3987
- /**
3988
- Callback that is called when the LLM response and the final object validation are finished.
3989
- */
3990
- onFinish?: StreamObjectOnFinishCallback<RESULT>;
3991
- /**
3992
4240
  * Internal. For test use only. May change without notice.
3993
4241
  */
3994
4242
  _internal?: {
3995
4243
  generateId?: () => string;
3996
4244
  currentDate?: () => Date;
3997
- now?: () => number;
3998
4245
  };
3999
- }): StreamObjectResult<OUTPUT extends 'enum' ? string : OUTPUT extends 'array' ? RESULT : DeepPartial<RESULT>, OUTPUT extends 'array' ? RESULT : RESULT, OUTPUT extends 'array' ? RESULT extends Array<infer U> ? AsyncIterableStream<U> : never : never>;
4000
-
4001
- /**
4002
- * A generated audio file.
4003
- */
4004
- interface GeneratedAudioFile extends GeneratedFile {
4005
- /**
4006
- * Audio format of the file (e.g., 'mp3', 'wav', etc.)
4007
- */
4008
- readonly format: string;
4009
- }
4246
+ }): Promise<GenerateObjectResult<RESULT>>;
4010
4247
 
4011
4248
  /**
4012
- The result of a `generateSpeech` call.
4013
- It contains the audio data and additional information.
4249
+ The result of a `streamObject` call that contains the partial object stream and additional information.
4014
4250
  */
4015
- interface SpeechResult {
4251
+ interface StreamObjectResult<PARTIAL, RESULT, ELEMENT_STREAM> {
4016
4252
  /**
4017
- * The audio data as a base64 encoded string or binary data.
4018
- */
4019
- readonly audio: GeneratedAudioFile;
4253
+ Warnings from the model provider (e.g. unsupported settings)
4254
+ */
4255
+ readonly warnings: Promise<CallWarning[] | undefined>;
4020
4256
  /**
4021
- Warnings for the call, e.g. unsupported settings.
4257
+ The token usage of the generated response. Resolved when the response is finished.
4022
4258
  */
4023
- readonly warnings: Array<SpeechWarning>;
4259
+ readonly usage: Promise<LanguageModelUsage>;
4024
4260
  /**
4025
- Response metadata from the provider. There may be multiple responses if we made multiple calls to the model.
4261
+ Additional provider-specific metadata. They are passed through
4262
+ from the provider to the AI SDK and enable provider-specific
4263
+ results that can be fully encapsulated in the provider.
4026
4264
  */
4027
- readonly responses: Array<SpeechModelResponseMetadata>;
4265
+ readonly providerMetadata: Promise<ProviderMetadata | undefined>;
4028
4266
  /**
4029
- Provider metadata from the provider.
4030
- */
4031
- readonly providerMetadata: Record<string, Record<string, JSONValue$1>>;
4032
- }
4033
-
4034
- /**
4035
- Generates speech audio using a speech model.
4036
-
4037
- @param model - The speech model to use.
4038
- @param text - The text to convert to speech.
4039
- @param voice - The voice to use for speech generation.
4040
- @param outputFormat - The output format to use for speech generation e.g. "mp3", "wav", etc.
4041
- @param instructions - Instructions for the speech generation e.g. "Speak in a slow and steady tone".
4042
- @param speed - The speed of the speech generation.
4043
- @param providerOptions - Additional provider-specific options that are passed through to the provider
4044
- as body parameters.
4045
- @param maxRetries - Maximum number of retries. Set to 0 to disable retries. Default: 2.
4046
- @param abortSignal - An optional abort signal that can be used to cancel the call.
4047
- @param headers - Additional HTTP headers to be sent with the request. Only applicable for HTTP-based providers.
4048
-
4049
- @returns A result object that contains the generated audio data.
4050
- */
4051
- declare function generateSpeech({ model, text, voice, outputFormat, instructions, speed, providerOptions, maxRetries: maxRetriesArg, abortSignal, headers, }: {
4267
+ Additional request information from the last step.
4268
+ */
4269
+ readonly request: Promise<LanguageModelRequestMetadata>;
4052
4270
  /**
4053
- The speech model to use.
4054
- */
4055
- model: SpeechModelV1;
4271
+ Additional response information.
4272
+ */
4273
+ readonly response: Promise<LanguageModelResponseMetadata>;
4056
4274
  /**
4057
- The text to convert to speech.
4058
- */
4059
- text: string;
4275
+ The generated object (typed according to the schema). Resolved when the response is finished.
4276
+ */
4277
+ readonly object: Promise<RESULT>;
4060
4278
  /**
4061
- The voice to use for speech generation.
4062
- */
4063
- voice?: string;
4279
+ Stream of partial objects. It gets more complete as the stream progresses.
4280
+
4281
+ Note that the partial object is not validated.
4282
+ If you want to be certain that the actual content matches your schema, you need to implement your own validation for partial results.
4283
+ */
4284
+ readonly partialObjectStream: AsyncIterableStream<PARTIAL>;
4064
4285
  /**
4065
- * The desired output format for the audio e.g. "mp3", "wav", etc.
4286
+ * Stream over complete array elements. Only available if the output strategy is set to `array`.
4066
4287
  */
4067
- outputFormat?: 'mp3' | 'wav' | (string & {});
4288
+ readonly elementStream: ELEMENT_STREAM;
4068
4289
  /**
4069
- Instructions for the speech generation e.g. "Speak in a slow and steady tone".
4070
- */
4071
- instructions?: string;
4290
+ Text stream of the JSON representation of the generated object. It contains text chunks.
4291
+ When the stream is finished, the object is valid JSON that can be parsed.
4292
+ */
4293
+ readonly textStream: AsyncIterableStream<string>;
4072
4294
  /**
4073
- The speed of the speech generation.
4074
- */
4075
- speed?: number;
4295
+ Stream of different types of events, including partial objects, errors, and finish events.
4296
+ Only errors that stop the stream, such as network errors, are thrown.
4297
+ */
4298
+ readonly fullStream: AsyncIterableStream<ObjectStreamPart<PARTIAL>>;
4076
4299
  /**
4077
- Additional provider-specific options that are passed through to the provider
4078
- as body parameters.
4300
+ Writes text delta output to a Node.js response-like object.
4301
+ It sets a `Content-Type` header to `text/plain; charset=utf-8` and
4302
+ writes each text delta as a separate chunk.
4079
4303
 
4080
- The outer record is keyed by the provider name, and the inner
4081
- record is keyed by the provider-specific metadata key.
4082
- ```ts
4083
- {
4084
- "openai": {}
4085
- }
4086
- ```
4304
+ @param response A Node.js response-like object (ServerResponse).
4305
+ @param init Optional headers, status code, and status text.
4087
4306
  */
4088
- providerOptions?: ProviderOptions;
4307
+ pipeTextStreamToResponse(response: ServerResponse$1, init?: ResponseInit): void;
4089
4308
  /**
4090
- Maximum number of retries per speech model call. Set to 0 to disable retries.
4309
+ Creates a simple text stream response.
4310
+ The response has a `Content-Type` header set to `text/plain; charset=utf-8`.
4311
+ Each text delta is encoded as UTF-8 and sent as a separate chunk.
4312
+ Non-text-delta events are ignored.
4091
4313
 
4092
- @default 2
4093
- */
4094
- maxRetries?: number;
4095
- /**
4096
- Abort signal.
4097
- */
4098
- abortSignal?: AbortSignal;
4099
- /**
4100
- Additional headers to include in the request.
4101
- Only applicable for HTTP-based providers.
4102
- */
4103
- headers?: Record<string, string>;
4104
- }): Promise<SpeechResult>;
4105
-
4106
- /**
4107
- * Applies default settings for a language model.
4108
- */
4109
- declare function defaultSettingsMiddleware({ settings, }: {
4110
- settings: Partial<{
4111
- maxOutputTokens?: LanguageModelV2CallOptions['maxOutputTokens'];
4112
- temperature?: LanguageModelV2CallOptions['temperature'];
4113
- stopSequences?: LanguageModelV2CallOptions['stopSequences'];
4114
- topP?: LanguageModelV2CallOptions['topP'];
4115
- topK?: LanguageModelV2CallOptions['topK'];
4116
- presencePenalty?: LanguageModelV2CallOptions['presencePenalty'];
4117
- frequencyPenalty?: LanguageModelV2CallOptions['frequencyPenalty'];
4118
- responseFormat?: LanguageModelV2CallOptions['responseFormat'];
4119
- seed?: LanguageModelV2CallOptions['seed'];
4120
- tools?: LanguageModelV2CallOptions['tools'];
4121
- toolChoice?: LanguageModelV2CallOptions['toolChoice'];
4122
- headers?: LanguageModelV2CallOptions['headers'];
4123
- providerOptions?: LanguageModelV2CallOptions['providerOptions'];
4124
- }>;
4125
- }): LanguageModelV2Middleware;
4314
+ @param init Optional headers, status code, and status text.
4315
+ */
4316
+ toTextStreamResponse(init?: ResponseInit): Response;
4317
+ }
4318
+ type ObjectStreamPart<PARTIAL> = {
4319
+ type: 'object';
4320
+ object: PARTIAL;
4321
+ } | {
4322
+ type: 'text-delta';
4323
+ textDelta: string;
4324
+ } | {
4325
+ type: 'error';
4326
+ error: unknown;
4327
+ } | {
4328
+ type: 'finish';
4329
+ finishReason: FinishReason;
4330
+ usage: LanguageModelUsage;
4331
+ response: LanguageModelResponseMetadata;
4332
+ providerMetadata?: ProviderMetadata;
4333
+ };
4126
4334
 
4127
4335
  /**
4128
- * Extract an XML-tagged reasoning section from the generated text and exposes it
4129
- * as a `reasoning` property on the result.
4130
- *
4131
- * @param tagName - The name of the XML tag to extract reasoning from.
4132
- * @param separator - The separator to use between reasoning and text sections.
4133
- * @param startWithReasoning - Whether to start with reasoning tokens.
4134
- */
4135
- declare function extractReasoningMiddleware({ tagName, separator, startWithReasoning, }: {
4136
- tagName: string;
4137
- separator?: string;
4138
- startWithReasoning?: boolean;
4139
- }): LanguageModelV2Middleware;
4336
+ Callback that is set using the `onError` option.
4140
4337
 
4141
- /**
4142
- * Simulates streaming chunks with the response from a generate call.
4338
+ @param event - The event that is passed to the callback.
4143
4339
  */
4144
- declare function simulateStreamingMiddleware(): LanguageModelV2Middleware;
4145
-
4340
+ type StreamObjectOnErrorCallback = (event: {
4341
+ error: unknown;
4342
+ }) => Promise<void> | void;
4146
4343
  /**
4147
- * Wraps a LanguageModelV2 instance with middleware functionality.
4148
- * This function allows you to apply middleware to transform parameters,
4149
- * wrap generate operations, and wrap stream operations of a language model.
4150
- *
4151
- * @param options - Configuration options for wrapping the language model.
4152
- * @param options.model - The original LanguageModelV2 instance to be wrapped.
4153
- * @param options.middleware - The middleware to be applied to the language model. When multiple middlewares are provided, the first middleware will transform the input first, and the last middleware will be wrapped directly around the model.
4154
- * @param options.modelId - Optional custom model ID to override the original model's ID.
4155
- * @param options.providerId - Optional custom provider ID to override the original model's provider.
4156
- * @returns A new LanguageModelV2 instance with middleware applied.
4157
- */
4158
- declare const wrapLanguageModel: ({ model, middleware: middlewareArg, modelId, providerId, }: {
4159
- model: LanguageModelV2;
4160
- middleware: LanguageModelV2Middleware | LanguageModelV2Middleware[];
4161
- modelId?: string;
4162
- providerId?: string;
4163
- }) => LanguageModelV2;
4344
+ Callback that is set using the `onFinish` option.
4164
4345
 
4165
- /**
4166
- * Creates a custom provider with specified language models, text embedding models, and an optional fallback provider.
4167
- *
4168
- * @param {Object} options - The options for creating the custom provider.
4169
- * @param {Record<string, LanguageModel>} [options.languageModels] - A record of language models, where keys are model IDs and values are LanguageModel instances.
4170
- * @param {Record<string, EmbeddingModel<string>>} [options.textEmbeddingModels] - A record of text embedding models, where keys are model IDs and values are EmbeddingModel<string> instances.
4171
- * @param {Record<string, ImageModel>} [options.imageModels] - A record of image models, where keys are model IDs and values are ImageModel instances.
4172
- * @param {Provider} [options.fallbackProvider] - An optional fallback provider to use when a requested model is not found in the custom provider.
4173
- * @returns {Provider} A Provider object with languageModel, textEmbeddingModel, and imageModel methods.
4174
- *
4175
- * @throws {NoSuchModelError} Throws when a requested model is not found and no fallback provider is available.
4346
+ @param event - The event that is passed to the callback.
4176
4347
  */
4177
- declare function customProvider<LANGUAGE_MODELS extends Record<string, LanguageModelV2>, EMBEDDING_MODELS extends Record<string, EmbeddingModelV2<string>>, IMAGE_MODELS extends Record<string, ImageModelV2>>({ languageModels, textEmbeddingModels, imageModels, fallbackProvider, }: {
4178
- languageModels?: LANGUAGE_MODELS;
4179
- textEmbeddingModels?: EMBEDDING_MODELS;
4180
- imageModels?: IMAGE_MODELS;
4181
- fallbackProvider?: ProviderV2;
4182
- }): ProviderV2 & {
4183
- languageModel(modelId: ExtractModelId<LANGUAGE_MODELS>): LanguageModelV2;
4184
- textEmbeddingModel(modelId: ExtractModelId<EMBEDDING_MODELS>): EmbeddingModelV2<string>;
4185
- imageModel(modelId: ExtractModelId<IMAGE_MODELS>): ImageModelV2;
4186
- };
4348
+ type StreamObjectOnFinishCallback<RESULT> = (event: {
4349
+ /**
4350
+ The token usage of the generated response.
4351
+ */
4352
+ usage: LanguageModelUsage;
4353
+ /**
4354
+ The generated object. Can be undefined if the final object does not match the schema.
4355
+ */
4356
+ object: RESULT | undefined;
4357
+ /**
4358
+ Optional error object. This is e.g. a TypeValidationError when the final object does not match the schema.
4359
+ */
4360
+ error: unknown | undefined;
4361
+ /**
4362
+ Response metadata.
4363
+ */
4364
+ response: LanguageModelResponseMetadata;
4365
+ /**
4366
+ Warnings from the model provider (e.g. unsupported settings).
4367
+ */
4368
+ warnings?: CallWarning[];
4369
+ /**
4370
+ Additional provider-specific metadata. They are passed through
4371
+ to the provider from the AI SDK and enable provider-specific
4372
+ functionality that can be fully encapsulated in the provider.
4373
+ */
4374
+ providerMetadata: ProviderMetadata | undefined;
4375
+ }) => Promise<void> | void;
4187
4376
  /**
4188
- * @deprecated Use `customProvider` instead.
4189
- */
4190
- declare const experimental_customProvider: typeof customProvider;
4191
- type ExtractModelId<MODELS extends Record<string, unknown>> = Extract<keyof MODELS, string>;
4377
+ Generate a structured, typed object for a given prompt and schema using a language model.
4192
4378
 
4193
- declare const symbol: unique symbol;
4194
- declare class NoSuchProviderError extends NoSuchModelError {
4195
- private readonly [symbol];
4196
- readonly providerId: string;
4197
- readonly availableProviders: string[];
4198
- constructor({ modelId, modelType, providerId, availableProviders, message, }: {
4199
- modelId: string;
4200
- modelType: 'languageModel' | 'textEmbeddingModel';
4201
- providerId: string;
4202
- availableProviders: string[];
4203
- message?: string;
4204
- });
4205
- static isInstance(error: unknown): error is NoSuchProviderError;
4206
- }
4379
+ This function streams the output. If you do not want to stream the output, use `generateObject` instead.
4207
4380
 
4208
- type ExtractLiteralUnion<T> = T extends string ? string extends T ? never : T : never;
4209
- interface ProviderRegistryProvider<PROVIDERS extends Record<string, ProviderV2> = Record<string, ProviderV2>, SEPARATOR extends string = ':'> {
4210
- languageModel<KEY extends keyof PROVIDERS>(id: KEY extends string ? `${KEY & string}${SEPARATOR}${ExtractLiteralUnion<Parameters<NonNullable<PROVIDERS[KEY]['languageModel']>>[0]>}` : never): LanguageModelV2;
4211
- languageModel<KEY extends keyof PROVIDERS>(id: KEY extends string ? `${KEY & string}${SEPARATOR}${string}` : never): LanguageModelV2;
4212
- textEmbeddingModel<KEY extends keyof PROVIDERS>(id: KEY extends string ? `${KEY & string}${SEPARATOR}${ExtractLiteralUnion<Parameters<NonNullable<PROVIDERS[KEY]['textEmbeddingModel']>>[0]>}` : never): EmbeddingModelV2<string>;
4213
- textEmbeddingModel<KEY extends keyof PROVIDERS>(id: KEY extends string ? `${KEY & string}${SEPARATOR}${string}` : never): EmbeddingModelV2<string>;
4214
- imageModel<KEY extends keyof PROVIDERS>(id: KEY extends string ? `${KEY & string}${SEPARATOR}${ExtractLiteralUnion<Parameters<NonNullable<PROVIDERS[KEY]['imageModel']>>[0]>}` : never): ImageModelV2;
4215
- imageModel<KEY extends keyof PROVIDERS>(id: KEY extends string ? `${KEY & string}${SEPARATOR}${string}` : never): ImageModelV2;
4216
- }
4217
- /**
4218
- * Creates a registry for the given providers.
4219
- */
4220
- declare function createProviderRegistry<PROVIDERS extends Record<string, ProviderV2>, SEPARATOR extends string = ':'>(providers: PROVIDERS, { separator, }?: {
4221
- separator?: SEPARATOR;
4222
- }): ProviderRegistryProvider<PROVIDERS, SEPARATOR>;
4223
- /**
4224
- * @deprecated Use `createProviderRegistry` instead.
4225
- */
4226
- declare const experimental_createProviderRegistry: typeof createProviderRegistry;
4381
+ @param model - The language model to use.
4382
+ @param tools - Tools that are accessible to and can be called by the model. The model needs to support calling tools.
4227
4383
 
4228
- /**
4229
- The result of a `transcribe` call.
4230
- It contains the transcript and additional information.
4384
+ @param system - A system message that will be part of the prompt.
4385
+ @param prompt - A simple text prompt. You can either use `prompt` or `messages` but not both.
4386
+ @param messages - A list of messages. You can either use `prompt` or `messages` but not both.
4387
+
4388
+ @param maxOutputTokens - Maximum number of tokens to generate.
4389
+ @param temperature - Temperature setting.
4390
+ The value is passed through to the provider. The range depends on the provider and model.
4391
+ It is recommended to set either `temperature` or `topP`, but not both.
4392
+ @param topP - Nucleus sampling.
4393
+ The value is passed through to the provider. The range depends on the provider and model.
4394
+ It is recommended to set either `temperature` or `topP`, but not both.
4395
+ @param topK - Only sample from the top K options for each subsequent token.
4396
+ Used to remove "long tail" low probability responses.
4397
+ Recommended for advanced use cases only. You usually only need to use temperature.
4398
+ @param presencePenalty - Presence penalty setting.
4399
+ It affects the likelihood of the model to repeat information that is already in the prompt.
4400
+ The value is passed through to the provider. The range depends on the provider and model.
4401
+ @param frequencyPenalty - Frequency penalty setting.
4402
+ It affects the likelihood of the model to repeatedly use the same words or phrases.
4403
+ The value is passed through to the provider. The range depends on the provider and model.
4404
+ @param stopSequences - Stop sequences.
4405
+ If set, the model will stop generating text when one of the stop sequences is generated.
4406
+ @param seed - The seed (integer) to use for random sampling.
4407
+ If set and supported by the model, calls will generate deterministic results.
4408
+
4409
+ @param maxRetries - Maximum number of retries. Set to 0 to disable retries. Default: 2.
4410
+ @param abortSignal - An optional abort signal that can be used to cancel the call.
4411
+ @param headers - Additional HTTP headers to be sent with the request. Only applicable for HTTP-based providers.
4412
+
4413
+ @param schema - The schema of the object that the model should generate.
4414
+ @param schemaName - Optional name of the output that should be generated.
4415
+ Used by some providers for additional LLM guidance, e.g.
4416
+ via tool or schema name.
4417
+ @param schemaDescription - Optional description of the output that should be generated.
4418
+ Used by some providers for additional LLM guidance, e.g.
4419
+ via tool or schema description.
4420
+
4421
+ @param output - The type of the output.
4422
+
4423
+ - 'object': The output is an object.
4424
+ - 'array': The output is an array.
4425
+ - 'enum': The output is an enum.
4426
+ - 'no-schema': The output is not a schema.
4427
+
4428
+ @param experimental_telemetry - Optional telemetry configuration (experimental).
4429
+
4430
+ @param providerOptions - Additional provider-specific options. They are passed through
4431
+ to the provider from the AI SDK and enable provider-specific
4432
+ functionality that can be fully encapsulated in the provider.
4433
+
4434
+ @returns
4435
+ A result object for accessing the partial object stream and additional information.
4231
4436
  */
4232
- interface TranscriptionResult {
4233
- /**
4234
- * The complete transcribed text from the audio.
4235
- */
4236
- readonly text: string;
4237
- /**
4238
- * Array of transcript segments with timing information.
4239
- * Each segment represents a portion of the transcribed text with start and end times.
4240
- */
4241
- readonly segments: Array<{
4242
- /**
4243
- * The text content of this segment.
4244
- */
4245
- readonly text: string;
4246
- /**
4247
- * The start time of this segment in seconds.
4248
- */
4249
- readonly startSecond: number;
4250
- /**
4251
- * The end time of this segment in seconds.
4252
- */
4253
- readonly endSecond: number;
4254
- }>;
4437
+ declare function streamObject<SCHEMA extends z3.Schema | z4.$ZodType | Schema = z4.$ZodType<JSONValue$1>, OUTPUT extends 'object' | 'array' | 'enum' | 'no-schema' = InferSchema<SCHEMA> extends string ? 'enum' : 'object', RESULT = OUTPUT extends 'array' ? Array<InferSchema<SCHEMA>> : InferSchema<SCHEMA>>(options: Omit<CallSettings, 'stopSequences'> & Prompt & (OUTPUT extends 'enum' ? {
4255
4438
  /**
4256
- * The detected language of the audio content, as an ISO-639-1 code (e.g., 'en' for English).
4257
- * May be undefined if the language couldn't be detected.
4258
- */
4259
- readonly language: string | undefined;
4439
+ The enum values that the model should use.
4440
+ */
4441
+ enum: Array<RESULT>;
4442
+ mode?: 'json';
4443
+ output: 'enum';
4444
+ } : OUTPUT extends 'no-schema' ? {} : {
4260
4445
  /**
4261
- * The total duration of the audio file in seconds.
4262
- * May be undefined if the duration couldn't be determined.
4263
- */
4264
- readonly durationInSeconds: number | undefined;
4446
+ The schema of the object that the model should generate.
4447
+ */
4448
+ schema: SCHEMA;
4265
4449
  /**
4266
- Warnings for the call, e.g. unsupported settings.
4267
- */
4268
- readonly warnings: Array<TranscriptionWarning>;
4450
+ Optional name of the output that should be generated.
4451
+ Used by some providers for additional LLM guidance, e.g.
4452
+ via tool or schema name.
4453
+ */
4454
+ schemaName?: string;
4269
4455
  /**
4270
- Response metadata from the provider. There may be multiple responses if we made multiple calls to the model.
4271
- */
4272
- readonly responses: Array<TranscriptionModelResponseMetadata>;
4456
+ Optional description of the output that should be generated.
4457
+ Used by some providers for additional LLM guidance, e.g.
4458
+ via tool or schema description.
4459
+ */
4460
+ schemaDescription?: string;
4273
4461
  /**
4274
- Provider metadata from the provider.
4275
- */
4276
- readonly providerMetadata: Record<string, Record<string, JSONValue$1>>;
4277
- }
4462
+ The mode to use for object generation.
4278
4463
 
4279
- /**
4280
- Generates transcripts using a transcription model.
4464
+ The schema is converted into a JSON schema and used in one of the following ways
4281
4465
 
4282
- @param model - The transcription model to use.
4283
- @param audio - The audio data to transcribe as DataContent (string | Uint8Array | ArrayBuffer | Buffer) or a URL.
4284
- @param providerOptions - Additional provider-specific options that are passed through to the provider
4285
- as body parameters.
4286
- @param maxRetries - Maximum number of retries. Set to 0 to disable retries. Default: 2.
4287
- @param abortSignal - An optional abort signal that can be used to cancel the call.
4288
- @param headers - Additional HTTP headers to be sent with the request. Only applicable for HTTP-based providers.
4466
+ - 'auto': The provider will choose the best mode for the model.
4467
+ - 'tool': A tool with the JSON schema as parameters is provided and the provider is instructed to use it.
4468
+ - 'json': The JSON schema and an instruction are injected into the prompt. If the provider supports JSON mode, it is enabled. If the provider supports JSON grammars, the grammar is used.
4289
4469
 
4290
- @returns A result object that contains the generated transcript.
4291
- */
4292
- declare function transcribe({ model, audio, providerOptions, maxRetries: maxRetriesArg, abortSignal, headers, }: {
4470
+ Please note that most providers do not support all modes.
4471
+
4472
+ Default and recommended: 'auto' (best mode for the model).
4473
+ */
4474
+ mode?: 'auto' | 'json' | 'tool';
4475
+ }) & {
4476
+ output?: OUTPUT;
4293
4477
  /**
4294
- The transcription model to use.
4295
- */
4296
- model: TranscriptionModelV1;
4478
+ The language model to use.
4479
+ */
4480
+ model: LanguageModel;
4297
4481
  /**
4298
- The audio data to transcribe.
4482
+ Optional telemetry configuration (experimental).
4299
4483
  */
4300
- audio: DataContent | URL;
4484
+ experimental_telemetry?: TelemetrySettings;
4301
4485
  /**
4302
- Additional provider-specific options that are passed through to the provider
4303
- as body parameters.
4304
-
4305
- The outer record is keyed by the provider name, and the inner
4306
- record is keyed by the provider-specific metadata key.
4307
- ```ts
4308
- {
4309
- "openai": {
4310
- "temperature": 0
4311
- }
4312
- }
4313
- ```
4314
- */
4486
+ Additional provider-specific options. They are passed through
4487
+ to the provider from the AI SDK and enable provider-specific
4488
+ functionality that can be fully encapsulated in the provider.
4489
+ */
4315
4490
  providerOptions?: ProviderOptions;
4316
4491
  /**
4317
- Maximum number of retries per transcript model call. Set to 0 to disable retries.
4318
-
4319
- @default 2
4320
- */
4321
- maxRetries?: number;
4322
- /**
4323
- Abort signal.
4324
- */
4325
- abortSignal?: AbortSignal;
4326
- /**
4327
- Additional headers to include in the request.
4328
- Only applicable for HTTP-based providers.
4492
+ Callback that is invoked when an error occurs during streaming.
4493
+ You can use it to log errors.
4494
+ The stream processing will pause until the callback promise is resolved.
4329
4495
  */
4330
- headers?: Record<string, string>;
4331
- }): Promise<TranscriptionResult>;
4332
-
4333
- type DataUIMessageStreamPart = {
4334
- type: `data-${string}`;
4335
- id?: string;
4336
- data: unknown;
4337
- };
4338
- type UIMessageStreamPart = {
4339
- type: 'text';
4340
- text: string;
4341
- } | {
4342
- type: 'error';
4343
- errorText: string;
4344
- } | {
4345
- type: 'tool-call';
4346
- toolCallId: string;
4347
- toolName: string;
4348
- args: unknown;
4349
- } | {
4350
- type: 'tool-result';
4351
- toolCallId: string;
4352
- result: unknown;
4353
- providerMetadata?: ProviderMetadata;
4354
- } | {
4355
- type: 'tool-call-streaming-start';
4356
- toolCallId: string;
4357
- toolName: string;
4358
- } | {
4359
- type: 'tool-call-delta';
4360
- toolCallId: string;
4361
- argsTextDelta: string;
4362
- } | {
4363
- type: 'reasoning';
4364
- text: string;
4365
- providerMetadata?: ProviderMetadata;
4366
- } | {
4367
- type: 'source-url';
4368
- sourceId: string;
4369
- url: string;
4370
- title?: string;
4371
- providerMetadata?: ProviderMetadata;
4372
- } | {
4373
- type: 'file';
4374
- url: string;
4375
- mediaType: string;
4376
- } | DataUIMessageStreamPart | {
4377
- type: 'metadata';
4378
- metadata: unknown;
4379
- } | {
4380
- type: 'start-step';
4381
- metadata?: unknown;
4382
- } | {
4383
- type: 'finish-step';
4384
- metadata?: unknown;
4385
- } | {
4386
- type: 'start';
4387
- messageId?: string;
4388
- metadata?: unknown;
4389
- } | {
4390
- type: 'finish';
4391
- metadata?: unknown;
4392
- } | {
4393
- type: 'reasoning-part-finish';
4394
- };
4395
-
4396
- interface UIMessageStreamWriter {
4397
- /**
4398
- * Appends a data stream part to the stream.
4399
- */
4400
- write(part: UIMessageStreamPart): void;
4496
+ onError?: StreamObjectOnErrorCallback;
4401
4497
  /**
4402
- * Merges the contents of another stream to this stream.
4403
- */
4404
- merge(stream: ReadableStream<UIMessageStreamPart>): void;
4498
+ Callback that is called when the LLM response and the final object validation are finished.
4499
+ */
4500
+ onFinish?: StreamObjectOnFinishCallback<RESULT>;
4405
4501
  /**
4406
- * Error handler that is used by the data stream writer.
4407
- * This is intended for forwarding when merging streams
4408
- * to prevent duplicated error masking.
4502
+ * Internal. For test use only. May change without notice.
4409
4503
  */
4410
- onError: ((error: unknown) => string) | undefined;
4411
- }
4504
+ _internal?: {
4505
+ generateId?: () => string;
4506
+ currentDate?: () => Date;
4507
+ now?: () => number;
4508
+ };
4509
+ }): StreamObjectResult<OUTPUT extends 'enum' ? string : OUTPUT extends 'array' ? RESULT : DeepPartial<RESULT>, OUTPUT extends 'array' ? RESULT : RESULT, OUTPUT extends 'array' ? RESULT extends Array<infer U> ? AsyncIterableStream<U> : never : never>;
4412
4510
 
4413
- declare function createUIMessageStream({ execute, onError, // mask error messages for safety by default
4414
- originalMessages, onFinish, }: {
4415
- execute: (options: {
4416
- writer: UIMessageStreamWriter;
4417
- }) => Promise<void> | void;
4418
- onError?: (error: unknown) => string;
4511
+ /**
4512
+ * A generated audio file.
4513
+ */
4514
+ interface GeneratedAudioFile extends GeneratedFile {
4419
4515
  /**
4420
- * The original messages.
4516
+ * Audio format of the file (e.g., 'mp3', 'wav', etc.)
4421
4517
  */
4422
- originalMessages?: UIMessage[];
4423
- onFinish?: (options: {
4424
- /**
4425
- * The updates list of UI messages.
4426
- */
4427
- messages: UIMessage[];
4428
- /**
4429
- * Indicates whether the response message is a continuation of the last original message,
4430
- * or if a new message was created.
4431
- */
4432
- isContinuation: boolean;
4433
- /**
4434
- * The message that was sent to the client as a response
4435
- * (including the original message if it was extended).
4436
- */
4437
- responseMessage: UIMessage;
4438
- }) => void;
4439
- }): ReadableStream<UIMessageStreamPart>;
4440
-
4441
- declare function createUIMessageStreamResponse({ status, statusText, headers, stream, }: ResponseInit & {
4442
- stream: ReadableStream<UIMessageStreamPart>;
4443
- }): Response;
4444
-
4445
- declare function pipeUIMessageStreamToResponse({ response, status, statusText, headers, stream, }: {
4446
- response: ServerResponse;
4447
- stream: ReadableStream<UIMessageStreamPart>;
4448
- } & ResponseInit): void;
4449
-
4450
- declare class JsonToSseTransformStream extends TransformStream<unknown, string> {
4451
- constructor();
4452
- }
4453
-
4454
- interface ChatTransport<MESSAGE_METADATA, DATA_TYPES extends UIDataTypes> {
4455
- submitMessages: (options: {
4456
- chatId: string;
4457
- messages: UIMessage<MESSAGE_METADATA, DATA_TYPES>[];
4458
- abortSignal: AbortSignal | undefined;
4459
- requestType: 'generate' | 'resume';
4460
- } & ChatRequestOptions) => Promise<ReadableStream<UIMessageStreamPart>>;
4518
+ readonly format: string;
4461
4519
  }
4462
4520
 
4463
- type ChatRequestOptions = {
4464
- /**
4465
- Additional headers that should be to be passed to the API endpoint.
4466
- */
4467
- headers?: Record<string, string> | Headers;
4468
- /**
4469
- Additional body JSON properties that should be sent to the API endpoint.
4470
- */
4471
- body?: object;
4472
- metadata?: unknown;
4473
- };
4474
- interface ChatSubscriber {
4475
- onChange: (event: ChatEvent) => void;
4476
- }
4477
- interface ChatEvent {
4478
- type: 'messages-changed' | 'status-changed';
4479
- }
4480
- type ChatStatus = 'submitted' | 'streaming' | 'ready' | 'error';
4481
- interface ChatState<MESSAGE_METADATA, DATA_TYPES extends UIDataTypes> {
4482
- status: ChatStatus;
4483
- error: Error | undefined;
4484
- messages: UIMessage<MESSAGE_METADATA, DATA_TYPES>[];
4485
- pushMessage: (message: UIMessage<MESSAGE_METADATA, DATA_TYPES>) => void;
4486
- popMessage: () => void;
4487
- replaceMessage: (index: number, message: UIMessage<MESSAGE_METADATA, DATA_TYPES>) => void;
4488
- snapshot: <T>(thing: T) => T;
4489
- }
4490
- interface ChatInit<MESSAGE_METADATA = unknown, UI_DATA_PART_SCHEMAS extends UIDataPartSchemas = UIDataPartSchemas> {
4491
- /**
4492
- * A unique identifier for the chat. If not provided, a random one will be
4493
- * generated.
4494
- */
4495
- id?: string;
4496
- messageMetadataSchema?: Validator<MESSAGE_METADATA> | StandardSchemaV1<MESSAGE_METADATA>;
4497
- dataPartSchemas?: UI_DATA_PART_SCHEMAS;
4498
- messages?: UIMessage<MESSAGE_METADATA, InferUIDataParts<UI_DATA_PART_SCHEMAS>>[];
4499
- /**
4500
- * A way to provide a function that is going to be used for ids for messages and the chat.
4501
- * If not provided the default AI SDK `generateId` is used.
4502
- */
4503
- generateId?: IdGenerator;
4504
- transport?: ChatTransport<NoInfer<MESSAGE_METADATA>, NoInfer<InferUIDataParts<UI_DATA_PART_SCHEMAS>>>;
4505
- maxSteps?: number;
4521
+ /**
4522
+ The result of a `generateSpeech` call.
4523
+ It contains the audio data and additional information.
4524
+ */
4525
+ interface SpeechResult {
4506
4526
  /**
4507
- * Callback function to be called when an error is encountered.
4527
+ * The audio data as a base64 encoded string or binary data.
4508
4528
  */
4509
- onError?: (error: Error) => void;
4529
+ readonly audio: GeneratedAudioFile;
4510
4530
  /**
4511
- Optional callback function that is invoked when a tool call is received.
4512
- Intended for automatic client-side tool execution.
4513
-
4514
- You can optionally return a result for the tool call,
4515
- either synchronously or asynchronously.
4531
+ Warnings for the call, e.g. unsupported settings.
4516
4532
  */
4517
- onToolCall?: ({ toolCall, }: {
4518
- toolCall: ToolCall<string, unknown>;
4519
- }) => void | Promise<unknown> | unknown;
4533
+ readonly warnings: Array<SpeechWarning>;
4520
4534
  /**
4521
- * Optional callback function that is called when the assistant message is finished streaming.
4522
- *
4523
- * @param message The message that was streamed.
4535
+ Response metadata from the provider. There may be multiple responses if we made multiple calls to the model.
4524
4536
  */
4525
- onFinish?: (options: {
4526
- message: UIMessage<NoInfer<MESSAGE_METADATA>, NoInfer<InferUIDataParts<UI_DATA_PART_SCHEMAS>>>;
4527
- }) => void;
4537
+ readonly responses: Array<SpeechModelResponseMetadata>;
4538
+ /**
4539
+ Provider metadata from the provider.
4540
+ */
4541
+ readonly providerMetadata: Record<string, Record<string, JSONValue$1>>;
4528
4542
  }
4529
- declare abstract class AbstractChat<MESSAGE_METADATA = unknown, UI_DATA_PART_SCHEMAS extends UIDataPartSchemas = UIDataPartSchemas> {
4530
- readonly id: string;
4531
- readonly generateId: IdGenerator;
4532
- protected state: ChatState<MESSAGE_METADATA, InferUIDataParts<UI_DATA_PART_SCHEMAS>>;
4533
- private readonly subscribers;
4534
- private messageMetadataSchema;
4535
- private dataPartSchemas;
4536
- private readonly transport;
4537
- private maxSteps;
4538
- private onError?;
4539
- private onToolCall?;
4540
- private onFinish?;
4541
- private activeResponse;
4542
- private jobExecutor;
4543
- constructor({ generateId, id, transport, maxSteps, messageMetadataSchema, dataPartSchemas, state, onError, onToolCall, onFinish, }: Omit<ChatInit<MESSAGE_METADATA, UI_DATA_PART_SCHEMAS>, 'messages'> & {
4544
- state: ChatState<MESSAGE_METADATA, InferUIDataParts<UI_DATA_PART_SCHEMAS>>;
4545
- });
4543
+
4544
+ /**
4545
+ Generates speech audio using a speech model.
4546
+
4547
+ @param model - The speech model to use.
4548
+ @param text - The text to convert to speech.
4549
+ @param voice - The voice to use for speech generation.
4550
+ @param outputFormat - The output format to use for speech generation e.g. "mp3", "wav", etc.
4551
+ @param instructions - Instructions for the speech generation e.g. "Speak in a slow and steady tone".
4552
+ @param speed - The speed of the speech generation.
4553
+ @param providerOptions - Additional provider-specific options that are passed through to the provider
4554
+ as body parameters.
4555
+ @param maxRetries - Maximum number of retries. Set to 0 to disable retries. Default: 2.
4556
+ @param abortSignal - An optional abort signal that can be used to cancel the call.
4557
+ @param headers - Additional HTTP headers to be sent with the request. Only applicable for HTTP-based providers.
4558
+
4559
+ @returns A result object that contains the generated audio data.
4560
+ */
4561
+ declare function generateSpeech({ model, text, voice, outputFormat, instructions, speed, providerOptions, maxRetries: maxRetriesArg, abortSignal, headers, }: {
4546
4562
  /**
4547
- * Hook status:
4548
- *
4549
- * - `submitted`: The message has been sent to the API and we're awaiting the start of the response stream.
4550
- * - `streaming`: The response is actively streaming in from the API, receiving chunks of data.
4551
- * - `ready`: The full response has been received and processed; a new user message can be submitted.
4552
- * - `error`: An error occurred during the API request, preventing successful completion.
4563
+ The speech model to use.
4564
+ */
4565
+ model: SpeechModelV1;
4566
+ /**
4567
+ The text to convert to speech.
4553
4568
  */
4554
- get status(): ChatStatus;
4555
- protected setStatus({ status, error, }: {
4556
- status: ChatStatus;
4557
- error?: Error;
4558
- }): void;
4559
- get error(): Error | undefined;
4560
- get messages(): UIMessage<MESSAGE_METADATA, InferUIDataParts<UI_DATA_PART_SCHEMAS>>[];
4561
- get lastMessage(): UIMessage<MESSAGE_METADATA, InferUIDataParts<UI_DATA_PART_SCHEMAS>> | undefined;
4562
- subscribe(subscriber: ChatSubscriber): () => void;
4563
- set messages(messages: UIMessage<MESSAGE_METADATA, InferUIDataParts<UI_DATA_PART_SCHEMAS>>[]);
4564
- removeAssistantResponse: () => void;
4569
+ text: string;
4565
4570
  /**
4566
- * Append a user message to the chat list. This triggers the API call to fetch
4567
- * the assistant's response.
4571
+ The voice to use for speech generation.
4568
4572
  */
4569
- sendMessage: (message: (CreateUIMessage<MESSAGE_METADATA, InferUIDataParts<UI_DATA_PART_SCHEMAS>> & {
4570
- text?: never;
4571
- files?: never;
4572
- }) | {
4573
- text: string;
4574
- files?: FileList | FileUIPart[];
4575
- metadata?: MESSAGE_METADATA;
4576
- parts?: never;
4577
- } | {
4578
- files: FileList | FileUIPart[];
4579
- metadata?: MESSAGE_METADATA;
4580
- parts?: never;
4581
- }, options?: ChatRequestOptions) => Promise<void>;
4573
+ voice?: string;
4582
4574
  /**
4583
- * Regenerate the last assistant message.
4575
+ * The desired output format for the audio e.g. "mp3", "wav", etc.
4584
4576
  */
4585
- reload: (options?: ChatRequestOptions) => Promise<void>;
4577
+ outputFormat?: 'mp3' | 'wav' | (string & {});
4586
4578
  /**
4587
- * Resume an ongoing chat generation stream. This does not resume an aborted generation.
4579
+ Instructions for the speech generation e.g. "Speak in a slow and steady tone".
4580
+ */
4581
+ instructions?: string;
4582
+ /**
4583
+ The speed of the speech generation.
4588
4584
  */
4589
- experimental_resume: (options?: ChatRequestOptions) => Promise<void>;
4590
- addToolResult: ({ toolCallId, result, }: {
4591
- toolCallId: string;
4592
- result: unknown;
4593
- }) => Promise<void>;
4585
+ speed?: number;
4594
4586
  /**
4595
- * Abort the current request immediately, keep the generated tokens if any.
4587
+ Additional provider-specific options that are passed through to the provider
4588
+ as body parameters.
4589
+
4590
+ The outer record is keyed by the provider name, and the inner
4591
+ record is keyed by the provider-specific metadata key.
4592
+ ```ts
4593
+ {
4594
+ "openai": {}
4595
+ }
4596
+ ```
4597
+ */
4598
+ providerOptions?: ProviderOptions;
4599
+ /**
4600
+ Maximum number of retries per speech model call. Set to 0 to disable retries.
4601
+
4602
+ @default 2
4596
4603
  */
4597
- stop: () => Promise<void>;
4598
- private emit;
4599
- private triggerRequest;
4600
- }
4601
-
4602
- declare function convertFileListToFileUIParts(files: FileList | undefined): Promise<Array<FileUIPart>>;
4604
+ maxRetries?: number;
4605
+ /**
4606
+ Abort signal.
4607
+ */
4608
+ abortSignal?: AbortSignal;
4609
+ /**
4610
+ Additional headers to include in the request.
4611
+ Only applicable for HTTP-based providers.
4612
+ */
4613
+ headers?: Record<string, string>;
4614
+ }): Promise<SpeechResult>;
4603
4615
 
4604
4616
  /**
4605
- Converts an array of messages from useChat into an array of CoreMessages that can be used
4606
- with the AI core functions (e.g. `streamText`).
4617
+ * Applies default settings for a language model.
4607
4618
  */
4608
- declare function convertToModelMessages<TOOLS extends ToolSet = never>(messages: Array<Omit<UIMessage, 'id'>>, options?: {
4609
- tools?: TOOLS;
4610
- }): ModelMessage[];
4619
+ declare function defaultSettingsMiddleware({ settings, }: {
4620
+ settings: Partial<{
4621
+ maxOutputTokens?: LanguageModelV2CallOptions['maxOutputTokens'];
4622
+ temperature?: LanguageModelV2CallOptions['temperature'];
4623
+ stopSequences?: LanguageModelV2CallOptions['stopSequences'];
4624
+ topP?: LanguageModelV2CallOptions['topP'];
4625
+ topK?: LanguageModelV2CallOptions['topK'];
4626
+ presencePenalty?: LanguageModelV2CallOptions['presencePenalty'];
4627
+ frequencyPenalty?: LanguageModelV2CallOptions['frequencyPenalty'];
4628
+ responseFormat?: LanguageModelV2CallOptions['responseFormat'];
4629
+ seed?: LanguageModelV2CallOptions['seed'];
4630
+ tools?: LanguageModelV2CallOptions['tools'];
4631
+ toolChoice?: LanguageModelV2CallOptions['toolChoice'];
4632
+ headers?: LanguageModelV2CallOptions['headers'];
4633
+ providerOptions?: LanguageModelV2CallOptions['providerOptions'];
4634
+ }>;
4635
+ }): LanguageModelV2Middleware;
4636
+
4611
4637
  /**
4612
- @deprecated Use `convertToModelMessages` instead.
4638
+ * Extract an XML-tagged reasoning section from the generated text and exposes it
4639
+ * as a `reasoning` property on the result.
4640
+ *
4641
+ * @param tagName - The name of the XML tag to extract reasoning from.
4642
+ * @param separator - The separator to use between reasoning and text sections.
4643
+ * @param startWithReasoning - Whether to start with reasoning tokens.
4613
4644
  */
4614
- declare const convertToCoreMessages: typeof convertToModelMessages;
4645
+ declare function extractReasoningMiddleware({ tagName, separator, startWithReasoning, }: {
4646
+ tagName: string;
4647
+ separator?: string;
4648
+ startWithReasoning?: boolean;
4649
+ }): LanguageModelV2Middleware;
4615
4650
 
4616
- type PrepareRequest<MESSAGE_METADATA, DATA_TYPES extends UIDataTypes> = (options: {
4617
- id: string;
4618
- messages: UIMessage<MESSAGE_METADATA, DATA_TYPES>[];
4619
- requestMetadata: unknown;
4620
- body: Record<string, any> | undefined;
4621
- credentials: RequestCredentials | undefined;
4622
- headers: HeadersInit | undefined;
4623
- }) => {
4624
- body: object;
4625
- headers?: HeadersInit;
4626
- credentials?: RequestCredentials;
4627
- };
4651
+ /**
4652
+ * Simulates streaming chunks with the response from a generate call.
4653
+ */
4654
+ declare function simulateStreamingMiddleware(): LanguageModelV2Middleware;
4628
4655
 
4629
- declare class DefaultChatTransport<MESSAGE_METADATA, DATA_TYPES extends UIDataTypes> implements ChatTransport<MESSAGE_METADATA, DATA_TYPES> {
4630
- private api;
4631
- private credentials?;
4632
- private headers?;
4633
- private body?;
4634
- private fetch?;
4635
- private prepareRequest?;
4636
- constructor({ api, credentials, headers, body, fetch, prepareRequest, }?: {
4637
- api?: string;
4638
- /**
4639
- * The credentials mode to be used for the fetch request.
4640
- * Possible values are: 'omit', 'same-origin', 'include'.
4641
- * Defaults to 'same-origin'.
4642
- */
4643
- credentials?: RequestCredentials;
4644
- /**
4645
- * HTTP headers to be sent with the API request.
4646
- */
4647
- headers?: Record<string, string> | Headers;
4648
- /**
4649
- * Extra body object to be sent with the API request.
4650
- * @example
4651
- * Send a `sessionId` to the API along with the messages.
4652
- * ```js
4653
- * useChat({
4654
- * body: {
4655
- * sessionId: '123',
4656
- * }
4657
- * })
4658
- * ```
4659
- */
4660
- body?: object;
4661
- /**
4662
- Custom fetch implementation. You can use it as a middleware to intercept requests,
4663
- or to provide a custom fetch implementation for e.g. testing.
4664
- */
4665
- fetch?: FetchFunction;
4666
- /**
4667
- * When a function is provided, it will be used
4668
- * to prepare the request body for the chat API. This can be useful for
4669
- * customizing the request body based on the messages and data in the chat.
4670
- *
4671
- * @param id The id of the chat.
4672
- * @param messages The current messages in the chat.
4673
- * @param requestBody The request body object passed in the chat request.
4674
- */
4675
- prepareRequest?: PrepareRequest<MESSAGE_METADATA, DATA_TYPES>;
4656
+ /**
4657
+ * Wraps a LanguageModelV2 instance with middleware functionality.
4658
+ * This function allows you to apply middleware to transform parameters,
4659
+ * wrap generate operations, and wrap stream operations of a language model.
4660
+ *
4661
+ * @param options - Configuration options for wrapping the language model.
4662
+ * @param options.model - The original LanguageModelV2 instance to be wrapped.
4663
+ * @param options.middleware - The middleware to be applied to the language model. When multiple middlewares are provided, the first middleware will transform the input first, and the last middleware will be wrapped directly around the model.
4664
+ * @param options.modelId - Optional custom model ID to override the original model's ID.
4665
+ * @param options.providerId - Optional custom provider ID to override the original model's provider.
4666
+ * @returns A new LanguageModelV2 instance with middleware applied.
4667
+ */
4668
+ declare const wrapLanguageModel: ({ model, middleware: middlewareArg, modelId, providerId, }: {
4669
+ model: LanguageModelV2;
4670
+ middleware: LanguageModelV2Middleware | LanguageModelV2Middleware[];
4671
+ modelId?: string;
4672
+ providerId?: string;
4673
+ }) => LanguageModelV2;
4674
+
4675
+ /**
4676
+ * Creates a custom provider with specified language models, text embedding models, and an optional fallback provider.
4677
+ *
4678
+ * @param {Object} options - The options for creating the custom provider.
4679
+ * @param {Record<string, LanguageModel>} [options.languageModels] - A record of language models, where keys are model IDs and values are LanguageModel instances.
4680
+ * @param {Record<string, EmbeddingModel<string>>} [options.textEmbeddingModels] - A record of text embedding models, where keys are model IDs and values are EmbeddingModel<string> instances.
4681
+ * @param {Record<string, ImageModel>} [options.imageModels] - A record of image models, where keys are model IDs and values are ImageModel instances.
4682
+ * @param {Provider} [options.fallbackProvider] - An optional fallback provider to use when a requested model is not found in the custom provider.
4683
+ * @returns {Provider} A Provider object with languageModel, textEmbeddingModel, and imageModel methods.
4684
+ *
4685
+ * @throws {NoSuchModelError} Throws when a requested model is not found and no fallback provider is available.
4686
+ */
4687
+ declare function customProvider<LANGUAGE_MODELS extends Record<string, LanguageModelV2>, EMBEDDING_MODELS extends Record<string, EmbeddingModelV2<string>>, IMAGE_MODELS extends Record<string, ImageModelV2>>({ languageModels, textEmbeddingModels, imageModels, fallbackProvider, }: {
4688
+ languageModels?: LANGUAGE_MODELS;
4689
+ textEmbeddingModels?: EMBEDDING_MODELS;
4690
+ imageModels?: IMAGE_MODELS;
4691
+ fallbackProvider?: ProviderV2;
4692
+ }): ProviderV2 & {
4693
+ languageModel(modelId: ExtractModelId<LANGUAGE_MODELS>): LanguageModelV2;
4694
+ textEmbeddingModel(modelId: ExtractModelId<EMBEDDING_MODELS>): EmbeddingModelV2<string>;
4695
+ imageModel(modelId: ExtractModelId<IMAGE_MODELS>): ImageModelV2;
4696
+ };
4697
+ /**
4698
+ * @deprecated Use `customProvider` instead.
4699
+ */
4700
+ declare const experimental_customProvider: typeof customProvider;
4701
+ type ExtractModelId<MODELS extends Record<string, unknown>> = Extract<keyof MODELS, string>;
4702
+
4703
+ declare const symbol: unique symbol;
4704
+ declare class NoSuchProviderError extends NoSuchModelError {
4705
+ private readonly [symbol];
4706
+ readonly providerId: string;
4707
+ readonly availableProviders: string[];
4708
+ constructor({ modelId, modelType, providerId, availableProviders, message, }: {
4709
+ modelId: string;
4710
+ modelType: 'languageModel' | 'textEmbeddingModel';
4711
+ providerId: string;
4712
+ availableProviders: string[];
4713
+ message?: string;
4676
4714
  });
4677
- submitMessages({ chatId, messages, abortSignal, metadata, headers, body, requestType, }: Parameters<ChatTransport<MESSAGE_METADATA, DATA_TYPES>['submitMessages']>[0]): Promise<ReadableStream<UIMessageStreamPart>>;
4715
+ static isInstance(error: unknown): error is NoSuchProviderError;
4678
4716
  }
4679
4717
 
4680
- declare function getToolInvocations(message: UIMessage): ToolInvocation[];
4718
+ type ExtractLiteralUnion<T> = T extends string ? string extends T ? never : T : never;
4719
+ interface ProviderRegistryProvider<PROVIDERS extends Record<string, ProviderV2> = Record<string, ProviderV2>, SEPARATOR extends string = ':'> {
4720
+ languageModel<KEY extends keyof PROVIDERS>(id: KEY extends string ? `${KEY & string}${SEPARATOR}${ExtractLiteralUnion<Parameters<NonNullable<PROVIDERS[KEY]['languageModel']>>[0]>}` : never): LanguageModelV2;
4721
+ languageModel<KEY extends keyof PROVIDERS>(id: KEY extends string ? `${KEY & string}${SEPARATOR}${string}` : never): LanguageModelV2;
4722
+ textEmbeddingModel<KEY extends keyof PROVIDERS>(id: KEY extends string ? `${KEY & string}${SEPARATOR}${ExtractLiteralUnion<Parameters<NonNullable<PROVIDERS[KEY]['textEmbeddingModel']>>[0]>}` : never): EmbeddingModelV2<string>;
4723
+ textEmbeddingModel<KEY extends keyof PROVIDERS>(id: KEY extends string ? `${KEY & string}${SEPARATOR}${string}` : never): EmbeddingModelV2<string>;
4724
+ imageModel<KEY extends keyof PROVIDERS>(id: KEY extends string ? `${KEY & string}${SEPARATOR}${ExtractLiteralUnion<Parameters<NonNullable<PROVIDERS[KEY]['imageModel']>>[0]>}` : never): ImageModelV2;
4725
+ imageModel<KEY extends keyof PROVIDERS>(id: KEY extends string ? `${KEY & string}${SEPARATOR}${string}` : never): ImageModelV2;
4726
+ }
4727
+ /**
4728
+ * Creates a registry for the given providers.
4729
+ */
4730
+ declare function createProviderRegistry<PROVIDERS extends Record<string, ProviderV2>, SEPARATOR extends string = ':'>(providers: PROVIDERS, { separator, }?: {
4731
+ separator?: SEPARATOR;
4732
+ }): ProviderRegistryProvider<PROVIDERS, SEPARATOR>;
4733
+ /**
4734
+ * @deprecated Use `createProviderRegistry` instead.
4735
+ */
4736
+ declare const experimental_createProviderRegistry: typeof createProviderRegistry;
4681
4737
 
4682
- declare class TextStreamChatTransport<MESSAGE_METADATA, DATA_TYPES extends UIDataTypes> implements ChatTransport<MESSAGE_METADATA, DATA_TYPES> {
4683
- private api;
4684
- private credentials?;
4685
- private headers?;
4686
- private body?;
4687
- private fetch?;
4688
- private prepareRequest?;
4689
- constructor({ api, credentials, headers, body, fetch, prepareRequest, }: {
4690
- api: string;
4691
- /**
4692
- * The credentials mode to be used for the fetch request.
4693
- * Possible values are: 'omit', 'same-origin', 'include'.
4694
- * Defaults to 'same-origin'.
4695
- */
4696
- credentials?: RequestCredentials;
4738
+ /**
4739
+ The result of a `transcribe` call.
4740
+ It contains the transcript and additional information.
4741
+ */
4742
+ interface TranscriptionResult {
4743
+ /**
4744
+ * The complete transcribed text from the audio.
4745
+ */
4746
+ readonly text: string;
4747
+ /**
4748
+ * Array of transcript segments with timing information.
4749
+ * Each segment represents a portion of the transcribed text with start and end times.
4750
+ */
4751
+ readonly segments: Array<{
4697
4752
  /**
4698
- * HTTP headers to be sent with the API request.
4753
+ * The text content of this segment.
4699
4754
  */
4700
- headers?: Record<string, string> | Headers;
4755
+ readonly text: string;
4701
4756
  /**
4702
- * Extra body object to be sent with the API request.
4703
- * @example
4704
- * Send a `sessionId` to the API along with the messages.
4705
- * ```js
4706
- * useChat({
4707
- * body: {
4708
- * sessionId: '123',
4709
- * }
4710
- * })
4711
- * ```
4757
+ * The start time of this segment in seconds.
4712
4758
  */
4713
- body?: object;
4714
- /**
4715
- Custom fetch implementation. You can use it as a middleware to intercept requests,
4716
- or to provide a custom fetch implementation for e.g. testing.
4717
- */
4718
- fetch?: FetchFunction;
4759
+ readonly startSecond: number;
4719
4760
  /**
4720
- * When a function is provided, it will be used
4721
- * to prepare the request body for the chat API. This can be useful for
4722
- * customizing the request body based on the messages and data in the chat.
4723
- *
4724
- * @param id The id of the chat.
4725
- * @param messages The current messages in the chat.
4726
- * @param requestBody The request body object passed in the chat request.
4761
+ * The end time of this segment in seconds.
4727
4762
  */
4728
- prepareRequest?: NoInfer<PrepareRequest<MESSAGE_METADATA, DATA_TYPES>>;
4729
- });
4730
- submitMessages({ chatId, messages, abortSignal, metadata, headers, body, requestType, }: Parameters<ChatTransport<MESSAGE_METADATA, DATA_TYPES>['submitMessages']>[0]): Promise<ReadableStream<UIMessageStreamPart>>;
4731
- }
4732
-
4733
- type CompletionRequestOptions = {
4734
- /**
4735
- An optional object of headers to be passed to the API endpoint.
4736
- */
4737
- headers?: Record<string, string> | Headers;
4738
- /**
4739
- An optional object to be passed to the API endpoint.
4740
- */
4741
- body?: object;
4742
- };
4743
- type UseCompletionOptions = {
4763
+ readonly endSecond: number;
4764
+ }>;
4744
4765
  /**
4745
- * The API endpoint that accepts a `{ prompt: string }` object and returns
4746
- * a stream of tokens of the AI completion response. Defaults to `/api/completion`.
4766
+ * The detected language of the audio content, as an ISO-639-1 code (e.g., 'en' for English).
4767
+ * May be undefined if the language couldn't be detected.
4747
4768
  */
4748
- api?: string;
4769
+ readonly language: string | undefined;
4749
4770
  /**
4750
- * An unique identifier for the chat. If not provided, a random one will be
4751
- * generated. When provided, the `useChat` hook with the same `id` will
4752
- * have shared states across components.
4771
+ * The total duration of the audio file in seconds.
4772
+ * May be undefined if the duration couldn't be determined.
4753
4773
  */
4754
- id?: string;
4774
+ readonly durationInSeconds: number | undefined;
4755
4775
  /**
4756
- * Initial prompt input of the completion.
4757
- */
4758
- initialInput?: string;
4776
+ Warnings for the call, e.g. unsupported settings.
4777
+ */
4778
+ readonly warnings: Array<TranscriptionWarning>;
4759
4779
  /**
4760
- * Initial completion result. Useful to load an existing history.
4780
+ Response metadata from the provider. There may be multiple responses if we made multiple calls to the model.
4761
4781
  */
4762
- initialCompletion?: string;
4782
+ readonly responses: Array<TranscriptionModelResponseMetadata>;
4763
4783
  /**
4764
- * Callback function to be called when the completion is finished streaming.
4784
+ Provider metadata from the provider.
4765
4785
  */
4766
- onFinish?: (prompt: string, completion: string) => void;
4786
+ readonly providerMetadata: Record<string, Record<string, JSONValue$1>>;
4787
+ }
4788
+
4789
+ /**
4790
+ Generates transcripts using a transcription model.
4791
+
4792
+ @param model - The transcription model to use.
4793
+ @param audio - The audio data to transcribe as DataContent (string | Uint8Array | ArrayBuffer | Buffer) or a URL.
4794
+ @param providerOptions - Additional provider-specific options that are passed through to the provider
4795
+ as body parameters.
4796
+ @param maxRetries - Maximum number of retries. Set to 0 to disable retries. Default: 2.
4797
+ @param abortSignal - An optional abort signal that can be used to cancel the call.
4798
+ @param headers - Additional HTTP headers to be sent with the request. Only applicable for HTTP-based providers.
4799
+
4800
+ @returns A result object that contains the generated transcript.
4801
+ */
4802
+ declare function transcribe({ model, audio, providerOptions, maxRetries: maxRetriesArg, abortSignal, headers, }: {
4767
4803
  /**
4768
- * Callback function to be called when an error is encountered.
4769
- */
4770
- onError?: (error: Error) => void;
4804
+ The transcription model to use.
4805
+ */
4806
+ model: TranscriptionModelV1;
4771
4807
  /**
4772
- * The credentials mode to be used for the fetch request.
4773
- * Possible values are: 'omit', 'same-origin', 'include'.
4774
- * Defaults to 'same-origin'.
4808
+ The audio data to transcribe.
4775
4809
  */
4776
- credentials?: RequestCredentials;
4810
+ audio: DataContent | URL;
4777
4811
  /**
4778
- * HTTP headers to be sent with the API request.
4779
- */
4780
- headers?: Record<string, string> | Headers;
4812
+ Additional provider-specific options that are passed through to the provider
4813
+ as body parameters.
4814
+
4815
+ The outer record is keyed by the provider name, and the inner
4816
+ record is keyed by the provider-specific metadata key.
4817
+ ```ts
4818
+ {
4819
+ "openai": {
4820
+ "temperature": 0
4821
+ }
4822
+ }
4823
+ ```
4824
+ */
4825
+ providerOptions?: ProviderOptions;
4781
4826
  /**
4782
- * Extra body object to be sent with the API request.
4783
- * @example
4784
- * Send a `sessionId` to the API along with the prompt.
4785
- * ```js
4786
- * useChat({
4787
- * body: {
4788
- * sessionId: '123',
4789
- * }
4790
- * })
4791
- * ```
4827
+ Maximum number of retries per transcript model call. Set to 0 to disable retries.
4828
+
4829
+ @default 2
4792
4830
  */
4793
- body?: object;
4831
+ maxRetries?: number;
4794
4832
  /**
4795
- Streaming protocol that is used. Defaults to `data`.
4796
- */
4797
- streamProtocol?: 'data' | 'text';
4833
+ Abort signal.
4834
+ */
4835
+ abortSignal?: AbortSignal;
4798
4836
  /**
4799
- Custom fetch implementation. You can use it as a middleware to intercept requests,
4800
- or to provide a custom fetch implementation for e.g. testing.
4801
- */
4802
- fetch?: FetchFunction;
4803
- };
4837
+ Additional headers to include in the request.
4838
+ Only applicable for HTTP-based providers.
4839
+ */
4840
+ headers?: Record<string, string>;
4841
+ }): Promise<TranscriptionResult>;
4804
4842
 
4805
- export { AbstractChat, AssistantContent, AssistantModelMessage, CallSettings, CallWarning, ChatEvent, ChatInit, ChatRequestOptions, ChatState, ChatStatus, ChatTransport, ChunkDetector, CompletionRequestOptions, CoreAssistantMessage, CoreMessage, CoreSystemMessage, CoreToolMessage, CoreUserMessage, CreateUIMessage, DataContent, DataUIPart, 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, GLOBAL_DEFAULT_PROVIDER, GenerateObjectResult, GenerateTextOnStepFinishCallback, GenerateTextResult, GeneratedAudioFile, GeneratedFile, ImageModel, ImageGenerationWarning as ImageModelCallWarning, ImageModelProviderMetadata, ImageModelResponseMetadata, ImagePart, InferUIDataParts, 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, PrepareStepFunction, PrepareStepResult, Prompt, Provider, ProviderMetadata, ProviderOptions, ProviderRegistryProvider, ReasoningUIPart, RepairTextFunction, RetryError, SerialJobExecutor, SourceUrlUIPart, SpeechModel, SpeechModelResponseMetadata, SpeechWarning, StepResult, StepStartUIPart, StopCondition, StreamObjectOnFinishCallback, StreamObjectResult, StreamTextOnChunkCallback, StreamTextOnErrorCallback, StreamTextOnFinishCallback, StreamTextOnStepFinishCallback, StreamTextResult, StreamTextTransform, SystemModelMessage, TelemetrySettings, TextPart, TextStreamChatTransport, TextStreamPart, TextUIPart, Tool, ToolCallPart, ToolCallRepairError, ToolCallRepairFunction, ToolCallUnion, ToolChoice, ToolContent, ToolExecutionError, ToolExecutionOptions, ToolInvocation, ToolInvocationUIPart, ToolModelMessage, ToolResultPart, ToolResultUnion, ToolSet, TranscriptionModel, TranscriptionModelResponseMetadata, TranscriptionWarning, UIDataPartSchemas, UIDataTypes, UIMessage, UIMessagePart, UIMessageStreamOptions, UIMessageStreamPart, UIMessageStreamWriter, UseCompletionOptions, UserContent, UserModelMessage, assistantModelMessageSchema, callCompletionApi, convertFileListToFileUIParts, convertToCoreMessages, convertToModelMessages, coreAssistantMessageSchema, coreMessageSchema, coreSystemMessageSchema, coreToolMessageSchema, coreUserMessageSchema, cosineSimilarity, createProviderRegistry, createTextStreamResponse, createUIMessageStream, createUIMessageStreamResponse, customProvider, defaultSettingsMiddleware, embed, embedMany, createMCPClient as experimental_createMCPClient, experimental_createProviderRegistry, experimental_customProvider, generateImage as experimental_generateImage, generateSpeech as experimental_generateSpeech, transcribe as experimental_transcribe, extractReasoningMiddleware, generateObject, generateText, getTextFromDataUrl, getToolInvocations, hasToolCall, isDeepEqualData, modelMessageSchema, parsePartialJson, pipeTextStreamToResponse, pipeUIMessageStreamToResponse, simulateReadableStream, simulateStreamingMiddleware, smoothStream, stepCountIs, streamObject, streamText, systemModelMessageSchema, tool, toolModelMessageSchema, userModelMessageSchema, wrapLanguageModel };
4843
+ export { AbstractChat, AssistantContent, AssistantModelMessage, CallSettings, CallWarning, ChatInit, ChatRequestOptions, ChatState, ChatStatus, ChatTransport, ChunkDetector, CompletionRequestOptions, CoreAssistantMessage, CoreMessage, CoreSystemMessage, CoreToolMessage, CoreUserMessage, CreateUIMessage, DataContent, DataUIPart, 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, GLOBAL_DEFAULT_PROVIDER, GenerateObjectResult, GenerateTextOnStepFinishCallback, GenerateTextResult, GeneratedAudioFile, GeneratedFile, ImageModel, ImageGenerationWarning as ImageModelCallWarning, ImageModelProviderMetadata, ImageModelResponseMetadata, ImagePart, InferUIDataParts, 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, PrepareStepFunction, PrepareStepResult, Prompt, Provider, ProviderMetadata, ProviderOptions, ProviderRegistryProvider, ReasoningUIPart, RepairTextFunction, RetryError, SerialJobExecutor, SourceUrlUIPart, SpeechModel, SpeechModelResponseMetadata, SpeechWarning, StepResult, StepStartUIPart, StopCondition, StreamObjectOnFinishCallback, StreamObjectResult, StreamTextOnChunkCallback, StreamTextOnErrorCallback, StreamTextOnFinishCallback, StreamTextOnStepFinishCallback, StreamTextResult, StreamTextTransform, SystemModelMessage, TelemetrySettings, TextPart, TextStreamChatTransport, TextStreamPart, TextUIPart, Tool, ToolCallOptions, ToolCallPart, ToolCallRepairError, ToolCallRepairFunction, ToolCallUnion, ToolChoice, ToolContent, ToolExecutionError, ToolInvocation, ToolInvocationUIPart, ToolModelMessage, ToolResultPart, ToolResultUnion, ToolSet, TranscriptionModel, TranscriptionModelResponseMetadata, TranscriptionWarning, UIDataPartSchemas, UIDataTypes, UIMessage, UIMessagePart, UIMessageStreamOptions, UIMessageStreamPart, UIMessageStreamWriter, UseCompletionOptions, UserContent, UserModelMessage, assistantModelMessageSchema, callCompletionApi, convertFileListToFileUIParts, convertToCoreMessages, convertToModelMessages, coreAssistantMessageSchema, coreMessageSchema, coreSystemMessageSchema, coreToolMessageSchema, coreUserMessageSchema, cosineSimilarity, createProviderRegistry, createTextStreamResponse, createUIMessageStream, createUIMessageStreamResponse, customProvider, defaultSettingsMiddleware, embed, embedMany, createMCPClient as experimental_createMCPClient, experimental_createProviderRegistry, experimental_customProvider, generateImage as experimental_generateImage, generateSpeech as experimental_generateSpeech, transcribe as experimental_transcribe, extractReasoningMiddleware, generateObject, generateText, getTextFromDataUrl, getToolInvocations, hasToolCall, isDeepEqualData, modelMessageSchema, parsePartialJson, pipeTextStreamToResponse, pipeUIMessageStreamToResponse, simulateReadableStream, simulateStreamingMiddleware, smoothStream, stepCountIs, streamObject, streamText, systemModelMessageSchema, tool, toolModelMessageSchema, userModelMessageSchema, wrapLanguageModel };