mirror of
https://github.com/microsoft/autogen.git
synced 2026-02-15 04:05:00 -05:00
remove binding from base agent (#100)
This commit is contained in:
@@ -7,7 +7,7 @@ from ._agent_id import AgentId
|
||||
from ._agent_metadata import AgentMetadata
|
||||
from ._agent_props import AgentChildren
|
||||
from ._agent_proxy import AgentProxy
|
||||
from ._agent_runtime import AgentRuntime, AllNamespaces
|
||||
from ._agent_runtime import AgentRuntime, AllNamespaces, agent_instantiation_context
|
||||
from ._base_agent import BaseAgent
|
||||
from ._cancellation_token import CancellationToken
|
||||
|
||||
@@ -21,4 +21,5 @@ __all__ = [
|
||||
"BaseAgent",
|
||||
"CancellationToken",
|
||||
"AgentChildren",
|
||||
"agent_instantiation_context",
|
||||
]
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from asyncio import Future
|
||||
from contextvars import ContextVar
|
||||
from typing import Any, Callable, Mapping, Protocol, Sequence, Type, TypeVar, overload, runtime_checkable
|
||||
|
||||
from ._agent import Agent
|
||||
@@ -13,6 +14,8 @@ from ._cancellation_token import CancellationToken
|
||||
|
||||
T = TypeVar("T", bound=Agent)
|
||||
|
||||
agent_instantiation_context: ContextVar[tuple[AgentRuntime, AgentId]] = ContextVar("agent_instantiation_context")
|
||||
|
||||
|
||||
class AllNamespaces:
|
||||
pass
|
||||
|
||||
@@ -6,7 +6,7 @@ from typing import Any, Mapping, Sequence
|
||||
from ._agent import Agent
|
||||
from ._agent_id import AgentId
|
||||
from ._agent_metadata import AgentMetadata
|
||||
from ._agent_runtime import AgentRuntime
|
||||
from ._agent_runtime import AgentRuntime, agent_instantiation_context
|
||||
from ._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 = id
|
||||
self._description = description
|
||||
self._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
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
return self.id.name
|
||||
|
||||
@property
|
||||
def id(self) -> AgentId:
|
||||
if self._id is None:
|
||||
raise RuntimeError("Agent has not been bound to an id.")
|
||||
|
||||
return self._id
|
||||
|
||||
@property
|
||||
def 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()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user