mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-24 16:04:21 +00:00
fix(agents): answer AskUserQuestion in the conversation (#5034)
The open AskUserQuestion card was pinned above the composer: with several options it covered the conversation (max 50vh, its own scroll), and in a short conversation it floated away from its messages. The card now renders in the conversation at the asking entry and scrolls with it; picking an option on a single-select question moves on to the next one. Permission forms and questions that outlive their turn still wait above the composer at the composer's width. Closes #5033 Co-authored-by: panda-student <71381346+panda-student@users.noreply.github.com>
This commit is contained in:
@@ -47,10 +47,6 @@
|
||||
.fitWidth .composerArea {
|
||||
max-width: none;
|
||||
}
|
||||
.fitWidth .questionCard {
|
||||
width: calc(100% - var(--space-3) * 2);
|
||||
max-width: none;
|
||||
}
|
||||
|
||||
/* Under the composer: where the task works on the left; changes, meters and terminal on the right. */
|
||||
.statusBar {
|
||||
@@ -1559,18 +1555,11 @@
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
|
||||
/* An open AskUserQuestion: every question with its options, answered at once above the composer. */
|
||||
/* An open AskUserQuestion: every question with its options, answered at once in the conversation. */
|
||||
.questionCard {
|
||||
flex: none;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: var(--space-3);
|
||||
max-height: 50vh;
|
||||
overflow-y: auto;
|
||||
/* The composer's width, so the card sits exactly over the box it belongs to. */
|
||||
width: 100%;
|
||||
max-width: var(--session-content-width);
|
||||
margin: 0 auto 8px;
|
||||
padding: 12px;
|
||||
border: 1px solid var(--border-strong);
|
||||
border-radius: var(--radius-control);
|
||||
@@ -1578,6 +1567,19 @@
|
||||
font-size: var(--agent-fs-ui, var(--fs-ui));
|
||||
container: question-card / inline-size;
|
||||
}
|
||||
/* A question that outlived its turn waits above the composer, at the composer's width. */
|
||||
.questionCardPinned {
|
||||
flex: none;
|
||||
max-height: 50vh;
|
||||
overflow-y: auto;
|
||||
width: 100%;
|
||||
max-width: var(--session-content-width);
|
||||
margin: 0 auto 8px;
|
||||
}
|
||||
.fitWidth .questionCardPinned {
|
||||
width: calc(100% - var(--space-3) * 2);
|
||||
max-width: none;
|
||||
}
|
||||
.questionTabCheck {
|
||||
flex: none;
|
||||
color: var(--accent);
|
||||
@@ -1588,10 +1590,6 @@
|
||||
grid-template-columns: minmax(0, 1fr);
|
||||
min-width: 0;
|
||||
}
|
||||
.questionPanelsPaged {
|
||||
min-height: 0;
|
||||
overflow-y: auto;
|
||||
}
|
||||
.questionPanel {
|
||||
grid-area: 1 / 1;
|
||||
min-width: 0;
|
||||
|
||||
@@ -25,7 +25,7 @@ import {
|
||||
} from "./model";
|
||||
import { ChangesPane, useChangesOpen, useLocalHead } from "./session/ChangesPane";
|
||||
import { FoldedContent } from "./session/Folds";
|
||||
import { QuestionCard } from "./session/QuestionForm";
|
||||
import { QuestionCard, type QuestionAnswer } from "./session/QuestionForm";
|
||||
import { SessionComposer } from "./session/SessionComposer";
|
||||
import { Stream } from "./session/Stream";
|
||||
import { focusedSession, registerReachableSession, setFocusedSession, type FocusedSession } from "./sessionFocus";
|
||||
@@ -98,6 +98,8 @@ function TaskSession({ id, docked }: { id: string; docked: boolean }) {
|
||||
}
|
||||
}, [id, view, connected]);
|
||||
const openSubagent = useCallback((sub: string) => setPath((p) => [...p, sub]), []);
|
||||
// Answers in flight, kept across entry reloads so a refresh doesn't lose what was typed.
|
||||
const drafts = useRef(new Map<string, QuestionAnswer[]>());
|
||||
const [finding, setFinding] = useState(false);
|
||||
const [findFocus, setFindFocus] = useState(0);
|
||||
const [inputFocus, setInputFocus] = useState(0);
|
||||
@@ -164,6 +166,7 @@ function TaskSession({ id, docked }: { id: string; docked: boolean }) {
|
||||
findFocus={findFocus}
|
||||
fontZoom={fontZoom}
|
||||
onCloseFind={() => setFinding(false)}
|
||||
drafts={drafts}
|
||||
/>
|
||||
</FoldedContent.Provider>
|
||||
{view ? (
|
||||
@@ -172,7 +175,7 @@ function TaskSession({ id, docked }: { id: string; docked: boolean }) {
|
||||
</div>
|
||||
) : (
|
||||
<>
|
||||
<QuestionCard task={task} entries={entries ?? null} />
|
||||
<QuestionCard task={task} entries={entries ?? null} drafts={drafts} />
|
||||
<SessionComposer
|
||||
task={task}
|
||||
focusRequest={inputFocus}
|
||||
|
||||
@@ -131,7 +131,7 @@ export function PermissionLine({ task, entry }: { task: AgentTask; entry: Permis
|
||||
);
|
||||
}
|
||||
|
||||
interface QuestionAnswer {
|
||||
export interface QuestionAnswer {
|
||||
question: AgentQuestion;
|
||||
picked: string[];
|
||||
/** The typed answer; `null` while "Other" isn't chosen. */
|
||||
@@ -150,9 +150,16 @@ const plainLabel = (label: string) => (isRecommended(label) ? label.slice(0, -RE
|
||||
|
||||
type AsyncQuestionEntry = Extract<AgentEntry, { type: "question" }>;
|
||||
|
||||
/** Blocking requests take priority; async questions stay available after their turn ends. */
|
||||
export function QuestionCard({ task, entries }: { task: AgentTask; entries: AgentEntry[] | null }) {
|
||||
const drafts = useRef(new Map<string, QuestionAnswer[]>());
|
||||
/** Permission forms and questions that outlive their turn: pinned above the composer. */
|
||||
export function QuestionCard({
|
||||
task,
|
||||
entries,
|
||||
drafts,
|
||||
}: {
|
||||
task: AgentTask;
|
||||
entries: AgentEntry[] | null;
|
||||
drafts: RefObject<Map<string, QuestionAnswer[]>>;
|
||||
}) {
|
||||
useEffect(() => {
|
||||
const open = new Set(
|
||||
entries
|
||||
@@ -168,7 +175,7 @@ export function QuestionCard({ task, entries }: { task: AgentTask; entries: Agen
|
||||
const entry =
|
||||
entries?.find(
|
||||
(candidate): candidate is PermissionEntry =>
|
||||
candidate.type === "permission" && !candidate.chosen && asksInCard(candidate),
|
||||
candidate.type === "permission" && !candidate.chosen && !!candidate.form,
|
||||
) ??
|
||||
entries?.find((candidate): candidate is AsyncQuestionEntry => candidate.type === "question" && !candidate.chosen);
|
||||
if (entry?.type === "permission" && entry.form)
|
||||
@@ -180,20 +187,24 @@ export function QuestionCard({ task, entries }: { task: AgentTask; entries: Agen
|
||||
entry={entry}
|
||||
questions={entry.questions}
|
||||
drafts={drafts}
|
||||
pinned
|
||||
/>
|
||||
) : null;
|
||||
}
|
||||
|
||||
function QuestionForm({
|
||||
export function QuestionForm({
|
||||
task,
|
||||
entry,
|
||||
questions,
|
||||
drafts,
|
||||
pinned = false,
|
||||
}: {
|
||||
task: AgentTask;
|
||||
entry: PermissionEntry | AsyncQuestionEntry;
|
||||
questions: AgentQuestion[];
|
||||
drafts: RefObject<Map<string, QuestionAnswer[]>>;
|
||||
/** Above the composer rather than in the conversation. */
|
||||
pinned?: boolean;
|
||||
}) {
|
||||
const cacheKey = `${entry.type}:${entry.id}`;
|
||||
const [answers, setAnswers] = useState<QuestionAnswer[]>(
|
||||
@@ -210,15 +221,14 @@ function QuestionForm({
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const pending = useRef(false);
|
||||
const questionPrefix = useId();
|
||||
const panelsRef = useRef<HTMLDivElement>(null);
|
||||
const panelRef = useRef<HTMLDivElement>(null);
|
||||
// Paging with the buttons lands the keyboard focus on the question itself.
|
||||
const focusPanel = useRef(false);
|
||||
const multiple = answers.length > 1;
|
||||
const complete = answers.every(isAnswered);
|
||||
const last = position === answers.length - 1;
|
||||
const questionId = (part: string, index: number) => `${questionPrefix}-${part}-${index}`;
|
||||
useEffect(() => {
|
||||
if (panelsRef.current) panelsRef.current.scrollTop = 0;
|
||||
if (!focusPanel.current) return;
|
||||
focusPanel.current = false;
|
||||
panelRef.current?.focus();
|
||||
@@ -249,7 +259,13 @@ function QuestionForm({
|
||||
);
|
||||
const update = (previous: QuestionAnswer, next: QuestionAnswer) =>
|
||||
setAnswers(answers.map((answer) => (answer === previous ? next : answer)));
|
||||
const choose = (answer: QuestionAnswer, label: string, checked: boolean) =>
|
||||
const goTo = (next: number, moveFocus = false) => {
|
||||
if (next < 0 || next >= answers.length) return;
|
||||
focusPanel.current = moveFocus;
|
||||
setPosition(next);
|
||||
setHovered(null);
|
||||
};
|
||||
const choose = (answer: QuestionAnswer, label: string, checked: boolean) => {
|
||||
update(
|
||||
answer,
|
||||
answer.question.multiSelect
|
||||
@@ -259,6 +275,10 @@ function QuestionForm({
|
||||
}
|
||||
: { ...answer, picked: [label], other: null },
|
||||
);
|
||||
// Picking an option answers a single-select question in one go; answering it shows the next one.
|
||||
if (answer.question.multiSelect || isAnswered(answer) || last) return;
|
||||
goTo(position + 1, true);
|
||||
};
|
||||
const chooseOther = (answer: QuestionAnswer, checked: boolean) =>
|
||||
update(
|
||||
answer,
|
||||
@@ -271,15 +291,9 @@ function QuestionForm({
|
||||
if (option) choose(answer, option.label, !answer.picked.includes(option.label));
|
||||
else if (number === answer.question.options.length + 1) chooseOther(answer, answer.other === null);
|
||||
};
|
||||
const goTo = (next: number, moveFocus = false) => {
|
||||
if (next < 0 || next >= answers.length) return;
|
||||
focusPanel.current = moveFocus;
|
||||
setPosition(next);
|
||||
setHovered(null);
|
||||
};
|
||||
return (
|
||||
<form
|
||||
className={styles.questionCard}
|
||||
className={clsx(styles.questionCard, pinned && styles.questionCardPinned)}
|
||||
aria-label={entry.title}
|
||||
onSubmit={(event) => {
|
||||
event.preventDefault();
|
||||
@@ -318,7 +332,7 @@ function QuestionForm({
|
||||
</div>
|
||||
)}
|
||||
{/* All questions stay mounted in one grid cell, so the card keeps the tallest one's height. */}
|
||||
<div ref={panelsRef} className={clsx(styles.questionPanels, multiple && styles.questionPanelsPaged)}>
|
||||
<div className={styles.questionPanels}>
|
||||
{answers.map((item, index) => {
|
||||
const current = index === position;
|
||||
const group = questionId("group", index);
|
||||
|
||||
@@ -51,6 +51,7 @@ import { FindBar } from "./FindBar";
|
||||
import { OpenFolds, RevealFold } from "./Folds";
|
||||
import { Entry } from "./messages";
|
||||
import { ProcessRun, TurnFold } from "./ProcessRows";
|
||||
import { type QuestionAnswer } from "./QuestionForm";
|
||||
import { providerName } from "./shared";
|
||||
import { WorkingRow } from "./WorkingRow";
|
||||
|
||||
@@ -157,6 +158,8 @@ interface StreamProps {
|
||||
findFocus: number;
|
||||
fontZoom: number;
|
||||
onCloseFind: () => void;
|
||||
/** Answers in flight, kept across entry reloads so a refresh doesn't lose what was typed. */
|
||||
drafts: RefObject<Map<string, QuestionAnswer[]>>;
|
||||
}
|
||||
|
||||
/** Entries rendered at a time when a conversation opens at its end. */
|
||||
@@ -181,6 +184,7 @@ export function Stream({
|
||||
findFocus,
|
||||
fontZoom,
|
||||
onCloseFind,
|
||||
drafts,
|
||||
}: StreamProps) {
|
||||
const ref = useRef<HTMLDivElement>(null);
|
||||
useSelectionBoundary(ref);
|
||||
@@ -416,11 +420,12 @@ export function Stream({
|
||||
time={times?.get(row.index) ?? null}
|
||||
thoughtMs={thinking?.get(row.index) ?? null}
|
||||
working={row.index === lastIndex && busy}
|
||||
drafts={drafts}
|
||||
onOpenSubagent={onOpenSubagent}
|
||||
onDiagramDrawn={onDiagramDrawn}
|
||||
/>
|
||||
),
|
||||
[task, turns, replies, times, thinking, lastIndex, busy, onOpenSubagent, onDiagramDrawn],
|
||||
[task, turns, replies, times, thinking, lastIndex, busy, drafts, onOpenSubagent, onDiagramDrawn],
|
||||
);
|
||||
const renderConversationRow = (row: ConversationRow): ReactNode =>
|
||||
row.kind === "turn" ? (
|
||||
|
||||
@@ -5,7 +5,7 @@ import { basename } from "@/platform/project";
|
||||
import { copyText, fileIconName, Icon, IconButton, T } from "@/ui";
|
||||
import { notifications } from "@/workbench/contributions";
|
||||
import clsx from "clsx";
|
||||
import { memo, useLayoutEffect, useRef, useState } from "react";
|
||||
import { memo, useLayoutEffect, useRef, useState, type RefObject } from "react";
|
||||
import { AgentReply, MessageActions, ReplyActions } from "../AgentReply";
|
||||
import { ImagePreview, imageSrc } from "../ImagePreview";
|
||||
import { ReferencedText } from "../ReferenceChip";
|
||||
@@ -18,7 +18,7 @@ import { useAgentErrorText } from "../loginHint";
|
||||
import { isBusy, type ReplyTimes, type ReplyTurn } from "../model";
|
||||
import { useRevealedText } from "../reveal";
|
||||
import { forkTask, rerunFrom, resendMessage } from "../store";
|
||||
import { PermissionLine } from "./QuestionForm";
|
||||
import { PermissionLine, QuestionForm, type QuestionAnswer } from "./QuestionForm";
|
||||
import { type UserEntry } from "./shared";
|
||||
import { SubagentCard, ThoughtEntry, ToolEntry } from "./toolEntries";
|
||||
|
||||
@@ -101,6 +101,7 @@ export const Entry = memo(function Entry({
|
||||
time,
|
||||
thoughtMs,
|
||||
working,
|
||||
drafts,
|
||||
onOpenSubagent,
|
||||
onDiagramDrawn,
|
||||
}: {
|
||||
@@ -115,6 +116,8 @@ export const Entry = memo(function Entry({
|
||||
/** How long a finished thought took. */
|
||||
thoughtMs?: number | null;
|
||||
working: boolean;
|
||||
/** Answers in flight, kept across entry reloads so a refresh doesn't lose what was typed. */
|
||||
drafts: RefObject<Map<string, QuestionAnswer[]>>;
|
||||
onOpenSubagent: (id: string) => void;
|
||||
onDiagramDrawn: () => void;
|
||||
}) {
|
||||
@@ -190,8 +193,11 @@ export const Entry = memo(function Entry({
|
||||
</div>
|
||||
);
|
||||
case "permission":
|
||||
// Waiting for an answer, it is asked above the composer; the conversation keeps the record.
|
||||
return entry.chosen || entry.questions?.length ? <PermissionLine task={task} entry={entry} /> : null;
|
||||
// An open AskUserQuestion is the card itself, scrolling with the conversation; answered,
|
||||
// the record stays. Permission forms and plain approvals wait above the composer.
|
||||
if (entry.chosen) return <PermissionLine task={task} entry={entry} />;
|
||||
if (!entry.questions?.length) return null;
|
||||
return <QuestionForm key={entry.id} task={task} entry={entry} questions={entry.questions} drafts={drafts} />;
|
||||
case "question":
|
||||
return (
|
||||
<div className={clsx(styles.permissionLine, entry.chosen ? styles.permissionDone : styles.permissionAsking)}>
|
||||
|
||||
@@ -1248,6 +1248,22 @@ describe("an asynchronous Codex question", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("waits above the composer at its width, while a blocking question sits in the conversation", async () => {
|
||||
freeQuestion();
|
||||
const card = await screen.findByRole("form", { name: "What deadline?" });
|
||||
expect(card).toHaveClass(styles.questionCardPinned!);
|
||||
const blocking = {
|
||||
type: "permission",
|
||||
id: 100,
|
||||
title: "Blocking question",
|
||||
options: [],
|
||||
chosen: null,
|
||||
questions: [{ header: "", question: "Blocking question", multiSelect: false, options: [] }],
|
||||
} as AgentEntry;
|
||||
act(() => useAgents.setState({ entries: new Map([["A", [blocking]]]) }));
|
||||
expect(await screen.findByRole("form", { name: "Blocking question" })).not.toHaveClass(styles.questionCardPinned!);
|
||||
});
|
||||
|
||||
it("skips without submitting an answer", async () => {
|
||||
freeQuestion();
|
||||
const card = await screen.findByRole("form", { name: "What deadline?" });
|
||||
@@ -1434,11 +1450,10 @@ describe("a pending AskUserQuestion", () => {
|
||||
const submit = within(card).getByRole("button", { name: "Submit" });
|
||||
fireEvent.click(within(card).getByRole("radio", { name: /^List/ }));
|
||||
expect(submit).toHaveProperty("disabled", true);
|
||||
// Paging moves between questions, without losing the answers already given.
|
||||
expect(within(card).getByRole("button", { name: "Previous" })).toHaveProperty("disabled", true);
|
||||
fireEvent.click(within(card).getByRole("button", { name: "Next" }));
|
||||
// Picking an option completes the single-select question: the next one opens, focused.
|
||||
expect(within(card).getByRole("tab", { name: "Checks" })).toHaveAttribute("aria-selected", "true");
|
||||
expect(within(card).getByRole("tabpanel", { name: "Checks" })).toHaveFocus();
|
||||
// Paging back keeps the answer; picking again stays for review instead of advancing again.
|
||||
fireEvent.click(within(card).getByRole("tab", { name: "Layout" }));
|
||||
expect(within(card).getByRole("radio", { name: /^List/ })).toHaveProperty("checked", true);
|
||||
fireEvent.click(within(card).getByRole("tab", { name: "Checks" }));
|
||||
|
||||
Reference in New Issue
Block a user