ai 6.0.95 → 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 +12 -0
- package/dist/index.d.mts +446 -420
- package/dist/index.d.ts +446 -420
- package/dist/index.js +205 -71
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +205 -71
- 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/03-ai-sdk-core/05-generating-text.mdx +56 -0
- package/docs/07-reference/01-ai-sdk-core/02-stream-text.mdx +407 -0
- 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/generate-text/callback-events.ts +332 -0
- package/src/generate-text/execute-tool-call.ts +14 -28
- package/src/generate-text/generate-text.ts +39 -346
- package/src/generate-text/index.ts +4 -0
- package/src/generate-text/run-tools-transformation.ts +16 -0
- package/src/generate-text/stream-text.ts +205 -33
- 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
|
@@ -4,6 +4,14 @@ import {
|
|
|
4
4
|
ProviderOptions,
|
|
5
5
|
SystemModelMessage,
|
|
6
6
|
} from '@ai-sdk/provider-utils';
|
|
7
|
+
import type {
|
|
8
|
+
OnFinishEvent,
|
|
9
|
+
OnStartEvent,
|
|
10
|
+
OnStepFinishEvent,
|
|
11
|
+
OnStepStartEvent,
|
|
12
|
+
OnToolCallFinishEvent,
|
|
13
|
+
OnToolCallStartEvent,
|
|
14
|
+
} from '../generate-text/callback-events';
|
|
7
15
|
import { Output } from '../generate-text/output';
|
|
8
16
|
import { PrepareStepFunction } from '../generate-text/prepare-step';
|
|
9
17
|
import { StopCondition } from '../generate-text/stop-condition';
|
|
@@ -15,8 +23,32 @@ import { TelemetrySettings } from '../telemetry/telemetry-settings';
|
|
|
15
23
|
import { LanguageModel, ToolChoice } from '../types/language-model';
|
|
16
24
|
import { DownloadFunction } from '../util/download/download-function';
|
|
17
25
|
import { AgentCallParameters } from './agent';
|
|
18
|
-
|
|
19
|
-
|
|
26
|
+
|
|
27
|
+
export type ToolLoopAgentOnStartCallback<
|
|
28
|
+
TOOLS extends ToolSet = ToolSet,
|
|
29
|
+
OUTPUT extends Output = Output,
|
|
30
|
+
> = (event: OnStartEvent<TOOLS, OUTPUT>) => PromiseLike<void> | void;
|
|
31
|
+
|
|
32
|
+
export type ToolLoopAgentOnStepStartCallback<
|
|
33
|
+
TOOLS extends ToolSet = ToolSet,
|
|
34
|
+
OUTPUT extends Output = Output,
|
|
35
|
+
> = (event: OnStepStartEvent<TOOLS, OUTPUT>) => PromiseLike<void> | void;
|
|
36
|
+
|
|
37
|
+
export type ToolLoopAgentOnToolCallStartCallback<
|
|
38
|
+
TOOLS extends ToolSet = ToolSet,
|
|
39
|
+
> = (event: OnToolCallStartEvent<TOOLS>) => PromiseLike<void> | void;
|
|
40
|
+
|
|
41
|
+
export type ToolLoopAgentOnToolCallFinishCallback<
|
|
42
|
+
TOOLS extends ToolSet = ToolSet,
|
|
43
|
+
> = (event: OnToolCallFinishEvent<TOOLS>) => PromiseLike<void> | void;
|
|
44
|
+
|
|
45
|
+
export type ToolLoopAgentOnStepFinishCallback<TOOLS extends ToolSet = {}> = (
|
|
46
|
+
stepResult: OnStepFinishEvent<TOOLS>,
|
|
47
|
+
) => Promise<void> | void;
|
|
48
|
+
|
|
49
|
+
export type ToolLoopAgentOnFinishCallback<TOOLS extends ToolSet = {}> = (
|
|
50
|
+
event: OnFinishEvent<TOOLS>,
|
|
51
|
+
) => PromiseLike<void> | void;
|
|
20
52
|
|
|
21
53
|
/**
|
|
22
54
|
* Configuration options for an agent.
|
|
@@ -89,6 +121,33 @@ export type ToolLoopAgentSettings<
|
|
|
89
121
|
*/
|
|
90
122
|
experimental_repairToolCall?: ToolCallRepairFunction<NoInfer<TOOLS>>;
|
|
91
123
|
|
|
124
|
+
/**
|
|
125
|
+
* Callback that is called when the agent operation begins, before any LLM calls.
|
|
126
|
+
*/
|
|
127
|
+
experimental_onStart?: ToolLoopAgentOnStartCallback<NoInfer<TOOLS>, OUTPUT>;
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Callback that is called when a step (LLM call) begins, before the provider is called.
|
|
131
|
+
*/
|
|
132
|
+
experimental_onStepStart?: ToolLoopAgentOnStepStartCallback<
|
|
133
|
+
NoInfer<TOOLS>,
|
|
134
|
+
OUTPUT
|
|
135
|
+
>;
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Callback that is called before each tool execution begins.
|
|
139
|
+
*/
|
|
140
|
+
experimental_onToolCallStart?: ToolLoopAgentOnToolCallStartCallback<
|
|
141
|
+
NoInfer<TOOLS>
|
|
142
|
+
>;
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Callback that is called after each tool execution completes.
|
|
146
|
+
*/
|
|
147
|
+
experimental_onToolCallFinish?: ToolLoopAgentOnToolCallFinishCallback<
|
|
148
|
+
NoInfer<TOOLS>
|
|
149
|
+
>;
|
|
150
|
+
|
|
92
151
|
/**
|
|
93
152
|
* Callback that is called when each step (LLM call) is finished, including intermediate steps.
|
|
94
153
|
*/
|
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
import { generateText } from '../generate-text/generate-text';
|
|
2
2
|
import { GenerateTextResult } from '../generate-text/generate-text-result';
|
|
3
3
|
import { Output } from '../generate-text/output';
|
|
4
|
-
import { StepResult } from '../generate-text/step-result';
|
|
5
4
|
import { stepCountIs } from '../generate-text/stop-condition';
|
|
6
5
|
import { streamText } from '../generate-text/stream-text';
|
|
7
6
|
import { StreamTextResult } from '../generate-text/stream-text-result';
|
|
8
7
|
import { ToolSet } from '../generate-text/tool-set';
|
|
9
8
|
import { Prompt } from '../prompt';
|
|
10
9
|
import { Agent, AgentCallParameters, AgentStreamParameters } from './agent';
|
|
11
|
-
import { ToolLoopAgentOnStepFinishCallback } from './tool-loop-agent-on-step-finish-callback';
|
|
12
10
|
import { ToolLoopAgentSettings } from './tool-loop-agent-settings';
|
|
13
11
|
|
|
14
12
|
/**
|
|
@@ -57,15 +55,29 @@ export class ToolLoopAgent<
|
|
|
57
55
|
}): Promise<
|
|
58
56
|
Omit<
|
|
59
57
|
ToolLoopAgentSettings<CALL_OPTIONS, TOOLS, OUTPUT>,
|
|
60
|
-
|
|
58
|
+
| 'prepareCall'
|
|
59
|
+
| 'instructions'
|
|
60
|
+
| 'experimental_onStart'
|
|
61
|
+
| 'experimental_onStepStart'
|
|
62
|
+
| 'experimental_onToolCallStart'
|
|
63
|
+
| 'experimental_onToolCallFinish'
|
|
64
|
+
| 'onStepFinish'
|
|
65
|
+
| 'onFinish'
|
|
61
66
|
> &
|
|
62
67
|
Prompt
|
|
63
68
|
> {
|
|
64
|
-
const {
|
|
65
|
-
|
|
69
|
+
const {
|
|
70
|
+
experimental_onStart: _settingsOnStart,
|
|
71
|
+
experimental_onStepStart: _settingsOnStepStart,
|
|
72
|
+
experimental_onToolCallStart: _settingsOnToolCallStart,
|
|
73
|
+
experimental_onToolCallFinish: _settingsOnToolCallFinish,
|
|
74
|
+
onStepFinish: _settingsOnStepFinish,
|
|
75
|
+
onFinish: _settingsOnFinish,
|
|
76
|
+
...settingsWithoutCallbacks
|
|
77
|
+
} = this.settings;
|
|
66
78
|
|
|
67
79
|
const baseCallArgs = {
|
|
68
|
-
...
|
|
80
|
+
...settingsWithoutCallbacks,
|
|
69
81
|
stopWhen: this.settings.stopWhen ?? stepCountIs(20),
|
|
70
82
|
...options,
|
|
71
83
|
};
|
|
@@ -89,19 +101,17 @@ export class ToolLoopAgent<
|
|
|
89
101
|
};
|
|
90
102
|
}
|
|
91
103
|
|
|
92
|
-
private
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
await
|
|
100
|
-
|
|
101
|
-
};
|
|
104
|
+
private mergeCallbacks<T extends (event: any) => PromiseLike<void> | void>(
|
|
105
|
+
settingsCallback: T | undefined,
|
|
106
|
+
methodCallback: T | undefined,
|
|
107
|
+
): T | undefined {
|
|
108
|
+
if (methodCallback && settingsCallback) {
|
|
109
|
+
return (async (event: Parameters<T>[0]) => {
|
|
110
|
+
await settingsCallback(event);
|
|
111
|
+
await methodCallback(event);
|
|
112
|
+
}) as unknown as T;
|
|
102
113
|
}
|
|
103
|
-
|
|
104
|
-
return methodCallback ?? constructorCallback;
|
|
114
|
+
return methodCallback ?? settingsCallback;
|
|
105
115
|
}
|
|
106
116
|
|
|
107
117
|
/**
|
|
@@ -110,7 +120,12 @@ export class ToolLoopAgent<
|
|
|
110
120
|
async generate({
|
|
111
121
|
abortSignal,
|
|
112
122
|
timeout,
|
|
123
|
+
experimental_onStart,
|
|
124
|
+
experimental_onStepStart,
|
|
125
|
+
experimental_onToolCallStart,
|
|
126
|
+
experimental_onToolCallFinish,
|
|
113
127
|
onStepFinish,
|
|
128
|
+
onFinish,
|
|
114
129
|
...options
|
|
115
130
|
}: AgentCallParameters<CALL_OPTIONS, TOOLS>): Promise<
|
|
116
131
|
GenerateTextResult<TOOLS, OUTPUT>
|
|
@@ -119,7 +134,27 @@ export class ToolLoopAgent<
|
|
|
119
134
|
...(await this.prepareCall(options)),
|
|
120
135
|
abortSignal,
|
|
121
136
|
timeout,
|
|
122
|
-
|
|
137
|
+
experimental_onStart: this.mergeCallbacks(
|
|
138
|
+
this.settings.experimental_onStart,
|
|
139
|
+
experimental_onStart,
|
|
140
|
+
),
|
|
141
|
+
experimental_onStepStart: this.mergeCallbacks(
|
|
142
|
+
this.settings.experimental_onStepStart,
|
|
143
|
+
experimental_onStepStart,
|
|
144
|
+
),
|
|
145
|
+
experimental_onToolCallStart: this.mergeCallbacks(
|
|
146
|
+
this.settings.experimental_onToolCallStart,
|
|
147
|
+
experimental_onToolCallStart,
|
|
148
|
+
),
|
|
149
|
+
experimental_onToolCallFinish: this.mergeCallbacks(
|
|
150
|
+
this.settings.experimental_onToolCallFinish,
|
|
151
|
+
experimental_onToolCallFinish,
|
|
152
|
+
),
|
|
153
|
+
onStepFinish: this.mergeCallbacks(
|
|
154
|
+
this.settings.onStepFinish,
|
|
155
|
+
onStepFinish,
|
|
156
|
+
),
|
|
157
|
+
onFinish: this.mergeCallbacks(this.settings.onFinish, onFinish),
|
|
123
158
|
});
|
|
124
159
|
}
|
|
125
160
|
|
|
@@ -130,7 +165,12 @@ export class ToolLoopAgent<
|
|
|
130
165
|
abortSignal,
|
|
131
166
|
timeout,
|
|
132
167
|
experimental_transform,
|
|
168
|
+
experimental_onStart,
|
|
169
|
+
experimental_onStepStart,
|
|
170
|
+
experimental_onToolCallStart,
|
|
171
|
+
experimental_onToolCallFinish,
|
|
133
172
|
onStepFinish,
|
|
173
|
+
onFinish,
|
|
134
174
|
...options
|
|
135
175
|
}: AgentStreamParameters<CALL_OPTIONS, TOOLS>): Promise<
|
|
136
176
|
StreamTextResult<TOOLS, OUTPUT>
|
|
@@ -140,7 +180,27 @@ export class ToolLoopAgent<
|
|
|
140
180
|
abortSignal,
|
|
141
181
|
timeout,
|
|
142
182
|
experimental_transform,
|
|
143
|
-
|
|
183
|
+
experimental_onStart: this.mergeCallbacks(
|
|
184
|
+
this.settings.experimental_onStart,
|
|
185
|
+
experimental_onStart,
|
|
186
|
+
),
|
|
187
|
+
experimental_onStepStart: this.mergeCallbacks(
|
|
188
|
+
this.settings.experimental_onStepStart,
|
|
189
|
+
experimental_onStepStart,
|
|
190
|
+
),
|
|
191
|
+
experimental_onToolCallStart: this.mergeCallbacks(
|
|
192
|
+
this.settings.experimental_onToolCallStart,
|
|
193
|
+
experimental_onToolCallStart,
|
|
194
|
+
),
|
|
195
|
+
experimental_onToolCallFinish: this.mergeCallbacks(
|
|
196
|
+
this.settings.experimental_onToolCallFinish,
|
|
197
|
+
experimental_onToolCallFinish,
|
|
198
|
+
),
|
|
199
|
+
onStepFinish: this.mergeCallbacks(
|
|
200
|
+
this.settings.onStepFinish,
|
|
201
|
+
onStepFinish,
|
|
202
|
+
),
|
|
203
|
+
onFinish: this.mergeCallbacks(this.settings.onFinish, onFinish),
|
|
144
204
|
});
|
|
145
205
|
}
|
|
146
206
|
}
|
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
import type { LanguageModelV3ToolChoice } from '@ai-sdk/provider';
|
|
2
|
+
import type {
|
|
3
|
+
ModelMessage,
|
|
4
|
+
ProviderOptions,
|
|
5
|
+
SystemModelMessage,
|
|
6
|
+
} from '@ai-sdk/provider-utils';
|
|
7
|
+
import type { TimeoutConfiguration } from '../prompt/call-settings';
|
|
8
|
+
import type { ToolChoice } from '../types/language-model';
|
|
9
|
+
import type { LanguageModelUsage } from '../types/usage';
|
|
10
|
+
import type { Output } from './output';
|
|
11
|
+
import type { StepResult } from './step-result';
|
|
12
|
+
import type { StopCondition } from './stop-condition';
|
|
13
|
+
import type { TypedToolCall } from './tool-call';
|
|
14
|
+
import type { ToolSet } from './tool-set';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Common model information used across callback events.
|
|
18
|
+
*/
|
|
19
|
+
export interface CallbackModelInfo {
|
|
20
|
+
/** The provider identifier (e.g., 'openai', 'anthropic'). */
|
|
21
|
+
readonly provider: string;
|
|
22
|
+
/** The specific model identifier (e.g., 'gpt-4o'). */
|
|
23
|
+
readonly modelId: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Event passed to the `onStart` callback.
|
|
28
|
+
*
|
|
29
|
+
* Called when the generation operation begins, before any LLM calls.
|
|
30
|
+
*/
|
|
31
|
+
export interface OnStartEvent<
|
|
32
|
+
TOOLS extends ToolSet = ToolSet,
|
|
33
|
+
OUTPUT extends Output = Output,
|
|
34
|
+
INCLUDE = { requestBody?: boolean; responseBody?: boolean },
|
|
35
|
+
> {
|
|
36
|
+
/** The model being used for generation. */
|
|
37
|
+
readonly model: CallbackModelInfo;
|
|
38
|
+
|
|
39
|
+
/** The system message(s) provided to the model. */
|
|
40
|
+
readonly system:
|
|
41
|
+
| string
|
|
42
|
+
| SystemModelMessage
|
|
43
|
+
| Array<SystemModelMessage>
|
|
44
|
+
| undefined;
|
|
45
|
+
|
|
46
|
+
/** The prompt string or array of messages if using the prompt option. */
|
|
47
|
+
readonly prompt: string | Array<ModelMessage> | undefined;
|
|
48
|
+
|
|
49
|
+
/** The messages array if using the messages option. */
|
|
50
|
+
readonly messages: Array<ModelMessage> | undefined;
|
|
51
|
+
|
|
52
|
+
/** The tools available for this generation. */
|
|
53
|
+
readonly tools: TOOLS | undefined;
|
|
54
|
+
|
|
55
|
+
/** The tool choice strategy for this generation. */
|
|
56
|
+
readonly toolChoice: ToolChoice<NoInfer<TOOLS>> | undefined;
|
|
57
|
+
|
|
58
|
+
/** Limits which tools are available for the model to call. */
|
|
59
|
+
readonly activeTools: Array<keyof TOOLS> | undefined;
|
|
60
|
+
|
|
61
|
+
/** Maximum number of tokens to generate. */
|
|
62
|
+
readonly maxOutputTokens: number | undefined;
|
|
63
|
+
/** Sampling temperature for generation. */
|
|
64
|
+
readonly temperature: number | undefined;
|
|
65
|
+
/** Top-p (nucleus) sampling parameter. */
|
|
66
|
+
readonly topP: number | undefined;
|
|
67
|
+
/** Top-k sampling parameter. */
|
|
68
|
+
readonly topK: number | undefined;
|
|
69
|
+
/** Presence penalty for generation. */
|
|
70
|
+
readonly presencePenalty: number | undefined;
|
|
71
|
+
/** Frequency penalty for generation. */
|
|
72
|
+
readonly frequencyPenalty: number | undefined;
|
|
73
|
+
/** Sequences that will stop generation. */
|
|
74
|
+
readonly stopSequences: string[] | undefined;
|
|
75
|
+
/** Random seed for reproducible generation. */
|
|
76
|
+
readonly seed: number | undefined;
|
|
77
|
+
/** Maximum number of retries for failed requests. */
|
|
78
|
+
readonly maxRetries: number;
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Timeout configuration for the generation.
|
|
82
|
+
* Can be a number (milliseconds) or an object with totalMs, stepMs, chunkMs.
|
|
83
|
+
*/
|
|
84
|
+
readonly timeout: TimeoutConfiguration | undefined;
|
|
85
|
+
|
|
86
|
+
/** Additional HTTP headers sent with the request. */
|
|
87
|
+
readonly headers: Record<string, string | undefined> | undefined;
|
|
88
|
+
|
|
89
|
+
/** Additional provider-specific options. */
|
|
90
|
+
readonly providerOptions: ProviderOptions | undefined;
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Condition(s) for stopping the generation.
|
|
94
|
+
* When the condition is an array, any of the conditions can be met to stop.
|
|
95
|
+
*/
|
|
96
|
+
readonly stopWhen:
|
|
97
|
+
| StopCondition<TOOLS>
|
|
98
|
+
| Array<StopCondition<TOOLS>>
|
|
99
|
+
| undefined;
|
|
100
|
+
|
|
101
|
+
/** The output specification for structured outputs, if configured. */
|
|
102
|
+
readonly output: OUTPUT | undefined;
|
|
103
|
+
|
|
104
|
+
/** Abort signal for cancelling the operation. */
|
|
105
|
+
readonly abortSignal: AbortSignal | undefined;
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Settings for controlling what data is included in step results.
|
|
109
|
+
*/
|
|
110
|
+
readonly include: INCLUDE | undefined;
|
|
111
|
+
|
|
112
|
+
/** Identifier from telemetry settings for grouping related operations. */
|
|
113
|
+
readonly functionId: string | undefined;
|
|
114
|
+
|
|
115
|
+
/** Additional metadata passed to the generation. */
|
|
116
|
+
readonly metadata: Record<string, unknown> | undefined;
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* User-defined context object that flows through the entire generation lifecycle.
|
|
120
|
+
* Can be accessed and modified in `prepareStep` and tool `execute` functions.
|
|
121
|
+
*/
|
|
122
|
+
readonly experimental_context: unknown;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Event passed to the `onStepStart` callback.
|
|
127
|
+
*
|
|
128
|
+
* Called when a step (LLM call) begins, before the provider is called.
|
|
129
|
+
* Each step represents a single LLM invocation.
|
|
130
|
+
*/
|
|
131
|
+
export interface OnStepStartEvent<
|
|
132
|
+
TOOLS extends ToolSet = ToolSet,
|
|
133
|
+
OUTPUT extends Output = Output,
|
|
134
|
+
INCLUDE = { requestBody?: boolean; responseBody?: boolean },
|
|
135
|
+
> {
|
|
136
|
+
/** Zero-based index of the current step. */
|
|
137
|
+
readonly stepNumber: number;
|
|
138
|
+
|
|
139
|
+
/** The model being used for this step. */
|
|
140
|
+
readonly model: CallbackModelInfo;
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* The system message for this step.
|
|
144
|
+
*/
|
|
145
|
+
readonly system:
|
|
146
|
+
| string
|
|
147
|
+
| SystemModelMessage
|
|
148
|
+
| Array<SystemModelMessage>
|
|
149
|
+
| undefined;
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* The messages that will be sent to the model for this step.
|
|
153
|
+
* Uses the user-facing `ModelMessage` format.
|
|
154
|
+
* May be overridden by prepareStep.
|
|
155
|
+
*/
|
|
156
|
+
readonly messages: Array<ModelMessage>;
|
|
157
|
+
|
|
158
|
+
/** The tools available for this generation. */
|
|
159
|
+
readonly tools: TOOLS | undefined;
|
|
160
|
+
|
|
161
|
+
/** The tool choice configuration for this step. */
|
|
162
|
+
readonly toolChoice: LanguageModelV3ToolChoice | undefined;
|
|
163
|
+
|
|
164
|
+
/** Limits which tools are available for this step. */
|
|
165
|
+
readonly activeTools: Array<keyof TOOLS> | undefined;
|
|
166
|
+
|
|
167
|
+
/** Array of results from previous steps (empty for first step). */
|
|
168
|
+
readonly steps: ReadonlyArray<StepResult<TOOLS>>;
|
|
169
|
+
|
|
170
|
+
/** Additional provider-specific options for this step. */
|
|
171
|
+
readonly providerOptions: ProviderOptions | undefined;
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Timeout configuration for the generation.
|
|
175
|
+
* Can be a number (milliseconds) or an object with totalMs, stepMs, chunkMs.
|
|
176
|
+
*/
|
|
177
|
+
readonly timeout: TimeoutConfiguration | undefined;
|
|
178
|
+
|
|
179
|
+
/** Additional HTTP headers sent with the request. */
|
|
180
|
+
readonly headers: Record<string, string | undefined> | undefined;
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Condition(s) for stopping the generation.
|
|
184
|
+
* When the condition is an array, any of the conditions can be met to stop.
|
|
185
|
+
*/
|
|
186
|
+
readonly stopWhen:
|
|
187
|
+
| StopCondition<TOOLS>
|
|
188
|
+
| Array<StopCondition<TOOLS>>
|
|
189
|
+
| undefined;
|
|
190
|
+
|
|
191
|
+
/** The output specification for structured outputs, if configured. */
|
|
192
|
+
readonly output: OUTPUT | undefined;
|
|
193
|
+
|
|
194
|
+
/** Abort signal for cancelling the operation. */
|
|
195
|
+
readonly abortSignal: AbortSignal | undefined;
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Settings for controlling what data is included in step results.
|
|
199
|
+
*/
|
|
200
|
+
readonly include: INCLUDE | undefined;
|
|
201
|
+
|
|
202
|
+
/** Identifier from telemetry settings for grouping related operations. */
|
|
203
|
+
readonly functionId: string | undefined;
|
|
204
|
+
|
|
205
|
+
/** Additional metadata from telemetry settings. */
|
|
206
|
+
readonly metadata: Record<string, unknown> | undefined;
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* User-defined context object. May be updated from `prepareStep` between steps.
|
|
210
|
+
*/
|
|
211
|
+
readonly experimental_context: unknown;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Event passed to the `onToolCallStart` callback.
|
|
216
|
+
*
|
|
217
|
+
* Called when a tool execution begins, before the tool's `execute` function is invoked.
|
|
218
|
+
*/
|
|
219
|
+
export interface OnToolCallStartEvent<TOOLS extends ToolSet = ToolSet> {
|
|
220
|
+
/** Zero-based index of the current step where this tool call occurs. */
|
|
221
|
+
readonly stepNumber: number | undefined;
|
|
222
|
+
|
|
223
|
+
/** The model being used for this step. */
|
|
224
|
+
readonly model: CallbackModelInfo | undefined;
|
|
225
|
+
|
|
226
|
+
/** The full tool call object. */
|
|
227
|
+
readonly toolCall: TypedToolCall<TOOLS>;
|
|
228
|
+
|
|
229
|
+
/** The conversation messages available at tool execution time. */
|
|
230
|
+
readonly messages: Array<ModelMessage>;
|
|
231
|
+
|
|
232
|
+
/** Signal for cancelling the operation. */
|
|
233
|
+
readonly abortSignal: AbortSignal | undefined;
|
|
234
|
+
|
|
235
|
+
/** Identifier from telemetry settings for grouping related operations. */
|
|
236
|
+
readonly functionId: string | undefined;
|
|
237
|
+
|
|
238
|
+
/** Additional metadata from telemetry settings. */
|
|
239
|
+
readonly metadata: Record<string, unknown> | undefined;
|
|
240
|
+
|
|
241
|
+
/** User-defined context object flowing through the generation. */
|
|
242
|
+
readonly experimental_context: unknown;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Event passed to the `onToolCallFinish` callback.
|
|
247
|
+
*
|
|
248
|
+
* Called when a tool execution completes, either successfully or with an error.
|
|
249
|
+
* Uses a discriminated union on the `success` field.
|
|
250
|
+
*/
|
|
251
|
+
export type OnToolCallFinishEvent<TOOLS extends ToolSet = ToolSet> = {
|
|
252
|
+
/** Zero-based index of the current step where this tool call occurred. */
|
|
253
|
+
readonly stepNumber: number | undefined;
|
|
254
|
+
|
|
255
|
+
/** The model being used for this step. */
|
|
256
|
+
readonly model: CallbackModelInfo | undefined;
|
|
257
|
+
|
|
258
|
+
/** The full tool call object. */
|
|
259
|
+
readonly toolCall: TypedToolCall<TOOLS>;
|
|
260
|
+
|
|
261
|
+
/** The conversation messages available at tool execution time. */
|
|
262
|
+
readonly messages: Array<ModelMessage>;
|
|
263
|
+
|
|
264
|
+
/** Signal for cancelling the operation. */
|
|
265
|
+
readonly abortSignal: AbortSignal | undefined;
|
|
266
|
+
|
|
267
|
+
/** Execution time of the tool call in milliseconds. */
|
|
268
|
+
readonly durationMs: number;
|
|
269
|
+
|
|
270
|
+
/** Identifier from telemetry settings for grouping related operations. */
|
|
271
|
+
readonly functionId: string | undefined;
|
|
272
|
+
|
|
273
|
+
/** Additional metadata from telemetry settings. */
|
|
274
|
+
readonly metadata: Record<string, unknown> | undefined;
|
|
275
|
+
|
|
276
|
+
/** User-defined context object flowing through the generation. */
|
|
277
|
+
readonly experimental_context: unknown;
|
|
278
|
+
} & (
|
|
279
|
+
| {
|
|
280
|
+
/** Indicates the tool call succeeded. */
|
|
281
|
+
readonly success: true;
|
|
282
|
+
/** The tool's return value. */
|
|
283
|
+
readonly output: unknown;
|
|
284
|
+
readonly error?: never;
|
|
285
|
+
}
|
|
286
|
+
| {
|
|
287
|
+
/** Indicates the tool call failed. */
|
|
288
|
+
readonly success: false;
|
|
289
|
+
readonly output?: never;
|
|
290
|
+
/** The error that occurred during tool execution. */
|
|
291
|
+
readonly error: unknown;
|
|
292
|
+
}
|
|
293
|
+
);
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Event passed to the `onStepFinish` callback.
|
|
297
|
+
*
|
|
298
|
+
* Called when a step (LLM call) completes.
|
|
299
|
+
* This is simply the StepResult for that step.
|
|
300
|
+
*/
|
|
301
|
+
export type OnStepFinishEvent<TOOLS extends ToolSet = ToolSet> =
|
|
302
|
+
StepResult<TOOLS>;
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Event passed to the `onFinish` callback.
|
|
306
|
+
*
|
|
307
|
+
* Called when the entire generation completes (all steps finished).
|
|
308
|
+
* Includes the final step's result along with aggregated data from all steps.
|
|
309
|
+
*/
|
|
310
|
+
export type OnFinishEvent<TOOLS extends ToolSet = ToolSet> =
|
|
311
|
+
StepResult<TOOLS> & {
|
|
312
|
+
/** Array containing results from all steps in the generation. */
|
|
313
|
+
readonly steps: StepResult<TOOLS>[];
|
|
314
|
+
|
|
315
|
+
/** Aggregated token usage across all steps. */
|
|
316
|
+
readonly totalUsage: LanguageModelUsage;
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* The final state of the user-defined context object.
|
|
320
|
+
*
|
|
321
|
+
* Experimental (can break in patch releases).
|
|
322
|
+
*
|
|
323
|
+
* @default undefined
|
|
324
|
+
*/
|
|
325
|
+
experimental_context: unknown;
|
|
326
|
+
|
|
327
|
+
/** Identifier from telemetry settings for grouping related operations. */
|
|
328
|
+
readonly functionId: string | undefined;
|
|
329
|
+
|
|
330
|
+
/** Additional metadata from telemetry settings. */
|
|
331
|
+
readonly metadata: Record<string, unknown> | undefined;
|
|
332
|
+
};
|
|
@@ -60,6 +60,17 @@ export async function executeToolCall<TOOLS extends ToolSet>({
|
|
|
60
60
|
return undefined;
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
+
const baseCallbackEvent = {
|
|
64
|
+
stepNumber,
|
|
65
|
+
model,
|
|
66
|
+
toolCall,
|
|
67
|
+
messages,
|
|
68
|
+
abortSignal,
|
|
69
|
+
functionId: telemetry?.functionId,
|
|
70
|
+
metadata: telemetry?.metadata as Record<string, unknown> | undefined,
|
|
71
|
+
experimental_context,
|
|
72
|
+
};
|
|
73
|
+
|
|
63
74
|
return recordSpan({
|
|
64
75
|
name: 'ai.toolCall',
|
|
65
76
|
attributes: selectTelemetryAttributes({
|
|
@@ -81,16 +92,7 @@ export async function executeToolCall<TOOLS extends ToolSet>({
|
|
|
81
92
|
let output: unknown;
|
|
82
93
|
|
|
83
94
|
try {
|
|
84
|
-
await onToolCallStart?.(
|
|
85
|
-
stepNumber,
|
|
86
|
-
model,
|
|
87
|
-
toolCall,
|
|
88
|
-
messages,
|
|
89
|
-
abortSignal,
|
|
90
|
-
functionId: telemetry?.functionId,
|
|
91
|
-
metadata: telemetry?.metadata as Record<string, unknown> | undefined,
|
|
92
|
-
experimental_context,
|
|
93
|
-
});
|
|
95
|
+
await onToolCallStart?.(baseCallbackEvent);
|
|
94
96
|
} catch (_ignored) {
|
|
95
97
|
// Errors in callbacks should not break the generation flow.
|
|
96
98
|
}
|
|
@@ -126,19 +128,10 @@ export async function executeToolCall<TOOLS extends ToolSet>({
|
|
|
126
128
|
|
|
127
129
|
try {
|
|
128
130
|
await onToolCallFinish?.({
|
|
129
|
-
|
|
130
|
-
model,
|
|
131
|
-
toolCall,
|
|
132
|
-
messages,
|
|
133
|
-
abortSignal,
|
|
131
|
+
...baseCallbackEvent,
|
|
134
132
|
success: false,
|
|
135
133
|
error,
|
|
136
134
|
durationMs,
|
|
137
|
-
functionId: telemetry?.functionId,
|
|
138
|
-
metadata: telemetry?.metadata as
|
|
139
|
-
| Record<string, unknown>
|
|
140
|
-
| undefined,
|
|
141
|
-
experimental_context,
|
|
142
135
|
});
|
|
143
136
|
} catch (_ignored) {
|
|
144
137
|
// Errors in callbacks should not break the generation flow.
|
|
@@ -162,17 +155,10 @@ export async function executeToolCall<TOOLS extends ToolSet>({
|
|
|
162
155
|
|
|
163
156
|
try {
|
|
164
157
|
await onToolCallFinish?.({
|
|
165
|
-
|
|
166
|
-
model,
|
|
167
|
-
toolCall,
|
|
168
|
-
messages,
|
|
169
|
-
abortSignal,
|
|
158
|
+
...baseCallbackEvent,
|
|
170
159
|
success: true,
|
|
171
160
|
output,
|
|
172
161
|
durationMs,
|
|
173
|
-
functionId: telemetry?.functionId,
|
|
174
|
-
metadata: telemetry?.metadata as Record<string, unknown> | undefined,
|
|
175
|
-
experimental_context,
|
|
176
162
|
});
|
|
177
163
|
} catch (_ignored) {
|
|
178
164
|
// Errors in callbacks should not break the generation flow.
|