Repository navigation
Discovery endpoint for MCS client #630
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
2 commits
Select commit
Hold shift + click to select a range
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
106 changes: 106 additions & 0 deletions
106
...otstudio-client/microsoft_agents/copilotstudio/client/_conversation_endpoint_discovery.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,106 @@ | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. | ||
|
|
||
| from dataclasses import dataclass | ||
| from typing import Literal, Optional | ||
| from urllib.parse import quote, urlsplit, urlunsplit | ||
|
|
||
| from .connection_settings import ConnectionSettings | ||
| from .errors import copilot_studio_errors | ||
| from .power_platform_environment import PowerPlatformEnvironment | ||
|
|
||
| ConversationOperation = Literal["createConversation", "executeTurn", "subscribe"] | ||
| _CONVERSATION_ID = "{conversationId}" | ||
|
|
||
|
|
||
| def get_discovery_url(settings: ConnectionSettings) -> str: | ||
| url = urlsplit(PowerPlatformEnvironment.get_copilot_studio_connection_url(settings)) | ||
| return urlunsplit( | ||
| ( | ||
| url.scheme, | ||
| url.netloc, | ||
| f"/copilotstudio/agents/{quote(settings.agent_identifier, safe='')}/conversation-endpoints", | ||
| "", | ||
| "", | ||
| ) | ||
| ) | ||
|
|
||
|
|
||
| def _validate_url( | ||
| value: object, discovery_url: str, requires_conversation_id: bool | ||
| ) -> str: | ||
| error = str(copilot_studio_errors.InvalidDiscoveryOperationUrl) | ||
| if not isinstance(value, str) or not value.strip(): | ||
| raise ValueError(error) | ||
|
|
||
| remainder = value.replace(_CONVERSATION_ID, "") | ||
| if ( | ||
| requires_conversation_id != (_CONVERSATION_ID in value) | ||
| or "{" in remainder | ||
| or "}" in remainder | ||
| or "\\" in value | ||
| or any(character.isspace() or ord(character) < 32 for character in value) | ||
| ): | ||
| raise ValueError(error) | ||
|
|
||
| try: | ||
| discovery = urlsplit(discovery_url) | ||
| url = urlsplit(value.replace(_CONVERSATION_ID, "conversation")) | ||
| host = discovery.hostname or "" | ||
| suffix_index = host.find(".environment.") | ||
| suffix = host[suffix_index:] if suffix_index >= 0 else "." + host | ||
| valid = ( | ||
| url.scheme == "https" | ||
| and (url.port if url.port is not None else 443) | ||
| == (discovery.port if discovery.port is not None else 443) | ||
| and bool(url.hostname) | ||
| and url.hostname.endswith(suffix) | ||
| and url.username is None | ||
| and url.password is None | ||
| and not url.fragment | ||
| ) | ||
| except ValueError as exc: | ||
| raise ValueError(error) from exc | ||
| if not valid: | ||
| raise ValueError(error) | ||
| return value | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class ConversationEndpoints: | ||
| create_conversation: str | ||
| execute_turn: str | ||
| continue_turn: str | ||
| subscribe: Optional[str] | ||
|
|
||
| @classmethod | ||
| def from_response(cls, data: object, discovery_url: str) -> "ConversationEndpoints": | ||
| if not isinstance(data, dict): | ||
| raise ValueError(str(copilot_studio_errors.InvalidDiscoveryResponse)) | ||
|
|
||
| return cls( | ||
| create_conversation=_validate_url( | ||
| data.get("createConversation"), discovery_url, False | ||
| ), | ||
| execute_turn=_validate_url(data.get("executeTurn"), discovery_url, True), | ||
| continue_turn=_validate_url(data.get("continueTurn"), discovery_url, True), | ||
| subscribe=( | ||
| _validate_url(data["subscribe"], discovery_url, True) | ||
| if data.get("subscribe") is not None | ||
| else None | ||
| ), | ||
| ) | ||
|
|
||
| def get_url( | ||
| self, operation: ConversationOperation, conversation_id: Optional[str] = None | ||
| ) -> str: | ||
| if operation == "createConversation": | ||
| return self.create_conversation | ||
| if not conversation_id: | ||
| raise ValueError(str(copilot_studio_errors.DiscoveryConversationIdRequired)) | ||
| url = self.execute_turn if operation == "executeTurn" else self.subscribe | ||
| if url is None: | ||
| raise NotImplementedError( | ||
| str(copilot_studio_errors.DiscoverySubscribeNotSupported) | ||
| ) | ||
| return url.replace(_CONVERSATION_ID, quote(conversation_id, safe="")) |
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.
Uh oh!
There was an error while loading. Please reload this page.