Skip to content

Commit

Permalink
sort agents by directory-last-modified time (#574)
Browse files Browse the repository at this point in the history
* sort agents by directory-last-modified time

* only save agent config when agent is saved

---------

Co-authored-by: Sarah Wooders <[email protected]>
  • Loading branch information
cpacker and sarahwooders authored Dec 4, 2023
1 parent 60d2b06 commit 91a9b72
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
3 changes: 3 additions & 0 deletions memgpt/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,9 @@ def save(self):
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)
Expand Down
2 changes: 0 additions & 2 deletions memgpt/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,6 @@ def __init__(
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
Expand Down
22 changes: 18 additions & 4 deletions memgpt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down

0 comments on commit 91a9b72

Please sign in to comment.