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.
Migrate to mixins for Context and Workspace features
Reinier van der Leer4e64519a263c
diff --git a/autogpt/agents/agent.py b/autogpt/agents/agent.pyindex 09e2d77514..2ce2fc7617 100644--- a/autogpt/agents/agent.py+++ b/autogpt/agents/agent.py@@ -31,10 +31,10 @@ from autogpt.models.agent_actions import ()from autogpt.models.command import CommandOutputfrom autogpt.models.context_item import ContextItem-from autogpt.workspace import Workspacefrom .base import BaseAgent-from .utils.context import ContextMixin+from .features.context import ContextMixin+from .features.workspace import WorkspaceMixinfrom .utils.exceptions import (AgentException,CommandExecutionError,@@ -45,7 +45,7 @@ from .utils.exceptions import (logger = logging.getLogger(__name__)-class Agent(BaseAgent, ContextMixin):+class Agent(BaseAgent, ContextMixin, WorkspaceMixin):"""Agent class for interacting with Auto-GPT."""def __init__(@@ -68,9 +68,6 @@ class Agent(BaseAgent, ContextMixin):self.memory = memory"""VectorMemoryProvider used to manage the agent's context (TODO)"""- 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")"""Timestamp the agent was created; only used for structured debug logging."""diff --git a/autogpt/agents/utils/context.py b/autogpt/agents/features/context.pysimilarity index 99%rename from autogpt/agents/utils/context.pyrename to autogpt/agents/features/context.pyindex ea3ef1585e..3a2f81e327 100644--- a/autogpt/agents/utils/context.py+++ b/autogpt/agents/features/context.py@@ -39,9 +39,10 @@ class ContextMixin:context: AgentContextdef __init__(self, **kwargs):- super(ContextMixin, self).__init__(**kwargs)self.context = AgentContext()+ super(ContextMixin, self).__init__(**kwargs)+def get_agent_context(agent: BaseAgent) -> AgentContext | None:if isinstance(agent, ContextMixin):diff --git a/autogpt/agents/features/workspace.py b/autogpt/agents/features/workspace.pynew file mode 100644index 0000000000..41fffaeac5--- /dev/null+++ b/autogpt/agents/features/workspace.py@@ -0,0 +1,34 @@+from __future__ import annotations++from typing import TYPE_CHECKING++if TYPE_CHECKING:+ from ..base import BaseAgent++from autogpt.config import Config+from autogpt.workspace import Workspace+++class WorkspaceMixin:+ """Mixin that adds workspace support to a class"""++ workspace: Workspace+ """Workspace that the agent has access to, e.g. for reading/writing files."""++ def __init__(self, **kwargs):+ config: Config = getattr(self, "config")+ if not isinstance(config, Config):+ raise ValueError(f"Cannot initialize Workspace for Agent without Config")+ if not config.workspace_path:+ raise ValueError(f"Cannot set up Workspace: no WORKSPACE_PATH in config")++ self.workspace = Workspace(config.workspace_path, config.restrict_to_workspace)++ super(WorkspaceMixin, self).__init__(**kwargs)+++def get_agent_workspace(agent: BaseAgent) -> Workspace | None:+ if isinstance(agent, WorkspaceMixin):+ return agent.workspace++ return Nonediff --git a/autogpt/agents/planning_agent.py b/autogpt/agents/planning_agent.pyindex 22187bdea6..366d3885f9 100644--- a/autogpt/agents/planning_agent.py+++ b/autogpt/agents/planning_agent.py@@ -29,16 +29,16 @@ from autogpt.models.agent_actions import (ActionSuccessResult,)from autogpt.models.context_item import ContextItem-from autogpt.workspace import Workspacefrom .agent import execute_command, extract_commandfrom .base import BaseAgent-from .utils.context import AgentContext+from .features.context import ContextMixin+from .features.workspace import WorkspaceMixinlogger = logging.getLogger(__name__)-class PlanningAgent(BaseAgent):+class PlanningAgent(BaseAgent, ContextMixin, WorkspaceMixin):"""Agent class for interacting with Auto-GPT."""ThoughtProcessID = Literal["plan", "action", "evaluate"]@@ -63,9 +63,6 @@ class PlanningAgent(BaseAgent):self.memory = memory"""VectorMemoryProvider used to manage the agent's context (TODO)"""- 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")"""Timestamp the agent was created; only used for structured debug logging."""@@ -74,9 +71,6 @@ class PlanningAgent(BaseAgent):self.action_history = ActionHistory()- self.context = AgentContext()- """Dynamic segment of the prompt, to provide the LLM with relevant context"""-self.plan: list[str] = []"""List of steps that the Agent plans to take"""diff --git a/autogpt/commands/file_context.py b/autogpt/commands/file_context.pyindex e0b1d06a6f..bfcaba5eec 100644--- a/autogpt/commands/file_context.py+++ b/autogpt/commands/file_context.py@@ -7,9 +7,12 @@ COMMAND_CATEGORY_TITLE = "File Operations"import contextlibfrom pathlib import Path+from typing import TYPE_CHECKING-from autogpt.agents.agent import Agent-from autogpt.agents.utils.context import get_agent_context+if TYPE_CHECKING:+ from autogpt.agents import Agent, BaseAgent++from autogpt.agents.features.context import ContextMixin, get_agent_contextfrom autogpt.agents.utils.exceptions import (CommandExecutionError,DuplicateOperationError,@@ -20,6 +23,10 @@ from autogpt.models.context_item import FileContextItem, FolderContextItemfrom .decorators import sanitize_path_arg+def compatible_with_agent(agent: BaseAgent):+ return isinstance(agent, ContextMixin)++@command("open_file","Open a file for editing, creating it if it does not exist yet",@@ -30,6 +37,7 @@ from .decorators import sanitize_path_arg"required": True,}},+ available=compatible_with_agent,)@sanitize_path_arg("file_path")def open_file(file_path: Path, agent: Agent) -> tuple[str, FileContextItem]:@@ -78,6 +86,7 @@ def open_file(file_path: Path, agent: Agent) -> tuple[str, FileContextItem]:"required": True,}},+ available=compatible_with_agent,)@sanitize_path_arg("path")def open_folder(path: Path, agent: Agent) -> tuple[str, FolderContextItem]: