mirror of
https://github.com/aljazceru/Auto-GPT.git
synced 2025-12-20 07:24:21 +01:00
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:
@@ -5,10 +5,14 @@ import requests
|
|||||||
import yaml
|
import yaml
|
||||||
from colorama import Fore, Style
|
from colorama import Fore, Style
|
||||||
from git.repo import Repo
|
from git.repo import Repo
|
||||||
|
from prompt_toolkit import ANSI, PromptSession
|
||||||
|
from prompt_toolkit.history import InMemoryHistory
|
||||||
|
|
||||||
from autogpt.config import Config
|
from autogpt.config import Config
|
||||||
from autogpt.logs import logger
|
from autogpt.logs import logger
|
||||||
|
|
||||||
|
session = PromptSession(history=InMemoryHistory())
|
||||||
|
|
||||||
|
|
||||||
def batch(iterable, max_batch_length: int, overlap: int = 0):
|
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."""
|
"""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
|
# ask for input, default when just pressing Enter is y
|
||||||
logger.info("Asking user via keyboard...")
|
logger.info("Asking user via keyboard...")
|
||||||
answer = input(prompt)
|
answer = session.prompt(ANSI(prompt))
|
||||||
return answer
|
return answer
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
logger.info("You interrupted Auto-GPT")
|
logger.info("You interrupted Auto-GPT")
|
||||||
|
|||||||
@@ -111,7 +111,7 @@ def test_information_retrieval_challenge_a(kubernetes_agent, monkeypatch) -> Non
|
|||||||
"""
|
"""
|
||||||
input_sequence = ["s", "s", "s", "s", "s", "EXIT"]
|
input_sequence = ["s", "s", "s", "s", "s", "EXIT"]
|
||||||
gen = input_generator(input_sequence)
|
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):
|
with contextlib.suppress(SystemExit):
|
||||||
run_interaction_loop(kubernetes_agent, None)
|
run_interaction_loop(kubernetes_agent, None)
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ click
|
|||||||
charset-normalizer>=3.1.0
|
charset-normalizer>=3.1.0
|
||||||
spacy>=3.0.0,<4.0.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
|
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
|
##Dev
|
||||||
coverage
|
coverage
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ def setup_mock_input(monkeypatch: pytest.MonkeyPatch, cycle_count: int) -> None:
|
|||||||
yield from input_sequence
|
yield from input_sequence
|
||||||
|
|
||||||
gen = input_generator()
|
gen = input_generator()
|
||||||
monkeypatch.setattr("builtins.input", lambda _: next(gen))
|
monkeypatch.setattr("autogpt.utils.session.prompt", lambda _: next(gen))
|
||||||
|
|
||||||
|
|
||||||
def run_interaction_loop(
|
def run_interaction_loop(
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ from tests.utils import requires_api_key
|
|||||||
@requires_api_key("OPENAI_API_KEY")
|
@requires_api_key("OPENAI_API_KEY")
|
||||||
def test_generate_aiconfig_automatic_default(patched_api_requestor):
|
def test_generate_aiconfig_automatic_default(patched_api_requestor):
|
||||||
user_inputs = [""]
|
user_inputs = [""]
|
||||||
with patch("builtins.input", side_effect=user_inputs):
|
with patch("autogpt.utils.session.prompt", side_effect=user_inputs):
|
||||||
ai_config = prompt_user()
|
ai_config = prompt_user()
|
||||||
|
|
||||||
assert isinstance(ai_config, AIConfig)
|
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()
|
ai_config = prompt_user()
|
||||||
|
|
||||||
assert isinstance(ai_config, AIConfig)
|
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()
|
ai_config = prompt_user()
|
||||||
|
|
||||||
assert isinstance(ai_config, AIConfig)
|
assert isinstance(ai_config, AIConfig)
|
||||||
|
|||||||
Reference in New Issue
Block a user