Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions llm/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1267,13 +1267,13 @@ def chat(
"""
Hold an ongoing chat with a model.
"""
# Left and right arrow keys to move cursor:
if sys.platform != "win32":
# Left and right arrow keys to move cursor.
# pyreadline3 on Windows does not implement Bash `bind -x`.
try:
readline.parse_and_bind("\\e[D: backward-char")
readline.parse_and_bind("\\e[C: forward-char")
else:
readline.parse_and_bind("bind -x '\\e[D: backward-char'")
readline.parse_and_bind("bind -x '\\e[C: forward-char'")
except Exception:
pass
log_path = pathlib.Path(database) if database else logs_db_path()
(log_path.parent).mkdir(parents=True, exist_ok=True)
db = sqlite_utils.Database(log_path)
Expand Down
32 changes: 32 additions & 0 deletions tests/test_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,3 +355,35 @@ def test_chat_fragments(tmpdir):
).output
assert '"prompt": "one' in output
assert '"prompt": "two"' in output


def test_chat_readline_does_not_use_bash_bind_x(mock_model, logs_db, monkeypatch):
calls = []
monkeypatch.setattr(
llm.cli.readline, "parse_and_bind", lambda spec: calls.append(spec)
)
mock_model.enqueue(["ok"])
result = CliRunner().invoke(
llm.cli.cli,
["chat", "-m", "mock"],
input="Hi\nquit\n",
catch_exceptions=False,
)
assert result.exit_code == 0
assert calls == ["\\e[D: backward-char", "\\e[C: forward-char"]


def test_chat_survives_readline_bind_errors(mock_model, logs_db, monkeypatch):
def boom(_spec):
raise ValueError("pyreadline3 does not support this bind")

monkeypatch.setattr(llm.cli.readline, "parse_and_bind", boom)
mock_model.enqueue(["ok"])
result = CliRunner().invoke(
llm.cli.cli,
["chat", "-m", "mock"],
input="Hi\nquit\n",
catch_exceptions=False,
)
assert result.exit_code == 0
assert "ok" in result.output