@ai-sdk/provider-utils 5.0.0-beta.9 → 5.0.0-canary.32

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.
Files changed (105) hide show
  1. package/CHANGELOG.md +184 -0
  2. package/dist/index.d.ts +1346 -878
  3. package/dist/index.js +813 -325
  4. package/dist/index.js.map +1 -1
  5. package/dist/test/index.d.ts +2 -1
  6. package/dist/test/index.js +18 -37
  7. package/dist/test/index.js.map +1 -1
  8. package/package.json +13 -11
  9. package/src/add-additional-properties-to-json-schema.ts +1 -1
  10. package/src/as-array.ts +12 -0
  11. package/src/convert-image-model-file-to-data-uri.ts +1 -1
  12. package/src/convert-inline-file-data-to-uint8-array.ts +30 -0
  13. package/src/create-tool-name-mapping.ts +1 -1
  14. package/src/detect-media-type.ts +312 -0
  15. package/src/filter-nullable.ts +11 -0
  16. package/src/get-error-message.ts +1 -15
  17. package/src/get-from-api.ts +2 -2
  18. package/src/has-required-key.ts +6 -0
  19. package/src/index.ts +39 -14
  20. package/src/inject-json-instruction.ts +1 -1
  21. package/src/is-buffer.ts +9 -0
  22. package/src/is-json-serializable.ts +29 -0
  23. package/src/is-provider-reference.ts +21 -0
  24. package/src/is-url-supported.ts +17 -2
  25. package/src/load-api-key.ts +1 -1
  26. package/src/load-setting.ts +1 -1
  27. package/src/map-reasoning-to-provider.ts +4 -1
  28. package/src/maybe-promise-like.ts +3 -0
  29. package/src/parse-json-event-stream.ts +3 -3
  30. package/src/parse-json.ts +3 -3
  31. package/src/parse-provider-options.ts +1 -1
  32. package/src/post-to-api.ts +4 -4
  33. package/src/{provider-tool-factory.ts → provider-defined-tool-factory.ts} +22 -29
  34. package/src/provider-executed-tool-factory.ts +69 -0
  35. package/src/resolve-full-media-type.ts +49 -0
  36. package/src/resolve-provider-reference.ts +26 -0
  37. package/src/resolve.ts +16 -1
  38. package/src/response-handler.ts +3 -3
  39. package/src/schema.ts +6 -3
  40. package/src/secure-json-parse.ts +1 -1
  41. package/src/serialize-model-options.ts +63 -0
  42. package/src/streaming-tool-call-tracker.ts +241 -0
  43. package/src/test/convert-response-stream-to-array.ts +1 -1
  44. package/src/test/is-node-version.ts +22 -1
  45. package/src/to-json-schema/zod3-to-json-schema/options.ts +3 -3
  46. package/src/to-json-schema/zod3-to-json-schema/parse-def.ts +3 -3
  47. package/src/to-json-schema/zod3-to-json-schema/parse-types.ts +22 -22
  48. package/src/to-json-schema/zod3-to-json-schema/parsers/array.ts +3 -3
  49. package/src/to-json-schema/zod3-to-json-schema/parsers/bigint.ts +1 -1
  50. package/src/to-json-schema/zod3-to-json-schema/parsers/branded.ts +2 -2
  51. package/src/to-json-schema/zod3-to-json-schema/parsers/catch.ts +2 -2
  52. package/src/to-json-schema/zod3-to-json-schema/parsers/date.ts +4 -4
  53. package/src/to-json-schema/zod3-to-json-schema/parsers/default.ts +3 -3
  54. package/src/to-json-schema/zod3-to-json-schema/parsers/effects.ts +3 -3
  55. package/src/to-json-schema/zod3-to-json-schema/parsers/enum.ts +1 -1
  56. package/src/to-json-schema/zod3-to-json-schema/parsers/intersection.ts +5 -5
  57. package/src/to-json-schema/zod3-to-json-schema/parsers/literal.ts +1 -1
  58. package/src/to-json-schema/zod3-to-json-schema/parsers/map.ts +4 -5
  59. package/src/to-json-schema/zod3-to-json-schema/parsers/native-enum.ts +1 -1
  60. package/src/to-json-schema/zod3-to-json-schema/parsers/never.ts +1 -2
  61. package/src/to-json-schema/zod3-to-json-schema/parsers/nullable.ts +4 -4
  62. package/src/to-json-schema/zod3-to-json-schema/parsers/number.ts +1 -1
  63. package/src/to-json-schema/zod3-to-json-schema/parsers/object.ts +3 -3
  64. package/src/to-json-schema/zod3-to-json-schema/parsers/optional.ts +3 -3
  65. package/src/to-json-schema/zod3-to-json-schema/parsers/pipeline.ts +4 -4
  66. package/src/to-json-schema/zod3-to-json-schema/parsers/promise.ts +3 -3
  67. package/src/to-json-schema/zod3-to-json-schema/parsers/readonly.ts +2 -2
  68. package/src/to-json-schema/zod3-to-json-schema/parsers/record.ts +9 -10
  69. package/src/to-json-schema/zod3-to-json-schema/parsers/set.ts +3 -3
  70. package/src/to-json-schema/zod3-to-json-schema/parsers/string.ts +2 -2
  71. package/src/to-json-schema/zod3-to-json-schema/parsers/tuple.ts +3 -3
  72. package/src/to-json-schema/zod3-to-json-schema/parsers/undefined.ts +1 -2
  73. package/src/to-json-schema/zod3-to-json-schema/parsers/union.ts +3 -3
  74. package/src/to-json-schema/zod3-to-json-schema/parsers/unknown.ts +1 -2
  75. package/src/to-json-schema/zod3-to-json-schema/refs.ts +3 -3
  76. package/src/to-json-schema/zod3-to-json-schema/select-parser.ts +2 -2
  77. package/src/to-json-schema/zod3-to-json-schema/zod3-to-json-schema.ts +3 -3
  78. package/src/types/assistant-model-message.ts +3 -3
  79. package/src/types/content-part.ts +138 -19
  80. package/src/types/executable-tool.ts +17 -0
  81. package/src/types/execute-tool.ts +23 -23
  82. package/src/types/file-data.ts +48 -0
  83. package/src/types/index.ts +19 -3
  84. package/src/types/infer-tool-context.ts +7 -2
  85. package/src/types/infer-tool-set-context.ts +7 -9
  86. package/src/types/model-message.ts +4 -4
  87. package/src/types/never-optional.ts +7 -0
  88. package/src/types/provider-options.ts +1 -1
  89. package/src/types/provider-reference.ts +10 -0
  90. package/src/types/sensitive-context.ts +9 -0
  91. package/src/types/system-model-message.ts +1 -1
  92. package/src/types/tool-approval-request.ts +7 -0
  93. package/src/types/tool-execute-function.ts +50 -0
  94. package/src/types/tool-model-message.ts +3 -3
  95. package/src/types/tool-needs-approval-function.ts +39 -0
  96. package/src/types/tool.ts +236 -223
  97. package/src/types/user-model-message.ts +2 -2
  98. package/src/validate-types.ts +5 -3
  99. package/dist/index.d.mts +0 -1578
  100. package/dist/index.mjs +0 -2817
  101. package/dist/index.mjs.map +0 -1
  102. package/dist/test/index.d.mts +0 -17
  103. package/dist/test/index.mjs +0 -77
  104. package/dist/test/index.mjs.map +0 -1
  105. package/src/types/union-to-intersection.ts +0 -17
