@ai-sdk/provider-utils 5.0.0-beta.3 → 5.0.0-beta.30

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 (110) hide show
  1. package/CHANGELOG.md +284 -0
  2. package/dist/index.d.ts +1339 -813
  3. package/dist/index.js +837 -288
  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 -13
  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 +3 -3
  12. package/src/convert-inline-file-data-to-uint8-array.ts +30 -0
  13. package/src/create-tool-name-mapping.ts +6 -22
  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 +42 -12
  20. package/src/inject-json-instruction.ts +6 -6
  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 +108 -0
  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-defined-tool-factory.ts +129 -0
  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 +5 -3
  79. package/src/types/content-part.ts +102 -24
  80. package/src/types/context.ts +4 -0
  81. package/src/types/executable-tool.ts +17 -0
  82. package/src/types/execute-tool.ts +29 -9
  83. package/src/types/file-data.ts +48 -0
  84. package/src/types/index.ts +26 -11
  85. package/src/types/infer-tool-context.ts +12 -0
  86. package/src/types/infer-tool-input.ts +7 -0
  87. package/src/types/infer-tool-output.ts +7 -0
  88. package/src/types/infer-tool-set-context.ts +15 -0
  89. package/src/types/model-message.ts +4 -4
  90. package/src/types/never-optional.ts +7 -0
  91. package/src/types/provider-options.ts +2 -2
  92. package/src/types/provider-reference.ts +10 -0
  93. package/src/types/sensitive-context.ts +9 -0
  94. package/src/types/system-model-message.ts +1 -1
  95. package/src/types/tool-approval-request.ts +7 -0
  96. package/src/types/tool-execute-function.ts +50 -0
  97. package/src/types/tool-model-message.ts +3 -3
  98. package/src/types/tool-needs-approval-function.ts +39 -0
  99. package/src/types/tool-set.ts +22 -0
  100. package/src/types/tool.ts +251 -222
  101. package/src/types/user-model-message.ts +2 -2
  102. package/src/validate-download-url.ts +7 -2
  103. package/src/validate-types.ts +5 -3
  104. package/dist/index.d.mts +0 -1458
  105. package/dist/index.mjs +0 -2759
  106. package/dist/index.mjs.map +0 -1
  107. package/dist/test/index.d.mts +0 -17
  108. package/dist/test/index.mjs +0 -77
  109. package/dist/test/index.mjs.map +0 -1
  110. package/src/provider-tool-factory.ts +0 -125
@@ -1,4 +1,4 @@
1
- import { ZodLiteralDef } from 'zod/v3';
1
+ import type { ZodLiteralDef } from 'zod/v3';
2
2
 
3
3
  export type JsonSchema7LiteralType =
