mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-19 18:00:15 +01:00
💽 refactor(client): Optimize ModelsConfig Query Cache (#2330)
* refactor(client): remove double caching of models via recoil to rely exclusively on react-query * chore(useConversation): add modelsQuery.data dep to callback
This commit is contained in:
parent
fb80af05be
commit
f6a84887e1
11 changed files with 76 additions and 94 deletions
|
|
@ -14,7 +14,6 @@ type TempOverrideType = Record<string, unknown> & {
|
|||
};
|
||||
|
||||
export default function useConfigOverride() {
|
||||
const setModelsConfig = useSetRecoilState(store.modelsConfig);
|
||||
const setEndpointsQueryEnabled = useSetRecoilState(store.endpointsQueryEnabled);
|
||||
const overrideQuery = useGetEndpointsConfigOverride({
|
||||
staleTime: Infinity,
|
||||
|
|
@ -33,10 +32,9 @@ export default function useConfigOverride() {
|
|||
if (modelsConfig) {
|
||||
await queryClient.cancelQueries([QueryKeys.models]);
|
||||
queryClient.setQueryData([QueryKeys.models], modelsConfig);
|
||||
setModelsConfig(modelsConfig);
|
||||
}
|
||||
},
|
||||
[queryClient, setEndpointsQueryEnabled, setModelsConfig],
|
||||
[queryClient, setEndpointsQueryEnabled],
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import { useCallback } from 'react';
|
||||
import { useSetRecoilState, useResetRecoilState, useRecoilCallback } from 'recoil';
|
||||
import { useGetEndpointsQuery } from 'librechat-data-provider/react-query';
|
||||
import { useGetEndpointsQuery, useGetModelsQuery } from 'librechat-data-provider/react-query';
|
||||
import type {
|
||||
TConversation,
|
||||
TMessagesAtom,
|
||||
|
|
@ -16,20 +16,21 @@ import store from '~/store';
|
|||
const useConversation = () => {
|
||||
const navigate = useOriginNavigate();
|
||||
const setConversation = useSetRecoilState(store.conversation);
|
||||
const resetLatestMessage = useResetRecoilState(store.latestMessage);
|
||||
const setMessages = useSetRecoilState<TMessagesAtom>(store.messages);
|
||||
const setSubmission = useSetRecoilState<TSubmission | null>(store.submission);
|
||||
const resetLatestMessage = useResetRecoilState(store.latestMessage);
|
||||
const { data: endpointsConfig = {} as TEndpointsConfig } = useGetEndpointsQuery();
|
||||
const modelsQuery = useGetModelsQuery();
|
||||
|
||||
const switchToConversation = useRecoilCallback(
|
||||
({ snapshot }) =>
|
||||
() =>
|
||||
async (
|
||||
conversation: TConversation,
|
||||
messages: TMessagesAtom = null,
|
||||
preset: TPreset | null = null,
|
||||
modelsData?: TModelsConfig,
|
||||
) => {
|
||||
const modelsConfig = modelsData ?? snapshot.getLoadable(store.modelsConfig).contents;
|
||||
const modelsConfig = modelsData ?? modelsQuery.data;
|
||||
const { endpoint = null } = conversation;
|
||||
|
||||
if (endpoint === null) {
|
||||
|
|
@ -61,7 +62,7 @@ const useConversation = () => {
|
|||
navigate('new');
|
||||
}
|
||||
},
|
||||
[endpointsConfig],
|
||||
[endpointsConfig, modelsQuery.data],
|
||||
);
|
||||
|
||||
const newConversation = useCallback(
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import { useCallback } from 'react';
|
||||
import { EModelEndpoint, FileSources, defaultOrderQuery } from 'librechat-data-provider';
|
||||
import { useGetEndpointsQuery } from 'librechat-data-provider/react-query';
|
||||
import { useGetEndpointsQuery, useGetModelsQuery } from 'librechat-data-provider/react-query';
|
||||
import {
|
||||
useSetRecoilState,
|
||||
useResetRecoilState,
|
||||
|
|
@ -35,6 +35,7 @@ const useNewConvo = (index = 0) => {
|
|||
const setSubmission = useSetRecoilState<TSubmission | null>(store.submissionByIndex(index));
|
||||
const resetLatestMessage = useResetRecoilState(store.latestMessageFamily(index));
|
||||
const { data: endpointsConfig = {} as TEndpointsConfig } = useGetEndpointsQuery();
|
||||
const modelsQuery = useGetModelsQuery();
|
||||
|
||||
const { data: assistants = [] } = useListAssistantsQuery(defaultOrderQuery, {
|
||||
select: (res) =>
|
||||
|
|
@ -51,7 +52,7 @@ const useNewConvo = (index = 0) => {
|
|||
});
|
||||
|
||||
const switchToConversation = useRecoilCallback(
|
||||
({ snapshot }) =>
|
||||
() =>
|
||||
async (
|
||||
conversation: TConversation,
|
||||
preset: Partial<TPreset> | null = null,
|
||||
|
|
@ -59,7 +60,7 @@ const useNewConvo = (index = 0) => {
|
|||
buildDefault?: boolean,
|
||||
keepLatestMessage?: boolean,
|
||||
) => {
|
||||
const modelsConfig = modelsData ?? snapshot.getLoadable(store.modelsConfig).contents;
|
||||
const modelsConfig = modelsData ?? modelsQuery.data;
|
||||
const { endpoint = null } = conversation;
|
||||
const buildDefaultConversation = endpoint === null || buildDefault;
|
||||
const activePreset =
|
||||
|
|
@ -137,7 +138,7 @@ const useNewConvo = (index = 0) => {
|
|||
navigate('new');
|
||||
}
|
||||
},
|
||||
[endpointsConfig, defaultPreset, assistants],
|
||||
[endpointsConfig, defaultPreset, assistants, modelsQuery.data],
|
||||
);
|
||||
|
||||
const newConversation = useCallback(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue