π OpenHands: AI-Driven Development
Refactor agent controller's filter (#8792)
Engel Nystdf2374bd8594
diff --git a/openhands/controller/agent_controller.py b/openhands/controller/agent_controller.pyindex 5ec198e797..4abecefcb4 100644--- a/openhands/controller/agent_controller.py+++ b/openhands/controller/agent_controller.py@@ -5,7 +5,7 @@ import copyimport osimport timeimport traceback-from typing import Callable, ClassVar+from typing import Callableimport litellm # noqafrom litellm.exceptions import ( # noqa@@ -61,6 +61,7 @@ from openhands.events.action import ()from openhands.events.action.agent import CondensationAction, RecallActionfrom openhands.events.event import Event+from openhands.events.event_filter import EventFilterfrom openhands.events.observation import (AgentDelegateObservation,AgentStateChangedObservation,@@ -93,12 +94,6 @@ class AgentController:delegate: 'AgentController | None' = None_pending_action_info: tuple[Action, float] | None = None # (action, timestamp)_closed: bool = False- filter_out: ClassVar[tuple[type[Event], ...]] = (- NullAction,- NullObservation,- ChangeAgentStateAction,- AgentStateChangedObservation,- )_cached_first_user_message: MessageAction | None = Nonedef __init__(@@ -150,6 +145,18 @@ class AgentController:EventStreamSubscriber.AGENT_CONTROLLER, self.on_event, self.id)+ # filter out events that are not relevant to the agent+ # so they will not be included in the agent history+ self.agent_history_filter = EventFilter(+ exclude_types=(+ NullAction,+ NullObservation,+ ChangeAgentStateAction,+ AgentStateChangedObservation,+ ),+ exclude_hidden=True,+ )+# state from the previous session, state from a parent agent, or a fresh stateself.set_initial_state(state=initial_state,@@ -219,12 +226,11 @@ class AgentController:else self.event_stream.get_latest_event_id())self.state.history = list(- self.event_stream.get_events(+ self.event_stream.search_events(start_id=start_id,end_id=end_id,reverse=False,- filter_out_type=self.filter_out,- filter_hidden=True,+ filter=self.agent_history_filter,))@@ -406,7 +412,7 @@ class AgentController:return# if the event is not filtered out, add it to the history- if not any(isinstance(event, filter_type) for filter_type in self.filter_out):+ if self.agent_history_filter.include(event):self.state.history.append(event)if isinstance(event, Action):@@ -1091,12 +1097,11 @@ class AgentController:# Get rest of historyevents_to_add = list(- self.event_stream.get_events(+ self.event_stream.search_events(start_id=start_id,end_id=end_id,reverse=False,- filter_out_type=self.filter_out,- filter_hidden=True,+ filter=self.agent_history_filter,))events.extend(events_to_add)