Skip to content

Commit 5f1c0c3

Browse files
committed
fix(web): keep effort selection with trailing spaces
1 parent 0f936e6 commit 5f1c0c3

3 files changed

Lines changed: 37 additions & 7 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6432,13 +6432,13 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
64326432
},
64336433
);
64346434
}
6435-
if (change.prompt !== promptRef.current) {
6435+
if ("promptSelectionOffset" in change) {
64366436
const snapshot = readComposerSnapshotStable();
64376437
const selection = composerEditorRef.current?.readSelectionRange() ?? {
64386438
start: snapshot.expandedCursor,
64396439
end: snapshot.expandedCursor,
64406440
};
6441-
const offset = change.prompt.length - snapshot.value.length;
6441+
const offset = change.promptSelectionOffset;
64426442
applyPromptReplacementStable(0, promptRef.current.length, change.prompt, {
64436443
expandedCursorAfterReplace: Math.max(0, selection.start + offset),
64446444
expandedSelectionEndAfterReplace: Math.max(0, selection.end + offset),

‎apps/web/src/components/chat/TraitsPicker.test.ts‎

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ describe("reasoning level shortcuts", () => {
123123
...reportedInput,
124124
modelOptions: [{ id: "effort", value: "high" }],
125125
}),
126-
).toEqual({ prompt: "Ultrathink:\nSolve this" });
126+
).toEqual({ prompt: "Ultrathink:\nSolve this", promptSelectionOffset: 12 });
127127
expect(
128128
getReasoningLevelChange({
129129
...reportedInput,
@@ -132,13 +132,13 @@ describe("reasoning level shortcuts", () => {
132132
instanceId: ProviderInstanceId.make("another-codex"),
133133
},
134134
}),
135-
).toEqual({ prompt: "Ultrathink:\nSolve this" });
135+
).toEqual({ prompt: "Ultrathink:\nSolve this", promptSelectionOffset: 12 });
136136
expect(
137137
getReasoningLevelChange({
138138
...reportedInput,
139139
reportedModelSelection: { ...reportedModelSelection, model: "another-model" },
140140
}),
141-
).toEqual({ prompt: "Ultrathink:\nSolve this" });
141+
).toEqual({ prompt: "Ultrathink:\nSolve this", promptSelectionOffset: 12 });
142142
});
143143

144144
it("uses a catalog default for an unset OpenCode variant", () => {
@@ -155,11 +155,12 @@ describe("reasoning level shortcuts", () => {
155155
it("wraps at both ends, including prompt-controlled ultrathink", () => {
156156
expect(
157157
getReasoningLevelChange({ ...input, modelOptions: [{ id: "effort", value: "low" }] }),
158-
).toEqual({ prompt: "Ultrathink:\nSolve this" });
158+
).toEqual({ prompt: "Ultrathink:\nSolve this", promptSelectionOffset: 12 });
159159
expect(
160160
getReasoningLevelChange({ ...input, prompt: "Ultrathink:\nSolve this", direction: 1 }),
161161
).toEqual({
162162
prompt: "Solve this",
163+
promptSelectionOffset: -12,
163164
modelOptions: [
164165
{ id: "effort", value: "low" },
165166
{ id: "fastMode", value: true },
@@ -170,9 +171,11 @@ describe("reasoning level shortcuts", () => {
170171
it("enters and leaves prompt-controlled ultrathink", () => {
171172
expect(getReasoningLevelChange({ ...input, direction: 1 })).toEqual({
172173
prompt: "Ultrathink:\nSolve this",
174+
promptSelectionOffset: 12,
173175
});
174176
expect(getReasoningLevelChange({ ...input, prompt: "Ultrathink:\nSolve this" })).toEqual({
175177
prompt: "Solve this",
178+
promptSelectionOffset: -12,
176179
modelOptions: [
177180
{ id: "effort", value: "high" },
178181
{ id: "fastMode", value: true },
@@ -189,9 +192,29 @@ describe("reasoning level shortcuts", () => {
189192
});
190193
expect(getReasoningLevelChange({ ...input, prompt: "", direction: 1 })).toEqual({
191194
prompt: "Ultrathink:\n",
195+
promptSelectionOffset: 12,
192196
});
193197
});
194198

199+
it.each([
200+
{ prompt: "Alpha beta gamma ", direction: 1 as const },
201+
{ prompt: " Alpha beta gamma ", direction: 1 as const },
202+
{ prompt: "Ultrathink:\nAlpha beta gamma ", direction: -1 as const },
203+
{ prompt: "Ultrathink:\n Alpha beta gamma ", direction: 1 as const },
204+
])("keeps both selection endpoints in retained text for $prompt", ({ prompt, direction }) => {
205+
const change = getReasoningLevelChange({ ...input, prompt, direction });
206+
if (!change || !("promptSelectionOffset" in change)) {
207+
throw new Error("Expected an effort prompt change");
208+
}
209+
const offset = change.promptSelectionOffset;
210+
const start = prompt.indexOf("beta") + offset;
211+
const end = start + "beta".length;
212+
expect(change.prompt.slice(start, end)).toBe("beta");
213+
expect(change.prompt.slice(0, start) + "X" + change.prompt.slice(end)).toBe(
214+
change.prompt.replace("beta", "X"),
215+
);
216+
});
217+
195218
it.each([
196219
["codex", "reasoningEffort"],
197220
["claudeAgent", "effort"],

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,14 @@ export function getReasoningLevelChange(
311311
descriptor.options[
312312
(index + input.direction + descriptor.options.length) % descriptor.options.length
313313
];
314-
return next ? getTraitSelectChange(selected, descriptor, input.prompt, next.id) : null;
314+
const change = next ? getTraitSelectChange(selected, descriptor, input.prompt, next.id) : null;
315+
if (!change || change.prompt === input.prompt) return change;
316+
return {
317+
...change,
318+
promptSelectionOffset:
319+
change.prompt.length -
320+
("modelOptions" in change ? input.prompt.length : input.prompt.trimEnd().length),
321+
};
315322
}
316323

317324
export function shouldRenderTraitsControls(input: {

0 commit comments

Comments
 (0)