package/src/index.ts CHANGED
@@ -1,37 +1,45 @@
1
+ export { asArray } from './as-array';
2
+ export type { Arrayable } from './as-array';
1
3
  export * from './combine-headers';
2
4
  export { convertAsyncIteratorToReadableStream } from './convert-async-iterator-to-readable-stream';
5
+ export { convertInlineFileDataToUint8Array } from './convert-inline-file-data-to-uint8-array';
6
+ export { convertImageModelFileToDataUri } from './convert-image-model-file-to-data-uri';
7
+ export { convertToFormData } from './convert-to-form-data';
3
8
  export {
4
9
  createToolNameMapping,
5
10
  type ToolNameMapping,
6
11
  } from './create-tool-name-mapping';
7
12
  export * from './delay';
8
13
  export { DelayedPromise } from './delayed-promise';
9
- export * from './extract-response-headers';
10
- export { convertImageModelFileToDataUri } from './convert-image-model-file-to-data-uri';
11
- export { convertToFormData } from './convert-to-form-data';
14
+ export {
15
+ detectMediaType,
16
+ getTopLevelMediaType,
17
+ isFullMediaType,
18
+ } from './detect-media-type';
12
19
  export { downloadBlob } from './download-blob';
13
20
  export { DownloadError } from './download-error';
14
- export {
15
- readResponseWithSizeLimit,
16
- DEFAULT_MAX_DOWNLOAD_SIZE,
17
- } from './read-response-with-size-limit';
21
+ export * from './extract-response-headers';
18
22
  export * from './fetch-function';
23
+ export { filterNullable } from './filter-nullable';
19
24
  export { createIdGenerator, generateId, type IdGenerator } from './generate-id';
20
25
  export * from './get-error-message';
21
26
  export * from './get-from-api';
22
27
  export { getRuntimeEnvironmentUserAgent } from './get-runtime-environment-user-agent';
28
+ export type { HasRequiredKey } from './has-required-key';
23
29
  export { injectJsonInstructionIntoMessages } from './inject-json-instruction';
24
30
  export * from './is-abort-error';
