ai 6.0.96 → 6.0.97
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 +6 -0
- package/dist/index.d.mts +137 -126
- package/dist/index.d.ts +137 -126
- package/dist/index.js +69 -12
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +69 -12
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.js +1 -1
- package/dist/internal/index.mjs +1 -1
- package/docs/03-agents/02-building-agents.mdx +47 -5
- package/docs/07-reference/01-ai-sdk-core/15-agent.mdx +25 -0
- package/docs/07-reference/01-ai-sdk-core/16-tool-loop-agent.mdx +478 -3
- package/package.json +1 -1
- package/src/agent/agent.ts +33 -1
- package/src/agent/create-agent-ui-stream-response.ts +1 -1
- package/src/agent/create-agent-ui-stream.ts +1 -1
- package/src/agent/index.ts +6 -2
- package/src/agent/pipe-agent-ui-stream-to-response.ts +1 -1
- package/src/agent/tool-loop-agent-settings.ts +61 -2
- package/src/agent/tool-loop-agent.ts +80 -20
- package/src/agent/tool-loop-agent-on-finish-callback.ts +0 -31
- package/src/agent/tool-loop-agent-on-step-finish-callback.ts +0 -11
package/dist/internal/index.js
CHANGED
|
@@ -153,7 +153,7 @@ var import_provider_utils2 = require("@ai-sdk/provider-utils");
|
|
|
153
153
|
var import_provider_utils3 = require("@ai-sdk/provider-utils");
|
|
154
154
|
|
|
155
155
|
// src/version.ts
|
|
156
|
-
var VERSION = true ? "6.0.
|
|
156
|
+
var VERSION = true ? "6.0.97" : "0.0.0-test";
|
|
157
157
|
|
|
158
158
|
// src/util/download/download.ts
|
|
159
159
|
var download = async ({
|
package/dist/internal/index.mjs
CHANGED
|
@@ -329,15 +329,39 @@ export async function POST(request: Request) {
|
|
|
329
329
|
}
|
|
330
330
|
```
|
|
331
331
|
|
|
332
|
-
###
|
|
332
|
+
### Lifecycle Callbacks
|
|
333
333
|
|
|
334
|
-
|
|
335
|
-
|
|
334
|
+
<Note type="warning">
|
|
335
|
+
Experimental callbacks are subject to breaking changes in incremental package
|
|
336
|
+
releases.
|
|
337
|
+
</Note>
|
|
338
|
+
|
|
339
|
+
Agents provide lifecycle callbacks that let you hook into different phases of the agent execution.
|
|
340
|
+
These are useful for logging, observability, debugging, and custom telemetry.
|
|
336
341
|
|
|
337
342
|
```ts
|
|
338
343
|
const result = await myAgent.generate({
|
|
339
344
|
prompt: 'Research and summarize the latest AI trends',
|
|
340
|
-
|
|
345
|
+
|
|
346
|
+
experimental_onStart({ model, functionId }) {
|
|
347
|
+
console.log('Agent started', { model: model.modelId, functionId });
|
|
348
|
+
},
|
|
349
|
+
|
|
350
|
+
experimental_onStepStart({ stepNumber, model }) {
|
|
351
|
+
console.log(`Step ${stepNumber} starting`, { model: model.modelId });
|
|
352
|
+
},
|
|
353
|
+
|
|
354
|
+
experimental_onToolCallStart({ toolCall }) {
|
|
355
|
+
console.log(`Tool call starting: ${toolCall.toolName}`);
|
|
356
|
+
},
|
|
357
|
+
|
|
358
|
+
experimental_onToolCallFinish({ toolCall, durationMs, success }) {
|
|
359
|
+
console.log(`Tool call finished: ${toolCall.toolName} (${durationMs}ms)`, {
|
|
360
|
+
success,
|
|
361
|
+
});
|
|
362
|
+
},
|
|
363
|
+
|
|
364
|
+
onStepFinish({ stepNumber, usage, finishReason, toolCalls }) {
|
|
341
365
|
console.log(`Step ${stepNumber} completed:`, {
|
|
342
366
|
inputTokens: usage.inputTokens,
|
|
343
367
|
outputTokens: usage.outputTokens,
|
|
@@ -345,10 +369,28 @@ const result = await myAgent.generate({
|
|
|
345
369
|
toolsUsed: toolCalls?.map(tc => tc.toolName),
|
|
346
370
|
});
|
|
347
371
|
},
|
|
372
|
+
|
|
373
|
+
onFinish({ totalUsage, steps }) {
|
|
374
|
+
console.log('Agent finished:', {
|
|
375
|
+
totalSteps: steps.length,
|
|
376
|
+
totalTokens: totalUsage.totalTokens,
|
|
377
|
+
});
|
|
378
|
+
},
|
|
348
379
|
});
|
|
349
380
|
```
|
|
350
381
|
|
|
351
|
-
|
|
382
|
+
The available lifecycle callbacks are:
|
|
383
|
+
|
|
384
|
+
- **`experimental_onStart`**: Called once when the agent operation begins, before any LLM calls. Receives model info, prompt, settings, and telemetry metadata.
|
|
385
|
+
- **`experimental_onStepStart`**: Called before each step (LLM call). Receives the step number, model, messages being sent, tools, and prior steps.
|
|
386
|
+
- **`experimental_onToolCallStart`**: Called right before a tool's `execute` function runs. Receives the tool call object with tool name, call ID, and input.
|
|
387
|
+
- **`experimental_onToolCallFinish`**: Called right after a tool's `execute` function completes or errors. Receives the tool call, `durationMs`, and a `success` discriminator (`output` when successful, `error` when failed).
|
|
388
|
+
- **`onStepFinish`**: Called after each step finishes. Receives step results including usage, finish reason, and tool calls.
|
|
389
|
+
- **`onFinish`**: Called when all steps are finished and the response is complete. Receives all step results, total usage, and telemetry metadata.
|
|
390
|
+
|
|
391
|
+
#### Constructor vs. Method Callbacks
|
|
392
|
+
|
|
393
|
+
All lifecycle callbacks can be defined in the constructor for agent-wide tracking, in the `generate()`/`stream()` call for per-call tracking, or both. When both are provided, both are called (constructor first, then the method callback):
|
|
352
394
|
|
|
353
395
|
```ts
|
|
354
396
|
const agent = new ToolLoopAgent({
|
|
@@ -65,10 +65,30 @@ export type AgentCallParameters<CALL_OPTIONS, TOOLS extends ToolSet = {}> = ([
|
|
|
65
65
|
* Can be used alongside abortSignal.
|
|
66
66
|
*/
|
|
67
67
|
timeout?: number | { totalMs?: number };
|
|
68
|
+
/**
|
|
69
|
+
* Callback that is called when the agent operation begins, before any LLM calls.
|
|
70
|
+
*/
|
|
71
|
+
experimental_onStart?: ToolLoopAgentOnStartCallback<TOOLS>;
|
|
72
|
+
/**
|
|
73
|
+
* Callback that is called when a step (LLM call) begins, before the provider is called.
|
|
74
|
+
*/
|
|
75
|
+
experimental_onStepStart?: ToolLoopAgentOnStepStartCallback<TOOLS>;
|
|
76
|
+
/**
|
|
77
|
+
* Callback that is called before each tool execution begins.
|
|
78
|
+
*/
|
|
79
|
+
experimental_onToolCallStart?: ToolLoopAgentOnToolCallStartCallback<TOOLS>;
|
|
80
|
+
/**
|
|
81
|
+
* Callback that is called after each tool execution completes.
|
|
82
|
+
*/
|
|
83
|
+
experimental_onToolCallFinish?: ToolLoopAgentOnToolCallFinishCallback<TOOLS>;
|
|
68
84
|
/**
|
|
69
85
|
* Callback that is called when each step (LLM call) is finished, including intermediate steps.
|
|
70
86
|
*/
|
|
71
87
|
onStepFinish?: ToolLoopAgentOnStepFinishCallback<TOOLS>;
|
|
88
|
+
/**
|
|
89
|
+
* Callback that is called when all steps are finished and the response is complete.
|
|
90
|
+
*/
|
|
91
|
+
onFinish?: ToolLoopAgentOnFinishCallback<TOOLS>;
|
|
72
92
|
};
|
|
73
93
|
|
|
74
94
|
/**
|
|
@@ -142,7 +162,12 @@ Both `generate()` and `stream()` accept an `AgentCallParameters<CALL_OPTIONS, TO
|
|
|
142
162
|
- `options` (optional): Additional call options when `CALL_OPTIONS` is not `never`
|
|
143
163
|
- `abortSignal` (optional): An `AbortSignal` to cancel the operation
|
|
144
164
|
- `timeout` (optional): A timeout in milliseconds. Can be specified as a number or as an object with a `totalMs` property. The call will be aborted if it takes longer than the specified timeout. Can be used alongside `abortSignal`.
|
|
165
|
+
- `experimental_onStart` (optional): Callback invoked when the agent operation begins, before any LLM calls. Experimental.
|
|
166
|
+
- `experimental_onStepStart` (optional): Callback invoked when a step (LLM call) begins, before the provider is called. Experimental.
|
|
167
|
+
- `experimental_onToolCallStart` (optional): Callback invoked right before a tool's execute function runs. Experimental.
|
|
168
|
+
- `experimental_onToolCallFinish` (optional): Callback invoked right after a tool's execute function completes or errors. Experimental.
|
|
145
169
|
- `onStepFinish` (optional): A callback invoked after each agent step (LLM/tool call) completes. Useful for tracking token usage or logging.
|
|
170
|
+
- `onFinish` (optional): A callback invoked when all steps are finished and the response is complete.
|
|
146
171
|
|
|
147
172
|
## Example: Custom Agent Implementation
|
|
148
173
|
|
|
@@ -104,19 +104,424 @@ To see `ToolLoopAgent` in action, check out [these examples](#examples).
|
|
|
104
104
|
description:
|
|
105
105
|
'Optional callback to attempt automatic recovery when a tool call cannot be parsed.',
|
|
106
106
|
},
|
|
107
|
+
{
|
|
108
|
+
name: 'experimental_onStart',
|
|
109
|
+
type: 'ToolLoopAgentOnStartCallback',
|
|
110
|
+
isOptional: true,
|
|
111
|
+
description:
|
|
112
|
+
'Callback that is called when the agent operation begins, before any LLM calls are made. Useful for logging, analytics, or initializing state. If also specified in `generate()` or `stream()`, both callbacks are called (constructor first). Experimental (can break in patch releases).',
|
|
113
|
+
properties: [
|
|
114
|
+
{
|
|
115
|
+
type: 'OnStartEvent',
|
|
116
|
+
parameters: [
|
|
117
|
+
{
|
|
118
|
+
name: 'model',
|
|
119
|
+
type: '{ provider: string; modelId: string }',
|
|
120
|
+
description: 'The model being used for the generation.',
|
|
121
|
+
},
|
|
122
|
+
{
|
|
123
|
+
name: 'system',
|
|
124
|
+
type: 'string | SystemModelMessage | Array<SystemModelMessage> | undefined',
|
|
125
|
+
description: 'The system message(s) provided to the model.',
|
|
126
|
+
},
|
|
127
|
+
{
|
|
128
|
+
name: 'prompt',
|
|
129
|
+
type: 'string | Array<ModelMessage> | undefined',
|
|
130
|
+
description:
|
|
131
|
+
'The prompt string or array of messages if using the prompt option.',
|
|
132
|
+
},
|
|
133
|
+
{
|
|
134
|
+
name: 'messages',
|
|
135
|
+
type: 'Array<ModelMessage> | undefined',
|
|
136
|
+
description: 'The messages array if using the messages option.',
|
|
137
|
+
},
|
|
138
|
+
{
|
|
139
|
+
name: 'tools',
|
|
140
|
+
type: 'TOOLS | undefined',
|
|
141
|
+
description: 'The tools available for this generation.',
|
|
142
|
+
},
|
|
143
|
+
{
|
|
144
|
+
name: 'toolChoice',
|
|
145
|
+
type: 'ToolChoice<TOOLS> | undefined',
|
|
146
|
+
description: 'The tool choice strategy for this generation.',
|
|
147
|
+
},
|
|
148
|
+
{
|
|
149
|
+
name: 'activeTools',
|
|
150
|
+
type: 'Array<keyof TOOLS> | undefined',
|
|
151
|
+
description:
|
|
152
|
+
'Limits which tools are available for the model to call.',
|
|
153
|
+
},
|
|
154
|
+
{
|
|
155
|
+
name: 'maxOutputTokens',
|
|
156
|
+
type: 'number | undefined',
|
|
157
|
+
description: 'Maximum number of tokens to generate.',
|
|
158
|
+
},
|
|
159
|
+
{
|
|
160
|
+
name: 'temperature',
|
|
161
|
+
type: 'number | undefined',
|
|
162
|
+
description: 'Sampling temperature for generation.',
|
|
163
|
+
},
|
|
164
|
+
{
|
|
165
|
+
name: 'topP',
|
|
166
|
+
type: 'number | undefined',
|
|
167
|
+
description: 'Top-p (nucleus) sampling parameter.',
|
|
168
|
+
},
|
|
169
|
+
{
|
|
170
|
+
name: 'topK',
|
|
171
|
+
type: 'number | undefined',
|
|
172
|
+
description: 'Top-k sampling parameter.',
|
|
173
|
+
},
|
|
174
|
+
{
|
|
175
|
+
name: 'presencePenalty',
|
|
176
|
+
type: 'number | undefined',
|
|
177
|
+
description: 'Presence penalty for generation.',
|
|
178
|
+
},
|
|
179
|
+
{
|
|
180
|
+
name: 'frequencyPenalty',
|
|
181
|
+
type: 'number | undefined',
|
|
182
|
+
description: 'Frequency penalty for generation.',
|
|
183
|
+
},
|
|
184
|
+
{
|
|
185
|
+
name: 'stopSequences',
|
|
186
|
+
type: 'string[] | undefined',
|
|
187
|
+
description: 'Sequences that will stop generation.',
|
|
188
|
+
},
|
|
189
|
+
{
|
|
190
|
+
name: 'seed',
|
|
191
|
+
type: 'number | undefined',
|
|
192
|
+
description: 'Random seed for reproducible generation.',
|
|
193
|
+
},
|
|
194
|
+
{
|
|
195
|
+
name: 'maxRetries',
|
|
196
|
+
type: 'number',
|
|
197
|
+
description: 'Maximum number of retries for failed requests.',
|
|
198
|
+
},
|
|
199
|
+
{
|
|
200
|
+
name: 'timeout',
|
|
201
|
+
type: 'number | { totalMs?: number; stepMs?: number; chunkMs?: number } | undefined',
|
|
202
|
+
description: 'Timeout configuration for the generation.',
|
|
203
|
+
},
|
|
204
|
+
{
|
|
205
|
+
name: 'headers',
|
|
206
|
+
type: 'Record<string, string | undefined> | undefined',
|
|
207
|
+
description: 'Additional HTTP headers sent with the request.',
|
|
208
|
+
},
|
|
209
|
+
{
|
|
210
|
+
name: 'providerOptions',
|
|
211
|
+
type: 'ProviderOptions | undefined',
|
|
212
|
+
description: 'Additional provider-specific options.',
|
|
213
|
+
},
|
|
214
|
+
{
|
|
215
|
+
name: 'stopWhen',
|
|
216
|
+
type: 'StopCondition<TOOLS> | Array<StopCondition<TOOLS>> | undefined',
|
|
217
|
+
description: 'Condition(s) for stopping the generation.',
|
|
218
|
+
},
|
|
219
|
+
{
|
|
220
|
+
name: 'output',
|
|
221
|
+
type: 'OUTPUT | undefined',
|
|
222
|
+
description:
|
|
223
|
+
'The output specification for structured outputs, if configured.',
|
|
224
|
+
},
|
|
225
|
+
{
|
|
226
|
+
name: 'abortSignal',
|
|
227
|
+
type: 'AbortSignal | undefined',
|
|
228
|
+
description: 'Abort signal for cancelling the operation.',
|
|
229
|
+
},
|
|
230
|
+
{
|
|
231
|
+
name: 'include',
|
|
232
|
+
type: '{ requestBody?: boolean; responseBody?: boolean } | undefined',
|
|
233
|
+
description:
|
|
234
|
+
'Settings for controlling what data is included in step results.',
|
|
235
|
+
},
|
|
236
|
+
{
|
|
237
|
+
name: 'functionId',
|
|
238
|
+
type: 'string | undefined',
|
|
239
|
+
description:
|
|
240
|
+
'Identifier from telemetry settings for grouping related operations.',
|
|
241
|
+
},
|
|
242
|
+
{
|
|
243
|
+
name: 'metadata',
|
|
244
|
+
type: 'Record<string, unknown> | undefined',
|
|
245
|
+
description: 'Additional metadata passed to the generation.',
|
|
246
|
+
},
|
|
247
|
+
{
|
|
248
|
+
name: 'experimental_context',
|
|
249
|
+
type: 'unknown',
|
|
250
|
+
description:
|
|
251
|
+
'User-defined context object that flows through the entire generation lifecycle.',
|
|
252
|
+
},
|
|
253
|
+
],
|
|
254
|
+
},
|
|
255
|
+
],
|
|
256
|
+
},
|
|
257
|
+
{
|
|
258
|
+
name: 'experimental_onStepStart',
|
|
259
|
+
type: 'ToolLoopAgentOnStepStartCallback',
|
|
260
|
+
isOptional: true,
|
|
261
|
+
description:
|
|
262
|
+
'Callback that is called when a step (LLM call) begins, before the provider is called. Each step represents a single LLM invocation. If also specified in `generate()` or `stream()`, both callbacks are called (constructor first). Experimental (can break in patch releases).',
|
|
263
|
+
properties: [
|
|
264
|
+
{
|
|
265
|
+
type: 'OnStepStartEvent',
|
|
266
|
+
parameters: [
|
|
267
|
+
{
|
|
268
|
+
name: 'stepNumber',
|
|
269
|
+
type: 'number',
|
|
270
|
+
description: 'Zero-based index of the current step.',
|
|
271
|
+
},
|
|
272
|
+
{
|
|
273
|
+
name: 'model',
|
|
274
|
+
type: '{ provider: string; modelId: string }',
|
|
275
|
+
description: 'The model being used for this step.',
|
|
276
|
+
},
|
|
277
|
+
{
|
|
278
|
+
name: 'system',
|
|
279
|
+
type: 'string | SystemModelMessage | Array<SystemModelMessage> | undefined',
|
|
280
|
+
description: 'The system message for this step.',
|
|
281
|
+
},
|
|
282
|
+
{
|
|
283
|
+
name: 'messages',
|
|
284
|
+
type: 'Array<ModelMessage>',
|
|
285
|
+
description:
|
|
286
|
+
'The messages that will be sent to the model for this step.',
|
|
287
|
+
},
|
|
288
|
+
{
|
|
289
|
+
name: 'tools',
|
|
290
|
+
type: 'TOOLS | undefined',
|
|
291
|
+
description: 'The tools available for this generation.',
|
|
292
|
+
},
|
|
293
|
+
{
|
|
294
|
+
name: 'toolChoice',
|
|
295
|
+
type: 'LanguageModelV3ToolChoice | undefined',
|
|
296
|
+
description: 'The tool choice configuration for this step.',
|
|
297
|
+
},
|
|
298
|
+
{
|
|
299
|
+
name: 'activeTools',
|
|
300
|
+
type: 'Array<keyof TOOLS> | undefined',
|
|
301
|
+
description: 'Limits which tools are available for this step.',
|
|
302
|
+
},
|
|
303
|
+
{
|
|
304
|
+
name: 'steps',
|
|
305
|
+
type: 'ReadonlyArray<StepResult<TOOLS>>',
|
|
306
|
+
description:
|
|
307
|
+
'Array of results from previous steps (empty for first step).',
|
|
308
|
+
},
|
|
309
|
+
{
|
|
310
|
+
name: 'providerOptions',
|
|
311
|
+
type: 'ProviderOptions | undefined',
|
|
312
|
+
description:
|
|
313
|
+
'Additional provider-specific options for this step.',
|
|
314
|
+
},
|
|
315
|
+
{
|
|
316
|
+
name: 'timeout',
|
|
317
|
+
type: 'number | { totalMs?: number; stepMs?: number; chunkMs?: number } | undefined',
|
|
318
|
+
description: 'Timeout configuration for the generation.',
|
|
319
|
+
},
|
|
320
|
+
{
|
|
321
|
+
name: 'headers',
|
|
322
|
+
type: 'Record<string, string | undefined> | undefined',
|
|
323
|
+
description: 'Additional HTTP headers sent with the request.',
|
|
324
|
+
},
|
|
325
|
+
{
|
|
326
|
+
name: 'stopWhen',
|
|
327
|
+
type: 'StopCondition<TOOLS> | Array<StopCondition<TOOLS>> | undefined',
|
|
328
|
+
description: 'Condition(s) for stopping the generation.',
|
|
329
|
+
},
|
|
330
|
+
{
|
|
331
|
+
name: 'output',
|
|
332
|
+
type: 'OUTPUT | undefined',
|
|
333
|
+
description:
|
|
334
|
+
'The output specification for structured outputs, if configured.',
|
|
335
|
+
},
|
|
336
|
+
{
|
|
337
|
+
name: 'abortSignal',
|
|
338
|
+
type: 'AbortSignal | undefined',
|
|
339
|
+
description: 'Abort signal for cancelling the operation.',
|
|
340
|
+
},
|
|
341
|
+
{
|
|
342
|
+
name: 'include',
|
|
343
|
+
type: '{ requestBody?: boolean; responseBody?: boolean } | undefined',
|
|
344
|
+
description:
|
|
345
|
+
'Settings for controlling what data is included in step results.',
|
|
346
|
+
},
|
|
347
|
+
{
|
|
348
|
+
name: 'functionId',
|
|
349
|
+
type: 'string | undefined',
|
|
350
|
+
description:
|
|
351
|
+
'Identifier from telemetry settings for grouping related operations.',
|
|
352
|
+
},
|
|
353
|
+
{
|
|
354
|
+
name: 'metadata',
|
|
355
|
+
type: 'Record<string, unknown> | undefined',
|
|
356
|
+
description: 'Additional metadata from telemetry settings.',
|
|
357
|
+
},
|
|
358
|
+
{
|
|
359
|
+
name: 'experimental_context',
|
|
360
|
+
type: 'unknown',
|
|
361
|
+
description:
|
|
362
|
+
'User-defined context object. May be updated from prepareStep between steps.',
|
|
363
|
+
},
|
|
364
|
+
],
|
|
365
|
+
},
|
|
366
|
+
],
|
|
367
|
+
},
|
|
368
|
+
{
|
|
369
|
+
name: 'experimental_onToolCallStart',
|
|
370
|
+
type: 'ToolLoopAgentOnToolCallStartCallback',
|
|
371
|
+
isOptional: true,
|
|
372
|
+
description:
|
|
373
|
+
"Callback that is called right before a tool's execute function runs. If also specified in `generate()` or `stream()`, both callbacks are called (constructor first). Experimental (can break in patch releases).",
|
|
374
|
+
properties: [
|
|
375
|
+
{
|
|
376
|
+
type: 'OnToolCallStartEvent',
|
|
377
|
+
parameters: [
|
|
378
|
+
{
|
|
379
|
+
name: 'stepNumber',
|
|
380
|
+
type: 'number | undefined',
|
|
381
|
+
description:
|
|
382
|
+
'The zero-based index of the current step where this tool call occurs. May be undefined in streaming contexts.',
|
|
383
|
+
},
|
|
384
|
+
{
|
|
385
|
+
name: 'model',
|
|
386
|
+
type: '{ provider: string; modelId: string } | undefined',
|
|
387
|
+
description:
|
|
388
|
+
'Information about the model being used. May be undefined in streaming contexts.',
|
|
389
|
+
},
|
|
390
|
+
{
|
|
391
|
+
name: 'toolCall',
|
|
392
|
+
type: 'TypedToolCall<TOOLS>',
|
|
393
|
+
description:
|
|
394
|
+
'The full tool call object containing toolName, toolCallId, input, and metadata.',
|
|
395
|
+
},
|
|
396
|
+
{
|
|
397
|
+
name: 'messages',
|
|
398
|
+
type: 'Array<ModelMessage>',
|
|
399
|
+
description:
|
|
400
|
+
'The conversation messages available at tool execution time.',
|
|
401
|
+
},
|
|
402
|
+
{
|
|
403
|
+
name: 'abortSignal',
|
|
404
|
+
type: 'AbortSignal | undefined',
|
|
405
|
+
description: 'Signal for cancelling the operation.',
|
|
406
|
+
},
|
|
407
|
+
{
|
|
408
|
+
name: 'functionId',
|
|
409
|
+
type: 'string | undefined',
|
|
410
|
+
description:
|
|
411
|
+
'Identifier from telemetry settings for grouping related operations.',
|
|
412
|
+
},
|
|
413
|
+
{
|
|
414
|
+
name: 'metadata',
|
|
415
|
+
type: 'Record<string, unknown> | undefined',
|
|
416
|
+
description: 'Additional metadata from telemetry settings.',
|
|
417
|
+
},
|
|
418
|
+
{
|
|
419
|
+
name: 'experimental_context',
|
|
420
|
+
type: 'unknown',
|
|
421
|
+
description:
|
|
422
|
+
'User-defined context object flowing through the generation.',
|
|
423
|
+
},
|
|
424
|
+
],
|
|
425
|
+
},
|
|
426
|
+
],
|
|
427
|
+
},
|
|
428
|
+
{
|
|
429
|
+
name: 'experimental_onToolCallFinish',
|
|
430
|
+
type: 'ToolLoopAgentOnToolCallFinishCallback',
|
|
431
|
+
isOptional: true,
|
|
432
|
+
description:
|
|
433
|
+
"Callback that is called right after a tool's execute function completes (or errors). Uses a discriminated union on the `success` field: when `success: true`, `output` contains the tool result; when `success: false`, `error` contains the error. If also specified in `generate()` or `stream()`, both callbacks are called (constructor first). Experimental (can break in patch releases).",
|
|
434
|
+
properties: [
|
|
435
|
+
{
|
|
436
|
+
type: 'OnToolCallFinishEvent',
|
|
437
|
+
parameters: [
|
|
438
|
+
{
|
|
439
|
+
name: 'stepNumber',
|
|
440
|
+
type: 'number | undefined',
|
|
441
|
+
description:
|
|
442
|
+
'The zero-based index of the current step where this tool call occurred. May be undefined in streaming contexts.',
|
|
443
|
+
},
|
|
444
|
+
{
|
|
445
|
+
name: 'model',
|
|
446
|
+
type: '{ provider: string; modelId: string } | undefined',
|
|
447
|
+
description:
|
|
448
|
+
'Information about the model being used. May be undefined in streaming contexts.',
|
|
449
|
+
},
|
|
450
|
+
{
|
|
451
|
+
name: 'toolCall',
|
|
452
|
+
type: 'TypedToolCall<TOOLS>',
|
|
453
|
+
description:
|
|
454
|
+
'The full tool call object containing toolName, toolCallId, input, and metadata.',
|
|
455
|
+
},
|
|
456
|
+
{
|
|
457
|
+
name: 'messages',
|
|
458
|
+
type: 'Array<ModelMessage>',
|
|
459
|
+
description:
|
|
460
|
+
'The conversation messages available at tool execution time.',
|
|
461
|
+
},
|
|
462
|
+
{
|
|
463
|
+
name: 'abortSignal',
|
|
464
|
+
type: 'AbortSignal | undefined',
|
|
465
|
+
description: 'Signal for cancelling the operation.',
|
|
466
|
+
},
|
|
467
|
+
{
|
|
468
|
+
name: 'durationMs',
|
|
469
|
+
type: 'number',
|
|
470
|
+
description:
|
|
471
|
+
'The wall-clock duration of the tool execution in milliseconds.',
|
|
472
|
+
},
|
|
473
|
+
{
|
|
474
|
+
name: 'functionId',
|
|
475
|
+
type: 'string | undefined',
|
|
476
|
+
description:
|
|
477
|
+
'Identifier from telemetry settings for grouping related operations.',
|
|
478
|
+
},
|
|
479
|
+
{
|
|
480
|
+
name: 'metadata',
|
|
481
|
+
type: 'Record<string, unknown> | undefined',
|
|
482
|
+
description: 'Additional metadata from telemetry settings.',
|
|
483
|
+
},
|
|
484
|
+
{
|
|
485
|
+
name: 'experimental_context',
|
|
486
|
+
type: 'unknown',
|
|
487
|
+
description:
|
|
488
|
+
'User-defined context object flowing through the generation.',
|
|
489
|
+
},
|
|
490
|
+
{
|
|
491
|
+
name: 'success',
|
|
492
|
+
type: 'boolean',
|
|
493
|
+
description:
|
|
494
|
+
'Discriminator indicating whether the tool call succeeded. When true, output is available. When false, error is available.',
|
|
495
|
+
},
|
|
496
|
+
{
|
|
497
|
+
name: 'output',
|
|
498
|
+
type: 'unknown',
|
|
499
|
+
description:
|
|
500
|
+
"The tool's return value (only present when `success: true`).",
|
|
501
|
+
},
|
|
502
|
+
{
|
|
503
|
+
name: 'error',
|
|
504
|
+
type: 'unknown',
|
|
505
|
+
description:
|
|
506
|
+
'The error that occurred during tool execution (only present when `success: false`).',
|
|
507
|
+
},
|
|
508
|
+
],
|
|
509
|
+
},
|
|
510
|
+
],
|
|
511
|
+
},
|
|
107
512
|
{
|
|
108
513
|
name: 'onStepFinish',
|
|
109
|
-
type: '
|
|
514
|
+
type: 'ToolLoopAgentOnStepFinishCallback',
|
|
110
515
|
isOptional: true,
|
|
111
516
|
description:
|
|
112
|
-
'Callback invoked after each agent step (LLM/tool call) completes.',
|
|
517
|
+
'Callback invoked after each agent step (LLM/tool call) completes. If also specified in `generate()` or `stream()`, both callbacks are called (constructor first).',
|
|
113
518
|
},
|
|
114
519
|
{
|
|
115
520
|
name: 'onFinish',
|
|
116
521
|
type: 'ToolLoopAgentOnFinishCallback',
|
|
117
522
|
isOptional: true,
|
|
118
523
|
description:
|
|
119
|
-
'Callback that is called when all agent steps are finished and the response is complete. Receives
|
|
524
|
+
'Callback that is called when all agent steps are finished and the response is complete. Receives step results, total usage, experimental_context, functionId, and metadata. If also specified in `generate()` or `stream()`, both callbacks are called (constructor first).',
|
|
120
525
|
},
|
|
121
526
|
{
|
|
122
527
|
name: 'experimental_context',
|
|
@@ -276,6 +681,34 @@ const result = await agent.generate({
|
|
|
276
681
|
description:
|
|
277
682
|
'Custom call options when the agent is configured with a callOptionsSchema.',
|
|
278
683
|
},
|
|
684
|
+
{
|
|
685
|
+
name: 'experimental_onStart',
|
|
686
|
+
type: 'ToolLoopAgentOnStartCallback',
|
|
687
|
+
isOptional: true,
|
|
688
|
+
description:
|
|
689
|
+
'Callback that is called when the agent operation begins, before any LLM calls are made. If also specified in the constructor, both callbacks are called (constructor first). Experimental (can break in patch releases).',
|
|
690
|
+
},
|
|
691
|
+
{
|
|
692
|
+
name: 'experimental_onStepStart',
|
|
693
|
+
type: 'ToolLoopAgentOnStepStartCallback',
|
|
694
|
+
isOptional: true,
|
|
695
|
+
description:
|
|
696
|
+
'Callback that is called when a step (LLM call) begins, before the provider is called. If also specified in the constructor, both callbacks are called (constructor first). Experimental (can break in patch releases).',
|
|
697
|
+
},
|
|
698
|
+
{
|
|
699
|
+
name: 'experimental_onToolCallStart',
|
|
700
|
+
type: 'ToolLoopAgentOnToolCallStartCallback',
|
|
701
|
+
isOptional: true,
|
|
702
|
+
description:
|
|
703
|
+
"Callback that is called right before a tool's execute function runs. If also specified in the constructor, both callbacks are called (constructor first). Experimental (can break in patch releases).",
|
|
704
|
+
},
|
|
705
|
+
{
|
|
706
|
+
name: 'experimental_onToolCallFinish',
|
|
707
|
+
type: 'ToolLoopAgentOnToolCallFinishCallback',
|
|
708
|
+
isOptional: true,
|
|
709
|
+
description:
|
|
710
|
+
"Callback that is called right after a tool's execute function completes (or errors). If also specified in the constructor, both callbacks are called (constructor first). Experimental (can break in patch releases).",
|
|
711
|
+
},
|
|
279
712
|
{
|
|
280
713
|
name: 'onStepFinish',
|
|
281
714
|
type: 'ToolLoopAgentOnStepFinishCallback',
|
|
@@ -283,6 +716,13 @@ const result = await agent.generate({
|
|
|
283
716
|
description:
|
|
284
717
|
'Callback invoked after each agent step (LLM/tool call) completes. If also specified in the constructor, both callbacks are called (constructor first, then this one).',
|
|
285
718
|
},
|
|
719
|
+
{
|
|
720
|
+
name: 'onFinish',
|
|
721
|
+
type: 'ToolLoopAgentOnFinishCallback',
|
|
722
|
+
isOptional: true,
|
|
723
|
+
description:
|
|
724
|
+
'Callback that is called when all agent steps are finished and the response is complete. If also specified in the constructor, both callbacks are called (constructor first, then this one).',
|
|
725
|
+
},
|
|
286
726
|
]}
|
|
287
727
|
/>
|
|
288
728
|
|
|
@@ -344,6 +784,34 @@ for await (const chunk of stream.textStream) {
|
|
|
344
784
|
description:
|
|
345
785
|
'Optional stream transformation(s). They are applied in the order provided and must maintain the stream structure. See `streamText` docs for details.',
|
|
346
786
|
},
|
|
787
|
+
{
|
|
788
|
+
name: 'experimental_onStart',
|
|
789
|
+
type: 'ToolLoopAgentOnStartCallback',
|
|
790
|
+
isOptional: true,
|
|
791
|
+
description:
|
|
792
|
+
'Callback that is called when the agent operation begins, before any LLM calls are made. If also specified in the constructor, both callbacks are called (constructor first). Experimental (can break in patch releases).',
|
|
793
|
+
},
|
|
794
|
+
{
|
|
795
|
+
name: 'experimental_onStepStart',
|
|
796
|
+
type: 'ToolLoopAgentOnStepStartCallback',
|
|
797
|
+
isOptional: true,
|
|
798
|
+
description:
|
|
799
|
+
'Callback that is called when a step (LLM call) begins, before the provider is called. If also specified in the constructor, both callbacks are called (constructor first). Experimental (can break in patch releases).',
|
|
800
|
+
},
|
|
801
|
+
{
|
|
802
|
+
name: 'experimental_onToolCallStart',
|
|
803
|
+
type: 'ToolLoopAgentOnToolCallStartCallback',
|
|
804
|
+
isOptional: true,
|
|
805
|
+
description:
|
|
806
|
+
"Callback that is called right before a tool's execute function runs. If also specified in the constructor, both callbacks are called (constructor first). Experimental (can break in patch releases).",
|
|
807
|
+
},
|
|
808
|
+
{
|
|
809
|
+
name: 'experimental_onToolCallFinish',
|
|
810
|
+
type: 'ToolLoopAgentOnToolCallFinishCallback',
|
|
811
|
+
isOptional: true,
|
|
812
|
+
description:
|
|
813
|
+
"Callback that is called right after a tool's execute function completes (or errors). If also specified in the constructor, both callbacks are called (constructor first). Experimental (can break in patch releases).",
|
|
814
|
+
},
|
|
347
815
|
{
|
|
348
816
|
name: 'onStepFinish',
|
|
349
817
|
type: 'ToolLoopAgentOnStepFinishCallback',
|
|
@@ -351,6 +819,13 @@ for await (const chunk of stream.textStream) {
|
|
|
351
819
|
description:
|
|
352
820
|
'Callback invoked after each agent step (LLM/tool call) completes. If also specified in the constructor, both callbacks are called (constructor first, then this one).',
|
|
353
821
|
},
|
|
822
|
+
{
|
|
823
|
+
name: 'onFinish',
|
|
824
|
+
type: 'ToolLoopAgentOnFinishCallback',
|
|
825
|
+
isOptional: true,
|
|
826
|
+
description:
|
|
827
|
+
'Callback that is called when all agent steps are finished and the response is complete. If also specified in the constructor, both callbacks are called (constructor first, then this one).',
|
|
828
|
+
},
|
|
354
829
|
]}
|
|
355
830
|
/>
|
|
356
831
|
|