A programming framework for agentic AI
remove binding from base agent (#100)
Jack Gerritsafdbae2bb9b1
diff --git a/python/src/agnext/application/_single_threaded_agent_runtime.py b/python/src/agnext/application/_single_threaded_agent_runtime.pyindex 6a59d21c2..0db1ae5e3 100644--- a/python/src/agnext/application/_single_threaded_agent_runtime.py+++ b/python/src/agnext/application/_single_threaded_agent_runtime.py@@ -8,7 +8,16 @@ from collections.abc import Sequencefrom dataclasses import dataclassfrom typing import Any, Awaitable, Callable, DefaultDict, Dict, List, Mapping, ParamSpec, Set, Type, TypeVar, cast-from ..core import Agent, AgentId, AgentMetadata, AgentProxy, AgentRuntime, AllNamespaces, BaseAgent, CancellationToken+from ..core import (+ Agent,+ AgentId,+ AgentMetadata,+ AgentProxy,+ AgentRuntime,+ AllNamespaces,+ CancellationToken,+ agent_instantiation_context,+)from ..core.exceptions import MessageDroppedExceptionfrom ..core.intervention import DropMessage, InterventionHandler@@ -395,6 +404,8 @@ class SingleThreadedAgentRuntime(AgentRuntime):def _invoke_agent_factory(self, agent_factory: Callable[[], T] | Callable[[AgentRuntime, AgentId], T], agent_id: AgentId) -> T:+ token = agent_instantiation_context.set((self, agent_id))+if len(inspect.signature(agent_factory).parameters) == 0:factory_one = cast(Callable[[], T], agent_factory)agent = factory_one()@@ -404,10 +415,7 @@ class SingleThreadedAgentRuntime(AgentRuntime):else:raise ValueError("Agent factory must take 0 or 2 arguments.")- # TODO: should this be part of the base agent interface?- if isinstance(agent, BaseAgent):- agent.bind_id(agent_id)- agent.bind_runtime(self)+ agent_instantiation_context.reset(token)return agentdiff --git a/python/src/agnext/core/__init__.py b/python/src/agnext/core/__init__.pyindex 76f404387..210e87fd3 100644--- a/python/src/agnext/core/__init__.py+++ b/python/src/agnext/core/__init__.py@@ -7,7 +7,7 @@ from ._agent_id import AgentIdfrom ._agent_metadata import AgentMetadatafrom ._agent_props import AgentChildrenfrom ._agent_proxy import AgentProxy-from ._agent_runtime import AgentRuntime, AllNamespaces+from ._agent_runtime import AgentRuntime, AllNamespaces, agent_instantiation_contextfrom ._base_agent import BaseAgentfrom ._cancellation_token import CancellationToken@@ -21,4 +21,5 @@ __all__ = ["BaseAgent","CancellationToken","AgentChildren",+ "agent_instantiation_context",]diff --git a/python/src/agnext/core/_agent_runtime.py b/python/src/agnext/core/_agent_runtime.pyindex 12b2f97c5..a372eb1fc 100644--- a/python/src/agnext/core/_agent_runtime.py+++ b/python/src/agnext/core/_agent_runtime.py@@ -1,6 +1,7 @@from __future__ import annotationsfrom asyncio import Future+from contextvars import ContextVarfrom typing import Any, Callable, Mapping, Protocol, Sequence, Type, TypeVar, overload, runtime_checkablefrom ._agent import Agent@@ -13,6 +14,8 @@ from ._cancellation_token import CancellationTokenT = TypeVar("T", bound=Agent)+agent_instantiation_context: ContextVar[tuple[AgentRuntime, AgentId]] = ContextVar("agent_instantiation_context")+class AllNamespaces:passdiff --git a/python/src/agnext/core/_base_agent.py b/python/src/agnext/core/_base_agent.pyindex 239bc7a1c..041328f62 100644--- a/python/src/agnext/core/_base_agent.py+++ b/python/src/agnext/core/_base_agent.py@@ -6,7 +6,7 @@ from typing import Any, Mapping, Sequencefrom ._agent import Agentfrom ._agent_id import AgentIdfrom ._agent_metadata import AgentMetadata-from ._agent_runtime import AgentRuntime+from ._agent_runtime import AgentRuntime, agent_instantiation_contextfrom ._cancellation_token import CancellationToken@@ -22,38 +22,28 @@ class BaseAgent(ABC, Agent):)def __init__(self, description: str, subscriptions: Sequence[type]) -> None:- self._runtime: AgentRuntime | None = None- self._id: AgentId | None = None+ try:+ runtime, id = agent_instantiation_context.get()+ except LookupError as e:+ raise RuntimeError(+ "BaseAgent must be instantiated within the context of an AgentRuntime. It cannot be directly instantiated."+ ) from e++ self._runtime: AgentRuntime = runtime+ self._id: AgentId = idself._description = descriptionself._subscriptions = subscriptions- def bind_runtime(self, runtime: AgentRuntime) -> None:- if self._runtime is not None:- raise RuntimeError("Agent has already been bound to a runtime.")-- self._runtime = runtime-- def bind_id(self, agent_id: AgentId) -> None:- if self._id is not None:- raise RuntimeError("Agent has already been bound to an id.")- self._id = agent_id-@propertydef name(self) -> str:return self.id.name@propertydef id(self) -> AgentId:- if self._id is None:- raise RuntimeError("Agent has not been bound to an id.")-return self._id@propertydef runtime(self) -> AgentRuntime:- if self._runtime is None:- raise RuntimeError("Agent has not been bound to a runtime.")-return self._runtime@abstractmethod@@ -67,9 +57,6 @@ class BaseAgent(ABC, Agent):*,cancellation_token: CancellationToken | None = None,) -> Future[Any]:- if self._runtime is None:- raise RuntimeError("Agent has not been bound to a runtime.")-if cancellation_token is None:cancellation_token = CancellationToken()@@ -88,9 +75,6 @@ class BaseAgent(ABC, Agent):*,cancellation_token: CancellationToken | None = None,) -> Future[None]:- if self._runtime is None:- raise RuntimeError("Agent has not been bound to a runtime.")-if cancellation_token is None:cancellation_token = CancellationToken()