31
+ export { isBuffer } from './is-buffer';
25
32
  export { isNonNullable } from './is-non-nullable';
33
+ export { isProviderReference } from './is-provider-reference';
26
34
  export { isUrlSupported } from './is-url-supported';
27
35
  export * from './load-api-key';
36
+ export { loadOptionalSetting } from './load-optional-setting';
37
+ export { loadSetting } from './load-setting';
28
38
  export {
29
39
  isCustomReasoning,
30
40
  mapReasoningToProviderBudget,
31
41
  mapReasoningToProviderEffort,
32
42
  } from './map-reasoning-to-provider';
33
- export { loadOptionalSetting } from './load-optional-setting';
34
- export { loadSetting } from './load-setting';
35
43
  export { type MaybePromiseLike } from './maybe-promise-like';
36
44
  export { mediaTypeToExtension } from './media-type-to-extension';
37
45
  export { normalizeHeaders } from './normalize-headers';
@@ -40,13 +48,23 @@ export { parseJsonEventStream } from './parse-json-event-stream';
40
48
  export { parseProviderOptions } from './parse-provider-options';
41
49
  export * from './post-to-api';
42
50
  export {
43
- createProviderToolFactory,
44
- createProviderToolFactoryWithOutputSchema,
45
- type ProviderToolFactory,
46
- type ProviderToolFactoryWithOutputSchema,
47
- } from './provider-tool-factory';
51
+ createProviderDefinedToolFactory,
52
+ createProviderDefinedToolFactoryWithOutputSchema,
53
+ type ProviderDefinedToolFactory,
54
+ type ProviderDefinedToolFactoryWithOutputSchema,
55
+ } from './provider-defined-tool-factory';
56
+ export {
57
+ createProviderExecutedToolFactory,
58
+ type ProviderExecutedToolFactory,
59
+ } from './provider-executed-tool-factory';
60
+ export {
61
+ DEFAULT_MAX_DOWNLOAD_SIZE,
62
+ readResponseWithSizeLimit,
63
+ } from './read-response-with-size-limit';
48
64
  export * from './remove-undefined-entries';
49
65
  export * from './resolve';
66
+ export { resolveFullMediaType } from './resolve-full-media-type';
67
+ export { resolveProviderReference } from './resolve-provider-reference';
50
68
  export * from './response-handler';
51
69
  export {
52
70
  asSchema,
@@ -59,6 +77,12 @@ export {
59
77
  type Schema,
60
78
  type ValidationResult,
61
79
  } from './schema';
80
+ export { serializeModelOptions } from './serialize-model-options';
81
+ export {
82
+ StreamingToolCallTracker,
83
+ type StreamingToolCallDelta,
84
+ type StreamingToolCallTrackerOptions,
85
+ } from './streaming-tool-call-tracker';
62
86
  export { stripFileExtension } from './strip-file-extension';
63
87
  export * from './uint8-utils';
64
88
  export { validateDownloadUrl } from './validate-download-url';
@@ -72,6 +96,7 @@ export * from './types';
72
96
 
73
97
  // external re-exports
74
98
  export type * from '@standard-schema/spec';
99
+ export { WORKFLOW_DESERIALIZE, WORKFLOW_SERIALIZE } from '@workflow/serde';
75
100
  export {
76
101
  EventSourceParserStream,
77
102
  type EventSourceMessage,
@@ -1,4 +1,4 @@
1
- import {
1
+ import type {
2
2
  JSONSchema7,
3
3
  LanguageModelV4Message,
4
4
  LanguageModelV4Prompt,
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Type-guard for Node.js `Buffer` instances.
3
+ *
4
+ * Uses optional chaining on `globalThis.Buffer` so it returns `false` in
5
+ * runtimes where `Buffer` is not available (e.g. CloudFlare Workers).
6
+ */
7
+ export function isBuffer(value: unknown): value is Buffer {
8
+ return globalThis.Buffer?.isBuffer(value) ?? false;
9
+ }
@@ -0,0 +1,29 @@
1
+ import type { JSONValue } from '@ai-sdk/provider';
2
+
3
+ /**
4
+ * Checks whether a value can cross a workflow serialization boundary.
5
+ *
6
+ * The check accepts JSON-like primitives, arrays, and plain objects whose
7
+ * nested values are also serializable. It rejects functions, symbols,
8
+ * bigints, and non-plain objects such as class instances, dates, and regexes.
9
+ */
10
+ export function isJSONSerializable(value: unknown): value is JSONValue {
11
+ if (value === null || value === undefined) return true;
12
+
13
+ const type = typeof value;
14
+ if (type === 'string' || type === 'number' || type === 'boolean') return true;
15
+ if (type === 'function' || type === 'symbol' || type === 'bigint')
16
+ return false;
17
+
18
+ if (Array.isArray(value)) {
19
+ return value.every(isJSONSerializable);
20
+ }
21
+
22
+ if (Object.getPrototypeOf(value) === Object.prototype) {
23
+ return Object.values(value as Record<string, unknown>).every(
24
+ isJSONSerializable,
25
+ );
26
+ }
27
+
28
+ return false;
29
+ }
@@ -0,0 +1,21 @@
1
+ import type { SharedV4ProviderReference } from '@ai-sdk/provider';
2
+ import { isBuffer } from './is-buffer';
3
+
4
+ /**
5
+ * Checks whether a value is a provider reference (a mapping of provider names
6
+ * to provider-specific identifiers) as opposed to raw bytes, a URL, or a
7
+ * tagged `{ type: ... }` object.
8
+ */
9
+ export function isProviderReference(
10
+ data: unknown,
11
+ ): data is SharedV4ProviderReference {
12
+ return (
13
+ typeof data === 'object' &&
14
+ data !== null &&
15
+ !(data instanceof Uint8Array) &&
16
+ !(data instanceof URL) &&
17
+ !(data instanceof ArrayBuffer) &&
18
+ !isBuffer(data) &&
19
+ !('type' in data)
20
+ );
21
+ }
@@ -1,7 +1,9 @@
1
1
  /**
2
2
  * Checks if the given URL is supported natively by the model.
3
3
  *
4
- * @param mediaType - The media type of the URL. Case-sensitive.
4
+ * @param mediaType - The media type of the URL. Case-sensitive. May be a full
5
+ * `type/subtype`, a wildcard `type/*`, or just the
6
+ * top-level segment (e.g. `image`).
5
7
  * @param url - The URL to check.
6
8
  * @param supportedUrls - A record where keys are case-sensitive media types (or '*')
7
9
  * and values are arrays of RegExp patterns for URLs.
@@ -22,6 +24,8 @@ export function isUrlSupported({
22
24
  url = url.toLowerCase();
23
25
  mediaType = mediaType.toLowerCase();
24
26
 
27
+ const isTopLevelOnly = !mediaType.includes('/');
28
+
25
29
  return (
26
30
  Object.entries(supportedUrls)
27
31
  // standardize supported url map into lowercase prefixes:
@@ -32,7 +36,18 @@ export function isUrlSupported({
32
36
  : { mediaTypePrefix: mediaType.replace(/\*/, ''), regexes: value };
33
37
  })
