-
Notifications
You must be signed in to change notification settings - Fork 56
Support sub channel identification from Activities #150
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
Merged
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
a33e803
Adding skeleton for sub-channel handling
rodrigobr-msft ac741d5
ChannelId test setup
rodrigobr-msft 0fca846
Defining serialized and validators for Activity and ChannelId
rodrigobr-msft 1c3fd96
Passing ChannelId tests
rodrigobr-msft 9b8532a
Fixing imports
rodrigobr-msft 14d2f3d
Reorganizing Activity tests
rodrigobr-msft e10f9ab
Test adjustment
rodrigobr-msft 2b13902
Adjusting setter/getter for _channel_id in Activity
rodrigobr-msft dbdedbc
Fixing test cases and finalizing Activity serializer
rodrigobr-msft 0aeb75e
Tweaks to docstrings
rodrigobr-msft 15a3c96
Merge branch 'main' of https://github.com/microsoft/Agents-for-python…
rodrigobr-msft 7408a1e
Fixing merge conflicts
rodrigobr-msft f014443
Addressing review comments
rodrigobr-msft 3da2c64
Addressing edge case
rodrigobr-msft c361f9d
Completed fix for serializing a None
rodrigobr-msft affcdd7
Refactoring to make ChannelId a subclass of str
rodrigobr-msft 2f2894b
Updated implementation details
rodrigobr-msft bc79986
Removing Self import from typing
rodrigobr-msft c39c526
Addressing PR comments
rodrigobr-msft 9128cfe
Merge branch 'main' into users/robrandao/sub-channels
rodrigobr-msft d4516ff
Addressing PR review and making entities subclass from Entity
rodrigobr-msft f508ae8
Raising exceptions when ProductInfo and channel_id.sub_channel conflict
rodrigobr-msft d040586
Merge branch 'main' of https://github.com/microsoft/Agents-for-python…
rodrigobr-msft 3705d8a
Merge branch 'users/robrandao/sub-channels' of https://github.com/mic…
rodrigobr-msft 26f1704
Adding copyright comment
rodrigobr-msft 946da69
Reverting strenum usage
rodrigobr-msft 32487fb
Removing unnecessary str conversion and unnecessary comments
rodrigobr-msft b2fbb0b
Merge branch 'main' into users/robrandao/sub-channels
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
96 changes: 96 additions & 0 deletions
96
libraries/microsoft-agents-activity/microsoft_agents/activity/_channel_id_field_mixin.py
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,96 @@ | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
| from typing import Optional, Any | ||
|
|
||
| from pydantic import ( | ||
| ModelWrapValidatorHandler, | ||
| SerializerFunctionWrapHandler, | ||
| computed_field, | ||
| model_validator, | ||
| model_serializer, | ||
| ) | ||
|
|
||
| from .channel_id import ChannelId | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| # can be generalized in the future, if needed | ||
| class _ChannelIdFieldMixin: | ||
| """A mixin to add a computed field channel_id of type ChannelId to a Pydantic model.""" | ||
|
|
||
| _channel_id: Optional[ChannelId] = None | ||
|
|
||
| # required to define the setter below | ||
| @computed_field(return_type=Optional[ChannelId], alias="channelId") | ||
| @property | ||
| def channel_id(self) -> Optional[ChannelId]: | ||
| """Gets the _channel_id field""" | ||
| return self._channel_id | ||
|
|
||
| # necessary for backward compatibility | ||
| # previously, channel_id was directly assigned with strings | ||
| @channel_id.setter | ||
| def channel_id(self, value: Any): | ||
| """Sets the channel_id after validating it as a ChannelId model.""" | ||
| if isinstance(value, ChannelId): | ||
| self._channel_id = value | ||
| elif isinstance(value, str): | ||
| self._channel_id = ChannelId(value) | ||
| else: | ||
| raise ValueError( | ||
| f"Invalid type for channel_id: {type(value)}. " | ||
| "Expected ChannelId or str." | ||
| ) | ||
|
|
||
| def _set_validated_channel_id(self, data: Any) -> None: | ||
| """Sets the channel_id after validating it as a ChannelId model.""" | ||
| if "channelId" in data: | ||
| self.channel_id = data["channelId"] | ||
| elif "channel_id" in data: | ||
| self.channel_id = data["channel_id"] | ||
|
|
||
| @model_validator(mode="wrap") | ||
| @classmethod | ||
| def _validate_channel_id( | ||
| cls, data: Any, handler: ModelWrapValidatorHandler | ||
| ) -> _ChannelIdFieldMixin: | ||
| """Validate the _channel_id field after model initialization. | ||
|
|
||
| :return: The model instance itself. | ||
| """ | ||
| try: | ||
| model = handler(data) | ||
| model._set_validated_channel_id(data) | ||
| return model | ||
| except Exception: | ||
| logging.error("Model %s failed to validate with data %s", cls, data) | ||
| raise | ||
|
|
||
| def _remove_serialized_unset_channel_id( | ||
| self, serialized: dict[str, object] | ||
| ) -> None: | ||
| """Remove the _channel_id field if it is not set.""" | ||
| if not self._channel_id: | ||
| if "channelId" in serialized: | ||
| del serialized["channelId"] | ||
| elif "channel_id" in serialized: | ||
| del serialized["channel_id"] | ||
|
|
||
| @model_serializer(mode="wrap") | ||
| def _serialize_channel_id( | ||
| self, handler: SerializerFunctionWrapHandler | ||
| ) -> dict[str, object]: | ||
| """Serialize the model using Pydantic's standard serialization. | ||
|
|
||
| :param handler: The serialization handler provided by Pydantic. | ||
| :return: A dictionary representing the serialized model. | ||
| """ | ||
| serialized = handler(self) | ||
| if self: # serialization can be called with None | ||
| self._remove_serialized_unset_channel_id(serialized) | ||
| return serialized |
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.
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.