mirror of
https://github.com/khoaliber/khoj.git
synced 2026-03-02 21:19:12 +00:00
Centralize use of useUserConfig and use that to retrieve default model and chat model options
This commit is contained in:
@@ -171,7 +171,7 @@ export default function Agents() {
|
||||
error: authenticationError,
|
||||
isLoading: authenticationLoading,
|
||||
} = useAuthenticatedData();
|
||||
const { userConfig } = useUserConfig(true);
|
||||
const { data: userConfig } = useUserConfig(true);
|
||||
const [showLoginPrompt, setShowLoginPrompt] = useState(false);
|
||||
const isMobileWidth = useIsMobileWidth();
|
||||
|
||||
|
||||
@@ -90,15 +90,16 @@ export interface UserConfig {
|
||||
export function useUserConfig(detailed: boolean = false) {
|
||||
const url = `/api/settings?detailed=${detailed}`;
|
||||
const {
|
||||
data: userConfig,
|
||||
data,
|
||||
error,
|
||||
isLoading: isLoadingUserConfig,
|
||||
isLoading,
|
||||
} = useSWR<UserConfig>(url, fetcher, { revalidateOnFocus: false });
|
||||
|
||||
if (error || !userConfig || userConfig?.detail === "Forbidden")
|
||||
return { userConfig: null, isLoadingUserConfig };
|
||||
if (error || !data || data?.detail === "Forbidden") {
|
||||
return { data: null, error, isLoading };
|
||||
}
|
||||
|
||||
return { userConfig, isLoadingUserConfig };
|
||||
return { data, error, isLoading };
|
||||
}
|
||||
|
||||
export function useChatModelOptions() {
|
||||
|
||||
@@ -17,14 +17,13 @@ import {
|
||||
CommandItem,
|
||||
CommandList,
|
||||
} from "@/components/ui/command";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import {
|
||||
Popover,
|
||||
PopoverContent,
|
||||
PopoverTrigger,
|
||||
} from "@/components/ui/popover";
|
||||
|
||||
import { ModelOptions, useChatModelOptions, useUserConfig } from "./auth";
|
||||
import { ModelOptions, useUserConfig } from "./auth";
|
||||
import { HoverCard, HoverCardContent, HoverCardTrigger } from "@/components/ui/hover-card";
|
||||
import { Skeleton } from "@/components/ui/skeleton";
|
||||
|
||||
@@ -36,34 +35,39 @@ interface ModelSelectorProps extends PopoverProps {
|
||||
|
||||
export function ModelSelector({ ...props }: ModelSelectorProps) {
|
||||
const [open, setOpen] = React.useState(false)
|
||||
const { models, isLoading, error } = useChatModelOptions();
|
||||
const [peekedModel, setPeekedModel] = useState<ModelOptions | undefined>(undefined);
|
||||
const [selectedModel, setSelectedModel] = useState<ModelOptions | undefined>(undefined);
|
||||
const { userConfig } = useUserConfig();
|
||||
const { data: userConfig, error, isLoading: isLoadingUserConfig } = useUserConfig(true);
|
||||
const [models, setModels] = useState<ModelOptions[]>([]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!models?.length) return;
|
||||
if (isLoadingUserConfig) return;
|
||||
|
||||
if (props.selectedModel) {
|
||||
const model = models.find(model => model.name === props.selectedModel);
|
||||
setSelectedModel(model || models[0]);
|
||||
return;
|
||||
} else if (userConfig) {
|
||||
const model = models.find(model => model.id === userConfig.selected_chat_model_config);
|
||||
setSelectedModel(model || models[0]);
|
||||
if (userConfig) {
|
||||
setModels(userConfig.chat_model_options);
|
||||
const selectedChatModelOption = userConfig.chat_model_options.find(model => model.id === userConfig.selected_chat_model_config);
|
||||
setSelectedModel(selectedChatModelOption);
|
||||
return;
|
||||
} else {
|
||||
setSelectedModel(models[0]);
|
||||
}
|
||||
|
||||
setSelectedModel(models[0]);
|
||||
}, [models, props.selectedModel, userConfig]);
|
||||
|
||||
useEffect(() => {
|
||||
if (props.selectedModel) {
|
||||
const model = models.find(model => model.name === props.selectedModel);
|
||||
setSelectedModel(model);
|
||||
}
|
||||
}, [props.selectedModel]);
|
||||
|
||||
useEffect(() => {
|
||||
if (selectedModel) {
|
||||
props.onSelect(selectedModel);
|
||||
}
|
||||
}, [selectedModel]);
|
||||
|
||||
if (isLoading) {
|
||||
if (isLoadingUserConfig) {
|
||||
return (
|
||||
<Skeleton className="w-full h-10" />
|
||||
);
|
||||
|
||||
@@ -486,7 +486,7 @@ export default function Home() {
|
||||
const [uploadedFiles, setUploadedFiles] = useState<AttachedFileText[] | null>(null);
|
||||
const isMobileWidth = useIsMobileWidth();
|
||||
|
||||
const { userConfig: initialUserConfig, isLoadingUserConfig } = useUserConfig(true);
|
||||
const { data: initialUserConfig, isLoading: isLoadingUserConfig } = useUserConfig(true);
|
||||
const [userConfig, setUserConfig] = useState<UserConfig | null>(null);
|
||||
|
||||
const {
|
||||
|
||||
@@ -595,7 +595,7 @@ enum PhoneNumberValidationState {
|
||||
}
|
||||
|
||||
export default function SettingsView() {
|
||||
const { userConfig: initialUserConfig } = useUserConfig(true);
|
||||
const { data: initialUserConfig } = useUserConfig(true);
|
||||
const [userConfig, setUserConfig] = useState<UserConfig | null>(null);
|
||||
const [name, setName] = useState<string | undefined>(undefined);
|
||||
const [notionToken, setNotionToken] = useState<string | null>(null);
|
||||
|
||||
Reference in New Issue
Block a user