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): stopping a response shows the stopped card, not an error (#14682)

Reinier van der Leera1398a8b30bf
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/useHydrateOnStreamEnd.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/useHydrateOnStreamEnd.test.ts
index f67b62e1b5..485b077fe8 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/useHydrateOnStreamEnd.test.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/useHydrateOnStreamEnd.test.ts
@@ -5,6 +5,7 @@ import {
_resetInterruptedToastLedgerForTests,
useHydrateOnStreamEnd,
} from "../useHydrateOnStreamEnd";
+import { CANCELLED_MARKER } from "../useCopilotStop";
vi.mock("@/components/molecules/Toast/use-toast", () => ({
toast: vi.fn(),
@@ -371,3 +372,56 @@ describe("useHydrateOnStreamEnd — continuation-turn flash guard", () => {
expect(captured.current!.map(seqOf)).toEqual(range(1, 10).map(seqOf));
});
});
+
+describe("useHydrateOnStreamEnd — a turn the user stopped", () => {
+ afterEach(() => {
+ _resetInterruptedToastLedgerForTests();
+ cleanup();
+ });
+
+ function stoppedAssistant(): UIMessage<unknown, UIDataTypes, UITools> {
+ return {
+ id: "live-assistant",
+ role: "assistant",
+ parts: [
+ { type: "text", text: "Half an answer", state: "done" },
+ { type: "text", text: CANCELLED_MARKER, state: "done" },
+ ],
+ };
+ }
+
+ it("keeps the partial and its marker when the refetch has no reply for the turn", () => {
+ const result = runForceHydrate({
+ prev: [seqMessage(1), stoppedAssistant()],
+ staleWindow: [],
+ freshWindow: [seqMessage(1)],
+ });
+
+ expect(result).toEqual([seqMessage(1), stoppedAssistant()]);
+ });
+
+ it("defers to the database once it carries the stopped turn", () => {
+ const persisted: Messages = [
+ seqMessage(1),
+ {
+ id: `${SESSION_ID}-seq-2`,
+ role: "assistant",
+ parts: [
+ {
+ type: "text",
+ text: `Half an answer ${CANCELLED_MARKER}`,
+ state: "done",
+ },
+ ],
+ },
+ ];
+
+ expect(
+ runForceHydrate({
+ prev: [seqMessage(1), stoppedAssistant()],
+ staleWindow: [],
+ freshWindow: persisted,
+ }),
+ ).toEqual(persisted);
+ });
+});
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotStop.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotStop.ts
index c8220657c2..a54f5b4a14 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotStop.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotStop.ts
@@ -11,7 +11,7 @@ import { resolveInProgressTools } from "./helpers";
* reload shows the same state. Must match `COPILOT_ERROR_PREFIX` in
* `ChatMessagesContainer/helpers.ts`.
*/
-const CANCELLED_MARKER = "[__COPILOT_ERROR_f7a1__] Operation cancelled";
+export const CANCELLED_MARKER = "[__COPILOT_ERROR_f7a1__] Operation cancelled";
interface UseCopilotStopArgs {
sessionId: string | null;
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/useHydrateOnStreamEnd.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/useHydrateOnStreamEnd.ts
index d340c23d85..37ea0de6e8 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/useHydrateOnStreamEnd.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/useHydrateOnStreamEnd.ts
@@ -8,6 +8,7 @@ import {
resolveInterruptedMessage,
} from "./helpers";
import { extractDbSequence } from "./helpers/convertChatSessionToUiMessages";
+import { CANCELLED_MARKER } from "./useCopilotStop";
const PROMOTED_BUBBLE_ID_PREFIX = "promoted-";
@@ -92,6 +93,27 @@ function preservePromotedUserBubbles(
return [...hydrated, ...orphans];
}
+/**
+ * A stopped turn is finalised client-side — ``useCopilotStop`` appends the
+ * cancellation marker — and the backend persists nothing for it, so a blind
+ * force-replace drops the partial answer and the marker with it, and the error
+ * banner that marker suppresses takes the stopped card's place. Re-attach the
+ * local bubble while the hydrated view still ends on the user's prompt.
+ */
+function preserveStoppedAssistantMessage(
+ prev: UIMessage[],
+ hydrated: UIMessage[],
+): UIMessage[] {
+ const stopped = prev[prev.length - 1];
+ if (stopped?.role !== "assistant") return hydrated;
+ const isStopped = stopped.parts.some(
+ (part) => part.type === "text" && part.text.includes(CANCELLED_MARKER),
+ );
+ if (!isStopped) return hydrated;
+ if (hydrated[hydrated.length - 1]?.role !== "user") return hydrated;
+ return [...hydrated, stopped];
+}
+
type ChatStatus = "submitted" | "streaming" | "ready" | "error";
// Survive remount on session re-entry: useRef would reset every time
@@ -249,7 +271,13 @@ export function useHydrateOnStreamEnd({
// lands once the backend goes idle, so no timeout fallback is needed.
if (hasActiveStream) return;
setMessages((prev) =>
- preservePromotedUserBubbles(prev, retainOlderHistory(prev, finalized)),
+ preservePromotedUserBubbles(
+ prev,
+ preserveStoppedAssistantMessage(
+ prev,
+ retainOlderHistory(prev, finalized),
+ ),
+ ),
);
needsForceHydrateRef.current = false;
staleRefAtStreamEnd.current = null;