34
38
  // gather all regexp pattern from matched media type prefixes:
35
- .filter(({ mediaTypePrefix }) => mediaType.startsWith(mediaTypePrefix))
39
+ .filter(({ mediaTypePrefix }) => {
40
+ if (mediaTypePrefix === '') {
41
+ return true;
42
+ }
43
+ // For a top-level-only media type (e.g. `image`), we cannot determine
44
+ // whether a specific subtype (e.g. `image/png`) would apply, so we
45
+ // only match the corresponding `type/*` prefix exactly.
46
+ if (isTopLevelOnly) {
47
+ return `${mediaType}/` === mediaTypePrefix;
48
+ }
49
+ return mediaType.startsWith(mediaTypePrefix);
50
+ })
36
51
  .flatMap(({ regexes }) => regexes)
37
52
  // check if any pattern matches the url:
38
53
  .some(pattern => pattern.test(url))
@@ -23,7 +23,7 @@ export function loadApiKey({
23
23
 
24
24
  if (typeof process === 'undefined') {
25
25
  throw new LoadAPIKeyError({
26
- message: `${description} API key is missing. Pass it using the '${apiKeyParameterName}' parameter. Environment variables is not supported in this environment.`,
26
+ message: `${description} API key is missing. Pass it using the '${apiKeyParameterName}' parameter. Environment variables are not supported in this environment.`,
27
27
  });
28
28
  }
29
29
 
@@ -35,7 +35,7 @@ export function loadSetting({
35
35
  message:
36
36
  `${description} setting is missing. ` +
37
37
  `Pass it using the '${settingName}' parameter. ` +
38
- `Environment variables is not supported in this environment.`,
38
+ `Environment variables are not supported in this environment.`,
39
39
  });
40
40
  }
41
41
 
@@ -1,4 +1,7 @@
1
- import { LanguageModelV4CallOptions, SharedV4Warning } from '@ai-sdk/provider';
1
+ import type {
2
+ LanguageModelV4CallOptions,
3
+ SharedV4Warning,
4
+ } from '@ai-sdk/provider';
2
5
 
3
6
  export type ReasoningLevel = Exclude<
4
7
  LanguageModelV4CallOptions['reasoning'],
@@ -1,3 +1,6 @@
1
+ /**
2
+ * A value that can be provided either synchronously or as a promise-like.
3
+ */
1
4
  export type MaybePromiseLike<T> =
2
5
  | T // Raw value
3
6
  | PromiseLike<T>; // Promise of value
@@ -1,9 +1,9 @@
1
1
  import {
2
- EventSourceMessage,
3
2
  EventSourceParserStream,
3
+ type EventSourceMessage,
4
4
  } from 'eventsource-parser/stream';
5
- import { ParseResult, safeParseJSON } from './parse-json';
6
- import { FlexibleSchema } from './schema';
5
+ import { safeParseJSON, type ParseResult } from './parse-json';
6
+ import type { FlexibleSchema } from './schema';
7
7
 
8
8
  /**
9
9
  * Parses a JSON event stream into a stream of parsed JSON objects.
package/src/parse-json.ts CHANGED
@@ -1,11 +1,11 @@
1
1
  import {
2
2
  JSONParseError,
3
- JSONValue,
4
3
  TypeValidationError,
4
+ type JSONValue,
5
5
  } from '@ai-sdk/provider';
6
6
  import { secureJsonParse } from './secure-json-parse';
7
7
  import { safeValidateTypes, validateTypes } from './validate-types';
8
- import { FlexibleSchema } from './schema';
8
+ import type { FlexibleSchema } from './schema';
9
9
 
10
10
  /**
11
11
  * Parses a JSON string into an unknown object.
@@ -43,7 +43,7 @@ export async function parseJSON<T>({
43
43
  return value;
44
44
  }
45
45
 
46
- return validateTypes<T>({ value, schema });
46
+ return await validateTypes<T>({ value, schema });
47
47
  } catch (error) {
48
48
  if (
49
49
  JSONParseError.isInstance(error) ||
@@ -1,6 +1,6 @@
1
1
  import { InvalidArgumentError } from '@ai-sdk/provider';
2
2
  import { safeValidateTypes } from './validate-types';
3
- import { FlexibleSchema } from './schema';
3
+ import type { FlexibleSchema } from './schema';
4
4
 
5
5
  export async function parseProviderOptions<OPTIONS>({
6
6
  provider,
@@ -1,9 +1,9 @@
1
1
  import { APICallError } from '@ai-sdk/provider';
2
2
  import { extractResponseHeaders } from './extract-response-headers';
3
- import { FetchFunction } from './fetch-function';
3
+ import type { FetchFunction } from './fetch-function';
4
4
  import { handleFetchError } from './handle-fetch-error';
5
5
  import { isAbortError } from './is-abort-error';
6
- import { ResponseHandler } from './response-handler';
6
+ import type { ResponseHandler } from './response-handler';
7
7
  import { getRuntimeEnvironmentUserAgent } from './get-runtime-environment-user-agent';
8
8
  import { withUserAgentSuffix } from './with-user-agent-suffix';
9
9
  import { VERSION } from './version';
@@ -28,7 +28,7 @@ export const postJsonToApi = async <T>({
28
28
  abortSignal?: AbortSignal;
29
29
  fetch?: FetchFunction;
30
30
  }) =>
31
- postToApi({
31
+ await postToApi({
32
32
  url,
33
33
  headers: {
34
34
  'Content-Type': 'application/json',
@@ -61,7 +61,7 @@ export const postFormDataToApi = async <T>({
61
61
  abortSignal?: AbortSignal;
62
62
  fetch?: FetchFunction;
63
63
  }) =>
64
- postToApi({
64
+ await postToApi({
65
65
  url,
66
66
  headers,
67
67
  body: {
@@ -1,8 +1,12 @@
1
- import { tool, Tool, ToolExecuteFunction } from './types/tool';
2
- import { FlexibleSchema } from './schema';
3
- import { Context } from './types/context';
4
-
5
- export type ProviderToolFactory<
1
+ import { tool, type ProviderDefinedTool, type Tool } from './types/tool';
2
+ import type { FlexibleSchema } from './schema';
3
+ import type { Context } from './types/context';
4
+ import type { ToolExecuteFunction } from './types/tool-execute-function';
5
+ /**
6
+ * A provider-defined tool is a tool for which the provider defines the input
7
+ * and output schemas, but does not execute the tool.
8
+ */
9
+ export type ProviderDefinedToolFactory<
6
10
  INPUT,
7
11
  ARGS extends object,
8
12
  CONTEXT extends Context = {},
@@ -15,9 +19,9 @@ export type ProviderToolFactory<
15
19
  onInputDelta?: Tool<INPUT, OUTPUT, CONTEXT>['onInputDelta'];
16
20
  onInputAvailable?: Tool<INPUT, OUTPUT, CONTEXT>['onInputAvailable'];
17
21
  },
