🙌 OpenHands: AI-Driven Development
feat: allow partial modification of CLI settings (#10240)
aeft6bdc5563cfd5
diff --git a/openhands/cli/settings.py b/openhands/cli/settings.pyindex fe737d5cff..1deffab99f 100644--- a/openhands/cli/settings.py+++ b/openhands/cli/settings.py@@ -1,4 +1,5 @@from pathlib import Path+from typing import Optionalfrom prompt_toolkit import PromptSession, print_formatted_textfrom prompt_toolkit.completion import FuzzyWordCompleter@@ -19,6 +20,7 @@ from openhands.cli.utils import (VERIFIED_OPENAI_MODELS,VERIFIED_OPENHANDS_MODELS,VERIFIED_PROVIDERS,+ extract_model_and_provider,organize_models_and_providers,)from openhands.controller.agent import Agent@@ -124,12 +126,36 @@ async def get_validated_input(completer=None,validator=None,error_message: str = 'Input cannot be empty',+ *,+ default_value: str = '',+ enter_keeps_value: Optional[str] = None,) -> str:+ """+ Get validated input from user.++ Args:+ session: PromptSession instance+ prompt_text: The text to display before the input+ completer: Completer instance+ validator: Function to validate input+ error_message: Error message to display if input is invalid+ default_value: Value to show prefilled in the prompt (prompt placeholder)+ enter_keeps_value: If provided, pressing Enter on an empty input will+ return this value (useful for keeping existing sensitive values)++ Returns:+ str: The validated input+ """+session.completer = completervalue = Nonewhile True:- value = await session.prompt_async(prompt_text)+ value = await session.prompt_async(prompt_text, default=default_value)++ # If user submits empty input and a keep-value is provided, use it.+ if not value.strip() and enter_keeps_value is not None:+ value = enter_keeps_valueif validator:is_valid = validator(value)@@ -160,6 +186,50 @@ def save_settings_confirmation(config: OpenHandsConfig) -> bool:)+def _get_current_values_for_modification_basic(+ config: OpenHandsConfig,+) -> tuple[str, str, str]:+ llm_config = config.get_llm_config()+ current_provider = ''+ current_model = ''+ current_api_key = (+ llm_config.api_key.get_secret_value() if llm_config.api_key else ''+ )+ if llm_config.model:+ model_info = extract_model_and_provider(llm_config.model)+ current_provider = model_info.provider or ''+ current_model = model_info.model or ''+ return current_provider, current_model, current_api_key+++def _get_default_provider(provider_list: list[str]) -> str:+ if 'anthropic' in provider_list:+ return 'anthropic'+ else:+ return provider_list[0] if provider_list else ''+++def _get_initial_provider_index(+ verified_providers: list[str],+ current_provider: str,+ default_provider: str,+ provider_choices: list[str],+) -> int:+ if (current_provider or default_provider) in verified_providers:+ return verified_providers.index(current_provider or default_provider)+ elif current_provider or default_provider:+ return len(provider_choices) - 1+ return 0+++def _get_initial_model_index(+ verified_models: list[str], current_model: str, default_model: str+) -> int:+ if (current_model or default_model) in verified_models:+ return verified_models.index(current_model or default_model)+ return 0++async def modify_llm_settings_basic(config: OpenHandsConfig, settings_store: FileSettingsStore) -> None:@@ -174,23 +244,32 @@ async def modify_llm_settings_basic(provider_completer = FuzzyWordCompleter(provider_list)session = PromptSession(key_bindings=kb_cancel())- # Set default provider - prefer 'anthropic' if available, otherwise use first- provider = 'anthropic' if 'anthropic' in provider_list else provider_list[0]+ current_provider, current_model, current_api_key = (+ _get_current_values_for_modification_basic(config)+ )++ default_provider = _get_default_provider(provider_list)++ provider = Nonemodel = Noneapi_key = Nonetry:# Show the default provider but allow changing itprint_formatted_text(- HTML(f'\n<grey>Default provider: </grey><green>{provider}</green>')+ HTML(f'\n<grey>Default provider: </grey><green>{default_provider}</green>'))# Show verified providers plus "Select another provider" optionprovider_choices = verified_providers + ['Select another provider']+provider_choice = cli_confirm(config,'(Step 1/3) Select LLM Provider:',provider_choices,+ initial_selection=_get_initial_provider_index(+ verified_providers, current_provider, default_provider, provider_choices+ ),)# Ensure provider_choice is an integer (for test compatibility)@@ -211,8 +290,19 @@ async def modify_llm_settings_basic(completer=provider_completer,validator=lambda x: x in organized_models,error_message='Invalid provider selected',+ default_value=(+ # Prefill only for unverified providers.+ current_provider+ if current_provider not in verified_providers+ else ''+ ),)+ # Reset current model and api key if provider changes+ if provider != current_provider:+ current_model = ''+ current_api_key = ''+# Make sure the provider exists in organized_modelsif provider not in organized_models:# If the provider doesn't exist, prefer 'anthropic' if available,@@ -276,6 +366,9 @@ async def modify_llm_settings_basic(+ 'LLM usage is billed at the providers’ rates with no markup. Details: https://docs.all-hands.dev/usage/llms/openhands-llms'),model_choices,+ initial_selection=_get_initial_model_index(+ VERIFIED_OPENHANDS_MODELS, current_model, default_model+ ),)# Get the selected model from the list@@ -291,6 +384,9 @@ async def modify_llm_settings_basic(config,'Do you want to use a different model?',[f'Use {default_model}', 'Select another model'],+ initial_selection=0+ if (current_model or default_model) == default_model+ else 1,)== 1)@@ -320,6 +416,10 @@ async def modify_llm_settings_basic(completer=model_completer,validator=model_validator,error_message='Model name cannot be empty',+ default_value=(+ # Prefill only for models that are not the default model.+ current_model if current_model != default_model else ''+ ),)else:# Use the default model@@ -332,10 +432,15 @@ async def modify_llm_settings_basic())+ prompt_text = '(Step 3/3) Enter API Key (CTRL-c to cancel): '+ if current_api_key:+ prompt_text = f'(Step 3/3) Enter API Key [{current_api_key[:4]}***{current_api_key[-4:]}] (CTRL-c to cancel, ENTER to keep current, type new to change): 'api_key = await get_validated_input(session,- '(Step 3/3) Enter API Key (CTRL-c to cancel): ',+ prompt_text,error_message='API Key cannot be empty',+ default_value='',+ enter_keeps_value=current_api_key,)except (@@ -386,6 +491,7 @@ async def modify_llm_settings_advanced(config: OpenHandsConfig, settings_store: FileSettingsStore) -> None:session = PromptSession(key_bindings=kb_cancel())+ llm_config = config.get_llm_config()custom_model = Nonebase_url = None@@ -397,18 +503,28 @@ async def modify_llm_settings_advanced(session,'(Step 1/6) Custom Model (CTRL-c to cancel): ',error_message='Custom Model cannot be empty',+ default_value=llm_config.model or '',)base_url = await get_validated_input(session,'(Step 2/6) Base URL (CTRL-c to cancel): ',error_message='Base URL cannot be empty',+ default_value=llm_config.base_url or '',)+ prompt_text = '(Step 3/6) API Key (CTRL-c to cancel): '+ current_api_key = (+ llm_config.api_key.get_secret_value() if llm_config.api_key else ''+ )+ if current_api_key:+ prompt_text = f'(Step 3/6) API Key [{current_api_key[:4]}***{current_api_key[-4:]}] (CTRL-c to cancel, ENTER to keep current, type new to change): 'api_key = await get_validated_input(session,- '(Step 3/6) API Key (CTRL-c to cancel): ',+ prompt_text,error_message='API Key cannot be empty',+ default_value='',+ enter_keeps_value=current_api_key,)agent_list = Agent.list_agents()@@ -419,6 +535,7 @@ async def modify_llm_settings_advanced(completer=agent_completer,validator=lambda x: x in agent_list,error_message='Invalid agent selected',+ default_value=config.default_agent or '',)enable_confirmation_mode = (@@ -426,6 +543,7 @@ async def modify_llm_settings_advanced(config,question='(Step 5/6) Confirmation Mode (CTRL-c to cancel):',choices=['Enable', 'Disable'],+ initial_selection=0 if config.security.confirmation_mode else 1,)== 0)@@ -435,6 +553,7 @@ async def modify_llm_settings_advanced(config,question='(Step 6/6) Memory Condensation (CTRL-c to cancel):',choices=['Enable', 'Disable'],+ initial_selection=0 if config.enable_default_condenser else 1,)== 0)@@ -463,6 +582,7 @@ async def modify_llm_settings_advanced(config.default_agent = agentconfig.security.confirmation_mode = enable_confirmation_mode+ config.enable_default_condenser = enable_memory_condensationagent_config = config.get_agent_config(config.default_agent)if enable_memory_condensation:diff --git a/openhands/cli/tui.py b/openhands/cli/tui.pyindex c164193dbd..a15d8a6db9 100644--- a/openhands/cli/tui.py+++ b/openhands/cli/tui.py@@ -842,6 +842,7 @@ def cli_confirm(config: OpenHandsConfig,question: str = 'Are you sure?',choices: list[str] | None = None,+ initial_selection: int = 0,) -> int:"""Display a confirmation prompt with the given question and choices.@@ -849,7 +850,7 @@ def cli_confirm("""if choices is None:choices = ['Yes', 'No']- selected = [0] # Using list to allow modification in closure+ selected = [initial_selection] # Using list to allow modification in closuredef get_choice_text() -> list:return [diff --git a/tests/unit/test_cli_default_model.py b/tests/unit/test_cli_default_model.pyindex d618311162..b0eaff13fc 100644--- a/tests/unit/test_cli_default_model.py+++ b/tests/unit/test_cli_default_model.py@@ -40,6 +40,7 @@ async def test_anthropic_default_model_is_best_verified(# Mock config and settings storeapp_config = MagicMock()llm_config = MagicMock()+ llm_config.model = 'anthropic/claude-sonnet-4-20250514'app_config.get_llm_config.return_value = llm_configsettings_store = AsyncMock()diff --git a/tests/unit/test_cli_settings.py b/tests/unit/test_cli_settings.pyindex 758c8d70f8..c9c7466efb 100644--- a/tests/unit/test_cli_settings.py+++ b/tests/unit/test_cli_settings.py@@ -331,65 +331,6 @@ class TestModifyLLMSettingsBasic:assert settings.llm_api_key.get_secret_value() == 'new-api-key'assert settings.llm_base_url is None- def test_default_provider_preference(self):- """Test that the default provider prefers 'anthropic' if available."""- # This is a simple test to verify that the default provider prefers 'anthropic'- # We're directly checking the code in settings.py where the default provider is set-- # Import the settings module to check the default provider- # Find the line where the default provider is set- import inspect-- import openhands.cli.settings as settings_module-- source_lines = inspect.getsource(- settings_module.modify_llm_settings_basic- ).splitlines()-- # Look for the line that sets the default provider- default_provider_found = False- for i, line in enumerate(source_lines):- if "# Set default provider - prefer 'anthropic' if available" in line:- default_provider_found = True- break-- # Assert that the default provider comment exists- assert default_provider_found, 'Could not find the default provider comment'-- # Now look for the actual implementation- provider_impl_found = False- for i, line in enumerate(source_lines):- if "'anthropic'" in line and "if 'anthropic' in provider_list" in line:- provider_impl_found = True- break-- assert provider_impl_found, (- "Could not find the implementation that prefers 'anthropic'"- )-- # Also check the fallback provider when provider not in organized_models- fallback_comment_found = False- for i, line in enumerate(source_lines):- if (- "# If the provider doesn't exist, prefer 'anthropic' if available"- in line- ):- fallback_comment_found = True- break-- assert fallback_comment_found, 'Could not find the fallback provider comment'-- # Now look for the actual implementation- fallback_impl_found = False- for i, line in enumerate(source_lines):- if "'anthropic'" in line and "if 'anthropic' in organized_models" in line:- fallback_impl_found = True- break-- assert fallback_impl_found, (- "Could not find the fallback implementation that prefers 'anthropic'"- )-def test_default_model_selection(self):"""Test that the default model selection uses the first model in the list."""# This is a simple test to verify that the default model selection uses the first model in the list@@ -437,6 +378,426 @@ class TestModifyLLMSettingsBasic:'Default model selection should use the first model in the list')+ @pytest.mark.asyncio+ @patch(+ 'openhands.cli.settings.VERIFIED_PROVIDERS',+ ['openhands', 'anthropic', 'openai'],+ )+ @patch('openhands.cli.settings.VERIFIED_ANTHROPIC_MODELS', ['claude-3-opus'])+ @patch('openhands.cli.settings.get_supported_llm_models')+ @patch('openhands.cli.settings.organize_models_and_providers')+ @patch('openhands.cli.settings.PromptSession')+ @patch('openhands.cli.settings.cli_confirm')+ @patch('openhands.cli.settings.print_formatted_text')+ @patch(+ 'openhands.cli.settings.LLMSummarizingCondenserConfig',+ MockLLMSummarizingCondenserConfig,+ )+ async def test_default_provider_print_and_initial_selection(+ self,+ mock_print,+ mock_confirm,+ mock_session,+ mock_organize,+ mock_get_models,+ app_config,+ settings_store,+ ):+ """Verify default provider printing and initial provider selection index."""+ mock_get_models.return_value = [+ 'openhands/o3',+ 'anthropic/claude-3-opus',+ 'openai/gpt-4',+ ]+ mock_organize.return_value = {+ 'openhands': {'models': ['o3'], 'separator': '/'},+ 'anthropic': {'models': ['claude-3-opus'], 'separator': '/'},+ 'openai': {'models': ['gpt-4'], 'separator': '/'},+ }++ session_instance = MagicMock()+ session_instance.prompt_async = AsyncMock(side_effect=['api-key-123'])+ mock_session.return_value = session_instance+ mock_confirm.side_effect = [1, 0, 0]++ await modify_llm_settings_basic(app_config, settings_store)++ # Assert printing of default provider+ default_print_calls = [+ c+ for c in mock_print.call_args_list+ if c+ and c[0]+ and isinstance(c[0][0], HTML)+ and 'Default provider:' in c[0][0].value+ ]+ assert default_print_calls, 'Default provider line was not printed'+ printed_html = default_print_calls[0][0][0].value+ assert 'anthropic' in printed_html++ # Assert initial_selection for provider prompt+ provider_confirm_call = mock_confirm.call_args_list[0]+ # initial_selection prefers current_provider (openai) over default_provider (anthropic)+ # VERIFIED_PROVIDERS = ['openhands', 'anthropic', 'openai'] → index of 'openai' is 2+ assert provider_confirm_call[1]['initial_selection'] == 2++ @pytest.fixture+ def app_config_with_existing(self):+ config = MagicMock(spec=OpenHandsConfig)+ llm_config = MagicMock()+ # Set existing configuration+ llm_config.model = 'anthropic/claude-3-opus'+ llm_config.api_key = SecretStr('existing-api-key')+ llm_config.base_url = None+ config.get_llm_config.return_value = llm_config+ config.set_llm_config = MagicMock()+ config.set_agent_config = MagicMock()++ agent_config = MagicMock()+ config.get_agent_config.return_value = agent_config++ # Set up security as a separate mock+ security_mock = MagicMock()+ security_mock.confirmation_mode = True+ config.security = security_mock++ return config++ @pytest.mark.asyncio+ @patch(+ 'openhands.cli.settings.VERIFIED_PROVIDERS',+ ['openhands', 'anthropic'],+ )+ @patch(+ 'openhands.cli.settings.VERIFIED_ANTHROPIC_MODELS',+ ['claude-3-opus', 'claude-3-sonnet'],+ )+ @patch('openhands.cli.settings.get_supported_llm_models')+ @patch('openhands.cli.settings.organize_models_and_providers')+ @patch('openhands.cli.settings.PromptSession')+ @patch('openhands.cli.settings.cli_confirm')+ @patch(+ 'openhands.cli.settings.LLMSummarizingCondenserConfig',+ MockLLMSummarizingCondenserConfig,+ )+ async def test_modify_llm_settings_basic_keep_existing_values(+ self,+ mock_confirm,+ mock_session,+ mock_organize,+ mock_get_models,+ app_config_with_existing,+ settings_store,+ ):+ """Test keeping existing configuration values by pressing Enter/selecting defaults."""+ # Setup mocks+ mock_get_models.return_value = ['anthropic/claude-3-opus', 'openai/gpt-4']+ mock_organize.return_value = {+ 'openhands': {'models': [], 'separator': '/'},+ 'anthropic': {+ 'models': ['claude-3-opus', 'claude-3-sonnet'],+ 'separator': '/',+ },+ }++ session_instance = MagicMock()+ # Simulate user pressing Enter to keep existing values+ session_instance.prompt_async = AsyncMock(+ side_effect=[+ '',+ ]+ )+ mock_session.return_value = session_instance++ # Mock cli_confirm to select existing provider and model (keeping current values)+ mock_confirm.side_effect = [+ 1, # Select anthropic (matches initial_selection position)+ 0, # Select first model option (use default)+ 0, # Save settings+ ]++ await modify_llm_settings_basic(app_config_with_existing, settings_store)++ # Check that initial_selection was used for provider selection+ provider_confirm_call = mock_confirm.call_args_list[0]+ # anthropic is at index 1 in VERIFIED_PROVIDERS ['openhands', 'anthropic']+ assert provider_confirm_call[1]['initial_selection'] == 1++ # Check that initial_selection was used for model selection+ model_confirm_call = mock_confirm.call_args_list[1]+ # claude-3-opus should be at index 0 in our mocked VERIFIED_OPENHANDS_MODELS+ assert 'initial_selection' in model_confirm_call[1]+ assert (+ model_confirm_call[1]['initial_selection'] == 0+ ) # claude-3-opus is at index 0++ # Verify API key prompt shows existing key indicator+ api_key_prompt_call = session_instance.prompt_async.call_args_list[0]+ prompt_text = api_key_prompt_call[0][0]+ assert 'exis***-key' in prompt_text+ assert 'ENTER to keep current' in prompt_text++ # Verify settings were saved with existing values (no changes)+ settings_store.store.assert_called_once()+ args, kwargs = settings_store.store.call_args+ settings = args[0]+ assert settings.llm_model == 'anthropic/claude-3-opus'+ assert settings.llm_api_key.get_secret_value() == 'existing-api-key'++ @pytest.mark.asyncio+ @patch(+ 'openhands.cli.settings.VERIFIED_PROVIDERS',+ ['openhands', 'anthropic'],+ )+ @patch(+ 'openhands.cli.settings.VERIFIED_ANTHROPIC_MODELS',+ ['claude-3-opus', 'claude-3-sonnet'],+ )+ @patch('openhands.cli.settings.get_supported_llm_models')+ @patch('openhands.cli.settings.organize_models_and_providers')+ @patch('openhands.cli.settings.PromptSession')+ @patch('openhands.cli.settings.cli_confirm')+ @patch(+ 'openhands.cli.settings.LLMSummarizingCondenserConfig',+ MockLLMSummarizingCondenserConfig,+ )+ async def test_modify_llm_settings_basic_change_only_api_key(+ self,+ mock_confirm,+ mock_session,+ mock_organize,+ mock_get_models,+ app_config_with_existing,+ settings_store,+ ):+ """Test changing only the API key while keeping provider and model."""+ # Setup mocks+ mock_get_models.return_value = ['anthropic/claude-3-opus']+ mock_organize.return_value = {+ 'openhands': {'models': [], 'separator': '/'},+ 'anthropic': {+ 'models': ['claude-3-opus', 'claude-3-sonnet'],+ 'separator': '/',+ },+ }++ session_instance = MagicMock()+ # User enters a new API key+ session_instance.prompt_async = AsyncMock(+ side_effect=[+ 'new-api-key-12345',+ ]+ )+ mock_session.return_value = session_instance++ # Keep existing provider and model+ mock_confirm.side_effect = [+ 1, # Select anthropic (matches initial_selection position)+ 0, # Select first model option (use default)+ 0, # Save settings+ ]++ await modify_llm_settings_basic(app_config_with_existing, settings_store)++ # Verify settings were saved with only API key changed+ settings_store.store.assert_called_once()+ args, kwargs = settings_store.store.call_args+ settings = args[0]+ # Model should remain the same+ assert settings.llm_model == 'anthropic/claude-3-opus'+ # API key should be the new one+ assert settings.llm_api_key.get_secret_value() == 'new-api-key-12345'++ @pytest.mark.asyncio+ @patch(+ 'openhands.cli.settings.VERIFIED_PROVIDERS',+ ['openhands', 'anthropic'],+ )+ @patch(+ 'openhands.cli.settings.VERIFIED_OPENHANDS_MODELS',+ ['claude-sonnet-4-20250514', 'claude-opus-4-20250514', 'o3'],+ )+ @patch('openhands.cli.settings.get_supported_llm_models')+ @patch('openhands.cli.settings.organize_models_and_providers')+ @patch('openhands.cli.settings.PromptSession')+ @patch('openhands.cli.settings.cli_confirm')+ @patch(+ 'openhands.cli.settings.LLMSummarizingCondenserConfig',+ MockLLMSummarizingCondenserConfig,+ )+ async def test_modify_llm_settings_basic_change_provider_and_model(+ self,+ mock_confirm,+ mock_session,+ mock_organize,+ mock_get_models,+ app_config_with_existing,+ settings_store,+ ):+ """Test changing provider and model requires re-entering API key when provider changes."""+ # Setup mocks+ mock_get_models.return_value = [+ 'openhands/claude-sonnet-4-20250514',+ 'openhands/claude-opus-4-20250514',+ 'openhands/o3',+ ]+ mock_organize.return_value = {+ 'openhands': {+ 'models': [+ 'claude-sonnet-4-20250514',+ 'claude-opus-4-20250514',+ 'o3',+ ],+ 'separator': '/',+ },+ 'anthropic': {+ 'models': ['claude-3-opus', 'claude-3-sonnet'],+ 'separator': '/',+ },+ }++ session_instance = MagicMock()+ # Must enter a new API key because provider changed (current key cleared)+ session_instance.prompt_async = AsyncMock(+ side_effect=[+ 'new-api-key-after-provider-change',+ ]+ )+ mock_session.return_value = session_instance++ # Change provider and model+ mock_confirm.side_effect = [+ 0, # Select openhands (index 0 in ['openhands', 'anthropic'])+ 2, # Select o3 (index 2 in ['claude-sonnet-4-20250514', 'claude-opus-4-20250514', 'o3'])+ 0, # Save settings+ ]++ await modify_llm_settings_basic(app_config_with_existing, settings_store)++ # Verify API key prompt does NOT show existing key indicator after provider change+ api_key_prompt_call = session_instance.prompt_async.call_args_list[0]+ prompt_text = api_key_prompt_call[0][0]+ assert '***' not in prompt_text+ assert 'ENTER to keep current' not in prompt_text++ # Verify settings were saved with new provider/model and new API key+ settings_store.store.assert_called_once()+ args, kwargs = settings_store.store.call_args+ settings = args[0]+ assert settings.llm_model == 'openhands/o3'+ # API key should be the newly entered one+ assert (+ settings.llm_api_key.get_secret_value()+ == 'new-api-key-after-provider-change'+ )++ @pytest.mark.asyncio+ @patch(+ 'openhands.cli.settings.VERIFIED_PROVIDERS',+ ['openhands', 'anthropic'],+ )+ @patch(+ 'openhands.cli.settings.VERIFIED_OPENHANDS_MODELS',+ ['anthropic/claude-3-opus', 'anthropic/claude-3-sonnet'],+ )+ @patch(+ 'openhands.cli.settings.VERIFIED_ANTHROPIC_MODELS',+ ['claude-sonnet-4-20250514', 'claude-3-opus'],+ )+ @patch('openhands.cli.settings.get_supported_llm_models')+ @patch('openhands.cli.settings.organize_models_and_providers')+ @patch('openhands.cli.settings.PromptSession')+ @patch('openhands.cli.settings.cli_confirm')+ @patch(+ 'openhands.cli.settings.LLMSummarizingCondenserConfig',+ MockLLMSummarizingCondenserConfig,+ )+ async def test_modify_llm_settings_basic_from_scratch(+ self,+ mock_confirm,+ mock_session,+ mock_organize,+ mock_get_models,+ settings_store,+ ):+ """Test setting up LLM configuration from scratch (no existing settings)."""+ # Create a fresh config with no existing LLM settings+ config = MagicMock(spec=OpenHandsConfig)+ llm_config = MagicMock()+ llm_config.model = None # No existing model+ llm_config.api_key = None # No existing API key+ llm_config.base_url = None+ config.get_llm_config.return_value = llm_config+ config.set_llm_config = MagicMock()+ config.set_agent_config = MagicMock()++ agent_config = MagicMock()+ config.get_agent_config.return_value = agent_config++ # Set up security as a separate mock+ security_mock = MagicMock()+ security_mock.confirmation_mode = True+ config.security = security_mock++ config.enable_default_condenser = True+ config.default_agent = 'test-agent'+ config.file_store_path = '/tmp'++ # Setup mocks+ mock_get_models.return_value = [+ 'anthropic/claude-sonnet-4-20250514',+ 'anthropic/claude-3-opus',+ ]+ mock_organize.return_value = {+ 'openhands': {'models': [], 'separator': '/'},+ 'anthropic': {+ 'models': ['claude-sonnet-4-20250514', 'claude-3-opus'],+ 'separator': '/',+ },+ }++ session_instance = MagicMock()+ # User enters a new API key (no existing key to keep)+ session_instance.prompt_async = AsyncMock(+ side_effect=[+ 'new-api-key-12345',+ ]+ )+ mock_session.return_value = session_instance++ # Mock cli_confirm to select anthropic provider and use default model+ mock_confirm.side_effect = [+ 1, # Select anthropic (index 1 in ['openhands', 'anthropic'])+ 0, # Use default model (claude-sonnet-4-20250514)+ 0, # Save settings+ ]++ await modify_llm_settings_basic(config, settings_store)++ # Check that initial_selection was used for provider selection+ provider_confirm_call = mock_confirm.call_args_list[0]+ # Since there's no existing provider, it defaults to 'anthropic' which is at index 1+ assert provider_confirm_call[1]['initial_selection'] == 1++ # Check that initial_selection was used for model selection+ model_confirm_call = mock_confirm.call_args_list[1]+ # Since there's no existing model, it should default to using the first option (default model)+ assert 'initial_selection' in model_confirm_call[1]+ assert model_confirm_call[1]['initial_selection'] == 0++ # Verify API key prompt does NOT show existing key indicator+ api_key_prompt_call = session_instance.prompt_async.call_args_list[0]+ prompt_text = api_key_prompt_call[0][0]+ assert '***' not in prompt_text+ assert 'ENTER to keep current' not in prompt_text++ # Verify settings were saved with new values+ settings_store.store.assert_called_once()+ args, kwargs = settings_store.store.call_args+ settings = args[0]+ assert settings.llm_model == 'anthropic/claude-sonnet-4-20250514'+ assert settings.llm_api_key.get_secret_value() == 'new-api-key-12345'+class TestModifyLLMSettingsAdvanced:@pytest.fixture@@ -449,6 +810,8 @@ class TestModifyLLMSettingsAdvanced:config.get_llm_config.return_value = llm_configconfig.set_llm_config = MagicMock()config.set_agent_config = MagicMock()+ config.default_agent = 'test-agent'+ config.enable_default_condenser = Trueagent_config = MagicMock()config.get_agent_config.return_value = agent_config@@ -654,6 +1017,342 @@ class TestModifyLLMSettingsAdvanced:app_config.set_llm_config.assert_not_called()settings_store.store.assert_not_called()+ @pytest.fixture+ def app_config_with_existing(self):+ config = MagicMock(spec=OpenHandsConfig)+ llm_config = MagicMock()+ llm_config.model = 'custom-existing-model'+ llm_config.api_key = SecretStr('existing-advanced-key')+ llm_config.base_url = 'https://existing-api.com'+ config.get_llm_config.return_value = llm_config+ config.set_llm_config = MagicMock()+ config.set_agent_config = MagicMock()+ config.default_agent = 'existing-agent'+ config.enable_default_condenser = False++ agent_config = MagicMock()+ config.get_agent_config.return_value = agent_config++ # Set up security as a separate mock+ security_mock = MagicMock()+ security_mock.confirmation_mode = False+ config.security = security_mock++ return config++ @pytest.mark.asyncio+ @patch('openhands.cli.settings.Agent.list_agents')+ @patch('openhands.cli.settings.PromptSession')+ @patch('openhands.cli.settings.cli_confirm')+ @patch(+ 'openhands.cli.settings.LLMSummarizingCondenserConfig',+ MockLLMSummarizingCondenserConfig,+ )+ @patch(+ 'openhands.cli.settings.ConversationWindowCondenserConfig',+ MockConversationWindowCondenserConfig,+ )+ @patch(+ 'openhands.cli.settings.CondenserPipelineConfig', MockCondenserPipelineConfig+ )+ async def test_modify_llm_settings_advanced_keep_existing_values(+ self,+ mock_confirm,+ mock_session,+ mock_list_agents,+ app_config_with_existing,+ settings_store,+ ):+ """Test keeping all existing values in advanced settings by pressing Enter."""+ # Setup mocks+ mock_list_agents.return_value = ['default', 'existing-agent', 'test-agent']++ session_instance = MagicMock()+ session_instance.prompt_async = AsyncMock(+ side_effect=[+ 'custom-existing-model', # Keep existing model (simulating prefill behavior)+ 'https://existing-api.com', # Keep existing base URL (simulating prefill behavior)+ '', # Keep existing API key (press Enter)+ 'existing-agent', # Keep existing agent (simulating prefill behavior)+ ]+ )+ mock_session.return_value = session_instance++ # Mock user confirmations+ mock_confirm.side_effect = [+ 1, # Disable confirmation mode (current is False)+ 1, # Disable memory condensation (current is False)+ 0, # Save settings+ ]++ await modify_llm_settings_advanced(app_config_with_existing, settings_store)++ # Verify all prompts were called with prefill=True and current values+ prompt_calls = session_instance.prompt_async.call_args_list++ # Check model prompt+ assert prompt_calls[0][1]['default'] == 'custom-existing-model'++ # Check base URL prompt+ assert prompt_calls[1][1]['default'] == 'https://existing-api.com'++ # Check API key prompt (should not prefill but show indicator)+ api_key_prompt = prompt_calls[2][0][0]+ assert 'exis***-key' in api_key_prompt+ assert 'ENTER to keep current' in api_key_prompt+ assert prompt_calls[2][1]['default'] == '' # Not prefilled for security++ # Check agent prompt+ assert prompt_calls[3][1]['default'] == 'existing-agent'++ # Verify initial selections for confirmation mode and condenser+ confirm_calls = mock_confirm.call_args_list+ assert confirm_calls[0][1]['initial_selection'] == 1 # Disable (current)+ assert confirm_calls[1][1]['initial_selection'] == 1 # Disable (current)++ # Verify settings were saved with existing values+ settings_store.store.assert_called_once()+ args, kwargs = settings_store.store.call_args+ settings = args[0]+ assert settings.llm_model == 'custom-existing-model'+ assert settings.llm_api_key.get_secret_value() == 'existing-advanced-key'+ assert settings.llm_base_url == 'https://existing-api.com'+ assert settings.agent == 'existing-agent'+ assert settings.confirmation_mode is False+ assert settings.enable_default_condenser is False++ @pytest.mark.asyncio+ @patch('openhands.cli.settings.Agent.list_agents')+ @patch('openhands.cli.settings.PromptSession')+ @patch('openhands.cli.settings.cli_confirm')+ @patch(+ 'openhands.cli.settings.LLMSummarizingCondenserConfig',+ MockLLMSummarizingCondenserConfig,+ )+ @patch(+ 'openhands.cli.settings.ConversationWindowCondenserConfig',+ MockConversationWindowCondenserConfig,+ )+ @patch(+ 'openhands.cli.settings.CondenserPipelineConfig', MockCondenserPipelineConfig+ )+ async def test_modify_llm_settings_advanced_partial_change(+ self,+ mock_confirm,+ mock_session,+ mock_list_agents,+ app_config_with_existing,+ settings_store,+ ):+ """Test changing only some values in advanced settings while keeping others."""+ # Setup mocks+ mock_list_agents.return_value = ['default', 'existing-agent', 'test-agent']++ session_instance = MagicMock()+ session_instance.prompt_async = AsyncMock(+ side_effect=[+ 'new-custom-model', # Change model+ 'https://existing-api.com', # Keep existing base URL (simulating prefill behavior)+ 'new-api-key-123', # Change API key+ 'test-agent', # Change agent+ ]+ )+ mock_session.return_value = session_instance++ # Mock user confirmations - change some settings+ mock_confirm.side_effect = [+ 0, # Enable confirmation mode (change from current False)+ 1, # Disable memory condensation (keep current False)+ 0, # Save settings+ ]++ await modify_llm_settings_advanced(app_config_with_existing, settings_store)++ # Verify settings were saved with mixed changes+ settings_store.store.assert_called_once()+ args, kwargs = settings_store.store.call_args+ settings = args[0]+ assert settings.llm_model == 'new-custom-model' # Changed+ assert settings.llm_api_key.get_secret_value() == 'new-api-key-123' # Changed+ assert settings.llm_base_url == 'https://existing-api.com' # Kept same+ assert settings.agent == 'test-agent' # Changed+ assert settings.confirmation_mode is True # Changed+ assert settings.enable_default_condenser is False # Kept same++ @pytest.mark.asyncio+ @patch('openhands.cli.settings.Agent.list_agents')+ @patch('openhands.cli.settings.PromptSession')+ @patch('openhands.cli.settings.cli_confirm')+ @patch(+ 'openhands.cli.settings.LLMSummarizingCondenserConfig',+ MockLLMSummarizingCondenserConfig,+ )+ @patch(+ 'openhands.cli.settings.ConversationWindowCondenserConfig',+ MockConversationWindowCondenserConfig,+ )+ @patch(+ 'openhands.cli.settings.CondenserPipelineConfig', MockCondenserPipelineConfig+ )+ async def test_modify_llm_settings_advanced_from_scratch(+ self, mock_confirm, mock_session, mock_list_agents, settings_store+ ):+ """Test setting up advanced configuration from scratch (no existing settings)."""+ # Create a fresh config with no existing settings+ config = MagicMock(spec=OpenHandsConfig)+ llm_config = MagicMock()+ llm_config.model = None # No existing model+ llm_config.api_key = None # No existing API key+ llm_config.base_url = None # No existing base URL+ config.get_llm_config.return_value = llm_config+ config.set_llm_config = MagicMock()+ config.set_agent_config = MagicMock()+ config.default_agent = None # No existing agent+ config.enable_default_condenser = True # Default value++ agent_config = MagicMock()+ config.get_agent_config.return_value = agent_config++ # Set up security as a separate mock+ security_mock = MagicMock()+ security_mock.confirmation_mode = True # Default value+ config.security = security_mock++ # Setup mocks+ mock_list_agents.return_value = ['default', 'test-agent', 'advanced-agent']++ session_instance = MagicMock()+ session_instance.prompt_async = AsyncMock(+ side_effect=[+ 'from-scratch-model', # New custom model+ 'https://new-api-endpoint.com', # New base URL+ 'brand-new-api-key', # New API key+ 'advanced-agent', # New agent+ ]+ )+ mock_session.return_value = session_instance++ # Mock user confirmations - set up from scratch+ mock_confirm.side_effect = [+ 1, # Disable confirmation mode (change from default True)+ 0, # Enable memory condensation (keep default True)+ 0, # Save settings+ ]++ await modify_llm_settings_advanced(config, settings_store)++ # Check that prompts don't show prefilled values since nothing exists+ prompt_calls = session_instance.prompt_async.call_args_list++ # Check model prompt - no prefill for empty model+ assert prompt_calls[0][1]['default'] == ''++ # Check base URL prompt - no prefill for empty base_url+ assert prompt_calls[1][1]['default'] == ''++ # Check API key prompt - should not show existing key indicator+ api_key_prompt = prompt_calls[2][0][0]+ assert '***' not in api_key_prompt+ assert 'ENTER to keep current' not in api_key_prompt+ assert prompt_calls[2][1]['default'] == '' # Not prefilled++ # Check agent prompt - no prefill for empty agent+ assert prompt_calls[3][1]['default'] == ''++ # Verify initial selections for confirmation mode and condenser+ confirm_calls = mock_confirm.call_args_list+ assert confirm_calls[0][1]['initial_selection'] == 0 # Enable (default)+ assert confirm_calls[1][1]['initial_selection'] == 0 # Enable (default)++ # Verify settings were saved with new values+ settings_store.store.assert_called_once()+ args, kwargs = settings_store.store.call_args+ settings = args[0]+ assert settings.llm_model == 'from-scratch-model'+ assert settings.llm_api_key.get_secret_value() == 'brand-new-api-key'+ assert settings.llm_base_url == 'https://new-api-endpoint.com'+ assert settings.agent == 'advanced-agent'+ assert settings.confirmation_mode is False # Changed from default+ assert settings.enable_default_condenser is True # Kept default+++class TestGetValidatedInput:+ @pytest.mark.asyncio+ @patch('openhands.cli.settings.PromptSession')+ async def test_get_validated_input_with_prefill(self, mock_session):+ """Test get_validated_input with default_value prefilled."""+ from openhands.cli.settings import get_validated_input++ session_instance = MagicMock()+ session_instance.prompt_async = AsyncMock(return_value='modified-value')++ result = await get_validated_input(+ session_instance,+ 'Enter value: ',+ default_value='existing-value',+ )++ # Verify prompt was called with default parameter+ session_instance.prompt_async.assert_called_once_with(+ 'Enter value: ', default='existing-value'+ )+ assert result == 'modified-value'++ @pytest.mark.asyncio+ @patch('openhands.cli.settings.PromptSession')+ async def test_get_validated_input_empty_returns_current(self, mock_session):+ """Test that pressing Enter with empty input returns enter_keeps_value."""+ from openhands.cli.settings import get_validated_input++ session_instance = MagicMock()+ # Simulate user pressing Enter (empty input)+ session_instance.prompt_async = AsyncMock(return_value=' ')++ result = await get_validated_input(+ session_instance,+ 'Enter value: ',+ default_value='',+ enter_keeps_value='existing-value',+ )++ # Verify prompt was called with empty default+ session_instance.prompt_async.assert_called_once_with(+ 'Enter value: ', default=''+ )+ # Empty input should return enter_keeps_value+ assert result == 'existing-value'++ @pytest.mark.asyncio+ @patch('openhands.cli.settings.PromptSession')+ async def test_get_validated_input_with_validator(self, mock_session):+ """Test get_validated_input with validator and error message."""+ from openhands.cli.settings import get_validated_input++ session_instance = MagicMock()+ # First attempt fails validation, second succeeds+ session_instance.prompt_async = AsyncMock(+ side_effect=['invalid', 'valid-input']+ )++ # Mock print_formatted_text to verify error message+ with patch('openhands.cli.settings.print_formatted_text') as mock_print:+ result = await get_validated_input(+ session_instance,+ 'Enter value: ',+ validator=lambda x: x.startswith('valid'),+ error_message='Input must start with "valid"',+ )++ # Verify error message was shown+ assert mock_print.call_count == 3+ # The second call contains the error message+ error_message_call = mock_print.call_args_list[1]+ args, kwargs = error_message_call+ assert isinstance(args[0], HTML)+ assert 'Input must start with "valid"' in args[0].value++ assert result == 'valid-input'+class TestModifySearchApiSettings:@pytest.fixture