AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
feat(platform/copilot): selectable ask_question options and Needs-You prompting rule (#14156)
Abhimanyu Yadavbe8f3751ffa2
diff --git a/autogpt_platform/backend/backend/copilot/prompting.py b/autogpt_platform/backend/backend/copilot/prompting.pyindex 904b983adc..411d2a17b6 100644--- a/autogpt_platform/backend/backend/copilot/prompting.py+++ b/autogpt_platform/backend/backend/copilot/prompting.py@@ -214,6 +214,15 @@ Correct flow for *any* integration request:SendAuthenticatedWebRequestBlock / browser automation / feature request.```+### Asking the user questions — use `ask_question`+When your turn ends blocked on the user's input — a decision, a missing+detail, an approval — ask via the `ask_question` tool (with concrete+`options` when the choices are known) instead of only writing the question+as prose. Questions asked only in text are invisible to the user's Home+"Needs You" feed, so if they have stepped away the work stalls silently;+the tool call is what parks the question for them. A short closing sentence+may restate it, but never replace the tool call with prose.+### Complex multi-step work- Use `TodoWrite` to track the plan once the job has 3+ distinct steps.- Delegate self-contained subtasks to `run_sub_session` to keep theirdiff --git a/autogpt_platform/backend/backend/copilot/tools/ask_question.py b/autogpt_platform/backend/backend/copilot/tools/ask_question.pyindex e669859fd9..8c8c4056be 100644--- a/autogpt_platform/backend/backend/copilot/tools/ask_question.py+++ b/autogpt_platform/backend/backend/copilot/tools/ask_question.py@@ -12,6 +12,13 @@ from .models import ClarificationNeededResponse, ClarifyingQuestion, ToolResponslogger = logging.getLogger(__name__)+# The model can be steered by untrusted page content (agent_browser, MCP tools),+# and every option is replayed into context on each subsequent turn.+MAX_QUESTIONS = 10+MAX_OPTIONS = 25+MAX_OPTION_LENGTH = 200+MAX_OPTION_SCAN = 500+class AskQuestionTool(BaseTool):"""Ask the user one or more clarifying questions and wait for answers.@@ -51,7 +58,13 @@ class AskQuestionTool(BaseTool):"options": {"type": "array","items": {"type": "string"},- "description": "Options for this question.",+ "description": (+ "Options for this question, offered to the "+ "user as choices; they can always type a "+ "custom answer instead. Exactly one option "+ "is chosen, so split a question that needs "+ "several answers into separate questions."+ ),},"keyword": {"type": "string",@@ -123,7 +136,9 @@ async def _mark_pending(session: ChatSession, text: str) -> None:def _parse_questions(raw: list[Any]) -> list[ClarifyingQuestion]:"""Parse and validate raw question dicts into ClarifyingQuestion objects."""return [- q for idx, item in enumerate(raw) if (q := _parse_one(item, idx)) is not None+ q+ for idx, item in enumerate(raw[:MAX_QUESTIONS])+ if (q := _parse_one(item, idx)) is not None]@@ -148,15 +163,35 @@ def _parse_one(item: Any, idx: int) -> ClarifyingQuestion | None:else f"question-{idx}")- raw_options = item.get("options")- options = (- [str(o) for o in raw_options if o is not None and str(o).strip()]- if isinstance(raw_options, list)- else []- )+ options = _parse_options(item.get("options"))return ClarifyingQuestion(question=text.strip(),keyword=keyword,example=", ".join(options) if options else None,+ options=options,)+++def _parse_options(raw: Any) -> list[str]:+ """Strip the option strings and drop repeats, keeping the model's order.++ Repeats would otherwise reach `example` as "Yes, Yes". Non-strings are+ dropped rather than coerced: the schema declares strings, and `str()` would+ surface a Python repr like "['a']" as a tappable choice. Capped because the+ options are LLM-controlled and get replayed into context every turn.+ """+ if not isinstance(raw, list):+ return []+ unique: dict[str, None] = {}+ # Collect up to the cap rather than normalizing everything and slicing+ # after: a huge array would otherwise cost a strip per entry. Blanks and+ # repeats drop out, so the scan needs its own bound to stay finite.+ for option in raw[:MAX_OPTION_SCAN]:+ if not isinstance(option, str):+ continue+ if trimmed := option.strip()[:MAX_OPTION_LENGTH]:+ unique[trimmed] = None+ if len(unique) == MAX_OPTIONS:+ break+ return list(unique)diff --git a/autogpt_platform/backend/backend/copilot/tools/ask_question_test.py b/autogpt_platform/backend/backend/copilot/tools/ask_question_test.pyindex d46f48f307..93a49bd39c 100644--- a/autogpt_platform/backend/backend/copilot/tools/ask_question_test.py+++ b/autogpt_platform/backend/backend/copilot/tools/ask_question_test.py@@ -5,7 +5,12 @@ from unittest.mock import AsyncMock, MagicMock, patchimport pytestfrom backend.copilot.model import ChatSession, clear_pending_question-from backend.copilot.tools.ask_question import AskQuestionTool+from backend.copilot.tools.ask_question import (+ MAX_OPTION_LENGTH,+ MAX_OPTIONS,+ MAX_QUESTIONS,+ AskQuestionTool,+)from backend.copilot.tools.models import ClarificationNeededResponse@@ -57,6 +62,29 @@ async def test_single_question_with_options(assert isinstance(result, ClarificationNeededResponse)q = result.questions[0]assert q.example == "Email, Slack, Google Docs"+ assert q.options == ["Email", "Slack", "Google Docs"]+++async def test_options_are_stripped_and_deduped(+ tool: AskQuestionTool, session: ChatSession+):+ result = await tool._execute(+ user_id=None,+ session=session,+ questions=[+ {+ "question": "Which channel?",+ "options": [" Email ", "Slack", " ", "Email"],+ "keyword": "channel",+ }+ ],+ )++ assert isinstance(result, ClarificationNeededResponse)+ q = result.questions[0]+ assert q.options == ["Email", "Slack"]+ assert q.example == "Email, Slack"@pytest.mark.asyncio@@ -172,6 +200,68 @@ async def test_options_filters_none_and_empty(assert result.questions[0].example == "Email, Slack"+async def test_options_drops_non_strings_instead_of_coercing(+ tool: AskQuestionTool, session: ChatSession+):+ # str() would surface a Python repr like "['a']" as a tappable choice, and+ # the frontend's recovery path only keeps strings anyway.+ result = await tool._execute(+ user_id=None,+ session=session,+ questions=[{"question": "Pick", "options": [None, 42, "Slack", ["a"]]}],+ )++ assert isinstance(result, ClarificationNeededResponse)+ assert result.questions[0].options == ["Slack"]+++async def test_options_are_capped_in_count_and_length(+ tool: AskQuestionTool, session: ChatSession+):+ result = await tool._execute(+ user_id=None,+ session=session,+ questions=[+ {"question": "Pick", "options": [f"opt-{i}" for i in range(60)]},+ {"question": "Pick", "options": ["x" * 500]},+ ],+ )++ assert isinstance(result, ClarificationNeededResponse)+ assert len(result.questions[0].options) == MAX_OPTIONS+ assert result.questions[0].options[0] == "opt-0"+ assert result.questions[1].options == ["x" * MAX_OPTION_LENGTH]+++async def test_options_stop_scanning_past_the_bound(+ tool: AskQuestionTool, session: ChatSession+):+ # Blanks never reach the cap, so only the scan bound stops the walk.+ result = await tool._execute(+ user_id=None,+ session=session,+ questions=[{"question": "Pick", "options": [""] * 5000 + ["Slack"]}],+ )++ assert isinstance(result, ClarificationNeededResponse)+ assert result.questions[0].options == []+++async def test_questions_are_capped(tool: AskQuestionTool, session: ChatSession):+ result = await tool._execute(+ user_id=None,+ session=session,+ questions=[{"question": f"Q{i}"} for i in range(40)],+ )++ assert isinstance(result, ClarificationNeededResponse)+ assert len(result.questions) == MAX_QUESTIONS++@pytest.mark.asyncioasync def test_no_options_gives_none_example(tool: AskQuestionTool, session: ChatSessiondiff --git a/autogpt_platform/backend/backend/copilot/tools/models.py b/autogpt_platform/backend/backend/copilot/tools/models.pyindex a5505861ad..cbdcf67cd3 100644--- a/autogpt_platform/backend/backend/copilot/tools/models.py+++ b/autogpt_platform/backend/backend/copilot/tools/models.py@@ -625,6 +625,7 @@ class ClarifyingQuestion(BaseModel):question: strkeyword: strexample: str | None = None+ options: list[str] = Field(default_factory=list)class AgentPreviewResponse(ToolResponseBase):diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/QuestionAnswerField.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/QuestionAnswerField.tsxnew file mode 100644index 0000000000..6b9e54aa67--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/QuestionAnswerField.tsx@@ -0,0 +1,107 @@+"use client";++import { Icon } from "@/components/atoms/Icon/Icon";+import { PencilEdit02Icon } from "@hugeicons/core-free-icons";+import { useState } from "react";+import type { ClarifyingQuestion } from "../../tools/clarifying-questions";+import { QuestionOptionList } from "./QuestionOptionList";++interface Props {+ question: ClarifyingQuestion;+ value: string;+ labelId: string;+ autoFocus: boolean;+ onChange: (value: string) => void;+ onSubmit: () => void;+}++/** The answer input for one question: options render as tappable rows with a+ * trailing "Type something…" escape hatch into free text; questions without+ * options go straight to the textarea. Mounted per-question — the parent keys+ * this component on the question's pager id, so the typing toggle resets+ * between questions instead of leaking across them. */+export function QuestionAnswerField({+ question,+ value,+ labelId,+ autoFocus,+ onChange,+ onSubmit,+}: Props) {+ const options = question.options ?? [];+ const isCustom = value.trim().length > 0 && !options.includes(value.trim());+ const [typing, setTyping] = useState(() => options.length > 0 && isCustom);+ // Focus follows an explicit toggle. A pre-filled custom answer opens the+ // textarea too, but must not steal focus when the card first renders —+ // that is what the pager's autoFocus is for.+ const [toggled, setToggled] = useState(false);++ if (options.length === 0 || typing) {+ return (+ <div className="flex flex-col gap-1.5">+ <textarea+ required+ rows={3}+ aria-labelledby={labelId}+ autoFocus={autoFocus || toggled}+ value={value}+ onChange={(e) => onChange(e.target.value)}+ // Enter advances the pager; Shift+Enter is the newline.+ onKeyDown={(e) => {+ if (e.key !== "Enter" || e.shiftKey) return;+ e.preventDefault();+ onSubmit();+ }}+ // The example is the options joined, so replaying it to someone who+ // just declined those options would only be noise.+ placeholder={+ options.length === 0 && question.example+ ? `e.g. ${question.example}`+ : "Type your answer"+ }+ className="resize-none rounded-2xl bg-zinc-50 px-3 py-2 text-sm leading-relaxed text-zinc-800 ring-1 ring-zinc-100 transition-shadow placeholder:text-zinc-400 focus:outline-none focus:ring-zinc-300"+ />+ {options.length > 0 && (+ <button+ type="button"+ onClick={() => {+ // Drop the free text, or the pager would happily submit a value+ // the option list gives no sign of having selected.+ if (isCustom) onChange("");+ setToggled(true);+ setTyping(false);+ }}+ className="self-start rounded-full px-2 py-0.5 text-xs text-zinc-500 transition-colors hover:bg-zinc-100 hover:text-zinc-700"+ >+ Choose from options instead+ </button>+ )}+ </div>+ );+ }++ return (+ <div className="flex flex-col gap-1.5">+ <QuestionOptionList+ options={options}+ value={value}+ labelId={labelId}+ focusActiveOption={autoFocus || toggled}+ onChange={onChange}+ onSubmit={onSubmit}+ />+ <button+ type="button"+ onClick={() => {+ if (options.includes(value.trim())) onChange("");+ setToggled(true);+ setTyping(true);+ }}+ className="flex items-center gap-2 rounded-2xl border border-dashed border-zinc-200 px-3 py-2 text-left text-sm text-zinc-500 transition-colors hover:border-zinc-300 hover:text-zinc-700"+ >+ <Icon icon={PencilEdit02Icon} size={14} className="shrink-0" />+ Type something…+ </button>+ </div>+ );+}diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/QuestionOptionList.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/QuestionOptionList.tsxnew file mode 100644index 0000000000..20b0dc5f90--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/QuestionOptionList.tsx@@ -0,0 +1,101 @@+"use client";++import { Icon } from "@/components/atoms/Icon/Icon";+import { Tick02Icon } from "@hugeicons/core-free-icons";+import { useEffect, useRef } from "react";++interface Props {+ /** Expected trimmed and deduped — `toOptions` establishes that invariant,+ * and the option strings double as React keys and selection identities. */+ options: string[];+ value: string;+ labelId: string;+ focusActiveOption: boolean;+ onChange: (value: string) => void;+ onSubmit: () => void;+}++/** The options for one question as a real radiogroup: a single stop in the tab+ * order, with arrows moving and selecting. Anything less would leave the radio+ * roles promising assistive tech a keyboard model that isn't there. */+export function QuestionOptionList({+ options,+ value,+ labelId,+ focusActiveOption,+ onChange,+ onSubmit,+}: Props) {+ const refs = useRef<(HTMLButtonElement | null)[]>([]);+ const selected = options.indexOf(value.trim());+ const active = selected === -1 ? 0 : selected;++ useEffect(() => {+ if (focusActiveOption) refs.current[active]?.focus();+ }, [focusActiveOption, active]);++ function moveTo(index: number) {+ onChange(options[index]);+ refs.current[index]?.focus();+ }++ function handleKeyDown(event: React.KeyboardEvent, index: number) {+ // Enter would otherwise re-click the focused option and leave the user+ // tabbing past the pager to reach send. Selecting first means tabbing in+ // and hitting Enter can't submit an option nobody chose — and arrowing or+ // clicking already selects, so those reach the pager on the first Enter.+ if (event.key === "Enter") {+ event.preventDefault();+ if (options[index] === value.trim()) onSubmit();+ else onChange(options[index]);+ return;+ }+ const step =+ event.key === "ArrowDown" || event.key === "ArrowRight"+ ? 1+ : event.key === "ArrowUp" || event.key === "ArrowLeft"+ ? -1+ : 0;+ if (step === 0) return;+ event.preventDefault();+ moveTo((index + step + options.length) % options.length);+ }++ return (+ <div+ role="radiogroup"+ aria-labelledby={labelId}+ aria-required="true"+ className="flex flex-col gap-1.5"+ >+ {options.map((option, index) => {+ const isSelected = option === value.trim();+ return (+ <button+ key={option}+ ref={(element) => {+ refs.current[index] = element;+ }}+ type="button"+ role="radio"+ aria-checked={isSelected}+ tabIndex={index === active ? 0 : -1}+ onClick={() => onChange(option)}+ onKeyDown={(event) => handleKeyDown(event, index)}+ className={+ "flex items-center justify-between gap-2 rounded-2xl px-3 py-2 text-left text-sm leading-relaxed transition-all " ++ (isSelected+ ? "bg-white text-zinc-900 ring-2 ring-zinc-800"+ : "bg-zinc-50 text-zinc-700 ring-1 ring-zinc-100 hover:bg-zinc-100")+ }+ >+ <span>{option}</span>+ {isSelected && (+ <Icon icon={Tick02Icon} size={14} className="shrink-0" />+ )}+ </button>+ );+ })}+ </div>+ );+}diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/QuestionsSection.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/QuestionsSection.tsxindex 125b7c11fc..08fbc16079 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/QuestionsSection.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/QuestionsSection.tsx@@ -8,8 +8,9 @@ import {SentIcon,} from "@hugeicons/core-free-icons";import { m } from "framer-motion";-import { useState } from "react";+import { useId, useState } from "react";import type { QuestionRequest } from "./helpers";+import { QuestionAnswerField } from "./QuestionAnswerField";interface Props {requests: QuestionRequest[];@@ -22,13 +23,20 @@ interface Props {* step, drafts every answer into the chat input. */export function QuestionsSection({ requests, isReady, onProceed }: Props) {const [step, setStep] = useState(0);- const questions = requests.flatMap((request) =>- request.questions.map((question) => ({ request, question })),- );+ const sectionId = useId();+ // Keyed by position, not keyword: keywords are unique only within a request,+ // and a duplicate would collapse two questions onto one id — the field would+ // never remount and the second question would show the first one's answer.+ const questions = requests+ .flatMap((request) =>+ request.questions.map((question) => ({ request, question })),+ )+ .map((entry, index) => ({ ...entry, id: `${entry.request.id}-${index}` }));if (questions.length === 0) return null;const current = Math.min(step, questions.length - 1);- const { request, question } = questions[current];+ const { request, question, id } = questions[current];+ const labelId = `${sectionId}-${id}`;const answered = (request.answers[question.keyword] ?? "").trim().length > 0;const isLast = current === questions.length - 1;const actionEnabled = isLast ? isReady : answered;@@ -60,27 +68,27 @@ export function QuestionsSection({ requests, isReady, onProceed }: Props) {</div><m.div- key={question.keyword}+ key={id}initial={{ opacity: 0, y: 8 }}animate={{ opacity: 1, y: 0 }}transition={{ duration: 0.25, ease: [0.23, 1, 0.32, 1] }}className="flex flex-col gap-1.5 px-4 py-3">- <label className="flex flex-col gap-1.5">- <span className="text-sm text-zinc-700">{question.question}</span>- <input- type="text"- required- autoFocus={current > 0}- value={request.answers[question.keyword] ?? ""}- onChange={(e) => request.onAnswer(question.keyword, e.target.value)}- onKeyDown={(e) => e.key === "Enter" && handleAction()}- placeholder={- question.example ? `e.g. ${question.example}` : "Type your answer"- }- className="rounded-2xl bg-zinc-50 px-3 py-2 text-sm text-zinc-800 ring-1 ring-zinc-100 transition-shadow placeholder:text-zinc-400 focus:outline-none focus:ring-zinc-300"- />- </label>+ <span id={labelId} className="text-sm text-zinc-700">+ {question.question}+ </span>+ <QuestionAnswerField+ // The field's typing toggle must reset per question; keying it here+ // rather than only on the wrapper keeps that contract local to the+ // component that owns the state.+ key={id}+ question={question}+ value={request.answers[question.keyword] ?? ""}+ labelId={labelId}+ autoFocus={current > 0}+ onChange={(value) => request.onAnswer(question.keyword, value)}+ onSubmit={handleAction}+ /></m.div><div className="flex items-center justify-between px-4 pb-3 pt-1">@@ -95,9 +103,9 @@ export function QuestionsSection({ requests, isReady, onProceed }: Props) {<Icon icon={ArrowLeft01Icon} size={14} /></button><span className="flex items-center gap-1.5">- {questions.map(({ question: q }, i) => (+ {questions.map(({ id: questionId }, i) => (<button- key={q.keyword}+ key={questionId}type="button"aria-label={`Go to question ${i + 1}`}aria-current={i === current ? "step" : undefined}diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/__tests__/ChainActionCard.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/__tests__/ChainActionCard.test.tsxindex cdb338b769..176e4ea617 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/__tests__/ChainActionCard.test.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/__tests__/ChainActionCard.test.tsx@@ -510,6 +510,516 @@ describe("ChainActionCard", () => {expect(onProceed).toHaveBeenCalledOnce();});+ it("renders options as choices and selects one on click", () => {+ const request = questionRequest({+ questions: [+ {+ question: "Which region?",+ keyword: "region",+ options: ["Europe", "Americas"],+ },+ ],+ });+ renderCard({ questions: [request] });++ expect(screen.queryByPlaceholderText("Type your answer")).toBeNull();+ fireEvent.click(screen.getByRole("radio", { name: "Europe" }));+ expect(request.onAnswer).toHaveBeenCalledWith("region", "Europe");+ });++ it("marks the option matching the answer as selected", () => {+ renderCard({+ questions: [+ questionRequest({+ questions: [+ {+ question: "Which region?",+ keyword: "region",+ options: ["Europe", "Americas"],+ },+ ],+ answers: { region: "Europe" },+ }),+ ],+ });++ expect(+ screen+ .getByRole("radio", { name: "Europe" })+ .getAttribute("aria-checked"),+ ).toBe("true");+ expect(+ screen+ .getByRole("radio", { name: "Americas" })+ .getAttribute("aria-checked"),+ ).toBe("false");+ });++ it("swaps to free text via Type something and clears a picked option", () => {+ const request = questionRequest({+ questions: [+ {+ question: "Which region?",+ keyword: "region",+ options: ["Europe", "Americas"],+ },+ ],+ answers: { region: "Europe" },+ });+ renderCard({ questions: [request] });++ fireEvent.click(screen.getByText("Type something…"));+ expect(request.onAnswer).toHaveBeenCalledWith("region", "");+ expect(screen.getByPlaceholderText("Type your answer")).toBeDefined();++ fireEvent.click(screen.getByText("Choose from options instead"));+ expect(screen.getByRole("radio", { name: "Europe" })).toBeDefined();+ });++ it("opens in free text when the answer matches no option", () => {+ renderCard({+ questions: [+ questionRequest({+ questions: [+ {+ question: "Which region?",+ keyword: "region",+ options: ["Europe", "Americas"],+ },+ ],+ answers: { region: "Antarctica" },+ }),+ ],+ });++ expect(screen.getByDisplayValue("Antarctica")).toBeDefined();+ });++ it("clears a custom answer when going back to the options", () => {+ const request = questionRequest({+ questions: [+ {+ question: "Which region?",+ keyword: "region",+ options: ["Europe", "Americas"],+ },+ ],+ answers: { region: "Antarctica" },+ });+ renderCard({ questions: [request] });++ fireEvent.click(screen.getByText("Choose from options instead"));+ expect(request.onAnswer).toHaveBeenCalledWith("region", "");+ });++ it("names the option group after the question", () => {+ renderCard({+ questions: [+ questionRequest({+ questions: [+ {+ question: "Which region?",+ keyword: "region",+ options: ["Europe", "Americas"],+ },+ ],+ }),+ ],+ });++ expect(+ screen.getByRole("radiogroup", { name: "Which region?" }),+ ).toBeDefined();+ });++ it("moves and selects between options with the arrow keys", () => {+ const request = questionRequest({+ questions: [+ {+ question: "Which region?",+ keyword: "region",+ options: ["Europe", "Americas"],+ },+ ],+ answers: { region: "Europe" },+ });+ renderCard({ questions: [request] });++ fireEvent.keyDown(screen.getByRole("radio", { name: "Europe" }), {+ key: "ArrowDown",+ });+ expect(request.onAnswer).toHaveBeenCalledWith("region", "Americas");+ });++ it("keeps only the active option in the tab order", () => {+ renderCard({+ questions: [+ questionRequest({+ questions: [+ {+ question: "Which region?",+ keyword: "region",+ options: ["Europe", "Americas"],+ },+ ],+ answers: { region: "Americas" },+ }),+ ],+ });++ expect(+ screen+ .getByRole("radio", { name: "Americas" })+ .getAttribute("tabindex"),+ ).toBe("0");+ expect(+ screen.getByRole("radio", { name: "Europe" }).getAttribute("tabindex"),+ ).toBe("-1");+ });++ it("does not steal focus when the first question opens in free text", () => {+ renderCard({+ questions: [+ questionRequest({+ questions: [+ {+ question: "Which region?",+ keyword: "region",+ options: ["Europe", "Americas"],+ },+ ],+ answers: { region: "Antarctica" },+ }),+ ],+ });++ expect(document.activeElement).toBe(document.body);+ });++ it("focuses the free-text box when the user asks to type", () => {+ renderCard({+ questions: [+ questionRequest({+ questions: [+ {+ question: "Which region?",+ keyword: "region",+ options: ["Europe", "Americas"],+ },+ ],+ }),+ ],+ });++ fireEvent.click(screen.getByText("Type something…"));+ expect(document.activeElement).toBe(+ screen.getByPlaceholderText("Type your answer"),+ );+ });++ it("focuses the active option when the pager reaches a question", () => {+ renderCard({+ questions: [+ questionRequest({+ questions: [+ { question: "Which region?", keyword: "region" },+ {+ question: "Which channel?",+ keyword: "channel",+ options: ["Email", "Slack"],+ },+ ],+ answers: { region: "Europe" },+ }),+ ],+ });++ fireEvent.click(screen.getByRole("button", { name: "Go to question 2" }));+ expect(document.activeElement).toBe(+ screen.getByRole("radio", { name: "Email" }),+ );+ });++ it("does not leak the free-text toggle between same-keyword requests", () => {+ renderCard({+ questions: [+ questionRequest({+ id: "questions-1",+ questions: [+ {+ question: "Which region?",+ keyword: "region",+ options: ["Europe", "Americas"],+ },+ ],+ answers: { region: "Europe" },+ }),+ questionRequest({+ id: "questions-2",+ questions: [+ {+ question: "Which region?",+ keyword: "region",+ options: ["Notion", "Drive"],+ },+ ],+ answers: {},+ }),+ ],+ });++ fireEvent.click(screen.getByText("Type something…"));+ expect(screen.getByPlaceholderText("Type your answer")).toBeDefined();++ fireEvent.click(screen.getByRole("button", { name: "Go to question 2" }));+ expect(screen.getByRole("radio", { name: "Notion" })).toBeDefined();+ });++ it("moves focus onto the option the arrow keys select", () => {+ renderCard({+ questions: [+ questionRequest({+ questions: [+ {+ question: "Which region?",+ keyword: "region",+ options: ["Europe", "Americas"],+ },+ ],+ answers: { region: "Europe" },+ }),+ ],+ });++ fireEvent.keyDown(screen.getByRole("radio", { name: "Europe" }), {+ key: "ArrowDown",+ });+ expect(document.activeElement).toBe(+ screen.getByRole("radio", { name: "Americas" }),+ );+ });++ it("selects backwards with ArrowUp and ArrowLeft", () => {+ const request = questionRequest({+ questions: [+ {+ question: "Which region?",+ keyword: "region",+ options: ["Europe", "Americas", "Asia"],+ },+ ],+ answers: { region: "Asia" },+ });+ renderCard({ questions: [request] });++ fireEvent.keyDown(screen.getByRole("radio", { name: "Asia" }), {+ key: "ArrowUp",+ });+ expect(request.onAnswer).toHaveBeenCalledWith("region", "Americas");++ fireEvent.keyDown(screen.getByRole("radio", { name: "Asia" }), {+ key: "ArrowLeft",+ });+ expect(request.onAnswer).toHaveBeenLastCalledWith("region", "Americas");+ });++ it("wraps around at both ends of the option list", () => {+ // Without the modulo, ArrowUp on the first option reads options[-1] and+ // answers the question with undefined.+ const first = questionRequest({+ questions: [+ {+ question: "Which region?",+ keyword: "region",+ options: ["Europe", "Americas", "Asia"],+ },+ ],+ answers: { region: "Europe" },+ });+ renderCard({ questions: [first] });+ fireEvent.keyDown(screen.getByRole("radio", { name: "Europe" }), {+ key: "ArrowUp",+ });+ expect(first.onAnswer).toHaveBeenCalledWith("region", "Asia");++ cleanup();++ const last = questionRequest({+ questions: [+ {+ question: "Which region?",+ keyword: "region",+ options: ["Europe", "Americas", "Asia"],+ },+ ],+ answers: { region: "Asia" },+ });+ renderCard({ questions: [last] });+ fireEvent.keyDown(screen.getByRole("radio", { name: "Asia" }), {+ key: "ArrowDown",+ });+ expect(last.onAnswer).toHaveBeenCalledWith("region", "Europe");+ });++ it("submits from the option list on Enter once an option is selected", () => {+ const request = questionRequest({+ questions: [+ {+ question: "Which region?",+ keyword: "region",+ options: ["Europe", "Americas"],+ },+ ],+ answers: { region: "Europe" },+ });+ const { onProceed } = renderCard({ questions: [request] });++ fireEvent.keyDown(screen.getByRole("radio", { name: "Europe" }), {+ key: "Enter",+ });+ expect(onProceed).toHaveBeenCalledOnce();+ });++ it("selects rather than submits on Enter when nothing is chosen yet", () => {+ const request = questionRequest({+ questions: [+ {+ question: "Which region?",+ keyword: "region",+ options: ["Europe", "Americas"],+ },+ ],+ });+ const { onProceed } = renderCard({ questions: [request] });++ fireEvent.keyDown(screen.getByRole("radio", { name: "Europe" }), {+ key: "Enter",+ });+ expect(request.onAnswer).toHaveBeenCalledWith("region", "Europe");+ expect(onProceed).not.toHaveBeenCalled();+ });++ it("matches the selected option against the trimmed answer", () => {+ renderCard({+ questions: [+ questionRequest({+ questions: [+ {+ question: "Which region?",+ keyword: "region",+ options: ["Europe", "Americas"],+ },+ ],+ answers: { region: " Europe " },+ }),+ ],+ });++ expect(+ screen+ .getByRole("radio", { name: "Europe" })+ .getAttribute("aria-checked"),+ ).toBe("true");+ });++ it("does not replay the declined options as the free-text placeholder", () => {+ // The backend sets example to the joined options, so echoing it back to+ // someone who just left the option list would be pure noise.+ renderCard({+ questions: [+ questionRequest({+ questions: [+ {+ question: "Which region?",+ keyword: "region",+ example: "Europe, Americas",+ options: ["Europe", "Americas"],+ },+ ],+ }),+ ],+ });++ fireEvent.click(screen.getByText("Type something…"));+ expect(screen.getByPlaceholderText("Type your answer")).toBeDefined();+ expect(screen.queryByPlaceholderText("e.g. Europe, Americas")).toBeNull();+ });++ it("gates the send button on an option actually being picked", () => {+ const request = questionRequest({+ questions: [+ {+ question: "Which region?",+ keyword: "region",+ options: ["Europe", "Americas"],+ },+ ],+ });+ const { onProceed, rerender } = renderCard({ questions: [request] });++ expect(+ (+ screen.getByRole("button", {+ name: "Add answers to message",+ }) as HTMLButtonElement+ ).disabled,+ ).toBe(true);++ fireEvent.click(screen.getByRole("radio", { name: "Europe" }));+ expect(request.onAnswer).toHaveBeenCalledWith("region", "Europe");++ rerender(+ <ChainActionCard+ connectors={[]}+ mcp={[]}+ inputs={[]}+ questions={[{ ...request, answers: { region: "Europe" } }]}+ isReady+ onProceed={onProceed}+ />,+ );++ const send = screen.getByRole("button", {+ name: "Add answers to message",+ }) as HTMLButtonElement;+ expect(send.disabled).toBe(false);+ fireEvent.click(send);+ expect(onProceed).toHaveBeenCalledOnce();+ });++ it("keeps two same-keyword questions on their own cards", () => {+ // Keywords are unique only within a request, so the pager keys on+ // position — sharing an id would stop the field remounting, leaving the+ // second question's options blank while the first one's answer submits.+ renderCard({+ questions: [+ questionRequest({+ questions: [+ {+ question: "Which source channel?",+ keyword: "channel",+ options: ["Email", "Slack"],+ },+ {+ question: "Which destination channel?",+ keyword: "channel",+ options: ["Notion", "Drive"],+ },+ ],+ answers: { channel: "Email" },+ }),+ ],+ });++ expect(screen.getByRole("radio", { name: "Email" })).toBeDefined();+ fireEvent.click(screen.getByRole("button", { name: "Go to question 2" }));++ // Remounted: "Email" matches neither option here, so the field opens in+ // free text with the stray value visible instead of silently hiding it.+ expect(screen.getByDisplayValue("Email")).toBeDefined();+ expect(screen.queryByRole("radio", { name: "Notion" })).toBeNull();+ });+it("keeps the questions card sendable when an unready sibling exists", () => {// An unconnected MCP row must not freeze the questions footer.const { onProceed } = renderCard({diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/tools/AskQuestion/helpers.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/tools/AskQuestion/helpers.tsindex bd2f635622..c4248cd264 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/tools/AskQuestion/helpers.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/tools/AskQuestion/helpers.ts@@ -1,16 +1,11 @@+import type { ClarifyingQuestion } from "@/app/api/__generated__/models/clarifyingQuestion";import { ResponseType } from "@/app/api/__generated__/models/responseType";import type { ToolUIPart } from "ai";-interface ClarifyingQuestionPayload {- question: string;- keyword: string;- example?: string;-}-export interface AskQuestionOutput {type: string;message: string;- questions: ClarifyingQuestionPayload[];+ questions: ClarifyingQuestion[];session_id?: string;}diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/tools/__tests__/clarifying-questions.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/tools/__tests__/clarifying-questions.test.tsindex 301397297b..d02dc7fbcc 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/tools/__tests__/clarifying-questions.test.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/tools/__tests__/clarifying-questions.test.ts@@ -1,6 +1,7 @@import { describe, expect, it } from "vitest";import {buildClarificationAnswersMessage,+ extractClarifyingQuestions,normalizeClarifyingQuestions,} from "../clarifying-questions";@@ -59,6 +60,140 @@ describe("normalizeClarifyingQuestions", () => {it("returns an empty array for empty input", () => {expect(normalizeClarifyingQuestions([])).toEqual([]);});++ it("keeps string options and drops blank or non-string entries", () => {+ const result = normalizeClarifyingQuestions([+ { question: "Q1", keyword: "k1", options: ["Email", " ", 42, "Slack"] },+ { question: "Q2", keyword: "k2", options: [] },+ { question: "Q3", keyword: "k3", options: "Email" },+ ]);+ expect(result[0].options).toEqual(["Email", "Slack"]);+ expect(result[1].options).toBeUndefined();+ expect(result[2].options).toBeUndefined();+ });++ it("trims padded options and drops duplicates", () => {+ const result = normalizeClarifyingQuestions([+ { question: "Q1", keyword: "k1", options: [" Email ", "Slack", "Email"] },+ ]);+ expect(result[0].options).toEqual(["Email", "Slack"]);+ });+});++describe("extractClarifyingQuestions", () => {+ it("reads options straight from the output when present", () => {+ const result = extractClarifyingQuestions({+ output: {+ questions: [+ {+ question: "Which channel?",+ keyword: "channel",+ options: ["Email", "Slack"],+ },+ ],+ },+ });+ expect(result[0].options).toEqual(["Email", "Slack"]);+ });++ it("recovers options from the input when the output collapsed them", () => {+ const result = extractClarifyingQuestions({+ input: {+ questions: [+ {+ question: "Which channel?",+ keyword: "channel",+ options: ["Email", "Slack"],+ },+ ],+ },+ output: {+ questions: [+ {+ question: "Which channel?",+ keyword: "channel",+ example: "Email, Slack",+ },+ ],+ },+ });+ expect(result[0].options).toEqual(["Email", "Slack"]);+ expect(result[0].example).toBe("Email, Slack");+ });++ it("keeps same-worded questions apart when recovering from the input", () => {+ const result = extractClarifyingQuestions({+ input: {+ questions: [+ {+ question: "Which channel?",+ keyword: "source",+ options: ["Email", "Slack"],+ },+ {+ question: "Which channel?",+ keyword: "destination",+ options: ["Notion", "Drive"],+ },+ ],+ },+ output: {+ questions: [+ { question: "Which channel?", keyword: "source" },+ { question: "Which channel?", keyword: "destination" },+ ],+ },+ });+ expect(result[0].options).toEqual(["Email", "Slack"]);+ expect(result[1].options).toEqual(["Notion", "Drive"]);+ });++ it("keeps a keyword/question pair that a space separator would collide", () => {+ // "a" + "b c" and "a b" + "c" join to the same string under a space, so+ // the recovery key has to use a separator neither half can contain.+ const result = extractClarifyingQuestions({+ input: {+ questions: [+ { question: "b c", keyword: "a", options: ["Email", "Slack"] },+ { question: "c", keyword: "a b", options: ["Notion", "Drive"] },+ ],+ },+ output: {+ questions: [+ { question: "b c", keyword: "a" },+ { question: "c", keyword: "a b" },+ ],+ },+ });+ expect(result[0].options).toEqual(["Email", "Slack"]);+ expect(result[1].options).toEqual(["Notion", "Drive"]);+ });++ it("falls back to the input while the call is still in flight", () => {+ const result = extractClarifyingQuestions({+ input: {+ questions: [+ {+ question: "Which channel?",+ keyword: "channel",+ options: ["Email", "Slack"],+ },+ ],+ },+ });+ expect(result).toHaveLength(1);+ expect(result[0].question).toBe("Which channel?");+ expect(result[0].options).toEqual(["Email", "Slack"]);+ });++ it("leaves options unset when neither side carries them", () => {+ const result = extractClarifyingQuestions({+ output: {+ questions: [{ question: "Which channel?", keyword: "channel" }],+ },+ });+ expect(result[0].options).toBeUndefined();+ });});describe("buildClarificationAnswersMessage", () => {diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/tools/clarifying-questions.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/tools/clarifying-questions.tsindex 4c7c986a93..30c744842a 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/tools/clarifying-questions.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/tools/clarifying-questions.ts@@ -2,10 +2,24 @@ export interface ClarifyingQuestion {question: string;keyword: string;example?: string;+ options?: string[];+}++function toOptions(value: unknown): string[] | undefined {+ if (!Array.isArray(value)) return undefined;+ const options = value.flatMap((option) =>+ typeof option === "string" && option.trim() ? [option.trim()] : [],+ );+ return options.length > 0 ? Array.from(new Set(options)) : undefined;}export function normalizeClarifyingQuestions(- questions: Array<{ question: string; keyword: string; example?: unknown }>,+ questions: Array<{+ question: string;+ keyword: string;+ example?: unknown;+ options?: unknown;+ }>,): ClarifyingQuestion[] {const seen = new Set<string>();@@ -32,6 +46,8 @@ export function normalizeClarifyingQuestions(? q.example.trim(): null;if (example) item.example = example;+ const options = toOptions(q.options);+ if (options) item.options = options;return item;});}@@ -59,11 +75,39 @@ function questionItems(value: unknown): Record<string, unknown>[] | null {return items.length > 0 ? items : null;}+/** Identifies a question across the tool call's input and output. Question text+ * alone is not unique — the same prompt can be asked for two keywords (e.g. a+ * source and a destination channel) with different options each.+ *+ * The separator must be a character that cannot appear in either half, hence+ * an escaped NUL: with a space, keyword "a" + question "b c" and keyword "a b" ++ * question "c" would collide. Keep it escaped — a literal NUL byte makes the+ * whole file binary to git and unreviewable on GitHub. */+function recoveryKey(item: Record<string, unknown>): string {+ const keyword =+ typeof item.keyword === "string" ? item.keyword.trim().toLowerCase() : "";+ const question =+ typeof item.question === "string" ? item.question.trim() : "";+ return `${keyword}\u0000${question}`;+}+export function extractClarifyingQuestions(source: {input?: unknown;output?: unknown;}): ClarifyingQuestion[] {- const raw = questionItems(source.output) ?? questionItems(source.input) ?? [];+ const fromInput = questionItems(source.input);+ const raw = questionItems(source.output) ?? fromInput ?? [];+ // Older tool outputs collapse options into the example string, so when the+ // output is the source the selectable options only survive in the input.+ // This runs per render on an unmemoized chain row, so skip the whole map+ // when every item already carries its own options.+ const inputOptions = new Map<string, string[]>();+ if (raw.some((item) => !toOptions(item.options))) {+ for (const item of fromInput ?? []) {+ const options = toOptions(item.options);+ if (options) inputOptions.set(recoveryKey(item), options);+ }+ }const valid = raw.flatMap((item) =>typeof item.question === "string" &&item.question.trim() &&@@ -73,6 +117,8 @@ export function extractClarifyingQuestions(source: {question: item.question.trim(),keyword: item.keyword,example: item.example,+ options:+ toOptions(item.options) ?? inputOptions.get(recoveryKey(item)),},]: [],diff --git a/autogpt_platform/frontend/src/app/api/openapi.json b/autogpt_platform/frontend/src/app/api/openapi.jsonindex c96571e592..b14ed524fa 100644--- a/autogpt_platform/frontend/src/app/api/openapi.json+++ b/autogpt_platform/frontend/src/app/api/openapi.json@@ -16910,6 +16910,11 @@"example": {"anyOf": [{ "type": "string" }, { "type": "null" }],"title": "Example"+ },+ "options": {+ "items": { "type": "string" },+ "type": "array",+ "title": "Options"}},"type": "object",