From de4507f4139709237cbff144b8edeac5d866a47e Mon Sep 17 00:00:00 2001 From: Anish Sarkar <104695310+AnishSarkar22@users.noreply.github.com> Date: Fri, 26 Jun 2026 21:00:55 +0530 Subject: [PATCH] feat: enhance session management in chat page - Implemented session refresh logic in the fetchWithTurnCancellingRetry function to handle 401 errors more gracefully. - Added a new import for refreshSession utility to facilitate session renewal. --- .../[search_space_id]/new-chat/[[...chat_id]]/page.tsx | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/surfsense_web/app/dashboard/[search_space_id]/new-chat/[[...chat_id]]/page.tsx b/surfsense_web/app/dashboard/[search_space_id]/new-chat/[[...chat_id]]/page.tsx index 9c3a7c617..407a9c53b 100644 --- a/surfsense_web/app/dashboard/[search_space_id]/new-chat/[[...chat_id]]/page.tsx +++ b/surfsense_web/app/dashboard/[search_space_id]/new-chat/[[...chat_id]]/page.tsx @@ -72,6 +72,7 @@ import { useThreadDetail, useThreadMessages } from "@/hooks/use-thread-queries"; import { getAgentFilesystemSelection } from "@/lib/agent-filesystem"; import { documentsApiService } from "@/lib/apis/documents-api.service"; import { getDesktopAccessToken } from "@/lib/auth-fetch"; +import { refreshSession } from "@/lib/auth-utils"; import { type ChatFlow, classifyChatError } from "@/lib/chat/chat-error-classifier"; import { tagPreAcceptSendFailure, toHttpResponseError } from "@/lib/chat/chat-request-errors"; import { getMentionDocKey } from "@/lib/chat/mention-doc-key"; @@ -688,11 +689,19 @@ export default function NewChatPage() { const fetchWithTurnCancellingRetry = useCallback(async (runFetch: () => Promise) => { const maxAttempts = 4; + let didRefreshAuth = false; for (let attempt = 1; attempt <= maxAttempts; attempt += 1) { const response = await runFetch(); if (response.ok) { return response; } + if (response.status === 401 && !didRefreshAuth) { + didRefreshAuth = true; + const refreshed = await refreshSession(); + if (refreshed) { + continue; + } + } const error = await toHttpResponseError(response); const withMeta = error as Error & { errorCode?: string; retryAfterMs?: number }; const isTurnCancelling = withMeta.errorCode === "TURN_CANCELLING";