2025-01-07 12:51:35 -05:00
|
|
|
from typing import Any
|
|
|
|
|
2024-06-05 15:48:14 -04:00
|
|
|
import pytest
|
2025-01-07 12:51:35 -05:00
|
|
|
from autogen_core import (
|
|
|
|
AgentId,
|
|
|
|
DefaultInterventionHandler,
|
|
|
|
DefaultSubscription,
|
|
|
|
DefaultTopicId,
|
|
|
|
DropMessage,
|
|
|
|
MessageContext,
|
|
|
|
SingleThreadedAgentRuntime,
|
|
|
|
)
|
2024-12-03 17:00:44 -08:00
|
|
|
from autogen_core.exceptions import MessageDroppedException
|
2024-12-04 16:23:20 -08:00
|
|
|
from autogen_test_utils import LoopbackAgent, MessageType
|
2024-05-20 17:30:45 -06:00
|
|
|
|
2024-06-05 15:48:14 -04:00
|
|
|
|
2024-05-20 17:30:45 -06:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_intervention_count_messages() -> None:
|
2024-06-18 14:53:18 -04:00
|
|
|
class DebugInterventionHandler(DefaultInterventionHandler):
|
2024-05-26 08:45:02 -04:00
|
|
|
def __init__(self) -> None:
|
2025-01-07 12:51:35 -05:00
|
|
|
self.num_send_messages = 0
|
|
|
|
self.num_publish_messages = 0
|
|
|
|
self.num_response_messages = 0
|
|
|
|
|
|
|
|
async def on_send(self, message: Any, *, message_context: MessageContext, recipient: AgentId) -> Any:
|
|
|
|
self.num_send_messages += 1
|
|
|
|
return message
|
|
|
|
|
|
|
|
async def on_publish(self, message: Any, *, message_context: MessageContext) -> Any:
|
|
|
|
self.num_publish_messages += 1
|
|
|
|
return message
|
2024-05-20 17:30:45 -06:00
|
|
|
|
2025-01-07 12:51:35 -05:00
|
|
|
async def on_response(self, message: Any, *, sender: AgentId, recipient: AgentId | None) -> Any:
|
|
|
|
self.num_response_messages += 1
|
2024-05-20 17:30:45 -06:00
|
|
|
return message
|
|
|
|
|
|
|
|
handler = DebugInterventionHandler()
|
2024-08-30 12:24:25 -07:00
|
|
|
runtime = SingleThreadedAgentRuntime(intervention_handlers=[handler])
|
2024-11-26 19:31:23 -05:00
|
|
|
await LoopbackAgent.register(runtime, "name", LoopbackAgent)
|
2024-08-20 14:41:24 -04:00
|
|
|
loopback = AgentId("name", key="default")
|
2024-08-21 13:59:59 -07:00
|
|
|
runtime.start()
|
2024-05-20 17:30:45 -06:00
|
|
|
|
2024-07-01 11:53:45 -04:00
|
|
|
_response = await runtime.send_message(MessageType(), recipient=loopback)
|
2024-05-20 17:30:45 -06:00
|
|
|
|
2025-01-07 12:51:35 -05:00
|
|
|
await runtime.stop_when_idle()
|
2024-05-20 17:30:45 -06:00
|
|
|
|
2025-01-07 12:51:35 -05:00
|
|
|
assert handler.num_send_messages == 1
|
|
|
|
assert handler.num_response_messages == 1
|
2024-07-23 16:38:37 -07:00
|
|
|
loopback_agent = await runtime.try_get_underlying_agent_instance(loopback, type=LoopbackAgent)
|
2024-06-18 14:53:18 -04:00
|
|
|
assert loopback_agent.num_calls == 1
|
2024-05-20 17:30:45 -06:00
|
|
|
|
2025-01-07 12:51:35 -05:00
|
|
|
runtime.start()
|
|
|
|
await runtime.add_subscription(DefaultSubscription(agent_type="name"))
|
|
|
|
|
|
|
|
await runtime.publish_message(MessageType(), topic_id=DefaultTopicId())
|
|
|
|
|
|
|
|
await runtime.stop_when_idle()
|
|
|
|
assert loopback_agent.num_calls == 2
|
|
|
|
assert handler.num_publish_messages == 1
|
|
|
|
|
2024-09-13 10:41:15 -04:00
|
|
|
|
2024-05-20 17:30:45 -06:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_intervention_drop_send() -> None:
|
2024-06-18 14:53:18 -04:00
|
|
|
class DropSendInterventionHandler(DefaultInterventionHandler):
|
2024-09-13 10:41:15 -04:00
|
|
|
async def on_send(
|
2025-01-07 12:51:35 -05:00
|
|
|
self, message: MessageType, *, message_context: MessageContext, recipient: AgentId
|
2024-09-13 10:41:15 -04:00
|
|
|
) -> MessageType | type[DropMessage]:
|
2024-06-18 14:53:18 -04:00
|
|
|
return DropMessage
|
2024-05-20 17:30:45 -06:00
|
|
|
|
|
|
|
handler = DropSendInterventionHandler()
|
2024-08-30 12:24:25 -07:00
|
|
|
runtime = SingleThreadedAgentRuntime(intervention_handlers=[handler])
|
2024-05-20 17:30:45 -06:00
|
|
|
|
2024-11-26 19:31:23 -05:00
|
|
|
await LoopbackAgent.register(runtime, "name", LoopbackAgent)
|
2024-08-20 14:41:24 -04:00
|
|
|
loopback = AgentId("name", key="default")
|
2024-08-21 13:59:59 -07:00
|
|
|
runtime.start()
|
2024-05-20 17:30:45 -06:00
|
|
|
|
|
|
|
with pytest.raises(MessageDroppedException):
|
2024-07-01 11:53:45 -04:00
|
|
|
_response = await runtime.send_message(MessageType(), recipient=loopback)
|
|
|
|
|
2024-08-21 13:59:59 -07:00
|
|
|
await runtime.stop()
|
2024-05-20 17:30:45 -06:00
|
|
|
|
2024-07-23 16:38:37 -07:00
|
|
|
loopback_agent = await runtime.try_get_underlying_agent_instance(loopback, type=LoopbackAgent)
|
2024-06-18 14:53:18 -04:00
|
|
|
assert loopback_agent.num_calls == 0
|
2024-05-20 17:30:45 -06:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_intervention_drop_response() -> None:
|
2024-06-18 14:53:18 -04:00
|
|
|
class DropResponseInterventionHandler(DefaultInterventionHandler):
|
2024-09-13 10:41:15 -04:00
|
|
|
async def on_response(
|
|
|
|
self, message: MessageType, *, sender: AgentId, recipient: AgentId | None
|
|
|
|
) -> MessageType | type[DropMessage]:
|
2024-06-18 14:53:18 -04:00
|
|
|
return DropMessage
|
2024-05-20 17:30:45 -06:00
|
|
|
|
|
|
|
handler = DropResponseInterventionHandler()
|
2024-08-30 12:24:25 -07:00
|
|
|
runtime = SingleThreadedAgentRuntime(intervention_handlers=[handler])
|
2024-05-20 17:30:45 -06:00
|
|
|
|
2024-11-26 19:31:23 -05:00
|
|
|
await LoopbackAgent.register(runtime, "name", LoopbackAgent)
|
2024-08-20 14:41:24 -04:00
|
|
|
loopback = AgentId("name", key="default")
|
2024-08-21 13:59:59 -07:00
|
|
|
runtime.start()
|
2024-05-20 17:30:45 -06:00
|
|
|
|
|
|
|
with pytest.raises(MessageDroppedException):
|
2024-07-01 11:53:45 -04:00
|
|
|
_response = await runtime.send_message(MessageType(), recipient=loopback)
|
|
|
|
|
2024-08-21 13:59:59 -07:00
|
|
|
await runtime.stop()
|
2024-05-20 17:30:45 -06:00
|
|
|
|
2024-06-17 17:34:56 -07:00
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_intervention_raise_exception_on_send() -> None:
|
|
|
|
class InterventionException(Exception):
|
|
|
|
pass
|
|
|
|
|
2024-09-13 10:41:15 -04:00
|
|
|
class ExceptionInterventionHandler(DefaultInterventionHandler): # type: ignore
|
|
|
|
async def on_send(
|
2025-01-07 12:51:35 -05:00
|
|
|
self, message: MessageType, *, message_context: MessageContext, recipient: AgentId
|
2024-09-13 10:41:15 -04:00
|
|
|
) -> MessageType | type[DropMessage]: # type: ignore
|
2024-06-17 17:34:56 -07:00
|
|
|
raise InterventionException
|
|
|
|
|
|
|
|
handler = ExceptionInterventionHandler()
|
2024-08-30 12:24:25 -07:00
|
|
|
runtime = SingleThreadedAgentRuntime(intervention_handlers=[handler])
|
2024-06-17 17:34:56 -07:00
|
|
|
|
2024-11-26 19:31:23 -05:00
|
|
|
await LoopbackAgent.register(runtime, "name", LoopbackAgent)
|
2024-08-20 14:41:24 -04:00
|
|
|
loopback = AgentId("name", key="default")
|
2024-08-21 13:59:59 -07:00
|
|
|
runtime.start()
|
2024-06-17 17:34:56 -07:00
|
|
|
|
|
|
|
with pytest.raises(InterventionException):
|
2024-08-20 14:41:24 -04:00
|
|
|
_response = await runtime.send_message(MessageType(), recipient=loopback)
|
2024-07-01 11:53:45 -04:00
|
|
|
|
2024-08-21 13:59:59 -07:00
|
|
|
await runtime.stop()
|
2024-06-17 17:34:56 -07:00
|
|
|
|
2024-08-20 14:41:24 -04:00
|
|
|
long_running_agent = await runtime.try_get_underlying_agent_instance(loopback, type=LoopbackAgent)
|
2024-06-18 14:53:18 -04:00
|
|
|
assert long_running_agent.num_calls == 0
|
2024-06-17 17:34:56 -07:00
|
|
|
|
2024-09-13 10:41:15 -04:00
|
|
|
|
2024-06-17 17:34:56 -07:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_intervention_raise_exception_on_respond() -> None:
|
|
|
|
class InterventionException(Exception):
|
|
|
|
pass
|
|
|
|
|
2024-09-13 10:41:15 -04:00
|
|
|
class ExceptionInterventionHandler(DefaultInterventionHandler): # type: ignore
|
|
|
|
async def on_response(
|
|
|
|
self, message: MessageType, *, sender: AgentId, recipient: AgentId | None
|
|
|
|
) -> MessageType | type[DropMessage]: # type: ignore
|
2024-06-17 17:34:56 -07:00
|
|
|
raise InterventionException
|
|
|
|
|
|
|
|
handler = ExceptionInterventionHandler()
|
2024-08-30 12:24:25 -07:00
|
|
|
runtime = SingleThreadedAgentRuntime(intervention_handlers=[handler])
|
2024-06-17 17:34:56 -07:00
|
|
|
|
2024-11-26 19:31:23 -05:00
|
|
|
await LoopbackAgent.register(runtime, "name", LoopbackAgent)
|
2024-08-20 14:41:24 -04:00
|
|
|
loopback = AgentId("name", key="default")
|
2024-08-21 13:59:59 -07:00
|
|
|
runtime.start()
|
2024-06-17 17:34:56 -07:00
|
|
|
with pytest.raises(InterventionException):
|
2024-08-20 14:41:24 -04:00
|
|
|
_response = await runtime.send_message(MessageType(), recipient=loopback)
|
2024-07-01 11:53:45 -04:00
|
|
|
|
2024-08-21 13:59:59 -07:00
|
|
|
await runtime.stop()
|
2024-06-17 17:34:56 -07:00
|
|
|
|
2024-08-20 14:41:24 -04:00
|
|
|
long_running_agent = await runtime.try_get_underlying_agent_instance(loopback, type=LoopbackAgent)
|
2024-06-18 14:53:18 -04:00
|
|
|
assert long_running_agent.num_calls == 1
|