Skip to content

A programming framework for agentic AI

Replatform closure agent on base agent (#4389)

Jack Gerrits0d3bc948a173
diff --git a/python/packages/autogen-agentchat/src/autogen_agentchat/teams/_group_chat/_base_group_chat.py b/python/packages/autogen-agentchat/src/autogen_agentchat/teams/_group_chat/_base_group_chat.py
index fbca26449..b038d8573 100644
--- a/python/packages/autogen-agentchat/src/autogen_agentchat/teams/_group_chat/_base_group_chat.py
+++ b/python/packages/autogen-agentchat/src/autogen_agentchat/teams/_group_chat/_base_group_chat.py
@@ -14,6 +14,7 @@ from autogen_core.base import (
MessageContext,
)
from autogen_core.components import ClosureAgent, TypeSubscription
+from autogen_core.components._closure_agent import ClosureContext
from ... import EVENT_LOGGER_NAME
from ...base import ChatAgent, TaskResult, Team, TerminationCondition
@@ -139,8 +140,7 @@ class BaseGroupChat(Team, ABC):
)
async def collect_output_messages(
- _runtime: AgentRuntime,
- id: AgentId,
+ _runtime: ClosureContext,
message: GroupChatStart | GroupChatMessage | GroupChatTermination,
ctx: MessageContext,
) -> None:
@@ -150,7 +150,7 @@ class BaseGroupChat(Team, ABC):
return
await self._output_message_queue.put(message.message)
- await ClosureAgent.register(
+ await ClosureAgent.register_closure(
runtime,
type=self._collector_agent_type,
closure=collect_output_messages,
diff --git a/python/packages/autogen-core/docs/src/user-guide/core-user-guide/cookbook/extracting-results-with-an-agent.ipynb b/python/packages/autogen-core/docs/src/user-guide/core-user-guide/cookbook/extracting-results-with-an-agent.ipynb
index c386699f0..e86b807d7 100644
--- a/python/packages/autogen-core/docs/src/user-guide/core-user-guide/cookbook/extracting-results-with-an-agent.ipynb
+++ b/python/packages/autogen-core/docs/src/user-guide/core-user-guide/cookbook/extracting-results-with-an-agent.ipynb
@@ -18,7 +18,7 @@
},
{
"cell_type": "code",
- "execution_count": 1,
+ "execution_count": null,
"metadata": {},
"outputs": [],
"source": [
@@ -26,8 +26,8 @@
"from dataclasses import dataclass\n",
"\n",
"from autogen_core.application import SingleThreadedAgentRuntime\n",
- "from autogen_core.base import AgentId, AgentRuntime, MessageContext\n",
- "from autogen_core.components import ClosureAgent, DefaultSubscription, DefaultTopicId"
+ "from autogen_core.base import MessageContext\n",
+ "from autogen_core.components import ClosureAgent, ClosureContext, DefaultSubscription, DefaultTopicId"
]
},
{
@@ -77,11 +77,11 @@
},
{
"cell_type": "code",
- "execution_count": 4,
+ "execution_count": null,
"metadata": {},
"outputs": [],
"source": [
- "async def output_result(_runtime: AgentRuntime, id: AgentId, message: FinalResult, ctx: MessageContext) -> None:\n",
+ "async def output_result(_agent: ClosureContext, message: FinalResult, ctx: MessageContext) -> None:\n",
" await queue.put(message)"
]
},
@@ -94,7 +94,7 @@
},
{
"cell_type": "code",
- "execution_count": 5,
+ "execution_count": null,
"metadata": {},
"outputs": [
{
@@ -110,7 +110,9 @@
],
"source": [
"runtime = SingleThreadedAgentRuntime()\n",
- "await ClosureAgent.register(runtime, \"output_result\", output_result, subscriptions=lambda: [DefaultSubscription()])"
+ "await ClosureAgent.register_closure(\n",
+ " runtime, \"output_result\", output_result, subscriptions=lambda: [DefaultSubscription()]\n",
+ ")"
]
},
{
diff --git a/python/packages/autogen-core/samples/semantic_router/run_semantic_router.py b/python/packages/autogen-core/samples/semantic_router/run_semantic_router.py
index 48f57eece..35d057fb9 100644
--- a/python/packages/autogen-core/samples/semantic_router/run_semantic_router.py
+++ b/python/packages/autogen-core/samples/semantic_router/run_semantic_router.py
@@ -32,8 +32,8 @@ from _semantic_router_components import (
WorkerAgentMessage,
)
from autogen_core.application import WorkerAgentRuntime
-from autogen_core.base import AgentId, AgentRuntime, MessageContext
-from autogen_core.components import ClosureAgent, DefaultSubscription, DefaultTopicId
+from autogen_core.base import MessageContext
+from autogen_core.components import ClosureAgent, ClosureContext, DefaultSubscription, DefaultTopicId
class MockIntentClassifier(IntentClassifierBase):
@@ -60,12 +60,12 @@ class MockAgentRegistry(AgentRegistryBase):
async def output_result(
- _runtime: AgentRuntime, id: AgentId, message: WorkerAgentMessage | FinalResult, ctx: MessageContext
+ closure_ctx: ClosureContext, message: WorkerAgentMessage | FinalResult, ctx: MessageContext
) -> None:
if isinstance(message, WorkerAgentMessage):
print(f"{message.source} Agent: {message.content}")
new_message = input("User response: ")
- await _runtime.publish_message(
+ await closure_ctx.publish_message(
UserProxyMessage(content=new_message, source="user"),
topic_id=DefaultTopicId(type=message.source, source="user"),
)
@@ -73,7 +73,7 @@ async def output_result(
print(f"{message.source} Agent: {message.content}")
print("Conversation ended")
new_message = input("Enter a new conversation start: ")
- await _runtime.publish_message(
+ await closure_ctx.publish_message(
UserProxyMessage(content=new_message, source="user"), topic_id=DefaultTopicId(type="default", source="user")
)
@@ -95,7 +95,7 @@ async def run_workers():
await agent_runtime.add_subscription(DefaultSubscription(topic_type="user_proxy", agent_type="user_proxy"))
# A closure agent surfaces the final result to external systems (e.g. an API) so that the system can interact with the user
- await ClosureAgent.register(
+ await ClosureAgent.register_closure(
agent_runtime,
"closure_agent",
output_result,
diff --git a/python/packages/autogen-core/src/autogen_core/components/__init__.py b/python/packages/autogen-core/src/autogen_core/components/__init__.py
index 75bb5eabc..37d1ad48a 100644
--- a/python/packages/autogen-core/src/autogen_core/components/__init__.py
+++ b/python/packages/autogen-core/src/autogen_core/components/__init__.py
@@ -3,7 +3,7 @@ The :mod:`autogen_core.components` module provides building blocks for creating
"""
from ..base._type_prefix_subscription import TypePrefixSubscription
-from ._closure_agent import ClosureAgent
+from ._closure_agent import ClosureAgent, ClosureContext
from ._default_subscription import DefaultSubscription, default_subscription, type_subscription
from ._default_topic import DefaultTopicId
from ._image import Image
@@ -16,6 +16,7 @@ __all__ = [
"RoutedAgent",
"TypeRoutedAgent",
"ClosureAgent",
+ "ClosureContext",
"message_handler",
"event",
"rpc",
diff --git a/python/packages/autogen-core/src/autogen_core/components/_closure_agent.py b/python/packages/autogen-core/src/autogen_core/components/_closure_agent.py
index 1123c3ee4..12e5faae6 100644
--- a/python/packages/autogen-core/src/autogen_core/components/_closure_agent.py
+++ b/python/packages/autogen-core/src/autogen_core/components/_closure_agent.py
@@ -1,32 +1,38 @@
+from __future__ import annotations
+
import inspect
-from typing import Any, Awaitable, Callable, List, Mapping, Sequence, TypeVar, get_type_hints
+from typing import Any, Awaitable, Callable, List, Mapping, Protocol, Sequence, TypeVar, get_type_hints
+
+from autogen_core.base._serialization import try_get_known_serializers_for_type
+from autogen_core.base._subscription_context import SubscriptionInstantiationContext
from ..base import (
- Agent,
AgentId,
AgentInstantiationContext,
AgentMetadata,
AgentRuntime,
AgentType,
+ BaseAgent,
+ CancellationToken,
MessageContext,
Subscription,
- SubscriptionInstantiationContext,
- try_get_known_serializers_for_type,
+ TopicId,
)
from ..base._type_helpers import get_types
from ..base.exceptions import CantHandleException
T = TypeVar("T")
+ClosureAgentType = TypeVar("ClosureAgentType", bound="ClosureAgent")
def get_handled_types_from_closure(
- closure: Callable[[AgentRuntime, AgentId, T, MessageContext], Awaitable[Any]],
+ closure: Callable[[ClosureAgent, T, MessageContext], Awaitable[Any]],
) -> Sequence[type]:
args = inspect.getfullargspec(closure)[0]
- if len(args) != 4:
+ if len(args) != 3:
raise AssertionError("Closure must have 4 arguments")
- message_arg_name = args[2]
+ message_arg_name = args[1]
type_hints = get_type_hints(closure)
@@ -47,9 +53,30 @@ def get_handled_types_from_closure(
return target_types
-class ClosureAgent(Agent):
+class ClosureContext(Protocol):
+ @property
+ def id(self) -> AgentId: ...
+
+ async def send_message(
+ self,
+ message: Any,
+ recipient: AgentId,
+ *,
+ cancellation_token: CancellationToken | None = None,
+ ) -> Any: ...
+
+ async def publish_message(
+ self,
+ message: Any,
+ topic_id: TopicId,
+ *,
+ cancellation_token: CancellationToken | None = None,
+ ) -> None: ...
+
+
+class ClosureAgent(BaseAgent, ClosureContext):
def __init__(
- self, description: str, closure: Callable[[AgentRuntime, AgentId, T, MessageContext], Awaitable[Any]]
+ self, description: str, closure: Callable[[ClosureContext, T, MessageContext], Awaitable[Any]]
) -> None:
try:
runtime = AgentInstantiationContext.current_runtime()
@@ -65,6 +92,7 @@ class ClosureAgent(Agent):
handled_types = get_handled_types_from_closure(closure)
self._expected_types = handled_types
self._closure = closure
+ super().__init__(description)
@property
def metadata(self) -> AgentMetadata:
@@ -88,7 +116,7 @@ class ClosureAgent(Agent):
raise CantHandleException(
f"Message type {type(message)} not in target types {self._expected_types} of {self.id}"
)
- return await self._closure(self._runtime, self._id, message, ctx)
+ return await self._closure(self, message, ctx)
async def save_state(self) -> Mapping[str, Any]:
raise ValueError("save_state not implemented for ClosureAgent")
@@ -97,16 +125,28 @@ class ClosureAgent(Agent):
raise ValueError("load_state not implemented for ClosureAgent")
@classmethod
- async def register(
+ async def register_closure(
cls,
runtime: AgentRuntime,
type: str,
- closure: Callable[[AgentRuntime, AgentId, T, MessageContext], Awaitable[Any]],
+ closure: Callable[[ClosureContext, T, MessageContext], Awaitable[Any]],
*,
+ skip_class_subscriptions: bool = False,
+ skip_direct_message_subscription: bool = False,
description: str = "",
subscriptions: Callable[[], list[Subscription] | Awaitable[list[Subscription]]] | None = None,
) -> AgentType:
- agent_type = AgentType(type)
+ def factory() -> ClosureAgent:
+ return ClosureAgent(description=description, closure=closure)
+
+ agent_type = await cls.register(
+ runtime=runtime,
+ type=type,
+ factory=factory, # type: ignore
+ skip_class_subscriptions=skip_class_subscriptions,
+ skip_direct_message_subscription=skip_direct_message_subscription,
+ )
+
subscriptions_list: List[Subscription] = []
if subscriptions is not None:
with SubscriptionInstantiationContext.populate_context(agent_type):
@@ -117,11 +157,6 @@ class ClosureAgent(Agent):
# just ignore mypy here
subscriptions_list.extend(subscriptions_list_result) # type: ignore
- agent_type = await runtime.register_factory(
- type=agent_type,
- agent_factory=lambda: ClosureAgent(description=description, closure=closure),
- expected_class=cls,
- )
for subscription in subscriptions_list:
await runtime.add_subscription(subscription)
diff --git a/python/packages/autogen-core/tests/test_closure_agent.py b/python/packages/autogen-core/tests/test_closure_agent.py
index a8731bca3..328fe2374 100644
--- a/python/packages/autogen-core/tests/test_closure_agent.py
+++ b/python/packages/autogen-core/tests/test_closure_agent.py
@@ -3,9 +3,8 @@ from dataclasses import dataclass
import pytest
from autogen_core.application import SingleThreadedAgentRuntime
-from autogen_core.base import AgentId, AgentRuntime, MessageContext
-from autogen_core.components import ClosureAgent, DefaultSubscription
-from autogen_core.components._default_topic import DefaultTopicId
+from autogen_core.base import MessageContext
+from autogen_core.components import ClosureAgent, ClosureContext, DefaultSubscription, DefaultTopicId
@dataclass
@@ -19,11 +18,11 @@ async def test_register_receives_publish() -> None:
queue = asyncio.Queue[tuple[str, str]]()
- async def log_message(_runtime: AgentRuntime, id: AgentId, message: Message, ctx: MessageContext) -> None:
- key = id.key
+ async def log_message(closure_ctx: ClosureContext, message: Message, ctx: MessageContext) -> None:
+ key = closure_ctx.id.key
await queue.put((key, message.content))
- await ClosureAgent.register(runtime, "name", log_message, subscriptions=lambda: [DefaultSubscription()])
+ await ClosureAgent.register_closure(runtime, "name", log_message, subscriptions=lambda: [DefaultSubscription()])
runtime.start()
await runtime.publish_message(Message("first message"), topic_id=DefaultTopicId())