files.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. from dataclasses import dataclass
  2. from typing import ClassVar
  3. from openhands.core.schema import ActionType
  4. from openhands.events.action.action import Action, ActionSecurityRisk
  5. @dataclass
  6. class FileReadAction(Action):
  7. """Reads a file from a given path.
  8. Can be set to read specific lines using start and end
  9. Default lines 0:-1 (whole file)
  10. """
  11. path: str
  12. start: int = 0
  13. end: int = -1
  14. thought: str = ''
  15. action: str = ActionType.READ
  16. runnable: ClassVar[bool] = True
  17. security_risk: ActionSecurityRisk | None = None
  18. @property
  19. def message(self) -> str:
  20. return f'Reading file: {self.path}'
  21. @dataclass
  22. class FileWriteAction(Action):
  23. """Writes a file to a given path.
  24. Can be set to write specific lines using start and end
  25. Default lines 0:-1 (whole file)
  26. """
  27. path: str
  28. content: str
  29. start: int = 0
  30. end: int = -1
  31. thought: str = ''
  32. action: str = ActionType.WRITE
  33. runnable: ClassVar[bool] = True
  34. security_risk: ActionSecurityRisk | None = None
  35. @property
  36. def message(self) -> str:
  37. return f'Writing file: {self.path}'
  38. @dataclass
  39. class FileEditAction(Action):
  40. """Edits a file by provided a draft at a given path.
  41. Can be set to edit specific lines using start and end (1-index, inclusive) if the file is too long.
  42. Default lines 1:-1 (whole file).
  43. If start is set to -1, the FileEditAction will simply append the content to the file.
  44. """
  45. path: str
  46. content: str
  47. start: int = 1
  48. end: int = -1
  49. thought: str = ''
  50. action: str = ActionType.EDIT
  51. runnable: ClassVar[bool] = True
  52. security_risk: ActionSecurityRisk | None = None
  53. def __repr__(self) -> str:
  54. ret = '**FileEditAction**\n'
  55. ret += f'Thought: {self.thought}\n'
  56. ret += f'Range: [L{self.start}:L{self.end}]\n'
  57. ret += f'Path: [{self.path}]\n'
  58. ret += f'Content:\n```\n{self.content}\n```\n'
  59. return ret