-
Notifications
You must be signed in to change notification settings - Fork 67
Proactive Improvements #350
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
11 commits
Select commit
Hold shift + click to select a range
c4d5519
Proactive testing WIP
axelsrz b9d2bda
formating: Proactive testing WIP
axelsrz d06e7b5
Readme changes
axelsrz c3dce47
Merge branch 'main' of https://github.com/microsoft/Agents-for-python…
axelsrz 144b9aa
Proactive testing WIP
axelsrz f65d923
Adding unit tests to proactive scenario
axelsrz b95750d
Merge branch 'main' of https://github.com/microsoft/Agents-for-python…
axelsrz 104b770
Merge branch 'main' of https://github.com/microsoft/Agents-for-python…
axelsrz cbf906d
Addressing PR comments
axelsrz 831902c
Fixing tests
axelsrz 5813d9a
More PR comments
axelsrz 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 |
|---|---|---|
|
|
@@ -127,4 +127,7 @@ cython_debug/ | |
| .vscode/ | ||
|
|
||
| # Binary files | ||
| bin/ | ||
| bin/ | ||
|
|
||
| # Claude | ||
| .claude/ | ||
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
20 changes: 20 additions & 0 deletions
20
...ies/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/proactive/__init__.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,20 @@ | ||
| """ | ||
| Copyright (c) Microsoft Corporation. All rights reserved. | ||
| Licensed under the MIT License. | ||
| """ | ||
|
|
||
| from .conversation import Conversation | ||
| from .conversation_builder import ConversationBuilder | ||
| from .conversation_reference_builder import ConversationReferenceBuilder | ||
| from .create_conversation_options import CreateConversationOptions | ||
| from .proactive import Proactive | ||
| from .proactive_options import ProactiveOptions | ||
|
|
||
| __all__ = [ | ||
| "Conversation", | ||
| "ConversationBuilder", | ||
| "ConversationReferenceBuilder", | ||
| "CreateConversationOptions", | ||
| "Proactive", | ||
| "ProactiveOptions", | ||
| ] |
152 changes: 152 additions & 0 deletions
152
...microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/proactive/conversation.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,152 @@ | ||
| """ | ||
| Copyright (c) Microsoft Corporation. All rights reserved. | ||
| Licensed under the MIT License. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import Optional, TYPE_CHECKING | ||
|
|
||
| from microsoft_agents.activity import ConversationReference | ||
| from microsoft_agents.hosting.core.authorization import ClaimsIdentity | ||
| from microsoft_agents.hosting.core.storage.store_item import StoreItem | ||
|
|
||
| if TYPE_CHECKING: | ||
| from microsoft_agents.hosting.core.turn_context import TurnContext | ||
| from microsoft_agents.hosting.core.channel_adapter import ChannelAdapter | ||
|
|
||
| # JWT claim keys that are persisted alongside a ConversationReference. | ||
| _PERSISTED_CLAIM_KEYS = frozenset({"aud", "azp", "appid", "idtyp", "ver", "iss", "tid"}) | ||
|
|
||
|
|
||
| class Conversation(StoreItem): | ||
| """ | ||
| Bundles a :class:`~microsoft_agents.activity.ConversationReference` together | ||
| with a filtered set of JWT claims so that a proactive continuation can be | ||
| performed without holding onto the full :class:`~microsoft_agents.hosting.core.authorization.ClaimsIdentity`. | ||
|
|
||
| Instances are typically created via | ||
| :meth:`~microsoft_agents.hosting.core.app.proactive.conversation_builder.ConversationBuilder` | ||
| or via :meth:`from_turn_context`. | ||
|
|
||
| :param claims: Filtered JWT claims (``aud``, ``azp``, ``appid``, ``idtyp``, | ||
| ``ver``, ``iss``, ``tid``). May be a raw ``dict`` or a | ||
| :class:`~microsoft_agents.hosting.core.authorization.ClaimsIdentity`. | ||
| :type claims: dict[str, str] or ClaimsIdentity | ||
| :param conversation_reference: The conversation reference. | ||
| :type conversation_reference: :class:`~microsoft_agents.activity.ConversationReference` | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| claims: "dict[str, str] | ClaimsIdentity", | ||
| conversation_reference: ConversationReference, | ||
| ) -> None: | ||
| if isinstance(claims, ClaimsIdentity): | ||
| self.claims: dict[str, str] = Conversation.claims_from_identity(claims) | ||
| else: | ||
| self.claims = { | ||
| k: v for k, v in claims.items() if k in _PERSISTED_CLAIM_KEYS | ||
| } | ||
| self.conversation_reference: ConversationReference = conversation_reference | ||
|
|
||
| # ------------------------------------------------------------------ | ||
| # Factory helpers | ||
| # ------------------------------------------------------------------ | ||
|
|
||
| @classmethod | ||
| def from_turn_context(cls, context: "TurnContext") -> "Conversation": | ||
| """ | ||
| Create a :class:`Conversation` from the current turn context. | ||
|
|
||
| :param context: The active turn context. | ||
| :type context: :class:`~microsoft_agents.hosting.core.turn_context.TurnContext` | ||
| :return: A new :class:`Conversation` capturing the current turn's identity | ||
| and conversation reference. | ||
| :rtype: :class:`Conversation` | ||
| """ | ||
| from microsoft_agents.hosting.core.channel_adapter import ChannelAdapter | ||
|
|
||
| identity: Optional[ClaimsIdentity] = context.turn_state.get( | ||
| ChannelAdapter.AGENT_IDENTITY_KEY | ||
| ) | ||
| reference = context.activity.get_conversation_reference() | ||
| return cls(identity or {}, reference) | ||
|
|
||
| # ------------------------------------------------------------------ | ||
| # Claims helpers | ||
| # ------------------------------------------------------------------ | ||
|
|
||
| @staticmethod | ||
| def claims_from_identity(identity: ClaimsIdentity) -> "dict[str, str]": | ||
| """ | ||
| Return the subset of claims from *identity* that are relevant for proactive | ||
| messaging (``aud``, ``azp``, ``appid``, ``idtyp``, ``ver``, ``iss``, ``tid``). | ||
|
|
||
| :param identity: The full claims identity. | ||
| :type identity: :class:`~microsoft_agents.hosting.core.authorization.ClaimsIdentity` | ||
| :return: Filtered claims dictionary. | ||
| :rtype: dict[str, str] | ||
| """ | ||
| return {k: v for k, v in identity.claims.items() if k in _PERSISTED_CLAIM_KEYS} | ||
|
|
||
| @staticmethod | ||
| def identity_from_claims(claims: "dict[str, str]") -> ClaimsIdentity: | ||
| """ | ||
| Reconstruct a :class:`~microsoft_agents.hosting.core.authorization.ClaimsIdentity` | ||
| from a previously persisted claims dict. | ||
|
|
||
| :param claims: Filtered claims dictionary (as produced by :meth:`claims_from_identity`). | ||
| :type claims: dict[str, str] | ||
| :return: Reconstituted claims identity. | ||
| :rtype: :class:`~microsoft_agents.hosting.core.authorization.ClaimsIdentity` | ||
| """ | ||
| return ClaimsIdentity(claims=dict(claims), is_authenticated=True) | ||
|
|
||
| # ------------------------------------------------------------------ | ||
| # Validation | ||
| # ------------------------------------------------------------------ | ||
|
|
||
| def validate(self) -> None: | ||
| """ | ||
| Raise :exc:`ValueError` if required fields are missing. | ||
|
|
||
| :raises ValueError: If ``conversation_reference``, its nested | ||
| ``conversation``, or ``service_url`` are absent. | ||
| """ | ||
| if not self.conversation_reference: | ||
| raise ValueError("Conversation.conversation_reference is required.") | ||
| if not self.conversation_reference.conversation: | ||
| raise ValueError( | ||
| "Conversation.conversation_reference.conversation is required." | ||
|
axelsrz marked this conversation as resolved.
|
||
| ) | ||
| if not self.conversation_reference.conversation.id: | ||
| raise ValueError( | ||
| "Conversation.conversation_reference.conversation.id is required." | ||
| ) | ||
| if not self.conversation_reference.service_url: | ||
| raise ValueError( | ||
| "Conversation.conversation_reference.service_url is required." | ||
| ) | ||
|
axelsrz marked this conversation as resolved.
|
||
|
|
||
| # ------------------------------------------------------------------ | ||
| # StoreItem serialization | ||
| # ------------------------------------------------------------------ | ||
|
|
||
| def store_item_to_json(self) -> dict: | ||
| return { | ||
| "claims": self.claims, | ||
| "conversation_reference": self.conversation_reference.model_dump( | ||
| mode="json", by_alias=True, exclude_unset=True | ||
| ), | ||
| } | ||
|
|
||
| @staticmethod | ||
| def from_json_to_store_item(json_data: dict) -> "Conversation": | ||
| reference = ConversationReference.model_validate( | ||
| json_data.get("conversation_reference", {}) | ||
| ) | ||
| return Conversation( | ||
| claims=json_data.get("claims", {}), | ||
| conversation_reference=reference, | ||
| ) | ||
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.
Uh oh!
There was an error while loading. Please reload this page.