ai 6.0.80 → 6.0.82

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.
@@ -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.80" : "0.0.0-test";
156
+ var VERSION = true ? "6.0.82" : "0.0.0-test";
157
157
 
158
158
  // src/util/download/download.ts
159
159
  var download = async ({ url }) => {
@@ -128,7 +128,7 @@ import {
128
128
  } from "@ai-sdk/provider-utils";
129
129
 
130
130
  // src/version.ts
131
- var VERSION = true ? "6.0.80" : "0.0.0-test";
131
+ var VERSION = true ? "6.0.82" : "0.0.0-test";
132
132
 
133
133
  // src/util/download/download.ts
134
134
  var download = async ({ url }) => {
@@ -178,9 +178,11 @@ const { text } = await generateText({
178
178
 
179
179
  You can publish your own tool packages to npm for others to use. Simply export your tool objects from your package:
180
180
 
181
- ```ts
182
- // my-tools/index.ts
183
- export const myTool = {
181
+ ```ts filename="my-tools/index.ts"
182
+ import { tool } from 'ai';
183
+ import { z } from 'zod';
184
+
185
+ export const myTool = tool({
184
186
  description: 'A helpful tool',
185
187
  inputSchema: z.object({
186
188
  query: z.string(),
@@ -189,7 +191,7 @@ export const myTool = {
189
191
  // your tool logic
190
192
  return result;
191
193
  },
192
- };
194
+ });
193
195
  ```
194
196
 
195
197
  Anyone can then install and use your tools by importing them.
@@ -132,7 +132,7 @@ export const weatherTool = tool({
132
132
  isOptional: true,
133
133
  type: 'Zod Schema | JSON Schema',
134
134
  description:
135
- 'The schema of the output that the tool produces. Used for validation and type inference.',
135
+ 'The schema of the output that the tool produces. Used for type inference.',
136
136
  },
137
137
  {
138
138
  name: 'toModelOutput',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ai",
3
- "version": "6.0.80",
3
+ "version": "6.0.82",
4
4
  "description": "AI SDK by Vercel - The AI Toolkit for TypeScript and JavaScript",
5
5
  "license": "Apache-2.0",
6
6
  "sideEffects": false,
@@ -45,7 +45,7 @@
45
45
  },
46
46
  "dependencies": {
47
47
  "@opentelemetry/api": "1.9.0",
48
- "@ai-sdk/gateway": "3.0.41",
48
+ "@ai-sdk/gateway": "3.0.42",
49
49
  "@ai-sdk/provider": "3.0.8",
50
50
  "@ai-sdk/provider-utils": "4.0.14"
51
51
  },
@@ -7,6 +7,7 @@ import { UIMessage } from '../ui/ui-messages';
7
7
  import { handleUIMessageStreamFinish } from './handle-ui-message-stream-finish';
8
8
  import { InferUIMessageChunk } from './ui-message-chunks';
9
9
  import { UIMessageStreamOnFinishCallback } from './ui-message-stream-on-finish-callback';
10
+ import { UIMessageStreamOnStepFinishCallback } from './ui-message-stream-on-step-finish-callback';
10
11
  import { UIMessageStreamWriter } from './ui-message-stream-writer';
11
12
 
12
13
  /**
@@ -16,6 +17,7 @@ import { UIMessageStreamWriter } from './ui-message-stream-writer';
16
17
  * @param options.onError - A function that extracts an error message from an error. Defaults to `getErrorMessage`.
17
18
  * @param options.originalMessages - The original messages. If provided, persistence mode is assumed
18
19
  * and a message ID is provided for the response message.
20
+ * @param options.onStepFinish - A callback that is called when each step finishes. Useful for persisting intermediate messages.
19
21
  * @param options.onFinish - A callback that is called when the stream finishes.
20
22
  * @param options.generateId - A function that generates a unique ID. Defaults to the built-in ID generator.
21
23
  *
@@ -25,6 +27,7 @@ export function createUIMessageStream<UI_MESSAGE extends UIMessage>({
25
27
  execute,
26
28
  onError = getErrorMessage,
27
29
  originalMessages,
30
+ onStepFinish,
28
31
  onFinish,
29
32
  generateId = generateIdFunc,
30
33
  }: {
@@ -39,6 +42,11 @@ export function createUIMessageStream<UI_MESSAGE extends UIMessage>({
39
42
  */
40
43
  originalMessages?: UI_MESSAGE[];
41
44
 
45
+ /**
46
+ * Callback that is called when each step finishes during multi-step agent runs.
47
+ */
48
+ onStepFinish?: UIMessageStreamOnStepFinishCallback<UI_MESSAGE>;
49
+
42
50
  onFinish?: UIMessageStreamOnFinishCallback<UI_MESSAGE>;
43
51
 
44
52
  generateId?: IdGenerator;
@@ -130,6 +138,7 @@ export function createUIMessageStream<UI_MESSAGE extends UIMessage>({
130
138
  stream,
131
139
  messageId: generateId(),
132
140
  originalMessages,
141
+ onStepFinish,
133
142
  onFinish,
134
143
  onError,
135
144
  });
@@ -7,10 +7,12 @@ import { UIMessage } from '../ui/ui-messages';
7
7
  import { ErrorHandler } from '../util/error-handler';
8
8
  import { InferUIMessageChunk, UIMessageChunk } from './ui-message-chunks';
9
9
  import { UIMessageStreamOnFinishCallback } from './ui-message-stream-on-finish-callback';
10
+ import { UIMessageStreamOnStepFinishCallback } from './ui-message-stream-on-step-finish-callback';
10
11
 
11
12
  export function handleUIMessageStreamFinish<UI_MESSAGE extends UIMessage>({
12
13
  messageId,
13
14
  originalMessages = [],
15
+ onStepFinish,
14
16
  onFinish,
15
17
  onError,
16
18
  stream,
@@ -30,6 +32,11 @@ export function handleUIMessageStreamFinish<UI_MESSAGE extends UIMessage>({
30
32
 
31
33
  onError: ErrorHandler;
32
34
 
35
+ /**
36
+ * Callback that is called when each step finishes during multi-step agent runs.
37
+ */
38
+ onStepFinish?: UIMessageStreamOnStepFinishCallback<UI_MESSAGE>;
39
+
33
40
  onFinish?: UIMessageStreamOnFinishCallback<UI_MESSAGE>;
34
41
  }): ReadableStream<InferUIMessageChunk<UI_MESSAGE>> {
35
42
  // last message is only relevant for assistant messages
@@ -69,7 +76,8 @@ export function handleUIMessageStreamFinish<UI_MESSAGE extends UIMessage>({
69
76
  }),
70
77
  );
71
78
 
72
- if (onFinish == null) {
79
+ // Only process the stream if we need to track state for callbacks
80
+ if (onFinish == null && onStepFinish == null) {
73
81
  return idInjectedStream;
74
82
  }
75
83
 
@@ -110,6 +118,29 @@ export function handleUIMessageStreamFinish<UI_MESSAGE extends UIMessage>({
110
118
  });
111
119
  };
112
120
 
121
+ const callOnStepFinish = async () => {
122
+ if (!onStepFinish) {
123
+ return;
124
+ }
125
+
126
+ const isContinuation = state.message.id === lastMessage?.id;
127
+
128
+ try {
129
+ await onStepFinish({
130
+ isContinuation,
131
+ responseMessage: structuredClone(state.message) as UI_MESSAGE,
132
+ messages: [
133
+ ...(isContinuation
134
+ ? originalMessages.slice(0, -1)
135
+ : originalMessages),
136
+ structuredClone(state.message),
137
+ ] as UI_MESSAGE[],
138
+ });
139
+ } catch (error) {
140
+ onError(error);
141
+ }
142
+ };
143
+
113
144
  return processUIMessageStream<UI_MESSAGE>({
114
145
  stream: idInjectedStream,
115
146
  runUpdateMessageJob,
@@ -119,7 +150,11 @@ export function handleUIMessageStreamFinish<UI_MESSAGE extends UIMessage>({
119
150
  InferUIMessageChunk<UI_MESSAGE>,
120
151
  InferUIMessageChunk<UI_MESSAGE>
121
152
  >({
122
- transform(chunk, controller) {
153
+ async transform(chunk, controller) {
154
+ if (chunk.type === 'finish-step') {
155
+ await callOnStepFinish();
156
+ }
157
+
123
158
  controller.enqueue(chunk);
124
159
  },
125
160
  // @ts-expect-error cancel is still new and missing from types https://developer.mozilla.org/en-US/docs/Web/API/TransformStream#browser_compatibility
@@ -10,4 +10,5 @@ export {
10
10
  } from './ui-message-chunks';
11
11
  export { UI_MESSAGE_STREAM_HEADERS } from './ui-message-stream-headers';
12
12
  export type { UIMessageStreamOnFinishCallback } from './ui-message-stream-on-finish-callback';
13
+ export type { UIMessageStreamOnStepFinishCallback } from './ui-message-stream-on-step-finish-callback';
13
14
  export type { UIMessageStreamWriter } from './ui-message-stream-writer';
@@ -0,0 +1,25 @@
1
+ import { UIMessage } from '../ui/ui-messages';
2
+
3
+ /**
4
+ * Callback that is called when a step finishes during streaming.
5
+ * This is useful for persisting intermediate UI messages during multi-step agent runs.
6
+ */
7
+ export type UIMessageStreamOnStepFinishCallback<UI_MESSAGE extends UIMessage> =
8
+ (event: {
9
+ /**
10
+ * The updated list of UI messages at the end of this step.
11
+ */
12
+ messages: UI_MESSAGE[];
13
+
14
+ /**
15
+ * Indicates whether the response message is a continuation of the last original message,
16
+ * or if a new message was created.
17
+ */
18
+ isContinuation: boolean;
19
+
20
+ /**
21
+ * The message that was sent to the client as a response
22
+ * (including the original message if it was extended).
23
+ */
24
+ responseMessage: UI_MESSAGE;
25
+ }) => PromiseLike<void> | void;