Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
Pattern,
TypeVar,
cast,
overload,
)

from microsoft_agents.activity import (
Expand Down Expand Up @@ -312,6 +313,8 @@ async def on_event(context: TurnContext, state: TurnState):
"""

def __selector(context: TurnContext):
if isinstance(activity_type, list):
return context.activity.type in activity_type
return activity_type == context.activity.type

def __call(func: RouteHandler[StateT]) -> RouteHandler[StateT]:
Expand Down Expand Up @@ -353,9 +356,18 @@ def __selector(context: TurnContext):
return False

text = context.activity.text if context.activity.text else ""

if isinstance(select, list):
for item in select:
if isinstance(item, Pattern):
if re.fullmatch(item, text) is not None:
return True
Comment on lines +360 to +364

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Since compiled experssions are ususally the preferred pattern could you take this suggestion?

elif text == item:
return True
return False

if isinstance(select, Pattern):
hits = re.fullmatch(select, text)
return hits is not None
return re.fullmatch(select, text) is not None
Comment on lines 369 to +370

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Same here


return text == select

Expand Down Expand Up @@ -535,12 +547,36 @@ def __call(func: RouteHandler[StateT]) -> RouteHandler[StateT]:

return __call

@overload
def handoff(
self,
func: Callable[[TurnContext, StateT, str], Awaitable[None]],
) -> Callable[[TurnContext, StateT, str], Awaitable[None]]: ...
Comment on lines +550 to +554

@overload
def handoff(
self, *, auth_handlers: Optional[list[str]] = None, **kwargs
self,
*,
auth_handlers: Optional[list[str]] = None,
**kwargs: Any,
) -> Callable[
[Callable[[TurnContext, StateT, str], Awaitable[None]]],
Callable[[TurnContext, StateT, str], Awaitable[None]],
]:
]: ...
Comment thread
rodrigobr-msft marked this conversation as resolved.

def handoff(
self,
func: Optional[Callable[[TurnContext, StateT, str], Awaitable[None]]] = None,
*,
auth_handlers: Optional[list[str]] = None,
**kwargs,
) -> (
Callable[[TurnContext, StateT, str], Awaitable[None]]
| Callable[
[Callable[[TurnContext, StateT, str], Awaitable[None]]],
Callable[[TurnContext, StateT, str], Awaitable[None]],
]
):
"""
Register a handler to hand off conversations from one copilot to another.

Expand All @@ -551,6 +587,8 @@ def handoff(
async def on_handoff(context: TurnContext, state: TurnState, continuation: str):
print(continuation)

:param func: Optional handler to register directly without using decorator syntax.
:type func: Optional[Callable[[TurnContext, StateT, str], Awaitable[None]]]
:param auth_handlers: Optional list of authorization handler IDs for the route.
:type auth_handlers: Optional[list[str]]
:param kwargs: Additional route configuration passed to :meth:`microsoft_agents.hosting.core.AgentApplication.add_route`.
Expand All @@ -566,24 +604,29 @@ def __call(
func: Callable[[TurnContext, StateT, str], Awaitable[None]],
) -> Callable[[TurnContext, StateT, str], Awaitable[None]]:
async def __handler(context: TurnContext, state: StateT):
if not context.activity.value:
return False
await func(context, state, context.activity.value["continuation"])
if (
isinstance(context.activity.value, dict)
and "continuation" in context.activity.value
):
await func(context, state, context.activity.value["continuation"])
else:
logger.warning("Invalid handoff action received")
await context.send_activity(
Comment thread
rodrigobr-msft marked this conversation as resolved.
Activity(
type=ActivityTypes.invoke_response,
value=InvokeResponse(status=200),
)
)
return True

logger.debug(
f"Registering handoff handler for route handler {func.__name__} with auth handlers: {auth_handlers}"
)

self.add_route(__selector, func, auth_handlers=auth_handlers, **kwargs)
self.add_route(__selector, __handler, auth_handlers=auth_handlers, **kwargs)
return func

if func is not None:
return __call(func)
return __call

def on_sign_in_success(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ def copy_to(self, context: "TurnContext") -> None:
setattr(context, attribute, getattr(self, attribute))

@property
def activity(self):
def activity(self) -> Activity:
"""
The received activity.
:return:
"""
return self._activity
return self._activity # type: ignore[return-value]
Comment thread
rodrigobr-msft marked this conversation as resolved.

@activity.setter
def activity(self, value):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
)
from microsoft_agents.hosting.core.authorization import ClaimsIdentity


# ---------------------------------------------------------------------------
# Helper
# ---------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
_service_url_for_channel,
)


# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
Expand Down
1 change: 0 additions & 1 deletion tests/hosting_core/app/proactive/test_proactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
from microsoft_agents.hosting.core.authorization import ClaimsIdentity
from microsoft_agents.hosting.core.channel_adapter import ChannelAdapter


# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
Expand Down
Loading
Loading