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): let Enter queue a follow-up while Otto answers, and hold a refused follow-up until the local stream settles (#15105)

Abhimanyu Yadav9cbdc5d69c86
diff --git a/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/useBuilderChatPanel.ts b/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/useBuilderChatPanel.ts
index 10e8b7813f..0c5dabfd01 100644
--- a/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/useBuilderChatPanel.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/useBuilderChatPanel.ts
@@ -479,11 +479,8 @@ export function useBuilderChatPanel({
await queueFollowUpMessage(sessionId, trimmed);
} catch (err) {
Sentry.captureException(err);
- toast({
- variant: "destructive",
- title: "Could not queue message",
- description: "Please wait for the current response to finish.",
- });
+ // The composer restores the draft and shows the one toast for it.
+ throw err;
}
return;
}
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/followup-after-local-stream.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/followup-after-local-stream.test.tsx
new file mode 100644
index 0000000000..df6f779852
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/followup-after-local-stream.test.tsx
@@ -0,0 +1,349 @@
+import { act, renderHook, waitFor } from "@/tests/integrations/test-utils";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+// A follow-up sent while this tab is still drawing the previous answer, but
+// after the backend's turn has ended: `/messages/pending` answers 409, and
+// before this guard the fallback started a second `useChat` request on top
+// of the live stream, which froze the answer mid-sentence (SECRT-2772).
+// The session-id mock is static on purpose: the chat host is keyed by
+// session id, so a chat switch unmounts this hook rather than re-rendering
+// it with a new id.
+
+const streamState = vi.hoisted(() => ({
+ status: "ready" as "ready" | "submitted" | "streaming" | "error",
+ isFinishProbing: false,
+ isReconnecting: false,
+ isUserStopping: false,
+}));
+const sessionState = vi.hoisted(() => ({ sessionId: "session-1" }));
+
+const sendNewMessage = vi.hoisted(() => vi.fn());
+const queueFollowUpMessage = vi.hoisted(() => vi.fn());
+const queueMessage = vi.hoisted(() => vi.fn());
+const toast = vi.hoisted(() => vi.fn());
+
+vi.mock("@/services/feature-flags/use-get-flag", () => ({
+ Flag: {
+ CHAT_MODE_OPTION: "chat-mode-option",
+ HIRE_EXPERTS: "hire-experts",
+ ONBOARDING_BRAIN_DUMP: "onboarding-brain-dump",
+ },
+ useGetFlag: () => false,
+}));
+
+vi.mock("nuqs", () => ({
+ parseAsString: {},
+ useQueryState: () => [null, vi.fn()],
+}));
+
+vi.mock("@/lib/auth/hooks/useAuth", () => ({
+ useAuth: () => ({
+ user: { id: "user-1" },
+ isUserLoading: false,
+ isLoggedIn: true,
+ }),
+}));
+
+vi.mock("@/app/api/__generated__/endpoints/brain-dump/brain-dump", () => ({
+ useCompleteBrainDumpGreeting: () => ({ mutate: vi.fn() }),
+}));
+
+vi.mock("@/components/molecules/Toast/use-toast", () => ({
+ toast,
+ useToast: () => ({ toast, dismiss: vi.fn() }),
+}));
+
+vi.mock("../useExpertMap", async (importOriginal) => {
+ const actual = await importOriginal<typeof import("../useExpertMap")>();
+ return {
+ ...actual,
+ useExpertMap: () => ({
+ expertsById: new Map(),
+ isLoadingExperts: false,
+ hasExpertsSettled: true,
+ hasExpertsErrored: false,
+ }),
+ };
+});
+
+vi.mock("../useChatSession", () => ({
+ useChatSession: () => ({
+ sessionId: sessionState.sessionId,
+ setSessionId: vi.fn(),
+ sessionLlmAuthProvider: "platform",
+ sessionExpertId: null,
+ isAdoptingExpertSession: false,
+ hydratedMessages: [],
+ rawSessionMessages: [],
+ historicalTurnStats: new Map(),
+ hasActiveStream: false,
+ activeStreamStartedAt: null,
+ hasMoreMessages: false,
+ oldestSequence: null,
+ isLoadingSession: false,
+ isSessionError: false,
+ createSession: vi.fn(),
+ isCreatingSession: false,
+ refetchSession: vi.fn(),
+ sessionDryRun: false,
+ sessionChatStatus: "idle",
+ }),
+}));
+
+vi.mock("../useCopilotStream", () => ({
+ useCopilotStream: () => ({
+ messages: [],
+ setMessages: vi.fn(),
+ sendMessage: vi.fn(),
+ stop: vi.fn(),
+ status: streamState.status,
+ error: undefined,
+ isReconnecting: streamState.isReconnecting,
+ isFinishProbing: streamState.isFinishProbing,
+ isRestoringActiveSession: false,
+ isSyncing: false,
+ isUserStoppingRef: { current: false },
+ isUserStopping: streamState.isUserStopping,
+ rateLimitMessage: null,
+ dismissRateLimit: vi.fn(),
+ }),
+}));
+
+vi.mock("../useSendMessage", async (importOriginal) => {
+ const actual = await importOriginal<typeof import("../useSendMessage")>();
+ return {
+ ...actual,
+ useSendMessage: () => ({
+ onSend: sendNewMessage,
+ isUploadingFiles: false,
+ setPendingFileParts: vi.fn(),
+ }),
+ };
+});
+
+vi.mock("../useLoadMoreMessages", () => ({
+ useLoadMoreMessages: () => ({
+ pagedMessages: [],
+ pagedTurnStats: new Map(),
+ hasMore: false,
+ isLoadingMore: false,
+ loadMore: vi.fn(),
+ }),
+}));
+
+vi.mock("../useCopilotPendingChips", () => ({
+ useCopilotPendingChips: () => ({ queuedMessages: [], queueMessage }),
+}));
+
+vi.mock("../useCopilotNotifications", () => ({
+ useCopilotNotifications: () => undefined,
+}));
+vi.mock("../useSessionTitlePoll", () => ({
+ useSessionTitlePoll: () => undefined,
+}));
+vi.mock("../useWorkflowImportAutoSubmit", () => ({
+ useWorkflowImportAutoSubmit: () => undefined,
+}));
+vi.mock("../useExpertKickoff", () => ({
+ useExpertKickoff: () => ({ isKickoffStarting: false }),
+}));
+vi.mock("../helpers/queueFollowUpMessage", () => ({ queueFollowUpMessage }));
+
+import { useCopilotUIStore } from "../store";
+import { useCopilotPage } from "../useCopilotPage";
+
+function noActiveTurn() {
+ const error = new Error("Session has no active turn to queue against.");
+ error.name = "QueueFollowUpNotActiveError";
+ return error;
+}
+
+async function flush() {
+ await act(async () => {
+ await Promise.resolve();
+ });
+}
+
+beforeEach(() => {
+ streamState.status = "streaming";
+ streamState.isFinishProbing = false;
+ streamState.isReconnecting = false;
+ streamState.isUserStopping = false;
+ sessionState.sessionId = "session-1";
+ sendNewMessage.mockResolvedValue(undefined);
+ queueFollowUpMessage.mockRejectedValue(noActiveTurn());
+ useCopilotUIStore.getState().setInitialPrompt(null);
+});
+
+afterEach(() => {
+ vi.clearAllMocks();
+ window.sessionStorage.clear();
+});
+
+describe("useCopilotPage — follow-up refused by the backend mid-stream", () => {
+ it("holds the follow-up until this tab's stream has settled", async () => {
+ const { result, rerender } = renderHook(() => useCopilotPage());
+
+ let settled = false;
+ const send = result.current.onSend("After the essay, say PINEAPPLE");
+ void send.then(() => (settled = true));
+ await flush();
+
+ expect(queueFollowUpMessage).toHaveBeenCalledWith(
+ "session-1",
+ "After the essay, say PINEAPPLE",
+ );
+ expect(sendNewMessage).not.toHaveBeenCalled();
+ expect(settled).toBe(false);
+
+ streamState.status = "ready";
+ rerender();
+ await send;
+
+ expect(sendNewMessage).toHaveBeenCalledTimes(1);
+ expect(sendNewMessage).toHaveBeenCalledWith(
+ "After the essay, say PINEAPPLE",
+ undefined,
+ undefined,
+ undefined,
+ );
+ expect(queueMessage).not.toHaveBeenCalled();
+ });
+
+ it("waits out the post-finish probe and a reconnect as well", async () => {
+ const { result, rerender } = renderHook(() => useCopilotPage());
+ const send = result.current.onSend("and then?");
+ await flush();
+
+ streamState.status = "ready";
+ streamState.isFinishProbing = true;
+ rerender();
+ await flush();
+ expect(sendNewMessage).not.toHaveBeenCalled();
+
+ streamState.isFinishProbing = false;
+ streamState.isReconnecting = true;
+ rerender();
+ await flush();
+ expect(sendNewMessage).not.toHaveBeenCalled();
+
+ streamState.isReconnecting = false;
+ rerender();
+ await send;
+ expect(sendNewMessage).toHaveBeenCalledTimes(1);
+ });
+
+ it("resolves once the follow-up is dispatched, not when its answer ends", async () => {
+ sendNewMessage.mockReturnValue(new Promise(() => {}));
+ const { result, rerender } = renderHook(() => useCopilotPage());
+ const send = result.current.onSend("and then?");
+ await flush();
+
+ streamState.status = "ready";
+ rerender();
+ await expect(send).resolves.toBeUndefined();
+ expect(sendNewMessage).toHaveBeenCalledTimes(1);
+ });
+
+ it("queues a second held follow-up behind the first instead of starting two turns", async () => {
+ const { result, rerender } = renderHook(() => useCopilotPage());
+ const first = result.current.onSend("first");
+ const second = result.current.onSend("second");
+ await flush();
+ expect(queueFollowUpMessage).toHaveBeenCalledTimes(2);
+
+ // Once the first has dispatched, the backend has a turn to queue against.
+ queueFollowUpMessage.mockResolvedValue({
+ buffer_length: 1,
+ max_buffer_length: 10,
+ turn_in_flight: true,
+ });
+ streamState.status = "ready";
+ rerender();
+ await Promise.all([first, second]);
+
+ expect(sendNewMessage).toHaveBeenCalledTimes(1);
+ expect(sendNewMessage).toHaveBeenCalledWith(
+ "first",
+ undefined,
+ undefined,
+ undefined,
+ );
+ expect(queueMessage).toHaveBeenCalledWith("second");
+ // Both held copies are released: one went out, one is now the
+ // backend's chip.
+ await waitFor(() => expect(result.current.queuedMessages).toEqual([]));
+ expect(window.sessionStorage.length).toBe(0);
+ });
+
+ it("still queues normally when the backend accepts the follow-up", async () => {
+ queueFollowUpMessage.mockResolvedValue({
+ buffer_length: 1,
+ max_buffer_length: 10,
+ turn_in_flight: true,
+ });
+ const { result } = renderHook(() => useCopilotPage());
+ await result.current.onSend("and then?");
+
+ expect(queueMessage).toHaveBeenCalledWith("and then?");
+ expect(sendNewMessage).not.toHaveBeenCalled();
+ });
+
+ it("shows the held follow-up as a queued chip until it goes out", async () => {
+ const { result, rerender } = renderHook(() => useCopilotPage());
+ const send = result.current.onSend("and then?");
+ await flush();
+ expect(result.current.queuedMessages).toEqual(["and then?"]);
+ expect(
+ window.sessionStorage.getItem("copilot-held-follow-ups:session-1"),
+ ).toBe(JSON.stringify(["and then?"]));
+
+ streamState.status = "ready";
+ rerender();
+ await send;
+ await waitFor(() => expect(result.current.queuedMessages).toEqual([]));
+ expect(window.sessionStorage.length).toBe(0);
+ });
+
+ it("gives up the send, but not the copy, when the chat host unmounts mid-hold", async () => {
+ const { result, unmount } = renderHook(() => useCopilotPage());
+ const send = result.current.onSend("and then?");
+ await flush();
+
+ unmount();
+ await expect(send).resolves.toBeUndefined();
+ expect(sendNewMessage).not.toHaveBeenCalled();
+ expect(
+ window.sessionStorage.getItem("copilot-held-follow-ups:session-1"),
+ ).toBe(JSON.stringify(["and then?"]));
+ });
+
+ it("lets a queue failure reach the composer once, without a toast of its own", async () => {
+ queueFollowUpMessage.mockRejectedValue(new Error("Expected 200; got 500"));
+ const { result } = renderHook(() => useCopilotPage());
+
+ await expect(result.current.onSend("and then?")).rejects.toThrow(
+ "Expected 200; got 500",
+ );
+ expect(toast).not.toHaveBeenCalled();
+ expect(sendNewMessage).not.toHaveBeenCalled();
+ expect(result.current.queuedMessages).toEqual([]);
+ });
+
+ it("puts a follow-up left behind by a reload back in the composer", async () => {
+ window.sessionStorage.setItem(
+ "copilot-held-follow-ups:session-1",
+ JSON.stringify(["and then?"]),
+ );
+ renderHook(() => useCopilotPage());
+
+ await waitFor(() =>
+ expect(useCopilotUIStore.getState().initialPrompt).toBe("and then?"),
+ );
+ expect(window.sessionStorage.length).toBe(0);
+ expect(toast).toHaveBeenCalledWith(
+ expect.objectContaining({ title: "Follow-up not sent" }),
+ );
+ expect(sendNewMessage).not.toHaveBeenCalled();
+ });
+});
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/useCopilotStream.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/useCopilotStream.test.tsx
index a508673938..4f78cbead1 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/useCopilotStream.test.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/useCopilotStream.test.tsx
@@ -124,6 +124,30 @@ describe("useCopilotStream — isFinishProbing lifecycle", () => {
});
});
+ it("still resets the flag under Strict Mode's mount, cleanup, mount", async () => {
+ // The mount effect's cleanup marks the hook unmounted; the simulated
+ // unmount left it that way for the real mount, so the probe's finally
+ // skipped the reset and the flag stayed true after the first turn.
+ server.use(
+ copilotStreamHandler({
+ baseUrl: TEST_BACKEND_BASE_URL,
+ sessionId: TEST_SESSION_ID,
+ chunks: assistantTextChunks("Hi."),
+ }),
+ );
+
+ renderHost({ strictMode: true });
+ await typeAndSend("hi");
+ await screen.findByText("Hi.", undefined, { timeout: 5000 });
+
+ await waitFor(() => expect(isFinishProbingHistory).toContain(true), {
+ timeout: 5000,
+ });
+ await waitFor(() => expect(isFinishProbingHistory.at(-1)).toBe(false), {
+ timeout: 5000,
+ });
+ });
+
it(
"resets the flag via finally when the probe finds a continuation stream",
{ timeout: 15000 },
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 d46dde2ac7..1e1e828e53 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
@@ -177,31 +177,35 @@ export function ChatInput({
// during normal streaming (users can type and queue the next message).
const isTextareaDisabled = disabled || isUploadingFiles;
- const { value, setValue, handleSubmit } = useChatInput({
- onSend: async (message: string) => {
- const { localFiles, workspaceAttachments } =
- partitionAttachments(attachments);
- // 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;
- setAttachments([]);
- setRefusedCount(0);
- try {
- await onSend(
- message,
- localFiles.length > 0 ? localFiles : undefined,
- workspaceAttachments.length > 0 ? workspaceAttachments : undefined,
- );
- } catch (error) {
- setAttachments((prev) => (prev.length > 0 ? prev : sent));
- throw error;
- }
- },
- disabled: isTextareaDisabled,
- canSendEmpty: hasAttachments,
- inputId,
- });
+ const { value, setValue, handleSubmit, handleEnqueue, isEnqueueing } =
+ useChatInput({
+ onSend: async (message: string) => {
+ const { localFiles, workspaceAttachments } =
+ partitionAttachments(attachments);
+ // 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;
+ setAttachments([]);
+ setRefusedCount(0);
+ try {
+ await onSend(
+ message,
+ localFiles.length > 0 ? localFiles : undefined,
+ workspaceAttachments.length > 0 ? workspaceAttachments : undefined,
+ );
+ } catch (error) {
+ setAttachments((prev) => (prev.length > 0 ? prev : sent));
+ throw error;
+ }
+ },
+ onEnqueue,
+ isStreaming,
+ hasAttachments,
+ disabled: isTextareaDisabled,
+ canSendEmpty: hasAttachments,
+ inputId,
+ });
const integrations = useConnectedIntegrations(expertId);
@@ -219,8 +223,6 @@ export function ChatInput({
integrations,
});
- const [isEnqueueing, setIsEnqueueing] = useState(false);
-
const {
isRecording,
isTranscribing,
@@ -500,25 +502,13 @@ export function ChatInput({
}}
/>
)}
- {isStreaming && canSend && onEnqueue && (
+ {isStreaming && canSend && onEnqueue && !hasAttachments && (
<PromptInputButton
aria-label="Queue message"
tooltip="Queue message"
variant="default"
disabled={isEnqueueing}
- onClick={async () => {
- if (isEnqueueing) return;
- const trimmed = value.trim();
- if (trimmed) {
- setIsEnqueueing(true);
- try {
- await onEnqueue(trimmed);
- setValue("");
- } finally {
- setIsEnqueueing(false);
- }
- }
- }}
+ onClick={() => void handleEnqueue()}
className={cn(
"size-[2.625rem] rounded-full border-zinc-800 bg-zinc-800 text-white hover:border-zinc-900 hover:bg-zinc-900 disabled:border-zinc-200 disabled:bg-zinc-200 disabled:text-white disabled:opacity-100",
sendButtonClass,
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 6c9ada84bd..b70908e062 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
@@ -402,6 +402,142 @@ describe("ChatInput queue button", () => {
});
});
+describe("ChatInput Enter while streaming", () => {
+ // Enter submits the form (CredentialMentionEditor calls requestSubmit), so
+ // these drive the submit event directly.
+ it("queues the text instead of sending it, and clears the box", async () => {
+ const onEnqueue = vi.fn().mockResolvedValue(undefined);
+ render(<ChatInput onSend={mockOnSend} onEnqueue={onEnqueue} isStreaming />);
+ const textarea = screen.getByTestId("textarea") as HTMLTextAreaElement;
+ fireEvent.change(textarea, { target: { value: " and then? " } });
+ await act(async () => {
+ fireEvent.submit(textarea.closest("form")!);
+ });
+ expect(onEnqueue).toHaveBeenCalledWith("and then?");
+ expect(mockOnSend).not.toHaveBeenCalled();
+ expect(textarea.value).toBe("");
+ });
+
+ it("queues while an earlier send from this composer is still streaming", async () => {
+ // The composer's own send guard holds until the whole answer has
+ // streamed; Enter must not be stuck behind it.
+ const onSend = vi.fn(() => new Promise<void>(() => {}));
+ const onEnqueue = vi.fn().mockResolvedValue(undefined);
+ const { rerender } = render(
+ <ChatInput onSend={onSend} onEnqueue={onEnqueue} />,
+ );
+ const textarea = screen.getByTestId("textarea") as HTMLTextAreaElement;
+ fireEvent.change(textarea, { target: { value: "write an essay" } });
+ await act(async () => {
+ fireEvent.submit(textarea.closest("form")!);
+ });
+ expect(onSend).toHaveBeenCalledTimes(1);
+
+ rerender(<ChatInput onSend={onSend} onEnqueue={onEnqueue} isStreaming />);
+ fireEvent.change(textarea, { target: { value: "then say PINEAPPLE" } });
+ await act(async () => {
+ fireEvent.submit(textarea.closest("form")!);
+ });
+ expect(onEnqueue).toHaveBeenCalledWith("then say PINEAPPLE");
+ expect(onSend).toHaveBeenCalledTimes(1);
+ expect(textarea.value).toBe("");
+ });
+
+ it("puts the text back and toasts when queueing fails", async () => {
+ const onEnqueue = vi
+ .fn()
+ .mockRejectedValue(new Error("Session has no active turn"));
+ render(<ChatInput onSend={mockOnSend} onEnqueue={onEnqueue} isStreaming />);
+ const textarea = screen.getByTestId("textarea") as HTMLTextAreaElement;
+ fireEvent.change(textarea, { target: { value: "and then?" } });
+ await act(async () => {
+ fireEvent.submit(textarea.closest("form")!);
+ });
+ expect(textarea.value).toBe("and then?");
+ expect(toast).toHaveBeenCalledWith(
+ expect.objectContaining({
+ title: "Couldn't send message",
+ variant: "destructive",
+ }),
+ );
+ });
+
+ it("ignores a second Enter while the first queue request is pending", async () => {
+ let settle: (() => void) | undefined;
+ const onEnqueue = vi.fn(
+ () =>
+ new Promise<void>((resolve) => {
+ settle = resolve;
+ }),
+ );
+ render(<ChatInput onSend={mockOnSend} onEnqueue={onEnqueue} isStreaming />);
+ const textarea = screen.getByTestId("textarea") as HTMLTextAreaElement;
+ fireEvent.change(textarea, { target: { value: "first" } });
+ const form = textarea.closest("form")!;
+ fireEvent.submit(form);
+ fireEvent.change(textarea, { target: { value: "second" } });
+ fireEvent.submit(form);
+ expect(onEnqueue).toHaveBeenCalledTimes(1);
+ expect(textarea.value).toBe("second");
+ await act(async () => {
+ settle?.();
+ });
+ });
+
+ it("keeps attachments on the send path, which the queue cannot carry", async () => {
+ // Attachments are added between turns (paste is ignored mid-stream);
+ // the turn then starts from elsewhere, e.g. a question card answer.
+ const onSend = vi.fn().mockResolvedValue(undefined);
+ const onEnqueue = vi.fn().mockResolvedValue(undefined);
+ const { rerender } = render(
+ <ChatInput onSend={onSend} onEnqueue={onEnqueue} />,
+ );
+ const textarea = screen.getByTestId("textarea") as HTMLTextAreaElement;
+ fireEvent.paste(textarea, {
+ clipboardData: {
+ files: [new File(["png"], "shot.png", { type: "image/png" })],
+ },
+ });
+ rerender(<ChatInput onSend={onSend} onEnqueue={onEnqueue} isStreaming />);
+ fireEvent.change(textarea, { target: { value: "see attached" } });
+ await act(async () => {
+ fireEvent.submit(textarea.closest("form")!);
+ });
+ expect(onEnqueue).not.toHaveBeenCalled();
+ expect(onSend).toHaveBeenCalledWith(
+ "see attached",
+ [expect.any(File)],
+ undefined,
+ );
+ });
+
+ it("hides the queue button while attachments are present", async () => {
+ const { rerender } = render(
+ <ChatInput onSend={mockOnSend} onEnqueue={vi.fn()} />,
+ );
+ const textarea = screen.getByTestId("textarea") as HTMLTextAreaElement;
+ fireEvent.paste(textarea, {
+ clipboardData: {
+ files: [new File(["png"], "shot.png", { type: "image/png" })],
+ },
+ });
+ rerender(<ChatInput onSend={mockOnSend} onEnqueue={vi.fn()} isStreaming />);
+ fireEvent.change(textarea, { target: { value: "see attached" } });
+ expect(screen.queryByLabelText(/queue message/i)).toBeNull();
+ });
+
+ it("falls back to a normal send when nothing can queue", async () => {
+ const onSend = vi.fn().mockResolvedValue(undefined);
+ render(<ChatInput onSend={onSend} isStreaming />);
+ const textarea = screen.getByTestId("textarea") as HTMLTextAreaElement;
+ fireEvent.change(textarea, { target: { value: "hello" } });
+ await act(async () => {
+ fireEvent.submit(textarea.closest("form")!);
+ });
+ expect(onSend).toHaveBeenCalledWith("hello", undefined, undefined);
+ });
+});
+
describe("ChatInput dry-run toggle", () => {
it("does not render dry-run toggle when flag is disabled", () => {
mockFlagValue = false;
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 ea37a24107..d3cca2b924 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
@@ -5,6 +5,13 @@ import { ChangeEvent, FormEvent, useEffect, useRef, useState } from "react";
interface Args {
onSend: (message: string) => void;
+ /** Queues text behind the live turn. While `isStreaming`, a submit goes
+ * here instead of `onSend`. */
+ onEnqueue?: (message: string) => void | Promise<void>;
+ isStreaming?: boolean;
+ /** Attachments can't be queued (the pending endpoint takes text only), so
+ * a submit with any attached stays on the `onSend` path. */
+ hasAttachments?: boolean;
disabled?: boolean;
/** Allow sending when text is empty (e.g. when files are attached). */
canSendEmpty?: boolean;
@@ -13,15 +20,20 @@ interface Args {
export function useChatInput({
onSend,
+ onEnqueue,
+ isStreaming = false,
+ hasAttachments = false,
disabled = false,
canSendEmpty = false,
inputId = "chat-input",
}: Args) {
const [value, setValue] = useState("");
const [isSending, setIsSending] = useState(false);
+ const [isEnqueueing, setIsEnqueueing] = useState(false);
// Synchronous guard against double-submit — refs update immediately,
// unlike state which batches and can leave a gap for a second call.
const isSubmittingRef = useRef(false);
+ const isEnqueueingRef = useRef(false);
const { initialPrompt, setInitialPrompt, notifyMessageSent } =
useCopilotUIStore();
@@ -90,11 +102,46 @@ export function useChatInput({
}
}
+ // Deliberately not behind `isSubmittingRef`: in an existing chat `onSend`
+ // settles only when the whole answer has streamed, so that guard is what
+ // kept Enter from doing anything while Otto answered. A follow-up typed
+ // mid-turn is queued, never sent, so it has a guard of its own.
+ async function handleEnqueue(message = value) {
+ const trimmedMessage = message.trim();
+ if (!onEnqueue || disabled || !trimmedMessage) return;
+ if (isEnqueueingRef.current) return;
+
+ isEnqueueingRef.current = true;
+ setIsEnqueueing(true);
+ setValue("");
+ try {
+ await onEnqueue(trimmedMessage);
+ } catch (error) {
+ setValue((current) => restoreFailedDraft(current, message));
+ toast({
+ title: "Couldn't send message",
+ description: describeSendFailure(
+ error,
+ "your message is back in the composer",
+ ),
+ variant: "destructive",
+ });
+ } finally {
+ isEnqueueingRef.current = false;
+ setIsEnqueueing(false);
+ }
+ }
+
function handleSubmit(e: FormEvent<HTMLFormElement>) {
e.preventDefault();
const formData = new FormData(e.currentTarget);
- const message = formData.get("message");
- void handleSend(typeof message === "string" ? message : value);
+ const field = formData.get("message");
+ const message = typeof field === "string" ? field : value;
+ if (isStreaming && onEnqueue && !hasAttachments) {
+ void handleEnqueue(message);
+ return;
+ }
+ void handleSend(message);
}
function handleChange(e: ChangeEvent<HTMLTextAreaElement>) {
@@ -105,9 +152,11 @@ export function useChatInput({
value,
setValue,
handleSend,
+ handleEnqueue,
handleSubmit,
handleChange,
isSending,
+ isEnqueueing,
};
}
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/__tests__/heldFollowUps.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/__tests__/heldFollowUps.test.ts
new file mode 100644
index 0000000000..8100e0982b
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/__tests__/heldFollowUps.test.ts
@@ -0,0 +1,74 @@
+import { afterEach, describe, expect, it, vi } from "vitest";
+import {
+ forgetHeldFollowUp,
+ rememberHeldFollowUp,
+ takeHeldFollowUps,
+} from "../heldFollowUps";
+
+afterEach(() => {
+ window.sessionStorage.clear();
+});
+
+describe("heldFollowUps", () => {
+ it("hands back what was remembered for a chat, once", () => {
+ rememberHeldFollowUp("s1", "first");
+ rememberHeldFollowUp("s1", "second");
+ rememberHeldFollowUp("s2", "elsewhere");
+
+ expect(takeHeldFollowUps("s1")).toEqual(["first", "second"]);
+ expect(takeHeldFollowUps("s1")).toEqual([]);
+ expect(takeHeldFollowUps("s2")).toEqual(["elsewhere"]);
+ });
+
+ it("forgets only the one copy that went out", () => {
+ rememberHeldFollowUp("s1", "same");
+ rememberHeldFollowUp("s1", "same");
+ forgetHeldFollowUp("s1", "same");
+ expect(takeHeldFollowUps("s1")).toEqual(["same"]);
+ });
+
+ it("drops the key once nothing is held", () => {
+ rememberHeldFollowUp("s1", "only");
+ forgetHeldFollowUp("s1", "only");
+ expect(window.sessionStorage.length).toBe(0);
+ });
+
+ it("keeps going when storage refuses the write", () => {
+ const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
+ const real = window.sessionStorage;
+ const full = {
+ getItem: () => null,
+ setItem: () => {
+ throw new DOMException("full", "QuotaExceededError");
+ },
+ removeItem: () => {
+ throw new DOMException("blocked", "SecurityError");
+ },
+ };
+ Object.defineProperty(window, "sessionStorage", {
+ value: full,
+ configurable: true,
+ });
+ try {
+ expect(() => rememberHeldFollowUp("s1", "text")).not.toThrow();
+ expect(() => takeHeldFollowUps("s1")).not.toThrow();
+ expect(warn).toHaveBeenCalledTimes(2);
+ } finally {
+ Object.defineProperty(window, "sessionStorage", {
+ value: real,
+ configurable: true,
+ });
+ warn.mockRestore();
+ }
+ });
+
+ it("ignores a corrupt entry instead of throwing", () => {
+ window.sessionStorage.setItem("copilot-held-follow-ups:s1", "{not json");
+ expect(takeHeldFollowUps("s1")).toEqual([]);
+ window.sessionStorage.setItem(
+ "copilot-held-follow-ups:s1",
+ JSON.stringify([1, "text", null]),
+ );
+ expect(takeHeldFollowUps("s1")).toEqual(["text"]);
+ });
+});
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/heldFollowUps.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/heldFollowUps.ts
new file mode 100644
index 0000000000..38df4e6400
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/heldFollowUps.ts
@@ -0,0 +1,67 @@
+const KEY_PREFIX = "copilot-held-follow-ups:";
+
+function storage(): Storage | null {
+ try {
+ return typeof window === "undefined" ? null : window.sessionStorage;
+ } catch {
+ return null;
+ }
+}
+
+function read(store: Storage, key: string): string[] {
+ try {
+ const parsed: unknown = JSON.parse(store.getItem(key) ?? "[]");
+ return Array.isArray(parsed)
+ ? parsed.filter((entry): entry is string => typeof entry === "string")
+ : [];
+ } catch {
+ return [];
+ }
+}
+
+// Best effort: a full or blocked storage must not stop the follow-up itself.
+function write(store: Storage, key: string, texts: string[]) {
+ try {
+ if (texts.length === 0) store.removeItem(key);
+ else store.setItem(key, JSON.stringify(texts));
+ } catch (error) {
+ console.warn("Could not persist held follow-ups", error);
+ }
+}
+
+/**
+ * A follow-up the page is holding until its own stream has settled (see
+ * `useCopilotPage.onSend`) exists nowhere but in memory. These keep a copy in
+ * sessionStorage for the chat it belongs to, so a reload or a switch to
+ * another chat before it went out can hand it back to the composer.
+ */
+export function rememberHeldFollowUp(sessionId: string, text: string) {
+ const store = storage();
+ if (!store) return;
+ const key = `${KEY_PREFIX}${sessionId}`;
+ write(store, key, [...read(store, key), text]);
+}
+
+export function forgetHeldFollowUp(sessionId: string, text: string) {
+ const store = storage();
+ if (!store) return;
+ const key = `${KEY_PREFIX}${sessionId}`;
+ const texts = read(store, key);
+ const index = texts.indexOf(text);
+ if (index === -1) return;
+ write(
+ store,
+ key,
+ texts.filter((_, i) => i !== index),
+ );
+}
+
+/** Returns and clears what was left behind for this chat. */
+export function takeHeldFollowUps(sessionId: string): string[] {
+ const store = storage();
+ if (!store) return [];
+ const key = `${KEY_PREFIX}${sessionId}`;
+ const texts = read(store, key);
+ write(store, key, []);
+ return texts;
+}
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPage.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPage.ts
index cb586b038d..56d6ca4d2f 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPage.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPage.ts
@@ -4,10 +4,15 @@ import { isValidUUID } from "@/lib/utils";
import { Flag, useGetFlag } from "@/services/feature-flags/use-get-flag";
import type { UIMessage } from "ai";
import { parseAsString, useQueryState } from "nuqs";
-import { useEffect, useMemo, useRef } from "react";
+import { useEffect, useMemo, useRef, useState } from "react";
import { concatWithAssistantMerge } from "./helpers/convertChatSessionToUiMessages";
import { getLatestAssistantStatusMessage } from "./messageParts";
import type { WorkspaceAttachment } from "./helpers/workspaceAttachments";
+import {
+ forgetHeldFollowUp,
+ rememberHeldFollowUp,
+ takeHeldFollowUps,
+} from "./helpers/heldFollowUps";
import { queueFollowUpMessage } from "./helpers/queueFollowUpMessage";
import { stripReplayPrefix } from "./helpers/stripReplayPrefix";
import { useCopilotStreamStore } from "./copilotStreamStore";
@@ -26,7 +31,7 @@ import { useCopilotNotifications } from "./useCopilotNotifications";
import { useCopilotStream } from "./useCopilotStream";
import { resolveExpertIdentity, useExpertMap } from "./useExpertMap";
import { useLoadMoreMessages } from "./useLoadMoreMessages";
-import { useSendMessage } from "./useSendMessage";
+import { recoverFailedDeferredSend, useSendMessage } from "./useSendMessage";
import { useSessionTitlePoll } from "./useSessionTitlePoll";
import { useWorkflowImportAutoSubmit } from "./useWorkflowImportAutoSubmit";
import { useCompleteBrainDumpGreeting } from "@/app/api/__generated__/endpoints/brain-dump/brain-dump";
@@ -117,7 +122,7 @@ export function useCopilotPage() {
setKickoffParam,
]);
- const { copilotLlmModel, isDryRun } = useCopilotUIStore();
+ const { copilotLlmModel, isDryRun, setInitialPrompt } = useCopilotUIStore();
const { mutate: completeGreeting } = useCompleteBrainDumpGreeting();
const {
@@ -232,6 +237,66 @@ export function useCopilotPage() {
isInflightRef.current =
!isUserStopping && (status === "streaming" || status === "submitted");
+ // Whether this tab has finished drawing the previous turn. The server's
+ // turn can end well before the screen does: the smoothing transform paces
+ // text out word by word (copilotStreamSmoothing.ts), and the post-finish
+ // probe may still turn into a reconnect. A follow-up the backend refused
+ // to queue (409, no active turn) waits here before going out as a new
+ // turn — see onSend.
+ const isLocalStreamSettled =
+ !isInflightRef.current && !isFinishProbing && !isReconnecting;
+ const isLocalStreamSettledRef = useRef(isLocalStreamSettled);
+ isLocalStreamSettledRef.current = isLocalStreamSettled;
+ // Each waiter learns whether the stream settled (send now) or the hook
+ // went away first (the chat host unmounts on a chat switch and on reload;
+ // the follow-up then stays in sessionStorage, see below).
+ const settleWaitersRef = useRef<Array<(settled: boolean) => void>>([]);
+ useEffect(() => {
+ if (!isLocalStreamSettled) return;
+ settleWaitersRef.current.splice(0).forEach((resolve) => resolve(true));
+ }, [isLocalStreamSettled]);
+ useEffect(
+ () => () => {
+ settleWaitersRef.current.splice(0).forEach((resolve) => resolve(false));
+ },
+ [],
+ );
+
+ function waitForLocalSettle() {
+ if (isLocalStreamSettledRef.current) return Promise.resolve(true);
+ return new Promise<boolean>((resolve) => {
+ settleWaitersRef.current.push(resolve);
+ });
+ }
+
+ // Follow-ups waiting on the settle above. Shown as "Queued" chips beside
+ // the backend-buffered ones, since from the user's side they are the same
+ // thing. Mirrored into sessionStorage: this host is unmounted on a chat
+ // switch and gone on a reload, and the only copy was in memory.
+ const [heldFollowUps, setHeldFollowUps] = useState<string[]>([]);
+ function holdFollowUp(forSessionId: string, text: string) {
+ setHeldFollowUps((prev) => [...prev, text]);
+ rememberHeldFollowUp(forSessionId, text);
+ }
+ function releaseFollowUp(forSessionId: string, text: string) {
+ setHeldFollowUps((prev) => {
+ const index = prev.indexOf(text);
+ return index === -1 ? prev : prev.filter((_, i) => i !== index);
+ });
+ forgetHeldFollowUp(forSessionId, text);
+ }
+ useEffect(() => {
+ if (!sessionId) return;
+ const leftBehind = takeHeldFollowUps(sessionId);
+ if (leftBehind.length === 0) return;
+ setInitialPrompt(leftBehind.join("\n\n"));
+ toast({
+ title: "Follow-up not sent",
+ description:
+ "You left the chat before it could go out. It's back in the composer.",
+ });
+ }, [sessionId, setInitialPrompt]);
+
// Combine paginated messages with current page messages, merging consecutive
// assistant UIMessages at the page boundary so reasoning + response parts
// stay in a single bubble. Paged messages are older history prepended before
@@ -326,7 +391,12 @@ export function useCopilotPage() {
trackBrainDump("intro_followup_sent", { chars: trimmed.length });
}
- if (sessionId && isInflightRef.current) {
+ let heldForLocalSettle = false;
+ // A loop, not an `if`: a held follow-up re-checks the in-flight ref once
+ // the screen settles, because another held follow-up may have dispatched
+ // a new turn in the same settle (in which case this one queues behind
+ // it) — and never sends while a turn is still being drawn.
+ while (sessionId && isInflightRef.current) {
if (hasAttachments) {
toast({
title: "Please wait to attach files",
@@ -339,23 +409,29 @@ export function useCopilotPage() {
try {
await queueFollowUpMessage(sessionId, trimmed);
+ if (heldForLocalSettle) releaseFollowUp(sessionId, trimmed);
queueMessage(trimmed);
+ return;
} catch (err) {
+ // Any other failure propagates to the composer, which restores the
+ // draft and shows the one toast for it.
if (
- err instanceof Error &&
- err.name === "QueueFollowUpNotActiveError"
+ !(err instanceof Error && err.name === "QueueFollowUpNotActiveError")
) {
- await sendNewMessage(message, files, workspaceFiles, metadata);
- return;
+ if (heldForLocalSettle) releaseFollowUp(sessionId, trimmed);
+ throw err;
}
- toast({
- title: "Could not queue message",
- description: "Please wait for the current response to finish.",
- variant: "destructive",
- });
- throw err;
}
- return;
+
+ // The backend's turn is already over, but this tab may still be
+ // drawing it. Starting a second `useChat` request now cuts the live
+ // answer off mid-sentence: AI SDK only streams into the last message
+ // while its id matches, and the new user bubble takes that slot (see
+ // midTurnSplit.ts). Hold the follow-up until the local stream has
+ // settled, then send it as a normal turn below the finished answer.
+ if (!heldForLocalSettle) holdFollowUp(sessionId, trimmed);
+ heldForLocalSettle = true;
+ if (!(await waitForLocalSettle())) return;
}
// Mark in-flight synchronously before dispatching so a rapid second
@@ -363,6 +439,17 @@ export function useCopilotPage() {
// instead of triggering a duplicate /stream POST.
if (sessionId) {
isInflightRef.current = true;
+ isLocalStreamSettledRef.current = false;
+ }
+ if (heldForLocalSettle && sessionId) {
+ releaseFollowUp(sessionId, trimmed);
+ // Resolve once dispatched, not when the whole answer has streamed:
+ // the composer's enqueue path is waiting on this, and holding it for
+ // the entire turn would lock Enter and the queue button again.
+ void sendNewMessage(message, files, workspaceFiles, metadata).catch(
+ (err: unknown) => recoverFailedDeferredSend(trimmed, [], err),
+ );
+ return;
}
await sendNewMessage(message, files, workspaceFiles, metadata);
}
@@ -427,7 +514,10 @@ export function useCopilotPage() {
// onEnqueue delegates to onSend, which internally routes to the queue
// endpoint when isInflightRef.current is true.
onEnqueue: onSend,
- queuedMessages,
+ queuedMessages:
+ heldFollowUps.length > 0
+ ? [...queuedMessages, ...heldFollowUps]
+ : queuedMessages,
hasMoreMessages: hasMore,
isLoadingMore,
loadMore,
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotStream.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotStream.ts
index 6ce50ffad7..d778bc81aa 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotStream.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotStream.ts
@@ -662,6 +662,12 @@ export function useCopilotStream({
// not tear down its live SSE stream.
// ---------------------------------------------------------------------------
useMountEffect(() => {
+ // Strict Mode runs mount → cleanup → mount in development. Without this
+ // reset the simulated unmount left the flag false for the whole life of
+ // the real mount, so `handleFinish` never cleared `isFinishProbing` and
+ // every consumer gated on it (post-finish hydration, a held follow-up)
+ // stayed stuck after the first turn.
+ isMountedRef.current = true;
return () => {
isMountedRef.current = false;
};
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/useSendMessage.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/useSendMessage.ts
index 36cbcad154..1c3b9c3554 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/useSendMessage.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/useSendMessage.ts
@@ -207,7 +207,7 @@ export function useSendMessage({
void dispatchRef
.current(sessionId, send.text, send.files, parts, send.metadata)
.catch((error: unknown) => {
- recoverFailedFirstSend(send.text, parts, error);
+ recoverFailedDeferredSend(send.text, parts, error);
});
}, [sessionId]);
@@ -330,16 +330,18 @@ export function useSendMessage({
}
/**
- * Failure recovery for the first send of a new chat, which is dispatched from
- * an effect long after `onSend` returned.
+ * Failure recovery for a send whose caller can no longer observe it: the
+ * first send of a new chat (dispatched from an effect long after `onSend`
+ * returned) and a follow-up held until the local stream settled (see
+ * `useCopilotPage`), whose `onSend` resolves once the turn is dispatched.
*
* Everything goes through the stores because the `"new"`-keyed host that
- * started the send is already unmounted — its draft state and chips are gone.
- * The text comes back as the composer's initial prompt and the workspace
- * references as pending file parts; local `File` chips cannot be restored,
- * they only ever existed in that unmounted host.
+ * started a first send is already unmounted — its draft state and chips are
+ * gone. The text comes back as the composer's initial prompt and the
+ * workspace references as pending file parts; local `File` chips cannot be
+ * restored, they only ever existed in that unmounted host.
*/
-function recoverFailedFirstSend(
+export function recoverFailedDeferredSend(
text: string,
parts: StoredAttachmentPart[],
error: unknown,
diff --git a/autogpt_platform/frontend/src/app/(platform)/settings/memory/components/MemoryChatPanel/useMemoryChatPanel.ts b/autogpt_platform/frontend/src/app/(platform)/settings/memory/components/MemoryChatPanel/useMemoryChatPanel.ts
index 15dca8d2d3..7f216a5da8 100644
--- a/autogpt_platform/frontend/src/app/(platform)/settings/memory/components/MemoryChatPanel/useMemoryChatPanel.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/settings/memory/components/MemoryChatPanel/useMemoryChatPanel.ts
@@ -6,7 +6,6 @@ import {
useGetV2GetSession,
usePostV2CreateSession,
} from "@/app/api/__generated__/endpoints/chat/chat";
-import { toast } from "@/components/molecules/Toast/use-toast";
import * as Sentry from "@sentry/nextjs";
import type { UIDataTypes, UIMessage, UITools } from "ai";
import { useEffect, useMemo, useRef, useState } from "react";
@@ -155,11 +154,8 @@ export function useMemoryChatPanel({ scopeExpertID }: Args) {
return;
}
Sentry.captureException(err);
- toast({
- variant: "destructive",
- title: "Could not queue message",
- description: "Please wait for the current response to finish.",
- });
+ // The composer restores the draft and shows the one toast for it.
+ throw err;
}
return;
}
diff --git a/autogpt_platform/frontend/src/app/(platform)/team/components/ExpertChatDrawer/useExpertChatDrawer.ts b/autogpt_platform/frontend/src/app/(platform)/team/components/ExpertChatDrawer/useExpertChatDrawer.ts
index 19b2ec0f69..1b2785b9af 100644
--- a/autogpt_platform/frontend/src/app/(platform)/team/components/ExpertChatDrawer/useExpertChatDrawer.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/team/components/ExpertChatDrawer/useExpertChatDrawer.ts
@@ -438,11 +438,8 @@ export function useExpertChatDrawer({
return;
}
Sentry.captureException(err);
- toast({
- variant: "destructive",
- title: "Could not queue message",
- description: "Please wait for the current response to finish.",
- });
+ // The composer restores the draft and shows the one toast for it.
+ throw err;
}
return;
}