Repository navigation
ApplicationOptions configurability #656
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Rodrigo Brandão (rodrigobr-msft)
merged 13 commits into
main
from
users/robrandao/app-config
Nov 19, 2026
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
dbccb19
ApplicationOptions configurability
rodrigobr-msft cdec055
Merge branch 'main' into users/robrandao/app-config
rodrigobr-msft a05df5a
Merge branch 'main' of https://github.com/microsoft/Agents-for-python…
rodrigobr-msft 3cc11d9
Preserving back-compatibility with arbitrary kwarg handling in AgentA…
rodrigobr-msft e28bd4c
Merge branch 'users/robrandao/app-config' of https://github.com/micro…
rodrigobr-msft 090d689
Updating changelog
rodrigobr-msft 6613d66
Improved back-compat
rodrigobr-msft 07df07c
Changelog update
rodrigobr-msft cefa536
Fixing root config handling
rodrigobr-msft a38bc06
fixing issue
rodrigobr-msft 3a63f43
Metadata handling
rodrigobr-msft ba3bc98
Fixing changelog conflict
rodrigobr-msft 6832cef
Adding improved test coverage
rodrigobr-msft File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,205 @@ | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. | ||
|
|
||
| from unittest.mock import Mock | ||
|
|
||
| import pytest | ||
|
|
||
| from microsoft_agents.activity.config import load_configuration_from_env | ||
| from microsoft_agents.hosting.core import MemoryStorage | ||
| from microsoft_agents.hosting.core.app import ( | ||
| AgentApplication, | ||
| ApplicationOptions, | ||
| InputFileDownloader, | ||
| TurnState, | ||
| ) | ||
| from microsoft_agents.hosting.core.app.oauth import Authorization, AuthHandlerConfig | ||
| from microsoft_agents.hosting.core.app.proactive import ProactiveOptions | ||
| from microsoft_agents.hosting.core.app.typing_indicator import TypingOptions | ||
| from microsoft_agents.hosting.core.channel_service_adapter import ChannelServiceAdapter | ||
| from tests._common import create_configuration | ||
| from tests._common.testing_objects import TestingConnectionManager as _ConnectionManager | ||
|
|
||
|
|
||
| def _key(name, style): | ||
| if style == "pascal": | ||
| return "".join(part.capitalize() for part in name.split("_")) | ||
| if style == "upper": | ||
| return name.upper() | ||
| return name | ||
|
|
||
|
|
||
| def _configuration(values, is_root): | ||
| return create_configuration( | ||
| {"AgentApplication": values} if is_root else values, | ||
| is_root=is_root, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("is_root", [False, True], ids=["scoped", "root"]) | ||
| @pytest.mark.parametrize("style", ["snake", "pascal", "upper"]) | ||
| def test_binds_scalar_settings_with_configuration_aliases(is_root, style): | ||
| values = { | ||
| "bot_app_id": "test-agent", | ||
| "start_typing_timer": "false", | ||
| "remove_recipient_mention": "0", | ||
| "long_running_messages": "true", | ||
| } | ||
| config = _configuration( | ||
| {_key(name, style): value for name, value in values.items()}, is_root | ||
| ) | ||
|
|
||
| options = ApplicationOptions.from_config(config) | ||
|
|
||
| assert options.bot_app_id == "test-agent" | ||
| assert options.start_typing_timer is False | ||
| assert options.remove_recipient_mention is False | ||
| assert options.long_running_messages is True | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def runtime_dependencies(): | ||
| return { | ||
| "storage": MemoryStorage(), | ||
| "typing": TypingOptions(initial_delay_ms=100, interval_ms=2000), | ||
| "proactive": ProactiveOptions(fail_on_unsigned_in_connections=False), | ||
| "turn_state_factory": TurnState, | ||
| "file_downloaders": [Mock(spec=InputFileDownloader)], | ||
| "adapter": Mock(spec=ChannelServiceAdapter), | ||
| "authorization_handlers": {"graph": AuthHandlerConfig(name="graph")}, | ||
| } | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("is_root", [False, True], ids=["scoped", "root"]) | ||
| def test_preserves_runtime_dependencies(runtime_dependencies, is_root): | ||
| config = _configuration(runtime_dependencies, is_root) | ||
|
|
||
| options = ApplicationOptions.from_config(config) | ||
|
|
||
| for name in ("storage", "typing", "proactive", "turn_state_factory", "adapter"): | ||
| assert getattr(options, name) is runtime_dependencies[name], name | ||
| assert options.file_downloaders[0] is runtime_dependencies["file_downloaders"][0] | ||
| assert ( | ||
| options.authorization_handlers["graph"] | ||
| is runtime_dependencies["authorization_handlers"]["graph"] | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "name, value, message", | ||
| [ | ||
| ("storage", object(), "storage must be an instance of Storage"), | ||
| ("typing", {}, "typing must be an instance of TypingOptions"), | ||
| ("proactive", {}, "proactive must be an instance of ProactiveOptions"), | ||
| ("adapter", object(), "adapter must be an instance of ChannelServiceAdapter"), | ||
| ("authorization_handlers", [], "authorization_handlers must be a mapping"), | ||
| ("file_downloaders", object(), "file_downloaders must be a list"), | ||
| ("file_downloaders", [object()], "each item in file_downloaders"), | ||
| ], | ||
| ) | ||
| def test_rejects_invalid_runtime_dependencies(name, value, message): | ||
| with pytest.raises(TypeError, match=message): | ||
| ApplicationOptions.from_config({name: value}) | ||
|
|
||
|
|
||
| def test_invalid_scalar_reports_root_configuration_path_without_value(): | ||
| config = _configuration({"StartTypingTimer": "invalid-setting-value"}, True) | ||
|
|
||
| with pytest.raises(ValueError, match="AgentApplication::StartTypingTimer") as error: | ||
| ApplicationOptions.from_config(config) | ||
|
|
||
| assert "invalid-setting-value" not in str(error.value) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("bind_options", [False, True], ids=["legacy-kwargs", "bound"]) | ||
| def test_application_preserves_legacy_scope_and_forwards_authorization(bind_options): | ||
| config = load_configuration_from_env( | ||
| { | ||
| "AGENTAPPLICATION__STARTTYPINGTIMER": "false", | ||
| "AGENTAPPLICATION__REMOVERECIPIENTMENTION": "false", | ||
| "AGENTAPPLICATION__USERAUTHORIZATION__AUTO_SIGN_IN": "true", | ||
| } | ||
| ) | ||
| original_config = dict(config) | ||
| storage = MemoryStorage() | ||
| kwargs = ( | ||
| {"options": ApplicationOptions.from_config(config), "config": config} | ||
| if bind_options | ||
| else dict(config) | ||
| ) | ||
|
|
||
| app = AgentApplication( | ||
| storage=storage, | ||
| connection_manager=_ConnectionManager(), | ||
| **kwargs, | ||
| ) | ||
|
|
||
| assert app.options.start_typing_timer is not bind_options | ||
| assert app.options.remove_recipient_mention is not bind_options | ||
| assert app.options.storage is storage | ||
| assert app.auth._auto_sign_in is True | ||
| assert isinstance(app._turn_state_factory(), TurnState) | ||
| assert config == original_config | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "kwargs, error_type, message", | ||
| [ | ||
| ( | ||
| {"start_typing_timer": "invalid-setting"}, | ||
| ValueError, | ||
| "Invalid boolean for start_typing_timer", | ||
| ), | ||
| ( | ||
| {"typing": {"invalid": True}}, | ||
| TypeError, | ||
| "typing must be an instance of TypingOptions", | ||
| ), | ||
| ], | ||
| ) | ||
| def test_explicit_options_still_validate_kwargs(kwargs, error_type, message): | ||
| storage = MemoryStorage() | ||
|
|
||
| with pytest.raises(error_type, match=message): | ||
| AgentApplication( | ||
| options=ApplicationOptions(storage=storage, start_typing_timer=False), | ||
| authorization=Authorization(storage, _ConnectionManager()), | ||
| **kwargs, | ||
| ) | ||
|
|
||
|
|
||
| def test_explicit_options_override_valid_kwargs(): | ||
| storage = MemoryStorage() | ||
| app = AgentApplication( | ||
| options=ApplicationOptions(storage=storage, start_typing_timer=False), | ||
| authorization=Authorization(storage, _ConnectionManager()), | ||
| storage=MemoryStorage(), | ||
| start_typing_timer=True, | ||
| ) | ||
|
|
||
| assert app.options.storage is storage | ||
| assert app.options.start_typing_timer is False | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("has_explicit_downloaders", [False, True]) | ||
| def test_downloaders_use_kwargs_when_explicit_options_are_empty( | ||
| has_explicit_downloaders, | ||
| ): | ||
| storage = MemoryStorage() | ||
| explicit_downloaders = ( | ||
| [Mock(spec=InputFileDownloader)] if has_explicit_downloaders else [] | ||
| ) | ||
| fallback_downloader = Mock(spec=InputFileDownloader) | ||
| app = AgentApplication( | ||
| options=ApplicationOptions( | ||
| storage=storage, file_downloaders=explicit_downloaders | ||
| ), | ||
| authorization=Authorization(storage, _ConnectionManager()), | ||
| file_downloaders=[fallback_downloader], | ||
| ) | ||
|
|
||
| expected = ( | ||
| explicit_downloaders[0] if has_explicit_downloaders else fallback_downloader | ||
| ) | ||
| assert len(app.options.file_downloaders) == 1 | ||
| assert app.options.file_downloaders[0] is expected |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.