'use client'; import { useEffect, useState } from 'react'; import { setCanChangeKeys, setLLMConfig } from '@/store/slices/userConfig'; import { hasValidLLMConfig } from '@/utils/storeHelpers'; import { usePathname, useRouter } from 'next/navigation'; import { useDispatch } from 'react-redux'; import { checkIfSelectedOllamaModelIsPulled } from '@/utils/providerUtils'; import { LLMConfig } from '@/types/llm_config'; export function ConfigurationInitializer({ children }: { children: React.ReactNode }) { const dispatch = useDispatch(); const [isLoading, setIsLoading] = useState(true); const router = useRouter(); const route = usePathname(); // Fetch user config state useEffect(() => { fetchUserConfigState(); }, []); const setLoadingToFalseAfterNavigatingTo = (pathname: string) => { const interval = setInterval(() => { if (window.location.pathname === pathname) { clearInterval(interval); setIsLoading(false); } }, 500); } const fetchUserConfigState = async () => { setIsLoading(true); const response = await fetch('/api/can-change-keys'); const canChangeKeys = (await response.json()).canChange; dispatch(setCanChangeKeys(canChangeKeys)); if (canChangeKeys) { const response = await fetch('/api/user-config'); const llmConfig = await response.json(); if (!llmConfig.LLM) { llmConfig.LLM = 'openai'; } dispatch(setLLMConfig(llmConfig)); const isValid = hasValidLLMConfig(llmConfig); if (isValid) { // Check if the selected Ollama model is pulled if (llmConfig.LLM === 'ollama') { const isPulled = await checkIfSelectedOllamaModelIsPulled(llmConfig.OLLAMA_MODEL); if (!isPulled) { router.push('/'); setLoadingToFalseAfterNavigatingTo('/'); return; } } if (llmConfig.LLM === 'custom') { const isAvailable = await checkIfSelectedCustomModelIsAvailable(llmConfig); if (!isAvailable) { router.push('/'); setLoadingToFalseAfterNavigatingTo('/'); return; } } if (route === '/') { router.push('/dashboard'); setLoadingToFalseAfterNavigatingTo('/dashboard'); } else { setIsLoading(false); } } else if (route !== '/') { router.push('/'); setLoadingToFalseAfterNavigatingTo('/'); } else { setIsLoading(false); } } else { if (route === '/') { router.push('/dashboard'); setLoadingToFalseAfterNavigatingTo('/dashboard'); } else { setIsLoading(false); } } } const checkIfSelectedCustomModelIsAvailable = async (llmConfig: LLMConfig) => { try { const response = await fetch('/api/v1/ppt/openai/models/available', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ url: llmConfig.CUSTOM_LLM_URL, api_key: llmConfig.CUSTOM_LLM_API_KEY, }), }); const data = await response.json(); return data.includes(llmConfig.CUSTOM_MODEL); } catch (error) { console.error('Error fetching custom models:', error); return false; } } if (isLoading) { return (
{/* Logo/Branding */}

Oliver DeckForge

{/* Loading Text */}

Initializing Application

Loading configuration and checking model availability...

{/* Progress Indicator */}
); } return children; }