Skip to content

Commit 864e265

Browse files
committed
fix(web): an incoming question no longer takes over the message you're typing
A pending question took the composer, its caret and Send, so a half-typed message was split and only its second half reached the agent as the answer.
1 parent 4059607 commit 864e265

7 files changed

Lines changed: 133 additions & 18 deletions

File tree

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3224,6 +3224,11 @@ export default function ChatView(props: ChatViewProps) {
32243224
activeThreadId,
32253225
activePendingUserInput?.requestId,
32263226
]);
3227+
// A question only takes the composer and Send once the user chooses to
3228+
// answer it; until then the composer keeps the message being written.
3229+
const [answeringRequestKey, setAnsweringRequestKey] = useState<string | null>(null);
3230+
const isAnsweringPendingUserInput =
3231+
activePendingUserInput !== null && answeringRequestKey === activePendingRequestKey;
32273232
const pendingQuestionDraftKeys = useMemo(
32283233
() =>
32293234
activeThreadId
@@ -8393,7 +8398,7 @@ export default function ChatView(props: ChatViewProps) {
83938398
});
83948399
return;
83958400
}
8396-
if (activePendingProgress) {
8401+
if (isAnsweringPendingUserInput && activePendingProgress) {
83978402
if (directAnnotation) {
83988403
notifyDirectAnnotationAttached();
83998404
return;
@@ -9854,15 +9859,19 @@ export default function ChatView(props: ChatViewProps) {
98549859
},
98559860
};
98569861
});
9857-
promptRef.current = "";
9858-
composerRef.current?.resetCursorState({ cursor: 0 });
9862+
// Only an answering composer shows the custom answer the option cleared.
9863+
if (isAnsweringPendingUserInput) {
9864+
promptRef.current = "";
9865+
composerRef.current?.resetCursorState({ cursor: 0 });
9866+
}
98599867
},
98609868
[
98619869
activePendingProgress?.activeQuestion,
98629870
activePendingUserInput,
98639871
activePendingRequestKey,
98649872
composerDraftTarget,
98659873
composerRef,
9874+
isAnsweringPendingUserInput,
98669875
pendingUserInputAnswersByRequestId,
98679876
setComposerDraftPrompt,
98689877
],
@@ -9937,6 +9946,16 @@ export default function ChatView(props: ChatViewProps) {
99379946
setActivePendingUserInputQuestionIndex(Math.max(activePendingProgress.questionIndex - 1, 0));
99389947
}, [activePendingProgress, setActivePendingUserInputQuestionIndex]);
99399948

9949+
const onToggleAnsweringPendingUserInput = useCallback(() => {
9950+
setAnsweringRequestKey(isAnsweringPendingUserInput ? null : activePendingRequestKey);
9951+
scheduleComposerFocus();
9952+
}, [
9953+
activePendingRequestKey,
9954+
isAnsweringPendingUserInput,
9955+
scheduleComposerFocus,
9956+
setAnsweringRequestKey,
9957+
]);
9958+
99409959
async function onSubmitPlanFollowUp({
99419960
text,
99429961
context,
@@ -11218,7 +11237,10 @@ export default function ChatView(props: ChatViewProps) {
1121811237
activePendingApproval={activePendingApproval}
1121911238
pendingApprovals={pendingApprovals}
1122011239
pendingUserInputs={pendingUserInputs}
11221-
activePendingProgress={activePendingProgress}
11240+
activePendingProgress={
11241+
isAnsweringPendingUserInput ? activePendingProgress : null
11242+
}
11243+
onToggleAnsweringPendingUserInput={onToggleAnsweringPendingUserInput}
1122211244
activePendingResolvedAnswers={activePendingResolvedAnswers}
1122311245
activePendingIsResponding={activePendingIsResponding}
1122411246
activePendingDraftAnswers={activePendingDraftAnswers}

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

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ import {
7777
collapseExpandedComposerCursor,
7878
composerSubmissionIntentForKey,
7979
composerStateAtPromptEnd,
80+
composerStateForReturningDraft,
8081
detectComposerTrigger,
8182
expandCollapsedComposerCursor,
8283
formatAssistantCitationForComposer,
@@ -1546,6 +1547,7 @@ export interface ChatComposerProps {
15461547
activePendingApproval: PendingApproval | null;
15471548
pendingApprovals: PendingApproval[];
15481549
pendingUserInputs: PendingUserInput[];
1550+
/** Present only while the user is answering the active question. */
15491551
activePendingProgress: {
15501552
questionIndex: number;
15511553
isLastQuestion: boolean;
@@ -1557,6 +1559,7 @@ export interface ChatComposerProps {
15571559
allowCustomAnswer?: boolean | undefined;
15581560
} | null;
15591561
} | null;
1562+
onToggleAnsweringPendingUserInput: () => void;
15601563
activePendingResolvedAnswers: Record<string, unknown> | null;
15611564
activePendingIsResponding: boolean;
15621565
activePendingDraftAnswers: Record<string, PendingUserInputDraftAnswer>;
@@ -1713,8 +1716,9 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
17131716
environmentUnavailable,
17141717
activePendingApproval,
17151718
pendingApprovals,
1716-
pendingUserInputs,
1719+
pendingUserInputs: availablePendingUserInputs,
17171720
activePendingProgress,
1721+
onToggleAnsweringPendingUserInput,
17181722
activePendingResolvedAnswers,
17191723
activePendingIsResponding,
17201724
activePendingDraftAnswers,
@@ -1791,6 +1795,9 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
17911795
const activeTasksProgress = shownSyncPhase === null ? props.activeTasksProgress : null;
17921796
const activeTaskSteps = shownSyncPhase === null ? props.activeTaskSteps : null;
17931797
const isEditingQueuedMessage = editingQueuedAttachments !== null;
1798+
// The question panel shows every pending question; the editor, Send and
1799+
// attachments belong to a question only while the user is answering it.
1800+
const pendingUserInputs = activePendingProgress ? availablePendingUserInputs : [];
17941801
// ------------------------------------------------------------------
17951802
// Store subscriptions (prompt / images / terminal contexts)
17961803
// ------------------------------------------------------------------
@@ -2769,12 +2776,12 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
27692776
const isComposerApprovalState = activePendingApproval !== null;
27702777
const composerSuggestionsVisible = composerMenuOpen && !isComposerApprovalState;
27712778
const composerSuggestionListVisible = composerSuggestionsVisible && composerMenuItems.length > 0;
2772-
const activePendingUserInput = pendingUserInputs[0] ?? null;
2779+
const activePendingUserInput = availablePendingUserInputs[0] ?? null;
27732780
const isChoiceOnlyPendingQuestion =
27742781
activePendingProgress?.activeQuestion?.allowCustomAnswer === false;
27752782
const showComposerTopDrawer =
27762783
isComposerApprovalState ||
2777-
pendingUserInputs.length > 0 ||
2784+
availablePendingUserInputs.length > 0 ||
27782785
(!isComposerCollapsedMobile && showPlanFollowUpPrompt && activeProposedPlan !== null);
27792786
const showCollapsedMobilePromptRow =
27802787
isComposerCollapsedMobile && !isComposerApprovalState && pendingUserInputs.length === 0;
@@ -3386,18 +3393,24 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
33863393
requestId: string | null;
33873394
questionId: string | null;
33883395
} | null>(null);
3396+
// Where the message caret was when the user chose to answer a question.
3397+
const messageCaretBeforeAnsweringRef = useRef<{ text: string; cursor: number } | null>(null);
33893398

33903399
useEffect(() => {
33913400
const nextCustomAnswer = activePendingProgress?.customAnswer;
33923401
if (typeof nextCustomAnswer !== "string") {
3393-
// The question is gone and the editor shows the thread draft again. The
3394-
// ref still holds the last answer text, and Send reads the ref. Place
3395-
// the caret at the end so the next keystroke appends.
3402+
// The editor shows the thread draft again: the user went back to it, or
3403+
// the question is gone. The ref still holds the last answer text, and
3404+
// Send reads the ref. Return the caret to where the message was left.
33963405
if (lastSyncedPendingInputRef.current !== null) {
33973406
promptRef.current = prompt;
3398-
const { cursor, trigger } = composerStateAtPromptEnd(prompt);
3407+
const { cursor, trigger } = composerStateForReturningDraft(
3408+
prompt,
3409+
messageCaretBeforeAnsweringRef.current,
3410+
);
33993411
setComposerCursor(cursor);
34003412
resetComposerTrigger(trigger);
3413+
messageCaretBeforeAnsweringRef.current = null;
34013414
}
34023415
lastSyncedPendingInputRef.current = null;
34033416
return;
@@ -3849,6 +3862,14 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
38493862
};
38503863
}, [composerCursor, promptRef]);
38513864

3865+
const toggleAnsweringPendingUserInput = useCallback(() => {
3866+
if (!activePendingProgress) {
3867+
const { value, cursor } = readComposerSnapshot();
3868+
messageCaretBeforeAnsweringRef.current = { text: value, cursor };
3869+
}
3870+
onToggleAnsweringPendingUserInput();
3871+
}, [activePendingProgress, onToggleAnsweringPendingUserInput, readComposerSnapshot]);
3872+
38523873
const resolveActiveComposerTrigger = useCallback((): {
38533874
snapshot: { value: string; cursor: number; expandedCursor: number };
38543875
trigger: ComposerTrigger | null;
@@ -4202,7 +4223,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
42024223
compactDisabled ||
42034224
noProviderAvailable ||
42044225
activePendingApproval !== null ||
4205-
pendingUserInputs.length > 0 ||
4226+
availablePendingUserInputs.length > 0 ||
42064227
phase === "running" ||
42074228
isSendBusy ||
42084229
isConnecting ||
@@ -4214,12 +4235,12 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
42144235
}, [
42154236
activePendingApproval,
42164237
activeThreadId,
4238+
availablePendingUserInputs.length,
42174239
compactDisabled,
42184240
isConnecting,
42194241
isSendBusy,
42204242
noProviderAvailable,
42214243
onCompactContext,
4222-
pendingUserInputs.length,
42234244
phase,
42244245
]);
42254246
const expandMobileComposer = useCallback(() => {
@@ -5029,7 +5050,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
50295050
}, []);
50305051
const hasBannerItems = props.bannerItems.length > 0;
50315052
const hasBlockingComposerTopDrawer =
5032-
activePendingApproval !== null || pendingUserInputs.length > 0;
5053+
activePendingApproval !== null || availablePendingUserInputs.length > 0;
50335054
const showInlineTasksBadge =
50345055
activeTasksProgress !== null &&
50355056
activeTaskSteps !== null &&
@@ -6613,16 +6634,19 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
66136634
/>
66146635
</ComposerBanner.Actions>
66156636
</ComposerBanner.Row>
6616-
) : !isComposerCollapsedMobile && pendingUserInputs.length > 0 ? (
6637+
) : availablePendingUserInputs.length > 0 &&
6638+
(!isComposerCollapsedMobile || !activePendingProgress) ? (
66176639
<ComposerPendingUserInputPanel
6618-
pendingUserInputs={pendingUserInputs}
6640+
pendingUserInputs={availablePendingUserInputs}
66196641
respondingRequestIds={
66206642
activePendingIsResponding && activePendingUserInput
66216643
? [...respondingRequestIds, activePendingUserInput.requestId]
66226644
: respondingRequestIds
66236645
}
66246646
answers={activePendingDraftAnswers}
66256647
questionIndex={activePendingQuestionIndex}
6648+
isAnswering={activePendingProgress !== null}
6649+
onToggleAnswering={toggleAnsweringPendingUserInput}
66266650
onToggleOption={onSelectActivePendingUserInputOption}
66276651
onAdvance={onAdvanceActivePendingUserInput}
66286652
onDismiss={onDismissActivePendingUserInput}
@@ -6643,6 +6667,8 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
66436667
}
66446668
answers={activePendingDraftAnswers}
66456669
questionIndex={activePendingQuestionIndex}
6670+
isAnswering
6671+
onToggleAnswering={toggleAnsweringPendingUserInput}
66466672
onToggleOption={onSelectActivePendingUserInputOption}
66476673
onAdvance={onAdvanceActivePendingUserInput}
66486674
onDismiss={onDismissActivePendingUserInput}
@@ -7350,7 +7376,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
73507376
isComposerApprovalState ||
73517377
projectSelectionRequired ||
73527378
isChoiceOnlyPendingQuestion ||
7353-
activePendingIsResponding
7379+
(activePendingProgress !== null && activePendingIsResponding)
73547380
}
73557381
/>
73567382
</ComposerContextActionsContext>

‎apps/web/src/components/chat/ComposerPendingUserInputPanel.test.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ function renderPanel(pendingUserInput: PendingUserInput = prompt) {
3131
respondingRequestIds={[]}
3232
answers={{}}
3333
questionIndex={0}
34+
isAnswering={false}
35+
onToggleAnswering={() => {}}
3436
onToggleOption={() => {}}
3537
onAdvance={() => {}}
3638
onDismiss={() => {}}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@ import {
88
import { CheckIcon } from "lucide-react";
99
import { Collapsible, CollapsiblePanel, CollapsibleTrigger } from "../ui/collapsible";
1010
import { cn } from "~/lib/utils";
11+
import { Button } from "../ui/button";
1112
import { ComposerBanner } from "./ComposerBanner";
1213

1314
interface PendingUserInputPanelProps {
1415
pendingUserInputs: PendingUserInput[];
1516
respondingRequestIds: RuntimeRequestId[];
1617
answers: Record<string, PendingUserInputDraftAnswer>;
1718
questionIndex: number;
19+
/** Whether the composer is answering the question instead of writing a message. */
20+
isAnswering: boolean;
21+
onToggleAnswering: () => void;
1822
onToggleOption: (questionId: string, optionValue: string) => void;
1923
onAdvance: () => void;
2024
onDismiss: (requestId: RuntimeRequestId) => void;
@@ -25,6 +29,8 @@ export const ComposerPendingUserInputPanel = memo(function ComposerPendingUserIn
2529
respondingRequestIds,
2630
answers,
2731
questionIndex,
32+
isAnswering,
33+
onToggleAnswering,
2834
onToggleOption,
2935
onAdvance,
3036
onDismiss,
@@ -40,6 +46,8 @@ export const ComposerPendingUserInputPanel = memo(function ComposerPendingUserIn
4046
isResponding={respondingRequestIds.includes(activePrompt.requestId)}
4147
answers={answers}
4248
questionIndex={questionIndex}
49+
isAnswering={isAnswering}
50+
onToggleAnswering={onToggleAnswering}
4351
onToggleOption={onToggleOption}
4452
onAdvance={onAdvance}
4553
onDismiss={onDismiss}
@@ -52,6 +60,8 @@ const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard(
5260
isResponding,
5361
answers,
5462
questionIndex,
63+
isAnswering,
64+
onToggleAnswering,
5565
onToggleOption,
5666
onAdvance,
5767
onDismiss,
@@ -60,6 +70,8 @@ const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard(
6070
isResponding: boolean;
6171
answers: Record<string, PendingUserInputDraftAnswer>;
6272
questionIndex: number;
73+
isAnswering: boolean;
74+
onToggleAnswering: () => void;
6375
onToggleOption: (questionId: string, optionValue: string) => void;
6476
onAdvance: () => void;
6577
onDismiss: (requestId: RuntimeRequestId) => void;
@@ -291,6 +303,17 @@ const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard(
291303
);
292304
})}
293305
</div>
306+
<div className="mt-2">
307+
<Button
308+
type="button"
309+
size="xs"
310+
variant="outline"
311+
disabled={responseDisabled}
312+
onClick={onToggleAnswering}
313+
>
314+
{isAnswering ? "Back to message" : "Answer question"}
315+
</Button>
316+
</div>
294317
</ComposerBanner.Body>
295318
</ComposerBanner.Scroll>
296319
</CollapsiblePanel>

‎apps/web/src/composer-logic.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
collapseExpandedComposerCursor,
1919
composerSubmissionIntentForKey,
2020
composerStateAtPromptEnd,
21+
composerStateForReturningDraft,
2122
detectComposerTrigger,
2223
expandCollapsedComposerCursor,
2324
formatAssistantCitationForComposer,
@@ -593,6 +594,29 @@ describe("composerStateAtPromptEnd", () => {
593594
});
594595
});
595596

597+
describe("composerStateForReturningDraft", () => {
598+
it("puts the caret back where the user left the message", () => {
599+
const prompt = "Also keep the README short, and";
600+
601+
expect(composerStateForReturningDraft(prompt, { text: prompt, cursor: 5 })).toEqual({
602+
cursor: 5,
603+
trigger: null,
604+
});
605+
});
606+
607+
it("continues at the end when the message changed while answering", () => {
608+
const prompt = carryDisplacedCustomAnswerIntoPrompt("first half", "second half");
609+
610+
expect(composerStateForReturningDraft(prompt, { text: "first half", cursor: 5 })).toEqual(
611+
composerStateAtPromptEnd(prompt),
612+
);
613+
});
614+
615+
it("continues at the end when no caret was saved", () => {
616+
expect(composerStateForReturningDraft("draft", null)).toEqual({ cursor: 5, trigger: null });
617+
});
618+
});
619+
596620
describe("collapseExpandedComposerCursor", () => {
597621
it("keeps cursor unchanged when no mention segment is present", () => {
598622
expect(collapseExpandedComposerCursor("plain text", 5)).toBe(5);

‎apps/web/src/composer-logic.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,24 @@ export function composerStateAtPromptEnd(text: string): {
303303
};
304304
}
305305

306+
/**
307+
* Caret and trigger for a draft the editor shows again after answering a
308+
* question: where the user left it, or the end if the draft changed meanwhile.
309+
*/
310+
export function composerStateForReturningDraft(
311+
text: string,
312+
left: { readonly text: string; readonly cursor: number } | null,
313+
): { cursor: number; trigger: ComposerTrigger | null } {
314+
if (left === null || left.text !== text) {
315+
return composerStateAtPromptEnd(text);
316+
}
317+
const cursor = clampCollapsedComposerCursor(text, left.cursor);
318+
return {
319+
cursor,
320+
trigger: detectComposerTrigger(text, expandCollapsedComposerCursor(text, cursor)),
321+
};
322+
}
323+
306324
export function parseStandaloneComposerSlashCommand(
307325
text: string,
308326
): Exclude<ComposerSlashCommand, "model"> | null {

‎docs/user/question-attachments.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Files in question answers
22

3-
When an agent asks a question that accepts a custom answer, use **Attach files** or paste an image into the answer field. On mobile, use the attachment button to choose photos or files. You can send a file by itself, with a selected option, or with a typed answer.
3+
When an agent asks a question that accepts a custom answer, choose **Answer question** on web or desktop, then use **Attach files** or paste an image into the answer field. Until then the composer keeps the message you are writing, and **Back to message** returns to it. On mobile, use the attachment button to choose photos or files. You can send a file by itself, with a selected option, or with a typed answer.
44

55
Each question keeps its own attachments as you move between questions. Your normal prompt draft stays separate. Wait for uploads to finish before submitting; retry or remove any failed upload. A failed response keeps the draft available to try again.
66

0 commit comments

Comments
 (0)