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): a second tap right after Send no longer stops the message (#15547)

Reinier van der Leerbcd4f0bbbb97
diff --git a/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/BuilderChatPanel.tsx b/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/BuilderChatPanel.tsx
index c75c2fc916..fb24c93d2e 100644
--- a/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/BuilderChatPanel.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderChatPanel/BuilderChatPanel.tsx
@@ -117,6 +117,7 @@ export function BuilderChatPanel({ className }: Props) {
onSend={onSend}
disabled={false}
isStreaming={isStreaming}
+ isTurnStarting={status === "submitted"}
onStop={stop}
onEnqueue={onSend}
placeholder="Ask the builder to edit or run this workflow…"
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/autopilot-lifecycle.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/autopilot-lifecycle.test.tsx
index 096383037b..2551bdf4fa 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/autopilot-lifecycle.test.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/autopilot-lifecycle.test.tsx
@@ -3,8 +3,11 @@ import {
assistantTextChunks,
copilotResumeHandler,
copilotStreamHandler,
+ createStreamHold,
} from "@/tests/integrations/copilot-sse";
-import { screen, waitFor } from "@testing-library/react";
+import { screen, waitFor, within } from "@testing-library/react";
+import userEvent from "@testing-library/user-event";
+import { http, HttpResponse } from "msw";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import {
clickStop,
@@ -130,6 +133,74 @@ describe.each(STREAM_PATHS)("on the %s path", (path) => {
// Post-stop chunks never reach the bubble.
expect(screen.queryByText(/should be cut off/i)).toBeNull();
});
+
+ // A phone user's second taps came up to 1.5 s after Send, while his sends
+ // took up to 1.6 s to name their turn: past Stop's arming, still connecting.
+ it("a second tap on Send while the send connects neither stops nor loses it", async () => {
+ const { hold, release } = createStreamHold(0);
+ server.use(
+ copilotStreamHandler({
+ baseUrl: TEST_BACKEND_BASE_URL,
+ sessionId: TEST_SESSION_ID,
+ chunks: assistantTextChunks("Here is the answer."),
+ hold,
+ }),
+ );
+ renderHost();
+ const cancels = countCancels();
+
+ await typeAndSend("make some screenshots");
+ await sleep(1200);
+ await userEvent.setup().click(sendSlot());
+ await sleep(200);
+ expect(cancels()).toBe(0);
+
+ release();
+ expect(
+ await screen.findByText("Here is the answer.", undefined, {
+ timeout: 5000,
+ }),
+ ).toBeDefined();
+ }, 15_000);
+
+ // The same user's second tap landed 0.15 s after Stop replaced Send.
+ it("a Stop that has just replaced Send ignores a tap, then stops once armed", async () => {
+ const { hold, release } = createStreamHold(4);
+ server.use(
+ copilotStreamHandler({
+ baseUrl: TEST_BACKEND_BASE_URL,
+ sessionId: TEST_SESSION_ID,
+ chunks: [
+ { type: "start", messageId: "msg-1" },
+ { type: "start-step" },
+ { type: "text-start", id: "t1" },
+ { type: "text-delta", id: "t1", delta: "Working on it " },
+ { type: "text-delta", id: "t1", delta: "and done." },
+ { type: "text-end", id: "t1" },
+ { type: "finish-step" },
+ { type: "finish" },
+ ],
+ hold,
+ }),
+ );
+ renderHost();
+ const cancels = countCancels();
+ const user = userEvent.setup();
+
+ await typeAndSend("hello");
+ const stop = await screen.findByRole("button", { name: /^stop$/i });
+ await sleep(150);
+ await user.click(stop);
+ await sleep(200);
+ expect(cancels()).toBe(0);
+
+ await clickStop();
+ await waitFor(() => expect(cancels()).toBe(1));
+ expect(
+ await screen.findByText(/task stopped/i, undefined, { timeout: 5000 }),
+ ).toBeDefined();
+ release();
+ }, 15_000);
});
describe("Otto streaming — resume on mount", () => {
@@ -160,3 +231,26 @@ describe.each(STREAM_PATHS)("on the %s path", (path) => {
});
});
});
+
+function countCancels() {
+ let count = 0;
+ server.use(
+ http.post(`*/api/proxy/api/chat/sessions/${TEST_SESSION_ID}/cancel`, () => {
+ count += 1;
+ return HttpResponse.json({ cancelled: true, reason: null });
+ }),
+ );
+ return () => count;
+}
+
+// Whatever button sits where Send was: the composer's last.
+function sendSlot() {
+ const form = screen.getByLabelText(/chat message input/i).closest("form");
+ if (!form) throw new Error("no composer form");
+ const buttons = within(form).getAllByRole("button");
+ return buttons[buttons.length - 1];
+}
+
+function sleep(ms: number) {
+ return new Promise((resolve) => setTimeout(resolve, ms));
+}
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/sse-helpers.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/sse-helpers.tsx
index 86f0f20d0e..e3de28699b 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/sse-helpers.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/sse-helpers.tsx
@@ -200,7 +200,12 @@ export async function typeAndSend(text: string) {
await user.click(screen.getByRole("button", { name: /submit/i }));
}
+/** Stop ignores taps for a moment after it appears; wait until it takes one. */
export async function clickStop() {
const user = userEvent.setup();
- await user.click(await screen.findByRole("button", { name: /stop/i }));
+ const stop = await screen.findByRole("button", { name: /stop/i });
+ await waitFor(() => expect(stop.hasAttribute("disabled")).toBe(false), {
+ timeout: 3000,
+ });
+ await user.click(stop);
}
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/ChatContainer.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/ChatContainer.tsx
index 385d4b0514..7b1e6856f0 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/ChatContainer.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatContainer/ChatContainer.tsx
@@ -467,6 +467,7 @@ export const ChatContainer = ({
onSend={guardedOnSend}
disabled={isInputDisabled}
isStreaming={isStreaming}
+ isTurnStarting={status === "submitted"}
isUploadingFiles={isUploadingFiles}
onStop={onStop}
onEnqueue={onEnqueue}
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 c79966713b..b7b985c484 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
@@ -7,11 +7,6 @@ import {
import { isGuidedPrompt } from "@/components/contextual/guidedPrompts";
import { toast } from "@/components/molecules/Toast/use-toast";
import { InputGroup, InputGroupAddon } from "@/components/ui/input-group";
-import {
- Tooltip,
- TooltipContent,
- TooltipTrigger,
-} from "@/components/ui/tooltip";
import { cn } from "@/lib/utils";
import { Flag, useGetFlag } from "@/services/feature-flags/use-get-flag";
import {
@@ -45,6 +40,7 @@ import { ConnectionPicker } from "./components/ConnectionPicker/ConnectionPicker
import { NoAIConnectionNotice } from "./components/NoAIConnectionNotice/NoAIConnectionNotice";
import { RecordingButton } from "./components/RecordingButton";
import { RecordingIndicator } from "./components/RecordingIndicator";
+import { StopButton } from "./components/StopButton";
import { TranscriptionErrorBar } from "./components/TranscriptionErrorBar";
import { WorkspaceFilePicker } from "./components/WorkspaceFilePicker/WorkspaceFilePicker";
import { useCopilotUIStore } from "../../store";
@@ -77,6 +73,9 @@ interface Props {
) => void | Promise<void>;
disabled?: boolean;
isStreaming?: boolean;
+ /** The sent message's turn has not started: a Stop now would cancel nothing
+ * on the server and drop the message, so the button only shows progress. */
+ isTurnStarting?: boolean;
isUploadingFiles?: boolean;
onStop?: () => void;
/** Called to enqueue a message when copilot is streaming and user has typed text. */
@@ -123,6 +122,7 @@ export function ChatInput({
onSend,
disabled = false,
isStreaming = false,
+ isTurnStarting = false,
isUploadingFiles = false,
onStop,
onEnqueue,
@@ -569,17 +569,14 @@ export function ChatInput({
<Icon icon={ArrowUp02Icon} className="size-4" />
</PromptInputButton>
)}
- {isStreaming ? (
- <Tooltip>
- <TooltipTrigger asChild>
- <PromptInputSubmit
- status="streaming"
- onStop={onStop}
- className={sendButtonClass}
- />
- </TooltipTrigger>
- <TooltipContent side="top">Stop</TooltipContent>
- </Tooltip>
+ {isStreaming && isTurnStarting ? (
+ <PromptInputSubmit
+ status="submitted"
+ aria-label="Sending"
+ className={sendButtonClass}
+ />
+ ) : isStreaming ? (
+ <StopButton onStop={onStop} className={sendButtonClass} />
) : hideSubmitWhenEmpty && !canSend ? null : (
<PromptInputSubmit
disabled={!canSend}
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/StopButton.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/StopButton.tsx
new file mode 100644
index 0000000000..2e4fde880e
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatInput/components/StopButton.tsx
@@ -0,0 +1,39 @@
+import { PromptInputSubmit } from "@/components/ai-elements/prompt-input";
+import {
+ Tooltip,
+ TooltipContent,
+ TooltipTrigger,
+} from "@/components/ui/tooltip";
+import { useEffect, useState } from "react";
+
+// Stop takes Send's place under the finger that just used it; a stray second
+// tap on a phone lands well inside this.
+const STOP_ARM_MS = 1000;
+
+interface Props {
+ onStop?: () => void;
+ className?: string;
+}
+
+export function StopButton({ onStop, className }: Props) {
+ const [isArmed, setIsArmed] = useState(false);
+
+ useEffect(() => {
+ const timer = setTimeout(() => setIsArmed(true), STOP_ARM_MS);
+ return () => clearTimeout(timer);
+ }, []);
+
+ return (
+ <Tooltip>
+ <TooltipTrigger asChild>
+ <PromptInputSubmit
+ status="streaming"
+ onStop={onStop}
+ disabled={!isArmed}
+ className={className}
+ />
+ </TooltipTrigger>
+ <TooltipContent side="top">Stop</TooltipContent>
+ </Tooltip>
+ );
+}
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/stream/__tests__/turnRuntime.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/stream/__tests__/turnRuntime.test.ts
index ab41d7658b..c3542b448c 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/stream/__tests__/turnRuntime.test.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/stream/__tests__/turnRuntime.test.ts
@@ -166,6 +166,72 @@ describe("§1.7 — every disconnect cause is expected but one", () => {
expect(snapshot().stopped).toBe(true);
});
+ it("an approval answered after a stop follows the turn it starts", async () => {
+ void runtime.send({ text: PROMPT }, undefined);
+ await advance(0);
+ runtime.stop();
+ backend.beginRunning();
+ backend.publish(FIRST_BLOCK_DONE);
+ runtime.followBackendTurn();
+ await advance(2_000);
+ expect(snapshot().phase).toBe("live");
+ expect(snapshot().stopped).toBe(false);
+ });
+
+ it("an approval answered while a stopped send's turn still runs is followed after it", async () => {
+ void runtime.send({ text: PROMPT }, undefined);
+ await advance(0);
+ runtime.stop();
+ // The cancel came before the turn existed, so it runs on.
+ backend.publish(FIRST_BLOCK_DONE);
+ let approvalStarted = false;
+ runtime.bind({}, async () => {
+ const view = backend.view();
+ if (!approvalStarted || view.active_stream) return view;
+ return {
+ ...view,
+ chat_status: "running",
+ active_stream: { turn_id: "approval-turn", checkpoint: null },
+ };
+ });
+ runtime.followBackendTurn();
+ await advance(1_000);
+ backend.publish();
+ // The approval's turn starts once that turn has released the session.
+ await advance(700);
+ approvalStarted = true;
+ await advance(5_000);
+ expect(
+ backend.requests.some(
+ (r) => r.url.searchParams.get("turn") === "approval-turn",
+ ),
+ ).toBe(true);
+ });
+
+ it("an approval answered while a stopped turn drains is followed once it has", async () => {
+ await sendAndPublish(FIRST_BLOCK_DONE);
+ runtime.stop();
+ let approvalStarted = false;
+ runtime.bind({}, async () => {
+ const view = backend.view();
+ if (!approvalStarted) return view;
+ return {
+ ...view,
+ chat_status: "running",
+ active_stream: { turn_id: "approval-turn", checkpoint: null },
+ };
+ });
+ runtime.followBackendTurn();
+ await advance(1_200);
+ approvalStarted = true;
+ await advance(5_000);
+ expect(
+ backend.requests.some(
+ (r) => r.url.searchParams.get("turn") === "approval-turn",
+ ),
+ ).toBe(true);
+ });
+
it("a connection silent for 30 s is replaced without a toast", async () => {
await sendAndPublish(FIRST_BLOCK_DONE);
backend.connections[0].silence();
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/stream/turnRuntime.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/stream/turnRuntime.ts
index d90aff87cd..5c99078b83 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/stream/turnRuntime.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/stream/turnRuntime.ts
@@ -386,11 +386,20 @@ export class TurnRuntime {
/** An answered approval starts the chat's next turn on the server; attach to it. */
followBackendTurn() {
+ // Like a send, it asks for a turn, so an earlier Stop no longer holds. A
+ // stopped turn may still run; the approval's starts after it ends.
+ if (this.stopped) this.followPending = true;
+ this.stopped = false;
+ this.suppressAttach = false;
+ this.queueDeclined = false;
if (this.runningTurn() || this.isPostPending()) {
this.followPending = true;
return;
}
- void this.probe(null, CONTINUATION_PROBES, true);
+ const last = this.segments.findLast(
+ (s): s is TurnSegment => s.kind === "turn",
+ );
+ void this.probe(last?.log.turnId ?? null, CONTINUATION_PROBES, true);
}
dispose() {
diff --git a/autogpt_platform/frontend/src/app/(platform)/settings/memory/components/MemoryChatPanel/MemoryChatPanel.tsx b/autogpt_platform/frontend/src/app/(platform)/settings/memory/components/MemoryChatPanel/MemoryChatPanel.tsx
index 18c7d08577..bca9f9171b 100644
--- a/autogpt_platform/frontend/src/app/(platform)/settings/memory/components/MemoryChatPanel/MemoryChatPanel.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/settings/memory/components/MemoryChatPanel/MemoryChatPanel.tsx
@@ -104,6 +104,7 @@ export function MemoryChatPanel({
onSend={onSend}
disabled={false}
isStreaming={isStreaming}
+ isTurnStarting={status === "submitted"}
onStop={onStop}
onEnqueue={onSend}
placeholder="Ask or update…"
diff --git a/autogpt_platform/frontend/src/app/(platform)/team/components/ExpertChatDrawer/ExpertChatDrawer.tsx b/autogpt_platform/frontend/src/app/(platform)/team/components/ExpertChatDrawer/ExpertChatDrawer.tsx
index 7582a3dc73..914b00510b 100644
--- a/autogpt_platform/frontend/src/app/(platform)/team/components/ExpertChatDrawer/ExpertChatDrawer.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/team/components/ExpertChatDrawer/ExpertChatDrawer.tsx
@@ -185,6 +185,7 @@ function ChatPanelBody({ target, identity, chat }: BodyProps) {
onSend={onSend}
disabled={isResolvingSession || isCreating}
isStreaming={isStreaming}
+ isTurnStarting={status === "submitted"}
onStop={stop}
onEnqueue={onSend}
placeholder={`Message ${target.name}…`}