Skip to content

Commit 1ac5272

Browse files
MohMazMohammad Mazraehrysweet
authored
add subscription deduplication (#594)
* add subscription deduplication * format --------- Co-authored-by: Mohammad Mazraeh <[email protected]> Co-authored-by: Ryan Sweet <[email protected]>
1 parent 58ee8b7 commit 1ac5272

File tree

5 files changed

+29
-3
lines changed

5 files changed

+29
-3
lines changed

python/packages/autogen-core/samples/patterns/group_chat.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from autogen_core.application import SingleThreadedAgentRuntime
2121
from autogen_core.base import AgentId, AgentInstantiationContext
2222
from autogen_core.components import DefaultTopicId, RoutedAgent, message_handler
23-
from autogen_core.components._default_subscription import DefaultSubscription
23+
from autogen_core.components import DefaultSubscription
2424
from autogen_core.components.models import (
2525
AssistantMessage,
2626
ChatCompletionClient,

python/packages/autogen-core/src/autogen_core/application/_helpers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def __init__(self) -> None:
3737

3838
async def add_subscription(self, subscription: Subscription) -> None:
3939
# Check if the subscription already exists
40-
if any(sub.id == subscription.id for sub in self._subscriptions):
40+
if any(sub == subscription for sub in self._subscriptions):
4141
raise ValueError("Subscription already exists")
4242

4343
self._subscriptions.append(subscription)

python/packages/autogen-core/src/autogen_core/components/_type_subscription.py

+6
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,11 @@ def map_to_agent(self, topic_id: TopicId) -> AgentId:
5252

5353
return AgentId(type=self._agent_type, key=topic_id.source)
5454

55+
def __eq__(self, other: object) -> bool:
56+
if not isinstance(other, TypeSubscription):
57+
return False
58+
59+
return self.id == other.id or (self.agent_type == other.agent_type and self.topic_type == other.topic_type)
60+
5561

5662
BaseAgentType = TypeVar("BaseAgentType", bound="BaseAgent")

python/packages/autogen-core/tests/test_closure_agent.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from autogen_core.application import SingleThreadedAgentRuntime
66
from autogen_core.base import AgentId, AgentRuntime, MessageContext
77
from autogen_core.components import ClosureAgent
8-
from autogen_core.components._default_subscription import DefaultSubscription
8+
from autogen_core.components import DefaultSubscription
99
from autogen_core.components._default_topic import DefaultTopicId
1010

1111

python/packages/autogen-core/tests/test_subscription.py

+20
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from autogen_core.base import AgentId, TopicId
44
from autogen_core.base.exceptions import CantHandleException
55
from autogen_core.components import DefaultTopicId, TypeSubscription
6+
from autogen_core.components import DefaultSubscription
67
from test_utils import LoopbackAgent, MessageType
78

89

@@ -96,3 +97,22 @@ async def test_skipped_class_subscriptions() -> None:
9697
AgentId("MyAgent", key="default"), type=LoopbackAgent
9798
)
9899
assert agent_instance.num_calls == 0
100+
101+
102+
@pytest.mark.asyncio
103+
async def test_subscription_deduplication() -> None:
104+
runtime = SingleThreadedAgentRuntime()
105+
agent_type = "MyAgent"
106+
107+
# Test TypeSubscription
108+
type_subscription_1 = TypeSubscription("default", agent_type)
109+
type_subscription_2 = TypeSubscription("default", agent_type)
110+
111+
await runtime.add_subscription(type_subscription_1)
112+
with pytest.raises(ValueError, match="Subscription already exists"):
113+
await runtime.add_subscription(type_subscription_2)
114+
115+
# Test DefaultSubscription
116+
default_subscription = DefaultSubscription(agent_type=agent_type)
117+
with pytest.raises(ValueError, match="Subscription already exists"):
118+
await runtime.add_subscription(default_subscription)

0 commit comments

Comments
 (0)