@ai-sdk/provider-utils 5.0.0-beta.28 → 5.0.0-beta.29

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 (82) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/dist/index.d.ts +967 -843
  3. package/dist/index.js +340 -4
  4. package/dist/index.js.map +1 -1
  5. package/package.json +2 -2
  6. package/src/add-additional-properties-to-json-schema.ts +1 -1
  7. package/src/convert-image-model-file-to-data-uri.ts +1 -1
  8. package/src/convert-inline-file-data-to-uint8-array.ts +30 -0
  9. package/src/create-tool-name-mapping.ts +1 -1
  10. package/src/detect-media-type.ts +312 -0
  11. package/src/get-from-api.ts +2 -2
  12. package/src/index.ts +8 -0
  13. package/src/inject-json-instruction.ts +1 -1
  14. package/src/is-buffer.ts +9 -0
  15. package/src/is-json-serializable.ts +1 -1
  16. package/src/is-provider-reference.ts +11 -8
  17. package/src/is-url-supported.ts +17 -2
  18. package/src/map-reasoning-to-provider.ts +4 -1
  19. package/src/parse-json-event-stream.ts +3 -3
  20. package/src/parse-json.ts +2 -2
  21. package/src/parse-provider-options.ts +1 -1
  22. package/src/post-to-api.ts +2 -2
  23. package/src/provider-defined-tool-factory.ts +10 -9
  24. package/src/provider-executed-tool-factory.ts +6 -7
  25. package/src/resolve-full-media-type.ts +49 -0
  26. package/src/resolve-provider-reference.ts +1 -2
  27. package/src/resolve.ts +1 -1
  28. package/src/response-handler.ts +2 -2
  29. package/src/schema.ts +6 -3
  30. package/src/serialize-model-options.ts +2 -2
  31. package/src/streaming-tool-call-tracker.ts +2 -2
  32. package/src/to-json-schema/zod3-to-json-schema/options.ts +3 -3
  33. package/src/to-json-schema/zod3-to-json-schema/parse-def.ts +3 -3
  34. package/src/to-json-schema/zod3-to-json-schema/parse-types.ts +22 -22
  35. package/src/to-json-schema/zod3-to-json-schema/parsers/array.ts +3 -3
  36. package/src/to-json-schema/zod3-to-json-schema/parsers/bigint.ts +1 -1
  37. package/src/to-json-schema/zod3-to-json-schema/parsers/branded.ts +2 -2
  38. package/src/to-json-schema/zod3-to-json-schema/parsers/catch.ts +2 -2
  39. package/src/to-json-schema/zod3-to-json-schema/parsers/date.ts +3 -3
  40. package/src/to-json-schema/zod3-to-json-schema/parsers/default.ts +3 -3
  41. package/src/to-json-schema/zod3-to-json-schema/parsers/effects.ts +3 -3
  42. package/src/to-json-schema/zod3-to-json-schema/parsers/enum.ts +1 -1
  43. package/src/to-json-schema/zod3-to-json-schema/parsers/intersection.ts +4 -4
  44. package/src/to-json-schema/zod3-to-json-schema/parsers/literal.ts +1 -1
  45. package/src/to-json-schema/zod3-to-json-schema/parsers/map.ts +4 -5
  46. package/src/to-json-schema/zod3-to-json-schema/parsers/native-enum.ts +1 -1
  47. package/src/to-json-schema/zod3-to-json-schema/parsers/never.ts +1 -2
  48. package/src/to-json-schema/zod3-to-json-schema/parsers/nullable.ts +4 -4
  49. package/src/to-json-schema/zod3-to-json-schema/parsers/number.ts +1 -1
  50. package/src/to-json-schema/zod3-to-json-schema/parsers/object.ts +3 -3
  51. package/src/to-json-schema/zod3-to-json-schema/parsers/optional.ts +3 -3
  52. package/src/to-json-schema/zod3-to-json-schema/parsers/pipeline.ts +4 -4
  53. package/src/to-json-schema/zod3-to-json-schema/parsers/promise.ts +3 -3
  54. package/src/to-json-schema/zod3-to-json-schema/parsers/readonly.ts +2 -2
  55. package/src/to-json-schema/zod3-to-json-schema/parsers/record.ts +7 -8
  56. package/src/to-json-schema/zod3-to-json-schema/parsers/set.ts +3 -3
  57. package/src/to-json-schema/zod3-to-json-schema/parsers/string.ts +2 -2
  58. package/src/to-json-schema/zod3-to-json-schema/parsers/tuple.ts +3 -3
  59. package/src/to-json-schema/zod3-to-json-schema/parsers/undefined.ts +1 -2
  60. package/src/to-json-schema/zod3-to-json-schema/parsers/union.ts +3 -3
  61. package/src/to-json-schema/zod3-to-json-schema/parsers/unknown.ts +1 -2
  62. package/src/to-json-schema/zod3-to-json-schema/refs.ts +3 -3
  63. package/src/to-json-schema/zod3-to-json-schema/select-parser.ts +2 -2
  64. package/src/to-json-schema/zod3-to-json-schema/zod3-to-json-schema.ts +3 -3
  65. package/src/types/assistant-model-message.ts +3 -3
  66. package/src/types/content-part.ts +37 -14
  67. package/src/types/executable-tool.ts +1 -1
  68. package/src/types/execute-tool.ts +6 -5
  69. package/src/types/file-data.ts +48 -0
  70. package/src/types/index.ts +16 -3
  71. package/src/types/infer-tool-context.ts +1 -1
  72. package/src/types/model-message.ts +4 -4
  73. package/src/types/never-optional.ts +7 -0
  74. package/src/types/provider-options.ts +1 -1
  75. package/src/types/provider-reference.ts +1 -1
  76. package/src/types/system-model-message.ts +1 -1
  77. package/src/types/tool-execute-function.ts +50 -0
  78. package/src/types/tool-model-message.ts +3 -3
  79. package/src/types/tool-needs-approval-function.ts +39 -0
  80. package/src/types/tool.ts +200 -271
  81. package/src/types/user-model-message.ts +2 -2
  82. package/src/validate-types.ts +5 -3
@@ -1,7 +1,8 @@
1
- import { JSONValue } from '@ai-sdk/provider';
2
- import { DataContent } from './data-content';
3
- import { ProviderOptions } from './provider-options';
4
- import { ProviderReference } from './provider-reference';
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';
5
6
 
6
7
  /**
7
8
  * Text content part of a prompt. It contains a string of text.
@@ -24,6 +25,9 @@ export interface TextPart {
24
25
 
25
26
  /**
26
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 } }`.
27
31
  */
28
32
  export interface ImagePart {
29
33
  type: 'image';
@@ -59,13 +63,16 @@ export interface FilePart {
59
63
  type: 'file';
60
64
 
61
65
  /**
62
- * File data. Can either be:
66
+ * File data. Either a tagged shape or a bare shorthand:
63
67
  *
64
- * - data: a base64-encoded string, a Uint8Array, an ArrayBuffer, or a Buffer
65
- * - URL: a URL that points to the file
66
- * - ProviderReference: a provider reference from `uploadFile`
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)
67
74
  */
68
- data: DataContent | URL | ProviderReference;
75
+ data: FileData | DataContent | URL | ProviderReference;
69
76
 
70
77
  /**
71
78
  * Optional filename of the file.
@@ -73,7 +80,14 @@ export interface FilePart {
73
80
  filename?: string;
74
81
 
75
82
  /**
76
- * 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.
77
91
  *
78
92
  * @see https://www.iana.org/assignments/media-types/media-types.xhtml
79
93
  */
@@ -133,12 +147,21 @@ export interface ReasoningFilePart {
133
147
  type: 'reasoning-file';
134
148
 
135
149
  /**
136
- * File data. Can either be:
150
+ * Reasoning file data.
137
151
  *
138
- * - data: a base64-encoded string, a Uint8Array, an ArrayBuffer, or a Buffer
139
- * - 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
140
163
  */
141
- data: DataContent | URL;
164
+ data: FileDataData | FileDataUrl | DataContent | URL;
142
165
 
143
166
  /**
144
167
  * IANA media type of the file.
@@ -1,4 +1,4 @@
1
- import { Tool } from './tool';
1
+ import type { Tool } from './tool';
2
2
 
3
3
  /**
4
4
  * A tool that is guaranteed to expose an execute function.
@@ -1,9 +1,10 @@
1
1
  import { isAsyncIterable } from '../is-async-iterable';
2
- import { ExecutableTool } from './executable-tool';
3
- import { InferToolContext } from './infer-tool-context';
4
- import { InferToolInput } from './infer-tool-input';
5
- import { InferToolOutput } from './infer-tool-output';
6
- import { Tool, ToolExecutionOptions } 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';
7
8
 
8
9
  /**
9
10
  * Executes a tool function and normalizes its results into a stream of outputs.
@@ -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;
@@ -16,6 +16,13 @@ export type {
16
16
  export type { Context } from './context';
17
17
  export type { DataContent } from './data-content';
18
18
  export { executeTool } from './execute-tool';
19
+ export type {
20
+ FileData,
21
+ FileDataData,
22
+ FileDataReference,
23
+ FileDataText,
24
+ FileDataUrl,
25
+ } from './file-data';
19
26
  export { isExecutableTool, type ExecutableTool } from './executable-tool';
20
27
  export type { InferToolContext } from './infer-tool-context';
21
28
  export type { InferToolInput } from './infer-tool-input';
@@ -29,11 +36,17 @@ export type { SystemModelMessage } from './system-model-message';
29
36
  export {
30
37
  dynamicTool,
31
38
  tool,
39
+ type DynamicTool,
40
+ type FunctionTool,
41
+ type ProviderDefinedTool,
42
+ type ProviderExecutedTool,
32
43
  type Tool,
33
- type ToolExecuteFunction,
34
- type ToolExecutionOptions,
35
- type ToolNeedsApprovalFunction,
36
44
  } from './tool';
45
+ export type {
46
+ ToolExecuteFunction,
47
+ ToolExecutionOptions,
48
+ } from './tool-execute-function';
49
+ export type { ToolNeedsApprovalFunction } from './tool-needs-approval-function';
37
50
  export type { ToolSet } from './tool-set';
38
51
  export type { ToolApprovalRequest } from './tool-approval-request';
39
52
  export type { ToolApprovalResponse } from './tool-approval-response';
@@ -1,4 +1,4 @@
1
- import { HasRequiredKey } from '../has-required-key';
1
+ import type { HasRequiredKey } from '../has-required-key';
2
2
  import type { Tool } from './tool';
3
3
 
4
4
  /**
@@ -1,7 +1,7 @@
1
- import { AssistantModelMessage } from './assistant-model-message';
2
- import { SystemModelMessage } from './system-model-message';
3
- import { ToolModelMessage } from './tool-model-message';
4
- import { UserModelMessage } from './user-model-message';
1
+ import type { AssistantModelMessage } from './assistant-model-message';
2
+ import type { SystemModelMessage } from './system-model-message';
3
+ import type { ToolModelMessage } from './tool-model-message';
4
+ import type { UserModelMessage } from './user-model-message';
5
5
 
6
6
  /**
7
7
  * A message that can be used in the `messages` field of a prompt.
@@ -0,0 +1,7 @@
1
+ // 0 extends 1 & N checks for any
2
+ // [N] extends [never] checks for never
3
+ export type NeverOptional<N, T> = 0 extends 1 & N
4
+ ? Partial<T>
5
+ : [N] extends [never]
6
+ ? Partial<Record<keyof T, undefined>>
7
+ : T;
@@ -1,4 +1,4 @@
1
- import { SharedV4ProviderOptions } from '@ai-sdk/provider';
1
+ import type { SharedV4ProviderOptions } from '@ai-sdk/provider';
2
2
 
3
3
  /**
4
4
  * Additional provider-specific options.
@@ -1,4 +1,4 @@
1
- import { SharedV4ProviderReference } from '@ai-sdk/provider';
1
+ import type { SharedV4ProviderReference } from '@ai-sdk/provider';
2
2
 
3
3
  /**
4
4
  * A mapping of provider names to provider-specific file identifiers.
@@ -1,4 +1,4 @@
1
- import { ProviderOptions } from './provider-options';
1
+ import type { ProviderOptions } from './provider-options';
2
2
 
3
3
  /**
4
4
  * A system message. It can contain system information.
@@ -0,0 +1,50 @@
1
+ import type { Context } from './context';
2
+ import type { ModelMessage } from './model-message';
3
+
4
+ /**
5
+ * Additional options that are sent into each tool execution.
6
+ */
7
+ export interface ToolExecutionOptions<
8
+ CONTEXT extends Context | unknown | never,
9
+ > {
10
+ /**
11
+ * The ID of the tool call. You can use it e.g. when sending tool-call related information with stream data.
12
+ */
13
+ toolCallId: string;
14
+
15
+ /**
16
+ * Messages that were sent to the language model to initiate the response that contained the tool call.
17
+ * The messages **do not** include the system prompt nor the assistant response that contained the tool call.
18
+ */
19
+ messages: ModelMessage[];
20
+
21
+ /**
22
+ * An optional abort signal that indicates that the overall operation should be aborted.
23
+ */
24
+ abortSignal?: AbortSignal;
25
+
26
+ /**
27
+ * Tool context as defined by the tool's context schema.
28
+ * The tool context is specific to the tool and is passed to the tool execution.
29
+ *
30
+ * Treat the context object as immutable inside tools.
31
+ * Mutating the context object can lead to race conditions and unexpected results
32
+ * when tools are called in parallel.
33
+ *
34
+ * If you need to mutate the context, analyze the tool calls and results
35
+ * in `prepareStep` and update it there.
36
+ */
37
+ context: CONTEXT;
38
+ }
39
+
40
+ /**
41
+ * Function that executes the tool and returns either a single result or a stream of results.
42
+ */
43
+ export type ToolExecuteFunction<
44
+ INPUT,
45
+ OUTPUT,
46
+ CONTEXT extends Context | unknown | never,
47
+ > = (
48
+ input: INPUT,
49
+ options: ToolExecutionOptions<CONTEXT>,
50
+ ) => AsyncIterable<OUTPUT> | PromiseLike<OUTPUT> | OUTPUT;
@@ -1,6 +1,6 @@
1
- import { ToolResultPart } from './content-part';
2
- import { ProviderOptions } from './provider-options';
3
- import { ToolApprovalResponse } from './tool-approval-response';
1
+ import type { ToolResultPart } from './content-part';
2
+ import type { ProviderOptions } from './provider-options';
3
+ import type { ToolApprovalResponse } from './tool-approval-response';
4
4
 
5
5
  /**
6
6
  * A tool message. It contains the result of one or more tool calls.
@@ -0,0 +1,39 @@
1
+ import type { Context } from './context';
2
+ import type { ModelMessage } from './model-message';
3
+
4
+ /**
5
+ * Function that is called to determine if the tool needs approval before it can be executed.
6
+ *
7
+ * @deprecated Tool approval is handled on a `generateText` / `streamText` level now.
8
+ */
9
+ export type ToolNeedsApprovalFunction<
10
+ INPUT,
11
+ CONTEXT extends Context | unknown | never,
12
+ > = (
13
+ input: INPUT,
14
+ options: {
15
+ /**
16
+ * The ID of the tool call. You can use it e.g. when sending tool-call related information with stream data.
17
+ */
18
+ toolCallId: string;
19
+
20
+ /**
21
+ * Messages that were sent to the language model to initiate the response that contained the tool call.
22
+ * The messages **do not** include the system prompt nor the assistant response that contained the tool call.
23
+ */
24
+ messages: ModelMessage[];
25
+
26
+ /**
27
+ * Tool context as defined by the tool's context schema.
28
+ * The tool context is specific to the tool and is passed to the tool execution.
29
+ *
30
+ * Treat the context object as immutable inside tools.
31
+ * Mutating the context object can lead to race conditions and unexpected results
32
+ * when tools are called in parallel.
33
+ *
34
+ * If you need to mutate the context, analyze the tool calls and results
35
+ * in `prepareStep` and update it there.
36
+ */
37
+ context: CONTEXT;
38
+ },
39
+ ) => boolean | PromiseLike<boolean>;