From 6a964123a079a0cb710dbbc9ace9339dbea47f42 Mon Sep 17 00:00:00 2001 From: Charles Packer Date: Sun, 17 Dec 2023 10:54:17 -0800 Subject: [PATCH] feat: added memgpt folder command (#632) * added memgpt folder command * comments --- memgpt/cli/cli.py | 11 ++++++++++- memgpt/main.py | 3 ++- memgpt/utils.py | 29 +++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/memgpt/cli/cli.py b/memgpt/cli/cli.py index 7cc2d58d57..9ad3adf24f 100644 --- a/memgpt/cli/cli.py +++ b/memgpt/cli/cli.py @@ -16,7 +16,7 @@ from memgpt.cli.cli_config import configure import memgpt.presets.presets as presets import memgpt.utils as utils -from memgpt.utils import printd +from memgpt.utils import printd, open_folder_in_explorer from memgpt.persistence_manager import LocalStateManager from memgpt.config import MemGPTConfig, AgentConfig from memgpt.constants import MEMGPT_DIR, CLI_WARNING_PREFIX @@ -25,6 +25,15 @@ from memgpt.server.constants import WS_DEFAULT_PORT, REST_DEFAULT_PORT +def open_folder(): + """Open a folder viewer of the MemGPT home directory""" + try: + print(f"Opening home folder: {MEMGPT_DIR}") + open_folder_in_explorer(MEMGPT_DIR) + except Exception as e: + print(f"Failed to open folder with system viewer, error:\n{e}") + + class ServerChoice(Enum): rest_api = "rest" ws_api = "websocket" diff --git a/memgpt/main.py b/memgpt/main.py index 01dd947c58..8bc3e00e30 100644 --- a/memgpt/main.py +++ b/memgpt/main.py @@ -21,7 +21,7 @@ import memgpt.agent as agent import memgpt.system as system import memgpt.constants as constants -from memgpt.cli.cli import run, attach, version, server +from memgpt.cli.cli import run, attach, version, server, open_folder from memgpt.cli.cli_config import configure, list, add from memgpt.cli.cli_load import app as load_app from memgpt.connectors.storage import StorageConnector @@ -34,6 +34,7 @@ app.command(name="list")(list) app.command(name="add")(add) app.command(name="server")(server) +app.command(name="folder")(open_folder) # load data commands app.add_typer(load_app, name="load") diff --git a/memgpt/utils.py b/memgpt/utils.py index d84c7f5240..ec62bd4394 100644 --- a/memgpt/utils.py +++ b/memgpt/utils.py @@ -2,6 +2,9 @@ import json import os import pickle +import platform +import subprocess + import difflib import demjson3 as demjson @@ -18,6 +21,32 @@ DEBUG = False +def open_folder_in_explorer(folder_path): + """ + Opens the specified folder in the system's native file explorer. + + :param folder_path: Absolute path to the folder to be opened. + """ + if not os.path.exists(folder_path): + raise ValueError(f"The specified folder {folder_path} does not exist.") + + # Determine the operating system + os_name = platform.system() + + # Open the folder based on the operating system + if os_name == "Windows": + # Windows: use 'explorer' command + subprocess.run(["explorer", folder_path], check=True) + elif os_name == "Darwin": + # macOS: use 'open' command + subprocess.run(["open", folder_path], check=True) + elif os_name == "Linux": + # Linux: use 'xdg-open' command (works for most Linux distributions) + subprocess.run(["xdg-open", folder_path], check=True) + else: + raise OSError(f"Unsupported operating system {os_name}.") + + # Custom unpickler class OpenAIBackcompatUnpickler(pickle.Unpickler): def find_class(self, module, name):