From 349b1f9c79ae0fc611b891f1c42f071524c6ff5a Mon Sep 17 00:00:00 2001 From: Lluis Agusti Date: Wed, 28 Jan 2026 02:53:45 +0700 Subject: [PATCH] hotfix(frontend): copilot session handling refinements... --- .../components/CopilotShell/CopilotShell.tsx | 17 +++++++- .../CopilotShell/useCopilotShell.ts | 4 +- .../CopilotShell/useShellSessionList.ts | 10 ++--- .../(platform)/copilot/copilot-page-store.ts | 7 ++++ .../src/app/(platform)/copilot/page.tsx | 15 ------- .../app/(platform)/copilot/useCopilotPage.ts | 41 +++++++++++++++---- .../contextual/Chat/useChatSession.ts | 1 + .../src/services/storage/session-storage.ts | 1 + 8 files changed, 65 insertions(+), 31 deletions(-) diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/CopilotShell/CopilotShell.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/CopilotShell/CopilotShell.tsx index fe2f4f3625..3f695da5ed 100644 --- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/CopilotShell/CopilotShell.tsx +++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/CopilotShell/CopilotShell.tsx @@ -1,6 +1,7 @@ "use client"; import { ChatLoader } from "@/components/contextual/Chat/components/ChatLoader/ChatLoader"; +import { Text } from "@/components/atoms/Text/Text"; import { NAVBAR_HEIGHT_PX } from "@/lib/constants"; import type { ReactNode } from "react"; import { DesktopSidebar } from "./components/DesktopSidebar/DesktopSidebar"; @@ -17,6 +18,7 @@ export function CopilotShell({ children }: Props) { isMobile, isDrawerOpen, isLoading, + isCreatingSession, isLoggedIn, hasActiveSession, sessions, @@ -60,7 +62,20 @@ export function CopilotShell({ children }: Props) {
{isMobile && } -
{children}
+
+ {isCreatingSession ? ( +
+
+ + + Creating your chat... + +
+
+ ) : ( + children + )} +
{isMobile && ( diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/CopilotShell/useCopilotShell.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/CopilotShell/useCopilotShell.ts index 8429b75b2b..74fd663ab2 100644 --- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/CopilotShell/useCopilotShell.ts +++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/CopilotShell/useCopilotShell.ts @@ -72,6 +72,7 @@ export function useCopilotShell() { const stopStream = useChatStore((s) => s.stopStream); const onStreamComplete = useChatStore((s) => s.onStreamComplete); const isStreaming = useCopilotStore((s) => s.isStreaming); + const isCreatingSession = useCopilotStore((s) => s.isCreatingSession); const setIsSwitchingSession = useCopilotStore((s) => s.setIsSwitchingSession); const openInterruptModal = useCopilotStore((s) => s.openInterruptModal); @@ -154,7 +155,8 @@ export function useCopilotShell() { isLoggedIn, hasActiveSession: Boolean(currentSessionId) && (!isOnHomepage || Boolean(paramSessionId)), - isLoading, + isLoading: isLoading || isCreatingSession, + isCreatingSession, sessions, currentSessionId: urlSessionId, handleOpenDrawer, diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/CopilotShell/useShellSessionList.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/CopilotShell/useShellSessionList.ts index 30e2b6aba1..fb39a11096 100644 --- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/CopilotShell/useShellSessionList.ts +++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/CopilotShell/useShellSessionList.ts @@ -75,12 +75,10 @@ export function useShellSessionList({ }, [accumulatedSessions]); useEffect(() => { - const unsubscribe = onStreamComplete((completedSessionId) => { - if (recentlyCreatedSessionsRef.current.has(completedSessionId)) { - queryClient.invalidateQueries({ - queryKey: getGetV2ListSessionsQueryKey(), - }); - } + const unsubscribe = onStreamComplete(() => { + queryClient.invalidateQueries({ + queryKey: getGetV2ListSessionsQueryKey(), + }); }); return unsubscribe; }, [onStreamComplete, queryClient]); diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/copilot-page-store.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/copilot-page-store.ts index 201c8824c1..9fc97a14e3 100644 --- a/autogpt_platform/frontend/src/app/(platform)/copilot/copilot-page-store.ts +++ b/autogpt_platform/frontend/src/app/(platform)/copilot/copilot-page-store.ts @@ -5,6 +5,7 @@ import { create } from "zustand"; interface CopilotStoreState { isStreaming: boolean; isSwitchingSession: boolean; + isCreatingSession: boolean; isInterruptModalOpen: boolean; pendingAction: (() => void) | null; } @@ -12,6 +13,7 @@ interface CopilotStoreState { interface CopilotStoreActions { setIsStreaming: (isStreaming: boolean) => void; setIsSwitchingSession: (isSwitchingSession: boolean) => void; + setIsCreatingSession: (isCreating: boolean) => void; openInterruptModal: (onConfirm: () => void) => void; confirmInterrupt: () => void; cancelInterrupt: () => void; @@ -22,6 +24,7 @@ type CopilotStore = CopilotStoreState & CopilotStoreActions; export const useCopilotStore = create((set, get) => ({ isStreaming: false, isSwitchingSession: false, + isCreatingSession: false, isInterruptModalOpen: false, pendingAction: null, @@ -33,6 +36,10 @@ export const useCopilotStore = create((set, get) => ({ set({ isSwitchingSession }); }, + setIsCreatingSession(isCreatingSession) { + set({ isCreatingSession }); + }, + openInterruptModal(onConfirm) { set({ isInterruptModalOpen: true, pendingAction: onConfirm }); }, diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/page.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/page.tsx index dfc531557f..104b238895 100644 --- a/autogpt_platform/frontend/src/app/(platform)/copilot/page.tsx +++ b/autogpt_platform/frontend/src/app/(platform)/copilot/page.tsx @@ -5,7 +5,6 @@ import { Skeleton } from "@/components/atoms/Skeleton/Skeleton"; import { Text } from "@/components/atoms/Text/Text"; import { Chat } from "@/components/contextual/Chat/Chat"; import { ChatInput } from "@/components/contextual/Chat/components/ChatInput/ChatInput"; -import { ChatLoader } from "@/components/contextual/Chat/components/ChatLoader/ChatLoader"; import { Dialog } from "@/components/molecules/Dialog/Dialog"; import { useCopilotStore } from "./copilot-page-store"; import { useCopilotPage } from "./useCopilotPage"; @@ -19,7 +18,6 @@ export default function CopilotPage() { greetingName, quickActions, isLoading, - isCreating, hasSession, initialPrompt, isReady, @@ -82,19 +80,6 @@ export default function CopilotPage() { ); } - if (isCreating) { - return ( -
-
- - - Creating your chat... - -
-
- ); - } - return (
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPage.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPage.ts index 35401d4415..38796946f4 100644 --- a/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPage.ts +++ b/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPage.ts @@ -10,11 +10,12 @@ import { type FlagValues, useGetFlag, } from "@/services/feature-flags/use-get-flag"; +import { SessionKey, sessionStorage } from "@/services/storage/session-storage"; import * as Sentry from "@sentry/nextjs"; import { useQueryClient } from "@tanstack/react-query"; import { useFlags } from "launchdarkly-react-client-sdk"; import { useRouter } from "next/navigation"; -import { useEffect, useRef, useState } from "react"; +import { useEffect } from "react"; import { useCopilotStore } from "./copilot-page-store"; import { getGreetingName, getQuickActions } from "./helpers"; import { useCopilotSessionId } from "./useCopilotSessionId"; @@ -27,6 +28,8 @@ export function useCopilotPage() { const { urlSessionId, setUrlSessionId } = useCopilotSessionId(); const setIsStreaming = useCopilotStore((s) => s.setIsStreaming); + const isCreating = useCopilotStore((s) => s.isCreatingSession); + const setIsCreating = useCopilotStore((s) => s.setIsCreatingSession); const isChatEnabled = useGetFlag(Flag.CHAT); const flags = useFlags(); @@ -37,15 +40,12 @@ export function useCopilotPage() { const isFlagReady = !isLaunchDarklyConfigured || flags[Flag.CHAT] !== undefined; - const [isCreating, setIsCreating] = useState(false); - const initialPromptsRef = useRef>({}); - const greetingName = getGreetingName(user); const quickActions = getQuickActions(); const hasSession = Boolean(urlSessionId); const initialPrompt = urlSessionId - ? initialPromptsRef.current[urlSessionId] + ? getInitialPrompt(urlSessionId) : undefined; useEffect(() => { @@ -72,13 +72,13 @@ export function useCopilotPage() { } const sessionId = sessionResponse.data.id; - initialPromptsRef.current[sessionId] = trimmedPrompt; + setInitialPrompt(sessionId, trimmedPrompt); await queryClient.invalidateQueries({ queryKey: getGetV2ListSessionsQueryKey(), }); - await setUrlSessionId(sessionId, { shallow: false }); + await setUrlSessionId(sessionId, { shallow: true }); } catch (error) { console.error("[CopilotPage] Failed to start chat:", error); toast({ title: "Failed to start chat", variant: "destructive" }); @@ -105,7 +105,6 @@ export function useCopilotPage() { greetingName, quickActions, isLoading: isUserLoading, - isCreating, hasSession, initialPrompt, isReady: isFlagReady && isChatEnabled !== false && isLoggedIn, @@ -118,3 +117,29 @@ export function useCopilotPage() { }, }; } + +function getInitialPrompt(sessionId: string): string | undefined { + try { + const prompts = JSON.parse( + sessionStorage.get(SessionKey.CHAT_INITIAL_PROMPTS) || "{}", + ); + return prompts[sessionId]; + } catch { + return undefined; + } +} + +function setInitialPrompt(sessionId: string, prompt: string): void { + try { + const prompts = JSON.parse( + sessionStorage.get(SessionKey.CHAT_INITIAL_PROMPTS) || "{}", + ); + prompts[sessionId] = prompt; + sessionStorage.set( + SessionKey.CHAT_INITIAL_PROMPTS, + JSON.stringify(prompts), + ); + } catch { + // Ignore storage errors + } +} diff --git a/autogpt_platform/frontend/src/components/contextual/Chat/useChatSession.ts b/autogpt_platform/frontend/src/components/contextual/Chat/useChatSession.ts index 553e348f79..e02a15605b 100644 --- a/autogpt_platform/frontend/src/components/contextual/Chat/useChatSession.ts +++ b/autogpt_platform/frontend/src/components/contextual/Chat/useChatSession.ts @@ -58,6 +58,7 @@ export function useChatSession({ query: { enabled: !!sessionId, select: okData, + staleTime: 0, retry: shouldRetrySessionLoad, retryDelay: getSessionRetryDelay, }, diff --git a/autogpt_platform/frontend/src/services/storage/session-storage.ts b/autogpt_platform/frontend/src/services/storage/session-storage.ts index 8404da571c..1be82c98fb 100644 --- a/autogpt_platform/frontend/src/services/storage/session-storage.ts +++ b/autogpt_platform/frontend/src/services/storage/session-storage.ts @@ -3,6 +3,7 @@ import { environment } from "../environment"; export enum SessionKey { CHAT_SENT_INITIAL_PROMPTS = "chat_sent_initial_prompts", + CHAT_INITIAL_PROMPTS = "chat_initial_prompts", } function get(key: SessionKey) {