Skip to content

Commit 603d9b4

Browse files
committed
fix(web): a question no longer traps the message you were writing
Once a pending question showed, it took the composer with no way back to the message short of answering. The panel now offers Back to message and Answer question.
1 parent 3857132 commit 603d9b4

8 files changed

Lines changed: 118 additions & 18 deletions

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10294,8 +10294,11 @@ export default function ChatView(props: ChatViewProps) {
1029410294
},
1029510295
};
1029610296
});
10297-
promptRef.current = "";
10298-
composerRef.current?.resetCursorState({ cursor: 0 });
10297+
// Only an answering composer shows the custom answer the option cleared.
10298+
if (composerRef.current?.getSendContext().answeringPendingUserInput !== false) {
10299+
promptRef.current = "";
10300+
composerRef.current?.resetCursorState({ cursor: 0 });
10301+
}
1029910302
},
1030010303
[
1030110304
activePendingProgress?.activeQuestion,

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

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ import {
8181
collapseExpandedComposerCursor,
8282
composerSubmissionIntentForKey,
8383
composerStateAtPromptEnd,
84+
composerStateForReturningDraft,
8485
detectComposerTrigger,
8586
expandCollapsedComposerCursor,
8687
formatAssistantCitationForComposer,
@@ -1830,9 +1831,17 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
18301831
const holdingUserInput = incomingPendingUserInputs[0]
18311832
? heldRequestIds.has(incomingPendingUserInputs[0].requestId)
18321833
: false;
1833-
const pendingUserInputs = holdingUserInput ? [] : incomingPendingUserInputs;
1834-
const activePendingProgress = holdingUserInput ? null : incomingPendingProgress;
1835-
const activePendingIsResponding = !holdingUserInput && incomingPendingIsResponding;
1834+
// The panel shows a question once it is no longer held. The editor, Send and
1835+
// attachments belong to it unless the user went Back to message.
1836+
const [backToMessageRequestId, setBackToMessageRequestId] = useState<string | null>(null);
1837+
const shownPendingUserInputs = holdingUserInput ? [] : incomingPendingUserInputs;
1838+
const shownPendingUserInput = shownPendingUserInputs[0] ?? null;
1839+
const shownPendingIsResponding = !holdingUserInput && incomingPendingIsResponding;
1840+
const answeringUserInput =
1841+
shownPendingUserInput !== null && shownPendingUserInput.requestId !== backToMessageRequestId;
1842+
const pendingUserInputs = answeringUserInput ? shownPendingUserInputs : [];
1843+
const activePendingProgress = answeringUserInput ? incomingPendingProgress : null;
1844+
const activePendingIsResponding = answeringUserInput && incomingPendingIsResponding;
18361845
const isLiteralPendingAnswer = activePendingProgress?.activeQuestion?.initialAnswer !== undefined;
18371846
// Opening a running thread resyncs for a few frames. Show the sync row, and
18381847
// hide the tasks row for it, only when the sync lasts. Logic that depends on
@@ -2876,7 +2885,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
28762885
activePendingProgress?.activeQuestion?.allowCustomAnswer === false;
28772886
const showComposerTopDrawer =
28782887
isComposerApprovalState ||
2879-
pendingUserInputs.length > 0 ||
2888+
shownPendingUserInputs.length > 0 ||
28802889
(!isComposerCollapsedMobile && showPlanFollowUpPrompt && activeProposedPlan !== null);
28812890
const showCollapsedMobilePromptRow =
28822891
isComposerCollapsedMobile && !isComposerApprovalState && pendingUserInputs.length === 0;
@@ -3493,16 +3502,23 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
34933502
requestId: string | null;
34943503
questionId: string | null;
34953504
} | null>(null);
3505+
// Where the message caret was while the editor last showed the message, so
3506+
// the effect below can put it back after a question had the editor.
3507+
const messageCaretRef = useRef<{ text: string; cursor: number } | null>(null);
3508+
useEffect(() => {
3509+
if (activePendingProgress !== null || lastSyncedPendingInputRef.current !== null) return;
3510+
messageCaretRef.current = { text: prompt, cursor: composerCursor };
3511+
}, [activePendingProgress, composerCursor, prompt]);
34963512

34973513
useEffect(() => {
34983514
const nextCustomAnswer = activePendingProgress?.customAnswer;
34993515
if (typeof nextCustomAnswer !== "string") {
3500-
// The question is gone and the editor shows the thread draft again. The
3501-
// ref still holds the last answer text, and Send reads the ref. Place
3502-
// the caret at the end so the next keystroke appends.
3516+
// The editor shows the thread draft again: the user went back to it, or
3517+
// the question is gone. The ref still holds the last answer text, and
3518+
// Send reads the ref. Return the caret to where the message was left.
35033519
if (lastSyncedPendingInputRef.current !== null) {
35043520
promptRef.current = prompt;
3505-
const { cursor, trigger } = composerStateAtPromptEnd(prompt);
3521+
const { cursor, trigger } = composerStateForReturningDraft(prompt, messageCaretRef.current);
35063522
setComposerCursor(cursor);
35073523
resetComposerTrigger(trigger);
35083524
}
@@ -3541,6 +3557,13 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
35413557
resetComposerTrigger,
35423558
]);
35433559

3560+
const toggleAnsweringPendingUserInput = useCallback(() => {
3561+
if (!shownPendingUserInput) return;
3562+
setBackToMessageRequestId(answeringUserInput ? shownPendingUserInput.requestId : null);
3563+
// Focus once the editor shows the other draft, at the caret it brings back.
3564+
window.requestAnimationFrame(() => composerEditorRef.current?.focus());
3565+
}, [answeringUserInput, shownPendingUserInput]);
3566+
35443567
// ------------------------------------------------------------------
35453568
// Reset compositor state on thread/draft change
35463569
// ------------------------------------------------------------------
@@ -4347,7 +4370,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
43474370
compactDisabled ||
43484371
noProviderAvailable ||
43494372
activePendingApproval !== null ||
4350-
pendingUserInputs.length > 0 ||
4373+
incomingPendingUserInputs.length > 0 ||
43514374
phase === "running" ||
43524375
isSendBusy ||
43534376
isConnecting ||
@@ -4362,11 +4385,11 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
43624385
compactDisabled,
43634386
composerDraftTarget,
43644387
environmentId,
4388+
incomingPendingUserInputs.length,
43654389
isConnecting,
43664390
isSendBusy,
43674391
noProviderAvailable,
43684392
onCompactContext,
4369-
pendingUserInputs.length,
43704393
phase,
43714394
]);
43724395
const expandMobileComposer = useCallback(() => {
@@ -5181,7 +5204,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
51815204
}, []);
51825205
const hasBannerItems = props.bannerItems.length > 0;
51835206
const hasBlockingComposerTopDrawer =
5184-
activePendingApproval !== null || pendingUserInputs.length > 0;
5207+
activePendingApproval !== null || shownPendingUserInputs.length > 0;
51855208
const showInlineTasksBadge =
51865209
activeTasksProgress !== null &&
51875210
activeTaskSteps !== null &&
@@ -6802,17 +6825,19 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
68026825
/>
68036826
</ComposerBanner.Actions>
68046827
</ComposerBanner.Row>
6805-
) : !isComposerCollapsedMobile && pendingUserInputs.length > 0 ? (
6828+
) : shownPendingUserInput && (!isComposerCollapsedMobile || !answeringUserInput) ? (
68066829
<ComposerPendingUserInputPanel
6807-
pendingUserInputs={pendingUserInputs}
6830+
pendingUserInputs={shownPendingUserInputs}
68086831
disabled={!canOperateThread}
68096832
respondingRequestIds={
6810-
activePendingIsResponding && activePendingUserInput
6811-
? [...respondingRequestIds, activePendingUserInput.requestId]
6833+
shownPendingIsResponding
6834+
? [...respondingRequestIds, shownPendingUserInput.requestId]
68126835
: respondingRequestIds
68136836
}
68146837
answers={activePendingDraftAnswers}
68156838
questionIndex={activePendingQuestionIndex}
6839+
isAnswering={answeringUserInput}
6840+
onToggleAnswering={toggleAnsweringPendingUserInput}
68166841
onToggleOption={onSelectActivePendingUserInputOption}
68176842
onAdvance={onAdvanceActivePendingUserInput}
68186843
onDismiss={onDismissActivePendingUserInput}
@@ -6834,6 +6859,8 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
68346859
}
68356860
answers={activePendingDraftAnswers}
68366861
questionIndex={activePendingQuestionIndex}
6862+
isAnswering
6863+
onToggleAnswering={toggleAnsweringPendingUserInput}
68376864
onToggleOption={onSelectActivePendingUserInputOption}
68386865
onAdvance={onAdvanceActivePendingUserInput}
68396866
onDismiss={onDismissActivePendingUserInput}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ it("cancels an answer's pending auto-submit on revocation and resumes after a ne
4747
respondingRequestIds={[]}
4848
answers={{}}
4949
questionIndex={0}
50+
isAnswering
51+
onToggleAnswering={() => {}}
5052
onToggleOption={onToggleOption}
5153
onAdvance={onAdvance}
5254
onDismiss={onDismiss}

‎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
35+
onToggleAnswering={() => {}}
3436
onToggleOption={() => {}}
3537
onAdvance={() => {}}
3638
onDismiss={() => {}}

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ 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 {
@@ -16,6 +17,9 @@ interface PendingUserInputPanelProps {
1617
respondingRequestIds: RuntimeRequestId[];
1718
answers: Record<string, PendingUserInputDraftAnswer>;
1819
questionIndex: number;
20+
/** Whether the composer holds the answer rather than the user's message. */
21+
isAnswering: boolean;
22+
onToggleAnswering: () => void;
1923
onToggleOption: (questionId: string, optionValue: string) => void;
2024
onAdvance: () => void;
2125
onDismiss: (requestId: RuntimeRequestId) => void;
@@ -27,6 +31,8 @@ export const ComposerPendingUserInputPanel = memo(function ComposerPendingUserIn
2731
respondingRequestIds,
2832
answers,
2933
questionIndex,
34+
isAnswering,
35+
onToggleAnswering,
3036
onToggleOption,
3137
onAdvance,
3238
onDismiss,
@@ -43,6 +49,8 @@ export const ComposerPendingUserInputPanel = memo(function ComposerPendingUserIn
4349
isResponding={respondingRequestIds.includes(activePrompt.requestId)}
4450
answers={answers}
4551
questionIndex={questionIndex}
52+
isAnswering={isAnswering}
53+
onToggleAnswering={onToggleAnswering}
4654
onToggleOption={onToggleOption}
4755
onAdvance={onAdvance}
4856
onDismiss={onDismiss}
@@ -56,6 +64,8 @@ const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard(
5664
isResponding,
5765
answers,
5866
questionIndex,
67+
isAnswering,
68+
onToggleAnswering,
5969
onToggleOption,
6070
onAdvance,
6171
onDismiss,
@@ -65,6 +75,8 @@ const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard(
6575
isResponding: boolean;
6676
answers: Record<string, PendingUserInputDraftAnswer>;
6777
questionIndex: number;
78+
isAnswering: boolean;
79+
onToggleAnswering: () => void;
6880
onToggleOption: (questionId: string, optionValue: string) => void;
6981
onAdvance: () => void;
7082
onDismiss: (requestId: RuntimeRequestId) => void;
@@ -304,6 +316,18 @@ const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard(
304316
);
305317
})}
306318
</div>
319+
<div className="mt-2">
320+
{/* Back to message stays available when the question cannot be answered. */}
321+
<Button
322+
type="button"
323+
size="xs"
324+
variant="outline"
325+
disabled={isAnswering ? disabled : responseDisabled}
326+
onClick={onToggleAnswering}
327+
>
328+
{isAnswering ? "Back to message" : "Answer question"}
329+
</Button>
330+
</div>
307331
</ComposerBanner.Body>
308332
</ComposerBanner.Scroll>
309333
</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
@@ -323,6 +323,24 @@ export function composerStateAtPromptEnd(
323323
};
324324
}
325325

326+
/**
327+
* Caret and trigger for the message when the editor shows it again after a
328+
* question: where the user left it, or the end if the message changed meanwhile.
329+
*/
330+
export function composerStateForReturningDraft(
331+
text: string,
332+
left: { readonly text: string; readonly cursor: number } | null,
333+
): { cursor: number; trigger: ComposerTrigger | null } {
334+
if (left === null || left.text !== text) {
335+
return composerStateAtPromptEnd(text);
336+
}
337+
const cursor = clampCollapsedComposerCursor(text, left.cursor);
338+
return {
339+
cursor,
340+
trigger: detectComposerTrigger(text, expandCollapsedComposerCursor(text, cursor)),
341+
};
342+
}
343+
326344
export function parseStandaloneComposerSlashCommand(
327345
text: string,
328346
): Exclude<ComposerSlashCommand, "model"> | null {

‎docs/user/question-attachments.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
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.
44

5-
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.
5+
Each question keeps its own attachments as you move between questions. Your normal prompt draft stays separate: on web and desktop, **Back to message** on the question returns to it without answering, and **Answer question** brings the answer field back. Wait for uploads to finish before submitting; retry or remove any failed upload. A failed response keeps the draft available to try again.
66

77
Files upload to the environment running the thread, including remote environments. The agent receives paths to those saved files with your answer and can open them with its available tools. Questions that only accept predefined choices do not offer attachments. Older servers need updating before this option appears.
88

0 commit comments

Comments
 (0)