소스 검색

Handle Unicode errors (#1388)

* Add errors for non-unicode file data or command return, trim excessively long command returns.

* Fix lint issue.

* Fix lint issue (try 2).

* Realized that prompts were trimmed elsewhere and dropped the new addition.
Christian Balcom 1 년 전
부모
커밋
546be7ca8e
3개의 변경된 파일16개의 추가작업 그리고 9개의 파일을 삭제
  1. 2 2
      opendevin/action/bash.py
  2. 4 0
      opendevin/action/fileop.py
  3. 10 7
      opendevin/controller/action_manager.py

+ 2 - 2
opendevin/action/bash.py

@@ -6,7 +6,7 @@ from opendevin.schema import ActionType
 
 if TYPE_CHECKING:
     from opendevin.controller import AgentController
-    from opendevin.observation import CmdOutputObservation
+    from opendevin.observation import CmdOutputObservation, Observation
 
 
 @dataclass
@@ -15,7 +15,7 @@ class CmdRunAction(ExecutableAction):
     background: bool = False
     action: str = ActionType.RUN
 
-    async def run(self, controller: 'AgentController') -> 'CmdOutputObservation':
+    async def run(self, controller: 'AgentController') -> 'Observation':
         return controller.action_manager.run_command(self.command, self.background)
 
     @property

+ 4 - 0
opendevin/action/fileop.py

@@ -85,6 +85,8 @@ class FileReadAction(ExecutableAction):
                         code_view = ''.join(read_lines)
                 except FileNotFoundError:
                     return AgentErrorObservation(f'File not found: {self.path}')
+                except UnicodeDecodeError:
+                    return AgentErrorObservation(f'File could not be decoded as utf-8: {self.path}')
                 except IsADirectoryError:
                     return AgentErrorObservation(f'Path is a directory: {self.path}. You can only read files')
             except PermissionError:
@@ -144,6 +146,8 @@ class FileWriteAction(ExecutableAction):
                     return AgentErrorObservation(f'File not found: {self.path}')
                 except IsADirectoryError:
                     return AgentErrorObservation(f'Path is a directory: {self.path}. You can only write to files')
+                except UnicodeDecodeError:
+                    return AgentErrorObservation(f'File could not be decoded as utf-8: {self.path}')
             except PermissionError:
                 return AgentErrorObservation(f'Malformed paths not permitted: {self.path}')
         return FileWriteObservation(content='', path=self.path)

+ 10 - 7
opendevin/controller/action_manager.py

@@ -1,7 +1,7 @@
 from typing import List
 
 from opendevin import config
-from opendevin.observation import CmdOutputObservation
+from opendevin.observation import CmdOutputObservation, AgentErrorObservation
 from opendevin.sandbox import DockerExecBox, DockerSSHBox, Sandbox, LocalBox, E2BBox
 from opendevin.schema import ConfigType
 from opendevin.action import (
@@ -54,17 +54,20 @@ class ActionManager:
         observation = await action.run(agent_controller)
         return observation
 
-    def run_command(self, command: str, background=False) -> CmdOutputObservation:
+    def run_command(self, command: str, background=False) -> Observation:
         if background:
             return self._run_background(command)
         else:
             return self._run_immediately(command)
 
-    def _run_immediately(self, command: str) -> CmdOutputObservation:
-        exit_code, output = self.sandbox.execute(command)
-        return CmdOutputObservation(
-            command_id=-1, content=output, command=command, exit_code=exit_code
-        )
+    def _run_immediately(self, command: str) -> Observation:
+        try:
+            exit_code, output = self.sandbox.execute(command)
+            return CmdOutputObservation(
+                command_id=-1, content=output, command=command, exit_code=exit_code
+            )
+        except UnicodeDecodeError:
+            return AgentErrorObservation('Command output could not be decoded as utf-8')
 
     def _run_background(self, command: str) -> CmdOutputObservation:
         bg_cmd = self.sandbox.execute_in_background(command)