Bitsak/AutoGPT2
0
1from __future__ import annotations2 3import os4from pathlib import Path5 6from autogpt.config import Config7 8CFG = Config()9 10# Set a dedicated folder for file I/O11WORKSPACE_PATH = Path(os.getcwd()) / "auto_gpt_workspace"12 13# Create the directory if it doesn't exist14if not os.path.exists(WORKSPACE_PATH):15 os.makedirs(WORKSPACE_PATH)16 17 18def path_in_workspace(relative_path: str | Path) -> Path:19 """Get full path for item in workspace20 21 Parameters:22 relative_path (str | Path): Path to translate into the workspace23 24 Returns:25 Path: Absolute path for the given path in the workspace26 """27 return safe_path_join(WORKSPACE_PATH, relative_path)28 29 30def safe_path_join(base: Path, *paths: str | Path) -> Path:31 """Join one or more path components, asserting the resulting path is within the workspace.32 33 Args:34 base (Path): The base path35 *paths (str): The paths to join to the base path36 37 Returns:38 Path: The joined path39 """40 joined_path = base.joinpath(*paths).resolve()41 42 if CFG.restrict_to_workspace and not joined_path.is_relative_to(base):43 raise ValueError(44 f"Attempted to access path '{joined_path}' outside of workspace '{base}'."45 )46 47 return joined_path48 