fix(cli): enable editing in custom model prompt

This commit is contained in:
Gille
2026-08-19 16:40:10 -06:00
committed by Teknium
parent 8408edcfb5
commit fa62b22ee7
4 changed files with 100 additions and 4 deletions
+3 -2
View File
@@ -7578,6 +7578,7 @@ def _prompt_model_selection(
If *unavailable_models* is provided, those models are shown grayed out
and unselectable, with an upgrade link to *portal_url*.
"""
from hermes_cli.cli_output import line_input
from hermes_cli.models import (
_format_price_per_mtok,
compute_sale_discount,
@@ -7794,7 +7795,7 @@ def _prompt_model_selection(
return _confirmed_selection(ordered[idx])
elif idx == len(ordered):
try:
custom = input("Enter model name: ").strip()
custom = line_input("Enter model name: ").strip()
except (EOFError, KeyboardInterrupt):
return None
return _confirmed_selection(custom) if custom else None
@@ -7838,7 +7839,7 @@ def _prompt_model_selection(
if 1 <= idx <= n:
return _confirmed_selection(ordered[idx - 1])
elif idx == n + 1:
custom = input("Enter model name: ").strip()
custom = line_input("Enter model name: ").strip()
return _confirmed_selection(custom) if custom else None
elif idx == n + 2:
return None
+22
View File
@@ -5,6 +5,8 @@ functions previously duplicated across setup.py, tools_config.py,
mcp_config.py, and memory_setup.py.
"""
import sys
from hermes_cli.colors import Colors, color
from hermes_cli.secret_prompt import masked_secret_prompt
@@ -40,6 +42,26 @@ def print_header(text: str) -> None:
# ─── Input Prompts ────────────────────────────────────────────────────────────
def line_input(prompt_text: str) -> str:
"""Read non-secret text with normal cursor-editing keys on a real TTY.
Setup and model-selection commands run outside the interactive chat's
prompt-toolkit application, so they can safely use a short-lived prompt
here. Redirected input and output retain the built-in ``input`` behavior
used by scripts, tests, and numbered fallbacks.
"""
if not (sys.stdin.isatty() and sys.stdout.isatty()):
return input(prompt_text)
try:
from prompt_toolkit import prompt as prompt_toolkit_prompt
from prompt_toolkit.formatted_text import ANSI
except ImportError:
return input(prompt_text)
return prompt_toolkit_prompt(ANSI(prompt_text))
def prompt(
question: str,
default: str | None = None,
+41
View File
@@ -1,6 +1,47 @@
from hermes_cli import cli_output
class _TTY:
def isatty(self):
return True
class _NotTTY:
def isatty(self):
return False
def test_line_input_supports_cursor_and_emacs_navigation(monkeypatch):
from prompt_toolkit.application.current import create_app_session
from prompt_toolkit.input.defaults import create_pipe_input
from prompt_toolkit.output import DummyOutput
monkeypatch.setattr(cli_output.sys, "stdin", _TTY())
monkeypatch.setattr(cli_output.sys, "stdout", _TTY())
with create_pipe_input() as pipe_input:
# Type "ac", move left, insert "b", move right, append "d", then
# exercise Ctrl+A/Ctrl+E before accepting the line.
pipe_input.send_text("ac\x1b[Db\x1b[Cd\x01X\x05Y\r")
with create_app_session(input=pipe_input, output=DummyOutput()):
assert cli_output.line_input("Enter model name: ") == "XabcdY"
def test_line_input_preserves_builtin_input_for_redirected_stdin(monkeypatch):
seen = {}
def fake_input(prompt_text):
seen["prompt"] = prompt_text
return "piped-model"
monkeypatch.setattr(cli_output.sys, "stdin", _NotTTY())
monkeypatch.setattr(cli_output.sys, "stdout", _TTY())
monkeypatch.setattr("builtins.input", fake_input)
assert cli_output.line_input("Enter model name: ") == "piped-model"
assert seen["prompt"] == "Enter model name: "
def test_password_prompt_uses_masked_secret_prompt(monkeypatch):
seen = {}
@@ -36,6 +36,40 @@ def test_prompt_model_selection_requires_expensive_confirmation(monkeypatch, cap
assert "EXPENSIVE MODEL WARNING" in out
def test_prompt_model_selection_uses_line_editor_for_custom_model(monkeypatch):
from hermes_cli.auth import _prompt_model_selection
monkeypatch.setattr(
"hermes_cli.curses_ui.curses_radiolist",
lambda _title, choices, **_kwargs: len(choices) - 2,
)
monkeypatch.setattr(
"hermes_cli.cli_output.line_input",
lambda prompt_text: (
"vendor/edited-model" if prompt_text == "Enter model name: " else ""
),
)
assert _prompt_model_selection(["vendor/default-model"]) == "vendor/edited-model"
def test_prompt_model_selection_fallback_uses_line_editor_for_custom_model(
monkeypatch,
):
from hermes_cli.auth import _prompt_model_selection
monkeypatch.setattr("hermes_cli.curses_ui.curses_radiolist", _raise_menu)
monkeypatch.setattr("builtins.input", lambda _prompt="": "2")
monkeypatch.setattr(
"hermes_cli.cli_output.line_input",
lambda prompt_text: (
"vendor/edited-model" if prompt_text == "Enter model name: " else ""
),
)
assert _prompt_model_selection(["vendor/default-model"]) == "vendor/edited-model"
def test_remove_custom_provider_falls_back_on_menu_runtime_error(tmp_path, monkeypatch):
from hermes_cli.main import _remove_custom_provider
@@ -58,5 +92,3 @@ def test_remove_custom_provider_falls_back_on_menu_runtime_error(tmp_path, monke
assert reloaded["custom_providers"] == [
{"name": "Local B", "base_url": "http://localhost:8002/v1"},
]