@@ -14,7 +14,7 @@ import {
1414} from "@roo-code/types"
1515
1616import { vscode } from "@src/utils/vscode"
17- import { validateApiConfiguration } from "@src/utils/validate"
17+ import { validateApiConfigurationExcludingModelErrors , getModelValidationError } from "@src/utils/validate"
1818import { useAppTranslation } from "@src/i18n/TranslationContext"
1919import { useRouterModels } from "@src/components/ui/hooks/useRouterModels"
2020import { useSelectedModel } from "@src/components/ui/hooks/useSelectedModel"
@@ -176,8 +176,11 @@ const ApiOptions = ({
176176 )
177177
178178 useEffect ( ( ) => {
179- const apiValidationResult = validateApiConfiguration ( apiConfiguration , routerModels , organizationAllowList )
180-
179+ const apiValidationResult = validateApiConfigurationExcludingModelErrors (
180+ apiConfiguration ,
181+ routerModels ,
182+ organizationAllowList ,
183+ )
181184 setErrorMessage ( apiValidationResult )
182185 } , [ apiConfiguration , routerModels , organizationAllowList , setErrorMessage ] )
183186
@@ -197,51 +200,58 @@ const ApiOptions = ({
197200
198201 const onProviderChange = useCallback (
199202 ( value : ProviderName ) => {
200- // It would be much easier to have a single attribute that stores
201- // the modelId, but we have a separate attribute for each of
202- // OpenRouter, Glama, Unbound, and Requesty.
203- // If you switch to one of these providers and the corresponding
204- // modelId is not set then you immediately end up in an error state.
205- // To address that we set the modelId to the default value for th
206- // provider if it's not already set.
203+ setApiConfigurationField ( "apiProvider" , value )
204+
205+ // Helper function to validate and reset model if invalid
206+ const validateAndResetModel = (
207+ modelId : string | undefined ,
208+ field : keyof ProviderSettings ,
209+ defaultValue ?: string ,
210+ ) => {
211+ if ( modelId ) {
212+ const tempConfig = { ...apiConfiguration , apiProvider : value , apiModelId : modelId }
213+ const modelError = getModelValidationError ( tempConfig , routerModels , organizationAllowList )
214+ if ( modelError ) {
215+ setApiConfigurationField ( field , "" )
216+ }
217+ } else if ( defaultValue ) {
218+ setApiConfigurationField ( field , defaultValue )
219+ }
220+ }
221+
222+ // Validate current model for the new provider
207223 switch ( value ) {
208224 case "openrouter" :
209- if ( ! apiConfiguration . openRouterModelId ) {
210- setApiConfigurationField ( "openRouterModelId" , openRouterDefaultModelId )
211- }
225+ validateAndResetModel (
226+ apiConfiguration . openRouterModelId ,
227+ "openRouterModelId" ,
228+ openRouterDefaultModelId ,
229+ )
212230 break
213231 case "glama" :
214- if ( ! apiConfiguration . glamaModelId ) {
215- setApiConfigurationField ( "glamaModelId" , glamaDefaultModelId )
216- }
232+ validateAndResetModel ( apiConfiguration . glamaModelId , "glamaModelId" , glamaDefaultModelId )
217233 break
218234 case "unbound" :
219- if ( ! apiConfiguration . unboundModelId ) {
220- setApiConfigurationField ( "unboundModelId" , unboundDefaultModelId )
221- }
235+ validateAndResetModel ( apiConfiguration . unboundModelId , "unboundModelId" , unboundDefaultModelId )
222236 break
223237 case "requesty" :
224- if ( ! apiConfiguration . requestyModelId ) {
225- setApiConfigurationField ( "requestyModelId" , requestyDefaultModelId )
226- }
238+ validateAndResetModel ( apiConfiguration . requestyModelId , "requestyModelId" , requestyDefaultModelId )
227239 break
228240 case "litellm" :
229- if ( ! apiConfiguration . litellmModelId ) {
230- setApiConfigurationField ( "litellmModelId" , litellmDefaultModelId )
231- }
241+ validateAndResetModel ( apiConfiguration . litellmModelId , "litellmModelId" , litellmDefaultModelId )
242+ break
243+ case "openai" :
244+ validateAndResetModel ( apiConfiguration . openAiModelId , "openAiModelId" )
245+ break
246+ case "ollama" :
247+ validateAndResetModel ( apiConfiguration . ollamaModelId , "ollamaModelId" )
248+ break
249+ case "lmstudio" :
250+ validateAndResetModel ( apiConfiguration . lmStudioModelId , "lmStudioModelId" )
232251 break
233252 }
234-
235- setApiConfigurationField ( "apiProvider" , value )
236253 } ,
237- [
238- setApiConfigurationField ,
239- apiConfiguration . openRouterModelId ,
240- apiConfiguration . glamaModelId ,
241- apiConfiguration . unboundModelId ,
242- apiConfiguration . requestyModelId ,
243- apiConfiguration . litellmModelId ,
244- ] ,
254+ [ setApiConfigurationField , apiConfiguration , routerModels , organizationAllowList ] ,
245255 )
246256
247257 const docs = useMemo ( ( ) => {
0 commit comments