@ai-sdk/workflow 1.0.0-beta.10 → 1.0.0-beta.101

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.
187
+ */
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.
206
194
  */
207
- experimental_context: unknown;
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.
239
227
  */
240
- 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>;
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,67 @@ 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>;
375
+ /**
376
+ * Callback that is called when the LLM response and all request tool executions are finished.
377
+ */
378
+ onEnd?: WorkflowAgentOnEndCallback<TTools, TRuntimeContext>;
362
379
  /**
363
380
  * Callback that is called when the LLM response and all request tool executions are finished.
381
+ *
382
+ * @deprecated Use `onEnd` instead.
364
383
  */
365
- onFinish?: WorkflowAgentOnFinishCallback<ToolSet>;
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
+ /**
408
+ * Whether to allow system messages inside the `prompt` or `messages` fields.
409
+ * When `false` (the default), system messages in `prompt` or `messages` are
410
+ * rejected to prevent prompt-injection attacks. Set to `true` only when you
411
+ * intentionally interleave system messages with user messages.
412
+ *
413
+ * @default false
414
+ */
415
+ allowSystemInMessages?: boolean;
416
+ };
389
417
  /**
390
418
  * Callback that is called when the LLM response and all request tool executions are finished.
391
419
  */
392
- type WorkflowAgentOnFinishCallback<TTools extends ToolSet = ToolSet, OUTPUT = never> = (event: {
420
+ type WorkflowAgentOnEndCallback<TTools extends ToolSet = ToolSet, TRuntimeContext extends Context = Context, OUTPUT = never> = (event: {
393
421
  /**
394
422
  * Details for all steps.
395
423
  */
396
- readonly steps: StepResult<TTools, any>[];
424
+ readonly steps: StepResult<TTools, TRuntimeContext>[];
397
425
  /**
398
426
  * The final messages including all tool calls and results.
399
427
  */
@@ -406,20 +434,34 @@ type WorkflowAgentOnFinishCallback<TTools extends ToolSet = ToolSet, OUTPUT = ne
406
434
  * The finish reason from the last step.
407
435
  */
408
436
  readonly finishReason: FinishReason;
437
+ /**
438
+ * The total token usage across all steps.
439
+ */
440
+ readonly usage: LanguageModelUsage;
409
441
  /**
410
442
  * The total token usage across all steps.
411
443
  */
412
444
  readonly totalUsage: LanguageModelUsage;
413
445
  /**
414
- * Context that is passed into tool execution.
446
+ * The runtime context at the end of the agent loop.
415
447
  */
416
- readonly experimental_context: unknown;
448
+ readonly runtimeContext: TRuntimeContext;
449
+ /**
450
+ * The per-tool context at the end of the agent loop.
451
+ */
452
+ readonly toolsContext: InferToolSetContext<TTools>;
417
453
  /**
418
454
  * The generated structured output. It uses the `output` specification.
419
455
  * Only available when `output` is specified.
420
456
  */
421
457
  readonly output: OUTPUT;
422
458
  }) => PromiseLike<void> | void;
459
+ /**
460
+ * Callback that is called when the LLM response and all request tool executions are finished.
461
+ *
462
+ * @deprecated Use `WorkflowAgentOnEndCallback` instead.
463
+ */
464
+ type WorkflowAgentOnFinishCallback<TTools extends ToolSet = ToolSet, TRuntimeContext extends Context = Context, OUTPUT = never> = WorkflowAgentOnEndCallback<TTools, TRuntimeContext, OUTPUT>;
423
465
  /**
424
466
  * Callback that is invoked when an error occurs during streaming.
425
467
  */
@@ -438,16 +480,20 @@ type WorkflowAgentOnAbortCallback<TTools extends ToolSet = ToolSet> = (event: {
438
480
  /**
439
481
  * Callback that is called when the agent starts streaming, before any LLM calls.
440
482
  */
441
- type WorkflowAgentOnStartCallback = (event: {
483
+ type WorkflowAgentOnStartCallback<TTools extends ToolSet = ToolSet, TRuntimeContext extends Context = Context> = (event: {
442
484
  /** The model being used */
443
485
  readonly model: LanguageModel;
444
486
  /** The messages being sent */
445
487
  readonly messages: ModelMessage[];
488
+ /** Shared runtime context for this agent loop */
489
+ readonly runtimeContext: TRuntimeContext;
490
+ /** Per-tool context map for this agent loop */
491
+ readonly toolsContext: InferToolSetContext<TTools>;
446
492
  }) => PromiseLike<void> | void;
447
493
  /**
448
494
  * Callback that is called before each step (LLM call) begins.
449
495
  */
450
- type WorkflowAgentOnStepStartCallback<TTools extends ToolSet = ToolSet> = (event: {
496
+ type WorkflowAgentOnStepStartCallback<TTools extends ToolSet = ToolSet, TRuntimeContext extends Context = Context> = (event: {
451
497
  /** The current step number (0-based) */
452
498
  readonly stepNumber: number;
453
499
  /** The model being used for this step */
@@ -455,29 +501,41 @@ type WorkflowAgentOnStepStartCallback<TTools extends ToolSet = ToolSet> = (event
455
501
  /** The messages being sent for this step */
456
502
  readonly messages: ModelMessage[];
457
503
  /** Results from all previously finished steps */
458
- readonly steps: ReadonlyArray<StepResult<TTools, any>>;
504
+ readonly steps: ReadonlyArray<StepResult<TTools, TRuntimeContext>>;
505
+ /** Shared runtime context for this step */
506
+ readonly runtimeContext: TRuntimeContext;
507
+ /** Per-tool context map for this step */
508
+ readonly toolsContext: InferToolSetContext<TTools>;
459
509
  }) => PromiseLike<void> | void;
460
510
  /**
461
511
  * Callback that is called before a tool's execute function runs.
462
512
  */
463
- type WorkflowAgentOnToolCallStartCallback = (event: {
513
+ type WorkflowAgentOnToolExecutionStartCallback<TTools extends ToolSet = ToolSet> = (event: {
464
514
  /** The tool call being executed */
465
515
  readonly toolCall: ToolCall;
466
516
  /** The current step number (0-based) */
467
517
  readonly stepNumber: number;
518
+ /** Messages sent to the language model for the step that produced the call */
519
+ readonly messages: ModelMessage[];
520
+ /** Tool-specific context passed to the tool */
521
+ readonly toolContext: InferToolSetContext<TTools>[keyof InferToolSetContext<TTools>] | undefined;
468
522
  }) => PromiseLike<void> | void;
469
523
  /**
470
524
  * Callback that is called after a tool execution completes.
471
525
  * Uses a discriminated union pattern: check `success` to determine
472
526
  * whether `output` or `error` is available.
473
527
  */
474
- type WorkflowAgentOnToolCallFinishCallback = (event: {
528
+ type WorkflowAgentOnToolExecutionEndCallback<TTools extends ToolSet = ToolSet> = (event: {
475
529
  /** The tool call that was executed */
476
530
  readonly toolCall: ToolCall;
477
531
  /** The current step number (0-based) */
478
532
  readonly stepNumber: number;
479
533
  /** Execution time in milliseconds */
480
534
  readonly durationMs: number;
535
+ /** Messages sent to the language model for the step that produced the call */
536
+ readonly messages: ModelMessage[];
537
+ /** Tool-specific context passed to the tool */
538
+ readonly toolContext: InferToolSetContext<TTools>[keyof InferToolSetContext<TTools>] | undefined;
481
539
  /** Whether the tool call succeeded */
482
540
  readonly success: true;
483
541
  /** The tool result */
@@ -490,6 +548,10 @@ type WorkflowAgentOnToolCallFinishCallback = (event: {
490
548
  readonly stepNumber: number;
491
549
  /** Execution time in milliseconds */
492
550
  readonly durationMs: number;
551
+ /** Messages sent to the language model for the step that produced the call */
552
+ readonly messages: ModelMessage[];
553
+ /** Tool-specific context passed to the tool */
554
+ readonly toolContext: InferToolSetContext<TTools>[keyof InferToolSetContext<TTools>] | undefined;
493
555
  /** Whether the tool call succeeded */
494
556
  readonly success: false;
495
557
  /** The error that occurred */
@@ -499,7 +561,7 @@ type WorkflowAgentOnToolCallFinishCallback = (event: {
499
561
  /**
500
562
  * Options for the {@link WorkflowAgent.stream} method.
501
563
  */
502
- type WorkflowAgentStreamOptions<TTools extends ToolSet = ToolSet, OUTPUT = never, PARTIAL_OUTPUT = never> = Partial<GenerationSettings> & ({
564
+ type WorkflowAgentStreamOptions<TTools extends ToolSet = ToolSet, TRuntimeContext extends Context = Context, OUTPUT = never, PARTIAL_OUTPUT = never> = Partial<GenerationSettings> & ({
503
565
  /**
504
566
  * A prompt. It can be either a text prompt or a list of messages.
505
567
  *
@@ -558,12 +620,6 @@ type WorkflowAgentStreamOptions<TTools extends ToolSet = ToolSet, OUTPUT = never
558
620
  * When the condition is an array, any of the conditions can be met to stop the generation.
559
621
  */
560
622
  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
623
  /**
568
624
  * The tool choice strategy. Default: 'auto'.
569
625
  * Overrides the toolChoice from the constructor if provided.
@@ -573,17 +629,34 @@ type WorkflowAgentStreamOptions<TTools extends ToolSet = ToolSet, OUTPUT = never
573
629
  * Limits the tools that are available for the model to call without
574
630
  * changing the tool call and result types in the result.
575
631
  */
576
- activeTools?: Array<keyof NoInfer<TTools>>;
632
+ activeTools?: ActiveTools<NoInfer<TTools>>;
633
+ /**
634
+ * Optional telemetry configuration.
635
+ */
636
+ telemetry?: TelemetryOptions<TRuntimeContext, TTools>;
577
637
  /**
578
- * Optional telemetry configuration (experimental).
638
+ * Runtime context that flows through the agent loop.
639
+ *
640
+ * Treat as immutable; return a new `runtimeContext` from `prepareStep`
641
+ * to update it between steps.
642
+ *
643
+ * In workflow context, keep values serializable so they can cross workflow
644
+ * and step boundaries.
645
+ *
646
+ * Overrides the constructor-level `runtimeContext` if provided.
579
647
  */
580
- experimental_telemetry?: TelemetrySettings;
648
+ runtimeContext?: TRuntimeContext;
581
649
  /**
582
- * Context that is passed into tool execution.
583
- * Experimental (can break in patch releases).
584
- * @default undefined
650
+ * Per-tool context, keyed by tool name. Each tool receives only its own
651
+ * validated entry as `context` during execution. Tools that declare a
652
+ * `contextSchema` validate their entry against the schema.
653
+ *
654
+ * In workflow context, keep values serializable so they can cross workflow
655
+ * and step boundaries.
656
+ *
657
+ * Overrides the constructor-level `toolsContext` if provided.
585
658
  */
586
- experimental_context?: unknown;
659
+ toolsContext?: InferToolSetContext<TTools>;
587
660
  /**
588
661
  * Optional specification for parsing structured outputs from the LLM response.
589
662
  * Use `Output.object({ schema })` for structured output or `Output.text()` for text output.
@@ -633,7 +706,13 @@ type WorkflowAgentStreamOptions<TTools extends ToolSet = ToolSet, OUTPUT = never
633
706
  /**
634
707
  * Callback function to be called after each step completes.
635
708
  */
636
- onStepFinish?: WorkflowAgentOnStepFinishCallback<TTools>;
709
+ onStepEnd?: WorkflowAgentOnStepEndCallback<TTools, TRuntimeContext>;
710
+ /**
711
+ * Callback function to be called after each step completes.
712
+ *
713
+ * @deprecated Use `onStepEnd` instead.
714
+ */
715
+ onStepFinish?: WorkflowAgentOnStepFinishCallback<TTools, TRuntimeContext>;
637
716
  /**
638
717
  * Callback that is invoked when an error occurs during streaming.
639
718
  * You can use it to log errors.
@@ -643,7 +722,14 @@ type WorkflowAgentStreamOptions<TTools extends ToolSet = ToolSet, OUTPUT = never
643
722
  * Callback that is called when the LLM response and all request tool executions
644
723
  * (for tools that have an `execute` function) are finished.
645
724
  */
646
- onFinish?: WorkflowAgentOnFinishCallback<TTools, OUTPUT>;
725
+ onEnd?: WorkflowAgentOnEndCallback<TTools, TRuntimeContext, OUTPUT>;
726
+ /**
727
+ * Callback that is called when the LLM response and all request tool executions
728
+ * (for tools that have an `execute` function) are finished.
729
+ *
730
+ * @deprecated Use `onEnd` instead.
731
+ */
732
+ onFinish?: WorkflowAgentOnFinishCallback<TTools, TRuntimeContext, OUTPUT>;
647
733
  /**
648
734
  * Callback that is called when the operation is aborted.
649
735
  */
@@ -651,19 +737,19 @@ type WorkflowAgentStreamOptions<TTools extends ToolSet = ToolSet, OUTPUT = never
651
737
  /**
652
738
  * Callback called when the agent starts streaming, before any LLM calls.
653
739
  */
654
- experimental_onStart?: WorkflowAgentOnStartCallback;
740
+ experimental_onStart?: WorkflowAgentOnStartCallback<TTools, TRuntimeContext>;
655
741
  /**
656
742
  * Callback called before each step (LLM call) begins.
657
743
  */
658
- experimental_onStepStart?: WorkflowAgentOnStepStartCallback;
744
+ experimental_onStepStart?: WorkflowAgentOnStepStartCallback<TTools, TRuntimeContext>;
659
745
  /**
660
746
  * Callback called before a tool's execute function runs.
661
747
  */
662
- experimental_onToolCallStart?: WorkflowAgentOnToolCallStartCallback;
748
+ onToolExecutionStart?: WorkflowAgentOnToolExecutionStartCallback<TTools>;
663
749
  /**
664
750
  * Callback called after a tool execution completes.
665
751
  */
666
- experimental_onToolCallFinish?: WorkflowAgentOnToolCallFinishCallback;
752
+ onToolExecutionEnd?: WorkflowAgentOnToolExecutionEndCallback<TTools>;
667
753
  /**
668
754
  * Callback function called before each step in the agent loop.
669
755
  * Use this to modify settings, manage context, or inject messages dynamically.
@@ -682,7 +768,7 @@ type WorkflowAgentStreamOptions<TTools extends ToolSet = ToolSet, OUTPUT = never
682
768
  * }
683
769
  * ```
684
770
  */
685
- prepareStep?: PrepareStepCallback<TTools>;
771
+ prepareStep?: PrepareStepCallback<TTools, TRuntimeContext>;
686
772
  /**
687
773
  * Timeout in milliseconds for the stream operation.
688
774
  * When specified, creates an AbortSignal that will abort the operation after the given time.
@@ -798,7 +884,7 @@ interface WorkflowAgentStreamResult<TTools extends ToolSet = ToolSet, OUTPUT = n
798
884
  * });
799
885
  * ```
800
886
  */
801
- declare class WorkflowAgent<TBaseTools extends ToolSet = ToolSet> {
887
+ declare class WorkflowAgent<TBaseTools extends ToolSet = ToolSet, TRuntimeContext extends Context = Context> {
802
888
  /**
803
889
  * The id of the agent.
804
890
  */
@@ -812,23 +898,25 @@ declare class WorkflowAgent<TBaseTools extends ToolSet = ToolSet> {
812
898
  private generationSettings;
813
899
  private toolChoice?;
814
900
  private telemetry?;
815
- private experimentalContext;
901
+ private runtimeContext?;
902
+ private toolsContext?;
816
903
  private stopWhen?;
817
904
  private activeTools?;
818
905
  private output?;
819
906
  private experimentalRepairToolCall?;
820
907
  private experimentalDownload?;
821
908
  private prepareStep?;
822
- private constructorOnStepFinish?;
823
- private constructorOnFinish?;
909
+ private allowSystemInMessages;
910
+ private constructorOnStepEnd?;
911
+ private constructorOnEnd?;
824
912
  private constructorOnStart?;
825
913
  private constructorOnStepStart?;
826
- private constructorOnToolCallStart?;
827
- private constructorOnToolCallFinish?;
914
+ private constructorOnToolExecutionStart?;
915
+ private constructorOnToolExecutionEnd?;
828
916
  private prepareCall?;
829
- constructor(options: WorkflowAgentOptions<TBaseTools>);
917
+ constructor(options: WorkflowAgentOptions<TBaseTools, TRuntimeContext>);
830
918
  generate(): void;
831
- stream<TTools extends TBaseTools = TBaseTools, OUTPUT = never, PARTIAL_OUTPUT = never>(options: WorkflowAgentStreamOptions<TTools, OUTPUT, PARTIAL_OUTPUT>): Promise<WorkflowAgentStreamResult<TTools, OUTPUT>>;
919
+ stream<TTools extends TBaseTools = TBaseTools, OUTPUT = never, PARTIAL_OUTPUT = never>(options: WorkflowAgentStreamOptions<TTools, TRuntimeContext, OUTPUT, PARTIAL_OUTPUT>): Promise<WorkflowAgentStreamResult<TTools, OUTPUT>>;
832
920
  }
833
921
 
834
922
  /**
@@ -997,4 +1085,4 @@ declare class WorkflowChatTransport<UI_MESSAGE extends UIMessage> implements Cha
997
1085
  private onFinish;
998
1086
  }
999
1087
 
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 };
1088
+ 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 };