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.

fix(frontend): keep answered questions readable in the chat history (#15873)

Ubbef110b644ff12
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/AnsweredQuestions/AnsweredQuestions.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/AnsweredQuestions/AnsweredQuestions.tsx
new file mode 100644
index 0000000000..8a9891f03d
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/AnsweredQuestions/AnsweredQuestions.tsx
@@ -0,0 +1,38 @@
+import { MessageQuestionIcon } from "@hugeicons/core-free-icons";
+import { Icon } from "@/components/atoms/Icon/Icon";
+
+export interface AnsweredQuestion {
+ key: string;
+ question: string;
+ answer?: string;
+}
+
+interface Props {
+ items: AnsweredQuestion[];
+}
+
+/** A question card the user has moved past, kept in the thread so they can
+ * reread what they were asked and what they answered. */
+export function AnsweredQuestions({ items }: Props) {
+ return (
+ <div className="flex w-full max-w-xl flex-col gap-2.5 rounded-xl bg-white p-3 ring-1 ring-zinc-200/70">
+ {items.map((item) => (
+ <div key={item.key} className="flex items-start gap-2">
+ <Icon
+ icon={MessageQuestionIcon}
+ size={14}
+ className="mt-0.5 shrink-0 text-zinc-400"
+ />
+ <div className="min-w-0">
+ <p className="text-[13px] text-zinc-500">{item.question}</p>
+ {item.answer && (
+ <p className="whitespace-pre-wrap text-[13px] text-zinc-800">
+ {item.answer}
+ </p>
+ )}
+ </div>
+ </div>
+ ))}
+ </div>
+ );
+}
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/ChatMessagesContainer.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/ChatMessagesContainer.tsx
index 0ddde2e36c..f50dc0f0e1 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/ChatMessagesContainer.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/ChatMessagesContainer.tsx
@@ -663,10 +663,9 @@ export function ChatMessagesContainer({
// Every chat surface renders through here, so the pending-answer state
// (clarifying questions, a hire's onboarding, held calls) lives here too
- // instead of in each host. A read-only viewer answers nothing.
- if (readOnly) return thread;
+ // instead of in each host. Read-only viewers receive saved answers only.
return (
- <PendingAnswerContexts messages={allMessages}>
+ <PendingAnswerContexts messages={allMessages} readOnly={readOnly}>
{thread}
</PendingAnswerContexts>
);
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/__tests__/ChatMessagesContainer.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/__tests__/ChatMessagesContainer.test.tsx
index cdc88fd5b0..48d9852aac 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/__tests__/ChatMessagesContainer.test.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/__tests__/ChatMessagesContainer.test.tsx
@@ -1,4 +1,7 @@
-import type { ComponentProps } from "react";
+import { useContext, type ComponentProps } from "react";
+import { AnsweredQuestionsContext } from "../../QuestionDock/AnsweredQuestionsContext";
+import { PendingQuestionsContext } from "../../QuestionDock/PendingQuestionsContext";
+import { PendingOnboardingContext } from "../../ExpertOnboardingCard/PendingOnboardingContext";
import { act } from "@testing-library/react";
import type { UIDataTypes, UIMessage, UITools } from "ai";
import { render, screen, cleanup } from "@/tests/integrations/test-utils";
@@ -78,19 +81,29 @@ vi.mock("../components/AssistantMessageActions", () => ({
AssistantMessageActions: () => null,
}));
vi.mock("../components/ChainMessageParts", () => ({
- ChainMessageParts: ({
+ ChainMessageParts: function ChainMessageParts({
parts,
isCurrentlyStreaming,
}: {
parts: unknown[];
isCurrentlyStreaming?: boolean;
- }) => (
- <div
- data-testid="chain-message-parts"
- data-parts={JSON.stringify(parts)}
- data-streaming={String(!!isCurrentlyStreaming)}
- />
- ),
+ }) {
+ const answered = useContext(AnsweredQuestionsContext);
+ const pending = useContext(PendingQuestionsContext);
+ const onboarding = useContext(PendingOnboardingContext);
+ return (
+ <div
+ data-testid="chain-message-parts"
+ data-parts={JSON.stringify(parts)}
+ data-streaming={String(!!isCurrentlyStreaming)}
+ data-answered={JSON.stringify(
+ [...answered].map(([id, answers]) => [id, [...answers]]),
+ )}
+ data-pending={String(pending !== null)}
+ data-onboarding={onboarding ?? ""}
+ />
+ );
+ },
}));
vi.mock("../components/QueueBadge", () => ({
@@ -211,6 +224,68 @@ describe("ChatMessagesContainer — assistant rendering", () => {
cleanup();
});
+ it("shares saved answers with read-only viewers without activating question forms", () => {
+ const question = {
+ type: "tool-ask_question",
+ toolCallId: "ask-1",
+ state: "output-available",
+ input: {},
+ output: {
+ type: "agent_builder_clarification_needed",
+ questions: [{ question: "Which region?", keyword: "region" }],
+ },
+ } as Message["parts"][number];
+ render(
+ <ChatMessagesContainer
+ {...baseProps}
+ readOnly
+ messages={[
+ { id: "asked", role: "assistant", parts: [question] },
+ {
+ id: "answered",
+ role: "user",
+ parts: [
+ {
+ type: "text",
+ text: "**Here are my answers:**\n\n> Which region?\n\nEurope\n\nPlease proceed.",
+ },
+ ],
+ },
+ {
+ id: "pending",
+ role: "assistant",
+ parts: [
+ { ...question, toolCallId: "ask-2" } as Message["parts"][number],
+ {
+ type: "tool-expert_onboarding",
+ toolCallId: "hire",
+ state: "output-available",
+ input: {},
+ output: {
+ type: "expert_onboarding",
+ steps: [
+ {
+ question: "Which outcome?",
+ keyword: "outcome",
+ options: [],
+ },
+ ],
+ },
+ } as Message["parts"][number],
+ ],
+ },
+ ]}
+ />,
+ );
+ for (const row of screen.getAllByTestId("chain-message-parts")) {
+ expect(JSON.parse(row.getAttribute("data-answered") ?? "[]")).toEqual([
+ ["ask-1", [["region", "Europe"]]],
+ ]);
+ expect(row.getAttribute("data-pending")).toBe("false");
+ expect(row.getAttribute("data-onboarding")).toBe("");
+ }
+ });
+
it("renders assistant messages through the chain renderer", () => {
render(<ChatMessagesContainer {...baseProps} messages={messages} />);
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/PendingAnswerContexts.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/PendingAnswerContexts.tsx
index 38d3890470..ee7f1c68b7 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/PendingAnswerContexts.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/PendingAnswerContexts.tsx
@@ -7,6 +7,8 @@ import { getHeldOutcomes, type HeldOutcome } from "../heldCallRows";
import { HeldOutcomesContext } from "../HeldOutcomesContext";
import { getPendingOnboardingCallId } from "../../ExpertOnboardingCard/helpers";
import { PendingOnboardingContext } from "../../ExpertOnboardingCard/PendingOnboardingContext";
+import { AnsweredQuestionsContext } from "../../QuestionDock/AnsweredQuestionsContext";
+import { getAnsweredQuestions } from "../../QuestionDock/answeredQuestions";
import { getPendingQuestions } from "../../QuestionDock/helpers";
import { PendingQuestionsContext } from "../../QuestionDock/PendingQuestionsContext";
import { CurrentPauseContext } from "../../TurnPausedCard/CurrentPauseContext";
@@ -15,6 +17,7 @@ import { getCurrentPause } from "../../TurnPausedCard/helpers";
interface Props {
messages: UIMessage<unknown, UIDataTypes, UITools>[];
children: React.ReactNode;
+ readOnly?: boolean;
}
const NO_HELD_OUTCOMES: ReadonlyMap<string, HeldOutcome> = new Map();
@@ -22,8 +25,13 @@ const NO_HELD_OUTCOMES: ReadonlyMap<string, HeldOutcome> = new Map();
/** What the session is still waiting on the user for: the clarifying-question
* dock, the hire's onboarding card, a held call's row and a Paused card each
* decide from here whether they are live or already history, rather than
- * from local state a reload would lose. */
-export function PendingAnswerContexts({ messages, children }: Props) {
+ * from local state a reload would lose. A question card that is history
+ * reads the user's answers from here too. */
+export function PendingAnswerContexts({
+ messages,
+ children,
+ readOnly = false,
+}: Props) {
// The questions and held outcomes are rebuilt as new objects on every call,
// and `messages` changes on every streamed flush, so each one keeps the
// object it last published while its content is the same; otherwise every
@@ -33,23 +41,35 @@ export function PendingAnswerContexts({ messages, children }: Props) {
const pendingQuestions = useEqualValue(
useMemo(() => getPendingQuestions(messages), [messages]),
);
+ const answeredQuestions = useEqualValue(
+ useMemo(() => getAnsweredQuestions(messages), [messages]),
+ );
const heldOutcomes = useEqualValue(
useMemo(() => {
const outcomes = getHeldOutcomes(messages);
return outcomes.size === 0 ? NO_HELD_OUTCOMES : outcomes;
}, [messages]),
);
+ if (readOnly) {
+ return (
+ <AnsweredQuestionsContext.Provider value={answeredQuestions}>
+ {children}
+ </AnsweredQuestionsContext.Provider>
+ );
+ }
return (
<PendingQuestionsContext.Provider value={pendingQuestions}>
- <PendingOnboardingContext.Provider
- value={getPendingOnboardingCallId(messages)}
- >
- <HeldOutcomesContext.Provider value={heldOutcomes}>
- <CurrentPauseContext.Provider value={getCurrentPause(messages)}>
- {children}
- </CurrentPauseContext.Provider>
- </HeldOutcomesContext.Provider>
- </PendingOnboardingContext.Provider>
+ <AnsweredQuestionsContext.Provider value={answeredQuestions}>
+ <PendingOnboardingContext.Provider
+ value={getPendingOnboardingCallId(messages)}
+ >
+ <HeldOutcomesContext.Provider value={heldOutcomes}>
+ <CurrentPauseContext.Provider value={getCurrentPause(messages)}>
+ {children}
+ </CurrentPauseContext.Provider>
+ </HeldOutcomesContext.Provider>
+ </PendingOnboardingContext.Provider>
+ </AnsweredQuestionsContext.Provider>
</PendingQuestionsContext.Provider>
);
}
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/__tests__/PendingAnswerContexts.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/__tests__/PendingAnswerContexts.test.tsx
index da84b81e4e..32ac999e93 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/__tests__/PendingAnswerContexts.test.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/components/__tests__/PendingAnswerContexts.test.tsx
@@ -3,6 +3,7 @@ import type { UIDataTypes, UIMessage, UITools } from "ai";
import { memo, useContext } from "react";
import { describe, expect, it } from "vitest";
import { PendingOnboardingContext } from "../../../ExpertOnboardingCard/PendingOnboardingContext";
+import { AnsweredQuestionsContext } from "../../../QuestionDock/AnsweredQuestionsContext";
import { PendingQuestionsContext } from "../../../QuestionDock/PendingQuestionsContext";
import { CurrentPauseContext } from "../../../TurnPausedCard/CurrentPauseContext";
import { HeldOutcomesContext } from "../../HeldOutcomesContext";
@@ -13,6 +14,7 @@ type Part = Message["parts"][number];
interface Seen {
questions: unknown;
+ answered: ReadonlyMap<string, ReadonlyMap<string, string>>;
onboarding: unknown;
held: ReadonlyMap<string, unknown>;
pause: unknown;
@@ -39,7 +41,14 @@ const askPart = {
input: {},
output: {
type: "agent_builder_clarification_needed",
- questions: [{ question: "Which region?", keyword: "region" }],
+ questions: [
+ {
+ question: "Which region?",
+ keyword: "region",
+ options: ["Europe", "Asia"],
+ allow_multiple: true,
+ },
+ ],
},
} as unknown as Part;
@@ -56,6 +65,7 @@ function setup(initial: Message[]) {
const Consumer = memo(function Consumer() {
seen.push({
questions: useContext(PendingQuestionsContext),
+ answered: useContext(AnsweredQuestionsContext),
onboarding: useContext(PendingOnboardingContext),
held: useContext(HeldOutcomesContext),
pause: useContext(CurrentPauseContext),
@@ -160,6 +170,26 @@ describe("PendingAnswerContexts", () => {
expect(seen[1].questions).toBeNull();
});
+ it("reads the answers back from the user's reply", () => {
+ const { seen, rerender } = setup([assistant("Hi")]);
+
+ rerender([
+ assistant("Hi"),
+ {
+ id: "u1",
+ role: "user",
+ parts: [
+ {
+ type: "text",
+ text: "**Here are my answers:**\n\n> Which region?\n\n- Europe\n- Asia\n\nPlease proceed.",
+ },
+ ],
+ },
+ ]);
+
+ expect(seen[1].answered.get("ask-1")?.get("region")).toBe("Europe, Asia");
+ });
+
it("shares one empty map across different messages when nothing is held", () => {
const { seen, rerender } = setup([
{ id: "u1", role: "user", parts: [{ type: "text", text: "Hi" }] },
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.tsx
index a989828bf7..fe6cd49420 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
@@ -12,7 +12,9 @@ import type { ToolUIPart } from "ai";
import { domAnimation, LazyMotion, m } from "framer-motion";
import { useContext, useId } from "react";
import { useExpertMap } from "../../useExpertMap";
+import { AnsweredQuestions } from "../AnsweredQuestions/AnsweredQuestions";
import { MorphingTextAnimation } from "../MorphingTextAnimation/MorphingTextAnimation";
+import { AnsweredQuestionsContext } from "../QuestionDock/AnsweredQuestionsContext";
import { OnboardingChoices } from "./components/OnboardingChoices/OnboardingChoices";
import { parseExpertOnboarding, type ExpertOnboardingOutput } from "./helpers";
import { PendingOnboardingContext } from "./PendingOnboardingContext";
@@ -66,6 +68,7 @@ interface FormProps {
function OnboardingForm({ callId, onboarding, isLive }: FormProps) {
const { expertsById } = useExpertMap();
+ const answers = useContext(AnsweredQuestionsContext).get(callId);
const sectionId = useId();
const {
current,
@@ -93,14 +96,24 @@ function OnboardingForm({ callId, onboarding, isLive }: FormProps) {
const labelId = `${sectionId}-${currentStep.keyword}`;
if (isDone) {
+ // Skipped questions have no answer to show, so only answered ones stay.
+ const answered = onboarding.steps.flatMap((step) => {
+ const answer = answers?.get(step.keyword);
+ return answer
+ ? [{ key: step.keyword, question: step.question, answer }]
+ : [];
+ });
return (
- <div className="flex items-center gap-2 py-2 text-sm text-zinc-500">
- <Icon
- icon={CheckmarkCircle02Icon}
- size={16}
- className="shrink-0 text-zinc-400"
- />
- Setup questions from {name}
+ <div className="flex flex-col gap-1 py-2">
+ <div className="flex items-center gap-2 text-sm text-zinc-500">
+ <Icon
+ icon={CheckmarkCircle02Icon}
+ size={16}
+ className="shrink-0 text-zinc-400"
+ />
+ Setup questions from {name}
+ </div>
+ {answered.length > 0 && <AnsweredQuestions items={answered} />}
</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.tsx
index 18d5585c06..8f46addff8 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
@@ -8,6 +8,7 @@ import {
import type { ToolUIPart } from "ai";
import { afterEach, describe, expect, it, vi } from "vitest";
import { CopilotChatActionsProvider } from "../../CopilotChatActionsProvider/CopilotChatActionsProvider";
+import { AnsweredQuestionsContext } from "../../QuestionDock/AnsweredQuestionsContext";
import { ExpertOnboardingCard } from "../ExpertOnboardingCard";
import { PendingOnboardingContext } from "../PendingOnboardingContext";
@@ -242,6 +243,40 @@ describe("ExpertOnboardingCard", () => {
expect(screen.getByText(/Setup questions from/)).toBeDefined();
});
+ it("keeps each question and the answer given once it is history", () => {
+ render(
+ <CopilotChatActionsProvider onSend={createSendMock()}>
+ <AnsweredQuestionsContext.Provider
+ value={
+ new Map([
+ [
+ CALL_ID,
+ new Map([
+ ["outcome", "Campaign briefs"],
+ ["service", "Linear, GitHub"],
+ ]),
+ ],
+ ])
+ }
+ >
+ <PendingOnboardingContext.Provider value={null}>
+ <ExpertOnboardingCard part={onboardingPart()} />
+ </PendingOnboardingContext.Provider>
+ </AnsweredQuestionsContext.Provider>
+ </CopilotChatActionsProvider>,
+ );
+
+ expect(screen.queryByRole("radio")).toBeNull();
+ expect(
+ screen.getByText("Which outcome should I start with?"),
+ ).toBeDefined();
+ expect(screen.getByText("Campaign briefs")).toBeDefined();
+ expect(
+ screen.getByText("Which service should I be connected to?"),
+ ).toBeDefined();
+ expect(screen.getByText("Linear, GitHub")).toBeDefined();
+ });
+
it("shows a pending line while the card is still streaming", () => {
const part = {
type: "tool-expert_onboarding",
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/QuestionDock/AnsweredQuestionsContext.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/QuestionDock/AnsweredQuestionsContext.ts
new file mode 100644
index 0000000000..e4aeceb997
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/QuestionDock/AnsweredQuestionsContext.ts
@@ -0,0 +1,10 @@
+"use client";
+
+import { createContext } from "react";
+import type { AnsweredQuestions } from "./answeredQuestions";
+
+/** What the user answered to the question cards they have moved past,
+ * provided by PendingAnswerContexts so each card can show its answers. */
+export const AnsweredQuestionsContext = createContext<AnsweredQuestions>(
+ new Map(),
+);
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/QuestionDock/__tests__/answeredQuestions.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/QuestionDock/__tests__/answeredQuestions.test.ts
new file mode 100644
index 0000000000..5a85b17cdb
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/QuestionDock/__tests__/answeredQuestions.test.ts
@@ -0,0 +1,198 @@
+import type { UIDataTypes, UIMessage, UITools } from "ai";
+import { describe, expect, it } from "vitest";
+import type { ClarifyingQuestion } from "../../../tools/clarifying-questions";
+import { buildAnswersMessage } from "../helpers";
+import { getAnsweredQuestions } from "../answeredQuestions";
+import { buildOnboardingAnswersMessage } from "../../ExpertOnboardingCard/helpers";
+
+type Message = UIMessage<unknown, UIDataTypes, UITools>;
+type Part = Message["parts"][number];
+
+function ask(callId: string, questions: ClarifyingQuestion[]): Part {
+ return {
+ type: "tool-ask_question",
+ toolCallId: callId,
+ state: "output-available",
+ input: {},
+ output: { type: "agent_builder_clarification_needed", questions },
+ } as Part;
+}
+
+function assistant(parts: Part[], id = "a1"): Message {
+ return { id, role: "assistant", parts };
+}
+
+function reply(text: string, id = "u1"): Message {
+ return { id, role: "user", parts: [{ type: "text", text }] };
+}
+
+const region = { question: "Which region?", keyword: "region" };
+
+describe("getAnsweredQuestions", () => {
+ it("keeps separate answers to identical prompts within one call", () => {
+ const questions = [
+ { question: "Which channel?", keyword: "source" },
+ { question: "Which channel?", keyword: "destination" },
+ ];
+ const answers = getAnsweredQuestions([
+ assistant([ask("call-1", questions)]),
+ reply(
+ buildAnswersMessage(questions, {
+ source: "Email",
+ destination: "Drive",
+ }),
+ ),
+ ]);
+ expect(answers.get("call-1")).toEqual(
+ new Map([
+ ["source", "Email"],
+ ["destination", "Drive"],
+ ]),
+ );
+ });
+
+ it("keeps separate answers to identical prompts across calls", () => {
+ const questions = [{ question: "Which channel?", keyword: "channel" }];
+ const answers = getAnsweredQuestions([
+ assistant([ask("source", questions), ask("destination", questions)]),
+ reply(
+ buildAnswersMessage(
+ [...questions, { ...questions[0], keyword: "channel-1" }],
+ { channel: "Email", "channel-1": "Drive" },
+ ),
+ ),
+ ]);
+ expect(answers.get("source")?.get("channel")).toBe("Email");
+ expect(answers.get("destination")?.get("channel")).toBe("Drive");
+ });
+
+ it("reads onboarding answers by step keyword", () => {
+ const steps = [
+ { question: "Which channel?", keyword: "source", options: [] },
+ { question: "Which channel?", keyword: "destination", options: [] },
+ ];
+ const part = {
+ type: "tool-expert_onboarding",
+ toolCallId: "hire",
+ state: "output-available",
+ input: {},
+ output: { type: "expert_onboarding", steps },
+ } as Part;
+ const answers = getAnsweredQuestions([
+ assistant([part]),
+ reply(
+ buildOnboardingAnswersMessage(steps, {
+ source: "Email",
+ destination: "Drive",
+ }),
+ ),
+ ]);
+ expect(answers.get("hire")).toEqual(
+ new Map([
+ ["source", "Email"],
+ ["destination", "Drive"],
+ ]),
+ );
+ });
+
+ it("preserves multiline questions and typed answers", () => {
+ const questions = [
+ {
+ question: "Which region?\n\nInclude any exclusions.",
+ keyword: "region",
+ },
+ ];
+ const answer =
+ "Europe\n\n> Budget note\n\n- Exclude France\n- Include Spain";
+ const answers = getAnsweredQuestions([
+ assistant([ask("call-1", questions)]),
+ reply(buildAnswersMessage(questions, { region: answer })),
+ ]);
+ expect(answers.get("call-1")?.get("region")).toBe(answer);
+ });
+
+ it("does not mistake typed 'Please proceed' text for the footer", () => {
+ const answer = "Europe\n\nPlease proceed only after checking the budget.";
+ const answers = getAnsweredQuestions([
+ assistant([ask("call-1", [region])]),
+ reply(buildAnswersMessage([region], { region: answer })),
+ ]);
+ expect(answers.get("call-1")?.get("region")).toBe(answer);
+ });
+
+ it("shows multi-select picks on one line", () => {
+ const questions = [
+ { ...region, options: ["Europe", "Asia"], allow_multiple: true },
+ ];
+ const answers = getAnsweredQuestions([
+ assistant([ask("call-1", questions)]),
+ reply(
+ buildAnswersMessage(questions, {
+ region: { selected: ["Europe", "Asia"], custom: "" },
+ }),
+ ),
+ ]);
+ expect(answers.get("call-1")?.get("region")).toBe("Europe, Asia");
+ });
+
+ it("keeps typed bullet lists in single-answer questions", () => {
+ const answer = "- Europe\n- Asia";
+ const answers = getAnsweredQuestions([
+ assistant([ask("call-1", [region])]),
+ reply(buildAnswersMessage([region], { region: answer })),
+ ]);
+ expect(answers.get("call-1")?.get("region")).toBe(answer);
+ });
+
+ it("reads all answer sections in a combined chain reply", () => {
+ const format = { question: "Which format?", keyword: "format" };
+ const answers = getAnsweredQuestions([
+ assistant([ask("region", [region]), ask("format", [format])]),
+ reply(
+ [
+ buildAnswersMessage([format], { format: "CSV" }),
+ buildAnswersMessage([region], { region: "Europe" }),
+ ].join("\n\n"),
+ ),
+ ]);
+ expect(answers.get("region")?.get("region")).toBe("Europe");
+ expect(answers.get("format")?.get("format")).toBe("CSV");
+ });
+
+ it("uses the first subsequent user reply and does not fill skipped questions later", () => {
+ const answers = getAnsweredQuestions([
+ assistant([ask("skipped", [region])]),
+ reply("Skip questions"),
+ assistant([ask("answered", [region])], "a2"),
+ reply(buildAnswersMessage([region], { region: "Europe" }), "u2"),
+ ]);
+ expect(answers.has("skipped")).toBe(false);
+ expect(answers.get("answered")?.get("region")).toBe("Europe");
+ });
+
+ it("ignores unanswered calls, unrelated tools and unstructured replies", () => {
+ expect(getAnsweredQuestions([]).size).toBe(0);
+ expect(
+ getAnsweredQuestions([assistant([ask("call-1", [region])])]).size,
+ ).toBe(0);
+ expect(
+ getAnsweredQuestions([
+ assistant([ask("call-1", [region])]),
+ reply("Europe"),
+ ]).size,
+ ).toBe(0);
+ const part = {
+ type: "tool-web_search",
+ toolCallId: "search",
+ state: "output-available",
+ input: {},
+ output: {},
+ } as Part;
+ expect(
+ getAnsweredQuestions([
+ assistant([part]),
+ reply(buildAnswersMessage([region], { region: "Europe" })),
+ ]).size,
+ ).toBe(0);
+ });
+});
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/QuestionDock/answeredQuestions.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/QuestionDock/answeredQuestions.ts
new file mode 100644
index 0000000000..17cbb89079
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/QuestionDock/answeredQuestions.ts
@@ -0,0 +1,123 @@
+import type { ToolUIPart, UIDataTypes, UIMessage, UITools } from "ai";
+import {
+ type ClarifyingQuestion,
+ extractClarifyingQuestions,
+} from "../../tools/clarifying-questions";
+import {
+ EXPERT_ONBOARDING_PART_TYPE,
+ parseExpertOnboarding,
+} from "../ExpertOnboardingCard/helpers";
+import { isQuestionTool } from "./helpers";
+
+type Message = UIMessage<unknown, UIDataTypes, UITools>;
+
+export type AnsweredQuestions = ReadonlyMap<
+ string,
+ ReadonlyMap<string, string>
+>;
+
+interface ParsedAnswer {
+ question: string;
+ answer: string;
+}
+
+function parseAnswersMessage(
+ text: string,
+ questions: string[],
+): ParsedAnswer[] {
+ const answers: ParsedAnswer[] = [];
+ const prompts = questions.toSorted((a, b) => b.length - a.length);
+ for (const section of text.split("**Here are my answers:**\n\n").slice(1)) {
+ const footer = section.lastIndexOf("\n\nPlease proceed");
+ if (footer < 0) continue;
+ const body = section.slice(0, footer);
+ const markers = [...body.matchAll(/(?:^|\n\n)> /g)].flatMap((match) => {
+ const start = match.index + match[0].length;
+ const question = prompts.find((prompt) =>
+ body.startsWith(`${prompt}\n\n`, start),
+ );
+ return question
+ ? [
+ {
+ question,
+ start: match.index,
+ answerStart: start + question.length + 2,
+ },
+ ]
+ : [];
+ });
+ for (const [index, marker] of markers.entries()) {
+ answers.push({
+ question: marker.question,
+ answer: body
+ .slice(marker.answerStart, markers[index + 1]?.start)
+ .trim(),
+ });
+ }
+ }
+ return answers;
+}
+
+function askedQuestions(part: ToolUIPart): ClarifyingQuestion[] {
+ if (part.state !== "output-available") return [];
+ if (part.type === EXPERT_ONBOARDING_PART_TYPE) {
+ return parseExpertOnboarding(part)?.steps ?? [];
+ }
+ return isQuestionTool(part.type.slice("tool-".length), part.output)
+ ? extractClarifyingQuestions(part)
+ : [];
+}
+
+function displayAnswer(question: ClarifyingQuestion, answer: string): string {
+ const lines = answer.split("\n");
+ return question.allow_multiple && lines.every((line) => line.startsWith("- "))
+ ? lines.map((line) => line.slice(2)).join(", ")
+ : answer;
+}
+
+function answersForMessage(message: Message, reply: Message) {
+ const text = reply.parts
+ .flatMap((part) => (part.type === "text" ? [part.text] : []))
+ .join("\n");
+ const questionsByCall = message.parts.flatMap((part) => {
+ if (!part.type.startsWith("tool-")) return [];
+ const tool = part as ToolUIPart;
+ return [{ callId: tool.toolCallId, questions: askedQuestions(tool) }];
+ });
+ const remaining = parseAnswersMessage(
+ text,
+ questionsByCall.flatMap((call) => call.questions.map((q) => q.question)),
+ );
+ const calls = new Map<string, ReadonlyMap<string, string>>();
+ for (const { callId, questions } of questionsByCall) {
+ const answers = new Map<string, string>();
+ for (const question of questions) {
+ const index = remaining.findIndex(
+ (entry) => entry.question === question.question,
+ );
+ if (index < 0) continue;
+ const [entry] = remaining.splice(index, 1);
+ if (entry.answer) {
+ answers.set(question.keyword, displayAnswer(question, entry.answer));
+ }
+ }
+ if (answers.size > 0) calls.set(callId, answers);
+ }
+ return calls;
+}
+
+export function getAnsweredQuestions(messages: Message[]): AnsweredQuestions {
+ const answered = new Map<string, ReadonlyMap<string, string>>();
+ let nextReply: Message | null = null;
+ for (let index = messages.length - 1; index >= 0; index--) {
+ const message = messages[index];
+ if (message.role === "user") {
+ nextReply = message;
+ } else if (message.role === "assistant" && nextReply) {
+ for (const [callId, answers] of answersForMessage(message, nextReply)) {
+ answered.set(callId, answers);
+ }
+ }
+ }
+ return answered;
+}
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 f674ca946a..b6c6fb40b9 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
@@ -35,6 +35,17 @@ function outputType(output: unknown): string | null {
return typeof type === "string" ? type : null;
}
+/** Whether a tool call asked the user clarifying questions. `toolName` is the
+ * name without the part's `tool-` prefix. */
+export function isQuestionTool(
+ toolName: string | undefined,
+ output: unknown,
+): boolean {
+ return (
+ toolName === "ask_question" || outputType(output) === QUESTION_RESPONSE_TYPE
+ );
+}
+
// Questions are pending only while the asking assistant message is the last
// message — any user reply (answers, skip, or a fresh request) supersedes it.
export function getPendingQuestions(
@@ -49,10 +60,7 @@ export function getPendingQuestions(
if (!part.type.startsWith("tool-")) continue;
if (!("state" in part) || part.state !== "output-available") continue;
const tool = part as ToolUIPart;
- const isQuestionPart =
- part.type === "tool-ask_question" ||
- outputType(tool.output) === QUESTION_RESPONSE_TYPE;
- if (!isQuestionPart) continue;
+ if (!isQuestionTool(part.type.slice("tool-".length), tool.output)) continue;
const extracted = extractClarifyingQuestions(tool);
if (extracted.length === 0) continue;
questions.push(...extracted);
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ToolChain/ToolChain.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ToolChain/ToolChain.tsx
index 0b2e889899..c1ef52900d 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ToolChain/ToolChain.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ToolChain/ToolChain.tsx
@@ -25,7 +25,9 @@ import { Button } from "@/components/atoms/Button/Button";
import { Icon } from "@/components/atoms/Icon/Icon";
import { toastSendFailure } from "../../helpers/sendFailureToast";
import { useCopilotChatActions } from "../CopilotChatActionsProvider/useCopilotChatActions";
+import { AnsweredQuestions } from "../AnsweredQuestions/AnsweredQuestions";
import { ChainActionCard } from "../ChainActionCard/ChainActionCard";
+import { AnsweredQuestionsContext } from "../QuestionDock/AnsweredQuestionsContext";
import { PendingQuestionsContext } from "../QuestionDock/PendingQuestionsContext";
import type { MessagePart } from "../ChatMessagesContainer/helpers";
import { ACCORDION_PANEL, accordionState, PANEL_REVEAL } from "./accordion";
@@ -41,6 +43,7 @@ import { applyHeldOutcome } from "./heldRow";
import { useHeldAnswersStore } from "../ApprovalQueue/heldAnswersStore";
import {
type ChainRow,
+ getAskedQuestions,
getChainHeading,
isLiftedSetupRow,
isToolCallPending,
@@ -67,6 +70,7 @@ export function ToolChain({ parts, isStreaming, readOnly = false }: Props) {
const reducedMotion = useReducedMotion();
const pendingQuestions = useContext(PendingQuestionsContext);
+ const answeredQuestions = useContext(AnsweredQuestionsContext);
const heldOutcomes = useContext(HeldOutcomesContext);
const heldAnswers = useHeldAnswersStore((state) => state.answers);
const { onSend } = useCopilotChatActions();
@@ -186,7 +190,25 @@ export function ToolChain({ parts, isStreaming, readOnly = false }: Props) {
);
if (rows.length === 0) return null;
- const shownRows = rows.filter((row) => !row.lifted);
+ const askedRecords = rows.flatMap((row) => {
+ const questions = getAskedQuestions(row);
+ if (questions.length === 0) return [];
+ const answers = answeredQuestions.get(row.key);
+ return [
+ {
+ key: row.key,
+ items: questions.map((q) => ({
+ key: q.keyword,
+ question: q.question,
+ answer: answers?.get(q.keyword),
+ })),
+ },
+ ];
+ });
+ const shownRows = rows.filter(
+ (row) =>
+ !row.lifted && !askedRecords.some((record) => record.key === row.key),
+ );
const liftedRows = rows.filter((row) => row.lifted);
const expanded = manualExpanded === true;
@@ -389,6 +411,16 @@ export function ToolChain({ parts, isStreaming, readOnly = false }: Props) {
</>
)}
+ {/* Questions the user has moved past stay where their card was,
+ with the answers given, instead of folding away with the chain. */}
+ {askedRecords.length > 0 && (
+ <div className="mt-1 flex flex-col gap-2">
+ {askedRecords.map((record) => (
+ <AnsweredQuestions key={record.key} items={record.items} />
+ ))}
+ </div>
+ )}
+
{/* Lifted rows render off-screen: their cards must stay mounted so
the ChainActionCard below keeps its registrations. A read-only
transcript has no card to feed, so they stay unmounted. */}
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ToolChain/__tests__/ToolChain.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ToolChain/__tests__/ToolChain.test.tsx
index 297d720a42..7ac0d4a687 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ToolChain/__tests__/ToolChain.test.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ToolChain/__tests__/ToolChain.test.tsx
@@ -9,6 +9,7 @@ import { toast } from "@/components/molecules/Toast/use-toast";
import { useCopilotUIStore } from "@/app/(platform)/copilot/store";
import { CopilotChatActionsProvider } from "../../CopilotChatActionsProvider/CopilotChatActionsProvider";
import type { MessagePart } from "../../ChatMessagesContainer/helpers";
+import { AnsweredQuestionsContext } from "../../QuestionDock/AnsweredQuestionsContext";
import type { PendingQuestions } from "../../QuestionDock/helpers";
import { PendingQuestionsContext } from "../../QuestionDock/PendingQuestionsContext";
import { ToolChain } from "../ToolChain";
@@ -441,6 +442,45 @@ describe("ToolChain", () => {
await expectTypingKept(field, " 日本語");
});
+ it("keeps an answered question and its answer on screen once the chain settles", () => {
+ render(
+ <AnsweredQuestionsContext.Provider
+ value={
+ new Map([["call-ask_question", new Map([["region", "Europe"]])]])
+ }
+ >
+ <ToolChain
+ parts={[
+ toolPart("web_search", "output-available", {
+ input: { query: "regions" },
+ output: { results: [] },
+ }),
+ toolPart("ask_question", "output-available", {
+ output: {
+ type: "agent_builder_clarification_needed",
+ questions: [
+ {
+ question: "Which region?",
+ keyword: "region",
+ options: ["Europe", "Asia"],
+ },
+ ],
+ },
+ }),
+ ]}
+ isStreaming={false}
+ />
+ </AnsweredQuestionsContext.Provider>,
+ );
+
+ // The chain is collapsed, yet the question and the answer stay visible.
+ expect(
+ getChainHeader(/searched the web/i).getAttribute("aria-expanded"),
+ ).toBe("false");
+ expect(screen.getByText("Which region?")).toBeDefined();
+ expect(screen.getByText("Europe")).toBeDefined();
+ });
+
it("reports a rejected send without bringing the card back", async () => {
const user = userEvent.setup();
onSend.mockRejectedValueOnce(new Error("boom"));
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ToolChain/helpers.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ToolChain/helpers.ts
index f3ae16a298..790426b4c4 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ToolChain/helpers.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ToolChain/helpers.ts
@@ -2,6 +2,11 @@ import type { ToolUIPart } from "ai";
import { getBlockDisplayName } from "../../helpers/toolDisplay";
import type { MessagePart } from "../ChatMessagesContainer/helpers";
import { EXPERT_ONBOARDING_PART_TYPE } from "../ExpertOnboardingCard/helpers";
+import { isQuestionTool } from "../QuestionDock/helpers";
+import {
+ type ClarifyingQuestion,
+ extractClarifyingQuestions,
+} from "../../tools/clarifying-questions";
import {
extractToolName,
getAnimationText,
@@ -149,6 +154,15 @@ export function isLiftedSetupRow(row: ChainRow): boolean {
return data.type === "setup_requirements" && !!asObject(data.setup_info);
}
+/** The questions a settled row asked once the user has moved past them. They
+ * render under the chain, where their card was, so collapsing the chain
+ * cannot hide what the user was asked. */
+export function getAskedQuestions(row: ChainRow): ClarifyingQuestion[] {
+ if (row.state !== "done" || row.lifted) return [];
+ if (!isQuestionTool(row.tool, row.output)) return [];
+ return extractClarifyingQuestions(row);
+}
+
function getProviderIconSrc(tool: ToolUIPart): string | undefined {
for (const source of [tool.input, tool.output]) {
if (source && typeof source === "object" && "provider" in source) {