@ai-sdk/provider-utils 5.0.0-beta.4 → 5.0.0-beta.49

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 (117) hide show
  1. package/CHANGELOG.md +373 -11
  2. package/dist/index.d.ts +1460 -553
  3. package/dist/index.js +1044 -361
  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 +16 -16
  9. package/src/add-additional-properties-to-json-schema.ts +1 -1
  10. package/src/as-array.ts +12 -0
  11. package/src/cancel-response-body.ts +19 -0
  12. package/src/convert-image-model-file-to-data-uri.ts +1 -1
  13. package/src/convert-inline-file-data-to-uint8-array.ts +30 -0
  14. package/src/create-tool-name-mapping.ts +1 -1
  15. package/src/detect-media-type.ts +312 -0
  16. package/src/download-blob.ts +8 -9
  17. package/src/extract-lines.ts +31 -0
  18. package/src/fetch-with-validated-redirects.ts +87 -0
  19. package/src/filter-nullable.ts +11 -0
  20. package/src/get-error-message.ts +1 -15
  21. package/src/get-from-api.ts +2 -2
  22. package/src/has-required-key.ts +6 -0
  23. package/src/index.ts +47 -12
  24. package/src/inject-json-instruction.ts +1 -1
  25. package/src/is-browser-runtime.ts +13 -0
  26. package/src/is-buffer.ts +9 -0
  27. package/src/is-json-serializable.ts +29 -0
  28. package/src/is-provider-reference.ts +21 -0
  29. package/src/is-same-origin.ts +19 -0
  30. package/src/is-url-supported.ts +17 -2
  31. package/src/load-api-key.ts +1 -1
  32. package/src/load-setting.ts +1 -1
  33. package/src/map-reasoning-to-provider.ts +108 -0
  34. package/src/maybe-promise-like.ts +3 -0
  35. package/src/parse-json-event-stream.ts +3 -3
  36. package/src/parse-json.ts +3 -3
  37. package/src/parse-provider-options.ts +1 -1
  38. package/src/post-to-api.ts +4 -4
  39. package/src/provider-defined-tool-factory.ts +129 -0
  40. package/src/provider-executed-tool-factory.ts +69 -0
  41. package/src/read-response-with-size-limit.ts +4 -0
  42. package/src/resolve-full-media-type.ts +49 -0
  43. package/src/resolve-provider-reference.ts +26 -0
  44. package/src/resolve.ts +16 -1
  45. package/src/response-handler.ts +3 -3
  46. package/src/schema.ts +11 -4
  47. package/src/secure-json-parse.ts +1 -1
  48. package/src/serialize-model-options.ts +63 -0
  49. package/src/streaming-tool-call-tracker.ts +241 -0
  50. package/src/test/convert-response-stream-to-array.ts +1 -1
  51. package/src/test/is-node-version.ts +22 -1
  52. package/src/to-json-schema/zod3-to-json-schema/options.ts +3 -3
  53. package/src/to-json-schema/zod3-to-json-schema/parse-def.ts +3 -3
  54. package/src/to-json-schema/zod3-to-json-schema/parse-types.ts +22 -22
  55. package/src/to-json-schema/zod3-to-json-schema/parsers/array.ts +3 -3
  56. package/src/to-json-schema/zod3-to-json-schema/parsers/bigint.ts +1 -1
  57. package/src/to-json-schema/zod3-to-json-schema/parsers/branded.ts +2 -2
  58. package/src/to-json-schema/zod3-to-json-schema/parsers/catch.ts +2 -2
  59. package/src/to-json-schema/zod3-to-json-schema/parsers/date.ts +4 -4
  60. package/src/to-json-schema/zod3-to-json-schema/parsers/default.ts +3 -3
  61. package/src/to-json-schema/zod3-to-json-schema/parsers/effects.ts +3 -3
  62. package/src/to-json-schema/zod3-to-json-schema/parsers/enum.ts +1 -1
  63. package/src/to-json-schema/zod3-to-json-schema/parsers/intersection.ts +5 -5
  64. package/src/to-json-schema/zod3-to-json-schema/parsers/literal.ts +1 -1
  65. package/src/to-json-schema/zod3-to-json-schema/parsers/map.ts +4 -5
  66. package/src/to-json-schema/zod3-to-json-schema/parsers/native-enum.ts +1 -1
  67. package/src/to-json-schema/zod3-to-json-schema/parsers/never.ts +1 -2
  68. package/src/to-json-schema/zod3-to-json-schema/parsers/nullable.ts +4 -4
  69. package/src/to-json-schema/zod3-to-json-schema/parsers/number.ts +1 -1
  70. package/src/to-json-schema/zod3-to-json-schema/parsers/object.ts +3 -3
  71. package/src/to-json-schema/zod3-to-json-schema/parsers/optional.ts +3 -3
  72. package/src/to-json-schema/zod3-to-json-schema/parsers/pipeline.ts +10 -8
  73. package/src/to-json-schema/zod3-to-json-schema/parsers/promise.ts +3 -3
  74. package/src/to-json-schema/zod3-to-json-schema/parsers/readonly.ts +2 -2
  75. package/src/to-json-schema/zod3-to-json-schema/parsers/record.ts +9 -10
  76. package/src/to-json-schema/zod3-to-json-schema/parsers/set.ts +3 -3
  77. package/src/to-json-schema/zod3-to-json-schema/parsers/string.ts +2 -2
  78. package/src/to-json-schema/zod3-to-json-schema/parsers/tuple.ts +3 -3
  79. package/src/to-json-schema/zod3-to-json-schema/parsers/undefined.ts +1 -2
  80. package/src/to-json-schema/zod3-to-json-schema/parsers/union.ts +3 -3
  81. package/src/to-json-schema/zod3-to-json-schema/parsers/unknown.ts +1 -2
  82. package/src/to-json-schema/zod3-to-json-schema/refs.ts +3 -3
  83. package/src/to-json-schema/zod3-to-json-schema/select-parser.ts +2 -2
  84. package/src/to-json-schema/zod3-to-json-schema/zod3-to-json-schema.ts +3 -3
  85. package/src/types/assistant-model-message.ts +5 -3
  86. package/src/types/content-part.ts +158 -19
  87. package/src/types/context.ts +4 -0
  88. package/src/types/executable-tool.ts +17 -0
  89. package/src/types/execute-tool.ts +29 -9
  90. package/src/types/file-data.ts +48 -0
  91. package/src/types/index.ts +29 -11
  92. package/src/types/infer-tool-context.ts +41 -0
  93. package/src/types/infer-tool-input.ts +7 -0
  94. package/src/types/infer-tool-output.ts +7 -0
  95. package/src/types/infer-tool-set-context.ts +44 -0
  96. package/src/types/model-message.ts +4 -4
  97. package/src/types/never-optional.ts +7 -0
  98. package/src/types/provider-options.ts +1 -1
  99. package/src/types/provider-reference.ts +10 -0
  100. package/src/types/sandbox.ts +217 -0
  101. package/src/types/system-model-message.ts +1 -1
  102. package/src/types/tool-approval-request.ts +13 -0
  103. package/src/types/tool-execute-function.ts +56 -0
  104. package/src/types/tool-model-message.ts +3 -3
  105. package/src/types/tool-needs-approval-function.ts +39 -0
  106. package/src/types/tool-set.ts +22 -0
  107. package/src/types/tool.ts +278 -225
  108. package/src/types/user-model-message.ts +2 -2
  109. package/src/validate-download-url.ts +120 -33
  110. package/src/validate-types.ts +5 -3
  111. package/dist/index.d.mts +0 -1455
  112. package/dist/index.mjs +0 -2754
  113. package/dist/index.mjs.map +0 -1
  114. package/dist/test/index.d.mts +0 -17
  115. package/dist/test/index.mjs +0 -77
  116. package/dist/test/index.mjs.map +0 -1
  117. package/src/provider-tool-factory.ts +0 -125
