@ai-sdk/workflow 1.0.0-beta.23 → 1.0.0-beta.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # @ai-sdk/workflow
2
2
 
3
+ ## 1.0.0-beta.24
4
+
5
+ ### Patch Changes
6
+
7
+ - eea8d98: refactoring: rename tool execution events
8
+ - Updated dependencies [ec98264]
9
+ - Updated dependencies [eea8d98]
10
+ - Updated dependencies [75ef93e]
11
+ - ai@7.0.0-beta.109
12
+ - @ai-sdk/provider-utils@5.0.0-beta.25
13
+
3
14
  ## 1.0.0-beta.23
4
15
 
5
16
  ### Patch Changes
package/dist/index.d.mts CHANGED
@@ -374,11 +374,11 @@ interface WorkflowAgentOptions<TTools extends ToolSet = ToolSet> extends Generat
374
374
  /**
375
375
  * Callback called before a tool's execute function runs.
376
376
  */
377
- experimental_onToolCallStart?: WorkflowAgentOnToolCallStartCallback;
377
+ experimental_onToolExecutionStart?: WorkflowAgentOnToolExecutionStartCallback;
378
378
  /**
379
379
  * Callback called after a tool execution completes.
380
380
  */
381
- experimental_onToolCallFinish?: WorkflowAgentOnToolCallFinishCallback;
381
+ experimental_onToolExecutionEnd?: WorkflowAgentOnToolExecutionEndCallback;
382
382
  /**
383
383
  * Prepare the parameters for the stream call.
384
384
  * Called once before the agent loop starts. Use this to transform
@@ -460,7 +460,7 @@ type WorkflowAgentOnStepStartCallback<TTools extends ToolSet = ToolSet> = (event
460
460
  /**
461
461
  * Callback that is called before a tool's execute function runs.
462
462
  */