18
- ) => Tool<INPUT, OUTPUT, CONTEXT>;
22
+ ) => ProviderDefinedTool<INPUT, OUTPUT, CONTEXT>;
19
23
 
20
- export function createProviderToolFactory<
24
+ export function createProviderDefinedToolFactory<
21
25
  INPUT,
22
26
  ARGS extends object,
23
27
  CONTEXT extends Context = {},
@@ -27,7 +31,7 @@ export function createProviderToolFactory<
27
31
  }: {
28
32
  id: `${string}.${string}`;
29
33
  inputSchema: FlexibleSchema<INPUT>;
30
- }): ProviderToolFactory<INPUT, ARGS, CONTEXT> {
34
+ }): ProviderDefinedToolFactory<INPUT, ARGS, CONTEXT> {
31
35
  return <OUTPUT>({
32
36
  execute,
33
37
  outputSchema,
@@ -45,9 +49,10 @@ export function createProviderToolFactory<
45
49
  onInputStart?: Tool<INPUT, OUTPUT, CONTEXT>['onInputStart'];
46
50
  onInputDelta?: Tool<INPUT, OUTPUT, CONTEXT>['onInputDelta'];
47
51
  onInputAvailable?: Tool<INPUT, OUTPUT, CONTEXT>['onInputAvailable'];
48
- }): Tool<INPUT, OUTPUT, CONTEXT> =>
52
+ }): ProviderDefinedTool<INPUT, OUTPUT, CONTEXT> =>
49
53
  tool({
50
54
  type: 'provider',
55
+ isProviderExecuted: false,
51
56
  id,
52
57
  args,
53
58
  inputSchema,
@@ -58,10 +63,10 @@ export function createProviderToolFactory<
58
63
  onInputStart,
59
64
  onInputDelta,
60
65
  onInputAvailable,
61
- });
66
+ }) as ProviderDefinedTool<INPUT, OUTPUT, CONTEXT>;
62
67
  }
