AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
Improve key validation and handling in overwrite_memory and message_agent functions
slavakurilyaked5952782fbd
diff --git a/scripts/commands.py b/scripts/commands.pyindex f8f96fe72c..c1d5db9d30 100644--- a/scripts/commands.py+++ b/scripts/commands.py@@ -16,6 +16,13 @@ from googleapiclient.errors import HttpErrorcfg = Config()+def is_valid_int(value):+ try:+ int(value)+ return True+ except ValueError:+ return False+def get_command(response):try:response_json = fix_and_parse_json(response)@@ -194,14 +201,28 @@ def delete_memory(key):def overwrite_memory(key, string):- if int(key) >= 0 and key < len(mem.permanent_memory):- _text = "Overwriting memory with key " + \- str(key) + " and string " + string- mem.permanent_memory[key] = string+ # Check if the key is a valid integer+ if is_valid_int(key):+ key_int = int(key)+ # Check if the integer key is within the range of the permanent_memory list+ if 0 <= key_int < len(mem.permanent_memory):+ _text = "Overwriting memory with key " + str(key) + " and string " + string+ # Overwrite the memory slot with the given integer key and string+ mem.permanent_memory[key_int] = string+ print(_text)+ return _text+ else:+ print(f"Invalid key '{key}', out of range.")+ return None+ # Check if the key is a valid string+ elif isinstance(key, str):+ _text = "Overwriting memory with key " + key + " and string " + string+ # Overwrite the memory slot with the given string key and string+ mem.string_key_memory[key] = stringprint(_text)return _textelse:- print("Invalid key, cannot overwrite memory.")+ print(f"Invalid key '{key}', must be an integer or a string.")return None@@ -235,7 +256,12 @@ def start_agent(name, task, prompt, model=cfg.fast_llm_model):def message_agent(key, message):global cfg- agent_response = agents.message_agent(key, message)++ # Check if the key is a valid integer+ if not is_valid_int(key):+ return "Invalid key, cannot message agent."++ agent_response = agents.message_agent(int(key), message)# Speak responseif cfg.speak_mode: