Skip to content

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.

Use prompt_toolkit to enable keyboard navigation in CLI (#4649)

Erik Petersonfd04db12faaf
diff --git a/autogpt/utils.py b/autogpt/utils.py
index 653841a232..91e570a0f5 100644
--- a/autogpt/utils.py
+++ b/autogpt/utils.py
@@ -5,10 +5,14 @@ import requests
import 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."""
@@ -52,7 +56,7 @@ def clean_input(prompt: str = "", talk=False):
# ask for input, default when just pressing Enter is y
logger.info("Asking user via keyboard...")
- answer = input(prompt)
+ answer = session.prompt(ANSI(prompt))
return answer
except KeyboardInterrupt:
logger.info("You interrupted Auto-GPT")
diff --git a/docs/challenges/building_challenges.md b/docs/challenges/building_challenges.md
index 0bd416cc26..0c3d89ac35 100644
--- a/docs/challenges/building_challenges.md
+++ b/docs/challenges/building_challenges.md
@@ -111,7 +111,7 @@ def test_information_retrieval_challenge_a(kubernetes_agent, monkeypatch) -> Non
"""
input_sequence = ["s", "s", "s", "s", "s", "EXIT"]
gen = input_generator(input_sequence)
- monkeypatch.setattr("builtins.input", lambda _: next(gen))
+ monkeypatch.setattr("autogpt.utils.session.prompt", lambda _: next(gen))
with contextlib.suppress(SystemExit):
run_interaction_loop(kubernetes_agent, None)
diff --git a/requirements.txt b/requirements.txt
index 16ed85589b..8c171f8338 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -27,6 +27,7 @@ click
charset-normalizer>=3.1.0
spacy>=3.0.0,<4.0.0
en-core-web-sm @ https://github.com/explosion/spacy-models/releases/download/en_core_web_sm-3.5.0/en_core_web_sm-3.5.0-py3-none-any.whl
+prompt_toolkit>=3.0.38
##Dev
coverage
diff --git a/tests/challenges/utils.py b/tests/challenges/utils.py
index c53126015f..9a357d9687 100644
--- a/tests/challenges/utils.py
+++ b/tests/challenges/utils.py
@@ -35,7 +35,7 @@ def setup_mock_input(monkeypatch: pytest.MonkeyPatch, cycle_count: int) -> None:
yield from input_sequence
gen = input_generator()
- monkeypatch.setattr("builtins.input", lambda _: next(gen))
+ monkeypatch.setattr("autogpt.utils.session.prompt", lambda _: next(gen))
def run_interaction_loop(
diff --git a/tests/integration/test_setup.py b/tests/integration/test_setup.py
index 4e2a505d9a..5217d72abe 100644
--- a/tests/integration/test_setup.py
+++ b/tests/integration/test_setup.py
@@ -11,7 +11,7 @@ from tests.utils import requires_api_key
@requires_api_key("OPENAI_API_KEY")
def test_generate_aiconfig_automatic_default(patched_api_requestor):
user_inputs = [""]
- with patch("builtins.input", side_effect=user_inputs):
+ with patch("autogpt.utils.session.prompt", side_effect=user_inputs):
ai_config = prompt_user()
assert isinstance(ai_config, AIConfig)
@@ -44,7 +44,7 @@ def test_generate_aiconfig_automatic_fallback(patched_api_requestor):
"",
"",
]
- with patch("builtins.input", side_effect=user_inputs):
+ with patch("autogpt.utils.session.prompt", side_effect=user_inputs):
ai_config = prompt_user()
assert isinstance(ai_config, AIConfig)
@@ -65,7 +65,7 @@ def test_prompt_user_manual_mode(patched_api_requestor):
"",
"",
]
- with patch("builtins.input", side_effect=user_inputs):
+ with patch("autogpt.utils.session.prompt", side_effect=user_inputs):
ai_config = prompt_user()
assert isinstance(ai_config, AIConfig)