aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Reinier van der Leer <pwuts@agpt.co> 2024-05-02 02:41:03 +0200
committerGravatar Reinier van der Leer <pwuts@agpt.co> 2024-05-02 02:41:03 +0200
commitd57ccf7ec9e8ce6c1c2d5077e1488403bdcf35c7 (patch)
treefad4e5349f24d2f25f88ff1abed6be6d0e324e3b
parentrefactor(agent)!: Use Pydantic models for `Agent` process output (#7116) (diff)
downloadAuto-GPT-d57ccf7ec9e8ce6c1c2d5077e1488403bdcf35c7.tar.gz
Auto-GPT-d57ccf7ec9e8ce6c1c2d5077e1488403bdcf35c7.tar.bz2
Auto-GPT-d57ccf7ec9e8ce6c1c2d5077e1488403bdcf35c7.zip
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.
-rw-r--r--autogpts/autogpt/autogpt/agents/features/context.py32
1 files changed, 14 insertions, 18 deletions
diff --git a/autogpts/autogpt/autogpt/agents/features/context.py b/autogpts/autogpt/autogpt/agents/features/context.py
index 5de7b7967..405c6e559 100644
--- a/autogpts/autogpt/autogpt/agents/features/context.py
+++ b/autogpts/autogpt/autogpt/agents/features/context.py
@@ -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;
creates it if it does not exist yet.
Note: If you only need to read or write a file once,
use `write_to_file` instead.
Args:
- file_path (Path): The path of the file to open
+ file_path (str | Path): The path of the file to open
Returns:
str: A status message indicating what happened
"""
- # Try to make the file path relative
- relative_file_path = None
- with contextlib.suppress(ValueError):
- relative_file_path = file_path.relative_to(self.workspace.root)
+ if not isinstance(file_path, Path):
+ file_path = Path(file_path)
created = False
if not self.workspace.exists(file_path):
await self.workspace.write_file(file_path, "")
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)
-
self.context.add(file)
-
return (
f"File {file_path}{' created,' if created else ''} has been opened"
" 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
Args:
- path (Path): The path of the folder to open
+ path (str | Path): The path of the folder to open
Returns:
str: A status message indicating what happened
"""
- # Try to make the path relative
- relative_path = None
- with contextlib.suppress(ValueError):
- relative_path = path.relative_to(self.workspace.root)
+ if not isinstance(path, Path):
+ path = Path(path)
if not self.workspace.exists(path):
raise FileNotFoundError(
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)
-
self.context.add(folder)
-
return f"Folder {path} has been opened and added to the context ✅"
@command(