ai 3.1.16 → 3.1.18

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ai",
3
- "version": "3.1.16",
3
+ "version": "3.1.18",
4
4
  "license": "Apache-2.0",
5
5
  "sideEffects": false,
6
6
  "main": "./dist/index.js",
@@ -80,7 +80,7 @@
80
80
  "@mistralai/mistralai": "0.1.3",
81
81
  "@solidjs/testing-library": "0.8.4",
82
82
  "@testing-library/jest-dom": "^6.4.5",
83
- "@testing-library/react": "^14.0.0",
83
+ "@testing-library/react": "^15.0.7",
84
84
  "@testing-library/user-event": "^14.5.1",
85
85
  "@testing-library/vue": "^8.0.1",
86
86
  "@types/json-schema": "7.0.15",
@@ -40,6 +40,9 @@ interface ToolResult<NAME extends string, ARGS, RESULT> {
40
40
  result: RESULT;
41
41
  }
42
42
 
43
+ /**
44
+ * @deprecated use AI SDK 3.1 CoreTool / ToolResult instead
45
+ */
43
46
  interface FunctionCall {
44
47
  /**
45
48
  * The arguments to call the function with, as generated by the model in JSON
@@ -54,6 +57,8 @@ interface FunctionCall {
54
57
  name?: string;
55
58
  }
56
59
  /**
60
+ * @deprecated use AI SDK 3.1 CoreTool / ToolResult instead
61
+ *
57
62
  * The tool calls generated by the model, such as function calls.
58
63
  */
59
64
  interface ToolCall {
@@ -71,17 +76,20 @@ Once the call is complete, the invocation is a tool result.
71
76
  */
72
77
  type ToolInvocation = ToolCall$1<string, any> | ToolResult<string, any, any>;
73
78
  /**
74
- * Shared types between the API and UI packages.
79
+ * AI SDK UI Messages. They are used in the client and to communicate between the frontend and the API routes.
75
80
  */
76
81
  interface Message {
77
82
  id: string;
78
- tool_call_id?: string;
79
83
  createdAt?: Date;
80
84
  content: string;
85
+ tool_call_id?: string;
81
86
  /**
82
87
  @deprecated Use AI SDK RSC instead: https://sdk.vercel.ai/docs/ai-sdk-rsc
83
88
  */
84
89
  ui?: string | JSX.Element | JSX.Element[] | null | undefined;
90
+ /**
91
+ * `function` and `tool` roles are deprecated.
92
+ */
85
93
  role: 'system' | 'user' | 'assistant' | 'function' | 'data' | 'tool';
86
94
  /**
87
95
  *
@@ -90,6 +98,8 @@ interface Message {
90
98
  */
91
99
  name?: string;
92
100
  /**
101
+ * @deprecated Use AI SDK 3.1 `toolInvocations` instead.
102
+ *
93
103
  * If the assistant role makes a function call, the `function_call` field
94
104
  * contains the function call name and arguments. Otherwise, the field should
95
105
  * not be set. (Deprecated and replaced by tool_calls.)
@@ -97,6 +107,8 @@ interface Message {
97
107
  function_call?: string | FunctionCall;
98
108
  data?: JSONValue;
99
109
  /**
110
+ * @deprecated Use AI SDK 3.1 `toolInvocations` instead.
111
+ *
100
112
  * If the assistant role makes a tool call, the `tool_calls` field contains
101
113
  * the tool call name and arguments. Otherwise, the field should not be set.
102
114
  */
@@ -40,6 +40,9 @@ interface ToolResult<NAME extends string, ARGS, RESULT> {
40
40
  result: RESULT;
41
41
  }
42
42
 
43
+ /**
44
+ * @deprecated use AI SDK 3.1 CoreTool / ToolResult instead
45
+ */
43
46
  interface FunctionCall {
44
47
  /**
45
48
  * The arguments to call the function with, as generated by the model in JSON
@@ -54,6 +57,8 @@ interface FunctionCall {
54
57
  name?: string;
55
58
  }
56
59
  /**
60
+ * @deprecated use AI SDK 3.1 CoreTool / ToolResult instead
61
+ *
57
62
  * The tool calls generated by the model, such as function calls.
58
63
  */
59
64
  interface ToolCall {
@@ -71,17 +76,20 @@ Once the call is complete, the invocation is a tool result.
71
76
  */
72
77
  type ToolInvocation = ToolCall$1<string, any> | ToolResult<string, any, any>;
73
78
  /**
74
- * Shared types between the API and UI packages.
79
+ * AI SDK UI Messages. They are used in the client and to communicate between the frontend and the API routes.
75
80
  */
76
81
  interface Message {
77
82
  id: string;
78
- tool_call_id?: string;
79
83
  createdAt?: Date;
80
84
  content: string;
85
+ tool_call_id?: string;
81
86
  /**
82
87
  @deprecated Use AI SDK RSC instead: https://sdk.vercel.ai/docs/ai-sdk-rsc
83
88
  */
84
89
  ui?: string | JSX.Element | JSX.Element[] | null | undefined;
90
+ /**
91
+ * `function` and `tool` roles are deprecated.
92
+ */
85
93
  role: 'system' | 'user' | 'assistant' | 'function' | 'data' | 'tool';
86
94
  /**
87
95
  *
@@ -90,6 +98,8 @@ interface Message {
90
98
  */
91
99
  name?: string;
92
100
  /**
101
+ * @deprecated Use AI SDK 3.1 `toolInvocations` instead.
102
+ *
93
103
  * If the assistant role makes a function call, the `function_call` field
94
104
  * contains the function call name and arguments. Otherwise, the field should
95
105
  * not be set. (Deprecated and replaced by tool_calls.)
@@ -97,6 +107,8 @@ interface Message {
97
107
  function_call?: string | FunctionCall;
98
108
  data?: JSONValue;
99
109
  /**
110
+ * @deprecated Use AI SDK 3.1 `toolInvocations` instead.
111
+ *
100
112
  * If the assistant role makes a tool call, the `tool_calls` field contains
101
113
  * the tool call name and arguments. Otherwise, the field should not be set.
102
114
  */
@@ -72,6 +72,9 @@ type UseAssistantOptions = {
72
72
  onError?: (error: Error) => void;
73
73
  };
74
74
 
75
+ /**
76
+ * @deprecated use AI SDK 3.1 CoreTool / ToolResult instead
77
+ */
75
78
  interface FunctionCall {
76
79
  /**
77
80
  * The arguments to call the function with, as generated by the model in JSON
@@ -86,6 +89,8 @@ interface FunctionCall {
86
89
  name?: string;
87
90
  }
88
91
  /**
92
+ * @deprecated use AI SDK 3.1 CoreTool / ToolResult instead
93
+ *
89
94
  * The tool calls generated by the model, such as function calls.
90
95
  */
91
96
  interface ToolCall {
@@ -97,6 +102,8 @@ interface ToolCall {
97
102
  };
98
103
  }
99
104
  /**
105
+ * @deprecated use AI SDK 3.1 CoreTool / ToolChoice instead
106
+ *
100
107
  * Controls which (if any) function is called by the model.
101
108
  * - none means the model will not call a function and instead generates a message.
102
109
  * - auto means the model can pick between generating a message or calling a function.
@@ -110,6 +117,8 @@ type ToolChoice = 'none' | 'auto' | {
110
117
  };
111
118
  };
112
119
  /**
120
+ * @deprecated use AI SDK 3.1 CoreTool instead
121
+ *
113
122
  * A list of tools the model may call. Currently, only functions are supported as a tool.
114
123
  * Use this to provide a list of functions the model may generate JSON inputs for.
115
124
  */
@@ -117,6 +126,9 @@ interface Tool {
117
126
  type: 'function';
118
127
  function: Function;
119
128
  }
129
+ /**
130
+ * @deprecated use AI SDK 3.1 CoreTool instead
131
+ */
120
132
  interface Function {
121
133
  /**
122
134
  * The name of the function to be called. Must be a-z, A-Z, 0-9, or contain
@@ -147,17 +159,20 @@ Once the call is complete, the invocation is a tool result.
147
159
  */
148
160
  type ToolInvocation = ToolCall$1<string, any> | ToolResult<string, any, any>;
149
161
  /**
150
- * Shared types between the API and UI packages.
162
+ * AI SDK UI Messages. They are used in the client and to communicate between the frontend and the API routes.
151
163
  */
152
164
  interface Message {
153
165
  id: string;
154
- tool_call_id?: string;
155
166
  createdAt?: Date;
156
167
  content: string;
168
+ tool_call_id?: string;
157
169
  /**
158
170
  @deprecated Use AI SDK RSC instead: https://sdk.vercel.ai/docs/ai-sdk-rsc
159
171
  */
160
172
  ui?: string | JSX.Element | JSX.Element[] | null | undefined;
173
+ /**
174
+ * `function` and `tool` roles are deprecated.
175
+ */
161
176
  role: 'system' | 'user' | 'assistant' | 'function' | 'data' | 'tool';
162
177
  /**
163
178
  *
@@ -166,6 +181,8 @@ interface Message {
166
181
  */
167
182
  name?: string;
168
183
  /**
184
+ * @deprecated Use AI SDK 3.1 `toolInvocations` instead.
185
+ *
169
186
  * If the assistant role makes a function call, the `function_call` field
170
187
  * contains the function call name and arguments. Otherwise, the field should
171
188
  * not be set. (Deprecated and replaced by tool_calls.)
@@ -173,6 +190,8 @@ interface Message {
173
190
  function_call?: string | FunctionCall;
174
191
  data?: JSONValue;
175
192
  /**
193
+ * @deprecated Use AI SDK 3.1 `toolInvocations` instead.
194
+ *
176
195
  * If the assistant role makes a tool call, the `tool_calls` field contains
177
196
  * the tool call name and arguments. Otherwise, the field should not be set.
178
197
  */
@@ -199,7 +218,13 @@ type ChatRequest = {
199
218
  tools?: Array<Tool>;
200
219
  tool_choice?: ToolChoice;
201
220
  };
221
+ /**
222
+ * @deprecated Use AI SDK 3.1 `streamText` and `onToolCall` instead.
223
+ */
202
224
  type FunctionCallHandler = (chatMessages: Message[], functionCall: FunctionCall) => Promise<ChatRequest | void>;
225
+ /**
226
+ * @deprecated Use AI SDK 3.1 `streamText` and `onToolCall` instead.
227
+ */
203
228
  type ToolCallHandler = (chatMessages: Message[], toolCalls: ToolCall[]) => Promise<ChatRequest | void>;
204
229
  type RequestOptions = {
205
230
  headers?: Record<string, string> | Headers;
@@ -234,18 +259,32 @@ type UseChatOptions = {
234
259
  */
235
260
  initialInput?: string;
236
261
  /**
262
+ * @deprecated Use AI SDK 3.1 `streamText` and `onToolCall` instead.
263
+ *
237
264
  * Callback function to be called when a function call is received.
238
265
  * If the function returns a `ChatRequest` object, the request will be sent
239
266
  * automatically to the API and will be used to update the chat.
240
267
  */
241
268
  experimental_onFunctionCall?: FunctionCallHandler;
242
269
  /**
270
+ * @deprecated Use AI SDK 3.1 `streamText` and `onToolCall` instead.
271
+ *
243
272
  * Callback function to be called when a tool call is received.
244
273
  * If the function returns a `ChatRequest` object, the request will be sent
245
274
  * automatically to the API and will be used to update the chat.
246
275
  */
247
276
  experimental_onToolCall?: ToolCallHandler;
248
277
  /**
278
+ Optional callback function that is invoked when a tool call is received.
279
+ Intended for automatic client-side tool execution.
280
+
281
+ You can optionally return a result for the tool call,
282
+ either synchronously or asynchronously.
283
+ */
284
+ onToolCall?: ({ toolCall, }: {
285
+ toolCall: ToolCall$1<string, unknown>;
286
+ }) => void | Promise<unknown> | unknown;
287
+ /**
249
288
  * Callback function to be called when the API response is received.
250
289
  */
251
290
  onResponse?: (response: Response) => void | Promise<void>;
@@ -447,7 +486,7 @@ type StreamingReactResponseAction = (payload: {
447
486
  messages: Message[];
448
487
  data?: Record<string, string>;
449
488
  }) => Promise<experimental_StreamingReactResponse>;
450
- declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, experimental_onFunctionCall, experimental_onToolCall, experimental_maxAutomaticRoundtrips, streamMode, onResponse, onFinish, onError, credentials, headers, body, generateId, }?: Omit<UseChatOptions, 'api'> & {
489
+ declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, experimental_onFunctionCall, experimental_onToolCall, onToolCall, experimental_maxAutomaticRoundtrips, streamMode, onResponse, onFinish, onError, credentials, headers, body, generateId, }?: Omit<UseChatOptions, 'api'> & {
451
490
  api?: string | StreamingReactResponseAction;
452
491
  key?: string;
453
492
  /**
@@ -72,6 +72,9 @@ type UseAssistantOptions = {
72
72
  onError?: (error: Error) => void;
73
73
  };
74
74
 
75
+ /**
76
+ * @deprecated use AI SDK 3.1 CoreTool / ToolResult instead
77
+ */
75
78
  interface FunctionCall {
76
79
  /**
77
80
  * The arguments to call the function with, as generated by the model in JSON
@@ -86,6 +89,8 @@ interface FunctionCall {
86
89
  name?: string;
87
90
  }
88
91
  /**
92
+ * @deprecated use AI SDK 3.1 CoreTool / ToolResult instead
93
+ *
89
94
  * The tool calls generated by the model, such as function calls.
90
95
  */
91
96
  interface ToolCall {
@@ -97,6 +102,8 @@ interface ToolCall {
97
102
  };
98
103
  }
99
104
  /**
105
+ * @deprecated use AI SDK 3.1 CoreTool / ToolChoice instead
106
+ *
100
107
  * Controls which (if any) function is called by the model.
101
108
  * - none means the model will not call a function and instead generates a message.
102
109
  * - auto means the model can pick between generating a message or calling a function.
@@ -110,6 +117,8 @@ type ToolChoice = 'none' | 'auto' | {
110
117
  };
111
118
  };
112
119
  /**
120
+ * @deprecated use AI SDK 3.1 CoreTool instead
121
+ *
113
122
  * A list of tools the model may call. Currently, only functions are supported as a tool.
114
123
  * Use this to provide a list of functions the model may generate JSON inputs for.
115
124
  */
@@ -117,6 +126,9 @@ interface Tool {
117
126
  type: 'function';
118
127
  function: Function;
119
128
  }
129
+ /**
130
+ * @deprecated use AI SDK 3.1 CoreTool instead
131
+ */
120
132
  interface Function {
121
133
  /**
122
134
  * The name of the function to be called. Must be a-z, A-Z, 0-9, or contain
@@ -147,17 +159,20 @@ Once the call is complete, the invocation is a tool result.
147
159
  */
148
160
  type ToolInvocation = ToolCall$1<string, any> | ToolResult<string, any, any>;
149
161
  /**
150
- * Shared types between the API and UI packages.
162
+ * AI SDK UI Messages. They are used in the client and to communicate between the frontend and the API routes.
151
163
  */
152
164
  interface Message {
153
165
  id: string;
154
- tool_call_id?: string;
155
166
  createdAt?: Date;
156
167
  content: string;
168
+ tool_call_id?: string;
157
169
  /**
158
170
  @deprecated Use AI SDK RSC instead: https://sdk.vercel.ai/docs/ai-sdk-rsc
159
171
  */
160
172
  ui?: string | JSX.Element | JSX.Element[] | null | undefined;
173
+ /**
174
+ * `function` and `tool` roles are deprecated.
175
+ */
161
176
  role: 'system' | 'user' | 'assistant' | 'function' | 'data' | 'tool';
162
177
  /**
163
178
  *
@@ -166,6 +181,8 @@ interface Message {
166
181
  */
167
182
  name?: string;
168
183
  /**
184
+ * @deprecated Use AI SDK 3.1 `toolInvocations` instead.
185
+ *
169
186
  * If the assistant role makes a function call, the `function_call` field
170
187
  * contains the function call name and arguments. Otherwise, the field should
171
188
  * not be set. (Deprecated and replaced by tool_calls.)
@@ -173,6 +190,8 @@ interface Message {
173
190
  function_call?: string | FunctionCall;
174
191
  data?: JSONValue;
175
192
  /**
193
+ * @deprecated Use AI SDK 3.1 `toolInvocations` instead.
194
+ *
176
195
  * If the assistant role makes a tool call, the `tool_calls` field contains
177
196
  * the tool call name and arguments. Otherwise, the field should not be set.
178
197
  */
@@ -199,7 +218,13 @@ type ChatRequest = {
199
218
  tools?: Array<Tool>;
200
219
  tool_choice?: ToolChoice;
201
220
  };
221
+ /**
222
+ * @deprecated Use AI SDK 3.1 `streamText` and `onToolCall` instead.
223
+ */
202
224
  type FunctionCallHandler = (chatMessages: Message[], functionCall: FunctionCall) => Promise<ChatRequest | void>;
225
+ /**
226
+ * @deprecated Use AI SDK 3.1 `streamText` and `onToolCall` instead.
227
+ */
203
228
  type ToolCallHandler = (chatMessages: Message[], toolCalls: ToolCall[]) => Promise<ChatRequest | void>;
204
229
  type RequestOptions = {
205
230
  headers?: Record<string, string> | Headers;
@@ -234,18 +259,32 @@ type UseChatOptions = {
234
259
  */
235
260
  initialInput?: string;
236
261
  /**
262
+ * @deprecated Use AI SDK 3.1 `streamText` and `onToolCall` instead.
263
+ *
237
264
  * Callback function to be called when a function call is received.
238
265
  * If the function returns a `ChatRequest` object, the request will be sent
239
266
  * automatically to the API and will be used to update the chat.
240
267
  */
241
268
  experimental_onFunctionCall?: FunctionCallHandler;
242
269
  /**
270
+ * @deprecated Use AI SDK 3.1 `streamText` and `onToolCall` instead.
271
+ *
243
272
  * Callback function to be called when a tool call is received.
244
273
  * If the function returns a `ChatRequest` object, the request will be sent
245
274
  * automatically to the API and will be used to update the chat.
246
275
  */
247
276
  experimental_onToolCall?: ToolCallHandler;
248
277
  /**
278
+ Optional callback function that is invoked when a tool call is received.
279
+ Intended for automatic client-side tool execution.
280
+
281
+ You can optionally return a result for the tool call,
282
+ either synchronously or asynchronously.
283
+ */
284
+ onToolCall?: ({ toolCall, }: {
285
+ toolCall: ToolCall$1<string, unknown>;
286
+ }) => void | Promise<unknown> | unknown;
287
+ /**
249
288
  * Callback function to be called when the API response is received.
250
289
  */
251
290
  onResponse?: (response: Response) => void | Promise<void>;
@@ -447,7 +486,7 @@ type StreamingReactResponseAction = (payload: {
447
486
  messages: Message[];
448
487
  data?: Record<string, string>;
449
488
  }) => Promise<experimental_StreamingReactResponse>;
450
- declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, experimental_onFunctionCall, experimental_onToolCall, experimental_maxAutomaticRoundtrips, streamMode, onResponse, onFinish, onError, credentials, headers, body, generateId, }?: Omit<UseChatOptions, 'api'> & {
489
+ declare function useChat({ api, id, initialMessages, initialInput, sendExtraMessageFields, experimental_onFunctionCall, experimental_onToolCall, onToolCall, experimental_maxAutomaticRoundtrips, streamMode, onResponse, onFinish, onError, credentials, headers, body, generateId, }?: Omit<UseChatOptions, 'api'> & {
451
490
  api?: string | StreamingReactResponseAction;
452
491
  key?: string;
453
492
  /**
@@ -310,6 +310,7 @@ async function parseComplexResponse({
310
310
  reader,
311
311
  abortControllerRef,
312
312
  update,
313
+ onToolCall,
313
314
  onFinish,
314
315
  generateId: generateId2 = generateId,
315
316
  getCurrentDate = () => /* @__PURE__ */ new Date()
@@ -350,6 +351,14 @@ async function parseComplexResponse({
350
351
  prefixMap.text.toolInvocations = [];
351
352
  }
352
353
  prefixMap.text.toolInvocations.push(value);
354
+ if (onToolCall) {
355
+ console.log("onToolCall", value);
356
+ const result = await onToolCall({ toolCall: value });
357
+ console.log("onToolCall result", result);
358
+ if (result != null) {
359
+ prefixMap.text.toolInvocations[prefixMap.text.toolInvocations.length - 1] = { ...value, result };
360
+ }
361
+ }
353
362
  } else if (type === "tool_result") {
354
363
  if (prefixMap.text == null) {
355
364
  prefixMap.text = {
@@ -474,6 +483,7 @@ async function callChatApi({
474
483
  onResponse,
475
484
  onUpdate,
476
485
  onFinish,
486
+ onToolCall,
477
487
  generateId: generateId2
478
488
  }) {
479
489
  var _a;
@@ -543,6 +553,7 @@ async function callChatApi({
543
553
  reader,
544
554
  abortControllerRef: abortController != null ? { current: abortController() } : void 0,
545
555
  update: onUpdate,
556
+ onToolCall,
546
557
  onFinish(prefixMap) {
547
558
  if (onFinish && prefixMap.text != null) {
548
559
  onFinish(prefixMap.text);
@@ -675,7 +686,7 @@ async function processChatStream({
675
686
  }
676
687
 
677
688
  // react/use-chat.ts
678
- var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, existingData, extraMetadataRef, messagesRef, abortControllerRef, generateId2, streamMode, onFinish, onResponse, sendExtraMessageFields) => {
689
+ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, existingData, extraMetadataRef, messagesRef, abortControllerRef, generateId2, streamMode, onFinish, onResponse, onToolCall, sendExtraMessageFields) => {
679
690
  var _a, _b;
680
691
  const previousMessages = messagesRef.current;
681
692
  mutate(chatRequest.messages, false);
@@ -767,6 +778,7 @@ var getStreamedResponse = async (api, chatRequest, mutate, mutateStreamData, exi
767
778
  mutate([...chatRequest.messages, ...merged], false);
768
779
  mutateStreamData([...existingData || [], ...data || []], false);
769
780
  },
781
+ onToolCall,
770
782
  onFinish,
771
783
  generateId: generateId2
772
784
  });
@@ -779,6 +791,7 @@ function useChat({
779
791
  sendExtraMessageFields,
780
792
  experimental_onFunctionCall,
781
793
  experimental_onToolCall,
794
+ onToolCall,
782
795
  experimental_maxAutomaticRoundtrips = 0,
783
796
  streamMode,
784
797
  onResponse,
@@ -842,6 +855,7 @@ function useChat({
842
855
  streamMode,
843
856
  onFinish,
844
857
  onResponse,
858
+ onToolCall,
845
859
  sendExtraMessageFields
846
860
  ),
847
861
  experimental_onFunctionCall,
@@ -891,6 +905,7 @@ function useChat({
891
905
  sendExtraMessageFields,
892
906
  experimental_onFunctionCall,
893
907
  experimental_onToolCall,
908
+ onToolCall,
894
909
  experimental_maxAutomaticRoundtrips,
895
910
  messagesRef,
896
911
  abortControllerRef,