A programming framework for agentic AI
Remove static subscription restriction (#439)
Jack Gerritsba64f442de6c
diff --git a/python/packages/autogen-core/src/autogen_core/application/_helpers.py b/python/packages/autogen-core/src/autogen_core/application/_helpers.pyindex 6c7fae0e2..abb38b98d 100644--- a/python/packages/autogen-core/src/autogen_core/application/_helpers.py+++ b/python/packages/autogen-core/src/autogen_core/application/_helpers.py@@ -40,10 +40,8 @@ class SubscriptionManager:if any(sub.id == subscription.id for sub in self._subscriptions):raise ValueError("Subscription already exists")- if len(self._seen_topics) > 0:- raise NotImplementedError("Cannot add subscription after topics have been seen yet")-self._subscriptions.append(subscription)+ self._rebuild_subscriptions(self._seen_topics)async def remove_subscription(self, id: str) -> None:# Check if the subscription exists@@ -70,9 +68,6 @@ class SubscriptionManager:self._build_for_new_topic(topic)def _build_for_new_topic(self, topic: TopicId) -> None:- if topic in self._seen_topics:- return-self._seen_topics.add(topic)for subscription in self._subscriptions:if subscription.is_match(topic):diff --git a/python/packages/autogen-core/src/autogen_core/application/_worker_runtime.py b/python/packages/autogen-core/src/autogen_core/application/_worker_runtime.pyindex c6edf6fd0..a3a112d6b 100644--- a/python/packages/autogen-core/src/autogen_core/application/_worker_runtime.py+++ b/python/packages/autogen-core/src/autogen_core/application/_worker_runtime.py@@ -481,7 +481,7 @@ class WorkerAgentRuntime(AgentRuntime):await self._host_connection.send(message)async def remove_subscription(self, id: str) -> None:- raise NotImplementedError("Subscriptions are not yet implemented.")+ raise NotImplementedError("Subscriptions cannot be removed while using distributed runtime currently.")async def get(self, id_or_type: AgentId | AgentType | str, /, key: str = "default", *, lazy: bool = Truediff --git a/python/packages/autogen-core/tests/test_subscription.py b/python/packages/autogen-core/tests/test_subscription.pyindex 80a9c86fe..99b22b182 100644--- a/python/packages/autogen-core/tests/test_subscription.py+++ b/python/packages/autogen-core/tests/test_subscription.py@@ -1,5 +1,12 @@+from autogen_core.application import SingleThreadedAgentRuntimefrom autogen_core.components import TypeSubscriptionfrom autogen_core.base import TopicId, AgentId+import pytest+from autogen_core.application import SingleThreadedAgentRuntime+from autogen_core.components import TypeSubscription, DefaultTopicId+from autogen_core.base import AgentId+from autogen_core.base import TopicId+from test_utils import LoopbackAgent, MessageTypeimport pytest@@ -20,3 +27,61 @@ def test_type_subscription_map() -> None:with pytest.raises(CantHandleException):_agent_id = sub.map_to_agent(TopicId(type="t0", source="s1"))+++async def test_non_default_default_subscription() -> None:+ runtime = SingleThreadedAgentRuntime()++ await runtime.register("MyAgent", LoopbackAgent)+ runtime.start()+ await runtime.publish_message(MessageType(), topic_id=DefaultTopicId())+ await runtime.stop_when_idle()++ # Not subscribed+ agent_instance = await runtime.try_get_underlying_agent_instance(AgentId("MyAgent", key="default"), type=LoopbackAgent)+ assert agent_instance.num_calls == 0++ # Subscribed+ default_subscription = TypeSubscription("default", "MyAgent")+ await runtime.add_subscription(default_subscription)++ runtime.start()+ await runtime.publish_message(MessageType(), topic_id=DefaultTopicId())+ await runtime.stop_when_idle()++ assert agent_instance.num_calls == 1++ # Publish to a different unsubscribed topic+ runtime.start()+ await runtime.publish_message(MessageType(), topic_id=DefaultTopicId(type="other"))+ await runtime.stop_when_idle()++ assert agent_instance.num_calls == 1++ # Add a subscription to the other topic+ await runtime.add_subscription(TypeSubscription("other", "MyAgent"))++ runtime.start()+ await runtime.publish_message(MessageType(), topic_id=DefaultTopicId(type="other"))+ await runtime.stop_when_idle()++ assert agent_instance.num_calls == 2++ # Remove the subscription+ await runtime.remove_subscription(default_subscription.id)++ # Publish to the default topic+ runtime.start()+ await runtime.publish_message(MessageType(), topic_id=DefaultTopicId())+ await runtime.stop_when_idle()++ assert agent_instance.num_calls == 2++ # Publish to the other topic+ runtime.start()+ await runtime.publish_message(MessageType(), topic_id=DefaultTopicId(type="other"))+ await runtime.stop_when_idle()++ assert agent_instance.num_calls == 3+diff --git a/python/pyproject.toml b/python/pyproject.tomlindex 0aae8731b..590d15daa 100644--- a/python/pyproject.toml+++ b/python/pyproject.toml@@ -66,6 +66,7 @@ exclude = ["src/autogen_core/application/protos"][tool.poe.tasks]fmt = "python run_task_in_pkgs_if_exist.py fmt"+format.ref = "fmt"lint = "python run_task_in_pkgs_if_exist.py lint"pyright = "python run_task_in_pkgs_if_exist.py pyright"mypy = "python run_task_in_pkgs_if_exist.py mypy"diff --git a/python/shared_tasks.toml b/python/shared_tasks.tomlindex 77be629a3..75774dd1a 100644--- a/python/shared_tasks.toml+++ b/python/shared_tasks.toml@@ -1,5 +1,6 @@[tool.poe.tasks]fmt = "ruff format"+format.ref = "fmt"lint = "ruff check"mypy = "mypy --config-file $POE_ROOT/../../pyproject.toml src tests"pyright = "pyright"diff --git a/python/uv.lock b/python/uv.lockindex 767d0d69b..37c64a4e0 100644--- a/python/uv.lock+++ b/python/uv.lock@@ -7,9 +7,15 @@ resolution-markers = ["python_full_version == '3.11.*' and platform_system == 'Darwin'","python_full_version == '3.11.*' and platform_machine == 'aarch64' and platform_system == 'Linux'","(python_full_version == '3.11.*' and platform_machine != 'aarch64' and platform_system != 'Darwin') or (python_full_version == '3.11.*' and platform_system != 'Darwin' and platform_system != 'Linux')",- "python_full_version < '3.12.4' and platform_system == 'Darwin'",- "python_full_version < '3.12.4' and platform_machine == 'aarch64' and platform_system == 'Linux'",- "(python_full_version < '3.12.4' and platform_machine != 'aarch64' and platform_system != 'Darwin') or (python_full_version < '3.12.4' and platform_system != 'Darwin' and platform_system != 'Linux')",+ "python_full_version < '3.11' and platform_system == 'Darwin'",+ "python_full_version == '3.11.*' and platform_system == 'Darwin'",+ "python_full_version >= '3.12' and python_full_version < '3.12.4' and platform_system == 'Darwin'",+ "python_full_version < '3.11' and platform_machine == 'aarch64' and platform_system == 'Linux'",+ "python_full_version == '3.11.*' and platform_machine == 'aarch64' and platform_system == 'Linux'",+ "python_full_version >= '3.12' and python_full_version < '3.12.4' and platform_machine == 'aarch64' and platform_system == 'Linux'",+ "(python_full_version < '3.11' and platform_machine != 'aarch64' and platform_system != 'Darwin') or (python_full_version < '3.11' and platform_system != 'Darwin' and platform_system != 'Linux')",+ "(python_full_version == '3.11.*' and platform_machine != 'aarch64' and platform_system != 'Darwin') or (python_full_version == '3.11.*' and platform_system != 'Darwin' and platform_system != 'Linux')",+ "(python_full_version >= '3.12' and python_full_version < '3.12.4' and platform_machine != 'aarch64' and platform_system != 'Darwin') or (python_full_version >= '3.12' and python_full_version < '3.12.4' and platform_system != 'Darwin' and platform_system != 'Linux')","python_full_version >= '3.12.4' and python_full_version < '3.13' and platform_system == 'Darwin'","python_full_version >= '3.12.4' and python_full_version < '3.13' and platform_machine == 'aarch64' and platform_system == 'Linux'","(python_full_version >= '3.12.4' and python_full_version < '3.13' and platform_machine != 'aarch64' and platform_system != 'Darwin') or (python_full_version >= '3.12.4' and python_full_version < '3.13' and platform_system != 'Darwin' and platform_system != 'Linux')",@@ -271,7 +277,7 @@ wheels = [[[package]]name = "autogen-core"-version = "0.3.0.dev1"+version = "0.3.dev0"source = { editable = "packages/autogen-core" }dependencies = [{ name = "aiohttp" },@@ -301,18 +307,12 @@ dev = [{ name = "llama-index-readers-wikipedia" },{ name = "llama-index-tools-wikipedia" },{ name = "markdownify" },- { name = "mypy" },{ name = "myst-nb" },{ name = "nbqa" },{ name = "pip" },{ name = "polars" },- { name = "pytest" },- { name = "pytest-asyncio" },- { name = "pytest-mock" },- { name = "pytest-xdist" },{ name = "python-dotenv" },{ name = "requests" },- { name = "ruff" },{ name = "sphinx" },{ name = "sphinx-autobuild" },{ name = "sphinxcontrib-apidoc" },@@ -320,7 +320,6 @@ dev = [{ name = "textual" },{ name = "textual-dev" },{ name = "textual-imageview" },- { name = "tiktoken" },{ name = "types-aiofiles" },{ name = "types-pillow" },{ name = "types-protobuf" },@@ -357,18 +356,12 @@ dev = [{ name = "llama-index-readers-wikipedia" },{ name = "llama-index-tools-wikipedia" },{ name = "markdownify" },- { name = "mypy", specifier = "==1.10.0" },{ name = "myst-nb" },{ name = "nbqa" },{ name = "pip" },{ name = "polars" },- { name = "pytest" },- { name = "pytest-asyncio" },- { name = "pytest-mock" },- { name = "pytest-xdist" },{ name = "python-dotenv" },{ name = "requests" },- { name = "ruff", specifier = "==0.4.8" },{ name = "sphinx" },{ name = "sphinx-autobuild" },{ name = "sphinxcontrib-apidoc" },@@ -376,7 +369,6 @@ dev = [{ name = "textual" },{ name = "textual-dev" },{ name = "textual-imageview" },- { name = "tiktoken" },{ name = "types-aiofiles" },{ name = "types-pillow" },{ name = "types-protobuf" },@@ -2491,7 +2483,7 @@ name = "nvidia-cudnn-cu12"version = "9.1.0.70"source = { registry = "https://pypi.org/simple" }dependencies = [- { name = "nvidia-cublas-cu12" },+ { name = "nvidia-cublas-cu12", marker = "(platform_machine != 'aarch64' and platform_system != 'Darwin') or (platform_system != 'Darwin' and platform_system != 'Linux')" },]wheels = [{ url = "https://files.pythonhosted.org/packages/9f/fd/713452cd72343f682b1c7b9321e23829f00b842ceaedcda96e742ea0b0b3/nvidia_cudnn_cu12-9.1.0.70-py3-none-manylinux2014_x86_64.whl", hash = "sha256:165764f44ef8c61fcdfdfdbe769d687e06374059fbb388b6c89ecb0e28793a6f", size = 664752741 },@@ -2521,9 +2513,9 @@ name = "nvidia-cusolver-cu12"version = "11.4.5.107"source = { registry = "https://pypi.org/simple" }dependencies = [- { name = "nvidia-cublas-cu12" },- { name = "nvidia-cusparse-cu12" },- { name = "nvidia-nvjitlink-cu12" },+ { name = "nvidia-cublas-cu12", marker = "(platform_machine != 'aarch64' and platform_system != 'Darwin') or (platform_system != 'Darwin' and platform_system != 'Linux')" },+ { name = "nvidia-cusparse-cu12", marker = "(platform_machine != 'aarch64' and platform_system != 'Darwin') or (platform_system != 'Darwin' and platform_system != 'Linux')" },+ { name = "nvidia-nvjitlink-cu12", marker = "(platform_machine != 'aarch64' and platform_system != 'Darwin') or (platform_system != 'Darwin' and platform_system != 'Linux')" },]wheels = [{ url = "https://files.pythonhosted.org/packages/bc/1d/8de1e5c67099015c834315e333911273a8c6aaba78923dd1d1e25fc5f217/nvidia_cusolver_cu12-11.4.5.107-py3-none-manylinux1_x86_64.whl", hash = "sha256:8a7ec542f0412294b15072fa7dab71d31334014a69f953004ea7a118206fe0dd", size = 124161928 },@@ -2535,7 +2527,7 @@ name = "nvidia-cusparse-cu12"version = "12.1.0.106"source = { registry = "https://pypi.org/simple" }dependencies = [- { name = "nvidia-nvjitlink-cu12" },+ { name = "nvidia-nvjitlink-cu12", marker = "(platform_machine != 'aarch64' and platform_system != 'Darwin') or (platform_system != 'Darwin' and platform_system != 'Linux')" },]wheels = [{ url = "https://files.pythonhosted.org/packages/65/5b/cfaeebf25cd9fdec14338ccb16f6b2c4c7fa9163aefcf057d86b9cc248bb/nvidia_cusparse_cu12-12.1.0.106-py3-none-manylinux1_x86_64.whl", hash = "sha256:f3b50f42cf363f86ab21f720998517a659a48131e8d538dc02f8768237bd884c", size = 195958278 },@@ -4513,7 +4505,7 @@ name = "triton"version = "3.0.0"source = { registry = "https://pypi.org/simple" }dependencies = [- { name = "filelock", marker = "python_full_version < '3.13'" },+ { name = "filelock", marker = "(python_full_version < '3.13' and platform_machine != 'aarch64' and platform_system != 'Darwin') or (python_full_version < '3.13' and platform_system != 'Darwin' and platform_system != 'Linux')" },]wheels = [{ url = "https://files.pythonhosted.org/packages/45/27/14cc3101409b9b4b9241d2ba7deaa93535a217a211c86c4cc7151fb12181/triton-3.0.0-1-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:e1efef76935b2febc365bfadf74bcb65a6f959a9872e5bddf44cc9e0adce1e1a", size = 209376304 },