Skip to content

Commit

Permalink
Refactor hook registration and processing methods (#2853)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
colombod authored and victordibia committed Jul 30, 2024
1 parent e3a5826 commit bedc6c0
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions autogen/agentchat/conversable_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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": [],
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.

Expand Down

0 comments on commit bedc6c0

Please sign in to comment.