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.
This commit is contained in:
Anish Sarkar 2026-06-26 21:00:55 +05:30
parent 5b5e95971e
commit de4507f413

View file

@ -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<Response>) => {
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";