Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

Enable `open_file` and `open_folder`

Reinier van der Leer2a8cc4816e7d
diff --git a/autogpt/commands/file_context.py b/autogpt/commands/file_context.py
index bfcaba5eec..9a64539325 100644
--- a/autogpt/commands/file_context.py
+++ b/autogpt/commands/file_context.py
@@ -23,7 +23,7 @@ from autogpt.models.context_item import FileContextItem, FolderContextItem
from .decorators import sanitize_path_arg
-def compatible_with_agent(agent: BaseAgent):
+def compatible_with_agent(agent: BaseAgent) -> bool:
return isinstance(agent, ContextMixin)
@@ -54,10 +54,7 @@ def open_file(file_path: Path, agent: Agent) -> tuple[str, FileContextItem]:
with contextlib.suppress(ValueError):
file_path = file_path.relative_to(agent.workspace.root)
- if (agent_context := get_agent_context(agent)) is None:
- raise NotImplementedError(
- f"{agent.__class__.__name__} does not implement context"
- )
+ assert (agent_context := get_agent_context(agent)) is not None
created = False
if not file_path.exists():
@@ -103,10 +100,7 @@ def open_folder(path: Path, agent: Agent) -> tuple[str, FolderContextItem]:
with contextlib.suppress(ValueError):
path = path.relative_to(agent.workspace.root)
- if (agent_context := get_agent_context(agent)) is None:
- raise NotImplementedError(
- f"{agent.__class__.__name__} does not implement context"
- )
+ assert (agent_context := get_agent_context(agent)) is not None
if not path.exists():
raise FileNotFoundError(f"open_folder {path} failed: no such file or directory")
diff --git a/autogpt/commands/file_operations.py b/autogpt/commands/file_operations.py
index e4280a3ca2..9e4faaabbf 100644
--- a/autogpt/commands/file_operations.py
+++ b/autogpt/commands/file_operations.py
@@ -19,6 +19,7 @@ from autogpt.command_decorator import command
from autogpt.memory.vector import MemoryItem, VectorMemory
from .decorators import sanitize_path_arg
+from .file_context import open_file, open_folder # NOQA
from .file_operations_utils import read_textual_file
logger = logging.getLogger(__name__)
diff --git a/autogpt/models/context_item.py b/autogpt/models/context_item.py
index 6a626c64db..eec579f7ca 100644
--- a/autogpt/models/context_item.py
+++ b/autogpt/models/context_item.py
@@ -1,3 +1,4 @@
+import logging
from abc import ABC, abstractmethod
from dataclasses import dataclass
from pathlib import Path
@@ -5,6 +6,8 @@ from typing import Optional
from autogpt.commands.file_operations_utils import read_textual_file
+logger = logging.getLogger(__name__)
+
class ContextItem(ABC):
@property
@@ -48,7 +51,7 @@ class FileContextItem(ContextItem):
@property
def content(self) -> str:
- return read_textual_file(self.file_path)
+ return read_textual_file(self.file_path, logger)
@dataclass