Skip to content
Open
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
4 changes: 0 additions & 4 deletions apps/web/src/components/ChatView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1497,7 +1497,6 @@ export default function ChatView(props: ChatViewProps) {
onSelectActivePendingUserInputOption: onSelectActivePendingUserInputOptionStable,
onChangeActivePendingUserInputCustomAnswer,
onAdvanceActivePendingUserInput,
onAdvanceActivePendingUserInputStable,
onPreviousActivePendingUserInputQuestion: onPreviousActivePendingUserInputQuestionStable,
} = usePendingThreadRequests({
environmentId,
Expand Down Expand Up @@ -4015,9 +4014,6 @@ export default function ChatView(props: ChatViewProps) {
onSelectActivePendingUserInputOption={
onSelectActivePendingUserInputOptionStable
}
onAdvanceActivePendingUserInput={
onAdvanceActivePendingUserInputStable
}
onDismissActivePendingUserInput={onDismissUserInputStable}
onPreviousActivePendingUserInputQuestion={
onPreviousActivePendingUserInputQuestionStable
Expand Down
3 changes: 0 additions & 3 deletions apps/web/src/components/chat/ChatComposer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,6 @@ export interface ChatComposerProps {
onResume: () => void;
onInterrupt: () => void;
onSelectActivePendingUserInputOption: (questionId: string, optionValue: string) => void;
onAdvanceActivePendingUserInput: () => void;
onDismissActivePendingUserInput: (requestId: RuntimeRequestId) => void;
onPreviousActivePendingUserInputQuestion: () => void;
onChangeActivePendingUserInputCustomAnswer: (
Expand Down Expand Up @@ -618,7 +617,6 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
onResume,
onInterrupt,
onSelectActivePendingUserInputOption,
onAdvanceActivePendingUserInput,
onDismissActivePendingUserInput,
onPreviousActivePendingUserInputQuestion,
onChangeActivePendingUserInputCustomAnswer,
Expand Down Expand Up @@ -3839,7 +3837,6 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
answers={activePendingDraftAnswers}
questionIndex={activePendingQuestionIndex}
onToggleOption={onSelectActivePendingUserInputOption}
onAdvance={onAdvanceActivePendingUserInput}
onDismiss={onDismissActivePendingUserInput}
/>
) : null}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ function panel(onToggleOption: (questionId: string, optionValue: string) => void
answers={{}}
questionIndex={0}
onToggleOption={onToggleOption}
onAdvance={vi.fn()}
onDismiss={vi.fn()}
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ const renderPanel = (questions: Question[], questionIndex = 0) =>
answers={{}}
questionIndex={questionIndex}
onToggleOption={() => {}}
onAdvance={() => {}}
onDismiss={() => {}}
/>,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { DraftId, useComposerDraftStore } from "../../composerDraftStore";
import { makeThreadProjectionFixture } from "../../test-fixtures";
import { toastManager } from "../ui/toast";
import { usePendingThreadRequests, usePendingUserInputDraft } from "./usePendingThreadRequests";
import { ComposerPendingUserInputPanel } from "./ComposerPendingUserInputPanel";

const threadRef = { environmentId: EnvironmentId.make("env"), threadId: ThreadId.make("thread") };

Expand All @@ -33,7 +34,10 @@ const prompt = {
id: `question-${index}`,
header: `Question ${index + 1}`,
question: `Choose for question ${index + 1}`,
options: [{ label: "Incremental", description: "One module at a time" }],
options: [
{ label: "Incremental", description: "One module at a time" },
{ label: "All at once", description: "One release" },
],
multiSelect: false,
})),
};
Expand Down Expand Up @@ -217,31 +221,48 @@ function QuestionPositionProbe(props: ProbeProps) {
return (
<>
<output>Question {state.activePendingQuestionIndex + 1}</output>
<button
type="button"
onClick={() => state.onSelectActivePendingUserInputOption("question-0", "Incremental")}
>
Choose Incremental
</button>
<ComposerPendingUserInputPanel
pendingUserInputs={state.pendingUserInputs}
cwd={undefined}
threadRef={{ environmentId: props.environmentId, threadId: props.projection.thread.id }}
respondingRequestIds={[]}
answers={state.activePendingDraftAnswers}
questionIndex={state.activePendingQuestionIndex}
onToggleOption={state.onSelectActivePendingUserInputOption}
onDismiss={state.onDismissUserInput}
/>
<button type="button" onClick={() => state.onAdvanceActivePendingUserInput()}>
Submit
</button>
</>
);
}

it("stays on an unanswered question when the composer submits", async () => {
it("waits for explicit confirmation and lets the user correct a question's selection", async () => {
await using screen = await render(
<QuestionPositionProbe
environmentId={EnvironmentId.make("unanswered-environment")}
projection={questionProjection(threadRef.threadId)}
/>,
);
using _timers = fakeTimers();
const submit = screen.getByRole("button", { name: "Submit" });
await userEvent.click(submit);
await expect.element(screen.getByRole("status")).toHaveTextContent("Question 1");

await userEvent.click(screen.getByRole("button", { name: "Choose Incremental" }));
const incremental = screen.getByRole("button", { name: /Incremental/ });
const allAtOnce = screen.getByRole("button", { name: /All at once/ });
await userEvent.click(incremental);
await act(() => vi.advanceTimersByTimeAsync(250));
await expect.element(screen.getByRole("status")).toHaveTextContent("Question 1");
await expect.element(incremental).toHaveAttribute("aria-pressed", "true");

await userEvent.keyboard("2");
await act(() => vi.advanceTimersByTimeAsync(250));
await expect.element(screen.getByRole("status")).toHaveTextContent("Question 1");
await expect.element(incremental).toHaveAttribute("aria-pressed", "false");
await expect.element(allAtOnce).toHaveAttribute("aria-pressed", "true");

await userEvent.click(submit);
await expect.element(screen.getByRole("status")).toHaveTextContent("Question 2");
});
Expand Down
61 changes: 5 additions & 56 deletions apps/web/src/components/chat/ComposerPendingUserInputPanel.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { type RuntimeRequestId, type ScopedThreadRef } from "@t3tools/contracts";
import { markdownProseDirection, textDirection } from "~/textDirection";
import { useGT } from "gt-react";
import { memo, use, useEffect, useEffectEvent, useRef, useState } from "react";
import { memo, use, useEffect, useEffectEvent, useState } from "react";
import { BackgroundThreadContext } from "./backgroundThread";
import { useStableCallback } from "~/hooks/useStableCallback";
import { type PendingUserInput } from "../../session-logic";
import type { PendingUserInputDraftAnswer } from "@t3tools/client-runtime/state/pending-user-input";
import { derivePendingUserInputProgress } from "../../pendingUserInput";
Expand All @@ -25,7 +24,6 @@ interface PendingUserInputPanelProps {
answers: Record<string, PendingUserInputDraftAnswer>;
questionIndex: number;
onToggleOption: (questionId: string, optionValue: string) => void;
onAdvance: () => void;
onDismiss: (requestId: RuntimeRequestId) => void;
}

Expand All @@ -38,7 +36,6 @@ export const ComposerPendingUserInputPanel = memo(function ComposerPendingUserIn
answers,
questionIndex,
onToggleOption,
onAdvance,
onDismiss,
}: PendingUserInputPanelProps) {
if (pendingUserInputs.length === 0) return null;
Expand All @@ -56,7 +53,6 @@ export const ComposerPendingUserInputPanel = memo(function ComposerPendingUserIn
answers={answers}
questionIndex={questionIndex}
onToggleOption={onToggleOption}
onAdvance={onAdvance}
onDismiss={onDismiss}
/>
);
Expand All @@ -71,7 +67,6 @@ const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard(
answers,
questionIndex,
onToggleOption,
onAdvance,
onDismiss,
}: {
prompt: PendingUserInput;
Expand All @@ -82,19 +77,12 @@ const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard(
answers: Record<string, PendingUserInputDraftAnswer>;
questionIndex: number;
onToggleOption: (questionId: string, optionValue: string) => void;
onAdvance: () => void;
onDismiss: (requestId: RuntimeRequestId) => void;
}) {
const gt = useGT();
// Message-mode requests remain answerable after their provider turn ends.
const canRespond = prompt.responseCapability !== "not_resumable";
const responseDisabled = disabled || isResponding || !canRespond;
const autoAdvanceTimerRef = useRef<number | null>(null);
const advance = useStableCallback(onAdvance);
const [optimisticSingleSelect, setOptimisticSingleSelect] = useState<{
questionId: string;
optionValue: string;
} | null>(null);
const [collapsedQuestionId, setCollapsedQuestionId] = useState<string | null>(null);
// Keep question identity stable for its shortcut listener.
const progress = derivePendingUserInputProgress(prompt.questions, answers, questionIndex);
Expand All @@ -107,44 +95,9 @@ const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard(
);
const isCollapsed = collapsedQuestionId !== null && collapsedQuestionId === activeQuestion?.id;

if (
optimisticSingleSelect &&
activeQuestion &&
!activeQuestion.multiSelect &&
(optimisticSingleSelect.questionId !== activeQuestion.id ||
(!customAnswerActive &&
progress.selectedOptionValues.includes(optimisticSingleSelect.optionValue)))
) {
setOptimisticSingleSelect(null);
}

// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- clears the auto-advance timer when the question changes, input is disabled, or the panel unmounts
useEffect(() => {
const clearAutoAdvance = () => {
if (autoAdvanceTimerRef.current !== null) {
window.clearTimeout(autoAdvanceTimerRef.current);
autoAdvanceTimerRef.current = null;
}
};
if (disabled || activeQuestion?.id === undefined) clearAutoAdvance();
return clearAutoAdvance;
}, [activeQuestion?.id, disabled]);

const handleOptionSelection = (questionId: string, optionValue: string) => {
if (disabled || isResponding) return;
if (activeQuestion?.multiSelect) {
onToggleOption(questionId, optionValue);
return;
}
setOptimisticSingleSelect({ questionId, optionValue });
if (responseDisabled) return;
onToggleOption(questionId, optionValue);
if (autoAdvanceTimerRef.current !== null) {
window.clearTimeout(autoAdvanceTimerRef.current);
}
autoAdvanceTimerRef.current = window.setTimeout(() => {
autoAdvanceTimerRef.current = null;
advance();
}, 200);
};

const selectOptionFromShortcut = useEffectEvent(handleOptionSelection);
Expand Down Expand Up @@ -276,11 +229,7 @@ const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard(
<div className="-ms-2 mt-1.5 sm:-ms-1.5">
{activeQuestion.options.map((option, index) => {
const optionValue = option.value ?? option.label;
const isOptimisticallySelected =
optimisticSingleSelect?.questionId === activeQuestion.id &&
optimisticSingleSelect.optionValue === optionValue;
const isSelected =
isOptimisticallySelected || draftSelectedOptions?.[index] === true;
const isSelected = draftSelectedOptions?.[index] === true;
const className = cn(
questionTextAlign,
"group flex w-full items-start gap-2.5 rounded-md bg-transparent py-1.5 ps-2 sm:ps-1.5 outline-none transition-colors duration-150 focus-visible:ring-1 focus-visible:ring-inset focus-visible:ring-primary/25",
Expand Down Expand Up @@ -318,7 +267,7 @@ const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard(
) : null}
</>
);
// The preview sits outside the button: it can hold links, and a click on a single-select option advances.
// The preview sits outside the button because it can hold links.
return (
<div key={`${activeQuestion.id}:${optionValue}`}>
<button
Expand Down Expand Up @@ -353,7 +302,7 @@ const ComposerPendingUserInputCard = memo(function ComposerPendingUserInputCard(
});

// Drawn with ui/radio-group's look instead of using its Radio: Radio only checks inside a
// RadioGroup, whose arrow keys select options and would trip the single-select auto-advance.
// RadioGroup, whose arrow keys would conflict with the panel's option shortcuts.
function OptionRadio({ checked }: { checked: boolean }) {
return (
<span
Expand Down
2 changes: 0 additions & 2 deletions apps/web/src/components/chat/usePendingThreadRequests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,6 @@ export function usePendingThreadRequests(input: {
if (!activePendingProgress.canAdvance) return;
setActivePendingUserInputQuestionIndex(activePendingProgress.questionIndex + 1);
};
const onAdvanceActivePendingUserInputStable = useStableCallback(onAdvanceActivePendingUserInput);

const onPreviousActivePendingUserInputQuestion = () => {
const { activePendingProgress } = readPendingUserInputDraft(draftScope);
Expand All @@ -676,7 +675,6 @@ export function usePendingThreadRequests(input: {
onSelectActivePendingUserInputOption: onSelectActivePendingUserInputOptionStable,
onChangeActivePendingUserInputCustomAnswer,
onAdvanceActivePendingUserInput,
onAdvanceActivePendingUserInputStable,
onPreviousActivePendingUserInputQuestion: onPreviousActivePendingUserInputQuestionStable,
};
}
Loading