@ai-sdk/provider-utils 5.0.69 → 5.0.71

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-sdk/provider-utils",
3
- "version": "5.0.69",
3
+ "version": "5.0.71",
4
4
  "type": "module",
5
5
  "license": "Apache-2.0",
6
6
  "sideEffects": false,
@@ -8,36 +8,73 @@ export type ReasoningLevel = Exclude<
8
8
  'none' | 'provider-default' | undefined
9
9
  >;
10
10
 
11
+ /**
12
+ * Reasoning levels in ascending order.
13
+ */
14
+ const REASONING_LEVELS: readonly ReasoningLevel[] = [
15
+ 'minimal',
16
+ 'low',
17
+ 'medium',
18
+ 'high',
19
+ 'xhigh',
20
+ 'max',
21
+ ];
22
+
11
23
  export function isCustomReasoning(
12
24
  reasoning: LanguageModelV4CallOptions['reasoning'],
13
- ): reasoning is Exclude<
14
- LanguageModelV4CallOptions['reasoning'],
15
- 'provider-default' | undefined
16
- > {
25
+ ): reasoning is ReasoningLevel | 'none' {
17
26
  return reasoning !== undefined && reasoning !== 'provider-default';
18
27
  }
19
28
 
20
29
  /**
21
- * Maps a top-level reasoning level to a provider-specific effort string using
22
- * the given effort map. Pushes a compatibility warning if the reasoning level
23
- * maps to a different string, or an unsupported warning if the level is not
24
- * present in the map.
30
+ * Maps a top-level reasoning value to a provider-specific effort string.
31
+ *
32
+ * A `providerOptionsEffort` (the reasoning effort set in provider options) is
33
+ * returned unchanged. Otherwise, `undefined` and `'provider-default'` return
34
+ * `undefined`, and other reasoning values are mapped with the effort map.
25
35
  *
26
- * @returns The mapped effort string, or `undefined` if the level is not
27
- * supported.
36
+ * The effort map should only contain the values that the model supports. An
37
+ * unsupported value maps to the next lower supported level, or the next higher
38
+ * one if there is no lower level. `'none'` is never used as a fallback, so an
39
+ * unsupported `'none'` maps to the lowest supported level.
40
+ *
41
+ * Pushes a compatibility warning if the effort that is sent differs from the
42
+ * requested value, or an unsupported warning if the effort map is empty.
43
+ *
44
+ * @returns The provider options effort, the mapped effort string, or
45
+ * `undefined` if no effort should be sent.
28
46
  */
