@ai-sdk/workflow 0.0.0-bf6e4b15-20260402200305
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +52 -0
- package/LICENSE +13 -0
- package/README.md +62 -0
- package/dist/index.d.mts +739 -0
- package/dist/index.mjs +1261 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +77 -0
- package/src/do-stream-step.ts +329 -0
- package/src/index.ts +37 -0
- package/src/providers/mock-function-wrapper.ts +11 -0
- package/src/providers/mock.ts +123 -0
- package/src/serializable-schema.ts +81 -0
- package/src/stream-iterator.ts +46 -0
- package/src/stream-text-iterator.ts +444 -0
- package/src/telemetry.ts +199 -0
- package/src/test/agent-e2e-workflows.ts +507 -0
- package/src/test/calculate-workflow.ts +19 -0
- package/src/to-ui-message-chunk.ts +214 -0
- package/src/types.ts +11 -0
- package/src/workflow-agent.ts +1647 -0
- package/src/workflow-chat-transport.ts +359 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,739 @@
|
|
|
1
|
+
import { LanguageModelV4, SharedV4ProviderOptions, LanguageModelV4Prompt, LanguageModelV4CallOptions, LanguageModelV4StreamPart } from '@ai-sdk/provider';
|
|
2
|
+
import { ToolSet, SystemModelMessage, ToolChoice, StepResult, StreamTextOnStepFinishCallback, ModelMessage, FinishReason, LanguageModelUsage, Experimental_ModelCallStreamPart, StopCondition, LanguageModelResponseMetadata, ToolCallRepairFunction, UIMessage, UIMessageChunk } from 'ai';
|
|
3
|
+
export { Experimental_ModelCallStreamPart as ModelCallStreamPart, Output, ToolCallRepairFunction } from 'ai';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Shared types for AI SDK V4.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Language model type for AI SDK V4.
|
|
11
|
+
*
|
|
12
|
+
* This is a simple alias for LanguageModelV4 from @ai-sdk/provider.
|
|
13
|
+
*/
|
|
14
|
+
type CompatibleLanguageModel = LanguageModelV4;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Infer the type of the tools of a workflow agent.
|
|
18
|
+
*/
|
|
19
|
+
type InferWorkflowAgentTools<WORKFLOW_AGENT> = WORKFLOW_AGENT extends WorkflowAgent<infer TOOLS> ? TOOLS : never;
|
|
20
|
+
/**
|
|
21
|
+
* Infer the UI message type of a workflow agent.
|
|
22
|
+
*/
|
|
23
|
+
type InferWorkflowAgentUIMessage<WORKFLOW_AGENT, MESSAGE_METADATA = unknown> = UIMessage<MESSAGE_METADATA>;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Output specification interface for structured outputs.
|
|
27
|
+
* Use `Output.object({ schema })` or `Output.text()` to create an output specification.
|
|
28
|
+
*/
|
|
29
|
+
interface OutputSpecification<OUTPUT, PARTIAL> {
|
|
30
|
+
readonly name: string;
|
|
31
|
+
responseFormat: PromiseLike<LanguageModelV4CallOptions['responseFormat']>;
|
|
32
|
+
parsePartialOutput(options: {
|
|
33
|
+
text: string;
|
|
34
|
+
}): Promise<{
|
|
35
|
+
partial: PARTIAL;
|
|
36
|
+
} | undefined>;
|
|
37
|
+
parseCompleteOutput(options: {
|
|
38
|
+
text: string;
|
|
39
|
+
}, context: {
|
|
40
|
+
response: LanguageModelResponseMetadata;
|
|
41
|
+
usage: LanguageModelUsage;
|
|
42
|
+
finishReason: FinishReason;
|
|
43
|
+
}): Promise<OUTPUT>;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Provider-specific options type. This is equivalent to SharedV4ProviderOptions from @ai-sdk/provider.
|
|
47
|
+
*/
|
|
48
|
+
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
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* A transformation that is applied to the stream.
|
|
86
|
+
*/
|
|
87
|
+
type StreamTextTransform<TTools extends ToolSet> = (options: {
|
|
88
|
+
tools: TTools;
|
|
89
|
+
stopStream: () => void;
|
|
90
|
+
}) => TransformStream<LanguageModelV4StreamPart, LanguageModelV4StreamPart>;
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Custom download function for URLs.
|
|
94
|
+
* The function receives an array of URLs with information about whether
|
|
95
|
+
* the model supports them directly.
|
|
96
|
+
*/
|
|
97
|
+
type DownloadFunction = (options: {
|
|
98
|
+
url: URL;
|
|
99
|
+
isUrlSupportedByModel: boolean;
|
|
100
|
+
}[]) => PromiseLike<({
|
|
101
|
+
data: Uint8Array;
|
|
102
|
+
mediaType: string | undefined;
|
|
103
|
+
} | null)[]>;
|
|
104
|
+
/**
|
|
105
|
+
* Generation settings that can be passed to the model.
|
|
106
|
+
* These map directly to LanguageModelV4CallOptions.
|
|
107
|
+
*/
|
|
108
|
+
interface GenerationSettings {
|
|
109
|
+
/**
|
|
110
|
+
* Maximum number of tokens to generate.
|
|
111
|
+
*/
|
|
112
|
+
maxOutputTokens?: number;
|
|
113
|
+
/**
|
|
114
|
+
* Temperature setting. The range depends on the provider and model.
|
|
115
|
+
* It is recommended to set either `temperature` or `topP`, but not both.
|
|
116
|
+
*/
|
|
117
|
+
temperature?: number;
|
|
118
|
+
/**
|
|
119
|
+
* Nucleus sampling. This is a number between 0 and 1.
|
|
120
|
+
* E.g. 0.1 would mean that only tokens with the top 10% probability mass are considered.
|
|
121
|
+
* It is recommended to set either `temperature` or `topP`, but not both.
|
|
122
|
+
*/
|
|
123
|
+
topP?: number;
|
|
124
|
+
/**
|
|
125
|
+
* Only sample from the top K options for each subsequent token.
|
|
126
|
+
* Used to remove "long tail" low probability responses.
|
|
127
|
+
* Recommended for advanced use cases only. You usually only need to use temperature.
|
|
128
|
+
*/
|
|
129
|
+
topK?: number;
|
|
130
|
+
/**
|
|
131
|
+
* Presence penalty setting. It affects the likelihood of the model to
|
|
132
|
+
* repeat information that is already in the prompt.
|
|
133
|
+
* The presence penalty is a number between -1 (increase repetition)
|
|
134
|
+
* and 1 (maximum penalty, decrease repetition). 0 means no penalty.
|
|
135
|
+
*/
|
|
136
|
+
presencePenalty?: number;
|
|
137
|
+
/**
|
|
138
|
+
* Frequency penalty setting. It affects the likelihood of the model
|
|
139
|
+
* to repeatedly use the same words or phrases.
|
|
140
|
+
* The frequency penalty is a number between -1 (increase repetition)
|
|
141
|
+
* and 1 (maximum penalty, decrease repetition). 0 means no penalty.
|
|
142
|
+
*/
|
|
143
|
+
frequencyPenalty?: number;
|
|
144
|
+
/**
|
|
145
|
+
* Stop sequences. If set, the model will stop generating text when one of the stop sequences is generated.
|
|
146
|
+
* Providers may have limits on the number of stop sequences.
|
|
147
|
+
*/
|
|
148
|
+
stopSequences?: string[];
|
|
149
|
+
/**
|
|
150
|
+
* The seed (integer) to use for random sampling. If set and supported
|
|
151
|
+
* by the model, calls will generate deterministic results.
|
|
152
|
+
*/
|
|
153
|
+
seed?: number;
|
|
154
|
+
/**
|
|
155
|
+
* Maximum number of retries. Set to 0 to disable retries.
|
|
156
|
+
* Note: In workflow context, retries are typically handled by the workflow step mechanism.
|
|
157
|
+
* @default 2
|
|
158
|
+
*/
|
|
159
|
+
maxRetries?: number;
|
|
160
|
+
/**
|
|
161
|
+
* Abort signal for cancelling the operation.
|
|
162
|
+
*/
|
|
163
|
+
abortSignal?: AbortSignal;
|
|
164
|
+
/**
|
|
165
|
+
* Additional HTTP headers to be sent with the request.
|
|
166
|
+
* Only applicable for HTTP-based providers.
|
|
167
|
+
*/
|
|
168
|
+
headers?: Record<string, string | undefined>;
|
|
169
|
+
/**
|
|
170
|
+
* Additional provider-specific options. They are passed through
|
|
171
|
+
* to the provider from the AI SDK and enable provider-specific
|
|
172
|
+
* functionality that can be fully encapsulated in the provider.
|
|
173
|
+
*/
|
|
174
|
+
providerOptions?: ProviderOptions;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Information passed to the prepareStep callback.
|
|
178
|
+
*/
|
|
179
|
+
interface PrepareStepInfo<TTools extends ToolSet = ToolSet> {
|
|
180
|
+
/**
|
|
181
|
+
* The current model configuration (string or function).
|
|
182
|
+
* The function should return a LanguageModelV4 instance.
|
|
183
|
+
*/
|
|
184
|
+
model: string | (() => Promise<CompatibleLanguageModel>);
|
|
185
|
+
/**
|
|
186
|
+
* The current step number (0-indexed).
|
|
187
|
+
*/
|
|
188
|
+
stepNumber: number;
|
|
189
|
+
/**
|
|
190
|
+
* All previous steps with their results.
|
|
191
|
+
*/
|
|
192
|
+
steps: StepResult<TTools, any>[];
|
|
193
|
+
/**
|
|
194
|
+
* The messages that will be sent to the model.
|
|
195
|
+
* This is the LanguageModelV4Prompt format used internally.
|
|
196
|
+
*/
|
|
197
|
+
messages: LanguageModelV4Prompt;
|
|
198
|
+
/**
|
|
199
|
+
* The context passed via the experimental_context setting (experimental).
|
|
200
|
+
*/
|
|
201
|
+
experimental_context: unknown;
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Return type from the prepareStep callback.
|
|
205
|
+
* All properties are optional - only return the ones you want to override.
|
|
206
|
+
*/
|
|
207
|
+
interface PrepareStepResult extends Partial<GenerationSettings> {
|
|
208
|
+
/**
|
|
209
|
+
* Override the model for this step.
|
|
210
|
+
* The function should return a LanguageModelV4 instance.
|
|
211
|
+
*/
|
|
212
|
+
model?: string | (() => Promise<CompatibleLanguageModel>);
|
|
213
|
+
/**
|
|
214
|
+
* Override the system message for this step.
|
|
215
|
+
*/
|
|
216
|
+
system?: string;
|
|
217
|
+
/**
|
|
218
|
+
* Override the messages for this step.
|
|
219
|
+
* Use this for context management or message injection.
|
|
220
|
+
*/
|
|
221
|
+
messages?: LanguageModelV4Prompt;
|
|
222
|
+
/**
|
|
223
|
+
* Override the tool choice for this step.
|
|
224
|
+
*/
|
|
225
|
+
toolChoice?: ToolChoice<ToolSet>;
|
|
226
|
+
/**
|
|
227
|
+
* Override the active tools for this step.
|
|
228
|
+
* Limits the tools that are available for the model to call.
|
|
229
|
+
*/
|
|
230
|
+
activeTools?: string[];
|
|
231
|
+
/**
|
|
232
|
+
* Context that is passed into tool execution. Experimental.
|
|
233
|
+
* Changing the context will affect the context in this step and all subsequent steps.
|
|
234
|
+
*/
|
|
235
|
+
experimental_context?: unknown;
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Callback function called before each step in the agent loop.
|
|
239
|
+
* Use this to modify settings, manage context, or implement dynamic behavior.
|
|
240
|
+
*/
|
|
241
|
+
type PrepareStepCallback<TTools extends ToolSet = ToolSet> = (info: PrepareStepInfo<TTools>) => PrepareStepResult | Promise<PrepareStepResult>;
|
|
242
|
+
/**
|
|
243
|
+
* Options passed to the prepareCall callback.
|
|
244
|
+
*/
|
|
245
|
+
interface PrepareCallOptions<TTools extends ToolSet = ToolSet> extends Partial<GenerationSettings> {
|
|
246
|
+
model: string | (() => Promise<CompatibleLanguageModel>);
|
|
247
|
+
tools: TTools;
|
|
248
|
+
instructions?: string | SystemModelMessage | Array<SystemModelMessage>;
|
|
249
|
+
toolChoice?: ToolChoice<TTools>;
|
|
250
|
+
experimental_telemetry?: TelemetrySettings;
|
|
251
|
+
experimental_context?: unknown;
|
|
252
|
+
messages: ModelMessage[];
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Result of the prepareCall callback. All fields are optional —
|
|
256
|
+
* only returned fields override the defaults.
|
|
257
|
+
* Note: `tools` cannot be overridden via prepareCall because they are
|
|
258
|
+
* bound at construction time for type safety.
|
|
259
|
+
*/
|
|
260
|
+
type PrepareCallResult<TTools extends ToolSet = ToolSet> = Partial<Omit<PrepareCallOptions<TTools>, 'tools'>>;
|
|
261
|
+
/**
|
|
262
|
+
* Callback called once before the agent loop starts to transform call parameters.
|
|
263
|
+
*/
|
|
264
|
+
type PrepareCallCallback<TTools extends ToolSet = ToolSet> = (options: PrepareCallOptions<TTools>) => PrepareCallResult<TTools> | Promise<PrepareCallResult<TTools>>;
|
|
265
|
+
/**
|
|
266
|
+
* Configuration options for creating a {@link WorkflowAgent} instance.
|
|
267
|
+
*/
|
|
268
|
+
interface WorkflowAgentOptions<TTools extends ToolSet = ToolSet> extends GenerationSettings {
|
|
269
|
+
/**
|
|
270
|
+
* The model provider to use for the agent.
|
|
271
|
+
*
|
|
272
|
+
* 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.
|
|
274
|
+
*/
|
|
275
|
+
model: string | (() => Promise<CompatibleLanguageModel>);
|
|
276
|
+
/**
|
|
277
|
+
* A set of tools available to the agent.
|
|
278
|
+
* Tools can be implemented as workflow steps for automatic retries and persistence,
|
|
279
|
+
* or as regular workflow-level logic using core library features like sleep() and Hooks.
|
|
280
|
+
*/
|
|
281
|
+
tools?: TTools;
|
|
282
|
+
/**
|
|
283
|
+
* Agent instructions. Can be a string, a SystemModelMessage, or an array of SystemModelMessages.
|
|
284
|
+
* Supports provider-specific options (e.g., caching) when using the SystemModelMessage form.
|
|
285
|
+
*/
|
|
286
|
+
instructions?: string | SystemModelMessage | Array<SystemModelMessage>;
|
|
287
|
+
/**
|
|
288
|
+
* Optional system prompt to guide the agent's behavior.
|
|
289
|
+
* @deprecated Use `instructions` instead.
|
|
290
|
+
*/
|
|
291
|
+
system?: string;
|
|
292
|
+
/**
|
|
293
|
+
* The tool choice strategy. Default: 'auto'.
|
|
294
|
+
*/
|
|
295
|
+
toolChoice?: ToolChoice<TTools>;
|
|
296
|
+
/**
|
|
297
|
+
* Optional telemetry configuration (experimental).
|
|
298
|
+
*/
|
|
299
|
+
experimental_telemetry?: TelemetrySettings;
|
|
300
|
+
/**
|
|
301
|
+
* Default context that is passed into tool execution for every stream call on this agent.
|
|
302
|
+
*
|
|
303
|
+
* Per-stream `experimental_context` values passed to `stream()` override this default.
|
|
304
|
+
* Experimental (can break in patch releases).
|
|
305
|
+
* @default undefined
|
|
306
|
+
*/
|
|
307
|
+
experimental_context?: unknown;
|
|
308
|
+
/**
|
|
309
|
+
* Default callback function called before each step in the agent loop.
|
|
310
|
+
* Use this to modify settings, manage context, or inject messages dynamically
|
|
311
|
+
* for every stream call on this agent instance.
|
|
312
|
+
*
|
|
313
|
+
* Per-stream `prepareStep` values passed to `stream()` override this default.
|
|
314
|
+
*/
|
|
315
|
+
prepareStep?: PrepareStepCallback<TTools>;
|
|
316
|
+
/**
|
|
317
|
+
* Callback function to be called after each step completes.
|
|
318
|
+
*/
|
|
319
|
+
onStepFinish?: StreamTextOnStepFinishCallback<ToolSet, any>;
|
|
320
|
+
/**
|
|
321
|
+
* Callback that is called when the LLM response and all request tool executions are finished.
|
|
322
|
+
*/
|
|
323
|
+
onFinish?: StreamTextOnFinishCallback<ToolSet>;
|
|
324
|
+
/**
|
|
325
|
+
* Callback called when the agent starts streaming, before any LLM calls.
|
|
326
|
+
*/
|
|
327
|
+
experimental_onStart?: WorkflowAgentOnStartCallback;
|
|
328
|
+
/**
|
|
329
|
+
* Callback called before each step (LLM call) begins.
|
|
330
|
+
*/
|
|
331
|
+
experimental_onStepStart?: WorkflowAgentOnStepStartCallback;
|
|
332
|
+
/**
|
|
333
|
+
* Callback called before a tool's execute function runs.
|
|
334
|
+
*/
|
|
335
|
+
experimental_onToolCallStart?: WorkflowAgentOnToolCallStartCallback;
|
|
336
|
+
/**
|
|
337
|
+
* Callback called after a tool execution completes.
|
|
338
|
+
*/
|
|
339
|
+
experimental_onToolCallFinish?: WorkflowAgentOnToolCallFinishCallback;
|
|
340
|
+
/**
|
|
341
|
+
* Prepare the parameters for the stream call.
|
|
342
|
+
* Called once before the agent loop starts. Use this to transform
|
|
343
|
+
* model, tools, instructions, or other settings based on runtime context.
|
|
344
|
+
*/
|
|
345
|
+
prepareCall?: PrepareCallCallback<TTools>;
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* Callback that is called when the LLM response and all request tool executions are finished.
|
|
349
|
+
*/
|
|
350
|
+
type StreamTextOnFinishCallback<TTools extends ToolSet = ToolSet, OUTPUT = never> = (event: {
|
|
351
|
+
/**
|
|
352
|
+
* Details for all steps.
|
|
353
|
+
*/
|
|
354
|
+
readonly steps: StepResult<TTools, any>[];
|
|
355
|
+
/**
|
|
356
|
+
* The final messages including all tool calls and results.
|
|
357
|
+
*/
|
|
358
|
+
readonly messages: ModelMessage[];
|
|
359
|
+
/**
|
|
360
|
+
* The text output from the last step.
|
|
361
|
+
*/
|
|
362
|
+
readonly text: string;
|
|
363
|
+
/**
|
|
364
|
+
* The finish reason from the last step.
|
|
365
|
+
*/
|
|
366
|
+
readonly finishReason: FinishReason;
|
|
367
|
+
/**
|
|
368
|
+
* The total token usage across all steps.
|
|
369
|
+
*/
|
|
370
|
+
readonly totalUsage: LanguageModelUsage;
|
|
371
|
+
/**
|
|
372
|
+
* Context that is passed into tool execution.
|
|
373
|
+
*/
|
|
374
|
+
readonly experimental_context: unknown;
|
|
375
|
+
/**
|
|
376
|
+
* The generated structured output. It uses the `experimental_output` specification.
|
|
377
|
+
* Only available when `experimental_output` is specified.
|
|
378
|
+
*/
|
|
379
|
+
readonly experimental_output: OUTPUT;
|
|
380
|
+
}) => PromiseLike<void> | void;
|
|
381
|
+
/**
|
|
382
|
+
* Callback that is invoked when an error occurs during streaming.
|
|
383
|
+
*/
|
|
384
|
+
type StreamTextOnErrorCallback = (event: {
|
|
385
|
+
error: unknown;
|
|
386
|
+
}) => PromiseLike<void> | void;
|
|
387
|
+
/**
|
|
388
|
+
* Callback that is set using the `onAbort` option.
|
|
389
|
+
*/
|
|
390
|
+
type StreamTextOnAbortCallback<TTools extends ToolSet = ToolSet> = (event: {
|
|
391
|
+
/**
|
|
392
|
+
* Details for all previously finished steps.
|
|
393
|
+
*/
|
|
394
|
+
readonly steps: StepResult<TTools, any>[];
|
|
395
|
+
}) => PromiseLike<void> | void;
|
|
396
|
+
/**
|
|
397
|
+
* Callback that is called when the agent starts streaming, before any LLM calls.
|
|
398
|
+
*/
|
|
399
|
+
type WorkflowAgentOnStartCallback = (event: {
|
|
400
|
+
/** The model being used */
|
|
401
|
+
readonly model: string | (() => Promise<CompatibleLanguageModel>);
|
|
402
|
+
/** The messages being sent */
|
|
403
|
+
readonly messages: ModelMessage[];
|
|
404
|
+
}) => PromiseLike<void> | void;
|
|
405
|
+
/**
|
|
406
|
+
* Callback that is called before each step (LLM call) begins.
|
|
407
|
+
*/
|
|
408
|
+
type WorkflowAgentOnStepStartCallback = (event: {
|
|
409
|
+
/** The current step number (0-based) */
|
|
410
|
+
readonly stepNumber: number;
|
|
411
|
+
/** The model being used for this step */
|
|
412
|
+
readonly model: string | (() => Promise<CompatibleLanguageModel>);
|
|
413
|
+
/** The messages being sent for this step */
|
|
414
|
+
readonly messages: ModelMessage[];
|
|
415
|
+
}) => PromiseLike<void> | void;
|
|
416
|
+
/**
|
|
417
|
+
* Callback that is called before a tool's execute function runs.
|
|
418
|
+
*/
|
|
419
|
+
type WorkflowAgentOnToolCallStartCallback = (event: {
|
|
420
|
+
/** The tool call being executed */
|
|
421
|
+
readonly toolCall: ToolCall;
|
|
422
|
+
}) => PromiseLike<void> | void;
|
|
423
|
+
/**
|
|
424
|
+
* Callback that is called after a tool execution completes.
|
|
425
|
+
*/
|
|
426
|
+
type WorkflowAgentOnToolCallFinishCallback = (event: {
|
|
427
|
+
/** The tool call that was executed */
|
|
428
|
+
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;
|
|
433
|
+
}) => PromiseLike<void> | void;
|
|
434
|
+
/**
|
|
435
|
+
* Options for the {@link WorkflowAgent.stream} method.
|
|
436
|
+
*/
|
|
437
|
+
interface WorkflowAgentStreamOptions<TTools extends ToolSet = ToolSet, OUTPUT = never, PARTIAL_OUTPUT = never> extends Partial<GenerationSettings> {
|
|
438
|
+
/**
|
|
439
|
+
* The conversation messages to process. Should follow the AI SDK's ModelMessage format.
|
|
440
|
+
*/
|
|
441
|
+
messages: ModelMessage[];
|
|
442
|
+
/**
|
|
443
|
+
* Optional system prompt override. If provided, overrides the system prompt from the constructor.
|
|
444
|
+
*/
|
|
445
|
+
system?: string;
|
|
446
|
+
/**
|
|
447
|
+
* A WritableStream that receives raw LanguageModelV4StreamPart chunks in real-time
|
|
448
|
+
* as the model generates them. This enables streaming to the client without
|
|
449
|
+
* coupling WorkflowAgent to UIMessageChunk format.
|
|
450
|
+
*
|
|
451
|
+
* Convert to UIMessageChunks at the response boundary using
|
|
452
|
+
* `createUIMessageChunkTransform()` from `@ai-sdk/workflow`.
|
|
453
|
+
*
|
|
454
|
+
* @example
|
|
455
|
+
* ```typescript
|
|
456
|
+
* // In the workflow:
|
|
457
|
+
* await agent.stream({
|
|
458
|
+
* messages,
|
|
459
|
+
* writable: getWritable<ModelCallStreamPart>(),
|
|
460
|
+
* });
|
|
461
|
+
*
|
|
462
|
+
* // In the route handler:
|
|
463
|
+
* return createUIMessageStreamResponse({
|
|
464
|
+
* stream: run.readable.pipeThrough(createModelCallToUIChunkTransform()),
|
|
465
|
+
* });
|
|
466
|
+
* ```
|
|
467
|
+
*/
|
|
468
|
+
writable?: WritableStream<Experimental_ModelCallStreamPart<ToolSet>>;
|
|
469
|
+
/**
|
|
470
|
+
* Condition for stopping the generation when there are tool results in the last step.
|
|
471
|
+
* When the condition is an array, any of the conditions can be met to stop the generation.
|
|
472
|
+
*/
|
|
473
|
+
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
|
+
/**
|
|
481
|
+
* The tool choice strategy. Default: 'auto'.
|
|
482
|
+
* Overrides the toolChoice from the constructor if provided.
|
|
483
|
+
*/
|
|
484
|
+
toolChoice?: ToolChoice<TTools>;
|
|
485
|
+
/**
|
|
486
|
+
* Limits the tools that are available for the model to call without
|
|
487
|
+
* changing the tool call and result types in the result.
|
|
488
|
+
*/
|
|
489
|
+
activeTools?: Array<keyof NoInfer<TTools>>;
|
|
490
|
+
/**
|
|
491
|
+
* Optional telemetry configuration (experimental).
|
|
492
|
+
*/
|
|
493
|
+
experimental_telemetry?: TelemetrySettings;
|
|
494
|
+
/**
|
|
495
|
+
* Context that is passed into tool execution.
|
|
496
|
+
* Experimental (can break in patch releases).
|
|
497
|
+
* @default undefined
|
|
498
|
+
*/
|
|
499
|
+
experimental_context?: unknown;
|
|
500
|
+
/**
|
|
501
|
+
* Optional specification for parsing structured outputs from the LLM response.
|
|
502
|
+
* Use `Output.object({ schema })` for structured output or `Output.text()` for text output.
|
|
503
|
+
*
|
|
504
|
+
* @example
|
|
505
|
+
* ```typescript
|
|
506
|
+
* import { Output } from '@workflow/ai';
|
|
507
|
+
* import { z } from 'zod';
|
|
508
|
+
*
|
|
509
|
+
* const result = await agent.stream({
|
|
510
|
+
* messages: [...],
|
|
511
|
+
* writable: getWritable(),
|
|
512
|
+
* experimental_output: Output.object({
|
|
513
|
+
* schema: z.object({
|
|
514
|
+
* sentiment: z.enum(['positive', 'negative', 'neutral']),
|
|
515
|
+
* confidence: z.number(),
|
|
516
|
+
* }),
|
|
517
|
+
* }),
|
|
518
|
+
* });
|
|
519
|
+
*
|
|
520
|
+
* console.log(result.experimental_output); // { sentiment: 'positive', confidence: 0.95 }
|
|
521
|
+
* ```
|
|
522
|
+
*/
|
|
523
|
+
experimental_output?: OutputSpecification<OUTPUT, PARTIAL_OUTPUT>;
|
|
524
|
+
/**
|
|
525
|
+
* Whether to include raw chunks from the provider in the stream.
|
|
526
|
+
* When enabled, you will receive raw chunks with type 'raw' that contain the unprocessed data from the provider.
|
|
527
|
+
* This allows access to cutting-edge provider features not yet wrapped by the AI SDK.
|
|
528
|
+
* Defaults to false.
|
|
529
|
+
*/
|
|
530
|
+
includeRawChunks?: boolean;
|
|
531
|
+
/**
|
|
532
|
+
* A function that attempts to repair a tool call that failed to parse.
|
|
533
|
+
*/
|
|
534
|
+
experimental_repairToolCall?: ToolCallRepairFunction<TTools>;
|
|
535
|
+
/**
|
|
536
|
+
* Optional stream transformations.
|
|
537
|
+
* They are applied in the order they are provided.
|
|
538
|
+
* The stream transformations must maintain the stream structure for streamText to work correctly.
|
|
539
|
+
*/
|
|
540
|
+
experimental_transform?: StreamTextTransform<TTools> | Array<StreamTextTransform<TTools>>;
|
|
541
|
+
/**
|
|
542
|
+
* Custom download function to use for URLs.
|
|
543
|
+
* By default, files are downloaded if the model does not support the URL for the given media type.
|
|
544
|
+
*/
|
|
545
|
+
experimental_download?: DownloadFunction;
|
|
546
|
+
/**
|
|
547
|
+
* Callback function to be called after each step completes.
|
|
548
|
+
*/
|
|
549
|
+
onStepFinish?: StreamTextOnStepFinishCallback<TTools, any>;
|
|
550
|
+
/**
|
|
551
|
+
* Callback that is invoked when an error occurs during streaming.
|
|
552
|
+
* You can use it to log errors.
|
|
553
|
+
*/
|
|
554
|
+
onError?: StreamTextOnErrorCallback;
|
|
555
|
+
/**
|
|
556
|
+
* Callback that is called when the LLM response and all request tool executions
|
|
557
|
+
* (for tools that have an `execute` function) are finished.
|
|
558
|
+
*/
|
|
559
|
+
onFinish?: StreamTextOnFinishCallback<TTools, OUTPUT>;
|
|
560
|
+
/**
|
|
561
|
+
* Callback that is called when the operation is aborted.
|
|
562
|
+
*/
|
|
563
|
+
onAbort?: StreamTextOnAbortCallback<TTools>;
|
|
564
|
+
/**
|
|
565
|
+
* Callback called when the agent starts streaming, before any LLM calls.
|
|
566
|
+
*/
|
|
567
|
+
experimental_onStart?: WorkflowAgentOnStartCallback;
|
|
568
|
+
/**
|
|
569
|
+
* Callback called before each step (LLM call) begins.
|
|
570
|
+
*/
|
|
571
|
+
experimental_onStepStart?: WorkflowAgentOnStepStartCallback;
|
|
572
|
+
/**
|
|
573
|
+
* Callback called before a tool's execute function runs.
|
|
574
|
+
*/
|
|
575
|
+
experimental_onToolCallStart?: WorkflowAgentOnToolCallStartCallback;
|
|
576
|
+
/**
|
|
577
|
+
* Callback called after a tool execution completes.
|
|
578
|
+
*/
|
|
579
|
+
experimental_onToolCallFinish?: WorkflowAgentOnToolCallFinishCallback;
|
|
580
|
+
/**
|
|
581
|
+
* Callback function called before each step in the agent loop.
|
|
582
|
+
* Use this to modify settings, manage context, or inject messages dynamically.
|
|
583
|
+
*
|
|
584
|
+
* @example
|
|
585
|
+
* ```typescript
|
|
586
|
+
* prepareStep: async ({ messages, stepNumber }) => {
|
|
587
|
+
* // Inject messages from a queue
|
|
588
|
+
* const queuedMessages = await getQueuedMessages();
|
|
589
|
+
* if (queuedMessages.length > 0) {
|
|
590
|
+
* return {
|
|
591
|
+
* messages: [...messages, ...queuedMessages],
|
|
592
|
+
* };
|
|
593
|
+
* }
|
|
594
|
+
* return {};
|
|
595
|
+
* }
|
|
596
|
+
* ```
|
|
597
|
+
*/
|
|
598
|
+
prepareStep?: PrepareStepCallback<TTools>;
|
|
599
|
+
/**
|
|
600
|
+
* Timeout in milliseconds for the stream operation.
|
|
601
|
+
* When specified, creates an AbortSignal that will abort the operation after the given time.
|
|
602
|
+
* If both `timeout` and `abortSignal` are provided, whichever triggers first will abort.
|
|
603
|
+
*/
|
|
604
|
+
timeout?: number;
|
|
605
|
+
}
|
|
606
|
+
/**
|
|
607
|
+
* A tool call made by the model. Matches the AI SDK's tool call shape.
|
|
608
|
+
*/
|
|
609
|
+
interface ToolCall {
|
|
610
|
+
/** Discriminator for content part arrays */
|
|
611
|
+
type: 'tool-call';
|
|
612
|
+
/** The unique identifier of the tool call */
|
|
613
|
+
toolCallId: string;
|
|
614
|
+
/** The name of the tool that was called */
|
|
615
|
+
toolName: string;
|
|
616
|
+
/** The parsed input arguments for the tool call */
|
|
617
|
+
input: unknown;
|
|
618
|
+
}
|
|
619
|
+
/**
|
|
620
|
+
* A tool result from executing a tool. Matches the AI SDK's tool result shape.
|
|
621
|
+
*/
|
|
622
|
+
interface ToolResult {
|
|
623
|
+
/** Discriminator for content part arrays */
|
|
624
|
+
type: 'tool-result';
|
|
625
|
+
/** The tool call ID this result corresponds to */
|
|
626
|
+
toolCallId: string;
|
|
627
|
+
/** The name of the tool that was executed */
|
|
628
|
+
toolName: string;
|
|
629
|
+
/** The parsed input arguments that were passed to the tool */
|
|
630
|
+
input: unknown;
|
|
631
|
+
/** The output produced by the tool */
|
|
632
|
+
output: unknown;
|
|
633
|
+
}
|
|
634
|
+
/**
|
|
635
|
+
* Result of the WorkflowAgent.stream method.
|
|
636
|
+
*/
|
|
637
|
+
interface WorkflowAgentStreamResult<TTools extends ToolSet = ToolSet, OUTPUT = never> {
|
|
638
|
+
/**
|
|
639
|
+
* The final messages including all tool calls and results.
|
|
640
|
+
*/
|
|
641
|
+
messages: ModelMessage[];
|
|
642
|
+
/**
|
|
643
|
+
* Details for all steps.
|
|
644
|
+
*/
|
|
645
|
+
steps: StepResult<TTools, any>[];
|
|
646
|
+
/**
|
|
647
|
+
* The tool calls from the last step.
|
|
648
|
+
* Includes all tool calls regardless of whether they were executed.
|
|
649
|
+
*
|
|
650
|
+
* When the agent stops because a tool without an `execute` function was called,
|
|
651
|
+
* this array will contain those calls. Compare with `toolResults` to find
|
|
652
|
+
* unresolved tool calls that need client-side handling:
|
|
653
|
+
*
|
|
654
|
+
* ```ts
|
|
655
|
+
* const unresolved = result.toolCalls.filter(
|
|
656
|
+
* tc => !result.toolResults.some(tr => tr.toolCallId === tc.toolCallId)
|
|
657
|
+
* );
|
|
658
|
+
* ```
|
|
659
|
+
*
|
|
660
|
+
* This matches the AI SDK's `GenerateTextResult.toolCalls` behavior.
|
|
661
|
+
*/
|
|
662
|
+
toolCalls: ToolCall[];
|
|
663
|
+
/**
|
|
664
|
+
* The tool results from the last step.
|
|
665
|
+
* Only includes results for tools that were actually executed (server-side or provider-executed).
|
|
666
|
+
* Tools without an `execute` function will NOT have entries here.
|
|
667
|
+
*
|
|
668
|
+
* This matches the AI SDK's `GenerateTextResult.toolResults` behavior.
|
|
669
|
+
*/
|
|
670
|
+
toolResults: ToolResult[];
|
|
671
|
+
/**
|
|
672
|
+
* The generated structured output. It uses the `experimental_output` specification.
|
|
673
|
+
* Only available when `experimental_output` is specified.
|
|
674
|
+
*/
|
|
675
|
+
experimental_output: OUTPUT;
|
|
676
|
+
}
|
|
677
|
+
/**
|
|
678
|
+
* A class for building durable AI agents within workflows.
|
|
679
|
+
*
|
|
680
|
+
* WorkflowAgent enables you to create AI-powered agents that can maintain state
|
|
681
|
+
* across workflow steps, call tools, and gracefully handle interruptions and resumptions.
|
|
682
|
+
* It integrates seamlessly with the AI SDK and the Workflow DevKit for
|
|
683
|
+
* production-grade reliability.
|
|
684
|
+
*
|
|
685
|
+
* @example
|
|
686
|
+
* ```typescript
|
|
687
|
+
* const agent = new WorkflowAgent({
|
|
688
|
+
* model: 'anthropic/claude-opus',
|
|
689
|
+
* tools: {
|
|
690
|
+
* getWeather: {
|
|
691
|
+
* description: 'Get weather for a location',
|
|
692
|
+
* inputSchema: z.object({ location: z.string() }),
|
|
693
|
+
* execute: getWeatherStep,
|
|
694
|
+
* },
|
|
695
|
+
* },
|
|
696
|
+
* instructions: 'You are a helpful weather assistant.',
|
|
697
|
+
* });
|
|
698
|
+
*
|
|
699
|
+
* const result = await agent.stream({
|
|
700
|
+
* messages: [{ role: 'user', content: 'What is the weather?' }],
|
|
701
|
+
* });
|
|
702
|
+
* ```
|
|
703
|
+
*/
|
|
704
|
+
declare class WorkflowAgent<TBaseTools extends ToolSet = ToolSet> {
|
|
705
|
+
private model;
|
|
706
|
+
/**
|
|
707
|
+
* The tool set configured for this agent.
|
|
708
|
+
*/
|
|
709
|
+
readonly tools: TBaseTools;
|
|
710
|
+
private instructions?;
|
|
711
|
+
private generationSettings;
|
|
712
|
+
private toolChoice?;
|
|
713
|
+
private telemetry?;
|
|
714
|
+
private experimentalContext;
|
|
715
|
+
private prepareStep?;
|
|
716
|
+
private constructorOnStepFinish?;
|
|
717
|
+
private constructorOnFinish?;
|
|
718
|
+
private constructorOnStart?;
|
|
719
|
+
private constructorOnStepStart?;
|
|
720
|
+
private constructorOnToolCallStart?;
|
|
721
|
+
private constructorOnToolCallFinish?;
|
|
722
|
+
private prepareCall?;
|
|
723
|
+
constructor(options: WorkflowAgentOptions<TBaseTools>);
|
|
724
|
+
generate(): void;
|
|
725
|
+
stream<TTools extends TBaseTools = TBaseTools, OUTPUT = never, PARTIAL_OUTPUT = never>(options: WorkflowAgentStreamOptions<TTools, OUTPUT, PARTIAL_OUTPUT>): Promise<WorkflowAgentStreamResult<TTools, OUTPUT>>;
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
/**
|
|
729
|
+
* Convert a single ModelCallStreamPart to a UIMessageChunk.
|
|
730
|
+
* Returns undefined for parts that don't map to UI chunks.
|
|
731
|
+
*/
|
|
732
|
+
declare function toUIMessageChunk(part: Experimental_ModelCallStreamPart<ToolSet>): UIMessageChunk | undefined;
|
|
733
|
+
/**
|
|
734
|
+
* Create a TransformStream that converts ModelCallStreamPart to UIMessageChunk.
|
|
735
|
+
* Wraps toUIMessageChunk with start/start-step/finish-step lifecycle chunks.
|
|
736
|
+
*/
|
|
737
|
+
declare function createModelCallToUIChunkTransform(): TransformStream<Experimental_ModelCallStreamPart<ToolSet>, UIMessageChunk>;
|
|
738
|
+
|
|
739
|
+
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 StreamTextOnAbortCallback, type StreamTextOnErrorCallback, type StreamTextOnFinishCallback, type StreamTextTransform, type TelemetrySettings, WorkflowAgent, type WorkflowAgentOnStartCallback, type WorkflowAgentOnStepStartCallback, type WorkflowAgentOnToolCallFinishCallback, type WorkflowAgentOnToolCallStartCallback, type WorkflowAgentOptions, type WorkflowAgentStreamOptions, type WorkflowAgentStreamResult, createModelCallToUIChunkTransform, toUIMessageChunk };
|