@ai-sdk/provider-utils 5.0.70 → 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/CHANGELOG.md +14 -0
- package/dist/index.d.ts +32 -20
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +54 -24
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/map-reasoning-to-provider.ts +76 -33
package/package.json
CHANGED
|
@@ -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
|
|
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
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
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
|
-
*
|
|
27
|
-
*
|
|
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<
|
|
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:
|
|
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
|
-
|
|
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 (
|
|
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
|
|
71
|
-
* the model's max output tokens by a percentage
|
|
72
|
-
*
|
|
73
|
-
* `
|
|
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
|
-
*
|
|
77
|
-
*
|
|
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
|
|
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
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
if (pct == null) {
|
|
134
|
+
}): number {
|
|
135
|
+
if (reasoning === 'none') {
|
|
97
136
|
warnings.push({
|
|
98
|
-
type: '
|
|
137
|
+
type: 'compatibility',
|
|
99
138
|
feature: 'reasoning',
|
|
100
|
-
details: `reasoning "
|
|
139
|
+
details: `reasoning "none" is not supported by this model. mapped to the minimum reasoning budget of ${minReasoningBudget} tokens.`,
|
|
101
140
|
});
|
|
102
|
-
return
|
|
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 *
|
|
150
|
+
Math.max(minReasoningBudget, Math.round(maxOutputTokens * percentage)),
|
|
108
151
|
);
|
|
109
152
|
}
|