ai 3.1.15 → 3.1.17

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/dist/index.d.mts CHANGED
@@ -39,6 +39,18 @@ Warning from the model provider for this call. The call will proceed, but e.g.
39
39
  some settings might not be supported, which can lead to suboptimal results.
40
40
  */
41
41
  type CallWarning = LanguageModelV1CallWarning;
42
+ /**
43
+ Tool choice for the generation. It supports the following settings:
44
+
45
+ - `auto` (default): the model can choose whether and which tools to call.
46
+ - `required`: the model must call a tool. It can choose which tool to call.
47
+ - `none`: the model must not call tools
48
+ - `{ type: 'tool', tooName: string (typed) }`: the model must call the specified tool
49
+ */
50
+ type CoreToolChoice<TOOLS extends Record<string, unknown>> = 'auto' | 'none' | 'required' | {
51
+ type: 'tool';
52
+ toolName: keyof TOOLS;
53
+ };
42
54
 
43
55
  /**
44
56
  Embed a value using an embedding model. The type of the value is defined by the embedding model.
@@ -820,7 +832,7 @@ If set and supported by the model, calls will generate deterministic results.
820
832
  @returns
821
833
  A result object that contains the generated text, the results of the tool calls, and additional information.
822
834
  */
823
- declare function generateText<TOOLS extends Record<string, CoreTool>>({ model, tools, system, prompt, messages, maxRetries, abortSignal, ...settings }: CallSettings & Prompt & {
835
+ declare function generateText<TOOLS extends Record<string, CoreTool>>({ model, tools, toolChoice, system, prompt, messages, maxRetries, abortSignal, ...settings }: CallSettings & Prompt & {
824
836
  /**
825
837
  The language model to use.
826
838
  */
@@ -829,6 +841,10 @@ The language model to use.
829
841
  The tools that the model can call. The model needs to support calling tools.
830
842
  */
831
843
  tools?: TOOLS;
844
+ /**
845
+ The tool choice strategy. Default: 'auto'.
846
+ */
847
+ toolChoice?: CoreToolChoice<TOOLS>;
832
848
  }): Promise<GenerateTextResult<TOOLS>>;
833
849
  /**
834
850
  The result of a `generateText` call.
@@ -928,7 +944,7 @@ If set and supported by the model, calls will generate deterministic results.
928
944
  @return
929
945
  A result object for accessing different stream types and additional information.
930
946
  */
931
- declare function streamText<TOOLS extends Record<string, CoreTool>>({ model, tools, system, prompt, messages, maxRetries, abortSignal, onFinish, ...settings }: CallSettings & Prompt & {
947
+ declare function streamText<TOOLS extends Record<string, CoreTool>>({ model, tools, toolChoice, system, prompt, messages, maxRetries, abortSignal, onFinish, ...settings }: CallSettings & Prompt & {
932
948
  /**
933
949
  The language model to use.
934
950
  */
@@ -938,6 +954,10 @@ The tools that the model can call. The model needs to support calling tools.
938
954
  */
939
955
  tools?: TOOLS;
940
956
  /**
957
+ The tool choice strategy. Default: 'auto'.
958
+ */
959
+ toolChoice?: CoreToolChoice<TOOLS>;
960
+ /**
941
961
  Callback that is called when the LLM response and all request tool executions
942
962
  (for tools that have an `execute` function) are finished.
943
963
  */
@@ -1165,6 +1185,9 @@ type UseAssistantOptions = {
1165
1185
  onError?: (error: Error) => void;
1166
1186
  };
1167
1187
 
1188
+ /**
1189
+ * @deprecated use AI SDK 3.1 CoreTool / ToolResult instead
1190
+ */
1168
1191
  interface FunctionCall$1 {
1169
1192
  /**
1170
1193
  * The arguments to call the function with, as generated by the model in JSON
@@ -1179,6 +1202,8 @@ interface FunctionCall$1 {
1179
1202
  name?: string;
1180
1203
  }
1181
1204
  /**
1205
+ * @deprecated use AI SDK 3.1 CoreTool / ToolResult instead
1206
+ *
1182
1207
  * The tool calls generated by the model, such as function calls.
1183
1208
  */
1184
1209
  interface ToolCall {
@@ -1190,6 +1215,8 @@ interface ToolCall {
1190
1215
  };
1191
1216
  }
1192
1217
  /**
1218
+ * @deprecated use AI SDK 3.1 CoreTool / ToolChoice instead
1219
+ *
1193
1220
  * Controls which (if any) function is called by the model.
1194
1221
  * - none means the model will not call a function and instead generates a message.
1195
1222
  * - auto means the model can pick between generating a message or calling a function.
@@ -1203,6 +1230,8 @@ type ToolChoice = 'none' | 'auto' | {
1203
1230
  };
1204
1231
  };
1205
1232
  /**
1233
+ * @deprecated use AI SDK 3.1 CoreTool instead
1234
+ *
1206
1235
  * A list of tools the model may call. Currently, only functions are supported as a tool.
1207
1236
  * Use this to provide a list of functions the model may generate JSON inputs for.
1208
1237
  */
@@ -1210,6 +1239,9 @@ interface Tool {
1210
1239
  type: 'function';
1211
1240
  function: Function;
1212
1241
  }
1242
+ /**
1243
+ * @deprecated use AI SDK 3.1 CoreTool instead
1244
+ */
1213
1245
  interface Function {
1214
1246
  /**
1215
1247
  * The name of the function to be called. Must be a-z, A-Z, 0-9, or contain
@@ -1240,17 +1272,20 @@ Once the call is complete, the invocation is a tool result.
1240
1272
  */
1241
1273
  type ToolInvocation = ToolCall$1<string, any> | ToolResult<string, any, any>;
1242
1274
  /**
1243
- * Shared types between the API and UI packages.
1275
+ * AI SDK UI Messages. They are used in the client and to communicate between the frontend and the API routes.
1244
1276
  */
1245
1277
  interface Message$1 {
1246
1278
  id: string;
1247
- tool_call_id?: string;
1248
1279
  createdAt?: Date;
1249
1280
  content: string;
1281
+ tool_call_id?: string;
1250
1282
  /**
1251
1283
  @deprecated Use AI SDK RSC instead: https://sdk.vercel.ai/docs/ai-sdk-rsc
1252
1284
  */
1253
1285
  ui?: string | JSX.Element | JSX.Element[] | null | undefined;
1286
+ /**
1287
+ * `function` and `tool` roles are deprecated.
1288
+ */
1254
1289
  role: 'system' | 'user' | 'assistant' | 'function' | 'data' | 'tool';
1255
1290
  /**
1256
1291
  *
@@ -1259,6 +1294,8 @@ interface Message$1 {
1259
1294
  */
1260
1295
  name?: string;
1261
1296
  /**
1297
+ * @deprecated Use AI SDK 3.1 `toolInvocations` instead.
1298
+ *
1262
1299
  * If the assistant role makes a function call, the `function_call` field
1263
1300
  * contains the function call name and arguments. Otherwise, the field should
1264
1301
  * not be set. (Deprecated and replaced by tool_calls.)
@@ -1266,6 +1303,8 @@ interface Message$1 {
1266
1303
  function_call?: string | FunctionCall$1;
1267
1304
  data?: JSONValue;
1268
1305
  /**
1306
+ * @deprecated Use AI SDK 3.1 `toolInvocations` instead.
1307
+ *
1269
1308
  * If the assistant role makes a tool call, the `tool_calls` field contains
1270
1309
  * the tool call name and arguments. Otherwise, the field should not be set.
1271
1310
  */
@@ -1292,7 +1331,13 @@ type ChatRequest = {
1292
1331
  tools?: Array<Tool>;
1293
1332
  tool_choice?: ToolChoice;
1294
1333
  };
1334
+ /**
1335
+ * @deprecated Use AI SDK 3.1 `streamText` and `onToolCall` instead.
1336
+ */
1295
1337
  type FunctionCallHandler = (chatMessages: Message$1[], functionCall: FunctionCall$1) => Promise<ChatRequest | void>;
1338
+ /**
1339
+ * @deprecated Use AI SDK 3.1 `streamText` and `onToolCall` instead.
1340
+ */
1296
1341
  type ToolCallHandler = (chatMessages: Message$1[], toolCalls: ToolCall[]) => Promise<ChatRequest | void>;
1297
1342
  type RequestOptions = {
1298
1343
  headers?: Record<string, string> | Headers;
@@ -1327,18 +1372,32 @@ type UseChatOptions = {
1327
1372
  */
1328
1373
  initialInput?: string;
1329
1374
  /**
1375
+ * @deprecated Use AI SDK 3.1 `streamText` and `onToolCall` instead.
1376
+ *
1330
1377
  * Callback function to be called when a function call is received.
1331
1378
  * If the function returns a `ChatRequest` object, the request will be sent
1332
1379
  * automatically to the API and will be used to update the chat.
1333
1380
  */
1334
1381
  experimental_onFunctionCall?: FunctionCallHandler;
1335
1382
  /**
1383
+ * @deprecated Use AI SDK 3.1 `streamText` and `onToolCall` instead.
1384
+ *
1336
1385
  * Callback function to be called when a tool call is received.
1337
1386
  * If the function returns a `ChatRequest` object, the request will be sent
1338
1387
  * automatically to the API and will be used to update the chat.
1339
1388
  */
1340
1389
  experimental_onToolCall?: ToolCallHandler;
1341
1390
  /**
1391
+ Optional callback function that is invoked when a tool call is received.
1392
+ Intended for automatic client-side tool execution.
1393
+
1394
+ You can optionally return a result for the tool call,
1395
+ either synchronously or asynchronously.
1396
+ */
1397
+ onToolCall?: ({ toolCall, }: {
1398
+ toolCall: ToolCall$1<string, unknown>;
1399
+ }) => void | Promise<unknown> | unknown;
1400
+ /**
1342
1401
  * Callback function to be called when the API response is received.
1343
1402
  */
1344
1403
  onResponse?: (response: Response) => void | Promise<void>;
@@ -2324,4 +2383,4 @@ declare class StreamingTextResponse extends Response {
2324
2383
  constructor(res: ReadableStream, init?: ResponseInit, data?: StreamData);
2325
2384
  }
2326
2385
 
2327
- export { AIStream, AIStreamCallbacksAndOptions, AIStreamParser, AIStreamParserOptions, AWSBedrockAnthropicMessagesStream, AWSBedrockAnthropicStream, AWSBedrockCohereStream, AWSBedrockLlama2Stream, AWSBedrockStream, AnthropicStream, AssistantContent, AssistantMessage, AssistantResponse, AssistantStatus, CallWarning, ChatRequest, ChatRequestOptions, CohereStream, CompletionUsage, CoreAssistantMessage, CoreMessage, CoreSystemMessage, CoreTool, CoreToolMessage, CoreUserMessage, CreateMessage, DataContent, DataMessage, DeepPartial, EmbedManyResult, EmbedResult, Embedding, EmbeddingModel, ExperimentalAssistantMessage, ExperimentalMessage, ExperimentalTool, ExperimentalToolMessage, ExperimentalUserMessage, FinishReason, Function, FunctionCall$1 as FunctionCall, FunctionCallHandler, FunctionCallPayload, GenerateObjectResult, GenerateTextResult, GoogleGenerativeAIStream, HuggingFaceStream, IdGenerator, ImagePart, InkeepAIStreamCallbacksAndOptions, InkeepChatResultCallbacks, InkeepOnFinalMetadata, InkeepStream, JSONValue, langchainAdapter as LangChainAdapter, LangChainStream, LanguageModel, LogProbs, Message$1 as Message, MistralStream, ObjectStreamPart, ObjectStreamPartInput, OpenAIStream, OpenAIStreamCallbacks, ReactResponseRow, ReplicateStream, RequestOptions, StreamData, StreamObjectResult, StreamPart, StreamString, StreamTextResult, StreamingTextResponse, TextPart$1 as TextPart, TextStreamPart, Tool, ToolCall, ToolCallHandler, ToolCallPart, ToolCallPayload, ToolChoice, ToolContent, ToolInvocation, ToolResultPart, UseAssistantOptions, UseChatOptions, UseCompletionOptions, UserContent, convertDataContentToBase64String, convertDataContentToUint8Array, convertToCoreMessages, createCallbacksTransformer, createChunkDecoder, createEventStreamTransformer, createStreamDataTransformer, embed, embedMany, experimental_AssistantResponse, experimental_StreamData, experimental_StreamingReactResponse, experimental_generateObject, experimental_generateText, experimental_streamObject, experimental_streamText, formatStreamPart, generateId, generateObject, generateText, isStreamStringEqualToType, generateId as nanoid, parseStreamPart, readDataStream, readableFromAsyncIterable, streamObject, streamText, streamToResponse, tool, trimStartOfStreamHelper };
2386
+ export { AIStream, AIStreamCallbacksAndOptions, AIStreamParser, AIStreamParserOptions, AWSBedrockAnthropicMessagesStream, AWSBedrockAnthropicStream, AWSBedrockCohereStream, AWSBedrockLlama2Stream, AWSBedrockStream, AnthropicStream, AssistantContent, AssistantMessage, AssistantResponse, AssistantStatus, CallWarning, ChatRequest, ChatRequestOptions, CohereStream, CompletionUsage, CoreAssistantMessage, CoreMessage, CoreSystemMessage, CoreTool, CoreToolChoice, CoreToolMessage, CoreUserMessage, CreateMessage, DataContent, DataMessage, DeepPartial, EmbedManyResult, EmbedResult, Embedding, EmbeddingModel, ExperimentalAssistantMessage, ExperimentalMessage, ExperimentalTool, ExperimentalToolMessage, ExperimentalUserMessage, FinishReason, Function, FunctionCall$1 as FunctionCall, FunctionCallHandler, FunctionCallPayload, GenerateObjectResult, GenerateTextResult, GoogleGenerativeAIStream, HuggingFaceStream, IdGenerator, ImagePart, InkeepAIStreamCallbacksAndOptions, InkeepChatResultCallbacks, InkeepOnFinalMetadata, InkeepStream, JSONValue, langchainAdapter as LangChainAdapter, LangChainStream, LanguageModel, LogProbs, Message$1 as Message, MistralStream, ObjectStreamPart, ObjectStreamPartInput, OpenAIStream, OpenAIStreamCallbacks, ReactResponseRow, ReplicateStream, RequestOptions, StreamData, StreamObjectResult, StreamPart, StreamString, StreamTextResult, StreamingTextResponse, TextPart$1 as TextPart, TextStreamPart, Tool, ToolCall, ToolCallHandler, ToolCallPart, ToolCallPayload, ToolChoice, ToolContent, ToolInvocation, ToolResultPart, UseAssistantOptions, UseChatOptions, UseCompletionOptions, UserContent, convertDataContentToBase64String, convertDataContentToUint8Array, convertToCoreMessages, createCallbacksTransformer, createChunkDecoder, createEventStreamTransformer, createStreamDataTransformer, embed, embedMany, experimental_AssistantResponse, experimental_StreamData, experimental_StreamingReactResponse, experimental_generateObject, experimental_generateText, experimental_streamObject, experimental_streamText, formatStreamPart, generateId, generateObject, generateText, isStreamStringEqualToType, generateId as nanoid, parseStreamPart, readDataStream, readableFromAsyncIterable, streamObject, streamText, streamToResponse, tool, trimStartOfStreamHelper };
package/dist/index.d.ts CHANGED
@@ -39,6 +39,18 @@ Warning from the model provider for this call. The call will proceed, but e.g.
39
39
  some settings might not be supported, which can lead to suboptimal results.
40
40
  */
41
41
  type CallWarning = LanguageModelV1CallWarning;
42
+ /**
43
+ Tool choice for the generation. It supports the following settings:
44
+
45
+ - `auto` (default): the model can choose whether and which tools to call.
46
+ - `required`: the model must call a tool. It can choose which tool to call.
47
+ - `none`: the model must not call tools
48
+ - `{ type: 'tool', tooName: string (typed) }`: the model must call the specified tool
49
+ */
50
+ type CoreToolChoice<TOOLS extends Record<string, unknown>> = 'auto' | 'none' | 'required' | {
51
+ type: 'tool';
52
+ toolName: keyof TOOLS;
53
+ };
42
54
 
43
55
  /**
44
56
  Embed a value using an embedding model. The type of the value is defined by the embedding model.
@@ -820,7 +832,7 @@ If set and supported by the model, calls will generate deterministic results.
820
832
  @returns
821
833
  A result object that contains the generated text, the results of the tool calls, and additional information.
822
834
  */
823
- declare function generateText<TOOLS extends Record<string, CoreTool>>({ model, tools, system, prompt, messages, maxRetries, abortSignal, ...settings }: CallSettings & Prompt & {
835
+ declare function generateText<TOOLS extends Record<string, CoreTool>>({ model, tools, toolChoice, system, prompt, messages, maxRetries, abortSignal, ...settings }: CallSettings & Prompt & {
824
836
  /**
825
837
  The language model to use.
826
838
  */
@@ -829,6 +841,10 @@ The language model to use.
829
841
  The tools that the model can call. The model needs to support calling tools.
830
842
  */
831
843
  tools?: TOOLS;
844
+ /**
845
+ The tool choice strategy. Default: 'auto'.
846
+ */
847
+ toolChoice?: CoreToolChoice<TOOLS>;
832
848
  }): Promise<GenerateTextResult<TOOLS>>;
833
849
  /**
834
850
  The result of a `generateText` call.
@@ -928,7 +944,7 @@ If set and supported by the model, calls will generate deterministic results.
928
944
  @return
929
945
  A result object for accessing different stream types and additional information.
930
946
  */
931
- declare function streamText<TOOLS extends Record<string, CoreTool>>({ model, tools, system, prompt, messages, maxRetries, abortSignal, onFinish, ...settings }: CallSettings & Prompt & {
947
+ declare function streamText<TOOLS extends Record<string, CoreTool>>({ model, tools, toolChoice, system, prompt, messages, maxRetries, abortSignal, onFinish, ...settings }: CallSettings & Prompt & {
932
948
  /**
933
949
  The language model to use.
934
950
  */
@@ -938,6 +954,10 @@ The tools that the model can call. The model needs to support calling tools.
938
954
  */
939
955
  tools?: TOOLS;
940
956
  /**
957
+ The tool choice strategy. Default: 'auto'.
958
+ */
959
+ toolChoice?: CoreToolChoice<TOOLS>;
960
+ /**
941
961
  Callback that is called when the LLM response and all request tool executions
942
962
  (for tools that have an `execute` function) are finished.
943
963
  */
@@ -1165,6 +1185,9 @@ type UseAssistantOptions = {
1165
1185
  onError?: (error: Error) => void;
1166
1186
  };
1167
1187
 
1188
+ /**
1189
+ * @deprecated use AI SDK 3.1 CoreTool / ToolResult instead
1190
+ */
1168
1191
  interface FunctionCall$1 {
1169
1192
  /**
1170
1193
  * The arguments to call the function with, as generated by the model in JSON
@@ -1179,6 +1202,8 @@ interface FunctionCall$1 {
1179
1202
  name?: string;
1180
1203
  }
1181
1204
  /**
1205
+ * @deprecated use AI SDK 3.1 CoreTool / ToolResult instead
1206
+ *
1182
1207
  * The tool calls generated by the model, such as function calls.
1183
1208
  */
1184
1209
  interface ToolCall {
@@ -1190,6 +1215,8 @@ interface ToolCall {
1190
1215
  };
1191
1216
  }
1192
1217
  /**
1218
+ * @deprecated use AI SDK 3.1 CoreTool / ToolChoice instead
1219
+ *
1193
1220
  * Controls which (if any) function is called by the model.
1194
1221
  * - none means the model will not call a function and instead generates a message.
1195
1222
  * - auto means the model can pick between generating a message or calling a function.
@@ -1203,6 +1230,8 @@ type ToolChoice = 'none' | 'auto' | {
1203
1230
  };
1204
1231
  };
1205
1232
  /**
1233
+ * @deprecated use AI SDK 3.1 CoreTool instead
1234
+ *
1206
1235
  * A list of tools the model may call. Currently, only functions are supported as a tool.
1207
1236
  * Use this to provide a list of functions the model may generate JSON inputs for.
1208
1237
  */
@@ -1210,6 +1239,9 @@ interface Tool {
1210
1239
  type: 'function';
1211
1240
  function: Function;
1212
1241
  }
1242
+ /**
1243
+ * @deprecated use AI SDK 3.1 CoreTool instead
1244
+ */
1213
1245
  interface Function {
1214
1246
  /**
1215
1247
  * The name of the function to be called. Must be a-z, A-Z, 0-9, or contain
@@ -1240,17 +1272,20 @@ Once the call is complete, the invocation is a tool result.
1240
1272
  */
1241
1273
  type ToolInvocation = ToolCall$1<string, any> | ToolResult<string, any, any>;
1242
1274
  /**
1243
- * Shared types between the API and UI packages.
1275
+ * AI SDK UI Messages. They are used in the client and to communicate between the frontend and the API routes.
1244
1276
  */
1245
1277
  interface Message$1 {
1246
1278
  id: string;
1247
- tool_call_id?: string;
1248
1279
  createdAt?: Date;
1249
1280
  content: string;
1281
+ tool_call_id?: string;
1250
1282
  /**
1251
1283
  @deprecated Use AI SDK RSC instead: https://sdk.vercel.ai/docs/ai-sdk-rsc
1252
1284
  */
1253
1285
  ui?: string | JSX.Element | JSX.Element[] | null | undefined;
1286
+ /**
1287
+ * `function` and `tool` roles are deprecated.
1288
+ */
1254
1289
  role: 'system' | 'user' | 'assistant' | 'function' | 'data' | 'tool';
1255
1290
  /**
1256
1291
  *
@@ -1259,6 +1294,8 @@ interface Message$1 {
1259
1294
  */
1260
1295
  name?: string;
1261
1296
  /**
1297
+ * @deprecated Use AI SDK 3.1 `toolInvocations` instead.
1298
+ *
1262
1299
  * If the assistant role makes a function call, the `function_call` field
1263
1300
  * contains the function call name and arguments. Otherwise, the field should
1264
1301
  * not be set. (Deprecated and replaced by tool_calls.)
@@ -1266,6 +1303,8 @@ interface Message$1 {
1266
1303
  function_call?: string | FunctionCall$1;
1267
1304
  data?: JSONValue;
1268
1305
  /**
1306
+ * @deprecated Use AI SDK 3.1 `toolInvocations` instead.
1307
+ *
1269
1308
  * If the assistant role makes a tool call, the `tool_calls` field contains
1270
1309
  * the tool call name and arguments. Otherwise, the field should not be set.
1271
1310
  */
@@ -1292,7 +1331,13 @@ type ChatRequest = {
1292
1331
  tools?: Array<Tool>;
1293
1332
  tool_choice?: ToolChoice;
1294
1333
  };
1334
+ /**
1335
+ * @deprecated Use AI SDK 3.1 `streamText` and `onToolCall` instead.
1336
+ */
1295
1337
  type FunctionCallHandler = (chatMessages: Message$1[], functionCall: FunctionCall$1) => Promise<ChatRequest | void>;
1338
+ /**
1339
+ * @deprecated Use AI SDK 3.1 `streamText` and `onToolCall` instead.
1340
+ */
1296
1341
  type ToolCallHandler = (chatMessages: Message$1[], toolCalls: ToolCall[]) => Promise<ChatRequest | void>;
1297
1342
  type RequestOptions = {
1298
1343
  headers?: Record<string, string> | Headers;
@@ -1327,18 +1372,32 @@ type UseChatOptions = {
1327
1372
  */
1328
1373
  initialInput?: string;
1329
1374
  /**
1375
+ * @deprecated Use AI SDK 3.1 `streamText` and `onToolCall` instead.
1376
+ *
1330
1377
  * Callback function to be called when a function call is received.
1331
1378
  * If the function returns a `ChatRequest` object, the request will be sent
1332
1379
  * automatically to the API and will be used to update the chat.
1333
1380
  */
1334
1381
  experimental_onFunctionCall?: FunctionCallHandler;
1335
1382
  /**
1383
+ * @deprecated Use AI SDK 3.1 `streamText` and `onToolCall` instead.
1384
+ *
1336
1385
  * Callback function to be called when a tool call is received.
1337
1386
  * If the function returns a `ChatRequest` object, the request will be sent
1338
1387
  * automatically to the API and will be used to update the chat.
1339
1388
  */
1340
1389
  experimental_onToolCall?: ToolCallHandler;
1341
1390
  /**
1391
+ Optional callback function that is invoked when a tool call is received.
1392
+ Intended for automatic client-side tool execution.
1393
+
1394
+ You can optionally return a result for the tool call,
1395
+ either synchronously or asynchronously.
1396
+ */
1397
+ onToolCall?: ({ toolCall, }: {
1398
+ toolCall: ToolCall$1<string, unknown>;
1399
+ }) => void | Promise<unknown> | unknown;
1400
+ /**
1342
1401
  * Callback function to be called when the API response is received.
1343
1402
  */
1344
1403
  onResponse?: (response: Response) => void | Promise<void>;
@@ -2324,4 +2383,4 @@ declare class StreamingTextResponse extends Response {
2324
2383
  constructor(res: ReadableStream, init?: ResponseInit, data?: StreamData);
2325
2384
  }
2326
2385
 
2327
- export { AIStream, AIStreamCallbacksAndOptions, AIStreamParser, AIStreamParserOptions, AWSBedrockAnthropicMessagesStream, AWSBedrockAnthropicStream, AWSBedrockCohereStream, AWSBedrockLlama2Stream, AWSBedrockStream, AnthropicStream, AssistantContent, AssistantMessage, AssistantResponse, AssistantStatus, CallWarning, ChatRequest, ChatRequestOptions, CohereStream, CompletionUsage, CoreAssistantMessage, CoreMessage, CoreSystemMessage, CoreTool, CoreToolMessage, CoreUserMessage, CreateMessage, DataContent, DataMessage, DeepPartial, EmbedManyResult, EmbedResult, Embedding, EmbeddingModel, ExperimentalAssistantMessage, ExperimentalMessage, ExperimentalTool, ExperimentalToolMessage, ExperimentalUserMessage, FinishReason, Function, FunctionCall$1 as FunctionCall, FunctionCallHandler, FunctionCallPayload, GenerateObjectResult, GenerateTextResult, GoogleGenerativeAIStream, HuggingFaceStream, IdGenerator, ImagePart, InkeepAIStreamCallbacksAndOptions, InkeepChatResultCallbacks, InkeepOnFinalMetadata, InkeepStream, JSONValue, langchainAdapter as LangChainAdapter, LangChainStream, LanguageModel, LogProbs, Message$1 as Message, MistralStream, ObjectStreamPart, ObjectStreamPartInput, OpenAIStream, OpenAIStreamCallbacks, ReactResponseRow, ReplicateStream, RequestOptions, StreamData, StreamObjectResult, StreamPart, StreamString, StreamTextResult, StreamingTextResponse, TextPart$1 as TextPart, TextStreamPart, Tool, ToolCall, ToolCallHandler, ToolCallPart, ToolCallPayload, ToolChoice, ToolContent, ToolInvocation, ToolResultPart, UseAssistantOptions, UseChatOptions, UseCompletionOptions, UserContent, convertDataContentToBase64String, convertDataContentToUint8Array, convertToCoreMessages, createCallbacksTransformer, createChunkDecoder, createEventStreamTransformer, createStreamDataTransformer, embed, embedMany, experimental_AssistantResponse, experimental_StreamData, experimental_StreamingReactResponse, experimental_generateObject, experimental_generateText, experimental_streamObject, experimental_streamText, formatStreamPart, generateId, generateObject, generateText, isStreamStringEqualToType, generateId as nanoid, parseStreamPart, readDataStream, readableFromAsyncIterable, streamObject, streamText, streamToResponse, tool, trimStartOfStreamHelper };
2386
+ export { AIStream, AIStreamCallbacksAndOptions, AIStreamParser, AIStreamParserOptions, AWSBedrockAnthropicMessagesStream, AWSBedrockAnthropicStream, AWSBedrockCohereStream, AWSBedrockLlama2Stream, AWSBedrockStream, AnthropicStream, AssistantContent, AssistantMessage, AssistantResponse, AssistantStatus, CallWarning, ChatRequest, ChatRequestOptions, CohereStream, CompletionUsage, CoreAssistantMessage, CoreMessage, CoreSystemMessage, CoreTool, CoreToolChoice, CoreToolMessage, CoreUserMessage, CreateMessage, DataContent, DataMessage, DeepPartial, EmbedManyResult, EmbedResult, Embedding, EmbeddingModel, ExperimentalAssistantMessage, ExperimentalMessage, ExperimentalTool, ExperimentalToolMessage, ExperimentalUserMessage, FinishReason, Function, FunctionCall$1 as FunctionCall, FunctionCallHandler, FunctionCallPayload, GenerateObjectResult, GenerateTextResult, GoogleGenerativeAIStream, HuggingFaceStream, IdGenerator, ImagePart, InkeepAIStreamCallbacksAndOptions, InkeepChatResultCallbacks, InkeepOnFinalMetadata, InkeepStream, JSONValue, langchainAdapter as LangChainAdapter, LangChainStream, LanguageModel, LogProbs, Message$1 as Message, MistralStream, ObjectStreamPart, ObjectStreamPartInput, OpenAIStream, OpenAIStreamCallbacks, ReactResponseRow, ReplicateStream, RequestOptions, StreamData, StreamObjectResult, StreamPart, StreamString, StreamTextResult, StreamingTextResponse, TextPart$1 as TextPart, TextStreamPart, Tool, ToolCall, ToolCallHandler, ToolCallPart, ToolCallPayload, ToolChoice, ToolContent, ToolInvocation, ToolResultPart, UseAssistantOptions, UseChatOptions, UseCompletionOptions, UserContent, convertDataContentToBase64String, convertDataContentToUint8Array, convertToCoreMessages, createCallbacksTransformer, createChunkDecoder, createEventStreamTransformer, createStreamDataTransformer, embed, embedMany, experimental_AssistantResponse, experimental_StreamData, experimental_StreamingReactResponse, experimental_generateObject, experimental_generateText, experimental_streamObject, experimental_streamText, formatStreamPart, generateId, generateObject, generateText, isStreamStringEqualToType, generateId as nanoid, parseStreamPart, readDataStream, readableFromAsyncIterable, streamObject, streamText, streamToResponse, tool, trimStartOfStreamHelper };
package/dist/index.js CHANGED
@@ -1265,6 +1265,33 @@ var StreamObjectResult = class {
1265
1265
  };
1266
1266
  var experimental_streamObject = streamObject;
1267
1267
 
1268
+ // core/util/is-non-empty-object.ts
1269
+ function isNonEmptyObject(object) {
1270
+ return object != null && Object.keys(object).length > 0;
1271
+ }
1272
+
1273
+ // core/prompt/prepare-tools-and-tool-choice.ts
1274
+ function prepareToolsAndToolChoice({
1275
+ tools,
1276
+ toolChoice
1277
+ }) {
1278
+ if (!isNonEmptyObject(tools)) {
1279
+ return {
1280
+ tools: void 0,
1281
+ toolChoice: void 0
1282
+ };
1283
+ }
1284
+ return {
1285
+ tools: Object.entries(tools).map(([name, tool2]) => ({
1286
+ type: "function",
1287
+ name,
1288
+ description: tool2.description,
1289
+ parameters: convertZodToJSONSchema(tool2.parameters)
1290
+ })),
1291
+ toolChoice: toolChoice == null ? { type: "auto" } : typeof toolChoice === "string" ? { type: toolChoice } : { type: "tool", toolName: toolChoice.toolName }
1292
+ };
1293
+ }
1294
+
1268
1295
  // core/generate-text/tool-call.ts
1269
1296
  var import_provider6 = require("@ai-sdk/provider");
1270
1297
  var import_provider_utils4 = require("@ai-sdk/provider-utils");
@@ -1306,6 +1333,7 @@ function parseToolCall({
1306
1333
  async function generateText({
1307
1334
  model,
1308
1335
  tools,
1336
+ toolChoice,
1309
1337
  system,
1310
1338
  prompt,
1311
1339
  messages,
@@ -1320,12 +1348,7 @@ async function generateText({
1320
1348
  return model.doGenerate({
1321
1349
  mode: {
1322
1350
  type: "regular",
1323
- tools: tools == null ? void 0 : Object.entries(tools).map(([name, tool2]) => ({
1324
- type: "function",
1325
- name,
1326
- description: tool2.description,
1327
- parameters: convertZodToJSONSchema(tool2.parameters)
1328
- }))
1351
+ ...prepareToolsAndToolChoice({ tools, toolChoice })
1329
1352
  },
1330
1353
  ...prepareCallSettings(settings),
1331
1354
  inputFormat: validatedPrompt.type,
@@ -1538,6 +1561,7 @@ function runToolsTransformation({
1538
1561
  async function streamText({
1539
1562
  model,
1540
1563
  tools,
1564
+ toolChoice,
1541
1565
  system,
1542
1566
  prompt,
1543
1567
  messages,
@@ -1552,12 +1576,7 @@ async function streamText({
1552
1576
  () => model.doStream({
1553
1577
  mode: {
1554
1578
  type: "regular",
1555
- tools: tools == null ? void 0 : Object.entries(tools).map(([name, tool2]) => ({
1556
- type: "function",
1557
- name,
1558
- description: tool2.description,
1559
- parameters: convertZodToJSONSchema(tool2.parameters)
1560
- }))
1579
+ ...prepareToolsAndToolChoice({ tools, toolChoice })
1561
1580
  },
1562
1581
  ...prepareCallSettings(settings),
1563
1582
  inputFormat: validatedPrompt.type,
@@ -3285,6 +3304,7 @@ async function parseComplexResponse({
3285
3304
  reader,
3286
3305
  abortControllerRef,
3287
3306
  update,
3307
+ onToolCall,
3288
3308
  onFinish,
3289
3309
  generateId: generateId2 = generateId,
3290
3310
  getCurrentDate = () => /* @__PURE__ */ new Date()
@@ -3325,6 +3345,14 @@ async function parseComplexResponse({
3325
3345
  prefixMap.text.toolInvocations = [];
3326
3346
  }
3327
3347
  prefixMap.text.toolInvocations.push(value);
3348
+ if (onToolCall) {
3349
+ console.log("onToolCall", value);
3350
+ const result = await onToolCall({ toolCall: value });
3351
+ console.log("onToolCall result", result);
3352
+ if (result != null) {
3353
+ prefixMap.text.toolInvocations[prefixMap.text.toolInvocations.length - 1] = { ...value, result };
3354
+ }
3355
+ }
3328
3356
  } else if (type === "tool_result") {
3329
3357
  if (prefixMap.text == null) {
3330
3358
  prefixMap.text = {