Skip to content

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): allow multi-select questions in the copilot ask-user card (#14741)

Abhimanyu Yadavdae3a0d2a5c1
diff --git a/autogpt_platform/backend/backend/copilot/bot/turn_stream.py b/autogpt_platform/backend/backend/copilot/bot/turn_stream.py
index 65d1b2b926..188aeea68e 100644
--- a/autogpt_platform/backend/backend/copilot/bot/turn_stream.py
+++ b/autogpt_platform/backend/backend/copilot/bot/turn_stream.py
@@ -635,6 +635,10 @@ def _fits_native(adapter: PlatformAdapter, question: Any) -> bool:
options = _question_options(question)
if not adapter.supports_choice_buttons or not text or not options:
return False
+ # A native button consumes the question on the first click, so a
+ # multi-select one goes as numbered text the user can answer "1, 3" to.
+ if question.get("allow_multiple"):
+ return False
if len(options) > adapter.max_choice_options:
return False
# A clipped label is worse than no button: two options sharing a prefix
@@ -753,6 +757,8 @@ def _clarification_message(clarification_output: dict[str, Any]) -> str:
if options:
numbered = "\n".join(f"{i}. {o}" for i, o in enumerate(options, 1))
block = f"{block}\n{numbered}"
+ if question.get("allow_multiple"):
+ block = f"{block}\n(Pick one or more.)"
blocks.append(block)
if not blocks:
diff --git a/autogpt_platform/backend/backend/copilot/bot/turn_stream_test.py b/autogpt_platform/backend/backend/copilot/bot/turn_stream_test.py
index c780a455f4..910338339d 100644
--- a/autogpt_platform/backend/backend/copilot/bot/turn_stream_test.py
+++ b/autogpt_platform/backend/backend/copilot/bot/turn_stream_test.py
@@ -342,6 +342,37 @@ class TestNativeChoices:
assert "Region?" in adapter.send_message.await_args.args[1]
choices_mock.clear_choice.assert_awaited_once()
+ @pytest.mark.asyncio
+ async def test_multi_select_question_uses_numbered_text(self):
+ # A native button consumes the question on the first click, so there
+ # is no way to pick a second option through one.
+ adapter = _choice_adapter()
+
+ await _clarify(
+ adapter,
+ [
+ {
+ "question": "Which areas?",
+ "options": ["Research", "Outreach"],
+ "allow_multiple": True,
+ }
+ ],
+ )
+
+ adapter.send_choice_buttons.assert_not_awaited()
+ sent = adapter.send_message.await_args.args[1]
+ assert "1. Research" in sent
+ assert "(Pick one or more.)" in sent
+
+ @pytest.mark.asyncio
+ async def test_single_select_question_keeps_no_multi_hint(self):
+ adapter = _choice_adapter()
+ adapter.supports_choice_buttons = False
+
+ await _clarify(adapter, [{"question": "Region?", "options": ["EU", "US"]}])
+
+ assert "Pick one or more" not in adapter.send_message.await_args.args[1]
+
@pytest.mark.asyncio
async def test_too_many_options_for_this_platform_uses_text(self):
adapter = _choice_adapter(max_options=6)
diff --git a/autogpt_platform/backend/backend/copilot/prompting.py b/autogpt_platform/backend/backend/copilot/prompting.py
index 4ff3772503..70da9a4c78 100644
--- a/autogpt_platform/backend/backend/copilot/prompting.py
+++ b/autogpt_platform/backend/backend/copilot/prompting.py
@@ -188,8 +188,9 @@ regression that overrides any worked example earlier in this prompt.
### 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
+`options` when the choices are known, and `allow_multiple` when several of
+them can apply at once) 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.
diff --git a/autogpt_platform/backend/backend/copilot/sdk/agent_generation_guide.md b/autogpt_platform/backend/backend/copilot/sdk/agent_generation_guide.md
index b23ec97717..a1a211ac91 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/agent_generation_guide.md
+++ b/autogpt_platform/backend/backend/copilot/sdk/agent_generation_guide.md
@@ -12,7 +12,8 @@ data source, trigger, or a choice between candidate blocks.
Discover the platform's real options first (e.g. `find_capability`), then
call `ask_question` listing those options ("The platform supports
Gmail, Slack, and Google Docs — which for delivery?") and **wait for
-the answer**.
+the answer**. Set `allow_multiple` on a question whose options combine
+("which of these should it post to?") rather than compete.
**Skip** when the goal already specifies every dimension (e.g. "scrape
prices from Amazon and email me daily"). Data-shape questions are usually
diff --git a/autogpt_platform/backend/backend/copilot/tools/ask_question.py b/autogpt_platform/backend/backend/copilot/tools/ask_question.py
index f136288690..5f51856323 100644
--- a/autogpt_platform/backend/backend/copilot/tools/ask_question.py
+++ b/autogpt_platform/backend/backend/copilot/tools/ask_question.py
@@ -61,9 +61,17 @@ class AskQuestionTool(BaseTool):
"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."
+ "custom answer instead."
+ ),
+ },
+ "allow_multiple": {
+ "type": "boolean",
+ "description": (
+ "Let the user pick several of 'options'. "
+ "Set it when the answers combine ('which "
+ "roles?'); leave it out for a choice "
+ "between alternatives. Ignored without "
+ "options."
),
},
"keyword": {
@@ -75,7 +83,8 @@ class AskQuestionTool(BaseTool):
},
"description": (
"One or more clarifying questions. Each item has "
- "'question' (required), 'options', and 'keyword'."
+ "'question' (required), 'options', 'allow_multiple' "
+ "and 'keyword'."
),
},
},
@@ -170,6 +179,9 @@ def _parse_one(item: Any, idx: int) -> ClarifyingQuestion | None:
keyword=keyword,
example=", ".join(options) if options else None,
options=options,
+ # A free-text question has nothing to multi-select, so the flag is
+ # dropped rather than sent on for the card to render a lone checkbox.
+ allow_multiple=bool(options) and _parse_allow_multiple(item),
)
@@ -195,3 +207,15 @@ def _parse_options(raw: Any) -> list[str]:
if len(unique) == MAX_OPTIONS:
break
return list(unique)
+
+
+def _parse_allow_multiple(item: dict[str, Any]) -> bool:
+ """Read the multi-select flag, tolerating the string a model may send.
+
+ Anything else is False: a question defaults to one answer, and guessing
+ at a truthy value would silently change what the card asks for.
+ """
+ raw = item.get("allow_multiple")
+ if isinstance(raw, bool):
+ return raw
+ return isinstance(raw, str) and raw.strip().lower() == "true"
diff --git a/autogpt_platform/backend/backend/copilot/tools/ask_question_test.py b/autogpt_platform/backend/backend/copilot/tools/ask_question_test.py
index 93a49bd39c..4b65cb5872 100644
--- a/autogpt_platform/backend/backend/copilot/tools/ask_question_test.py
+++ b/autogpt_platform/backend/backend/copilot/tools/ask_question_test.py
@@ -65,6 +65,103 @@ async def test_single_question_with_options(
assert q.options == ["Email", "Slack", "Google Docs"]
+async def test_allow_multiple_marks_the_question_multi_select(
+ tool: AskQuestionTool, session: ChatSession
+):
+ result = await tool._execute(
+ user_id=None,
+ session=session,
+ questions=[
+ {
+ "question": "What areas should they own?",
+ "options": ["Research", "Outreach", "Reporting"],
+ "allow_multiple": True,
+ "keyword": "areas",
+ }
+ ],
+ )
+
+ assert isinstance(result, ClarificationNeededResponse)
+ assert result.questions[0].allow_multiple is True
+
+
+async def test_questions_are_single_select_by_default(
+ tool: AskQuestionTool, session: ChatSession
+):
+ result = await tool._execute(
+ user_id=None,
+ session=session,
+ questions=[
+ {
+ "question": "Which channel?",
+ "options": ["Email", "Slack"],
+ "keyword": "channel",
+ }
+ ],
+ )
+
+ assert isinstance(result, ClarificationNeededResponse)
+ assert result.questions[0].allow_multiple is False
+
+
+async def test_allow_multiple_accepts_the_string_a_model_may_send(
+ tool: AskQuestionTool, session: ChatSession
+):
+ result = await tool._execute(
+ user_id=None,
+ session=session,
+ questions=[
+ {
+ "question": "Which areas?",
+ "options": ["Research", "Outreach"],
+ "allow_multiple": " TRUE ",
+ "keyword": "areas",
+ },
+ {
+ "question": "Which channel?",
+ "options": ["Email", "Slack"],
+ "allow_multiple": "no",
+ "keyword": "channel",
+ },
+ ],
+ )
+
+ assert isinstance(result, ClarificationNeededResponse)
+ assert [q.allow_multiple for q in result.questions] == [True, False]
+
+
+async def test_allow_multiple_is_dropped_without_options(
+ tool: AskQuestionTool, session: ChatSession
+):
+ result = await tool._execute(
+ user_id=None,
+ session=session,
+ questions=[
+ {
+ "question": "Anything else?",
+ "allow_multiple": True,
+ "keyword": "notes",
+ }
+ ],
+ )
+
+ assert isinstance(result, ClarificationNeededResponse)
+ assert result.questions[0].allow_multiple is False
+
+
+async def test_allow_multiple_is_advertised_in_the_tool_schema(
+ tool: AskQuestionTool,
+):
+ item = tool.parameters["properties"]["questions"]["items"]
+ assert item["properties"]["allow_multiple"]["type"] == "boolean"
+ assert "allow_multiple" not in item["required"]
+
+
@pytest.mark.asyncio
async def test_options_are_stripped_and_deduped(
tool: AskQuestionTool, session: ChatSession
diff --git a/autogpt_platform/backend/backend/copilot/tools/models.py b/autogpt_platform/backend/backend/copilot/tools/models.py
index 7ffbbcff61..3d14e73ab6 100644
--- a/autogpt_platform/backend/backend/copilot/tools/models.py
+++ b/autogpt_platform/backend/backend/copilot/tools/models.py
@@ -786,6 +786,9 @@ class ClarifyingQuestion(BaseModel):
keyword: str
example: str | None = None
options: list[str] = Field(default_factory=list)
+ # Several of `options` may be picked. Only ever set alongside options:
+ # there is nothing to multi-select in a free-text question.
+ allow_multiple: bool = False
class AgentPreviewResponse(ToolResponseBase):
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/ChainActionCard.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/ChainActionCard.tsx
index 3a23279ef6..204311ec59 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/ChainActionCard.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/ChainActionCard.tsx
@@ -4,6 +4,7 @@ import { useGetV1ListProviders } from "@/app/api/__generated__/endpoints/integra
import { Button } from "@/components/atoms/Button/Button";
import { Icon } from "@/components/atoms/Icon/Icon";
import { PlugSocketIcon } from "@hugeicons/core-free-icons";
+import { isAnswered } from "../../tools/clarifying-questions";
import { useCopilotModal } from "../../useCopilotModal";
import { ConnectorRow } from "./ConnectorRow";
import { InputsSection } from "./InputsSection";
@@ -74,9 +75,7 @@ export function ChainActionCard({
const questionsReady =
hasQuestions &&
questions.every((request) =>
- request.questions.every(
- (q) => (request.answers[q.keyword] ?? "").trim().length > 0,
- ),
+ request.questions.every((q) => isAnswered(request.answers[q.keyword])),
);
if (rows.length === 0 && mcp.length === 0 && !hasInputs && !hasQuestions)
return null;
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/QuestionCheckboxList.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/QuestionCheckboxList.tsx
new file mode 100644
index 0000000000..d60a118ea3
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/QuestionCheckboxList.tsx
@@ -0,0 +1,112 @@
+"use client";
+
+import { Icon } from "@/components/atoms/Icon/Icon";
+import { Tick02Icon } from "@hugeicons/core-free-icons";
+import { isKey } from "@/lib/keyboard";
+import { forwardRef, useEffect, useImperativeHandle, 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[];
+ selected: string[];
+ labelId: string;
+ focusActiveOption: boolean;
+ onToggle: (option: string) => void;
+}
+
+export interface QuestionCheckboxListHandle {
+ focus: () => void;
+}
+
+/** The options for a multi-select question as a real checkbox group: one stop
+ * in the tab order, arrows moving focus only. The radiogroup's "arrows also
+ * select" model cannot work here — arrowing past four boxes would tick all
+ * four on the way to the fifth. */
+export const QuestionCheckboxList = forwardRef<
+ QuestionCheckboxListHandle,
+ Props
+>(function QuestionCheckboxList(
+ { options, selected, labelId, focusActiveOption, onToggle },
+ ref,
+) {
+ const refs = useRef<(HTMLButtonElement | null)[]>([]);
+ const first = options.findIndex((option) => selected.includes(option));
+ const active = first === -1 ? 0 : first;
+
+ useImperativeHandle(ref, () => ({
+ focus: () => refs.current[active]?.focus(),
+ }));
+
+ // Focus lands on the group once, when asked. `active` is the tab stop, not
+ // the caret: it follows the first tick, and re-running on every change would
+ // yank focus off the box the user just unticked.
+ const focused = useRef(false);
+ useEffect(() => {
+ if (!focusActiveOption) {
+ focused.current = false;
+ return;
+ }
+ if (focused.current) return;
+ focused.current = true;
+ refs.current[active]?.focus();
+ }, [focusActiveOption, active]);
+
+ function handleKeyDown(event: React.KeyboardEvent, index: number) {
+ // Enter toggles rather than submits: with several picks to make, an Enter
+ // that sent the answer would cut the user off after their first box.
+ if (isKey(event, "Enter")) {
+ event.preventDefault();
+ onToggle(options[index]);
+ return;
+ }
+ const step = isKey(event, "ArrowDown", "ArrowRight")
+ ? 1
+ : isKey(event, "ArrowUp", "ArrowLeft")
+ ? -1
+ : 0;
+ if (step === 0) return;
+ event.preventDefault();
+ refs.current[(index + step + options.length) % options.length]?.focus();
+ }
+
+ return (
+ <div role="group" aria-labelledby={labelId} className="flex flex-col gap-2">
+ {options.map((option, index) => {
+ const isSelected = selected.includes(option);
+ return (
+ <button
+ key={option}
+ ref={(element) => {
+ refs.current[index] = element;
+ }}
+ type="button"
+ role="checkbox"
+ aria-checked={isSelected}
+ tabIndex={index === active ? 0 : -1}
+ onClick={() => onToggle(option)}
+ onKeyDown={(event) => handleKeyDown(event, index)}
+ className={
+ "flex items-center justify-between gap-3 rounded-2xl px-4 py-3 text-left text-base leading-snug 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>
+ <span
+ className={
+ "flex size-5 shrink-0 items-center justify-center rounded-md transition-colors " +
+ (isSelected
+ ? "bg-zinc-800 text-white"
+ : "ring-1 ring-inset ring-zinc-300")
+ }
+ >
+ {isSelected && <Icon icon={Tick02Icon} size={13} />}
+ </span>
+ </button>
+ );
+ })}
+ </div>
+ );
+});
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/QuestionMultiAnswerField.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/QuestionMultiAnswerField.tsx
new file mode 100644
index 0000000000..d8d4d52d8e
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/QuestionMultiAnswerField.tsx
@@ -0,0 +1,119 @@
+"use client";
+
+import { Icon } from "@/components/atoms/Icon/Icon";
+import { PencilEdit02Icon } from "@hugeicons/core-free-icons";
+import { isKey } from "@/lib/keyboard";
+import { forwardRef, useImperativeHandle, useRef, useState } from "react";
+import type { MultiAnswer } from "../../tools/clarifying-questions";
+import {
+ QuestionCheckboxList,
+ type QuestionCheckboxListHandle,
+} from "./QuestionCheckboxList";
+
+interface Props {
+ options: string[];
+ value: MultiAnswer;
+ labelId: string;
+ autoFocus: boolean;
+ onChange: (value: MultiAnswer) => void;
+ onSubmit: () => void;
+}
+
+export interface QuestionMultiAnswerFieldHandle {
+ focus: () => void;
+}
+
+/** The answer input for a question that takes several answers: the options
+ * as a checkbox group, plus typed text kept *alongside* the ticks rather
+ * than replacing them — "any of these, and also…" is the whole point. */
+export const QuestionMultiAnswerField = forwardRef<
+ QuestionMultiAnswerFieldHandle,
+ Props
+>(function QuestionMultiAnswerField(
+ { options, value, labelId, autoFocus, onChange, onSubmit },
+ ref,
+) {
+ const listRef = useRef<QuestionCheckboxListHandle>(null);
+ const textareaRef = useRef<HTMLTextAreaElement>(null);
+ // The ticks and the typed text arrive in separate slots and stay that way:
+ // text that happens to equal an option is still the user's own words, so it
+ // neither ticks the box nor drains out of the textarea — including after a
+ // remount, since the answer itself remembers which was which.
+ const selected = value.selected.filter((pick) => options.includes(pick));
+ const custom = value.custom;
+ const [typing, setTyping] = useState(() => custom.length > 0);
+ // Focus follows an explicit toggle. A saved custom answer reopens the
+ // textarea too, but must not steal focus when the card pages back to it —
+ // that is what the pager's autoFocus is for.
+ const [toggled, setToggled] = useState(false);
+
+ // "Answer this one" lands wherever the user left off: the open textarea if
+ // they were typing, otherwise the checkbox group.
+ useImperativeHandle(ref, () => ({
+ focus: () => {
+ if (typing) textareaRef.current?.focus();
+ else listRef.current?.focus();
+ },
+ }));
+
+ function commit(nextSelected: string[], nextCustom: string) {
+ // Rebuilt in the order they were offered, so the reply reads like the
+ // question however the user ticked their way down it.
+ onChange({
+ selected: options.filter((option) => nextSelected.includes(option)),
+ custom: nextCustom,
+ });
+ }
+
+ function handleToggle(option: string) {
+ commit(
+ selected.includes(option)
+ ? selected.filter((pick) => pick !== option)
+ : [...selected, option],
+ custom,
+ );
+ }
+
+ return (
+ <div className="flex flex-col gap-2">
+ <QuestionCheckboxList
+ ref={listRef}
+ options={options}
+ selected={selected}
+ labelId={labelId}
+ focusActiveOption={autoFocus && !typing}
+ onToggle={handleToggle}
+ />
+ {typing ? (
+ <textarea
+ ref={textareaRef}
+ rows={2}
+ aria-labelledby={labelId}
+ autoFocus={autoFocus || toggled}
+ value={custom}
+ onChange={(e) => commit(selected, e.target.value)}
+ // Enter advances the pager; Shift+Enter is the newline.
+ onKeyDown={(e) => {
+ if (!isKey(e, "Enter") || e.shiftKey) return;
+ e.preventDefault();
+ onSubmit();
+ }}
+ placeholder="Add your own answer"
+ className="resize-none rounded-2xl bg-zinc-50 px-4 py-3 text-base leading-relaxed text-zinc-800 ring-1 ring-zinc-100 transition-shadow placeholder:text-zinc-400 focus:outline-none focus:ring-zinc-300"
+ />
+ ) : (
+ <button
+ type="button"
+ onClick={() => {
+ setToggled(true);
+ setTyping(true);
+ }}
+ className="flex items-center gap-2.5 rounded-2xl border border-dashed border-zinc-200 px-4 py-3 text-left text-base text-zinc-500 transition-colors hover:border-zinc-300 hover:text-zinc-700"
+ >
+ <Icon icon={PencilEdit02Icon} size={16} className="shrink-0" />
+ Type something…
+ </button>
+ )}
+ </div>
+ );
+});
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/QuestionsSection.stories.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/QuestionsSection.stories.tsx
new file mode 100644
index 0000000000..c8cc5ed89f
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/QuestionsSection.stories.tsx
@@ -0,0 +1,93 @@
+import type { Meta, StoryObj } from "@storybook/nextjs";
+import { domAnimation, LazyMotion } from "framer-motion";
+import { useState } from "react";
+import {
+ type ClarifyingQuestion,
+ isAnswered,
+ type QuestionAnswer,
+} from "../../tools/clarifying-questions";
+import { QuestionsSection } from "./QuestionsSection";
+
+interface CardProps {
+ questions: ClarifyingQuestion[];
+ answers?: Record<string, QuestionAnswer>;
+}
+
+/** The section as the chain renders it: inside the action card's chrome and the
+ * tool chain's LazyMotion, with the answer state the chain normally owns so
+ * picks actually stick and readiness derived from it the way the card does. */
+function QuestionsCard({ questions, answers: initial = {} }: CardProps) {
+ const [answers, setAnswers] =
+ useState<Record<string, QuestionAnswer>>(initial);
+ const isReady =
+ questions.length > 0 &&
+ questions.every((q) => isAnswered(answers[q.keyword]));
+ return (
+ <LazyMotion features={domAnimation} strict>
+ <div className="w-[34rem] overflow-hidden rounded-3xl border border-zinc-100 bg-white">
+ <QuestionsSection
+ requests={[
+ {
+ id: "questions-1",
+ questions,
+ answers,
+ onAnswer: (keyword, value) =>
+ setAnswers((prev) => ({ ...prev, [keyword]: value })),
+ onSkip: () => undefined,
+ },
+ ]}
+ isReady={isReady}
+ onProceed={() => undefined}
+ />
+ </div>
+ </LazyMotion>
+ );
+}
+
+const meta: Meta<typeof QuestionsCard> = {
+ title: "Copilot/QuestionsSection",
+ component: QuestionsCard,
+ parameters: {
+ docs: {
+ description: {
+ component:
+ 'The copilot\'s "Answer a few questions" card. One question per step: single-select options are radios that advance the pager on a pick, multi-select ones are checkboxes that stay put so several can be ticked, and either can be answered in free text instead.',
+ },
+ },
+ },
+};
+export default meta;
+
+type Story = StoryObj<typeof QuestionsCard>;
+
+export const SingleSelect: Story = {
+ args: {
+ questions: [
+ {
+ question: "Which channel should it post to?",
+ keyword: "channel",
+ options: ["Email", "Slack", "Google Docs"],
+ },
+ ],
+ },
+};
+
+export const MultiSelect: Story = {
+ args: {
+ questions: [
+ {
+ question: "What areas should they own? Pick roles",
+ keyword: "areas",
+ options: ["Research", "Outreach", "Reporting", "Scheduling"],
+ allow_multiple: true,
+ },
+ ],
+ },
+};
+
+export const MultiSelectAnswered: Story = {
+ args: {
+ ...MultiSelect.args,
+ answers: { areas: { selected: ["Research", "Reporting"], custom: "" } },
+ },
+};
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.tsx
index 735de9ab31..9320ca7852 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
@@ -9,8 +9,14 @@ import {
} from "@hugeicons/core-free-icons";
import { m } from "framer-motion";
import { useId, useState } from "react";
+import {
+ isAnswered,
+ toAnswerText,
+ toMultiAnswer,
+} from "../../tools/clarifying-questions";
import type { QuestionRequest } from "./helpers";
import { QuestionAnswerField } from "./QuestionAnswerField";
+import { QuestionMultiAnswerField } from "./QuestionMultiAnswerField";
interface Props {
requests: QuestionRequest[];
@@ -18,7 +24,8 @@ interface Props {
onProceed: () => void;
}
-/** One question per step. Picking an option advances on its own; the footer
+/** One question per step. Picking an option advances on its own — except on a
+ * multi-select question, where there is a second pick to make; the footer
* pager (chevrons + ring dots) moves between questions, and the round action
* button advances and, on the last step, sends every answer as one message. */
export function QuestionsSection({ requests, isReady, onProceed }: Props) {
@@ -37,7 +44,9 @@ export function QuestionsSection({ requests, isReady, onProceed }: Props) {
const current = Math.min(step, questions.length - 1);
const { request, question, id } = questions[current];
const labelId = `${sectionId}-${id}`;
- const answered = (request.answers[question.keyword] ?? "").trim().length > 0;
+ const answer = request.answers[question.keyword];
+ const multiOptions = question.allow_multiple ? (question.options ?? []) : [];
+ const answered = isAnswered(answer);
const isLast = current === questions.length - 1;
const actionEnabled = isLast ? isReady : answered;
@@ -87,19 +96,31 @@ export function QuestionsSection({ requests, isReady, onProceed }: Props) {
>
{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)}
- onPick={handlePick}
- onSubmit={handleAction}
- />
+ {multiOptions.length > 0 ? (
+ <QuestionMultiAnswerField
+ key={id}
+ options={multiOptions}
+ value={toMultiAnswer(answer)}
+ labelId={labelId}
+ autoFocus={current > 0}
+ onChange={(value) => request.onAnswer(question.keyword, value)}
+ onSubmit={handleAction}
+ />
+ ) : (
+ <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={toAnswerText(answer)}
+ labelId={labelId}
+ autoFocus={current > 0}
+ onChange={(value) => request.onAnswer(question.keyword, value)}
+ onPick={handlePick}
+ onSubmit={handleAction}
+ />
+ )}
</m.div>
<div className="flex items-center justify-between px-5 pb-4 pt-1">
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.tsx
index 2dd4ca5c29..b9f5346bf6 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
@@ -1307,4 +1307,162 @@ describe("ChainActionCard", () => {
expect(onProceed).toHaveBeenCalledOnce();
});
});
+ describe("multi-select questions", () => {
+ const areas = {
+ question: "What areas should they own?",
+ keyword: "areas",
+ options: ["Research", "Outreach", "Reporting"],
+ allow_multiple: true,
+ };
+
+ it("renders the options as checkboxes instead of radios", () => {
+ renderCard({ questions: [questionRequest({ questions: [areas] })] });
+
+ expect(screen.getByRole("checkbox", { name: "Research" })).toBeDefined();
+ expect(screen.queryByRole("radio", { name: "Research" })).toBeNull();
+ });
+
+ it("sends every pick, in the order the options were offered", () => {
+ const request = questionRequest({ questions: [areas] });
+ const { rerender } = renderCard({ questions: [request] });
+
+ fireEvent.click(screen.getByRole("checkbox", { name: "Reporting" }));
+ expect(request.onAnswer).toHaveBeenLastCalledWith("areas", {
+ selected: ["Reporting"],
+ custom: "",
+ });
+
+ const withOne = questionRequest({
+ questions: [areas],
+ answers: { areas: { selected: ["Reporting"], custom: "" } },
+ onAnswer: request.onAnswer,
+ });
+ rerender(
+ <ChainActionCard
+ connectors={[]}
+ mcp={[]}
+ inputs={[]}
+ questions={[withOne]}
+ manualProceed={false}
+ isReady
+ onProceed={vi.fn()}
+ />,
+ );
+ fireEvent.click(screen.getByRole("checkbox", { name: "Research" }));
+
+ expect(withOne.onAnswer).toHaveBeenLastCalledWith("areas", {
+ selected: ["Research", "Reporting"],
+ custom: "",
+ });
+ });
+
+ it("unticks a pick that is clicked again", () => {
+ const request = questionRequest({
+ questions: [areas],
+ answers: { areas: { selected: ["Research", "Outreach"], custom: "" } },
+ });
+ renderCard({ questions: [request] });
+
+ fireEvent.click(screen.getByRole("checkbox", { name: "Research" }));
+ expect(request.onAnswer).toHaveBeenLastCalledWith("areas", {
+ selected: ["Outreach"],
+ custom: "",
+ });
+ });
+
+ it("keeps the options on screen while typing an extra answer", () => {
+ const request = questionRequest({
+ questions: [areas],
+ answers: { areas: { selected: ["Research"], custom: "" } },
+ });
+ renderCard({ questions: [request] });
+
+ fireEvent.click(screen.getByText("Type something…"));
+ fireEvent.change(screen.getByRole("textbox"), {
+ target: { value: "Partnerships" },
+ });
+
+ expect(screen.getByRole("checkbox", { name: "Research" })).toBeDefined();
+ expect(request.onAnswer).toHaveBeenLastCalledWith("areas", {
+ selected: ["Research"],
+ custom: "Partnerships",
+ });
+ });
+
+ it("stays on the question after a pick instead of advancing", () => {
+ const request = questionRequest({
+ questions: [areas, { question: "Which region?", keyword: "region" }],
+ });
+ renderCard({ questions: [request] });
+
+ fireEvent.click(screen.getByRole("checkbox", { name: "Outreach" }));
+ expect(screen.getByText("What areas should they own?")).toBeDefined();
+ expect(screen.queryByText("Which region?")).toBeNull();
+ });
+
+ it("gates the send button until at least one option is ticked", () => {
+ const { onProceed } = renderCard({
+ questions: [questionRequest({ questions: [areas] })],
+ });
+
+ const send = screen
+ .getByRole("button", { name: "Send answers" })
+ .closest("button") as HTMLButtonElement;
+ expect(send.disabled).toBe(true);
+ fireEvent.click(send);
+ expect(onProceed).not.toHaveBeenCalled();
+ });
+
+ it("sends once the picks are in", () => {
+ const { onProceed } = renderCard({
+ questions: [
+ questionRequest({
+ questions: [areas],
+ answers: {
+ areas: { selected: ["Research", "Outreach"], custom: "" },
+ },
+ }),
+ ],
+ });
+
+ fireEvent.click(screen.getByRole("button", { name: "Send answers" }));
+ expect(onProceed).toHaveBeenCalledOnce();
+ });
+
+ it("leaves a single-select question picking exactly one option", () => {
+ const request = questionRequest({
+ questions: [
+ {
+ question: "Which channel?",
+ keyword: "channel",
+ options: ["Email", "Slack"],
+ },
+ ],
+ });
+ renderCard({ questions: [request] });
+
+ fireEvent.click(screen.getByRole("radio", { name: "Slack" }));
+ expect(request.onAnswer).toHaveBeenLastCalledWith("channel", "Slack");
+ expect(screen.queryByRole("checkbox")).toBeNull();
+ });
+
+ it("ignores the flag on a question with no options", () => {
+ renderCard({
+ questions: [
+ questionRequest({
+ questions: [
+ {
+ question: "Anything else?",
+ keyword: "notes",
+ allow_multiple: true,
+ },
+ ],
+ }),
+ ],
+ });
+
+ expect(screen.queryByRole("checkbox")).toBeNull();
+ expect(screen.getByRole("textbox")).toBeDefined();
+ });
+ });
});
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/__tests__/QuestionMultiAnswerField.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/__tests__/QuestionMultiAnswerField.test.tsx
new file mode 100644
index 0000000000..0f84319473
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/__tests__/QuestionMultiAnswerField.test.tsx
@@ -0,0 +1,231 @@
+import { cleanup, render, screen } from "@testing-library/react";
+import userEvent from "@testing-library/user-event";
+import { useState } from "react";
+import { afterEach, describe, expect, it } from "vitest";
+import type {
+ MultiAnswer,
+ QuestionAnswer,
+} from "../../../tools/clarifying-questions";
+import { QuestionMultiAnswerField } from "../QuestionMultiAnswerField";
+import { QuestionsSection } from "../QuestionsSection";
+
+const options = ["Research", "Outreach", "Reporting"];
+
+// The field is controlled, so every keystroke round-trips through state the
+// way it does under the chain card; a fired change event would not catch text
+// that is lost between one render and the next.
+function FieldHarness({
+ initial,
+ autoFocus = false,
+}: {
+ initial: MultiAnswer;
+ autoFocus?: boolean;
+}) {
+ const [value, setValue] = useState<MultiAnswer>(initial);
+ return (
+ <>
+ <span id="areas">What areas should they own?</span>
+ <QuestionMultiAnswerField
+ options={options}
+ value={value}
+ labelId="areas"
+ autoFocus={autoFocus}
+ onChange={setValue}
+ onSubmit={() => undefined}
+ />
+ <output data-testid="answer">{JSON.stringify(value)}</output>
+ </>
+ );
+}
+
+function SectionHarness() {
+ const [answers, setAnswers] = useState<Record<string, QuestionAnswer>>({});
+ return (
+ <>
+ <QuestionsSection
+ requests={[
+ {
+ id: "questions-1",
+ questions: [
+ {
+ question: "What areas should they own?",
+ keyword: "areas",
+ options,
+ allow_multiple: true,
+ },
+ { question: "Which region?", keyword: "region" },
+ ],
+ answers,
+ onAnswer: (keyword, value) =>
+ setAnswers((prev) => ({ ...prev, [keyword]: value })),
+ onSkip: () => undefined,
+ },
+ ]}
+ isReady={false}
+ onProceed={() => undefined}
+ />
+ <output data-testid="answer">{JSON.stringify(answers.areas)}</output>
+ </>
+ );
+}
+
+function empty(): MultiAnswer {
+ return { selected: [], custom: "" };
+}
+
+function answer(): unknown {
+ return JSON.parse(screen.getByTestId("answer").textContent ?? "null");
+}
+
+function textbox(): HTMLTextAreaElement {
+ return screen.getByRole("textbox") as HTMLTextAreaElement;
+}
+
+function ticked(): string[] {
+ return screen
+ .getAllByRole("checkbox")
+ .filter((box) => box.getAttribute("aria-checked") === "true")
+ .map((box) => box.textContent ?? "");
+}
+
+describe("QuestionMultiAnswerField", () => {
+ afterEach(cleanup);
+
+ it("keeps typed text whole when it passes through an option on the way", async () => {
+ const user = userEvent.setup();
+ render(<FieldHarness initial={empty()} />);
+
+ await user.click(screen.getByRole("button", { name: "Type something…" }));
+ await user.type(textbox(), "Research and development");
+
+ expect(textbox().value).toBe("Research and development");
+ expect(ticked()).toEqual([]);
+ expect(answer()).toEqual({
+ selected: [],
+ custom: "Research and development",
+ });
+ });
+
+ it("keeps text that exactly matches an option as the user's own words", async () => {
+ const user = userEvent.setup();
+ render(<FieldHarness initial={empty()} />);
+
+ await user.click(screen.getByRole("button", { name: "Type something…" }));
+ await user.type(textbox(), "Research");
+
+ expect(textbox().value).toBe("Research");
+ expect(ticked()).toEqual([]);
+ expect(answer()).toEqual({ selected: [], custom: "Research" });
+ });
+
+ it("lets an existing custom answer be edited through an option value", async () => {
+ const user = userEvent.setup();
+ render(<FieldHarness initial={{ selected: [], custom: "Researcher" }} />);
+
+ expect(textbox().value).toBe("Researcher");
+ await user.type(textbox(), "{backspace}{backspace}");
+ expect(textbox().value).toBe("Research");
+ expect(ticked()).toEqual([]);
+
+ await user.type(textbox(), "ing");
+
+ expect(textbox().value).toBe("Researching");
+ expect(ticked()).toEqual([]);
+ expect(answer()).toEqual({ selected: [], custom: "Researching" });
+ });
+
+ it("does not steal focus for a saved custom answer unless asked to", async () => {
+ const user = userEvent.setup();
+ render(<FieldHarness initial={{ selected: [], custom: "Researcher" }} />);
+ expect(document.activeElement).not.toBe(textbox());
+
+ cleanup();
+ render(
+ <FieldHarness
+ initial={{ selected: [], custom: "Researcher" }}
+ autoFocus
+ />,
+ );
+ expect(document.activeElement).toBe(textbox());
+
+ cleanup();
+ render(<FieldHarness initial={empty()} />);
+ await user.click(screen.getByRole("button", { name: "Type something…" }));
+ expect(document.activeElement).toBe(textbox());
+ });
+
+ it("keeps the ticks and the text apart while both change", async () => {
+ const user = userEvent.setup();
+ render(<FieldHarness initial={{ selected: ["Research"], custom: "" }} />);
+
+ await user.click(screen.getByRole("button", { name: "Type something…" }));
+ await user.type(textbox(), "Research");
+ expect(ticked()).toEqual(["Research"]);
+ expect(textbox().value).toBe("Research");
+ expect(answer()).toEqual({ selected: ["Research"], custom: "Research" });
+
+ await user.click(screen.getByRole("checkbox", { name: "Outreach" }));
+ expect(ticked()).toEqual(["Research", "Outreach"]);
+ expect(textbox().value).toBe("Research");
+
+ await user.click(screen.getByRole("checkbox", { name: "Research" }));
+ expect(ticked()).toEqual(["Outreach"]);
+ expect(textbox().value).toBe("Research");
+ expect(answer()).toEqual({ selected: ["Outreach"], custom: "Research" });
+
+ await user.type(textbox(), " lead");
+ expect(textbox().value).toBe("Research lead");
+ expect(ticked()).toEqual(["Outreach"]);
+ });
+
+ it("keeps keyboard focus on the box just unticked instead of the first tick", async () => {
+ const user = userEvent.setup();
+ render(<FieldHarness initial={empty()} autoFocus />);
+
+ const research = screen.getByRole("checkbox", { name: "Research" });
+ const reporting = screen.getByRole("checkbox", { name: "Reporting" });
+ expect(document.activeElement).toBe(research);
+
+ await user.keyboard("{ArrowDown}{ArrowDown}{Enter}");
+ expect(ticked()).toEqual(["Reporting"]);
+ expect(document.activeElement).toBe(reporting);
+
+ await user.keyboard("{ArrowDown}{Enter}");
+ expect(ticked()).toEqual(["Research", "Reporting"]);
+ expect(document.activeElement).toBe(research);
+
+ await user.keyboard("{Enter}");
+ expect(ticked()).toEqual(["Reporting"]);
+ expect(document.activeElement).toBe(research);
+ });
+
+ it("orders the ticks as the options were offered", async () => {
+ const user = userEvent.setup();
+ render(<FieldHarness initial={empty()} />);
+
+ await user.click(screen.getByRole("checkbox", { name: "Reporting" }));
+ await user.click(screen.getByRole("checkbox", { name: "Research" }));
+
+ expect(answer()).toEqual({
+ selected: ["Research", "Reporting"],
+ custom: "",
+ });
+ });
+
+ it("keeps a custom answer that equals an option when paging away and back", async () => {
+ const user = userEvent.setup();
+ render(<SectionHarness />);
+
+ await user.click(screen.getByRole("checkbox", { name: "Outreach" }));
+ await user.click(screen.getByRole("button", { name: "Type something…" }));
+ await user.type(textbox(), "Research");
+
+ await user.click(screen.getByRole("button", { name: "Go to question 2" }));
+ expect(screen.getByText("Which region?")).toBeDefined();
+ await user.click(screen.getByRole("button", { name: "Go to question 1" }));
+
+ expect(textbox().value).toBe("Research");
+ expect(ticked()).toEqual(["Outreach"]);
+ expect(answer()).toEqual({ selected: ["Outreach"], custom: "Research" });
+ });
+});
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/helpers.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/helpers.ts
index 71c5354a6a..c7f5ddd57e 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/helpers.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/helpers.ts
@@ -4,15 +4,18 @@ import { formatProviderName } from "@/components/contextual/IntegrationsPanel/he
import type { CredentialsMetaInput } from "@/lib/autogpt-server-api/types";
import type { RJSFSchema } from "@rjsf/utils";
import type { ExpertGrant } from "../SetupRequirementsCard/helpers";
-import type { ClarifyingQuestion } from "../../tools/clarifying-questions";
+import type {
+ ClarifyingQuestion,
+ QuestionAnswer,
+} from "../../tools/clarifying-questions";
/** One question card's ask. The card owns the inputs; the asking component
* keeps the answers so it can still build its own message. */
export interface QuestionRequest {
id: string;
questions: ClarifyingQuestion[];
- answers: Record<string, string>;
- onAnswer: (keyword: string, value: string) => void;
+ answers: Record<string, QuestionAnswer>;
+ onAnswer: (keyword: string, value: QuestionAnswer) => void;
onSkip: () => void;
}
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/QuestionDock/QuestionDock.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/QuestionDock/QuestionDock.tsx
index ee85db5e3b..25703f6321 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/QuestionDock/QuestionDock.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/QuestionDock/QuestionDock.tsx
@@ -1,7 +1,14 @@
"use client";
import type { UIDataTypes, UIMessage, UITools } from "ai";
-import { useContext, useEffect, useRef, useState } from "react";
+import { useContext, useEffect, useId, useRef, useState } from "react";
+import {
+ isAnswered,
+ toAnswerText,
+ toMultiAnswer,
+ type QuestionAnswer,
+} from "../../tools/clarifying-questions";
+import { QuestionMultiAnswerField } from "../ChainActionCard/QuestionMultiAnswerField";
import { CopilotChatActionsContext } from "../CopilotChatActionsProvider/useCopilotChatActions";
import { ChainActionsContext } from "../ToolChain/chainActions";
import {
@@ -21,13 +28,14 @@ interface FormProps {
export function QuestionsForm({ dockId, questions }: FormProps) {
const actions = useContext(CopilotChatActionsContext);
const chainActions = useContext(ChainActionsContext);
- const [answers, setAnswers] = useState<Record<string, string>>({});
+ const [answers, setAnswers] = useState<Record<string, QuestionAnswer>>({});
const [dismissedId, setDismissedId] = useState<string | null>(null);
const [renderedDockId, setRenderedDockId] = useState<string | null>(null);
- const inputRefs = useRef<Record<string, HTMLInputElement | null>>({});
+ const inputRefs = useRef<Record<string, { focus: () => void } | null>>({});
+ const formId = useId();
const dismissed = dockId === dismissedId;
- const allAnswered = questions.every((q) => answers[q.keyword]?.trim());
+ const allAnswered = questions.every((q) => isAnswered(answers[q.keyword]));
// Inside a tool chain the Answer button is replaced by the chain's single
// Proceed step — register readiness + message instead.
@@ -68,7 +76,7 @@ export function QuestionsForm({ dockId, questions }: FormProps) {
if (chainActions) return;
if (!actions) return;
if (!allAnswered) {
- const unanswered = questions.find((q) => !answers[q.keyword]?.trim());
+ const unanswered = questions.find((q) => !isAnswered(answers[q.keyword]));
if (unanswered) inputRefs.current[unanswered.keyword]?.focus();
return;
}
@@ -91,30 +99,56 @@ export function QuestionsForm({ dockId, questions }: FormProps) {
</button>
</div>
<div className="flex flex-col gap-2.5">
- {questions.map((q) => (
- <label key={q.keyword} className="flex flex-col gap-1">
- <span className="text-[13px] text-zinc-700">{q.question}</span>
- <input
- ref={(element) => {
- inputRefs.current[q.keyword] = element;
- }}
- type="text"
- required
- value={answers[q.keyword] ?? ""}
- onChange={(e) =>
- setAnswers((prev) => ({
- ...prev,
- [q.keyword]: e.target.value,
- }))
- }
- onKeyDown={(e) => {
- if (isKey(e, "Enter")) handleSubmit();
- }}
- placeholder={q.example ? `e.g. ${q.example}` : "Type your answer"}
- className="rounded-xl bg-zinc-50 px-2.5 py-1.5 text-[13px] text-zinc-800 ring-1 ring-zinc-200/70 transition-shadow placeholder:text-zinc-400 focus:outline-none focus:ring-zinc-400"
- />
- </label>
- ))}
+ {questions.map((q) =>
+ q.allow_multiple && q.options ? (
+ <div key={q.keyword} className="flex flex-col gap-1">
+ <span
+ id={`${formId}-${q.keyword}`}
+ className="text-[13px] text-zinc-700"
+ >
+ {q.question}
+ </span>
+ <QuestionMultiAnswerField
+ ref={(element) => {
+ inputRefs.current[q.keyword] = element;
+ }}
+ options={q.options}
+ value={toMultiAnswer(answers[q.keyword])}
+ labelId={`${formId}-${q.keyword}`}
+ autoFocus={false}
+ onChange={(value) =>
+ setAnswers((prev) => ({ ...prev, [q.keyword]: value }))
+ }
+ onSubmit={handleSubmit}
+ />
+ </div>
+ ) : (
+ <label key={q.keyword} className="flex flex-col gap-1">
+ <span className="text-[13px] text-zinc-700">{q.question}</span>
+ <input
+ ref={(element) => {
+ inputRefs.current[q.keyword] = element;
+ }}
+ type="text"
+ required
+ value={toAnswerText(answers[q.keyword])}
+ onChange={(e) =>
+ setAnswers((prev) => ({
+ ...prev,
+ [q.keyword]: e.target.value,
+ }))
+ }
+ onKeyDown={(e) => {
+ if (isKey(e, "Enter")) handleSubmit();
+ }}
+ placeholder={
+ q.example ? `e.g. ${q.example}` : "Type your answer"
+ }
+ className="rounded-xl bg-zinc-50 px-2.5 py-1.5 text-[13px] text-zinc-800 ring-1 ring-zinc-200/70 transition-shadow placeholder:text-zinc-400 focus:outline-none focus:ring-zinc-400"
+ />
+ </label>
+ ),
+ )}
</div>
{!chainActions && (
<div className="flex justify-end pt-2.5">
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/QuestionDock/__tests__/QuestionDock.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/QuestionDock/__tests__/QuestionDock.test.tsx
index 75c7363028..b919645ac7 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/QuestionDock/__tests__/QuestionDock.test.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/QuestionDock/__tests__/QuestionDock.test.tsx
@@ -81,6 +81,68 @@ describe("QuestionDock", () => {
expect(screen.queryByRole("textbox")).toBeNull();
});
+ it("keeps a multi-select answer as a list and sends the picks as bullets", async () => {
+ const user = userEvent.setup();
+ const onSend = renderDock([
+ questionMessage([
+ {
+ question: "What areas should they own?",
+ keyword: "areas",
+ options: ["Research", "Outreach", "Reporting"],
+ allow_multiple: true,
+ },
+ ]),
+ ]);
+
+ expect(
+ screen.getByRole("group", { name: "What areas should they own?" }),
+ ).toBeDefined();
+ await user.click(screen.getByRole("checkbox", { name: "Research" }));
+ await user.click(screen.getByRole("checkbox", { name: "Reporting" }));
+ expect(
+ screen
+ .getByRole("checkbox", { name: "Outreach" })
+ .getAttribute("aria-checked"),
+ ).toBe("false");
+
+ await user.click(screen.getByRole("button", { name: "Answer" }));
+
+ expect(onSend).toHaveBeenCalledOnce();
+ expect(onSend).toHaveBeenCalledWith(
+ "**Here are my answers:**\n\n> What areas should they own?\n\n- Research\n- Reporting\n\nPlease proceed.",
+ );
+ });
+
+ it("focuses an unanswered multi-select question instead of sending", async () => {
+ const user = userEvent.setup();
+ const onSend = renderDock([
+ questionMessage([
+ { question: "Which region?", keyword: "region" },
+ {
+ question: "What areas should they own?",
+ keyword: "areas",
+ options: ["Research", "Outreach", "Reporting"],
+ allow_multiple: true,
+ },
+ ]),
+ ]);
+
+ await user.type(screen.getByLabelText("Which region?"), "Europe");
+ await user.click(screen.getByRole("button", { name: "Answer" }));
+
+ expect(onSend).not.toHaveBeenCalled();
+ expect(document.activeElement).toBe(
+ screen.getByRole("checkbox", { name: "Research" }),
+ );
+
+ await user.keyboard("{ArrowDown}{Enter}");
+ await user.click(screen.getByRole("button", { name: "Answer" }));
+ expect(onSend).toHaveBeenCalledOnce();
+ expect(onSend).toHaveBeenCalledWith(
+ "**Here are my answers:**\n\n> Which region?\n\nEurope\n\n> What areas should they own?\n\nOutreach\n\nPlease proceed.",
+ );
+ });
+
it("hides after skipping", async () => {
const user = userEvent.setup();
const onSend = renderDock([
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/QuestionDock/__tests__/helpers.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/QuestionDock/__tests__/helpers.test.ts
index 37c8473700..86468ae43c 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/QuestionDock/__tests__/helpers.test.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/QuestionDock/__tests__/helpers.test.ts
@@ -158,6 +158,33 @@ describe("getPendingQuestions", () => {
});
describe("buildAnswersMessage", () => {
+ it("renders a multi-select answer as a bullet list", () => {
+ const message = buildAnswersMessage(
+ [
+ {
+ question: "What areas should they own?",
+ keyword: "areas",
+ options: ["Research", "Outreach"],
+ allow_multiple: true,
+ },
+ ],
+ { areas: { selected: ["Research"], custom: " Outreach " } },
+ );
+
+ expect(message).toContain(
+ "> What areas should they own?\n\n- Research\n- Outreach",
+ );
+ });
+
+ it("leaves a one-pick multi-select answer inline", () => {
+ const message = buildAnswersMessage(
+ [{ question: "Which areas?", keyword: "areas", allow_multiple: true }],
+ { areas: { selected: ["Research"], custom: "" } },
+ );
+
+ expect(message).toContain("> Which areas?\n\nResearch\n\nPlease proceed.");
+ });
+
it("quotes each question above its trimmed answer", () => {
const message = buildAnswersMessage(
[
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/QuestionDock/helpers.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/QuestionDock/helpers.ts
index 3dd74fd150..7b23e8a80e 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/QuestionDock/helpers.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/QuestionDock/helpers.ts
@@ -1,7 +1,9 @@
import type { ToolUIPart, UIDataTypes, UIMessage, UITools } from "ai";
import {
type ClarifyingQuestion,
+ type QuestionAnswer,
extractClarifyingQuestions,
+ formatAnswer,
normalizeClarifyingQuestions,
} from "../../tools/clarifying-questions";
@@ -64,10 +66,10 @@ export function getPendingQuestions(
export function buildAnswersMessage(
questions: ClarifyingQuestion[],
- answers: Record<string, string>,
+ answers: Record<string, QuestionAnswer>,
): string {
const body = questions
- .map((q) => `> ${q.question}\n\n${answers[q.keyword].trim()}`)
+ .map((q) => `> ${q.question}\n\n${formatAnswer(answers[q.keyword])}`)
.join("\n\n");
return `**Here are my answers:**\n\n${body}\n\nPlease proceed.`;
}
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.ts
index d02dc7fbcc..917106425a 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
@@ -2,7 +2,11 @@ import { describe, expect, it } from "vitest";
import {
buildClarificationAnswersMessage,
extractClarifyingQuestions,
+ formatAnswer,
+ isAnswered,
normalizeClarifyingQuestions,
+ toAnswerList,
+ toMultiAnswer,
} from "../clarifying-questions";
describe("normalizeClarifyingQuestions", () => {
@@ -81,6 +85,78 @@ describe("normalizeClarifyingQuestions", () => {
});
describe("extractClarifyingQuestions", () => {
+ it("carries the multi-select flag through from the output", () => {
+ const result = extractClarifyingQuestions({
+ output: {
+ questions: [
+ {
+ question: "Which areas?",
+ keyword: "areas",
+ options: ["Research", "Outreach"],
+ allow_multiple: true,
+ },
+ ],
+ },
+ });
+ expect(result[0].allow_multiple).toBe(true);
+ });
+
+ it("recovers the multi-select flag from the input the model sent", () => {
+ const result = extractClarifyingQuestions({
+ input: {
+ questions: [
+ {
+ question: "Which areas?",
+ keyword: "areas",
+ options: ["Research", "Outreach"],
+ allow_multiple: true,
+ },
+ ],
+ },
+ output: {
+ questions: [
+ {
+ question: "Which areas?",
+ keyword: "areas",
+ example: "Research, Outreach",
+ },
+ ],
+ },
+ });
+ expect(result[0].allow_multiple).toBe(true);
+ expect(result[0].options).toEqual(["Research", "Outreach"]);
+ });
+
+ it("leaves a question single-select when nothing asked for more", () => {
+ const result = extractClarifyingQuestions({
+ output: {
+ questions: [
+ {
+ question: "Which channel?",
+ keyword: "channel",
+ options: ["Email", "Slack"],
+ },
+ ],
+ },
+ });
+ expect(result[0].allow_multiple).toBeUndefined();
+ });
+
+ it("drops the multi-select flag from a question with no options", () => {
+ const result = extractClarifyingQuestions({
+ output: {
+ questions: [
+ {
+ question: "Anything else?",
+ keyword: "notes",
+ allow_multiple: true,
+ },
+ ],
+ },
+ });
+ expect(result[0].allow_multiple).toBeUndefined();
+ });
+
it("reads options straight from the output when present", () => {
const result = extractClarifyingQuestions({
output: {
@@ -196,6 +272,53 @@ describe("extractClarifyingQuestions", () => {
});
});
+describe("multi-select answers", () => {
+ it("reads one answer and many through the same list", () => {
+ expect(toAnswerList(" Europe ")).toEqual(["Europe"]);
+ expect(
+ toAnswerList({ selected: ["Research"], custom: " Outreach " }),
+ ).toEqual(["Research", "Outreach"]);
+ expect(toAnswerList({ selected: ["Research"], custom: " " })).toEqual([
+ "Research",
+ ]);
+ });
+
+ it("keeps typed text after the ticks, even when it equals one", () => {
+ expect(
+ toAnswerList({ selected: ["Research"], custom: "Research" }),
+ ).toEqual(["Research", "Research"]);
+ });
+
+ it("treats a blank answer of either shape as unanswered", () => {
+ expect(isAnswered(undefined)).toBe(false);
+ expect(isAnswered(" ")).toBe(false);
+ expect(isAnswered({ selected: [], custom: "" })).toBe(false);
+ expect(isAnswered({ selected: [], custom: " " })).toBe(false);
+ expect(isAnswered({ selected: ["Research"], custom: "" })).toBe(true);
+ expect(isAnswered({ selected: [], custom: "Partnerships" })).toBe(true);
+ });
+
+ it("bullets several picks and leaves one inline", () => {
+ expect(
+ formatAnswer({ selected: ["Research", "Outreach"], custom: "" }),
+ ).toBe("- Research\n- Outreach");
+ expect(formatAnswer({ selected: ["Research"], custom: "" })).toBe(
+ "Research",
+ );
+ expect(formatAnswer("Europe")).toBe("Europe");
+ });
+
+ it("reads a multi-select field's value from either answer shape", () => {
+ expect(toMultiAnswer(undefined)).toEqual({ selected: [], custom: "" });
+ expect(toMultiAnswer("Partnerships")).toEqual({
+ selected: [],
+ custom: "Partnerships",
+ });
+ const answer = { selected: ["Research"], custom: "Research" };
+ expect(toMultiAnswer(answer)).toBe(answer);
+ });
+});
+
describe("buildClarificationAnswersMessage", () => {
it("formats answers with create mode", () => {
const result = buildClarificationAnswersMessage(
@@ -217,6 +340,15 @@ describe("buildClarificationAnswersMessage", () => {
expect(result).toContain("Please proceed with editing the agent.");
});
+ it("lists a multi-select answer under its question", () => {
+ const result = buildClarificationAnswersMessage(
+ { areas: { selected: ["Research", "Outreach"], custom: "" } },
+ [{ question: "Which areas?", keyword: "areas" }],
+ "create",
+ );
+ expect(result).toContain("> Which areas?\n\n- Research\n- Outreach");
+ });
+
it("uses empty string for missing answers", () => {
const result = 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.ts
index 30c744842a..21f8872137 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
@@ -3,6 +3,57 @@ export interface ClarifyingQuestion {
keyword: string;
example?: string;
options?: string[];
+ /** Several of `options` may be picked. Never set without options. */
+ allow_multiple?: boolean;
+}
+
+/** A multi-select answer: the ticked options, and any typed text kept in its
+ * own slot rather than mixed in with them. Membership of `options` cannot
+ * tell the two apart — "Research" on the way to "Research and development"
+ * is typing, not a tick — so the shape has to. */
+export interface MultiAnswer {
+ selected: string[];
+ custom: string;
+}
+
+/** One question's answer: a single-select pick or typed text is a string, a
+ * multi-select one is a MultiAnswer. */
+export type QuestionAnswer = string | MultiAnswer;
+
+/** An answer as its list of picks, blanks dropped — so an empty list is the
+ * one definition of "not answered yet" every caller shares. */
+export function toAnswerList(answer: QuestionAnswer | undefined): string[] {
+ const picks =
+ typeof answer === "string" || answer === undefined
+ ? [answer ?? ""]
+ : [...answer.selected, answer.custom];
+ return picks.flatMap((pick) => (pick.trim() ? [pick.trim()] : []));
+}
+
+export function isAnswered(answer: QuestionAnswer | undefined): boolean {
+ return toAnswerList(answer).length > 0;
+}
+
+/** An answer as the raw text of a single-answer field. Untrimmed, so the
+ * space the user just typed survives the round trip through state. */
+export function toAnswerText(answer: QuestionAnswer | undefined): string {
+ return typeof answer === "string" ? answer : "";
+}
+
+/** An answer as a multi-select field reads it. A string answer was never a
+ * pick, so it becomes the typed text. */
+export function toMultiAnswer(answer: QuestionAnswer | undefined): MultiAnswer {
+ if (typeof answer === "string") return { selected: [], custom: answer };
+ return answer ?? { selected: [], custom: "" };
+}
+
+/** The answer as it reads in the message sent back: several picks become a
+ * bullet list, one stays inline so single-select replies are unchanged. */
+export function formatAnswer(answer: QuestionAnswer | undefined): string {
+ const picks = toAnswerList(answer);
+ return picks.length > 1
+ ? picks.map((pick) => `- ${pick}`).join("\n")
+ : (picks[0] ?? "");
}
function toOptions(value: unknown): string[] | undefined {
@@ -19,6 +70,7 @@ export function normalizeClarifyingQuestions(
keyword: string;
example?: unknown;
options?: unknown;
+ allow_multiple?: unknown;
}>,
): ClarifyingQuestion[] {
const seen = new Set<string>();
@@ -48,6 +100,9 @@ export function normalizeClarifyingQuestions(
if (example) item.example = example;
const options = toOptions(q.options);
if (options) item.options = options;
+ // Without options the card has nothing to toggle, so the flag would only
+ // promise a multi-select the user never gets.
+ if (options && q.allow_multiple === true) item.allow_multiple = true;
return item;
});
}
@@ -97,32 +152,33 @@ export function extractClarifyingQuestions(source: {
}): ClarifyingQuestion[] {
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);
- }
+ // Older tool outputs collapse options into the example string and predate
+ // allow_multiple, so when the output is the source both only survive in the
+ // input the model actually sent.
+ const fromInputByKey = new Map<string, Record<string, unknown>>();
+ for (const item of fromInput ?? []) {
+ fromInputByKey.set(recoveryKey(item), item);
}
- const valid = raw.flatMap((item) =>
- typeof item.question === "string" &&
- item.question.trim() &&
- typeof item.keyword === "string"
- ? [
- {
- question: item.question.trim(),
- keyword: item.keyword,
- example: item.example,
- options:
- toOptions(item.options) ?? inputOptions.get(recoveryKey(item)),
- },
- ]
- : [],
- );
+ const valid = raw.flatMap((item) => {
+ if (
+ typeof item.question !== "string" ||
+ !item.question.trim() ||
+ typeof item.keyword !== "string"
+ ) {
+ return [];
+ }
+ const asked = fromInputByKey.get(recoveryKey(item));
+ return [
+ {
+ question: item.question.trim(),
+ keyword: item.keyword,
+ example: item.example,
+ options: toOptions(item.options) ?? toOptions(asked?.options),
+ allow_multiple:
+ item.allow_multiple === true || asked?.allow_multiple === true,
+ },
+ ];
+ });
return normalizeClarifyingQuestions(valid);
}
@@ -130,15 +186,12 @@ export function extractClarifyingQuestions(source: {
* Formats clarification answers as a context message and sends it via onSend.
*/
export function buildClarificationAnswersMessage(
- answers: Record<string, string>,
+ answers: Record<string, QuestionAnswer>,
rawQuestions: Array<{ question: string; keyword: string }>,
mode: "create" | "edit",
): string {
const contextMessage = rawQuestions
- .map((q) => {
- const answer = answers[q.keyword] || "";
- return `> ${q.question}\n\n${answer}`;
- })
+ .map((q) => `> ${q.question}\n\n${formatAnswer(answers[q.keyword])}`)
.join("\n\n");
const action = mode === "create" ? "creating" : "editing";
diff --git a/autogpt_platform/frontend/src/app/api/openapi.json b/autogpt_platform/frontend/src/app/api/openapi.json
index a163879e16..6a358376e1 100644
--- a/autogpt_platform/frontend/src/app/api/openapi.json
+++ b/autogpt_platform/frontend/src/app/api/openapi.json
@@ -19023,6 +19023,11 @@
"items": { "type": "string" },
"type": "array",
"title": "Options"
+ },
+ "allow_multiple": {
+ "type": "boolean",
+ "title": "Allow Multiple",
+ "default": false
}
},
"type": "object",