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/copilot): clear the composer as soon as a message is sent

Lluis Agusti763e2ef326f3
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/ChatInput.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/ChatInput.tsx
index f3adc26ed4..5aff9d38a6 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/ChatInput.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/ChatInput.tsx
@@ -104,7 +104,6 @@ export function ChatInput({
setIsDryRun,
} = useCopilotUIStore();
const showModeToggle = useGetFlag(Flag.CHAT_MODE_OPTION);
- const isNewToolUI = useGetFlag(Flag.NEW_TOOL_UI);
const showDryRunToggle = showModeToggle;
const showWorkspaceFiles = useGetFlag(Flag.CHAT_WORKSPACE_FILES);
const [attachments, setAttachments] = useState<Attachment[]>([]);
@@ -187,23 +186,19 @@ export function ChatInput({
} = useChatInput({
onSend: async (message: string) => {
const { localFiles, workspaceFiles } = partitionAttachments(attachments);
- // New tool UI clears eagerly for the same reason the text clears
- // eagerly (see useChatInput.handleSend); a failed send restores the
- // chips unless the user already attached new ones. The old UI keeps
- // its clear-after-successful-send behaviour.
+ // Chips clear eagerly for the same reason the text does (see
+ // useChatInput.handleSend); a failed send restores them unless the
+ // user already attached new ones in the meantime.
const sent = attachments;
- if (isNewToolUI) setAttachments([]);
+ setAttachments([]);
try {
await onSend(
message,
localFiles.length > 0 ? localFiles : undefined,
workspaceFiles.length > 0 ? workspaceFiles : undefined,
);
- if (!isNewToolUI) setAttachments([]);
} catch (error) {
- if (isNewToolUI) {
- setAttachments((prev) => (prev.length > 0 ? prev : sent));
- }
+ setAttachments((prev) => (prev.length > 0 ? prev : sent));
throw error;
}
},
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/__tests__/ChatInput.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/__tests__/ChatInput.test.tsx
index 2a95a93598..3ab7ae5b18 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/__tests__/ChatInput.test.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/__tests__/ChatInput.test.tsx
@@ -87,11 +87,9 @@ vi.mock("@/app/(platform)/copilot/store", () => ({
}));
let mockFlagValue = false;
-let mockNewToolUIFlag = false;
vi.mock("@/services/feature-flags/use-get-flag", () => ({
- Flag: { CHAT_MODE_OPTION: "CHAT_MODE_OPTION", NEW_TOOL_UI: "NEW_TOOL_UI" },
- useGetFlag: (flag: string) =>
- flag === "NEW_TOOL_UI" ? mockNewToolUIFlag : mockFlagValue,
+ Flag: { CHAT_MODE_OPTION: "CHAT_MODE_OPTION" },
+ useGetFlag: () => mockFlagValue,
}));
vi.mock("@/components/molecules/Toast/use-toast", () => ({
@@ -257,7 +255,6 @@ afterEach(() => {
mockCopilotLlmModel = "standard";
mockCopilotLlmAuthProvider = "platform";
mockFlagValue = false;
- mockNewToolUIFlag = false;
mockInitialPrompt = null;
});
@@ -690,6 +687,66 @@ describe("ChatInput submit behavior", () => {
});
});
+ it("clears the textarea on submit, without waiting for the stream to end", async () => {
+ // onSend resolves only when the whole assistant turn finishes, so a
+ // clear-after-await left the sent message sitting in the composer for
+ // the entire stream.
+ let finishStream: (() => void) | undefined;
+ const onSend = vi.fn(
+ () =>
+ new Promise<void>((resolve) => {
+ finishStream = resolve;
+ }),
+ );
+ render(<ChatInput onSend={onSend} />);
+ const textarea = screen.getByTestId("textarea") as HTMLTextAreaElement;
+ fireEvent.change(textarea, { target: { value: "hello" } });
+ fireEvent.submit(textarea.closest("form")!);
+
+ await waitFor(() => {
+ expect(textarea.value).toBe("");
+ });
+ expect(onSend).toHaveBeenCalledWith("hello", undefined, undefined);
+ await act(async () => {
+ finishStream?.();
+ });
+ });
+
+ it("clears attachment chips on submit, without waiting for the stream to end", async () => {
+ let finishStream: (() => void) | undefined;
+ const onSend = vi.fn(
+ () =>
+ new Promise<void>((resolve) => {
+ finishStream = resolve;
+ }),
+ );
+ render(<ChatInput onSend={onSend} />);
+ const textarea = screen.getByTestId("textarea") as HTMLTextAreaElement;
+ fireEvent.paste(textarea, {
+ clipboardData: {
+ files: [new File(["png"], "shot.png", { type: "image/png" })],
+ },
+ });
+ // An attachment alone makes the message sendable, so the submit button
+ // going back to disabled is proof the chips were dropped.
+ await waitFor(() => {
+ expect((screen.getByTestId("submit") as HTMLButtonElement).disabled).toBe(
+ false,
+ );
+ });
+ fireEvent.submit(textarea.closest("form")!);
+
+ await waitFor(() => {
+ expect((screen.getByTestId("submit") as HTMLButtonElement).disabled).toBe(
+ true,
+ );
+ });
+ expect(onSend).toHaveBeenCalledWith("", [expect.any(File)], undefined);
+ await act(async () => {
+ finishStream?.();
+ });
+ });
+
it("does not call onSend when disabled", () => {
const onSend = vi.fn().mockResolvedValue(undefined);
render(<ChatInput onSend={onSend} disabled />);
@@ -721,42 +778,32 @@ describe("ChatInput submit behavior", () => {
});
it("allows sending again after a failed send", async () => {
- const swallowWindow = (e: PromiseRejectionEvent) => e.preventDefault();
- const swallowProcess = () => undefined;
- window.addEventListener("unhandledrejection", swallowWindow);
- process.on("unhandledRejection", swallowProcess);
- try {
- let failNext = true;
- const onSend = vi.fn(async () => {
- if (failNext) {
- failNext = false;
- throw new Error("fail");
- }
- });
- render(<ChatInput onSend={onSend} />);
- const textarea = screen.getByTestId("textarea") as HTMLTextAreaElement;
- fireEvent.change(textarea, { target: { value: "hello" } });
- const form = textarea.closest("form")!;
- fireEvent.submit(form);
- await waitFor(() => {
- expect(onSend).toHaveBeenCalledTimes(1);
- });
- fireEvent.change(textarea, { target: { value: "retry" } });
- fireEvent.submit(form);
- await waitFor(() => {
- expect(onSend).toHaveBeenCalledTimes(2);
- });
- expect(onSend).toHaveBeenLastCalledWith("retry", undefined, undefined);
- } finally {
- window.removeEventListener("unhandledrejection", swallowWindow);
- process.off("unhandledRejection", swallowProcess);
- }
+ let failNext = true;
+ const onSend = vi.fn(async () => {
+ if (failNext) {
+ failNext = false;
+ throw new Error("fail");
+ }
+ });
+ render(<ChatInput onSend={onSend} />);
+ const textarea = screen.getByTestId("textarea") as HTMLTextAreaElement;
+ fireEvent.change(textarea, { target: { value: "hello" } });
+ const form = textarea.closest("form")!;
+ fireEvent.submit(form);
+ await waitFor(() => {
+ expect(toast).toHaveBeenCalled();
+ });
+ fireEvent.change(textarea, { target: { value: "retry" } });
+ fireEvent.submit(form);
+ await waitFor(() => {
+ expect(onSend).toHaveBeenCalledTimes(2);
+ });
+ expect(onSend).toHaveBeenLastCalledWith("retry", undefined, undefined);
});
});
-describe("ChatInput send failure with the new tool UI", () => {
+describe("ChatInput send failure", () => {
it("toasts and puts the failed message back in the composer", async () => {
- mockNewToolUIFlag = true;
const onSend = vi.fn().mockRejectedValue(new Error("Backend exploded"));
render(<ChatInput onSend={onSend} />);
const textarea = screen.getByTestId("textarea") as HTMLTextAreaElement;
@@ -776,7 +823,6 @@ describe("ChatInput send failure with the new tool UI", () => {
});
it("keeps a draft typed during the stream alongside the failed message", async () => {
- mockNewToolUIFlag = true;
let rejectSend: ((error: Error) => void) | undefined;
const onSend = vi.fn(
() =>
@@ -802,7 +848,6 @@ describe("ChatInput send failure with the new tool UI", () => {
});
it("does not toast when the send succeeds", async () => {
- mockNewToolUIFlag = true;
const onSend = vi.fn().mockResolvedValue(undefined);
render(<ChatInput onSend={onSend} />);
const textarea = screen.getByTestId("textarea") as HTMLTextAreaElement;
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/useChatInput.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/useChatInput.ts
index a79bfb75e6..30d6241188 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/useChatInput.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/useChatInput.ts
@@ -1,6 +1,5 @@
import { useCopilotUIStore } from "@/app/(platform)/copilot/store";
import { toast } from "@/components/molecules/Toast/use-toast";
-import { Flag, useGetFlag } from "@/services/feature-flags/use-get-flag";
import { ChangeEvent, FormEvent, useEffect, useRef, useState } from "react";
interface Args {
@@ -24,9 +23,6 @@ export function useChatInput({
const isSubmittingRef = useRef(false);
const { initialPrompt, setInitialPrompt, notifyMessageSent } =
useCopilotUIStore();
- // Eager clear-and-restore ships with the new tool UI; the old UI keeps
- // its clear-after-send behaviour (and lets a failed send propagate).
- const isNewToolUI = useGetFlag(Flag.NEW_TOOL_UI);
useEffect(
function consumeInitialPrompt() {
@@ -73,13 +69,11 @@ export function useChatInput({
// Clear eagerly: onSend can resolve only when the whole stream finishes,
// which would leave the sent text sitting in the composer for the entire
// turn. On failure the draft is restored (see restoreFailedDraft).
- if (isNewToolUI) setValue("");
+ setValue("");
try {
await onSend(trimmedMessage);
- if (!isNewToolUI) setValue("");
notifyMessageSent();
} catch (error) {
- if (!isNewToolUI) throw error;
setValue((current) => restoreFailedDraft(current, message));
toast({
title: "Couldn't send message",