MCPcopy Create free account
hub / github.com/CodeFox-Repo/codefox / useModels

Function useModels

frontend/src/hooks/useModels.ts:14–86  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

12}
13const CACHE_DURATION = 30 * 60 * 1000;
14export const useModels = () => {
15 const { isAuthorized } = useAuthContext();
16 const [selectedModel, setSelectedModel] = useState<string | undefined>(
17 undefined
18 );
19
20 const shouldUpdateCache = (): boolean => {
21 try {
22 const cachedData = sessionStorage.getItem(LocalStore.models);
23 if (!cachedData) return true;
24 const { lastUpdate } = JSON.parse(cachedData) as ModelsCache;
25 const now = Date.now();
26 return now - lastUpdate > CACHE_DURATION;
27 } catch {
28 return true;
29 }
30 };
31
32 const getCachedModels = (): string[] => {
33 try {
34 const cachedData = sessionStorage.getItem(LocalStore.models);
35 if (!cachedData) return [];
36 const { models } = JSON.parse(cachedData) as ModelsCache;
37 return models;
38 } catch {
39 return [];
40 }
41 };
42
43 const updateCache = (models: string[]) => {
44 const cacheData: ModelsCache = {
45 models,
46 lastUpdate: Date.now(),
47 };
48 sessionStorage.setItem(LocalStore.models, JSON.stringify(cacheData));
49 };
50
51 const { data, loading, error } = useQuery<{
52 getAvailableModelTags: string[];
53 }>(GET_MODEL_TAGS, {
54 skip: !isAuthorized || !shouldUpdateCache(),
55 onCompleted: (data) => {
56 logger.info(data);
57 if (data?.getAvailableModelTags) {
58 updateCache(data.getAvailableModelTags);
59 }
60 },
61 });
62
63 if (error) {
64 logger.info(error);
65 toast.error('Failed to load models');
66 }
67
68 const currentModels = !shouldUpdateCache()
69 ? getCachedModels()
70 : data?.getAvailableModelTags || getCachedModels();
71

Callers 2

ChatFunction · 0.90
prompt-form.tsxFile · 0.90

Calls 4

useAuthContextFunction · 0.90
shouldUpdateCacheFunction · 0.85
updateCacheFunction · 0.85
getCachedModelsFunction · 0.85

Tested by

no test coverage detected