From 60d0f5edace0e4594d7e3b5b9ff59c1aca4636ef Mon Sep 17 00:00:00 2001 From: merwanehamadi Date: Sun, 23 Jul 2023 21:28:12 -0700 Subject: [PATCH] Fix workspace crashing (#5041) Signed-off-by: Merwane Hamadi --- agbenchmark/benchmarks.py | 5 ++--- autogpt/agents/agent.py | 4 +--- autogpt/app/main.py | 7 ++++--- autogpt/workspace/workspace.py | 8 ++++---- docs/challenges/building_challenges.md | 1 - tests/conftest.py | 1 - tests/integration/agent_factory.py | 1 - tests/unit/test_message_history.py | 2 -- 8 files changed, 11 insertions(+), 18 deletions(-) diff --git a/agbenchmark/benchmarks.py b/agbenchmark/benchmarks.py index e8675f92..ea884b3a 100644 --- a/agbenchmark/benchmarks.py +++ b/agbenchmark/benchmarks.py @@ -27,8 +27,8 @@ def bootstrap_agent(task, continuous_mode) -> Agent: config.plain_output = True command_registry = get_command_registry(config) config.memory_backend = "no_memory" - Workspace.set_workspace_directory(config) - Workspace.build_file_logger_path(config, config.workspace_path) + config.workspace_path = Workspace.set_workspace_directory(config) + config.file_logger_path = Workspace.build_file_logger_path(config.workspace_path) ai_config = AIConfig( ai_name="Auto-GPT", ai_role="a multi-purpose AI assistant.", @@ -41,7 +41,6 @@ def bootstrap_agent(task, continuous_mode) -> Agent: ai_config=ai_config, config=config, triggering_prompt=DEFAULT_TRIGGERING_PROMPT, - workspace_directory=str(config.workspace_path), ) diff --git a/autogpt/agents/agent.py b/autogpt/agents/agent.py index f3fee609..93d3de86 100644 --- a/autogpt/agents/agent.py +++ b/autogpt/agents/agent.py @@ -3,7 +3,6 @@ from __future__ import annotations import json import time from datetime import datetime -from pathlib import Path from typing import TYPE_CHECKING, Any, Optional if TYPE_CHECKING: @@ -37,7 +36,6 @@ class Agent(BaseAgent): command_registry: CommandRegistry, memory: VectorMemory, triggering_prompt: str, - workspace_directory: str | Path, config: Config, cycle_budget: Optional[int] = None, ): @@ -52,7 +50,7 @@ class Agent(BaseAgent): self.memory = memory """VectorMemoryProvider used to manage the agent's context (TODO)""" - self.workspace = Workspace(workspace_directory, config.restrict_to_workspace) + self.workspace = Workspace(config.workspace_path, config.restrict_to_workspace) """Workspace that the agent has access to, e.g. for reading/writing files.""" self.created_at = datetime.now().strftime("%Y%m%d_%H%M%S") diff --git a/autogpt/app/main.py b/autogpt/app/main.py index a3c7d1d8..fa61eeaf 100644 --- a/autogpt/app/main.py +++ b/autogpt/app/main.py @@ -126,10 +126,12 @@ def run_auto_gpt( # TODO: have this directory live outside the repository (e.g. in a user's # home directory) and have it come in as a command line argument or part of # the env file. - Workspace.set_workspace_directory(config, workspace_directory) + config.workspace_path = Workspace.set_workspace_directory( + config, workspace_directory + ) # HACK: doing this here to collect some globals that depend on the workspace. - Workspace.build_file_logger_path(config, config.workspace_path) + config.file_logger_path = Workspace.build_file_logger_path(config.workspace_path) config.plugins = scan_plugins(config, config.debug_mode) # Create a CommandRegistry instance and scan default folder @@ -192,7 +194,6 @@ def run_auto_gpt( memory=memory, command_registry=command_registry, triggering_prompt=DEFAULT_TRIGGERING_PROMPT, - workspace_directory=workspace_directory, ai_config=ai_config, config=config, ) diff --git a/autogpt/workspace/workspace.py b/autogpt/workspace/workspace.py index e580d4c4..510d18a4 100644 --- a/autogpt/workspace/workspace.py +++ b/autogpt/workspace/workspace.py @@ -144,21 +144,21 @@ class Workspace: return full_path @staticmethod - def build_file_logger_path(config: Config, workspace_directory: Path): + def build_file_logger_path(workspace_directory: Path) -> str: file_logger_path = workspace_directory / "file_logger.txt" if not file_logger_path.exists(): with file_logger_path.open(mode="w", encoding="utf-8") as f: f.write("File Operation Logger ") - config.file_logger_path = str(file_logger_path) + return str(file_logger_path) @staticmethod def set_workspace_directory( config: Config, workspace_directory: Optional[str | Path] = None - ) -> None: + ) -> Path: if workspace_directory is None: workspace_directory = config.workdir / "auto_gpt_workspace" elif type(workspace_directory) == str: workspace_directory = Path(workspace_directory) # TODO: pass in the ai_settings file and the env file and have them cloned into # the workspace directory so we can bind them to the agent. - config.workspace_path = Workspace.make_workspace(workspace_directory) + return Workspace.make_workspace(workspace_directory) diff --git a/docs/challenges/building_challenges.md b/docs/challenges/building_challenges.md index a4d0fa08..9caf5cdd 100644 --- a/docs/challenges/building_challenges.md +++ b/docs/challenges/building_challenges.md @@ -59,7 +59,6 @@ def kubernetes_agent( config=ai_config, next_action_count=0, triggering_prompt=DEFAULT_TRIGGERING_PROMPT, - workspace_directory=workspace.root, ) return agent diff --git a/tests/conftest.py b/tests/conftest.py index 2becc8bf..1efd6204 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -103,5 +103,4 @@ def agent(config: Config, workspace: Workspace) -> Agent: ai_config=ai_config, config=config, triggering_prompt=DEFAULT_TRIGGERING_PROMPT, - workspace_directory=workspace.root, ) diff --git a/tests/integration/agent_factory.py b/tests/integration/agent_factory.py index 89e3b763..45d195e3 100644 --- a/tests/integration/agent_factory.py +++ b/tests/integration/agent_factory.py @@ -38,7 +38,6 @@ def dummy_agent(config: Config, memory_json_file, workspace: Workspace): ai_config=ai_config, config=config, triggering_prompt="dummy triggering prompt", - workspace_directory=workspace.root, ) return agent diff --git a/tests/unit/test_message_history.py b/tests/unit/test_message_history.py index e434f9d5..08a3a24b 100644 --- a/tests/unit/test_message_history.py +++ b/tests/unit/test_message_history.py @@ -19,7 +19,6 @@ def agent(config: Config): command_registry = MagicMock() ai_config = AIConfig(ai_name="Test AI") triggering_prompt = "Triggering prompt" - workspace_directory = "workspace_directory" agent = Agent( memory=memory, @@ -27,7 +26,6 @@ def agent(config: Config): ai_config=ai_config, config=config, triggering_prompt=triggering_prompt, - workspace_directory=workspace_directory, ) return agent