diff --git a/core/config/yaml/loadYaml.ts b/core/config/yaml/loadYaml.ts index 1edb48604..1ad9fd2c6 100644 --- a/core/config/yaml/loadYaml.ts +++ b/core/config/yaml/loadYaml.ts @@ -157,7 +157,7 @@ async function configYamlToContinueConfig( summarize: [], }, selectedModelByRole: { - chat: null, // not currently used - defaultModel on GUI is used + chat: null, edit: null, // not currently used apply: null, embed: null, diff --git a/gui/src/components/OnboardingCard/components/BestExperienceConfigForm.tsx b/gui/src/components/OnboardingCard/components/BestExperienceConfigForm.tsx index d05481dfb..d739a8434 100644 --- a/gui/src/components/OnboardingCard/components/BestExperienceConfigForm.tsx +++ b/gui/src/components/OnboardingCard/components/BestExperienceConfigForm.tsx @@ -1,11 +1,12 @@ import { CubeIcon } from "@heroicons/react/24/outline"; import { FormEventHandler, useContext, useState } from "react"; -import { useDispatch } from "react-redux"; import { Button, Input, InputSubtext, lightGray } from "../.."; +import { useAuth } from "../../../context/Auth"; import { IdeMessengerContext } from "../../../context/IdeMessenger"; import { models } from "../../../pages/AddNewModel/configs/models"; import { providers } from "../../../pages/AddNewModel/configs/providers"; -import { setSelectedChatModel } from "../../../redux/slices/configSlice"; +import { useAppDispatch } from "../../../redux/hooks"; +import { updateSelectedModelByRole } from "../../../redux/thunks"; import AddModelButtonSubtext from "../../AddModelButtonSubtext"; const { anthropic, mistral } = providers; @@ -25,7 +26,8 @@ interface BestExperienceConfigFormProps { function BestExperienceConfigForm({ onComplete, }: BestExperienceConfigFormProps) { - const dispatch = useDispatch(); + const dispatch = useAppDispatch(); + const { selectedProfile } = useAuth(); const ideMessenger = useContext(IdeMessengerContext); @@ -61,8 +63,13 @@ function BestExperienceConfigForm({ model: repoMapConfig, role: "repoMapFileSelection", }); + dispatch( - setSelectedChatModel({ title: chatModelConfig.title, force: true }), + updateSelectedModelByRole({ + selectedProfile, + role: "chat", + modelTitle: chatModelConfig.title, + }), ); if (!!autocompleteApiKey) { diff --git a/gui/src/components/OnboardingCard/tabs/OnboardingLocalTab.tsx b/gui/src/components/OnboardingCard/tabs/OnboardingLocalTab.tsx index 54f036118..0f1e05da0 100644 --- a/gui/src/components/OnboardingCard/tabs/OnboardingLocalTab.tsx +++ b/gui/src/components/OnboardingCard/tabs/OnboardingLocalTab.tsx @@ -6,11 +6,12 @@ import { LOCAL_ONBOARDING_PROVIDER_TITLE, } from "core/config/onboarding"; import { useContext, useEffect, useState } from "react"; -import { useDispatch } from "react-redux"; import { Button } from "../.."; +import { useAuth } from "../../../context/Auth"; import { IdeMessengerContext } from "../../../context/IdeMessenger"; -import { setSelectedChatModel } from "../../../redux/slices/configSlice"; +import { useAppDispatch } from "../../../redux/hooks"; import { setDialogMessage, setShowDialog } from "../../../redux/slices/uiSlice"; +import { updateSelectedModelByRole } from "../../../redux/thunks"; import AddModelButtonSubtext from "../../AddModelButtonSubtext"; import OllamaModelDownload from "../components/OllamaModelDownload"; import { OllamaStatus } from "../components/OllamaStatus"; @@ -23,13 +24,14 @@ interface OnboardingLocalTabProps { } function OnboardingLocalTab({ isDialog }: OnboardingLocalTabProps) { - const dispatch = useDispatch(); + const dispatch = useAppDispatch(); const ideMessenger = useContext(IdeMessengerContext); const { submitOnboarding } = useSubmitOnboarding("Local", isDialog); const [hasLoadedChatModel, setHasLoadedChatModel] = useState(false); const [downloadedOllamaModels, setDownloadedOllamaModels] = useState< string[] >([]); + const { selectedProfile } = useAuth(); const [isOllamaConnected, setIsOllamaConnected] = useState(false); @@ -135,11 +137,11 @@ function OnboardingLocalTab({ isDialog }: OnboardingLocalTabProps) { dispatch(setShowDialog(false)); } - // Set the selected model to the local chat model dispatch( - setSelectedChatModel({ - title: LOCAL_ONBOARDING_CHAT_TITLE, - force: true, // Because it doesn't exist in the webview's config object yet + updateSelectedModelByRole({ + selectedProfile, + role: "chat", + modelTitle: LOCAL_ONBOARDING_CHAT_TITLE, }), ); }} diff --git a/gui/src/components/mainInput/Lump/sections/ModelsSection.tsx b/gui/src/components/mainInput/Lump/sections/ModelsSection.tsx index f6edc7400..b128db0a2 100644 --- a/gui/src/components/mainInput/Lump/sections/ModelsSection.tsx +++ b/gui/src/components/mainInput/Lump/sections/ModelsSection.tsx @@ -1,41 +1,31 @@ import { ModelRole } from "@continuedev/config-yaml"; import { ModelDescription } from "core"; -import { useContext } from "react"; import { useAuth } from "../../../../context/Auth"; -import { IdeMessengerContext } from "../../../../context/IdeMessenger"; import ModelRoleSelector from "../../../../pages/config/ModelRoleSelector"; import { useAppDispatch, useAppSelector } from "../../../../redux/hooks"; -import { updateConfig } from "../../../../redux/slices/configSlice"; +import { updateSelectedModelByRole } from "../../../../redux/thunks"; import { isJetBrains } from "../../../../util"; import { ExploreBlocksButton } from "./ExploreBlocksButton"; export function ModelsSection() { const { selectedProfile } = useAuth(); const dispatch = useAppDispatch(); - const ideMessenger = useContext(IdeMessengerContext); const config = useAppSelector((state) => state.config.config); const jetbrains = isJetBrains(); function handleRoleUpdate(role: ModelRole, model: ModelDescription | null) { - if (!selectedProfile) { + if (!model) { return; } - // Optimistic update + dispatch( - updateConfig({ - ...config, - selectedModelByRole: { - ...config.selectedModelByRole, - [role]: model, - }, + updateSelectedModelByRole({ + role, + selectedProfile, + modelTitle: model.title, }), ); - ideMessenger.post("config/updateSelectedModel", { - profileId: selectedProfile.id, - role, - title: model?.title ?? null, - }); } return ( diff --git a/gui/src/components/modelSelection/ModelSelect.tsx b/gui/src/components/modelSelection/ModelSelect.tsx index a6e7756ac..a25fd210d 100644 --- a/gui/src/components/modelSelection/ModelSelect.tsx +++ b/gui/src/components/modelSelection/ModelSelect.tsx @@ -6,18 +6,15 @@ import { PlusIcon, } from "@heroicons/react/24/outline"; import { useContext, useEffect, useRef, useState } from "react"; -import { useDispatch } from "react-redux"; import styled from "styled-components"; import { defaultBorderRadius, lightGray } from ".."; import { useAuth } from "../../context/Auth"; import { IdeMessengerContext } from "../../context/IdeMessenger"; import AddModelForm from "../../forms/AddModelForm"; -import { useAppSelector } from "../../redux/hooks"; -import { - selectSelectedChatModel, - setSelectedChatModel, -} from "../../redux/slices/configSlice"; +import { useAppDispatch, useAppSelector } from "../../redux/hooks"; +import { selectSelectedChatModel } from "../../redux/slices/configSlice"; import { setDialogMessage, setShowDialog } from "../../redux/slices/uiSlice"; +import { updateSelectedModelByRole } from "../../redux/thunks"; import { getMetaKeyLabel, isMetaEquivalentKeyPressed } from "../../util"; import { Listbox, ListboxButton, ListboxOption, ListboxOptions } from "../ui"; @@ -122,8 +119,8 @@ function ModelOption({ } function ModelSelect() { - const dispatch = useDispatch(); - const defaultModel = useAppSelector(selectSelectedChatModel); + const dispatch = useAppDispatch(); + const selectedChatModel = useAppSelector(selectSelectedChatModel); const allModels = useAppSelector((state) => state.config.config.models); const buttonRef = useRef(null); const [options, setOptions] = useState([]); @@ -157,14 +154,25 @@ function ModelSelect() { useEffect(() => { const handleKeyDown = (event: KeyboardEvent) => { if (event.key === "'" && isMetaEquivalentKeyPressed(event as any)) { + if (!selectedProfile) { + return; + } + const direction = event.shiftKey ? -1 : 1; const currentIndex = options.findIndex( - (option) => option.value === defaultModel?.title, + (option) => option.value === selectedChatModel?.title, ); let nextIndex = (currentIndex + 1 * direction) % options.length; if (nextIndex < 0) nextIndex = options.length - 1; const newModelTitle = options[nextIndex].value; - dispatch(setSelectedChatModel({ title: newModelTitle })); + + dispatch( + updateSelectedModelByRole({ + selectedProfile, + role: "chat", + modelTitle: newModelTitle, + }), + ); } }; @@ -172,7 +180,7 @@ function ModelSelect() { return () => { window.removeEventListener("keydown", handleKeyDown); }; - }, [options, defaultModel]); + }, [options, selectedChatModel]); function onClickAddModel(e: MouseEvent) { e.stopPropagation(); @@ -195,11 +203,16 @@ function ModelSelect() { } return ( - // Hiad sfasdfasdf asdfasdf { - if (val === defaultModel?.title) return; - dispatch(setSelectedChatModel({ title: val })); + if (val === selectedChatModel?.title) return; + dispatch( + updateSelectedModelByRole({ + selectedProfile, + role: "chat", + modelTitle: val, + }), + ); }} >
@@ -209,7 +222,7 @@ function ModelSelect() { className="h-[18px] gap-1 border-none text-gray-400" > - {modelSelectTitle(defaultModel) || "Select model"} + {modelSelectTitle(selectedChatModel) || "Select model"} ))}
diff --git a/gui/src/forms/AddModelForm.tsx b/gui/src/forms/AddModelForm.tsx index 259d7c1d3..94a26adbc 100644 --- a/gui/src/forms/AddModelForm.tsx +++ b/gui/src/forms/AddModelForm.tsx @@ -1,11 +1,11 @@ import { ArrowTopRightOnSquareIcon } from "@heroicons/react/24/outline"; import { useContext, useEffect, useState } from "react"; import { FormProvider, useForm } from "react-hook-form"; -import { useDispatch } from "react-redux"; import { Button, Input, InputSubtext, StyledActionButton } from "../components"; import AddModelButtonSubtext from "../components/AddModelButtonSubtext"; import Alert from "../components/gui/Alert"; import ModelSelectionListbox from "../components/modelSelection/ModelSelectionListbox"; +import { useAuth } from "../context/Auth"; import { IdeMessengerContext } from "../context/IdeMessenger"; import { completionParamsInputs } from "../pages/AddNewModel/configs/completionParamsInputs"; import { DisplayInfo } from "../pages/AddNewModel/configs/models"; @@ -13,7 +13,8 @@ import { ProviderInfo, providers, } from "../pages/AddNewModel/configs/providers"; -import { setSelectedChatModel } from "../redux/slices/configSlice"; +import { useAppDispatch } from "../redux/hooks"; +import { updateSelectedModelByRole } from "../redux/thunks"; import { FREE_TRIAL_LIMIT_REQUESTS, hasPassedFTL } from "../util/freeTrial"; interface QuickModelSetupProps { @@ -33,13 +34,14 @@ function AddModelForm({ const [selectedProvider, setSelectedProvider] = useState( providers["openai"]!, ); + const dispatch = useAppDispatch(); + const { selectedProfile } = useAuth(); const [selectedModel, setSelectedModel] = useState( selectedProvider.packages[0], ); const formMethods = useForm(); - const dispatch = useDispatch(); const ideMessenger = useContext(IdeMessengerContext); const popularProviderTitles = [ @@ -115,7 +117,13 @@ function AddModelForm({ profileId: "local", }); - dispatch(setSelectedChatModel({ title: model.title, force: true })); + dispatch( + updateSelectedModelByRole({ + selectedProfile, + role: "chat", + modelTitle: model.title, + }), + ); onDone(); } diff --git a/gui/src/redux/slices/configSlice.ts b/gui/src/redux/slices/configSlice.ts index c1528daa9..ca1856421 100644 --- a/gui/src/redux/slices/configSlice.ts +++ b/gui/src/redux/slices/configSlice.ts @@ -78,24 +78,6 @@ export const configSlice = createSlice({ ) => { state.config = config; }, - setSelectedChatModel: ( - state, - { payload }: PayloadAction<{ title: string; force?: boolean }>, - ) => { - const model = state.config.modelsByRole["chat"].find( - (model) => model.title === payload.title, - ); - - if (!model && !payload.force) return; - - state.config = { - ...state.config, - selectedModelByRole: { - ...state.config.selectedModelByRole, - chat: model ?? null, - }, - }; - }, }, selectors: { selectSelectedChatModelContextLength: (state): number => { @@ -113,8 +95,7 @@ export const configSlice = createSlice({ }, }); -export const { updateConfig, setConfigResult, setSelectedChatModel } = - configSlice.actions; +export const { updateConfig, setConfigResult } = configSlice.actions; export const { selectSelectedChatModelContextLength, diff --git a/gui/src/redux/thunks/index.ts b/gui/src/redux/thunks/index.ts index 59b556f8d..ad987fafa 100644 --- a/gui/src/redux/thunks/index.ts +++ b/gui/src/redux/thunks/index.ts @@ -5,3 +5,4 @@ export { streamNormalInput } from "./streamNormalInput"; export { streamResponseThunk } from "./streamResponse"; export { streamResponseAfterToolCall } from "./streamResponseAfterToolCall"; export { streamThunkWrapper } from "./streamThunkWrapper"; +export { updateSelectedModelByRole } from "./updateSelectedModelByRole"; diff --git a/gui/src/redux/thunks/updateSelectedModelByRole.ts b/gui/src/redux/thunks/updateSelectedModelByRole.ts new file mode 100644 index 000000000..8ad4f3df5 --- /dev/null +++ b/gui/src/redux/thunks/updateSelectedModelByRole.ts @@ -0,0 +1,58 @@ +import { ModelRole } from "@continuedev/config-yaml"; +import { createAsyncThunk } from "@reduxjs/toolkit"; +import { ProfileDescription } from "core/config/ProfileLifecycleManager"; +import { updateConfig } from "../slices/configSlice"; +import { ThunkApiType } from "../store"; + +export const updateSelectedModelByRole = createAsyncThunk< + void, + { + role: ModelRole; + modelTitle: string; + selectedProfile: ProfileDescription | null; + }, + ThunkApiType +>( + "config/updateSelectedModel", + async ( + { role, modelTitle, selectedProfile }, + { dispatch, extra, getState }, + ) => { + if (!selectedProfile) { + return; + } + + const state = getState(); + + const { + config: { config }, + } = state; + + const model = state.config.config.modelsByRole[role]?.find( + (m) => m.title === modelTitle, + ); + + if (!model) { + console.error( + `Model with title "${modelTitle}" not found for role "${role}"`, + ); + return; + } + + dispatch( + updateConfig({ + ...config, + selectedModelByRole: { + ...config.selectedModelByRole, + [role]: model, + }, + }), + ); + + extra.ideMessenger.post("config/updateSelectedModel", { + role, + profileId: selectedProfile.id, + title: modelTitle, + }); + }, +);