@ai-sdk/provider-utils 4.0.0-beta.38 → 4.0.0-beta.39

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 CHANGED
@@ -1,5 +1,13 @@
1
1
  # @ai-sdk/provider-utils
2
2
 
3
+ ## 4.0.0-beta.39
4
+
5
+ ### Patch Changes
6
+
7
+ - 954c356: feat(openai): allow custom names for provider-defined tools
8
+ - Updated dependencies [954c356]
9
+ - @ai-sdk/provider@3.0.0-beta.21
10
+
3
11
  ## 4.0.0-beta.38
4
12
 
5
13
  ### Patch Changes
package/dist/index.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { JSONSchema7, JSONParseError, TypeValidationError, JSONValue, APICallError, LanguageModelV3Prompt, SharedV3ProviderOptions } from '@ai-sdk/provider';
1
+ import { LanguageModelV3FunctionTool, LanguageModelV3ProviderDefinedTool, JSONSchema7, JSONParseError, TypeValidationError, JSONValue, APICallError, LanguageModelV3Prompt, SharedV3ProviderOptions } from '@ai-sdk/provider';
2
2
  import { StandardSchemaV1 } from '@standard-schema/spec';
3
3
  export * from '@standard-schema/spec';
4
4
  import * as z3 from 'zod/v3';
@@ -16,6 +16,42 @@ declare function combineHeaders(...headers: Array<Record<string, string | undefi
16
16
  */
17
17
  declare function convertAsyncIteratorToReadableStream<T>(iterator: AsyncIterator<T>): ReadableStream<T>;
18
18
 
19
+ /**
20
+ * Interface for mapping between custom tool names and provider tool names.
21
+ */
22
+ interface ToolNameMapping {
23
+ /**
24
+ * Maps a custom tool name (used by the client) to the provider's tool name.
25
+ * If the custom tool name does not have a mapping, returns the input name.
26
+ *
27
+ * @param customToolName - The custom name of the tool defined by the client.
28
+ * @returns The corresponding provider tool name, or the input name if not mapped.
29
+ */
30
+ toProviderToolName: (customToolName: string) => string;
31
+ /**
32
+ * Maps a provider tool name to the custom tool name used by the client.
33
+ * If the provider tool name does not have a mapping, returns the input name.
34
+ *
35
+ * @param providerToolName - The name of the tool as understood by the provider.
36
+ * @returns The corresponding custom tool name, or the input name if not mapped.
37
+ */
38
+ toCustomToolName: (providerToolName: string) => string;
39
+ }
40
+ /**
41
+ * @param tools - Tools that were passed to the language model.
42
+ * @param providerToolNames - Maps the provider tool ids to the provider tool names.
43
+ */
44
+ declare function createToolNameMapping({ tools, providerToolNames, }: {
45
+ /**
46
+ * Tools that were passed to the language model.
47
+ */
48
+ tools: Array<LanguageModelV3FunctionTool | LanguageModelV3ProviderDefinedTool> | undefined;
49
+ /**
50
+ * Maps the provider tool ids to the provider tool names.
51
+ */
52
+ providerToolNames: Record<`${string}.${string}`, string>;
53
+ }): ToolNameMapping;
54
+
19
55
  /**
20
56
  * Creates a Promise that resolves after a specified delay
21
57
  * @param delayInMs - The delay duration in milliseconds. If null or undefined, resolves immediately.
@@ -960,14 +996,10 @@ Tool with provider-defined input and output schemas.
960
996
  */
961
997
  type: 'provider-defined';
962
998
  /**
963
- The ID of the tool. Should follow the format `<provider-name>.<unique-tool-name>`.
999
+ The ID of the tool. Must follow the format `<provider-name>.<unique-tool-name>`.
964
1000
  */
965
1001
  id: `${string}.${string}`;
966
1002
  /**
967
- The name of the tool that the user must use in the tool set.
968
- */
969
- name: string;
970
- /**
971
1003
  The arguments for configuring the tool. Must match the expected arguments defined by the provider for this tool.
972
1004
  */
973
1005
  args: Record<string, unknown>;
@@ -1013,9 +1045,8 @@ type ProviderDefinedToolFactory<INPUT, ARGS extends object> = <OUTPUT>(options:
1013
1045
  onInputDelta?: Tool<INPUT, OUTPUT>['onInputDelta'];
1014
1046
  onInputAvailable?: Tool<INPUT, OUTPUT>['onInputAvailable'];
1015
1047
  }) => Tool<INPUT, OUTPUT>;
