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.
Adds config singleton.
Torantulino406b63d04d26
diff --git a/AutonomousAI/config.py b/AutonomousAI/config.pynew file mode 100644index 0000000000..f7e73aad05--- /dev/null+++ b/AutonomousAI/config.py@@ -0,0 +1,27 @@+class Singleton(type):+ """+ Singleton metaclass for ensuring only one instance of a class.+ """++ _instances = {}++ def __call__(cls, *args, **kwargs):+ if cls not in cls._instances:+ cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)+ return cls._instances[cls]+++class Config(metaclass=Singleton):+ """+ Configuration class to store the state of bools for different scripts access.+ """++ def __init__(self):+ self.continuous_mode = False+ self.speak_mode = False++ def set_continuous_mode(self, value: bool):+ self.continuous_mode = value++ def set_speak_mode(self, value: bool):+ self.speak_mode = value\ No newline at end of filediff --git a/AutonomousAI/main.py b/AutonomousAI/main.pyindex 9501c3967f..63320e1c12 100644--- a/AutonomousAI/main.py+++ b/AutonomousAI/main.py@@ -10,6 +10,7 @@ import timeimport speakfrom enum import Enum, autoimport sys+from config import Configclass Argument(Enum):CONTINUOUS_MODE = "continuous-mode"@@ -34,6 +35,7 @@ def print_to_console(title, title_color, content, speak_text = False, min_typingdef print_assistant_thoughts(assistant_reply):global ai_name+ global cfgtry:# Parse and print Assistant responseassistant_reply_json = json.loads(assistant_reply)@@ -69,7 +71,7 @@ def print_assistant_thoughts(assistant_reply):print_to_console("CRITICISM:", Fore.YELLOW, assistant_thoughts_criticism)# Speak the assistant's thoughts- if speak_mode and assistant_thoughts_speak:+ if cfg.speak_mode and assistant_thoughts_speak:speak.say_text(assistant_thoughts_speak)except json.decoder.JSONDecodeError:@@ -122,20 +124,20 @@ def construct_prompt():# Check if the python script was executed with arguments, get those argumentsdef parse_arguments():- global continuous_mode- global speak_mode-+ global cfg+ cfg.set_continuous_mode(False)+ cfg.set_speak_mode(False)for arg in sys.argv[1:]:if arg == Argument.CONTINUOUS_MODE.value:print_to_console("Continuous Mode: ", Fore.RED, "ENABLED")print_to_console("WARNING: ", Fore.RED, "Continuous mode is not recommended. It is potentially dangerous and may cause your AI to run forever or carry out actions you would not usually authorise. Use at your own risk.")- continuous_mode = True+ cfg.set_continuous_mode(True)elif arg == Argument.SPEAK_MODE.value:print_to_console("Speak Mode: ", Fore.GREEN, "ENABLED")- speak_mode = True+ cfg.set_speak_mode(True)++cfg = Config()-continuous_mode = False-speak_mode = Falseparse_arguments()ai_name = ""prompt = construct_prompt()@@ -161,7 +163,7 @@ while True:print_to_console("Error: \n", Fore.RED, str(e))- if not continuous_mode:+ if not cfg.continuous_mode:### GET USER AUTHORIZATION TO EXECUTE COMMAND #### Get key press: Prompt the user to press enter to continue or escape to exituser_input = ""