import json def is_valid_int(value: str) -> bool: """Check if the value is a valid integer Args: value (str): The value to check Returns: bool: True if the value is a valid integer, False otherwise """ try: int(value) return True except ValueError: return False def get_command(response_json: Dict): """Parse the response and return the command name and arguments Args: response_json (json): The response from the AI Returns: tuple: The command name and arguments Raises: json.decoder.JSONDecodeError: If the response is not valid JSON Exception: If any other error occurs """ try: if "command" not in response_json: return "Error:", "Missing 'command' object in JSON" if not isinstance(response_json, dict): return "Error:", f"'response_json' object is not dictionary {response_json}" command = response_json["command"] if not isinstance(command, dict): return "Error:", "'command' object is not a dictionary" if "name" not in command: return "Error:", "Missing 'name' field in 'command' object" command_name = command["name"] # Use an empty dictionary if 'args' field is not present in 'command' object arguments = command.get("args", {}) return command_name, arguments except json.decoder.JSONDecodeError: return "Error:", "Invalid JSON" # All other errors, return "Error: + error message" except Exception as e: return "Error:", str(e) def execute_command( command_registry: CommandRegistry, command_name: str, arguments, prompt: PromptGenerator, ): """Execute the command and return the result Args: command_name (str): The name of the command to execute arguments (dict): The arguments for the command Returns: str: The result of the command """ try: cmd = command_registry.commands.get(command_name) # If the command is found, call it with the provided arguments if cmd: return cmd(**arguments) # TODO: Remove commands below after they are moved to the command registry. command_name = map_command_synonyms(command_name.lower()) if command_name == "memory_add": return get_memory(CFG).add(arguments["string"]) # TODO: Change these to take in a file rather than pasted code, if # non-file is given, return instructions "Input should be a python # filepath, write your code to file and try again else: for command in prompt.commands: if ( command_name == command["label"].lower() or command_name == command["name"].lower() ): return command["function"](**arguments) return ( f"Unknown command '{command_name}'. Please refer to the 'COMMANDS'" " list for available commands and only respond in the specified JSON" " format." ) except Exception as e: return f"Error: {str(e)}"