@@ -0,0 +1,217 @@
1
+ /**
2
+ * Options for executing a command in the sandbox via `run` or `spawn`.
3
+ */
4
+ type SandboxProcessOptions = {
5
+ /**
6
+ * Command to execute in the sandbox.
7
+ */
8
+ command: string;
9
+
10
+ /**
11
+ * Working directory to execute the command in.
12
+ */
13
+ workingDirectory?: string;
14
+
15
+ /**
16
+ * Environment variables to set for this command. Merged with the
17
+ * sandbox's default environment; values here take precedence.
18
+ * Supporting environment variables as an option is preferable from a
19
+ * security perspective, e.g. to avoid them leaking in logs.
20
+ */
21
+ env?: Record<string, string>;
22
+
23
+ /**
24
+ * Signal that can be used to abort the command. When aborted, the running
25
+ * process is killed; for `spawn`, `wait()` rejects with the abort reason.
26
+ */
27
+ abortSignal?: AbortSignal;
28
+ };
29
+
30
+ /**
31
+ * Options for reading a file from the sandbox.
32
+ */
33
+ type ReadFileOptions = {
34
+ /**
35
+ * Path of the file to read.
36
+ */
37
+ path: string;
38
+
39
+ /**
40
+ * Signal that can be used to abort the read.
41
+ */
42
+ abortSignal?: AbortSignal;
43
+ };
44
+
45
+ /**
46
+ * Options for writing a file to the sandbox. `CONTENT` is the payload written
47
+ * to the file: a byte stream, raw bytes, or a string.
48
+ */
49
+ type WriteFileOptions<CONTENT> = {
50
+ /**
51
+ * Path of the file to write.
52
+ */
53
+ path: string;
54
+
55
+ /**
56
+ * Content to write to the file.
57
+ */
58
+ content: CONTENT;
59
+
60
+ /**
61
+ * Signal that can be used to abort the write.
62
+ */
63
+ abortSignal?: AbortSignal;
64
+ };
65
+
66
+ /**
67
+ * Sandbox session that can execute commands and read/write files.
68
+ */
69
+ export type SandboxSession = {
70
+ /**
71
+ * Description of the sandbox environment that can be added to the agent's instructions
72
+ * so that the agent knows about relevant details such as the root directory, exposed
73
+ * ports, the public hostname, etc.
74
+ */
75
+ readonly description: string;
76
+
77
+ /**
78
+ * Read one file from the sandbox as a stream of bytes. Resolves to `null`
79
+ * when the file does not exist.
80
+ *
81
+ * Relative path handling is implementation-defined. This is the lowest-level
82
+ * read primitive; prefer `readBinaryFile` or `readTextFile` unless you need
83
+ * to stream bytes.
84
+ */
85
+ readonly readFile: (
86
+ options: ReadFileOptions,
87
+ ) => PromiseLike<ReadableStream<Uint8Array> | null>;
88
+
89
+ /**
90
+ * Read one file from the sandbox as raw bytes. Resolves to `null` when the
91
+ * file does not exist.
92
+ */
93
+ readonly readBinaryFile: (
94
+ options: ReadFileOptions,
95
+ ) => PromiseLike<Uint8Array | null>;
96
+
97
+ /**
98
+ * Read one text file from the sandbox, decoded using the requested encoding.
99
+ * Resolves to `null` when the file does not exist.
100
+ *
101
+ * Line ranges are 1-based and inclusive. When `endLine` is past EOF the read
102
+ * returns through EOF without error.
103
+ */
104
+ readonly readTextFile: (
105
+ options: ReadFileOptions & {
106
+ /**
107
+ * Text encoding used to decode the file bytes. Defaults to `"utf-8"`.
108
+ */
109
+ encoding?: string;
110
+
111
+ /**
112
+ * 1-based inclusive start line. Defaults to 1.
113
+ */
114
+ startLine?: number;
115
+
116
+ /**
117
+ * 1-based inclusive end line. When past the file's line count, the read
118
+ * returns through EOF without error.
119
+ */
120
+ endLine?: number;
121
+ },
122
+ ) => PromiseLike<string | null>;
123
+
124
+ /**
125
+ * Write one file to the sandbox from a stream of bytes. Creates parent
126
+ * directories recursively and overwrites any existing file.
127
+ *
128
+ * This is the lowest-level write primitive; prefer `writeBinaryFile` or
129
+ * `writeTextFile` when the full content is already materialized in memory.
130
+ */
131
+ readonly writeFile: (
132
+ options: WriteFileOptions<ReadableStream<Uint8Array>>,
133
+ ) => PromiseLike<void>;
134
+
135
+ /**
136
+ * Write one file to the sandbox from raw bytes. Creates parent directories
137
+ * recursively and overwrites any existing file.
138
+ */
139
+ readonly writeBinaryFile: (
140
+ options: WriteFileOptions<Uint8Array>,
141
+ ) => PromiseLike<void>;
142
+
143
+ /**
144
+ * Write one file to the sandbox from a string, encoded using the requested
145
+ * encoding. Creates parent directories recursively and overwrites any
146
+ * existing file.
147
+ */
148
+ readonly writeTextFile: (
149
+ options: WriteFileOptions<string> & {
150
+ /**
151
+ * Text encoding used to encode the string to bytes. Defaults to `"utf-8"`.
152
+ */
153
+ encoding?: string;
154
+ },
155
+ ) => PromiseLike<void>;
156
+
157
+ /**
158
+ * Spawn a long-running process in the sandbox. Returns immediately with a
159
+ * handle that streams stdout/stderr, can be waited on, and can be killed.
160
+ *
161
+ * `run` is conceptually a thin wrapper over this primitive: spawn,
162
+ * collect both streams to strings, await `wait()`, return the result.
163
+ */
164
+ readonly spawn: (
165
+ options: SandboxProcessOptions,
166
+ ) => PromiseLike<SandboxProcess>;
167
+
168
+ /**
169
+ * Run a command in the sandbox.
170
+ */
171
+ readonly run: (options: SandboxProcessOptions) => PromiseLike<{
172
+ /**
173
+ * Exit code returned by the command.
174
+ */
175
+ exitCode: number;
176
+
177
+ /**
178
+ * Standard output produced by the command.
179
+ */
180
+ stdout: string;
181
+
182
+ /**
183
+ * Standard error produced by the command.
184
+ */
185
+ stderr: string;
186
+ }>;
187
+ };
188
+
189
+ /**
190
+ * Handle to a long-running process started via `SandboxSession.spawn`.
191
+ */
192
+ export type SandboxProcess = {
193
+ /**
194
+ * Process identifier, if the sandbox implementation exposes one.
195
+ */
196
+ readonly pid?: number;
197
+
198
+ /**
199
+ * Stream of bytes written by the process to standard output.
200
+ */
201
+ readonly stdout: ReadableStream<Uint8Array>;
202
+
203
+ /**
204
+ * Stream of bytes written by the process to standard error.
205
+ */
206
+ readonly stderr: ReadableStream<Uint8Array>;
207
+
208
+ /**
209
+ * Resolve when the process exits, yielding its exit code.
210
+ */
211
+ wait(): PromiseLike<{ exitCode: number }>;
212
+
213
+ /**
214
+ * Terminate the process. Idempotent.
215
+ */
216
+ kill(): PromiseLike<void>;
217
+ };
@@ -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.
@@ -13,4 +13,17 @@ export type ToolApprovalRequest = {
13
13
  * ID of the tool call that the approval request is for.
14
14
  */
15
15
  toolCallId: string;
16
+
17
+ /**
18
+ * Flag indicating whether the tool was automatically approved or denied.
19
+ *
20
+ * @default false
21
+ */
22
+ isAutomatic?: boolean;
23
+
24
+ /**
25
+ * HMAC-SHA256 signature binding this approval to its tool call.
26
+ * Present only when `experimental_toolApprovalSecret` is configured.
27
+ */
28
+ signature?: string;
16
29
  };
