From ed5952782fbdf388f335768d12a958088c611d68 Mon Sep 17 00:00:00 2001 From: slavakurilyak Date: Tue, 4 Apr 2023 15:53:59 -0500 Subject: [PATCH 1/4] Improve key validation and handling in overwrite_memory and message_agent functions --- scripts/commands.py | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/scripts/commands.py b/scripts/commands.py index f8f96fe72..c1d5db9d3 100644 --- a/scripts/commands.py +++ b/scripts/commands.py @@ -16,6 +16,13 @@ from googleapiclient.errors import HttpError cfg = 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] = string print(_text) return _text else: - 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 response if cfg.speak_mode: From b418861d70399eb021c2bec71ff1b7bc7780155f Mon Sep 17 00:00:00 2001 From: slavakurilyak Date: Tue, 4 Apr 2023 20:53:41 -0500 Subject: [PATCH 2/4] Update message_agent function to support string keys --- scripts/commands.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/scripts/commands.py b/scripts/commands.py index c1d5db9d3..2adb84cf5 100644 --- a/scripts/commands.py +++ b/scripts/commands.py @@ -256,18 +256,20 @@ def start_agent(name, task, prompt, model=cfg.fast_llm_model): def message_agent(key, message): global cfg - + # 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) + if is_valid_int(key): + agent_response = agents.message_agent(int(key), message) + # Check if the key is a valid string + elif isinstance(key, str): + agent_response = agents.message_agent(key, message) + else: + return "Invalid key, must be an integer or a string." # Speak response if cfg.speak_mode: - speak.say_text(agent_response, 1) - - return f"Agent {key} responded: {agent_response}" + say.speak(agent_response) + return agent_response def list_agents(): From 962fc9a42a8d3e69b3a14f4791cf8a84b67f5504 Mon Sep 17 00:00:00 2001 From: Toran Bruce Richards Date: Thu, 6 Apr 2023 08:07:54 +0100 Subject: [PATCH 3/4] Changes string_key_memory to permanent_memory. Fixes: ```Command memory_ovr returned: Error: module 'memory' has no attribute 'string_key_memory'``` --- scripts/commands.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/commands.py b/scripts/commands.py index 2adb84cf5..f0e5911e8 100644 --- a/scripts/commands.py +++ b/scripts/commands.py @@ -218,7 +218,7 @@ def overwrite_memory(key, 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] = string + mem.permanent_memory[key] = string print(_text) return _text else: From dcc29a5568a9d026648657859a8ece1043430952 Mon Sep 17 00:00:00 2001 From: Toran Bruce Richards Date: Thu, 6 Apr 2023 08:29:28 +0100 Subject: [PATCH 4/4] Fixes broken reference to speak.say_text --- scripts/commands.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/commands.py b/scripts/commands.py index f0e5911e8..b0b2f3a6d 100644 --- a/scripts/commands.py +++ b/scripts/commands.py @@ -268,7 +268,7 @@ def message_agent(key, message): # Speak response if cfg.speak_mode: - say.speak(agent_response) + speak.say_text(agent_response, 1) return agent_response