29
- export function mapReasoningToProviderEffort<T extends string>({
47
+ export function mapReasoningToProviderEffort<
48
+ T extends string,
49
+ P extends string = never,
50
+ >({
30
51
  reasoning,
31
52
  effortMap,
53
+ providerOptionsEffort,
32
54
  warnings,
33
55
  }: {
34
- reasoning: ReasoningLevel;
35
- effortMap: Partial<Record<ReasoningLevel, T>>;
56
+ reasoning: LanguageModelV4CallOptions['reasoning'];
57
+ effortMap: Partial<Record<ReasoningLevel | 'none', T>>;
58
+ providerOptionsEffort?: P | null;
36
59
  warnings: SharedV4Warning[];
37
- }): T | undefined {
38
- const mapped = effortMap[reasoning];
60
+ }): T | P | undefined {
61
+ if (providerOptionsEffort != null) {
62
+ return providerOptionsEffort;
63
+ }
64
+
65
+ if (!isCustomReasoning(reasoning)) {
66
+ return undefined;
67
+ }
68
+
69
+ // indexOf is -1 for 'none', so every level is a higher fallback.
70
+ const index = REASONING_LEVELS.indexOf(reasoning as ReasoningLevel);
71
+ const level = [
72
+ reasoning,
73
+ ...REASONING_LEVELS.slice(0, Math.max(index, 0)).reverse(),
74
+ ...REASONING_LEVELS.slice(index + 1),
75
+ ].find(candidate => effortMap[candidate] != null);
39
76
 
40
- if (mapped == null) {
77
+ if (level == null) {
41
78
  warnings.push({
42
79
  type: 'unsupported',
43
80
  feature: 'reasoning',
@@ -46,6 +83,8 @@ export function mapReasoningToProviderEffort<T extends string>({
46
83
  return undefined;
47
84
  }
48
85
 
86
+ const mapped = effortMap[level]!;
87
+
49
88
  if (mapped !== reasoning) {
50
89
  warnings.push({
51
90
  type: 'compatibility',
@@ -67,43 +106,47 @@ const DEFAULT_REASONING_BUDGET_PERCENTAGES: Record<ReasoningLevel, number> = {
67
106
  };
68
107
 
69
108
  /**
70
- * Maps a top-level reasoning level to an absolute token budget by multiplying
71
- * the model's max output tokens by a percentage from the budget percentages
72
- * map. The result is clamped between `minReasoningBudget` (default 1024) and
73
- * `maxReasoningBudget`. Pushes an unsupported warning if the level is not
74
- * present in the budget percentages map.
109
+ * Maps a top-level reasoning value to an absolute token budget by multiplying
110
+ * the model's max output tokens by a percentage for the level. Levels missing
111
+ * from `budgetPercentages` use the default percentages. The result is clamped
112
+ * between `minReasoningBudget` (default 1024) and `maxReasoningBudget`.
75
113
  *
76
- * @returns The computed token budget, or `undefined` if the level is not
77
- * supported.
114
+ * Handle `'none'` before calling this function when the model can disable
115
+ * reasoning. Otherwise, `'none'` maps to `minReasoningBudget` with a
116
+ * compatibility warning.
117
+ *
118
+ * @returns The computed token budget.
78
119
  */
79
120
  export function mapReasoningToProviderBudget({
80
121
  reasoning,
81
122
  maxOutputTokens,
82
123
  maxReasoningBudget,
83
124
  minReasoningBudget = 1024,
84
- budgetPercentages = DEFAULT_REASONING_BUDGET_PERCENTAGES,
125
+ budgetPercentages,
85
126
  warnings,
86
127
  }: {
87
- reasoning: ReasoningLevel;
128
+ reasoning: ReasoningLevel | 'none';
88
129
  maxOutputTokens: number;
89
130
  maxReasoningBudget: number;
90
131
  minReasoningBudget?: number;
91
132
  budgetPercentages?: Partial<Record<ReasoningLevel, number>>;
92
133
  warnings: SharedV4Warning[];
93
- }): number | undefined {
94
- const pct = budgetPercentages[reasoning];
95
-
96
- if (pct == null) {
134
+ }): number {
135
+ if (reasoning === 'none') {
97
136
  warnings.push({
98
- type: 'unsupported',
137
+ type: 'compatibility',
99
138
  feature: 'reasoning',
100
- details: `reasoning "${reasoning}" is not supported by this model.`,
139
+ details: `reasoning "none" is not supported by this model. mapped to the minimum reasoning budget of ${minReasoningBudget} tokens.`,
101
140
  });
102
- return undefined;
141
+ return minReasoningBudget;
103
142
  }
104
143
 
144
+ const percentage =
145
+ budgetPercentages?.[reasoning] ??
146
+ DEFAULT_REASONING_BUDGET_PERCENTAGES[reasoning];
147
+
105
148
  return Math.min(
106
149
  maxReasoningBudget,
107
- Math.max(minReasoningBudget, Math.round(maxOutputTokens * pct)),
150
+ Math.max(minReasoningBudget, Math.round(maxOutputTokens * percentage)),
108
151
  );
109
152
  }
@@ -34,6 +34,17 @@ export type { ProviderReference } from './provider-reference';
34
34
  export type {
35
35
  SandboxSession as Experimental_SandboxSession,
36
36
  SandboxProcess as Experimental_SandboxProcess,
37
+ SandboxRunOptions as Experimental_SandboxRunOptions,
38
+ SandboxReadFileOptions as Experimental_SandboxReadFileOptions,
39
+ SandboxReadBinaryFileOptions as Experimental_SandboxReadBinaryFileOptions,
40
+ SandboxReadTextFileOptions as Experimental_SandboxReadTextFileOptions,
41
+ SandboxSpawnOptions as Experimental_SandboxSpawnOptions,
42
+ SandboxSpawnOptionsWithStdin as Experimental_SandboxSpawnOptionsWithStdin,
43
+ SandboxSpawnOptionsWithoutStdin as Experimental_SandboxSpawnOptionsWithoutStdin,
44
+ SandboxWriteFileOptions as Experimental_SandboxWriteFileOptions,
45
+ SandboxWriteBinaryFileOptions as Experimental_SandboxWriteBinaryFileOptions,
46
+ SandboxWriteTextFileOptions as Experimental_SandboxWriteTextFileOptions,
47
+ WritableSandboxProcess as Experimental_WritableSandboxProcess,
37
48
  } from './sandbox';
38
49
  export type { SystemModelMessage } from './system-model-message';
39
50
  export {
@@ -27,10 +27,24 @@ type SandboxProcessOptions = {
27
27
  abortSignal?: AbortSignal;
28
28
  };
29
29
 
30
+ export type SandboxRunOptions = SandboxProcessOptions;
31
+
32
+ export type SandboxSpawnOptions = SandboxProcessOptions & {
33
+ stdin?: boolean;
34
+ };
35
+
36
+ export type SandboxSpawnOptionsWithStdin = SandboxProcessOptions & {
37
+ stdin: true;
38
+ };
39
+
40
+ export type SandboxSpawnOptionsWithoutStdin = SandboxProcessOptions & {
41
+ stdin?: false;
42
+ };
43
+
30
44
  /**
31
45
  * Options for reading a file from the sandbox.
32
46
  */
33
- type ReadFileOptions = {
47
+ export type SandboxReadFileOptions = {
34
48
  /**
35
49
  * Path of the file to read.
36
50
  */
@@ -42,6 +56,26 @@ type ReadFileOptions = {
42
56
  abortSignal?: AbortSignal;
43
57
  };
44
58
 
59
+ export type SandboxReadBinaryFileOptions = SandboxReadFileOptions;
60
+
61
+ export type SandboxReadTextFileOptions = SandboxReadFileOptions & {
62
+ /**
63
+ * Text encoding used to decode the file bytes. Defaults to `"utf-8"`.
64
+ */
65
+ encoding?: string;
66
+
67
+ /**
68
+ * 1-based inclusive start line. Defaults to 1.
69
+ */
70
+ startLine?: number;
71
+
72
+ /**
73
+ * 1-based inclusive end line. When past the file's line count, the read
74
+ * returns through EOF without error.
75
+ */
76
+ endLine?: number;
77
+ };
78
+
45
79
  /**
46
80
  * Options for writing a file to the sandbox. `CONTENT` is the payload written
47
81
  * to the file: a byte stream, raw bytes, or a string.
@@ -63,6 +97,19 @@ type WriteFileOptions<CONTENT> = {
63
97
  abortSignal?: AbortSignal;
64
98
  };
65
99
 
100
+ export type SandboxWriteFileOptions = WriteFileOptions<
101
+ ReadableStream<Uint8Array>
102
+ >;
103
+
104
+ export type SandboxWriteBinaryFileOptions = WriteFileOptions<Uint8Array>;
105
+
106
+ export type SandboxWriteTextFileOptions = WriteFileOptions<string> & {
107
+ /**
108
+ * Text encoding used to encode the string to bytes. Defaults to `"utf-8"`.
109
+ */
110
+ encoding?: string;
111
+ };
112
+
66
113
  /**
67
114
  * Sandbox session that can execute commands and read/write files.
68
115
  */
@@ -83,7 +130,7 @@ export type SandboxSession = {
83
130
  * to stream bytes.
84
131
  */
85
132
  readonly readFile: (
86
- options: ReadFileOptions,
133
+ options: SandboxReadFileOptions,
87
134
  ) => PromiseLike<ReadableStream<Uint8Array> | null>;
88
135
 
89
136
  /**
@@ -91,7 +138,7 @@ export type SandboxSession = {
91
138
  * file does not exist.
92
139
  */
93
140
  readonly readBinaryFile: (
94
- options: ReadFileOptions,
141
+ options: SandboxReadBinaryFileOptions,
95
142
  ) => PromiseLike<Uint8Array | null>;
96
143
 
97
144
  /**
@@ -102,23 +149,7 @@ export type SandboxSession = {
102
149
  * returns through EOF without error.
103
150
  */
104
151
  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
- },
152
+ options: SandboxReadTextFileOptions,
122
153
  ) => PromiseLike<string | null>;
123
154
 
124
155
  /**
@@ -128,16 +159,14 @@ export type SandboxSession = {
128
159
  * This is the lowest-level write primitive; prefer `writeBinaryFile` or
129
160
  * `writeTextFile` when the full content is already materialized in memory.
130
161
  */
131
- readonly writeFile: (
132
- options: WriteFileOptions<ReadableStream<Uint8Array>>,
133
- ) => PromiseLike<void>;
162
+ readonly writeFile: (options: SandboxWriteFileOptions) => PromiseLike<void>;
134
163
 
135
164
  /**
136
165
  * Write one file to the sandbox from raw bytes. Creates parent directories
137
166
  * recursively and overwrites any existing file.
138
167
  */
139
168
  readonly writeBinaryFile: (
140
- options: WriteFileOptions<Uint8Array>,
169
+ options: SandboxWriteBinaryFileOptions,
141
170
  ) => PromiseLike<void>;
142
171
 
143
172
  /**
@@ -146,12 +175,7 @@ export type SandboxSession = {
146
175
  * existing file.
147
176
  */
148
177
  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
- },
178
+ options: SandboxWriteTextFileOptions,
155
179
  ) => PromiseLike<void>;
156
180
 
157
181
  /**
@@ -160,15 +184,26 @@ export type SandboxSession = {
160
184
  *
161
185
  * `run` is conceptually a thin wrapper over this primitive: spawn,
162
186
  * collect both streams to strings, await `wait()`, return the result.
187
+ *
188
+ * Set `stdin` to `true` to keep standard input open and expose a writable
189
+ * stream on the returned handle. Implementations that do not support writable
190
+ * stdin must reject before starting the command. When omitted or `false`,
191
+ * standard input is closed and the handle has no `stdin` property.
163
192
  */
164
- readonly spawn: (
165
- options: SandboxProcessOptions,
166
- ) => PromiseLike<SandboxProcess>;
193
+ readonly spawn: {
194
+ (
195
+ options: SandboxSpawnOptionsWithStdin,
196
+ ): PromiseLike<WritableSandboxProcess>;
197
+ (options: SandboxSpawnOptionsWithoutStdin): PromiseLike<SandboxProcess>;
198
+ (
199
+ options: SandboxSpawnOptions,
200
+ ): PromiseLike<SandboxProcess | WritableSandboxProcess>;
201
+ };
167
202
 
168
203
  /**
169
204
  * Run a command in the sandbox.
170
205
  */
171
- readonly run: (options: SandboxProcessOptions) => PromiseLike<{
206
+ readonly run: (options: SandboxRunOptions) => PromiseLike<{
172
207
  /**
173
208
  * Exit code returned by the command.
174
209
  */
@@ -215,3 +250,11 @@ export type SandboxProcess = {
215
250
  */
216
251
  kill(): PromiseLike<void>;
217
252
  };
253
+
254
+ export type WritableSandboxProcess = SandboxProcess & {
255
+ /**
256
+ * Stream of bytes written to the process's standard input. Closing the stream
257
+ * delivers EOF after pending writes without terminating the process.
258
+ */
259
+ readonly stdin: WritableStream<Uint8Array>;
260
+ };