-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add system event listers for monitoring
- Loading branch information
Showing
4 changed files
with
91 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from abc import ABC, abstractmethod | ||
from enum import StrEnum | ||
from typing import Any | ||
|
||
|
||
class SystemEventType(StrEnum): | ||
CONVERSATION_START = 'conversation_start' | ||
AGENT_STATUS_ERROR = 'agent_status_error' | ||
|
||
|
||
class SystemEventListener(ABC): | ||
@abstractmethod | ||
def on_event(self, type: SystemEventType, data: dict[str, Any]): | ||
pass | ||
|
||
|
||
class SystemEventHandler: | ||
_listeners: list[SystemEventListener] | ||
|
||
def __init__(self): | ||
self._listeners = [] | ||
|
||
def add_listener(self, listener: SystemEventListener): | ||
"""Forward future on_event calls to listener.""" | ||
self._listeners.append(listener) | ||
|
||
def on_event(self, type: SystemEventType, session_id: str, **kwargs): | ||
"""Forwards on_event calls to all listeners, swallowing exceptions.""" | ||
for listener in self._listeners: | ||
try: | ||
event_data = {'session_id': session_id, **kwargs} | ||
listener.on_event(type, event_data) | ||
except Exception as _: | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from unittest.mock import Mock | ||
|
||
from openhands.server.system_event import ( | ||
SystemEventHandler, | ||
SystemEventListener, | ||
SystemEventType, | ||
) | ||
|
||
|
||
def test_event_forwarding(): | ||
"""Test that events are forwarded to all listeners.""" | ||
handler = SystemEventHandler() | ||
mock_listener1 = Mock(spec=SystemEventListener) | ||
mock_listener2 = Mock(spec=SystemEventListener) | ||
|
||
handler.add_listener(mock_listener1) | ||
handler.add_listener(mock_listener2) | ||
|
||
session_id = 'test_session' | ||
event_type = SystemEventType.CONVERSATION_START | ||
|
||
handler.on_event(event_type, session_id) | ||
|
||
expected_data = {'session_id': session_id} | ||
mock_listener1.on_event.assert_called_once_with(event_type, expected_data) | ||
mock_listener2.on_event.assert_called_once_with(event_type, expected_data) | ||
|
||
|
||
def test_exception_handling(): | ||
"""Test that exceptions from listeners are caught and don't affect other listeners.""" | ||
handler = SystemEventHandler() | ||
mock_listener1 = Mock(spec=SystemEventListener) | ||
mock_listener1.on_event.side_effect = Exception('Test error') | ||
|
||
mock_listener2 = Mock(spec=SystemEventListener) | ||
|
||
handler.add_listener(mock_listener1) | ||
handler.add_listener(mock_listener2) | ||
|
||
session_id = 'test_session' | ||
event_type = SystemEventType.AGENT_STATUS_ERROR | ||
|
||
# Should not raise an exception | ||
handler.on_event(event_type, session_id) | ||
|
||
# Second listener should still be called | ||
expected_data = {'session_id': session_id} | ||
mock_listener2.on_event.assert_called_once_with(event_type, expected_data) |