files.py 2.2 KB

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