diff --git a/libraries/microsoft-agents-activity/microsoft_agents/activity/_utils/_deferred_string.py b/libraries/microsoft-agents-activity/microsoft_agents/activity/_utils/_deferred_string.py index 39eb10ad..d2f7e475 100644 --- a/libraries/microsoft-agents-activity/microsoft_agents/activity/_utils/_deferred_string.py +++ b/libraries/microsoft-agents-activity/microsoft_agents/activity/_utils/_deferred_string.py @@ -2,12 +2,26 @@ # Licensed under the MIT License. import logging +from typing import Callable logger = logging.getLogger(__name__) class _DeferredString: - def __init__(self, func, *args, **kwargs): + """A wrapper around a function to allow for deferred evaluation. + + The result of the function is converted to a string with str(). + If an error occurs during evaluation, an error is logged and a default + string is returned. + """ + + def __init__(self, func: Callable, *args, **kwargs): + """Initializes a DeferredString instance. + + :param func: The function to be called to get the string value. + :param args: Positional arguments to pass to the function. + :param kwargs: Keyword arguments to pass to the function. + """ self.func = func self.args = args self.kwargs = kwargs diff --git a/libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/state/state.py b/libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/state/state.py index 0b64406c..ce4c7064 100644 --- a/libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/state/state.py +++ b/libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/app/state/state.py @@ -109,7 +109,7 @@ async def save( data = self.copy() del data["__key__"] - logger.info(f"Saving state {self.__key__}") + logger.info("Saving state %s", self.__key__) await storage.delete(self.__deleted__) await storage.write( { diff --git a/libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/connector/client/connector_client.py b/libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/connector/client/connector_client.py index 0ede8cde..fe4b0423 100644 --- a/libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/connector/client/connector_client.py +++ b/libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/connector/client/connector_client.py @@ -74,11 +74,13 @@ async def get_attachment_info(self, attachment_id: str) -> AttachmentInfo: url = f"v3/attachments/{attachment_id}" - logger.info(f"Getting attachment info for ID: {attachment_id}") + logger.info("Getting attachment info for ID: %s", attachment_id) async with self.client.get(url) as response: - if response.status >= 400: + if response.status >= 300: logger.error( - f"Error getting attachment info: {response.status}", stack_info=True + "Error getting attachment info: %s", + response.status, + stack_info=True, ) response.raise_for_status() @@ -108,11 +110,13 @@ async def get_attachment(self, attachment_id: str, view_id: str) -> BytesIO: url = f"v3/attachments/{attachment_id}/views/{view_id}" - logger.info(f"Getting attachment for ID: {attachment_id}, View ID: {view_id}") + logger.info( + "Getting attachment for ID: %s, View ID: %s", attachment_id, view_id + ) async with self.client.get(url) as response: - if response.status >= 400: + if response.status >= 300: logger.error( - f"Error getting attachment: {response.status}", stack_info=True + "Error getting attachment: %s", response.status, stack_info=True ) response.raise_for_status() @@ -143,12 +147,12 @@ async def get_conversations( ) logger.info( - f"Getting conversations with continuation token: {continuation_token}" + "Getting conversations with continuation token: %s", continuation_token ) async with self.client.get("v3/conversations", params=params) as response: - if response.status >= 400: + if response.status >= 300: logger.error( - f"Error getting conversations: {response.status}", stack_info=True + "Error getting conversations: %s", response.status, stack_info=True ) response.raise_for_status() @@ -170,9 +174,9 @@ async def create_conversation( "v3/conversations", json=body.model_dump(by_alias=True, exclude_unset=True, mode="json"), ) as response: - if response.status >= 400: + if response.status >= 300: logger.error( - f"Error creating conversation: {response.status}", stack_info=True + "Error creating conversation: %s", response.status, stack_info=True ) response.raise_for_status() @@ -201,7 +205,10 @@ async def reply_to_activity( url = f"v3/conversations/{conversation_id}/activities/{activity_id}" logger.info( - f"Replying to activity: {activity_id} in conversation: {conversation_id}. Activity type is {body.type}" + "Replying to activity: %s in conversation: %s. Activity type is %s", + activity_id, + conversation_id, + body.type, ) async with self.client.post( @@ -212,15 +219,16 @@ async def reply_to_activity( ) as response: result = await response.json() if response.content_length else {} - if response.status >= 400: + if response.status >= 300: logger.error( - f"Error replying to activity: {result or response.status}", + "Error replying to activity: %s", + result or response.status, stack_info=True, ) response.raise_for_status() logger.info( - f"Reply to conversation/activity: {result.get('id')}, {activity_id}" + "Reply to conversation/activity: %s, %s", result.get("id"), activity_id ) return ResourceResponse.model_validate(result) @@ -246,15 +254,19 @@ async def send_to_conversation( url = f"v3/conversations/{conversation_id}/activities" logger.info( - f"Sending to conversation: {conversation_id}. Activity type is {body.type}" + "Sending to conversation: %s. Activity type is %s", + conversation_id, + body.type, ) async with self.client.post( url, json=body.model_dump(by_alias=True, exclude_unset=True, mode="json"), ) as response: - if response.status >= 400: + if response.status >= 300: logger.error( - f"Error sending to conversation: {response.status}", stack_info=True + "Error sending to conversation: %s", + response.status, + stack_info=True, ) response.raise_for_status() @@ -283,15 +295,18 @@ async def update_activity( url = f"v3/conversations/{conversation_id}/activities/{activity_id}" logger.info( - f"Updating activity: {activity_id} in conversation: {conversation_id}. Activity type is {body.type}" + "Updating activity: %s in conversation: %s. Activity type is %s", + activity_id, + conversation_id, + body.type, ) async with self.client.put( url, json=body.model_dump(by_alias=True, exclude_unset=True), ) as response: - if response.status >= 400: + if response.status >= 300: logger.error( - f"Error updating activity: {response.status}", stack_info=True + "Error updating activity: %s", response.status, stack_info=True ) response.raise_for_status() @@ -316,12 +331,14 @@ async def delete_activity(self, conversation_id: str, activity_id: str) -> None: url = f"v3/conversations/{conversation_id}/activities/{activity_id}" logger.info( - f"Deleting activity: {activity_id} from conversation: {conversation_id}" + "Deleting activity: %s from conversation: %s", + activity_id, + conversation_id, ) async with self.client.delete(url) as response: - if response.status >= 400: + if response.status >= 300: logger.error( - f"Error deleting activity: {response.status}", stack_info=True + "Error deleting activity: %s", response.status, stack_info=True ) response.raise_for_status() @@ -354,12 +371,14 @@ async def upload_attachment( } logger.info( - f"Uploading attachment to conversation: {conversation_id}, Attachment name: {body.name}" + "Uploading attachment to conversation: %s, Attachment name: %s", + conversation_id, + body.name, ) async with self.client.post(url, json=attachment_dict) as response: - if response.status >= 400: + if response.status >= 300: logger.error( - f"Error uploading attachment: {response.status}", stack_info=True + "Error uploading attachment: %s", response.status, stack_info=True ) response.raise_for_status() @@ -385,11 +404,14 @@ async def get_conversation_members( conversation_id = self._normalize_conversation_id(conversation_id) url = f"v3/conversations/{conversation_id}/members" - logger.info(f"Getting conversation members for conversation: {conversation_id}") + logger.info( + "Getting conversation members for conversation: %s", conversation_id + ) async with self.client.get(url) as response: - if response.status >= 400: + if response.status >= 300: logger.error( - f"Error getting conversation members: {response.status}", + "Error getting conversation members: %s", + response.status, stack_info=True, ) response.raise_for_status() @@ -418,12 +440,15 @@ async def get_conversation_member( url = f"v3/conversations/{conversation_id}/members/{member_id}" logger.info( - f"Getting conversation member: {member_id} from conversation: {conversation_id}" + "Getting conversation member: %s from conversation: %s", + member_id, + conversation_id, ) async with self.client.get(url) as response: - if response.status >= 400: + if response.status >= 300: logger.error( - f"Error getting conversation member: {response.status}", + "Error getting conversation member: %s", + response.status, stack_info=True, ) response.raise_for_status() @@ -451,12 +476,15 @@ async def delete_conversation_member( url = f"v3/conversations/{conversation_id}/members/{member_id}" logger.info( - f"Deleting conversation member: {member_id} from conversation: {conversation_id}" + "Deleting conversation member: %s from conversation: %s", + member_id, + conversation_id, ) async with self.client.delete(url) as response: - if response.status >= 400 and response.status != 204: + if response.status >= 300: logger.error( - f"Error deleting conversation member: {response.status}", + "Error deleting conversation member: %s", + response.status, stack_info=True, ) response.raise_for_status() @@ -482,12 +510,15 @@ async def get_activity_members( url = f"v3/conversations/{conversation_id}/activities/{activity_id}/members" logger.info( - f"Getting activity members for conversation: {conversation_id}, Activity ID: {activity_id}" + "Getting activity members for conversation: %s, Activity ID: %s", + conversation_id, + activity_id, ) async with self.client.get(url) as response: - if response.status >= 400: + if response.status >= 300: logger.error( - f"Error getting activity members: {response.status}", + "Error getting activity members: %s", + response.status, stack_info=True, ) response.raise_for_status() @@ -526,12 +557,16 @@ async def get_conversation_paged_members( url = f"v3/conversations/{conversation_id}/pagedmembers" logger.info( - f"Getting paged members for conversation: {conversation_id}, Page Size: {page_size}, Continuation Token: {continuation_token}" + "Getting paged members for conversation: %s, Page Size: %s, Continuation Token: %s", + conversation_id, + page_size, + continuation_token, ) async with self.client.get(url, params=params) as response: - if response.status >= 400: + if response.status >= 300: logger.error( - f"Error getting conversation paged members: {response.status}", + "Error getting conversation paged members: %s", + response.status, stack_info=True, ) response.raise_for_status() @@ -559,15 +594,12 @@ async def send_conversation_history( conversation_id = self._normalize_conversation_id(conversation_id) url = f"v3/conversations/{conversation_id}/activities/history" - logger.info(f"Sending conversation history to conversation: {conversation_id}") + logger.info("Sending conversation history to conversation: %s", conversation_id) async with self.client.post(url, json=body) as response: - if ( - response.status >= 400 - and response.status != 201 - and response.status != 202 - ): + if response.status >= 300: logger.error( - f"Error sending conversation history: {response.status}", + "Error sending conversation history: %s", + response.status, stack_info=True, ) response.raise_for_status() @@ -602,7 +634,9 @@ def __init__(self, endpoint: str, token: str, *, session: ClientSession = None): headers=headers, ) logger.debug( - f"ConnectorClient initialized with endpoint: {endpoint} and headers: {headers}" + "ConnectorClient initialized with endpoint: %s and headers: %s", + endpoint, + headers, ) if len(token) > 1: diff --git a/libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/connector/client/user_token_client.py b/libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/connector/client/user_token_client.py index 0d1b8c4c..f19618c6 100644 --- a/libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/connector/client/user_token_client.py +++ b/libraries/microsoft-agents-hosting-core/microsoft_agents/hosting/core/connector/client/user_token_client.py @@ -53,13 +53,14 @@ async def get_sign_in_url( params["finalRedirect"] = final_redirect logger.info( - f"AgentSignIn.get_sign_in_url(): Getting sign-in URL with params: {params}" + "AgentSignIn.get_sign_in_url(): Getting sign-in URL with params: %s", + params, ) async with self.client.get( "api/agentsignin/getSignInUrl", params=params ) as response: - if response.status >= 400: - logger.error(f"Error getting sign-in URL: {response.status}") + if response.status >= 300: + logger.error("Error getting sign-in URL: %s", response.status) response.raise_for_status() return await response.text() @@ -89,13 +90,14 @@ async def get_sign_in_resource( params["finalRedirect"] = final_redirect logger.info( - f"AgentSignIn.get_sign_in_resource(): Getting sign-in resource with params: {params}" + "AgentSignIn.get_sign_in_resource(): Getting sign-in resource with params: %s", + params, ) async with self.client.get( "api/botsignin/getSignInResource", params=params ) as response: - if response.status >= 400: - logger.error(f"Error getting sign-in resource: {response.status}") + if response.status >= 300: + logger.error("Error getting sign-in resource: %s", response.status) response.raise_for_status() data = await response.json() @@ -122,12 +124,10 @@ async def get_token( if code: params["code"] = code - logger.info(f"User_token.get_token(): Getting token with params: {params}") + logger.info("User_token.get_token(): Getting token with params: %s", params) async with self.client.get("api/usertoken/GetToken", params=params) as response: - if response.status == 404: - return TokenResponse(model_validate={}) - if response.status >= 400: - logger.error(f"Error getting token: {response.status}") + if response.status >= 300: + logger.error("Error getting token: %s", response.status) response.raise_for_status() data = await response.json() @@ -179,12 +179,12 @@ async def get_aad_tokens( if channel_id: params["channelId"] = channel_id - logger.info(f"Getting AAD tokens with params: {params} and body: {body}") + logger.info("Getting AAD tokens with params: %s and body: %s", params, body) async with self.client.post( "api/usertoken/GetAadTokens", params=params, json=body ) as response: - if response.status >= 400: - logger.error(f"Error getting AAD tokens: {response.status}") + if response.status >= 300: + logger.error("Error getting AAD tokens: %s", response.status) response.raise_for_status() data = await response.json() @@ -203,12 +203,12 @@ async def sign_out( if channel_id: params["channelId"] = channel_id - logger.info(f"Signing out user {user_id} with params: {params}") + logger.info("Signing out user %s with params: %s", user_id, params) async with self.client.delete( "api/usertoken/SignOut", params=params ) as response: - if response.status >= 400 and response.status != 204: - logger.error(f"Error signing out: {response.status}") + if response.status >= 300: + logger.error("Error signing out: %s", response.status) response.raise_for_status() async def get_token_status( @@ -224,12 +224,12 @@ async def get_token_status( if include: params["include"] = include - logger.info(f"Getting token status for user {user_id} with params: {params}") + logger.info("Getting token status for user %s with params: %s", user_id, params) async with self.client.get( "api/usertoken/GetTokenStatus", params=params ) as response: - if response.status >= 400: - logger.error(f"Error getting token status: {response.status}") + if response.status >= 300: + logger.error("Error getting token status: %s", response.status) response.raise_for_status() data = await response.json() @@ -248,12 +248,12 @@ async def exchange_token( "channelId": channel_id, } - logger.info(f"Exchanging token with params: {params} and body: {body}") + logger.info("Exchanging token with params: %s and body: %s", params, body) async with self.client.post( "api/usertoken/exchange", params=params, json=body ) as response: - if response.status >= 400 and response.status != 404: - logger.error(f"Error exchanging token: {response.status}") + if response.status >= 300: + logger.error("Error exchanging token: %s", response.status) response.raise_for_status() data = await response.json() @@ -289,7 +289,9 @@ def __init__(self, endpoint: str, token: str, *, session: ClientSession = None): headers=headers, ) logger.debug( - f"Creating UserTokenClient with endpoint: {endpoint} and headers: {headers}" + "Creating UserTokenClient with endpoint: %s and headers: %s", + endpoint, + headers, ) if len(token) > 1: