diff --git a/surfsense_web/app/dashboard/[search_space_id]/onboard/page.tsx b/surfsense_web/app/dashboard/[search_space_id]/onboard/page.tsx index 960021da3..ac6c2475b 100644 --- a/surfsense_web/app/dashboard/[search_space_id]/onboard/page.tsx +++ b/surfsense_web/app/dashboard/[search_space_id]/onboard/page.tsx @@ -12,8 +12,10 @@ import { OnboardHeader } from "@/components/onboard/onboard-header"; import { OnboardLLMSetup } from "@/components/onboard/onboard-llm-setup"; import { OnboardLoading } from "@/components/onboard/onboard-loading"; import { OnboardStats } from "@/components/onboard/onboard-stats"; -import { useGlobalLLMConfigs, useLLMConfigs, useLLMPreferences } from "@/hooks/use-llm-configs"; +import { useGlobalLLMConfigs, useLLMPreferences } from "@/hooks/use-llm-configs"; import { getBearerToken, redirectToLogin } from "@/lib/auth-utils"; +import { useAtomValue } from "jotai"; +import { llmConfigsAtom } from "@/atoms/llm-config/llm-config-query.atoms"; const OnboardPage = () => { const t = useTranslations("onboard"); @@ -21,7 +23,7 @@ const OnboardPage = () => { const params = useParams(); const searchSpaceId = Number(params.search_space_id); - const { llmConfigs, loading: configsLoading, refreshConfigs } = useLLMConfigs(searchSpaceId); + const { data: llmConfigs = [], isFetching: configsLoading, refetch: refreshConfigs } = useAtomValue(llmConfigsAtom); const { globalConfigs, loading: globalConfigsLoading } = useGlobalLLMConfigs(); const { preferences, @@ -165,8 +167,8 @@ const OnboardPage = () => { ? t("configure_providers_and_assign_roles") : t("complete_role_assignment") } - onConfigCreated={refreshConfigs} - onConfigDeleted={refreshConfigs} + onConfigCreated={() => refreshConfigs()} + onConfigDeleted={() => refreshConfigs()} onPreferencesUpdated={refreshPreferences} /> ); @@ -257,8 +259,8 @@ const OnboardPage = () => { setShowLLMSettings={setShowAdvancedSettings} showPromptSettings={showPromptSettings} setShowPromptSettings={setShowPromptSettings} - onConfigCreated={refreshConfigs} - onConfigDeleted={refreshConfigs} + onConfigCreated={() => refreshConfigs()} + onConfigDeleted={() => refreshConfigs()} onPreferencesUpdated={refreshPreferences} /> diff --git a/surfsense_web/hooks/use-llm-configs.ts b/surfsense_web/hooks/use-llm-configs.ts index 8d0af2aa7..dec7b4ee2 100644 --- a/surfsense_web/hooks/use-llm-configs.ts +++ b/surfsense_web/hooks/use-llm-configs.ts @@ -2,7 +2,6 @@ import { useEffect, useState } from "react"; import { toast } from "sonner"; import { authenticatedFetch } from "@/lib/auth-utils"; -import { UpdateLLMConfigRequest } from "@/contracts/types/llm-config.types"; export interface LLMConfig { id: number; @@ -50,132 +49,6 @@ export interface UpdateLLMConfig { litellm_params?: Record; } -export function useLLMConfigs(searchSpaceId: number | null) { - const [llmConfigs, setLlmConfigs] = useState([]); - const [loading, setLoading] = useState(true); - const [error, setError] = useState(null); - - const fetchLLMConfigs = async () => { - if (!searchSpaceId) { - setLoading(false); - return; - } - - try { - setLoading(true); - const response = await authenticatedFetch( - `${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/llm-configs?search_space_id=${searchSpaceId}`, - { method: "GET" } - ); - - if (!response.ok) { - throw new Error("Failed to fetch LLM configurations"); - } - - const data = await response.json(); - setLlmConfigs(data); - setError(null); - } catch (err: any) { - setError(err.message || "Failed to fetch LLM configurations"); - console.error("Error fetching LLM configurations:", err); - } finally { - setLoading(false); - } - }; - - useEffect(() => { - fetchLLMConfigs(); - }, [searchSpaceId]); - - const createLLMConfig = async (config: CreateLLMConfig): Promise => { - try { - const response = await authenticatedFetch( - `${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/llm-configs`, - { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify(config), - } - ); - - if (!response.ok) { - const errorData = await response.json(); - throw new Error(errorData.detail || "Failed to create LLM configuration"); - } - - const newConfig = await response.json(); - setLlmConfigs((prev) => [...prev, newConfig]); - toast.success("LLM configuration created successfully"); - return newConfig; - } catch (err: any) { - toast.error(err.message || "Failed to create LLM configuration"); - console.error("Error creating LLM configuration:", err); - return null; - } - }; - - const deleteLLMConfig = async (id: number): Promise => { - try { - const response = await authenticatedFetch( - `${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/llm-configs/${id}`, - { method: "DELETE" } - ); - - if (!response.ok) { - throw new Error("Failed to delete LLM configuration"); - } - - setLlmConfigs((prev) => prev.filter((config) => config.id !== id)); - toast.success("LLM configuration deleted successfully"); - return true; - } catch (err: any) { - toast.error(err.message || "Failed to delete LLM configuration"); - console.error("Error deleting LLM configuration:", err); - return false; - } - }; - - const updateLLMConfig = async ( - id: number, - config: UpdateLLMConfigRequest["data"] - ): Promise => { - try { - const response = await authenticatedFetch( - `${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/llm-configs/${id}`, - { - method: "PUT", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify(config), - } - ); - - if (!response.ok) { - const errorData = await response.json(); - throw new Error(errorData.detail || "Failed to update LLM configuration"); - } - - const updatedConfig = await response.json(); - setLlmConfigs((prev) => prev.map((c) => (c.id === id ? updatedConfig : c))); - toast.success("LLM configuration updated successfully"); - return updatedConfig; - } catch (err: any) { - toast.error(err.message || "Failed to update LLM configuration"); - console.error("Error updating LLM configuration:", err); - return null; - } - }; - - return { - llmConfigs, - loading, - error, - createLLMConfig, - updateLLMConfig, - deleteLLMConfig, - refreshConfigs: fetchLLMConfigs, - }; -} - export function useLLMPreferences(searchSpaceId: number | null) { const [preferences, setPreferences] = useState({}); const [loading, setLoading] = useState(true);