Skip to content
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

using protocols for message handler decorated functions #4046

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
runtime_checkable,
)

from ..base import BaseAgent, MessageContext, MessageSerializer, try_get_known_serializers_for_type
from ..base import Agent, BaseAgent, MessageContext, MessageSerializer, try_get_known_serializers_for_type
from ..base._type_helpers import AnyType, get_types
from ..base.exceptions import CantHandleException

logger = logging.getLogger("autogen_core")

AgentT = TypeVar("AgentT")
AgentT = TypeVar("AgentT", bound=Agent)
ReceivesT = TypeVar("ReceivesT")
ProducesT = TypeVar("ProducesT", covariant=True)

Expand All @@ -47,13 +47,19 @@ class MessageHandler(Protocol[AgentT, ReceivesT, ProducesT]): # type: ignore
async def __call__(agent_instance: AgentT, message: ReceivesT, ctx: MessageContext) -> ProducesT: ...


# NOTE: this works on concrete types and not inheritance
# TODO: Use a protocol for the outer function to check checked arg names
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jackgerrits @ekzhu I addressed this TODO but we're still facing same issue. Any guidance you might be able to provide?

@runtime_checkable
class MessageHandlerFunction(Protocol[AgentT, ReceivesT, ProducesT]): # type: ignore
@staticmethod
async def __call__(
self: AgentT, # type: ignore [reportSelfClsParameterName]
message: ReceivesT,
ctx: MessageContext,
) -> ProducesT: ...


@overload
def message_handler(
func: Callable[[AgentT, ReceivesT, MessageContext], Coroutine[Any, Any, ProducesT]],
func: MessageHandlerFunction[AgentT, ReceivesT, ProducesT],
) -> MessageHandler[AgentT, ReceivesT, ProducesT]: ...


Expand All @@ -64,7 +70,7 @@ def message_handler(
match: None = ...,
strict: bool = ...,
) -> Callable[
[Callable[[AgentT, ReceivesT, MessageContext], Coroutine[Any, Any, ProducesT]]],
[MessageHandlerFunction[AgentT, ReceivesT, ProducesT]],
MessageHandler[AgentT, ReceivesT, ProducesT],
]: ...

Expand All @@ -76,19 +82,19 @@ def message_handler(
match: Callable[[ReceivesT, MessageContext], bool],
strict: bool = ...,
) -> Callable[
[Callable[[AgentT, ReceivesT, MessageContext], Coroutine[Any, Any, ProducesT]]],
[MessageHandlerFunction[AgentT, ReceivesT, ProducesT]],
MessageHandler[AgentT, ReceivesT, ProducesT],
]: ...


def message_handler(
func: None | Callable[[AgentT, ReceivesT, MessageContext], Coroutine[Any, Any, ProducesT]] = None,
func: None | MessageHandlerFunction[AgentT, ReceivesT, ProducesT] = None,
*,
strict: bool = True,
match: None | Callable[[ReceivesT, MessageContext], bool] = None,
) -> (
Callable[
[Callable[[AgentT, ReceivesT, MessageContext], Coroutine[Any, Any, ProducesT]]],
[MessageHandlerFunction[AgentT, ReceivesT, ProducesT]],
MessageHandler[AgentT, ReceivesT, ProducesT],
]
| MessageHandler[AgentT, ReceivesT, ProducesT]
Expand All @@ -115,7 +121,7 @@ def message_handler(
"""

def decorator(
func: Callable[[AgentT, ReceivesT, MessageContext], Coroutine[Any, Any, ProducesT]],
func: MessageHandlerFunction[AgentT, ReceivesT, ProducesT],
) -> MessageHandler[AgentT, ReceivesT, ProducesT]:
type_hints = get_type_hints(func)
if "message" not in type_hints:
Expand Down
12 changes: 9 additions & 3 deletions python/packages/autogen-core/tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from types import NoneType
from typing import Any, List, Optional, Union

from autogen_core.base import MessageContext
from autogen_core.base import Agent, MessageContext
from autogen_core.base._serialization import has_nested_base_model
from autogen_core.base._type_helpers import AnyType, get_types
from autogen_core.components._routed_agent import message_handler
Expand All @@ -21,7 +21,7 @@ def test_get_types() -> None:


def test_handler() -> None:
class HandlerClass:
class HandlerClass(Agent):
@message_handler()
async def handler(self, message: int, ctx: MessageContext) -> Any:
return None
Expand All @@ -37,7 +37,13 @@ async def handler2(self, message: str | bool, ctx: MessageContext) -> None:
assert HandlerClass.handler2.produces_types == [NoneType]


class HandlerClass:
class HandlerClass(Agent):
@message_handler()
async def handler(self, message: int, ctx: MessageContext) -> Any:
return None


class HandlerClass2(HandlerClass): # Testing for type chekers not breaking on inheretence
@message_handler()
async def handler(self, message: int, ctx: MessageContext) -> Any:
return None
Expand Down
Loading