From bedc6c0d466010a83cf7ecee78d16291a5e11da2 Mon Sep 17 00:00:00 2001 From: Diego Colombo Date: Mon, 3 Jun 2024 15:16:58 -0700 Subject: [PATCH] Refactor hook registration and processing methods (#2853) * Refactor hook registration and processing methods - Refactored the `hook_lists` dictionary to use type hints for better readability. - Updated the `register_hook` method signature to include type hints for the `hook` parameter. - Added type hints to the `process_last_received_message` method parameters and return value. This commit refactors the code related to hook registration and processing in the `conversable_agent.py` file. The changes improve code readability and maintainability by using type hints and updating method signatures. * Refactor hook_lists initialization and add type hints - Refactored the initialization of `hook_lists` to use a colon instead of an equal sign. - Added type hints for the parameters and return types of `process_last_received_message` method. * Refactor hook registration and processing in conversable_agent.py - Refactored the `hook_lists` dictionary to use a more generic type for the list of hooks. - Updated the signature check for `process_message_before_send`, `process_all_messages_before_reply`, and `process_last_received_message` hooks to ensure they are callable with the correct signatures. - Added error handling to raise a ValueError or TypeError if any hook does not have the expected signature. * Refactor hook processing in conversable_agent.py - Simplify the code by removing unnecessary type checks and error handling. - Consolidate the logic for processing hooks in `_process_message_before_send`, `process_all_messages_before_reply`, and `process_last_received_message` methods. * Refactor register_hook method signature for flexibility The commit changes the signature of the `register_hook` method in `conversable_agent.py`. The second argument, `hook`, is now of type `Callable` instead of `Callable[[List[Dict]], List[Dict]]`. This change allows for more flexibility when registering hooks. --- autogen/agentchat/conversable_agent.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/autogen/agentchat/conversable_agent.py b/autogen/agentchat/conversable_agent.py index c3394a96bb6a..aeea27cff675 100644 --- a/autogen/agentchat/conversable_agent.py +++ b/autogen/agentchat/conversable_agent.py @@ -242,7 +242,7 @@ def __init__( # Registered hooks are kept in lists, indexed by hookable method, to be called in their order of registration. # New hookable methods should be added to this list as required to support new agent capabilities. - self.hook_lists = { + self.hook_lists: Dict[str, List[Callable]] = { "process_last_received_message": [], "process_all_messages_before_reply": [], "process_message_before_send": [], @@ -2724,7 +2724,7 @@ def process_all_messages_before_reply(self, messages: List[Dict]) -> List[Dict]: processed_messages = hook(processed_messages) return processed_messages - def process_last_received_message(self, messages): + def process_last_received_message(self, messages: List[Dict]) -> List[Dict]: """ Calls any registered capability hooks to use and potentially modify the text of the last message, as long as the last message is not a function call or exit command. @@ -2758,6 +2758,7 @@ def process_last_received_message(self, messages): processed_user_content = user_content for hook in hook_list: processed_user_content = hook(processed_user_content) + if processed_user_content == user_content: return messages # No hooks actually modified the user's message.