From a0f149fcb2e1d8a618823d766464702ecb43591f Mon Sep 17 00:00:00 2001 From: Zamil Majdy Date: Wed, 15 Apr 2026 20:50:13 +0700 Subject: [PATCH] fix(frontend): reset paged state on send to prevent misordering during streaming When a completed session (forwardPaginated=true) has forward-loaded history pages and the user sends a new message, pagedMessages would appear after the new streaming turn instead of before it. Reset pagedRawMessages before calling sendMessage so ordering is always correct during the active stream. Expose resetPaged() from useLoadMoreMessages for this purpose. --- .../src/app/(platform)/copilot/useCopilotPage.ts | 11 ++++++++++- .../app/(platform)/copilot/useLoadMoreMessages.ts | 12 +++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPage.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPage.ts index 0976ae2899..e1f19f08fb 100644 --- a/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPage.ts +++ b/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotPage.ts @@ -86,7 +86,7 @@ export function useCopilotPage() { copilotModel: isModeToggleEnabled ? copilotLlmModel : undefined, }); - const { pagedMessages, hasMore, isLoadingMore, loadMore } = + const { pagedMessages, hasMore, isLoadingMore, loadMore, resetPaged } = useLoadMoreMessages({ sessionId, initialOldestSequence: oldestSequence, @@ -261,6 +261,15 @@ export function useCopilotPage() { isUserStoppingRef.current = false; if (sessionId) { + // When continuing a completed session that had forward-paginated history + // loaded, the paged messages would appear in wrong position relative to + // the new streaming turn (pagedMessages are newer pages, so they'd end + // up after the streaming turn). Reset paged state so ordering is correct + // during streaming; the user can reload history afterward if needed. + if (forwardPaginated && pagedMessages.length > 0) { + resetPaged(); + } + if (files && files.length > 0) { setIsUploadingFiles(true); try { diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/useLoadMoreMessages.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/useLoadMoreMessages.ts index 6db18aea80..f2420efbd0 100644 --- a/autogpt_platform/frontend/src/app/(platform)/copilot/useLoadMoreMessages.ts +++ b/autogpt_platform/frontend/src/app/(platform)/copilot/useLoadMoreMessages.ts @@ -188,5 +188,15 @@ export function useLoadMoreMessages({ } } - return { pagedMessages, hasMore, isLoadingMore, loadMore }; + function resetPaged() { + setPagedRawMessages([]); + setOldestSequence(initialOldestSequence); + setNewestSequence(initialNewestSequence); + setHasMore(initialHasMore); + isLoadingMoreRef.current = false; + consecutiveErrorsRef.current = 0; + epochRef.current += 1; + } + + return { pagedMessages, hasMore, isLoadingMore, loadMore, resetPaged }; }