From 2a8cc4816e7d838ea444b90fe4c22ab2095c3deb Mon Sep 17 00:00:00 2001 From: Reinier van der Leer Date: Tue, 22 Aug 2023 23:31:16 +0200 Subject: [PATCH] Enable `open_file` and `open_folder` --- autogpt/commands/file_context.py | 12 +++--------- autogpt/commands/file_operations.py | 1 + autogpt/models/context_item.py | 5 ++++- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/autogpt/commands/file_context.py b/autogpt/commands/file_context.py index bfcaba5e..9a645393 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 e4280a3c..9e4faaab 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 6a626c64..eec579f7 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