浏览代码

add error observations (#196)

Robert Brennan 1 年之前
父节点
当前提交
9e924ab4b1
共有 2 个文件被更改,包括 25 次插入7 次删除
  1. 15 7
      opendevin/controller/__init__.py
  2. 10 0
      opendevin/observation.py

+ 15 - 7
opendevin/controller/__init__.py

@@ -13,6 +13,7 @@ from opendevin.action import (
 )
 from opendevin.observation import (
     Observation,
+    AgentErrorObservation,
     NullObservation
 )
 
@@ -78,9 +79,16 @@ class AgentController:
             print_with_indent("\nBACKGROUND LOG:\n%s" % obs)
 
         state: State = self.get_current_state()
-        action: Action = self.agent.step(state)
+        action: Action = NullAction()
+        observation: Observation = NullObservation("")
+        try:
+            action = self.agent.step(state)
+            print_with_indent("\nACTION:\n%s" % action)
+        except Exception as e:
+            observation = AgentErrorObservation(str(e))
+            print_with_indent("\nAGENT ERROR:\n%s" % observation)
+            traceback.print_exc()
 
-        print_with_indent("\nACTION:\n%s" % action)
         await self._run_callbacks(action)
 
         if isinstance(action, AgentFinishAction):
@@ -93,14 +101,14 @@ class AgentController:
             action = action_cls(**_kwargs)
             print(action, flush=True)
         if action.executable:
-            observation: Observation = action.run(self)
-        else:
-            observation = NullObservation("")
-        print_with_indent("\nOBSERVATION:\n%s" % observation)
+            observation = action.run(self)
+
+        if not isinstance(observation, NullObservation):
+            print_with_indent("\nOBSERVATION:\n%s" % observation)
+
         self.add_history(action, observation)
         await self._run_callbacks(observation)
 
-
     async def _run_callbacks(self, event):
         if event is None:
             return

+ 10 - 0
opendevin/observation.py

@@ -105,6 +105,16 @@ class AgentRecallObservation(Observation):
         return "The agent recalled memories."
 
 
+@dataclass
+class AgentErrorObservation(Observation):
+    """
+    This data class represents an error encountered by the agent.
+    """
+
+    @property
+    def message(self) -> str:
+        return "Oops. Something went wrong: " + self.content
+
 @dataclass
 class NullObservation(Observation):
     """