Skip to content

Lazy creation of Teams ApiClient - #505

Closed
Rodrigo Brandão (rodrigobr-msft) wants to merge 1 commit into
mainfrom
users/robrandao/lazy-client
Closed

Lazy creation of Teams ApiClient#505
Rodrigo Brandão (rodrigobr-msft) wants to merge 1 commit into
mainfrom
users/robrandao/lazy-client

Conversation

@rodrigobr-msft

Copy link
Copy Markdown
Contributor

No description provided.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the Microsoft Teams hosting layer so the microsoft_teams.api.ApiClient is no longer eagerly created during the Teams before_turn hook, and instead is created/cached on-demand when requested via TeamsTurnContext.api_client or TeamsAgentExtension.get_teams_api_client().

Changes:

  • Remove eager Teams ApiClient creation from the Teams before_turn hook and adjust tests accordingly.
  • Make TeamsTurnContext.api_client and TeamsAgentExtension.get_teams_api_client() lazily create/cache an ApiClient when absent.
  • Simplify the Teams API client helper by removing _get_teams_api_client and making _set_teams_api_client return the created/cached client.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
libraries/microsoft-agents-hosting-msteams/microsoft_agents/hosting/msteams/teams_turn_context.py Switch api_client accessor to lazy-get/create behavior.
libraries/microsoft-agents-hosting-msteams/microsoft_agents/hosting/msteams/teams_agent_extension.py Stop creating ApiClient in before_turn; lazily create in get_teams_api_client.
libraries/microsoft-agents-hosting-msteams/microsoft_agents/hosting/msteams/_teams_api_client.py Remove _get_teams_api_client; make _set_teams_api_client return the client.
tests/hosting_msteams/test_teams_turn_context.py Add a unit test for returning a cached Teams ApiClient.
tests/hosting_msteams/test_teams_agent_extension.py Update hook test expectations after removing eager ApiClient creation.
tests/hosting_msteams/test_internal.py Remove tests for deleted _get_teams_api_client helper.
tests/_common/testing_objects/mocks/mock_user_token_client.py Reformat an AsyncMock assignment (no functional change).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +99 to +102
api_client = self._services.get(ApiClient)
if not api_client:
return _set_teams_api_client(self, self._app.connection_manager)
return api_client
Comment on lines +300 to +303
api_client = context.services.get(ApiClient)
if not api_client:
return _set_teams_api_client(context, self._app.connection_manager)
return api_client
Comment on lines 23 to 28
"""
Set the Teams API client in the context if it is not already set.

:param context: The turn context.
:param connection_manager: The connection manager.
"""
Comment on lines 125 to 128
assert result is True
assert isinstance(activity.channel_data, ChannelData)
assert activity.channel_data.channel.id == "c1"
assert ctx.services.has(ApiClient)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use lazy and async-wrapped creation of Teams ApiClient to avoid unnecessary performance hit

2 participants