mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-01-15 23:15:30 +01:00
🌡️ 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:
parent
cf393b1308
commit
6ea2628b56
12 changed files with 81 additions and 14 deletions
48
client/src/data-provider/connection.ts
Normal file
48
client/src/data-provider/connection.ts
Normal 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;
|
||||
};
|
||||
|
|
@ -1,3 +1,4 @@
|
|||
export * from './connection';
|
||||
export * from './mutations';
|
||||
export * from './prompts';
|
||||
export * from './queries';
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue