@ai-sdk/workflow 1.0.0-beta.8 → 1.0.0-beta.94

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
1
  import { LanguageModelV4, SharedV4ProviderOptions, LanguageModelV4CallOptions, LanguageModelV4Prompt, LanguageModelV4StreamPart } from '@ai-sdk/provider';
2
- import { ToolSet, LanguageModel, SystemModelMessage, ToolChoice, StopCondition, LanguageModelResponseMetadata, LanguageModelUsage, FinishReason, ToolCallRepairFunction, StepResult, StreamTextOnStepFinishCallback, ModelMessage, Experimental_LanguageModelStreamPart, UIMessage, UIMessageChunk, ChatTransport, PrepareSendMessagesRequest, PrepareReconnectToStreamRequest, ChatRequestOptions } from 'ai';
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
  /**
@@ -15,14 +16,21 @@ type CompatibleLanguageModel = LanguageModelV4;
15
16
 
16
17
  /**
17
18
  * Callback function to be called after each step completes.
18
- * Alias for the AI SDK's StreamTextOnStepFinishCallback, using
19
+ * Alias for the AI SDK's GenerateTextOnStepEndCallback, using
19
20
  * WorkflowAgent-consistent naming.
20
21
  */
21
- type WorkflowAgentOnStepFinishCallback<TTools extends ToolSet = ToolSet> = StreamTextOnStepFinishCallback<TTools, any>;
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>;
22
30
  /**
23
31
  * Infer the type of the tools of a workflow agent.
24
32
  */
25
- 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;
26
34
  /**
27
35
  * Infer the UI message type of a workflow agent.
28
36
  */
