Skip to content

Commit ee9a581

Browse files
committed
refactor(web): simplify reasoning shortcut handling
Extract the reasoning cycle into a callback so the window shortcut listener no longer re-binds on every caret move, drop an unused effect dependency, collapse the model option branching in the shared transition, fold the two programmer-error statuses into unsupported, reword the unsupported toast, and document the shortcuts.
1 parent 044b6d5 commit ee9a581

5 files changed

Lines changed: 118 additions & 113 deletions

File tree

‎apps/web/src/components/chat/ChatComposer.tsx‎

Lines changed: 85 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -2548,7 +2548,6 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
25482548
activePendingProgress?.customAnswer,
25492549
activePendingProgress?.activeQuestion?.id,
25502550
activePendingUserInput?.requestId,
2551-
prompt,
25522551
promptRef,
25532552
]);
25542553

@@ -4315,6 +4314,89 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
43154314
setIsStashMenuOpen(false);
43164315
}, [prompt]);
43174316

4317+
// The cursor is only needed as a fallback when the editor has no snapshot,
4318+
// so read it through a ref instead of re-binding the shortcut listener on
4319+
// every caret move.
4320+
const composerCursorRef = useRef(composerCursor);
4321+
composerCursorRef.current = composerCursor;
4322+
4323+
const applyReasoningCycle = useCallback(
4324+
(direction: "increase" | "decrease") => {
4325+
const editorShowsDraftPrompt = !isComposerApprovalState && activePendingProgress === null;
4326+
const currentPrompt = editorShowsDraftPrompt
4327+
? promptRef.current
4328+
: (getComposerDraft(composerDraftTarget)?.prompt ?? "");
4329+
const currentModelOptions = composerModelOptions?.[selectedInstanceId];
4330+
const transition = resolveReasoningTransition({
4331+
capabilities: getProviderModelCapabilities(
4332+
selectedProviderModels,
4333+
selectedModel,
4334+
selectedProvider,
4335+
settings.planModeEnabled,
4336+
),
4337+
modelOptions: currentModelOptions,
4338+
prompt: currentPrompt,
4339+
action: { type: "cycle", direction },
4340+
});
4341+
if (transition.status === "blocked") {
4342+
toastManager.add({
4343+
type: "info",
4344+
title: "Remove “ultrathink” from the prompt text to change reasoning.",
4345+
});
4346+
return;
4347+
}
4348+
if (transition.status === "unsupported") {
4349+
toastManager.add({
4350+
type: "info",
4351+
title: "This model has no reasoning levels to switch between.",
4352+
});
4353+
return;
4354+
}
4355+
if (transition.status !== "changed") return;
4356+
if (transition.prompt !== currentPrompt) {
4357+
setPrompt(transition.prompt);
4358+
// When a pending question or approval hides the editor, the draft
4359+
// is updated in the store only. The cursor and trigger belong to
4360+
// the visible editor state and are rebuilt when the draft returns.
4361+
if (editorShowsDraftPrompt) {
4362+
const currentExpandedCursor =
4363+
composerEditorRef.current?.readSnapshot().expandedCursor ??
4364+
expandCollapsedComposerCursor(currentPrompt, composerCursorRef.current);
4365+
const nextExpandedCursor = mapComposerCursorAcrossLeadingPromptChange(
4366+
currentPrompt,
4367+
transition.prompt,
4368+
currentExpandedCursor,
4369+
);
4370+
promptRef.current = transition.prompt;
4371+
setComposerCursor(collapseExpandedComposerCursor(transition.prompt, nextExpandedCursor));
4372+
setComposerTrigger(detectComposerTrigger(transition.prompt, nextExpandedCursor));
4373+
}
4374+
}
4375+
if (transition.modelOptions !== currentModelOptions) {
4376+
setProviderModelOptions(composerDraftTarget, selectedProvider, transition.modelOptions, {
4377+
instanceId: selectedInstanceId,
4378+
model: selectedModel,
4379+
persistSticky: true,
4380+
});
4381+
}
4382+
},
4383+
[
4384+
activePendingProgress,
4385+
composerDraftTarget,
4386+
composerModelOptions,
4387+
getComposerDraft,
4388+
isComposerApprovalState,
4389+
promptRef,
4390+
selectedInstanceId,
4391+
selectedModel,
4392+
selectedProvider,
4393+
selectedProviderModels,
4394+
setPrompt,
4395+
setProviderModelOptions,
4396+
settings.planModeEnabled,
4397+
],
4398+
);
4399+
43184400
useEffect(() => {
43194401
const handler = (event: globalThis.KeyboardEvent) => {
43204402
const command = resolveShortcutCommand(event, keybindings, {
@@ -4335,67 +4417,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
43354417
return;
43364418
}
43374419
if (reasoningDirection !== null) {
4338-
const editorShowsDraftPrompt = !isComposerApprovalState && activePendingProgress === null;
4339-
const currentPrompt = editorShowsDraftPrompt
4340-
? promptRef.current
4341-
: (getComposerDraft(composerDraftTarget)?.prompt ?? "");
4342-
const currentModelOptions = composerModelOptions?.[selectedInstanceId];
4343-
const transition = resolveReasoningTransition({
4344-
capabilities: getProviderModelCapabilities(
4345-
selectedProviderModels,
4346-
selectedModel,
4347-
selectedProvider,
4348-
settings.planModeEnabled,
4349-
),
4350-
modelOptions: currentModelOptions,
4351-
prompt: currentPrompt,
4352-
action: { type: "cycle", direction: reasoningDirection },
4353-
});
4354-
if (transition.status === "changed") {
4355-
if (transition.prompt !== currentPrompt) {
4356-
setPrompt(transition.prompt);
4357-
// When a pending question or approval hides the editor, the draft
4358-
// is updated in the store only. The cursor and trigger belong to
4359-
// the visible editor state and are rebuilt when the draft returns.
4360-
if (editorShowsDraftPrompt) {
4361-
const currentExpandedCursor =
4362-
composerEditorRef.current?.readSnapshot().expandedCursor ??
4363-
expandCollapsedComposerCursor(currentPrompt, composerCursor);
4364-
const nextExpandedCursor = mapComposerCursorAcrossLeadingPromptChange(
4365-
currentPrompt,
4366-
transition.prompt,
4367-
currentExpandedCursor,
4368-
);
4369-
promptRef.current = transition.prompt;
4370-
setComposerCursor(
4371-
collapseExpandedComposerCursor(transition.prompt, nextExpandedCursor),
4372-
);
4373-
setComposerTrigger(detectComposerTrigger(transition.prompt, nextExpandedCursor));
4374-
}
4375-
}
4376-
if (transition.modelOptions !== currentModelOptions) {
4377-
setProviderModelOptions(
4378-
composerDraftTarget,
4379-
selectedProvider,
4380-
transition.modelOptions,
4381-
{
4382-
instanceId: selectedInstanceId,
4383-
model: selectedModel,
4384-
persistSticky: true,
4385-
},
4386-
);
4387-
}
4388-
} else if (transition.status === "blocked") {
4389-
toastManager.add({
4390-
type: "info",
4391-
title: "Remove “ultrathink” from the prompt text to change reasoning.",
4392-
});
4393-
} else if (transition.status === "unsupported") {
4394-
toastManager.add({
4395-
type: "info",
4396-
title: "This model does not advertise reasoning levels.",
4397-
});
4398-
}
4420+
applyReasoningCycle(reasoningDirection);
43994421
return;
44004422
}
44014423
if (pendingUserInputs.length > 0 && !isComposerApprovalState) {
@@ -4411,22 +4433,12 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
44114433
return () => window.removeEventListener("keydown", handler, true);
44124434
}, [
44134435
activePendingProgress,
4414-
composerDraftTarget,
4415-
composerCursor,
4416-
composerModelOptions,
4417-
getComposerDraft,
4436+
applyReasoningCycle,
44184437
isComposerApprovalState,
44194438
isComposerModelPickerOpen,
44204439
keybindings,
44214440
pendingUserInputs.length,
44224441
projectSelectionRequired,
4423-
selectedInstanceId,
4424-
selectedModel,
4425-
selectedProvider,
4426-
selectedProviderModels,
4427-
setPrompt,
4428-
setProviderModelOptions,
4429-
settings.planModeEnabled,
44304442
stashCurrentPrompt,
44314443
terminalOpen,
44324444
]);

‎apps/web/src/components/chat/TraitsPicker.tsx‎

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
getProviderOptionCurrentValue,
1313
getProviderOptionDescriptors,
1414
isClaudeUltrathinkPrompt,
15+
isReasoningDescriptorId,
1516
normalizeModelSlug,
1617
resolveReasoningTransition,
1718
stripClaudeUltrathinkPrefix,
@@ -140,25 +141,23 @@ export function resolveTraitsSelectChange(input: {
140141
prompt: string;
141142
modelOptions: ProviderOptions | null | undefined;
142143
}): { prompt: string; modelOptions: ProviderOptions | undefined } | null {
144+
if (!isReasoningDescriptorId(input.descriptor.id)) {
145+
return {
146+
prompt: input.prompt,
147+
modelOptions: buildProviderOptionSelectionsFromDescriptors(
148+
replaceDescriptorCurrentValue(input.descriptors, input.descriptor.id, input.value),
149+
),
150+
};
151+
}
143152
const transition = resolveReasoningTransition({
144153
capabilities: input.caps,
145154
modelOptions: input.modelOptions,
146155
prompt: input.prompt,
147156
action: { type: "select", descriptorId: input.descriptor.id, value: input.value },
148157
});
149-
if (transition.status === "changed") {
150-
return { prompt: transition.prompt, modelOptions: transition.modelOptions };
151-
}
152-
if (transition.status !== "not-applicable") {
153-
return null;
154-
}
155-
156-
return {
157-
prompt: input.prompt,
158-
modelOptions: buildProviderOptionSelectionsFromDescriptors(
159-
replaceDescriptorCurrentValue(input.descriptors, input.descriptor.id, input.value),
160-
),
161-
};
158+
return transition.status === "changed"
159+
? { prompt: transition.prompt, modelOptions: transition.modelOptions }
160+
: null;
162161
}
163162

164163
function getSelectedTraits(

‎docs/user/composer.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ returns to the remembered selection.
4444

4545
Leaving reasoning level or service tier unset uses the provider's own configuration.
4646

47+
Step the reasoning level up or down with `Cmd+Option+.` and `Cmd+Option+,` (`Ctrl+Alt` on
48+
Windows and Linux). Remap them in Settings → Keybindings as `reasoning.increase` and
49+
`reasoning.decrease`.
50+
4751
## Quote an assistant response
4852

4953
On web and desktop, select text within one assistant response and choose

‎packages/shared/src/model.test.ts‎

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ describe("resolveReasoningTransition", () => {
563563
capabilities: oneChoiceCaps,
564564
action: { type: "select", descriptorId: "reasoning", value: "missing" },
565565
}),
566-
).toEqual({ status: "invalid", reason: "unknown-value" });
566+
).toEqual({ status: "unsupported", reason: "unknown-value" });
567567
});
568568

