Improve default selection of models to avoid infinite loops

This commit is contained in:
sabaimran
2025-02-04 11:36:41 -08:00
parent 24b1dd3bff
commit 0d10c5fb02
2 changed files with 37 additions and 29 deletions

View File

@@ -49,13 +49,16 @@ export function ModelSelector({ ...props }: ModelSelectorProps) {
setModels(userConfig.chat_model_options); setModels(userConfig.chat_model_options);
if (!props.initialModel) { if (!props.initialModel) {
const selectedChatModelOption = userConfig.chat_model_options.find(model => model.id === userConfig.selected_chat_model_config); const selectedChatModelOption = userConfig.chat_model_options.find(model => model.id === userConfig.selected_chat_model_config);
if (!selectedChatModelOption) {
setSelectedModel(userConfig.chat_model_options[0]);
return;
} else {
setSelectedModel(selectedChatModelOption); setSelectedModel(selectedChatModelOption);
}
} else { } else {
const model = userConfig.chat_model_options.find(model => model.name === props.initialModel); const model = userConfig.chat_model_options.find(model => model.name === props.initialModel);
setSelectedModel(model); setSelectedModel(model);
} }
} else {
setSelectedModel(models[0]);
} }
}, [userConfig, props.initialModel, isLoadingUserConfig]); }, [userConfig, props.initialModel, isLoadingUserConfig]);

View File

@@ -57,7 +57,6 @@ export function ChatSidebar({ ...props }: ChatSideBarProps) {
function ChatSidebarInternal({ ...props }: ChatSideBarProps) { function ChatSidebarInternal({ ...props }: ChatSideBarProps) {
const [isEditable, setIsEditable] = useState<boolean>(false); const [isEditable, setIsEditable] = useState<boolean>(false);
const [isDefaultAgent, setIsDefaultAgent] = useState<boolean>(true);
const { data: agentConfigurationOptions, error: agentConfigurationOptionsError } = const { data: agentConfigurationOptions, error: agentConfigurationOptionsError } =
useSWR<AgentConfigurationOptions>("/api/agents/options", fetcher); useSWR<AgentConfigurationOptions>("/api/agents/options", fetcher);
@@ -73,6 +72,7 @@ function ChatSidebarInternal({ ...props }: ChatSideBarProps) {
const [inputTools, setInputTools] = useState<string[] | undefined>(); const [inputTools, setInputTools] = useState<string[] | undefined>();
const [outputModes, setOutputModes] = useState<string[] | undefined>(); const [outputModes, setOutputModes] = useState<string[] | undefined>();
const [hasModified, setHasModified] = useState<boolean>(false); const [hasModified, setHasModified] = useState<boolean>(false);
const [isDefaultAgent, setIsDefaultAgent] = useState<boolean>(agentData?.slug.toLowerCase() === "khoj" ? true : false);
const [isSaving, setIsSaving] = useState<boolean>(false); const [isSaving, setIsSaving] = useState<boolean>(false);
@@ -94,6 +94,7 @@ function ChatSidebarInternal({ ...props }: ChatSideBarProps) {
if (agentData.slug.toLowerCase() === "khoj") { if (agentData.slug.toLowerCase() === "khoj") {
setSelectedModel(undefined); setSelectedModel(undefined);
setCustomPrompt(undefined); setCustomPrompt(undefined);
setIsDefaultAgent(true);
} else { } else {
setIsDefaultAgent(false); setIsDefaultAgent(false);
setCustomPrompt(agentData.persona); setCustomPrompt(agentData.persona);
@@ -248,6 +249,8 @@ function ChatSidebarInternal({ ...props }: ChatSideBarProps) {
</SidebarMenu> </SidebarMenu>
</SidebarGroupContent> </SidebarGroupContent>
</SidebarGroup> </SidebarGroup>
{
!agentDataLoading && agentData && (
<SidebarGroup key={"model"}> <SidebarGroup key={"model"}>
<SidebarGroupContent> <SidebarGroupContent>
<SidebarGroupLabel> <SidebarGroupLabel>
@@ -265,13 +268,15 @@ function ChatSidebarInternal({ ...props }: ChatSideBarProps) {
<ModelSelector <ModelSelector
disabled={!isEditable || !authenticatedData?.is_active} disabled={!isEditable || !authenticatedData?.is_active}
onSelect={(model, userModification) => handleModelSelect(model.name, userModification)} onSelect={(model, userModification) => handleModelSelect(model.name, userModification)}
initialModel={isDefaultAgent ? '' : agentData?.chat_model} initialModel={isDefaultAgent ? undefined : agentData?.chat_model}
selectedModel={selectedModel} selectedModel={selectedModel}
/> />
</SidebarMenuItem> </SidebarMenuItem>
</SidebarMenu> </SidebarMenu>
</SidebarGroupContent> </SidebarGroupContent>
</SidebarGroup> </SidebarGroup>
)
}
<Popover defaultOpen={false}> <Popover defaultOpen={false}>
<SidebarGroup> <SidebarGroup>
<SidebarGroupLabel asChild> <SidebarGroupLabel asChild>