@@ -0,0 +1,56 @@
1
+ import type { Context } from './context';
2
+ import type { ModelMessage } from './model-message';
3
+ import type { SandboxSession } from './sandbox';
4
+
5
+ /**
6
+ * Additional options that are sent into each tool execution.
7
+ */
8
+ export interface ToolExecutionOptions<
9
+ CONTEXT extends Context | unknown | never,
10
+ > {
11
+ /**
12
+ * The ID of the tool call. You can use it e.g. when sending tool-call related information with stream data.
13
+ */
14
+ toolCallId: string;
15
+
16
+ /**
17
+ * Messages that were sent to the language model to initiate the response that contained the tool call.
18
+ * The messages **do not** include the system prompt nor the assistant response that contained the tool call.
19
+ */
20
+ messages: ModelMessage[];
21
+
22
+ /**
23
+ * An optional abort signal that indicates that the overall operation should be aborted.
24
+ */
25
+ abortSignal?: AbortSignal;
26
+
27
+ /**
28
+ * Tool context as defined by the tool's context schema.
29
+ * The tool context is specific to the tool and is passed to the tool execution.
30
+ *
31
+ * Treat the context object as immutable inside tools.
32
+ * Mutating the context object can lead to race conditions and unexpected results
33
+ * when tools are called in parallel.
34
+ *
35
+ * If you need to mutate the context, analyze the tool calls and results
36
+ * in `prepareStep` and update it there.
37
+ */
38
+ context: CONTEXT;
39
+
40
+ /**
41
+ * The sandbox environment that the tool is operating in.
42
+ */
43
+ experimental_sandbox?: SandboxSession;
44
+ }
45
+
46
+ /**
47
+ * Function that executes the tool and returns either a single result or a stream of results.
48
+ */
49
+ export type ToolExecuteFunction<
50
+ INPUT,
51
+ OUTPUT,
52
+ CONTEXT extends Context | unknown | never,
53
+ > = (
54
+ input: INPUT,
55
+ options: ToolExecutionOptions<CONTEXT>,
56
+ ) => 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>;
@@ -0,0 +1,22 @@
1
+ import type { Tool } from './tool';
2
+
3
+ /**
4
+ * A mapping of tool names to tool definitions.
5
+ */
6
+ export type ToolSet = Record<
7
+ string,
8
+ (
9
+ | Tool<never, never, any>
10
+ | Tool<any, any, any>
11
+ | Tool<any, never, any>
12
+ | Tool<never, any, any>
13
+ ) &
14
+ Pick<
15
+ Tool<any, any, any>,
16
+ | 'execute'
17
+ | 'onInputAvailable'
18
+ | 'onInputStart'
19
+ | 'onInputDelta'
20
+ | 'needsApproval'
21
+ >
22
+ >;