@@ -117,11 +117,54 @@ def _determine_engine_type(self, endpoint_config: Dict[str, str], endpoint_name:
117117 def _get_filtered_endpoints (self ) -> List [str ]:
118118 """Collect endpoints that support the goose client."""
119119 endpoints = self .config .get_sections (exclude_common = True )
120- return [
121- ep
122- for ep in endpoints
123- if self .endpoint_manager ._is_client_supported (ep , "goose" )
124- ]
120+ filtered = []
121+ for ep in endpoints :
122+ # Check if endpoint is enabled
123+ ep_config = self .config .get_endpoint_config (ep )
124+ enabled = ep_config .get ("enabled" , "true" ).lower () in ("true" , "1" , "yes" )
125+ if enabled and self .endpoint_manager ._is_client_supported (ep , "goose" ):
126+ filtered .append (ep )
127+ return filtered
128+
129+ def _validate_and_fix_config (self ) -> None :
130+ """Validate that config.yaml references valid custom providers.
131+
132+ If config references a provider that doesn't exist, try to use an available one.
133+ """
134+ config_file = Path .home () / ".config" / "goose" / "config.yaml"
135+ if not config_file .exists ():
136+ return
137+
138+ try :
139+ with open (config_file , "r" , encoding = "utf-8" ) as f :
140+ config_data = yaml .safe_load (f ) or {}
141+ except Exception :
142+ return
143+
144+ provider_name = config_data .get ("GOOSE_PROVIDER" )
145+ model_name = config_data .get ("GOOSE_MODEL" )
146+
147+ # Check if current provider exists
148+ custom_providers = self ._get_custom_providers ()
149+
150+ if provider_name and provider_name in custom_providers :
151+ # Provider exists, verify model exists
152+ if model_name and model_name in custom_providers [provider_name ]:
153+ # Everything is valid
154+ return
155+ else :
156+ # Provider exists but model doesn't; use first available model
157+ if custom_providers [provider_name ]:
158+ first_model = custom_providers [provider_name ][0 ]
159+ self ._write_default_to_config (provider_name , first_model )
160+ print (f"Fixed: Updated model to '{ first_model } '" )
161+ else :
162+ # Provider doesn't exist; use first available provider and model
163+ if custom_providers :
164+ first_provider = next (iter (custom_providers .keys ()))
165+ first_model = custom_providers [first_provider ][0 ]
166+ self ._write_default_to_config (first_provider , first_model )
167+ print (f"Fixed: Updated provider to '{ first_provider } ' and model to '{ first_model } '" )
125168
126169 def _get_custom_providers (self ) -> Dict [str , List [str ]]:
127170 """Read existing custom providers from ~/.config/goose/custom_providers/.
@@ -387,10 +430,23 @@ def _show_and_select_model(
387430
388431
389432 def _write_default_to_config (self , provider_name : str , model_name : str ) -> None :
390- """Write provider and model to goose config.yaml."""
433+ """Write provider and model to goose config.yaml.
434+
435+ Validates that the provider and model exist in custom providers before writing.
436+ """
391437 config_file = Path .home () / ".config" / "goose" / "config.yaml"
392438 config_file .parent .mkdir (parents = True , exist_ok = True )
393439
440+ # Validate that the provider and model exist
441+ custom_providers = self ._get_custom_providers ()
442+ if provider_name not in custom_providers :
443+ print (f"Warning: Provider '{ provider_name } ' not found in custom providers. Skipping config update." )
444+ return
445+
446+ if model_name not in custom_providers [provider_name ]:
447+ print (f"Warning: Model '{ model_name } ' not found in provider '{ provider_name } '. Skipping config update." )
448+ return
449+
394450 config_data = {}
395451 if config_file .exists ():
396452 try :
@@ -417,6 +473,9 @@ def run(self, args: List[str] = None) -> int:
417473 # Load environment variables first
418474 self ._load_environment ()
419475
476+ # Validate and fix config if needed
477+ self ._validate_and_fix_config ()
478+
420479 # Check if Goose is installed
421480 if not self ._ensure_tool_installed (
422481 self .command_name , self .tool_key , self .install_description
0 commit comments