Initial impl of topics and subscriptions (#350)

* initial impl of topics and subscriptions

* Update python/src/agnext/core/_agent_runtime.py

Co-authored-by: Eric Zhu <ekzhu@users.noreply.github.com>

* add topic in context

* migrate

* migrate code for topics

* migrate team one

* edit notebooks

* formatting

* fix imports

* Build proto

* Fix circular import

---------

Co-authored-by: Eric Zhu <ekzhu@users.noreply.github.com>
This commit is contained in:
Jack Gerrits
2024-08-20 14:41:24 -04:00
committed by GitHub
parent 4ba7e84721
commit e1a823fb6d
71 changed files with 685 additions and 495 deletions

View File

@@ -1,19 +1,16 @@
from typing import Any, Mapping, Sequence
from typing import Any, Mapping
import pytest
from agnext.application import SingleThreadedAgentRuntime
from agnext.core import BaseAgent, MessageContext
from agnext.core import AgentId
class StatefulAgent(BaseAgent):
def __init__(self) -> None:
super().__init__("A stateful agent", [])
super().__init__("A stateful agent")
self.state = 0
@property
def subscriptions(self) -> Sequence[type]:
return []
async def on_message(self, message: Any, ctx: MessageContext) -> None:
raise NotImplementedError
@@ -28,7 +25,8 @@ class StatefulAgent(BaseAgent):
async def test_agent_can_save_state() -> None:
runtime = SingleThreadedAgentRuntime()
agent1_id = await runtime.register_and_get("name1", StatefulAgent)
await runtime.register("name1", StatefulAgent)
agent1_id = AgentId("name1", key="default")
agent1: StatefulAgent = await runtime.try_get_underlying_agent_instance(agent1_id, type=StatefulAgent)
assert agent1.state == 0
agent1.state = 1
@@ -46,7 +44,8 @@ async def test_agent_can_save_state() -> None:
async def test_runtime_can_save_state() -> None:
runtime = SingleThreadedAgentRuntime()
agent1_id = await runtime.register_and_get("name1", StatefulAgent)
await runtime.register("name1", StatefulAgent)
agent1_id = AgentId("name1", key="default")
agent1: StatefulAgent = await runtime.try_get_underlying_agent_instance(agent1_id, type=StatefulAgent)
assert agent1.state == 0
agent1.state = 1
@@ -55,7 +54,8 @@ async def test_runtime_can_save_state() -> None:
runtime_state = await runtime.save_state()
runtime2 = SingleThreadedAgentRuntime()
agent2_id = await runtime2.register_and_get("name1", StatefulAgent)
await runtime2.register("name1", StatefulAgent)
agent2_id = AgentId("name1", key="default")
agent2: StatefulAgent = await runtime2.try_get_underlying_agent_instance(agent2_id, type=StatefulAgent)
await runtime2.load_state(runtime_state)