Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR refactors the TypingIndicator class from an asyncio-based implementation to a threading-based implementation using threading.Timer. The main changes include replacing the continuous asyncio task loop with a single-shot timer and simplifying the start/stop logic.
- Changed from asyncio-based task management to threading-based
Timer - Replaced continuous loop with single timer execution
- Changed default interval from 1 second to 10 units (with unit mismatch in log message)
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/typing_indicator.py
Outdated
Show resolved
Hide resolved
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/typing_indicator.py
Outdated
Show resolved
Hide resolved
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/typing_indicator.py
Show resolved
Hide resolved
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/typing_indicator.py
Outdated
Show resolved
Hide resolved
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/typing_indicator.py
Outdated
Show resolved
Hide resolved
…into users/robrandao/typing-indicator
There was a problem hiding this comment.
Pull Request Overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/typing_indicator.py
Outdated
Show resolved
Hide resolved
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/typing_indicator.py
Outdated
Show resolved
Hide resolved
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/typing_indicator.py
Outdated
Show resolved
Hide resolved
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/typing_indicator.py
Outdated
Show resolved
Hide resolved
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/typing_indicator.py
Outdated
Show resolved
Hide resolved
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/typing_indicator.py
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Pull Request Overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 7 comments.
Comments suppressed due to low confidence (2)
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/agent_application.py:736
- The
stop()method is now synchronous and should not be awaited. Remove theawaitkeyword:typing.stop()
typing.stop()
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/typing_indicator.py:11
- Import of 'datetime' is not used.
Import of 'timedelta' is not used.
from datetime import datetime, timedelta
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/typing_indicator.py
Outdated
Show resolved
Hide resolved
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/agent_application.py
Outdated
Show resolved
Hide resolved
…ng/core/app/typing_indicator.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull Request Overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 26 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/typing_indicator.py
Outdated
Show resolved
Hide resolved
…/microsoft/Agents-for-python into users/robrandao/typing-indicator
There was a problem hiding this comment.
Pull Request Overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.
Comments suppressed due to low confidence (1)
tests/hosting_core/app/test_typing_indicator.py:11
- Import of 'TurnContext' is not used.
from microsoft_agents.hosting.core import TurnContext
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/typing_indicator.py
Outdated
Show resolved
Hide resolved
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/typing_indicator.py
Outdated
Show resolved
Hide resolved
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/agent_application.py
Show resolved
Hide resolved
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/typing_indicator.py
Show resolved
Hide resolved
There was a problem hiding this comment.
Pull Request Overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/typing_indicator.py
Show resolved
Hide resolved
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/agent_application.py
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Pull Request Overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/typing_indicator.py
Show resolved
Hide resolved
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/typing_indicator.py
Outdated
Show resolved
Hide resolved
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/typing_indicator.py
Outdated
Show resolved
Hide resolved
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/typing_indicator.py
Outdated
Show resolved
Hide resolved
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/typing_indicator.py
Show resolved
Hide resolved
There was a problem hiding this comment.
Pull Request Overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/typing_indicator.py
Show resolved
Hide resolved
libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/typing_indicator.py
Show resolved
Hide resolved
…into users/robrandao/typing-indicator
…/microsoft/Agents-for-python into users/robrandao/typing-indicator
There was a problem hiding this comment.
Pull Request Overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| async def _run(self) -> None: | ||
| """Sends typing indicators at regular intervals.""" | ||
|
|
||
| running_task = self._task | ||
| try: | ||
| while True: | ||
| # Check running status under lock | ||
| async with self._lock: | ||
| if not self._running: | ||
| break | ||
|
|
||
| try: | ||
| logger.debug("Sending typing activity") | ||
| await context.send_activity(Activity(type=ActivityTypes.typing)) | ||
| except Exception as e: | ||
| logger.error(f"Error sending typing activity: {e}") | ||
| async with self._lock: | ||
| self._running = False | ||
| break | ||
|
|
||
| await asyncio.sleep(self._intervalSeconds) | ||
| while running_task is self._task: | ||
| await self._context.send_activity(Activity(type=ActivityTypes.typing)) | ||
| await asyncio.sleep(self._interval) |
There was a problem hiding this comment.
There's a race condition in the _run method. If stop() is called immediately after start() but before the task begins executing _run(), then when _run() starts:
- Line 40:
running_task = self._taskwill beNone(since stop() set it to None) - Line 42:
while None is self._taskevaluates towhile None is None, which isTrue - The loop will execute and try to send a typing activity even though stop() was already called
A more robust approach would be to initialize running_task to the current task object:
async def _run(self) -> None:
"""Sends typing indicators at regular intervals."""
running_task = asyncio.current_task()
try:
while running_task is self._task:
await self._context.send_activity(Activity(type=ActivityTypes.typing))
await asyncio.sleep(self._interval)
except asyncio.CancelledError:
passThis ensures the identity check works correctly even if stop() is called before _run() executes.
| """Test that start() begins sending typing activities at regular interval_secondss.""" | ||
| context = StubTurnContext() | ||
| indicator = TypingIndicator(intervalSeconds=0.01) | ||
| indicator = TypingIndicator(context, interval_seconds=0.01) # 10ms interval_seconds |
There was a problem hiding this comment.
The inline comment has a typo: "10ms interval_seconds" should be "10ms interval" (the parameter name shouldn't appear in the comment describing the value).
| indicator = TypingIndicator(context, interval_seconds=0.01) # 10ms interval_seconds | |
| indicator = TypingIndicator(context, interval_seconds=0.01) # 10ms interval |
|
|
||
| await asyncio.sleep(self._intervalSeconds) | ||
| while running_task is self._task: | ||
| await self._context.send_activity(Activity(type=ActivityTypes.typing)) |
There was a problem hiding this comment.
Missing error handling in the _run() method. If send_activity() raises an exception (e.g., network error, bot framework error), the task will crash and stop sending typing indicators without any logging or graceful handling.
The old implementation caught exceptions and logged them. Consider adding try-except handling around the send_activity call:
try:
await self._context.send_activity(Activity(type=ActivityTypes.typing))
except Exception as e:
logger.error("Error sending typing activity: %s", e)
# Optionally: break or continue depending on desired behavior| await self._context.send_activity(Activity(type=ActivityTypes.typing)) | |
| try: | |
| await self._context.send_activity(Activity(type=ActivityTypes.typing)) | |
| except Exception as e: | |
| logger.error("Error sending typing activity: %s", e) |
This pull request refactors the
TypingIndicatorclass to simplify its usage and improve reliability. The main changes include removing asynchronous locking and state management, updating the API to use explicitstart()andstop()methods, and revising the typing indicator logic to be scoped to a single conversation turn. The associated tests have been updated to reflect the new API and ensure robust behavior.TypingIndicator class refactor:
TypingIndicatorto acceptTurnContextand interval in the constructor, removed internal state and locking, and replaced the asynchronous start/stop methods with synchronousstart()andstop()methods. The indicator now sends typing activities at regular intervals using an internal asyncio task, and raises exceptions if started or stopped in an invalid state. (typing_indicator.py)agent_application.pyto match the new API: replacedawait typing.start(context)andawait typing.stop()withtyping.start()andtyping.stop(). (agent_application.py) [1] [2]Testing improvements:
TypingIndicatorto cover new behaviors, including correct activity sending, error handling for invalid state transitions, and multiple start/stop cycles. (test_typing_indicator.py)test_typing_indicator.py)Code style and imports:
typing_indicator.py.