🙌 OpenHands: AI-Driven Development
Streamline Logging Events (#2532)
மனோஜ்குமார் பழனிச்சாமி34c765688b93
diff --git a/opendevin/controller/agent_controller.py b/opendevin/controller/agent_controller.pyindex 8c6c50cbae..40c41ec3dc 100644--- a/opendevin/controller/agent_controller.py+++ b/opendevin/controller/agent_controller.py@@ -153,12 +153,10 @@ class AgentController:await self.set_agent_state_to(event.agent_state) # type: ignoreelif isinstance(event, MessageAction):if event.source == EventSource.USER:- logger.info(event, extra={'msg_type': 'OBSERVATION'})await self.add_history(event, NullObservation(''))if self.get_agent_state() != AgentState.RUNNING:await self.set_agent_state_to(AgentState.RUNNING)elif event.source == EventSource.AGENT and event.wait_for_response:- logger.info(event, extra={'msg_type': 'ACTION'})await self.set_agent_state_to(AgentState.AWAITING_USER_INPUT)elif isinstance(event, AgentDelegateAction):await self.start_delegate(event)@@ -347,8 +345,6 @@ class AgentController:await self.report_error(str(e))return- logger.info(action, extra={'msg_type': 'ACTION'})-if action.runnable:self._pending_action = actionelse:@@ -358,6 +354,7 @@ class AgentController:self.event_stream.add_event(action, EventSource.AGENT)await self.update_state_after_step()+ logger.info(action, extra={'msg_type': 'ACTION'})if self._is_stuck():await self.report_error('Agent got stuck in a loop')diff --git a/opendevin/core/logger.py b/opendevin/core/logger.pyindex 46f233cf48..b2eaae0aa7 100644--- a/opendevin/core/logger.py+++ b/opendevin/core/logger.py@@ -32,7 +32,9 @@ ColorType = Literal[LOG_COLORS: Mapping[str, ColorType] = {'ACTION': 'green',+ 'USER_ACTION': 'light_red','OBSERVATION': 'yellow',+ 'USER_OBSERVATION': 'light_green','DETAIL': 'cyan','ERROR': 'red','PLAN': 'light_magenta',@@ -41,7 +43,12 @@ LOG_COLORS: Mapping[str, ColorType] = {class ColoredFormatter(logging.Formatter):def format(self, record):- msg_type = record.__dict__.get('msg_type', None)+ msg_type = record.__dict__.get('msg_type')+ event_source = record.__dict__.get('event_source')+ if event_source:+ new_msg_type = f'{event_source.upper()}_{msg_type}'+ if new_msg_type in LOG_COLORS:+ msg_type = new_msg_typeif msg_type in LOG_COLORS and not DISABLE_COLOR_PRINTING:msg_type_color = colored(msg_type, LOG_COLORS[msg_type])msg = colored(record.msg, LOG_COLORS[msg_type])@@ -50,7 +57,7 @@ class ColoredFormatter(logging.Formatter):)name_str = colored(record.name, LOG_COLORS[msg_type])level_str = colored(record.levelname, LOG_COLORS[msg_type])- if msg_type in ['ERROR']:+ if msg_type in ['ERROR'] or config.debug:return f'{time_str} - {name_str}:{level_str}: {record.filename}:{record.lineno}\n{msg_type_color}\n{msg}'return f'{time_str} - {msg_type_color}\n{msg}'elif msg_type == 'STEP':diff --git a/opendevin/events/action/commands.py b/opendevin/events/action/commands.pyindex 84a1441cda..451da57310 100644--- a/opendevin/events/action/commands.py+++ b/opendevin/events/action/commands.py@@ -18,7 +18,7 @@ class CmdRunAction(Action):return f'Running command: {self.command}'def __str__(self) -> str:- ret = '**CmdRunAction**\n'+ ret = f'**CmdRunAction (source={self.source})**\n'if self.thought:ret += f'THOUGHT: {self.thought}\n'ret += f'COMMAND:\n{self.command}'diff --git a/opendevin/events/observation/commands.py b/opendevin/events/observation/commands.pyindex 1a199cec8f..089d8ae08c 100644--- a/opendevin/events/observation/commands.py+++ b/opendevin/events/observation/commands.py@@ -25,7 +25,7 @@ class CmdOutputObservation(Observation):return f'Command `{self.command}` executed with exit code {self.exit_code}.'def __str__(self) -> str:- return f'**CmdOutputObservation (exit code={self.exit_code})**\n{self.content}'+ return f'**CmdOutputObservation (source={self.source}, exit code={self.exit_code})**\n{self.content}'@dataclassdiff --git a/opendevin/runtime/runtime.py b/opendevin/runtime/runtime.pyindex 2a06be0fda..fecaf431ea 100644--- a/opendevin/runtime/runtime.py+++ b/opendevin/runtime/runtime.py@@ -4,7 +4,7 @@ from typing import Any, Optionalfrom opendevin.core.config import configfrom opendevin.core.exceptions import BrowserInitExceptionfrom opendevin.core.logger import opendevin_logger as logger-from opendevin.events import EventSource, EventStream, EventStreamSubscriber+from opendevin.events import EventStream, EventStreamSubscriberfrom opendevin.events.action import (Action,AgentRecallAction,@@ -105,8 +105,7 @@ class Runtime:if isinstance(event, Action):observation = await self.run_action(event)observation._cause = event.id # type: ignore[attr-defined]- source = event.source if event.source else EventSource.AGENT- self.event_stream.add_event(observation, source)+ self.event_stream.add_event(observation, event.source) # type: ignore[arg-type]async def run_action(self, action: Action) -> Observation:"""diff --git a/opendevin/server/session/session.py b/opendevin/server/session/session.pyindex eb5e37aaa5..6933baf266 100644--- a/opendevin/server/session/session.py+++ b/opendevin/server/session/session.py@@ -7,7 +7,7 @@ from opendevin.core.const.guide_url import TROUBLESHOOTING_URLfrom opendevin.core.logger import opendevin_logger as loggerfrom opendevin.core.schema import AgentStatefrom opendevin.core.schema.action import ActionType-from opendevin.events.action import ChangeAgentStateAction, NullAction+from opendevin.events.action import Action, ChangeAgentStateAction, NullActionfrom opendevin.events.event import Event, EventSourcefrom opendevin.events.observation import (AgentStateChangedObservation,@@ -103,6 +103,10 @@ class Session:returnevent = event_from_dict(data.copy())self.agent_session.event_stream.add_event(event, EventSource.USER)+ if isinstance(event, Action):+ logger.info(+ event, extra={'msg_type': 'ACTION', 'event_source': EventSource.USER}+ )async def send(self, data: dict[str, object]) -> bool:try: