refactor: complete migration of useLLMPreferences hook to jotai atoms

- Migrated llm-role-manager.tsx to use jotai atoms
- Fixed TypeScript errors in llm-role-manager.tsx
- Deleted unused use-llm-configs.ts file
- All LLM preferences now use centralized jotai + tanstack query atoms
This commit is contained in:
CREDO23 2025-12-10 11:34:01 +00:00
parent e59c920625
commit 3e8cdabd73
2 changed files with 29 additions and 161 deletions

View file

@ -27,10 +27,11 @@ import {
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import { useLLMPreferences } from "@/hooks/use-llm-configs";
import { useAtomValue } from "jotai";
import { llmConfigsAtom, globalLLMConfigsAtom } from "@/atoms/llm-config/llm-config-query.atoms";
import { llmConfigsAtom, globalLLMConfigsAtom, llmPreferencesAtom } from "@/atoms/llm-config/llm-config-query.atoms";
import { updateLLMPreferencesMutationAtom } from "@/atoms/llm-config/llm-config-mutation.atoms";
const ROLE_DESCRIPTIONS = {
long_context: {
icon: Brain,
@ -76,12 +77,13 @@ export function LLMRoleManager({ searchSpaceId }: LLMRoleManagerProps) {
refetch: refreshGlobalConfigs,
} = useAtomValue(globalLLMConfigsAtom);
const {
preferences,
loading: preferencesLoading,
data: preferences = {},
isFetching: preferencesLoading,
error: preferencesError,
updatePreferences,
refreshPreferences,
} = useLLMPreferences(searchSpaceId);
refetch: refreshPreferences,
} = useAtomValue(llmPreferencesAtom);
const { mutateAsync: updatePreferences } = useAtomValue(updateLLMPreferencesMutationAtom);
const [assignments, setAssignments] = useState({
long_context_llm_id: preferences.long_context_llm_id || "",
@ -148,14 +150,15 @@ export function LLMRoleManager({ searchSpaceId }: LLMRoleManagerProps) {
? parseInt(assignments.strategic_llm_id)
: undefined
: assignments.strategic_llm_id,
};
};
const success = await updatePreferences(numericAssignments);
await updatePreferences({
search_space_id: searchSpaceId,
data: numericAssignments
});
if (success) {
setHasChanges(false);
toast.success("LLM role assignments saved successfully!");
}
setHasChanges(false);
toast.success("LLM role assignments saved successfully!");
setIsSaving(false);
};
@ -214,12 +217,12 @@ export function LLMRoleManager({ searchSpaceId }: LLMRoleManagerProps) {
<span className="sm:hidden">Configs</span>
</Button>
<Button
variant="outline"
size="sm"
onClick={refreshPreferences}
disabled={isLoading}
className="flex items-center gap-2"
>
variant="outline"
size="sm"
onClick={() => refreshPreferences()}
disabled={isLoading}
className="flex items-center gap-2"
>
<RefreshCw className={`h-4 w-4 ${preferencesLoading ? "animate-spin" : ""}`} />
<span className="hidden sm:inline">Refresh Preferences</span>
<span className="sm:hidden">Prefs</span>
@ -230,11 +233,13 @@ export function LLMRoleManager({ searchSpaceId }: LLMRoleManagerProps) {
{/* Error Alert */}
{hasError && (
<Alert variant="destructive">
<AlertCircle className="h-4 w-4" />
<AlertDescription>
{configsError || preferencesError || globalConfigsError}
</AlertDescription>
</Alert>
<AlertCircle className="h-4 w-4" />
<AlertDescription>
{(configsError && "Failed to load LLM configurations") ||
(preferencesError && "Failed to load preferences") ||
(globalConfigsError && "Failed to load global configurations")}
</AlertDescription>
</Alert>
)}
{/* Loading State */}

View file

@ -1,137 +0,0 @@
"use client";
import { useEffect, useState } from "react";
import { toast } from "sonner";
import { authenticatedFetch } from "@/lib/auth-utils";
export interface LLMConfig {
id: number;
name: string;
provider: string;
custom_provider?: string;
model_name: string;
api_key: string;
api_base?: string;
language?: string;
litellm_params?: Record<string, any>;
created_at?: string;
search_space_id?: number;
is_global?: boolean;
}
export interface LLMPreferences {
long_context_llm_id?: number;
fast_llm_id?: number;
strategic_llm_id?: number;
long_context_llm?: LLMConfig;
fast_llm?: LLMConfig;
strategic_llm?: LLMConfig;
}
export interface CreateLLMConfig {
name: string;
provider: string;
custom_provider?: string;
model_name: string;
api_key: string;
api_base?: string;
language?: string;
litellm_params?: Record<string, any>;
search_space_id: number;
}
export interface UpdateLLMConfig {
name?: string;
provider?: string;
custom_provider?: string;
model_name?: string;
api_key?: string;
api_base?: string;
litellm_params?: Record<string, any>;
}
export function useLLMPreferences(searchSpaceId: number | null) {
const [preferences, setPreferences] = useState<LLMPreferences>({});
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const fetchPreferences = async () => {
if (!searchSpaceId) {
setLoading(false);
return;
}
try {
setLoading(true);
const response = await authenticatedFetch(
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/search-spaces/${searchSpaceId}/llm-preferences`,
{ method: "GET" }
);
if (!response.ok) {
throw new Error("Failed to fetch LLM preferences");
}
const data = await response.json();
setPreferences(data);
setError(null);
} catch (err: any) {
setError(err.message || "Failed to fetch LLM preferences");
console.error("Error fetching LLM preferences:", err);
} finally {
setLoading(false);
}
};
useEffect(() => {
fetchPreferences();
}, [searchSpaceId]);
const updatePreferences = async (newPreferences: Partial<LLMPreferences>): Promise<boolean> => {
if (!searchSpaceId) {
toast.error("Search space ID is required");
return false;
}
try {
const response = await authenticatedFetch(
`${process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL}/api/v1/search-spaces/${searchSpaceId}/llm-preferences`,
{
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(newPreferences),
}
);
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.detail || "Failed to update LLM preferences");
}
const updatedPreferences = await response.json();
setPreferences(updatedPreferences);
toast.success("LLM preferences updated successfully");
return true;
} catch (err: any) {
toast.error(err.message || "Failed to update LLM preferences");
console.error("Error updating LLM preferences:", err);
return false;
}
};
const isOnboardingComplete = (): boolean => {
return !!(
preferences.long_context_llm_id &&
preferences.fast_llm_id &&
preferences.strategic_llm_id
);
};
return {
preferences,
loading,
error,
updatePreferences,
refreshPreferences: fetchPreferences,
isOnboardingComplete,
};
}