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.
@@ -5,7 +5,14 @@ import { StreamTextTransform } from '../generate-text/stream-text';
5
5
  import { StreamTextResult } from '../generate-text/stream-text-result';
6
6
  import { ToolSet } from '../generate-text/tool-set';
7
7
  import { TimeoutConfiguration } from '../prompt/call-settings';
8
- import { ToolLoopAgentOnStepFinishCallback } from './tool-loop-agent-on-step-finish-callback';
8
+ import type {
9
+ ToolLoopAgentOnFinishCallback,
10
+ ToolLoopAgentOnStartCallback,
11
+ ToolLoopAgentOnStepFinishCallback,
12
+ ToolLoopAgentOnStepStartCallback,
13
+ ToolLoopAgentOnToolCallFinishCallback,
14
+ ToolLoopAgentOnToolCallStartCallback,
15
+ } from './tool-loop-agent-settings';
9
16
 
10
17
  /**
11
18
  * Parameters for calling an agent.
@@ -57,10 +64,35 @@ export type AgentCallParameters<CALL_OPTIONS, TOOLS extends ToolSet = {}> = ([
57
64
  */
58
65
  timeout?: TimeoutConfiguration;
59
66
 
67
+ /**
68
+ * Callback that is called when the agent operation begins, before any LLM calls.
69
+ */
70
+ experimental_onStart?: ToolLoopAgentOnStartCallback<TOOLS>;
71
+
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
+ /**
78
+ * Callback that is called before each tool execution begins.
79
+ */
80
+ experimental_onToolCallStart?: ToolLoopAgentOnToolCallStartCallback<TOOLS>;
81
+
82
+ /**
83
+ * Callback that is called after each tool execution completes.
84
+ */
85
+ experimental_onToolCallFinish?: ToolLoopAgentOnToolCallFinishCallback<TOOLS>;
86
+
60
87
  /**
61
88
  * Callback that is called when each step (LLM call) is finished, including intermediate steps.
62
89
  */
63
90
  onStepFinish?: ToolLoopAgentOnStepFinishCallback<TOOLS>;
91
+
92
+ /**
93
+ * Callback that is called when all steps are finished and the response is complete.
94
+ */
95
+ onFinish?: ToolLoopAgentOnFinishCallback<TOOLS>;
64
96
  };
65
97
 
