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): treat a missing session as no pending messages instead of an unhandled rejection (#15336)
Reinier van der Leera7ff851d9fb3
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPendingChips.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPendingChips.test.tsindex d15f18819b..a706cbc319 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPendingChips.test.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPendingChips.test.ts@@ -4,6 +4,7 @@ import { StrictMode } from "react";import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";import { getV2GetPendingMessages } from "@/app/api/__generated__/endpoints/chat/chat";+import { ApiError } from "@/lib/autogpt-server-api/helpers";import { splitMessagesAtDrainHints } from "./components/ChatMessagesContainer/midTurnSplit";import { useCopilotPendingChips } from "./useCopilotPendingChips";@@ -922,4 +923,61 @@ describe("useCopilotPendingChips", () => {);});});++ describe("a peek the backend refuses", () => {+ it("treats a missing session as an empty buffer, not an unhandled rejection", async () => {+ mockGetPending.mockRejectedValue(+ new ApiError("Session s1 not found.", 404, {+ detail: "Session s1 not found.",+ }),+ );++ const { rendered, reasons } = await unhandledRejectionsDuring(() =>+ renderHook(() =>+ useCopilotPendingChips({+ sessionId: "s1",+ status: "ready",+ messages: [],+ setMessages: vi.fn(),+ }),+ ),+ );++ expect(mockGetPending).toHaveBeenCalledWith("s1");+ expect(rendered.result.current.queuedMessages).toEqual([]);+ expect(reasons).toEqual([]);+ });++ it("still surfaces a peek that fails for any other reason", async () => {+ const serverError = new ApiError("Internal Server Error", 500, {});+ mockGetPending.mockRejectedValue(serverError);++ const { reasons } = await unhandledRejectionsDuring(() =>+ renderHook(() =>+ useCopilotPendingChips({+ sessionId: "s1",+ status: "ready",+ messages: [],+ setMessages: vi.fn(),+ }),+ ),+ );++ expect(reasons).toEqual([serverError]);+ });+ });});++/** Run `render`, then let Node report every rejection nothing handled. */+async function unhandledRejectionsDuring<T>(render: () => T) {+ const reasons: unknown[] = [];+ const record = (reason: unknown) => reasons.push(reason);+ process.on("unhandledRejection", record);+ try {+ const rendered = render();+ await new Promise((resolve) => setTimeout(resolve, 0));+ return { rendered, reasons };+ } finally {+ process.off("unhandledRejection", record);+ }+}diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPendingChips.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPendingChips.tsindex f96dd6973f..f9c62363d4 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPendingChips.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPendingChips.ts@@ -1,4 +1,5 @@import { getV2GetPendingMessages } from "@/app/api/__generated__/endpoints/chat/chat";+import { ApiError } from "@/lib/autogpt-server-api/helpers";import type { UIMessage } from "ai";import { useCallback, useEffect, useMemo, useRef, useState } from "react";@@ -187,7 +188,7 @@ function usePeekOnBoundary({const inFlightIds = new Set((sessionChanged ? [] : queueRef.current).map((entry) => entry.id),);- void getV2GetPendingMessages(sessionId).then((res) => {+ const peek = getV2GetPendingMessages(sessionId).then((res) => {if (prevSessionIdRef.current !== requestSessionId) return;if (peekSeq !== latestPeekSeqRef.current) return;if (res.status !== 200) return;@@ -245,9 +246,17 @@ function usePeekOnBoundary({return [...fromServer, ...queuedDuringWindow];});});+ void peek.catch(ignoreMissingSession);}, [sessionId, status, queueRef, setQueue]);}+// A deleted chat, or another account's, answers 404: nothing is pending. Any+// other failure is rethrown, so it still reaches Sentry as before.+function ignoreMissingSession(error: unknown) {+ if (error instanceof ApiError && error.status === 404) return;+ throw error;+}+// ── 2. Auto-continue promotion ─────────────────────────────────────────// When the backend auto-continues (a SECOND new assistant ID appears in// the same stream chain), promote drained chips into user bubbles before