Skip to content

Commit d57ccf7

Browse files
committed
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.
1 parent ada2e19 commit d57ccf7

File tree

1 file changed

+14
-18
lines changed
  • autogpts/autogpt/autogpt/agents/features

1 file changed

+14
-18
lines changed

autogpts/autogpt/autogpt/agents/features/context.py

+14-18
Original file line numberDiff line numberDiff line change
@@ -71,34 +71,32 @@ def get_commands(self) -> Iterator[Command]:
7171
)
7272
}
7373
)
74-
async def open_file(self, file_path: Path) -> str:
74+
async def open_file(self, file_path: str | Path) -> str:
7575
"""Opens a file for editing or continued viewing;
7676
creates it if it does not exist yet.
7777
Note: If you only need to read or write a file once,
7878
use `write_to_file` instead.
7979
8080
Args:
81-
file_path (Path): The path of the file to open
81+
file_path (str | Path): The path of the file to open
8282
8383
Returns:
8484
str: A status message indicating what happened
8585
"""
86-
# Try to make the file path relative
87-
relative_file_path = None
88-
with contextlib.suppress(ValueError):
89-
relative_file_path = file_path.relative_to(self.workspace.root)
86+
if not isinstance(file_path, Path):
87+
file_path = Path(file_path)
9088

9189
created = False
9290
if not self.workspace.exists(file_path):
9391
await self.workspace.write_file(file_path, "")
9492
created = True
9593

96-
file_path = relative_file_path or file_path
94+
# Try to make the file path relative
95+
with contextlib.suppress(ValueError):
96+
file_path = file_path.relative_to(self.workspace.root)
9797

9898
file = FileContextItem(path=file_path)
99-
10099
self.context.add(file)
101-
102100
return (
103101
f"File {file_path}{' created,' if created else ''} has been opened"
104102
" and added to the context ✅"
@@ -113,31 +111,29 @@ async def open_file(self, file_path: Path) -> str:
113111
)
114112
}
115113
)
116-
def open_folder(self, path: Path) -> str:
114+
def open_folder(self, path: str | Path) -> str:
117115
"""Open a folder to keep track of its content
118116
119117
Args:
120-
path (Path): The path of the folder to open
118+
path (str | Path): The path of the folder to open
121119
122120
Returns:
123121
str: A status message indicating what happened
124122
"""
125-
# Try to make the path relative
126-
relative_path = None
127-
with contextlib.suppress(ValueError):
128-
relative_path = path.relative_to(self.workspace.root)
123+
if not isinstance(path, Path):
124+
path = Path(path)
129125

130126
if not self.workspace.exists(path):
131127
raise FileNotFoundError(
132128
f"open_folder {path} failed: no such file or directory"
133129
)
134130

135-
path = relative_path or path
131+
# Try to make the path relative
132+
with contextlib.suppress(ValueError):
133+
path = path.relative_to(self.workspace.root)
136134

137135
folder = FolderContextItem(path=path)
138-
139136
self.context.add(folder)
140-
141137
return f"Folder {path} has been opened and added to the context ✅"
142138

143139
@command(

0 commit comments

Comments
 (0)