From f2a2942c661d2d2f2c9fb4ee1d41137f7eca31c8 Mon Sep 17 00:00:00 2001 From: Charles Packer Date: Mon, 4 Dec 2023 14:16:03 -0800 Subject: [PATCH] sort agents by directory-last-modified time (#574) * sort agents by directory-last-modified time * only save agent config when agent is saved --------- Co-authored-by: Sarah Wooders --- memgpt/agent.py | 3 +++ memgpt/config.py | 2 -- memgpt/utils.py | 22 ++++++++++++++++++---- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/memgpt/agent.py b/memgpt/agent.py index 0508b4df..44540be6 100644 --- a/memgpt/agent.py +++ b/memgpt/agent.py @@ -259,6 +259,9 @@ class Agent(object): timestamp = get_local_time().replace(" ", "_").replace(":", "_") agent_name = self.config.name # TODO: fix + # save config + self.config.save() + # save agent state filename = f"{timestamp}.json" os.makedirs(self.config.save_state_dir(), exist_ok=True) diff --git a/memgpt/config.py b/memgpt/config.py index 39af5c19..63c5fc1c 100644 --- a/memgpt/config.py +++ b/memgpt/config.py @@ -284,8 +284,6 @@ class AgentConfig: self.agent_config_path = ( os.path.join(MEMGPT_DIR, "agents", self.name, "config.json") if agent_config_path is None else agent_config_path ) - # assert not os.path.exists(self.agent_config_path), f"Agent config file already exists at {self.agent_config_path}" - self.save() def generate_agent_id(self, length=6): ## random character based diff --git a/memgpt/utils.py b/memgpt/utils.py index c9c57529..7b0ea1b8 100644 --- a/memgpt/utils.py +++ b/memgpt/utils.py @@ -88,11 +88,25 @@ def parse_json(string): raise e -def list_agent_config_files(): +def list_agent_config_files(sort="last_modified"): """List all agent config files, ignoring dotfiles.""" - files = os.listdir(os.path.join(MEMGPT_DIR, "agents")) - # remove dotfiles like .DS_Store - return [file for file in files if not file.startswith(".")] + agent_dir = os.path.join(MEMGPT_DIR, "agents") + files = os.listdir(agent_dir) + + # Remove dotfiles like .DS_Store + files = [file for file in files if not file.startswith(".")] + + # Remove anything that's not a directory + files = [file for file in files if os.path.isdir(os.path.join(agent_dir, file))] + + if sort is not None: + if sort == "last_modified": + # Sort the directories by last modified (most recent first) + files.sort(key=lambda x: os.path.getmtime(os.path.join(agent_dir, x)), reverse=True) + else: + raise ValueError(f"Unrecognized sorting option {sort}") + + return files def list_human_files():