fix(chat): redirect unknown sessions to /new immediately and disable history retries

This commit is contained in:
ibelick
2026-02-15 10:13:47 +01:00
parent e31a2e0a57
commit 0881bfc607
4 changed files with 19 additions and 10 deletions
@@ -5,6 +5,7 @@ import { useQuery, useQueryClient } from '@tanstack/react-query'
import {
deriveFriendlyIdFromKey,
isMissingGatewayAuth,
isSessionNotFound,
readError,
} from './utils'
import { createOptimisticMessage } from './chat-screen-utils'
@@ -147,16 +148,17 @@ export function ChatScreen({
navigate({ to: '/new', replace: true })
}, [navigate])
const stableContentStyle = useMemo<React.CSSProperties>(() => ({}), [])
const missingSessionError =
isSessionNotFound(historyError ?? '') ||
isSessionNotFound(sessionsError ?? '')
const shouldRedirectToNew =
!isNewChat &&
!forcedSessionKey &&
!isRecentSession(activeFriendlyId) &&
sessionsQuery.isSuccess &&
sessions.length > 0 &&
!sessions.some((session) => session.friendlyId === activeFriendlyId) &&
!historyQuery.isFetching &&
!historyQuery.isSuccess
(missingSessionError || (!historyQuery.isFetching && !historyQuery.isSuccess))
const refreshHistory = useCallback(() => {
void historyQuery.refetch()
@@ -520,7 +522,6 @@ export function ChatScreen({
isRedirecting,
shouldRedirectToNew,
sessionsReady: sessionsQuery.isSuccess,
sessionsCount: sessions.length,
sessionKeyForHistory,
queryClient,
setIsRedirecting,
@@ -27,8 +27,7 @@ export function useChatHistory({
sessionsReady,
queryClient,
}: UseChatHistoryInput) {
const sessionKeyForHistory =
forcedSessionKey || activeSessionKey || activeFriendlyId
const sessionKeyForHistory = forcedSessionKey || activeSessionKey || ''
const historyKey = chatQueryKeys.history(
activeFriendlyId,
sessionKeyForHistory,
@@ -77,6 +76,7 @@ export function useChatHistory({
Boolean(activeFriendlyId) &&
!isRedirecting &&
(!sessionsReady || activeExists),
retry: false,
placeholderData: function useCachedHistory(): HistoryResponse | undefined {
return queryClient.getQueryData(historyKey)
},
@@ -11,7 +11,6 @@ type UseChatRedirectInput = {
isRedirecting: boolean
shouldRedirectToNew: boolean
sessionsReady: boolean
sessionsCount: number
sessionKeyForHistory: string
queryClient: QueryClient
setIsRedirecting: (value: boolean) => void
@@ -23,7 +22,6 @@ export function useChatRedirect({
isRedirecting,
shouldRedirectToNew,
sessionsReady,
sessionsCount,
sessionKeyForHistory,
queryClient,
setIsRedirecting,
@@ -43,7 +41,6 @@ export function useChatRedirect({
if (isNewChat) return
if (!sessionsReady) return
if (sessionsCount === 0) return
if (!shouldRedirectToNew) return
resetPendingSend()
clearHistoryMessages(queryClient, activeFriendlyId, sessionKeyForHistory)
@@ -54,7 +51,6 @@ export function useChatRedirect({
navigate,
queryClient,
sessionKeyForHistory,
sessionsCount,
sessionsReady,
setIsRedirecting,
shouldRedirectToNew,
+12
View File
@@ -130,3 +130,15 @@ export const missingGatewayAuthMessage =
export function isMissingGatewayAuth(message: string): boolean {
return message.includes(missingGatewayAuthMessage)
}
export function isSessionNotFound(message: string): boolean {
if (!message) return false
const normalized = message.toLowerCase()
if (normalized.includes('session not found')) return true
if (normalized.includes('unknown session')) return true
if (normalized.includes('chat not found')) return true
if (normalized.includes('not found') && normalized.includes('session')) {
return true
}
return false
}