Skip to content

Commit c29218b

Browse files
authored
Add agent proxy (#84)
1 parent 89f1133 commit c29218b

File tree

5 files changed

+88
-1
lines changed

5 files changed

+88
-1
lines changed

src/agnext/application/_single_threaded_agent_runtime.py

+6
Original file line numberDiff line numberDiff line change
@@ -302,3 +302,9 @@ async def process_next(self) -> None:
302302

303303
def agent_metadata(self, agent: Agent) -> AgentMetadata:
304304
return agent.metadata
305+
306+
def agent_save_state(self, agent: Agent) -> Mapping[str, Any]:
307+
return agent.save_state()
308+
309+
def agent_load_state(self, agent: Agent, state: Mapping[str, Any]) -> None:
310+
agent.load_state(state)

src/agnext/core/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
"""
44

55
from ._agent import Agent
6+
from ._agent_id import AgentId
67
from ._agent_metadata import AgentMetadata
78
from ._agent_props import AgentChildren
89
from ._agent_runtime import AgentRuntime
910
from ._base_agent import BaseAgent
1011
from ._cancellation_token import CancellationToken
1112

12-
__all__ = ["Agent", "AgentMetadata", "AgentRuntime", "BaseAgent", "CancellationToken", "AgentChildren"]
13+
__all__ = ["Agent", "AgentId", "AgentMetadata", "AgentRuntime", "BaseAgent", "CancellationToken", "AgentChildren"]

src/agnext/core/_agent_id.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from typing_extensions import Self
2+
3+
4+
class AgentId:
5+
def __init__(self, name: str, namespace: str) -> None:
6+
self._name = name
7+
self._namespace = namespace
8+
9+
def __str__(self) -> str:
10+
return f"{self._namespace}/{self._name}"
11+
12+
def __hash__(self) -> int:
13+
return hash((self._namespace, self._name))
14+
15+
@classmethod
16+
def from_str(cls, agent_id: str) -> Self:
17+
namespace, name = agent_id.split("/")
18+
return cls(name, namespace)
19+
20+
@property
21+
def namespace(self) -> str:
22+
return self._namespace
23+
24+
@property
25+
def name(self) -> str:
26+
return self._name

src/agnext/core/_agent_proxy.py

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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)

src/agnext/core/_agent_runtime.py

+4
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,7 @@ def save_state(self) -> Mapping[str, Any]: ...
4444
def load_state(self, state: Mapping[str, Any]) -> None: ...
4545

4646
def agent_metadata(self, agent: Agent) -> AgentMetadata: ...
47+
48+
def agent_save_state(self, agent: Agent) -> Mapping[str, Any]: ...
49+
50+
def agent_load_state(self, agent: Agent, state: Mapping[str, Any]) -> None: ...

0 commit comments

Comments
 (0)