Skip to content

A programming framework for agentic AI

Allow multiple intervention handlers to be supplied to the runtime (#435)

Aamir042958e3abba
diff --git a/python/packages/autogen-core/docs/src/cookbook/termination-with-intervention.md b/python/packages/autogen-core/docs/src/cookbook/termination-with-intervention.md
index 7ad86447e..f38bf208b 100644
--- a/python/packages/autogen-core/docs/src/cookbook/termination-with-intervention.md
+++ b/python/packages/autogen-core/docs/src/cookbook/termination-with-intervention.md
@@ -68,7 +68,7 @@ Finally, we add this handler to the runtime and use it to detect termination and
async def main() -> None:
termination_handler = TerminationHandler()
runtime = SingleThreadedAgentRuntime(
- intervention_handler=termination_handler
+ intervention_handlers=[termination_handler]
)
# Add Agents and kick off task
diff --git a/python/packages/autogen-core/samples/tool-use/coding_direct_with_intercept.py b/python/packages/autogen-core/samples/tool-use/coding_direct_with_intercept.py
index 617908b50..25af7a3b5 100644
--- a/python/packages/autogen-core/samples/tool-use/coding_direct_with_intercept.py
+++ b/python/packages/autogen-core/samples/tool-use/coding_direct_with_intercept.py
@@ -39,7 +39,7 @@ class ToolInterventionHandler(DefaultInterventionHandler):
async def main() -> None:
# Create the runtime with the intervention handler.
- runtime = SingleThreadedAgentRuntime(intervention_handler=ToolInterventionHandler())
+ runtime = SingleThreadedAgentRuntime(intervention_handlers=[ToolInterventionHandler()])
# Define the tools.
tools: List[Tool] = [
# A tool that executes Python code.
diff --git a/python/packages/autogen-core/src/autogen_core/application/_single_threaded_agent_runtime.py b/python/packages/autogen-core/src/autogen_core/application/_single_threaded_agent_runtime.py
index f951e5555..81f9b2f49 100644
--- a/python/packages/autogen-core/src/autogen_core/application/_single_threaded_agent_runtime.py
+++ b/python/packages/autogen-core/src/autogen_core/application/_single_threaded_agent_runtime.py
@@ -124,14 +124,14 @@ class RunContext:
class SingleThreadedAgentRuntime(AgentRuntime):
- def __init__(self, *, intervention_handler: InterventionHandler | None = None) -> None:
+ def __init__(self, *, intervention_handlers: List[InterventionHandler] | None = None) -> None:
self._message_queue: List[PublishMessageEnvelope | SendMessageEnvelope | ResponseMessageEnvelope] = []
# (namespace, type) -> List[AgentId]
self._agent_factories: Dict[
str, Callable[[], Agent | Awaitable[Agent]] | Callable[[AgentRuntime, AgentId], Agent | Awaitable[Agent]]
] = {}
self._instantiated_agents: Dict[AgentId, Agent] = {}
- self._intervention_handler = intervention_handler
+ self._intervention_handlers = intervention_handlers
self._outstanding_tasks = Counter()
self._background_tasks: Set[Task[Any]] = set()
self._subscription_manager = SubscriptionManager()
@@ -365,19 +365,18 @@ class SingleThreadedAgentRuntime(AgentRuntime):
match message_envelope:
case SendMessageEnvelope(message=message, sender=sender, recipient=recipient, future=future):
- if self._intervention_handler is not None:
- try:
- temp_message = await self._intervention_handler.on_send(
- message, sender=sender, recipient=recipient
- )
- except BaseException as e:
- future.set_exception(e)
- return
- if temp_message is DropMessage or isinstance(temp_message, DropMessage):
- future.set_exception(MessageDroppedException())
- return
-
- message_envelope.message = temp_message
+ if self._intervention_handlers is not None:
+ for handler in self._intervention_handlers:
+ try:
+ temp_message = await handler.on_send(message, sender=sender, recipient=recipient)
+ except BaseException as e:
+ future.set_exception(e)
+ return
+ if temp_message is DropMessage or isinstance(temp_message, DropMessage):
+ future.set_exception(MessageDroppedException())
+ return
+
+ message_envelope.message = temp_message
self._outstanding_tasks.increment()
task = asyncio.create_task(self._process_send(message_envelope))
self._background_tasks.add(task)
@@ -386,37 +385,37 @@ class SingleThreadedAgentRuntime(AgentRuntime):
message=message,
sender=sender,
):
- if self._intervention_handler is not None:
- try:
- temp_message = await self._intervention_handler.on_publish(message, sender=sender)
- except BaseException as e:
- # TODO: we should raise the intervention exception to the publisher.
- logger.error(f"Exception raised in in intervention handler: {e}", exc_info=True)
- return
- if temp_message is DropMessage or isinstance(temp_message, DropMessage):
- # TODO log message dropped
- return
-
- message_envelope.message = temp_message
+ if self._intervention_handlers is not None:
+ for handler in self._intervention_handlers:
+ try:
+ temp_message = await handler.on_publish(message, sender=sender)
+ except BaseException as e:
+ # TODO: we should raise the intervention exception to the publisher.
+ logger.error(f"Exception raised in in intervention handler: {e}", exc_info=True)
+ return
+ if temp_message is DropMessage or isinstance(temp_message, DropMessage):
+ # TODO log message dropped
+ return
+
+ message_envelope.message = temp_message
self._outstanding_tasks.increment()
task = asyncio.create_task(self._process_publish(message_envelope))
self._background_tasks.add(task)
task.add_done_callback(self._background_tasks.discard)
case ResponseMessageEnvelope(message=message, sender=sender, recipient=recipient, future=future):
- if self._intervention_handler is not None:
- try:
- temp_message = await self._intervention_handler.on_response(
- message, sender=sender, recipient=recipient
- )
- except BaseException as e:
- # TODO: should we raise the exception to sender of the response instead?
- future.set_exception(e)
- return
- if temp_message is DropMessage or isinstance(temp_message, DropMessage):
- future.set_exception(MessageDroppedException())
- return
-
- message_envelope.message = temp_message
+ if self._intervention_handlers is not None:
+ for handler in self._intervention_handlers:
+ try:
+ temp_message = await handler.on_response(message, sender=sender, recipient=recipient)
+ except BaseException as e:
+ # TODO: should we raise the exception to sender of the response instead?
+ future.set_exception(e)
+ return
+ if temp_message is DropMessage or isinstance(temp_message, DropMessage):
+ future.set_exception(MessageDroppedException())
+ return
+
+ message_envelope.message = temp_message
self._outstanding_tasks.increment()
task = asyncio.create_task(self._process_response(message_envelope))
self._background_tasks.add(task)
diff --git a/python/packages/autogen-core/tests/test_intervention.py b/python/packages/autogen-core/tests/test_intervention.py
index fbec2a043..adbc76d7a 100644
--- a/python/packages/autogen-core/tests/test_intervention.py
+++ b/python/packages/autogen-core/tests/test_intervention.py
@@ -18,7 +18,7 @@ async def test_intervention_count_messages() -> None:
return message
handler = DebugInterventionHandler()
- runtime = SingleThreadedAgentRuntime(intervention_handler=handler)
+ runtime = SingleThreadedAgentRuntime(intervention_handlers=[handler])
await runtime.register("name", LoopbackAgent)
loopback = AgentId("name", key="default")
runtime.start()
@@ -39,7 +39,7 @@ async def test_intervention_drop_send() -> None:
return DropMessage
handler = DropSendInterventionHandler()
- runtime = SingleThreadedAgentRuntime(intervention_handler=handler)
+ runtime = SingleThreadedAgentRuntime(intervention_handlers=[handler])
await runtime.register("name", LoopbackAgent)
loopback = AgentId("name", key="default")
@@ -62,7 +62,7 @@ async def test_intervention_drop_response() -> None:
return DropMessage
handler = DropResponseInterventionHandler()
- runtime = SingleThreadedAgentRuntime(intervention_handler=handler)
+ runtime = SingleThreadedAgentRuntime(intervention_handlers=[handler])
await runtime.register("name", LoopbackAgent)
loopback = AgentId("name", key="default")
@@ -85,7 +85,7 @@ async def test_intervention_raise_exception_on_send() -> None:
raise InterventionException
handler = ExceptionInterventionHandler()
- runtime = SingleThreadedAgentRuntime(intervention_handler=handler)
+ runtime = SingleThreadedAgentRuntime(intervention_handlers=[handler])
await runtime.register("name", LoopbackAgent)
loopback = AgentId("name", key="default")
@@ -110,7 +110,7 @@ async def test_intervention_raise_exception_on_respond() -> None:
raise InterventionException
handler = ExceptionInterventionHandler()
- runtime = SingleThreadedAgentRuntime(intervention_handler=handler)
+ runtime = SingleThreadedAgentRuntime(intervention_handlers=[handler])
await runtime.register("name", LoopbackAgent)
loopback = AgentId("name", key="default")