mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-07-14 22:52:15 +02:00
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:
parent
5b5e95971e
commit
de4507f413
1 changed files with 9 additions and 0 deletions
|
|
@ -72,6 +72,7 @@ import { useThreadDetail, useThreadMessages } from "@/hooks/use-thread-queries";
|
||||||
import { getAgentFilesystemSelection } from "@/lib/agent-filesystem";
|
import { getAgentFilesystemSelection } from "@/lib/agent-filesystem";
|
||||||
import { documentsApiService } from "@/lib/apis/documents-api.service";
|
import { documentsApiService } from "@/lib/apis/documents-api.service";
|
||||||
import { getDesktopAccessToken } from "@/lib/auth-fetch";
|
import { getDesktopAccessToken } from "@/lib/auth-fetch";
|
||||||
|
import { refreshSession } from "@/lib/auth-utils";
|
||||||
import { type ChatFlow, classifyChatError } from "@/lib/chat/chat-error-classifier";
|
import { type ChatFlow, classifyChatError } from "@/lib/chat/chat-error-classifier";
|
||||||
import { tagPreAcceptSendFailure, toHttpResponseError } from "@/lib/chat/chat-request-errors";
|
import { tagPreAcceptSendFailure, toHttpResponseError } from "@/lib/chat/chat-request-errors";
|
||||||
import { getMentionDocKey } from "@/lib/chat/mention-doc-key";
|
import { getMentionDocKey } from "@/lib/chat/mention-doc-key";
|
||||||
|
|
@ -688,11 +689,19 @@ export default function NewChatPage() {
|
||||||
|
|
||||||
const fetchWithTurnCancellingRetry = useCallback(async (runFetch: () => Promise<Response>) => {
|
const fetchWithTurnCancellingRetry = useCallback(async (runFetch: () => Promise<Response>) => {
|
||||||
const maxAttempts = 4;
|
const maxAttempts = 4;
|
||||||
|
let didRefreshAuth = false;
|
||||||
for (let attempt = 1; attempt <= maxAttempts; attempt += 1) {
|
for (let attempt = 1; attempt <= maxAttempts; attempt += 1) {
|
||||||
const response = await runFetch();
|
const response = await runFetch();
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
if (response.status === 401 && !didRefreshAuth) {
|
||||||
|
didRefreshAuth = true;
|
||||||
|
const refreshed = await refreshSession();
|
||||||
|
if (refreshed) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
const error = await toHttpResponseError(response);
|
const error = await toHttpResponseError(response);
|
||||||
const withMeta = error as Error & { errorCode?: string; retryAfterMs?: number };
|
const withMeta = error as Error & { errorCode?: string; retryAfterMs?: number };
|
||||||
const isTurnCancelling = withMeta.errorCode === "TURN_CANCELLING";
|
const isTurnCancelling = withMeta.errorCode === "TURN_CANCELLING";
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue