From 5ee0959475aeeb3e9d5a71fa1f190cfc3d68e16c Mon Sep 17 00:00:00 2001 From: majiayu000 <1835304752@qq.com> Date: Wed, 24 Dec 2025 11:51:04 +0800 Subject: [PATCH] fix: add readline import for arrow key navigation in interactive mode Import readline module to enable proper arrow key navigation and command history browsing in interactive mode. Without readline, arrow keys produce escape sequences (^[[A, ^[[B, etc.) instead of expected behavior. The import is wrapped in try/except for Windows compatibility where readline may not be available by default. Fixes #223 --- main.py | 6 +++++ tests/test_readline_support.py | 47 ++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 tests/test_readline_support.py diff --git a/main.py b/main.py index 8cdc34b2..1b5399f2 100755 --- a/main.py +++ b/main.py @@ -20,6 +20,12 @@ import sys from urllib.parse import urlparse +# Enable readline support for arrow key navigation in interactive mode +try: + import readline # noqa: F401 +except ImportError: + pass # readline not available on Windows by default + from openai import OpenAI from phone_agent import PhoneAgent diff --git a/tests/test_readline_support.py b/tests/test_readline_support.py new file mode 100644 index 00000000..95bca04e --- /dev/null +++ b/tests/test_readline_support.py @@ -0,0 +1,47 @@ +"""Tests for readline support in interactive mode. + +This test verifies that main.py imports readline to enable arrow key +navigation and history browsing in interactive mode (Issue #223). +""" + +import os +import re +import unittest + + +class TestReadlineSupport(unittest.TestCase): + """Test that main.py has readline import for arrow key support.""" + + def test_main_py_imports_readline(self): + """Verify main.py imports readline module.""" + main_py_path = os.path.join( + os.path.dirname(os.path.dirname(__file__)), + 'main.py' + ) + + with open(main_py_path, 'r', encoding='utf-8') as f: + content = f.read() + + # Check for readline import (with try/except for cross-platform compatibility) + self.assertIn('import readline', content, + "main.py should import readline for arrow key support") + + def test_readline_import_has_error_handling(self): + """Verify readline import has try/except for Windows compatibility.""" + main_py_path = os.path.join( + os.path.dirname(os.path.dirname(__file__)), + 'main.py' + ) + + with open(main_py_path, 'r', encoding='utf-8') as f: + content = f.read() + + # Check that readline import is wrapped in try/except + pattern = r'try:\s*\n\s*import readline.*?\nexcept ImportError:' + match = re.search(pattern, content, re.DOTALL) + self.assertIsNotNone(match, + "readline import should be wrapped in try/except for Windows compatibility") + + +if __name__ == '__main__': + unittest.main()