fix(agent): Fix open_file and open_folder commands

They weren't ported properly to the new component-based architecture: the `@sanitize_path` decorator was removed, causing path handling issues.
This commit is contained in:
Reinier van der Leer 2024-05-02 02:41:03 +02:00
parent ada2e19829
commit d57ccf7ec9
No known key found for this signature in database
GPG Key ID: CDC1180FDAE06193

View File

@ -71,34 +71,32 @@ class ContextComponent(MessageProvider, CommandProvider):
) )
} }
) )
async def open_file(self, file_path: Path) -> str: async def open_file(self, file_path: str | Path) -> str:
"""Opens a file for editing or continued viewing; """Opens a file for editing or continued viewing;
creates it if it does not exist yet. creates it if it does not exist yet.
Note: If you only need to read or write a file once, Note: If you only need to read or write a file once,
use `write_to_file` instead. use `write_to_file` instead.
Args: Args:
file_path (Path): The path of the file to open file_path (str | Path): The path of the file to open
Returns: Returns:
str: A status message indicating what happened str: A status message indicating what happened
""" """
# Try to make the file path relative if not isinstance(file_path, Path):
relative_file_path = None file_path = Path(file_path)
with contextlib.suppress(ValueError):
relative_file_path = file_path.relative_to(self.workspace.root)
created = False created = False
if not self.workspace.exists(file_path): if not self.workspace.exists(file_path):
await self.workspace.write_file(file_path, "") await self.workspace.write_file(file_path, "")
created = True created = True
file_path = relative_file_path or file_path # Try to make the file path relative
with contextlib.suppress(ValueError):
file_path = file_path.relative_to(self.workspace.root)
file = FileContextItem(path=file_path) file = FileContextItem(path=file_path)
self.context.add(file) self.context.add(file)
return ( return (
f"File {file_path}{' created,' if created else ''} has been opened" f"File {file_path}{' created,' if created else ''} has been opened"
" and added to the context ✅" " and added to the context ✅"
@ -113,31 +111,29 @@ class ContextComponent(MessageProvider, CommandProvider):
) )
} }
) )
def open_folder(self, path: Path) -> str: def open_folder(self, path: str | Path) -> str:
"""Open a folder to keep track of its content """Open a folder to keep track of its content
Args: Args:
path (Path): The path of the folder to open path (str | Path): The path of the folder to open
Returns: Returns:
str: A status message indicating what happened str: A status message indicating what happened
""" """
# Try to make the path relative if not isinstance(path, Path):
relative_path = None path = Path(path)
with contextlib.suppress(ValueError):
relative_path = path.relative_to(self.workspace.root)
if not self.workspace.exists(path): if not self.workspace.exists(path):
raise FileNotFoundError( raise FileNotFoundError(
f"open_folder {path} failed: no such file or directory" f"open_folder {path} failed: no such file or directory"
) )
path = relative_path or path # Try to make the path relative
with contextlib.suppress(ValueError):
path = path.relative_to(self.workspace.root)
folder = FolderContextItem(path=path) folder = FolderContextItem(path=path)
self.context.add(folder) self.context.add(folder)
return f"Folder {path} has been opened and added to the context ✅" return f"Folder {path} has been opened and added to the context ✅"
@command( @command(