A programming framework for agentic AI
Add special topic for agent direct messaging (#4385)
Jack Gerritsdf183be35a59
diff --git a/python/packages/autogen-core/src/autogen_core/application/_worker_runtime_host_servicer.py b/python/packages/autogen-core/src/autogen_core/application/_worker_runtime_host_servicer.pyindex 5cd2bf8ea..7c597bd07 100644--- a/python/packages/autogen-core/src/autogen_core/application/_worker_runtime_host_servicer.py+++ b/python/packages/autogen-core/src/autogen_core/application/_worker_runtime_host_servicer.py@@ -4,7 +4,7 @@ from _collections_abc import AsyncIterator, Iteratorfrom asyncio import Future, Taskfrom typing import Any, Dict, Set-from autogen_core.components._type_prefix_subscription import TypePrefixSubscription+from autogen_core.base._type_prefix_subscription import TypePrefixSubscriptionfrom ..base import Subscription, TopicIdfrom ..components import TypeSubscriptiondiff --git a/python/packages/autogen-core/src/autogen_core/base/_base_agent.py b/python/packages/autogen-core/src/autogen_core/base/_base_agent.pyindex 5d8e94225..70481705c 100644--- a/python/packages/autogen-core/src/autogen_core/base/_base_agent.py+++ b/python/packages/autogen-core/src/autogen_core/base/_base_agent.py@@ -20,6 +20,7 @@ from ._serialization import MessageSerializer, try_get_known_serializers_for_typfrom ._subscription import Subscription, UnboundSubscriptionfrom ._subscription_context import SubscriptionInstantiationContextfrom ._topic import TopicId+from ._type_prefix_subscription import TypePrefixSubscriptionT = TypeVar("T", bound=Agent)@@ -149,6 +150,7 @@ class BaseAgent(ABC, Agent):factory: Callable[[], Self | Awaitable[Self]],*,skip_class_subscriptions: bool = False,+ skip_direct_message_subscription: bool = False,) -> AgentType:agent_type = AgentType(type)agent_type = await runtime.register_factory(type=agent_type, agent_factory=factory, expected_class=cls)@@ -166,6 +168,16 @@ class BaseAgent(ABC, Agent):for subscription in subscriptions:await runtime.add_subscription(subscription)+ if not skip_direct_message_subscription:+ # Additionally adds a special prefix subscription for this agent to receive direct messages+ await runtime.add_subscription(+ TypePrefixSubscription(+ # The prefix MUST include ":" to avoid collisions with other agents+ topic_type_prefix=agent_type.type + ":",+ agent_type=agent_type.type,+ )+ )+# TODO: deduplicationfor _message_type, serializer in cls._handles_types():runtime.add_message_serializer(serializer)diff --git a/python/packages/autogen-core/src/autogen_core/components/_type_prefix_subscription.py b/python/packages/autogen-core/src/autogen_core/base/_type_prefix_subscription.pysimilarity index 93%rename from python/packages/autogen-core/src/autogen_core/components/_type_prefix_subscription.pyrename to python/packages/autogen-core/src/autogen_core/base/_type_prefix_subscription.pyindex d71b587d1..f00119165 100644--- a/python/packages/autogen-core/src/autogen_core/components/_type_prefix_subscription.py+++ b/python/packages/autogen-core/src/autogen_core/base/_type_prefix_subscription.py@@ -1,7 +1,9 @@import uuid-from ..base import AgentId, Subscription, TopicId-from ..base.exceptions import CantHandleException+from ._agent_id import AgentId+from ._subscription import Subscription+from ._topic import TopicId+from .exceptions import CantHandleExceptionclass TypePrefixSubscription(Subscription):diff --git a/python/packages/autogen-core/src/autogen_core/components/__init__.py b/python/packages/autogen-core/src/autogen_core/components/__init__.pyindex 4c4d02f3b..75bb5eabc 100644--- a/python/packages/autogen-core/src/autogen_core/components/__init__.py+++ b/python/packages/autogen-core/src/autogen_core/components/__init__.py@@ -2,12 +2,12 @@The :mod:`autogen_core.components` module provides building blocks for creating single agents"""+from ..base._type_prefix_subscription import TypePrefixSubscriptionfrom ._closure_agent import ClosureAgentfrom ._default_subscription import DefaultSubscription, default_subscription, type_subscriptionfrom ._default_topic import DefaultTopicIdfrom ._image import Imagefrom ._routed_agent import RoutedAgent, TypeRoutedAgent, event, message_handler, rpc-from ._type_prefix_subscription import TypePrefixSubscriptionfrom ._type_subscription import TypeSubscriptionfrom ._types import FunctionCalldiff --git a/python/packages/autogen-core/tests/test_worker_runtime.py b/python/packages/autogen-core/tests/test_worker_runtime.pyindex d58233c3a..26c95dc01 100644--- a/python/packages/autogen-core/tests/test_worker_runtime.py+++ b/python/packages/autogen-core/tests/test_worker_runtime.py@@ -360,7 +360,7 @@ async def test_disconnected_agent() -> None:)subscriptions1 = get_current_subscriptions()- assert len(subscriptions1) == 1+ assert len(subscriptions1) == 2recipients1 = await get_subscribed_recipients()assert AgentId(type="worker1", key="default") in recipients1@@ -388,7 +388,7 @@ async def test_disconnected_agent() -> None:)subscriptions3 = get_current_subscriptions()- assert len(subscriptions3) == 1+ assert len(subscriptions3) == 2assert first_subscription_id not in [x.id for x in subscriptions3]recipients3 = await get_subscribed_recipients()