-
Notifications
You must be signed in to change notification settings - Fork 75
AgentApplication route registration improvements #429
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
Open
rodrigobr-msft
wants to merge
3
commits into
main
Choose a base branch
from
users/robrandao/agent-application
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ | |
| Pattern, | ||
| TypeVar, | ||
| cast, | ||
| overload, | ||
| ) | ||
|
|
||
| from microsoft_agents.activity import ( | ||
|
|
@@ -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]: | ||
|
|
@@ -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 | ||
| 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
|
|
||
| return text == select | ||
|
|
||
|
|
@@ -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]], | ||
| ]: | ||
| ]: ... | ||
|
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. | ||
|
|
||
|
|
@@ -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`. | ||
|
|
@@ -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( | ||
|
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( | ||
|
|
||
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
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?