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

* Use prompt_toolkit to enable keyboard navigation in CLI

* Also update other tests

---------

Co-authored-by: merwanehamadi <merwanehamadi@gmail.com>
This commit is contained in:
Erik Peterson
2023-06-11 14:19:42 -07:00
committed by GitHub
parent bc5dbb6692
commit fd04db12fa
5 changed files with 11 additions and 6 deletions

View File

@@ -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")

View File

@@ -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)

View File

@@ -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

View File

@@ -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(

View File

@@ -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)