4
4
  | {
@@ -1,10 +1,9 @@
1
- import { ZodMapDef } from 'zod/v3';
1
+ import type { ZodMapDef } from 'zod/v3';
2
2
  import { parseDef } from '../parse-def';
3
- import { JsonSchema7Type } from '../parse-types';
4
- import { Refs } from '../refs';
3
+ import type { JsonSchema7Type } from '../parse-types';
4
+ import type { Refs } from '../refs';
5
5
  import { parseAnyDef } from './any';
6
- import { JsonSchema7RecordType, parseRecordDef } from './record';
7
-
6
+ import { parseRecordDef, type JsonSchema7RecordType } from './record';
8
7
  export type JsonSchema7MapType = {
9
8
  type: 'array';
10
9
  maxItems: 125;
@@ -1,4 +1,4 @@
1
- import { ZodNativeEnumDef } from 'zod/v3';
1
+ import type { ZodNativeEnumDef } from 'zod/v3';
2
2
 
3
3
  export type JsonSchema7NativeEnumType = {
4
4
  type: 'string' | 'number' | ['string', 'number'];
@@ -1,5 +1,4 @@
1
- import { JsonSchema7AnyType, parseAnyDef } from './any';
2
-
1
+ import { parseAnyDef, type JsonSchema7AnyType } from './any';
3
2
  export type JsonSchema7NeverType = {
4
3
  not: JsonSchema7AnyType;
5
4
  };
@@ -1,8 +1,8 @@
1
- import { ZodNullableDef } from 'zod/v3';
1
+ import type { ZodNullableDef } from 'zod/v3';
2
2
  import { parseDef } from '../parse-def';
3
- import { JsonSchema7Type } from '../parse-types';
4
- import { Refs } from '../refs';
5
- import { JsonSchema7NullType } from './null';
3
+ import type { JsonSchema7Type } from '../parse-types';
4
+ import type { Refs } from '../refs';
5
+ import type { JsonSchema7NullType } from './null';
6
6
  import { primitiveMappings } from './union';
7
7
 
8
8
  export type JsonSchema7NullableType =
@@ -1,4 +1,4 @@
1
- import { ZodNumberDef } from 'zod/v3';
1
+ import type { ZodNumberDef } from 'zod/v3';
2
2
 
3
3
  export type JsonSchema7NumberType = {
4
4
  type: 'number' | 'integer';
@@ -1,7 +1,7 @@
1
- import { ZodObjectDef, ZodTypeAny } from 'zod/v3';
1
+ import type { ZodObjectDef, ZodTypeAny } from 'zod/v3';
2
2
  import { parseDef } from '../parse-def';
3
- import { JsonSchema7Type } from '../parse-types';
4
- import { Refs } from '../refs';
3
+ import type { JsonSchema7Type } from '../parse-types';
4
+ import type { Refs } from '../refs';
5
5
 
6
6
  export type JsonSchema7ObjectType = {
7
7
  type: 'object';
@@ -1,7 +1,7 @@
1
- import { ZodOptionalDef } from 'zod/v3';
1
+ import type { ZodOptionalDef } from 'zod/v3';
2
2
  import { parseDef } from '../parse-def';
3
- import { JsonSchema7Type } from '../parse-types';
4
- import { Refs } from '../refs';
3
+ import type { JsonSchema7Type } from '../parse-types';
4
+ import type { Refs } from '../refs';
5
5
  import { parseAnyDef } from './any';
6
6
 
7
7
  export const parseOptionalDef = (
@@ -1,8 +1,8 @@
1
- import { ZodPipelineDef } from 'zod/v3';
1
+ import type { ZodPipelineDef } from 'zod/v3';
2
2
  import { parseDef } from '../parse-def';
3
- import { JsonSchema7Type } from '../parse-types';
4
- import { Refs } from '../refs';
5
- import { JsonSchema7AllOfType } from './intersection';
3
+ import type { JsonSchema7Type } from '../parse-types';
4
+ import type { Refs } from '../refs';
5
+ import type { JsonSchema7AllOfType } from './intersection';
6
6
 
7
7
  export const parsePipelineDef = (
8
8
  def: ZodPipelineDef<any, any>,
@@ -1,7 +1,7 @@
1
- import { ZodPromiseDef } from 'zod/v3';
1
+ import type { ZodPromiseDef } from 'zod/v3';
2
2
  import { parseDef } from '../parse-def';
3
- import { JsonSchema7Type } from '../parse-types';
4
- import { Refs } from '../refs';
3
+ import type { JsonSchema7Type } from '../parse-types';
4
+ import type { Refs } from '../refs';
5
5
 
6
6
  export function parsePromiseDef(
7
7
  def: ZodPromiseDef,
@@ -1,6 +1,6 @@
1
- import { ZodReadonlyDef } from 'zod/v3';
1
+ import type { ZodReadonlyDef } from 'zod/v3';
2
2
  import { parseDef } from '../parse-def';
3
- import { Refs } from '../refs';
3
+ import type { Refs } from '../refs';
4
4
 
5
5
  export const parseReadonlyDef = (def: ZodReadonlyDef<any>, refs: Refs) => {
6
6
  return parseDef(def.innerType._def, refs);
@@ -1,16 +1,15 @@
1
1
  import {
2
2
  ZodFirstPartyTypeKind,
3
- ZodMapDef,
4
- ZodRecordDef,
5
- ZodTypeAny,
3
+ type ZodMapDef,
4
+ type ZodRecordDef,
5
+ type ZodTypeAny,
6
6
  } from 'zod/v3';
7
7
  import { parseDef } from '../parse-def';
8
- import { JsonSchema7Type } from '../parse-types';
9
- import { Refs } from '../refs';
8
+ import type { JsonSchema7Type } from '../parse-types';
9
+ import type { Refs } from '../refs';
10
10
  import { parseBrandedDef } from './branded';
11
- import { JsonSchema7EnumType } from './enum';
12
- import { JsonSchema7StringType, parseStringDef } from './string';
13
-
11
+ import type { JsonSchema7EnumType } from './enum';
12
+ import { parseStringDef, type JsonSchema7StringType } from './string';
14
13
  type JsonSchema7RecordPropertyNamesType =
15
14
  | Omit<JsonSchema7StringType, 'type'>
16
15
  | Omit<JsonSchema7EnumType, 'type'>;
@@ -38,7 +37,7 @@ export function parseRecordDef(
38
37
  def.keyType?._def.typeName === ZodFirstPartyTypeKind.ZodString &&
39
38
  def.keyType._def.checks?.length
40
39
  ) {
41
- const { type, ...keyType } = parseStringDef(def.keyType._def, refs);
40
+ const { type: _type, ...keyType } = parseStringDef(def.keyType._def, refs);
42
41
 
43
42
  return {
44
43
  ...schema,
@@ -56,7 +55,7 @@ export function parseRecordDef(
56
55
  def.keyType._def.type._def.typeName === ZodFirstPartyTypeKind.ZodString &&
57
56
  def.keyType._def.type._def.checks?.length
58
57
  ) {
59
- const { type, ...keyType } = parseBrandedDef(
58
+ const { type: _type, ...keyType } = parseBrandedDef(
60
59
  def.keyType._def,
61
60
  refs,
62
61
  ) as JsonSchema7StringType;
@@ -1,7 +1,7 @@
1
- import { ZodSetDef } from 'zod/v3';
1
+ import type { ZodSetDef } from 'zod/v3';
2
2
  import { parseDef } from '../parse-def';
3
- import { JsonSchema7Type } from '../parse-types';
4
- import { Refs } from '../refs';
3
+ import type { JsonSchema7Type } from '../parse-types';
4
+ import type { Refs } from '../refs';
5
5
 
6
6
  export type JsonSchema7SetType = {
7
7
  type: 'array';
@@ -1,5 +1,5 @@
1
- import { ZodStringDef } from 'zod/v3';
2
- import { Refs } from '../refs';
1
+ import type { ZodStringDef } from 'zod/v3';
2
+ import type { Refs } from '../refs';
3
3
 
4
4
  let emojiRegex: RegExp | undefined = undefined;
5
5
 
@@ -1,7 +1,7 @@
1
- import { ZodTupleDef, ZodTupleItems, ZodTypeAny } from 'zod/v3';
1
+ import type { ZodTupleDef, ZodTupleItems, ZodTypeAny } from 'zod/v3';
2
2
  import { parseDef } from '../parse-def';
3
- import { JsonSchema7Type } from '../parse-types';
4
- import { Refs } from '../refs';
3
+ import type { JsonSchema7Type } from '../parse-types';
4
+ import type { Refs } from '../refs';
5
5
 
6
6
  export type JsonSchema7TupleType = {
7
7
  type: 'array';
@@ -1,5 +1,4 @@
1
- import { JsonSchema7AnyType, parseAnyDef } from './any';
2
-
1
+ import { parseAnyDef, type JsonSchema7AnyType } from './any';
3
2
  export type JsonSchema7UndefinedType = {
4
3
  not: JsonSchema7AnyType;
5
4
  };
@@ -1,12 +1,12 @@
1
- import {
1
+ import type {
2
2
  ZodDiscriminatedUnionDef,
3
3
  ZodLiteralDef,
4
4
  ZodTypeAny,
5
5
  ZodUnionDef,
6
6
  } from 'zod/v3';
7
7
  import { parseDef } from '../parse-def';
8
- import { JsonSchema7Type } from '../parse-types';
9
- import { Refs } from '../refs';
8
+ import type { JsonSchema7Type } from '../parse-types';
9
+ import type { Refs } from '../refs';
10
10
 
11
11
  export const primitiveMappings = {
12
12
  ZodString: 'string',
@@ -1,5 +1,4 @@
1
- import { JsonSchema7AnyType, parseAnyDef } from './any';
2
-
1
+ import { parseAnyDef, type JsonSchema7AnyType } from './any';
3
2
  export type JsonSchema7UnknownType = JsonSchema7AnyType;
4
3
 
5
4
  export function parseUnknownDef(): JsonSchema7UnknownType {
@@ -1,6 +1,6 @@
1
- import { ZodTypeDef } from 'zod/v3';
2
- import { getDefaultOptions, Options } from './options';
3
- import { JsonSchema7Type } from './parse-types';
1
+ import type { ZodTypeDef } from 'zod/v3';
2
+ import { getDefaultOptions, type Options } from './options';
3
+ import type { JsonSchema7Type } from './parse-types';
4
4
 
5
5
  export type Refs = {
6
6
  seen: Map<ZodTypeDef, Seen>;
@@ -28,9 +28,9 @@ import { parseTupleDef } from './parsers/tuple';
28
28
  import { parseUndefinedDef } from './parsers/undefined';
29
29
  import { parseUnionDef } from './parsers/union';
30
30
  import { parseUnknownDef } from './parsers/unknown';
31
- import { Refs } from './refs';
31
+ import type { Refs } from './refs';
32
32
  import { parseReadonlyDef } from './parsers/readonly';
33
- import { JsonSchema7Type } from './parse-types';
33
+ import type { JsonSchema7Type } from './parse-types';
34
34
 
35
35
  export type InnerDefGetter = () => any;
36
36
 
@@ -1,7 +1,7 @@
1
- import { ZodSchema } from 'zod/v3';
2
- import { Options } from './options';
1
+ import type { ZodSchema } from 'zod/v3';
2
+ import type { Options } from './options';
3
3
  import { parseDef } from './parse-def';
4
- import { JsonSchema7Type } from './parse-types';
4
+ import type { JsonSchema7Type } from './parse-types';
5
5
  import { getRefs } from './refs';
6
6
  import { parseAnyDef } from './parsers/any';
7
7
 
@@ -1,4 +1,5 @@
1
- import {
1
+ import type {
2
+ CustomPart,
2
3
  FilePart,
3
4
  ReasoningFilePart,
4
5
  ReasoningPart,
@@ -6,8 +7,8 @@ import {
6
7
  ToolCallPart,
7
8
  ToolResultPart,
8
9
  } from './content-part';
9
- import { ProviderOptions } from './provider-options';
10
- import { ToolApprovalRequest } from './tool-approval-request';
10
+ import type { ProviderOptions } from './provider-options';
11
+ import type { ToolApprovalRequest } from './tool-approval-request';
11
12
 
12
13
  /**
13
14
  * An assistant message. It can contain text, tool calls, or a combination of text and tool calls.
@@ -32,6 +33,7 @@ export type AssistantContent =
32
33
  | string
33
34
  | Array<
34
35
  | TextPart
36
+ | CustomPart
35
37
  | FilePart
36
38
  | ReasoningPart
37
39
  | ReasoningFilePart
@@ -1,6 +1,8 @@
1
- import { JSONValue } from '@ai-sdk/provider';
2
- import { DataContent } from './data-content';
3
- import { ProviderOptions } from './provider-options';
1
+ import type { JSONValue } from '@ai-sdk/provider';
2
+ import type { DataContent } from './data-content';
3
+ import type { FileData, FileDataData, FileDataUrl } from './file-data';
4
+ import type { ProviderOptions } from './provider-options';
5
+ import type { ProviderReference } from './provider-reference';
4
6
 
5
7
  /**
6
8
  * Text content part of a prompt. It contains a string of text.
@@ -23,6 +25,9 @@ export interface TextPart {
23
25
 
24
26
  /**
25
27
  * Image content part of a prompt. It contains an image.
28
+ *
29
+ * @deprecated Use `FilePart` with `mediaType: 'image'` instead:
30
+ * `{ type: 'file', mediaType: 'image', data: { type: 'data', data } }`.
26
31
  */
27
32
  export interface ImagePart {
28
33
  type: 'image';
@@ -32,8 +37,9 @@ export interface ImagePart {
32
37
  *
33
38
  * - data: a base64-encoded string, a Uint8Array, an ArrayBuffer, or a Buffer
34
39
  * - URL: a URL that points to the image
40
+ * - ProviderReference: a provider reference from `uploadFile`
35
41
  */
36
- image: DataContent | URL;
42
+ image: DataContent | URL | ProviderReference;
37
43
 
38
44
  /**
39
45
  * Optional IANA media type of the image.
@@ -57,12 +63,16 @@ export interface FilePart {
57
63
  type: 'file';
58
64
 
59
65
  /**
60
- * File data. Can either be:
66
+ * File data. Either a tagged shape or a bare shorthand:
61
67
  *
62
- * - data: a base64-encoded string, a Uint8Array, an ArrayBuffer, or a Buffer
63
- * - URL: a URL that points to the image
68
+ * - `{ type: 'data', data }` or bare `DataContent`: raw bytes
69
+ * (base64 string, Uint8Array, ArrayBuffer, Buffer)
70
+ * - `{ type: 'url', url }` or bare `URL`: a URL that points to the file
71
+ * - `{ type: 'reference', reference }` or bare `ProviderReference`:
72
+ * a provider reference from `uploadFile`
73
+ * - `{ type: 'text', text }`: inline text content (tagged only)
64
74
  */
65
- data: DataContent | URL;
75
+ data: FileData | DataContent | URL | ProviderReference;
66
76
 
67
77
  /**
68
78
  * Optional filename of the file.
@@ -70,7 +80,14 @@ export interface FilePart {
70
80
  filename?: string;
71
81
 
72
82
  /**
73
- * IANA media type of the file.
83
+ * Either a full IANA media type (`type/subtype`, e.g. `image/png`) or just
84
+ * the top-level IANA segment (e.g. `image`, `audio`, `video`, `text`).
85
+ *
86
+ * `*`-subtype wildcards (e.g. `image/*`) are normalized as equivalent to the
87
+ * top-level segment alone (e.g. `image`). Providers can use the helpers in
88
+ * `@ai-sdk/provider-utils` (`isFullMediaType`, `getTopLevelMediaType`,
89
+ * `detectMediaType`) to resolve the field according to their API
90
+ * requirements.
74
91
  *
75
92
  * @see https://www.iana.org/assignments/media-types/media-types.xhtml
76
93
  */
@@ -103,6 +120,26 @@ export interface ReasoningPart {
103
120
  providerOptions?: ProviderOptions;
104
121
  }
105
122
 
123
+ /**
124
+ * Custom content part of a prompt. It contains no standardized payload beyond
125
+ * provider-specific options.
126
+ */
127
+ export interface CustomPart {
128
+ type: 'custom';
129
+
130
+ /**
131
+ * The kind of custom content, in the format `{provider}.{provider-type}`.
132
+ */
133
+ kind: `${string}.${string}`;
134
+
135
+ /**
136
+ * Additional provider-specific metadata. They are passed through
137
+ * to the provider from the AI SDK and enable provider-specific
138
+ * functionality that can be fully encapsulated in the provider.
139
+ */
140
+ providerOptions?: ProviderOptions;
141
+ }
142
+
106
143
  /**
107
144
  * Reasoning file content part of a prompt. It contains a file generated as part of reasoning.
108
145
  */
@@ -110,12 +147,21 @@ export interface ReasoningFilePart {
110
147
  type: 'reasoning-file';
111
148
 
112
149
  /**
113
- * File data. Can either be:
150
+ * Reasoning file data.
114
151
  *
115
- * - data: a base64-encoded string, a Uint8Array, an ArrayBuffer, or a Buffer
116
- * - URL: a URL that points to the file
152
+ * Reasoning files originate from a model's reasoning output and are always
153
+ * raw bytes or a fetchable URL. Unlike `FilePart.data`, the `reference` and
154
+ * `text` shapes are not supported here: provider references describe files
155
+ * uploaded by the user (not produced as model output), and reasoning text is
156
+ * carried by `ReasoningPart` rather than as a file.
157
+ *
158
+ * Either a tagged shape or a bare shorthand:
159
+ *
160
+ * - `{ type: 'data', data }` or bare `DataContent`: raw bytes
161
+ * (base64 string, Uint8Array, ArrayBuffer, Buffer)
162
+ * - `{ type: 'url', url }` or bare `URL`: a URL that points to the file
117
163
  */
118
- data: DataContent | URL;
164
+ data: FileDataData | FileDataUrl | DataContent | URL;
119
165
 
120
166
  /**
121
167
  * IANA media type of the file.
@@ -270,14 +316,6 @@ export type ToolResultOutput =
270
316
  */
271
317
  providerOptions?: ProviderOptions;
272
318
  }
273
- | {
274
- /**
275
- * @deprecated Use image-data or file-data instead.
276
- */
277
- type: 'media';
278
- data: string;
279
- mediaType: string;
280
- }
281
319
  | {
282
320
  type: 'file-data';
283
321
 
@@ -310,12 +348,21 @@ export type ToolResultOutput =
310
348
  */
311
349
  url: string;
312
350
 
351
+ /**
352
+ * IANA media type.
353
+ * @see https://www.iana.org/assignments/media-types/media-types.xhtml
354
+ */
355
+ mediaType?: string; // Temporarily optional. TODO: make required in v8, after migration period.
356
+
313
357
  /**
314
358
  * Provider-specific options.
315
359
  */
316
360
  providerOptions?: ProviderOptions;
317
361
  }
318
362
  | {
363
+ /**
364
+ * @deprecated Use file-reference instead.
365
+ */
319
366
  type: 'file-id';
320
367
 
321
368
  /**
@@ -328,6 +375,20 @@ export type ToolResultOutput =
328
375
  */
329
376
  fileId: string | Record<string, string>;
330
377
 
378
+ /**
379
+ * Provider-specific options.
380
+ */
381
+ providerOptions?: ProviderOptions;
382
+ }
383
+ | {
384
+ type: 'file-reference';
385
+
386
+ /**
387
+ * Provider-specific references for the file.
388
+ * The key is the provider name, e.g. 'openai' or 'anthropic'.
389
+ */
390
+ providerReference: ProviderReference;
391
+
331
392
  /**
332
393
  * Provider-specific options.
333
394
  */
@@ -335,7 +396,7 @@ export type ToolResultOutput =
335
396
  }
336
397
  | {
337
398
  /**
338
- * Images that are referenced using base64 encoded data.
399
+ * @deprecated Use file-data instead.
339
400
  */
340
401
  type: 'image-data';
341
402
 
@@ -357,7 +418,7 @@ export type ToolResultOutput =
357
418
  }
358
419
  | {
359
420
  /**
360
- * Images that are referenced using a URL.
421
+ * @deprecated Use file-url instead.
361
422
  */
362
423
  type: 'image-url';
363
424
 
@@ -373,7 +434,7 @@ export type ToolResultOutput =
373
434
  }
374
435
  | {
375
436
  /**
376
- * Images that are referenced using a provider file id.
437
+ * @deprecated Use file-reference instead.
377
438
  */
378
439
  type: 'image-file-id';
379
440
 
@@ -387,6 +448,23 @@ export type ToolResultOutput =
387
448
  */
388
449
  fileId: string | Record<string, string>;
389
450
 
451
+ /**
452
+ * Provider-specific options.
453
+ */
454
+ providerOptions?: ProviderOptions;
455
+ }
456
+ | {
457
+ /**
458
+ * @deprecated Use file-reference instead.
459
+ */
460
+ type: 'image-file-reference';
461
+
462
+ /**
463
+ * Provider-specific references for the image file.
464
+ * The key is the provider name, e.g. 'openai' or 'anthropic'.
465
+ */
466
+ providerReference: ProviderReference;
467
+
390
468
  /**
391
469
  * Provider-specific options.
392
470
  */
@@ -0,0 +1,4 @@
1
+ /**
2
+ * A context object that is passed into tool execution.
3
+ */
4
+ export type Context = Record<string, unknown>;
@@ -0,0 +1,17 @@
1
+ import type { Tool } from './tool';
2
+
3
+ /**
4
+ * A tool that is guaranteed to expose an execute function.
5
+ */
6
+ export type ExecutableTool<TOOL extends Tool = Tool> = TOOL & {
7
+ execute: NonNullable<TOOL['execute']>;
8
+ };
9
+
10
+ /**
11
+ * Checks whether a tool exposes an execute function.
12
+ */
13
+ export function isExecutableTool<TOOL extends Tool>(
14
+ tool: TOOL | undefined,
15
+ ): tool is ExecutableTool<TOOL> {
16
+ return tool != null && typeof tool.execute === 'function';
17
+ }
@@ -1,21 +1,41 @@
1
1
  import { isAsyncIterable } from '../is-async-iterable';
2
- import { ToolExecutionOptions, ToolExecuteFunction } from './tool';
2
+ import type { ExecutableTool } from './executable-tool';
3
+ import type { InferToolContext } from './infer-tool-context';
4
+ import type { InferToolInput } from './infer-tool-input';
5
+ import type { InferToolOutput } from './infer-tool-output';
6
+ import type { Tool } from './tool';
7
+ import type { ToolExecutionOptions } from './tool-execute-function';
3
8
 
4
- export async function* executeTool<INPUT, OUTPUT>({
5
- execute,
9
+ /**
10
+ * Executes a tool function and normalizes its results into a stream of outputs.
11
+ *
12
+ * - If the tool's `execute` function returns an `AsyncIterable`, each yielded value is emitted as
13
+ * `{ type: "preliminary", output }`. After iteration completes, the last yielded value is emitted
14
+ * again as `{ type: "final", output }`.
15
+ * - If the tool returns a direct value or Promise, a single `{ type: "final", output }` is yielded.
16
+ *
17
+ * @param params.tool The tool whose `execute` function should be invoked.
18
+ * @param params.input The input value to pass to the tool.
19
+ * @param params.options Additional options for tool execution.
20
+ * @yields A preliminary output for each streamed value, followed by a final output, or a single final
21
+ * output for non-streaming tools.
22
+ */
23
+ export async function* executeTool<TOOL extends Tool>({
24
+ tool,
6
25
  input,
7
26
  options,
8
27
  }: {
9
- execute: ToolExecuteFunction<INPUT, OUTPUT>;
10
- input: INPUT;
11
- options: ToolExecutionOptions;
28
+ tool: ExecutableTool<TOOL>;
29
+ input: InferToolInput<TOOL>;
30
+ options: ToolExecutionOptions<InferToolContext<TOOL>>;
12
31
  }): AsyncGenerator<
13
- { type: 'preliminary'; output: OUTPUT } | { type: 'final'; output: OUTPUT }
32
+ | { type: 'preliminary'; output: InferToolOutput<TOOL> }
33
+ | { type: 'final'; output: InferToolOutput<TOOL> }
14
34
  > {
15
- const result = execute(input, options);
35
+ const result = tool.execute(input, options);
16
36
 
17
37
  if (isAsyncIterable(result)) {
18
- let lastOutput: OUTPUT | undefined;
38
+ let lastOutput: InferToolOutput<TOOL> | undefined;
19
39
  for await (const output of result) {
20
40
  lastOutput = output;
21
41
  yield { type: 'preliminary', output };
@@ -0,0 +1,48 @@
1
+ import type {
2
+ SharedV4FileDataReference,
3
+ SharedV4FileDataText,
4
+ SharedV4FileDataUrl,
5
+ } from '@ai-sdk/provider';
6
+ import type { DataContent } from './data-content';
7
+
8
+ /**
9
+ * File data variant containing raw bytes (`Uint8Array`, `ArrayBuffer`, or
10
+ * `Buffer`) or a base64-encoded string.
11
+ *
12
+ * This is slightly more permissive than `SharedV4FileDataData`.
13
+ */
14
+ export interface FileDataData {
15
+ type: 'data';
16
+ data: DataContent;
17
+ }
18
+
19
+ /**
20
+ * File data variant containing a URL that points to the file.
21
+ */
22
+ export type FileDataUrl = SharedV4FileDataUrl;
23
+
24
+ /**
25
+ * File data variant containing a provider reference (`{ [provider]: id }`).
26
+ */
27
+ export type FileDataReference = SharedV4FileDataReference;
28
+
29
+ /**
30
+ * File data variant containing inline text content (e.g. an inline text
31
+ * document).
32
+ */
33
+ export type FileDataText = SharedV4FileDataText;
34
+
35
+ /**
36
+ * File data as a tagged discriminated union:
37
+ *
38
+ * - `{ type: 'data', data }`: raw bytes (`Uint8Array`, `ArrayBuffer`, or
39
+ * `Buffer`) or a base64-encoded string.
40
+ * - `{ type: 'url', url }`: a URL that points to the file.
41
+ * - `{ type: 'reference', reference }`: a provider reference (`{ [provider]: id }`).
42
+ * - `{ type: 'text', text }`: inline text content (e.g. an inline text document).
43
+ */
44
+ export type FileData =
45
+ | FileDataData
46
+ | FileDataUrl
47
+ | FileDataReference
48
+ | FileDataText;