569569
it("replaces a stale persisted value when selecting the effective default", () => {
@@ -590,15 +590,6 @@ describe("resolveReasoningTransition", () => {
590590
});
591591
expect(selected).toEqual(cycled);
592592
});
593-
594-
it("returns not-applicable for non-reasoning mouse selections", () => {
595-
expect(
596-
transition({
597-
capabilities: claudeCaps,
598-
action: { type: "select", descriptorId: "contextWindow", value: "200k" },
599-
}),
600-
).toEqual({ status: "not-applicable" });
601-
});
602593
});
603594

604595
describe("stripClaudeUltrathinkPrefix", () => {

‎packages/shared/src/model.ts‎

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -245,12 +245,18 @@ export type ReasoningTransitionResult =
245245
| { status: "blocked"; reason: "ultrathink-in-prompt-body" }
246246
| {
247247
status: "unsupported";
248-
reason: "missing-reasoning-option" | "missing-choices" | "unsupported-prompt-injected-value";
249-
}
250-
| { status: "not-applicable" }
251-
| { status: "invalid"; reason: "unknown-value" };
248+
reason:
249+
| "missing-reasoning-option"
250+
| "missing-choices"
251+
| "unsupported-prompt-injected-value"
252+
| "unknown-value";
253+
};
252254

253255
const REASONING_DESCRIPTOR_IDS = new Set(["reasoningEffort", "effort", "reasoning"]);
256+
257+
export function isReasoningDescriptorId(descriptorId: string): boolean {
258+
return REASONING_DESCRIPTOR_IDS.has(descriptorId);
259+
}
254260
const ULTRATHINK_VALUE = "ultrathink";
255261
const ULTRATHINK_PREFIX = "Ultrathink:\n";
256262
const CLAUDE_SLASH_COMMAND = /^\/[^\s/]+(?:\s|$)/u;
@@ -339,10 +345,6 @@ export function resolveReasoningTransition(input: {
339345
action: ReasoningTransitionAction;
340346
}): ReasoningTransitionResult {
341347
const { capabilities, modelOptions, prompt, action } = input;
342-
if (action.type === "select" && !REASONING_DESCRIPTOR_IDS.has(action.descriptorId)) {
343-
return { status: "not-applicable" };
344-
}
345-
346348
const descriptor = findReasoningDescriptor(capabilities);
347349
if (!descriptor || (action.type === "select" && action.descriptorId !== descriptor.id)) {
348350
return { status: "unsupported", reason: "missing-reasoning-option" };
@@ -360,9 +362,6 @@ export function resolveReasoningTransition(input: {
360362
promptInjectedUltrathink && (hasLeadingPrefix || ultrathinkInBody);
361363
const persistedValue = resolvePersistedReasoningValue(descriptor, modelOptions);
362364
const rawPersistedValue = getProviderOptionStringSelectionValue(modelOptions, descriptor.id);
363-
const persistedPromptInjectedValue =
364-
rawPersistedValue !== undefined &&
365-
(descriptor.promptInjectedValues?.includes(rawPersistedValue) ?? false);
366365
const currentValue = promptControlsUltrathink ? ULTRATHINK_VALUE : persistedValue;
367366
const cycleOptions = isClaudeSlashCommandPrompt(prompt)
368367
? descriptor.options.filter((option) => !descriptor.promptInjectedValues?.includes(option.id))
@@ -371,7 +370,7 @@ export function resolveReasoningTransition(input: {
371370
let targetValue: string;
372371
if (action.type === "select") {
373372
if (!descriptor.options.some((option) => option.id === action.value)) {
374-
return { status: "invalid", reason: "unknown-value" };
373+
return { status: "unsupported", reason: "unknown-value" };
375374
}
376375
targetValue = action.value;
377376
} else if (currentValue === undefined) {
@@ -401,7 +400,7 @@ export function resolveReasoningTransition(input: {
401400

402401
const targetOption = descriptor.options.find((option) => option.id === targetValue);
403402
if (!targetOption) {
404-
return { status: "invalid", reason: "unknown-value" };
403+
return { status: "unsupported", reason: "unknown-value" };
405404
}
406405

407406
const targetIsPromptInjected = descriptor.promptInjectedValues?.includes(targetValue) ?? false;
@@ -415,12 +414,12 @@ export function resolveReasoningTransition(input: {
415414
: hasLeadingPrefix
416415
? promptBody
417416
: prompt;
417+
// Leave an implicit default implicit; any stored value (including a stale
418+
// or prompt-injected one) is rewritten to the target.
418419
const nextModelOptions = targetIsPromptInjected
419420
? removePromptInjectedReasoningSelection(modelOptions, descriptor)
420-
: targetValue === persistedValue && !persistedPromptInjectedValue
421-
? rawPersistedValue === undefined || rawPersistedValue === persistedValue
422-
? (modelOptions ?? undefined)
423-
: updateReasoningSelection(modelOptions, descriptor.id, targetValue)
421+
: rawPersistedValue === undefined && targetValue === persistedValue
422+
? (modelOptions ?? undefined)
424423
: updateReasoningSelection(modelOptions, descriptor.id, targetValue);
425424
const normalizedInputOptions = modelOptions ?? undefined;
426425

0 commit comments

Comments
 (0)