63
68
 
64
- export type ProviderToolFactoryWithOutputSchema<
69
+ export type ProviderDefinedToolFactoryWithOutputSchema<
65
70
  INPUT,
66
71
  OUTPUT,
67
72
  ARGS extends object,
@@ -75,9 +80,9 @@ export type ProviderToolFactoryWithOutputSchema<
75
80
  onInputDelta?: Tool<INPUT, OUTPUT, CONTEXT>['onInputDelta'];
76
81
  onInputAvailable?: Tool<INPUT, OUTPUT, CONTEXT>['onInputAvailable'];
77
82
  },
78
- ) => Tool<INPUT, OUTPUT, CONTEXT>;
83
+ ) => ProviderDefinedTool<INPUT, OUTPUT, CONTEXT>;
79
84
 
80
- export function createProviderToolFactoryWithOutputSchema<
85
+ export function createProviderDefinedToolFactoryWithOutputSchema<
81
86
  INPUT,
82
87
  OUTPUT,
83
88
  ARGS extends object,
@@ -86,23 +91,11 @@ export function createProviderToolFactoryWithOutputSchema<
86
91
  id,
87
92
  inputSchema,
88
93
  outputSchema,
89
- supportsDeferredResults,
90
94
  }: {
91
95
  id: `${string}.${string}`;
92
96
  inputSchema: FlexibleSchema<INPUT>;
93
97
  outputSchema: FlexibleSchema<OUTPUT>;
94
- /**
95
- * Whether this provider-executed tool supports deferred results.
96
- *
97
- * When true, the tool result may not be returned in the same turn as the
98
- * tool call (e.g., when using programmatic tool calling where a server tool
99
- * triggers a client-executed tool, and the server tool's result is deferred
100
- * until the client tool is resolved).
101
- *
102
- * @default false
103
- */
104
- supportsDeferredResults?: boolean;
105
- }): ProviderToolFactoryWithOutputSchema<INPUT, OUTPUT, ARGS, CONTEXT> {
98
+ }): ProviderDefinedToolFactoryWithOutputSchema<INPUT, OUTPUT, ARGS, CONTEXT> {
106
99
  return ({
107
100
  execute,
108
101
  needsApproval,
@@ -118,9 +111,10 @@ export function createProviderToolFactoryWithOutputSchema<
118
111
  onInputStart?: Tool<INPUT, OUTPUT, CONTEXT>['onInputStart'];
119
112
  onInputDelta?: Tool<INPUT, OUTPUT, CONTEXT>['onInputDelta'];
120
113
  onInputAvailable?: Tool<INPUT, OUTPUT, CONTEXT>['onInputAvailable'];
121
- }): Tool<INPUT, OUTPUT, CONTEXT> =>
114
+ }): ProviderDefinedTool<INPUT, OUTPUT, CONTEXT> =>
122
115
  tool({
123
116
  type: 'provider',
117
+ isProviderExecuted: false,
124
118
  id,
125
119
  args,
126
120
  inputSchema,
@@ -131,6 +125,5 @@ export function createProviderToolFactoryWithOutputSchema<
131
125
  onInputStart,
132
126
  onInputDelta,
133
127
  onInputAvailable,
134
- supportsDeferredResults,
135
- });
128
+ }) as ProviderDefinedTool<INPUT, OUTPUT, CONTEXT>;
136
129
  }
