A programming framework for agentic AI
Register returns AgentType (#382)
Jack Gerrits29088d67a47c
diff --git a/python/src/agnext/application/_single_threaded_agent_runtime.py b/python/src/agnext/application/_single_threaded_agent_runtime.pyindex 5cfd3e5d4..87eab51fa 100644--- a/python/src/agnext/application/_single_threaded_agent_runtime.py+++ b/python/src/agnext/application/_single_threaded_agent_runtime.py@@ -12,7 +12,7 @@ from dataclasses import dataclassfrom enum import Enumfrom typing import Any, Awaitable, Callable, DefaultDict, Dict, List, Mapping, ParamSpec, Set, Type, TypeVar, cast-from agnext.core import Subscription, TopicId+from agnext.core import AgentType, Subscription, TopicIdfrom ..core import (Agent,@@ -445,10 +445,11 @@ class SingleThreadedAgentRuntime(AgentRuntime):self,type: str,agent_factory: Callable[[], T | Awaitable[T]] | Callable[[AgentRuntime, AgentId], T | Awaitable[T]],- ) -> None:+ ) -> AgentType:if type in self._agent_factories:raise ValueError(f"Agent with type {type} already exists.")self._agent_factories[type] = agent_factory+ return AgentType(type)async def _invoke_agent_factory(self,diff --git a/python/src/agnext/application/_worker_runtime.py b/python/src/agnext/application/_worker_runtime.pyindex 42e58bc63..ed101aa0d 100644--- a/python/src/agnext/application/_worker_runtime.py+++ b/python/src/agnext/application/_worker_runtime.py@@ -28,7 +28,7 @@ import grpcfrom grpc.aio import StreamStreamCallfrom typing_extensions import Self-from agnext.core import MESSAGE_TYPE_REGISTRY, MessageContext, Subscription, TopicId+from agnext.core import MESSAGE_TYPE_REGISTRY, AgentType, MessageContext, Subscription, TopicIdfrom ..core import Agent, AgentId, AgentInstantiationContext, AgentMetadata, AgentRuntime, CancellationTokenfrom .protos import AgentId as AgentIdProto@@ -352,7 +352,7 @@ class WorkerAgentRuntime(AgentRuntime):self,type: str,agent_factory: Callable[[], T | Awaitable[T]],- ) -> None:+ ) -> AgentType:if type in self._agent_factories:raise ValueError(f"Agent with type {type} already exists.")self._agent_factories[type] = agent_factory@@ -361,6 +361,7 @@ class WorkerAgentRuntime(AgentRuntime):message = Message(registerAgentType=RegisterAgentType(type=type))await self._host_connection.send(message)logger.info("Sent registerAgentType message for %s", type)+ return AgentType(type)async def _invoke_agent_factory(self,diff --git a/python/src/agnext/core/__init__.py b/python/src/agnext/core/__init__.pyindex 851a4f347..1c78ead2f 100644--- a/python/src/agnext/core/__init__.py+++ b/python/src/agnext/core/__init__.py@@ -9,6 +9,7 @@ from ._agent_metadata import AgentMetadatafrom ._agent_props import AgentChildrenfrom ._agent_proxy import AgentProxyfrom ._agent_runtime import AgentRuntime+from ._agent_type import AgentTypefrom ._base_agent import BaseAgentfrom ._cancellation_token import CancellationTokenfrom ._message_context import MessageContext@@ -33,4 +34,5 @@ __all__ = ["Subscription","MessageContext","Serialization",+ "AgentType",]diff --git a/python/src/agnext/core/_agent_id.py b/python/src/agnext/core/_agent_id.pyindex a1d9ae6d5..1459f250f 100644--- a/python/src/agnext/core/_agent_id.py+++ b/python/src/agnext/core/_agent_id.py@@ -1,8 +1,13 @@from typing_extensions import Self+from ._agent_type import AgentType+class AgentId:- def __init__(self, type: str, key: str) -> None:+ def __init__(self, type: str | AgentType, key: str) -> None:+ if isinstance(type, AgentType):+ type = type.type+if type.isidentifier() is False:raise ValueError(f"Invalid type: {type}")diff --git a/python/src/agnext/core/_agent_runtime.py b/python/src/agnext/core/_agent_runtime.pyindex 0f05f2f2d..33368c5dc 100644--- a/python/src/agnext/core/_agent_runtime.py+++ b/python/src/agnext/core/_agent_runtime.py@@ -5,6 +5,7 @@ from typing import Any, Awaitable, Callable, Mapping, Protocol, Type, TypeVar, rfrom ._agent import Agentfrom ._agent_id import AgentIdfrom ._agent_metadata import AgentMetadata+from ._agent_type import AgentTypefrom ._cancellation_token import CancellationTokenfrom ._subscription import Subscriptionfrom ._topic import TopicId@@ -70,7 +71,7 @@ class AgentRuntime(Protocol):self,type: str,agent_factory: Callable[[], T | Awaitable[T]],- ) -> None:+ ) -> AgentType:"""Register an agent factory with the runtime associated with a specific type. The type must be unique.Args:diff --git a/python/src/agnext/core/_agent_type.py b/python/src/agnext/core/_agent_type.pynew file mode 100644index 000000000..009f8c9c4--- /dev/null+++ b/python/src/agnext/core/_agent_type.py@@ -0,0 +1,7 @@+from dataclasses import dataclass+++@dataclass(eq=True, frozen=True)+class AgentType:+ type: str+ """String representation of this agent type."""