Skip to content

A programming framework for agentic AI

Implement AgentRuntime.get (#385)

Jack Gerrits63000e542424
diff --git a/python/src/agnext/application/_helpers.py b/python/src/agnext/application/_helpers.py
new file mode 100644
index 000000000..f9d4813a5
--- /dev/null
+++ b/python/src/agnext/application/_helpers.py
@@ -0,0 +1,26 @@
+from typing import Awaitable, Callable
+
+from ..core._agent import Agent
+from ..core._agent_id import AgentId
+from ..core._agent_type import AgentType
+
+
+async def get_impl(
+ *,
+ id_or_type: AgentId | AgentType | str,
+ key: str,
+ lazy: bool,
+ instance_getter: Callable[[AgentId], Awaitable[Agent]],
+) -> AgentId:
+ if isinstance(id_or_type, AgentId):
+ if not lazy:
+ await instance_getter(id_or_type)
+
+ return id_or_type
+
+ type_str = id_or_type if isinstance(id_or_type, str) else id_or_type.type
+ id = AgentId(type_str, key)
+ if not lazy:
+ await instance_getter(id)
+
+ return id
diff --git a/python/src/agnext/application/_single_threaded_agent_runtime.py b/python/src/agnext/application/_single_threaded_agent_runtime.py
index 87eab51fa..a68e5ebe2 100644
--- a/python/src/agnext/application/_single_threaded_agent_runtime.py
+++ b/python/src/agnext/application/_single_threaded_agent_runtime.py
@@ -25,6 +25,7 @@ from ..core import (
)
from ..core.exceptions import MessageDroppedException
from ..core.intervention import DropMessage, InterventionHandler
+from ._helpers import get_impl
logger = logging.getLogger("agnext")
event_logger = logging.getLogger("agnext.events")
@@ -537,3 +538,13 @@ class SingleThreadedAgentRuntime(AgentRuntime):
for subscription in self._subscriptions:
if subscription.is_match(topic):
self._subscribed_recipients[topic].append(subscription.map_to_agent(topic))
+
+ async def get(
+ self, id_or_type: AgentId | AgentType | str, /, key: str = "default", *, lazy: bool = True
+ ) -> AgentId:
+ return await get_impl(
+ id_or_type=id_or_type,
+ key=key,
+ lazy=lazy,
+ instance_getter=self._get_agent,
+ )
diff --git a/python/src/agnext/application/_worker_runtime.py b/python/src/agnext/application/_worker_runtime.py
index ed101aa0d..4bfbb26d7 100644
--- a/python/src/agnext/application/_worker_runtime.py
+++ b/python/src/agnext/application/_worker_runtime.py
@@ -28,9 +28,20 @@ import grpc
from grpc.aio import StreamStreamCall
from typing_extensions import Self
-from agnext.core import MESSAGE_TYPE_REGISTRY, AgentType, MessageContext, Subscription, TopicId
-
-from ..core import Agent, AgentId, AgentInstantiationContext, AgentMetadata, AgentRuntime, CancellationToken
+from ..core import (
+ MESSAGE_TYPE_REGISTRY,
+ Agent,
+ AgentId,
+ AgentInstantiationContext,
+ AgentMetadata,
+ AgentRuntime,
+ AgentType,
+ CancellationToken,
+ MessageContext,
+ Subscription,
+ TopicId,
+)
+from ._helpers import get_impl
from .protos import AgentId as AgentIdProto
from .protos import (
AgentRpcStub,
@@ -408,3 +419,13 @@ class WorkerAgentRuntime(AgentRuntime):
async def remove_subscription(self, id: str) -> None:
raise NotImplementedError("Subscriptions are not yet implemented.")
+
+ async def get(
+ self, id_or_type: AgentId | AgentType | str, /, key: str = "default", *, lazy: bool = True
+ ) -> AgentId:
+ return await get_impl(
+ id_or_type=id_or_type,
+ key=key,
+ lazy=lazy,
+ instance_getter=self._get_agent,
+ )
diff --git a/python/src/agnext/core/_agent_runtime.py b/python/src/agnext/core/_agent_runtime.py
index 33368c5dc..2c995d41c 100644
--- a/python/src/agnext/core/_agent_runtime.py
+++ b/python/src/agnext/core/_agent_runtime.py
@@ -1,6 +1,6 @@
from __future__ import annotations
-from typing import Any, Awaitable, Callable, Mapping, Protocol, Type, TypeVar, runtime_checkable
+from typing import Any, Awaitable, Callable, Mapping, Protocol, Type, TypeVar, overload, runtime_checkable
from ._agent import Agent
from ._agent_id import AgentId
@@ -116,6 +116,16 @@ class AgentRuntime(Protocol):
"""
...
+ @overload
+ async def get(self, id: AgentId, /, *, lazy: bool = ...) -> AgentId: ...
+
+ @overload
+ async def get(self, type: AgentType | str, /, key: str = ..., *, lazy: bool = ...) -> AgentId: ...
+
+ async def get(
+ self, id_or_type: AgentId | AgentType | str, /, key: str = "default", *, lazy: bool = True
+ ) -> AgentId: ...
+
async def save_state(self) -> Mapping[str, Any]:
"""Save the state of the entire runtime, including all hosted agents. The only way to restore the state is to pass it to :meth:`load_state`.