🌡️ feat: Periodic Health Check to prevent UI Inactivity Connection Errors (#3589)

* 🌡️ feat: Periodic Health Check to prevent UI Inactivity Connection Errors

* feat: Add refetchOnWindowFocus option for health check

* feat: programmatically scroll to end when a chat request is initiated (and messages have rendered)
This commit is contained in:
Danny Avila 2024-08-08 14:52:12 -04:00 committed by GitHub
parent cf393b1308
commit 6ea2628b56
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 81 additions and 14 deletions

View file

@ -0,0 +1,48 @@
import { useCallback, useRef } from 'react';
import { useQuery, useQueryClient } from '@tanstack/react-query';
import { QueryKeys, Time, dataService } from 'librechat-data-provider';
import { logger } from '~/utils';
export const useHealthCheck = () => {
useQuery([QueryKeys.health], () => dataService.healthCheck(), {
refetchInterval: Time.TEN_MINUTES,
retry: false,
onError: (error) => {
console.error('Health check failed:', error);
},
cacheTime: 0,
staleTime: 0,
refetchOnWindowFocus: (query) => {
if (!query.state.dataUpdatedAt) {
return true;
}
const lastUpdated = new Date(query.state.dataUpdatedAt);
const tenMinutesAgo = new Date(Date.now() - Time.TEN_MINUTES);
logger.log(`Last health check: ${lastUpdated.toISOString()}`);
logger.log(`Ten minutes ago: ${tenMinutesAgo.toISOString()}`);
return lastUpdated < tenMinutesAgo;
},
});
};
export const useInteractionHealthCheck = () => {
const queryClient = useQueryClient();
const lastInteractionTimeRef = useRef(Date.now());
const checkHealthOnInteraction = useCallback(() => {
const currentTime = Date.now();
if (currentTime - lastInteractionTimeRef.current > Time.FIVE_MINUTES) {
logger.log(
'Checking health on interaction. Time elapsed:',
currentTime - lastInteractionTimeRef.current,
);
queryClient.invalidateQueries([QueryKeys.health]);
lastInteractionTimeRef.current = currentTime;
}
}, [queryClient]);
return checkHealthOnInteraction;
};

View file

@ -1,3 +1,4 @@
export * from './connection';
export * from './mutations';
export * from './prompts';
export * from './queries';