diff options
Diffstat (limited to 'autogpts/autogpt/autogpt/agents/features/file_workspace.py')
-rw-r--r-- | autogpts/autogpt/autogpt/agents/features/file_workspace.py | 42 |
1 files changed, 25 insertions, 17 deletions
diff --git a/autogpts/autogpt/autogpt/agents/features/file_workspace.py b/autogpts/autogpt/autogpt/agents/features/file_workspace.py index ecdd28747..22ab8119d 100644 --- a/autogpts/autogpt/autogpt/agents/features/file_workspace.py +++ b/autogpts/autogpt/autogpt/agents/features/file_workspace.py @@ -5,11 +5,15 @@ from typing import TYPE_CHECKING if TYPE_CHECKING: from pathlib import Path - from ..base import BaseAgent + from ..base import BaseAgent, Config -from autogpt.file_workspace import FileWorkspace +from autogpt.file_workspace import ( + FileWorkspace, + FileWorkspaceBackendName, + get_workspace, +) -from ..base import AgentFileManager, BaseAgentConfiguration +from ..base import AgentFileManager, BaseAgentSettings class FileWorkspaceMixin: @@ -22,32 +26,36 @@ class FileWorkspaceMixin: # Initialize other bases first, because we need the config from BaseAgent super(FileWorkspaceMixin, self).__init__(**kwargs) - config: BaseAgentConfiguration = getattr(self, "config") - if not isinstance(config, BaseAgentConfiguration): - raise ValueError( - "Cannot initialize Workspace for Agent without compatible .config" - ) file_manager: AgentFileManager = getattr(self, "file_manager") if not file_manager: return - self.workspace = _setup_workspace(file_manager, config) + self._setup_workspace() def attach_fs(self, agent_dir: Path): res = super(FileWorkspaceMixin, self).attach_fs(agent_dir) - self.workspace = _setup_workspace(self.file_manager, self.config) + self._setup_workspace() return res + def _setup_workspace(self) -> None: + settings: BaseAgentSettings = getattr(self, "state") + assert settings.agent_id, "Cannot attach workspace to anonymous agent" + app_config: Config = getattr(self, "legacy_config") + file_manager: AgentFileManager = getattr(self, "file_manager") -def _setup_workspace(file_manager: AgentFileManager, config: BaseAgentConfiguration): - workspace = FileWorkspace( - file_manager.root / "workspace", - restrict_to_root=not config.allow_fs_access, - ) - workspace.initialize() - return workspace + ws_backend = app_config.workspace_backend + local = ws_backend == FileWorkspaceBackendName.LOCAL + workspace = get_workspace( + backend=ws_backend, + id=settings.agent_id if not local else "", + root_path=file_manager.root / "workspace" if local else None, + ) + if local and settings.config.allow_fs_access: + workspace._restrict_to_root = False # type: ignore + workspace.initialize() + self.workspace = workspace def get_agent_workspace(agent: BaseAgent) -> FileWorkspace | None: |