Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions apps/web/src/components/ChatView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ import {
} from "./chat/timelineScrollAnchoring";
import {
buildPendingUserInputAnswers,
carryDisplacedCustomAnswerIntoPrompt,
derivePendingUserInputProgress,
setPendingUserInputCustomAnswer,
togglePendingUserInputOptionSelection,
Expand Down Expand Up @@ -8810,6 +8811,16 @@ export default function ChatView(props: ChatViewProps) {
if (!activePendingUserInput) {
return;
}
// The option replaces the custom answer. Anything typed there is the
// user's text, so it goes back to the thread draft instead of vanishing.
const displacedAnswer =
pendingUserInputAnswersByRequestId[activePendingRequestKey]?.[questionId]?.customAnswer;
const currentPrompt =
useComposerDraftStore.getState().getComposerDraft(composerDraftTarget)?.prompt ?? "";
const nextPrompt = carryDisplacedCustomAnswerIntoPrompt(currentPrompt, displacedAnswer);
if (nextPrompt !== currentPrompt) {
setComposerDraftPrompt(composerDraftTarget, nextPrompt);
}
setPendingUserInputAnswersByRequestId((existing) => {
const question =
(activePendingProgress?.activeQuestion?.id === questionId
Expand Down Expand Up @@ -8839,7 +8850,10 @@ export default function ChatView(props: ChatViewProps) {
activePendingProgress?.activeQuestion,
activePendingUserInput,
activePendingRequestKey,
composerDraftTarget,
composerRef,
pendingUserInputAnswersByRequestId,
setComposerDraftPrompt,
],
);

Expand Down
6 changes: 6 additions & 0 deletions apps/web/src/components/chat/ChatComposer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3080,6 +3080,11 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
useEffect(() => {
const nextCustomAnswer = activePendingProgress?.customAnswer;
if (typeof nextCustomAnswer !== "string") {
// The question is gone and the editor shows the thread draft again. The
// ref still holds the last answer text, and Send reads the ref.
if (lastSyncedPendingInputRef.current !== null) {
promptRef.current = prompt;
}
lastSyncedPendingInputRef.current = null;
return;
}
Expand Down Expand Up @@ -3114,6 +3119,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
activePendingProgress?.customAnswer,
activePendingProgress?.activeQuestion?.id,
activePendingUserInput?.requestId,
prompt,
promptRef,
]);

Expand Down
20 changes: 20 additions & 0 deletions apps/web/src/pendingUserInput.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { describe, expect, it } from "vite-plus/test";

import {
buildPendingUserInputAnswers,
carryDisplacedCustomAnswerIntoPrompt,
countAnsweredPendingUserInputQuestions,
derivePendingUserInputProgress,
resolvePendingUserInputAnswer,
Expand Down Expand Up @@ -321,3 +322,22 @@ it("accepts attachment-only answers after every upload finishes", () => {
}),
).toBeNull();
});

describe("carryDisplacedCustomAnswerIntoPrompt", () => {
it("keeps the thread draft when nothing was typed into the answer", () => {
expect(carryDisplacedCustomAnswerIntoPrompt("draft", undefined)).toBe("draft");
expect(carryDisplacedCustomAnswerIntoPrompt("draft", " ")).toBe("draft");
});

it("moves the typed answer into an empty thread draft", () => {
expect(carryDisplacedCustomAnswerIntoPrompt("", "also rename the flag ")).toBe(
"also rename the flag",
);
});

it("appends the typed answer after an existing thread draft", () => {
expect(carryDisplacedCustomAnswerIntoPrompt("first half\n", "second half")).toBe(
"first half\n\nsecond half",
);
});
});
22 changes: 22 additions & 0 deletions apps/web/src/pendingUserInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,28 @@ export function setPendingUserInputCustomAnswer(
};
}

const DISPLACED_ANSWER_SEPARATOR = "\n\n";

/**
* Selecting an option replaces the custom answer, because a non-empty custom
* answer outranks selected options in `resolvePendingUserInputAnswer`. Text the
* user typed into the answer field must not vanish on that click: it moves back
* into the thread draft, after whatever was already waiting there.
*/
export function carryDisplacedCustomAnswerIntoPrompt(
prompt: string,
customAnswer: string | undefined,
): string {
const displaced = customAnswer?.trim() ?? "";
if (displaced.length === 0) {
return prompt;
}
if (prompt.trim().length === 0) {
return displaced;
}
return `${prompt.trimEnd()}${DISPLACED_ANSWER_SEPARATOR}${displaced}`;
}

export function togglePendingUserInputOptionSelection(
question: UserInputQuestion,
draft: PendingUserInputDraftAnswer | undefined,
Expand Down
Loading