Backup-bdg/OpenHands
0
1from dataclasses import dataclass2from typing import ClassVar3 4from openhands.core.schema import ActionType5from openhands.events.action.action import Action, ActionSecurityRisk6from openhands.events.event import FileEditSource, FileReadSource7 8 9@dataclass10class FileReadAction(Action):11 """Reads a file from a given path.12 Can be set to read specific lines using start and end13 Default lines 0:-1 (whole file)14 """15 16 path: str17 start: int = 018 end: int = -119 thought: str = ''20 action: str = ActionType.READ21 runnable: ClassVar[bool] = True22 security_risk: ActionSecurityRisk | None = None23 impl_source: FileReadSource = FileReadSource.DEFAULT24 view_range: list[int] | None = None # ONLY used in OH_ACI mode25 26 @property27 def message(self) -> str:28 return f'Reading file: {self.path}'29 30 31@dataclass32class FileWriteAction(Action):33 """Writes a file to a given path.34 Can be set to write specific lines using start and end35 Default lines 0:-1 (whole file)36 """37 38 path: str39 content: str40 start: int = 041 end: int = -142 thought: str = ''43 action: str = ActionType.WRITE44 runnable: ClassVar[bool] = True45 security_risk: ActionSecurityRisk | None = None46 47 @property48 def message(self) -> str:49 return f'Writing file: {self.path}'50 51 def __repr__(self) -> str:52 return (53 f'**FileWriteAction**\n'54 f'Path: {self.path}\n'55 f'Range: [L{self.start}:L{self.end}]\n'56 f'Thought: {self.thought}\n'57 f'Content:\n```\n{self.content}\n```\n'58 )59 60 61@dataclass62class FileEditAction(Action):63 """Edits a file using various commands including view, create, str_replace, insert, and undo_edit.64 65 This class supports two main modes of operation:66 1. LLM-based editing (impl_source = FileEditSource.LLM_BASED_EDIT)67 2. ACI-based editing (impl_source = FileEditSource.OH_ACI)68 69 Attributes:70 path (str): The path to the file being edited. Works for both LLM-based and OH_ACI editing.71 OH_ACI only arguments:72 command (str): The editing command to be performed (view, create, str_replace, insert, undo_edit, write).73 file_text (str): The content of the file to be created (used with 'create' command in OH_ACI mode).74 old_str (str): The string to be replaced (used with 'str_replace' command in OH_ACI mode).75 new_str (str): The string to replace old_str (used with 'str_replace' and 'insert' commands in OH_ACI mode).76 insert_line (int): The line number after which to insert new_str (used with 'insert' command in OH_ACI mode).77 LLM-based editing arguments:78 content (str): The content to be written or edited in the file (used in LLM-based editing and 'write' command).79 start (int): The starting line for editing (1-indexed, inclusive). Default is 1.80 end (int): The ending line for editing (1-indexed, inclusive). Default is -1 (end of file).81 thought (str): The reasoning behind the edit action.82 action (str): The type of action being performed (always ActionType.EDIT).83 runnable (bool): Indicates if the action can be executed (always True).84 security_risk (ActionSecurityRisk | None): Indicates any security risks associated with the action.85 impl_source (FileEditSource): The source of the implementation (LLM_BASED_EDIT or OH_ACI).86 87 Usage:88 - For LLM-based editing: Use path, content, start, and end attributes.89 - For ACI-based editing: Use path, command, and the appropriate attributes for the specific command.90 91 Note:92 - If start is set to -1 in LLM-based editing, the content will be appended to the file.93 - The 'write' command behaves similarly to LLM-based editing, using content, start, and end attributes.94 """95 96 path: str97 98 # OH_ACI arguments99 command: str = ''100 file_text: str | None = None101 old_str: str | None = None102 new_str: str | None = None103 insert_line: int | None = None104 105 # LLM-based editing arguments106 content: str = ''107 start: int = 1108 end: int = -1109 110 # Shared arguments111 thought: str = ''112 action: str = ActionType.EDIT113 runnable: ClassVar[bool] = True114 security_risk: ActionSecurityRisk | None = None115 impl_source: FileEditSource = FileEditSource.OH_ACI116 117 def __repr__(self) -> str:118 ret = '**FileEditAction**\n'119 ret += f'Path: [{self.path}]\n'120 ret += f'Thought: {self.thought}\n'121 122 if self.impl_source == FileEditSource.LLM_BASED_EDIT:123 ret += f'Range: [L{self.start}:L{self.end}]\n'124 ret += f'Content:\n```\n{self.content}\n```\n'125 else: # OH_ACI mode126 ret += f'Command: {self.command}\n'127 if self.command == 'create':128 ret += f'Created File with Text:\n```\n{self.file_text}\n```\n'129 elif self.command == 'str_replace':130 ret += f'Old String: ```\n{self.old_str}\n```\n'131 ret += f'New String: ```\n{self.new_str}\n```\n'132 elif self.command == 'insert':133 ret += f'Insert Line: {self.insert_line}\n'134 ret += f'New String: ```\n{self.new_str}\n```\n'135 elif self.command == 'undo_edit':136 ret += 'Undo Edit\n'137 # We ignore "view" command because it will be mapped to a FileReadAction138 return ret139 