π OpenHands: AI-Driven Development
Print with color in controller (#176)
Xingyao Wangfe736013cc86
diff --git a/agenthub/codeact_agent/codeact_agent.py b/agenthub/codeact_agent/codeact_agent.pyindex aa004a74c4..4f45c3e0b6 100644--- a/agenthub/codeact_agent/codeact_agent.py+++ b/agenthub/codeact_agent/codeact_agent.py@@ -1,7 +1,6 @@import refrom typing import List, Mapping-from termcolor import coloredfrom opendevin.agent import Agentfrom opendevin.state import State@@ -75,19 +74,16 @@ class CodeActAgent(Agent):{"role": "system", "content": SYSTEM_MESSAGE},{"role": "user", "content": state.plan.main_goal},]- print(colored("===USER:===\n" + state.plan.main_goal, "green"))updated_info = state.updated_infoif updated_info:for prev_action, obs in updated_info:assert isinstance(prev_action, (CmdRunAction, AgentEchoAction)), "Expecting CmdRunAction or AgentEchoAction for Action"if isinstance(obs, AgentMessageObservation): # warning message from itselfself.messages.append({"role": "user", "content": obs.content})- print(colored("===USER:===\n" + obs.content, "green"))elif isinstance(obs, CmdOutputObservation):content = "OBSERVATION:\n" + obs.contentcontent += f"\n[Command {obs.command_id} finished with exit code {obs.exit_code}]]"self.messages.append({"role": "user", "content": content})- print(colored("===ENV OBSERVATION:===\n" + content, "blue"))else:raise NotImplementedError(f"Unknown observation type: {obs.__class__}")response = self.llm.completion(@@ -98,27 +94,23 @@ class CodeActAgent(Agent):)action_str: str = parse_response(response)self.messages.append({"role": "assistant", "content": action_str})- print(colored("===ASSISTANT:===\n" + action_str, "yellow"))command = re.search(r"<execute>(.*)</execute>", action_str, re.DOTALL)if command is not None:# a command was foundcommand_group = command.group(1)if command_group.strip() == "exit":- print(colored("Exit received. Exiting...", "red"))return AgentFinishAction()return CmdRunAction(command = command_group)# # execute the code# # TODO: does exit_code get loaded into Message?# exit_code, observation = self.env.execute(command_group)# self._history.append(Message(Role.ASSISTANT, observation))- # print(colored("===ENV OBSERVATION:===\n" + observation, "blue"))else:# we could provide a error message for the model to continue similar to# https://github.com/xingyaoww/mint-bench/blob/main/mint/envs/general_env.py#L18-L23# observation = INVALID_INPUT_MESSAGE# self._history.append(Message(Role.ASSISTANT, observation))- # print(colored("===ENV OBSERVATION:===\n" + observation, "blue"))return AgentEchoAction(content=INVALID_INPUT_MESSAGE) # warning message to itselfdiff --git a/agenthub/planner_agent/prompt.py b/agenthub/planner_agent/prompt.pyindex fb9ae9c7f2..5b56fc3ffe 100644--- a/agenthub/planner_agent/prompt.py+++ b/agenthub/planner_agent/prompt.py@@ -1,7 +1,7 @@import jsonfrom typing import List, Tuple, Dict, Type-from opendevin.controller.agent_controller import print_with_indent+from opendevin.controller.agent_controller import print_with_colorfrom opendevin.plan import Planfrom opendevin.action import Action, action_from_dictfrom opendevin.observation import Observation@@ -178,7 +178,7 @@ def get_prompt(plan: Plan, history: List[Tuple[Action, Observation]]):elif latest_action_id == "finish":hint = ""- print_with_indent("HINT:\n" + hint)+ print_with_color("HINT:\n" + hint, "INFO")return prompt % {'task': plan.main_goal,'plan': plan_str,diff --git a/opendevin/controller/agent_controller.py b/opendevin/controller/agent_controller.pyindex beebfef0f6..34e4582f24 100644--- a/opendevin/controller/agent_controller.py+++ b/opendevin/controller/agent_controller.py@@ -1,7 +1,8 @@import asyncio-from typing import List, Callableimport traceback+from typing import List, Callable, Literal, Mapping, Any+from termcolor import coloredfrom opendevin.plan import Planfrom opendevin.state import State@@ -21,8 +22,24 @@ from opendevin.observation import (from .command_manager import CommandManager-def print_with_indent(text: str):- print("\t"+text.replace("\n","\n\t"), flush=True)++ColorType = Literal['red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'light_grey', 'dark_grey', 'light_red', 'light_green', 'light_yellow', 'light_blue', 'light_magenta', 'light_cyan', 'white']++def print_with_color(text: Any, print_type: str = "INFO"):+ TYPE_TO_COLOR: Mapping[str, ColorType] = {+ "BACKGROUND LOG": "blue",+ "ACTION": "green",+ "OBSERVATION": "yellow",+ "INFO": "cyan",+ "ERROR": "red",+ "PLAN": "light_magenta",+ }+ color = TYPE_TO_COLOR.get(print_type.upper(), TYPE_TO_COLOR["INFO"])+ print(+ colored(f"\n{print_type.upper()}:\n", color, attrs=["bold"])+ + colored(str(text), color),+ flush=True,+ )class AgentController:def __init__(@@ -72,14 +89,13 @@ class AgentController:async def step(self, i: int):print("\n\n==============", flush=True)print("STEP", i, flush=True)- print_with_indent("\nPLAN:\n")- print_with_indent(self.state.plan.__str__())+ print_with_color(self.state.plan.main_goal, "PLAN")log_obs = self.command_manager.get_background_obs()for obs in log_obs:self.add_history(NullAction(), obs)await self._run_callbacks(obs)- print_with_indent("\nBACKGROUND LOG:\n%s" % obs)+ print_with_color(obs, "BACKGROUND LOG")self.update_state_for_step(i)action: Action = NullAction()@@ -88,10 +104,10 @@ class AgentController:action = self.agent.step(self.state)if action is None:raise ValueError("Agent must return an action")- print_with_indent("\nACTION:\n%s" % action)+ print_with_color(action, "ACTION")except Exception as e:observation = AgentErrorObservation(str(e))- print_with_indent("\nAGENT ERROR:\n%s" % observation)+ print_with_color(observation, "ERROR")traceback.print_exc()# TODO Change to more robust error handlingif "The api_key client option must be set" in observation.content:@@ -102,7 +118,7 @@ class AgentController:finished = isinstance(action, AgentFinishAction)if finished:- print_with_indent("\nFINISHED")+ print_with_color(action, "INFO")return Trueif isinstance(action, AddTaskAction):@@ -110,14 +126,14 @@ class AgentController:self.state.plan.add_subtask(action.parent, action.goal, action.subtasks)except Exception as e:observation = AgentErrorObservation(str(e))- print_with_indent("\nADD TASK ERROR:\n%s" % observation)+ print_with_color(observation, "ERROR")traceback.print_exc()elif isinstance(action, ModifyTaskAction):try:self.state.plan.set_subtask_state(action.id, action.state)except Exception as e:observation = AgentErrorObservation(str(e))- print_with_indent("\nMODIFY TASK ERROR:\n%s" % observation)+ print_with_color(observation, "ERROR")traceback.print_exc()if action.executable:@@ -125,11 +141,11 @@ class AgentController:observation = action.run(self)except Exception as e:observation = AgentErrorObservation(str(e))- print_with_indent("\nACTION RUN ERROR:\n%s" % observation)+ print_with_color(observation, "ERROR")traceback.print_exc()if not isinstance(observation, NullObservation):- print_with_indent("\nOBSERVATION:\n%s" % observation)+ print_with_color(observation, "OBSERVATION")self.add_history(action, observation)await self._run_callbacks(observation)