66
98
  /**
@@ -7,7 +7,7 @@ import { UIMessageStreamResponseInit } from '../ui-message-stream/ui-message-str
7
7
  import { InferUITools, UIMessage } from '../ui/ui-messages';
8
8
  import { Agent } from './agent';
9
9
  import { createAgentUIStream } from './create-agent-ui-stream';
10
- import { ToolLoopAgentOnStepFinishCallback } from './tool-loop-agent-on-step-finish-callback';
10
+ import type { ToolLoopAgentOnStepFinishCallback } from './tool-loop-agent-settings';
11
11
 
12
12
  /**
13
13
  * Runs the agent and returns a response object with a UI message stream.
@@ -8,7 +8,7 @@ import { InferUITools, UIMessage } from '../ui/ui-messages';
8
8
  import { validateUIMessages } from '../ui/validate-ui-messages';
9
9
  import { AsyncIterableStream } from '../util/async-iterable-stream';
10
10
  import { Agent } from './agent';
11
- import { ToolLoopAgentOnStepFinishCallback } from './tool-loop-agent-on-step-finish-callback';
11
+ import type { ToolLoopAgentOnStepFinishCallback } from './tool-loop-agent-settings';
12
12
 
13
13
  /**
14
14
  * Runs the agent and stream the output as a UI message stream.
@@ -3,9 +3,13 @@ export {
3
3
  type AgentCallParameters,
4
4
  type AgentStreamParameters,
5
5
  } from './agent';
6
- export { type ToolLoopAgentOnFinishCallback } from './tool-loop-agent-on-finish-callback';
7
- export { type ToolLoopAgentOnStepFinishCallback } from './tool-loop-agent-on-step-finish-callback';
8
6
  export {
7
+ type ToolLoopAgentOnFinishCallback,
8
+ type ToolLoopAgentOnStartCallback,
9
+ type ToolLoopAgentOnStepFinishCallback,
10
+ type ToolLoopAgentOnStepStartCallback,
11
+ type ToolLoopAgentOnToolCallFinishCallback,
12
+ type ToolLoopAgentOnToolCallStartCallback,
9
13
  type ToolLoopAgentSettings,
10
14
 
11
15
  /**
@@ -8,7 +8,7 @@ import { UIMessageStreamResponseInit } from '../ui-message-stream/ui-message-str
8
8
  import { InferUITools, UIMessage } from '../ui/ui-messages';
9
9
  import { Agent } from './agent';
10
10
  import { createAgentUIStream } from './create-agent-ui-stream';
11
- import { ToolLoopAgentOnStepFinishCallback } from './tool-loop-agent-on-step-finish-callback';
11
+ import type { ToolLoopAgentOnStepFinishCallback } from './tool-loop-agent-settings';
12
12
 
13
13
  /**
14
14
  * Pipes the agent UI message stream to a Node.js ServerResponse object.
@@ -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
- import { ToolLoopAgentOnFinishCallback } from './tool-loop-agent-on-finish-callback';
19
- import { ToolLoopAgentOnStepFinishCallback } from './tool-loop-agent-on-step-finish-callback';
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
- 'prepareCall' | 'instructions' | 'onStepFinish'
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 { onStepFinish: _settingsOnStepFinish, ...settingsWithoutCallback } =
65
- this.settings;
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
- ...settingsWithoutCallback,
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 mergeOnStepFinishCallbacks(
93
- methodCallback: ToolLoopAgentOnStepFinishCallback<TOOLS> | undefined,
94
- ): ToolLoopAgentOnStepFinishCallback<TOOLS> | undefined {
95
- const constructorCallback = this.settings.onStepFinish;
96
-
97
- if (methodCallback && constructorCallback) {
98
- return async (stepResult: StepResult<TOOLS>) => {
99
- await constructorCallback(stepResult);
100
- await methodCallback(stepResult);
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
- onStepFinish: this.mergeOnStepFinishCallbacks(onStepFinish),
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
- onStepFinish: this.mergeOnStepFinishCallbacks(onStepFinish),
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
  }
@@ -1,31 +0,0 @@
1
- import { StepResult } from '../generate-text/step-result';
2
- import { ToolSet } from '../generate-text/tool-set';
3
- import { LanguageModelUsage } from '../types/usage';
4
-
5
- /**
6
- * Callback that is set using the `onFinish` option.
7
- *
8
- * @param event - The event that is passed to the callback.
9
- */
10
- export type ToolLoopAgentOnFinishCallback<TOOLS extends ToolSet = {}> = (
11
- event: StepResult<TOOLS> & {
12
- /**
13
- * Details for all steps.
14
- */
15
- readonly steps: StepResult<TOOLS>[];
16
-
17
- /**
18
- * Total usage for all steps. This is the sum of the usage of all steps.
19
- */
20
- readonly totalUsage: LanguageModelUsage;
21
-
22
- /**
23
- * Context that is passed into tool execution.
24
- *
25
- * Experimental (can break in patch releases).
26
- *
27
- * @default undefined
28
- */
29
- experimental_context: unknown;
30
- },
31
- ) => PromiseLike<void> | void;
@@ -1,11 +0,0 @@
1
- import { StepResult } from '../generate-text/step-result';
2
- import { ToolSet } from '../generate-text/tool-set';
3
-
4
- /**
5
- * Callback that is set using the `onStepFinish` option.
6
- *
7
- * @param stepResult - The result of the step.
8
- */
9
- export type ToolLoopAgentOnStepFinishCallback<TOOLS extends ToolSet = {}> = (
10
- stepResult: StepResult<TOOLS>,
11
- ) => Promise<void> | void;