463
- type WorkflowAgentOnToolCallStartCallback = (event: {
463
+ type WorkflowAgentOnToolExecutionStartCallback = (event: {
464
464
  /** The tool call being executed */
465
465
  readonly toolCall: ToolCall;
466
466
  /** The current step number (0-based) */
@@ -471,7 +471,7 @@ type WorkflowAgentOnToolCallStartCallback = (event: {
471
471
  * Uses a discriminated union pattern: check `success` to determine
472
472
  * whether `output` or `error` is available.
473
473
  */
474
- type WorkflowAgentOnToolCallFinishCallback = (event: {
474
+ type WorkflowAgentOnToolExecutionEndCallback = (event: {
475
475
  /** The tool call that was executed */
476
476
  readonly toolCall: ToolCall;
477
477
  /** The current step number (0-based) */
@@ -653,11 +653,11 @@ type WorkflowAgentStreamOptions<TTools extends ToolSet = ToolSet, OUTPUT = never
653
653
  /**
654
654
  * Callback called before a tool's execute function runs.
655
655
  */
656
- experimental_onToolCallStart?: WorkflowAgentOnToolCallStartCallback;
656
+ experimental_onToolExecutionStart?: WorkflowAgentOnToolExecutionStartCallback;
657
657
  /**
658
658
  * Callback called after a tool execution completes.
659
659
  */
660
- experimental_onToolCallFinish?: WorkflowAgentOnToolCallFinishCallback;
660
+ experimental_onToolExecutionEnd?: WorkflowAgentOnToolExecutionEndCallback;
661
661
  /**
662
662
  * Callback function called before each step in the agent loop.
663
663
  * Use this to modify settings, manage context, or inject messages dynamically.
@@ -817,8 +817,8 @@ declare class WorkflowAgent<TBaseTools extends ToolSet = ToolSet> {
817
817
  private constructorOnFinish?;
818
818
  private constructorOnStart?;
819
819
  private constructorOnStepStart?;
820
- private constructorOnToolCallStart?;
821
- private constructorOnToolCallFinish?;
820
+ private constructorOnToolExecutionStart?;
821
+ private constructorOnToolExecutionEnd?;
822
822
  private prepareCall?;
823
823
  constructor(options: WorkflowAgentOptions<TBaseTools>);
824
824
  generate(): void;
@@ -991,4 +991,4 @@ declare class WorkflowChatTransport<UI_MESSAGE extends UIMessage> implements Cha
991
991
  private onFinish;
992
992
  }
993
993
 
994
- export { type CompatibleLanguageModel, type DownloadFunction, type GenerationSettings, type InferWorkflowAgentTools, type InferWorkflowAgentUIMessage, type OutputSpecification, type PrepareCallCallback, type PrepareCallOptions, type PrepareCallResult, type PrepareStepCallback, type PrepareStepInfo, type PrepareStepResult, type ProviderOptions, type ReconnectToStreamOptions, type SendMessagesOptions, type StreamTextTransform, type TelemetryOptions, WorkflowAgent, type WorkflowAgentOnAbortCallback, type WorkflowAgentOnErrorCallback, type WorkflowAgentOnFinishCallback, type WorkflowAgentOnStartCallback, type WorkflowAgentOnStepFinishCallback, type WorkflowAgentOnStepStartCallback, type WorkflowAgentOnToolCallFinishCallback, type WorkflowAgentOnToolCallStartCallback, type WorkflowAgentOptions, type WorkflowAgentStreamOptions, type WorkflowAgentStreamResult, WorkflowChatTransport, type WorkflowChatTransportOptions, createModelCallToUIChunkTransform, toUIMessageChunk };
994
+ export { type CompatibleLanguageModel, type DownloadFunction, type GenerationSettings, type InferWorkflowAgentTools, type InferWorkflowAgentUIMessage, type OutputSpecification, type PrepareCallCallback, type PrepareCallOptions, type PrepareCallResult, type PrepareStepCallback, type PrepareStepInfo, type PrepareStepResult, type ProviderOptions, type ReconnectToStreamOptions, type SendMessagesOptions, type StreamTextTransform, type TelemetryOptions, WorkflowAgent, type WorkflowAgentOnAbortCallback, type WorkflowAgentOnErrorCallback, type WorkflowAgentOnFinishCallback, type WorkflowAgentOnStartCallback, type WorkflowAgentOnStepFinishCallback, type WorkflowAgentOnStepStartCallback, type WorkflowAgentOnToolExecutionEndCallback, type WorkflowAgentOnToolExecutionStartCallback, type WorkflowAgentOptions, type WorkflowAgentStreamOptions, type WorkflowAgentStreamResult, WorkflowChatTransport, type WorkflowChatTransportOptions, createModelCallToUIChunkTransform, toUIMessageChunk };
package/dist/index.mjs CHANGED
@@ -562,8 +562,8 @@ var WorkflowAgent = class {
562
562
  this.constructorOnFinish = options.onFinish;
563
563
  this.constructorOnStart = options.experimental_onStart;
564
564
  this.constructorOnStepStart = options.experimental_onStepStart;
565
- this.constructorOnToolCallStart = options.experimental_onToolCallStart;
566
- this.constructorOnToolCallFinish = options.experimental_onToolCallFinish;
565
+ this.constructorOnToolExecutionStart = options.experimental_onToolExecutionStart;
566
+ this.constructorOnToolExecutionEnd = options.experimental_onToolExecutionEnd;
567
567
  this.prepareCall = options.prepareCall;
568
568
  this.generationSettings = {
569
569
  maxOutputTokens: options.maxOutputTokens,
@@ -790,13 +790,13 @@ var WorkflowAgent = class {
790
790
  this.constructorOnStepStart,
791
791
  options.experimental_onStepStart
792
792
  );
793
- const mergedOnToolCallStart = mergeCallbacks(
794
- this.constructorOnToolCallStart,
795
- options.experimental_onToolCallStart
793
+ const mergedOnToolExecutionStart = mergeCallbacks(
794
+ this.constructorOnToolExecutionStart,
795
+ options.experimental_onToolExecutionStart
796
796
  );
797
- const mergedOnToolCallFinish = mergeCallbacks(
798
- this.constructorOnToolCallFinish,
799
- options.experimental_onToolCallFinish
797
+ const mergedOnToolExecutionEnd = mergeCallbacks(
798
+ this.constructorOnToolExecutionEnd,
799
+ options.experimental_onToolExecutionEnd
800
800
  );
801
801
  const effectiveToolChoice = effectiveToolChoiceFromPrepare;
802
802
  const effectiveTelemetry = effectiveTelemetryFromPrepare;
@@ -822,8 +822,8 @@ var WorkflowAgent = class {
822
822
  toolName: toolCall.toolName,
823
823
  input: toolCall.input
824
824
  };
825
- if (mergedOnToolCallStart) {
826
- await mergedOnToolCallStart({
825
+ if (mergedOnToolExecutionStart) {
826
+ await mergedOnToolExecutionStart({
827
827
  toolCall: toolCallEvent,
828
828
  stepNumber: currentStepNumber
829
829
  });
@@ -834,8 +834,8 @@ var WorkflowAgent = class {
834
834
  result = await executeTool(toolCall, tools, messages2, context);
835
835
  } catch (err) {
836
836
  const durationMs2 = Date.now() - startTime;
837
- if (mergedOnToolCallFinish) {
838
- await mergedOnToolCallFinish({
837
+ if (mergedOnToolExecutionEnd) {
838
+ await mergedOnToolExecutionEnd({
839
839
  toolCall: toolCallEvent,
840
840
  stepNumber: currentStepNumber,
841
841
  durationMs: durationMs2,
@@ -846,10 +846,10 @@ var WorkflowAgent = class {
846
846
  throw err;
847
847
  }
848
848
  const durationMs = Date.now() - startTime;
849
- if (mergedOnToolCallFinish) {
849
+ if (mergedOnToolExecutionEnd) {
850
850
  const isError = result.output && "type" in result.output && (result.output.type === "error-text" || result.output.type === "error-json");
851
851
  if (isError) {
852
- await mergedOnToolCallFinish({
852
+ await mergedOnToolExecutionEnd({
853
853
  toolCall: toolCallEvent,
854
854
  stepNumber: currentStepNumber,
855
855
  durationMs,
@@ -857,7 +857,7 @@ var WorkflowAgent = class {
857
857
  error: "value" in result.output ? result.output.value : void 0
858
858
  });
859
859
  } else {
860
- await mergedOnToolCallFinish({
860
+ await mergedOnToolExecutionEnd({
861
861
  toolCall: toolCallEvent,
862
862
  stepNumber: currentStepNumber,
863
863
  durationMs,
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/workflow-agent.ts","../src/stream-text-iterator.ts","../src/do-stream-step.ts","../src/serializable-schema.ts","../src/to-ui-message-chunk.ts","../src/workflow-chat-transport.ts"],"sourcesContent":["import type {\n JSONValue,\n LanguageModelV4CallOptions,\n LanguageModelV4Prompt,\n LanguageModelV4StreamPart,\n LanguageModelV4ToolResultPart,\n SharedV4ProviderOptions,\n} from '@ai-sdk/provider';\nimport {\n type Experimental_LanguageModelStreamPart as ModelCallStreamPart,\n type FinishReason,\n type LanguageModelResponseMetadata,\n type LanguageModelUsage,\n type ModelMessage,\n Output,\n type StepResult,\n type StopCondition,\n type StreamTextOnStepFinishCallback,\n type SystemModelMessage,\n type ToolCallRepairFunction,\n type ToolChoice,\n type ToolSet,\n type UIMessage,\n LanguageModel,\n experimental_filterActiveTools as filterActiveTools,\n} from 'ai';\nimport {\n convertToLanguageModelPrompt,\n mergeAbortSignals,\n mergeCallbacks,\n standardizePrompt,\n} from 'ai/internal';\nimport { streamTextIterator } from './stream-text-iterator.js';\nimport type { CompatibleLanguageModel } from './types.js';\n\n// Re-export for consumers\nexport type { CompatibleLanguageModel } from './types.js';\n\n/**\n * Callback function to be called after each step completes.\n * Alias for the AI SDK's StreamTextOnStepFinishCallback, using\n * WorkflowAgent-consistent naming.\n */\nexport type WorkflowAgentOnStepFinishCallback<\n TTools extends ToolSet = ToolSet,\n> = StreamTextOnStepFinishCallback<TTools, any>;\n\n/**\n * Infer the type of the tools of a workflow agent.\n */\nexport type InferWorkflowAgentTools<WORKFLOW_AGENT> =\n WORKFLOW_AGENT extends WorkflowAgent<infer TOOLS> ? TOOLS : never;\n\n/**\n * Infer the UI message type of a workflow agent.\n */\nexport type InferWorkflowAgentUIMessage<\n _WORKFLOW_AGENT,\n MESSAGE_METADATA = unknown,\n> = UIMessage<MESSAGE_METADATA>;\n\n/**\n * Re-export the Output helper for structured output specifications.\n * Use `Output.object({ schema })` for structured output or `Output.text()` for text output.\n */\nexport { Output };\n\n/**\n * Output specification interface for structured outputs.\n * Use `Output.object({ schema })` or `Output.text()` to create an output specification.\n */\nexport interface OutputSpecification<OUTPUT, PARTIAL> {\n readonly name: string;\n responseFormat: PromiseLike<LanguageModelV4CallOptions['responseFormat']>;\n parsePartialOutput(options: {\n text: string;\n }): Promise<{ partial: PARTIAL } | undefined>;\n parseCompleteOutput(\n options: { text: string },\n context: {\n response: LanguageModelResponseMetadata;\n usage: LanguageModelUsage;\n finishReason: FinishReason;\n },\n ): Promise<OUTPUT>;\n}\n\n/**\n * Provider-specific options type. This is equivalent to SharedV4ProviderOptions from @ai-sdk/provider.\n */\nexport type ProviderOptions = SharedV4ProviderOptions;\n\n/**\n * Telemetry settings for observability.\n */\nexport interface TelemetryOptions {\n /**\n * Enable or disable telemetry. Defaults to true.\n */\n isEnabled?: boolean;\n\n /**\n * Identifier for this function. Used to group telemetry data by function.\n */\n functionId?: string;\n\n /**\n * Additional information to include in the telemetry data.\n */\n metadata?: Record<\n string,\n | string\n | number\n | boolean\n | Array<string | number | boolean>\n | null\n | undefined\n >;\n\n /**\n * Enable or disable input recording. Enabled by default.\n *\n * You might want to disable input recording to avoid recording sensitive\n * information, to reduce data transfers, or to increase performance.\n */\n recordInputs?: boolean;\n\n /**\n * Enable or disable output recording. Enabled by default.\n *\n * You might want to disable output recording to avoid recording sensitive\n * information, to reduce data transfers, or to increase performance.\n */\n recordOutputs?: boolean;\n\n /**\n * Custom tracer for the telemetry.\n */\n tracer?: unknown;\n}\n\n/**\n * A transformation that is applied to the stream.\n */\nexport type StreamTextTransform<TTools extends ToolSet> = (options: {\n tools: TTools;\n stopStream: () => void;\n}) => TransformStream<LanguageModelV4StreamPart, LanguageModelV4StreamPart>;\n\n/**\n * Function to repair a tool call that failed to parse.\n * Re-exported from the AI SDK core.\n */\nexport type { ToolCallRepairFunction } from 'ai';\n\n/**\n * Custom download function for URLs.\n * The function receives an array of URLs with information about whether\n * the model supports them directly.\n */\nexport type DownloadFunction = (\n options: {\n url: URL;\n isUrlSupportedByModel: boolean;\n }[],\n) => PromiseLike<\n ({ data: Uint8Array; mediaType: string | undefined } | null)[]\n>;\n\n/**\n * Generation settings that can be passed to the model.\n * These map directly to LanguageModelV4CallOptions.\n */\nexport interface GenerationSettings {\n /**\n * Maximum number of tokens to generate.\n */\n maxOutputTokens?: number;\n\n /**\n * Temperature setting. The range depends on the provider and model.\n * It is recommended to set either `temperature` or `topP`, but not both.\n */\n temperature?: number;\n\n /**\n * Nucleus sampling. This is a number between 0 and 1.\n * E.g. 0.1 would mean that only tokens with the top 10% probability mass are considered.\n * It is recommended to set either `temperature` or `topP`, but not both.\n */\n topP?: number;\n\n /**\n * Only sample from the top K options for each subsequent token.\n * Used to remove \"long tail\" low probability responses.\n * Recommended for advanced use cases only. You usually only need to use temperature.\n */\n topK?: number;\n\n /**\n * Presence penalty setting. It affects the likelihood of the model to\n * repeat information that is already in the prompt.\n * The presence penalty is a number between -1 (increase repetition)\n * and 1 (maximum penalty, decrease repetition). 0 means no penalty.\n */\n presencePenalty?: number;\n\n /**\n * Frequency penalty setting. It affects the likelihood of the model\n * to repeatedly use the same words or phrases.\n * The frequency penalty is a number between -1 (increase repetition)\n * and 1 (maximum penalty, decrease repetition). 0 means no penalty.\n */\n frequencyPenalty?: number;\n\n /**\n * Stop sequences. If set, the model will stop generating text when one of the stop sequences is generated.\n * Providers may have limits on the number of stop sequences.\n */\n stopSequences?: string[];\n\n /**\n * The seed (integer) to use for random sampling. If set and supported\n * by the model, calls will generate deterministic results.\n */\n seed?: number;\n\n /**\n * Maximum number of retries. Set to 0 to disable retries.\n * Note: In workflow context, retries are typically handled by the workflow step mechanism.\n * @default 2\n */\n maxRetries?: number;\n\n /**\n * Abort signal for cancelling the operation.\n */\n abortSignal?: AbortSignal;\n\n /**\n * Additional HTTP headers to be sent with the request.\n * Only applicable for HTTP-based providers.\n */\n headers?: Record<string, string | undefined>;\n\n /**\n * Additional provider-specific options. They are passed through\n * to the provider from the AI SDK and enable provider-specific\n * functionality that can be fully encapsulated in the provider.\n */\n providerOptions?: ProviderOptions;\n}\n\n/**\n * Information passed to the prepareStep callback.\n */\nexport interface PrepareStepInfo<TTools extends ToolSet = ToolSet> {\n /**\n * The current model configuration (string or function).\n * The function should return a LanguageModelV4 instance.\n */\n model: LanguageModel;\n\n /**\n * The current step number (0-indexed).\n */\n stepNumber: number;\n\n /**\n * All previous steps with their results.\n */\n steps: StepResult<TTools, any>[];\n\n /**\n * The messages that will be sent to the model.\n * This is the LanguageModelV4Prompt format used internally.\n */\n messages: LanguageModelV4Prompt;\n\n /**\n * The context passed via the experimental_context setting (experimental).\n */\n experimental_context: unknown;\n}\n\n/**\n * Return type from the prepareStep callback.\n * All properties are optional - only return the ones you want to override.\n */\nexport interface PrepareStepResult extends Partial<GenerationSettings> {\n /**\n * Override the model for this step.\n */\n model?: LanguageModel;\n\n /**\n * Override the system message for this step.\n */\n system?: string;\n\n /**\n * Override the messages for this step.\n * Use this for context management or message injection.\n */\n messages?: LanguageModelV4Prompt;\n\n /**\n * Override the tool choice for this step.\n */\n toolChoice?: ToolChoice<ToolSet>;\n\n /**\n * Override the active tools for this step.\n * Limits the tools that are available for the model to call.\n */\n activeTools?: string[];\n\n /**\n * Context that is passed into tool execution. Experimental.\n * Changing the context will affect the context in this step and all subsequent steps.\n */\n experimental_context?: unknown;\n}\n\n/**\n * Callback function called before each step in the agent loop.\n * Use this to modify settings, manage context, or implement dynamic behavior.\n */\nexport type PrepareStepCallback<TTools extends ToolSet = ToolSet> = (\n info: PrepareStepInfo<TTools>,\n) => PrepareStepResult | Promise<PrepareStepResult>;\n\n/**\n * Options passed to the prepareCall callback.\n */\nexport interface PrepareCallOptions<\n TTools extends ToolSet = ToolSet,\n> extends Partial<GenerationSettings> {\n model: LanguageModel;\n tools: TTools;\n instructions?: string | SystemModelMessage | Array<SystemModelMessage>;\n toolChoice?: ToolChoice<TTools>;\n experimental_telemetry?: TelemetryOptions;\n experimental_context?: unknown;\n messages: ModelMessage[];\n}\n\n/**\n * Result of the prepareCall callback. All fields are optional —\n * only returned fields override the defaults.\n * Note: `tools` cannot be overridden via prepareCall because they are\n * bound at construction time for type safety.\n */\nexport type PrepareCallResult<TTools extends ToolSet = ToolSet> = Partial<\n Omit<PrepareCallOptions<TTools>, 'tools'>\n>;\n\n/**\n * Callback called once before the agent loop starts to transform call parameters.\n */\nexport type PrepareCallCallback<TTools extends ToolSet = ToolSet> = (\n options: PrepareCallOptions<TTools>,\n) => PrepareCallResult<TTools> | Promise<PrepareCallResult<TTools>>;\n\n/**\n * Configuration options for creating a {@link WorkflowAgent} instance.\n */\nexport interface WorkflowAgentOptions<\n TTools extends ToolSet = ToolSet,\n> extends GenerationSettings {\n /**\n * The id of the agent.\n */\n id?: string;\n\n /**\n * The model provider to use for the agent.\n *\n * This should be a string compatible with the Vercel AI Gateway (e.g., 'anthropic/claude-opus'),\n * or a LanguageModelV4 instance from a provider.\n */\n model: LanguageModel;\n\n /**\n * A set of tools available to the agent.\n * Tools can be implemented as workflow steps for automatic retries and persistence,\n * or as regular workflow-level logic using core library features like sleep() and Hooks.\n */\n tools?: TTools;\n\n /**\n * Agent instructions. Can be a string, a SystemModelMessage, or an array of SystemModelMessages.\n * Supports provider-specific options (e.g., caching) when using the SystemModelMessage form.\n */\n instructions?: string | SystemModelMessage | Array<SystemModelMessage>;\n\n /**\n * Optional system prompt to guide the agent's behavior.\n * @deprecated Use `instructions` instead.\n */\n system?: string;\n\n /**\n * The tool choice strategy. Default: 'auto'.\n */\n toolChoice?: ToolChoice<TTools>;\n\n /**\n * Optional telemetry configuration (experimental).\n */\n experimental_telemetry?: TelemetryOptions;\n\n /**\n * Default context that is passed into tool execution for every stream call on this agent.\n *\n * Per-stream `experimental_context` values passed to `stream()` override this default.\n * Experimental (can break in patch releases).\n * @default undefined\n */\n experimental_context?: unknown;\n\n /**\n * Default stop condition for the agent loop. When the condition is an array,\n * any of the conditions can be met to stop the generation.\n *\n * Per-stream `stopWhen` values passed to `stream()` override this default.\n */\n stopWhen?:\n | StopCondition<NoInfer<ToolSet>, any>\n | Array<StopCondition<NoInfer<ToolSet>, any>>;\n\n /**\n * Default set of active tools that limits which tools the model can call,\n * without changing the tool call and result types in the result.\n *\n * Per-stream `activeTools` values passed to `stream()` override this default.\n */\n activeTools?: Array<keyof NoInfer<TTools>>;\n\n /**\n * Default output specification for structured outputs.\n * Use `Output.object({ schema })` for structured output or `Output.text()` for text output.\n *\n * Per-stream `output` values passed to `stream()` override this default.\n */\n output?: OutputSpecification<any, any>;\n\n /**\n * Default function that attempts to repair a tool call that failed to parse.\n *\n * Per-stream `experimental_repairToolCall` values passed to `stream()` override this default.\n */\n experimental_repairToolCall?: ToolCallRepairFunction<TTools>;\n\n /**\n * Default custom download function to use for URLs.\n *\n * Per-stream `experimental_download` values passed to `stream()` override this default.\n */\n experimental_download?: DownloadFunction;\n\n /**\n * Default callback function called before each step in the agent loop.\n * Use this to modify settings, manage context, or inject messages dynamically\n * for every stream call on this agent instance.\n *\n * Per-stream `prepareStep` values passed to `stream()` override this default.\n */\n prepareStep?: PrepareStepCallback<TTools>;\n\n /**\n * Callback function to be called after each step completes.\n */\n onStepFinish?: WorkflowAgentOnStepFinishCallback<ToolSet>;\n\n /**\n * Callback that is called when the LLM response and all request tool executions are finished.\n */\n onFinish?: WorkflowAgentOnFinishCallback<ToolSet>;\n\n /**\n * Callback called when the agent starts streaming, before any LLM calls.\n */\n experimental_onStart?: WorkflowAgentOnStartCallback;\n\n /**\n * Callback called before each step (LLM call) begins.\n */\n experimental_onStepStart?: WorkflowAgentOnStepStartCallback;\n\n /**\n * Callback called before a tool's execute function runs.\n */\n experimental_onToolCallStart?: WorkflowAgentOnToolCallStartCallback;\n\n /**\n * Callback called after a tool execution completes.\n */\n experimental_onToolCallFinish?: WorkflowAgentOnToolCallFinishCallback;\n\n /**\n * Prepare the parameters for the stream call.\n * Called once before the agent loop starts. Use this to transform\n * model, tools, instructions, or other settings based on runtime context.\n */\n prepareCall?: PrepareCallCallback<TTools>;\n}\n\n/**\n * Callback that is called when the LLM response and all request tool executions are finished.\n */\nexport type WorkflowAgentOnFinishCallback<\n TTools extends ToolSet = ToolSet,\n OUTPUT = never,\n> = (event: {\n /**\n * Details for all steps.\n */\n readonly steps: StepResult<TTools, any>[];\n\n /**\n * The final messages including all tool calls and results.\n */\n readonly messages: ModelMessage[];\n\n /**\n * The text output from the last step.\n */\n readonly text: string;\n\n /**\n * The finish reason from the last step.\n */\n readonly finishReason: FinishReason;\n\n /**\n * The total token usage across all steps.\n */\n readonly totalUsage: LanguageModelUsage;\n\n /**\n * Context that is passed into tool execution.\n */\n readonly experimental_context: unknown;\n\n /**\n * The generated structured output. It uses the `output` specification.\n * Only available when `output` is specified.\n */\n readonly output: OUTPUT;\n}) => PromiseLike<void> | void;\n\n/**\n * Callback that is invoked when an error occurs during streaming.\n */\nexport type WorkflowAgentOnErrorCallback = (event: {\n error: unknown;\n}) => PromiseLike<void> | void;\n\n/**\n * Callback that is set using the `onAbort` option.\n */\nexport type WorkflowAgentOnAbortCallback<TTools extends ToolSet = ToolSet> =\n (event: {\n /**\n * Details for all previously finished steps.\n */\n readonly steps: StepResult<TTools, any>[];\n }) => PromiseLike<void> | void;\n\n/**\n * Callback that is called when the agent starts streaming, before any LLM calls.\n */\nexport type WorkflowAgentOnStartCallback = (event: {\n /** The model being used */\n readonly model: LanguageModel;\n /** The messages being sent */\n readonly messages: ModelMessage[];\n}) => PromiseLike<void> | void;\n\n/**\n * Callback that is called before each step (LLM call) begins.\n */\nexport type WorkflowAgentOnStepStartCallback<TTools extends ToolSet = ToolSet> =\n (event: {\n /** The current step number (0-based) */\n readonly stepNumber: number;\n /** The model being used for this step */\n readonly model: LanguageModel;\n /** The messages being sent for this step */\n readonly messages: ModelMessage[];\n /** Results from all previously finished steps */\n readonly steps: ReadonlyArray<StepResult<TTools, any>>;\n }) => PromiseLike<void> | void;\n\n/**\n * Callback that is called before a tool's execute function runs.\n */\nexport type WorkflowAgentOnToolCallStartCallback = (event: {\n /** The tool call being executed */\n readonly toolCall: ToolCall;\n /** The current step number (0-based) */\n readonly stepNumber: number;\n}) => PromiseLike<void> | void;\n\n/**\n * Callback that is called after a tool execution completes.\n * Uses a discriminated union pattern: check `success` to determine\n * whether `output` or `error` is available.\n */\nexport type WorkflowAgentOnToolCallFinishCallback = (\n event:\n | {\n /** The tool call that was executed */\n readonly toolCall: ToolCall;\n /** The current step number (0-based) */\n readonly stepNumber: number;\n /** Execution time in milliseconds */\n readonly durationMs: number;\n /** Whether the tool call succeeded */\n readonly success: true;\n /** The tool result */\n readonly output: unknown;\n readonly error?: never;\n }\n | {\n /** The tool call that was executed */\n readonly toolCall: ToolCall;\n /** The current step number (0-based) */\n readonly stepNumber: number;\n /** Execution time in milliseconds */\n readonly durationMs: number;\n /** Whether the tool call succeeded */\n readonly success: false;\n /** The error that occurred */\n readonly error: unknown;\n readonly output?: never;\n },\n) => PromiseLike<void> | void;\n\n/**\n * Options for the {@link WorkflowAgent.stream} method.\n */\nexport type WorkflowAgentStreamOptions<\n TTools extends ToolSet = ToolSet,\n OUTPUT = never,\n PARTIAL_OUTPUT = never,\n> = Partial<GenerationSettings> &\n (\n | {\n /**\n * A prompt. It can be either a text prompt or a list of messages.\n *\n * You can either use `prompt` or `messages` but not both.\n */\n prompt: string | Array<ModelMessage>;\n\n /**\n * A list of messages.\n *\n * You can either use `prompt` or `messages` but not both.\n */\n messages?: never;\n }\n | {\n /**\n * The conversation messages to process. Should follow the AI SDK's ModelMessage format.\n *\n * You can either use `prompt` or `messages` but not both.\n */\n messages: Array<ModelMessage>;\n\n /**\n * A prompt. It can be either a text prompt or a list of messages.\n *\n * You can either use `prompt` or `messages` but not both.\n */\n prompt?: never;\n }\n ) & {\n /**\n * Optional system prompt override. If provided, overrides the system prompt from the constructor.\n */\n system?: string;\n\n /**\n * A WritableStream that receives raw LanguageModelV4StreamPart chunks in real-time\n * as the model generates them. This enables streaming to the client without\n * coupling WorkflowAgent to UIMessageChunk format.\n *\n * Convert to UIMessageChunks at the response boundary using\n * `createUIMessageChunkTransform()` from `@ai-sdk/workflow`.\n *\n * @example\n * ```typescript\n * // In the workflow:\n * await agent.stream({\n * messages,\n * writable: getWritable<ModelCallStreamPart>(),\n * });\n *\n * // In the route handler:\n * return createUIMessageStreamResponse({\n * stream: run.readable.pipeThrough(createModelCallToUIChunkTransform()),\n * });\n * ```\n */\n writable?: WritableStream<ModelCallStreamPart<ToolSet>>;\n\n /**\n * Condition for stopping the generation when there are tool results in the last step.\n * When the condition is an array, any of the conditions can be met to stop the generation.\n */\n stopWhen?:\n | StopCondition<NoInfer<ToolSet>, any>\n | Array<StopCondition<NoInfer<ToolSet>, any>>;\n\n /**\n * The tool choice strategy. Default: 'auto'.\n * Overrides the toolChoice from the constructor if provided.\n */\n toolChoice?: ToolChoice<TTools>;\n\n /**\n * Limits the tools that are available for the model to call without\n * changing the tool call and result types in the result.\n */\n activeTools?: Array<keyof NoInfer<TTools>>;\n\n /**\n * Optional telemetry configuration (experimental).\n */\n experimental_telemetry?: TelemetryOptions;\n\n /**\n * Context that is passed into tool execution.\n * Experimental (can break in patch releases).\n * @default undefined\n */\n experimental_context?: unknown;\n\n /**\n * Optional specification for parsing structured outputs from the LLM response.\n * Use `Output.object({ schema })` for structured output or `Output.text()` for text output.\n *\n * @example\n * ```typescript\n * import { Output } from '@workflow/ai';\n * import { z } from 'zod';\n *\n * const result = await agent.stream({\n * messages: [...],\n * writable: getWritable(),\n * output: Output.object({\n * schema: z.object({\n * sentiment: z.enum(['positive', 'negative', 'neutral']),\n * confidence: z.number(),\n * }),\n * }),\n * });\n *\n * console.log(result.output); // { sentiment: 'positive', confidence: 0.95 }\n * ```\n */\n output?: OutputSpecification<OUTPUT, PARTIAL_OUTPUT>;\n\n /**\n * Whether to include raw chunks from the provider in the stream.\n * When enabled, you will receive raw chunks with type 'raw' that contain the unprocessed data from the provider.\n * This allows access to cutting-edge provider features not yet wrapped by the AI SDK.\n * Defaults to false.\n */\n includeRawChunks?: boolean;\n\n /**\n * A function that attempts to repair a tool call that failed to parse.\n */\n experimental_repairToolCall?: ToolCallRepairFunction<TTools>;\n\n /**\n * Optional stream transformations.\n * They are applied in the order they are provided.\n * The stream transformations must maintain the stream structure for streamText to work correctly.\n */\n experimental_transform?:\n | StreamTextTransform<TTools>\n | Array<StreamTextTransform<TTools>>;\n\n /**\n * Custom download function to use for URLs.\n * By default, files are downloaded if the model does not support the URL for the given media type.\n */\n experimental_download?: DownloadFunction;\n\n /**\n * Callback function to be called after each step completes.\n */\n onStepFinish?: WorkflowAgentOnStepFinishCallback<TTools>;\n\n /**\n * Callback that is invoked when an error occurs during streaming.\n * You can use it to log errors.\n */\n onError?: WorkflowAgentOnErrorCallback;\n\n /**\n * Callback that is called when the LLM response and all request tool executions\n * (for tools that have an `execute` function) are finished.\n */\n onFinish?: WorkflowAgentOnFinishCallback<TTools, OUTPUT>;\n\n /**\n * Callback that is called when the operation is aborted.\n */\n onAbort?: WorkflowAgentOnAbortCallback<TTools>;\n\n /**\n * Callback called when the agent starts streaming, before any LLM calls.\n */\n experimental_onStart?: WorkflowAgentOnStartCallback;\n\n /**\n * Callback called before each step (LLM call) begins.\n */\n experimental_onStepStart?: WorkflowAgentOnStepStartCallback;\n\n /**\n * Callback called before a tool's execute function runs.\n */\n experimental_onToolCallStart?: WorkflowAgentOnToolCallStartCallback;\n\n /**\n * Callback called after a tool execution completes.\n */\n experimental_onToolCallFinish?: WorkflowAgentOnToolCallFinishCallback;\n\n /**\n * Callback function called before each step in the agent loop.\n * Use this to modify settings, manage context, or inject messages dynamically.\n *\n * @example\n * ```typescript\n * prepareStep: async ({ messages, stepNumber }) => {\n * // Inject messages from a queue\n * const queuedMessages = await getQueuedMessages();\n * if (queuedMessages.length > 0) {\n * return {\n * messages: [...messages, ...queuedMessages],\n * };\n * }\n * return {};\n * }\n * ```\n */\n prepareStep?: PrepareStepCallback<TTools>;\n\n /**\n * Timeout in milliseconds for the stream operation.\n * When specified, creates an AbortSignal that will abort the operation after the given time.\n * If both `timeout` and `abortSignal` are provided, whichever triggers first will abort.\n */\n timeout?: number;\n\n /**\n * Whether to send a 'finish' chunk to the writable stream when streaming completes.\n * @default true\n */\n sendFinish?: boolean;\n\n /**\n * Whether to prevent the writable stream from being closed after streaming completes.\n * @default false\n */\n preventClose?: boolean;\n };\n\n/**\n * A tool call made by the model. Matches the AI SDK's tool call shape.\n */\nexport interface ToolCall {\n /** Discriminator for content part arrays */\n type: 'tool-call';\n /** The unique identifier of the tool call */\n toolCallId: string;\n /** The name of the tool that was called */\n toolName: string;\n /** The parsed input arguments for the tool call */\n input: unknown;\n}\n\n/**\n * A tool result from executing a tool. Matches the AI SDK's tool result shape.\n */\nexport interface ToolResult {\n /** Discriminator for content part arrays */\n type: 'tool-result';\n /** The tool call ID this result corresponds to */\n toolCallId: string;\n /** The name of the tool that was executed */\n toolName: string;\n /** The parsed input arguments that were passed to the tool */\n input: unknown;\n /** The output produced by the tool */\n output: unknown;\n}\n\n/**\n * Result of the WorkflowAgent.stream method.\n */\nexport interface WorkflowAgentStreamResult<\n TTools extends ToolSet = ToolSet,\n OUTPUT = never,\n> {\n /**\n * The final messages including all tool calls and results.\n */\n messages: ModelMessage[];\n\n /**\n * Details for all steps.\n */\n steps: StepResult<TTools, any>[];\n\n /**\n * The tool calls from the last step.\n * Includes all tool calls regardless of whether they were executed.\n *\n * When the agent stops because a tool without an `execute` function was called,\n * this array will contain those calls. Compare with `toolResults` to find\n * unresolved tool calls that need client-side handling:\n *\n * ```ts\n * const unresolved = result.toolCalls.filter(\n * tc => !result.toolResults.some(tr => tr.toolCallId === tc.toolCallId)\n * );\n * ```\n *\n * This matches the AI SDK's `GenerateTextResult.toolCalls` behavior.\n */\n toolCalls: ToolCall[];\n\n /**\n * The tool results from the last step.\n * Only includes results for tools that were actually executed (server-side or provider-executed).\n * Tools without an `execute` function will NOT have entries here.\n *\n * This matches the AI SDK's `GenerateTextResult.toolResults` behavior.\n */\n toolResults: ToolResult[];\n\n /**\n * The generated structured output. It uses the `output` specification.\n * Only available when `output` is specified.\n */\n output: OUTPUT;\n}\n\n/**\n * A class for building durable AI agents within workflows.\n *\n * WorkflowAgent enables you to create AI-powered agents that can maintain state\n * across workflow steps, call tools, and gracefully handle interruptions and resumptions.\n * It integrates seamlessly with the AI SDK and the Workflow DevKit for\n * production-grade reliability.\n *\n * @example\n * ```typescript\n * const agent = new WorkflowAgent({\n * model: 'anthropic/claude-opus',\n * tools: {\n * getWeather: {\n * description: 'Get weather for a location',\n * inputSchema: z.object({ location: z.string() }),\n * execute: getWeatherStep,\n * },\n * },\n * instructions: 'You are a helpful weather assistant.',\n * });\n *\n * const result = await agent.stream({\n * messages: [{ role: 'user', content: 'What is the weather?' }],\n * });\n * ```\n */\nexport class WorkflowAgent<TBaseTools extends ToolSet = ToolSet> {\n /**\n * The id of the agent.\n */\n public readonly id: string | undefined;\n\n private model: LanguageModel;\n /**\n * The tool set configured for this agent.\n */\n public readonly tools: TBaseTools;\n private instructions?:\n | string\n | SystemModelMessage\n | Array<SystemModelMessage>;\n private generationSettings: GenerationSettings;\n private toolChoice?: ToolChoice<TBaseTools>;\n private telemetry?: TelemetryOptions;\n private experimentalContext: unknown;\n private stopWhen?:\n | StopCondition<ToolSet, any>\n | Array<StopCondition<ToolSet, any>>;\n private activeTools?: Array<keyof TBaseTools>;\n private output?: OutputSpecification<any, any>;\n private experimentalRepairToolCall?: ToolCallRepairFunction<TBaseTools>;\n private experimentalDownload?: DownloadFunction;\n private prepareStep?: PrepareStepCallback<TBaseTools>;\n private constructorOnStepFinish?: WorkflowAgentOnStepFinishCallback<ToolSet>;\n private constructorOnFinish?: WorkflowAgentOnFinishCallback<ToolSet>;\n private constructorOnStart?: WorkflowAgentOnStartCallback;\n private constructorOnStepStart?: WorkflowAgentOnStepStartCallback;\n private constructorOnToolCallStart?: WorkflowAgentOnToolCallStartCallback;\n private constructorOnToolCallFinish?: WorkflowAgentOnToolCallFinishCallback;\n private prepareCall?: PrepareCallCallback<TBaseTools>;\n\n constructor(options: WorkflowAgentOptions<TBaseTools>) {\n this.id = options.id;\n this.model = options.model;\n this.tools = (options.tools ?? {}) as TBaseTools;\n // `instructions` takes precedence over deprecated `system`\n this.instructions = options.instructions ?? options.system;\n this.toolChoice = options.toolChoice;\n this.telemetry = options.experimental_telemetry;\n this.experimentalContext = options.experimental_context;\n this.stopWhen = options.stopWhen;\n this.activeTools = options.activeTools;\n this.output = options.output;\n this.experimentalRepairToolCall = options.experimental_repairToolCall;\n this.experimentalDownload = options.experimental_download;\n this.prepareStep = options.prepareStep;\n this.constructorOnStepFinish = options.onStepFinish;\n this.constructorOnFinish = options.onFinish;\n this.constructorOnStart = options.experimental_onStart;\n this.constructorOnStepStart = options.experimental_onStepStart;\n this.constructorOnToolCallStart = options.experimental_onToolCallStart;\n this.constructorOnToolCallFinish = options.experimental_onToolCallFinish;\n this.prepareCall = options.prepareCall;\n\n // Extract generation settings\n this.generationSettings = {\n maxOutputTokens: options.maxOutputTokens,\n temperature: options.temperature,\n topP: options.topP,\n topK: options.topK,\n presencePenalty: options.presencePenalty,\n frequencyPenalty: options.frequencyPenalty,\n stopSequences: options.stopSequences,\n seed: options.seed,\n maxRetries: options.maxRetries,\n abortSignal: options.abortSignal,\n headers: options.headers,\n providerOptions: options.providerOptions,\n };\n }\n\n generate() {\n throw new Error('Not implemented');\n }\n\n async stream<\n TTools extends TBaseTools = TBaseTools,\n OUTPUT = never,\n PARTIAL_OUTPUT = never,\n >(\n options: WorkflowAgentStreamOptions<TTools, OUTPUT, PARTIAL_OUTPUT>,\n ): Promise<WorkflowAgentStreamResult<TTools, OUTPUT>> {\n // Call prepareCall to transform parameters before the agent loop\n let effectiveModel: LanguageModel = this.model;\n let effectiveInstructions = options.system ?? this.instructions;\n let effectivePrompt: string | Array<ModelMessage> | undefined =\n options.prompt;\n let effectiveMessages: Array<ModelMessage> | undefined = options.messages;\n let effectiveGenerationSettings = { ...this.generationSettings };\n let effectiveExperimentalContext =\n options.experimental_context ?? this.experimentalContext;\n let effectiveToolChoiceFromPrepare = options.toolChoice ?? this.toolChoice;\n let effectiveTelemetryFromPrepare =\n options.experimental_telemetry ?? this.telemetry;\n\n // Resolve messages for prepareCall: use messages directly, or convert prompt\n const resolvedMessagesForPrepareCall: ModelMessage[] =\n effectiveMessages ??\n (typeof effectivePrompt === 'string'\n ? [{ role: 'user' as const, content: effectivePrompt }]\n : (effectivePrompt as ModelMessage[])) ??\n [];\n\n if (this.prepareCall) {\n const prepared = await this.prepareCall({\n model: effectiveModel,\n tools: this.tools,\n instructions: effectiveInstructions,\n toolChoice: effectiveToolChoiceFromPrepare as ToolChoice<TBaseTools>,\n experimental_telemetry: effectiveTelemetryFromPrepare,\n experimental_context: effectiveExperimentalContext,\n messages: resolvedMessagesForPrepareCall,\n ...effectiveGenerationSettings,\n } as PrepareCallOptions<TBaseTools>);\n\n if (prepared.model !== undefined) effectiveModel = prepared.model;\n if (prepared.instructions !== undefined)\n effectiveInstructions = prepared.instructions;\n if (prepared.messages !== undefined) {\n effectiveMessages = prepared.messages as Array<ModelMessage>;\n effectivePrompt = undefined; // messages from prepareCall take precedence\n }\n if (prepared.experimental_context !== undefined)\n effectiveExperimentalContext = prepared.experimental_context;\n if (prepared.toolChoice !== undefined)\n effectiveToolChoiceFromPrepare =\n prepared.toolChoice as ToolChoice<TBaseTools>;\n if (prepared.experimental_telemetry !== undefined)\n effectiveTelemetryFromPrepare = prepared.experimental_telemetry;\n if (prepared.maxOutputTokens !== undefined)\n effectiveGenerationSettings.maxOutputTokens = prepared.maxOutputTokens;\n if (prepared.temperature !== undefined)\n effectiveGenerationSettings.temperature = prepared.temperature;\n if (prepared.topP !== undefined)\n effectiveGenerationSettings.topP = prepared.topP;\n if (prepared.topK !== undefined)\n effectiveGenerationSettings.topK = prepared.topK;\n if (prepared.presencePenalty !== undefined)\n effectiveGenerationSettings.presencePenalty = prepared.presencePenalty;\n if (prepared.frequencyPenalty !== undefined)\n effectiveGenerationSettings.frequencyPenalty =\n prepared.frequencyPenalty;\n if (prepared.stopSequences !== undefined)\n effectiveGenerationSettings.stopSequences = prepared.stopSequences;\n if (prepared.seed !== undefined)\n effectiveGenerationSettings.seed = prepared.seed;\n if (prepared.headers !== undefined)\n effectiveGenerationSettings.headers = prepared.headers;\n if (prepared.providerOptions !== undefined)\n effectiveGenerationSettings.providerOptions = prepared.providerOptions;\n }\n\n const prompt = await standardizePrompt({\n system: effectiveInstructions,\n ...(effectivePrompt != null\n ? { prompt: effectivePrompt }\n : { messages: effectiveMessages! }),\n });\n\n // Process tool approval responses before starting the agent loop.\n // This mirrors how stream-text.ts handles tool-approval-response parts:\n // approved tools are executed, denied tools get denial results, and\n // approval parts are stripped from the messages.\n const { approvedToolApprovals, deniedToolApprovals } =\n collectToolApprovalsFromMessages(prompt.messages);\n\n if (approvedToolApprovals.length > 0 || deniedToolApprovals.length > 0) {\n const _toolResultMessages: ModelMessage[] = [];\n const toolResultContent: Array<{\n type: 'tool-result';\n toolCallId: string;\n toolName: string;\n output:\n | { type: 'text'; value: string }\n | { type: 'json'; value: JSONValue }\n | { type: 'execution-denied'; reason: string | undefined };\n }> = [];\n\n // Execute approved tools\n for (const approval of approvedToolApprovals) {\n const tool = (this.tools as ToolSet)[approval.toolName];\n if (tool && typeof tool.execute === 'function') {\n try {\n const { execute } = tool;\n const toolResult = await execute(approval.input, {\n toolCallId: approval.toolCallId,\n messages: [],\n context: effectiveExperimentalContext,\n });\n toolResultContent.push({\n type: 'tool-result' as const,\n toolCallId: approval.toolCallId,\n toolName: approval.toolName,\n output:\n typeof toolResult === 'string'\n ? { type: 'text' as const, value: toolResult }\n : { type: 'json' as const, value: toolResult },\n });\n } catch (error) {\n toolResultContent.push({\n type: 'tool-result' as const,\n toolCallId: approval.toolCallId,\n toolName: approval.toolName,\n output: {\n type: 'text' as const,\n value: getErrorMessage(error),\n },\n });\n }\n }\n }\n\n // Create denial results for denied tools\n for (const denial of deniedToolApprovals) {\n toolResultContent.push({\n type: 'tool-result' as const,\n toolCallId: denial.toolCallId,\n toolName: denial.toolName,\n output: {\n type: 'execution-denied' as const,\n reason: denial.reason,\n },\n });\n }\n\n // Strip approval parts from messages and inject tool results\n const cleanedMessages: ModelMessage[] = [];\n for (const msg of prompt.messages) {\n if (msg.role === 'assistant' && Array.isArray(msg.content)) {\n const filtered = (msg.content as any[]).filter(\n (p: any) => p.type !== 'tool-approval-request',\n );\n if (filtered.length > 0) {\n cleanedMessages.push({ ...msg, content: filtered });\n }\n } else if (msg.role === 'tool') {\n const filtered = (msg.content as any[]).filter(\n (p: any) => p.type !== 'tool-approval-response',\n );\n if (filtered.length > 0) {\n cleanedMessages.push({ ...msg, content: filtered });\n }\n } else {\n cleanedMessages.push(msg);\n }\n }\n\n // Add tool results as a new tool message\n if (toolResultContent.length > 0) {\n cleanedMessages.push({\n role: 'tool',\n content: toolResultContent,\n } as ModelMessage);\n }\n\n prompt.messages = cleanedMessages;\n\n // Write tool results and step boundaries to the stream so the UI\n // can transition approved/denied tool parts to the correct state\n // and properly separate them from the subsequent model step.\n if (options.writable && toolResultContent.length > 0) {\n const approvedResults = toolResultContent\n .filter(r => r.output.type !== 'execution-denied')\n .map(r => ({\n toolCallId: r.toolCallId,\n toolName: r.toolName,\n input: approvedToolApprovals.find(\n a => a.toolCallId === r.toolCallId,\n )?.input,\n output: 'value' in r.output ? r.output.value : undefined,\n }));\n const deniedResults = toolResultContent\n .filter(r => r.output.type === 'execution-denied')\n .map(r => ({ toolCallId: r.toolCallId }));\n await writeApprovalToolResults(\n options.writable,\n approvedResults,\n deniedResults,\n );\n }\n }\n\n const modelPrompt = await convertToLanguageModelPrompt({\n prompt,\n supportedUrls: {},\n download: options.experimental_download ?? this.experimentalDownload,\n });\n\n const effectiveAbortSignal = mergeAbortSignals(\n options.abortSignal ?? effectiveGenerationSettings.abortSignal,\n options.timeout != null\n ? AbortSignal.timeout(options.timeout)\n : undefined,\n );\n\n // Merge generation settings: constructor defaults < prepareCall < stream options\n const mergedGenerationSettings: GenerationSettings = {\n ...effectiveGenerationSettings,\n ...(options.maxOutputTokens !== undefined && {\n maxOutputTokens: options.maxOutputTokens,\n }),\n ...(options.temperature !== undefined && {\n temperature: options.temperature,\n }),\n ...(options.topP !== undefined && { topP: options.topP }),\n ...(options.topK !== undefined && { topK: options.topK }),\n ...(options.presencePenalty !== undefined && {\n presencePenalty: options.presencePenalty,\n }),\n ...(options.frequencyPenalty !== undefined && {\n frequencyPenalty: options.frequencyPenalty,\n }),\n ...(options.stopSequences !== undefined && {\n stopSequences: options.stopSequences,\n }),\n ...(options.seed !== undefined && { seed: options.seed }),\n ...(options.maxRetries !== undefined && {\n maxRetries: options.maxRetries,\n }),\n ...(effectiveAbortSignal !== undefined && {\n abortSignal: effectiveAbortSignal,\n }),\n ...(options.headers !== undefined && { headers: options.headers }),\n ...(options.providerOptions !== undefined && {\n providerOptions: options.providerOptions,\n }),\n };\n\n // Merge constructor + stream callbacks (constructor first, then stream)\n const mergedOnStepFinish = mergeCallbacks(\n this.constructorOnStepFinish as\n | WorkflowAgentOnStepFinishCallback<TTools>\n | undefined,\n options.onStepFinish,\n );\n const mergedOnFinish = mergeCallbacks(\n this.constructorOnFinish as\n | WorkflowAgentOnFinishCallback<TTools, OUTPUT>\n | undefined,\n options.onFinish,\n );\n const mergedOnStart = mergeCallbacks(\n this.constructorOnStart,\n options.experimental_onStart,\n );\n const mergedOnStepStart = mergeCallbacks(\n this.constructorOnStepStart,\n options.experimental_onStepStart,\n );\n const mergedOnToolCallStart = mergeCallbacks(\n this.constructorOnToolCallStart,\n options.experimental_onToolCallStart,\n );\n const mergedOnToolCallFinish = mergeCallbacks(\n this.constructorOnToolCallFinish,\n options.experimental_onToolCallFinish,\n );\n\n // Determine effective tool choice\n const effectiveToolChoice = effectiveToolChoiceFromPrepare;\n\n // Merge telemetry settings\n const effectiveTelemetry = effectiveTelemetryFromPrepare;\n\n // Filter tools if activeTools is specified (stream-level overrides constructor default)\n const effectiveActiveTools = options.activeTools ?? this.activeTools;\n const effectiveTools =\n effectiveActiveTools && effectiveActiveTools.length > 0\n ? (filterActiveTools({\n tools: this.tools,\n activeTools: effectiveActiveTools as string[],\n }) ?? this.tools)\n : this.tools;\n\n // Initialize context\n let experimentalContext = effectiveExperimentalContext;\n\n const steps: StepResult<TTools, any>[] = [];\n\n // Track tool calls and results from the last step for the result\n let lastStepToolCalls: ToolCall[] = [];\n let lastStepToolResults: ToolResult[] = [];\n\n // Call onStart before the agent loop\n if (mergedOnStart) {\n await mergedOnStart({\n model: effectiveModel,\n messages: prompt.messages,\n });\n }\n\n // Helper to wrap executeTool with onToolCallStart/onToolCallFinish callbacks\n const executeToolWithCallbacks = async (\n toolCall: { toolCallId: string; toolName: string; input: unknown },\n tools: ToolSet,\n messages: LanguageModelV4Prompt,\n context?: unknown,\n currentStepNumber: number = 0,\n ): Promise<LanguageModelV4ToolResultPart> => {\n const toolCallEvent: ToolCall = {\n type: 'tool-call',\n toolCallId: toolCall.toolCallId,\n toolName: toolCall.toolName,\n input: toolCall.input,\n };\n\n if (mergedOnToolCallStart) {\n await mergedOnToolCallStart({\n toolCall: toolCallEvent,\n stepNumber: currentStepNumber,\n });\n }\n\n const startTime = Date.now();\n let result: LanguageModelV4ToolResultPart;\n try {\n result = await executeTool(toolCall, tools, messages, context);\n } catch (err) {\n const durationMs = Date.now() - startTime;\n if (mergedOnToolCallFinish) {\n await mergedOnToolCallFinish({\n toolCall: toolCallEvent,\n stepNumber: currentStepNumber,\n durationMs,\n success: false,\n error: err,\n });\n }\n throw err;\n }\n\n const durationMs = Date.now() - startTime;\n if (mergedOnToolCallFinish) {\n const isError =\n result.output &&\n 'type' in result.output &&\n (result.output.type === 'error-text' ||\n result.output.type === 'error-json');\n if (isError) {\n await mergedOnToolCallFinish({\n toolCall: toolCallEvent,\n stepNumber: currentStepNumber,\n durationMs,\n success: false,\n error: 'value' in result.output ? result.output.value : undefined,\n });\n } else {\n await mergedOnToolCallFinish({\n toolCall: toolCallEvent,\n stepNumber: currentStepNumber,\n durationMs,\n success: true,\n output:\n result.output && 'value' in result.output\n ? result.output.value\n : undefined,\n });\n }\n }\n return result;\n };\n\n // Check for abort before starting\n if (mergedGenerationSettings.abortSignal?.aborted) {\n if (options.onAbort) {\n await options.onAbort({ steps });\n }\n return {\n messages: prompt.messages,\n steps,\n toolCalls: [],\n toolResults: [],\n output: undefined as OUTPUT,\n };\n }\n\n const iterator = streamTextIterator({\n model: effectiveModel,\n tools: effectiveTools as ToolSet,\n writable: options.writable,\n prompt: modelPrompt,\n stopConditions: options.stopWhen ?? this.stopWhen,\n\n onStepFinish: mergedOnStepFinish,\n onStepStart: mergedOnStepStart,\n onError: options.onError,\n prepareStep:\n options.prepareStep ??\n (this.prepareStep as PrepareStepCallback<ToolSet> | undefined),\n generationSettings: mergedGenerationSettings,\n toolChoice: effectiveToolChoice as ToolChoice<ToolSet>,\n experimental_context: experimentalContext,\n experimental_telemetry: effectiveTelemetry,\n includeRawChunks: options.includeRawChunks ?? false,\n repairToolCall: (options.experimental_repairToolCall ??\n this.experimentalRepairToolCall) as\n | ToolCallRepairFunction<ToolSet>\n | undefined,\n responseFormat: await (options.output ?? this.output)?.responseFormat,\n });\n\n // Track the final conversation messages from the iterator\n let finalMessages: LanguageModelV4Prompt | undefined;\n let encounteredError: unknown;\n let wasAborted = false;\n\n try {\n let result = await iterator.next();\n while (!result.done) {\n // Check for abort during iteration\n if (mergedGenerationSettings.abortSignal?.aborted) {\n wasAborted = true;\n if (options.onAbort) {\n await options.onAbort({ steps });\n }\n break;\n }\n\n const {\n toolCalls,\n messages: iterMessages,\n step,\n context,\n providerExecutedToolResults,\n } = result.value;\n // Capture current step number before pushing (0-based)\n const currentStepNumber = steps.length;\n if (step) {\n steps.push(step as unknown as StepResult<TTools, any>);\n }\n if (context !== undefined) {\n experimentalContext = context;\n }\n\n // Only execute tools if there are tool calls\n if (toolCalls.length > 0) {\n // Separate provider-executed tool calls from client-executed ones\n const nonProviderToolCalls = toolCalls.filter(\n tc => !tc.providerExecuted,\n );\n const providerToolCalls = toolCalls.filter(tc => tc.providerExecuted);\n\n // Check which tools need approval (can be async)\n const approvalNeeded = await Promise.all(\n nonProviderToolCalls.map(async tc => {\n const tool = (effectiveTools as ToolSet)[tc.toolName];\n if (!tool) return false;\n if (tool.needsApproval == null) return false;\n if (typeof tool.needsApproval === 'boolean')\n return tool.needsApproval;\n return tool.needsApproval(tc.input, {\n toolCallId: tc.toolCallId,\n messages: iterMessages as unknown as ModelMessage[],\n context: experimentalContext,\n });\n }),\n );\n\n // Further split non-provider tool calls into:\n // - executable: has execute function and doesn't need approval\n // - paused: no execute function (client-side) OR needs approval\n // Note: missing tools (!tool) are left to executeTool which will throw.\n const executableToolCalls = nonProviderToolCalls.filter((tc, i) => {\n const tool = (effectiveTools as ToolSet)[tc.toolName];\n return (\n (!tool || typeof tool.execute === 'function') &&\n !approvalNeeded[i]\n );\n });\n const pausedToolCalls = nonProviderToolCalls.filter((tc, i) => {\n const tool = (effectiveTools as ToolSet)[tc.toolName];\n return (\n (tool && typeof tool.execute !== 'function') || approvalNeeded[i]\n );\n });\n\n // If there are paused tool calls (client-side or needing approval),\n // stop the loop and return them.\n // This matches AI SDK behavior: tools without execute or needing\n // approval pause the agent loop.\n if (pausedToolCalls.length > 0) {\n // Execute any executable tools that were also called in this step\n const executableResults = await Promise.all(\n executableToolCalls.map(\n (toolCall): Promise<LanguageModelV4ToolResultPart> =>\n executeToolWithCallbacks(\n toolCall,\n effectiveTools as ToolSet,\n iterMessages,\n experimentalContext,\n currentStepNumber,\n ),\n ),\n );\n\n // Collect provider tool results\n const providerResults: LanguageModelV4ToolResultPart[] =\n providerToolCalls.map(toolCall =>\n resolveProviderToolResult(\n toolCall,\n providerExecutedToolResults,\n ),\n );\n\n const resolvedResults = [...executableResults, ...providerResults];\n\n const allToolCalls: ToolCall[] = toolCalls.map(tc => ({\n type: 'tool-call' as const,\n toolCallId: tc.toolCallId,\n toolName: tc.toolName,\n input: tc.input,\n }));\n\n const allToolResults: ToolResult[] = resolvedResults.map(r => ({\n type: 'tool-result' as const,\n toolCallId: r.toolCallId,\n toolName: r.toolName,\n input: toolCalls.find(tc => tc.toolCallId === r.toolCallId)\n ?.input,\n output: 'value' in r.output ? r.output.value : undefined,\n }));\n\n if (resolvedResults.length > 0) {\n iterMessages.push({\n role: 'tool',\n content: resolvedResults,\n });\n }\n\n const messages = iterMessages as unknown as ModelMessage[];\n\n if (mergedOnFinish && !wasAborted) {\n const lastStep = steps[steps.length - 1];\n await mergedOnFinish({\n steps,\n messages,\n text: lastStep?.text ?? '',\n finishReason: lastStep?.finishReason ?? 'other',\n totalUsage: aggregateUsage(steps),\n experimental_context: experimentalContext,\n output: undefined as OUTPUT,\n });\n }\n\n // Emit tool-approval-request chunks for tools that need approval\n // so useChat can show the approval UI\n if (options.writable) {\n const approvalToolCalls = pausedToolCalls.filter((_, i) => {\n const tcIndex = nonProviderToolCalls.indexOf(\n pausedToolCalls[i],\n );\n return approvalNeeded[tcIndex];\n });\n if (approvalToolCalls.length > 0) {\n await writeApprovalRequests(\n options.writable,\n approvalToolCalls.map(tc => ({\n toolCallId: tc.toolCallId,\n toolName: tc.toolName,\n })),\n );\n }\n }\n\n // Close the stream before returning for paused tools\n if (options.writable) {\n const sendFinish = options.sendFinish ?? true;\n const preventClose = options.preventClose ?? false;\n if (sendFinish || !preventClose) {\n await closeStream(options.writable, preventClose, sendFinish);\n }\n }\n\n return {\n messages,\n steps,\n toolCalls: allToolCalls,\n toolResults: allToolResults,\n output: undefined as OUTPUT,\n };\n }\n\n // Execute client tools (all have execute functions at this point)\n const clientToolResults = await Promise.all(\n nonProviderToolCalls.map(\n (toolCall): Promise<LanguageModelV4ToolResultPart> =>\n executeToolWithCallbacks(\n toolCall,\n effectiveTools as ToolSet,\n iterMessages,\n experimentalContext,\n currentStepNumber,\n ),\n ),\n );\n\n // For provider-executed tools, use the results from the stream\n const providerToolResults: LanguageModelV4ToolResultPart[] =\n providerToolCalls.map(toolCall =>\n resolveProviderToolResult(toolCall, providerExecutedToolResults),\n );\n\n // Combine results in the original order\n const toolResults = toolCalls.map(tc => {\n const clientResult = clientToolResults.find(\n r => r.toolCallId === tc.toolCallId,\n );\n if (clientResult) return clientResult;\n const providerResult = providerToolResults.find(\n r => r.toolCallId === tc.toolCallId,\n );\n if (providerResult) return providerResult;\n // This should never happen, but return empty result as fallback\n return {\n type: 'tool-result' as const,\n toolCallId: tc.toolCallId,\n toolName: tc.toolName,\n output: { type: 'text' as const, value: '' },\n };\n });\n\n // Write tool results and step boundaries to the stream so the\n // UI can transition tool parts to output-available state and\n // properly separate multi-step model calls in the message history.\n if (options.writable) {\n await writeToolResultsWithStepBoundary(\n options.writable,\n toolResults.map(r => ({\n toolCallId: r.toolCallId,\n toolName: r.toolName,\n input: toolCalls.find(tc => tc.toolCallId === r.toolCallId)\n ?.input,\n output: 'value' in r.output ? r.output.value : undefined,\n })),\n );\n }\n\n // Track the tool calls and results for this step\n lastStepToolCalls = toolCalls.map(tc => ({\n type: 'tool-call' as const,\n toolCallId: tc.toolCallId,\n toolName: tc.toolName,\n input: tc.input,\n }));\n lastStepToolResults = toolResults.map(r => ({\n type: 'tool-result' as const,\n toolCallId: r.toolCallId,\n toolName: r.toolName,\n input: toolCalls.find(tc => tc.toolCallId === r.toolCallId)?.input,\n output: 'value' in r.output ? r.output.value : undefined,\n }));\n\n result = await iterator.next(toolResults);\n } else {\n // Final step with no tool calls - reset tracking\n lastStepToolCalls = [];\n lastStepToolResults = [];\n result = await iterator.next([]);\n }\n }\n\n // When the iterator completes normally, result.value contains the final conversation prompt\n if (result.done) {\n finalMessages = result.value;\n }\n } catch (error) {\n encounteredError = error;\n // Check if this is an abort error\n if (error instanceof Error && error.name === 'AbortError') {\n wasAborted = true;\n if (options.onAbort) {\n await options.onAbort({ steps });\n }\n } else if (options.onError) {\n // Call onError for non-abort errors (including tool execution errors)\n await options.onError({ error });\n }\n // Don't throw yet - we want to call onFinish first\n }\n\n // Use the final messages from the iterator, or fall back to standardized messages\n const messages = (finalMessages ??\n prompt.messages) as unknown as ModelMessage[];\n\n // Parse structured output if output is specified (stream-level overrides constructor default)\n const effectiveOutput = options.output ?? this.output;\n let experimentalOutput: OUTPUT = undefined as OUTPUT;\n if (effectiveOutput && steps.length > 0) {\n const lastStep = steps[steps.length - 1];\n const text = lastStep.text;\n if (text) {\n try {\n experimentalOutput = await effectiveOutput.parseCompleteOutput(\n { text },\n {\n response: lastStep.response,\n usage: lastStep.usage,\n finishReason: lastStep.finishReason,\n },\n );\n } catch (parseError) {\n // If there's already an error, don't override it\n // If not, set this as the error\n if (!encounteredError) {\n encounteredError = parseError;\n }\n }\n }\n }\n\n // Call onFinish callback if provided (always call, even on errors, but not on abort)\n if (mergedOnFinish && !wasAborted) {\n const lastStep = steps[steps.length - 1];\n await mergedOnFinish({\n steps,\n messages: messages as ModelMessage[],\n text: lastStep?.text ?? '',\n finishReason: lastStep?.finishReason ?? 'other',\n totalUsage: aggregateUsage(steps),\n experimental_context: experimentalContext,\n output: experimentalOutput,\n });\n }\n\n // Re-throw any error that occurred\n if (encounteredError) {\n // Close the stream before throwing\n if (options.writable) {\n const sendFinish = options.sendFinish ?? true;\n const preventClose = options.preventClose ?? false;\n if (sendFinish || !preventClose) {\n await closeStream(options.writable, preventClose, sendFinish);\n }\n }\n throw encounteredError;\n }\n\n // Close the writable stream\n if (options.writable) {\n const sendFinish = options.sendFinish ?? true;\n const preventClose = options.preventClose ?? false;\n if (sendFinish || !preventClose) {\n await closeStream(options.writable, preventClose, sendFinish);\n }\n }\n\n return {\n messages: messages as ModelMessage[],\n steps,\n toolCalls: lastStepToolCalls,\n toolResults: lastStepToolResults,\n output: experimentalOutput,\n };\n }\n}\n\n/**\n * Filter tools to only include the specified active tools.\n */\n/**\n * Aggregate token usage across all steps.\n */\n/**\n * Close the writable stream, optionally sending a finish chunk first.\n * This is a step function because writable.getWriter() and writable.close()\n * cannot be called in workflow context (sandbox limitation).\n */\nasync function closeStream(\n writable: WritableStream<any>,\n preventClose?: boolean,\n sendFinish?: boolean,\n) {\n 'use step';\n if (sendFinish) {\n const writer = writable.getWriter();\n try {\n await writer.write({ type: 'finish' });\n } finally {\n writer.releaseLock();\n }\n }\n if (!preventClose) {\n await writable.close();\n }\n}\n\n/**\n * Write tool-approval-request chunks to the writable stream.\n * These are consumed by useChat to show the approval UI.\n */\nasync function writeApprovalRequests(\n writable: WritableStream<any>,\n toolCalls: Array<{ toolCallId: string; toolName: string }>,\n) {\n 'use step';\n const writer = writable.getWriter();\n try {\n for (const tc of toolCalls) {\n await writer.write({\n type: 'tool-approval-request',\n approvalId: `approval-${tc.toolCallId}`,\n toolCallId: tc.toolCallId,\n });\n }\n } finally {\n writer.releaseLock();\n }\n}\n\nasync function writeToolResultsWithStepBoundary(\n writable: WritableStream<any>,\n results: Array<{\n toolCallId: string;\n toolName: string;\n input: unknown;\n output: unknown;\n }>,\n) {\n 'use step';\n const writer = writable.getWriter();\n try {\n for (const r of results) {\n await writer.write({\n type: 'tool-result',\n toolCallId: r.toolCallId,\n toolName: r.toolName,\n input: r.input,\n output: r.output,\n });\n }\n // Emit step boundaries so the UI message history properly separates\n // the tool call step from the subsequent text step. This ensures\n // convertToModelMessages creates separate assistant messages for\n // tool calls and text responses.\n await writer.write({ type: 'finish-step' });\n await writer.write({ type: 'start-step' });\n } finally {\n writer.releaseLock();\n }\n}\n\nasync function writeApprovalToolResults(\n writable: WritableStream<any>,\n approvedResults: Array<{\n toolCallId: string;\n toolName: string;\n input: unknown;\n output: unknown;\n }>,\n deniedResults: Array<{ toolCallId: string }>,\n) {\n 'use step';\n const writer = writable.getWriter();\n try {\n for (const r of approvedResults) {\n await writer.write({\n type: 'tool-result',\n toolCallId: r.toolCallId,\n toolName: r.toolName,\n input: r.input,\n output: r.output,\n });\n }\n for (const r of deniedResults) {\n await writer.write({\n type: 'tool-output-denied',\n toolCallId: r.toolCallId,\n });\n }\n await writer.write({ type: 'finish-step' });\n await writer.write({ type: 'start-step' });\n } finally {\n writer.releaseLock();\n }\n}\n\nfunction aggregateUsage(steps: StepResult<any, any>[]): LanguageModelUsage {\n let inputTokens = 0;\n let outputTokens = 0;\n for (const step of steps) {\n inputTokens += step.usage?.inputTokens ?? 0;\n outputTokens += step.usage?.outputTokens ?? 0;\n }\n return {\n inputTokens,\n outputTokens,\n totalTokens: inputTokens + outputTokens,\n } as LanguageModelUsage;\n}\n\n// Matches AI SDK's getErrorMessage from @ai-sdk/provider-utils\nfunction getErrorMessage(error: unknown): string {\n if (error == null) {\n return 'unknown error';\n }\n\n if (typeof error === 'string') {\n return error;\n }\n\n if (error instanceof Error) {\n return error.message;\n }\n\n return JSON.stringify(error);\n}\n\nfunction resolveProviderToolResult(\n toolCall: { toolCallId: string; toolName: string },\n providerExecutedToolResults?: Map<\n string,\n { toolCallId: string; toolName: string; result: unknown; isError?: boolean }\n >,\n): LanguageModelV4ToolResultPart {\n const streamResult = providerExecutedToolResults?.get(toolCall.toolCallId);\n if (!streamResult) {\n console.warn(\n `[WorkflowAgent] Provider-executed tool \"${toolCall.toolName}\" (${toolCall.toolCallId}) ` +\n `did not receive a result from the stream. This may indicate a provider issue.`,\n );\n return {\n type: 'tool-result' as const,\n toolCallId: toolCall.toolCallId,\n toolName: toolCall.toolName,\n output: {\n type: 'text' as const,\n value: '',\n },\n };\n }\n\n const result = streamResult.result;\n const isString = typeof result === 'string';\n\n return {\n type: 'tool-result' as const,\n toolCallId: toolCall.toolCallId,\n toolName: toolCall.toolName,\n output: isString\n ? streamResult.isError\n ? { type: 'error-text' as const, value: result }\n : { type: 'text' as const, value: result }\n : streamResult.isError\n ? {\n type: 'error-json' as const,\n value: result as JSONValue,\n }\n : {\n type: 'json' as const,\n value: result as JSONValue,\n },\n };\n}\n\nasync function executeTool(\n toolCall: { toolCallId: string; toolName: string; input: unknown },\n tools: ToolSet,\n messages: LanguageModelV4Prompt,\n experimentalContext?: unknown,\n): Promise<LanguageModelV4ToolResultPart> {\n const tool = tools[toolCall.toolName];\n if (!tool) throw new Error(`Tool \"${toolCall.toolName}\" not found`);\n if (typeof tool.execute !== 'function') {\n throw new Error(\n `Tool \"${toolCall.toolName}\" does not have an execute function. ` +\n `Client-side tools should be filtered before calling executeTool.`,\n );\n }\n // Input is already parsed and validated by streamModelCall's parseToolCall\n const parsedInput = toolCall.input;\n\n try {\n // Extract execute function to avoid binding `this` to the tool object.\n // If we called `tool.execute(...)` directly, JavaScript would bind `this`\n // to `tool`, which contains non-serializable properties like `inputSchema`.\n // When the execute function is a workflow step (marked with 'use step'),\n // the step system captures `this` for serialization, causing failures.\n const { execute } = tool;\n const toolResult = await execute(parsedInput, {\n toolCallId: toolCall.toolCallId,\n // Pass the conversation messages to the tool so it has context about the conversation\n messages,\n // Pass context to the tool\n context: experimentalContext,\n });\n\n // Use the appropriate output type based on the result\n // AI SDK supports 'text' for strings and 'json' for objects\n const output =\n typeof toolResult === 'string'\n ? { type: 'text' as const, value: toolResult }\n : { type: 'json' as const, value: toolResult };\n\n return {\n type: 'tool-result' as const,\n toolCallId: toolCall.toolCallId,\n toolName: toolCall.toolName,\n output,\n };\n } catch (error) {\n // Convert tool errors to error-text results sent back to the model,\n // allowing the agent to recover rather than killing the entire stream.\n // This aligns with AI SDK's streamText behavior for individual tool failures.\n return {\n type: 'tool-result',\n toolCallId: toolCall.toolCallId,\n toolName: toolCall.toolName,\n output: {\n type: 'error-text',\n value: getErrorMessage(error),\n },\n };\n }\n}\n\n/**\n * Collected tool approval information for a single tool call.\n */\ninterface CollectedApproval {\n toolCallId: string;\n toolName: string;\n input: unknown;\n approvalId: string;\n reason?: string;\n}\n\n/**\n * Collect tool approval responses from model messages.\n * Mirrors the logic from `collectToolApprovals` in the AI SDK core\n * (`packages/ai/src/generate-text/collect-tool-approvals.ts`).\n *\n * Scans the last tool message for `tool-approval-response` parts,\n * matches them with `tool-approval-request` parts in assistant messages\n * and the corresponding `tool-call` parts.\n */\nfunction collectToolApprovalsFromMessages(messages: ModelMessage[]): {\n approvedToolApprovals: CollectedApproval[];\n deniedToolApprovals: CollectedApproval[];\n} {\n const lastMessage = messages.at(-1);\n\n if (lastMessage?.role !== 'tool') {\n return { approvedToolApprovals: [], deniedToolApprovals: [] };\n }\n\n // Gather tool calls from assistant messages\n const toolCallsByToolCallId: Record<\n string,\n { toolName: string; input: unknown }\n > = {};\n for (const message of messages) {\n if (message.role === 'assistant' && Array.isArray(message.content)) {\n for (const part of message.content as any[]) {\n if (part.type === 'tool-call') {\n toolCallsByToolCallId[part.toolCallId] = {\n toolName: part.toolName,\n input: part.input ?? part.args,\n };\n }\n }\n }\n }\n\n // Gather approval requests from assistant messages\n const approvalRequestsByApprovalId: Record<\n string,\n { approvalId: string; toolCallId: string }\n > = {};\n for (const message of messages) {\n if (message.role === 'assistant' && Array.isArray(message.content)) {\n for (const part of message.content as any[]) {\n if (part.type === 'tool-approval-request') {\n approvalRequestsByApprovalId[part.approvalId] = {\n approvalId: part.approvalId,\n toolCallId: part.toolCallId,\n };\n }\n }\n }\n }\n\n // Gather existing tool results to avoid re-executing\n const existingToolResults = new Set<string>();\n for (const part of lastMessage.content as any[]) {\n if (part.type === 'tool-result') {\n existingToolResults.add(part.toolCallId);\n }\n }\n\n const approvedToolApprovals: CollectedApproval[] = [];\n const deniedToolApprovals: CollectedApproval[] = [];\n\n // Collect approval responses from the last tool message\n const approvalResponses = (lastMessage.content as any[]).filter(\n (part: any) => part.type === 'tool-approval-response',\n );\n\n for (const response of approvalResponses) {\n const approvalRequest = approvalRequestsByApprovalId[response.approvalId];\n if (approvalRequest == null) continue;\n\n // Skip if there's already a tool result for this tool call\n if (existingToolResults.has(approvalRequest.toolCallId)) continue;\n\n const toolCall = toolCallsByToolCallId[approvalRequest.toolCallId];\n if (toolCall == null) continue;\n\n const approval: CollectedApproval = {\n toolCallId: approvalRequest.toolCallId,\n toolName: toolCall.toolName,\n input: toolCall.input,\n approvalId: response.approvalId,\n reason: response.reason,\n };\n\n if (response.approved) {\n approvedToolApprovals.push(approval);\n } else {\n deniedToolApprovals.push(approval);\n }\n }\n\n return { approvedToolApprovals, deniedToolApprovals };\n}\n","import type {\n LanguageModelV4CallOptions,\n LanguageModelV4Prompt,\n LanguageModelV4ToolResultPart,\n} from '@ai-sdk/provider';\nimport {\n type Experimental_LanguageModelStreamPart as ModelCallStreamPart,\n type LanguageModel,\n type ModelMessage,\n type StepResult,\n type ToolCallRepairFunction,\n type ToolChoice,\n type ToolSet,\n experimental_filterActiveTools as filterActiveTools,\n} from 'ai';\nimport {\n doStreamStep,\n type ModelStopCondition,\n type ParsedToolCall,\n type ProviderExecutedToolResult,\n} from './do-stream-step.js';\nimport { serializeToolSet } from './serializable-schema.js';\nimport type {\n GenerationSettings,\n PrepareStepCallback,\n WorkflowAgentOnErrorCallback,\n WorkflowAgentOnStepFinishCallback,\n TelemetryOptions,\n WorkflowAgentOnStepStartCallback,\n} from './workflow-agent.js';\n\n// Re-export for consumers\nexport type { ProviderExecutedToolResult } from './do-stream-step.js';\n\n/**\n * The value yielded by the stream text iterator when tool calls are requested.\n * Contains both the tool calls and the current conversation messages.\n */\nexport interface StreamTextIteratorYieldValue {\n /** The tool calls requested by the model (parsed with typed inputs) */\n toolCalls: ParsedToolCall[];\n /** The conversation messages up to (and including) the tool call request */\n messages: LanguageModelV4Prompt;\n /** The step result from the current step */\n step?: StepResult<ToolSet, any>;\n /** The current experimental context */\n context?: unknown;\n /** Provider-executed tool results (keyed by tool call ID) */\n providerExecutedToolResults?: Map<string, ProviderExecutedToolResult>;\n}\n\n// This runs in the workflow context\nexport async function* streamTextIterator({\n prompt,\n tools = {},\n writable,\n model,\n stopConditions,\n onStepFinish,\n onStepStart,\n onError,\n prepareStep,\n generationSettings,\n toolChoice,\n experimental_context,\n experimental_telemetry,\n includeRawChunks = false,\n repairToolCall,\n responseFormat,\n}: {\n prompt: LanguageModelV4Prompt;\n tools: ToolSet;\n writable?: WritableStream<ModelCallStreamPart<ToolSet>>;\n model: LanguageModel;\n stopConditions?: ModelStopCondition[] | ModelStopCondition;\n onStepFinish?: WorkflowAgentOnStepFinishCallback<any>;\n onStepStart?: WorkflowAgentOnStepStartCallback;\n onError?: WorkflowAgentOnErrorCallback;\n prepareStep?: PrepareStepCallback<any>;\n generationSettings?: GenerationSettings;\n toolChoice?: ToolChoice<ToolSet>;\n experimental_context?: unknown;\n experimental_telemetry?: TelemetryOptions;\n includeRawChunks?: boolean;\n repairToolCall?: ToolCallRepairFunction<ToolSet>;\n responseFormat?: LanguageModelV4CallOptions['responseFormat'];\n}): AsyncGenerator<\n StreamTextIteratorYieldValue,\n LanguageModelV4Prompt,\n LanguageModelV4ToolResultPart[]\n> {\n let conversationPrompt = [...prompt]; // Create a mutable copy\n let currentModel: LanguageModel = model;\n let currentGenerationSettings = generationSettings ?? {};\n let currentToolChoice = toolChoice;\n let currentContext = experimental_context;\n let currentActiveTools: string[] | undefined;\n\n const steps: StepResult<any, any>[] = [];\n let done = false;\n let _isFirstIteration = true;\n let stepNumber = 0;\n let lastStep: StepResult<any, any> | undefined;\n let lastStepWasToolCalls = false;\n\n while (!done) {\n // Check for abort signal\n if (currentGenerationSettings.abortSignal?.aborted) {\n break;\n }\n\n // Call prepareStep callback before each step if provided\n if (prepareStep) {\n const prepareResult = await prepareStep({\n model: currentModel,\n stepNumber,\n steps,\n messages: conversationPrompt,\n experimental_context: currentContext,\n });\n\n // Apply any overrides from prepareStep\n if (prepareResult.model !== undefined) {\n currentModel = prepareResult.model;\n }\n // Apply messages override BEFORE system so the system message\n // isn't lost when messages replaces the prompt.\n if (prepareResult.messages !== undefined) {\n conversationPrompt = [...prepareResult.messages];\n }\n if (prepareResult.system !== undefined) {\n // Update or prepend system message in the conversation prompt.\n // Applied AFTER messages override so the system message isn't\n // lost when messages replaces the prompt.\n if (\n conversationPrompt.length > 0 &&\n conversationPrompt[0].role === 'system'\n ) {\n // Replace existing system message\n conversationPrompt[0] = {\n role: 'system',\n content: prepareResult.system,\n };\n } else {\n // Prepend new system message\n conversationPrompt.unshift({\n role: 'system',\n content: prepareResult.system,\n });\n }\n }\n if (prepareResult.experimental_context !== undefined) {\n currentContext = prepareResult.experimental_context;\n }\n if (prepareResult.activeTools !== undefined) {\n currentActiveTools = prepareResult.activeTools;\n }\n // Apply generation settings overrides\n if (prepareResult.maxOutputTokens !== undefined) {\n currentGenerationSettings = {\n ...currentGenerationSettings,\n maxOutputTokens: prepareResult.maxOutputTokens,\n };\n }\n if (prepareResult.temperature !== undefined) {\n currentGenerationSettings = {\n ...currentGenerationSettings,\n temperature: prepareResult.temperature,\n };\n }\n if (prepareResult.topP !== undefined) {\n currentGenerationSettings = {\n ...currentGenerationSettings,\n topP: prepareResult.topP,\n };\n }\n if (prepareResult.topK !== undefined) {\n currentGenerationSettings = {\n ...currentGenerationSettings,\n topK: prepareResult.topK,\n };\n }\n if (prepareResult.presencePenalty !== undefined) {\n currentGenerationSettings = {\n ...currentGenerationSettings,\n presencePenalty: prepareResult.presencePenalty,\n };\n }\n if (prepareResult.frequencyPenalty !== undefined) {\n currentGenerationSettings = {\n ...currentGenerationSettings,\n frequencyPenalty: prepareResult.frequencyPenalty,\n };\n }\n if (prepareResult.stopSequences !== undefined) {\n currentGenerationSettings = {\n ...currentGenerationSettings,\n stopSequences: prepareResult.stopSequences,\n };\n }\n if (prepareResult.seed !== undefined) {\n currentGenerationSettings = {\n ...currentGenerationSettings,\n seed: prepareResult.seed,\n };\n }\n if (prepareResult.maxRetries !== undefined) {\n currentGenerationSettings = {\n ...currentGenerationSettings,\n maxRetries: prepareResult.maxRetries,\n };\n }\n if (prepareResult.headers !== undefined) {\n currentGenerationSettings = {\n ...currentGenerationSettings,\n headers: prepareResult.headers,\n };\n }\n if (prepareResult.providerOptions !== undefined) {\n currentGenerationSettings = {\n ...currentGenerationSettings,\n providerOptions: prepareResult.providerOptions,\n };\n }\n if (prepareResult.toolChoice !== undefined) {\n currentToolChoice = prepareResult.toolChoice;\n }\n }\n\n if (onStepStart) {\n await onStepStart({\n stepNumber,\n model: currentModel,\n messages: conversationPrompt as unknown as ModelMessage[],\n steps: [...steps],\n });\n }\n\n try {\n // Filter tools if activeTools is specified\n const effectiveTools =\n currentActiveTools && currentActiveTools.length > 0\n ? (filterActiveTools({\n tools,\n activeTools: currentActiveTools,\n }) ?? tools)\n : tools;\n\n // Serialize tools before crossing the step boundary — zod schemas\n // contain functions that can't be serialized by the workflow runtime.\n // Tools are reconstructed with Ajv validation inside doStreamStep.\n const serializedTools = serializeToolSet(effectiveTools);\n\n const { toolCalls, finish, step, providerExecutedToolResults } =\n await doStreamStep(\n conversationPrompt,\n currentModel,\n writable,\n serializedTools,\n {\n ...currentGenerationSettings,\n toolChoice: currentToolChoice,\n includeRawChunks,\n experimental_telemetry,\n repairToolCall,\n responseFormat,\n },\n );\n\n _isFirstIteration = false;\n stepNumber++;\n steps.push(step);\n lastStep = step;\n lastStepWasToolCalls = false;\n\n const finishReason = finish?.finishReason;\n\n if (finishReason === 'tool-calls') {\n lastStepWasToolCalls = true;\n\n // Add assistant message with tool calls to the conversation\n // Note: providerMetadata from the tool call is mapped to providerOptions\n // in the prompt format, following the AI SDK convention. This is critical\n // for providers like Gemini that require thoughtSignature to be preserved\n // across multi-turn tool calls. Some fields are sanitized before mapping.\n conversationPrompt.push({\n role: 'assistant',\n content: toolCalls.map(toolCall => {\n const sanitizedMetadata = sanitizeProviderMetadataForToolCall(\n toolCall.providerMetadata,\n );\n return {\n type: 'tool-call',\n toolCallId: toolCall.toolCallId,\n toolName: toolCall.toolName,\n input: toolCall.input,\n ...(sanitizedMetadata != null\n ? { providerOptions: sanitizedMetadata }\n : {}),\n };\n }) as typeof toolCalls,\n });\n\n // Yield the tool calls along with the current conversation messages\n // This allows executeTool to pass the conversation context to tool execute functions\n // Also include provider-executed tool results so they can be used instead of local execution\n const toolResults = yield {\n toolCalls,\n messages: conversationPrompt,\n step,\n context: currentContext,\n providerExecutedToolResults,\n };\n\n conversationPrompt.push({\n role: 'tool',\n content: toolResults,\n });\n\n if (stopConditions) {\n const stopConditionList = Array.isArray(stopConditions)\n ? stopConditions\n : [stopConditions];\n if (stopConditionList.some(test => test({ steps }))) {\n done = true;\n }\n }\n } else if (finishReason === 'stop') {\n // Add assistant message with text content to the conversation\n const textContent = step.content.filter(\n item => item.type === 'text',\n ) as Array<{ type: 'text'; text: string }>;\n\n if (textContent.length > 0) {\n conversationPrompt.push({\n role: 'assistant',\n content: textContent,\n });\n }\n\n done = true;\n } else if (finishReason === 'length') {\n // Model hit max tokens - stop but don't throw\n done = true;\n } else if (finishReason === 'content-filter') {\n // Content filter triggered - stop but don't throw\n done = true;\n } else if (finishReason === 'error') {\n // Model error - stop but don't throw\n done = true;\n } else if (finishReason === 'other') {\n // Other reason - stop but don't throw\n done = true;\n } else if (finishReason === 'unknown') {\n // Unknown reason - stop but don't throw\n done = true;\n } else if (!finishReason) {\n // No finish reason - this might happen on incomplete streams\n done = true;\n } else {\n throw new Error(\n `Unexpected finish reason: ${typeof finish?.finishReason === 'object' ? JSON.stringify(finish?.finishReason) : finish?.finishReason}`,\n );\n }\n\n if (onStepFinish) {\n await onStepFinish(step);\n }\n } catch (error) {\n if (onError) {\n await onError({ error });\n }\n throw error;\n }\n }\n\n // Yield the final step if it wasn't already yielded (tool-calls steps are yielded inside the loop)\n if (lastStep && !lastStepWasToolCalls) {\n yield {\n toolCalls: [],\n messages: conversationPrompt,\n step: lastStep,\n context: currentContext,\n };\n }\n\n return conversationPrompt;\n}\n\n/**\n * Strip OpenAI's itemId from providerMetadata (requires reasoning items we don't preserve).\n * Preserves all other provider metadata (e.g., Gemini's thoughtSignature).\n */\nfunction sanitizeProviderMetadataForToolCall(\n metadata: unknown,\n): Record<string, unknown> | undefined {\n if (metadata == null) return undefined;\n\n const meta = metadata as Record<string, unknown>;\n\n // Check if OpenAI metadata exists and needs sanitization\n if ('openai' in meta && meta.openai != null) {\n const { openai, ...restProviders } = meta;\n const openaiMeta = openai as Record<string, unknown>;\n\n // Remove itemId from OpenAI metadata - it requires reasoning items we don't preserve\n const { itemId: _itemId, ...restOpenai } = openaiMeta;\n\n // Reconstruct metadata without itemId\n const hasOtherOpenaiFields = Object.keys(restOpenai).length > 0;\n const hasOtherProviders = Object.keys(restProviders).length > 0;\n\n if (hasOtherOpenaiFields && hasOtherProviders) {\n return { ...restProviders, openai: restOpenai };\n } else if (hasOtherOpenaiFields) {\n return { openai: restOpenai };\n } else if (hasOtherProviders) {\n return restProviders;\n }\n return undefined;\n }\n\n return meta;\n}\n","import type {\n LanguageModelV4CallOptions,\n LanguageModelV4Prompt,\n} from '@ai-sdk/provider';\nimport {\n type Experimental_LanguageModelStreamPart as ModelCallStreamPart,\n experimental_streamLanguageModelCall as streamModelCall,\n type FinishReason,\n type LanguageModel,\n type LanguageModelUsage,\n type ModelMessage,\n type StepResult,\n type StopCondition,\n type ToolCallRepairFunction,\n type ToolChoice,\n type ToolSet,\n} from 'ai';\nimport { gateway } from 'ai';\nimport type { ProviderOptions, TelemetryOptions } from './workflow-agent.js';\nimport {\n resolveSerializableTools,\n type SerializableToolDef,\n} from './serializable-schema.js';\n\nexport type { Experimental_LanguageModelStreamPart as ModelCallStreamPart } from 'ai';\n\nexport type ModelStopCondition = StopCondition<NoInfer<ToolSet>, any>;\n\n/**\n * Provider-executed tool result captured from the stream.\n */\nexport interface ProviderExecutedToolResult {\n toolCallId: string;\n toolName: string;\n result: unknown;\n isError?: boolean;\n}\n\n/**\n * Options for the doStreamStep function.\n */\nexport interface DoStreamStepOptions {\n maxOutputTokens?: number;\n temperature?: number;\n topP?: number;\n topK?: number;\n presencePenalty?: number;\n frequencyPenalty?: number;\n stopSequences?: string[];\n seed?: number;\n maxRetries?: number;\n abortSignal?: AbortSignal;\n headers?: Record<string, string | undefined>;\n providerOptions?: ProviderOptions;\n toolChoice?: ToolChoice<ToolSet>;\n includeRawChunks?: boolean;\n experimental_telemetry?: TelemetryOptions;\n repairToolCall?: ToolCallRepairFunction<ToolSet>;\n responseFormat?: LanguageModelV4CallOptions['responseFormat'];\n}\n\n/**\n * Parsed tool call from the stream (parsed by streamModelCall's transform).\n */\nexport interface ParsedToolCall {\n type: 'tool-call';\n toolCallId: string;\n toolName: string;\n input: unknown;\n providerExecuted?: boolean;\n providerMetadata?: Record<string, unknown>;\n dynamic?: boolean;\n invalid?: boolean;\n error?: unknown;\n}\n\n/**\n * Finish metadata from the stream.\n */\nexport interface StreamFinish {\n finishReason: FinishReason;\n rawFinishReason: string | undefined;\n usage: LanguageModelUsage;\n providerMetadata?: Record<string, unknown>;\n}\n\nexport async function doStreamStep(\n conversationPrompt: LanguageModelV4Prompt,\n modelInit: LanguageModel,\n writable?: WritableStream<ModelCallStreamPart<ToolSet>>,\n serializedTools?: Record<string, SerializableToolDef>,\n options?: DoStreamStepOptions,\n) {\n 'use step';\n\n // Resolve model inside step (must happen here for serialization boundary)\n const model: LanguageModel =\n typeof modelInit === 'string'\n ? gateway.languageModel(modelInit)\n : modelInit;\n\n // Reconstruct tools from serializable definitions with Ajv validation.\n // Tools are serialized before crossing the step boundary because zod schemas\n // contain functions that can't be serialized by the workflow runtime.\n const tools = serializedTools\n ? resolveSerializableTools(serializedTools)\n : undefined;\n\n // streamModelCall handles: prompt standardization, tool preparation,\n // model.doStream(), retry logic, and stream part transformation\n // (tool call parsing, finish reason mapping, file wrapping).\n const { stream: modelStream } = await streamModelCall({\n model,\n // streamModelCall expects Prompt (ModelMessage[]) but we pass the\n // pre-converted LanguageModelV4Prompt. standardizePrompt inside\n // streamModelCall handles both formats.\n messages: conversationPrompt as unknown as ModelMessage[],\n tools,\n toolChoice: options?.toolChoice,\n includeRawChunks: options?.includeRawChunks,\n providerOptions: options?.providerOptions,\n abortSignal: options?.abortSignal,\n headers: options?.headers,\n maxOutputTokens: options?.maxOutputTokens,\n temperature: options?.temperature,\n topP: options?.topP,\n topK: options?.topK,\n presencePenalty: options?.presencePenalty,\n frequencyPenalty: options?.frequencyPenalty,\n stopSequences: options?.stopSequences,\n seed: options?.seed,\n repairToolCall: options?.repairToolCall,\n });\n\n // Consume the stream: capture data and write to writable in real-time\n const toolCalls: ParsedToolCall[] = [];\n const providerExecutedToolResults = new Map<\n string,\n ProviderExecutedToolResult\n >();\n let finish: StreamFinish | undefined;\n\n // Aggregation for StepResult\n let text = '';\n const reasoningParts: Array<{ text: string }> = [];\n let responseMetadata:\n | { id?: string; timestamp?: Date; modelId?: string }\n | undefined;\n let warnings: unknown[] | undefined;\n\n // Acquire writer once before the loop to avoid per-chunk lock overhead\n const writer = writable?.getWriter();\n\n try {\n for await (const part of modelStream) {\n switch (part.type) {\n case 'text-delta':\n text += part.text;\n break;\n case 'reasoning-delta':\n reasoningParts.push({ text: part.text });\n break;\n case 'tool-call': {\n // parseToolCall adds dynamic/invalid/error at runtime\n const toolCallPart = part as typeof part & Partial<ParsedToolCall>;\n toolCalls.push({\n type: 'tool-call',\n toolCallId: toolCallPart.toolCallId,\n toolName: toolCallPart.toolName,\n input: toolCallPart.input,\n providerExecuted: toolCallPart.providerExecuted,\n providerMetadata: toolCallPart.providerMetadata as\n | Record<string, unknown>\n | undefined,\n dynamic: toolCallPart.dynamic,\n invalid: toolCallPart.invalid,\n error: toolCallPart.error,\n });\n break;\n }\n case 'tool-result':\n if (part.providerExecuted) {\n providerExecutedToolResults.set(part.toolCallId, {\n toolCallId: part.toolCallId,\n toolName: part.toolName,\n result: part.output,\n isError: false,\n });\n }\n break;\n case 'tool-error': {\n const errorPart = part as typeof part & {\n providerExecuted?: boolean;\n };\n if (errorPart.providerExecuted) {\n providerExecutedToolResults.set(errorPart.toolCallId, {\n toolCallId: errorPart.toolCallId,\n toolName: errorPart.toolName,\n result: errorPart.error,\n isError: true,\n });\n }\n break;\n }\n case 'model-call-end':\n finish = {\n finishReason: part.finishReason,\n rawFinishReason: part.rawFinishReason,\n usage: part.usage,\n providerMetadata: part.providerMetadata as\n | Record<string, unknown>\n | undefined,\n };\n break;\n case 'model-call-start':\n warnings = part.warnings;\n break;\n case 'model-call-response-metadata':\n responseMetadata = part;\n break;\n }\n\n // Write to writable in real-time\n if (writer) {\n await writer.write(part);\n }\n }\n } finally {\n writer?.releaseLock();\n }\n\n // Build StepResult\n const reasoningText = reasoningParts.map(r => r.text).join('') || undefined;\n\n const step: StepResult<ToolSet, any> = {\n callId: 'workflow-agent',\n stepNumber: 0,\n model: {\n provider: responseMetadata?.modelId?.split(':')[0] ?? 'unknown',\n modelId: responseMetadata?.modelId ?? 'unknown',\n },\n functionId: undefined,\n metadata: undefined,\n runtimeContext: undefined,\n toolsContext: {},\n content: [\n ...(text ? [{ type: 'text' as const, text }] : []),\n ...toolCalls\n .filter(tc => !tc.invalid)\n .map(tc => ({\n type: 'tool-call' as const,\n toolCallId: tc.toolCallId,\n toolName: tc.toolName,\n input: tc.input,\n ...(tc.dynamic ? { dynamic: true as const } : {}),\n })),\n ],\n text,\n reasoning: reasoningParts.map(r => ({\n type: 'reasoning' as const,\n text: r.text,\n })),\n reasoningText,\n files: [],\n sources: [],\n toolCalls: toolCalls\n .filter(tc => !tc.invalid)\n .map(tc => ({\n type: 'tool-call' as const,\n toolCallId: tc.toolCallId,\n toolName: tc.toolName,\n input: tc.input,\n ...(tc.dynamic ? { dynamic: true as const } : {}),\n })),\n staticToolCalls: [],\n dynamicToolCalls: toolCalls\n .filter(tc => !tc.invalid && tc.dynamic)\n .map(tc => ({\n type: 'tool-call' as const,\n toolCallId: tc.toolCallId,\n toolName: tc.toolName,\n input: tc.input,\n dynamic: true as const,\n })),\n toolResults: [],\n staticToolResults: [],\n dynamicToolResults: [],\n finishReason: finish?.finishReason ?? 'other',\n rawFinishReason: finish?.rawFinishReason,\n usage:\n finish?.usage ??\n ({\n inputTokens: 0,\n inputTokenDetails: {\n noCacheTokens: undefined,\n cacheReadTokens: undefined,\n cacheWriteTokens: undefined,\n },\n outputTokens: 0,\n outputTokenDetails: {\n textTokens: undefined,\n reasoningTokens: undefined,\n },\n totalTokens: 0,\n } as LanguageModelUsage),\n warnings,\n request: { body: '' },\n response: {\n id: responseMetadata?.id ?? 'unknown',\n timestamp: responseMetadata?.timestamp ?? new Date(),\n modelId: responseMetadata?.modelId ?? 'unknown',\n messages: [],\n },\n providerMetadata: finish?.providerMetadata ?? {},\n } as StepResult<ToolSet, any>;\n\n return {\n toolCalls,\n finish,\n step,\n providerExecutedToolResults,\n };\n}\n","/**\n * Helpers for passing tool schemas across workflow step boundaries.\n *\n * Tool schemas (zod, valibot, arktype, etc.) contain functions that can't be\n * serialized by the workflow runtime. These helpers extract JSON Schema from\n * schemas, then reconstruct tools with Ajv validation inside step functions.\n *\n * Uses `asSchema()` from `@ai-sdk/provider-utils` for JSON Schema extraction,\n * which supports Standard Schema compatible libraries. When libraries adopt\n * `~standard.jsonSchema` (Standard Schema v2), extraction can be simplified\n * to use that interface directly.\n */\nimport type { JSONSchema7 } from '@ai-sdk/provider';\nimport { asSchema, jsonSchema } from '@ai-sdk/provider-utils';\nimport { tool, type ToolSet } from 'ai';\nimport Ajv from 'ajv';\n\n/**\n * Serializable tool definition — plain objects only, safe for workflow steps.\n */\nexport type SerializableToolDef = {\n description?: string;\n inputSchema: JSONSchema7;\n /** Present on provider tools (e.g. anthropic.tools.webSearch). */\n type?: 'provider';\n /** Provider tool ID, e.g. 'anthropic.web_search_20250305'. */\n id?: `${string}.${string}`;\n /** Provider tool configuration args (maxUses, allowedDomains, etc.). */\n args?: Record<string, unknown>;\n};\n\n/**\n * Converts a ToolSet (with zod/standard schemas and execute functions) to a\n * serializable record of tool definitions. Only description and inputSchema\n * (as JSON Schema) are preserved — execute functions are stripped since they\n * run outside the step.\n */\nexport function serializeToolSet(\n tools: ToolSet,\n): Record<string, SerializableToolDef> {\n return Object.fromEntries(\n Object.entries(tools).map(([name, t]) => {\n const def: SerializableToolDef = {\n description: t.description,\n inputSchema: asSchema(t.inputSchema).jsonSchema as JSONSchema7,\n };\n\n // Preserve provider tool identity so the Gateway can recognize\n // them as provider-executed tools (e.g. anthropic webSearch).\n if ((t as any).type === 'provider') {\n def.type = 'provider';\n def.id = (t as any).id;\n def.args = (t as any).args;\n }\n\n return [name, def];\n }),\n );\n}\n\n/**\n * Reconstructs tool objects from serializable tool definitions inside a step.\n *\n * Wraps each tool's JSON Schema with `jsonSchema()` and validates tool call\n * arguments against the schema using Ajv. This provides runtime type safety\n * equivalent to using zod schemas directly with the AI SDK.\n */\nexport function resolveSerializableTools(\n tools: Record<string, SerializableToolDef>,\n): ToolSet {\n const ajv = new Ajv();\n\n return Object.fromEntries(\n Object.entries(tools).map(([name, t]) => {\n // Provider tools are executed server-side — pass them through\n // with their identity intact, no client-side validation needed.\n if (t.type === 'provider') {\n return [\n name,\n tool({\n type: 'provider' as const,\n id: t.id!,\n args: t.args ?? {},\n inputSchema: jsonSchema(t.inputSchema),\n }),\n ];\n }\n\n const validateFn = ajv.compile(t.inputSchema);\n\n return [\n name,\n tool({\n description: t.description,\n inputSchema: jsonSchema(t.inputSchema, {\n validate: value => {\n if (validateFn(value)) {\n return { success: true, value: value as any };\n }\n return {\n success: false,\n error: new Error(ajv.errorsText(validateFn.errors)),\n };\n },\n }),\n }),\n ];\n }),\n );\n}\n","import { type ToolSet, type UIMessageChunk } from 'ai';\nimport type { Experimental_LanguageModelStreamPart as ModelCallStreamPart } from 'ai';\n\n/**\n * Convert a single ModelCallStreamPart to a UIMessageChunk.\n * Returns undefined for parts that don't map to UI chunks.\n */\nexport function toUIMessageChunk(\n part: ModelCallStreamPart<ToolSet>,\n): UIMessageChunk | undefined {\n switch (part.type) {\n case 'text-start':\n return {\n type: 'text-start',\n id: part.id,\n ...(part.providerMetadata != null\n ? { providerMetadata: part.providerMetadata }\n : {}),\n };\n\n case 'text-delta':\n return {\n type: 'text-delta',\n id: part.id,\n delta: part.text,\n ...(part.providerMetadata != null\n ? { providerMetadata: part.providerMetadata }\n : {}),\n };\n\n case 'text-end':\n return {\n type: 'text-end',\n id: part.id,\n ...(part.providerMetadata != null\n ? { providerMetadata: part.providerMetadata }\n : {}),\n };\n\n case 'reasoning-start':\n return {\n type: 'reasoning-start',\n id: part.id,\n ...(part.providerMetadata != null\n ? { providerMetadata: part.providerMetadata }\n : {}),\n };\n\n case 'reasoning-delta':\n return {\n type: 'reasoning-delta',\n id: part.id,\n delta: part.text,\n ...(part.providerMetadata != null\n ? { providerMetadata: part.providerMetadata }\n : {}),\n };\n\n case 'reasoning-end':\n return {\n type: 'reasoning-end',\n id: part.id,\n ...(part.providerMetadata != null\n ? { providerMetadata: part.providerMetadata }\n : {}),\n };\n\n case 'file': {\n const file = part.file;\n // GeneratedFile.base64 always has data (lazy-converted from Uint8Array if needed)\n return {\n type: 'file',\n mediaType: file.mediaType,\n url: `data:${file.mediaType};base64,${file.base64}`,\n };\n }\n\n case 'source': {\n if (part.sourceType === 'url') {\n return {\n type: 'source-url',\n sourceId: part.id,\n url: part.url,\n title: part.title,\n ...(part.providerMetadata != null\n ? { providerMetadata: part.providerMetadata }\n : {}),\n };\n }\n if (part.sourceType === 'document') {\n return {\n type: 'source-document',\n sourceId: part.id,\n mediaType: part.mediaType,\n title: part.title,\n filename: part.filename,\n ...(part.providerMetadata != null\n ? { providerMetadata: part.providerMetadata }\n : {}),\n };\n }\n return undefined;\n }\n\n case 'tool-input-start':\n return {\n type: 'tool-input-start',\n toolCallId: part.id,\n toolName: part.toolName,\n ...(part.providerExecuted != null\n ? { providerExecuted: part.providerExecuted }\n : {}),\n };\n\n case 'tool-input-delta':\n return {\n type: 'tool-input-delta',\n toolCallId: part.id,\n inputTextDelta: part.delta,\n };\n\n case 'tool-call': {\n // parseToolCall adds invalid/error at runtime for failed parses\n const toolCallPart = part as typeof part & {\n invalid?: boolean;\n error?: unknown;\n };\n if (toolCallPart.invalid) {\n return {\n type: 'tool-input-error',\n toolCallId: toolCallPart.toolCallId,\n toolName: toolCallPart.toolName,\n input: toolCallPart.input,\n errorText:\n toolCallPart.error instanceof Error\n ? toolCallPart.error.message\n : String(toolCallPart.error ?? 'Invalid tool call'),\n };\n }\n return {\n type: 'tool-input-available',\n toolCallId: part.toolCallId,\n toolName: part.toolName,\n input: part.input,\n ...(part.providerExecuted != null\n ? { providerExecuted: part.providerExecuted }\n : {}),\n ...(part.providerMetadata != null\n ? { providerMetadata: part.providerMetadata }\n : {}),\n };\n }\n\n case 'tool-result':\n return {\n type: 'tool-output-available',\n toolCallId: part.toolCallId,\n output: part.output,\n };\n\n case 'tool-error':\n return {\n type: 'tool-output-error',\n toolCallId: part.toolCallId,\n errorText:\n part.error instanceof Error ? part.error.message : String(part.error),\n };\n\n case 'error': {\n const error = part.error;\n return {\n type: 'error',\n errorText: error instanceof Error ? error.message : String(error),\n };\n }\n\n // These don't produce UI chunks\n case 'tool-input-end':\n case 'model-call-start':\n case 'model-call-response-metadata':\n case 'model-call-end':\n case 'raw':\n return undefined;\n\n default: {\n // Pass through tool-approval-request, step boundaries, and other\n // chunks as-is. Step boundaries (finish-step/start-step) are not\n // standard ModelCallStreamPart types but are written by the\n // WorkflowAgent between tool execution and the next model step\n // to ensure proper message splitting in convertToModelMessages.\n const p = part as any;\n if (p.type === 'tool-approval-request') {\n return {\n type: 'tool-approval-request',\n approvalId: p.approvalId,\n toolCallId: p.toolCallId,\n } as UIMessageChunk;\n }\n if (\n p.type === 'finish-step' ||\n p.type === 'start-step' ||\n p.type === 'tool-output-denied'\n ) {\n return p as UIMessageChunk;\n }\n return undefined;\n }\n }\n}\n\n/**\n * Create a TransformStream that converts ModelCallStreamPart to UIMessageChunk.\n * Wraps toUIMessageChunk with start/start-step/finish-step lifecycle chunks.\n */\nexport function createModelCallToUIChunkTransform(): TransformStream<\n ModelCallStreamPart<ToolSet>,\n UIMessageChunk\n> {\n return new TransformStream<ModelCallStreamPart<ToolSet>, UIMessageChunk>({\n start: controller => {\n controller.enqueue({ type: 'start' });\n controller.enqueue({ type: 'start-step' });\n },\n flush: controller => {\n controller.enqueue({ type: 'finish-step' });\n controller.enqueue({ type: 'finish' });\n },\n transform: (part, controller) => {\n const uiChunk = toUIMessageChunk(part);\n if (uiChunk) {\n controller.enqueue(uiChunk);\n }\n },\n });\n}\n","import {\n type ChatRequestOptions,\n type ChatTransport,\n type PrepareReconnectToStreamRequest,\n type PrepareSendMessagesRequest,\n parseJsonEventStream,\n type UIMessage,\n type UIMessageChunk,\n uiMessageChunkSchema,\n} from 'ai';\nimport {\n convertAsyncIteratorToReadableStream,\n getErrorMessage,\n} from '@ai-sdk/provider-utils';\nimport { createAsyncIterableStream } from 'ai/internal';\n\nexport interface SendMessagesOptions<UI_MESSAGE extends UIMessage> {\n trigger: 'submit-message' | 'regenerate-message';\n chatId: string;\n messageId?: string;\n messages: UI_MESSAGE[];\n abortSignal?: AbortSignal;\n}\n\nexport interface ReconnectToStreamOptions {\n chatId: string;\n abortSignal?: AbortSignal;\n /**\n * Override the `startIndex` for this reconnection.\n * Negative values read from the end of the stream.\n * When omitted, falls back to the constructor's `initialStartIndex`.\n */\n startIndex?: number;\n}\n\ntype OnChatSendMessage<UI_MESSAGE extends UIMessage> = (\n response: Response,\n options: SendMessagesOptions<UI_MESSAGE>,\n) => void | Promise<void>;\n\ntype OnChatEnd = ({\n chatId,\n chunkIndex,\n}: {\n chatId: string;\n chunkIndex: number;\n}) => void | Promise<void>;\n\n/**\n * Configuration options for the WorkflowChatTransport.\n *\n * @template UI_MESSAGE - The type of UI messages being sent and received,\n * must extend the UIMessage interface from the AI SDK.\n */\nexport interface WorkflowChatTransportOptions<UI_MESSAGE extends UIMessage> {\n /**\n * API endpoint for chat requests\n * Defaults to /api/chat if not provided\n */\n api?: string;\n\n /**\n * Custom fetch implementation to use for HTTP requests.\n * Defaults to the global fetch function if not provided.\n */\n fetch?: typeof fetch;\n\n /**\n * Callback invoked after successfully sending messages to the chat endpoint.\n * Useful for tracking chat history and inspecting response headers.\n *\n * @param response - The HTTP response object from the chat endpoint\n * @param options - The original options passed to sendMessages\n */\n onChatSendMessage?: OnChatSendMessage<UI_MESSAGE>;\n\n /**\n * Callback invoked when a chat stream ends (receives a \"finish\" chunk).\n * Useful for cleanup operations or state updates.\n *\n * @param chatId - The ID of the chat that ended\n * @param chunkIndex - The total number of chunks received\n */\n onChatEnd?: OnChatEnd;\n\n /**\n * Maximum number of consecutive errors allowed during reconnection attempts.\n * Defaults to 3 if not provided.\n */\n maxConsecutiveErrors?: number;\n\n /**\n * Default `startIndex` to use when reconnecting to a stream without a known\n * chunk position (i.e. the initial reconnection, not a retry).\n * Negative values read from the end of the stream (e.g. `-10` fetches the\n * last 10 chunks), which is useful for resuming a chat UI after a page\n * refresh without replaying the full conversation.\n *\n * Can be overridden per-call via `ReconnectToStreamOptions.startIndex`.\n *\n * Defaults to `0` (replay from the beginning).\n */\n initialStartIndex?: number;\n\n /**\n * Function to prepare the request for sending messages.\n * Allows customizing the API endpoint, headers, credentials, and body.\n */\n prepareSendMessagesRequest?: PrepareSendMessagesRequest<UI_MESSAGE>;\n\n /**\n * Function to prepare the request for reconnecting to a stream.\n * Allows customizing the API endpoint, headers, and credentials.\n */\n prepareReconnectToStreamRequest?: PrepareReconnectToStreamRequest;\n}\n\n/**\n * A transport implementation for managing chat workflows with support for\n * streaming responses and automatic reconnection to interrupted streams.\n *\n * This class implements the ChatTransport interface from the AI SDK and provides\n * reliable message streaming with automatic recovery from network interruptions\n * or function timeouts.\n *\n * @template UI_MESSAGE - The type of UI messages being sent and received,\n * must extend the UIMessage interface from the AI SDK.\n *\n * @implements {ChatTransport<UI_MESSAGE>}\n */\nexport class WorkflowChatTransport<\n UI_MESSAGE extends UIMessage,\n> implements ChatTransport<UI_MESSAGE> {\n private readonly api: string;\n private readonly fetch: typeof fetch;\n private readonly onChatSendMessage?: OnChatSendMessage<UI_MESSAGE>;\n private readonly onChatEnd?: OnChatEnd;\n private readonly maxConsecutiveErrors: number;\n private readonly initialStartIndex: number;\n private readonly prepareSendMessagesRequest?: PrepareSendMessagesRequest<UI_MESSAGE>;\n private readonly prepareReconnectToStreamRequest?: PrepareReconnectToStreamRequest;\n\n /**\n * Creates a new WorkflowChatTransport instance.\n *\n * @param options - Configuration options for the transport\n * @param options.api - API endpoint for chat requests (defaults to '/api/chat')\n * @param options.fetch - Custom fetch implementation (defaults to global fetch)\n * @param options.onChatSendMessage - Callback after sending messages\n * @param options.onChatEnd - Callback when chat stream ends\n * @param options.maxConsecutiveErrors - Maximum consecutive errors for reconnection\n * @param options.prepareSendMessagesRequest - Function to prepare send messages request\n * @param options.prepareReconnectToStreamRequest - Function to prepare reconnect request\n */\n constructor(options: WorkflowChatTransportOptions<UI_MESSAGE> = {}) {\n this.api = options.api ?? '/api/chat';\n this.fetch = options.fetch ?? fetch.bind(globalThis);\n this.onChatSendMessage = options.onChatSendMessage;\n this.onChatEnd = options.onChatEnd;\n this.maxConsecutiveErrors = options.maxConsecutiveErrors ?? 3;\n this.initialStartIndex = options.initialStartIndex ?? 0;\n this.prepareSendMessagesRequest = options.prepareSendMessagesRequest;\n this.prepareReconnectToStreamRequest =\n options.prepareReconnectToStreamRequest;\n }\n\n /**\n * Sends messages to the chat endpoint and returns a stream of response chunks.\n *\n * This method handles the entire chat lifecycle including:\n * - Sending messages to the /api/chat endpoint\n * - Streaming response chunks\n * - Automatic reconnection if the stream is interrupted\n *\n * @param options - Options for sending messages\n * @param options.trigger - The type of message submission ('submit-message' or 'regenerate-message')\n * @param options.chatId - Unique identifier for this chat session\n * @param options.messageId - Optional message ID for tracking specific messages\n * @param options.messages - Array of UI messages to send\n * @param options.abortSignal - Optional AbortSignal to cancel the request\n *\n * @returns A ReadableStream of UIMessageChunk objects containing the response\n * @throws Error if the fetch request fails or returns a non-OK status\n */\n async sendMessages(\n options: SendMessagesOptions<UI_MESSAGE> & ChatRequestOptions,\n ): Promise<ReadableStream<UIMessageChunk>> {\n return convertAsyncIteratorToReadableStream(\n this.sendMessagesIterator(options),\n );\n }\n\n private async *sendMessagesIterator(\n options: SendMessagesOptions<UI_MESSAGE> & ChatRequestOptions,\n ): AsyncGenerator<UIMessageChunk> {\n const { chatId, messages, abortSignal, trigger, messageId } = options;\n\n // We keep track of if the \"finish\" chunk is received to determine\n // if we need to reconnect, and keep track of the chunk index to resume from.\n let gotFinish = false;\n let chunkIndex = 0;\n\n // Prepare the request using the configurator if provided\n const requestConfig = this.prepareSendMessagesRequest\n ? await this.prepareSendMessagesRequest({\n id: chatId,\n messages,\n requestMetadata: options.metadata,\n body: options.body,\n credentials: undefined,\n headers: options.headers,\n api: this.api,\n trigger,\n messageId,\n })\n : undefined;\n\n const url = requestConfig?.api ?? this.api;\n const res = await this.fetch(url, {\n method: 'POST',\n body: JSON.stringify(\n requestConfig?.body ?? { messages, ...options.body },\n ),\n headers: requestConfig?.headers,\n credentials: requestConfig?.credentials,\n signal: abortSignal,\n });\n\n if (!res.ok || !res.body) {\n throw new Error(\n `Failed to fetch chat: ${res.status} ${await res.text()}`,\n );\n }\n\n const workflowRunId = res.headers.get('x-workflow-run-id');\n if (!workflowRunId) {\n throw new Error(\n 'Workflow run ID not found in \"x-workflow-run-id\" response header',\n );\n }\n\n // Notify the caller that the chat POST request was sent.\n // This is useful for tracking the chat history on the client\n // side and allows for inspecting response headers.\n await this.onChatSendMessage?.(res, options);\n\n // Flush the initial stream until the end or an error occurs\n try {\n const chunkStream = parseJsonEventStream({\n stream: res.body,\n schema: uiMessageChunkSchema,\n });\n for await (const chunk of createAsyncIterableStream(chunkStream)) {\n if (!chunk.success) {\n throw chunk.error;\n }\n\n chunkIndex++;\n\n yield chunk.value;\n\n if (chunk.value.type === 'finish') {\n gotFinish = true;\n }\n }\n } catch (error) {\n console.error('Error in chat POST stream', error);\n }\n\n if (gotFinish) {\n await this.onFinish(gotFinish, { chatId, chunkIndex });\n } else {\n // If the initial POST request did not include the \"finish\" chunk,\n // we need to reconnect to the stream. This could indicate that a\n // network error occurred or the Vercel Function timed out.\n yield* this.reconnectToStreamIterator(options, workflowRunId, chunkIndex);\n }\n }\n\n /**\n * Reconnects to an existing chat stream that was previously interrupted.\n *\n * This method is useful for resuming a chat session after network issues,\n * page refreshes, or Vercel Function timeouts.\n *\n * @param options - Options for reconnecting to the stream\n * @param options.chatId - The chat ID to reconnect to\n *\n * @returns A ReadableStream of UIMessageChunk objects\n * @throws Error if the reconnection request fails or returns a non-OK status\n */\n async reconnectToStream(\n options: ReconnectToStreamOptions & ChatRequestOptions,\n ): Promise<ReadableStream<UIMessageChunk> | null> {\n const it = this.reconnectToStreamIterator(options);\n return convertAsyncIteratorToReadableStream(it);\n }\n\n private async *reconnectToStreamIterator(\n options: ReconnectToStreamOptions & ChatRequestOptions,\n workflowRunId?: string,\n initialChunkIndex = 0,\n ): AsyncGenerator<UIMessageChunk> {\n let chunkIndex = initialChunkIndex;\n\n // When called from the public reconnectToStream (initialChunkIndex === 0),\n // honour the caller's startIndex (or the constructor default) for the\n // first request. This enables negative values so the client can read only\n // the tail of the stream (e.g. the last 10 chunks) instead of replaying\n // everything. After the first request, fall back to the running chunkIndex\n // so that retries resume from the correct position.\n const explicitStartIndex = options.startIndex ?? this.initialStartIndex;\n let useExplicitStartIndex =\n initialChunkIndex === 0 && explicitStartIndex !== 0;\n\n const defaultApi = `${this.api}/${encodeURIComponent(workflowRunId ?? options.chatId)}/stream`;\n\n // Prepare the request using the configurator if provided\n const requestConfig = this.prepareReconnectToStreamRequest\n ? await this.prepareReconnectToStreamRequest({\n id: options.chatId,\n requestMetadata: options.metadata,\n body: undefined,\n credentials: undefined,\n headers: undefined,\n api: defaultApi,\n })\n : undefined;\n\n const baseUrl = requestConfig?.api ?? defaultApi;\n\n let gotFinish = false;\n let consecutiveErrors = 0;\n // When a negative startIndex is used but the tail-index header is absent,\n // retries fall back to startIndex 0 (replay everything) instead of using\n // the incremental chunkIndex which would be wrong.\n let replayFromStart = false;\n\n while (!gotFinish) {\n const startIndex = useExplicitStartIndex\n ? explicitStartIndex\n : replayFromStart\n ? 0\n : chunkIndex;\n\n const url = `${baseUrl}?startIndex=${startIndex}`;\n const res = await this.fetch(url, {\n headers: requestConfig?.headers,\n credentials: requestConfig?.credentials,\n signal: options.abortSignal,\n });\n\n if (!res.ok || !res.body) {\n throw new Error(\n `Failed to fetch chat: ${res.status} ${await res.text()}`,\n );\n }\n\n // When using a negative startIndex, the server resolves it to an\n // absolute position. The reconnection endpoint should return the tail\n // index so we can compute the resolved position for subsequent retries.\n if (useExplicitStartIndex && explicitStartIndex > 0) {\n // Positive startIndex: the first request starts at this absolute\n // position, so set chunkIndex to match so subsequent retries\n // resume from (explicitStartIndex + chunks received).\n chunkIndex = explicitStartIndex;\n } else if (useExplicitStartIndex && explicitStartIndex < 0) {\n const tailIndexHeader = res.headers.get('x-workflow-stream-tail-index');\n const tailIndex =\n tailIndexHeader !== null ? parseInt(tailIndexHeader, 10) : NaN;\n\n if (!Number.isNaN(tailIndex)) {\n // Resolve: e.g. tailIndex=499, startIndex=-20 → 500 + (-20) = 480\n chunkIndex = Math.max(0, tailIndex + 1 + explicitStartIndex);\n } else {\n // Header missing or unparseable — fall back to replaying from the\n // beginning so retries don't resume from a wrong position.\n console.warn(\n '[WorkflowChatTransport] Negative initialStartIndex is configured ' +\n `(${explicitStartIndex}) but the reconnection endpoint did not ` +\n 'return a valid \"x-workflow-stream-tail-index\" header. Retries ' +\n 'will replay the stream from the beginning. See: ' +\n 'https://workflow.dev/docs/ai/resumable-streams#resuming-from-the-end-of-the-stream',\n );\n replayFromStart = true;\n }\n }\n useExplicitStartIndex = false;\n\n try {\n const chunkStream = parseJsonEventStream({\n stream: res.body,\n schema: uiMessageChunkSchema,\n });\n for await (const chunk of createAsyncIterableStream(chunkStream)) {\n if (!chunk.success) {\n throw chunk.error;\n }\n\n chunkIndex++;\n\n yield chunk.value;\n\n if (chunk.value.type === 'finish') {\n gotFinish = true;\n }\n }\n // Reset consecutive error count only after successful stream parsing\n consecutiveErrors = 0;\n } catch (error) {\n console.error('Error in chat GET reconnectToStream', error);\n consecutiveErrors++;\n\n if (consecutiveErrors >= this.maxConsecutiveErrors) {\n throw new Error(\n `Failed to reconnect after ${this.maxConsecutiveErrors} consecutive errors. Last error: ${getErrorMessage(error)}`,\n );\n }\n }\n }\n\n await this.onFinish(gotFinish, { chatId: options.chatId, chunkIndex });\n }\n\n private async onFinish(\n gotFinish: boolean,\n { chatId, chunkIndex }: { chatId: string; chunkIndex: number },\n ) {\n if (gotFinish) {\n await this.onChatEnd?.({ chatId, chunkIndex });\n } else {\n throw new Error('No finish chunk received');\n }\n }\n}\n"],"mappings":";AAQA;AAAA,EAME;AAAA,EAUA,kCAAkCA;AAAA,OAC7B;AACP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;;;AC1BP;AAAA,EAQE,kCAAkC;AAAA,OAC7B;;;ACVP;AAAA,EAEE,wCAAwC;AAAA,OAUnC;AACP,SAAS,eAAe;;;ACJxB,SAAS,UAAU,kBAAkB;AACrC,SAAS,YAA0B;AACnC,OAAO,SAAS;AAsBT,SAAS,iBACd,OACqC;AACrC,SAAO,OAAO;AAAA,IACZ,OAAO,QAAQ,KAAK,EAAE,IAAI,CAAC,CAAC,MAAM,CAAC,MAAM;AACvC,YAAM,MAA2B;AAAA,QAC/B,aAAa,EAAE;AAAA,QACf,aAAa,SAAS,EAAE,WAAW,EAAE;AAAA,MACvC;AAIA,UAAK,EAAU,SAAS,YAAY;AAClC,YAAI,OAAO;AACX,YAAI,KAAM,EAAU;AACpB,YAAI,OAAQ,EAAU;AAAA,MACxB;AAEA,aAAO,CAAC,MAAM,GAAG;AAAA,IACnB,CAAC;AAAA,EACH;AACF;AASO,SAAS,yBACd,OACS;AACT,QAAM,MAAM,IAAI,IAAI;AAEpB,SAAO,OAAO;AAAA,IACZ,OAAO,QAAQ,KAAK,EAAE,IAAI,C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Y;AAAA,UACd;AAAA,QACF;AAEA,4BAAoB;AAAA,MACtB,SAAS,OAAO;AACd,gBAAQ,MAAM,uCAAuC,KAAK;AAC1D;AAEA,YAAI,qBAAqB,KAAK,sBAAsB;AAClD,gBAAM,IAAI;AAAA,YACR,6BAA6B,KAAK,oBAAoB,oCAAoCA,iBAAgB,KAAK,CAAC;AAAA,UAClH;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAEA,UAAM,KAAK,SAAS,WAAW,EAAE,QAAQ,QAAQ,QAAQ,WAAW,CAAC;AAAA,EACvE;AAAA,EAEA,MAAc,SACZ,WACA,EAAE,QAAQ,WAAW,GACrB;AA3aJ;AA4aI,QAAI,WAAW;AACb,cAAM,UAAK,cAAL,8BAAiB,EAAE,QAAQ,WAAW;AAAA,IAC9C,OAAO;AACL,YAAM,IAAI,MAAM,0BAA0B;AAAA,IAC5C;AAAA,EACF;AACF;","names":["filterActiveTools","tool","_a","filterActiveTools","messages","durationMs","getErrorMessage"]}
1
+ {"version":3,"sources":["../src/workflow-agent.ts","../src/stream-text-iterator.ts","../src/do-stream-step.ts","../src/serializable-schema.ts","../src/to-ui-message-chunk.ts","../src/workflow-chat-transport.ts"],"sourcesContent":["import type {\n JSONValue,\n LanguageModelV4CallOptions,\n LanguageModelV4Prompt,\n LanguageModelV4StreamPart,\n LanguageModelV4ToolResultPart,\n SharedV4ProviderOptions,\n} from '@ai-sdk/provider';\nimport {\n type FinishReason,\n LanguageModel,\n type LanguageModelResponseMetadata,\n type LanguageModelUsage,\n type Experimental_LanguageModelStreamPart as ModelCallStreamPart,\n type ModelMessage,\n Output,\n type StepResult,\n type StopCondition,\n type StreamTextOnStepFinishCallback,\n type SystemModelMessage,\n type ToolCallRepairFunction,\n type ToolChoice,\n type ToolSet,\n type UIMessage,\n experimental_filterActiveTools as filterActiveTools,\n} from 'ai';\nimport {\n convertToLanguageModelPrompt,\n mergeAbortSignals,\n mergeCallbacks,\n standardizePrompt,\n} from 'ai/internal';\nimport { streamTextIterator } from './stream-text-iterator.js';\n\n// Re-export for consumers\nexport type { CompatibleLanguageModel } from './types.js';\n\n/**\n * Callback function to be called after each step completes.\n * Alias for the AI SDK's StreamTextOnStepFinishCallback, using\n * WorkflowAgent-consistent naming.\n */\nexport type WorkflowAgentOnStepFinishCallback<\n TTools extends ToolSet = ToolSet,\n> = StreamTextOnStepFinishCallback<TTools, any>;\n\n/**\n * Infer the type of the tools of a workflow agent.\n */\nexport type InferWorkflowAgentTools<WORKFLOW_AGENT> =\n WORKFLOW_AGENT extends WorkflowAgent<infer TOOLS> ? TOOLS : never;\n\n/**\n * Infer the UI message type of a workflow agent.\n */\nexport type InferWorkflowAgentUIMessage<\n _WORKFLOW_AGENT,\n MESSAGE_METADATA = unknown,\n> = UIMessage<MESSAGE_METADATA>;\n\n/**\n * Re-export the Output helper for structured output specifications.\n * Use `Output.object({ schema })` for structured output or `Output.text()` for text output.\n */\nexport { Output };\n\n/**\n * Output specification interface for structured outputs.\n * Use `Output.object({ schema })` or `Output.text()` to create an output specification.\n */\nexport interface OutputSpecification<OUTPUT, PARTIAL> {\n readonly name: string;\n responseFormat: PromiseLike<LanguageModelV4CallOptions['responseFormat']>;\n parsePartialOutput(options: {\n text: string;\n }): Promise<{ partial: PARTIAL } | undefined>;\n parseCompleteOutput(\n options: { text: string },\n context: {\n response: LanguageModelResponseMetadata;\n usage: LanguageModelUsage;\n finishReason: FinishReason;\n },\n ): Promise<OUTPUT>;\n}\n\n/**\n * Provider-specific options type. This is equivalent to SharedV4ProviderOptions from @ai-sdk/provider.\n */\nexport type ProviderOptions = SharedV4ProviderOptions;\n\n/**\n * Telemetry settings for observability.\n */\nexport interface TelemetryOptions {\n /**\n * Enable or disable telemetry. Defaults to true.\n */\n isEnabled?: boolean;\n\n /**\n * Identifier for this function. Used to group telemetry data by function.\n */\n functionId?: string;\n\n /**\n * Additional information to include in the telemetry data.\n */\n metadata?: Record<\n string,\n | string\n | number\n | boolean\n | Array<string | number | boolean>\n | null\n | undefined\n >;\n\n /**\n * Enable or disable input recording. Enabled by default.\n *\n * You might want to disable input recording to avoid recording sensitive\n * information, to reduce data transfers, or to increase performance.\n */\n recordInputs?: boolean;\n\n /**\n * Enable or disable output recording. Enabled by default.\n *\n * You might want to disable output recording to avoid recording sensitive\n * information, to reduce data transfers, or to increase performance.\n */\n recordOutputs?: boolean;\n\n /**\n * Custom tracer for the telemetry.\n */\n tracer?: unknown;\n}\n\n/**\n * A transformation that is applied to the stream.\n */\nexport type StreamTextTransform<TTools extends ToolSet> = (options: {\n tools: TTools;\n stopStream: () => void;\n}) => TransformStream<LanguageModelV4StreamPart, LanguageModelV4StreamPart>;\n\n/**\n * Function to repair a tool call that failed to parse.\n * Re-exported from the AI SDK core.\n */\nexport type { ToolCallRepairFunction } from 'ai';\n\n/**\n * Custom download function for URLs.\n * The function receives an array of URLs with information about whether\n * the model supports them directly.\n */\nexport type DownloadFunction = (\n options: {\n url: URL;\n isUrlSupportedByModel: boolean;\n }[],\n) => PromiseLike<\n ({ data: Uint8Array; mediaType: string | undefined } | null)[]\n>;\n\n/**\n * Generation settings that can be passed to the model.\n * These map directly to LanguageModelV4CallOptions.\n */\nexport interface GenerationSettings {\n /**\n * Maximum number of tokens to generate.\n */\n maxOutputTokens?: number;\n\n /**\n * Temperature setting. The range depends on the provider and model.\n * It is recommended to set either `temperature` or `topP`, but not both.\n */\n temperature?: number;\n\n /**\n * Nucleus sampling. This is a number between 0 and 1.\n * E.g. 0.1 would mean that only tokens with the top 10% probability mass are considered.\n * It is recommended to set either `temperature` or `topP`, but not both.\n */\n topP?: number;\n\n /**\n * Only sample from the top K options for each subsequent token.\n * Used to remove \"long tail\" low probability responses.\n * Recommended for advanced use cases only. You usually only need to use temperature.\n */\n topK?: number;\n\n /**\n * Presence penalty setting. It affects the likelihood of the model to\n * repeat information that is already in the prompt.\n * The presence penalty is a number between -1 (increase repetition)\n * and 1 (maximum penalty, decrease repetition). 0 means no penalty.\n */\n presencePenalty?: number;\n\n /**\n * Frequency penalty setting. It affects the likelihood of the model\n * to repeatedly use the same words or phrases.\n * The frequency penalty is a number between -1 (increase repetition)\n * and 1 (maximum penalty, decrease repetition). 0 means no penalty.\n */\n frequencyPenalty?: number;\n\n /**\n * Stop sequences. If set, the model will stop generating text when one of the stop sequences is generated.\n * Providers may have limits on the number of stop sequences.\n */\n stopSequences?: string[];\n\n /**\n * The seed (integer) to use for random sampling. If set and supported\n * by the model, calls will generate deterministic results.\n */\n seed?: number;\n\n /**\n * Maximum number of retries. Set to 0 to disable retries.\n * Note: In workflow context, retries are typically handled by the workflow step mechanism.\n * @default 2\n */\n maxRetries?: number;\n\n /**\n * Abort signal for cancelling the operation.\n */\n abortSignal?: AbortSignal;\n\n /**\n * Additional HTTP headers to be sent with the request.\n * Only applicable for HTTP-based providers.\n */\n headers?: Record<string, string | undefined>;\n\n /**\n * Additional provider-specific options. They are passed through\n * to the provider from the AI SDK and enable provider-specific\n * functionality that can be fully encapsulated in the provider.\n */\n providerOptions?: ProviderOptions;\n}\n\n/**\n * Information passed to the prepareStep callback.\n */\nexport interface PrepareStepInfo<TTools extends ToolSet = ToolSet> {\n /**\n * The current model configuration (string or function).\n * The function should return a LanguageModelV4 instance.\n */\n model: LanguageModel;\n\n /**\n * The current step number (0-indexed).\n */\n stepNumber: number;\n\n /**\n * All previous steps with their results.\n */\n steps: StepResult<TTools, any>[];\n\n /**\n * The messages that will be sent to the model.\n * This is the LanguageModelV4Prompt format used internally.\n */\n messages: LanguageModelV4Prompt;\n\n /**\n * The context passed via the experimental_context setting (experimental).\n */\n experimental_context: unknown;\n}\n\n/**\n * Return type from the prepareStep callback.\n * All properties are optional - only return the ones you want to override.\n */\nexport interface PrepareStepResult extends Partial<GenerationSettings> {\n /**\n * Override the model for this step.\n */\n model?: LanguageModel;\n\n /**\n * Override the system message for this step.\n */\n system?: string;\n\n /**\n * Override the messages for this step.\n * Use this for context management or message injection.\n */\n messages?: LanguageModelV4Prompt;\n\n /**\n * Override the tool choice for this step.\n */\n toolChoice?: ToolChoice<ToolSet>;\n\n /**\n * Override the active tools for this step.\n * Limits the tools that are available for the model to call.\n */\n activeTools?: string[];\n\n /**\n * Context that is passed into tool execution. Experimental.\n * Changing the context will affect the context in this step and all subsequent steps.\n */\n experimental_context?: unknown;\n}\n\n/**\n * Callback function called before each step in the agent loop.\n * Use this to modify settings, manage context, or implement dynamic behavior.\n */\nexport type PrepareStepCallback<TTools extends ToolSet = ToolSet> = (\n info: PrepareStepInfo<TTools>,\n) => PrepareStepResult | Promise<PrepareStepResult>;\n\n/**\n * Options passed to the prepareCall callback.\n */\nexport interface PrepareCallOptions<\n TTools extends ToolSet = ToolSet,\n> extends Partial<GenerationSettings> {\n model: LanguageModel;\n tools: TTools;\n instructions?: string | SystemModelMessage | Array<SystemModelMessage>;\n toolChoice?: ToolChoice<TTools>;\n experimental_telemetry?: TelemetryOptions;\n experimental_context?: unknown;\n messages: ModelMessage[];\n}\n\n/**\n * Result of the prepareCall callback. All fields are optional —\n * only returned fields override the defaults.\n * Note: `tools` cannot be overridden via prepareCall because they are\n * bound at construction time for type safety.\n */\nexport type PrepareCallResult<TTools extends ToolSet = ToolSet> = Partial<\n Omit<PrepareCallOptions<TTools>, 'tools'>\n>;\n\n/**\n * Callback called once before the agent loop starts to transform call parameters.\n */\nexport type PrepareCallCallback<TTools extends ToolSet = ToolSet> = (\n options: PrepareCallOptions<TTools>,\n) => PrepareCallResult<TTools> | Promise<PrepareCallResult<TTools>>;\n\n/**\n * Configuration options for creating a {@link WorkflowAgent} instance.\n */\nexport interface WorkflowAgentOptions<\n TTools extends ToolSet = ToolSet,\n> extends GenerationSettings {\n /**\n * The id of the agent.\n */\n id?: string;\n\n /**\n * The model provider to use for the agent.\n *\n * This should be a string compatible with the Vercel AI Gateway (e.g., 'anthropic/claude-opus'),\n * or a LanguageModelV4 instance from a provider.\n */\n model: LanguageModel;\n\n /**\n * A set of tools available to the agent.\n * Tools can be implemented as workflow steps for automatic retries and persistence,\n * or as regular workflow-level logic using core library features like sleep() and Hooks.\n */\n tools?: TTools;\n\n /**\n * Agent instructions. Can be a string, a SystemModelMessage, or an array of SystemModelMessages.\n * Supports provider-specific options (e.g., caching) when using the SystemModelMessage form.\n */\n instructions?: string | SystemModelMessage | Array<SystemModelMessage>;\n\n /**\n * Optional system prompt to guide the agent's behavior.\n * @deprecated Use `instructions` instead.\n */\n system?: string;\n\n /**\n * The tool choice strategy. Default: 'auto'.\n */\n toolChoice?: ToolChoice<TTools>;\n\n /**\n * Optional telemetry configuration (experimental).\n */\n experimental_telemetry?: TelemetryOptions;\n\n /**\n * Default context that is passed into tool execution for every stream call on this agent.\n *\n * Per-stream `experimental_context` values passed to `stream()` override this default.\n * Experimental (can break in patch releases).\n * @default undefined\n */\n experimental_context?: unknown;\n\n /**\n * Default stop condition for the agent loop. When the condition is an array,\n * any of the conditions can be met to stop the generation.\n *\n * Per-stream `stopWhen` values passed to `stream()` override this default.\n */\n stopWhen?:\n | StopCondition<NoInfer<ToolSet>, any>\n | Array<StopCondition<NoInfer<ToolSet>, any>>;\n\n /**\n * Default set of active tools that limits which tools the model can call,\n * without changing the tool call and result types in the result.\n *\n * Per-stream `activeTools` values passed to `stream()` override this default.\n */\n activeTools?: Array<keyof NoInfer<TTools>>;\n\n /**\n * Default output specification for structured outputs.\n * Use `Output.object({ schema })` for structured output or `Output.text()` for text output.\n *\n * Per-stream `output` values passed to `stream()` override this default.\n */\n output?: OutputSpecification<any, any>;\n\n /**\n * Default function that attempts to repair a tool call that failed to parse.\n *\n * Per-stream `experimental_repairToolCall` values passed to `stream()` override this default.\n */\n experimental_repairToolCall?: ToolCallRepairFunction<TTools>;\n\n /**\n * Default custom download function to use for URLs.\n *\n * Per-stream `experimental_download` values passed to `stream()` override this default.\n */\n experimental_download?: DownloadFunction;\n\n /**\n * Default callback function called before each step in the agent loop.\n * Use this to modify settings, manage context, or inject messages dynamically\n * for every stream call on this agent instance.\n *\n * Per-stream `prepareStep` values passed to `stream()` override this default.\n */\n prepareStep?: PrepareStepCallback<TTools>;\n\n /**\n * Callback function to be called after each step completes.\n */\n onStepFinish?: WorkflowAgentOnStepFinishCallback<ToolSet>;\n\n /**\n * Callback that is called when the LLM response and all request tool executions are finished.\n */\n onFinish?: WorkflowAgentOnFinishCallback<ToolSet>;\n\n /**\n * Callback called when the agent starts streaming, before any LLM calls.\n */\n experimental_onStart?: WorkflowAgentOnStartCallback;\n\n /**\n * Callback called before each step (LLM call) begins.\n */\n experimental_onStepStart?: WorkflowAgentOnStepStartCallback;\n\n /**\n * Callback called before a tool's execute function runs.\n */\n experimental_onToolExecutionStart?: WorkflowAgentOnToolExecutionStartCallback;\n\n /**\n * Callback called after a tool execution completes.\n */\n experimental_onToolExecutionEnd?: WorkflowAgentOnToolExecutionEndCallback;\n\n /**\n * Prepare the parameters for the stream call.\n * Called once before the agent loop starts. Use this to transform\n * model, tools, instructions, or other settings based on runtime context.\n */\n prepareCall?: PrepareCallCallback<TTools>;\n}\n\n/**\n * Callback that is called when the LLM response and all request tool executions are finished.\n */\nexport type WorkflowAgentOnFinishCallback<\n TTools extends ToolSet = ToolSet,\n OUTPUT = never,\n> = (event: {\n /**\n * Details for all steps.\n */\n readonly steps: StepResult<TTools, any>[];\n\n /**\n * The final messages including all tool calls and results.\n */\n readonly messages: ModelMessage[];\n\n /**\n * The text output from the last step.\n */\n readonly text: string;\n\n /**\n * The finish reason from the last step.\n */\n readonly finishReason: FinishReason;\n\n /**\n * The total token usage across all steps.\n */\n readonly totalUsage: LanguageModelUsage;\n\n /**\n * Context that is passed into tool execution.\n */\n readonly experimental_context: unknown;\n\n /**\n * The generated structured output. It uses the `output` specification.\n * Only available when `output` is specified.\n */\n readonly output: OUTPUT;\n}) => PromiseLike<void> | void;\n\n/**\n * Callback that is invoked when an error occurs during streaming.\n */\nexport type WorkflowAgentOnErrorCallback = (event: {\n error: unknown;\n}) => PromiseLike<void> | void;\n\n/**\n * Callback that is set using the `onAbort` option.\n */\nexport type WorkflowAgentOnAbortCallback<TTools extends ToolSet = ToolSet> =\n (event: {\n /**\n * Details for all previously finished steps.\n */\n readonly steps: StepResult<TTools, any>[];\n }) => PromiseLike<void> | void;\n\n/**\n * Callback that is called when the agent starts streaming, before any LLM calls.\n */\nexport type WorkflowAgentOnStartCallback = (event: {\n /** The model being used */\n readonly model: LanguageModel;\n /** The messages being sent */\n readonly messages: ModelMessage[];\n}) => PromiseLike<void> | void;\n\n/**\n * Callback that is called before each step (LLM call) begins.\n */\nexport type WorkflowAgentOnStepStartCallback<TTools extends ToolSet = ToolSet> =\n (event: {\n /** The current step number (0-based) */\n readonly stepNumber: number;\n /** The model being used for this step */\n readonly model: LanguageModel;\n /** The messages being sent for this step */\n readonly messages: ModelMessage[];\n /** Results from all previously finished steps */\n readonly steps: ReadonlyArray<StepResult<TTools, any>>;\n }) => PromiseLike<void> | void;\n\n/**\n * Callback that is called before a tool's execute function runs.\n */\nexport type WorkflowAgentOnToolExecutionStartCallback = (event: {\n /** The tool call being executed */\n readonly toolCall: ToolCall;\n /** The current step number (0-based) */\n readonly stepNumber: number;\n}) => PromiseLike<void> | void;\n\n/**\n * Callback that is called after a tool execution completes.\n * Uses a discriminated union pattern: check `success` to determine\n * whether `output` or `error` is available.\n */\nexport type WorkflowAgentOnToolExecutionEndCallback = (\n event:\n | {\n /** The tool call that was executed */\n readonly toolCall: ToolCall;\n /** The current step number (0-based) */\n readonly stepNumber: number;\n /** Execution time in milliseconds */\n readonly durationMs: number;\n /** Whether the tool call succeeded */\n readonly success: true;\n /** The tool result */\n readonly output: unknown;\n readonly error?: never;\n }\n | {\n /** The tool call that was executed */\n readonly toolCall: ToolCall;\n /** The current step number (0-based) */\n readonly stepNumber: number;\n /** Execution time in milliseconds */\n readonly durationMs: number;\n /** Whether the tool call succeeded */\n readonly success: false;\n /** The error that occurred */\n readonly error: unknown;\n readonly output?: never;\n },\n) => PromiseLike<void> | void;\n\n/**\n * Options for the {@link WorkflowAgent.stream} method.\n */\nexport type WorkflowAgentStreamOptions<\n TTools extends ToolSet = ToolSet,\n OUTPUT = never,\n PARTIAL_OUTPUT = never,\n> = Partial<GenerationSettings> &\n (\n | {\n /**\n * A prompt. It can be either a text prompt or a list of messages.\n *\n * You can either use `prompt` or `messages` but not both.\n */\n prompt: string | Array<ModelMessage>;\n\n /**\n * A list of messages.\n *\n * You can either use `prompt` or `messages` but not both.\n */\n messages?: never;\n }\n | {\n /**\n * The conversation messages to process. Should follow the AI SDK's ModelMessage format.\n *\n * You can either use `prompt` or `messages` but not both.\n */\n messages: Array<ModelMessage>;\n\n /**\n * A prompt. It can be either a text prompt or a list of messages.\n *\n * You can either use `prompt` or `messages` but not both.\n */\n prompt?: never;\n }\n ) & {\n /**\n * Optional system prompt override. If provided, overrides the system prompt from the constructor.\n */\n system?: string;\n\n /**\n * A WritableStream that receives raw LanguageModelV4StreamPart chunks in real-time\n * as the model generates them. This enables streaming to the client without\n * coupling WorkflowAgent to UIMessageChunk format.\n *\n * Convert to UIMessageChunks at the response boundary using\n * `createUIMessageChunkTransform()` from `@ai-sdk/workflow`.\n *\n * @example\n * ```typescript\n * // In the workflow:\n * await agent.stream({\n * messages,\n * writable: getWritable<ModelCallStreamPart>(),\n * });\n *\n * // In the route handler:\n * return createUIMessageStreamResponse({\n * stream: run.readable.pipeThrough(createModelCallToUIChunkTransform()),\n * });\n * ```\n */\n writable?: WritableStream<ModelCallStreamPart<ToolSet>>;\n\n /**\n * Condition for stopping the generation when there are tool results in the last step.\n * When the condition is an array, any of the conditions can be met to stop the generation.\n */\n stopWhen?:\n | StopCondition<NoInfer<ToolSet>, any>\n | Array<StopCondition<NoInfer<ToolSet>, any>>;\n\n /**\n * The tool choice strategy. Default: 'auto'.\n * Overrides the toolChoice from the constructor if provided.\n */\n toolChoice?: ToolChoice<TTools>;\n\n /**\n * Limits the tools that are available for the model to call without\n * changing the tool call and result types in the result.\n */\n activeTools?: Array<keyof NoInfer<TTools>>;\n\n /**\n * Optional telemetry configuration (experimental).\n */\n experimental_telemetry?: TelemetryOptions;\n\n /**\n * Context that is passed into tool execution.\n * Experimental (can break in patch releases).\n * @default undefined\n */\n experimental_context?: unknown;\n\n /**\n * Optional specification for parsing structured outputs from the LLM response.\n * Use `Output.object({ schema })` for structured output or `Output.text()` for text output.\n *\n * @example\n * ```typescript\n * import { Output } from '@workflow/ai';\n * import { z } from 'zod';\n *\n * const result = await agent.stream({\n * messages: [...],\n * writable: getWritable(),\n * output: Output.object({\n * schema: z.object({\n * sentiment: z.enum(['positive', 'negative', 'neutral']),\n * confidence: z.number(),\n * }),\n * }),\n * });\n *\n * console.log(result.output); // { sentiment: 'positive', confidence: 0.95 }\n * ```\n */\n output?: OutputSpecification<OUTPUT, PARTIAL_OUTPUT>;\n\n /**\n * Whether to include raw chunks from the provider in the stream.\n * When enabled, you will receive raw chunks with type 'raw' that contain the unprocessed data from the provider.\n * This allows access to cutting-edge provider features not yet wrapped by the AI SDK.\n * Defaults to false.\n */\n includeRawChunks?: boolean;\n\n /**\n * A function that attempts to repair a tool call that failed to parse.\n */\n experimental_repairToolCall?: ToolCallRepairFunction<TTools>;\n\n /**\n * Optional stream transformations.\n * They are applied in the order they are provided.\n * The stream transformations must maintain the stream structure for streamText to work correctly.\n */\n experimental_transform?:\n | StreamTextTransform<TTools>\n | Array<StreamTextTransform<TTools>>;\n\n /**\n * Custom download function to use for URLs.\n * By default, files are downloaded if the model does not support the URL for the given media type.\n */\n experimental_download?: DownloadFunction;\n\n /**\n * Callback function to be called after each step completes.\n */\n onStepFinish?: WorkflowAgentOnStepFinishCallback<TTools>;\n\n /**\n * Callback that is invoked when an error occurs during streaming.\n * You can use it to log errors.\n */\n onError?: WorkflowAgentOnErrorCallback;\n\n /**\n * Callback that is called when the LLM response and all request tool executions\n * (for tools that have an `execute` function) are finished.\n */\n onFinish?: WorkflowAgentOnFinishCallback<TTools, OUTPUT>;\n\n /**\n * Callback that is called when the operation is aborted.\n */\n onAbort?: WorkflowAgentOnAbortCallback<TTools>;\n\n /**\n * Callback called when the agent starts streaming, before any LLM calls.\n */\n experimental_onStart?: WorkflowAgentOnStartCallback;\n\n /**\n * Callback called before each step (LLM call) begins.\n */\n experimental_onStepStart?: WorkflowAgentOnStepStartCallback;\n\n /**\n * Callback called before a tool's execute function runs.\n */\n experimental_onToolExecutionStart?: WorkflowAgentOnToolExecutionStartCallback;\n\n /**\n * Callback called after a tool execution completes.\n */\n experimental_onToolExecutionEnd?: WorkflowAgentOnToolExecutionEndCallback;\n\n /**\n * Callback function called before each step in the agent loop.\n * Use this to modify settings, manage context, or inject messages dynamically.\n *\n * @example\n * ```typescript\n * prepareStep: async ({ messages, stepNumber }) => {\n * // Inject messages from a queue\n * const queuedMessages = await getQueuedMessages();\n * if (queuedMessages.length > 0) {\n * return {\n * messages: [...messages, ...queuedMessages],\n * };\n * }\n * return {};\n * }\n * ```\n */\n prepareStep?: PrepareStepCallback<TTools>;\n\n /**\n * Timeout in milliseconds for the stream operation.\n * When specified, creates an AbortSignal that will abort the operation after the given time.\n * If both `timeout` and `abortSignal` are provided, whichever triggers first will abort.\n */\n timeout?: number;\n\n /**\n * Whether to send a 'finish' chunk to the writable stream when streaming completes.\n * @default true\n */\n sendFinish?: boolean;\n\n /**\n * Whether to prevent the writable stream from being closed after streaming completes.\n * @default false\n */\n preventClose?: boolean;\n };\n\n/**\n * A tool call made by the model. Matches the AI SDK's tool call shape.\n */\nexport interface ToolCall {\n /** Discriminator for content part arrays */\n type: 'tool-call';\n /** The unique identifier of the tool call */\n toolCallId: string;\n /** The name of the tool that was called */\n toolName: string;\n /** The parsed input arguments for the tool call */\n input: unknown;\n}\n\n/**\n * A tool result from executing a tool. Matches the AI SDK's tool result shape.\n */\nexport interface ToolResult {\n /** Discriminator for content part arrays */\n type: 'tool-result';\n /** The tool call ID this result corresponds to */\n toolCallId: string;\n /** The name of the tool that was executed */\n toolName: string;\n /** The parsed input arguments that were passed to the tool */\n input: unknown;\n /** The output produced by the tool */\n output: unknown;\n}\n\n/**\n * Result of the WorkflowAgent.stream method.\n */\nexport interface WorkflowAgentStreamResult<\n TTools extends ToolSet = ToolSet,\n OUTPUT = never,\n> {\n /**\n * The final messages including all tool calls and results.\n */\n messages: ModelMessage[];\n\n /**\n * Details for all steps.\n */\n steps: StepResult<TTools, any>[];\n\n /**\n * The tool calls from the last step.\n * Includes all tool calls regardless of whether they were executed.\n *\n * When the agent stops because a tool without an `execute` function was called,\n * this array will contain those calls. Compare with `toolResults` to find\n * unresolved tool calls that need client-side handling:\n *\n * ```ts\n * const unresolved = result.toolCalls.filter(\n * tc => !result.toolResults.some(tr => tr.toolCallId === tc.toolCallId)\n * );\n * ```\n *\n * This matches the AI SDK's `GenerateTextResult.toolCalls` behavior.\n */\n toolCalls: ToolCall[];\n\n /**\n * The tool results from the last step.\n * Only includes results for tools that were actually executed (server-side or provider-executed).\n * Tools without an `execute` function will NOT have entries here.\n *\n * This matches the AI SDK's `GenerateTextResult.toolResults` behavior.\n */\n toolResults: ToolResult[];\n\n /**\n * The generated structured output. It uses the `output` specification.\n * Only available when `output` is specified.\n */\n output: OUTPUT;\n}\n\n/**\n * A class for building durable AI agents within workflows.\n *\n * WorkflowAgent enables you to create AI-powered agents that can maintain state\n * across workflow steps, call tools, and gracefully handle interruptions and resumptions.\n * It integrates seamlessly with the AI SDK and the Workflow DevKit for\n * production-grade reliability.\n *\n * @example\n * ```typescript\n * const agent = new WorkflowAgent({\n * model: 'anthropic/claude-opus',\n * tools: {\n * getWeather: {\n * description: 'Get weather for a location',\n * inputSchema: z.object({ location: z.string() }),\n * execute: getWeatherStep,\n * },\n * },\n * instructions: 'You are a helpful weather assistant.',\n * });\n *\n * const result = await agent.stream({\n * messages: [{ role: 'user', content: 'What is the weather?' }],\n * });\n * ```\n */\nexport class WorkflowAgent<TBaseTools extends ToolSet = ToolSet> {\n /**\n * The id of the agent.\n */\n public readonly id: string | undefined;\n\n private model: LanguageModel;\n /**\n * The tool set configured for this agent.\n */\n public readonly tools: TBaseTools;\n private instructions?:\n | string\n | SystemModelMessage\n | Array<SystemModelMessage>;\n private generationSettings: GenerationSettings;\n private toolChoice?: ToolChoice<TBaseTools>;\n private telemetry?: TelemetryOptions;\n private experimentalContext: unknown;\n private stopWhen?:\n | StopCondition<ToolSet, any>\n | Array<StopCondition<ToolSet, any>>;\n private activeTools?: Array<keyof TBaseTools>;\n private output?: OutputSpecification<any, any>;\n private experimentalRepairToolCall?: ToolCallRepairFunction<TBaseTools>;\n private experimentalDownload?: DownloadFunction;\n private prepareStep?: PrepareStepCallback<TBaseTools>;\n private constructorOnStepFinish?: WorkflowAgentOnStepFinishCallback<ToolSet>;\n private constructorOnFinish?: WorkflowAgentOnFinishCallback<ToolSet>;\n private constructorOnStart?: WorkflowAgentOnStartCallback;\n private constructorOnStepStart?: WorkflowAgentOnStepStartCallback;\n private constructorOnToolExecutionStart?: WorkflowAgentOnToolExecutionStartCallback;\n private constructorOnToolExecutionEnd?: WorkflowAgentOnToolExecutionEndCallback;\n private prepareCall?: PrepareCallCallback<TBaseTools>;\n\n constructor(options: WorkflowAgentOptions<TBaseTools>) {\n this.id = options.id;\n this.model = options.model;\n this.tools = (options.tools ?? {}) as TBaseTools;\n // `instructions` takes precedence over deprecated `system`\n this.instructions = options.instructions ?? options.system;\n this.toolChoice = options.toolChoice;\n this.telemetry = options.experimental_telemetry;\n this.experimentalContext = options.experimental_context;\n this.stopWhen = options.stopWhen;\n this.activeTools = options.activeTools;\n this.output = options.output;\n this.experimentalRepairToolCall = options.experimental_repairToolCall;\n this.experimentalDownload = options.experimental_download;\n this.prepareStep = options.prepareStep;\n this.constructorOnStepFinish = options.onStepFinish;\n this.constructorOnFinish = options.onFinish;\n this.constructorOnStart = options.experimental_onStart;\n this.constructorOnStepStart = options.experimental_onStepStart;\n this.constructorOnToolExecutionStart =\n options.experimental_onToolExecutionStart;\n this.constructorOnToolExecutionEnd =\n options.experimental_onToolExecutionEnd;\n this.prepareCall = options.prepareCall;\n\n // Extract generation settings\n this.generationSettings = {\n maxOutputTokens: options.maxOutputTokens,\n temperature: options.temperature,\n topP: options.topP,\n topK: options.topK,\n presencePenalty: options.presencePenalty,\n frequencyPenalty: options.frequencyPenalty,\n stopSequences: options.stopSequences,\n seed: options.seed,\n maxRetries: options.maxRetries,\n abortSignal: options.abortSignal,\n headers: options.headers,\n providerOptions: options.providerOptions,\n };\n }\n\n generate() {\n throw new Error('Not implemented');\n }\n\n async stream<\n TTools extends TBaseTools = TBaseTools,\n OUTPUT = never,\n PARTIAL_OUTPUT = never,\n >(\n options: WorkflowAgentStreamOptions<TTools, OUTPUT, PARTIAL_OUTPUT>,\n ): Promise<WorkflowAgentStreamResult<TTools, OUTPUT>> {\n // Call prepareCall to transform parameters before the agent loop\n let effectiveModel: LanguageModel = this.model;\n let effectiveInstructions = options.system ?? this.instructions;\n let effectivePrompt: string | Array<ModelMessage> | undefined =\n options.prompt;\n let effectiveMessages: Array<ModelMessage> | undefined = options.messages;\n let effectiveGenerationSettings = { ...this.generationSettings };\n let effectiveExperimentalContext =\n options.experimental_context ?? this.experimentalContext;\n let effectiveToolChoiceFromPrepare = options.toolChoice ?? this.toolChoice;\n let effectiveTelemetryFromPrepare =\n options.experimental_telemetry ?? this.telemetry;\n\n // Resolve messages for prepareCall: use messages directly, or convert prompt\n const resolvedMessagesForPrepareCall: ModelMessage[] =\n effectiveMessages ??\n (typeof effectivePrompt === 'string'\n ? [{ role: 'user' as const, content: effectivePrompt }]\n : (effectivePrompt as ModelMessage[])) ??\n [];\n\n if (this.prepareCall) {\n const prepared = await this.prepareCall({\n model: effectiveModel,\n tools: this.tools,\n instructions: effectiveInstructions,\n toolChoice: effectiveToolChoiceFromPrepare as ToolChoice<TBaseTools>,\n experimental_telemetry: effectiveTelemetryFromPrepare,\n experimental_context: effectiveExperimentalContext,\n messages: resolvedMessagesForPrepareCall,\n ...effectiveGenerationSettings,\n } as PrepareCallOptions<TBaseTools>);\n\n if (prepared.model !== undefined) effectiveModel = prepared.model;\n if (prepared.instructions !== undefined)\n effectiveInstructions = prepared.instructions;\n if (prepared.messages !== undefined) {\n effectiveMessages = prepared.messages as Array<ModelMessage>;\n effectivePrompt = undefined; // messages from prepareCall take precedence\n }\n if (prepared.experimental_context !== undefined)\n effectiveExperimentalContext = prepared.experimental_context;\n if (prepared.toolChoice !== undefined)\n effectiveToolChoiceFromPrepare =\n prepared.toolChoice as ToolChoice<TBaseTools>;\n if (prepared.experimental_telemetry !== undefined)\n effectiveTelemetryFromPrepare = prepared.experimental_telemetry;\n if (prepared.maxOutputTokens !== undefined)\n effectiveGenerationSettings.maxOutputTokens = prepared.maxOutputTokens;\n if (prepared.temperature !== undefined)\n effectiveGenerationSettings.temperature = prepared.temperature;\n if (prepared.topP !== undefined)\n effectiveGenerationSettings.topP = prepared.topP;\n if (prepared.topK !== undefined)\n effectiveGenerationSettings.topK = prepared.topK;\n if (prepared.presencePenalty !== undefined)\n effectiveGenerationSettings.presencePenalty = prepared.presencePenalty;\n if (prepared.frequencyPenalty !== undefined)\n effectiveGenerationSettings.frequencyPenalty =\n prepared.frequencyPenalty;\n if (prepared.stopSequences !== undefined)\n effectiveGenerationSettings.stopSequences = prepared.stopSequences;\n if (prepared.seed !== undefined)\n effectiveGenerationSettings.seed = prepared.seed;\n if (prepared.headers !== undefined)\n effectiveGenerationSettings.headers = prepared.headers;\n if (prepared.providerOptions !== undefined)\n effectiveGenerationSettings.providerOptions = prepared.providerOptions;\n }\n\n const prompt = await standardizePrompt({\n system: effectiveInstructions,\n ...(effectivePrompt != null\n ? { prompt: effectivePrompt }\n : { messages: effectiveMessages! }),\n });\n\n // Process tool approval responses before starting the agent loop.\n // This mirrors how stream-text.ts handles tool-approval-response parts:\n // approved tools are executed, denied tools get denial results, and\n // approval parts are stripped from the messages.\n const { approvedToolApprovals, deniedToolApprovals } =\n collectToolApprovalsFromMessages(prompt.messages);\n\n if (approvedToolApprovals.length > 0 || deniedToolApprovals.length > 0) {\n const _toolResultMessages: ModelMessage[] = [];\n const toolResultContent: Array<{\n type: 'tool-result';\n toolCallId: string;\n toolName: string;\n output:\n | { type: 'text'; value: string }\n | { type: 'json'; value: JSONValue }\n | { type: 'execution-denied'; reason: string | undefined };\n }> = [];\n\n // Execute approved tools\n for (const approval of approvedToolApprovals) {\n const tool = (this.tools as ToolSet)[approval.toolName];\n if (tool && typeof tool.execute === 'function') {\n try {\n const { execute } = tool;\n const toolResult = await execute(approval.input, {\n toolCallId: approval.toolCallId,\n messages: [],\n context: effectiveExperimentalContext,\n });\n toolResultContent.push({\n type: 'tool-result' as const,\n toolCallId: approval.toolCallId,\n toolName: approval.toolName,\n output:\n typeof toolResult === 'string'\n ? { type: 'text' as const, value: toolResult }\n : { type: 'json' as const, value: toolResult },\n });\n } catch (error) {\n toolResultContent.push({\n type: 'tool-result' as const,\n toolCallId: approval.toolCallId,\n toolName: approval.toolName,\n output: {\n type: 'text' as const,\n value: getErrorMessage(error),\n },\n });\n }\n }\n }\n\n // Create denial results for denied tools\n for (const denial of deniedToolApprovals) {\n toolResultContent.push({\n type: 'tool-result' as const,\n toolCallId: denial.toolCallId,\n toolName: denial.toolName,\n output: {\n type: 'execution-denied' as const,\n reason: denial.reason,\n },\n });\n }\n\n // Strip approval parts from messages and inject tool results\n const cleanedMessages: ModelMessage[] = [];\n for (const msg of prompt.messages) {\n if (msg.role === 'assistant' && Array.isArray(msg.content)) {\n const filtered = (msg.content as any[]).filter(\n (p: any) => p.type !== 'tool-approval-request',\n );\n if (filtered.length > 0) {\n cleanedMessages.push({ ...msg, content: filtered });\n }\n } else if (msg.role === 'tool') {\n const filtered = (msg.content as any[]).filter(\n (p: any) => p.type !== 'tool-approval-response',\n );\n if (filtered.length > 0) {\n cleanedMessages.push({ ...msg, content: filtered });\n }\n } else {\n cleanedMessages.push(msg);\n }\n }\n\n // Add tool results as a new tool message\n if (toolResultContent.length > 0) {\n cleanedMessages.push({\n role: 'tool',\n content: toolResultContent,\n } as ModelMessage);\n }\n\n prompt.messages = cleanedMessages;\n\n // Write tool results and step boundaries to the stream so the UI\n // can transition approved/denied tool parts to the correct state\n // and properly separate them from the subsequent model step.\n if (options.writable && toolResultContent.length > 0) {\n const approvedResults = toolResultContent\n .filter(r => r.output.type !== 'execution-denied')\n .map(r => ({\n toolCallId: r.toolCallId,\n toolName: r.toolName,\n input: approvedToolApprovals.find(\n a => a.toolCallId === r.toolCallId,\n )?.input,\n output: 'value' in r.output ? r.output.value : undefined,\n }));\n const deniedResults = toolResultContent\n .filter(r => r.output.type === 'execution-denied')\n .map(r => ({ toolCallId: r.toolCallId }));\n await writeApprovalToolResults(\n options.writable,\n approvedResults,\n deniedResults,\n );\n }\n }\n\n const modelPrompt = await convertToLanguageModelPrompt({\n prompt,\n supportedUrls: {},\n download: options.experimental_download ?? this.experimentalDownload,\n });\n\n const effectiveAbortSignal = mergeAbortSignals(\n options.abortSignal ?? effectiveGenerationSettings.abortSignal,\n options.timeout != null\n ? AbortSignal.timeout(options.timeout)\n : undefined,\n );\n\n // Merge generation settings: constructor defaults < prepareCall < stream options\n const mergedGenerationSettings: GenerationSettings = {\n ...effectiveGenerationSettings,\n ...(options.maxOutputTokens !== undefined && {\n maxOutputTokens: options.maxOutputTokens,\n }),\n ...(options.temperature !== undefined && {\n temperature: options.temperature,\n }),\n ...(options.topP !== undefined && { topP: options.topP }),\n ...(options.topK !== undefined && { topK: options.topK }),\n ...(options.presencePenalty !== undefined && {\n presencePenalty: options.presencePenalty,\n }),\n ...(options.frequencyPenalty !== undefined && {\n frequencyPenalty: options.frequencyPenalty,\n }),\n ...(options.stopSequences !== undefined && {\n stopSequences: options.stopSequences,\n }),\n ...(options.seed !== undefined && { seed: options.seed }),\n ...(options.maxRetries !== undefined && {\n maxRetries: options.maxRetries,\n }),\n ...(effectiveAbortSignal !== undefined && {\n abortSignal: effectiveAbortSignal,\n }),\n ...(options.headers !== undefined && { headers: options.headers }),\n ...(options.providerOptions !== undefined && {\n providerOptions: options.providerOptions,\n }),\n };\n\n // Merge constructor + stream callbacks (constructor first, then stream)\n const mergedOnStepFinish = mergeCallbacks(\n this.constructorOnStepFinish as\n | WorkflowAgentOnStepFinishCallback<TTools>\n | undefined,\n options.onStepFinish,\n );\n const mergedOnFinish = mergeCallbacks(\n this.constructorOnFinish as\n | WorkflowAgentOnFinishCallback<TTools, OUTPUT>\n | undefined,\n options.onFinish,\n );\n const mergedOnStart = mergeCallbacks(\n this.constructorOnStart,\n options.experimental_onStart,\n );\n const mergedOnStepStart = mergeCallbacks(\n this.constructorOnStepStart,\n options.experimental_onStepStart,\n );\n const mergedOnToolExecutionStart = mergeCallbacks(\n this.constructorOnToolExecutionStart,\n options.experimental_onToolExecutionStart,\n );\n const mergedOnToolExecutionEnd = mergeCallbacks(\n this.constructorOnToolExecutionEnd,\n options.experimental_onToolExecutionEnd,\n );\n\n // Determine effective tool choice\n const effectiveToolChoice = effectiveToolChoiceFromPrepare;\n\n // Merge telemetry settings\n const effectiveTelemetry = effectiveTelemetryFromPrepare;\n\n // Filter tools if activeTools is specified (stream-level overrides constructor default)\n const effectiveActiveTools = options.activeTools ?? this.activeTools;\n const effectiveTools =\n effectiveActiveTools && effectiveActiveTools.length > 0\n ? (filterActiveTools({\n tools: this.tools,\n activeTools: effectiveActiveTools as string[],\n }) ?? this.tools)\n : this.tools;\n\n // Initialize context\n let experimentalContext = effectiveExperimentalContext;\n\n const steps: StepResult<TTools, any>[] = [];\n\n // Track tool calls and results from the last step for the result\n let lastStepToolCalls: ToolCall[] = [];\n let lastStepToolResults: ToolResult[] = [];\n\n // Call onStart before the agent loop\n if (mergedOnStart) {\n await mergedOnStart({\n model: effectiveModel,\n messages: prompt.messages,\n });\n }\n\n // Helper to wrap executeTool with onToolExecutionStart/onToolExecutionEnd callbacks\n const executeToolWithCallbacks = async (\n toolCall: { toolCallId: string; toolName: string; input: unknown },\n tools: ToolSet,\n messages: LanguageModelV4Prompt,\n context?: unknown,\n currentStepNumber: number = 0,\n ): Promise<LanguageModelV4ToolResultPart> => {\n const toolCallEvent: ToolCall = {\n type: 'tool-call',\n toolCallId: toolCall.toolCallId,\n toolName: toolCall.toolName,\n input: toolCall.input,\n };\n\n if (mergedOnToolExecutionStart) {\n await mergedOnToolExecutionStart({\n toolCall: toolCallEvent,\n stepNumber: currentStepNumber,\n });\n }\n\n const startTime = Date.now();\n let result: LanguageModelV4ToolResultPart;\n try {\n result = await executeTool(toolCall, tools, messages, context);\n } catch (err) {\n const durationMs = Date.now() - startTime;\n if (mergedOnToolExecutionEnd) {\n await mergedOnToolExecutionEnd({\n toolCall: toolCallEvent,\n stepNumber: currentStepNumber,\n durationMs,\n success: false,\n error: err,\n });\n }\n throw err;\n }\n\n const durationMs = Date.now() - startTime;\n if (mergedOnToolExecutionEnd) {\n const isError =\n result.output &&\n 'type' in result.output &&\n (result.output.type === 'error-text' ||\n result.output.type === 'error-json');\n if (isError) {\n await mergedOnToolExecutionEnd({\n toolCall: toolCallEvent,\n stepNumber: currentStepNumber,\n durationMs,\n success: false,\n error: 'value' in result.output ? result.output.value : undefined,\n });\n } else {\n await mergedOnToolExecutionEnd({\n toolCall: toolCallEvent,\n stepNumber: currentStepNumber,\n durationMs,\n success: true,\n output:\n result.output && 'value' in result.output\n ? result.output.value\n : undefined,\n });\n }\n }\n return result;\n };\n\n // Check for abort before starting\n if (mergedGenerationSettings.abortSignal?.aborted) {\n if (options.onAbort) {\n await options.onAbort({ steps });\n }\n return {\n messages: prompt.messages,\n steps,\n toolCalls: [],\n toolResults: [],\n output: undefined as OUTPUT,\n };\n }\n\n const iterator = streamTextIterator({\n model: effectiveModel,\n tools: effectiveTools as ToolSet,\n writable: options.writable,\n prompt: modelPrompt,\n stopConditions: options.stopWhen ?? this.stopWhen,\n\n onStepFinish: mergedOnStepFinish,\n onStepStart: mergedOnStepStart,\n onError: options.onError,\n prepareStep:\n options.prepareStep ??\n (this.prepareStep as PrepareStepCallback<ToolSet> | undefined),\n generationSettings: mergedGenerationSettings,\n toolChoice: effectiveToolChoice as ToolChoice<ToolSet>,\n experimental_context: experimentalContext,\n experimental_telemetry: effectiveTelemetry,\n includeRawChunks: options.includeRawChunks ?? false,\n repairToolCall: (options.experimental_repairToolCall ??\n this.experimentalRepairToolCall) as\n | ToolCallRepairFunction<ToolSet>\n | undefined,\n responseFormat: await (options.output ?? this.output)?.responseFormat,\n });\n\n // Track the final conversation messages from the iterator\n let finalMessages: LanguageModelV4Prompt | undefined;\n let encounteredError: unknown;\n let wasAborted = false;\n\n try {\n let result = await iterator.next();\n while (!result.done) {\n // Check for abort during iteration\n if (mergedGenerationSettings.abortSignal?.aborted) {\n wasAborted = true;\n if (options.onAbort) {\n await options.onAbort({ steps });\n }\n break;\n }\n\n const {\n toolCalls,\n messages: iterMessages,\n step,\n context,\n providerExecutedToolResults,\n } = result.value;\n // Capture current step number before pushing (0-based)\n const currentStepNumber = steps.length;\n if (step) {\n steps.push(step as unknown as StepResult<TTools, any>);\n }\n if (context !== undefined) {\n experimentalContext = context;\n }\n\n // Only execute tools if there are tool calls\n if (toolCalls.length > 0) {\n // Separate provider-executed tool calls from client-executed ones\n const nonProviderToolCalls = toolCalls.filter(\n tc => !tc.providerExecuted,\n );\n const providerToolCalls = toolCalls.filter(tc => tc.providerExecuted);\n\n // Check which tools need approval (can be async)\n const approvalNeeded = await Promise.all(\n nonProviderToolCalls.map(async tc => {\n const tool = (effectiveTools as ToolSet)[tc.toolName];\n if (!tool) return false;\n if (tool.needsApproval == null) return false;\n if (typeof tool.needsApproval === 'boolean')\n return tool.needsApproval;\n return tool.needsApproval(tc.input, {\n toolCallId: tc.toolCallId,\n messages: iterMessages as unknown as ModelMessage[],\n context: experimentalContext,\n });\n }),\n );\n\n // Further split non-provider tool calls into:\n // - executable: has execute function and doesn't need approval\n // - paused: no execute function (client-side) OR needs approval\n // Note: missing tools (!tool) are left to executeTool which will throw.\n const executableToolCalls = nonProviderToolCalls.filter((tc, i) => {\n const tool = (effectiveTools as ToolSet)[tc.toolName];\n return (\n (!tool || typeof tool.execute === 'function') &&\n !approvalNeeded[i]\n );\n });\n const pausedToolCalls = nonProviderToolCalls.filter((tc, i) => {\n const tool = (effectiveTools as ToolSet)[tc.toolName];\n return (\n (tool && typeof tool.execute !== 'function') || approvalNeeded[i]\n );\n });\n\n // If there are paused tool calls (client-side or needing approval),\n // stop the loop and return them.\n // This matches AI SDK behavior: tools without execute or needing\n // approval pause the agent loop.\n if (pausedToolCalls.length > 0) {\n // Execute any executable tools that were also called in this step\n const executableResults = await Promise.all(\n executableToolCalls.map(\n (toolCall): Promise<LanguageModelV4ToolResultPart> =>\n executeToolWithCallbacks(\n toolCall,\n effectiveTools as ToolSet,\n iterMessages,\n experimentalContext,\n currentStepNumber,\n ),\n ),\n );\n\n // Collect provider tool results\n const providerResults: LanguageModelV4ToolResultPart[] =\n providerToolCalls.map(toolCall =>\n resolveProviderToolResult(\n toolCall,\n providerExecutedToolResults,\n ),\n );\n\n const resolvedResults = [...executableResults, ...providerResults];\n\n const allToolCalls: ToolCall[] = toolCalls.map(tc => ({\n type: 'tool-call' as const,\n toolCallId: tc.toolCallId,\n toolName: tc.toolName,\n input: tc.input,\n }));\n\n const allToolResults: ToolResult[] = resolvedResults.map(r => ({\n type: 'tool-result' as const,\n toolCallId: r.toolCallId,\n toolName: r.toolName,\n input: toolCalls.find(tc => tc.toolCallId === r.toolCallId)\n ?.input,\n output: 'value' in r.output ? r.output.value : undefined,\n }));\n\n if (resolvedResults.length > 0) {\n iterMessages.push({\n role: 'tool',\n content: resolvedResults,\n });\n }\n\n const messages = iterMessages as unknown as ModelMessage[];\n\n if (mergedOnFinish && !wasAborted) {\n const lastStep = steps[steps.length - 1];\n await mergedOnFinish({\n steps,\n messages,\n text: lastStep?.text ?? '',\n finishReason: lastStep?.finishReason ?? 'other',\n totalUsage: aggregateUsage(steps),\n experimental_context: experimentalContext,\n output: undefined as OUTPUT,\n });\n }\n\n // Emit tool-approval-request chunks for tools that need approval\n // so useChat can show the approval UI\n if (options.writable) {\n const approvalToolCalls = pausedToolCalls.filter((_, i) => {\n const tcIndex = nonProviderToolCalls.indexOf(\n pausedToolCalls[i],\n );\n return approvalNeeded[tcIndex];\n });\n if (approvalToolCalls.length > 0) {\n await writeApprovalRequests(\n options.writable,\n approvalToolCalls.map(tc => ({\n toolCallId: tc.toolCallId,\n toolName: tc.toolName,\n })),\n );\n }\n }\n\n // Close the stream before returning for paused tools\n if (options.writable) {\n const sendFinish = options.sendFinish ?? true;\n const preventClose = options.preventClose ?? false;\n if (sendFinish || !preventClose) {\n await closeStream(options.writable, preventClose, sendFinish);\n }\n }\n\n return {\n messages,\n steps,\n toolCalls: allToolCalls,\n toolResults: allToolResults,\n output: undefined as OUTPUT,\n };\n }\n\n // Execute client tools (all have execute functions at this point)\n const clientToolResults = await Promise.all(\n nonProviderToolCalls.map(\n (toolCall): Promise<LanguageModelV4ToolResultPart> =>\n executeToolWithCallbacks(\n toolCall,\n effectiveTools as ToolSet,\n iterMessages,\n experimentalContext,\n currentStepNumber,\n ),\n ),\n );\n\n // For provider-executed tools, use the results from the stream\n const providerToolResults: LanguageModelV4ToolResultPart[] =\n providerToolCalls.map(toolCall =>\n resolveProviderToolResult(toolCall, providerExecutedToolResults),\n );\n\n // Combine results in the original order\n const toolResults = toolCalls.map(tc => {\n const clientResult = clientToolResults.find(\n r => r.toolCallId === tc.toolCallId,\n );\n if (clientResult) return clientResult;\n const providerResult = providerToolResults.find(\n r => r.toolCallId === tc.toolCallId,\n );\n if (providerResult) return providerResult;\n // This should never happen, but return empty result as fallback\n return {\n type: 'tool-result' as const,\n toolCallId: tc.toolCallId,\n toolName: tc.toolName,\n output: { type: 'text' as const, value: '' },\n };\n });\n\n // Write tool results and step boundaries to the stream so the\n // UI can transition tool parts to output-available state and\n // properly separate multi-step model calls in the message history.\n if (options.writable) {\n await writeToolResultsWithStepBoundary(\n options.writable,\n toolResults.map(r => ({\n toolCallId: r.toolCallId,\n toolName: r.toolName,\n input: toolCalls.find(tc => tc.toolCallId === r.toolCallId)\n ?.input,\n output: 'value' in r.output ? r.output.value : undefined,\n })),\n );\n }\n\n // Track the tool calls and results for this step\n lastStepToolCalls = toolCalls.map(tc => ({\n type: 'tool-call' as const,\n toolCallId: tc.toolCallId,\n toolName: tc.toolName,\n input: tc.input,\n }));\n lastStepToolResults = toolResults.map(r => ({\n type: 'tool-result' as const,\n toolCallId: r.toolCallId,\n toolName: r.toolName,\n input: toolCalls.find(tc => tc.toolCallId === r.toolCallId)?.input,\n output: 'value' in r.output ? r.output.value : undefined,\n }));\n\n result = await iterator.next(toolResults);\n } else {\n // Final step with no tool calls - reset tracking\n lastStepToolCalls = [];\n lastStepToolResults = [];\n result = await iterator.next([]);\n }\n }\n\n // When the iterator completes normally, result.value contains the final conversation prompt\n if (result.done) {\n finalMessages = result.value;\n }\n } catch (error) {\n encounteredError = error;\n // Check if this is an abort error\n if (error instanceof Error && error.name === 'AbortError') {\n wasAborted = true;\n if (options.onAbort) {\n await options.onAbort({ steps });\n }\n } else if (options.onError) {\n // Call onError for non-abort errors (including tool execution errors)\n await options.onError({ error });\n }\n // Don't throw yet - we want to call onFinish first\n }\n\n // Use the final messages from the iterator, or fall back to standardized messages\n const messages = (finalMessages ??\n prompt.messages) as unknown as ModelMessage[];\n\n // Parse structured output if output is specified (stream-level overrides constructor default)\n const effectiveOutput = options.output ?? this.output;\n let experimentalOutput: OUTPUT = undefined as OUTPUT;\n if (effectiveOutput && steps.length > 0) {\n const lastStep = steps[steps.length - 1];\n const text = lastStep.text;\n if (text) {\n try {\n experimentalOutput = await effectiveOutput.parseCompleteOutput(\n { text },\n {\n response: lastStep.response,\n usage: lastStep.usage,\n finishReason: lastStep.finishReason,\n },\n );\n } catch (parseError) {\n // If there's already an error, don't override it\n // If not, set this as the error\n if (!encounteredError) {\n encounteredError = parseError;\n }\n }\n }\n }\n\n // Call onFinish callback if provided (always call, even on errors, but not on abort)\n if (mergedOnFinish && !wasAborted) {\n const lastStep = steps[steps.length - 1];\n await mergedOnFinish({\n steps,\n messages: messages as ModelMessage[],\n text: lastStep?.text ?? '',\n finishReason: lastStep?.finishReason ?? 'other',\n totalUsage: aggregateUsage(steps),\n experimental_context: experimentalContext,\n output: experimentalOutput,\n });\n }\n\n // Re-throw any error that occurred\n if (encounteredError) {\n // Close the stream before throwing\n if (options.writable) {\n const sendFinish = options.sendFinish ?? true;\n const preventClose = options.preventClose ?? false;\n if (sendFinish || !preventClose) {\n await closeStream(options.writable, preventClose, sendFinish);\n }\n }\n throw encounteredError;\n }\n\n // Close the writable stream\n if (options.writable) {\n const sendFinish = options.sendFinish ?? true;\n const preventClose = options.preventClose ?? false;\n if (sendFinish || !preventClose) {\n await closeStream(options.writable, preventClose, sendFinish);\n }\n }\n\n return {\n messages: messages as ModelMessage[],\n steps,\n toolCalls: lastStepToolCalls,\n toolResults: lastStepToolResults,\n output: experimentalOutput,\n };\n }\n}\n\n/**\n * Filter tools to only include the specified active tools.\n */\n/**\n * Aggregate token usage across all steps.\n */\n/**\n * Close the writable stream, optionally sending a finish chunk first.\n * This is a step function because writable.getWriter() and writable.close()\n * cannot be called in workflow context (sandbox limitation).\n */\nasync function closeStream(\n writable: WritableStream<any>,\n preventClose?: boolean,\n sendFinish?: boolean,\n) {\n 'use step';\n if (sendFinish) {\n const writer = writable.getWriter();\n try {\n await writer.write({ type: 'finish' });\n } finally {\n writer.releaseLock();\n }\n }\n if (!preventClose) {\n await writable.close();\n }\n}\n\n/**\n * Write tool-approval-request chunks to the writable stream.\n * These are consumed by useChat to show the approval UI.\n */\nasync function writeApprovalRequests(\n writable: WritableStream<any>,\n toolCalls: Array<{ toolCallId: string; toolName: string }>,\n) {\n 'use step';\n const writer = writable.getWriter();\n try {\n for (const tc of toolCalls) {\n await writer.write({\n type: 'tool-approval-request',\n approvalId: `approval-${tc.toolCallId}`,\n toolCallId: tc.toolCallId,\n });\n }\n } finally {\n writer.releaseLock();\n }\n}\n\nasync function writeToolResultsWithStepBoundary(\n writable: WritableStream<any>,\n results: Array<{\n toolCallId: string;\n toolName: string;\n input: unknown;\n output: unknown;\n }>,\n) {\n 'use step';\n const writer = writable.getWriter();\n try {\n for (const r of results) {\n await writer.write({\n type: 'tool-result',\n toolCallId: r.toolCallId,\n toolName: r.toolName,\n input: r.input,\n output: r.output,\n });\n }\n // Emit step boundaries so the UI message history properly separates\n // the tool call step from the subsequent text step. This ensures\n // convertToModelMessages creates separate assistant messages for\n // tool calls and text responses.\n await writer.write({ type: 'finish-step' });\n await writer.write({ type: 'start-step' });\n } finally {\n writer.releaseLock();\n }\n}\n\nasync function writeApprovalToolResults(\n writable: WritableStream<any>,\n approvedResults: Array<{\n toolCallId: string;\n toolName: string;\n input: unknown;\n output: unknown;\n }>,\n deniedResults: Array<{ toolCallId: string }>,\n) {\n 'use step';\n const writer = writable.getWriter();\n try {\n for (const r of approvedResults) {\n await writer.write({\n type: 'tool-result',\n toolCallId: r.toolCallId,\n toolName: r.toolName,\n input: r.input,\n output: r.output,\n });\n }\n for (const r of deniedResults) {\n await writer.write({\n type: 'tool-output-denied',\n toolCallId: r.toolCallId,\n });\n }\n await writer.write({ type: 'finish-step' });\n await writer.write({ type: 'start-step' });\n } finally {\n writer.releaseLock();\n }\n}\n\nfunction aggregateUsage(steps: StepResult<any, any>[]): LanguageModelUsage {\n let inputTokens = 0;\n let outputTokens = 0;\n for (const step of steps) {\n inputTokens += step.usage?.inputTokens ?? 0;\n outputTokens += step.usage?.outputTokens ?? 0;\n }\n return {\n inputTokens,\n outputTokens,\n totalTokens: inputTokens + outputTokens,\n } as LanguageModelUsage;\n}\n\n// Matches AI SDK's getErrorMessage from @ai-sdk/provider-utils\nfunction getErrorMessage(error: unknown): string {\n if (error == null) {\n return 'unknown error';\n }\n\n if (typeof error === 'string') {\n return error;\n }\n\n if (error instanceof Error) {\n return error.message;\n }\n\n return JSON.stringify(error);\n}\n\nfunction resolveProviderToolResult(\n toolCall: { toolCallId: string; toolName: string },\n providerExecutedToolResults?: Map<\n string,\n { toolCallId: string; toolName: string; result: unknown; isError?: boolean }\n >,\n): LanguageModelV4ToolResultPart {\n const streamResult = providerExecutedToolResults?.get(toolCall.toolCallId);\n if (!streamResult) {\n console.warn(\n `[WorkflowAgent] Provider-executed tool \"${toolCall.toolName}\" (${toolCall.toolCallId}) ` +\n `did not receive a result from the stream. This may indicate a provider issue.`,\n );\n return {\n type: 'tool-result' as const,\n toolCallId: toolCall.toolCallId,\n toolName: toolCall.toolName,\n output: {\n type: 'text' as const,\n value: '',\n },\n };\n }\n\n const result = streamResult.result;\n const isString = typeof result === 'string';\n\n return {\n type: 'tool-result' as const,\n toolCallId: toolCall.toolCallId,\n toolName: toolCall.toolName,\n output: isString\n ? streamResult.isError\n ? { type: 'error-text' as const, value: result }\n : { type: 'text' as const, value: result }\n : streamResult.isError\n ? {\n type: 'error-json' as const,\n value: result as JSONValue,\n }\n : {\n type: 'json' as const,\n value: result as JSONValue,\n },\n };\n}\n\nasync function executeTool(\n toolCall: { toolCallId: string; toolName: string; input: unknown },\n tools: ToolSet,\n messages: LanguageModelV4Prompt,\n experimentalContext?: unknown,\n): Promise<LanguageModelV4ToolResultPart> {\n const tool = tools[toolCall.toolName];\n if (!tool) throw new Error(`Tool \"${toolCall.toolName}\" not found`);\n if (typeof tool.execute !== 'function') {\n throw new Error(\n `Tool \"${toolCall.toolName}\" does not have an execute function. ` +\n `Client-side tools should be filtered before calling executeTool.`,\n );\n }\n // Input is already parsed and validated by streamModelCall's parseToolCall\n const parsedInput = toolCall.input;\n\n try {\n // Extract execute function to avoid binding `this` to the tool object.\n // If we called `tool.execute(...)` directly, JavaScript would bind `this`\n // to `tool`, which contains non-serializable properties like `inputSchema`.\n // When the execute function is a workflow step (marked with 'use step'),\n // the step system captures `this` for serialization, causing failures.\n const { execute } = tool;\n const toolResult = await execute(parsedInput, {\n toolCallId: toolCall.toolCallId,\n // Pass the conversation messages to the tool so it has context about the conversation\n messages,\n // Pass context to the tool\n context: experimentalContext,\n });\n\n // Use the appropriate output type based on the result\n // AI SDK supports 'text' for strings and 'json' for objects\n const output =\n typeof toolResult === 'string'\n ? { type: 'text' as const, value: toolResult }\n : { type: 'json' as const, value: toolResult };\n\n return {\n type: 'tool-result' as const,\n toolCallId: toolCall.toolCallId,\n toolName: toolCall.toolName,\n output,\n };\n } catch (error) {\n // Convert tool errors to error-text results sent back to the model,\n // allowing the agent to recover rather than killing the entire stream.\n // This aligns with AI SDK's streamText behavior for individual tool failures.\n return {\n type: 'tool-result',\n toolCallId: toolCall.toolCallId,\n toolName: toolCall.toolName,\n output: {\n type: 'error-text',\n value: getErrorMessage(error),\n },\n };\n }\n}\n\n/**\n * Collected tool approval information for a single tool call.\n */\ninterface CollectedApproval {\n toolCallId: string;\n toolName: string;\n input: unknown;\n approvalId: string;\n reason?: string;\n}\n\n/**\n * Collect tool approval responses from model messages.\n * Mirrors the logic from `collectToolApprovals` in the AI SDK core\n * (`packages/ai/src/generate-text/collect-tool-approvals.ts`).\n *\n * Scans the last tool message for `tool-approval-response` parts,\n * matches them with `tool-approval-request` parts in assistant messages\n * and the corresponding `tool-call` parts.\n */\nfunction collectToolApprovalsFromMessages(messages: ModelMessage[]): {\n approvedToolApprovals: CollectedApproval[];\n deniedToolApprovals: CollectedApproval[];\n} {\n const lastMessage = messages.at(-1);\n\n if (lastMessage?.role !== 'tool') {\n return { approvedToolApprovals: [], deniedToolApprovals: [] };\n }\n\n // Gather tool calls from assistant messages\n const toolCallsByToolCallId: Record<\n string,\n { toolName: string; input: unknown }\n > = {};\n for (const message of messages) {\n if (message.role === 'assistant' && Array.isArray(message.content)) {\n for (const part of message.content as any[]) {\n if (part.type === 'tool-call') {\n toolCallsByToolCallId[part.toolCallId] = {\n toolName: part.toolName,\n input: part.input ?? part.args,\n };\n }\n }\n }\n }\n\n // Gather approval requests from assistant messages\n const approvalRequestsByApprovalId: Record<\n string,\n { approvalId: string; toolCallId: string }\n > = {};\n for (const message of messages) {\n if (message.role === 'assistant' && Array.isArray(message.content)) {\n for (const part of message.content as any[]) {\n if (part.type === 'tool-approval-request') {\n approvalRequestsByApprovalId[part.approvalId] = {\n approvalId: part.approvalId,\n toolCallId: part.toolCallId,\n };\n }\n }\n }\n }\n\n // Gather existing tool results to avoid re-executing\n const existingToolResults = new Set<string>();\n for (const part of lastMessage.content as any[]) {\n if (part.type === 'tool-result') {\n existingToolResults.add(part.toolCallId);\n }\n }\n\n const approvedToolApprovals: CollectedApproval[] = [];\n const deniedToolApprovals: CollectedApproval[] = [];\n\n // Collect approval responses from the last tool message\n const approvalResponses = (lastMessage.content as any[]).filter(\n (part: any) => part.type === 'tool-approval-response',\n );\n\n for (const response of approvalResponses) {\n const approvalRequest = approvalRequestsByApprovalId[response.approvalId];\n if (approvalRequest == null) continue;\n\n // Skip if there's already a tool result for this tool call\n if (existingToolResults.has(approvalRequest.toolCallId)) continue;\n\n const toolCall = toolCallsByToolCallId[approvalRequest.toolCallId];\n if (toolCall == null) continue;\n\n const approval: CollectedApproval = {\n toolCallId: approvalRequest.toolCallId,\n toolName: toolCall.toolName,\n input: toolCall.input,\n approvalId: response.approvalId,\n reason: response.reason,\n };\n\n if (response.approved) {\n approvedToolApprovals.push(approval);\n } else {\n deniedToolApprovals.push(approval);\n }\n }\n\n return { approvedToolApprovals, deniedToolApprovals };\n}\n","import type {\n LanguageModelV4CallOptions,\n LanguageModelV4Prompt,\n LanguageModelV4ToolResultPart,\n} from '@ai-sdk/provider';\nimport {\n type Experimental_LanguageModelStreamPart as ModelCallStreamPart,\n type LanguageModel,\n type ModelMessage,\n type StepResult,\n type ToolCallRepairFunction,\n type ToolChoice,\n type ToolSet,\n experimental_filterActiveTools as filterActiveTools,\n} from 'ai';\nimport {\n doStreamStep,\n type ModelStopCondition,\n type ParsedToolCall,\n type ProviderExecutedToolResult,\n} from './do-stream-step.js';\nimport { serializeToolSet } from './serializable-schema.js';\nimport type {\n GenerationSettings,\n PrepareStepCallback,\n WorkflowAgentOnErrorCallback,\n WorkflowAgentOnStepFinishCallback,\n TelemetryOptions,\n WorkflowAgentOnStepStartCallback,\n} from './workflow-agent.js';\n\n// Re-export for consumers\nexport type { ProviderExecutedToolResult } from './do-stream-step.js';\n\n/**\n * The value yielded by the stream text iterator when tool calls are requested.\n * Contains both the tool calls and the current conversation messages.\n */\nexport interface StreamTextIteratorYieldValue {\n /** The tool calls requested by the model (parsed with typed inputs) */\n toolCalls: ParsedToolCall[];\n /** The conversation messages up to (and including) the tool call request */\n messages: LanguageModelV4Prompt;\n /** The step result from the current step */\n step?: StepResult<ToolSet, any>;\n /** The current experimental context */\n context?: unknown;\n /** Provider-executed tool results (keyed by tool call ID) */\n providerExecutedToolResults?: Map<string, ProviderExecutedToolResult>;\n}\n\n// This runs in the workflow context\nexport async function* streamTextIterator({\n prompt,\n tools = {},\n writable,\n model,\n stopConditions,\n onStepFinish,\n onStepStart,\n onError,\n prepareStep,\n generationSettings,\n toolChoice,\n experimental_context,\n experimental_telemetry,\n includeRawChunks = false,\n repairToolCall,\n responseFormat,\n}: {\n prompt: LanguageModelV4Prompt;\n tools: ToolSet;\n writable?: WritableStream<ModelCallStreamPart<ToolSet>>;\n model: LanguageModel;\n stopConditions?: ModelStopCondition[] | ModelStopCondition;\n onStepFinish?: WorkflowAgentOnStepFinishCallback<any>;\n onStepStart?: WorkflowAgentOnStepStartCallback;\n onError?: WorkflowAgentOnErrorCallback;\n prepareStep?: PrepareStepCallback<any>;\n generationSettings?: GenerationSettings;\n toolChoice?: ToolChoice<ToolSet>;\n experimental_context?: unknown;\n experimental_telemetry?: TelemetryOptions;\n includeRawChunks?: boolean;\n repairToolCall?: ToolCallRepairFunction<ToolSet>;\n responseFormat?: LanguageModelV4CallOptions['responseFormat'];\n}): AsyncGenerator<\n StreamTextIteratorYieldValue,\n LanguageModelV4Prompt,\n LanguageModelV4ToolResultPart[]\n> {\n let conversationPrompt = [...prompt]; // Create a mutable copy\n let currentModel: LanguageModel = model;\n let currentGenerationSettings = generationSettings ?? {};\n let currentToolChoice = toolChoice;\n let currentContext = experimental_context;\n let currentActiveTools: string[] | undefined;\n\n const steps: StepResult<any, any>[] = [];\n let done = false;\n let _isFirstIteration = true;\n let stepNumber = 0;\n let lastStep: StepResult<any, any> | undefined;\n let lastStepWasToolCalls = false;\n\n while (!done) {\n // Check for abort signal\n if (currentGenerationSettings.abortSignal?.aborted) {\n break;\n }\n\n // Call prepareStep callback before each step if provided\n if (prepareStep) {\n const prepareResult = await prepareStep({\n model: currentModel,\n stepNumber,\n steps,\n messages: conversationPrompt,\n experimental_context: currentContext,\n });\n\n // Apply any overrides from prepareStep\n if (prepareResult.model !== undefined) {\n currentModel = prepareResult.model;\n }\n // Apply messages override BEFORE system so the system message\n // isn't lost when messages replaces the prompt.\n if (prepareResult.messages !== undefined) {\n conversationPrompt = [...prepareResult.messages];\n }\n if (prepareResult.system !== undefined) {\n // Update or prepend system message in the conversation prompt.\n // Applied AFTER messages override so the system message isn't\n // lost when messages replaces the prompt.\n if (\n conversationPrompt.length > 0 &&\n conversationPrompt[0].role === 'system'\n ) {\n // Replace existing system message\n conversationPrompt[0] = {\n role: 'system',\n content: prepareResult.system,\n };\n } else {\n // Prepend new system message\n conversationPrompt.unshift({\n role: 'system',\n content: prepareResult.system,\n });\n }\n }\n if (prepareResult.experimental_context !== undefined) {\n currentContext = prepareResult.experimental_context;\n }\n if (prepareResult.activeTools !== undefined) {\n currentActiveTools = prepareResult.activeTools;\n }\n // Apply generation settings overrides\n if (prepareResult.maxOutputTokens !== undefined) {\n currentGenerationSettings = {\n ...currentGenerationSettings,\n maxOutputTokens: prepareResult.maxOutputTokens,\n };\n }\n if (prepareResult.temperature !== undefined) {\n currentGenerationSettings = {\n ...currentGenerationSettings,\n temperature: prepareResult.temperature,\n };\n }\n if (prepareResult.topP !== undefined) {\n currentGenerationSettings = {\n ...currentGenerationSettings,\n topP: prepareResult.topP,\n };\n }\n if (prepareResult.topK !== undefined) {\n currentGenerationSettings = {\n ...currentGenerationSettings,\n topK: prepareResult.topK,\n };\n }\n if (prepareResult.presencePenalty !== undefined) {\n currentGenerationSettings = {\n ...currentGenerationSettings,\n presencePenalty: prepareResult.presencePenalty,\n };\n }\n if (prepareResult.frequencyPenalty !== undefined) {\n currentGenerationSettings = {\n ...currentGenerationSettings,\n frequencyPenalty: prepareResult.frequencyPenalty,\n };\n }\n if (prepareResult.stopSequences !== undefined) {\n currentGenerationSettings = {\n ...currentGenerationSettings,\n stopSequences: prepareResult.stopSequences,\n };\n }\n if (prepareResult.seed !== undefined) {\n currentGenerationSettings = {\n ...currentGenerationSettings,\n seed: prepareResult.seed,\n };\n }\n if (prepareResult.maxRetries !== undefined) {\n currentGenerationSettings = {\n ...currentGenerationSettings,\n maxRetries: prepareResult.maxRetries,\n };\n }\n if (prepareResult.headers !== undefined) {\n currentGenerationSettings = {\n ...currentGenerationSettings,\n headers: prepareResult.headers,\n };\n }\n if (prepareResult.providerOptions !== undefined) {\n currentGenerationSettings = {\n ...currentGenerationSettings,\n providerOptions: prepareResult.providerOptions,\n };\n }\n if (prepareResult.toolChoice !== undefined) {\n currentToolChoice = prepareResult.toolChoice;\n }\n }\n\n if (onStepStart) {\n await onStepStart({\n stepNumber,\n model: currentModel,\n messages: conversationPrompt as unknown as ModelMessage[],\n steps: [...steps],\n });\n }\n\n try {\n // Filter tools if activeTools is specified\n const effectiveTools =\n currentActiveTools && currentActiveTools.length > 0\n ? (filterActiveTools({\n tools,\n activeTools: currentActiveTools,\n }) ?? tools)\n : tools;\n\n // Serialize tools before crossing the step boundary — zod schemas\n // contain functions that can't be serialized by the workflow runtime.\n // Tools are reconstructed with Ajv validation inside doStreamStep.\n const serializedTools = serializeToolSet(effectiveTools);\n\n const { toolCalls, finish, step, providerExecutedToolResults } =\n await doStreamStep(\n conversationPrompt,\n currentModel,\n writable,\n serializedTools,\n {\n ...currentGenerationSettings,\n toolChoice: currentToolChoice,\n includeRawChunks,\n experimental_telemetry,\n repairToolCall,\n responseFormat,\n },\n );\n\n _isFirstIteration = false;\n stepNumber++;\n steps.push(step);\n lastStep = step;\n lastStepWasToolCalls = false;\n\n const finishReason = finish?.finishReason;\n\n if (finishReason === 'tool-calls') {\n lastStepWasToolCalls = true;\n\n // Add assistant message with tool calls to the conversation\n // Note: providerMetadata from the tool call is mapped to providerOptions\n // in the prompt format, following the AI SDK convention. This is critical\n // for providers like Gemini that require thoughtSignature to be preserved\n // across multi-turn tool calls. Some fields are sanitized before mapping.\n conversationPrompt.push({\n role: 'assistant',\n content: toolCalls.map(toolCall => {\n const sanitizedMetadata = sanitizeProviderMetadataForToolCall(\n toolCall.providerMetadata,\n );\n return {\n type: 'tool-call',\n toolCallId: toolCall.toolCallId,\n toolName: toolCall.toolName,\n input: toolCall.input,\n ...(sanitizedMetadata != null\n ? { providerOptions: sanitizedMetadata }\n : {}),\n };\n }) as typeof toolCalls,\n });\n\n // Yield the tool calls along with the current conversation messages\n // This allows executeTool to pass the conversation context to tool execute functions\n // Also include provider-executed tool results so they can be used instead of local execution\n const toolResults = yield {\n toolCalls,\n messages: conversationPrompt,\n step,\n context: currentContext,\n providerExecutedToolResults,\n };\n\n conversationPrompt.push({\n role: 'tool',\n content: toolResults,\n });\n\n if (stopConditions) {\n const stopConditionList = Array.isArray(stopConditions)\n ? stopConditions\n : [stopConditions];\n if (stopConditionList.some(test => test({ steps }))) {\n done = true;\n }\n }\n } else if (finishReason === 'stop') {\n // Add assistant message with text content to the conversation\n const textContent = step.content.filter(\n item => item.type === 'text',\n ) as Array<{ type: 'text'; text: string }>;\n\n if (textContent.length > 0) {\n conversationPrompt.push({\n role: 'assistant',\n content: textContent,\n });\n }\n\n done = true;\n } else if (finishReason === 'length') {\n // Model hit max tokens - stop but don't throw\n done = true;\n } else if (finishReason === 'content-filter') {\n // Content filter triggered - stop but don't throw\n done = true;\n } else if (finishReason === 'error') {\n // Model error - stop but don't throw\n done = true;\n } else if (finishReason === 'other') {\n // Other reason - stop but don't throw\n done = true;\n } else if (finishReason === 'unknown') {\n // Unknown reason - stop but don't throw\n done = true;\n } else if (!finishReason) {\n // No finish reason - this might happen on incomplete streams\n done = true;\n } else {\n throw new Error(\n `Unexpected finish reason: ${typeof finish?.finishReason === 'object' ? JSON.stringify(finish?.finishReason) : finish?.finishReason}`,\n );\n }\n\n if (onStepFinish) {\n await onStepFinish(step);\n }\n } catch (error) {\n if (onError) {\n await onError({ error });\n }\n throw error;\n }\n }\n\n // Yield the final step if it wasn't already yielded (tool-calls steps are yielded inside the loop)\n if (lastStep && !lastStepWasToolCalls) {\n yield {\n toolCalls: [],\n messages: conversationPrompt,\n step: lastStep,\n context: currentContext,\n };\n }\n\n return conversationPrompt;\n}\n\n/**\n * Strip OpenAI's itemId from providerMetadata (requires reasoning items we don't preserve).\n * Preserves all other provider metadata (e.g., Gemini's thoughtSignature).\n */\nfunction sanitizeProviderMetadataForToolCall(\n metadata: unknown,\n): Record<string, unknown> | undefined {\n if (metadata == null) return undefined;\n\n const meta = metadata as Record<string, unknown>;\n\n // Check if OpenAI metadata exists and needs sanitization\n if ('openai' in meta && meta.openai != null) {\n const { openai, ...restProviders } = meta;\n const openaiMeta = openai as Record<string, unknown>;\n\n // Remove itemId from OpenAI metadata - it requires reasoning items we don't preserve\n const { itemId: _itemId, ...restOpenai } = openaiMeta;\n\n // Reconstruct metadata without itemId\n const hasOtherOpenaiFields = Object.keys(restOpenai).length > 0;\n const hasOtherProviders = Object.keys(restProviders).length > 0;\n\n if (hasOtherOpenaiFields && hasOtherProviders) {\n return { ...restProviders, openai: restOpenai };\n } else if (hasOtherOpenaiFields) {\n return { openai: restOpenai };\n } else if (hasOtherProviders) {\n return restProviders;\n }\n return undefined;\n }\n\n return meta;\n}\n","import type {\n LanguageModelV4CallOptions,\n LanguageModelV4Prompt,\n} from '@ai-sdk/provider';\nimport {\n type Experimental_LanguageModelStreamPart as ModelCallStreamPart,\n experimental_streamLanguageModelCall as streamModelCall,\n type FinishReason,\n type LanguageModel,\n type LanguageModelUsage,\n type ModelMessage,\n type StepResult,\n type StopCondition,\n type ToolCallRepairFunction,\n type ToolChoice,\n type ToolSet,\n} from 'ai';\nimport { gateway } from 'ai';\nimport type { ProviderOptions, TelemetryOptions } from './workflow-agent.js';\nimport {\n resolveSerializableTools,\n type SerializableToolDef,\n} from './serializable-schema.js';\n\nexport type { Experimental_LanguageModelStreamPart as ModelCallStreamPart } from 'ai';\n\nexport type ModelStopCondition = StopCondition<NoInfer<ToolSet>, any>;\n\n/**\n * Provider-executed tool result captured from the stream.\n */\nexport interface ProviderExecutedToolResult {\n toolCallId: string;\n toolName: string;\n result: unknown;\n isError?: boolean;\n}\n\n/**\n * Options for the doStreamStep function.\n */\nexport interface DoStreamStepOptions {\n maxOutputTokens?: number;\n temperature?: number;\n topP?: number;\n topK?: number;\n presencePenalty?: number;\n frequencyPenalty?: number;\n stopSequences?: string[];\n seed?: number;\n maxRetries?: number;\n abortSignal?: AbortSignal;\n headers?: Record<string, string | undefined>;\n providerOptions?: ProviderOptions;\n toolChoice?: ToolChoice<ToolSet>;\n includeRawChunks?: boolean;\n experimental_telemetry?: TelemetryOptions;\n repairToolCall?: ToolCallRepairFunction<ToolSet>;\n responseFormat?: LanguageModelV4CallOptions['responseFormat'];\n}\n\n/**\n * Parsed tool call from the stream (parsed by streamModelCall's transform).\n */\nexport interface ParsedToolCall {\n type: 'tool-call';\n toolCallId: string;\n toolName: string;\n input: unknown;\n providerExecuted?: boolean;\n providerMetadata?: Record<string, unknown>;\n dynamic?: boolean;\n invalid?: boolean;\n error?: unknown;\n}\n\n/**\n * Finish metadata from the stream.\n */\nexport interface StreamFinish {\n finishReason: FinishReason;\n rawFinishReason: string | undefined;\n usage: LanguageModelUsage;\n providerMetadata?: Record<string, unknown>;\n}\n\nexport async function doStreamStep(\n conversationPrompt: LanguageModelV4Prompt,\n modelInit: LanguageModel,\n writable?: WritableStream<ModelCallStreamPart<ToolSet>>,\n serializedTools?: Record<string, SerializableToolDef>,\n options?: DoStreamStepOptions,\n) {\n 'use step';\n\n // Resolve model inside step (must happen here for serialization boundary)\n const model: LanguageModel =\n typeof modelInit === 'string'\n ? gateway.languageModel(modelInit)\n : modelInit;\n\n // Reconstruct tools from serializable definitions with Ajv validation.\n // Tools are serialized before crossing the step boundary because zod schemas\n // contain functions that can't be serialized by the workflow runtime.\n const tools = serializedTools\n ? resolveSerializableTools(serializedTools)\n : undefined;\n\n // streamModelCall handles: prompt standardization, tool preparation,\n // model.doStream(), retry logic, and stream part transformation\n // (tool call parsing, finish reason mapping, file wrapping).\n const { stream: modelStream } = await streamModelCall({\n model,\n // streamModelCall expects Prompt (ModelMessage[]) but we pass the\n // pre-converted LanguageModelV4Prompt. standardizePrompt inside\n // streamModelCall handles both formats.\n messages: conversationPrompt as unknown as ModelMessage[],\n tools,\n toolChoice: options?.toolChoice,\n includeRawChunks: options?.includeRawChunks,\n providerOptions: options?.providerOptions,\n abortSignal: options?.abortSignal,\n headers: options?.headers,\n maxOutputTokens: options?.maxOutputTokens,\n temperature: options?.temperature,\n topP: options?.topP,\n topK: options?.topK,\n presencePenalty: options?.presencePenalty,\n frequencyPenalty: options?.frequencyPenalty,\n stopSequences: options?.stopSequences,\n seed: options?.seed,\n repairToolCall: options?.repairToolCall,\n });\n\n // Consume the stream: capture data and write to writable in real-time\n const toolCalls: ParsedToolCall[] = [];\n const providerExecutedToolResults = new Map<\n string,\n ProviderExecutedToolResult\n >();\n let finish: StreamFinish | undefined;\n\n // Aggregation for StepResult\n let text = '';\n const reasoningParts: Array<{ text: string }> = [];\n let responseMetadata:\n | { id?: string; timestamp?: Date; modelId?: string }\n | undefined;\n let warnings: unknown[] | undefined;\n\n // Acquire writer once before the loop to avoid per-chunk lock overhead\n const writer = writable?.getWriter();\n\n try {\n for await (const part of modelStream) {\n switch (part.type) {\n case 'text-delta':\n text += part.text;\n break;\n case 'reasoning-delta':\n reasoningParts.push({ text: part.text });\n break;\n case 'tool-call': {\n // parseToolCall adds dynamic/invalid/error at runtime\n const toolCallPart = part as typeof part & Partial<ParsedToolCall>;\n toolCalls.push({\n type: 'tool-call',\n toolCallId: toolCallPart.toolCallId,\n toolName: toolCallPart.toolName,\n input: toolCallPart.input,\n providerExecuted: toolCallPart.providerExecuted,\n providerMetadata: toolCallPart.providerMetadata as\n | Record<string, unknown>\n | undefined,\n dynamic: toolCallPart.dynamic,\n invalid: toolCallPart.invalid,\n error: toolCallPart.error,\n });\n break;\n }\n case 'tool-result':\n if (part.providerExecuted) {\n providerExecutedToolResults.set(part.toolCallId, {\n toolCallId: part.toolCallId,\n toolName: part.toolName,\n result: part.output,\n isError: false,\n });\n }\n break;\n case 'tool-error': {\n const errorPart = part as typeof part & {\n providerExecuted?: boolean;\n };\n if (errorPart.providerExecuted) {\n providerExecutedToolResults.set(errorPart.toolCallId, {\n toolCallId: errorPart.toolCallId,\n toolName: errorPart.toolName,\n result: errorPart.error,\n isError: true,\n });\n }\n break;\n }\n case 'model-call-end':\n finish = {\n finishReason: part.finishReason,\n rawFinishReason: part.rawFinishReason,\n usage: part.usage,\n providerMetadata: part.providerMetadata as\n | Record<string, unknown>\n | undefined,\n };\n break;\n case 'model-call-start':\n warnings = part.warnings;\n break;\n case 'model-call-response-metadata':\n responseMetadata = part;\n break;\n }\n\n // Write to writable in real-time\n if (writer) {\n await writer.write(part);\n }\n }\n } finally {\n writer?.releaseLock();\n }\n\n // Build StepResult\n const reasoningText = reasoningParts.map(r => r.text).join('') || undefined;\n\n const step: StepResult<ToolSet, any> = {\n callId: 'workflow-agent',\n stepNumber: 0,\n model: {\n provider: responseMetadata?.modelId?.split(':')[0] ?? 'unknown',\n modelId: responseMetadata?.modelId ?? 'unknown',\n },\n functionId: undefined,\n metadata: undefined,\n runtimeContext: undefined,\n toolsContext: {},\n content: [\n ...(text ? [{ type: 'text' as const, text }] : []),\n ...toolCalls\n .filter(tc => !tc.invalid)\n .map(tc => ({\n type: 'tool-call' as const,\n toolCallId: tc.toolCallId,\n toolName: tc.toolName,\n input: tc.input,\n ...(tc.dynamic ? { dynamic: true as const } : {}),\n })),\n ],\n text,\n reasoning: reasoningParts.map(r => ({\n type: 'reasoning' as const,\n text: r.text,\n })),\n reasoningText,\n files: [],\n sources: [],\n toolCalls: toolCalls\n .filter(tc => !tc.invalid)\n .map(tc => ({\n type: 'tool-call' as const,\n toolCallId: tc.toolCallId,\n toolName: tc.toolName,\n input: tc.input,\n ...(tc.dynamic ? { dynamic: true as const } : {}),\n })),\n staticToolCalls: [],\n dynamicToolCalls: toolCalls\n .filter(tc => !tc.invalid && tc.dynamic)\n .map(tc => ({\n type: 'tool-call' as const,\n toolCallId: tc.toolCallId,\n toolName: tc.toolName,\n input: tc.input,\n dynamic: true as const,\n })),\n toolResults: [],\n staticToolResults: [],\n dynamicToolResults: [],\n finishReason: finish?.finishReason ?? 'other',\n rawFinishReason: finish?.rawFinishReason,\n usage:\n finish?.usage ??\n ({\n inputTokens: 0,\n inputTokenDetails: {\n noCacheTokens: undefined,\n cacheReadTokens: undefined,\n cacheWriteTokens: undefined,\n },\n outputTokens: 0,\n outputTokenDetails: {\n textTokens: undefined,\n reasoningTokens: undefined,\n },\n totalTokens: 0,\n } as LanguageModelUsage),\n warnings,\n request: { body: '' },\n response: {\n id: responseMetadata?.id ?? 'unknown',\n timestamp: responseMetadata?.timestamp ?? new Date(),\n modelId: responseMetadata?.modelId ?? 'unknown',\n messages: [],\n },\n providerMetadata: finish?.providerMetadata ?? {},\n } as StepResult<ToolSet, any>;\n\n return {\n toolCalls,\n finish,\n step,\n providerExecutedToolResults,\n };\n}\n","/**\n * Helpers for passing tool schemas across workflow step boundaries.\n *\n * Tool schemas (zod, valibot, arktype, etc.) contain functions that can't be\n * serialized by the workflow runtime. These helpers extract JSON Schema from\n * schemas, then reconstruct tools with Ajv validation inside step functions.\n *\n * Uses `asSchema()` from `@ai-sdk/provider-utils` for JSON Schema extraction,\n * which supports Standard Schema compatible libraries. When libraries adopt\n * `~standard.jsonSchema` (Standard Schema v2), extraction can be simplified\n * to use that interface directly.\n */\nimport type { JSONSchema7 } from '@ai-sdk/provider';\nimport { asSchema, jsonSchema } from '@ai-sdk/provider-utils';\nimport { tool, type ToolSet } from 'ai';\nimport Ajv from 'ajv';\n\n/**\n * Serializable tool definition — plain objects only, safe for workflow steps.\n */\nexport type SerializableToolDef = {\n description?: string;\n inputSchema: JSONSchema7;\n /** Present on provider tools (e.g. anthropic.tools.webSearch). */\n type?: 'provider';\n /** Provider tool ID, e.g. 'anthropic.web_search_20250305'. */\n id?: `${string}.${string}`;\n /** Provider tool configuration args (maxUses, allowedDomains, etc.). */\n args?: Record<string, unknown>;\n};\n\n/**\n * Converts a ToolSet (with zod/standard schemas and execute functions) to a\n * serializable record of tool definitions. Only description and inputSchema\n * (as JSON Schema) are preserved — execute functions are stripped since they\n * run outside the step.\n */\nexport function serializeToolSet(\n tools: ToolSet,\n): Record<string, SerializableToolDef> {\n return Object.fromEntries(\n Object.entries(tools).map(([name, t]) => {\n const def: SerializableToolDef = {\n description: t.description,\n inputSchema: asSchema(t.inputSchema).jsonSchema as JSONSchema7,\n };\n\n // Preserve provider tool identity so the Gateway can recognize\n // them as provider-executed tools (e.g. anthropic webSearch).\n if ((t as any).type === 'provider') {\n def.type = 'provider';\n def.id = (t as any).id;\n def.args = (t as any).args;\n }\n\n return [name, def];\n }),\n );\n}\n\n/**\n * Reconstructs tool objects from serializable tool definitions inside a step.\n *\n * Wraps each tool's JSON Schema with `jsonSchema()` and validates tool call\n * arguments against the schema using Ajv. This provides runtime type safety\n * equivalent to using zod schemas directly with the AI SDK.\n */\nexport function resolveSerializableTools(\n tools: Record<string, SerializableToolDef>,\n): ToolSet {\n const ajv = new Ajv();\n\n return Object.fromEntries(\n Object.entries(tools).map(([name, t]) => {\n // Provider tools are executed server-side — pass them through\n // with their identity intact, no client-side validation needed.\n if (t.type === 'provider') {\n return [\n name,\n tool({\n type: 'provider' as const,\n id: t.id!,\n args: t.args ?? {},\n inputSchema: jsonSchema(t.inputSchema),\n }),\n ];\n }\n\n const validateFn = ajv.compile(t.inputSchema);\n\n return [\n name,\n tool({\n description: t.description,\n inputSchema: jsonSchema(t.inputSchema, {\n validate: value => {\n if (validateFn(value)) {\n return { success: true, value: value as any };\n }\n return {\n success: false,\n error: new Error(ajv.errorsText(validateFn.errors)),\n };\n },\n }),\n }),\n ];\n }),\n );\n}\n","import { type ToolSet, type UIMessageChunk } from 'ai';\nimport type { Experimental_LanguageModelStreamPart as ModelCallStreamPart } from 'ai';\n\n/**\n * Convert a single ModelCallStreamPart to a UIMessageChunk.\n * Returns undefined for parts that don't map to UI chunks.\n */\nexport function toUIMessageChunk(\n part: ModelCallStreamPart<ToolSet>,\n): UIMessageChunk | undefined {\n switch (part.type) {\n case 'text-start':\n return {\n type: 'text-start',\n id: part.id,\n ...(part.providerMetadata != null\n ? { providerMetadata: part.providerMetadata }\n : {}),\n };\n\n case 'text-delta':\n return {\n type: 'text-delta',\n id: part.id,\n delta: part.text,\n ...(part.providerMetadata != null\n ? { providerMetadata: part.providerMetadata }\n : {}),\n };\n\n case 'text-end':\n return {\n type: 'text-end',\n id: part.id,\n ...(part.providerMetadata != null\n ? { providerMetadata: part.providerMetadata }\n : {}),\n };\n\n case 'reasoning-start':\n return {\n type: 'reasoning-start',\n id: part.id,\n ...(part.providerMetadata != null\n ? { providerMetadata: part.providerMetadata }\n : {}),\n };\n\n case 'reasoning-delta':\n return {\n type: 'reasoning-delta',\n id: part.id,\n delta: part.text,\n ...(part.providerMetadata != null\n ? { providerMetadata: part.providerMetadata }\n : {}),\n };\n\n case 'reasoning-end':\n return {\n type: 'reasoning-end',\n id: part.id,\n ...(part.providerMetadata != null\n ? { providerMetadata: part.providerMetadata }\n : {}),\n };\n\n case 'file': {\n const file = part.file;\n // GeneratedFile.base64 always has data (lazy-converted from Uint8Array if needed)\n return {\n type: 'file',\n mediaType: file.mediaType,\n url: `data:${file.mediaType};base64,${file.base64}`,\n };\n }\n\n case 'source': {\n if (part.sourceType === 'url') {\n return {\n type: 'source-url',\n sourceId: part.id,\n url: part.url,\n title: part.title,\n ...(part.providerMetadata != null\n ? { providerMetadata: part.providerMetadata }\n : {}),\n };\n }\n if (part.sourceType === 'document') {\n return {\n type: 'source-document',\n sourceId: part.id,\n mediaType: part.mediaType,\n title: part.title,\n filename: part.filename,\n ...(part.providerMetadata != null\n ? { providerMetadata: part.providerMetadata }\n : {}),\n };\n }\n return undefined;\n }\n\n case 'tool-input-start':\n return {\n type: 'tool-input-start',\n toolCallId: part.id,\n toolName: part.toolName,\n ...(part.providerExecuted != null\n ? { providerExecuted: part.providerExecuted }\n : {}),\n };\n\n case 'tool-input-delta':\n return {\n type: 'tool-input-delta',\n toolCallId: part.id,\n inputTextDelta: part.delta,\n };\n\n case 'tool-call': {\n // parseToolCall adds invalid/error at runtime for failed parses\n const toolCallPart = part as typeof part & {\n invalid?: boolean;\n error?: unknown;\n };\n if (toolCallPart.invalid) {\n return {\n type: 'tool-input-error',\n toolCallId: toolCallPart.toolCallId,\n toolName: toolCallPart.toolName,\n input: toolCallPart.input,\n errorText:\n toolCallPart.error instanceof Error\n ? toolCallPart.error.message\n : String(toolCallPart.error ?? 'Invalid tool call'),\n };\n }\n return {\n type: 'tool-input-available',\n toolCallId: part.toolCallId,\n toolName: part.toolName,\n input: part.input,\n ...(part.providerExecuted != null\n ? { providerExecuted: part.providerExecuted }\n : {}),\n ...(part.providerMetadata != null\n ? { providerMetadata: part.providerMetadata }\n : {}),\n };\n }\n\n case 'tool-result':\n return {\n type: 'tool-output-available',\n toolCallId: part.toolCallId,\n output: part.output,\n };\n\n case 'tool-error':\n return {\n type: 'tool-output-error',\n toolCallId: part.toolCallId,\n errorText:\n part.error instanceof Error ? part.error.message : String(part.error),\n };\n\n case 'error': {\n const error = part.error;\n return {\n type: 'error',\n errorText: error instanceof Error ? error.message : String(error),\n };\n }\n\n // These don't produce UI chunks\n case 'tool-input-end':\n case 'model-call-start':\n case 'model-call-response-metadata':\n case 'model-call-end':\n case 'raw':\n return undefined;\n\n default: {\n // Pass through tool-approval-request, step boundaries, and other\n // chunks as-is. Step boundaries (finish-step/start-step) are not\n // standard ModelCallStreamPart types but are written by the\n // WorkflowAgent between tool execution and the next model step\n // to ensure proper message splitting in convertToModelMessages.\n const p = part as any;\n if (p.type === 'tool-approval-request') {\n return {\n type: 'tool-approval-request',\n approvalId: p.approvalId,\n toolCallId: p.toolCallId,\n } as UIMessageChunk;\n }\n if (\n p.type === 'finish-step' ||\n p.type === 'start-step' ||\n p.type === 'tool-output-denied'\n ) {\n return p as UIMessageChunk;\n }\n return undefined;\n }\n }\n}\n\n/**\n * Create a TransformStream that converts ModelCallStreamPart to UIMessageChunk.\n * Wraps toUIMessageChunk with start/start-step/finish-step lifecycle chunks.\n */\nexport function createModelCallToUIChunkTransform(): TransformStream<\n ModelCallStreamPart<ToolSet>,\n UIMessageChunk\n> {\n return new TransformStream<ModelCallStreamPart<ToolSet>, UIMessageChunk>({\n start: controller => {\n controller.enqueue({ type: 'start' });\n controller.enqueue({ type: 'start-step' });\n },\n flush: controller => {\n controller.enqueue({ type: 'finish-step' });\n controller.enqueue({ type: 'finish' });\n },\n transform: (part, controller) => {\n const uiChunk = toUIMessageChunk(part);\n if (uiChunk) {\n controller.enqueue(uiChunk);\n }\n },\n });\n}\n","import {\n type ChatRequestOptions,\n type ChatTransport,\n type PrepareReconnectToStreamRequest,\n type PrepareSendMessagesRequest,\n parseJsonEventStream,\n type UIMessage,\n type UIMessageChunk,\n uiMessageChunkSchema,\n} from 'ai';\nimport {\n convertAsyncIteratorToReadableStream,\n getErrorMessage,\n} from '@ai-sdk/provider-utils';\nimport { createAsyncIterableStream } from 'ai/internal';\n\nexport interface SendMessagesOptions<UI_MESSAGE extends UIMessage> {\n trigger: 'submit-message' | 'regenerate-message';\n chatId: string;\n messageId?: string;\n messages: UI_MESSAGE[];\n abortSignal?: AbortSignal;\n}\n\nexport interface ReconnectToStreamOptions {\n chatId: string;\n abortSignal?: AbortSignal;\n /**\n * Override the `startIndex` for this reconnection.\n * Negative values read from the end of the stream.\n * When omitted, falls back to the constructor's `initialStartIndex`.\n */\n startIndex?: number;\n}\n\ntype OnChatSendMessage<UI_MESSAGE extends UIMessage> = (\n response: Response,\n options: SendMessagesOptions<UI_MESSAGE>,\n) => void | Promise<void>;\n\ntype OnChatEnd = ({\n chatId,\n chunkIndex,\n}: {\n chatId: string;\n chunkIndex: number;\n}) => void | Promise<void>;\n\n/**\n * Configuration options for the WorkflowChatTransport.\n *\n * @template UI_MESSAGE - The type of UI messages being sent and received,\n * must extend the UIMessage interface from the AI SDK.\n */\nexport interface WorkflowChatTransportOptions<UI_MESSAGE extends UIMessage> {\n /**\n * API endpoint for chat requests\n * Defaults to /api/chat if not provided\n */\n api?: string;\n\n /**\n * Custom fetch implementation to use for HTTP requests.\n * Defaults to the global fetch function if not provided.\n */\n fetch?: typeof fetch;\n\n /**\n * Callback invoked after successfully sending messages to the chat endpoint.\n * Useful for tracking chat history and inspecting response headers.\n *\n * @param response - The HTTP response object from the chat endpoint\n * @param options - The original options passed to sendMessages\n */\n onChatSendMessage?: OnChatSendMessage<UI_MESSAGE>;\n\n /**\n * Callback invoked when a chat stream ends (receives a \"finish\" chunk).\n * Useful for cleanup operations or state updates.\n *\n * @param chatId - The ID of the chat that ended\n * @param chunkIndex - The total number of chunks received\n */\n onChatEnd?: OnChatEnd;\n\n /**\n * Maximum number of consecutive errors allowed during reconnection attempts.\n * Defaults to 3 if not provided.\n */\n maxConsecutiveErrors?: number;\n\n /**\n * Default `startIndex` to use when reconnecting to a stream without a known\n * chunk position (i.e. the initial reconnection, not a retry).\n * Negative values read from the end of the stream (e.g. `-10` fetches the\n * last 10 chunks), which is useful for resuming a chat UI after a page\n * refresh without replaying the full conversation.\n *\n * Can be overridden per-call via `ReconnectToStreamOptions.startIndex`.\n *\n * Defaults to `0` (replay from the beginning).\n */\n initialStartIndex?: number;\n\n /**\n * Function to prepare the request for sending messages.\n * Allows customizing the API endpoint, headers, credentials, and body.\n */\n prepareSendMessagesRequest?: PrepareSendMessagesRequest<UI_MESSAGE>;\n\n /**\n * Function to prepare the request for reconnecting to a stream.\n * Allows customizing the API endpoint, headers, and credentials.\n */\n prepareReconnectToStreamRequest?: PrepareReconnectToStreamRequest;\n}\n\n/**\n * A transport implementation for managing chat workflows with support for\n * streaming responses and automatic reconnection to interrupted streams.\n *\n * This class implements the ChatTransport interface from the AI SDK and provides\n * reliable message streaming with automatic recovery from network interruptions\n * or function timeouts.\n *\n * @template UI_MESSAGE - The type of UI messages being sent and received,\n * must extend the UIMessage interface from the AI SDK.\n *\n * @implements {ChatTransport<UI_MESSAGE>}\n */\nexport class WorkflowChatTransport<\n UI_MESSAGE extends UIMessage,\n> implements ChatTransport<UI_MESSAGE> {\n private readonly api: string;\n private readonly fetch: typeof fetch;\n private readonly onChatSendMessage?: OnChatSendMessage<UI_MESSAGE>;\n private readonly onChatEnd?: OnChatEnd;\n private readonly maxConsecutiveErrors: number;\n private readonly initialStartIndex: number;\n private readonly prepareSendMessagesRequest?: PrepareSendMessagesRequest<UI_MESSAGE>;\n private readonly prepareReconnectToStreamRequest?: PrepareReconnectToStreamRequest;\n\n /**\n * Creates a new WorkflowChatTransport instance.\n *\n * @param options - Configuration options for the transport\n * @param options.api - API endpoint for chat requests (defaults to '/api/chat')\n * @param options.fetch - Custom fetch implementation (defaults to global fetch)\n * @param options.onChatSendMessage - Callback after sending messages\n * @param options.onChatEnd - Callback when chat stream ends\n * @param options.maxConsecutiveErrors - Maximum consecutive errors for reconnection\n * @param options.prepareSendMessagesRequest - Function to prepare send messages request\n * @param options.prepareReconnectToStreamRequest - Function to prepare reconnect request\n */\n constructor(options: WorkflowChatTransportOptions<UI_MESSAGE> = {}) {\n this.api = options.api ?? '/api/chat';\n this.fetch = options.fetch ?? fetch.bind(globalThis);\n this.onChatSendMessage = options.onChatSendMessage;\n this.onChatEnd = options.onChatEnd;\n this.maxConsecutiveErrors = options.maxConsecutiveErrors ?? 3;\n this.initialStartIndex = options.initialStartIndex ?? 0;\n this.prepareSendMessagesRequest = options.prepareSendMessagesRequest;\n this.prepareReconnectToStreamRequest =\n options.prepareReconnectToStreamRequest;\n }\n\n /**\n * Sends messages to the chat endpoint and returns a stream of response chunks.\n *\n * This method handles the entire chat lifecycle including:\n * - Sending messages to the /api/chat endpoint\n * - Streaming response chunks\n * - Automatic reconnection if the stream is interrupted\n *\n * @param options - Options for sending messages\n * @param options.trigger - The type of message submission ('submit-message' or 'regenerate-message')\n * @param options.chatId - Unique identifier for this chat session\n * @param options.messageId - Optional message ID for tracking specific messages\n * @param options.messages - Array of UI messages to send\n * @param options.abortSignal - Optional AbortSignal to cancel the request\n *\n * @returns A ReadableStream of UIMessageChunk objects containing the response\n * @throws Error if the fetch request fails or returns a non-OK status\n */\n async sendMessages(\n options: SendMessagesOptions<UI_MESSAGE> & ChatRequestOptions,\n ): Promise<ReadableStream<UIMessageChunk>> {\n return convertAsyncIteratorToReadableStream(\n this.sendMessagesIterator(options),\n );\n }\n\n private async *sendMessagesIterator(\n options: SendMessagesOptions<UI_MESSAGE> & ChatRequestOptions,\n ): AsyncGenerator<UIMessageChunk> {\n const { chatId, messages, abortSignal, trigger, messageId } = options;\n\n // We keep track of if the \"finish\" chunk is received to determine\n // if we need to reconnect, and keep track of the chunk index to resume from.\n let gotFinish = false;\n let chunkIndex = 0;\n\n // Prepare the request using the configurator if provided\n const requestConfig = this.prepareSendMessagesRequest\n ? await this.prepareSendMessagesRequest({\n id: chatId,\n messages,\n requestMetadata: options.metadata,\n body: options.body,\n credentials: undefined,\n headers: options.headers,\n api: this.api,\n trigger,\n messageId,\n })\n : undefined;\n\n const url = requestConfig?.api ?? this.api;\n const res = await this.fetch(url, {\n method: 'POST',\n body: JSON.stringify(\n requestConfig?.body ?? { messages, ...options.body },\n ),\n headers: requestConfig?.headers,\n credentials: requestConfig?.credentials,\n signal: abortSignal,\n });\n\n if (!res.ok || !res.body) {\n throw new Error(\n `Failed to fetch chat: ${res.status} ${await res.text()}`,\n );\n }\n\n const workflowRunId = res.headers.get('x-workflow-run-id');\n if (!workflowRunId) {\n throw new Error(\n 'Workflow run ID not found in \"x-workflow-run-id\" response header',\n );\n }\n\n // Notify the caller that the chat POST request was sent.\n // This is useful for tracking the chat history on the client\n // side and allows for inspecting response headers.\n await this.onChatSendMessage?.(res, options);\n\n // Flush the initial stream until the end or an error occurs\n try {\n const chunkStream = parseJsonEventStream({\n stream: res.body,\n schema: uiMessageChunkSchema,\n });\n for await (const chunk of createAsyncIterableStream(chunkStream)) {\n if (!chunk.success) {\n throw chunk.error;\n }\n\n chunkIndex++;\n\n yield chunk.value;\n\n if (chunk.value.type === 'finish') {\n gotFinish = true;\n }\n }\n } catch (error) {\n console.error('Error in chat POST stream', error);\n }\n\n if (gotFinish) {\n await this.onFinish(gotFinish, { chatId, chunkIndex });\n } else {\n // If the initial POST request did not include the \"finish\" chunk,\n // we need to reconnect to the stream. This could indicate that a\n // network error occurred or the Vercel Function timed out.\n yield* this.reconnectToStreamIterator(options, workflowRunId, chunkIndex);\n }\n }\n\n /**\n * Reconnects to an existing chat stream that was previously interrupted.\n *\n * This method is useful for resuming a chat session after network issues,\n * page refreshes, or Vercel Function timeouts.\n *\n * @param options - Options for reconnecting to the stream\n * @param options.chatId - The chat ID to reconnect to\n *\n * @returns A ReadableStream of UIMessageChunk objects\n * @throws Error if the reconnection request fails or returns a non-OK status\n */\n async reconnectToStream(\n options: ReconnectToStreamOptions & ChatRequestOptions,\n ): Promise<ReadableStream<UIMessageChunk> | null> {\n const it = this.reconnectToStreamIterator(options);\n return convertAsyncIteratorToReadableStream(it);\n }\n\n private async *reconnectToStreamIterator(\n options: ReconnectToStreamOptions & ChatRequestOptions,\n workflowRunId?: string,\n initialChunkIndex = 0,\n ): AsyncGenerator<UIMessageChunk> {\n let chunkIndex = initialChunkIndex;\n\n // When called from the public reconnectToStream (initialChunkIndex === 0),\n // honour the caller's startIndex (or the constructor default) for the\n // first request. This enables negative values so the client can read only\n // the tail of the stream (e.g. the last 10 chunks) instead of replaying\n // everything. After the first request, fall back to the running chunkIndex\n // so that retries resume from the correct position.\n const explicitStartIndex = options.startIndex ?? this.initialStartIndex;\n let useExplicitStartIndex =\n initialChunkIndex === 0 && explicitStartIndex !== 0;\n\n const defaultApi = `${this.api}/${encodeURIComponent(workflowRunId ?? options.chatId)}/stream`;\n\n // Prepare the request using the configurator if provided\n const requestConfig = this.prepareReconnectToStreamRequest\n ? await this.prepareReconnectToStreamRequest({\n id: options.chatId,\n requestMetadata: options.metadata,\n body: undefined,\n credentials: undefined,\n headers: undefined,\n api: defaultApi,\n })\n : undefined;\n\n const baseUrl = requestConfig?.api ?? defaultApi;\n\n let gotFinish = false;\n let consecutiveErrors = 0;\n // When a negative startIndex is used but the tail-index header is absent,\n // retries fall back to startIndex 0 (replay everything) instead of using\n // the incremental chunkIndex which would be wrong.\n let replayFromStart = false;\n\n while (!gotFinish) {\n const startIndex = useExplicitStartIndex\n ? explicitStartIndex\n : replayFromStart\n ? 0\n : chunkIndex;\n\n const url = `${baseUrl}?startIndex=${startIndex}`;\n const res = await this.fetch(url, {\n headers: requestConfig?.headers,\n credentials: requestConfig?.credentials,\n signal: options.abortSignal,\n });\n\n if (!res.ok || !res.body) {\n throw new Error(\n `Failed to fetch chat: ${res.status} ${await res.text()}`,\n );\n }\n\n // When using a negative startIndex, the server resolves it to an\n // absolute position. The reconnection endpoint should return the tail\n // index so we can compute the resolved position for subsequent retries.\n if (useExplicitStartIndex && explicitStartIndex > 0) {\n // Positive startIndex: the first request starts at this absolute\n // position, so set chunkIndex to match so subsequent retries\n // resume from (explicitStartIndex + chunks received).\n chunkIndex = explicitStartIndex;\n } else if (useExplicitStartIndex && explicitStartIndex < 0) {\n const tailIndexHeader = res.headers.get('x-workflow-stream-tail-index');\n const tailIndex =\n tailIndexHeader !== null ? parseInt(tailIndexHeader, 10) : NaN;\n\n if (!Number.isNaN(tailIndex)) {\n // Resolve: e.g. tailIndex=499, startIndex=-20 → 500 + (-20) = 480\n chunkIndex = Math.max(0, tailIndex + 1 + explicitStartIndex);\n } else {\n // Header missing or unparseable — fall back to replaying from the\n // beginning so retries don't resume from a wrong position.\n console.warn(\n '[WorkflowChatTransport] Negative initialStartIndex is configured ' +\n `(${explicitStartIndex}) but the reconnection endpoint did not ` +\n 'return a valid \"x-workflow-stream-tail-index\" header. Retries ' +\n 'will replay the stream from the beginning. See: ' +\n 'https://workflow.dev/docs/ai/resumable-streams#resuming-from-the-end-of-the-stream',\n );\n replayFromStart = true;\n }\n }\n useExplicitStartIndex = false;\n\n try {\n const chunkStream = parseJsonEventStream({\n stream: res.body,\n schema: uiMessageChunkSchema,\n });\n for await (const chunk of createAsyncIterableStream(chunkStream)) {\n if (!chunk.success) {\n throw chunk.error;\n }\n\n chunkIndex++;\n\n yield chunk.value;\n\n if (chunk.value.type === 'finish') {\n gotFinish = true;\n }\n }\n // Reset consecutive error count only after successful stream parsing\n consecutiveErrors = 0;\n } catch (error) {\n console.error('Error in chat GET reconnectToStream', error);\n consecutiveErrors++;\n\n if (consecutiveErrors >= this.maxConsecutiveErrors) {\n throw new Error(\n `Failed to reconnect after ${this.maxConsecutiveErrors} consecutive errors. Last error: ${getErrorMessage(error)}`,\n );\n }\n }\n }\n\n await this.onFinish(gotFinish, { chatId: options.chatId, chunkIndex });\n }\n\n private async onFinish(\n gotFinish: boolean,\n { chatId, chunkIndex }: { chatId: string; chunkIndex: number },\n ) {\n if (gotFinish) {\n await this.onChatEnd?.({ chatId, chunkIndex });\n } else {\n throw new Error('No finish chunk received');\n }\n }\n}\n"],"mappings":";AAQA;AAAA,EAOE;AAAA,EASA,kCAAkCA;AAAA,OAC7B;AACP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;;;AC1BP;AAAA,EAQE,kCAAkC;AAAA,OAC7B;;;ACVP;AAAA,EAEE,wCAAwC;AAAA,OAUnC;AACP,SAAS,eAAe;;;ACJxB,SAAS,UAAU,kBAAkB;AACrC,SAAS,YAA0B;AACnC,OAAO,SAAS;AAsBT,SAAS,iBACd,OACqC;AACrC,SAAO,OAAO;AAAA,IACZ,OAAO,QAAQ,KAAK,EAAE,IAAI,CAAC,CAAC,MAAM,CAAC,MAAM;AACvC,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AAQ;AAAA,YACN,qEACM,kBAAkB;AAAA,UAI1B;AACA,4BAAkB;AAAA,QACpB;AAAA,MACF;AACA,8BAAwB;AAExB,UAAI;AACF,cAAM,cAAc,qBAAqB;AAAA,UACvC,QAAQ,IAAI;AAAA,UACZ,QAAQ;AAAA,QACV,CAAC;AACD,yBAAiB,SAAS,0BAA0B,WAAW,GAAG;AAChE,cAAI,CAAC,MAAM,SAAS;AAClB,kBAAM,MAAM;AAAA,UACd;AAEA;AAEA,gBAAM,MAAM;AAEZ,cAAI,MAAM,MAAM,SAAS,UAAU;AACjC,wBAAY;AAAA,UACd;AAAA,QACF;AAEA,4BAAoB;AAAA,MACtB,SAAS,OAAO;AACd,gBAAQ,MAAM,uCAAuC,KAAK;AAC1D;AAEA,YAAI,qBAAqB,KAAK,sBAAsB;AAClD,gBAAM,IAAI;AAAA,YACR,6BAA6B,KAAK,oBAAoB,oCAAoCA,iBAAgB,KAAK,CAAC;AAAA,UAClH;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAEA,UAAM,KAAK,SAAS,WAAW,EAAE,QAAQ,QAAQ,QAAQ,WAAW,CAAC;AAAA,EACvE;AAAA,EAEA,MAAc,SACZ,WACA,EAAE,QAAQ,WAAW,GACrB;AA3aJ;AA4aI,QAAI,WAAW;AACb,cAAM,UAAK,cAAL,8BAAiB,EAAE,QAAQ,WAAW;AAAA,IAC9C,OAAO;AACL,YAAM,IAAI,MAAM,0BAA0B;AAAA,IAC5C;AAAA,EACF;AACF;","names":["filterActiveTools","tool","_a","filterActiveTools","messages","durationMs","getErrorMessage"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-sdk/workflow",
3
- "version": "1.0.0-beta.23",
3
+ "version": "1.0.0-beta.24",
4
4
  "description": "WorkflowAgent for building AI agents with AI SDK",
5
5
  "license": "Apache-2.0",
6
6
  "main": "./dist/index.js",
@@ -28,8 +28,8 @@
28
28
  "dependencies": {
29
29
  "ajv": "^8.18.0",
30
30
  "@ai-sdk/provider": "4.0.0-beta.12",
31
- "@ai-sdk/provider-utils": "5.0.0-beta.24",
32
- "ai": "7.0.0-beta.108"
31
+ "@ai-sdk/provider-utils": "5.0.0-beta.25",
32
+ "ai": "7.0.0-beta.109"
33
33
  },
34
34
  "devDependencies": {
35
35
  "@types/node": "20.17.24",
package/src/index.ts CHANGED
@@ -26,8 +26,8 @@ export {
26
26
  type ToolCallRepairFunction,
27
27
  type WorkflowAgentOnStartCallback,
28
28
  type WorkflowAgentOnStepStartCallback,
29
- type WorkflowAgentOnToolCallStartCallback,
30
- type WorkflowAgentOnToolCallFinishCallback,
29
+ type WorkflowAgentOnToolExecutionStartCallback,
30
+ type WorkflowAgentOnToolExecutionEndCallback,
31
31
  } from './workflow-agent.js';
32
32
 
33
33
  export {
@@ -334,10 +334,10 @@ export async function agentOnStepStartE2e() {
334
334
  }
335
335
 
336
336
  // ============================================================================
337
- // GAP tests — experimental_onToolCallStart
337
+ // GAP tests — experimental_onToolExecutionStart
338
338
  // ============================================================================
339
339
 
340
- export async function agentOnToolCallStartE2e() {
340
+ export async function agentonToolExecutionStartE2e() {
341
341
  'use workflow';
342
342
  const calls: string[] = [];
343
343
  const agent = new WorkflowAgent({
@@ -356,14 +356,14 @@ export async function agentOnToolCallStartE2e() {
356
356
  execute: echoStep,
357
357
  },
358
358
  },
359
- experimental_onToolCallStart: async () => {
359
+ experimental_onToolExecutionStart: async () => {
360
360
  calls.push('constructor');
361
361
  },
362
362
  } as any);
363
363
  await agent.stream({
364
364
  messages: [{ role: 'user', content: 'test' }],
365
365
  writable: getWritable(),
366
- experimental_onToolCallStart: async () => {
366
+ experimental_onToolExecutionStart: async () => {
367
367
  calls.push('method');
368
368
  },
369
369
  } as any);
@@ -371,10 +371,10 @@ export async function agentOnToolCallStartE2e() {
371
371
  }
372
372
 
373
373
  // ============================================================================
374
- // GAP tests — experimental_onToolCallFinish
374
+ // GAP tests — experimental_onToolExecutionEnd
375
375
  // ============================================================================
376
376
 
377
- export async function agentOnToolCallFinishE2e() {
377
+ export async function agentonToolExecutionEndE2e() {
378
378
  'use workflow';
379
379
  const calls: string[] = [];
380
380
  let capturedEvent: any = null;
@@ -394,14 +394,14 @@ export async function agentOnToolCallFinishE2e() {
394
394
  execute: addNumbers,
395
395
  },
396
396
  },
397
- experimental_onToolCallFinish: async () => {
397
+ experimental_onToolExecutionEnd: async () => {
398
398
  calls.push('constructor');
399
399
  },
400
400
  } as any);
401
401
  await agent.stream({
402
402
  messages: [{ role: 'user', content: 'test' }],
403
403
  writable: getWritable(),
404
- experimental_onToolCallFinish: async (event: any) => {
404
+ experimental_onToolExecutionEnd: async (event: any) => {
405
405
  calls.push('method');
406
406
  capturedEvent = {
407
407
  toolName: event?.toolCall?.toolName,
@@ -7,10 +7,11 @@ import type {
7
7
  SharedV4ProviderOptions,
8
8
  } from '@ai-sdk/provider';
9
9
  import {
10
- type Experimental_LanguageModelStreamPart as ModelCallStreamPart,
11
10
  type FinishReason,
11
+ LanguageModel,
12
12
  type LanguageModelResponseMetadata,
13
13
  type LanguageModelUsage,
14
+ type Experimental_LanguageModelStreamPart as ModelCallStreamPart,
14
15
  type ModelMessage,
15
16
  Output,
16
17
  type StepResult,
@@ -21,7 +22,6 @@ import {
21
22
  type ToolChoice,
22
23
  type ToolSet,
23
24
  type UIMessage,
24
- LanguageModel,
25
25
  experimental_filterActiveTools as filterActiveTools,
26
26
  } from 'ai';
27
27
  import {
@@ -31,7 +31,6 @@ import {
31
31
  standardizePrompt,
32
32
  } from 'ai/internal';
33
33
  import { streamTextIterator } from './stream-text-iterator.js';
34
- import type { CompatibleLanguageModel } from './types.js';
35
34
 
36
35
  // Re-export for consumers
37
36
  export type { CompatibleLanguageModel } from './types.js';
@@ -491,12 +490,12 @@ export interface WorkflowAgentOptions<
491
490
  /**
492
491
  * Callback called before a tool's execute function runs.
493
492
  */
494
- experimental_onToolCallStart?: WorkflowAgentOnToolCallStartCallback;
493
+ experimental_onToolExecutionStart?: WorkflowAgentOnToolExecutionStartCallback;
495
494
 
496
495
  /**
497
496
  * Callback called after a tool execution completes.
498
497
  */
499
- experimental_onToolCallFinish?: WorkflowAgentOnToolCallFinishCallback;
498
+ experimental_onToolExecutionEnd?: WorkflowAgentOnToolExecutionEndCallback;
500
499
 
501
500
  /**
502
501
  * Prepare the parameters for the stream call.
@@ -596,7 +595,7 @@ export type WorkflowAgentOnStepStartCallback<TTools extends ToolSet = ToolSet> =
596
595
  /**
597
596
  * Callback that is called before a tool's execute function runs.
598
597
  */
599
- export type WorkflowAgentOnToolCallStartCallback = (event: {
598
+ export type WorkflowAgentOnToolExecutionStartCallback = (event: {
600
599
  /** The tool call being executed */
601
600
  readonly toolCall: ToolCall;
602
601
  /** The current step number (0-based) */
@@ -608,7 +607,7 @@ export type WorkflowAgentOnToolCallStartCallback = (event: {
608
607
  * Uses a discriminated union pattern: check `success` to determine
609
608
  * whether `output` or `error` is available.
610
609
  */
611
- export type WorkflowAgentOnToolCallFinishCallback = (
610
+ export type WorkflowAgentOnToolExecutionEndCallback = (
612
611
  event:
613
612
  | {
614
613
  /** The tool call that was executed */
@@ -827,12 +826,12 @@ export type WorkflowAgentStreamOptions<
827
826
  /**
828
827
  * Callback called before a tool's execute function runs.
829
828
  */
830
- experimental_onToolCallStart?: WorkflowAgentOnToolCallStartCallback;
829
+ experimental_onToolExecutionStart?: WorkflowAgentOnToolExecutionStartCallback;
831
830
 
832
831
  /**
833
832
  * Callback called after a tool execution completes.
834
833
  */
835
- experimental_onToolCallFinish?: WorkflowAgentOnToolCallFinishCallback;
834
+ experimental_onToolExecutionEnd?: WorkflowAgentOnToolExecutionEndCallback;
836
835
 
837
836
  /**
838
837
  * Callback function called before each step in the agent loop.
@@ -1013,8 +1012,8 @@ export class WorkflowAgent<TBaseTools extends ToolSet = ToolSet> {
1013
1012
  private constructorOnFinish?: WorkflowAgentOnFinishCallback<ToolSet>;
1014
1013
  private constructorOnStart?: WorkflowAgentOnStartCallback;
1015
1014
  private constructorOnStepStart?: WorkflowAgentOnStepStartCallback;
1016
- private constructorOnToolCallStart?: WorkflowAgentOnToolCallStartCallback;
1017
- private constructorOnToolCallFinish?: WorkflowAgentOnToolCallFinishCallback;
1015
+ private constructorOnToolExecutionStart?: WorkflowAgentOnToolExecutionStartCallback;
1016
+ private constructorOnToolExecutionEnd?: WorkflowAgentOnToolExecutionEndCallback;
1018
1017
  private prepareCall?: PrepareCallCallback<TBaseTools>;
1019
1018
 
1020
1019
  constructor(options: WorkflowAgentOptions<TBaseTools>) {
@@ -1036,8 +1035,10 @@ export class WorkflowAgent<TBaseTools extends ToolSet = ToolSet> {
1036
1035
  this.constructorOnFinish = options.onFinish;
1037
1036
  this.constructorOnStart = options.experimental_onStart;
1038
1037
  this.constructorOnStepStart = options.experimental_onStepStart;
1039
- this.constructorOnToolCallStart = options.experimental_onToolCallStart;
1040
- this.constructorOnToolCallFinish = options.experimental_onToolCallFinish;
1038
+ this.constructorOnToolExecutionStart =
1039
+ options.experimental_onToolExecutionStart;
1040
+ this.constructorOnToolExecutionEnd =
1041
+ options.experimental_onToolExecutionEnd;
1041
1042
  this.prepareCall = options.prepareCall;
1042
1043
 
1043
1044
  // Extract generation settings
@@ -1335,13 +1336,13 @@ export class WorkflowAgent<TBaseTools extends ToolSet = ToolSet> {
1335
1336
  this.constructorOnStepStart,
1336
1337
  options.experimental_onStepStart,
1337
1338
  );
1338
- const mergedOnToolCallStart = mergeCallbacks(
1339
- this.constructorOnToolCallStart,
1340
- options.experimental_onToolCallStart,
1339
+ const mergedOnToolExecutionStart = mergeCallbacks(
1340
+ this.constructorOnToolExecutionStart,
1341
+ options.experimental_onToolExecutionStart,
1341
1342
  );
1342
- const mergedOnToolCallFinish = mergeCallbacks(
1343
- this.constructorOnToolCallFinish,
1344
- options.experimental_onToolCallFinish,
1343
+ const mergedOnToolExecutionEnd = mergeCallbacks(
1344
+ this.constructorOnToolExecutionEnd,
1345
+ options.experimental_onToolExecutionEnd,
1345
1346
  );
1346
1347
 
1347
1348
  // Determine effective tool choice
@@ -1377,7 +1378,7 @@ export class WorkflowAgent<TBaseTools extends ToolSet = ToolSet> {
1377
1378
  });
1378
1379
  }
1379
1380
 
1380
- // Helper to wrap executeTool with onToolCallStart/onToolCallFinish callbacks
1381
+ // Helper to wrap executeTool with onToolExecutionStart/onToolExecutionEnd callbacks
1381
1382
  const executeToolWithCallbacks = async (
1382
1383
  toolCall: { toolCallId: string; toolName: string; input: unknown },
1383
1384
  tools: ToolSet,
@@ -1392,8 +1393,8 @@ export class WorkflowAgent<TBaseTools extends ToolSet = ToolSet> {
1392
1393
  input: toolCall.input,
1393
1394
  };
1394
1395
 
1395
- if (mergedOnToolCallStart) {
1396
- await mergedOnToolCallStart({
1396
+ if (mergedOnToolExecutionStart) {
1397
+ await mergedOnToolExecutionStart({
1397
1398
  toolCall: toolCallEvent,
1398
1399
  stepNumber: currentStepNumber,
1399
1400
  });
@@ -1405,8 +1406,8 @@ export class WorkflowAgent<TBaseTools extends ToolSet = ToolSet> {
1405
1406
  result = await executeTool(toolCall, tools, messages, context);
1406
1407
  } catch (err) {
1407
1408
  const durationMs = Date.now() - startTime;
1408
- if (mergedOnToolCallFinish) {
1409
- await mergedOnToolCallFinish({
1409
+ if (mergedOnToolExecutionEnd) {
1410
+ await mergedOnToolExecutionEnd({
1410
1411
  toolCall: toolCallEvent,
1411
1412
  stepNumber: currentStepNumber,
1412
1413
  durationMs,
@@ -1418,14 +1419,14 @@ export class WorkflowAgent<TBaseTools extends ToolSet = ToolSet> {
1418
1419
  }
1419
1420
 
1420
1421
  const durationMs = Date.now() - startTime;
1421
- if (mergedOnToolCallFinish) {
1422
+ if (mergedOnToolExecutionEnd) {
1422
1423
  const isError =
1423
1424
  result.output &&
1424
1425
  'type' in result.output &&
1425
1426
  (result.output.type === 'error-text' ||
1426
1427
  result.output.type === 'error-json');
1427
1428
  if (isError) {
1428
- await mergedOnToolCallFinish({
1429
+ await mergedOnToolExecutionEnd({
1429
1430
  toolCall: toolCallEvent,
1430
1431
  stepNumber: currentStepNumber,
1431
1432
  durationMs,
@@ -1433,7 +1434,7 @@ export class WorkflowAgent<TBaseTools extends ToolSet = ToolSet> {
1433
1434
  error: 'value' in result.output ? result.output.value : undefined,
1434
1435
  });
1435
1436
  } else {
1436
- await mergedOnToolCallFinish({
1437
+ await mergedOnToolExecutionEnd({
1437
1438
  toolCall: toolCallEvent,
1438
1439
  stepNumber: currentStepNumber,
1439
1440
  durationMs,