|
| 1 | +from asyncio import Future |
| 2 | +from typing import Any, Mapping |
| 3 | + |
| 4 | +from ._agent import Agent |
| 5 | +from ._agent_id import AgentId |
| 6 | +from ._agent_metadata import AgentMetadata |
| 7 | +from ._agent_runtime import AgentRuntime |
| 8 | +from ._cancellation_token import CancellationToken |
| 9 | + |
| 10 | + |
| 11 | +class AgentProxy: |
| 12 | + def __init__(self, agent: Agent, runtime: AgentRuntime): |
| 13 | + self._agent = agent |
| 14 | + self._runtime = runtime |
| 15 | + |
| 16 | + @property |
| 17 | + def id(self) -> AgentId: |
| 18 | + """Target agent for this proxy""" |
| 19 | + raise NotImplementedError |
| 20 | + |
| 21 | + @property |
| 22 | + def metadata(self) -> AgentMetadata: |
| 23 | + """Metadata of the agent.""" |
| 24 | + return self._runtime.agent_metadata(self._agent) |
| 25 | + |
| 26 | + def send_message( |
| 27 | + self, |
| 28 | + message: Any, |
| 29 | + *, |
| 30 | + sender: Agent, |
| 31 | + cancellation_token: CancellationToken | None = None, |
| 32 | + ) -> Future[Any]: |
| 33 | + return self._runtime.send_message( |
| 34 | + message, |
| 35 | + recipient=self._agent, |
| 36 | + sender=sender, |
| 37 | + cancellation_token=cancellation_token, |
| 38 | + ) |
| 39 | + |
| 40 | + def save_state(self) -> Mapping[str, Any]: |
| 41 | + """Save the state of the agent. The result must be JSON serializable.""" |
| 42 | + return self._runtime.agent_save_state(self._agent) |
| 43 | + |
| 44 | + def load_state(self, state: Mapping[str, Any]) -> None: |
| 45 | + """Load in the state of the agent obtained from `save_state`. |
| 46 | +
|
| 47 | + Args: |
| 48 | + state (Mapping[str, Any]): State of the agent. Must be JSON serializable. |
| 49 | + """ |
| 50 | + self._runtime.agent_load_state(self._agent, state) |
0 commit comments