diff --git a/apps/webclaw/src/screens/chat/chat-screen.tsx b/apps/webclaw/src/screens/chat/chat-screen.tsx index 6c4fac7..bcebbbe 100644 --- a/apps/webclaw/src/screens/chat/chat-screen.tsx +++ b/apps/webclaw/src/screens/chat/chat-screen.tsx @@ -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(() => ({}), []) + 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, diff --git a/apps/webclaw/src/screens/chat/hooks/use-chat-history.ts b/apps/webclaw/src/screens/chat/hooks/use-chat-history.ts index 2c6dfdd..0a0aff7 100644 --- a/apps/webclaw/src/screens/chat/hooks/use-chat-history.ts +++ b/apps/webclaw/src/screens/chat/hooks/use-chat-history.ts @@ -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) }, diff --git a/apps/webclaw/src/screens/chat/hooks/use-chat-redirect.ts b/apps/webclaw/src/screens/chat/hooks/use-chat-redirect.ts index 6995dd9..9c4c736 100644 --- a/apps/webclaw/src/screens/chat/hooks/use-chat-redirect.ts +++ b/apps/webclaw/src/screens/chat/hooks/use-chat-redirect.ts @@ -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, diff --git a/apps/webclaw/src/screens/chat/utils.ts b/apps/webclaw/src/screens/chat/utils.ts index 48748c0..7fec5d4 100644 --- a/apps/webclaw/src/screens/chat/utils.ts +++ b/apps/webclaw/src/screens/chat/utils.ts @@ -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 +}