@@ -0,0 +1,69 @@
1
+ import type { FlexibleSchema } from './schema';
2
+ import type { Context } from './types/context';
3
+ import { tool, type ProviderExecutedTool, type Tool } from './types/tool';
4
+ /**
5
+ * A provider-executed tool is a tool for which the provider executes the tool.
6
+ */
7
+ export type ProviderExecutedToolFactory<
8
+ INPUT,
9
+ OUTPUT,
10
+ ARGS extends object,
11
+ CONTEXT extends Context = {},
12
+ > = (
13
+ options: ARGS & {
14
+ onInputStart?: Tool<INPUT, OUTPUT, CONTEXT>['onInputStart'];
15
+ onInputDelta?: Tool<INPUT, OUTPUT, CONTEXT>['onInputDelta'];
16
+ onInputAvailable?: Tool<INPUT, OUTPUT, CONTEXT>['onInputAvailable'];
17
+ },
18
+ ) => ProviderExecutedTool<INPUT, OUTPUT, CONTEXT>;
19
+
20
+ export function createProviderExecutedToolFactory<
21
+ INPUT,
22
+ OUTPUT,
23
+ ARGS extends object,
24
+ CONTEXT extends Context = {},
25
+ >({
26
+ id,
27
+ inputSchema,
28
+ outputSchema,
29
+ supportsDeferredResults,
30
+ }: {
31
+ id: `${string}.${string}`;
32
+ inputSchema: FlexibleSchema<INPUT>;
33
+ outputSchema: FlexibleSchema<OUTPUT>;
34
+
35
+ /**
36
+ * Whether this provider-executed tool supports deferred results.
37
+ *
38
+ * When true, the tool result may not be returned in the same turn as the
39
+ * tool call (e.g., when using programmatic tool calling where a server tool
40
+ * triggers a client-executed tool, and the server tool's result is deferred
41
+ * until the client tool is resolved).
42
+ *
43
+ * @default false
44
+ */
45
+ supportsDeferredResults?: boolean;
46
+ }): ProviderExecutedToolFactory<INPUT, OUTPUT, ARGS, CONTEXT> {
47
+ return ({
48
+ onInputStart,
49
+ onInputDelta,
50
+ onInputAvailable,
51
+ ...args
52
+ }: ARGS & {
53
+ onInputStart?: Tool<INPUT, OUTPUT, CONTEXT>['onInputStart'];
54
+ onInputDelta?: Tool<INPUT, OUTPUT, CONTEXT>['onInputDelta'];
55
+ onInputAvailable?: Tool<INPUT, OUTPUT, CONTEXT>['onInputAvailable'];
56
+ }): ProviderExecutedTool<INPUT, OUTPUT, CONTEXT> =>
57
+ tool({
58
+ type: 'provider',
59
+ isProviderExecuted: true,
60
+ id,
61
+ args,
62
+ inputSchema,
63
+ outputSchema,
64
+ onInputStart,
65
+ onInputDelta,
66
+ onInputAvailable,
67
+ supportsDeferredResults,
68
+ }) as ProviderExecutedTool<INPUT, OUTPUT, CONTEXT>;
69
+ }
@@ -0,0 +1,49 @@
1
+ import {
2
+ UnsupportedFunctionalityError,
3
+ type LanguageModelV4FilePart,
4
+ } from '@ai-sdk/provider';
5
+ import {
6
+ detectMediaType,
7
+ getTopLevelMediaType,
8
+ isFullMediaType,
9
+ } from './detect-media-type';
10
+
11
+ /**
12
+ * Resolves a file part's media type to a full `type/subtype` form required by
13
+ * providers whose API demands the full IANA media type.
14
+ *
15
+ * - If `part.mediaType` is already a full media type (e.g. `image/png`), it is
16
+ * returned as-is.
17
+ * - Otherwise, when inline bytes are available (`part.data.type === 'data'`),
18
+ * the subtype is sniffed from the bytes using the signature table that
19
+ * corresponds to the top-level segment.
20
+ * - When neither applies (e.g. top-level-only with a URL source, or bytes that
21
+ * cannot be detected), an `UnsupportedFunctionalityError` is thrown.
22
+ */
23
+ export function resolveFullMediaType({
24
+ part,
25
+ }: {
26
+ part: LanguageModelV4FilePart;
27
+ }): string {
28
+ if (isFullMediaType(part.mediaType)) {
29
+ return part.mediaType;
30
+ }
31
+
32
+ if (part.data.type === 'data') {
33
+ const detected = detectMediaType({
34
+ data: part.data.data,
35
+ topLevelType: getTopLevelMediaType(part.mediaType),
36
+ });
37
+ if (detected) {
38
+ return detected;
39
+ }
40
+
41
+ throw new UnsupportedFunctionalityError({
42
+ functionality: `file of media type "${part.mediaType}" must specify subtype since it could not be auto-detected`,
43
+ });
44
+ }
45
+
46
+ throw new UnsupportedFunctionalityError({
47
+ functionality: `file of media type "${part.mediaType}" must specify subtype since it is not passed as inline bytes`,
48
+ });
49
+ }
@@ -0,0 +1,26 @@
1
+ import {
2
+ NoSuchProviderReferenceError,
3
+ type SharedV4ProviderReference,
4
+ } from '@ai-sdk/provider';
5
+ /**
6
+ * Resolves a provider reference to the provider-specific identifier for the
7
+ * given provider. Throws `NoSuchProviderReferenceError` if the provider is not
8
+ * found in the reference mapping.
9
+ */
10
+ export function resolveProviderReference({
11
+ reference,
12
+ provider,
13
+ }: {
14
+ reference: SharedV4ProviderReference;
15
+ provider: string;
16
+ }): string {
17
+ const id = reference[provider];
18
+ if (id != null) {
19
+ return id;
20
+ }
21
+
22
+ throw new NoSuchProviderReferenceError({
23
+ provider,
24
+ reference,
25
+ });
26
+ }
package/src/resolve.ts CHANGED
@@ -1,5 +1,20 @@
1
- import { MaybePromiseLike } from './maybe-promise-like';
1
+ import type { MaybePromiseLike } from './maybe-promise-like';
2
2
 
3
+ /**
4
+ * A value or a lazy provider of a value, each of which may be synchronous or asynchronous.
5
+ *
6
+ * @template T The resolved type after {@link resolve} runs.
7
+ *
8
+ * One of:
9
+ * - A plain value of type {@link T}
10
+ * - A {@link PromiseLike} of {@link T} (e.g. a `Promise<T>`)
11
+ * - A zero-argument function that returns a plain {@link T}
12
+ * - A zero-argument function that returns a {@link PromiseLike} of {@link T}
13
+ *
14
+ * The function form is only invoked when passed to {@link resolve}; it is not distinguished from
15
+ * a {@link T} that happens to be a function—callers should wrap function values if disambiguation
16
+ * is required.
17
+ */
3
18
  export type Resolvable<T> = MaybePromiseLike<T> | (() => MaybePromiseLike<T>);
4
19
 
5
20
  /**
@@ -1,8 +1,8 @@
1
1
  import { APICallError, EmptyResponseBodyError } from '@ai-sdk/provider';
2
2
  import { extractResponseHeaders } from './extract-response-headers';
3
- import { parseJSON, ParseResult, safeParseJSON } from './parse-json';
3
+ import { parseJSON, safeParseJSON, type ParseResult } from './parse-json';
4
4
  import { parseJsonEventStream } from './parse-json-event-stream';
5
- import { FlexibleSchema } from './schema';
5
+ import type { FlexibleSchema } from './schema';
6
6
 
7
7
  export type ResponseHandler<RETURN_TYPE> = (options: {
8
8
  url: string;
@@ -64,7 +64,7 @@ export const createJsonErrorResponseHandler =
64
64
  isRetryable: isRetryable?.(response, parsedError),
65
65
  }),
66
66
  };
67
- } catch (parseError) {
67
+ } catch {
68
68
  return {
69
69
  responseHeaders,
70
70
  value: new APICallError({
package/src/schema.ts CHANGED
@@ -1,6 +1,9 @@
1
- import { JSONSchema7, TypeValidationError } from '@ai-sdk/provider';
2
- import { StandardSchemaV1, StandardJSONSchemaV1 } from '@standard-schema/spec';
3
- import * as z3 from 'zod/v3';
1
+ import { TypeValidationError, type JSONSchema7 } from '@ai-sdk/provider';
2
+ import type {
3
+ StandardSchemaV1,
4
+ StandardJSONSchemaV1,
5
+ } from '@standard-schema/spec';
6
+ import type * as z3 from 'zod/v3';
4
7
  import * as z4 from 'zod/v4';
5
8
  import { addAdditionalPropertiesToJsonSchema } from './add-additional-properties-to-json-schema';
6
9
  import { zod3ToJsonSchema } from './to-json-schema/zod3-to-json-schema';
@@ -83,7 +83,7 @@ export function secureJsonParse(text: string) {
83
83
  try {
84
84
  // Performance optimization, see https://github.com/fastify/secure-json-parse/pull/90
85
85
  Error.stackTraceLimit = 0;
86
- } catch (e) {
86
+ } catch {
87
87
  // Fallback in case Error is immutable (v8 readonly)
88
88
  return _parse(text);
89
89
  }