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.
Move more app files to app package (#5036)
James Collinsa593c3272735
diff --git a/autogpt/app/main.py b/autogpt/app/main.pyindex e7d51ce636..ed77cd4381 100644--- a/autogpt/app/main.py+++ b/autogpt/app/main.py@@ -13,6 +13,14 @@ from colorama import Fore, Stylefrom autogpt.agents import Agent, AgentThoughts, CommandArgs, CommandNamefrom autogpt.app.configurator import create_configfrom autogpt.app.setup import prompt_user+from autogpt.app.spinner import Spinner+from autogpt.app.utils import (+ clean_input,+ get_current_git_branch,+ get_latest_bulletin,+ get_legal_warning,+ markdown_to_ansi_style,+)from autogpt.commands import COMMAND_CATEGORIESfrom autogpt.config import AIConfig, Config, ConfigBuilder, check_openai_api_keyfrom autogpt.llm.api_manager import ApiManager@@ -22,14 +30,6 @@ from autogpt.models.command_registry import CommandRegistryfrom autogpt.plugins import scan_pluginsfrom autogpt.prompts.prompt import DEFAULT_TRIGGERING_PROMPTfrom autogpt.speech import say_text-from autogpt.spinner import Spinner-from autogpt.utils import (- clean_input,- get_current_git_branch,- get_latest_bulletin,- get_legal_warning,- markdown_to_ansi_style,-)from autogpt.workspace import Workspacefrom scripts.install_plugin_deps import install_plugin_dependenciesdiff --git a/autogpt/app/setup.py b/autogpt/app/setup.pyindex cb6073adc0..f2879af6b3 100644--- a/autogpt/app/setup.py+++ b/autogpt/app/setup.py@@ -5,7 +5,7 @@ from typing import Optionalfrom colorama import Fore, Stylefrom jinja2 import Template-from autogpt import utils+from autogpt.app import utilsfrom autogpt.config import Configfrom autogpt.config.ai_config import AIConfigfrom autogpt.llm.base import ChatSequence, Messagediff --git a/autogpt/spinner.py b/autogpt/app/spinner.pysimilarity index 100%rename from autogpt/spinner.pyrename to autogpt/app/spinner.pydiff --git a/autogpt/app/utils.py b/autogpt/app/utils.pynew file mode 100644index 0000000000..5bf0d6c7cc--- /dev/null+++ b/autogpt/app/utils.py@@ -0,0 +1,147 @@+import os+import re++import requests+from colorama import Fore, Style+from git.repo import Repo+from prompt_toolkit import ANSI, PromptSession+from prompt_toolkit.history import InMemoryHistory++from autogpt.config import Config+from autogpt.logs import logger++session = PromptSession(history=InMemoryHistory())+++def clean_input(config: Config, prompt: str = "", talk=False):+ try:+ if config.chat_messages_enabled:+ for plugin in config.plugins:+ if not hasattr(plugin, "can_handle_user_input"):+ continue+ if not plugin.can_handle_user_input(user_input=prompt):+ continue+ plugin_response = plugin.user_input(user_input=prompt)+ if not plugin_response:+ continue+ if plugin_response.lower() in [+ "yes",+ "yeah",+ "y",+ "ok",+ "okay",+ "sure",+ "alright",+ ]:+ return config.authorise_key+ elif plugin_response.lower() in [+ "no",+ "nope",+ "n",+ "negative",+ ]:+ return config.exit_key+ return plugin_response++ # ask for input, default when just pressing Enter is y+ logger.info("Asking user via keyboard...")++ # handle_sigint must be set to False, so the signal handler in the+ # autogpt/main.py could be employed properly. This referes to+ # https://github.com/Significant-Gravitas/Auto-GPT/pull/4799/files/3966cdfd694c2a80c0333823c3bc3da090f85ed3#r1264278776+ answer = session.prompt(ANSI(prompt), handle_sigint=False)+ return answer+ except KeyboardInterrupt:+ logger.info("You interrupted Auto-GPT")+ logger.info("Quitting...")+ exit(0)+++def get_bulletin_from_web():+ try:+ response = requests.get(+ "https://raw.githubusercontent.com/Significant-Gravitas/Auto-GPT/master/BULLETIN.md"+ )+ if response.status_code == 200:+ return response.text+ except requests.exceptions.RequestException:+ pass++ return ""+++def get_current_git_branch() -> str:+ try:+ repo = Repo(search_parent_directories=True)+ branch = repo.active_branch+ return branch.name+ except:+ return ""+++def get_latest_bulletin() -> tuple[str, bool]:+ exists = os.path.exists("data/CURRENT_BULLETIN.md")+ current_bulletin = ""+ if exists:+ current_bulletin = open(+ "data/CURRENT_BULLETIN.md", "r", encoding="utf-8"+ ).read()+ new_bulletin = get_bulletin_from_web()+ is_new_news = new_bulletin != "" and new_bulletin != current_bulletin++ news_header = Fore.YELLOW + "Welcome to Auto-GPT!\n"+ if new_bulletin or current_bulletin:+ news_header += (+ "Below you'll find the latest Auto-GPT News and updates regarding features!\n"+ "If you don't wish to see this message, you "+ "can run Auto-GPT with the *--skip-news* flag.\n"+ )++ if new_bulletin and is_new_news:+ open("data/CURRENT_BULLETIN.md", "w", encoding="utf-8").write(new_bulletin)+ current_bulletin = f"{Fore.RED}::NEW BULLETIN::{Fore.RESET}\n\n{new_bulletin}"++ return f"{news_header}\n{current_bulletin}", is_new_news+++def markdown_to_ansi_style(markdown: str):+ ansi_lines: list[str] = []+ for line in markdown.split("\n"):+ line_style = ""++ if line.startswith("# "):+ line_style += Style.BRIGHT+ else:+ line = re.sub(+ r"(?<!\*)\*(\*?[^*]+\*?)\*(?!\*)",+ rf"{Style.BRIGHT}\1{Style.NORMAL}",+ line,+ )++ if re.match(r"^#+ ", line) is not None:+ line_style += Fore.CYAN+ line = re.sub(r"^#+ ", "", line)++ ansi_lines.append(f"{line_style}{line}{Style.RESET_ALL}")+ return "\n".join(ansi_lines)+++def get_legal_warning() -> str:+ legal_text = """+## DISCLAIMER AND INDEMNIFICATION AGREEMENT+### PLEASE READ THIS DISCLAIMER AND INDEMNIFICATION AGREEMENT CAREFULLY BEFORE USING THE AUTOGPT SYSTEM. BY USING THE AUTOGPT SYSTEM, YOU AGREE TO BE BOUND BY THIS AGREEMENT.++## Introduction+AutoGPT (the "System") is a project that connects a GPT-like artificial intelligence system to the internet and allows it to automate tasks. While the System is designed to be useful and efficient, there may be instances where the System could perform actions that may cause harm or have unintended consequences.++## No Liability for Actions of the System+The developers, contributors, and maintainers of the AutoGPT project (collectively, the "Project Parties") make no warranties or representations, express or implied, about the System's performance, accuracy, reliability, or safety. By using the System, you understand and agree that the Project Parties shall not be liable for any actions taken by the System or any consequences resulting from such actions.++## User Responsibility and Respondeat Superior Liability+As a user of the System, you are responsible for supervising and monitoring the actions of the System while it is operating on your+behalf. You acknowledge that using the System could expose you to potential liability including but not limited to respondeat superior and you agree to assume all risks and liabilities associated with such potential liability.++## Indemnification+By using the System, you agree to indemnify, defend, and hold harmless the Project Parties from and against any and all claims, liabilities, damages, losses, or expenses (including reasonable attorneys' fees and costs) arising out of or in connection with your use of the System, including, without limitation, any actions taken by the System on your behalf, any failure to properly supervise or monitor the System, and any resulting harm or unintended consequences.+ """+ return legal_textdiff --git a/autogpt/processing/text.py b/autogpt/processing/text.pyindex faaa50e000..dc245bb2ae 100644--- a/autogpt/processing/text.py+++ b/autogpt/processing/text.py@@ -10,7 +10,15 @@ from autogpt.llm.base import ChatSequencefrom autogpt.llm.providers.openai import OPEN_AI_MODELSfrom autogpt.llm.utils import count_string_tokens, create_chat_completionfrom autogpt.logs import logger-from autogpt.utils import batch+++def batch(iterable, max_batch_length: int, overlap: int = 0):+ """Batch data from iterable into slices of length N. The last batch may be shorter."""+ # batched('ABCDEFG', 3) --> ABC DEF G+ if max_batch_length < 1:+ raise ValueError("n must be at least one")+ for i in range(0, len(iterable), max_batch_length - overlap):+ yield iterable[i : i + max_batch_length]def _max_chunk_length(model: str, max: Optional[int] = None) -> int:diff --git a/autogpt/utils.py b/autogpt/utils.pyindex 28c4be517f..f69fe50f86 100644--- a/autogpt/utils.py+++ b/autogpt/utils.py@@ -1,70 +1,5 @@-import os-import re--import requestsimport yaml-from colorama import Fore, Style-from git.repo import Repo-from prompt_toolkit import ANSI, PromptSession-from prompt_toolkit.history import InMemoryHistory--from autogpt.config import Config-from autogpt.logs import logger--session = PromptSession(history=InMemoryHistory())---def batch(iterable, max_batch_length: int, overlap: int = 0):- """Batch data from iterable into slices of length N. The last batch may be shorter."""- # batched('ABCDEFG', 3) --> ABC DEF G- if max_batch_length < 1:- raise ValueError("n must be at least one")- for i in range(0, len(iterable), max_batch_length - overlap):- yield iterable[i : i + max_batch_length]---def clean_input(config: Config, prompt: str = "", talk=False):- try:- if config.chat_messages_enabled:- for plugin in config.plugins:- if not hasattr(plugin, "can_handle_user_input"):- continue- if not plugin.can_handle_user_input(user_input=prompt):- continue- plugin_response = plugin.user_input(user_input=prompt)- if not plugin_response:- continue- if plugin_response.lower() in [- "yes",- "yeah",- "y",- "ok",- "okay",- "sure",- "alright",- ]:- return config.authorise_key- elif plugin_response.lower() in [- "no",- "nope",- "n",- "negative",- ]:- return config.exit_key- return plugin_response-- # ask for input, default when just pressing Enter is y- logger.info("Asking user via keyboard...")-- # handle_sigint must be set to False, so the signal handler in the- # autogpt/main.py could be employed properly. This referes to- # https://github.com/Significant-Gravitas/Auto-GPT/pull/4799/files/3966cdfd694c2a80c0333823c3bc3da090f85ed3#r1264278776- answer = session.prompt(ANSI(prompt), handle_sigint=False)- return answer- except KeyboardInterrupt:- logger.info("You interrupted Auto-GPT")- logger.info("Quitting...")- exit(0)+from colorama import Foredef validate_yaml_file(file: str):@@ -80,106 +15,3 @@ def validate_yaml_file(file: str):)return (True, f"Successfully validated {Fore.CYAN}`{file}`{Fore.RESET}!")---def readable_file_size(size, decimal_places=2):- """Converts the given size in bytes to a readable format.- Args:- size: Size in bytes- decimal_places (int): Number of decimal places to display- """- for unit in ["B", "KB", "MB", "GB", "TB"]:- if size < 1024.0:- break- size /= 1024.0- return f"{size:.{decimal_places}f} {unit}"---def get_bulletin_from_web():- try:- response = requests.get(- "https://raw.githubusercontent.com/Significant-Gravitas/Auto-GPT/master/BULLETIN.md"- )- if response.status_code == 200:- return response.text- except requests.exceptions.RequestException:- pass-- return ""---def get_current_git_branch() -> str:- try:- repo = Repo(search_parent_directories=True)- branch = repo.active_branch- return branch.name- except:- return ""---def get_latest_bulletin() -> tuple[str, bool]:- exists = os.path.exists("data/CURRENT_BULLETIN.md")- current_bulletin = ""- if exists:- current_bulletin = open(- "data/CURRENT_BULLETIN.md", "r", encoding="utf-8"- ).read()- new_bulletin = get_bulletin_from_web()- is_new_news = new_bulletin != "" and new_bulletin != current_bulletin-- news_header = Fore.YELLOW + "Welcome to Auto-GPT!\n"- if new_bulletin or current_bulletin:- news_header += (- "Below you'll find the latest Auto-GPT News and updates regarding features!\n"- "If you don't wish to see this message, you "- "can run Auto-GPT with the *--skip-news* flag.\n"- )-- if new_bulletin and is_new_news:- open("data/CURRENT_BULLETIN.md", "w", encoding="utf-8").write(new_bulletin)- current_bulletin = f"{Fore.RED}::NEW BULLETIN::{Fore.RESET}\n\n{new_bulletin}"-- return f"{news_header}\n{current_bulletin}", is_new_news---def markdown_to_ansi_style(markdown: str):- ansi_lines: list[str] = []- for line in markdown.split("\n"):- line_style = ""-- if line.startswith("# "):- line_style += Style.BRIGHT- else:- line = re.sub(- r"(?<!\*)\*(\*?[^*]+\*?)\*(?!\*)",- rf"{Style.BRIGHT}\1{Style.NORMAL}",- line,- )-- if re.match(r"^#+ ", line) is not None:- line_style += Fore.CYAN- line = re.sub(r"^#+ ", "", line)-- ansi_lines.append(f"{line_style}{line}{Style.RESET_ALL}")- return "\n".join(ansi_lines)---def get_legal_warning() -> str:- legal_text = """-## DISCLAIMER AND INDEMNIFICATION AGREEMENT-### PLEASE READ THIS DISCLAIMER AND INDEMNIFICATION AGREEMENT CAREFULLY BEFORE USING THE AUTOGPT SYSTEM. BY USING THE AUTOGPT SYSTEM, YOU AGREE TO BE BOUND BY THIS AGREEMENT.--## Introduction-AutoGPT (the "System") is a project that connects a GPT-like artificial intelligence system to the internet and allows it to automate tasks. While the System is designed to be useful and efficient, there may be instances where the System could perform actions that may cause harm or have unintended consequences.--## No Liability for Actions of the System-The developers, contributors, and maintainers of the AutoGPT project (collectively, the "Project Parties") make no warranties or representations, express or implied, about the System's performance, accuracy, reliability, or safety. By using the System, you understand and agree that the Project Parties shall not be liable for any actions taken by the System or any consequences resulting from such actions.--## User Responsibility and Respondeat Superior Liability-As a user of the System, you are responsible for supervising and monitoring the actions of the System while it is operating on your-behalf. You acknowledge that using the System could expose you to potential liability including but not limited to respondeat superior and you agree to assume all risks and liabilities associated with such potential liability.--## Indemnification-By using the System, you agree to indemnify, defend, and hold harmless the Project Parties from and against any and all claims, liabilities, damages, losses, or expenses (including reasonable attorneys' fees and costs) arising out of or in connection with your use of the System, including, without limitation, any actions taken by the System on your behalf, any failure to properly supervise or monitor the System, and any resulting harm or unintended consequences.- """- return legal_textdiff --git a/tests/challenges/utils.py b/tests/challenges/utils.pyindex dd661b6e3f..67d7425c8d 100644--- a/tests/challenges/utils.py+++ b/tests/challenges/utils.py@@ -38,7 +38,9 @@ def setup_mock_input(monkeypatch: pytest.MonkeyPatch, cycle_count: int) -> None:yield from input_sequencegen = input_generator()- monkeypatch.setattr("autogpt.utils.session.prompt", lambda _, **kwargs: next(gen))+ monkeypatch.setattr(+ "autogpt.app.utils.session.prompt", lambda _, **kwargs: next(gen)+ )def setup_mock_log_cycle_agent_name(diff --git a/tests/integration/test_setup.py b/tests/integration/test_setup.pyindex f4bb9a5c8b..ff83eee6b5 100644--- a/tests/integration/test_setup.py+++ b/tests/integration/test_setup.py@@ -10,7 +10,7 @@ from autogpt.config.ai_config import AIConfig@pytest.mark.requires_openai_api_keydef test_generate_aiconfig_automatic_default(patched_api_requestor, config):user_inputs = [""]- with patch("autogpt.utils.session.prompt", side_effect=user_inputs):+ with patch("autogpt.app.utils.session.prompt", side_effect=user_inputs):ai_config = prompt_user(config)assert isinstance(ai_config, AIConfig)@@ -43,7 +43,7 @@ def test_generate_aiconfig_automatic_fallback(patched_api_requestor, config):"","",]- with patch("autogpt.utils.session.prompt", side_effect=user_inputs):+ with patch("autogpt.app.utils.session.prompt", side_effect=user_inputs):ai_config = prompt_user(config)assert isinstance(ai_config, AIConfig)@@ -64,7 +64,7 @@ def test_prompt_user_manual_mode(patched_api_requestor, config):"","",]- with patch("autogpt.utils.session.prompt", side_effect=user_inputs):+ with patch("autogpt.app.utils.session.prompt", side_effect=user_inputs):ai_config = prompt_user(config)assert isinstance(ai_config, AIConfig)diff --git a/tests/unit/test_spinner.py b/tests/unit/test_spinner.pyindex 4b22f24cbd..8f894b79d5 100644--- a/tests/unit/test_spinner.py+++ b/tests/unit/test_spinner.py@@ -1,7 +1,7 @@# Generated by CodiumAIimport time-from autogpt.spinner import Spinner+from autogpt.app.spinner import Spinner"""Code Analysisdiff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.pyindex eb49908f39..43d8dc39ed 100644--- a/tests/unit/test_utils.py+++ b/tests/unit/test_utils.py@@ -4,15 +4,14 @@ from unittest.mock import patchimport pytestimport requests-from autogpt.config import Config-from autogpt.json_utils.utilities import extract_dict_from_response, validate_dict-from autogpt.utils import (+from autogpt.app.utils import (get_bulletin_from_web,get_current_git_branch,get_latest_bulletin,- readable_file_size,- validate_yaml_file,)+from autogpt.config import Config+from autogpt.json_utils.utilities import extract_dict_from_response, validate_dict+from autogpt.utils import validate_yaml_filefrom tests.utils import skip_in_ci@@ -77,13 +76,6 @@ def test_validate_yaml_file_invalid():assert "There was an issue while trying to read" in message-def test_readable_file_size():- size_in_bytes = 1024 * 1024 * 3.5 # 3.5 MB- readable_size = readable_file_size(size_in_bytes)-- assert readable_size == "3.50 MB"--@patch("requests.get")def test_get_bulletin_from_web_success(mock_get):expected_content = "Test bulletin from web"@@ -127,7 +119,7 @@ def test_get_latest_bulletin_with_file():with open("data/CURRENT_BULLETIN.md", "w", encoding="utf-8") as f:f.write(expected_content)- with patch("autogpt.utils.get_bulletin_from_web", return_value=""):+ with patch("autogpt.app.utils.get_bulletin_from_web", return_value=""):bulletin, is_new = get_latest_bulletin()assert expected_content in bulletinassert is_new == False@@ -140,7 +132,9 @@ def test_get_latest_bulletin_with_new_bulletin():f.write("Old bulletin")expected_content = "New bulletin from web"- with patch("autogpt.utils.get_bulletin_from_web", return_value=expected_content):+ with patch(+ "autogpt.app.utils.get_bulletin_from_web", return_value=expected_content+ ):bulletin, is_new = get_latest_bulletin()assert "::NEW BULLETIN::" in bulletinassert expected_content in bulletin@@ -154,7 +148,9 @@ def test_get_latest_bulletin_new_bulletin_same_as_old_bulletin():with open("data/CURRENT_BULLETIN.md", "w", encoding="utf-8") as f:f.write(expected_content)- with patch("autogpt.utils.get_bulletin_from_web", return_value=expected_content):+ with patch(+ "autogpt.app.utils.get_bulletin_from_web", return_value=expected_content+ ):bulletin, is_new = get_latest_bulletin()assert expected_content in bulletinassert is_new == False@@ -170,7 +166,7 @@ def test_get_current_git_branch():assert branch_name != ""-@patch("autogpt.utils.Repo")+@patch("autogpt.app.utils.Repo")def test_get_current_git_branch_success(mock_repo):mock_repo.return_value.active_branch.name = "test-branch"branch_name = get_current_git_branch()@@ -178,7 +174,7 @@ def test_get_current_git_branch_success(mock_repo):assert branch_name == "test-branch"-@patch("autogpt.utils.Repo")+@patch("autogpt.app.utils.Repo")def test_get_current_git_branch_failure(mock_repo):mock_repo.side_effect = Exception()branch_name = get_current_git_branch()