-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathThinkingBudget.tsx
More file actions
293 lines (269 loc) · 11.5 KB
/
Copy pathThinkingBudget.tsx
File metadata and controls
293 lines (269 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
Semantics for Reasoning Effort (ThinkingBudget)
Capability surface:
- modelInfo.supportsReasoningEffort: boolean | Array<"disable" | "none" | "minimal" | "low" | "medium" | "high">
- true → UI shows ["low","medium","high"]
- array → UI shows exactly the provided values
Selection behavior:
- "disable":
- Label: t("settings:providers.reasoningEffort.none")
- set enableReasoningEffort = false
- persist reasoningEffort = "disable"
- request builders omit any reasoning parameter/body sections
- "none":
- Label: t("settings:providers.reasoningEffort.none")
- set enableReasoningEffort = true
- persist reasoningEffort = "none"
- request builders include reasoning with value "none"
- "minimal" | "low" | "medium" | "high":
- set enableReasoningEffort = true
- persist the selected value
- request builders include reasoning with the selected effort
Required:
- If modelInfo.requiredReasoningEffort is true, do not synthesize a "None" choice. Only show values from the capability.
- On mount, if unset and a default exists, set enableReasoningEffort = true and use modelInfo.reasoningEffort.
Notes:
- Current selection is normalized to the capability: unsupported persisted values are not shown.
- Both "disable" and "none" display as the "None" label per UX, but are wired differently as above.
- "minimal" uses t("settings:providers.reasoningEffort.minimal").
*/
import { useEffect } from "react"
import { Checkbox } from "vscrui"
import {
type ProviderSettings,
type ModelInfo,
type ReasoningEffortWithMinimal,
reasoningEfforts,
} from "@roo-code/types"
import {
DEFAULT_HYBRID_REASONING_MODEL_MAX_TOKENS,
DEFAULT_HYBRID_REASONING_MODEL_THINKING_TOKENS,
GEMINI_25_PRO_MIN_THINKING_TOKENS,
} from "@roo/api"
import { useAppTranslation } from "@src/i18n/TranslationContext"
import { Slider, Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@src/components/ui"
import { useSelectedModel } from "@src/components/ui/hooks/useSelectedModel"
interface ThinkingBudgetProps {
apiConfiguration: ProviderSettings
setApiConfigurationField: <K extends keyof ProviderSettings>(
field: K,
value: ProviderSettings[K],
isUserAction?: boolean,
) => void
modelInfo?: ModelInfo
}
export const ThinkingBudget = ({ apiConfiguration, setApiConfigurationField, modelInfo }: ThinkingBudgetProps) => {
const { t } = useAppTranslation()
const { id: selectedModelId } = useSelectedModel(apiConfiguration)
// Check if this is a Gemini 2.5 Pro model
const isGemini25Pro = selectedModelId && selectedModelId.includes("gemini-2.5-pro")
const minThinkingTokens = isGemini25Pro ? GEMINI_25_PRO_MIN_THINKING_TOKENS : 1024
// Check model capabilities
const isReasoningSupported = !!modelInfo && modelInfo.supportsReasoningBinary
const isReasoningBudgetSupported = !!modelInfo && modelInfo.supportsReasoningBudget
const isReasoningBudgetRequired = !!modelInfo && modelInfo.requiredReasoningBudget
const isReasoningEffortSupported = !!modelInfo && modelInfo.supportsReasoningEffort
// Build available reasoning efforts list from capability
const supports = modelInfo?.supportsReasoningEffort
const baseAvailableOptions: ReadonlyArray<ReasoningEffortWithMinimal> =
supports === true
? (reasoningEfforts as readonly ReasoningEffortWithMinimal[])
: Array.isArray(supports)
? (supports as ReadonlyArray<ReasoningEffortWithMinimal>)
: (reasoningEfforts as readonly ReasoningEffortWithMinimal[])
// "disable" turns off reasoning entirely; "none" is a valid reasoning level.
// Both display as "None" in the UI but behave differently.
// Add "disable" option only when:
// 1. requiredReasoningEffort is not true, AND
// 2. supportsReasoningEffort is boolean true (not an explicit array)
// When the model provides an explicit array, respect those exact values.
type ReasoningEffortOption = ReasoningEffortWithMinimal | "none" | "disable"
const shouldAutoAddDisable =
!modelInfo?.requiredReasoningEffort && supports === true && !baseAvailableOptions.includes("disable" as any)
const availableOptions: ReadonlyArray<ReasoningEffortOption> = shouldAutoAddDisable
? (["disable", ...baseAvailableOptions] as ReasoningEffortOption[])
: (baseAvailableOptions as ReadonlyArray<ReasoningEffortOption>)
// Default reasoning effort - use model's default if available
// GPT-5 models have "medium" as their default in the model configuration
const modelDefaultReasoningEffort = modelInfo?.reasoningEffort as ReasoningEffortWithMinimal | undefined
const defaultReasoningEffort: ReasoningEffortOption = modelInfo?.requiredReasoningEffort
? modelDefaultReasoningEffort || "medium"
: "disable"
// Current reasoning effort from settings, or fall back to default
const storedReasoningEffort = apiConfiguration.reasoningEffort as ReasoningEffortOption | undefined
const currentReasoningEffort: ReasoningEffortOption = storedReasoningEffort || defaultReasoningEffort
// Set default reasoning effort when model supports it and no value is set
useEffect(() => {
if (isReasoningEffortSupported && !apiConfiguration.reasoningEffort) {
// Only set a default if reasoning is required, otherwise leave as undefined (which maps to "disable")
if (modelInfo?.requiredReasoningEffort && defaultReasoningEffort !== "disable") {
setApiConfigurationField("reasoningEffort", defaultReasoningEffort as ReasoningEffortWithMinimal, false)
}
}
}, [
isReasoningEffortSupported,
apiConfiguration.reasoningEffort,
defaultReasoningEffort,
modelInfo?.requiredReasoningEffort,
setApiConfigurationField,
])
// Sync enableReasoningEffort based on selection
// "disable" turns off reasoning; "none" is a valid level (reasoning enabled)
useEffect(() => {
if (!isReasoningEffortSupported) return
const shouldEnable = modelInfo?.requiredReasoningEffort || currentReasoningEffort !== "disable"
if (shouldEnable && apiConfiguration.enableReasoningEffort !== true) {
setApiConfigurationField("enableReasoningEffort", true, false)
}
}, [
isReasoningEffortSupported,
modelInfo?.requiredReasoningEffort,
currentReasoningEffort,
apiConfiguration.enableReasoningEffort,
setApiConfigurationField,
])
const enableReasoningEffort = apiConfiguration.enableReasoningEffort
const customMaxOutputTokens = apiConfiguration.modelMaxTokens || DEFAULT_HYBRID_REASONING_MODEL_MAX_TOKENS
const customMaxThinkingTokens =
apiConfiguration.modelMaxThinkingTokens || DEFAULT_HYBRID_REASONING_MODEL_THINKING_TOKENS
// Dynamically expand or shrink the max thinking budget based on the custom
// max output tokens so that there's always a 20% buffer.
const modelMaxThinkingTokens = modelInfo?.maxThinkingTokens
? Math.min(modelInfo.maxThinkingTokens, Math.floor(0.8 * customMaxOutputTokens))
: Math.floor(0.8 * customMaxOutputTokens)
// If the custom max thinking tokens are going to exceed it's limit due
// to the custom max output tokens being reduced then we need to shrink it
// appropriately.
useEffect(() => {
if (isReasoningBudgetSupported && customMaxThinkingTokens > modelMaxThinkingTokens) {
setApiConfigurationField("modelMaxThinkingTokens", modelMaxThinkingTokens, false)
}
}, [isReasoningBudgetSupported, customMaxThinkingTokens, modelMaxThinkingTokens, setApiConfigurationField])
if (!modelInfo) {
return null
}
// Max output tokens control used by both binary reasoning and budget reasoning paths.
const maxOutputTokensControl = (
<div className="flex flex-col gap-1">
<div className="font-medium">{t("settings:thinkingBudget.maxTokens")}</div>
<div className="flex items-center gap-1">
<Slider
min={8192}
max={Math.max(
modelInfo.maxTokens || 8192,
customMaxOutputTokens,
DEFAULT_HYBRID_REASONING_MODEL_MAX_TOKENS,
)}
step={1024}
value={[customMaxOutputTokens]}
onValueChange={([value]) => setApiConfigurationField("modelMaxTokens", value)}
/>
<div className="w-12 text-sm text-center">{customMaxOutputTokens}</div>
</div>
</div>
)
// Models with supportsReasoningBinary (binary reasoning) show a simple on/off toggle
if (isReasoningSupported) {
return (
<div className="flex flex-col gap-1">
<Checkbox
checked={enableReasoningEffort}
onChange={(checked: boolean) =>
setApiConfigurationField("enableReasoningEffort", checked === true)
}>
{t("settings:providers.useReasoning")}
</Checkbox>
{maxOutputTokensControl}
</div>
)
}
return isReasoningBudgetSupported && !!modelInfo.maxTokens ? (
<>
{!isReasoningBudgetRequired && (
<div className="flex flex-col gap-1">
<Checkbox
checked={enableReasoningEffort}
onChange={(checked: boolean) =>
setApiConfigurationField("enableReasoningEffort", checked === true)
}>
{t("settings:providers.useReasoning")}
</Checkbox>
</div>
)}
{(isReasoningBudgetRequired || enableReasoningEffort) && (
<>
<div className="flex flex-col gap-1">
<div className="font-medium">{t("settings:thinkingBudget.maxTokens")}</div>
<div className="flex items-center gap-1">
<Slider
min={8192}
max={Math.max(
modelInfo.maxTokens || 8192,
customMaxOutputTokens,
DEFAULT_HYBRID_REASONING_MODEL_MAX_TOKENS,
)}
step={1024}
value={[customMaxOutputTokens]}
onValueChange={([value]) => setApiConfigurationField("modelMaxTokens", value)}
/>
<div className="w-12 text-sm text-center">{customMaxOutputTokens}</div>
</div>
</div>
<div className="flex flex-col gap-1">
<div className="font-medium">{t("settings:thinkingBudget.maxThinkingTokens")}</div>
<div className="flex items-center gap-1" data-testid="reasoning-budget">
<Slider
min={minThinkingTokens}
max={modelMaxThinkingTokens}
step={minThinkingTokens === 128 ? 128 : 1024}
value={[customMaxThinkingTokens]}
onValueChange={([value]) => setApiConfigurationField("modelMaxThinkingTokens", value)}
/>
<div className="w-12 text-sm text-center">{customMaxThinkingTokens}</div>
</div>
</div>
</>
)}
</>
) : isReasoningEffortSupported ? (
<div className="flex flex-col gap-1" data-testid="reasoning-effort">
<div className="flex justify-between items-center">
<label className="block font-medium mb-1">{t("settings:providers.reasoningEffort.label")}</label>
</div>
<Select
value={currentReasoningEffort}
onValueChange={(value: ReasoningEffortOption) => {
// "disable" turns off reasoning entirely; "none" is a valid reasoning level
if (value === "disable") {
setApiConfigurationField("enableReasoningEffort", false)
setApiConfigurationField("reasoningEffort", "disable")
} else {
// "none", "minimal", "low", "medium", "high" all enable reasoning
setApiConfigurationField("enableReasoningEffort", true)
setApiConfigurationField("reasoningEffort", value as ReasoningEffortWithMinimal)
}
}}>
<SelectTrigger className="w-full">
<SelectValue
placeholder={
currentReasoningEffort
? currentReasoningEffort === "none" || currentReasoningEffort === "disable"
? t("settings:providers.reasoningEffort.none")
: t(`settings:providers.reasoningEffort.${currentReasoningEffort}`)
: t("settings:common.select")
}
/>
</SelectTrigger>
<SelectContent>
{availableOptions.map((value) => (
<SelectItem key={value} value={value}>
{value === "none" || value === "disable"
? t("settings:providers.reasoningEffort.none")
: t(`settings:providers.reasoningEffort.${value}`)}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
) : null
}