Skip to content

armgit / letta

Public Imported from GitHub
0

Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.

feat: Properly handle old config migration/recreation on `memgpt run` (#804)

Charles Packer1af40860fd7b
diff --git a/memgpt/cli/cli.py b/memgpt/cli/cli.py
index 357b7047a..23cce6652 100644
--- a/memgpt/cli/cli.py
+++ b/memgpt/cli/cli.py
@@ -322,6 +322,37 @@ def run(
logger.setLevel(logging.DEBUG)
else:
logger.setLevel(logging.CRITICAL)
+
+ from memgpt.migrate import config_is_compatible, wipe_config_and_reconfigure, VERSION_CUTOFF
+
+ if not config_is_compatible(allow_empty=True):
+ typer.secho(f"\nYour current config file is incompatible with MemGPT versions later than {VERSION_CUTOFF}\n", fg=typer.colors.RED)
+ choices = [
+ "Run the full config setup (recommended)",
+ "Create a new config using defaults",
+ "Cancel",
+ ]
+ selection = questionary.select(
+ f"To use MemGPT, you must either downgrade your MemGPT version (<= {VERSION_CUTOFF}), or regenerate your config. Would you like to proceed?",
+ choices=choices,
+ default=choices[0],
+ ).ask()
+ if selection == choices[0]:
+ try:
+ wipe_config_and_reconfigure()
+ except Exception as e:
+ typer.secho(f"Fresh config generation failed - error:\n{e}", fg=typer.colors.RED)
+ raise
+ elif selection == choices[1]:
+ try:
+ wipe_config_and_reconfigure(run_configure=False)
+ except Exception as e:
+ typer.secho(f"Fresh config generation failed - error:\n{e}", fg=typer.colors.RED)
+ raise
+ else:
+ typer.secho("Migration cancelled (to migrate old agents, run `memgpt migrate`)", fg=typer.colors.RED)
+ raise KeyboardInterrupt()
+
if not MemGPTConfig.exists():
# if no config, ask about quickstart
# do you want to do:
@@ -394,6 +425,8 @@ def run(
if len(agents) > 0 and not any([persona, human, model]):
print()
select_agent = questionary.confirm("Would you like to select an existing agent?").ask()
+ if select_agent is None:
+ raise KeyboardInterrupt
if select_agent:
agent = questionary.select("Select agent:", choices=agents).ask()
diff --git a/memgpt/config.py b/memgpt/config.py
index 57e324e78..a0ffd86b2 100644
--- a/memgpt/config.py
+++ b/memgpt/config.py
@@ -5,6 +5,8 @@ import os
import uuid
from dataclasses import dataclass
import configparser
+import typer
+import questionary
import memgpt
import memgpt.utils as utils
@@ -221,6 +223,18 @@ class MemGPTConfig:
@classmethod
def load(cls) -> "MemGPTConfig":
+ # avoid circular import
+ from memgpt.migrate import config_is_compatible, VERSION_CUTOFF
+
+ if not config_is_compatible(allow_empty=True):
+ error_message = " ".join(
+ [
+ f"\nYour current config file is incompatible with MemGPT versions later than {VERSION_CUTOFF}.",
+ f"\nTo use MemGPT, you must either downgrade your MemGPT version (<= {VERSION_CUTOFF}) or regenerate your config using `memgpt configure`, or `memgpt migrate` if you would like to migrate old agents.",
+ ]
+ )
+ raise ValueError(error_message)
+
config = configparser.ConfigParser()
# allow overriding with env variables
@@ -277,6 +291,7 @@ class MemGPTConfig:
config = cls(anon_clientid=anon_clientid, config_path=config_path)
config.create_config_dir() # create dirs
config.save() # save updated config
+
return config
def save(self):
diff --git a/memgpt/migrate.py b/memgpt/migrate.py
index c6993dd53..d0e9ccc92 100644
--- a/memgpt/migrate.py
+++ b/memgpt/migrate.py
@@ -33,9 +33,13 @@ VERSION_CUTOFF = "0.2.12"
MIGRATION_BACKUP_FOLDER = "migration_backups"
-def wipe_config_and_reconfigure():
+def wipe_config_and_reconfigure(run_configure=True):
"""Wipe (backup) the config file, and launch `memgpt configure`"""
+ if not os.path.exists(os.path.join(MEMGPT_DIR, MIGRATION_BACKUP_FOLDER)):
+ os.makedirs(os.path.join(MEMGPT_DIR, MIGRATION_BACKUP_FOLDER))
+ os.makedirs(os.path.join(MEMGPT_DIR, MIGRATION_BACKUP_FOLDER, "agents"))
+
# Get the current timestamp in a readable format (e.g., YYYYMMDD_HHMMSS)
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
@@ -51,15 +55,21 @@ def wipe_config_and_reconfigure():
else:
typer.secho(f"Couldn't find an existing config file to delete", fg=typer.colors.RED)
- # Run configure
- configure()
+ if run_configure:
+ # Either run configure
+ configure()
+ else:
+ # Or create a new config with defaults
+ MemGPTConfig.load()
-def config_is_compatible() -> bool:
+def config_is_compatible(allow_empty=False, echo=False) -> bool:
"""Check if the config is OK to use with 0.2.12, or if it needs to be deleted"""
# NOTE: don't use built-in load(), since that will apply defaults
# memgpt_config = MemGPTConfig.load()
memgpt_config_file = os.path.join(MEMGPT_DIR, "config")
+ if not os.path.exists(memgpt_config_file):
+ return True if allow_empty else False
parser = configparser.ConfigParser()
parser.read(memgpt_config_file)
@@ -69,13 +79,16 @@ def config_is_compatible() -> bool:
version = None
if version is None:
- typer.secho(f"Current config version is missing", fg=typer.colors.RED)
+ if echo:
+ typer.secho(f"Current config version is missing", fg=typer.colors.RED)
return False
elif version_less_than(version, VERSION_CUTOFF):
- typer.secho(f"Current config version ({version}) is older than cutoff ({VERSION_CUTOFF})", fg=typer.colors.RED)
+ if echo:
+ typer.secho(f"Current config version ({version}) is older than migration cutoff ({VERSION_CUTOFF})", fg=typer.colors.RED)
return False
else:
- typer.secho(f"Current config version {version} is compatible!", fg=typer.colors.GREEN)
+ if echo:
+ typer.secho(f"Current config version {version} is compatible!", fg=typer.colors.GREEN)
return True
@@ -330,7 +343,7 @@ def migrate_all_agents(stop_on_fail: bool = False) -> dict:
os.makedirs(os.path.join(MEMGPT_DIR, MIGRATION_BACKUP_FOLDER))
os.makedirs(os.path.join(MEMGPT_DIR, MIGRATION_BACKUP_FOLDER, "agents"))
- if not config_is_compatible():
+ if not config_is_compatible(echo=True):
typer.secho(f"Your current config file is incompatible with MemGPT versions >= {VERSION_CUTOFF}", fg=typer.colors.RED)
if questionary.confirm(
"To migrate old MemGPT agents, you must delete your config file and run `memgpt configure`. Would you like to proceed?"