@ai-sdk/workflow 1.0.0-beta.1 → 1.0.0-beta.100

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -1,5 +1,6 @@
1
- import { LanguageModelV4, SharedV4ProviderOptions, LanguageModelV4Prompt, LanguageModelV4CallOptions, LanguageModelV4StreamPart } from '@ai-sdk/provider';
2
- import { ToolSet, SystemModelMessage, ToolChoice, StepResult, StreamTextOnStepFinishCallback, ModelMessage, FinishReason, LanguageModelUsage, Experimental_LanguageModelStreamPart, StopCondition, LanguageModelResponseMetadata, ToolCallRepairFunction, UIMessage, UIMessageChunk, ChatTransport, PrepareSendMessagesRequest, PrepareReconnectToStreamRequest, ChatRequestOptions } from 'ai';
1
+ import { LanguageModelV4, SharedV4ProviderOptions, LanguageModelV4CallOptions, LanguageModelV4Prompt, LanguageModelV4StreamPart } from '@ai-sdk/provider';
2
+ import { Context, HasRequiredKey, InferToolSetContext } from '@ai-sdk/provider-utils';
3
+ import { ToolSet, LanguageModel, Instructions, ToolChoice, TelemetryOptions as TelemetryOptions$1, StopCondition, ActiveTools, LanguageModelResponseMetadata, LanguageModelUsage, FinishReason, ToolCallRepairFunction, StepResult, GenerateTextOnStepEndCallback, ModelMessage, Experimental_LanguageModelStreamPart, UIMessage, UIMessageChunk, ChatTransport, PrepareSendMessagesRequest, PrepareReconnectToStreamRequest, ChatRequestOptions } from 'ai';
3
4
  export { Experimental_LanguageModelStreamPart as ModelCallStreamPart, Output, ToolCallRepairFunction } from 'ai';
4
5
 
5
6
  /**
@@ -13,10 +14,23 @@ export { Experimental_LanguageModelStreamPart as ModelCallStreamPart, Output, To
13
14
  */
14
15
  type CompatibleLanguageModel = LanguageModelV4;
15
16
 
17
+ /**
18
+ * Callback function to be called after each step completes.
19
+ * Alias for the AI SDK's GenerateTextOnStepEndCallback, using
20
+ * WorkflowAgent-consistent naming.
21
+ */
22
+ type WorkflowAgentOnStepEndCallback<TTools extends ToolSet = ToolSet, TRuntimeContext extends Context = Context> = GenerateTextOnStepEndCallback<TTools, TRuntimeContext>;
23
+ /**
24
+ * Callback function to be called after each step completes.
25
+ * Deprecated alias for `WorkflowAgentOnStepEndCallback`.
26
+ *
27
+ * @deprecated Use `WorkflowAgentOnStepEndCallback` instead.
28
+ */
29
+ type WorkflowAgentOnStepFinishCallback<TTools extends ToolSet = ToolSet, TRuntimeContext extends Context = Context> = WorkflowAgentOnStepEndCallback<TTools, TRuntimeContext>;
16
30
  /**
17
31
  * Infer the type of the tools of a workflow agent.
18
32
  */
19
- type InferWorkflowAgentTools<WORKFLOW_AGENT> = WORKFLOW_AGENT extends WorkflowAgent<infer TOOLS> ? TOOLS : never;
33
+ type InferWorkflowAgentTools<WORKFLOW_AGENT> = WORKFLOW_AGENT extends WorkflowAgent<infer TOOLS, any> ? TOOLS : never;
20
34
  /**
21
35
  * Infer the UI message type of a workflow agent.
22
36
  */
@@ -46,41 +60,12 @@ interface OutputSpecification<OUTPUT, PARTIAL> {
46
60
  * Provider-specific options type. This is equivalent to SharedV4ProviderOptions from @ai-sdk/provider.
47
61
  */
48
62
  type ProviderOptions = SharedV4ProviderOptions;
49
- /**
50
- * Telemetry settings for observability.
51
- */
52
- interface TelemetrySettings {
53
- /**
54
- * Enable or disable telemetry. Defaults to true.
55
- */
56
- isEnabled?: boolean;
57
- /**
58
- * Identifier for this function. Used to group telemetry data by function.
59
- */
60
- functionId?: string;
61
- /**
62
- * Additional information to include in the telemetry data.
63
- */
64
- metadata?: Record<string, string | number | boolean | Array<string | number | boolean> | null | undefined>;
65
- /**
66
- * Enable or disable input recording. Enabled by default.
67
- *
68
- * You might want to disable input recording to avoid recording sensitive
69
- * information, to reduce data transfers, or to increase performance.
70
- */
71
- recordInputs?: boolean;
72
- /**
73
- * Enable or disable output recording. Enabled by default.
74
- *
75
- * You might want to disable output recording to avoid recording sensitive
76
- * information, to reduce data transfers, or to increase performance.
77
- */
78
- recordOutputs?: boolean;
79
- /**
80
- * Custom tracer for the telemetry.
81
- */
82
- tracer?: unknown;
83
- }
63
+ type WorkflowAgentToolsContextParameter<TTools extends ToolSet> = HasRequiredKey<InferToolSetContext<TTools>> extends true ? {
64
+ toolsContext: InferToolSetContext<TTools>;
65
+ } : {
66
+ toolsContext?: never;
67
+ };
68
+ type TelemetryOptions<TRuntimeContext extends Context = Context, TTools extends ToolSet = ToolSet> = TelemetryOptions$1<TRuntimeContext, TTools>;
84
69
  /**
85
70
  * A transformation that is applied to the stream.
86
71
  */
@@ -176,12 +161,12 @@ interface GenerationSettings {
176
161
  /**
177
162
  * Information passed to the prepareStep callback.
178
163
  */
179
- interface PrepareStepInfo<TTools extends ToolSet = ToolSet> {
164
+ interface PrepareStepInfo<TTools extends ToolSet = ToolSet, TRuntimeContext extends Context = Context> {
180
165
  /**
181
166
  * The current model configuration (string or function).
182
167
  * The function should return a LanguageModelV4 instance.
183
168
  */
184
- model: string | CompatibleLanguageModel | (() => Promise<CompatibleLanguageModel>);
169
+ model: LanguageModel;
185
170
  /**
186
171
  * The current step number (0-indexed).
187
172
  */
@@ -189,27 +174,35 @@ interface PrepareStepInfo<TTools extends ToolSet = ToolSet> {
189
174
  /**
190
175
  * All previous steps with their results.
191
176
  */
192
- steps: StepResult<TTools, any>[];
177
+ steps: StepResult<TTools, TRuntimeContext>[];
193
178
  /**
194
179
  * The messages that will be sent to the model.
195
180
  * This is the LanguageModelV4Prompt format used internally.
196
181
  */
197
182
  messages: LanguageModelV4Prompt;
198
183
  /**
199
- * The context passed via the experimental_context setting (experimental).
184
+ * The runtime context that flows through the agent loop.
185
+ * Treat the value as immutable; return a new `runtimeContext` from
186
+ * `prepareStep` to update it for the current and subsequent steps.
200
187
  */
201
- experimental_context: unknown;
188
+ runtimeContext: TRuntimeContext;
189
+ /**
190
+ * Per-tool context, keyed by tool name. Each tool receives only its own
191
+ * validated entry as `context` during execution.
192
+ * Treat the value as immutable; return a new `toolsContext` from
193
+ * `prepareStep` to update it for the current and subsequent steps.
194
+ */
195
+ toolsContext: InferToolSetContext<TTools>;
202
196
  }
203
197
  /**
204
198
  * Return type from the prepareStep callback.
205
199
  * All properties are optional - only return the ones you want to override.
206
200
  */
207
- interface PrepareStepResult extends Partial<GenerationSettings> {
201
+ interface PrepareStepResult<TTools extends ToolSet = ToolSet, TRuntimeContext extends Context = Context> extends Partial<GenerationSettings> {
208
202
  /**
209
203
  * Override the model for this step.
210
- * The function should return a LanguageModelV4 instance.
211
204
  */
212
- model?: string | CompatibleLanguageModel | (() => Promise<CompatibleLanguageModel>);
205
+ model?: LanguageModel;
213
206
  /**
214
207
  * Override the system message for this step.
215
208
  */
@@ -229,26 +222,39 @@ interface PrepareStepResult extends Partial<GenerationSettings> {
229
222
  */
230
223
  activeTools?: string[];
231
224
  /**
232
- * Context that is passed into tool execution. Experimental.
233
- * Changing the context will affect the context in this step and all subsequent steps.
225
+ * Updated runtime context for the current and subsequent steps.
226
+ * Returning a value replaces the agent's runtime context.
234
227
  */
235
- experimental_context?: unknown;
228
+ runtimeContext?: TRuntimeContext;
229
+ /**
230
+ * Updated per-tool context for the current and subsequent steps.
231
+ * Returning a value replaces the agent's tools context.
232
+ */
233
+ toolsContext?: InferToolSetContext<TTools>;
236
234
  }
237
235
  /**
238
236
  * Callback function called before each step in the agent loop.
239
237
  * Use this to modify settings, manage context, or implement dynamic behavior.
240
238
  */
241
- type PrepareStepCallback<TTools extends ToolSet = ToolSet> = (info: PrepareStepInfo<TTools>) => PrepareStepResult | Promise<PrepareStepResult>;
239
+ type PrepareStepCallback<TTools extends ToolSet = ToolSet, TRuntimeContext extends Context = Context> = (info: PrepareStepInfo<TTools, TRuntimeContext>) => PrepareStepResult<TTools, TRuntimeContext> | undefined | Promise<PrepareStepResult<TTools, TRuntimeContext> | undefined>;
242
240
  /**
243
241
  * Options passed to the prepareCall callback.
244
242
  */
245
- interface PrepareCallOptions<TTools extends ToolSet = ToolSet> extends Partial<GenerationSettings> {
246
- model: string | CompatibleLanguageModel | (() => Promise<CompatibleLanguageModel>);
243
+ interface PrepareCallOptions<TTools extends ToolSet = ToolSet, TRuntimeContext extends Context = Context> extends Partial<GenerationSettings> {
244
+ model: LanguageModel;
247
245
  tools: TTools;
248
- instructions?: string | SystemModelMessage | Array<SystemModelMessage>;
246
+ instructions?: Instructions;
249
247
  toolChoice?: ToolChoice<TTools>;
250
- experimental_telemetry?: TelemetrySettings;
251
- experimental_context?: unknown;
248
+ telemetry?: TelemetryOptions<TRuntimeContext, TTools>;
249
+ /**
250
+ * Runtime context that flows through the agent loop.
251
+ * Treat as immutable; return a new `runtimeContext` to update it for the call.
252
+ */
253
+ runtimeContext?: TRuntimeContext;
254
+ /**
255
+ * Per-tool context, keyed by tool name.
256
+ */
257
+ toolsContext?: InferToolSetContext<TTools>;
252
258
  messages: ModelMessage[];
253
259
  }
254
260
  /**
@@ -257,22 +263,26 @@ interface PrepareCallOptions<TTools extends ToolSet = ToolSet> extends Partial<G
257
263
  * Note: `tools` cannot be overridden via prepareCall because they are
258
264
  * bound at construction time for type safety.
259
265
  */
260
- type PrepareCallResult<TTools extends ToolSet = ToolSet> = Partial<Omit<PrepareCallOptions<TTools>, 'tools'>>;
266
+ type PrepareCallResult<TTools extends ToolSet = ToolSet, TRuntimeContext extends Context = Context> = Partial<Omit<PrepareCallOptions<TTools, TRuntimeContext>, 'tools'>>;
261
267
  /**
262
268
  * Callback called once before the agent loop starts to transform call parameters.
263
269
  */
264
- type PrepareCallCallback<TTools extends ToolSet = ToolSet> = (options: PrepareCallOptions<TTools>) => PrepareCallResult<TTools> | Promise<PrepareCallResult<TTools>>;
270
+ type PrepareCallCallback<TTools extends ToolSet = ToolSet, TRuntimeContext extends Context = Context> = (options: PrepareCallOptions<TTools, TRuntimeContext>) => PrepareCallResult<TTools, TRuntimeContext> | Promise<PrepareCallResult<TTools, TRuntimeContext>>;
265
271
  /**
266
272
  * Configuration options for creating a {@link WorkflowAgent} instance.
267
273
  */
268
- interface WorkflowAgentOptions<TTools extends ToolSet = ToolSet> extends GenerationSettings {
274
+ type WorkflowAgentOptions<TTools extends ToolSet = ToolSet, TRuntimeContext extends Context = Context> = GenerationSettings & WorkflowAgentToolsContextParameter<TTools> & {
275
+ /**
276
+ * The id of the agent.
277
+ */
278
+ id?: string;
269
279
  /**
270
280
  * The model provider to use for the agent.
271
281
  *
272
282
  * This should be a string compatible with the Vercel AI Gateway (e.g., 'anthropic/claude-opus'),
273
- * or a step function that returns a LanguageModelV4 instance.
283
+ * or a LanguageModelV4 instance from a provider.
274
284
  */
275
- model: string | CompatibleLanguageModel | (() => Promise<CompatibleLanguageModel>);
285
+ model: LanguageModel;
276
286
  /**
277
287
  * A set of tools available to the agent.
278
288
  * Tools can be implemented as workflow steps for automatic retries and persistence,
@@ -283,7 +293,7 @@ interface WorkflowAgentOptions<TTools extends ToolSet = ToolSet> extends Generat
283
293
  * Agent instructions. Can be a string, a SystemModelMessage, or an array of SystemModelMessages.
284
294
  * Supports provider-specific options (e.g., caching) when using the SystemModelMessage form.
285
295
  */
286
- instructions?: string | SystemModelMessage | Array<SystemModelMessage>;
296
+ instructions?: Instructions;
287
297
  /**
288
298
  * Optional system prompt to guide the agent's behavior.
289
299
  * @deprecated Use `instructions` instead.
@@ -294,17 +304,56 @@ interface WorkflowAgentOptions<TTools extends ToolSet = ToolSet> extends Generat
294
304
  */
295
305
  toolChoice?: ToolChoice<TTools>;
296
306
  /**
297
- * Optional telemetry configuration (experimental).
307
+ * Optional telemetry configuration.
298
308
  */
299
- experimental_telemetry?: TelemetrySettings;
309
+ telemetry?: TelemetryOptions<TRuntimeContext, TTools>;
300
310
  /**
301
- * Default context that is passed into tool execution for every stream call on this agent.
311
+ * Default runtime context for every stream call on this agent.
302
312
  *
303
- * Per-stream `experimental_context` values passed to `stream()` override this default.
304
- * Experimental (can break in patch releases).
305
- * @default undefined
313
+ * The runtime context flows through `prepareStep`, lifecycle callbacks,
314
+ * and step results.
315
+ * Treat as immutable; return a new `runtimeContext` from `prepareStep`
316
+ * to update it between steps.
317
+ *
318
+ * In workflow context, keep values serializable so they can cross workflow
319
+ * and step boundaries.
320
+ *
321
+ * Per-stream `runtimeContext` values passed to `stream()` override this default.
306
322
  */
307
- experimental_context?: unknown;
323
+ runtimeContext?: TRuntimeContext;
324
+ /**
325
+ * Default stop condition for the agent loop. When the condition is an array,
326
+ * any of the conditions can be met to stop the generation.
327
+ *
328
+ * Per-stream `stopWhen` values passed to `stream()` override this default.
329
+ */
330
+ stopWhen?: StopCondition<NoInfer<ToolSet>, any> | Array<StopCondition<NoInfer<ToolSet>, any>>;
331
+ /**
332
+ * Default set of active tools that limits which tools the model can call,
333
+ * without changing the tool call and result types in the result.
334
+ *
335
+ * Per-stream `activeTools` values passed to `stream()` override this default.
336
+ */
337
+ activeTools?: ActiveTools<NoInfer<TTools>>;
338
+ /**
339
+ * Default output specification for structured outputs.
340
+ * Use `Output.object({ schema })` for structured output or `Output.text()` for text output.
341
+ *
342
+ * Per-stream `output` values passed to `stream()` override this default.
343
+ */
344
+ output?: OutputSpecification<any, any>;
345
+ /**
346
+ * Default function that attempts to repair a tool call that failed to parse.
347
+ *
348
+ * Per-stream `experimental_repairToolCall` values passed to `stream()` override this default.
349
+ */
350
+ experimental_repairToolCall?: ToolCallRepairFunction<TTools>;
351
+ /**
352
+ * Default custom download function to use for URLs.
353
+ *
354
+ * Per-stream `experimental_download` values passed to `stream()` override this default.
355
+ */
356
+ experimental_download?: DownloadFunction;
308
357
  /**
309
358
  * Default callback function called before each step in the agent loop.
310
359
  * Use this to modify settings, manage context, or inject messages dynamically
@@ -312,46 +361,58 @@ interface WorkflowAgentOptions<TTools extends ToolSet = ToolSet> extends Generat
312
361
  *
313
362
  * Per-stream `prepareStep` values passed to `stream()` override this default.
314
363
  */
315
- prepareStep?: PrepareStepCallback<TTools>;
364
+ prepareStep?: PrepareStepCallback<TTools, TRuntimeContext>;
316
365
  /**
317
366
  * Callback function to be called after each step completes.
318
367
  */
319
- onStepFinish?: StreamTextOnStepFinishCallback<ToolSet, any>;
368
+ onStepEnd?: WorkflowAgentOnStepEndCallback<TTools, TRuntimeContext>;
369
+ /**
370
+ * Callback function to be called after each step completes.
371
+ *
372
+ * @deprecated Use `onStepEnd` instead.
373
+ */
374
+ onStepFinish?: WorkflowAgentOnStepFinishCallback<TTools, TRuntimeContext>;
320
375
  /**
321
376
  * Callback that is called when the LLM response and all request tool executions are finished.
322
377
  */
323
- onFinish?: StreamTextOnFinishCallback<ToolSet>;
378
+ onEnd?: WorkflowAgentOnEndCallback<TTools, TRuntimeContext>;
379
+ /**
380
+ * Callback that is called when the LLM response and all request tool executions are finished.
381
+ *
382
+ * @deprecated Use `onEnd` instead.
383
+ */
384
+ onFinish?: WorkflowAgentOnFinishCallback<TTools, TRuntimeContext>;
324
385
  /**
325
386
  * Callback called when the agent starts streaming, before any LLM calls.
326
387
  */
327
- experimental_onStart?: WorkflowAgentOnStartCallback;
388
+ experimental_onStart?: WorkflowAgentOnStartCallback<TTools, TRuntimeContext>;
328
389
  /**
329
390
  * Callback called before each step (LLM call) begins.
330
391
  */
331
- experimental_onStepStart?: WorkflowAgentOnStepStartCallback;
392
+ experimental_onStepStart?: WorkflowAgentOnStepStartCallback<TTools, TRuntimeContext>;
332
393
  /**
333
394
  * Callback called before a tool's execute function runs.
334
395
  */
335
- experimental_onToolCallStart?: WorkflowAgentOnToolCallStartCallback;
396
+ onToolExecutionStart?: WorkflowAgentOnToolExecutionStartCallback<TTools>;
336
397
  /**
337
398
  * Callback called after a tool execution completes.
338
399
  */
339
- experimental_onToolCallFinish?: WorkflowAgentOnToolCallFinishCallback;
400
+ onToolExecutionEnd?: WorkflowAgentOnToolExecutionEndCallback<TTools>;
340
401
  /**
341
402
  * Prepare the parameters for the stream call.
342
403
  * Called once before the agent loop starts. Use this to transform
343
404
  * model, tools, instructions, or other settings based on runtime context.
344
405
  */
345
- prepareCall?: PrepareCallCallback<TTools>;
346
- }
406
+ prepareCall?: PrepareCallCallback<TTools, TRuntimeContext>;
407
+ };
347
408
  /**
348
409
  * Callback that is called when the LLM response and all request tool executions are finished.
349
410
  */
350
- type StreamTextOnFinishCallback<TTools extends ToolSet = ToolSet, OUTPUT = never> = (event: {
411
+ type WorkflowAgentOnEndCallback<TTools extends ToolSet = ToolSet, TRuntimeContext extends Context = Context, OUTPUT = never> = (event: {
351
412
  /**
352
413
  * Details for all steps.
353
414
  */
354
- readonly steps: StepResult<TTools, any>[];
415
+ readonly steps: StepResult<TTools, TRuntimeContext>[];
355
416
  /**
356
417
  * The final messages including all tool calls and results.
357
418
  */
@@ -364,30 +425,44 @@ type StreamTextOnFinishCallback<TTools extends ToolSet = ToolSet, OUTPUT = never
364
425
  * The finish reason from the last step.
365
426
  */
366
427
  readonly finishReason: FinishReason;
428
+ /**
429
+ * The total token usage across all steps.
430
+ */
431
+ readonly usage: LanguageModelUsage;
367
432
  /**
368
433
  * The total token usage across all steps.
369
434
  */
370
435
  readonly totalUsage: LanguageModelUsage;
371
436
  /**
372
- * Context that is passed into tool execution.
437
+ * The runtime context at the end of the agent loop.
438
+ */
439
+ readonly runtimeContext: TRuntimeContext;
440
+ /**
441
+ * The per-tool context at the end of the agent loop.
373
442
  */
374
- readonly experimental_context: unknown;
443
+ readonly toolsContext: InferToolSetContext<TTools>;
375
444
  /**
376
445
  * The generated structured output. It uses the `output` specification.
377
446
  * Only available when `output` is specified.
378
447
  */
379
448
  readonly output: OUTPUT;
380
449
  }) => PromiseLike<void> | void;
450
+ /**
451
+ * Callback that is called when the LLM response and all request tool executions are finished.
452
+ *
453
+ * @deprecated Use `WorkflowAgentOnEndCallback` instead.
454
+ */
455
+ type WorkflowAgentOnFinishCallback<TTools extends ToolSet = ToolSet, TRuntimeContext extends Context = Context, OUTPUT = never> = WorkflowAgentOnEndCallback<TTools, TRuntimeContext, OUTPUT>;
381
456
  /**
382
457
  * Callback that is invoked when an error occurs during streaming.
383
458
  */
384
- type StreamTextOnErrorCallback = (event: {
459
+ type WorkflowAgentOnErrorCallback = (event: {
385
460
  error: unknown;
386
461
  }) => PromiseLike<void> | void;
387
462
  /**
388
463
  * Callback that is set using the `onAbort` option.
389
464
  */
390
- type StreamTextOnAbortCallback<TTools extends ToolSet = ToolSet> = (event: {
465
+ type WorkflowAgentOnAbortCallback<TTools extends ToolSet = ToolSet> = (event: {
391
466
  /**
392
467
  * Details for all previously finished steps.
393
468
  */
@@ -396,49 +471,114 @@ type StreamTextOnAbortCallback<TTools extends ToolSet = ToolSet> = (event: {
396
471
  /**
397
472
  * Callback that is called when the agent starts streaming, before any LLM calls.
398
473
  */
399
- type WorkflowAgentOnStartCallback = (event: {
474
+ type WorkflowAgentOnStartCallback<TTools extends ToolSet = ToolSet, TRuntimeContext extends Context = Context> = (event: {
400
475
  /** The model being used */
401
- readonly model: string | CompatibleLanguageModel | (() => Promise<CompatibleLanguageModel>);
476
+ readonly model: LanguageModel;
402
477
  /** The messages being sent */
403
478
  readonly messages: ModelMessage[];
479
+ /** Shared runtime context for this agent loop */
480
+ readonly runtimeContext: TRuntimeContext;
481
+ /** Per-tool context map for this agent loop */
482
+ readonly toolsContext: InferToolSetContext<TTools>;
404
483
  }) => PromiseLike<void> | void;
405
484
  /**
406
485
  * Callback that is called before each step (LLM call) begins.
407
486
  */
408
- type WorkflowAgentOnStepStartCallback = (event: {
487
+ type WorkflowAgentOnStepStartCallback<TTools extends ToolSet = ToolSet, TRuntimeContext extends Context = Context> = (event: {
409
488
  /** The current step number (0-based) */
410
489
  readonly stepNumber: number;
411
490
  /** The model being used for this step */
412
- readonly model: string | CompatibleLanguageModel | (() => Promise<CompatibleLanguageModel>);
491
+ readonly model: LanguageModel;
413
492
  /** The messages being sent for this step */
414
493
  readonly messages: ModelMessage[];
494
+ /** Results from all previously finished steps */
495
+ readonly steps: ReadonlyArray<StepResult<TTools, TRuntimeContext>>;
496
+ /** Shared runtime context for this step */
497
+ readonly runtimeContext: TRuntimeContext;
498
+ /** Per-tool context map for this step */
499
+ readonly toolsContext: InferToolSetContext<TTools>;
415
500
  }) => PromiseLike<void> | void;
416
501
  /**
417
502
  * Callback that is called before a tool's execute function runs.
418
503
  */
419
- type WorkflowAgentOnToolCallStartCallback = (event: {
504
+ type WorkflowAgentOnToolExecutionStartCallback<TTools extends ToolSet = ToolSet> = (event: {
420
505
  /** The tool call being executed */
421
506
  readonly toolCall: ToolCall;
507
+ /** The current step number (0-based) */
508
+ readonly stepNumber: number;
509
+ /** Messages sent to the language model for the step that produced the call */
510
+ readonly messages: ModelMessage[];
511
+ /** Tool-specific context passed to the tool */
512
+ readonly toolContext: InferToolSetContext<TTools>[keyof InferToolSetContext<TTools>] | undefined;
422
513
  }) => PromiseLike<void> | void;
423
514
  /**
424
515
  * Callback that is called after a tool execution completes.
516
+ * Uses a discriminated union pattern: check `success` to determine
517
+ * whether `output` or `error` is available.
425
518
  */
426
- type WorkflowAgentOnToolCallFinishCallback = (event: {
519
+ type WorkflowAgentOnToolExecutionEndCallback<TTools extends ToolSet = ToolSet> = (event: {
520
+ /** The tool call that was executed */
521
+ readonly toolCall: ToolCall;
522
+ /** The current step number (0-based) */
523
+ readonly stepNumber: number;
524
+ /** Execution time in milliseconds */
525
+ readonly durationMs: number;
526
+ /** Messages sent to the language model for the step that produced the call */
527
+ readonly messages: ModelMessage[];
528
+ /** Tool-specific context passed to the tool */
529
+ readonly toolContext: InferToolSetContext<TTools>[keyof InferToolSetContext<TTools>] | undefined;
530
+ /** Whether the tool call succeeded */
531
+ readonly success: true;
532
+ /** The tool result */
533
+ readonly output: unknown;
534
+ readonly error?: never;
535
+ } | {
427
536
  /** The tool call that was executed */
428
537
  readonly toolCall: ToolCall;
429
- /** The tool result (undefined if execution failed) */
430
- readonly result?: unknown;
431
- /** The error if execution failed */
432
- readonly error?: unknown;
538
+ /** The current step number (0-based) */
539
+ readonly stepNumber: number;
540
+ /** Execution time in milliseconds */
541
+ readonly durationMs: number;
542
+ /** Messages sent to the language model for the step that produced the call */
543
+ readonly messages: ModelMessage[];
544
+ /** Tool-specific context passed to the tool */
545
+ readonly toolContext: InferToolSetContext<TTools>[keyof InferToolSetContext<TTools>] | undefined;
546
+ /** Whether the tool call succeeded */
547
+ readonly success: false;
548
+ /** The error that occurred */
549
+ readonly error: unknown;
550
+ readonly output?: never;
433
551
  }) => PromiseLike<void> | void;
434
552
  /**
435
553
  * Options for the {@link WorkflowAgent.stream} method.
436
554
  */
437
- interface WorkflowAgentStreamOptions<TTools extends ToolSet = ToolSet, OUTPUT = never, PARTIAL_OUTPUT = never> extends Partial<GenerationSettings> {
555
+ type WorkflowAgentStreamOptions<TTools extends ToolSet = ToolSet, TRuntimeContext extends Context = Context, OUTPUT = never, PARTIAL_OUTPUT = never> = Partial<GenerationSettings> & ({
556
+ /**
557
+ * A prompt. It can be either a text prompt or a list of messages.
558
+ *
559
+ * You can either use `prompt` or `messages` but not both.
560
+ */
561
+ prompt: string | Array<ModelMessage>;
562
+ /**
563
+ * A list of messages.
564
+ *
565
+ * You can either use `prompt` or `messages` but not both.
566
+ */
567
+ messages?: never;
568
+ } | {
438
569
  /**
439
570
  * The conversation messages to process. Should follow the AI SDK's ModelMessage format.
571
+ *
572
+ * You can either use `prompt` or `messages` but not both.
440
573
  */
441
- messages: ModelMessage[];
574
+ messages: Array<ModelMessage>;
575
+ /**
576
+ * A prompt. It can be either a text prompt or a list of messages.
577
+ *
578
+ * You can either use `prompt` or `messages` but not both.
579
+ */
580
+ prompt?: never;
581
+ }) & {
442
582
  /**
443
583
  * Optional system prompt override. If provided, overrides the system prompt from the constructor.
444
584
  */
@@ -471,12 +611,6 @@ interface WorkflowAgentStreamOptions<TTools extends ToolSet = ToolSet, OUTPUT =
471
611
  * When the condition is an array, any of the conditions can be met to stop the generation.
472
612
  */
473
613
  stopWhen?: StopCondition<NoInfer<ToolSet>, any> | Array<StopCondition<NoInfer<ToolSet>, any>>;
474
- /**
475
- * Maximum number of sequential LLM calls (steps), e.g. when you use tool calls.
476
- * A maximum number can be set to prevent infinite loops in the case of misconfigured tools.
477
- * By default, it's unlimited (the agent loops until completion).
478
- */
479
- maxSteps?: number;
480
614
  /**
481
615
  * The tool choice strategy. Default: 'auto'.
482
616
  * Overrides the toolChoice from the constructor if provided.
@@ -486,17 +620,34 @@ interface WorkflowAgentStreamOptions<TTools extends ToolSet = ToolSet, OUTPUT =
486
620
  * Limits the tools that are available for the model to call without
487
621
  * changing the tool call and result types in the result.
488
622
  */
489
- activeTools?: Array<keyof NoInfer<TTools>>;
623
+ activeTools?: ActiveTools<NoInfer<TTools>>;
490
624
  /**
491
- * Optional telemetry configuration (experimental).
625
+ * Optional telemetry configuration.
492
626
  */
493
- experimental_telemetry?: TelemetrySettings;
627
+ telemetry?: TelemetryOptions<TRuntimeContext, TTools>;
494
628
  /**
495
- * Context that is passed into tool execution.
496
- * Experimental (can break in patch releases).
497
- * @default undefined
629
+ * Runtime context that flows through the agent loop.
630
+ *
631
+ * Treat as immutable; return a new `runtimeContext` from `prepareStep`
632
+ * to update it between steps.
633
+ *
634
+ * In workflow context, keep values serializable so they can cross workflow
635
+ * and step boundaries.
636
+ *
637
+ * Overrides the constructor-level `runtimeContext` if provided.
498
638
  */
499
- experimental_context?: unknown;
639
+ runtimeContext?: TRuntimeContext;
640
+ /**
641
+ * Per-tool context, keyed by tool name. Each tool receives only its own
642
+ * validated entry as `context` during execution. Tools that declare a
643
+ * `contextSchema` validate their entry against the schema.
644
+ *
645
+ * In workflow context, keep values serializable so they can cross workflow
646
+ * and step boundaries.
647
+ *
648
+ * Overrides the constructor-level `toolsContext` if provided.
649
+ */
650
+ toolsContext?: InferToolSetContext<TTools>;
500
651
  /**
501
652
  * Optional specification for parsing structured outputs from the LLM response.
502
653
  * Use `Output.object({ schema })` for structured output or `Output.text()` for text output.
@@ -546,37 +697,50 @@ interface WorkflowAgentStreamOptions<TTools extends ToolSet = ToolSet, OUTPUT =
546
697
  /**
547
698
  * Callback function to be called after each step completes.
548
699
  */
549
- onStepFinish?: StreamTextOnStepFinishCallback<TTools, any>;
700
+ onStepEnd?: WorkflowAgentOnStepEndCallback<TTools, TRuntimeContext>;
701
+ /**
702
+ * Callback function to be called after each step completes.
703
+ *
704
+ * @deprecated Use `onStepEnd` instead.
705
+ */
706
+ onStepFinish?: WorkflowAgentOnStepFinishCallback<TTools, TRuntimeContext>;
550
707
  /**
551
708
  * Callback that is invoked when an error occurs during streaming.
552
709
  * You can use it to log errors.
553
710
  */
554
- onError?: StreamTextOnErrorCallback;
711
+ onError?: WorkflowAgentOnErrorCallback;
712
+ /**
713
+ * Callback that is called when the LLM response and all request tool executions
714
+ * (for tools that have an `execute` function) are finished.
715
+ */
716
+ onEnd?: WorkflowAgentOnEndCallback<TTools, TRuntimeContext, OUTPUT>;
555
717
  /**
556
718
  * Callback that is called when the LLM response and all request tool executions
557
719
  * (for tools that have an `execute` function) are finished.
720
+ *
721
+ * @deprecated Use `onEnd` instead.
558
722
  */
559
- onFinish?: StreamTextOnFinishCallback<TTools, OUTPUT>;
723
+ onFinish?: WorkflowAgentOnFinishCallback<TTools, TRuntimeContext, OUTPUT>;
560
724
  /**
561
725
  * Callback that is called when the operation is aborted.
562
726
  */
563
- onAbort?: StreamTextOnAbortCallback<TTools>;
727
+ onAbort?: WorkflowAgentOnAbortCallback<TTools>;
564
728
  /**
565
729
  * Callback called when the agent starts streaming, before any LLM calls.
566
730
  */
567
- experimental_onStart?: WorkflowAgentOnStartCallback;
731
+ experimental_onStart?: WorkflowAgentOnStartCallback<TTools, TRuntimeContext>;
568
732
  /**
569
733
  * Callback called before each step (LLM call) begins.
570
734
  */
571
- experimental_onStepStart?: WorkflowAgentOnStepStartCallback;
735
+ experimental_onStepStart?: WorkflowAgentOnStepStartCallback<TTools, TRuntimeContext>;
572
736
  /**
573
737
  * Callback called before a tool's execute function runs.
574
738
  */
575
- experimental_onToolCallStart?: WorkflowAgentOnToolCallStartCallback;
739
+ onToolExecutionStart?: WorkflowAgentOnToolExecutionStartCallback<TTools>;
576
740
  /**
577
741
  * Callback called after a tool execution completes.
578
742
  */
579
- experimental_onToolCallFinish?: WorkflowAgentOnToolCallFinishCallback;
743
+ onToolExecutionEnd?: WorkflowAgentOnToolExecutionEndCallback<TTools>;
580
744
  /**
581
745
  * Callback function called before each step in the agent loop.
582
746
  * Use this to modify settings, manage context, or inject messages dynamically.
@@ -595,7 +759,7 @@ interface WorkflowAgentStreamOptions<TTools extends ToolSet = ToolSet, OUTPUT =
595
759
  * }
596
760
  * ```
597
761
  */
598
- prepareStep?: PrepareStepCallback<TTools>;
762
+ prepareStep?: PrepareStepCallback<TTools, TRuntimeContext>;
599
763
  /**
600
764
  * Timeout in milliseconds for the stream operation.
601
765
  * When specified, creates an AbortSignal that will abort the operation after the given time.
@@ -612,7 +776,7 @@ interface WorkflowAgentStreamOptions<TTools extends ToolSet = ToolSet, OUTPUT =
612
776
  * @default false
613
777
  */
614
778
  preventClose?: boolean;
615
- }
779
+ };
616
780
  /**
617
781
  * A tool call made by the model. Matches the AI SDK's tool call shape.
618
782
  */
@@ -711,7 +875,11 @@ interface WorkflowAgentStreamResult<TTools extends ToolSet = ToolSet, OUTPUT = n
711
875
  * });
712
876
  * ```
713
877
  */
714
- declare class WorkflowAgent<TBaseTools extends ToolSet = ToolSet> {
878
+ declare class WorkflowAgent<TBaseTools extends ToolSet = ToolSet, TRuntimeContext extends Context = Context> {
879
+ /**
880
+ * The id of the agent.
881
+ */
882
+ readonly id: string | undefined;
715
883
  private model;
716
884
  /**
717
885
  * The tool set configured for this agent.
@@ -721,18 +889,24 @@ declare class WorkflowAgent<TBaseTools extends ToolSet = ToolSet> {
721
889
  private generationSettings;
722
890
  private toolChoice?;
723
891
  private telemetry?;
724
- private experimentalContext;
892
+ private runtimeContext?;
893
+ private toolsContext?;
894
+ private stopWhen?;
895
+ private activeTools?;
896
+ private output?;
897
+ private experimentalRepairToolCall?;
898
+ private experimentalDownload?;
725
899
  private prepareStep?;
726
- private constructorOnStepFinish?;
727
- private constructorOnFinish?;
900
+ private constructorOnStepEnd?;
901
+ private constructorOnEnd?;
728
902
  private constructorOnStart?;
729
903
  private constructorOnStepStart?;
730
- private constructorOnToolCallStart?;
731
- private constructorOnToolCallFinish?;
904
+ private constructorOnToolExecutionStart?;
905
+ private constructorOnToolExecutionEnd?;
732
906
  private prepareCall?;
733
- constructor(options: WorkflowAgentOptions<TBaseTools>);
907
+ constructor(options: WorkflowAgentOptions<TBaseTools, TRuntimeContext>);
734
908
  generate(): void;
735
- stream<TTools extends TBaseTools = TBaseTools, OUTPUT = never, PARTIAL_OUTPUT = never>(options: WorkflowAgentStreamOptions<TTools, OUTPUT, PARTIAL_OUTPUT>): Promise<WorkflowAgentStreamResult<TTools, OUTPUT>>;
909
+ stream<TTools extends TBaseTools = TBaseTools, OUTPUT = never, PARTIAL_OUTPUT = never>(options: WorkflowAgentStreamOptions<TTools, TRuntimeContext, OUTPUT, PARTIAL_OUTPUT>): Promise<WorkflowAgentStreamResult<TTools, OUTPUT>>;
736
910
  }
737
911
 
738
912
  /**
@@ -901,4 +1075,4 @@ declare class WorkflowChatTransport<UI_MESSAGE extends UIMessage> implements Cha
901
1075
  private onFinish;
902
1076
  }
903
1077
 
904
- 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 StreamTextOnAbortCallback, type StreamTextOnErrorCallback, type StreamTextOnFinishCallback, type StreamTextTransform, type TelemetrySettings, WorkflowAgent, type WorkflowAgentOnStartCallback, type WorkflowAgentOnStepStartCallback, type WorkflowAgentOnToolCallFinishCallback, type WorkflowAgentOnToolCallStartCallback, type WorkflowAgentOptions, type WorkflowAgentStreamOptions, type WorkflowAgentStreamResult, WorkflowChatTransport, type WorkflowChatTransportOptions, createModelCallToUIChunkTransform, toUIMessageChunk };
1078
+ 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 WorkflowAgentOnEndCallback, type WorkflowAgentOnErrorCallback, type WorkflowAgentOnFinishCallback, type WorkflowAgentOnStartCallback, type WorkflowAgentOnStepEndCallback, type WorkflowAgentOnStepFinishCallback, type WorkflowAgentOnStepStartCallback, type WorkflowAgentOnToolExecutionEndCallback, type WorkflowAgentOnToolExecutionStartCallback, type WorkflowAgentOptions, type WorkflowAgentStreamOptions, type WorkflowAgentStreamResult, WorkflowChatTransport, type WorkflowChatTransportOptions, createModelCallToUIChunkTransform, toUIMessageChunk };