2024-08-22 16:53:35 -04:00
|
|
|
import asyncio
|
2024-06-05 15:48:14 -04:00
|
|
|
import pytest
|
2024-06-22 14:50:32 -04:00
|
|
|
from agnext.application import SingleThreadedAgentRuntime
|
2024-08-23 16:01:57 -04:00
|
|
|
from agnext.components import TypeSubscription, DefaultTopicId, DefaultSubscription
|
2024-08-28 12:14:35 -04:00
|
|
|
from agnext.base import AgentId, AgentInstantiationContext
|
|
|
|
from agnext.base import TopicId
|
|
|
|
from agnext.base import Subscription
|
|
|
|
from agnext.base import SubscriptionInstantiationContext
|
2024-06-27 11:46:06 -07:00
|
|
|
from test_utils import CascadingAgent, CascadingMessageType, LoopbackAgent, MessageType, NoopAgent
|
2024-05-27 16:33:28 -04:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_agent_names_must_be_unique() -> None:
|
2024-06-17 10:44:46 -04:00
|
|
|
runtime = SingleThreadedAgentRuntime()
|
2024-05-27 16:33:28 -04:00
|
|
|
|
2024-08-02 11:02:45 -04:00
|
|
|
def agent_factory() -> NoopAgent:
|
|
|
|
id = AgentInstantiationContext.current_agent_id()
|
2024-06-22 14:50:32 -04:00
|
|
|
assert id == AgentId("name1", "default")
|
|
|
|
agent = NoopAgent()
|
|
|
|
assert agent.id == id
|
|
|
|
return agent
|
|
|
|
|
2024-08-20 14:41:24 -04:00
|
|
|
await runtime.register("name1", agent_factory)
|
2024-05-27 16:33:28 -04:00
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
2024-08-20 14:41:24 -04:00
|
|
|
await runtime.register("name1", NoopAgent)
|
2024-06-18 14:53:18 -04:00
|
|
|
|
2024-08-20 14:41:24 -04:00
|
|
|
await runtime.register("name3", NoopAgent)
|
2024-06-18 14:53:18 -04:00
|
|
|
|
2024-06-27 11:46:06 -07:00
|
|
|
|
2024-06-18 14:53:18 -04:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_register_receives_publish() -> None:
|
|
|
|
runtime = SingleThreadedAgentRuntime()
|
|
|
|
|
2024-07-23 11:49:38 -07:00
|
|
|
await runtime.register("name", LoopbackAgent)
|
2024-08-21 13:59:59 -07:00
|
|
|
runtime.start()
|
2024-08-20 14:41:24 -04:00
|
|
|
await runtime.add_subscription(TypeSubscription("default", "name"))
|
|
|
|
agent_id = AgentId("name", key="default")
|
|
|
|
topic_id = TopicId("default", "default")
|
|
|
|
await runtime.publish_message(MessageType(), topic_id=topic_id)
|
2024-05-27 16:33:28 -04:00
|
|
|
|
2024-08-21 13:59:59 -07:00
|
|
|
await runtime.stop_when_idle()
|
2024-05-27 16:33:28 -04:00
|
|
|
|
2024-06-18 14:53:18 -04:00
|
|
|
# Agent in default namespace should have received the message
|
2024-08-20 14:41:24 -04:00
|
|
|
long_running_agent = await runtime.try_get_underlying_agent_instance(agent_id, type=LoopbackAgent)
|
2024-06-18 14:53:18 -04:00
|
|
|
assert long_running_agent.num_calls == 1
|
|
|
|
|
|
|
|
# Agent in other namespace should not have received the message
|
2024-08-20 14:41:24 -04:00
|
|
|
other_long_running_agent: LoopbackAgent = await runtime.try_get_underlying_agent_instance(AgentId("name", key="other"), type=LoopbackAgent)
|
2024-06-18 14:53:18 -04:00
|
|
|
assert other_long_running_agent.num_calls == 0
|
|
|
|
|
2024-06-27 11:46:06 -07:00
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_register_receives_publish_cascade() -> None:
|
|
|
|
runtime = SingleThreadedAgentRuntime()
|
|
|
|
num_agents = 5
|
|
|
|
num_initial_messages = 5
|
|
|
|
max_rounds = 5
|
|
|
|
total_num_calls_expected = 0
|
|
|
|
for i in range(0, max_rounds):
|
|
|
|
total_num_calls_expected += num_initial_messages * ((num_agents - 1) ** i)
|
|
|
|
|
|
|
|
# Register agents
|
|
|
|
for i in range(num_agents):
|
2024-07-23 11:49:38 -07:00
|
|
|
await runtime.register(f"name{i}", lambda: CascadingAgent(max_rounds))
|
2024-08-20 14:41:24 -04:00
|
|
|
await runtime.add_subscription(TypeSubscription("default", f"name{i}"))
|
2024-07-01 11:53:45 -04:00
|
|
|
|
2024-08-21 13:59:59 -07:00
|
|
|
runtime.start()
|
2024-07-01 11:53:45 -04:00
|
|
|
|
2024-06-27 11:46:06 -07:00
|
|
|
# Publish messages
|
2024-08-20 14:41:24 -04:00
|
|
|
topic_id = TopicId("default", "default")
|
2024-06-27 11:46:06 -07:00
|
|
|
for _ in range(num_initial_messages):
|
2024-08-20 14:41:24 -04:00
|
|
|
await runtime.publish_message(CascadingMessageType(round=1), topic_id)
|
2024-06-27 11:46:06 -07:00
|
|
|
|
|
|
|
# Process until idle.
|
2024-08-21 13:59:59 -07:00
|
|
|
await runtime.stop_when_idle()
|
2024-06-27 11:46:06 -07:00
|
|
|
|
|
|
|
# Check that each agent received the correct number of messages.
|
|
|
|
for i in range(num_agents):
|
2024-08-20 14:41:24 -04:00
|
|
|
agent = await runtime.try_get_underlying_agent_instance(AgentId(f"name{i}", "default"), CascadingAgent)
|
2024-06-27 11:46:06 -07:00
|
|
|
assert agent.num_calls == total_num_calls_expected
|
2024-08-22 16:53:35 -04:00
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_register_factory_explicit_name() -> None:
|
|
|
|
runtime = SingleThreadedAgentRuntime()
|
|
|
|
|
|
|
|
await runtime.register("name", LoopbackAgent, lambda: [TypeSubscription("default", "name")])
|
|
|
|
runtime.start()
|
|
|
|
agent_id = AgentId("name", key="default")
|
|
|
|
topic_id = TopicId("default", "default")
|
|
|
|
await runtime.publish_message(MessageType(), topic_id=topic_id)
|
|
|
|
|
|
|
|
await runtime.stop_when_idle()
|
|
|
|
|
|
|
|
# Agent in default namespace should have received the message
|
|
|
|
long_running_agent = await runtime.try_get_underlying_agent_instance(agent_id, type=LoopbackAgent)
|
|
|
|
assert long_running_agent.num_calls == 1
|
|
|
|
|
|
|
|
# Agent in other namespace should not have received the message
|
|
|
|
other_long_running_agent: LoopbackAgent = await runtime.try_get_underlying_agent_instance(AgentId("name", key="other"), type=LoopbackAgent)
|
|
|
|
assert other_long_running_agent.num_calls == 0
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_register_factory_context_var_name() -> None:
|
|
|
|
runtime = SingleThreadedAgentRuntime()
|
|
|
|
|
|
|
|
await runtime.register("name", LoopbackAgent, lambda: [TypeSubscription("default", SubscriptionInstantiationContext.agent_type().type)])
|
|
|
|
runtime.start()
|
|
|
|
agent_id = AgentId("name", key="default")
|
|
|
|
topic_id = TopicId("default", "default")
|
|
|
|
await runtime.publish_message(MessageType(), topic_id=topic_id)
|
|
|
|
|
|
|
|
await runtime.stop_when_idle()
|
|
|
|
|
|
|
|
# Agent in default namespace should have received the message
|
|
|
|
long_running_agent = await runtime.try_get_underlying_agent_instance(agent_id, type=LoopbackAgent)
|
|
|
|
assert long_running_agent.num_calls == 1
|
|
|
|
|
|
|
|
# Agent in other namespace should not have received the message
|
|
|
|
other_long_running_agent: LoopbackAgent = await runtime.try_get_underlying_agent_instance(AgentId("name", key="other"), type=LoopbackAgent)
|
|
|
|
assert other_long_running_agent.num_calls == 0
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_register_factory_async() -> None:
|
|
|
|
runtime = SingleThreadedAgentRuntime()
|
|
|
|
|
|
|
|
async def sub_factory() -> list[Subscription]:
|
|
|
|
await asyncio.sleep(0.1)
|
|
|
|
return [TypeSubscription("default", SubscriptionInstantiationContext.agent_type().type)]
|
|
|
|
|
|
|
|
await runtime.register("name", LoopbackAgent, sub_factory)
|
|
|
|
runtime.start()
|
|
|
|
agent_id = AgentId("name", key="default")
|
|
|
|
topic_id = TopicId("default", "default")
|
|
|
|
await runtime.publish_message(MessageType(), topic_id=topic_id)
|
|
|
|
|
|
|
|
await runtime.stop_when_idle()
|
|
|
|
|
|
|
|
# Agent in default namespace should have received the message
|
|
|
|
long_running_agent = await runtime.try_get_underlying_agent_instance(agent_id, type=LoopbackAgent)
|
|
|
|
assert long_running_agent.num_calls == 1
|
|
|
|
|
|
|
|
# Agent in other namespace should not have received the message
|
|
|
|
other_long_running_agent: LoopbackAgent = await runtime.try_get_underlying_agent_instance(AgentId("name", key="other"), type=LoopbackAgent)
|
|
|
|
assert other_long_running_agent.num_calls == 0
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_register_factory_direct_list() -> None:
|
|
|
|
runtime = SingleThreadedAgentRuntime()
|
|
|
|
|
|
|
|
await runtime.register("name", LoopbackAgent, [TypeSubscription("default", "name")])
|
|
|
|
runtime.start()
|
|
|
|
agent_id = AgentId("name", key="default")
|
|
|
|
topic_id = TopicId("default", "default")
|
|
|
|
await runtime.publish_message(MessageType(), topic_id=topic_id)
|
|
|
|
|
|
|
|
await runtime.stop_when_idle()
|
|
|
|
|
|
|
|
# Agent in default namespace should have received the message
|
|
|
|
long_running_agent = await runtime.try_get_underlying_agent_instance(agent_id, type=LoopbackAgent)
|
|
|
|
assert long_running_agent.num_calls == 1
|
|
|
|
|
|
|
|
# Agent in other namespace should not have received the message
|
|
|
|
other_long_running_agent: LoopbackAgent = await runtime.try_get_underlying_agent_instance(AgentId("name", key="other"), type=LoopbackAgent)
|
|
|
|
assert other_long_running_agent.num_calls == 0
|
2024-08-23 16:01:57 -04:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_default_subscription() -> None:
|
|
|
|
runtime = SingleThreadedAgentRuntime()
|
|
|
|
|
|
|
|
await runtime.register("name", LoopbackAgent, lambda: [DefaultSubscription()])
|
|
|
|
runtime.start()
|
|
|
|
agent_id = AgentId("name", key="default")
|
|
|
|
await runtime.publish_message(MessageType(), topic_id=DefaultTopicId())
|
|
|
|
|
|
|
|
await runtime.stop_when_idle()
|
|
|
|
|
|
|
|
# Agent in default namespace should have received the message
|
|
|
|
long_running_agent = await runtime.try_get_underlying_agent_instance(agent_id, type=LoopbackAgent)
|
|
|
|
assert long_running_agent.num_calls == 1
|
|
|
|
|
|
|
|
# Agent in other namespace should not have received the message
|
|
|
|
other_long_running_agent: LoopbackAgent = await runtime.try_get_underlying_agent_instance(AgentId("name", key="other"), type=LoopbackAgent)
|
|
|
|
assert other_long_running_agent.num_calls == 0
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_non_default_default_subscription() -> None:
|
|
|
|
runtime = SingleThreadedAgentRuntime()
|
|
|
|
|
|
|
|
await runtime.register("name", LoopbackAgent, lambda: [DefaultSubscription(topic_type="Other")])
|
|
|
|
runtime.start()
|
|
|
|
agent_id = AgentId("name", key="default")
|
|
|
|
await runtime.publish_message(MessageType(), topic_id=DefaultTopicId(type="Other"))
|
|
|
|
|
|
|
|
await runtime.stop_when_idle()
|
|
|
|
|
|
|
|
# Agent in default namespace should have received the message
|
|
|
|
long_running_agent = await runtime.try_get_underlying_agent_instance(agent_id, type=LoopbackAgent)
|
|
|
|
assert long_running_agent.num_calls == 1
|
|
|
|
|
|
|
|
# Agent in other namespace should not have received the message
|
|
|
|
other_long_running_agent: LoopbackAgent = await runtime.try_get_underlying_agent_instance(AgentId("name", key="other"), type=LoopbackAgent)
|
|
|
|
assert other_long_running_agent.num_calls == 0
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_non_publish_to_other_source() -> None:
|
|
|
|
runtime = SingleThreadedAgentRuntime()
|
|
|
|
|
|
|
|
await runtime.register("name", LoopbackAgent, lambda: [DefaultSubscription()])
|
|
|
|
runtime.start()
|
|
|
|
agent_id = AgentId("name", key="default")
|
|
|
|
await runtime.publish_message(MessageType(), topic_id=DefaultTopicId(source="other"))
|
|
|
|
|
|
|
|
await runtime.stop_when_idle()
|
|
|
|
|
|
|
|
# Agent in default namespace should have received the message
|
|
|
|
long_running_agent = await runtime.try_get_underlying_agent_instance(agent_id, type=LoopbackAgent)
|
|
|
|
assert long_running_agent.num_calls == 0
|
|
|
|
|
|
|
|
# Agent in other namespace should not have received the message
|
|
|
|
other_long_running_agent: LoopbackAgent = await runtime.try_get_underlying_agent_instance(AgentId("name", key="other"), type=LoopbackAgent)
|
|
|
|
assert other_long_running_agent.num_calls == 1
|