1016
- declare function createProviderDefinedToolFactory<INPUT, ARGS extends object>({ id, name, inputSchema, }: {
1048
+ declare function createProviderDefinedToolFactory<INPUT, ARGS extends object>({ id, inputSchema, }: {
1017
1049
  id: `${string}.${string}`;
1018
- name: string;
1019
1050
  inputSchema: FlexibleSchema<INPUT>;
1020
1051
  }): ProviderDefinedToolFactory<INPUT, ARGS>;
1021
1052
  type ProviderDefinedToolFactoryWithOutputSchema<INPUT, OUTPUT, ARGS extends object> = (options: ARGS & {
@@ -1026,9 +1057,8 @@ type ProviderDefinedToolFactoryWithOutputSchema<INPUT, OUTPUT, ARGS extends obje
1026
1057
  onInputDelta?: Tool<INPUT, OUTPUT>['onInputDelta'];
1027
1058
  onInputAvailable?: Tool<INPUT, OUTPUT>['onInputAvailable'];
1028
1059
  }) => Tool<INPUT, OUTPUT>;
1029
- declare function createProviderDefinedToolFactoryWithOutputSchema<INPUT, OUTPUT, ARGS extends object>({ id, name, inputSchema, outputSchema, }: {
1060
+ declare function createProviderDefinedToolFactoryWithOutputSchema<INPUT, OUTPUT, ARGS extends object>({ id, inputSchema, outputSchema, }: {
1030
1061
  id: `${string}.${string}`;
1031
- name: string;
1032
1062
  inputSchema: FlexibleSchema<INPUT>;
1033
1063
  outputSchema: FlexibleSchema<OUTPUT>;
1034
1064
  }): ProviderDefinedToolFactoryWithOutputSchema<INPUT, OUTPUT, ARGS>;
@@ -1173,4 +1203,4 @@ interface ToolResult<NAME extends string, INPUT, OUTPUT> {
1173
1203
  dynamic?: boolean;
1174
1204
  }
1175
1205
 
1176
- export { type AssistantContent, type AssistantModelMessage, type DataContent, DelayedPromise, type FetchFunction, type FilePart, type FlexibleSchema, type IdGenerator, type ImagePart, type InferSchema, type InferToolInput, type InferToolOutput, type LazySchema, type MaybePromiseLike, type ModelMessage, type ParseResult, type ProviderDefinedToolFactory, type ProviderDefinedToolFactoryWithOutputSchema, type ProviderOptions, type ReasoningPart, type Resolvable, type ResponseHandler, type Schema, type SystemModelMessage, type TextPart, type Tool, type ToolApprovalRequest, type ToolApprovalResponse, type ToolCall, type ToolCallOptions, type ToolCallPart, type ToolContent, type ToolExecuteFunction, type ToolModelMessage, type ToolNeedsApprovalFunction, type ToolResult, type ToolResultOutput, type ToolResultPart, type UserContent, type UserModelMessage, VERSION, type ValidationResult, asSchema, combineHeaders, convertAsyncIteratorToReadableStream, convertBase64ToUint8Array, convertToBase64, convertUint8ArrayToBase64, createBinaryResponseHandler, createEventSourceResponseHandler, createIdGenerator, createJsonErrorResponseHandler, createJsonResponseHandler, createProviderDefinedToolFactory, createProviderDefinedToolFactoryWithOutputSchema, createStatusCodeErrorResponseHandler, delay, dynamicTool, executeTool, extractResponseHeaders, generateId, getErrorMessage, getFromApi, getRuntimeEnvironmentUserAgent, injectJsonInstructionIntoMessages, isAbortError, isNonNullable, isParsableJson, isUrlSupported, jsonSchema, lazySchema, loadApiKey, loadOptionalSetting, loadSetting, mediaTypeToExtension, normalizeHeaders, parseJSON, parseJsonEventStream, parseProviderOptions, postFormDataToApi, postJsonToApi, postToApi, removeUndefinedEntries, resolve, safeParseJSON, safeValidateTypes, tool, validateTypes, withUserAgentSuffix, withoutTrailingSlash, zodSchema };
1206
+ export { type AssistantContent, type AssistantModelMessage, type DataContent, DelayedPromise, type FetchFunction, type FilePart, type FlexibleSchema, type IdGenerator, type ImagePart, type InferSchema, type InferToolInput, type InferToolOutput, type LazySchema, type MaybePromiseLike, type ModelMessage, type ParseResult, type ProviderDefinedToolFactory, type ProviderDefinedToolFactoryWithOutputSchema, type ProviderOptions, type ReasoningPart, type Resolvable, type ResponseHandler, type Schema, type SystemModelMessage, type TextPart, type Tool, type ToolApprovalRequest, type ToolApprovalResponse, type ToolCall, type ToolCallOptions, type ToolCallPart, type ToolContent, type ToolExecuteFunction, type ToolModelMessage, type ToolNameMapping, type ToolNeedsApprovalFunction, type ToolResult, type ToolResultOutput, type ToolResultPart, type UserContent, type UserModelMessage, VERSION, type ValidationResult, asSchema, combineHeaders, convertAsyncIteratorToReadableStream, convertBase64ToUint8Array, convertToBase64, convertUint8ArrayToBase64, createBinaryResponseHandler, createEventSourceResponseHandler, createIdGenerator, createJsonErrorResponseHandler, createJsonResponseHandler, createProviderDefinedToolFactory, createProviderDefinedToolFactoryWithOutputSchema, createStatusCodeErrorResponseHandler, createToolNameMapping, delay, dynamicTool, executeTool, extractResponseHeaders, generateId, getErrorMessage, getFromApi, getRuntimeEnvironmentUserAgent, injectJsonInstructionIntoMessages, isAbortError, isNonNullable, isParsableJson, isUrlSupported, jsonSchema, lazySchema, loadApiKey, loadOptionalSetting, loadSetting, mediaTypeToExtension, normalizeHeaders, parseJSON, parseJsonEventStream, parseProviderOptions, postFormDataToApi, postJsonToApi, postToApi, removeUndefinedEntries, resolve, safeParseJSON, safeValidateTypes, tool, validateTypes, withUserAgentSuffix, withoutTrailingSlash, zodSchema };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { JSONSchema7, JSONParseError, TypeValidationError, JSONValue, APICallError, LanguageModelV3Prompt, SharedV3ProviderOptions } from '@ai-sdk/provider';
1
+ import { LanguageModelV3FunctionTool, LanguageModelV3ProviderDefinedTool, JSONSchema7, JSONParseError, TypeValidationError, JSONValue, APICallError, LanguageModelV3Prompt, SharedV3ProviderOptions } from '@ai-sdk/provider';
2
2
  import { StandardSchemaV1 } from '@standard-schema/spec';
3
3
  export * from '@standard-schema/spec';
4
4
  import * as z3 from 'zod/v3';
@@ -16,6 +16,42 @@ declare function combineHeaders(...headers: Array<Record<string, string | undefi
16
16
  */
17
17
  declare function convertAsyncIteratorToReadableStream<T>(iterator: AsyncIterator<T>): ReadableStream<T>;
18
18
 
19
+ /**
20
+ * Interface for mapping between custom tool names and provider tool names.
21
+ */
22
+ interface ToolNameMapping {
23
+ /**
24
+ * Maps a custom tool name (used by the client) to the provider's tool name.
25
+ * If the custom tool name does not have a mapping, returns the input name.
26
+ *
27
+ * @param customToolName - The custom name of the tool defined by the client.
28
+ * @returns The corresponding provider tool name, or the input name if not mapped.
29
+ */
30
+ toProviderToolName: (customToolName: string) => string;
31
+ /**
32
+ * Maps a provider tool name to the custom tool name used by the client.
33
+ * If the provider tool name does not have a mapping, returns the input name.
34
+ *
35
+ * @param providerToolName - The name of the tool as understood by the provider.
36
+ * @returns The corresponding custom tool name, or the input name if not mapped.
37
+ */
38
+ toCustomToolName: (providerToolName: string) => string;
39
+ }
40
+ /**
41
+ * @param tools - Tools that were passed to the language model.
42
+ * @param providerToolNames - Maps the provider tool ids to the provider tool names.
43
+ */
44
+ declare function createToolNameMapping({ tools, providerToolNames, }: {
45
+ /**
46
+ * Tools that were passed to the language model.
47
+ */
48
+ tools: Array<LanguageModelV3FunctionTool | LanguageModelV3ProviderDefinedTool> | undefined;
49
+ /**
50
+ * Maps the provider tool ids to the provider tool names.
51
+ */
52
+ providerToolNames: Record<`${string}.${string}`, string>;
53
+ }): ToolNameMapping;
54
+
19
55
  /**
20
56
  * Creates a Promise that resolves after a specified delay
21
57
  * @param delayInMs - The delay duration in milliseconds. If null or undefined, resolves immediately.
@@ -960,14 +996,10 @@ Tool with provider-defined input and output schemas.
960
996
  */
961
997
  type: 'provider-defined';
962
998
  /**
963
- The ID of the tool. Should follow the format `<provider-name>.<unique-tool-name>`.
999
+ The ID of the tool. Must follow the format `<provider-name>.<unique-tool-name>`.
964
1000
  */
965
1001
  id: `${string}.${string}`;
966
1002
  /**
967
- The name of the tool that the user must use in the tool set.
968
- */
969
- name: string;
970
- /**
971
1003
  The arguments for configuring the tool. Must match the expected arguments defined by the provider for this tool.
972
1004
  */
973
1005
  args: Record<string, unknown>;
@@ -1013,9 +1045,8 @@ type ProviderDefinedToolFactory<INPUT, ARGS extends object> = <OUTPUT>(options:
1013
1045
  onInputDelta?: Tool<INPUT, OUTPUT>['onInputDelta'];
1014
1046
  onInputAvailable?: Tool<INPUT, OUTPUT>['onInputAvailable'];
1015
1047
  }) => Tool<INPUT, OUTPUT>;
1016
- declare function createProviderDefinedToolFactory<INPUT, ARGS extends object>({ id, name, inputSchema, }: {
1048
+ declare function createProviderDefinedToolFactory<INPUT, ARGS extends object>({ id, inputSchema, }: {
1017
1049
  id: `${string}.${string}`;
1018
- name: string;
1019
1050
  inputSchema: FlexibleSchema<INPUT>;
1020
1051
  }): ProviderDefinedToolFactory<INPUT, ARGS>;
1021
1052
  type ProviderDefinedToolFactoryWithOutputSchema<INPUT, OUTPUT, ARGS extends object> = (options: ARGS & {
@@ -1026,9 +1057,8 @@ type ProviderDefinedToolFactoryWithOutputSchema<INPUT, OUTPUT, ARGS extends obje
1026
1057
  onInputDelta?: Tool<INPUT, OUTPUT>['onInputDelta'];
1027
1058
  onInputAvailable?: Tool<INPUT, OUTPUT>['onInputAvailable'];
1028
1059
  }) => Tool<INPUT, OUTPUT>;
1029
- declare function createProviderDefinedToolFactoryWithOutputSchema<INPUT, OUTPUT, ARGS extends object>({ id, name, inputSchema, outputSchema, }: {
1060
+ declare function createProviderDefinedToolFactoryWithOutputSchema<INPUT, OUTPUT, ARGS extends object>({ id, inputSchema, outputSchema, }: {
1030
1061
  id: `${string}.${string}`;
1031
- name: string;
1032
1062
  inputSchema: FlexibleSchema<INPUT>;
1033
1063
  outputSchema: FlexibleSchema<OUTPUT>;
1034
1064
  }): ProviderDefinedToolFactoryWithOutputSchema<INPUT, OUTPUT, ARGS>;
@@ -1173,4 +1203,4 @@ interface ToolResult<NAME extends string, INPUT, OUTPUT> {
1173
1203
  dynamic?: boolean;
1174
1204
  }
1175
1205
 
1176
- export { type AssistantContent, type AssistantModelMessage, type DataContent, DelayedPromise, type FetchFunction, type FilePart, type FlexibleSchema, type IdGenerator, type ImagePart, type InferSchema, type InferToolInput, type InferToolOutput, type LazySchema, type MaybePromiseLike, type ModelMessage, type ParseResult, type ProviderDefinedToolFactory, type ProviderDefinedToolFactoryWithOutputSchema, type ProviderOptions, type ReasoningPart, type Resolvable, type ResponseHandler, type Schema, type SystemModelMessage, type TextPart, type Tool, type ToolApprovalRequest, type ToolApprovalResponse, type ToolCall, type ToolCallOptions, type ToolCallPart, type ToolContent, type ToolExecuteFunction, type ToolModelMessage, type ToolNeedsApprovalFunction, type ToolResult, type ToolResultOutput, type ToolResultPart, type UserContent, type UserModelMessage, VERSION, type ValidationResult, asSchema, combineHeaders, convertAsyncIteratorToReadableStream, convertBase64ToUint8Array, convertToBase64, convertUint8ArrayToBase64, createBinaryResponseHandler, createEventSourceResponseHandler, createIdGenerator, createJsonErrorResponseHandler, createJsonResponseHandler, createProviderDefinedToolFactory, createProviderDefinedToolFactoryWithOutputSchema, createStatusCodeErrorResponseHandler, delay, dynamicTool, executeTool, extractResponseHeaders, generateId, getErrorMessage, getFromApi, getRuntimeEnvironmentUserAgent, injectJsonInstructionIntoMessages, isAbortError, isNonNullable, isParsableJson, isUrlSupported, jsonSchema, lazySchema, loadApiKey, loadOptionalSetting, loadSetting, mediaTypeToExtension, normalizeHeaders, parseJSON, parseJsonEventStream, parseProviderOptions, postFormDataToApi, postJsonToApi, postToApi, removeUndefinedEntries, resolve, safeParseJSON, safeValidateTypes, tool, validateTypes, withUserAgentSuffix, withoutTrailingSlash, zodSchema };
1206
+ export { type AssistantContent, type AssistantModelMessage, type DataContent, DelayedPromise, type FetchFunction, type FilePart, type FlexibleSchema, type IdGenerator, type ImagePart, type InferSchema, type InferToolInput, type InferToolOutput, type LazySchema, type MaybePromiseLike, type ModelMessage, type ParseResult, type ProviderDefinedToolFactory, type ProviderDefinedToolFactoryWithOutputSchema, type ProviderOptions, type ReasoningPart, type Resolvable, type ResponseHandler, type Schema, type SystemModelMessage, type TextPart, type Tool, type ToolApprovalRequest, type ToolApprovalResponse, type ToolCall, type ToolCallOptions, type ToolCallPart, type ToolContent, type ToolExecuteFunction, type ToolModelMessage, type ToolNameMapping, type ToolNeedsApprovalFunction, type ToolResult, type ToolResultOutput, type ToolResultPart, type UserContent, type UserModelMessage, VERSION, type ValidationResult, asSchema, combineHeaders, convertAsyncIteratorToReadableStream, convertBase64ToUint8Array, convertToBase64, convertUint8ArrayToBase64, createBinaryResponseHandler, createEventSourceResponseHandler, createIdGenerator, createJsonErrorResponseHandler, createJsonResponseHandler, createProviderDefinedToolFactory, createProviderDefinedToolFactoryWithOutputSchema, createStatusCodeErrorResponseHandler, createToolNameMapping, delay, dynamicTool, executeTool, extractResponseHeaders, generateId, getErrorMessage, getFromApi, getRuntimeEnvironmentUserAgent, injectJsonInstructionIntoMessages, isAbortError, isNonNullable, isParsableJson, isUrlSupported, jsonSchema, lazySchema, loadApiKey, loadOptionalSetting, loadSetting, mediaTypeToExtension, normalizeHeaders, parseJSON, parseJsonEventStream, parseProviderOptions, postFormDataToApi, postJsonToApi, postToApi, removeUndefinedEntries, resolve, safeParseJSON, safeValidateTypes, tool, validateTypes, withUserAgentSuffix, withoutTrailingSlash, zodSchema };
package/dist/index.js CHANGED
@@ -48,6 +48,7 @@ __export(src_exports, {
48
48
  createProviderDefinedToolFactory: () => createProviderDefinedToolFactory,
49
49
  createProviderDefinedToolFactoryWithOutputSchema: () => createProviderDefinedToolFactoryWithOutputSchema,
50
50
  createStatusCodeErrorResponseHandler: () => createStatusCodeErrorResponseHandler,
51
+ createToolNameMapping: () => createToolNameMapping,
51
52
  delay: () => delay,
52
53
  dynamicTool: () => dynamicTool,
53
54
  executeTool: () => executeTool,
@@ -135,6 +136,32 @@ function convertAsyncIteratorToReadableStream(iterator) {
135
136
  });
136
137
  }
137
138
 
139
+ // src/create-tool-name-mapping.ts
140
+ function createToolNameMapping({
141
+ tools = [],
142
+ providerToolNames
143
+ }) {
144
+ const customToolNameToProviderToolName = {};
145
+ const providerToolNameToCustomToolName = {};
146
+ for (const tool2 of tools) {
147
+ if (tool2.type === "provider-defined" && tool2.id in providerToolNames) {
148
+ const providerToolName = providerToolNames[tool2.id];
149
+ customToolNameToProviderToolName[tool2.name] = providerToolName;
150
+ providerToolNameToCustomToolName[providerToolName] = tool2.name;
151
+ }
152
+ }
153
+ return {
154
+ toProviderToolName: (customToolName) => {
155
+ var _a;
156
+ return (_a = customToolNameToProviderToolName[customToolName]) != null ? _a : customToolName;
157
+ },
158
+ toCustomToolName: (providerToolName) => {
159
+ var _a;
160
+ return (_a = providerToolNameToCustomToolName[providerToolName]) != null ? _a : providerToolName;
161
+ }
162
+ };
163
+ }
164
+
138
165
  // src/delay.ts
139
166
  async function delay(delayInMs, options) {
140
167
  if (delayInMs == null) {
@@ -351,7 +378,7 @@ function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
351
378
  }
352
379
 
353
380
  // src/version.ts
354
- var VERSION = true ? "4.0.0-beta.38" : "0.0.0-test";
381
+ var VERSION = true ? "4.0.0-beta.39" : "0.0.0-test";
355
382
 
356
383
  // src/get-from-api.ts
357
384
  var getOriginalFetch = () => globalThis.fetch;
@@ -2226,7 +2253,6 @@ function dynamicTool(tool2) {
2226
2253
  // src/provider-defined-tool-factory.ts
2227
2254
  function createProviderDefinedToolFactory({
2228
2255
  id,
2229
- name,
2230
2256
  inputSchema
2231
2257
  }) {
2232
2258
  return ({
@@ -2241,7 +2267,6 @@ function createProviderDefinedToolFactory({
2241
2267
  }) => tool({
2242
2268
  type: "provider-defined",
2243
2269
  id,
2244
- name,
2245
2270
  args,
2246
2271
  inputSchema,
2247
2272
  outputSchema,
@@ -2255,7 +2280,6 @@ function createProviderDefinedToolFactory({
2255
2280
  }
2256
2281
  function createProviderDefinedToolFactoryWithOutputSchema({
2257
2282
  id,
2258
- name,
2259
2283
  inputSchema,
2260
2284
  outputSchema
2261
2285
  }) {
@@ -2270,7 +2294,6 @@ function createProviderDefinedToolFactoryWithOutputSchema({
2270
2294
  }) => tool({
2271
2295
  type: "provider-defined",
2272
2296
  id,
2273
- name,
2274
2297
  args,
2275
2298
  inputSchema,
2276
2299
  outputSchema,
@@ -2506,6 +2529,7 @@ var import_stream2 = require("eventsource-parser/stream");
2506
2529
  createProviderDefinedToolFactory,
2507
2530
  createProviderDefinedToolFactoryWithOutputSchema,
2508
2531
  createStatusCodeErrorResponseHandler,
2532
+ createToolNameMapping,
2509
2533
  delay,
2510
2534
  dynamicTool,
2511
2535
  executeTool,