ai
Version:
AI SDK by Vercel - build apps like ChatGPT, Claude, Gemini, and more with a single interface for any model using the Vercel AI Gateway or go direct to OpenAI, Anthropic, Google, or any other model provider.
3,741 lines • 148 kB
TypeScript
import { Arrayable, AssistantModelMessage, AssistantModelMessage as AssistantModelMessage$1, Context, Experimental_SandboxSession, FetchFunction, FilePart, FlexibleSchema, HasRequiredKey, InferToolContext, InferToolInput, InferToolOutput, InferToolSetContext, MaybePromiseLike, ModelMessage, ModelMessage as ModelMessage$1, ProviderOptions, ReasoningFilePart, ReasoningPart, RetryFunction, ShouldRetryFunction, SystemModelMessage, TextPart, Tool, ToolApprovalRequest, ToolApprovalResponse, ToolExecutionOptions, ToolModelMessage, ToolModelMessage as ToolModelMessage$1, ToolResultOutput, ToolResultPart, ToolSet, UserModelMessage, convertAsyncIteratorToReadableStream } from "@ai-sdk/provider-utils";
import { AISDKError, EmbeddingModelV4Embedding, Experimental_DecisionModelV4CallOptions, Experimental_DecisionModelV4Question, Experimental_DecisionModelV4Result, JSONObject, JSONSchema7, JSONValue, LanguageModelV2, LanguageModelV3, LanguageModelV4, LanguageModelV4CallOptions, LanguageModelV4Citation, LanguageModelV4Content, LanguageModelV4FunctionTool, LanguageModelV4Prompt, LanguageModelV4ProviderTool, LanguageModelV4Source, LanguageModelV4ToolCall, LanguageModelV4ToolChoice, LanguageModelV4ToolResultOutput, LanguageModelV4Usage, ProviderV2, ProviderV3, ProviderV4, SharedV4AudioFormat, SharedV4ProviderMetadata, SharedV4Warning, TranscriptionModelV4TimestampGranularity } from "@ai-sdk/provider";
import { GatewayModelId } from "@ai-sdk/gateway";
import "zod/v4/mini";
import "zod/v4";
//#region src/types/citation.d.ts
/** A reference supporting a generated text part. */
type Citation = LanguageModelV4Citation;
//#endregion
//#region src/types/embedding-model.d.ts
/**
* Embedding.
*/
type Embedding = EmbeddingModelV4Embedding;
//#endregion
//#region src/types/language-model.d.ts
declare global {
/**
* Global interface that can be augmented by third-party packages to register custom model IDs.
*
* You can register model IDs in two ways:
*
* 1. Register based on Model IDs from a provider package:
* @example
* ```typescript
* import { openai } from '@ai-sdk/openai';
* type OpenAIResponsesModelId = Parameters<typeof openai>[0];
*
* declare global {
* interface RegisteredProviderModels {
* openai: OpenAIResponsesModelId;
* }
* }
* ```
*
* 2. Register individual model IDs directly as keys:
* @example
* ```typescript
* declare global {
* interface RegisteredProviderModels {
* 'my-provider:my-model': any;
* 'my-provider:another-model': any;
* }
* }
* ```
*/
interface RegisteredProviderModels {}
}
/**
* Global provider model ID type that defaults to GatewayModelId but can be augmented
* by third-party packages via declaration merging.
*/
type GlobalProviderModelId = [keyof RegisteredProviderModels] extends [never] ? GatewayModelId : keyof RegisteredProviderModels | RegisteredProviderModels[keyof RegisteredProviderModels];
/**
* Language model that is used by the AI SDK.
*/
type LanguageModel = GlobalProviderModelId | LanguageModelV4 | LanguageModelV3 | LanguageModelV2;
/**
* Reason why a language model finished generating a response.
*
* Can be one of the following:
* - `stop`: model generated stop sequence
* - `length`: model generated maximum number of tokens
* - `content-filter`: content filter violation stopped the model
* - `tool-calls`: model triggered tool calls
* - `error`: model stopped because of an error
* - `interrupted`: provider stream ended without a completion signal; output may be incomplete
* - `other`: model stopped for other reasons
*/
type FinishReason = 'stop' | 'length' | 'content-filter' | 'tool-calls' | 'error' | 'interrupted' | 'other';
/**
* Registry for call warning variants. Packages that expose additional warning
* protocols can augment this interface.
*/
interface CallWarningRegistry {
sharedV4: SharedV4Warning;
}
/**
* Warning from the model provider for this call. The call will proceed, but e.g.
* some settings might not be supported, which can lead to suboptimal results.
*/
type CallWarning = CallWarningRegistry[keyof CallWarningRegistry];
/**
* A source that has been used as input to generate the response.
*/
type Source = LanguageModelV4Source;
/**
* Tool choice for the generation. It supports the following settings:
*
* - `auto` (default): the model can choose whether and which tools to call.
* - `required`: the model must call a tool. It can choose which tool to call.
* - `none`: the model must not call tools
* - `{ type: 'tool', toolName: string (typed) }`: the model must call the specified tool
*/
type ToolChoice<TOOLS extends Record<string, unknown>> = 'auto' | 'none' | 'required' | {
type: 'tool';
toolName: Extract<keyof TOOLS, string>;
};
//#endregion
//#region src/types/language-model-request-metadata.d.ts
/**
* Metadata for a language model request.
*/
type LanguageModelRequestMetadata = {
/** HTTP origin used for this request, without URL credentials, paths, queries or fragments. */
readonly url?: string;
/**
* The input messages that were sent to the model for this step.
*/
readonly messages?: Array<ModelMessage>;
/**
* Request HTTP body that was sent to the provider API.
*/
readonly body?: unknown;
};
//#endregion
//#region src/generate-text/response-message.d.ts
/**
* A message that was generated during the generation process.
* It can be either an assistant message or a tool message.
*/
type ResponseMessage = AssistantModelMessage | ToolModelMessage;
//#endregion
//#region src/types/language-model-response-metadata.d.ts
/**
* Metadata for a language model response.
*/
type LanguageModelResponseMetadata = {
/**
* The response messages that were generated during the call.
* Response messages can be either assistant messages or tool messages.
* They contain a generated id.
*/
readonly messages: Array<ResponseMessage>;
/**
* ID for the generated response.
*/
readonly id: string;
/**
* Timestamp for the start of the generated response.
*/
readonly timestamp: Date;
/**
* The ID of the response model that was used to generate the response.
*/
readonly modelId: string;
/**
* Response headers (available only for providers that use HTTP requests).
*/
readonly headers?: Record<string, string>;
/**
* Response body (available only for providers that use HTTP requests).
*/
readonly body?: unknown;
};
//#endregion
//#region src/types/provider-metadata.d.ts
/**
* Additional provider-specific metadata that is returned from the provider.
*
* This is needed to enable provider-specific functionality that can be
* fully encapsulated in the provider.
*/
type ProviderMetadata = SharedV4ProviderMetadata;
//#endregion
//#region src/types/speech-model-response-metadata.d.ts
type SpeechModelResponseMetadata = {
/**
* Timestamp for the start of the generated response.
*/
timestamp: Date;
/**
* The ID of the response model that was used to generate the response.
*/
modelId: string;
/**
* Response headers.
*/
headers?: Record<string, string>;
/**
* Response body.
*/
body?: unknown;
};
//#endregion
//#region src/types/transcription-model-response-metadata.d.ts
type TranscriptionModelResponseMetadata = {
/**
* Timestamp for the start of the generated response.
*/
timestamp: Date;
/**
* The ID of the response model that was used to generate the response.
*/
modelId: string;
/**
* Response headers.
*/
headers?: Record<string, string>;
};
//#endregion
//#region src/types/usage.d.ts
/**
* Represents the number of tokens used in a prompt and completion.
*/
type LanguageModelUsage = {
/**
* The total number of input (prompt) tokens used.
*/
inputTokens: number | undefined;
/**
* Detailed information about the input tokens.
*/
inputTokenDetails: {
/**
* The number of non-cached input (prompt) tokens used.
*/
noCacheTokens: number | undefined;
/**
* The number of cached input (prompt) tokens read.
*/
cacheReadTokens: number | undefined;
/**
* The number of cached input (prompt) tokens written.
*/
cacheWriteTokens: number | undefined;
};
/**
* The number of total output (completion) tokens used.
*/
outputTokens: number | undefined;
/**
* Detailed information about the output tokens.
*/
outputTokenDetails: {
/**
* The number of text tokens used.
*/
textTokens: number | undefined;
/**
* The number of reasoning tokens used.
*/
reasoningTokens: number | undefined;
};
/**
* The total number of tokens used.
*/
totalTokens: number | undefined;
/**
* Raw usage information from the provider.
*
* This is the usage information in the shape that the provider returns.
* It can include additional information that is not part of the standard usage information.
*/
raw?: JSONObject;
};
/**
* Represents the number of tokens used in an embedding.
*/
type EmbeddingModelUsage = {
/**
* The number of tokens used in the embedding.
*/
tokens: number;
};
export declare function asLanguageModelUsage(usage: LanguageModelV4Usage): LanguageModelUsage;
export declare function createNullLanguageModelUsage(): LanguageModelUsage;
export declare function addLanguageModelUsage(usage1: LanguageModelUsage, usage2: LanguageModelUsage): LanguageModelUsage;
//#endregion
//#region src/types/warning.d.ts
/**
* Warning from the model provider for this call. The call will proceed, but e.g.
* some settings might not be supported, which can lead to suboptimal results.
*/
type Warning = SharedV4Warning;
//#endregion
//#region src/logger/log-warnings.d.ts
/**
* A function for logging warnings.
*
* You can assign it to the `AI_SDK_LOG_WARNINGS` global variable to use it as the default warning logger.
*
* @example
* ```ts
* globalThis.AI_SDK_LOG_WARNINGS = (options) => {
* console.log('WARNINGS:', options.warnings, options.provider, options.model);
* };
* ```
*/
type LogWarningsFunction = (options: {
/**
* The warnings returned by the model provider.
*/
warnings: Warning[];
/**
* The provider id used for the call, if scoped to a specific provider.
*/
provider?: string;
/**
* The model id used for the call, if scoped to a specific provider.
*/
model?: string;
}) => void;
/**
* Logs warnings to the console or uses a custom logger if configured.
*
* The behavior can be customized via the `AI_SDK_LOG_WARNINGS` global variable:
* - If set to `false`, warnings are suppressed.
* - If set to a function, that function is called with the warnings.
* - Otherwise, warnings use `process.emitWarning` when available, falling back
* to `console.warn`. Deprecations have stable codes and are emitted once per
* code by the default logger. Custom loggers receive every warning.
*
* @param options - The options containing warnings and context.
* @param options.warnings - The warnings to log.
* @param options.provider - The provider id used for the call, if scoped to a specific provider.
* @param options.model - The model id used for the call, if scoped to a specific provider.
*/
export declare const logWarnings: LogWarningsFunction;
//#endregion
//#region src/embed/embed-events.d.ts
/**
* Event passed to the `onStart` callback for embed and embedMany operations.
*
* Called when the operation begins, before the embedding model is called.
*/
type EmbedStartEvent<RUNTIME_CONTEXT extends Context = Context> = {
/** User-defined runtime context. */
readonly runtimeContext: RUNTIME_CONTEXT;
/** Unique identifier for this embed call, used to correlate events. */
readonly callId: string;
/** Identifies the operation type (e.g. 'ai.embed' or 'ai.embedMany'). */
readonly operationId: string;
/** The provider identifier (e.g., 'openai', 'anthropic'). */
readonly provider: string;
/** The specific model identifier (e.g., 'text-embedding-3-small'). */
readonly modelId: string;
/** The value(s) being embedded. A string for embed, an array for embedMany. */
readonly value: string | Array<string>;
/** Requested output dimensions, when specified. */
readonly dimensions?: number;
/** Maximum number of retries for failed requests. */
readonly maxRetries: number;
/** Additional HTTP headers sent with the request. */
readonly headers: Record<string, string | undefined> | undefined;
/** Additional provider-specific options. */
readonly providerOptions: ProviderOptions | undefined;
};
/**
* Event passed to the `onEnd` callback for embed and embedMany operations.
*
* Called when the operation completes, after the embedding model returns.
*/
type EmbedEndEvent<RUNTIME_CONTEXT extends Context = Context> = {
/** User-defined runtime context. */
readonly runtimeContext: RUNTIME_CONTEXT;
/** Unique identifier for this embed call, used to correlate events. */
readonly callId: string;
/** Identifies the operation type (e.g. 'ai.embed' or 'ai.embedMany'). */
readonly operationId: string;
/** The provider identifier (e.g., 'openai', 'anthropic'). */
readonly provider: string;
/** The specific model identifier (e.g., 'text-embedding-3-small'). */
readonly modelId: string;
/** The value(s) that were embedded. A string for embed, an array for embedMany. */
readonly value: string | Array<string>;
/** The resulting embedding(s). A single vector for embed, an array for embedMany. */
readonly embedding: Embedding | Array<Embedding>;
/** Token usage for the embedding operation. */
readonly usage: EmbeddingModelUsage;
/** Warnings from the embedding model, e.g. unsupported settings. */
readonly warnings: Array<Warning>;
/** Optional provider-specific metadata. */
readonly providerMetadata: ProviderMetadata | undefined;
/** Response data including headers and body. A single response for embed, an array for embedMany. */
readonly response: {
headers?: Record<string, string>;
body?: unknown;
} | Array<{
headers?: Record<string, string>;
body?: unknown;
} | undefined> | undefined;
};
/**
* Event fired when an individual embedding model call (inner operation doEmbed) begins.
*
* For `embed`, there is one call. For `embedMany`, there may be multiple
* calls when values are chunked.
*/
type EmbeddingModelCallStartEvent = {
/** Unique identifier for this embed call, used to correlate events. */
readonly callId: string;
/** Unique identifier for this individual doEmbed invocation, used to correlate start/finish within parallel chunks. */
readonly embedCallId: string;
/** Identifies the inner operation (e.g. 'ai.embed.doEmbed' or 'ai.embedMany.doEmbed'). */
readonly operationId: string;
/** The provider identifier. */
readonly provider: string;
/** The specific model identifier. */
readonly modelId: string;
/** The values being embedded in this particular model call. */
readonly values: Array<string>;
/** Requested output dimensions, when specified. */
readonly dimensions?: number;
};
/**
* Event fired when an individual embedding model call (doEmbed) completes.
*
* Contains the embeddings, usage, and any warnings from the model response.
*/
type EmbeddingModelCallEndEvent = {
/** Unique identifier for this embed call, used to correlate events. */
readonly callId: string;
/** Unique identifier for this individual doEmbed invocation, used to correlate start/finish within parallel chunks. */
readonly embedCallId: string;
/** Identifies the inner operation (e.g. 'ai.embed.doEmbed' or 'ai.embedMany.doEmbed'). */
readonly operationId: string;
/** The provider identifier. */
readonly provider: string;
/** The specific model identifier. */
readonly modelId: string;
/** The values that were embedded in this particular model call. */
readonly values: Array<string>;
/** The resulting embeddings from the model call. */
readonly embeddings: Array<Embedding>;
/** Token usage for this model call. */
readonly usage: EmbeddingModelUsage;
};
//#endregion
//#region src/util/download/download.d.ts
/**
* Download a file from a URL.
*
* @param url - The URL to download from.
* @param maxBytes - Maximum allowed download size in bytes. Defaults to 100 MiB.
* @param abortSignal - An optional abort signal to cancel the download.
* @param fetch - An optional fetch transport.
* @returns The downloaded data and media type.
*
* @throws DownloadError if the download fails or exceeds maxBytes.
*/
declare const download$1: ({ url, maxBytes, abortSignal, fetch }: {
url: URL;
maxBytes?: number;
abortSignal?: AbortSignal;
fetch?: FetchFunction;
}) => Promise<{
data: Uint8Array<ArrayBufferLike>;
mediaType: string | undefined;
}>;
//#endregion
//#region src/util/download/download-function.d.ts
/**
* Experimental. Can change in patch versions without warning.
*
* Download function. Called with the array of URLs and a boolean indicating
* whether the URL is supported by the model.
*
* The download function can decide for each URL:
* - to return null (which means that the URL should be passed to the model)
* - to download the asset and return the data (incl. retries, authentication, etc.)
*
* Should throw DownloadError if the download fails.
*
* Should return an array of objects sorted by the order of the requested downloads.
* For each object, the data should be a Uint8Array if the URL was downloaded.
* For each object, the mediaType should be the media type of the downloaded asset.
* For each object, the data should be null if the URL should be passed through as is.
*/
type DownloadFunction = (options: Array<{
url: URL;
isUrlSupportedByModel: boolean;
/** Signal for cancellation and timeouts of the enclosing call. */
abortSignal?: AbortSignal;
}>) => PromiseLike<Array<{
data: Uint8Array;
mediaType: string | undefined;
} | null>>;
/**
* Default download function.
* Downloads the file if it is not supported by the model.
*/
export declare const createDefaultDownloadFunction: (download?: typeof download$1, abortSignal?: AbortSignal) => DownloadFunction;
//#endregion
//#region src/decide/decision-state.d.ts
/** Shared text, JSON data, or ordered multimodal parts for a decision. */
type DecisionState = string | Readonly<JSONObject> | readonly DecisionStatePart[];
type DecisionStatePart = TextPart | FilePart | {
type: 'json';
value: JSONValue;
};
//#endregion
//#region src/decide/decision-result.d.ts
type DecisionAnswer<QUESTION extends Experimental_DecisionModelV4Question> = QUESTION extends {
type: 'choice';
criteria: infer CRITERIA;
} ? {
type: 'choice';
choice: Extract<keyof CRITERIA, string>;
probabilities?: Record<Extract<keyof CRITERIA, string>, number>;
} : QUESTION extends {
type: 'score';
} ? {
type: 'score';
score: number;
probabilities?: Record<string, number>;
} : QUESTION extends {
type: 'box';
} ? {
type: 'box';
box: {
top: number;
bottom: number;
left: number;
right: number;
};
} : {
type: 'boolean';
probability: number;
};
type DecisionResult<QUESTIONS extends Record<string, Experimental_DecisionModelV4Question>> = {
readonly answers: { [ID in keyof QUESTIONS]: DecisionAnswer<QUESTIONS[ID]>; };
readonly usage: {
inputTokens: number | undefined;
outputTokens: number | undefined;
totalTokens: number | undefined;
};
readonly warnings: Experimental_DecisionModelV4Result['warnings'];
readonly rounding: Experimental_DecisionModelV4Result['rounding'];
readonly providerMetadata: Experimental_DecisionModelV4Result['providerMetadata'];
readonly response: NonNullable<Experimental_DecisionModelV4Result['response']> & {
timestamp: Date;
modelId: string;
};
};
//#endregion
//#region src/decide/decide-events.d.ts
/**
* Event passed to the `onStart` callback for decision operations.
*
* Called when the operation begins, before the decision model is called.
*/
type DecideStartEvent<RUNTIME_CONTEXT extends Context = Context> = {
/** User-defined runtime context. */
readonly runtimeContext: RUNTIME_CONTEXT;
/** Unique identifier for this decision call, used to correlate events. */
readonly callId: string;
/** Identifies the operation type (`ai.decide`). */
readonly operationId: 'ai.decide';
/** The provider identifier. */
readonly provider: string;
/** The decision model identifier. */
readonly modelId: string;
/** The shared state used to make decisions. */
readonly state: DecisionState;
/** The questions to decide against the shared state. */
readonly questions: Readonly<Record<string, Experimental_DecisionModelV4Question>>;
/** Maximum output tokens requested for the decision. */
readonly maxOutputTokens?: number;
/** Reasoning effort requested for the decision. */
readonly reasoning?: Experimental_DecisionModelV4CallOptions['reasoning'];
/** Maximum number of retries for the decision model call. */
readonly maxRetries: number;
/** Additional HTTP headers sent with the request. */
readonly headers: Record<string, string> | undefined;
/** Additional provider-specific options. */
readonly providerOptions: ProviderOptions;
};
/**
* Event passed to the `onEnd` callback for decision operations.
*
* Called when the operation completes successfully.
*/
type DecideEndEvent<RUNTIME_CONTEXT extends Context = Context> = DecideStartEvent<RUNTIME_CONTEXT> & {
/** Exactly one typed answer per question ID. */
readonly answers: DecisionResult<Record<string, Experimental_DecisionModelV4Question>>['answers'];
/** Token usage for the decision operation. */
readonly usage: DecisionResult<Record<string, Experimental_DecisionModelV4Question>>['usage'];
/** Warnings from the decision model. */
readonly warnings: DecisionResult<Record<string, Experimental_DecisionModelV4Question>>['warnings'];
/** Provider-declared decimal precision for probabilities and scores. */
readonly rounding: DecisionResult<Record<string, Experimental_DecisionModelV4Question>>['rounding'];
/** Optional provider-specific metadata. */
readonly providerMetadata: DecisionResult<Record<string, Experimental_DecisionModelV4Question>>['providerMetadata'];
/** Response metadata, including the resolved model ID and timestamp. */
readonly response: DecisionResult<Record<string, Experimental_DecisionModelV4Question>>['response'];
};
/**
* Event fired when the decision model call begins.
*
* The logical model call includes any provider retries.
*/
type DecisionModelCallStartEvent = {
/** Unique identifier for the outer decision call. */
readonly callId: string;
/** Identifies the inner operation (`ai.decide.doDecide`). */
readonly operationId: 'ai.decide.doDecide';
/** The provider identifier. */
readonly provider: string;
/** The decision model identifier. */
readonly modelId: string;
/** The shared state used to make decisions. */
readonly state: Experimental_DecisionModelV4CallOptions['state'];
/** The questions to decide against the shared state. */
readonly questions: Experimental_DecisionModelV4CallOptions['questions'];
};
/**
* Event fired after the decision model response has been validated.
*
* Contains the result of the logical model call, including any retries.
*/
type DecisionModelCallEndEvent = DecisionModelCallStartEvent & {
/** Exactly one answer per question ID. */
readonly answers: Experimental_DecisionModelV4Result['answers'];
/** Token usage reported by the decision model. */
readonly usage?: Experimental_DecisionModelV4Result['usage'];
/** Warnings from the decision model. */
readonly warnings: Experimental_DecisionModelV4Result['warnings'];
/** Provider-declared decimal precision for probabilities and scores. */
readonly rounding?: Experimental_DecisionModelV4Result['rounding'];
/** Optional provider-specific metadata. */
readonly providerMetadata?: Experimental_DecisionModelV4Result['providerMetadata'];
/** Optional raw response metadata from the provider. */
readonly response?: Experimental_DecisionModelV4Result['response'];
};
//#endregion
//#region src/prompt/language-model-call-options.d.ts
/**
* Model-facing generation controls. These settings influence how the model
* generates its response (token limits, sampling, penalties, stop sequences,
* seed, reasoning).
*/
type LanguageModelCallOptions = {
/**
* Maximum number of tokens to generate.
*/
maxOutputTokens?: number;
/**
* Temperature setting. The range depends on the provider and model.
*
* It is recommended to set either `temperature` or `topP`, but not both.
*/
temperature?: number;
/**
* Nucleus sampling. This is a number between 0 and 1.
*
* E.g. 0.1 would mean that only tokens with the top 10% probability mass
* are considered.
*
* It is recommended to set either `temperature` or `topP`, but not both.
*/
topP?: number;
/**
* Only sample from the top K options for each subsequent token.
*
* Used to remove "long tail" low probability responses.
* Recommended for advanced use cases only. You usually only need to use temperature.
*/
topK?: number;
/**
* Presence penalty setting. It affects the likelihood of the model to
* repeat information that is already in the prompt.
*
* The presence penalty is a number between -1 (increase repetition)
* and 1 (maximum penalty, decrease repetition). 0 means no penalty.
*/
presencePenalty?: number;
/**
* Frequency penalty setting. It affects the likelihood of the model
* to repeatedly use the same words or phrases.
*
* The frequency penalty is a number between -1 (increase repetition)
* and 1 (maximum penalty, decrease repetition). 0 means no penalty.
*/
frequencyPenalty?: number;
/**
* Stop sequences.
* If set, the model will stop generating text when one of the stop sequences is generated.
* Providers may have limits on the number of stop sequences.
*/
stopSequences?: string[];
/**
* The seed (integer) to use for random sampling. If set and supported
* by the model, calls will generate deterministic results.
*/
seed?: number;
/**
* Reasoning effort level for the model. Controls how much reasoning
* the model performs before generating a response.
*
* Use `'provider-default'` to use the provider's default reasoning level.
* Use `'none'` to disable reasoning (if supported by the provider).
*/
reasoning?: LanguageModelV4CallOptions['reasoning'];
};
//#endregion
//#region src/prompt/request-options.d.ts
/**
* Timeout configuration for API calls. Can be specified as:
* - A number representing milliseconds
* - An object with `totalMs` property for the total timeout in milliseconds
* - An object with `stepMs` property for the timeout of each complete step, including model generation, retries, and local tool execution, in milliseconds
* - An object with `firstChunkMs` property for the timeout until the first content chunk of each step (streaming only)
* - An object with `chunkMs` property for the timeout between meaningful model output chunks, starting with the first output and ending when the model response ends (streaming only)
* - An object with `toolMs` property for the default timeout for all tool executions
* - An object with `tools` property for per-tool timeout overrides using `{toolName}Ms` keys
*/
type TimeoutConfiguration<TOOLS extends ToolSet> = number | {
totalMs?: number;
stepMs?: number;
firstChunkMs?: number;
chunkMs?: number;
toolMs?: number;
tools?: Partial<Record<`${keyof TOOLS & string}Ms`, number>>;
};
//#endregion
//#region src/prompt/prompt.d.ts
/**
* Instructions to include in the prompt. Can be used with `prompt` or `messages`.
*/
type Instructions = string | SystemModelMessage | Array<SystemModelMessage>;
/**
* Prompt part of the AI function options.
* It contains instructions, a simple text prompt, or a list of messages.
*/
type Prompt = {
/**
* Instructions to include in the prompt. Can be used with `prompt` or `messages`.
*/
instructions?: Instructions;
/**
* Instructions to include in the prompt. Can be used with `prompt` or `messages`.
*
* @deprecated Use `instructions` instead.
*/
system?: Instructions;
/**
* Whether system messages are allowed in the `prompt` or `messages` fields.
*
* When disabled, system messages must be provided through the `instructions`
* option.
*
* @default false
*/
allowSystemInMessages?: boolean;
} & ({
/**
* A prompt. It can be either a text prompt or a list of messages.
*
* You can either use `prompt` or `messages` but not both.
*/
prompt: string | Array<ModelMessage>;
/**
* A list of messages.
*
* You can either use `prompt` or `messages` but not both.
*/
messages?: never;
} | {
/**
* A list of messages.
*
* You can either use `prompt` or `messages` but not both.
*/
messages: Array<ModelMessage>;
/**
* A prompt. It can be either a text prompt or a list of messages.
*
* You can either use `prompt` or `messages` but not both.
*/
prompt?: never;
});
//#endregion
//#region src/generate-object/structured-output-events.d.ts
/**
* Event passed to the `onStart` callback of
* `generateObject` and `streamObject`.
*
* Called when the operation begins, before any LLM call.
*
* @deprecated
*/
interface GenerateObjectStartEvent {
/** Unique identifier for this generation call, used to correlate events. */
readonly callId: string;
/** Identifies the operation type (e.g. `'ai.generateObject'` or `'ai.streamObject'`). */
readonly operationId: string;
/** The provider identifier (e.g., 'openai', 'anthropic'). */
readonly provider: string;
/** The specific model identifier (e.g., 'gpt-4o'). */
readonly modelId: string;
/** The system message(s) provided to the model. */
readonly system: Instructions | undefined;
/** The prompt string or array of messages if using the prompt option. */
readonly prompt: string | Array<ModelMessage> | undefined;
/** The messages array if using the messages option. */
readonly messages: Array<ModelMessage> | undefined;
/** Maximum number of tokens to generate. */
readonly maxOutputTokens: number | undefined;
/** Sampling temperature for generation. */
readonly temperature: number | undefined;
/** Top-p (nucleus) sampling parameter. */
readonly topP: number | undefined;
/** Top-k sampling parameter. */
readonly topK: number | undefined;
/** Presence penalty for generation. */
readonly presencePenalty: number | undefined;
/** Frequency penalty for generation. */
readonly frequencyPenalty: number | undefined;
/** Random seed for reproducible generation. */
readonly seed: number | undefined;
/** Maximum number of retries for failed requests. */
readonly maxRetries: number;
/** Additional HTTP headers sent with the request. */
readonly headers: Record<string, string | undefined> | undefined;
/** Additional provider-specific options. */
readonly providerOptions: ProviderOptions | undefined;
/** The output strategy type. */
readonly output: 'object' | 'array' | 'enum' | 'no-schema';
/** The JSON Schema used for object generation, if any. */
readonly schema: Record<string, unknown> | undefined;
/** Optional name of the schema. */
readonly schemaName: string | undefined;
/** Optional description of the schema. */
readonly schemaDescription: string | undefined;
}
/**
* Event passed to the `onStepStart` callback of
* `generateObject` and `streamObject`.
*
* Called when the model call (step) begins, before the provider is called.
* For object generation, there is always exactly one step (step 0).
*
* @deprecated
*/
interface GenerateObjectStepStartEvent {
/** Unique identifier for this generation call, used to correlate events. */
readonly callId: string;
/** Zero-based index of the current step. Always `0` for object generation. */
readonly stepNumber: 0;
/** The provider identifier (e.g., 'openai', 'anthropic'). */
readonly provider: string;
/** The specific model identifier (e.g., 'gpt-4o'). */
readonly modelId: string;
/** Additional provider-specific options. */
readonly providerOptions: ProviderOptions | undefined;
/** Additional HTTP headers sent with the request. */
readonly headers: Record<string, string | undefined> | undefined;
/** The prompt messages in provider format (for telemetry). */
readonly promptMessages?: LanguageModelV4Prompt;
}
/**
* Event passed to the `onStepEnd` callback of
* `generateObject` and `streamObject`.
*
* Called when the model call (step) completes, with the raw result
* before JSON parsing and schema validation.
*
* @deprecated
*/
interface GenerateObjectStepEndEvent {
/** Unique identifier for this generation call, used to correlate events. */
readonly callId: string;
/** Zero-based index of the current step. Always `0` for object generation. */
readonly stepNumber: 0;
/** The provider identifier (e.g., 'openai', 'anthropic'). */
readonly provider: string;
/** The specific model identifier (e.g., 'gpt-4o'). */
readonly modelId: string;
/** The unified reason why the generation finished. */
readonly finishReason: FinishReason;
/** The token usage of the generated response. */
readonly usage: LanguageModelUsage;
/** The raw text output from the model (before JSON parsing). */
readonly objectText: string;
/** The reasoning generated by the model, if any. */
readonly reasoning: string | undefined;
/** Warnings from the model provider (e.g. unsupported settings). */
readonly warnings: CallWarning[] | undefined;
/** Additional request information. */
readonly request: Omit<LanguageModelRequestMetadata, 'messages'>;
/** Additional response information. */
readonly response: Omit<LanguageModelResponseMetadata, 'messages'>;
/** Additional provider-specific metadata. */
readonly providerMetadata: ProviderMetadata | undefined;
/** Milliseconds from the start of the stream to the first chunk (streaming only). */
readonly msToFirstChunk: number | undefined;
}
/**
* Event passed to the `onFinish` callback of
* `generateObject` and `streamObject`.
*
* Called when the entire operation completes, including JSON parsing
* and schema validation. For `streamObject`, the object may be undefined
* if validation failed (the error is provided in that case).
*
* @deprecated
*/
interface GenerateObjectEndEvent<RESULT> {
/** Unique identifier for this generation call, used to correlate events. */
readonly callId: string;
/**
* The generated object (typed according to the schema).
* Always defined for `generateObject`. May be `undefined` for `streamObject`
* when parsing or validation fails.
*/
readonly object: RESULT | undefined;
/**
* Error from parsing or schema validation, if any.
* Always `undefined` for `generateObject` (which throws instead).
*/
readonly error: unknown | undefined;
/** The reasoning generated by the model, if any. */
readonly reasoning: string | undefined;
/** The unified reason why the generation finished. */
readonly finishReason: FinishReason;
/** The token usage of the generated response. */
readonly usage: LanguageModelUsage;
/** Warnings from the model provider (e.g. unsupported settings). */
readonly warnings: CallWarning[] | undefined;
/** Additional request information. */
readonly request: Omit<LanguageModelRequestMetadata, 'messages'>;
/** Additional response information. */
readonly response: Omit<LanguageModelResponseMetadata, 'messages'>;
/** Additional provider-specific metadata. */
readonly providerMetadata: ProviderMetadata | undefined;
}
//#endregion
//#region src/prompt/standardize-prompt.d.ts
type StandardizedPrompt = {
/**
* Instructions.
*/
instructions: Instructions | undefined;
/**
* Messages.
*/
messages: ModelMessage[];
};
/**
* Converts a prompt input into a standardized prompt with model messages.
*
* @param prompt - The prompt definition to standardize.
* Set `allowSystemInMessages` to true to allow system messages in the
* `prompt` or `messages` fields. System messages in the `instructions`
* option are always allowed.
* @returns The standardized prompt.
* @throws {InvalidPromptError} When the prompt is invalid.
*/
export declare function standardizePrompt({ allowSystemInMessages, system, instructions, prompt, messages }: Prompt): Promise<StandardizedPrompt>;
//#endregion
//#region src/util/callback.d.ts
/**
* A callback function that can be used with `notify`.
*/
type Callback<EVENT> = (event: EVENT) => PromiseLike<void> | void;
//#endregion
//#region src/generate-text/active-tools.d.ts
/**
* Tool names that are enabled for a generation step.
*
* `undefined` means no tool restriction is applied. Tool names are object keys
* at runtime, so the type is restricted to the string keys of the configured
* tool set.
*/
type ActiveTools<TOOLS extends ToolSet> = ReadonlyArray<keyof TOOLS & string> | undefined;
//#endregion
//#region src/telemetry/telemetry-options.d.ts
type IncludedContext<CONTEXT extends Context | unknown | never> = { [KEY in keyof NoInfer<CONTEXT>]?: boolean; } | undefined;
type IncludedToolsContext<TOOLS extends ToolSet> = { [TOOL_NAME in keyof NoInfer<InferToolSetContext<TOOLS>>]?: IncludedContext<NoInfer<InferToolSetContext<TOOLS>[TOOL_NAME]>>; } | undefined;
/**
* Telemetry configuration.
*/
type TelemetryOptions<RUNTIME_CONTEXT extends Context = Context, TOOLS extends ToolSet = ToolSet> = {
/**
* Enable or disable telemetry. Enabled by default when a telemetry
* integration is registered. Set to `false` to opt out.
*/
isEnabled?: boolean;
/**
* Enable or disable input recording. Enabled by default.
*
* You might want to disable input recording to avoid recording sensitive
* information, to reduce data transfers, or to increase performance.
*/
recordInputs?: boolean;
/**
* Enable or disable output recording. Enabled by default.
*
* You might want to disable output recording to avoid recording sensitive
* information, to reduce data transfers, or to increase performance.
*/
recordOutputs?: boolean;
/**
* Identifier for this function. Used to group telemetry data by function.
*/
functionId?: string;
/**
* Top-level runtime context properties that should be included in telemetry.
* Runtime context properties are excluded unless they are explicitly set to `true`.
*/
includeRuntimeContext?: IncludedContext<RUNTIME_CONTEXT>;
/**
* Top-level tool context properties that should be included in telemetry,
* configured per tool.
*
* Tool context properties are excluded unless they are explicitly set to `true`.
*/
includeToolsContext?: IncludedToolsContext<TOOLS>;
/**
* Per-call telemetry integrations that receive lifecycle events during generation.
*
* When provided, these integrations will take precedence over the globally registered
* integrations for this call.
*/
integrations?: Arrayable<Telemetry>;
};
//#endregion
//#region src/generate-text/generated-file.d.ts
/**
* A generated file.
*/
interface GeneratedFile {
/**
* File as a base64 encoded string.
*/
readonly base64: string;
/**
* File as a Uint8Array.
*/
readonly uint8Array: Uint8Array;
/**
* The IANA media type of the file.
*
* @see https://www.iana.org/assignments/media-types/media-types.xhtml
*/
readonly mediaType: string;
/**
* Provider-specific metadata for this file.
*/
readonly providerMetadata?: Record<string, JSONObject>;
}
//#endregion
//#region src/generate-text/reasoning-output.d.ts
/**
* Reasoning output of a text generation. It contains a reasoning.
*/
interface ReasoningOutput {
type: 'reasoning';
/**
* The reasoning text.
*/
text: string;
/**
* Additional provider-specific metadata. They are passed through
* to the provider from the AI SDK and enable provider-specific
* functionality that can be fully encapsulated in the provider.
*/
providerMetadata?: ProviderMetadata;
}
/**
* Reasoning file output of a text generation.
* It contains a file generated as part of reasoning.
*/
interface ReasoningFileOutput {
type: 'reasoning-file';
/**
* The generated file.
*/
file: GeneratedFile;
/**
* Additional provider-specific metadata. They are passed through
* to the provider from the AI SDK and enable provider-specific
* functionality that can be fully encapsulated in the provider.
*/
providerMetadata?: ProviderMetadata;
}
//#endregion
//#region src/util/value-of.d.ts
/**
* Create a union of the given object's values, and optionally specify which keys to get the values from.
*
* Please upvote [this issue](https://github.com/microsoft/TypeScript/issues/31438) if you want to have this type as a built-in in TypeScript.
*
* @example
* ```
* // data.json
* {
* 'foo': 1,
* 'bar': 2,
* 'biz': 3
* }
*
* // main.ts
* import type {ValueOf} from 'type-fest';
* import data = require('./data.json');
*
* export function getData(name: string): ValueOf<typeof data> {
* return data[name];
* }
*
* export function onlyBar(name: string): ValueOf<typeof data, 'bar'> {
* return data[name];
* }
*
* // file.ts
* import {getData, onlyBar} from './main';
*
* getData('foo');
* //=> 1
*
* onlyBar('foo');
* //=> TypeError ...
*
* onlyBar('bar');
* //=> 2
* ```
* @see https://github.com/sindresorhus/type-fest/blob/main/source/value-of.d.ts
*/
type ValueOf<ObjectType, ValueType extends keyof ObjectType = keyof ObjectType> = ObjectType[ValueType];
//#endregion
//#region src/generate-text/tool-call.d.ts
type BaseToolCall = {
type: 'tool-call';
toolCallId: string;
providerExecuted?: boolean;
providerMetadata?: ProviderMetadata;
toolMetadata?: JSONObject;
};
/**
* A tool call whose `toolName` maps to a tool in the declared tool set,
* with an `input` type inferred from that tool's input schema.
*/
type StaticToolCall<TOOLS extends ToolSet> = ValueOf<{ [NAME in keyof TOOLS]-?: BaseToolCall & {
toolName: NAME & string;
input: InferToolInput<TOOLS[NAME]>;
dynamic?: false | undefined;
invalid?: false | undefined;
error?: never;
title?: string;
}; }>;
/**
* A tool call whose `toolName` is only known at runtime, such as an invalid
* or otherwise untyped call that cannot be matched to the declared tool set.
*/
type DynamicToolCall = BaseToolCall & {
toolName: string;
input: unknown;
dynamic: true;
title?: string;
/**
* True if this is caused by an unparsable tool call or
* a tool that does not exist.
*/
invalid?: boolean;
/**
* The error that caused the tool call to be invalid.
*/
error?: unknown;
};
/**
* A tool call returned by text generation, either statically typed from the
* declared tool set or dynamically typed when the tool cannot be inferred.
*/
type TypedToolCall<TOOLS extends ToolSet> = StaticToolCall<TOOLS> | DynamicToolCall;
//#endregion
//#region src/generate-text/tool-approval-request-output.d.ts
/**
* Output part that indicates that a tool approval request has been made.
*
* The tool approval request can be approved or denied in the next tool message.
*/
type ToolApprovalRequestOutput<TOOLS extends ToolSet> = {
type: 'tool-approval-request';
/**
* ID of the tool approval request.
*/
approvalId: string;
/**
* Tool call that the approval request is for.
*/
toolCall: TypedToolCall<TOOLS>;
/**
* Reason why the tool call requires approval.
*/
reason?: string;
/**
* Flag indicating whether the tool was automatically approved or denied.
*
* @default false
*/
isAutomatic?: boolean;
/**
* HMAC-SHA256 signature binding this approval request to its tool call.
*/
signature?: string;
};
//#endregion
//#region src/generate-text/tool-approval-response-output.d.ts
/**
* Output part that indicates that a tool approval response is available.
*/
type ToolApprovalResponseOutput<TOOLS extends ToolSet> = {
type: 'tool-approval-response';
/**
* ID of the tool approval.
*/
approvalId: string;
/**
* Tool call that the approval response is for.
*/
toolCall: TypedToolCall<TOOLS>;
/**
* Flag indicating whether the approval was granted or denied.
*/
approved: boolean;
/**
* Optional reason for the approval or denial.
*/
reason?: string;
/**
* Flag indicating whether the tool call is provider-executed.
* Only provider-executed tool approval responses should be sent to the model.
*/
providerExecuted?: boolean;
};
//#endregion
//#region src/generate-text/tool-error.d.ts
type StaticToolError<TOOLS extends ToolSet> = ValueOf<{ [NAME in keyof TOOLS]-?: {
type: 'tool-error';
toolCallId: string;
toolName: NAME & string;
input: InferToolInput<TOOLS[NAME]>;
error: unknown;
providerExecuted?: boolean;
providerMetadata?: ProviderMetadata;
toolMetadata?: JSONObject;
dynamic?: false | undefined;
title?: string;
}; }>;
type DynamicToolError = {
type: 'tool-error';
toolCallId: string;
toolName: string;
input: unknown;
error: unknown;
providerExecuted?: boolean;
providerMetadata?: ProviderMetadata;
toolMetadata?: JSONObject;
dynamic: true;
title?: string;
};
type TypedToolError<TOOLS extends ToolSet> = StaticToolError<TOOLS> | DynamicToolError;
//#endregion
//#region src/generate-text/tool-result.d.ts
type StaticToolResult<TOOLS extends ToolSet> = ValueOf<{ [NAME in keyof TOOLS]-?: {
type: 'tool-result';
toolCallId: string;
toolName: NAME & string;
input: InferToolInput<TOOLS[NAME]>;
output: InferToolOutput<TOOLS[NAME]>;
providerExecuted?: boolean;
providerMetadata?: ProviderMetadata;
toolMetadata?: JSONObject;
dynamic?: false | undefined;
preliminary?: boolean;
title?: string;
}; }>;
type DynamicToolResult = {
type: 'tool-result';
toolCallId: string;
toolName: string;
input: unknown;
output: unknown;
providerExecuted?: boolean;
providerMetadata?: ProviderMetadata;
toolMetadata?: JSONObject;
dynamic: true;
preliminary?: boolean;
title?: string;
};
type TypedToolResult<TOOLS extends ToolSet> = StaticToolResult<TOOLS> | DynamicToolResult;
//#endregion
//#region src/generate-text/content-part.d.ts
type ContentPart<TOOLS extends ToolSet> = {
type: 'text';
text: string;
citations?: Array<Citation>;
providerMetadata?: ProviderMetadata;
} | {
type: 'custom';
kind: `${string}.${string}`;
providerMetadata?: ProviderMetadata;
} | ReasoningOutput | ReasoningFileOutput | ({
type: 'source';
} & Source) | {
type: 'file';
file: GeneratedFile;
providerMetadata?: ProviderMetadata;
} | ({
type: 'tool-call';
} & TypedToolCall<TOOLS> & {
providerMetadata?: ProviderMetadata;
}) | ({
type: 'tool-result';
} & TypedToolResult<TOOLS> & {
providerMetadata?: ProviderMetadata;
}) | ({
type: 'tool-error';
} & TypedToolError<TOOLS> & {
providerMetadata?: ProviderMetadata;
}) | ToolApprovalRequestOutput<TOOLS> | ToolApprovalResponseOutput<TOOLS>;
//#endregion
//#region src/generate-text/step-result.d.ts
/**
* Timing statistics for the gaps between generated output chunks.
*/
type OutputChunkTimingStats = {
/** Shortest observed time between output chunks in milliseconds. */
readonly min: number;
/** 10th percentile time between output chunks in milliseconds. */
readonly p10: number;
/** Median time between output chunks in milliseconds. */
readonly median: number;
/** Average time between output chunks in milliseconds. */
readonly avg: number;
/** 90th percentile time between output chunks in milliseconds. */
readonly p90: number;
/** Longest observed time between output chunks in milliseconds. */
readonly max: number;
};
/**
* Performance metrics for a single step in the generation process.
*/
type StepResultPerformance = {
/**
* Effective number of output tokens per second over the full language model
* response.
*
* Calculated as `outputTokens / requestSeconds`.
*/
readonly effectiveOutputTokensPerSecond: number;
/**
* Number of output tokens per second after the first generated output chunk
* was received.
*
* Only available for streaming steps.
*
* Calculated as `outputTokens / outputStreamSeconds`.
*/
readonly outputTokensPerSecond: number | undefined;
/**
* Number of input tokens processed per second before the first generated
* output chunk was received.
*
* Only available for streaming steps.
*
* Calculated as `inputTokens / ttftSeconds`.
*/
readonly inputTokensPerSecond: number | undefined;
/**
* Effective number of input and output tokens per second over the full
* language model response.
*
* Calculated as `(inputTokens + outputTokens) / requestSeconds`.
*/
readonly effectiveTotalTokensPerSecond: number;
/**
* Total time spent on the step in milliseconds.
*/
readonly stepTimeMs: number;
/**
* Time spent waiting for the language model response in milliseconds.
*/
readonly responseTimeMs: number;
/**
* Time spent executing each client-side tool call in milliseconds, keyed by
* tool call ID.
*/
readonly toolExecutionMs: Readonly<Record<string, number>>;
/**
* Time until the first generated output chunk was received in milliseconds.
*
* This includes text deltas, reasoning deltas, generated files, reasoning
* files, tool input deltas, and tool calls.
*
* Only available for streaming steps.
*/
readonly timeToFirstOutputMs: number | undefined;
/**
* Timing statistics for the gaps between generated output chunks in
* milliseconds.
*
* Only available for streaming steps with at least two generated output
* chunks.
*/
readonly timeBetweenOutputChunksMs?: OutputChunkTimingStats;
};
/**
* The result of a single step in the generation process.
*/
type StepResult<TOOLS extends ToolSet, RUNTIME_CONTEXT extends Context = Context> = {
/**
* Unique identifier for the generation call this step belongs to.
*/
readonly callId: string;
/**
* Zero-based index of this step.
*/
readonly stepNumber: number;
/**
* Information about the model that produced this step.
*/
readonly model: {
/** The provider of the model. */
readonly provider: string;
/** The ID of the model. */
readonly modelId: string;
};
/**
* Tool context.
*/
readonly toolsContext: InferToolSetContext<TOOLS>;
/**
* The runtime context that was used as input for the step.
*/
readonly runtimeContext: RUNTIME_CONTEXT;
/**
* The content that was generated in the last step.
*/
readonly content: Array<ContentPart<TOOLS>>;
/**
* The concatenation of all text parts generated in this step.
* It is an empty string if the step contains no text parts.
*/
readonly text: string;
/**
* The reasoning that was generated during the generation.
*/
readonly reasoning: Array<ReasoningPart | ReasoningFilePart>;
/**
* The reasoning text that was generated during the generation.
*
* It is a concatenation of all reasoning parts (but excluding reasoning file parts).
* Can be undefined if the model has only generated text.
*/
readonly reasoningText: string | undefined;
/**
* The files that were generated during the generation.
*/
readonly files: Array<GeneratedFile>;
/**
* The sources that were used to generate the text.
*/
readonly sources: Array<Source>;
/**
* The tool calls that were made during the generation.
*/
readonly toolCalls: Array<TypedToolCall<TOOLS>>;
/**
* The static tool calls that were made in the last step.
*/
readonly staticToolCalls: Array<StaticToolCall<TOOLS>>;
/**
* The dynamic tool calls that were made in the last step.
*/
readonly dynamicToolCalls: Array<DynamicToolCall>;
/**
* The results of the tool calls.
*/
readonly toolResults: Array<TypedToolResult<TOOLS>>;
/**
* The static tool results that were made in the last step.
*/
readonly staticToolResults: Array<StaticToolResult<TOOLS>>;
/**
* The dynamic tool results that were made in the last step.
*/
readonly dynamicToolResults: Array<DynamicToolResult>;
/**
* The unified reason why the generation finished.
*/
readonly finishReason: FinishReason;
/**
* The raw reason why the generation finished (from the provider).
*/
readonly rawFinishReason: string | undefined;
/**
* The token usage of the generated text.
*/
readonly usage: LanguageModelUsage;
/**
* Performance metrics for the step.
*/
readonly performance: StepResultPerformance;
/**
* Warnings from the model provider (e.g. unsupported settings).
*/
readonly warnings: CallWarning[] | undefined;
/**
* Additional request information.
*/
readonly request: LanguageModelRequestMetadata;
/**
* Additional response information.
*/
readonly response: LanguageModelResponseMetadata;
/**
* Additional provider-specific metadata. They are passed through
* from the provider to the AI SDK and enable provider-specific
* results that can be fully encapsulated in the provider.
*/
readonly providerMetadata: ProviderMetadata | undefined;
/**
* The user messages that were sent with `experimental_steering` and added
* to the input of this step. Undefined when the step received no steering
* messages.
*/
readonly experimental_steeringMessages?: Array<UserModelMessage>;
};
export declare class DefaultStepResult<TOOLS extends ToolSet, RUNTIME_CONTEXT extends Context = Context> implements StepResult<TOOLS, RUNTIME_CONTEXT> {
readonly callId: StepResult<TOOLS, RUNTIME_CONTEXT>['callId'];
readonly stepNumber: StepResult<TOOLS, RUNTIME_CONTEXT>['stepNumber'];
readonly model: StepResult<TOOLS, RUNTIME_CONTEXT>['model'];
readonly toolsContext: StepResult<TOOLS, RUNTIME_CONTEXT>['toolsContext'];
readonly runtimeContext: StepResult<TOOLS, RUNTIME_CONTEXT>['runtimeContext'];
readonly content: StepResult<TOOLS, RUNTIME_CONTEXT>['content'];
readonly finishReason: StepResult<TOOLS, RUNTIME_CONTEXT>['finishReason'];
readonly rawFinishReason: StepResult<TOOLS, RUNTIME_CONTEXT>['rawFinishReason'];
readonly usage: StepResult<TOOLS, RUNTIME_CONTEXT>['usage'];
readonly performance: StepResult<TOOLS, RUNTIME_CONTEXT>['performance'];
readonly warnings: StepResult<TOOLS, RUNTIME_CONTEXT>['warnings'];
readonly request: StepResult<TOOLS, RUNTIME_CONTEXT>['request'];
readonly response: StepResult<TOOLS, RUNTIME_CONTEXT>['response'];
readonly providerMetadata: StepResult<TOOLS, RUNTIME_CONTEXT>['providerMetadata'];
readonly experimental_steeringMessages?: StepResult<TOOLS, RUNTIME_CONTEXT>['experimental_steeringMessages'];
constructor({ callId, stepNumber, provider, modelId, runtimeContext, toolsContext, content, finishReason, rawFinishReason, usage, performance, warnings, request, response, providerMetadata, steeringMessages }: {
callId: StepResult<TOOLS, RUNTIME_CONTEXT>['callId'];
stepNumber: StepResult<TOOLS, RUNTIME_CONTEXT>['stepNumber'];
provider: StepResult<TOOLS, RUNTIME_CONTEXT>['model']['provider'];
modelId: StepResult<TOOLS, RUNTIME_CONTEXT>['model']['modelId'];
runtimeContext: StepResult<TOOLS, RUNTIME_CONTEXT>['runtimeContext'];
toolsContext: StepResult<TOOLS, RUNTIME_CONTEXT>['toolsContext'];
content: StepResult<TOOLS, RUNTIME_CONTEXT>['content'];
finishReason: StepResult<TOOLS, RUNTIME_CONTEXT>['finishReason'];
rawFinishReason: StepResult<TOOLS, RUNTIME_CONTEXT>['rawFinishReason'];
usage: StepResult<TOOLS, RUNTIME_CONTEXT>['usage'];
performance: StepResult<TOOLS, RUNTIME_CONTEXT>['performance'];
warnings: StepResult<TOOLS, RUNTIME_CONTEXT>['warnings'];
request: StepResult<TOOLS, RUNTIME_CONTEXT>['request'];
response: StepResult<TOOLS, RUNTIME_CONTEXT>['response'];
providerMetadata: StepResult<TOOLS, RUNTIME_CONTEXT>['providerMetadata'];
steeringMessages?: Array<UserModelMessage>;
});
get text(): string;
get reasoning(): Array<ReasoningPart | ReasoningFilePart>;
get reasoningText(): string | undefined;
get files(): GeneratedFile[];
get sources(): (({
type: "source";
} & {
type: "source";
sourceType: "url";
id: string;
url: string;
title?: string;
providerMetadata?: import("@ai-sdk/provider").SharedV4ProviderMetadata;
}) | ({
type: "source";
} & {
type: "source";
sourceType: "document";
id: string;
mediaType: string;
title: string;
filename?: string;
providerMetadata?: import("@ai-sdk/provider").SharedV4ProviderMetadata;
}))[];
get toolCalls(): (({
type: "tool-call";
} & {
type: "tool-call";
toolCallId: string;
providerExecuted?: boolean;
providerMetadata?: ProviderMetadata;
toolMetadata?: import("@ai-sdk/provider").JSONObject;
} & {
toolName: string;
input: unknown;
dynamic: true;
title?: string;
invalid?: boolean;
error?: unknown;
} & {
providerMetadata?: ProviderMetadata;
}) | ({
type: "tool-call";
} & StaticToolCall<TOOLS> & {
providerMetadata?: ProviderMetadata;
}))[];
get staticToolCalls(): StaticToolCall<TOOLS>[];
get dynamicToolCalls(): DynamicToolCall[];
get toolResults(): (({
type: "tool-result";
} & DynamicToolResult & {
providerMetadata?: ProviderMetadata;
}) | ({
type: "tool-result";
} & StaticToolResult<TOOLS> & {
providerMetadata?: ProviderMetadata;
}))[];
get staticToolResults(): StaticToolResult<TOOLS>[];
get dynamicToolResults(): DynamicToolResult[];
}
//#endregion
//#region src/generate-text/language-model-events.d.ts
/**
* Common model information used across callback events.
*/
type ModelInfo = {
/** The provider identifier (e.g., 'openai', 'anthropic'). */
readonly provider: string;
/** The specific model identifier (e.g., 'gpt-4o'). */
readonly modelId: string;
};
/**
* Event passed to the `onLanguageModelCallStart` callback.
*
* Called immediately before the provider model call begins.
* Unlike `onStepStart`, this only represents model invocation work.
*/
type LanguageModelCallStartEvent = ModelInfo & {
/** Unique identifier for this generation call, used to correlate events. */
readonly callId: string;
/** Prepared tool definitions for the model call, if any. */
readonly tools: ReadonlyArray<Record<string, unknown>> | undefined;
} & StandardizedPrompt & LanguageModelCallOptions;
/**
* Event passed to the `onLanguageModelCallEnd` callback.
*
* Called after the model response has been normalized and parsed, but before
* any client-side tool execution begins.
*/
type LanguageModelCallEndEvent<TOOLS extends ToolSet = ToolSet> = ModelInfo & {
/** Unique identifier for this generation call, used to correlate events. */
readonly callId: string;
/** The unified reason why the model call finished. */
readonly finishReason: FinishReason;
/** The token usage reported by the model call. */
readonly usage: LanguageModelUsage;
/** The content parts produced by the model call. */
readonly content: ReadonlyArray<ContentPart<TOOLS>>;
/** The provider-returned response id for this model call. */
readonly responseId: string;
/** Optional provider-specific metadata for this model call. */
readonly providerMetadata?: ProviderMetadata;
/** HTTP origin reported by the provider, excluding URL credentials and path. */
readonly request?: {
readonly url?: string;
};
/** Performance metrics for the model call. */
readonly performance: {
/** Time spent waiting for the language model response in milliseconds. */
readonly responseTimeMs: number;
/**
* Effective number of output tokens per second over the full language
* model response.
*/
readonly effectiveOutputTokensPerSecond: number;
/**
* Number of output tokens per second after the first generated output
* chunk was received.
*
* Only available for streaming calls.
*/
readonly outputTokensPerSecond: number | undefined;
/**
* Number of input tokens processed per second before the first generated
* output chunk was received.
*
* Only available for streaming calls.
*/
readonly inputTokensPerSecond: number | undefined;
/**
* Effective number of input and output tokens per second over the full
* language model response.
*/
readonly effectiveTotalTokensPerSecond: number;
/**
* Time until the first generated output chunk was received in
* milliseconds.
*/
readonly timeToFirstOutputMs: number | undefined;
/**
* Timing statistics for the gaps between generated output chunks in
* milliseconds.
*
* Only available for streaming calls with at least two output chunks.
*/
readonly timeBetweenOutputChunksMs?: OutputChunkTimingStats;
};
};
/**
* Callback that is set using the `onLanguageModelCallStart` option.
*
* Called immediately before the provider model call begins.
* Unlike step-start callbacks, this is scoped to model work only and
* excludes any later client-side tool execution.
*
* @param event - The event object containing model-call-specific inputs.
*/
type OnLanguageModelCallStartCallback = Callback<LanguageModelCallStartEvent>;
/**
* Callback that is set using the `onLanguageModelCallEnd` option.
*
* Called after the model response has been normalized and parsed, but before
* any client-side tool execution begins.
*
* @param event - The event object containing model-call-specific outputs.
*/
type OnLanguageModelCallEndCallback<TOOLS extends ToolSet = ToolSet> = Callback<LanguageModelCallEndEvent<TOOLS>>;
//#endregion
//#region src/util/async-iterable-stream.d.ts
/**
* A type that combines AsyncIterable and ReadableStream.
* This allows a ReadableStream to be consumed using for-await-of syntax.
*/
type AsyncIterableStream<T> = AsyncIterable<T> & ReadableStream<T>;
/**
* Wraps a ReadableStream and returns an object that is both a ReadableStream and an AsyncIterable.
* This enables consumption of the stream using for-await-of, with proper resource cleanup on early exit or error.
*
* @template T The type of the stream's chunks.
* @param source The source ReadableStream to wrap.
* @returns An AsyncIterableStream that can be used as both a ReadableStream and an AsyncIterable.
*/
export declare function createAsyncIterableStream<T>(source: ReadableStream<T>): AsyncIterableStream<T>;
//#endregion
//#region src/generate-text/output-utils.d.ts
/**
* Infers the complete output type from the output specification.
*/
type InferCompleteOutput<OUTPUT extends Output> = OUTPUT extends Output<infer COMPLETE_OUTPUT, any, any> ? COMPLETE_OUTPUT : never;
//#endregion
//#region src/generate-text/tool-output-denied.d.ts
/**
* Tool output when the tool execution has been denied (for static tools).
*/
type StaticToolOutputDenied<TOOLS extends ToolSet> = ValueOf<{ [NAME in keyof TOOLS]-?: {
type: 'tool-output-denied';
toolCallId: string;
toolName: NAME & string;
providerExecuted?: boolean;
dynamic?: false | undefined;
}; }>;
//#endregion
//#region src/generate-text/stream-text-result.d.ts
type TextStreamTextDeltaPart = {
type: 'text-delta';
id: string;
providerMetadata?: ProviderMetadata;
text: string;
};
type TextStreamTextStartPart = {
type: 'text-start';
id: string;
providerMetadata?: ProviderMetadata;
};
type TextStreamTextEndPart = {
type: 'text-end';
citations?: Array<Citation>;
id: string;
providerMetadata?: ProviderMetadata;
};
type TextStreamReasoningStartPart = {
type: 'reasoning-start';
id: string;
providerMetadata?: ProviderMetadata;
};
type TextStreamReasoningEndPart = {
type: 'reasoning-end';
id: string;
providerMetadata?: ProviderMetadata;
};
type TextStreamReasoningDeltaPart = {
type: 'reasoning-delta';
providerMetadata?: ProviderMetadata;
id: string;
text: string;
};
type TextStreamCustomPart = {
type: 'custom';
kind: `${string}.${string}`;
providerMetadata?: ProviderMetadata;
};
type TextStreamToolInputStartPart = {
type: 'tool-input-start';
id: string;
toolName: string;
/** Encoding of the input deltas. Defaults to JSON when omitted. */
inputEncoding?: 'json' | 'text';
providerMetadata?: ProviderMetadata;
toolMetadata?: JSONObject;
providerExecuted?: boolean;
dynamic?: boolean;
title?: string;
};
type TextStreamToolInputEndPart = {
type: 'tool-input-end';
id: string;
providerMetadata?: ProviderMetadata;
};
type TextStreamToolInputDeltaPart = {
type: 'tool-input-delta';
id: string;
delta: string;
providerMetadata?: ProviderMetadata;
};
type TextStreamSourcePart = {
type: 'source';
} & Source;
type TextStreamFilePart = {
type: 'file';
file: GeneratedFile;
providerMetadata?: ProviderMetadata;
};
type TextStreamReasoningFilePart = {
type: 'reasoning-file';
file: GeneratedFile;
providerMetadata?: ProviderMetadata;
};
type TextStreamToolCallPart<TOOLS extends ToolSet> = {
type: 'tool-call';
} & TypedToolCall<TOOLS>;
type TextStreamToolResultPart<TOOLS extends ToolSet> = {
type: 'tool-result';
} & TypedToolResult<TOOLS>;
type TextStreamToolErrorPart<TOOLS extends ToolSet> = {
type: 'tool-error';
} & TypedToolError<TOOLS>;
type TextStreamToolOutputDeniedPart<TOOLS extends ToolSet> = {
type: 'tool-output-denied';
} & StaticToolOutputDenied<TOOLS>;
type TextStreamToolApprovalRequestPart<TOOLS extends ToolSet> = ToolApprovalRequestOutput<TOOLS>;
type TextStreamToolApprovalResponsePart<TOOLS extends ToolSet> = ToolApprovalResponseOutput<TOOLS>;
/**
* Experimental. A steering message that was added to the run before the step
* with the given number. Streamed before the output of that step.
*/
type TextStreamSteeringMessagePart = {
type: 'steering-message';
/** The id of the steering message. */
id: string;
/** The user message that was added to the run. */
message: UserModelMessage;
/** The number of the step whose model call received the message. */
stepNumber: number;
};
type TextStreamStartStepPart = {
type: 'start-step';
request: LanguageModelRequestMetadata;
warnings: CallWarning[];
};
type TextStreamFinishStepPart = {
type: 'finish-step';
response: Omit<LanguageModelResponseMetadata, 'messages' | 'body'>;
usage: LanguageModelUsage;
performance: StepResultPerformance;
finishReason: FinishReason;
rawFinishReason: string | undefined;
providerMetadata: ProviderMetadata | undefined;
};
type TextStreamStartPart = {
type: 'start';
};
type TextStreamFinishPart = {
type: 'finish';
finishReason: FinishReason;
rawFinishReason: string | undefined;
totalUsage: LanguageModelUsage;
};
type TextStreamAbortPart = {
type: 'abort';
reason?: string;
};
type TextStreamErrorPart = {
type: 'error';
error: unknown;
};
type TextStreamRawPart = {
type: 'raw';
rawValue: unknown;
};
type TextStreamPingPart = {
type: 'ping';
};
type TextStreamPart<TOOLS extends ToolSet> = TextStreamTextStartPart | TextStreamTextEndPart | TextStreamTextDeltaPart | TextStreamReasoningStartPart | TextStreamReasoningEndPart | TextStreamReasoningDeltaPart | TextStreamCustomPart | TextStreamToolInputStartPart | TextStreamToolInputEndPart | TextStreamToolInputDeltaPart | TextStreamSourcePart | TextStreamFilePart | TextStreamReasoningFilePart | TextStreamToolCallPart<TOOLS> | TextStreamToolResultPart<TOOLS> | TextStreamToolErrorPart<TOOLS> | TextStreamToolOutputDeniedPart<TOOLS> | TextStreamToolApprovalRequestPart<TOOLS> | TextStreamToolApprovalResponsePart<TOOLS> | TextStreamSteeringMessagePart | TextStreamStartStepPart | TextStreamFinishStepPart | TextStreamStartPart | TextStreamFinishPart | TextStreamAbortPart | TextStreamErrorPart | TextStreamPingPart | TextStreamRawPart;
//#endregion
//#region src/generate-text/tool-order.d.ts
/**
* Tool names that define the order in which tools are sent to the provider.
*
* Tool names are object keys at runtime, so the type is restricted to the
* string keys of the configured tool set. The list can be partial; tools not
* listed in `toolOrder` are sent after the listed tools, sorted alphabetically.
*/
type ToolOrder<TOOLS extends ToolSet> = ReadonlyArray<keyof TOOLS & string> | undefined;
//#endregion
//#region src/generate-text/tool-approval-configuration.d.ts
/**
* The approval status of a tool configuration. This can be one of the following:
*
* - 'not-applicable': The tool does not require approval.
* - 'approved': The tool is automatically approved.
* - 'denied': The tool is automatically denied.
* - 'user-approval': The tool requires user approval.
*
* In addition to the string statuses, you can also use object statuses with a reason property.
* For approved and denied statuses, the reason is emitted on the approval response.
* For user-approval statuses, the reason is emitted on the approval request so it can be shown to the approver.
*
* `undefined` is treated as the `not-applicable` status.
*/
type ToolApprovalStatus = undefined | 'not-applicable' | 'approved' | 'denied' | 'user-approval' | {
type: 'not-applicable';
reason?: never;
} | {
type: 'approved';
reason?: string;
} | {
type: 'denied';
reason?: string;
} | {
type: 'user-approval';
reason?: string;
};
/**
* Function that is called to determine if the tool needs approval before it can be executed.
*
* Return `undefined` for the same effect as the `not-applicable` status.
*/
type SingleToolApprovalFunction<INPUT, TOOL_CONTEXT extends Context | unknown | never, RUNTIME_CONTEXT extends Context | unknown | never> = (input: INPUT, options: Omit<ToolExecutionOptions<TOOL_CONTEXT>, 'abortSignal' | 'context'> & {
toolContext: TOOL_CONTEXT;
runtimeContext: RUNTIME_CONTEXT;
}) => MaybePromiseLike<ToolApprovalStatus>;
/**
* Function that is called to determine if a tool call needs approval before it can be executed.
*
* Return `undefined` for the same effect as the `not-applicable` status.
*/
type GenericToolApprovalFunction<TOOLS extends ToolSet, TOOLS_CONTEXT extends InferToolSetContext<TOOLS>, RUNTIME_CONTEXT extends Context | unknown | never> = (options: {
/**
* The tool call that needs approval.
*/
toolCall: TypedToolCall<TOOLS>;
/**
* All tools that are available for the model to call.
*/
tools: TOOLS | undefined;
/**
* Tool context for all tools that are available for the model to call.
*/
toolsContext: TOOLS_CONTEXT;
/**
* Runtime context.
*/
runtimeContext: RUNTIME_CONTEXT;
/**
* Messages that were sent to the language model to initiate the response that contained the tool call.
* The messages **do not** include the system prompt nor the assistant response that contained the tool call.
*/
messages: ModelMessage[];
}) => MaybePromiseLike<ToolApprovalStatus>;
/**
* Configure whether individual tools require approval before they can run.
*
* You can either use a generic function that is called for all tool calls,
* or you can use a per-tool function.
*
* For the per-tool functions, each tool can be assigned either an approval status
* or a function that produces an approval status at runtime.
*
* The approval status can be one of the following:
* - 'not-applicable': The tool does not require approval.
* - 'approved': The tool is automatically approved.
* - 'denied': The tool is automatically denied.
* - 'user-approval': The tool requires user approval.
*
* In addition to the string statuses, you can also use object statuses with a reason property.
* For approved and denied statuses, the reason is emitted on the approval response.
* For user-approval statuses, the reason is emitted on the approval request so it can be shown to the approver.
*/
type ToolApprovalConfiguration<TOOLS extends ToolSet, RUNTIME_CONTEXT extends Context | unknown | never> = GenericToolApprovalFunction<TOOLS, InferToolSetContext<TOOLS>, RUNTIME_CONTEXT> | { [key in keyof TOOLS]?: ToolApprovalStatus | SingleToolApprovalFunction<InferToolInput<TOOLS[key]>, InferToolContext<TOOLS[key]>, RUNTIME_CONTEXT>; };
//#endregion
//#region src/generate-text/tool-caller-configuration.d.ts
type ResolvedToolCallers = Record<string, ReadonlyArray<string>>;
//#endregion
//#region src/error/invalid-tool-input-error.d.ts
declare const symbol$1: unique symbol;
declare class InvalidToolInputError extends AISDKError {
private readonly [symbol$1];
readonly toolName: string;
readonly toolInput: string;
constructor({ toolInput, toolName, cause, message }: {
message?: string;
toolInput: string;
toolName: string;
cause: unknown;
});
static isInstance(error: unknown): error is InvalidToolInputError;
}
//#endregion
//#region src/error/no-such-tool-error.d.ts
declare const symbol: unique symbol;
declare class NoSuchToolError extends AISDKError {
private readonly [symbol];
readonly toolName: string;
readonly availableTools: string[] | undefined;
constructor({ toolName, availableTools, message }: {
toolName: string;
availableTools?: string[] | undefined;
message?: string;
});
static isInstance(error: unknown): error is NoSuchToolError;
}
//#endregion
//#region src/generate-text/tool-call-repair-function.d.ts
/**
* A function that attempts to repair a tool call that failed to parse.
*
* It receives the error and the context as arguments and returns the repair
* tool call JSON as text.
*
* @param options.instructions - The instructions provided to the model.
* @param options.system - The instructions provided to the model.
* @param options.messages - The messages in the current generation step.
* @param options.toolCall - The tool call that failed to parse.
* @param options.tools - The tools that are available.
* @param options.inputSchema - A function that returns the JSON Schema for a tool.
* @param options.error - The error that occurred while parsing the tool call.
* @param options.abortSignal - An optional signal for cancelling the repair.
*/
type ToolCallRepairFunction<TOOLS extends ToolSet> = (options: {
instructions: Instructions | undefined;
/**
* @deprecated Use `instructions` instead.
*/
system: Instructions | undefined;
messages: ModelMessage$1[];
toolCall: LanguageModelV4ToolCall;
tools: TOOLS;
inputSchema: (options: {
toolName: string;
}) => PromiseLike<JSONSchema7>;
error: NoSuchToolError | InvalidToolInputError;
abortSignal?: AbortSignal;
}) => Promise<LanguageModelV4ToolCall | null>;
//#endregion
//#region src/generate-text/tool-output.d.ts
type ToolOutput<TOOLS extends ToolSet> = TypedToolResult<TOOLS> | TypedToolError<TOOLS>;
//#endregion
//#region src/generate-text/tool-execution-events.d.ts
/**
* Resolves a single tool's context type, falling back to `undefined` when the
* tool does not declare a `contextSchema`.
*/
type ToolContextFor<TOOL extends ToolSet[keyof ToolSet]> = [InferToolContext<TOOL>] extends [never] ? undefined : InferToolContext<TOOL>;
type BaseToolExecutionStartFields = {
/** Unique identifier for this generation call, used to correlate events. */
readonly callId: string;
/**
* Messages that were sent to the language model to initiate the response that contained the tool call.
* The messages **do not** include the system prompt nor the assistant response that contained the tool call.
*/
readonly messages: ModelMessage[];
};
/**
* Precise start event union for statically known tools.
*
* Each union member ties a specific `toolCall.toolName` to that tool's
* validated `toolContext` type.
*/
type StaticToolExecutionStartEvent<TOOLS extends ToolSet> = ValueOf<{ [NAME in keyof TOOLS]-?: BaseToolExecutionStartFields & {
readonly toolCall: Extract<StaticToolCall<TOOLS>, {
toolName: NAME;
}>;
readonly toolContext: ToolContextFor<TOOLS[NAME]>;
}; }>;
/**
* Start event shape for dynamic or untyped tool calls.
*/
type DynamicToolExecutionStartEvent = BaseToolExecutionStartFields & {
readonly toolCall: DynamicToolCall;
readonly toolContext: unknown;
};
/**
* Broad start event shape used for the default `ToolSet` specialization.
*
* This keeps generic collectors ergonomic when the caller is not working with
* a concrete tool set and therefore cannot benefit from per-tool narrowing.
*/
type WidenedToolExecutionStartEvent = BaseToolExecutionStartFields & {
readonly toolCall: StaticToolCall<ToolSet> | DynamicToolCall;
readonly toolContext: unknown;
};
/**
* Event passed to the `onToolExecutionStart` callback.
*
* Called when a tool execution begins, before the tool's `execute` function is invoked.
*/
type ToolExecutionStartEvent<TOOLS extends ToolSet = ToolSet> = [ToolSet] extends [TOOLS] ? WidenedToolExecutionStartEvent : StaticToolExecutionStartEvent<TOOLS> | DynamicToolExecutionStartEvent;
type BaseToolExecutionEndFields = {
/** Unique identifier for this generation call, used to correlate events. */
readonly callId: string;
/** Execution time of the tool call in milliseconds. */
readonly toolExecutionMs: number;
/**
* Messages that were sent to the language model to initiate the response that contained the tool call.
* The messages **do not** include the system prompt nor the assistant response that contained the tool call.
*/
readonly messages: ModelMessage[];
};
/**
* Precise end event union for statically known tools.
*
* Each union member preserves the link between `toolCall.toolName`, the
* corresponding validated `toolContext`, and the tool execution result.
*/
type StaticToolExecutionEndEvent<TOOLS extends ToolSet> = ValueOf<{ [NAME in keyof TOOLS]-?: BaseToolExecutionEndFields & {
readonly toolCall: Extract<StaticToolCall<TOOLS>, {
toolName: NAME;
}>;
readonly toolContext: ToolContextFor<TOOLS[NAME]>;
readonly toolOutput: ToolOutput<TOOLS>;
}; }>;
/**
* End event shape for dynamic or untyped tool calls.
*/
type DynamicToolExecutionEndEvent<TOOLS extends ToolSet> = BaseToolExecutionEndFields & {
readonly toolCall: DynamicToolCall;
readonly toolContext: unknown;
readonly toolOutput: ToolOutput<TOOLS>;
};
/**
* Broad end event shape used for the default `ToolSet` specialization.
*
* This provides an assignable catch-all event type for generic consumers while
* the concrete-tool specialization retains full per-tool narrowing.
*/
type WidenedToolExecutionEndEvent = BaseToolExecutionEndFields & {
readonly toolCall: StaticToolCall<ToolSet> | DynamicToolCall;
readonly toolContext: unknown;
readonly toolOutput: ToolOutput<ToolSet>;
};
/**
* Event passed to the `onToolExecutionEnd` callback.
*
* Called when a tool execution completes, either successfully or with an error.
* Uses the `toolOutput.type` discriminator to distinguish success and error.
*/
type ToolExecutionEndEvent<TOOLS extends ToolSet = ToolSet> = [ToolSet] extends [TOOLS] ? WidenedToolExecutionEndEvent : StaticToolExecutionEndEvent<TOOLS> | DynamicToolExecutionEndEvent<TOOLS>;
/**
* Callback that is set using the `onToolExecutionStart` option.
*
* Called when a tool execution begins, before the tool's `execute` function is invoked.
* Use this for logging tool invocations, tracking tool usage, or pre-execution validation.
*
* @param event - The event object containing tool call information.
*/
type OnToolExecutionStartCallback<TOOLS extends ToolSet = ToolSet> = Callback<ToolExecutionStartEvent<TOOLS>>;
/**
* Callback that is set using the `onToolExecutionEnd` option.
*
* Called when a tool execution completes, either successfully or with an error.
* Use this for logging tool results, tracking execution time, or error handling.
*
* The event uses a discriminated union on `toolOutput.type`:
* - When `toolOutput.type === 'tool-result'`: `toolOutput.output` contains the tool result.
* - When `toolOutput.type === 'tool-error'`: `toolOutput.error` contains the error.
*
* @param event - The event object containing tool call result information.
*/
type OnToolExecutionEndCallback<TOOLS extends ToolSet = ToolSet> = Callback<ToolExecutionEndEvent<TOOLS>>;
//#endregion
//#region src/generate-text/tool-input-refinement.d.ts
/**
* Mapping of tool names to functions that refine parsed tool inputs.
*
* Each refinement function receives the typed input for its tool and must return
* an input with the same type shape. Refined inputs are used for tool execution,
* output parts, lifecycle callbacks, and telemetry.
*/
type ToolInputRefinement<TOOLS extends ToolSet> = { [NAME in keyof TOOLS]?: (input: InferToolInput<TOOLS[NAME]>) => MaybePromiseLike<InferToolInput<TOOLS[NAME]>>; };
//#endregion
//#region src/generate-text/tools-context-parameter.d.ts
/**
* Checks whether a tool context map contains any contextual tool entries.
*/
type IsEmptyObject<OBJECT> = keyof OBJECT extends never ? true : false;
/**
* Makes the toolsContext setting optional, required, or unavailable based on
* the tool set.
*/
type ToolsContextSettings<TOOLS extends ToolSet> = IsEmptyObject<InferToolSetContext<TOOLS>> extends true ? {
toolsContext?: never;
} : HasRequiredKey<InferToolSetContext<TOOLS>> extends true ? {
toolsContext: InferToolSetContext<TOOLS>;
} : {
toolsContext?: InferToolSetContext<TOOLS>;
};
//#endregion
//#region src/generate-text/stream-text.d.ts
type EnrichedStreamPart<TOOLS extends ToolSet, PARTIAL_OUTPUT> = {
part: TextStreamPart<TOOLS>;
partialOutput: PARTIAL_OUTPUT | undefined;
};
//#endregion
//#region src/generate-text/output.d.ts
interface Output<OUTPUT = any, PARTIAL = any, ELEMENT = any> {
/**
* The name of the output mode.
*/
name: string;
/**
* The response format to use for the model.
*/
responseFormat: PromiseLike<LanguageModelV4CallOptions['responseFormat']>;
/**
* Parses the complete output of the model.
*/
parseCompleteOutput(options: {
text: string;
}, context: {
response: Omit<LanguageModelResponseMetadata, 'messages' | 'body'>;
usage: LanguageModelUsage;
finishReason: FinishReason;
}): Promise<OUTPUT>;
/**
* Parses the partial output of the model.
*/
parsePartialOutput(options: {
text: string;
}): Promise<{
partial: PARTIAL;
} | undefined>;
/**
* Creates a stream transform that emits individual elements as they complete.
*/
createElementStreamTransform(): TransformStream<EnrichedStreamPart<any, PARTIAL>, ELEMENT> | undefined;
}
//#endregion
//#region src/generate-text/generate-text-events.d.ts
/**
* Event passed to the `onStart` callback.
*
* Called when the generation operation begins, before any LLM calls.
*/
type GenerateTextStartEvent<TOOLS extends ToolSet = ToolSet, RUNTIME_CONTEXT extends Context = Context, OUTPUT extends Output = Output> = {
/** Unique identifier for this generation call, used to correlate events. */
readonly callId: string;
/** Identifies the operation type (e.g. 'ai.generateText' or 'ai.streamText'). */
readonly operationId: string;
/** The provider identifier (e.g., 'openai', 'anthropic'). */
readonly provider: string;
/** The specific model identifier (e.g., 'gpt-4o'). */
readonly modelId: string;
/** The tools available for this generation. */
readonly tools: TOOLS | undefined;
/** The tool choice strategy for this generation. */
readonly toolChoice: ToolChoice<NoInfer<TOOLS>> | undefined;
/** Limits which tools are available for the model to call. */
readonly activeTools: ActiveTools<TOOLS>;
/** Controls the order in which tools are sent to the provider. */
readonly toolOrder: ToolOrder<TOOLS>;
/** Maximum number of retries for failed requests. */
readonly maxRetries: number;
/**
* Timeout configuration for the generation.
* Can be a number (milliseconds) or an object with totalMs, stepMs,
* firstChunkMs (streaming only), chunkMs, toolMs, and per-tool overrides via tools.
*/
readonly timeout: TimeoutConfiguration<TOOLS> | undefined;
/** Additional HTTP headers sent with the request. */
readonly headers: Record<string, string | undefined> | undefined;
/** Additional provider-specific options. */
readonly providerOptions: ProviderOptions | undefined;
/** The output specification for structured outputs, if configured. */
readonly output: OUTPUT | undefined;
/**
* Tool context.
*/
readonly toolsContext: InferToolSetContext<TOOLS>;
/**
* User-defined runtime context.
*/
readonly runtimeContext: RUNTIME_CONTEXT;
} & LanguageModelCallOptions & StandardizedPrompt;
/**
* Event passed to the `onStepStart` callback.
*
* Called when a step (LLM call) begins, before the provider is called.
* Each step represents a single LLM invocation.
*/
type GenerateTextStepStartEvent<TOOLS extends ToolSet = ToolSet, RUNTIME_CONTEXT extends Context = Context, OUTPUT extends Output = Output> = {
/** Unique identifier for this generation call, used to correlate events. */
readonly callId: string;
/** The provider identifier (e.g., 'openai', 'anthropic'). */
readonly provider: string;
/** The specific model identifier (e.g., 'gpt-4o'). */
readonly modelId: string;
/** Zero-based index of the current step. */
readonly stepNumber: number;
/** The tools available for this generation. */
readonly tools: TOOLS | undefined;
/** The tool choice configuration for this step. */
readonly toolChoice: ToolChoice<NoInfer<TOOLS>> | undefined;
/** Limits which tools are available for this step. */
readonly activeTools: ActiveTools<TOOLS>;
/** Controls the order in which tools are sent to the provider for this step. */
readonly toolOrder: ToolOrder<TOOLS>;
/** Array of results from previous steps (empty for first step). */
readonly steps: ReadonlyArray<StepResult<TOOLS, RUNTIME_CONTEXT>>;
/** Additional provider-specific options for this step. */
readonly providerOptions: ProviderOptions | undefined;
/** The output specification for structured outputs, if configured. */
readonly output: OUTPUT | undefined;
/**
* Runtime context. May be updated from `prepareStep` between steps.
*/
readonly runtimeContext: RUNTIME_CONTEXT;
/**
* Tool context. May be updated from `prepareStep` between steps.
*/
readonly toolsContext: InferToolSetContext<TOOLS>;
} & StandardizedPrompt;
/**
* Event passed to the `onStepEnd` callback.
*
* Called when a step (LLM call) completes.
* Includes the StepResult for that step along with the call identifier.
*/
type GenerateTextStepEndEvent<TOOLS extends ToolSet = ToolSet, RUNTIME_CONTEXT extends Context = Context> = StepResult<TOOLS, RUNTIME_CONTEXT>;
/**
* Event passed to the `onEnd` callback.
*
* Called when the entire generation completes (all steps finished).
* Includes the final step's result along with aggregated data from all steps.
*/
type GenerateTextEndEvent<TOOLS extends ToolSet = ToolSet, RUNTIME_CONTEXT extends Context = Context> = {
/** Unique identifier for this generation call, used to correlate events. */
readonly callId: string;
/** Zero-based index of the final step. */
readonly stepNumber: number;
/** Information about the model that produced the final step. */
readonly model: StepResult<TOOLS, RUNTIME_CONTEXT>['model'];
/**
* Tool context from the final step.
*
* @deprecated Use `finalStep.toolsContext` instead.
*/
readonly toolsContext: InferToolSetContext<TOOLS>;
/**
* Runtime context from the final step.
*
* @deprecated Use `finalStep.runtimeContext` instead.
*/
readonly runtimeContext: RUNTIME_CONTEXT;
/** The content that was generated in all steps. */
readonly content: StepResult<TOOLS, RUNTIME_CONTEXT>['content'];
/** The text that was generated in the final step. */
readonly text: StepResult<TOOLS, RUNTIME_CONTEXT>['text'];
/**
* The reasoning that was generated in the final step.
*
* @deprecated Use `finalStep.reasoning` instead.
*/
readonly reasoning: StepResult<TOOLS, RUNTIME_CONTEXT>['reasoning'];
/**
* The reasoning text that was generated in the final step.
*
* @deprecated Use `finalStep.reasoningText` instead.
*/
readonly reasoningText: StepResult<TOOLS, RUNTIME_CONTEXT>['reasoningText'];
/** Files that were generated in all steps. */
readonly files: StepResult<TOOLS, RUNTIME_CONTEXT>['files'];
/** Sources that were used as references in all steps. */
readonly sources: StepResult<TOOLS, RUNTIME_CONTEXT>['sources'];
/** Tool calls that were made in all steps. */
readonly toolCalls: StepResult<TOOLS, RUNTIME_CONTEXT>['toolCalls'];
/** Static tool calls that were made in all steps. */
readonly staticToolCalls: StepResult<TOOLS, RUNTIME_CONTEXT>['staticToolCalls'];
/** Dynamic tool calls that were made in all steps. */
readonly dynamicToolCalls: StepResult<TOOLS, RUNTIME_CONTEXT>['dynamicToolCalls'];
/** Tool results that were generated in all steps. */
readonly toolResults: StepResult<TOOLS, RUNTIME_CONTEXT>['toolResults'];
/** Static tool results that were generated in all steps. */
readonly staticToolResults: StepResult<TOOLS, RUNTIME_CONTEXT>['staticToolResults'];
/** Dynamic tool results that were generated in all steps. */
readonly dynamicToolResults: StepResult<TOOLS, RUNTIME_CONTEXT>['dynamicToolResults'];
/** The unified reason why the generation finished. Taken from the final step. */
readonly finishReason: StepResult<TOOLS, RUNTIME_CONTEXT>['finishReason'];
/** The raw reason why the generation finished. Taken from the final step. */
readonly rawFinishReason: StepResult<TOOLS, RUNTIME_CONTEXT>['rawFinishReason'];
/** Aggregated token usage across all steps. */
readonly usage: LanguageModelUsage;
/**
* Aggregated token usage across all steps.
*
* @deprecated Use `usage` instead.
*/
readonly totalUsage: LanguageModelUsage;
/** Warnings from the model provider in all steps. */
readonly warnings: StepResult<TOOLS, RUNTIME_CONTEXT>['warnings'];
/**
* Additional request information from the final step.
*
* @deprecated Use `finalStep.request` instead.
*/
readonly request: StepResult<TOOLS, RUNTIME_CONTEXT>['request'];
/**
* Additional response information from the final step.
*
* @deprecated Use `finalStep.response` instead.
*/
readonly response: StepResult<TOOLS, RUNTIME_CONTEXT>['response'];
/**
* Additional provider-specific metadata from the final step.
*
* @deprecated Use `finalStep.providerMetadata` instead.
*/
readonly providerMetadata: StepResult<TOOLS, RUNTIME_CONTEXT>['providerMetadata'];
/** The response messages that were generated during the call. */
readonly responseMessages: ResponseMessage[];
/** Array containing results from all steps in the generation. */
readonly steps: StepResult<TOOLS, RUNTIME_CONTEXT>[];
/** The final step. This is a shortcut for `steps.at(-1)`. */
readonly finalStep: StepResult<TOOLS, RUNTIME_CONTEXT>;
};
/**
* Event passed to an `onAbort` callback for text generation.
*
* Called when a streaming text generation operation is aborted before it
* completes.
*/
type GenerateTextAbortEvent<TOOLS extends ToolSet = ToolSet, RUNTIME_CONTEXT extends Context = Context> = {
/** Unique identifier for this generation call, used to correlate events. */
readonly callId: string;
/** Details for all previously finished steps. */
readonly steps: StepResult<TOOLS, RUNTIME_CONTEXT>[];
/** The abort reason from the AbortSignal, when one is available. */
readonly reason?: unknown;
};
/**
* Callback that is set using the `onStart` option.
*
* Called when the generateText operation begins, before any LLM calls.
* Use this callback for logging, analytics, or initializing state at the
* start of a generation.
*
* @param event - The event object containing generation configuration.
*/
type GenerateTextOnStartCallback<TOOLS extends ToolSet = ToolSet, RUNTIME_CONTEXT extends Context = Context, OUTPUT extends Output = Output> = Callback<GenerateTextStartEvent<TOOLS, RUNTIME_CONTEXT, OUTPUT>>;
/**
* Callback that is set using the `onStepStart` option.
*
* Called when a step (LLM call) begins, before the provider is called.
* Each step represents a single LLM invocation. Multiple steps occur when
* using tool calls (the model may be called multiple times in a loop).
*
* @param event - The event object containing step configuration.
*/
type GenerateTextOnStepStartCallback<TOOLS extends ToolSet = ToolSet, RUNTIME_CONTEXT extends Context = Context, OUTPUT extends Output = Output> = Callback<GenerateTextStepStartEvent<TOOLS, RUNTIME_CONTEXT, OUTPUT>>;
/**
* Callback that is set using the `onStepEnd` option.
*
* Called when a step (LLM call) completes. The event includes all step result
* properties (text, tool calls, usage, etc.) along with additional metadata.
*
* @param stepResult - The result of the step.
*/
type GenerateTextOnStepEndCallback<TOOLS extends ToolSet = ToolSet, RUNTIME_CONTEXT extends Context = Context> = Callback<GenerateTextStepEndEvent<TOOLS, RUNTIME_CONTEXT>>;
/**
* Callback that is set using the `onStepFinish` option.
*
* @deprecated Use `GenerateTextOnStepEndCallback` instead.
*/
type GenerateTextOnStepFinishCallback<TOOLS extends ToolSet = ToolSet, RUNTIME_CONTEXT extends Context = Context> = GenerateTextOnStepEndCallback<TOOLS, RUNTIME_CONTEXT>;
/**
* Event passed to the `onStepError` callback.
*
* Contains the step start event of the step that failed before it produced
* a step result, along with the error.
*/
type GenerateTextStepErrorEvent<TOOLS extends ToolSet = ToolSet, RUNTIME_CONTEXT extends Context = Context, OUTPUT extends Output = Output> = GenerateTextStepStartEvent<TOOLS, RUNTIME_CONTEXT, OUTPUT> & {
/** The error that ended the step. */
readonly error: unknown;
};
/**
* Callback that is set using the `onStepError` option.
*
* Called when a step that started fails before it produces a step result:
* when the provider call throws or rejects, or when the model stream ends
* without output. `onStepEnd` is not called for such a step. A provider
* `error` chunk does not fail the step, it still ends with `onStepEnd` and
* `finishReason: 'error'`.
*
* In `generateText`, an abort or timeout also ends the step here. The error
* is the abort or timeout error, or a provider error that has it as its
* `cause`. In `streamText`, an abort or timeout ends the step
* with `onAbort` instead.
*
* @param event - The step that failed and the error.
*/
type GenerateTextOnStepErrorCallback<TOOLS extends ToolSet = ToolSet, RUNTIME_CONTEXT extends Context = Context, OUTPUT extends Output = Output> = Callback<GenerateTextStepErrorEvent<TOOLS, RUNTIME_CONTEXT, OUTPUT>>;
/**
* Callback that is set using the `onEnd` option.
*
* Called when the entire generation completes (all steps finished).
* The event includes the final step's result properties along with
* aggregated data from all steps.
*
* @param event - The final result along with aggregated step data.
*/
type GenerateTextOnEndCallback<TOOLS extends ToolSet = ToolSet, RUNTIME_CONTEXT extends Context = Context> = Callback<GenerateTextEndEvent<TOOLS, RUNTIME_CONTEXT>>;
/**
* Callback that is set using the telemetry `onAbort` option.
*
* Called when a streaming text generation operation is aborted before it
* completes.
*
* @param event - The abort event, including finished steps and abort reason.
*/
type GenerateTextOnAbortCallback<TOOLS extends ToolSet = ToolSet, RUNTIME_CONTEXT extends Context = Context> = Callback<GenerateTextAbortEvent<TOOLS, RUNTIME_CONTEXT>>;
//#endregion
//#region src/rerank/rerank-events.d.ts
/**
* Event passed to the `onStart` callback for rerank operations.
*
* Called when the operation begins, before the reranking model is called.
*/
type RerankStartEvent<RUNTIME_CONTEXT extends Context = Context> = {
/** User-defined runtime context. */
readonly runtimeContext: RUNTIME_CONTEXT;
/** Unique identifier for this rerank call, used to correlate events. */
readonly callId: string;
/** Identifies the operation type ('ai.rerank'). */
readonly operationId: string;
readonly provider: string;
/** The specific model identifier (e.g., 'gpt-4o'). */
readonly modelId: string;
/** The documents being reranked. */
readonly documents: Array<JSONObject | string>;
/** The query to rerank the documents against. */
readonly query: string;
/** Number of top documents to return. */
readonly topN: number | undefined;
/** Maximum number of retries for failed requests. */
readonly maxRetries: number;
/** Additional HTTP headers sent with the request. */
readonly headers: Record<string, string | undefined> | undefined;
/** Additional provider-specific options. */
readonly providerOptions: ProviderOptions | undefined;
};
/**
* Event passed to the `onEnd` callback for rerank operations.
*
* Called when the operation completes, after the reranking model returns.
*/
type RerankEndEvent<RUNTIME_CONTEXT extends Context = Context> = {
/** User-defined runtime context. */
readonly runtimeContext: RUNTIME_CONTEXT;
/** Unique identifier for this rerank call, used to correlate events. */
readonly callId: string;
/** Identifies the operation type ('ai.rerank'). */
readonly operationId: string;
readonly provider: string;
/** The specific model identifier (e.g., 'gpt-4o'). */
readonly modelId: string;
/** The documents that were reranked. */
readonly documents: Array<JSONObject | string>;
/** The query that documents were reranked against. */
readonly query: string;
/** The reranked results sorted by relevance score in descending order. */
readonly ranking: Array<{
originalIndex: number;
score: number;
document: JSONObject | string;
}>;
/** Warnings from the reranking model. */
readonly warnings: Array<Warning>;
/** Optional provider-specific metadata. */
readonly providerMetadata: ProviderMetadata | undefined;
/** Response data including headers and body. */
readonly response: {
id?: string;
timestamp: Date;
modelId: string;
headers?: Record<string, string>;
body?: unknown;
};
};
/**
* Event fired when an individual reranking model call (inner doRerank) begins.
*/
type RerankingModelCallStartEvent = {
/** Unique identifier for this rerank call, used to correlate events. */
readonly callId: string;
/** Identifies the inner operation ('ai.rerank.doRerank'). */
readonly operationId: string;
/** The provider identifier. */
readonly provider: string;
/** The specific model identifier. */
readonly modelId: string;
/** The documents being reranked. */
readonly documents: Array<JSONObject | string>;
/** The type of documents ('text' or 'object'). */
readonly documentsType: string;
/** The query to rerank against. */
readonly query: string;
/** Number of top documents to return. */
readonly topN: number | undefined;
};
/**
* Event fired when an individual reranking model call (doRerank) completes.
*
* Contains the ranking results from the model response.
*/
type RerankingModelCallEndEvent = {
/** Unique identifier for this rerank call, used to correlate events. */
readonly callId: string;
/** Identifies the inner operation ('ai.rerank.doRerank'). */
readonly operationId: string;
/** The provider identifier. */
readonly provider: string;
/** The specific model identifier. */
readonly modelId: string;
/** The type of documents ('text' or 'object'). */
readonly documentsType: string;
/** The ranking results from the model. */
readonly ranking: Array<{
index: number;
relevanceScore: number;
}>;
};
//#endregion
//#region src/generate-speech/speech-events.d.ts
/**
* Event passed to telemetry integrations when speech generation begins.
*/
type GenerateSpeechStartEvent = {
/** Unique identifier for this speech generation call. */
readonly callId: string;
/** Identifies the operation type (`ai.generateSpeech`). */
readonly operationId: 'ai.generateSpeech';
/** The provider identifier. */
readonly provider: string;
/** The specific model identifier. */
readonly modelId: string;
/** The text to convert to speech. */
readonly text: string;
/** The requested voice. */
readonly voice: string | undefined;
/** The requested output format. */
readonly outputFormat: string | undefined;
/** Additional speech instructions. */
readonly instructions: string | undefined;
/** The requested speech speed. */
readonly speed: number | undefined;
/** The requested language. */
readonly language: string | undefined;
/** Maximum number of retries for failed requests. */
readonly maxRetries: number;
/** Additional HTTP headers sent with the request. */
readonly headers: Record<string, string> | undefined;
/** Additional provider-specific options. */
readonly providerOptions: ProviderOptions;
};
/**
* Event passed to telemetry integrations when speech generation completes.
*
* Raw audio is intentionally not included. Integrations receive only its size,
* media type, and format.
*/
type GenerateSpeechEndEvent = {
/** Unique identifier for this speech generation call. */
readonly callId: string;
/** Identifies the operation type (`ai.generateSpeech`). */
readonly operationId: 'ai.generateSpeech';
/** The provider identifier. */
readonly provider: string;
/** The specific model identifier. */
readonly modelId: string;
/** The text that was converted to speech. */
readonly text: string;
/** Metadata about the generated audio. */
readonly audio: {
readonly byteLength: number;
readonly mediaType: string;
readonly format: string;
};
/** Usage reported by the provider, when available. */
readonly usage: JSONObject | undefined;
/** Warnings from the speech model. */
readonly warnings: Array<Warning>;
/** Optional provider-specific metadata. */
readonly providerMetadata: ProviderMetadata | undefined;
/** Response metadata from the provider. */
readonly response: SpeechModelResponseMetadata;
};
//#endregion
//#region src/transcribe/transcription-events.d.ts
/**
* Event passed to telemetry integrations when transcription begins.
*/
type TranscriptionStartEventFor<OPERATION_ID extends string> = {
/** Unique identifier for this transcription call. */
readonly callId: string;
/** Identifies the operation type (`ai.transcribe` or `ai.streamTranscribe`). */
readonly operationId: OPERATION_ID;
/** The provider identifier. */
readonly provider: string;
/** The specific model identifier. */
readonly modelId: string;
/**
* Metadata about the input audio. Native URL transcription does not inspect
* the audio, so its byte length and media type are unknown. Streaming byte
* length is reported on the end event after consuming the stream.
*/
readonly audio: {
readonly byteLength: number | undefined;
/**
* The media type, when it is known before input preparation completes.
* URL downloads and invalid inline inputs can begin without this metadata.
*/
readonly mediaType: string | undefined;
};
/** The raw input format for streaming transcription. */
readonly inputAudioFormat: SharedV4AudioFormat | undefined;
/** Maximum number of retries for non-streaming transcription. */
readonly maxRetries: number | undefined;
/** Additional HTTP or WebSocket headers sent with the request. */
readonly headers: Record<string, string> | undefined;
/** Additional provider-specific options. */
readonly providerOptions: ProviderOptions;
};
type TranscriptionStartEvent = TranscriptionStartEventFor<'ai.transcribe'> & {
/** Requested shared timing granularities, when specified. */
readonly timestampGranularities?: Array<TranscriptionModelV4TimestampGranularity>;
};
type StreamTranscriptionStartEvent = TranscriptionStartEventFor<'ai.streamTranscribe'>;
/**
* Event passed to telemetry integrations when transcription completes.
*
* Raw audio is intentionally not included.
*/
type TranscriptionEndEventFor<OPERATION_ID extends string> = {
/** Unique identifier for this transcription call. */
readonly callId: string;
/** Identifies the operation type (`ai.transcribe` or `ai.streamTranscribe`). */
readonly operationId: OPERATION_ID;
/** The provider identifier. */
readonly provider: string;
/** The specific model identifier. */
readonly modelId: string;
/** Metadata about the input audio. */
readonly audio: {
readonly byteLength: OPERATION_ID extends 'ai.transcribe' ? number | undefined : number;
readonly mediaType: OPERATION_ID extends 'ai.transcribe' ? string | undefined : string;
};
/** The complete transcript. */
readonly text: string;
/** Transcript segments with timing information, retaining provider behavior. */
readonly segments: Array<{
readonly text: string;
readonly startSecond: number;
readonly endSecond: number;
}>;
/** The detected language. */
readonly language: string | undefined;
/** The duration of the input audio, when available. */
readonly durationInSeconds: number | undefined;
/** Usage reported by the provider, when available. */
readonly usage: JSONObject | undefined;
/** Warnings from the transcription model. */
readonly warnings: Array<Warning>;
/** Optional provider-specific metadata. */
readonly providerMetadata: ProviderMetadata | undefined;
/** Response metadata from the provider. */
readonly response: TranscriptionModelResponseMetadata;
};
type TranscriptionEndEvent = TranscriptionEndEventFor<'ai.transcribe'> & {
/** Word timings returned by the provider, whether requested or returned by default. */
readonly words?: Array<{
readonly text: string;
readonly startSecond: number;
readonly endSecond: number;
}>;
};
type StreamTranscriptionEndEvent = TranscriptionEndEventFor<'ai.streamTranscribe'>;
//#endregion
//#region src/telemetry/tracing-channel.d.ts
type TelemetryTracingEventType = 'generateText' | 'streamText' | 'step' | 'languageModelCall' | 'executeTool' | 'embed' | 'embedMany' | 'rerank' | 'generateSpeech' | 'transcribe' | 'streamTranscribe' | 'experimental_decide';
//#endregion
//#region src/telemetry/tracing-channel-publisher.d.ts
type TracingChannelContext = {
run<T>(execute: () => T): T;
};
//#endregion
//#region src/telemetry/telemetry.d.ts
/** Serializable tracing identity shared by the processes of a durable run. */
type DurableTelemetryContext = {
traceId: string;
rootSpanId: string;
/** The integration's root-span data, reused when the run is reported. */
rootSpanName?: string;
rootAttributes?: Record<string, string | number | boolean | Array<string | null | undefined> | Array<number | null | undefined> | Array<boolean | null | undefined> | undefined>;
traceFlags: number;
traceState?: string;
samplingDecision: 0 | 1 | 2;
parentSpanContext?: {
traceId: string;
spanId: string;
traceFlags: number;
traceState?: string;
};
};
/** Identifies live model events and the run events reported after completion. */
type DurableTelemetryEventContext = {
run: DurableTelemetryContext;
stepNumber?: number;
phase: 'live' | 'replay';
/** Context has already been filtered before crossing the step boundary. */
runtimeContext?: Record<string, unknown>;
headers?: Record<string, string | undefined>;
};
type InferTelemetryEvent<EVENT> = EVENT & Omit<TelemetryOptions, 'integrations' | 'isEnabled' | 'includeRuntimeContext'> & {
/**
* The version of the `ai` package that emitted this event.
* Absent in older releases.
*/
readonly aiVersion?: string;
/**
* Time at which the event occurred, in milliseconds since the Unix epoch.
*
* Set when events are reported after they happened, for example when a
* durable workflow reports a run that spanned several processes.
* Integrations should use it instead of the current time when present.
*/
readonly timestamp?: number;
readonly durableContext?: DurableTelemetryEventContext;
};
type BivariantCallback<EVENT> = {
bivarianceHack(event: EVENT): PromiseLike<void> | void;
}['bivarianceHack'];
type OperationStartEvent = GenerateTextStartEvent | GenerateObjectStartEvent | EmbedStartEvent | RerankStartEvent | GenerateSpeechStartEvent | TranscriptionStartEvent;
type OperationEndEvent = GenerateTextEndEvent<ToolSet> | GenerateObjectEndEvent<unknown> | EmbedEndEvent | RerankEndEvent | GenerateSpeechEndEvent | TranscriptionEndEvent;
interface TelemetryDispatcher {
/**
* Runs awaited work inside a diagnostics-channel tracing span.
*/
runInTracingChannelSpan?: <T>(options: {
type: TelemetryTracingEventType;
event: unknown;
execute: () => PromiseLike<T>;
}) => Promise<T>;
/**
* Opens a tracing span context whose completion is observed separately.
* This is used by streamed operations that must preserve stream timing while
* still creating child spans with the correct parent.
*/
startTracingChannelContext?: (options: {
type: TelemetryTracingEventType;
event: unknown;
completion: PromiseLike<unknown>;
}) => TracingChannelContext | undefined;
onStart?: Callback<OperationStartEvent>;
onStepStart?: Callback<GenerateTextStepStartEvent>;
onLanguageModelCallStart?: OnLanguageModelCallStartCallback;
onLanguageModelCallEnd?: OnLanguageModelCallEndCallback;
onToolExecutionStart?: Callback<ToolExecutionStartEvent>;
onToolExecutionEnd?: Callback<ToolExecutionEndEvent>;
onStepEnd?: Callback<GenerateTextStepEndEvent>;
/** @deprecated Use `onStepEnd` instead. */
onStepFinish?: Callback<GenerateTextStepEndEvent>;
onStepError?: Callback<GenerateTextStepErrorEvent>;
onObjectStepStart?: Callback<GenerateObjectStepStartEvent>;
onObjectStepEnd?: Callback<GenerateObjectStepEndEvent>;
onEmbedStart?: Callback<EmbeddingModelCallStartEvent>;
onEmbedEnd?: Callback<EmbeddingModelCallEndEvent>;
onRerankStart?: Callback<RerankingModelCallStartEvent>;
onRerankEnd?: Callback<RerankingModelCallEndEvent>;
experimental_onDecideStart?: Callback<DecideStartEvent>;
experimental_onDecisionModelCallStart?: Callback<DecisionModelCallStartEvent>;
experimental_onDecisionModelCallEnd?: Callback<DecisionModelCallEndEvent>;
experimental_onDecideEnd?: Callback<DecideEndEvent>;
experimental_onStreamTranscriptionStart?: Callback<StreamTranscriptionStartEvent>;
experimental_onStreamTranscriptionEnd?: Callback<StreamTranscriptionEndEvent>;
onEnd?: Callback<OperationEndEvent>;
onAbort?: Callback<GenerateTextAbortEvent<ToolSet>>;
onError?: Callback<unknown>;
executeLanguageModelCall?: Telemetry['executeLanguageModelCall'];
executeTool?: Telemetry['executeTool'];
}
/**
* Implement this interface to create custom telemetry integrations.
* Methods can be sync or return a PromiseLike.
*/
interface Telemetry {
/**
* Capture a serializable tracing identity before a durable run's model calls.
* Return undefined to preserve live per-model operations when the integration
* cannot reconstruct a run span across processes. Integrations without this
* hook receive the recorded lifecycle after completion.
*/
experimental_durable?: {
captureContext: (event: InferTelemetryEvent<GenerateTextStartEvent>) => DurableTelemetryContext | undefined | PromiseLike<DurableTelemetryContext | undefined>;
};
/**
* Called when an operation begins. Fired for text generation
* (generateText/streamText), object generation (generateObject/streamObject),
* embedding (embed/embedMany), reranking, speech generation, and
* non-streaming transcription operations.
*
* Use the `operationId` field to distinguish between operation types.
*/
onStart?: BivariantCallback<InferTelemetryEvent<OperationStartEvent>>;
/**
* Called when an individual step (single LLM invocation) begins.
* A generation may consist of multiple steps (e.g. when tool calls trigger
* follow-up LLM calls). Use this to create per-step spans or record
* step-level inputs.
*
* The event includes the step number, accumulated previous step results,
* and the messages that will be sent to the model.
*/
onStepStart?: Callback<InferTelemetryEvent<GenerateTextStepStartEvent>>;
/**
* Called immediately before the provider model call begins.
* Unlike `onStepStart`, this callback is scoped to model work only and
* excludes any later client-side tool execution.
*/
onLanguageModelCallStart?: Callback<InferTelemetryEvent<LanguageModelCallStartEvent>>;
/**
* Called after the model response has been normalized and parsed, but before
* any client-side tool execution begins.
*/
onLanguageModelCallEnd?: Callback<InferTelemetryEvent<LanguageModelCallEndEvent>>;
/**
* Called when a tool execution begins, before the tool's `execute` function
* is invoked. Use this to create tool-level spans or log tool invocations.
*/
onToolExecutionStart?: Callback<InferTelemetryEvent<ToolExecutionStartEvent>>;
/**
* Called when a tool execution completes, either successfully or with an error.
* The event uses a discriminated union on the `success` field — check
* `event.success` to determine whether `output` or `error` is available.
*
* The event includes execution time (`toolExecutionMs`) for performance tracking.
*/
onToolExecutionEnd?: Callback<InferTelemetryEvent<ToolExecutionEndEvent>>;
/**
* Called when an individual step (single LLM invocation) completes.
* The event is a `StepResult` containing the model's response, tool calls
* and results, usage statistics, finish reason, and optional request/response
* bodies.
*/
onStepEnd?: Callback<InferTelemetryEvent<GenerateTextStepEndEvent>>;
/**
* Called when an individual step (single LLM invocation) completes.
*
* @deprecated Use `onStepEnd` instead.
*/
onStepFinish?: Callback<InferTelemetryEvent<GenerateTextStepEndEvent>>;
/**
* Called when a step that started fails before it produces a step result.
* Use it to close step-level spans. The call then ends with `onError`.
*/
onStepError?: Callback<InferTelemetryEvent<GenerateTextStepErrorEvent>>;
/**
* Called when an object generation step (single LLM invocation) begins.
* For generateObject/streamObject there is always exactly one step.
*
* @deprecated
*/
onObjectStepStart?: Callback<InferTelemetryEvent<GenerateObjectStepStartEvent>>;
/**
* Called when an object generation step (single LLM invocation) completes,
* with the raw result before JSON parsing and schema validation.
*
* @deprecated
*/
onObjectStepEnd?: Callback<InferTelemetryEvent<GenerateObjectStepEndEvent>>;
/**
* Called when an individual embedding model call (doEmbed) begins.
* For `embed`, there is one call. For `embedMany`, there may be multiple
* calls when values are chunked.
*/
onEmbedStart?: Callback<InferTelemetryEvent<EmbeddingModelCallStartEvent>>;
/**
* Called when an individual embedding model call (doEmbed) completes.
* Contains the embeddings, usage, and any warnings from the model response.
*/
onEmbedEnd?: Callback<InferTelemetryEvent<EmbeddingModelCallEndEvent>>;
/**
* Called when an individual reranking model call (doRerank) begins.
* There is one call per `rerank` invocation.
*/
onRerankStart?: Callback<InferTelemetryEvent<RerankingModelCallStartEvent>>;
/**
* Called when an individual reranking model call (doRerank) completes.
* Contains the ranking results from the model response.
*/
onRerankEnd?: Callback<InferTelemetryEvent<RerankingModelCallEndEvent>>;
/** @deprecated Use `experimental_onDecideStart` instead. */
experimental_onEvaluateStart?: Callback<InferTelemetryEvent<DecideStartEvent>>;
/** @deprecated Use `experimental_onDecisionModelCallStart` instead. */
experimental_onEvaluationModelCallStart?: Callback<InferTelemetryEvent<DecisionModelCallStartEvent>>;
/** @deprecated Use `experimental_onDecisionModelCallEnd` instead. */
experimental_onEvaluationModelCallEnd?: Callback<InferTelemetryEvent<DecisionModelCallEndEvent>>;
/** @deprecated Use `experimental_onDecideEnd` instead. */
experimental_onEvaluateEnd?: Callback<InferTelemetryEvent<DecideEndEvent>>;
/** Called when an experimental decision operation begins. */
experimental_onDecideStart?: Callback<InferTelemetryEvent<DecideStartEvent>>;
/**
* Called immediately before an experimental decision model call begins.
* The logical model call includes any provider retries.
*/
experimental_onDecisionModelCallStart?: Callback<InferTelemetryEvent<DecisionModelCallStartEvent>>;
/**
* Called after an experimental decision model response has been validated.
* The logical model call includes any provider retries.
*/
experimental_onDecisionModelCallEnd?: Callback<InferTelemetryEvent<DecisionModelCallEndEvent>>;
/** Called when an experimental decision operation completes. */
experimental_onDecideEnd?: Callback<InferTelemetryEvent<DecideEndEvent>>;
/** Called when an experimental streaming transcription operation begins. */
experimental_onStreamTranscriptionStart?: Callback<InferTelemetryEvent<StreamTranscriptionStartEvent>>;
/** Called when an experimental streaming transcription operation completes. */
experimental_onStreamTranscriptionEnd?: Callback<InferTelemetryEvent<StreamTranscriptionEndEvent>>;
/**
* Called when an operation completes. Fired for text generation
* (generateText/streamText), object generation (generateObject/streamObject),
* embedding (embed/embedMany), reranking, speech generation, and
* non-streaming transcription operations.
*
* Use the event shape or `operationId` to distinguish between operation types.
*/
onEnd?: BivariantCallback<InferTelemetryEvent<OperationEndEvent>>;
/**
* Called when a streaming text generation operation is aborted before it
* completes.
*/
onAbort?: Callback<InferTelemetryEvent<GenerateTextAbortEvent<ToolSet>>>;
/**
* Called when an unrecoverable error occurs during the generation lifecycle.
* The error value is untyped — it may be an `Error` instance, an `AISDKError`,
* or any thrown value.
*
* Use this to record error details on telemetry spans and set error status.
*/
onError?: Callback<unknown>;
/**
* Optionally runs the language model call in a telemetry-integration-specific context. This enables
* auto-instrumented model provider requests to become children of the current
* model-call span.
*
* The options carry the model-call start-event content as context (the event
* fields are optional), alongside the always-present `callId` and the
* `execute` function that performs the model call.
*/
executeLanguageModelCall?: <T>(options: Partial<InferTelemetryEvent<LanguageModelCallStartEvent>> & {
callId: string;
execute: () => PromiseLike<T>;
}) => PromiseLike<T>;
/**
* Optionally runs the tool execute function in a telemetry-integration-specific context. This enables
* nested traces — e.g. when a tool's `execute` function calls `generateText`,
* the inner call's spans become children of the tool span.
*
* The options carry the tool-execution start-event content as context (the
* event fields are optional), alongside the always-present `callId`,
* `toolCallId`, and the `execute` function to run.
*/
executeTool?: <T>(options: Partial<InferTelemetryEvent<ToolExecutionStartEvent>> & {
callId: string;
toolCallId: string;
execute: () => PromiseLike<T>;
}) => PromiseLike<T>;
}
//#endregion
//#region src/global.d.ts
declare const aiSdkGlobalsKey: unique symbol;
declare global {
interface AISDKGlobalTypes {
[aiSdkGlobalsKey]: {
provider: ProviderV4 | ProviderV3 | ProviderV2;
logWarnings: LogWarningsFunction;
telemetry: Telemetry;
};
}
/**
* The default provider to use for the AI SDK.
* String model ids are resolved to the default provider and model id.
*
* If not set, the default provider is the Vercel AI gateway provider.
* An explicitly configured provider must support decision models to use experimental_decide.
*
* @see https://ai-sdk.dev/docs/ai-sdk-core/provider-management#global-provider-configuration
*/
var AI_SDK_DEFAULT_PROVIDER: AISDKGlobalTypes[keyof AISDKGlobalTypes]['provider'] | undefined;
/**
* The warning logger to use for the AI SDK.
*
* If not set, warnings use process.emitWarning when available, otherwise
* console.warn. Deprecations are emitted once per code by the default logger.
*
* If set to false, no warnings are logged.
*/
var AI_SDK_LOG_WARNINGS: AISDKGlobalTypes[keyof AISDKGlobalTypes]['logWarnings'] | undefined | false;
/**
* Globally registered telemetry integrations for the AI SDK.
*
* Integrations registered here receive lifecycle events (onStart, onStepStart,
* etc.) from every `generateText`, `streamText`, and similar call.
*
* Prefer using `registerTelemetry()` from `'ai'` instead of
* assigning this directly.
*/
var AI_SDK_TELEMETRY_INTEGRATIONS: Array<AISDKGlobalTypes[keyof AISDKGlobalTypes]['telemetry']> | undefined;
}
//#endregion
//#region src/prompt/convert-to-language-model-prompt.d.ts
export declare function convertToLanguageModelPrompt({ prompt, supportedUrls, download, abortSignal, provider }: {
prompt: StandardizedPrompt;
supportedUrls: Record<string, RegExp[]>;
download: DownloadFunction | undefined;
abortSignal?: AbortSignal;
provider?: string;
}): Promise<LanguageModelV4Prompt>;
/**
* Downloads files from URLs in the user messages.
*/
export declare function downloadAssets(messages: ModelMessage[], download: DownloadFunction, supportedUrls: Record<string, RegExp[]>, abortSignal?: AbortSignal): Promise<Record<string, {
mediaType: string | undefined;
data: Uint8Array;
}>>;
export declare function mapToolResultOutput({ output, provider, warnings, downloadedAssets }: {
output: ToolResultOutput;
provider?: string;
warnings?: Warning[];
downloadedAssets: Record<string, {
mediaType: string | undefined;
data: Uint8Array;
}>;
}): LanguageModelV4ToolResultOutput;
//#endregion
//#region src/prompt/create-tool-model-output.d.ts
export declare function createToolModelOutput({ toolCallId, input, output, tool, errorMode }: {
toolCallId: string;
input: unknown;
output: unknown;
tool: Tool | undefined;
errorMode: 'none' | 'text' | 'json';
}): Promise<ToolResultOutput>;
//#endregion
//#region src/prompt/prepare-tool-choice.d.ts
export declare function prepareToolChoice({ toolChoice }: {
toolChoice: ToolChoice<any> | undefined;
}): LanguageModelV4ToolChoice;
//#endregion
//#region src/prompt/prepare-tools.d.ts
export declare function prepareTools<TOOLS extends ToolSet>({ tools, toolOrder, toolsContext, experimental_sandbox: sandbox }: {
tools: TOOLS | undefined;
toolOrder?: ToolOrder<TOOLS>;
toolsContext?: InferToolSetContext<TOOLS>;
experimental_sandbox?: Experimental_SandboxSession;
}): Promise<Array<LanguageModelV4FunctionTool | LanguageModelV4ProviderTool> | undefined>;
//#endregion
//#region src/prompt/prepare-language-model-call-options.d.ts
/**
* Validates model call options and returns a new object with normalized values.
*/
export declare function prepareLanguageModelCallOptions({ maxOutputTokens, temperature, topP, topK, presencePenalty, frequencyPenalty, seed, stopSequences, reasoning }: LanguageModelCallOptions): LanguageModelCallOptions;
//#endregion
//#region src/util/prepare-retries.d.ts
/**
* Validate and prepare retries.
*/
export declare function prepareRetries({ maxRetries, abortSignal, additionalRetryableError, parameter, defaultMaxRetries }: {
maxRetries: number | undefined;
abortSignal: AbortSignal | undefined;
additionalRetryableError?: ShouldRetryFunction;
parameter?: string;
defaultMaxRetries?: number;
}): {
maxRetries: number;
retry: RetryFunction;
};
//#endregion
//#region src/model/resolve-model.d.ts
export declare function resolveLanguageModel(model: LanguageModel): LanguageModelV4;
//#endregion
//#region src/util/merge-abort-signals.d.ts
/**
* Merges multiple abort sources into a single `AbortSignal`.
* The returned signal will abort when any input signal aborts or when any
* numeric timeout elapses, using the reason from the first source to abort.
*
* @param signals - Abort signals or timeout durations in milliseconds.
* `null` and `undefined` values are ignored.
* @returns An `AbortSignal` that aborts when any valid source aborts,
* or `undefined` if no valid sources are provided.
*/
export declare function mergeAbortSignals(...signals: (AbortSignal | null | undefined | number)[]): AbortSignal | undefined;
//#endregion
//#region src/util/set-abort-timeout.d.ts
/**
* Schedules a timeout that aborts the given controller with a `TimeoutError`
* `DOMException`, matching the reason produced by `AbortSignal.timeout(ms)`.
*
* @param abortController - The controller to abort when the timeout elapses.
* If undefined, no timeout is scheduled.
* @param label - Human-readable label included in the error message
* (e.g. "Step", "Chunk").
* @param timeoutMs - Duration in milliseconds before the controller is aborted.
* If undefined, no timeout is scheduled.
* @returns The timeout id (suitable for passing to `clearTimeout`), or
* `undefined` if no timeout was scheduled.
*/
export declare function setAbortTimeout({ abortController, label, timeoutMs }: {
abortController: AbortController | undefined;
label: string;
timeoutMs: number | undefined;
}): ReturnType<typeof setTimeout> | undefined;
//#endregion
//#region src/util/merge-callbacks.d.ts
/**
* Creates an async callback that invokes the provided callbacks in parallel.
* Undefined callbacks are skipped, and thrown or rejected callback errors are
* ignored.
*
* @param callbacks The callbacks to invoke for each event.
* @returns A callback that forwards each event to all callbacks and waits for
* them to settle.
*/
export declare function mergeCallbacks<EVENT>(...callbacks: Array<Callback<EVENT> | undefined>): Callback<EVENT>;
//#endregion
//#region src/util/merge-objects.d.ts
/**
* Deeply merges two objects together.
* - Properties from the `overrides` object override those in the `base` object with the same key.
* - For nested objects, the merge is performed recursively (deep merge).
* - Arrays are replaced, not merged.
* - Primitive values are replaced.
* - If both `base` and `overrides` are undefined, returns undefined.
* - If one of `base` or `overrides` is undefined, returns the other.
*
* @param base The target object to merge into
* @param overrides The source object to merge from
* @returns A new object with the merged properties, or undefined if both inputs are undefined
*/
export declare function mergeObjects<T extends object, U extends object>(base: T | undefined, overrides: U | undefined): (T & U) | T | U | undefined;
//#endregion
//#region src/telemetry/create-telemetry-dispatcher.d.ts
/**
* Creates a telemetry dispatcher that sends telemetry events
* to the resolved set of integrations.
*
* When per-call integrations are provided, they take precedence over the globally
* registered integrations for that call. When no per-call integrations are
* provided, the globally registered integrations are used.
*
* @param args.telemetry - Optional per-call telemetry settings and integrations.
*
* @returns A telemetry dispatcher that fans out lifecycle events to the
* resolved set of integrations.
*/
export declare function createTelemetryDispatcher({ telemetry }: {
telemetry?: TelemetryOptions;
}): TelemetryDispatcher;
//#endregion
//#region src/telemetry/filter-included-context.d.ts
/**
* Returns a shallow copy of the runtime context with only top-level
* properties marked for telemetry inclusion.
*/
export declare function filterIncludedContext<CONTEXT extends Context>({ context, includeContext }: {
context: CONTEXT;
includeContext: IncludedContext<CONTEXT>;
}): Context;
//#endregion
//#region src/generate-text/restricted-telemetry-dispatcher.d.ts
/**
* Telemetry dispatcher for text generation with callbacks typed to the
* operation-specific tool set, runtime context, and output shape.
*/
type RestrictedTelemetryDispatcher<TOOLS extends ToolSet, RUNTIME_CONTEXT extends Context, OUTPUT extends Output> = Omit<TelemetryDispatcher, 'onStart' | 'onStepStart' | 'onStepEnd' | 'onStepFinish' | 'onStepError' | 'onEnd' | 'onAbort' | 'onToolExecutionStart' | 'onToolExecutionEnd'> & {
onStart: GenerateTextOnStartCallback<TOOLS, RUNTIME_CONTEXT, OUTPUT>;
onStepStart: GenerateTextOnStepStartCallback<TOOLS, RUNTIME_CONTEXT, OUTPUT>;
onStepEnd: GenerateTextOnStepEndCallback<TOOLS, RUNTIME_CONTEXT>;
/** @deprecated Use `onStepEnd` instead. */
onStepFinish: GenerateTextOnStepFinishCallback<TOOLS, RUNTIME_CONTEXT>;
onStepError: GenerateTextOnStepErrorCallback<TOOLS, RUNTIME_CONTEXT, OUTPUT>;
onEnd: GenerateTextOnEndCallback<TOOLS, RUNTIME_CONTEXT>;
onAbort?: GenerateTextOnAbortCallback<TOOLS, RUNTIME_CONTEXT>;
onToolExecutionStart?: OnToolExecutionStartCallback<TOOLS>;
onToolExecutionEnd?: OnToolExecutionEndCallback<TOOLS>;
};
/**
* Creates a telemetry dispatcher that only includes configured runtime context
* properties in text-generation lifecycle events before dispatching them.
*/
export declare function createRestrictedTelemetryDispatcher<TOOLS extends ToolSet, RUNTIME_CONTEXT extends Context, OUTPUT extends Output>({ telemetry, includeRuntimeContext, includeToolsContext }: {
telemetry?: TelemetryOptions<RUNTIME_CONTEXT, TOOLS>;
includeRuntimeContext: IncludedContext<RUNTIME_CONTEXT>;
includeToolsContext?: IncludedToolsContext<TOOLS>;
}): RestrictedTelemetryDispatcher<TOOLS, RUNTIME_CONTEXT, OUTPUT>;
//#endregion
//#region src/generate-text/parse-tool-call.d.ts
export declare function parseToolCall<TOOLS extends ToolSet>({ toolCall, tools, repairToolCall, refineToolInput, messages, instructions, abortSignal }: {
toolCall: LanguageModelV4ToolCall;
tools: TOOLS | undefined;
repairToolCall: ToolCallRepairFunction<TOOLS> | undefined;
refineToolInput?: ToolInputRefinement<TOOLS> | undefined;
instructions: Instructions | undefined;
messages: ModelMessage[];
abortSignal?: AbortSignal;
}): Promise<TypedToolCall<TOOLS>>;
//#endregion
//#region src/generate-text/stream-language-model-call.d.ts
type LanguageModelStreamPart<TOOLS extends ToolSet = ToolSet> = Exclude<TextStreamPart<TOOLS>, {
type: 'finish' | 'stream-start' | 'tool-output-denied' | 'start-step' | 'finish-step' | 'steering-message' | 'start' | 'abort';
}> | TextStreamTextDeltaPart | TextStreamReasoningDeltaPart | TextStreamFilePart | TextStreamReasoningFilePart | TextStreamToolApprovalRequestPart<TOOLS> | TextStreamToolApprovalResponsePart<TOOLS> | TextStreamToolCallPart<TOOLS> | TextStreamToolResultPart<TOOLS> | TextStreamToolErrorPart<TOOLS> | {
type: 'model-call-end';
finishReason: FinishReason;
rawFinishReason: string | undefined;
usage: LanguageModelUsage;
providerMetadata?: ProviderMetadata;
performance: {
responseTimeMs: number;
effectiveOutputTokensPerSecond: number;
outputTokensPerSecond: number | undefined;
inputTokensPerSecond: number | undefined;
effectiveTotalTokensPerSecond: number;
timeToFirstOutputMs: number | undefined;
timeBetweenOutputChunksMs?: OutputChunkTimingStats;
};
} | {
type: 'model-call-start';
warnings: Array<CallWarning>;
} | {
type: 'model-call-response-metadata';
/**
* ID for the generated response, if the provider sends one.
*/
id?: string;
/**
* Timestamp for the start of the generated response, if the provider sends one.
*/
timestamp?: Date;
/**
* The ID of the response model that was used to generate the response, if the provider sends one.
*/
modelId?: string;
};
//#endregion
//#region src/generate-text/stream-retry-attempt-boundary.d.ts
declare const streamRetryAttemptBoundarySymbol: unique symbol;
type StreamRetryAttemptBoundaryPart = {
[streamRetryAttemptBoundarySymbol]: true;
warnings: Array<SharedV4Warning>;
};
//#endregion
//#region src/generate-text/execute-tools-from-stream.d.ts
type ToolExecutionEndStreamPart = {
type: 'tool-execution-end';
toolCallId: string;
toolExecutionMs: number;
};
type ExecuteToolsStreamPart<TOOLS extends ToolSet = ToolSet> = LanguageModelStreamPart<TOOLS> | StaticToolOutputDenied<TOOLS> | ToolExecutionEndStreamPart | StreamRetryAttemptBoundaryPart;
//#endregion
//#region src/generate-text/is-output-chunk.d.ts
export declare function isOutputChunk(chunk: Exclude<ExecuteToolsStreamPart, StreamRetryAttemptBoundaryPart>): boolean;
//#endregion
//#region src/generate-text/validate-tool-context.d.ts
/**
* Validates a tool context value against the tool's optional context schema.
*
* When no context schema is defined, the original context value is returned as-is.
* Otherwise, the context is validated and normalized through the schema before
* being passed into tool execution and approval hooks.
*
* @throws {TypeValidationError} When the provided tool context does not match
* the tool's declared `contextSchema`.
*/
export declare function validateToolContext<CONTEXT>({ toolName, context, contextSchema }: {
toolName: string;
context: unknown;
contextSchema: FlexibleSchema<CONTEXT> | undefined;
}): Promise<CONTEXT>;
//#endregion
//#region src/tool-search/prepare-tool-search.d.ts
/** Create discovery state for one generation, never for a shared tool instance. */
export declare function createToolSearchState({ tools, toolCallers }: {
tools: ToolSet | undefined;
toolCallers: ResolvedToolCallers | undefined;
}): (activeTools: ToolSet | undefined, options?: {
toolsContext?: InferToolSetContext<ToolSet>;
experimental_sandbox?: Experimental_SandboxSession;
}) => ToolSet | undefined;
//#endregion
//#region src/agent/default-stop-condition.d.ts
/**
* Logs the warning for the default stopWhen condition.
*/
export declare function logDefaultStopConditionWarning({ stepCount, provider, modelId }: {
stepCount: number;
provider: string;
modelId: string;
}): void;
//#endregion
//#region src/generate-text/collect-tool-approvals.d.ts
type CollectedToolApprovals<TOOLS extends ToolSet> = {
approvalRequest: ToolApprovalRequest;
approvalResponse: ToolApprovalResponse;
toolCall: TypedToolCall<TOOLS>;
existingToolResult?: ToolResultPart;
};
/**
* If the last message is a tool message, this function collects all tool approvals
* from that message.
*/
export declare function collectToolApprovals<TOOLS extends ToolSet>({ messages }: {
messages: ModelMessage[];
}): {
approvedToolApprovals: Array<CollectedToolApprovals<TOOLS>>;
deniedToolApprovals: Array<CollectedToolApprovals<TOOLS>>;
};
//#endregion
//#region src/generate-text/tool-approval-signature.d.ts
export declare function signToolApproval({ secret, approvalId, toolCallId, toolName, input }: {
secret: string | Uint8Array;
approvalId: string;
toolCallId: string;
toolName: string;
input: unknown;
}): Promise<string>;
export declare function verifyToolApprovalSignature({ secret, signature, approvalId, toolCallId, toolName, input }: {
secret: string | Uint8Array;
signature: string;
approvalId: string;
toolCallId: string;
toolName: string;
input: unknown;
}): Promise<boolean>;
//#endregion
//#region src/generate-text/validate-tool-approvals.d.ts
/**
* Re-validates approved tool approvals reconstructed from client-supplied
* message history before they are executed. Checks HMAC signature (when
* configured), input schema, and approval policy.
*/
export declare function validateApprovedToolApprovals<TOOLS extends ToolSet, RUNTIME_CONTEXT extends Context | unknown | never>({ approvedToolApprovals, tools, toolApproval, messages, toolsContext, runtimeContext, toolApprovalSecret, refineToolInput }: {
approvedToolApprovals: Array<CollectedToolApprovals<TOOLS>>;
tools: TOOLS | undefined;
toolApproval: ToolApprovalConfiguration<TOOLS, RUNTIME_CONTEXT> | undefined;
messages: ModelMessage[];
toolsContext: InferToolSetContext<TOOLS>;
runtimeContext: RUNTIME_CONTEXT;
toolApprovalSecret?: string | Uint8Array;
refineToolInput?: ToolInputRefinement<TOOLS>;
}): Promise<{
approvedToolApprovals: Array<CollectedToolApprovals<TOOLS>>;
deniedToolApprovals: Array<CollectedToolApprovals<TOOLS>>;
invalidToolApprovals: Array<CollectedToolApprovals<TOOLS> & {
error: InvalidToolInputError;
}>;
}>;
//#endregion
//#region src/generate-text/resolve-tool-approval.d.ts
/**
* Resolves the approval state for a tool call by checking user-supplied and tool-defined
* approval settings, and normalizes the result to the object status shape.
* User-defined approval settings take precedence over tool-defined settings.
* If no approval settings are provided, the tool call does not require approval.
*/
export declare function resolveToolApproval<TOOLS extends ToolSet, RUNTIME_CONTEXT extends Context | unknown | never>({ tools, toolCall, toolApproval, messages, toolsContext, runtimeContext }: {
/**
* Tools that are available for the model to call.
*/
tools: TOOLS | undefined;
/**
* Valid tool call.
*/
toolCall: TypedToolCall<TOOLS>;
/**
* User-defined approval configuration for tools.
*
* This configuration takes precedence over tool-defined approval settings.
*/
toolApproval: ToolApprovalConfiguration<TOOLS, RUNTIME_CONTEXT> | undefined;
/**
* Messages that were sent to the language model to initiate the response that contained the tool call.
*/
messages: ModelMessage[];
/**
* Tool context as defined by the tool's context schema.
*/
toolsContext: InferToolSetContext<TOOLS>;
/**
* User-defined runtime context (same as `runtimeContext` on `generateText` / `streamText`).
*/
runtimeContext: RUNTIME_CONTEXT;
}): Promise<Exclude<ToolApprovalStatus, string | undefined>>;
//#endregion
//#region src/generate-text/to-response-messages.d.ts
/**
* Converts the result of a `generateText` or `streamText` call to a list of response messages.
*/
export declare function toResponseMessages<TOOLS extends ToolSet>({ content: inputContent, tools }: {
content: Array<ContentPart<TOOLS>>;
tools: TOOLS | undefined;
}): Promise<Array<AssistantModelMessage$1 | ToolModelMessage$1>>;
//#endregion
//#region src/generate-text/generate-text-result.d.ts
/**
* The result of a `generateText` call.
* It contains the generated text, the tool calls that were made during the generation, and the results of the tool calls.
*/
interface GenerateTextResult<TOOLS extends ToolSet, RUNTIME_CONTEXT extends Context, OUTPUT extends Output> {
/**
* The content that was generated in all steps.
*/
readonly content: Array<ContentPart<TOOLS>>;
/**
* The concatenation of all text parts generated in the final step.
* It is an empty string if the final step contains no text parts.
* Inspect `finalStep.content` to distinguish that case.
*/
readonly text: string;
/**
* The full reasoning that the model has generated in the last step.
*
* @deprecated Use `finalStep.reasoning` instead.
*/
readonly reasoning: Array<ReasoningOutput | ReasoningFileOutput>;
/**
* The reasoning text that the model has generated in the last step. Can be undefined if the model
* has only generated text.
*
* @deprecated Use `finalStep.reasoningText` instead.
*/
readonly reasoningText: string | undefined;
/**
* The files that were generated in all steps.
* Empty array if no files were generated.
*/
readonly files: Array<GeneratedFile>;
/**
* Sources that have been used as references in all steps.
*/
readonly sources: Array<Source>;
/**
* The tool calls that were made in all steps.
*/
readonly toolCalls: Array<TypedToolCall<TOOLS>>;
/**
* The static tool calls that were made in all steps.
*/
readonly staticToolCalls: Array<StaticToolCall<TOOLS>>;
/**
* The dynamic tool calls that were made in all steps.
*/
readonly dynamicToolCalls: Array<DynamicToolCall>;
/**
* The results of the tool calls from all steps.
*/
readonly toolResults: Array<TypedToolResult<TOOLS>>;
/**
* The static tool results that were made in all steps.
*/
readonly staticToolResults: Array<StaticToolResult<TOOLS>>;
/**
* The dynamic tool results that were made in all steps.
*/
readonly dynamicToolResults: Array<DynamicToolResult>;
/**
* The unified reason why the generation finished.
*/
readonly finishReason: FinishReason;
/**
* The raw reason why the generation finished (from the provider).
*/
readonly rawFinishReason: string | undefined;
/**
* The total token usage of all steps.
* When there are multiple steps, the usage is the sum of all step usages.
*/
readonly usage: LanguageModelUsage;
/**
* The total token usage of all steps.
* When there are multiple steps, the usage is the sum of all step usages.
*
* @deprecated Use `usage` instead.
*/
readonly totalUsage: LanguageModelUsage;
/**
* Warnings from the model provider (e.g. unsupported settings) in all steps.
*/
readonly warnings: CallWarning[] | undefined;
/**
* Additional request information from the last step.
*
* @deprecated Use `finalStep.request` instead.
*/
readonly request: StepResult<TOOLS, RUNTIME_CONTEXT>['request'];
/**
* Additional response information from the last step.
*
* @deprecated Use `finalStep.response` instead.
*/
readonly response: LanguageModelResponseMetadata;
/**
* The accumulated response messages of all steps that were generated during the call.
*/
readonly responseMessages: Array<ResponseMessage>;
/**
* Additional provider-specific metadata from the final step. They are passed
* through from the provider to the AI SDK and enable provider-specific
* results that can be fully encapsulated in the provider.
*
* @deprecated Use `finalStep.providerMetadata` instead.
*/
readonly providerMetadata: ProviderMetadata | undefined;
/**
* Details for all steps.
* You can use this to get information about intermediate steps,
* such as the tool calls or the response headers.
*/
readonly steps: Array<StepResult<TOOLS, RUNTIME_CONTEXT>>;
/**
* The final step. This is a shortcut for `steps.at(-1)`.
*/
readonly finalStep: StepResult<TOOLS, RUNTIME_CONTEXT>;
/**
* The generated output according to the `output` specification.
*
* @throws {NoOutputGeneratedError} When no output is available, for example
* when the final step finishes with a `tool-calls` reason, or when it
* contains no text and does not finish with a `stop` reason.
*/
readonly output: InferCompleteOutput<OUTPUT>;
}
//#endregion
//#region src/generate-text/default-generate-text-result.d.ts
export declare class DefaultGenerateTextResult<TOOLS extends ToolSet, RUNTIME_CONTEXT extends Context, OUTPUT extends Output> implements GenerateTextResult<TOOLS, RUNTIME_CONTEXT, OUTPUT> {
readonly steps: GenerateTextResult<TOOLS, RUNTIME_CONTEXT, OUTPUT>['steps'];
readonly totalUsage: LanguageModelUsage;
private readonly _output;
constructor(options: {
initialResponseMessages: Array<ResponseMessage>;
steps: GenerateTextResult<TOOLS, RUNTIME_CONTEXT, OUTPUT>['steps'];
output: InferCompleteOutput<OUTPUT> | undefined;
totalUsage: LanguageModelUsage;
});
private readonly initialResponseMessages;
get finalStep(): StepResult<TOOLS, RUNTIME_CONTEXT>;
get content(): ContentPart<TOOLS>[];
get text(): string;
get files(): GeneratedFile[];
get reasoningText(): string | undefined;
get reasoning(): (ReasoningOutput | ReasoningFileOutput)[];
get toolCalls(): TypedToolCall<TOOLS>[];
get staticToolCalls(): StaticToolCall<TOOLS>[];
get dynamicToolCalls(): DynamicToolCall[];
get toolResults(): TypedToolResult<TOOLS>[];
get staticToolResults(): StaticToolResult<TOOLS>[];
get dynamicToolResults(): DynamicToolResult[];
get sources(): import("@ai-sdk/provider").LanguageModelV4Source[];
get finishReason(): FinishReason;
get rawFinishReason(): string | undefined;
get warnings(): import("@ai-sdk/provider").SharedV4Warning[];
get providerMetadata(): import("@ai-sdk/provider").SharedV4ProviderMetadata | undefined;
get response(): LanguageModelResponseMetadata;
get responseMessages(): ResponseMessage[];
get request(): LanguageModelRequestMetadata;
get usage(): LanguageModelUsage;
get output(): NonNullable<InferCompleteOutput<OUTPUT>>;
}
//#endregion
//#region src/generate-text/resolve-generated-file-data.d.ts
type GeneratedFileData = Extract<LanguageModelV4Content, {
type: 'file' | 'reasoning-file';
}>['data'];
type GeneratedFileDataCache = WeakMap<GeneratedFileData, string | Uint8Array>;
//#endregion
//#region src/generate-text/convert-language-model-content.d.ts
export declare function convertLanguageModelContent<TOOLS extends ToolSet>({ content, toolCalls, toolOutputs, toolApprovalRequests, toolApprovalResponses, tools, download, abortSignal, generatedFileDataCache }: {
content: Array<LanguageModelV4Content>;
toolCalls: Array<TypedToolCall<TOOLS>>;
toolOutputs: Array<ToolOutput<TOOLS>>;
toolApprovalRequests: Array<ToolApprovalRequestOutput<TOOLS>>;
toolApprovalResponses: Array<ToolApprovalResponseOutput<TOOLS>>;
tools: TOOLS | undefined;
download?: DownloadFunction;
abortSignal?: AbortSignal;
generatedFileDataCache?: GeneratedFileDataCache;
}): Promise<Array<ContentPart<TOOLS>>>;
//#endregion
//#region src/generate-text/calculate-tokens-per-second.d.ts
/**
* Calculates a token rate in tokens per second.
*
* Returns 0 when the token count is unknown, the duration is unknown or 0, or
* the computed rate cannot be represented as a finite JSON-safe number.
*/
export declare function calculateTokensPerSecond({ tokens, durationMs }: {
tokens: number | undefined;
durationMs: number | undefined;
}): number;
//#endregion
//#region src/generate-text/tool-call-concurrency.d.ts
/**
* Validates the maximum number of tool calls that may execute concurrently.
*/
export declare function prepareToolCallConcurrency(toolCallConcurrency: number | undefined): number | undefined;
/**
* Maps items with an optional concurrency limit while preserving result order.
*
* Workers claim items in input order. When the signal is aborted or an
* execution rejects, no additional queued items are started.
*/
export declare function mapWithConcurrency<INPUT, OUTPUT>({ items, concurrency, abortSignal, execute }: {
items: Array<INPUT>;
concurrency: number | undefined;
abortSignal?: AbortSignal;
execute: (item: INPUT, index: number) => Promise<OUTPUT>;
}): Promise<Array<OUTPUT>>;
//#endregion
export { type CollectedToolApprovals, type DownloadFunction, type ToolsContextSettings, convertAsyncIteratorToReadableStream, prepareLanguageModelCallOptions as prepareCallSettings };
//# sourceMappingURL=index.d.ts.map