@@ -52,41 +60,12 @@ interface OutputSpecification<OUTPUT, PARTIAL> {
52
60
  * Provider-specific options type. This is equivalent to SharedV4ProviderOptions from @ai-sdk/provider.
53
61
  */
54
62
  type ProviderOptions = SharedV4ProviderOptions;
55
- /**
56
- * Telemetry settings for observability.
57
- */
58
- interface TelemetrySettings {
59
- /**
60
- * Enable or disable telemetry. Defaults to true.
61
- */
62
- isEnabled?: boolean;
63
- /**
64
- * Identifier for this function. Used to group telemetry data by function.
65
- */
66
- functionId?: string;
67
- /**
68
- * Additional information to include in the telemetry data.
69
- */
70
- metadata?: Record<string, string | number | boolean | Array<string | number | boolean> | null | undefined>;
71
- /**
72
- * Enable or disable input recording. Enabled by default.
73
- *
74
- * You might want to disable input recording to avoid recording sensitive
75
- * information, to reduce data transfers, or to increase performance.
76
- */
77
- recordInputs?: boolean;
78
- /**
79
- * Enable or disable output recording. Enabled by default.
80
- *
81
- * You might want to disable output recording to avoid recording sensitive
82
- * information, to reduce data transfers, or to increase performance.
83
- */
84
- recordOutputs?: boolean;
85
- /**
86
- * Custom tracer for the telemetry.
87
- */
88
- tracer?: unknown;
89
- }
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>;
90
69
  /**
91
70
  * A transformation that is applied to the stream.
92
71
  */
@@ -182,7 +161,7 @@ interface GenerationSettings {
182
161
  /**
183
162
  * Information passed to the prepareStep callback.
184
163
  */
185
- interface PrepareStepInfo<TTools extends ToolSet = ToolSet> {
164
+ interface PrepareStepInfo<TTools extends ToolSet = ToolSet, TRuntimeContext extends Context = Context> {
186
165
  /**
187
166
  * The current model configuration (string or function).
188
167
  * The function should return a LanguageModelV4 instance.
@@ -195,22 +174,31 @@ interface PrepareStepInfo<TTools extends ToolSet = ToolSet> {
195
174
  /**
196
175
  * All previous steps with their results.
197
176
  */
198
- steps: StepResult<TTools, any>[];
177
+ steps: StepResult<TTools, TRuntimeContext>[];
199
178
  /**
200
179
  * The messages that will be sent to the model.
201
180
  * This is the LanguageModelV4Prompt format used internally.
202
181
  */
203
182
  messages: LanguageModelV4Prompt;
204
183
  /**
205
- * 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.
206
187
  */
207
- 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>;
208
196
  }
209
197
  /**
210
198
  * Return type from the prepareStep callback.
211
199
  * All properties are optional - only return the ones you want to override.
212
200
  */
213
- interface PrepareStepResult extends Partial<GenerationSettings> {
201
+ interface PrepareStepResult<TTools extends ToolSet = ToolSet, TRuntimeContext extends Context = Context> extends Partial<GenerationSettings> {
214
202
  /**
215
203
  * Override the model for this step.
216
204
  */
@@ -234,26 +222,39 @@ interface PrepareStepResult extends Partial<GenerationSettings> {
234
222
  */
235
223
  activeTools?: string[];
236
224
  /**
237
- * Context that is passed into tool execution. Experimental.
238
- * 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.
227
+ */
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.
239
232
  */
240
- experimental_context?: unknown;
233
+ toolsContext?: InferToolSetContext<TTools>;
241
234
  }
242
235
  /**
243
236
  * Callback function called before each step in the agent loop.
244
237
  * Use this to modify settings, manage context, or implement dynamic behavior.
245
238
  */
246
- 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>;
247
240
  /**
248
241
  * Options passed to the prepareCall callback.
249
242
  */
250
- interface PrepareCallOptions<TTools extends ToolSet = ToolSet> extends Partial<GenerationSettings> {
243
+ interface PrepareCallOptions<TTools extends ToolSet = ToolSet, TRuntimeContext extends Context = Context> extends Partial<GenerationSettings> {
251
244
  model: LanguageModel;
252
245
  tools: TTools;
253
- instructions?: string | SystemModelMessage | Array<SystemModelMessage>;
246
+ instructions?: Instructions;
254
247
  toolChoice?: ToolChoice<TTools>;
255
- experimental_telemetry?: TelemetrySettings;
256
- 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>;
257
258
  messages: ModelMessage[];
258
259
  }
259
260
  /**
@@ -262,15 +263,15 @@ interface PrepareCallOptions<TTools extends ToolSet = ToolSet> extends Partial<G
262
263
  * Note: `tools` cannot be overridden via prepareCall because they are
263
264
  * bound at construction time for type safety.
264
265
  */
265
- 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'>>;
266
267
  /**
267
268
  * Callback called once before the agent loop starts to transform call parameters.
268
269
  */
269
- 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>>;
270
271
  /**
271
272
  * Configuration options for creating a {@link WorkflowAgent} instance.
272
273
  */
273
- interface WorkflowAgentOptions<TTools extends ToolSet = ToolSet> extends GenerationSettings {
274
+ type WorkflowAgentOptions<TTools extends ToolSet = ToolSet, TRuntimeContext extends Context = Context> = GenerationSettings & WorkflowAgentToolsContextParameter<TTools> & {
274
275
  /**
275
276
  * The id of the agent.
276
277
  */
@@ -292,7 +293,7 @@ interface WorkflowAgentOptions<TTools extends ToolSet = ToolSet> extends Generat
292
293
  * Agent instructions. Can be a string, a SystemModelMessage, or an array of SystemModelMessages.
293
294
  * Supports provider-specific options (e.g., caching) when using the SystemModelMessage form.
294
295
  */
295
- instructions?: string | SystemModelMessage | Array<SystemModelMessage>;
296
+ instructions?: Instructions;
296
297
  /**
297
298
  * Optional system prompt to guide the agent's behavior.
298
299
  * @deprecated Use `instructions` instead.
@@ -303,17 +304,23 @@ interface WorkflowAgentOptions<TTools extends ToolSet = ToolSet> extends Generat
303
304
  */
304
305
  toolChoice?: ToolChoice<TTools>;
305
306
  /**
306
- * Optional telemetry configuration (experimental).
307
+ * Optional telemetry configuration.
307
308
  */
308
- experimental_telemetry?: TelemetrySettings;
309
+ telemetry?: TelemetryOptions<TRuntimeContext, TTools>;
309
310
  /**
310
- * 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.
312
+ *
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.
311
320
  *
312
- * Per-stream `experimental_context` values passed to `stream()` override this default.
313
- * Experimental (can break in patch releases).
314
- * @default undefined
321
+ * Per-stream `runtimeContext` values passed to `stream()` override this default.
315
322
  */
316
- experimental_context?: unknown;
323
+ runtimeContext?: TRuntimeContext;
317
324
  /**
318
325
  * Default stop condition for the agent loop. When the condition is an array,
319
326
  * any of the conditions can be met to stop the generation.
@@ -327,7 +334,7 @@ interface WorkflowAgentOptions<TTools extends ToolSet = ToolSet> extends Generat
327
334
  *
328
335
  * Per-stream `activeTools` values passed to `stream()` override this default.
329
336
  */
330
- activeTools?: Array<keyof NoInfer<TTools>>;
337
+ activeTools?: ActiveTools<NoInfer<TTools>>;
331
338
  /**
332
339
  * Default output specification for structured outputs.
333
340
  * Use `Output.object({ schema })` for structured output or `Output.text()` for text output.
@@ -354,46 +361,58 @@ interface WorkflowAgentOptions<TTools extends ToolSet = ToolSet> extends Generat
354
361
  *
355
362
  * Per-stream `prepareStep` values passed to `stream()` override this default.
356
363
  */
357
- prepareStep?: PrepareStepCallback<TTools>;
364
+ prepareStep?: PrepareStepCallback<TTools, TRuntimeContext>;
358
365
  /**
359
366
  * Callback function to be called after each step completes.
360
367
  */
361
- onStepFinish?: WorkflowAgentOnStepFinishCallback<ToolSet>;
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>;
362
375
  /**
363
376
  * Callback that is called when the LLM response and all request tool executions are finished.
364
377
  */
365
- onFinish?: WorkflowAgentOnFinishCallback<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>;
366
385
  /**
367
386
  * Callback called when the agent starts streaming, before any LLM calls.
368
387
  */
369
- experimental_onStart?: WorkflowAgentOnStartCallback;
388
+ experimental_onStart?: WorkflowAgentOnStartCallback<TTools, TRuntimeContext>;
370
389
  /**
371
390
  * Callback called before each step (LLM call) begins.
372
391
  */
373
- experimental_onStepStart?: WorkflowAgentOnStepStartCallback;
392
+ experimental_onStepStart?: WorkflowAgentOnStepStartCallback<TTools, TRuntimeContext>;
374
393
  /**
375
394
  * Callback called before a tool's execute function runs.
376
395
  */
377
- experimental_onToolCallStart?: WorkflowAgentOnToolCallStartCallback;
396
+ onToolExecutionStart?: WorkflowAgentOnToolExecutionStartCallback<TTools>;
378
397
  /**
379
398
  * Callback called after a tool execution completes.
380
399
  */
381
- experimental_onToolCallFinish?: WorkflowAgentOnToolCallFinishCallback;
400
+ onToolExecutionEnd?: WorkflowAgentOnToolExecutionEndCallback<TTools>;
382
401
  /**
383
402
  * Prepare the parameters for the stream call.
384
403
  * Called once before the agent loop starts. Use this to transform
385
404
  * model, tools, instructions, or other settings based on runtime context.
386
405
  */
387
- prepareCall?: PrepareCallCallback<TTools>;
388
- }
406
+ prepareCall?: PrepareCallCallback<TTools, TRuntimeContext>;
407
+ };
389
408
  /**
390
409
  * Callback that is called when the LLM response and all request tool executions are finished.
391
410
  */
392
- type WorkflowAgentOnFinishCallback<TTools extends ToolSet = ToolSet, OUTPUT = never> = (event: {
411
+ type WorkflowAgentOnEndCallback<TTools extends ToolSet = ToolSet, TRuntimeContext extends Context = Context, OUTPUT = never> = (event: {
393
412
  /**
394
413
  * Details for all steps.
395
414
  */
396
- readonly steps: StepResult<TTools, any>[];
415
+ readonly steps: StepResult<TTools, TRuntimeContext>[];
397
416
  /**
398
417
  * The final messages including all tool calls and results.
399
418
  */
@@ -406,20 +425,34 @@ type WorkflowAgentOnFinishCallback<TTools extends ToolSet = ToolSet, OUTPUT = ne
406
425
  * The finish reason from the last step.
407
426
  */
408
427
  readonly finishReason: FinishReason;
428
+ /**
429
+ * The total token usage across all steps.
430
+ */
431
+ readonly usage: LanguageModelUsage;
409
432
  /**
410
433
  * The total token usage across all steps.
411
434
  */
412
435
  readonly totalUsage: LanguageModelUsage;
413
436
  /**
414
- * Context that is passed into tool execution.
437
+ * The runtime context at the end of the agent loop.
415
438
  */
416
- readonly experimental_context: unknown;
439
+ readonly runtimeContext: TRuntimeContext;
440
+ /**
441
+ * The per-tool context at the end of the agent loop.
442
+ */
443
+ readonly toolsContext: InferToolSetContext<TTools>;
417
444
  /**
418
445
  * The generated structured output. It uses the `output` specification.
419
446
  * Only available when `output` is specified.
420
447
  */
421
448
  readonly output: OUTPUT;
422
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>;
423
456
  /**
424
457
  * Callback that is invoked when an error occurs during streaming.
425
458
  */
@@ -438,16 +471,20 @@ type WorkflowAgentOnAbortCallback<TTools extends ToolSet = ToolSet> = (event: {
438
471
  /**
439
472
  * Callback that is called when the agent starts streaming, before any LLM calls.
440
473
  */
441
- type WorkflowAgentOnStartCallback = (event: {
474
+ type WorkflowAgentOnStartCallback<TTools extends ToolSet = ToolSet, TRuntimeContext extends Context = Context> = (event: {
442
475
  /** The model being used */
443
476
  readonly model: LanguageModel;
444
477
  /** The messages being sent */
445
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>;
446
483
  }) => PromiseLike<void> | void;
447
484
  /**
448
485
  * Callback that is called before each step (LLM call) begins.
449
486
  */
450
- type WorkflowAgentOnStepStartCallback<TTools extends ToolSet = ToolSet> = (event: {
487
+ type WorkflowAgentOnStepStartCallback<TTools extends ToolSet = ToolSet, TRuntimeContext extends Context = Context> = (event: {
451
488
  /** The current step number (0-based) */
452
489
  readonly stepNumber: number;
453
490
  /** The model being used for this step */
@@ -455,29 +492,41 @@ type WorkflowAgentOnStepStartCallback<TTools extends ToolSet = ToolSet> = (event
455
492
  /** The messages being sent for this step */
456
493
  readonly messages: ModelMessage[];
457
494
  /** Results from all previously finished steps */
458
- readonly steps: ReadonlyArray<StepResult<TTools, any>>;
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>;
459
500
  }) => PromiseLike<void> | void;
460
501
  /**
461
502
  * Callback that is called before a tool's execute function runs.
462
503
  */
463
- type WorkflowAgentOnToolCallStartCallback = (event: {
504
+ type WorkflowAgentOnToolExecutionStartCallback<TTools extends ToolSet = ToolSet> = (event: {
464
505
  /** The tool call being executed */
465
506
  readonly toolCall: ToolCall;
466
507
  /** The current step number (0-based) */
467
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;
468
513
  }) => PromiseLike<void> | void;
469
514
  /**
470
515
  * Callback that is called after a tool execution completes.
471
516
  * Uses a discriminated union pattern: check `success` to determine
472
517
  * whether `output` or `error` is available.
473
518
  */
474
- type WorkflowAgentOnToolCallFinishCallback = (event: {
519
+ type WorkflowAgentOnToolExecutionEndCallback<TTools extends ToolSet = ToolSet> = (event: {
475
520
  /** The tool call that was executed */
476
521
  readonly toolCall: ToolCall;
477
522
  /** The current step number (0-based) */
478
523
  readonly stepNumber: number;
479
524
  /** Execution time in milliseconds */
480
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;
481
530
  /** Whether the tool call succeeded */
482
531
  readonly success: true;
483
532
  /** The tool result */
@@ -490,6 +539,10 @@ type WorkflowAgentOnToolCallFinishCallback = (event: {
490
539
  readonly stepNumber: number;
491
540
  /** Execution time in milliseconds */
492
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;
493
546
  /** Whether the tool call succeeded */
494
547
  readonly success: false;
495
548
  /** The error that occurred */
@@ -499,7 +552,7 @@ type WorkflowAgentOnToolCallFinishCallback = (event: {
499
552
  /**
500
553
  * Options for the {@link WorkflowAgent.stream} method.
501
554
  */
502
- type WorkflowAgentStreamOptions<TTools extends ToolSet = ToolSet, OUTPUT = never, PARTIAL_OUTPUT = never> = Partial<GenerationSettings> & ({
555
+ type WorkflowAgentStreamOptions<TTools extends ToolSet = ToolSet, TRuntimeContext extends Context = Context, OUTPUT = never, PARTIAL_OUTPUT = never> = Partial<GenerationSettings> & ({
503
556
  /**
504
557
  * A prompt. It can be either a text prompt or a list of messages.
505
558
  *
@@ -558,12 +611,6 @@ type WorkflowAgentStreamOptions<TTools extends ToolSet = ToolSet, OUTPUT = never
558
611
  * When the condition is an array, any of the conditions can be met to stop the generation.
559
612
  */
560
613
  stopWhen?: StopCondition<NoInfer<ToolSet>, any> | Array<StopCondition<NoInfer<ToolSet>, any>>;
561
- /**
562
- * Maximum number of sequential LLM calls (steps), e.g. when you use tool calls.
563
- * A maximum number can be set to prevent infinite loops in the case of misconfigured tools.
564
- * By default, it's unlimited (the agent loops until completion).
565
- */
566
- maxSteps?: number;
567
614
  /**
568
615
  * The tool choice strategy. Default: 'auto'.
569
616
  * Overrides the toolChoice from the constructor if provided.
@@ -573,17 +620,34 @@ type WorkflowAgentStreamOptions<TTools extends ToolSet = ToolSet, OUTPUT = never
573
620
  * Limits the tools that are available for the model to call without
574
621
  * changing the tool call and result types in the result.
575
622
  */
576
- activeTools?: Array<keyof NoInfer<TTools>>;
623
+ activeTools?: ActiveTools<NoInfer<TTools>>;
577
624
  /**
578
- * Optional telemetry configuration (experimental).
625
+ * Optional telemetry configuration.
579
626
  */
580
- experimental_telemetry?: TelemetrySettings;
627
+ telemetry?: TelemetryOptions<TRuntimeContext, TTools>;
581
628
  /**
582
- * Context that is passed into tool execution.
583
- * Experimental (can break in patch releases).
584
- * @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.
638
+ */
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.
585
649
  */
586
- experimental_context?: unknown;
650
+ toolsContext?: InferToolSetContext<TTools>;
587
651
  /**
588
652
  * Optional specification for parsing structured outputs from the LLM response.
589
653
  * Use `Output.object({ schema })` for structured output or `Output.text()` for text output.
@@ -633,7 +697,13 @@ type WorkflowAgentStreamOptions<TTools extends ToolSet = ToolSet, OUTPUT = never
633
697
  /**
634
698
  * Callback function to be called after each step completes.
635
699
  */
636
- onStepFinish?: WorkflowAgentOnStepFinishCallback<TTools>;
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>;
637
707
  /**
638
708
  * Callback that is invoked when an error occurs during streaming.
639
709
  * You can use it to log errors.
@@ -643,7 +713,14 @@ type WorkflowAgentStreamOptions<TTools extends ToolSet = ToolSet, OUTPUT = never
643
713
  * Callback that is called when the LLM response and all request tool executions
644
714
  * (for tools that have an `execute` function) are finished.
645
715
  */
646
- onFinish?: WorkflowAgentOnFinishCallback<TTools, OUTPUT>;
716
+ onEnd?: WorkflowAgentOnEndCallback<TTools, TRuntimeContext, OUTPUT>;
717
+ /**
718
+ * Callback that is called when the LLM response and all request tool executions
719
+ * (for tools that have an `execute` function) are finished.
720
+ *
721
+ * @deprecated Use `onEnd` instead.
722
+ */
723
+ onFinish?: WorkflowAgentOnFinishCallback<TTools, TRuntimeContext, OUTPUT>;
647
724
  /**
648
725
  * Callback that is called when the operation is aborted.
649
726
  */
@@ -651,19 +728,19 @@ type WorkflowAgentStreamOptions<TTools extends ToolSet = ToolSet, OUTPUT = never
651
728
  /**
652
729
  * Callback called when the agent starts streaming, before any LLM calls.
653
730
  */
654
- experimental_onStart?: WorkflowAgentOnStartCallback;
731
+ experimental_onStart?: WorkflowAgentOnStartCallback<TTools, TRuntimeContext>;
655
732
  /**
656
733
  * Callback called before each step (LLM call) begins.
657
734
  */
658
- experimental_onStepStart?: WorkflowAgentOnStepStartCallback;
735
+ experimental_onStepStart?: WorkflowAgentOnStepStartCallback<TTools, TRuntimeContext>;
659
736
  /**
660
737
  * Callback called before a tool's execute function runs.
661
738
  */
662
- experimental_onToolCallStart?: WorkflowAgentOnToolCallStartCallback;
739
+ onToolExecutionStart?: WorkflowAgentOnToolExecutionStartCallback<TTools>;
663
740
  /**
664
741
  * Callback called after a tool execution completes.
665
742
  */
666
- experimental_onToolCallFinish?: WorkflowAgentOnToolCallFinishCallback;
743
+ onToolExecutionEnd?: WorkflowAgentOnToolExecutionEndCallback<TTools>;
667
744
  /**
668
745
  * Callback function called before each step in the agent loop.
669
746
  * Use this to modify settings, manage context, or inject messages dynamically.
@@ -682,7 +759,7 @@ type WorkflowAgentStreamOptions<TTools extends ToolSet = ToolSet, OUTPUT = never
682
759
  * }
683
760
  * ```
684
761
  */
685
- prepareStep?: PrepareStepCallback<TTools>;
762
+ prepareStep?: PrepareStepCallback<TTools, TRuntimeContext>;
686
763
  /**
687
764
  * Timeout in milliseconds for the stream operation.
688
765
  * When specified, creates an AbortSignal that will abort the operation after the given time.
@@ -798,7 +875,7 @@ interface WorkflowAgentStreamResult<TTools extends ToolSet = ToolSet, OUTPUT = n
798
875
  * });
799
876
  * ```
800
877
  */
801
- declare class WorkflowAgent<TBaseTools extends ToolSet = ToolSet> {
878
+ declare class WorkflowAgent<TBaseTools extends ToolSet = ToolSet, TRuntimeContext extends Context = Context> {
802
879
  /**
803
880
  * The id of the agent.
804
881
  */
@@ -812,23 +889,24 @@ declare class WorkflowAgent<TBaseTools extends ToolSet = ToolSet> {
812
889
  private generationSettings;
813
890
  private toolChoice?;
814
891
  private telemetry?;
815
- private experimentalContext;
892
+ private runtimeContext?;
893
+ private toolsContext?;
816
894
  private stopWhen?;
817
895
  private activeTools?;
818
896
  private output?;
819
897
  private experimentalRepairToolCall?;
820
898
  private experimentalDownload?;
821
899
  private prepareStep?;
822
- private constructorOnStepFinish?;
823
- private constructorOnFinish?;
900
+ private constructorOnStepEnd?;
901
+ private constructorOnEnd?;
824
902
  private constructorOnStart?;
825
903
  private constructorOnStepStart?;
826
- private constructorOnToolCallStart?;
827
- private constructorOnToolCallFinish?;
904
+ private constructorOnToolExecutionStart?;
905
+ private constructorOnToolExecutionEnd?;
828
906
  private prepareCall?;
829
- constructor(options: WorkflowAgentOptions<TBaseTools>);
907
+ constructor(options: WorkflowAgentOptions<TBaseTools, TRuntimeContext>);
830
908
  generate(): void;
831
- 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>>;
832
910
  }
833
911
 
834
912
  /**
@@ -997,4 +1075,4 @@ declare class WorkflowChatTransport<UI_MESSAGE extends UIMessage> implements Cha
997
1075
  private onFinish;
998
1076
  }
999
1077
 
1000
- 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 TelemetrySettings, 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 };
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 };