A programming framework for agentic AI
[Python] Specify subscriptions as strings and not types (#169)
Jack Gerritsbf32bb67ae66
diff --git a/python/pyproject.toml b/python/pyproject.tomlindex d2c42b57b..ef14f4141 100644--- a/python/pyproject.toml+++ b/python/pyproject.toml@@ -47,6 +47,7 @@ dependencies = ["pytest-mock","grpcio-tools","markdownify",+ "types-protobuf",][tool.hatch.envs.default.extra-scripts]diff --git a/python/src/agnext/application/_single_threaded_agent_runtime.py b/python/src/agnext/application/_single_threaded_agent_runtime.pyindex 3e9e05f21..f4511ee3a 100644--- a/python/src/agnext/application/_single_threaded_agent_runtime.py+++ b/python/src/agnext/application/_single_threaded_agent_runtime.py@@ -12,6 +12,7 @@ from enum import Enumfrom typing import Any, Awaitable, Callable, DefaultDict, Dict, List, Mapping, ParamSpec, Set, TypeVar, castfrom ..core import (+ MESSAGE_TYPE_REGISTRY,Agent,AgentId,AgentMetadata,@@ -121,7 +122,7 @@ class SingleThreadedAgentRuntime(AgentRuntime):def __init__(self, *, intervention_handler: InterventionHandler | None = None) -> None:self._message_queue: List[PublishMessageEnvelope | SendMessageEnvelope | ResponseMessageEnvelope] = []# (namespace, type) -> List[AgentId]- self._per_type_subscribers: DefaultDict[tuple[str, type], Set[AgentId]] = defaultdict(set)+ self._per_type_subscribers: DefaultDict[tuple[str, str], Set[AgentId]] = defaultdict(set)self._agent_factories: Dict[str, Callable[[], Agent] | Callable[[AgentRuntime, AgentId], Agent]] = {}self._instantiated_agents: Dict[AgentId, Agent] = {}self._intervention_handler = intervention_handler@@ -290,7 +291,9 @@ class SingleThreadedAgentRuntime(AgentRuntime):async def _process_publish(self, message_envelope: PublishMessageEnvelope) -> None:responses: List[Awaitable[Any]] = []target_namespace = message_envelope.namespace- for agent_id in self._per_type_subscribers[(target_namespace, type(message_envelope.message))]:+ for agent_id in self._per_type_subscribers[+ (target_namespace, MESSAGE_TYPE_REGISTRY.type_name(message_envelope.message))+ ]:if message_envelope.sender is not None and agent_id.name == message_envelope.sender.name:continuediff --git a/python/src/agnext/components/_closure_agent.py b/python/src/agnext/components/_closure_agent.pyindex ca9f0d6de..b2e9adec8 100644--- a/python/src/agnext/components/_closure_agent.py+++ b/python/src/agnext/components/_closure_agent.py@@ -6,6 +6,7 @@ from ..core._agent_id import AgentIdfrom ..core._agent_metadata import AgentMetadatafrom ..core._agent_runtime import AgentRuntime, agent_instantiation_contextfrom ..core._cancellation_token import CancellationToken+from ..core._serialization import MESSAGE_TYPE_REGISTRYfrom ..core.exceptions import CantHandleExceptionfrom ._type_helpers import get_types@@ -54,7 +55,11 @@ class ClosureAgent(Agent):self._runtime: AgentRuntime = runtimeself._id: AgentId = idself._description = description- self._subscriptions = get_subscriptions_from_closure(closure)+ subscription_types = get_subscriptions_from_closure(closure)+ for message_type in subscription_types:+ if not MESSAGE_TYPE_REGISTRY.is_registered(MESSAGE_TYPE_REGISTRY.type_name(message_type)):+ MESSAGE_TYPE_REGISTRY.add_type(message_type)+ self._subscriptions = [MESSAGE_TYPE_REGISTRY.type_name(message_type) for message_type in subscription_types]self._closure = closure@property@@ -80,7 +85,7 @@ class ClosureAgent(Agent):return self._runtimeasync def on_message(self, message: Any, cancellation_token: CancellationToken) -> Any:- if type(message) not in self._subscriptions:+ if MESSAGE_TYPE_REGISTRY.type_name(message) not in self._subscriptions:raise CantHandleException(f"Message type {type(message)} not in target types {self._subscriptions} of {self.id}")diff --git a/python/src/agnext/components/_type_routed_agent.py b/python/src/agnext/components/_type_routed_agent.pyindex 8aac424c4..2c3d9c655 100644--- a/python/src/agnext/components/_type_routed_agent.py+++ b/python/src/agnext/components/_type_routed_agent.py@@ -17,7 +17,7 @@ from typing import (runtime_checkable,)-from ..core import BaseAgent, CancellationToken+from ..core import MESSAGE_TYPE_REGISTRY, BaseAgent, CancellationTokenfrom ..core.exceptions import CantHandleExceptionfrom ._type_helpers import AnyType, get_types@@ -143,7 +143,11 @@ class TypeRoutedAgent(BaseAgent):for target_type in message_handler.target_types:self._handlers[target_type] = message_handlersubscriptions = list(self._handlers.keys())- super().__init__(description, subscriptions)+ for message_type in self._handlers.keys():+ if not MESSAGE_TYPE_REGISTRY.is_registered(MESSAGE_TYPE_REGISTRY.type_name(message_type)):+ MESSAGE_TYPE_REGISTRY.add_type(message_type)+ subscriptions_str = [MESSAGE_TYPE_REGISTRY.type_name(message_type) for message_type in subscriptions]+ super().__init__(description, subscriptions_str)async def on_message(self, message: Any, cancellation_token: CancellationToken) -> Any | None:key_type: Type[Any] = type(message) # type: ignorediff --git a/python/src/agnext/core/__init__.py b/python/src/agnext/core/__init__.pyindex 083456716..98265e618 100644--- a/python/src/agnext/core/__init__.py+++ b/python/src/agnext/core/__init__.py@@ -10,6 +10,7 @@ from ._agent_proxy import AgentProxyfrom ._agent_runtime import AgentRuntime, agent_instantiation_contextfrom ._base_agent import BaseAgentfrom ._cancellation_token import CancellationToken+from ._serialization import MESSAGE_TYPE_REGISTRY, TypeDeserializer, TypeSerializer__all__ = ["Agent",@@ -21,4 +22,7 @@ __all__ = ["CancellationToken","AgentChildren","agent_instantiation_context",+ "MESSAGE_TYPE_REGISTRY",+ "TypeSerializer",+ "TypeDeserializer",]diff --git a/python/src/agnext/core/_agent_metadata.py b/python/src/agnext/core/_agent_metadata.pyindex d3cb7259d..5b8bcb2ed 100644--- a/python/src/agnext/core/_agent_metadata.py+++ b/python/src/agnext/core/_agent_metadata.py@@ -5,4 +5,4 @@ class AgentMetadata(TypedDict):name: strnamespace: strdescription: str- subscriptions: Sequence[type]+ subscriptions: Sequence[str]diff --git a/python/src/agnext/core/_base_agent.py b/python/src/agnext/core/_base_agent.pyindex c30e7048e..fdc6c681f 100644--- a/python/src/agnext/core/_base_agent.py+++ b/python/src/agnext/core/_base_agent.py@@ -20,7 +20,7 @@ class BaseAgent(ABC, Agent):subscriptions=self._subscriptions,)- def __init__(self, description: str, subscriptions: Sequence[type]) -> None:+ def __init__(self, description: str, subscriptions: Sequence[str]) -> None:try:runtime, id = agent_instantiation_context.get()except LookupError as e:diff --git a/python/src/agnext/application/message_serialization.py b/python/src/agnext/core/_serialization.pysimilarity index 96%rename from python/src/agnext/application/message_serialization.pyrename to python/src/agnext/core/_serialization.pyindex 256add2bc..abf538594 100644--- a/python/src/agnext/application/message_serialization.py+++ b/python/src/agnext/core/_serialization.py@@ -114,3 +114,9 @@ class Serialization:def serialize(self, message: Any, *, type_name: str) -> str:return self._serializers[type_name].serialize(message)++ def is_registered(self, type_name: str) -> bool:+ return type_name in self._deserializers+++MESSAGE_TYPE_REGISTRY = Serialization()diff --git a/python/src/agnext/worker/worker_runtime.py b/python/src/agnext/worker/worker_runtime.pyindex 632370e0c..37cd61f91 100644--- a/python/src/agnext/worker/worker_runtime.py+++ b/python/src/agnext/worker/worker_runtime.py@@ -28,7 +28,7 @@ import grpcfrom grpc.aio import StreamStreamCallfrom typing_extensions import Self-from agnext.application.message_serialization import Serialization+from agnext.core import MESSAGE_TYPE_REGISTRYfrom ..core import (Agent,@@ -177,7 +177,7 @@ class WorkerAgentRuntime(AgentRuntime):def __init__(self) -> None:self._message_queue: List[PublishMessageEnvelope | SendMessageEnvelope | ResponseMessageEnvelope] = []# (namespace, type) -> List[AgentId]- self._per_type_subscribers: DefaultDict[tuple[str, type], Set[AgentId]] = defaultdict(set)+ self._per_type_subscribers: DefaultDict[tuple[str, str], Set[AgentId]] = defaultdict(set)self._agent_factories: Dict[str, Callable[[], Agent] | Callable[[AgentRuntime, AgentId], Agent]] = {}# If empty, then all namespaces are valid for that agent typeself._valid_namespaces: Dict[str, Sequence[str]] = {}@@ -185,7 +185,6 @@ class WorkerAgentRuntime(AgentRuntime):self._known_namespaces: set[str] = set()self._read_task: None | Task[None] = Noneself._running = False- self._serialization = Serialization()self._pending_requests: Dict[str, Future[Any]] = {}self._pending_requests_lock = threading.Lock()self._next_request_id = 0@@ -227,10 +226,10 @@ class WorkerAgentRuntime(AgentRuntime):future.set_result(response.result)case "event":event: Event = message.event- message = self._serialization.deserialize(event.data, type_name=event.type)+ message = MESSAGE_TYPE_REGISTRY.deserialize(event.data, type_name=event.type)namespace = event.namespace- for agent_id in self._per_type_subscribers[(namespace, type(message))]:+ for agent_id in self._per_type_subscribers[(namespace, MESSAGE_TYPE_REGISTRY.type_name(message))]:agent = self._get_agent(agent_id)try:await agent.on_message(message, CancellationToken())@@ -301,8 +300,8 @@ class WorkerAgentRuntime(AgentRuntime):assert explicit_namespace is not None or sender_namespace is not Noneactual_namespace = cast(str, explicit_namespace or sender_namespace)self._process_seen_namespace(actual_namespace)- message_type = self._serialization.type_name(message)- serialized_message = self._serialization.serialize(message, type_name=message_type)+ message_type = MESSAGE_TYPE_REGISTRY.type_name(message)+ serialized_message = MESSAGE_TYPE_REGISTRY.serialize(message, type_name=message_type)message = Message(event=Event(namespace=actual_namespace, type=message_type, data=serialized_message))async def write_message() -> None:@@ -371,7 +370,6 @@ class WorkerAgentRuntime(AgentRuntime):agent = self._invoke_agent_factory(agent_factory, agent_id)for message_type in agent.metadata["subscriptions"]:self._per_type_subscribers[(agent_id.namespace, message_type)].add(agent_id)- self._serialization.add_type(message_type)self._instantiated_agents[agent_id] = agentreturn agent@@ -392,6 +390,3 @@ class WorkerAgentRuntime(AgentRuntime):self._known_namespaces.add(namespace)for name in self._known_agent_names:self._get_agent(AgentId(name=name, namespace=namespace))-- def add_serialization_type(self, message_type: type) -> None:- self._serialization.add_type(message_type)diff --git a/python/tests/test_serialization.py b/python/tests/test_serialization.pyindex 5571cbf01..7ffd96388 100644--- a/python/tests/test_serialization.py+++ b/python/tests/test_serialization.py@@ -5,7 +5,7 @@ from dataclasses import dataclassimport pytest-from agnext.application.message_serialization import Serialization+from agnext.core._serialization import Serializationclass PydanticMessage(BaseModel):message: str