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(frontend): let users edit and combine suggested answers in question cards (#15052)
Ubbe9a226788dedf
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.tsxindex 7859f72bd6..ec47b9cf72 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/QuestionAnswerField.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/QuestionAnswerField.tsx@@ -2,7 +2,7 @@import { Icon } from "@/components/atoms/Icon/Icon";import { PencilEdit02Icon } from "@hugeicons/core-free-icons";-import { useState } from "react";+import { useRef, useState } from "react";import type { ClarifyingQuestion } from "../../tools/clarifying-questions";import { QuestionOptionList } from "./QuestionOptionList";import { isKey } from "@/lib/keyboard";@@ -13,24 +13,23 @@ interface Props {labelId: string;autoFocus: boolean;onChange: (value: string) => void;- /** Only the option list can fire this — free text has no single moment- * where the answer is done. */- onPick: (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. */+ * options go straight to the textarea. The options stay on screen while the+ * user types, and each one can be copied into the textarea to reword it or+ * merge it with others.+ * 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,- onPick,onSubmit,}: Props) {const options = question.options ?? [];@@ -40,49 +39,52 @@ export function QuestionAnswerField({// 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);+ const textareaRef = useRef<HTMLTextAreaElement>(null);- if (options.length === 0 || typing) {- return (- <div className="flex flex-col gap-2">- <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 (!isKey(e, "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-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"- />- {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.5 py-1 text-sm text-zinc-500 transition-colors hover:bg-zinc-100 hover:text-zinc-700"- >- Choose from options instead- </button>- )}- </div>- );+ const textarea = (+ <textarea+ ref={textareaRef}+ required+ rows={options.length > 0 ? 2 : 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 (!isKey(e, "Enter") || e.shiftKey) return;+ e.preventDefault();+ onSubmit();+ }}+ // The example is the options joined, so replaying it right under+ // those same 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-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"+ />+ );++ if (options.length === 0) return textarea;++ // Choosing an option replaces whatever was typed, so the textarea closes+ // rather than echoing the option's text back as if the user had written it.+ function handleOptionChange(option: string) {+ setTyping(false);+ onChange(option);+ }++ // A second edit adds to the draft rather than replacing it, so the user+ // can build one answer out of several options.+ function handleOptionEdit(option: string) {+ const draft =+ typing && value.trim() ? `${value.trimEnd()}\n${option}` : option;+ onChange(draft);+ setToggled(true);+ setTyping(true);+ textareaRef.current?.focus();}return (@@ -91,23 +93,27 @@ export function QuestionAnswerField({options={options}value={value}labelId={labelId}- focusActiveOption={autoFocus || toggled}- onChange={onChange}- onPick={onPick}+ focusActiveOption={!typing && (autoFocus || toggled)}+ onChange={handleOptionChange}+ onEdit={handleOptionEdit}onSubmit={onSubmit}/>- <button- type="button"- onClick={() => {- if (options.includes(value.trim())) onChange("");- 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>+ {typing ? (+ textarea+ ) : (+ <button+ type="button"+ onClick={() => {+ if (options.includes(value.trim())) onChange("");+ 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/QuestionOptionList.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/QuestionOptionList.tsxindex 311669c9ce..fdff6cec92 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/QuestionOptionList.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/QuestionOptionList.tsx@@ -1,7 +1,7 @@"use client";import { Icon } from "@/components/atoms/Icon/Icon";-import { Tick02Icon } from "@hugeicons/core-free-icons";+import { PencilEdit02Icon, Tick02Icon } from "@hugeicons/core-free-icons";import { useEffect, useRef } from "react";import { isKey } from "@/lib/keyboard";@@ -13,9 +13,9 @@ interface Props {labelId: string;focusActiveOption: boolean;onChange: (value: string) => void;- /** A pointer or tap choice, as opposed to `onChange`, which the arrow keys- * fire too while the user is only browsing the group. */- onPick: (value: string) => void;+ /** Copies the option into free text, for an answer that is close but not+ * quite right as written. */+ onEdit: (value: string) => void;onSubmit: () => void;}@@ -28,7 +28,7 @@ export function QuestionOptionList({labelId,focusActiveOption,onChange,- onPick,+ onEdit,onSubmit,}: Props) {const refs = useRef<(HTMLButtonElement | null)[]>([]);@@ -49,8 +49,6 @@ export function QuestionOptionList({// 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.- // Space reaches the native button's click the same way, so it only selects,- // like the arrows do, leaving a pointer click the one thing that advances.if (isKey(event, "Enter")) {event.preventDefault();if (options[index] === value.trim()) onSubmit();@@ -82,29 +80,39 @@ export function QuestionOptionList({{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={() => onPick(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>- {isSelected && (- <Icon icon={Tick02Icon} size={16} className="shrink-0" />- )}- </button>+ <div key={option} className="relative">+ <button+ 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 w-full items-center justify-between gap-3 rounded-2xl py-3 pl-4 pr-12 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>+ {isSelected && (+ <Icon icon={Tick02Icon} size={16} className="shrink-0" />+ )}+ </button>+ <button+ type="button"+ aria-label={`Edit ${option}`}+ title="Edit before sending"+ onClick={() => onEdit(option)}+ className="absolute right-2 top-1/2 flex size-8 -translate-y-1/2 items-center justify-center rounded-xl text-zinc-400 transition-colors hover:bg-zinc-200/60 hover:text-zinc-700"+ >+ <Icon icon={PencilEdit02Icon} size={16} />+ </button>+ </div>);})}</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 9320ca7852..5c36dadbc7 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@@ -24,10 +24,10 @@ interface Props {onProceed: () => void;}-/** 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. */+/** One question per step. Picking an option only selects it, so the user can+ * change their mind or edit it first; 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) {const [step, setStep] = useState(0);const sectionId = useId();@@ -58,13 +58,6 @@ export function QuestionsSection({ requests, isReady, onProceed }: Props) {}}- // A tap on an option is the whole answer, so the pager moves on by itself;- // the last question keeps the send button as its explicit final step.- function handlePick(value: string) {- request.onAnswer(question.keyword, value);- if (!isLast) setStep(current + 1);- }-return (<><div className="flex items-center justify-between gap-2.5 border-b border-zinc-100 px-4 py-3">@@ -117,7 +110,6 @@ export function QuestionsSection({ requests, isReady, onProceed }: Props) {labelId={labelId}autoFocus={current > 0}onChange={(value) => request.onAnswer(question.keyword, value)}- onPick={handlePick}onSubmit={handleAction}/>)}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 b9f5346bf6..b2d87ba241 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@@ -731,7 +731,7 @@ describe("ChainActionCard", () => {).toBe("false");});- it("swaps to free text via Type something and clears a picked option", () => {+ it("opens free text via Type something, clears a picked option and keeps the options on screen", () => {const request = questionRequest({questions: [{@@ -747,9 +747,8 @@ describe("ChainActionCard", () => {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();+ expect(screen.getByRole("radio", { name: "Americas" })).toBeDefined();});it("opens in free text when the answer matches no option", () => {@@ -771,7 +770,7 @@ describe("ChainActionCard", () => {expect(screen.getByDisplayValue("Antarctica")).toBeDefined();});- it("clears a custom answer when going back to the options", () => {+ it("replaces a custom answer and closes the textarea when an option is picked", () => {const request = questionRequest({questions: [{@@ -784,8 +783,10 @@ describe("ChainActionCard", () => {});renderCard({ questions: [request] });- fireEvent.click(screen.getByText("Choose from options instead"));- expect(request.onAnswer).toHaveBeenCalledWith("region", "");+ fireEvent.click(screen.getByRole("radio", { name: "Americas" }));+ expect(request.onAnswer).toHaveBeenCalledWith("region", "Americas");+ expect(screen.queryByDisplayValue("Antarctica")).toBeNull();+ expect(screen.getByText("Type something…")).toBeDefined();});it("names the option group after the question", () => {@@ -1164,7 +1165,7 @@ describe("ChainActionCard", () => {expect(onProceed).toHaveBeenCalledOnce();});- it("advances to the next question when an option is clicked", () => {+ it("stays on the question when an option is clicked", () => {const request = questionRequest({questions: [{@@ -1179,7 +1180,28 @@ describe("ChainActionCard", () => {fireEvent.click(screen.getByRole("radio", { name: "Europe" }));expect(request.onAnswer).toHaveBeenCalledWith("region", "Europe");- expect(screen.getByText("Which format?")).toBeDefined();+ expect(screen.getByText("Which region?")).toBeDefined();+ expect(screen.queryByText("Which format?")).toBeNull();+ });++ it("copies an option into the textarea to edit it", () => {+ const request = questionRequest({+ questions: [+ {+ question: "Which region?",+ keyword: "region",+ options: ["Europe", "Americas"],+ },+ ],+ });+ renderCard({ questions: [request] });++ fireEvent.click(screen.getByRole("button", { name: "Edit Americas" }));+ expect(request.onAnswer).toHaveBeenCalledWith("region", "Americas");+ expect(document.activeElement).toBe(+ screen.getByPlaceholderText("Type your answer"),+ );+ expect(screen.getByRole("radio", { name: "Europe" })).toBeDefined();});it("stays on the last question after an option is clicked", () => {@@ -1290,9 +1312,9 @@ describe("ChainActionCard", () => {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.+ // free text with the stray value visible next to the new options.expect(screen.getByDisplayValue("Email")).toBeDefined();- expect(screen.queryByRole("radio", { name: "Notion" })).toBeNull();+ expect(screen.getByRole("radio", { name: "Notion" })).toBeDefined();});it("keeps the questions card sendable when an unready sibling exists", () => {diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/__tests__/QuestionAnswerField.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/__tests__/QuestionAnswerField.test.tsxindex 23c6e8b337..8bbeab3468 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/__tests__/QuestionAnswerField.test.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChainActionCard/__tests__/QuestionAnswerField.test.tsx@@ -11,7 +11,6 @@ function renderField(value = "ねこ") {labelId="label-1"autoFocus={false}onChange={() => {}}- onPick={() => {}}onSubmit={onSubmit}/>,);diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ExpertOnboardingCard/ExpertOnboardingCard.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ExpertOnboardingCard/ExpertOnboardingCard.tsxindex b8c8dc7dff..9dcae91f51 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ExpertOnboardingCard/ExpertOnboardingCard.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ExpertOnboardingCard/ExpertOnboardingCard.tsx@@ -80,7 +80,6 @@ function OnboardingForm({ onboarding, isLive }: FormProps) {value,advance,goBack,- pickAnswer,setAnswer,skip,} = useExpertOnboardingCard({ steps: onboarding.steps, isLive });@@ -162,7 +161,6 @@ function OnboardingForm({ onboarding, isLive }: FormProps) {labelId={labelId}autoFocus={current > 0}onChange={setAnswer}- onPick={pickAnswer}onSubmit={advance}/></m.div>diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ExpertOnboardingCard/__tests__/ExpertOnboardingCard.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ExpertOnboardingCard/__tests__/ExpertOnboardingCard.test.tsxindex b360793488..faf2102eea 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ExpertOnboardingCard/__tests__/ExpertOnboardingCard.test.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ExpertOnboardingCard/__tests__/ExpertOnboardingCard.test.tsx@@ -111,24 +111,45 @@ describe("ExpertOnboardingCard", () => {expect(screen.getByText("1 of 2")).toBeDefined();});- it("moves on by itself when an option is tapped", () => {+ it("waits for Next after an option is tapped", () => {renderCard(onboardingPart());expect(actionButton("Next question").disabled).toBe(true);fireEvent.click(screen.getByRole("radio", { name: /Social listening/ }));- // A tap is the whole answer, so the tap is also the page turn.- expect(screen.getByText("2 of 2")).toBeDefined();+ // The tap only selects, leaving room to change or edit the answer.+ expect(screen.getByText("1 of 2")).toBeDefined();+ expect(actionButton("Next question").disabled).toBe(false);++ fireEvent.click(actionButton("Next question"));expect(screen.getByText("Which service should I be connected to?"),).toBeDefined();});+ it("merges edited options into one typed answer", () => {+ renderCard(onboardingPart());++ fireEvent.click(+ screen.getByRole("button", { name: "Edit Social listening" }),+ );+ fireEvent.click(+ screen.getByRole("button", { name: "Edit Campaign briefs" }),+ );++ expect((screen.getByRole("textbox") as HTMLTextAreaElement).value).toBe(+ "Social listening\nCampaign briefs",+ );+ expect(screen.getAllByRole("radio")).toHaveLength(2);+ expect(screen.getByText("1 of 2")).toBeDefined();+ });+it("will not send until the last step is answered", () => {renderCard(onboardingPart());fireEvent.click(screen.getByRole("radio", { name: /Social listening/ }));+ fireEvent.click(actionButton("Next question"));expect(actionButton("Send answers").disabled).toBe(true);@@ -157,6 +178,7 @@ describe("ExpertOnboardingCard", () => {const { onSend } = renderCard(onboardingPart());fireEvent.click(screen.getByRole("radio", { name: /Social listening/ }));+ fireEvent.click(actionButton("Next question"));expect(screen.getByText("2 of 2")).toBeDefined();fireEvent.click(screen.getByRole("radio", { name: /Linear/ }));@@ -174,6 +196,7 @@ describe("ExpertOnboardingCard", () => {renderCard(onboardingPart());fireEvent.click(screen.getByRole("radio", { name: /Social listening/ }));+ fireEvent.click(actionButton("Next question"));fireEvent.click(actionButton("Previous question"));expect(screen.getByText("1 of 2")).toBeDefined();@@ -230,6 +253,7 @@ describe("ExpertOnboardingCard", () => {renderCard(onboardingPart(), CALL_ID, onSend);fireEvent.click(screen.getByRole("radio", { name: /Social listening/ }));+ fireEvent.click(actionButton("Next question"));fireEvent.click(screen.getByRole("radio", { name: /Linear/ }));fireEvent.click(actionButton("Send answers"));diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ExpertOnboardingCard/useExpertOnboardingCard.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ExpertOnboardingCard/useExpertOnboardingCard.tsindex 6b311f7829..cc25382a08 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ExpertOnboardingCard/useExpertOnboardingCard.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ExpertOnboardingCard/useExpertOnboardingCard.ts@@ -34,13 +34,6 @@ export function useExpertOnboardingCard({ steps, isLive }: Args) {setAnswers((previous) => ({ ...previous, [currentStep.keyword]: next }));}- // A tap on an option is the whole answer, so the pager moves on by itself;- // the last question keeps the send button as its explicit final step.- function pickAnswer(next: string) {- setAnswer(next);- if (!isLast) setStep(current + 1);- }-function goBack() {setStep(Math.max(current - 1, 0));}@@ -85,7 +78,6 @@ export function useExpertOnboardingCard({ steps, isLive }: Args) {value,advance,goBack,- pickAnswer,setAnswer,skip,};