Skip to content

Commit 4a9ed1e

Browse files
committed
fix: readline in win32 bugfix
(cherry picked from commit 33b05957b6e85e0183e539e3fe913dd3e6f1c56e)
1 parent 1b7d1e3 commit 4a9ed1e

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

agentkit/toolkit/cli/interactive_config.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,16 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import readline
15+
# Cross-platform readline support:
16+
# - On Unix, use built-in `readline`
17+
# - On Windows, try `pyreadline3` if available; otherwise gracefully degrade
18+
try:
19+
import readline # type: ignore
20+
except ImportError: # ImportError on Windows or environments without GNU readline
21+
try:
22+
import pyreadline3 as readline # type: ignore
23+
except ImportError:
24+
readline = None # type: ignore
1625
from typing import (
1726
Any,
1827
Dict,
@@ -128,9 +137,10 @@ def _safe_input(self, prompt_text, default: str = "") -> str:
128137

129138
def prefill():
130139
try:
131-
readline.insert_text(default)
140+
# Ensure default is a string to avoid TypeError
141+
readline.insert_text(str(default))
132142
readline.redisplay()
133-
except (AttributeError, OSError):
143+
except (AttributeError, OSError, TypeError):
134144
# Some readline implementations (e.g., libedit) may not support insert_text or redisplay
135145
# In this case, we'll display the default value in the prompt as a fallback
136146
pass
@@ -157,8 +167,9 @@ def prefill():
157167
finally:
158168
# Clean up hook; use try-except to prevent errors on unsupported systems
159169
try:
160-
readline.set_pre_input_hook()
161-
except (AttributeError, OSError):
170+
# Unset hook explicitly to avoid TypeError on some implementations
171+
readline.set_pre_input_hook(None)
172+
except (AttributeError, OSError, TypeError):
162173
pass
163174

164175
def generate_config(
@@ -572,7 +583,13 @@ def _handle_string(
572583
while True:
573584
# Build complete prompt information
574585
if default:
575-
prompt_str = f"\n[{current}/{total}] {icon} {description} (current: {default}{'' if not ('{' in default and '}' in default) else ', content in curly braces is a dynamic placeholder, no need to fill manually'}): "
586+
default_str = str(default)
587+
placeholder_hint = (
588+
", content in curly braces is a dynamic placeholder, no need to fill manually"
589+
if ("{" in default_str and "}" in default_str)
590+
else ""
591+
)
592+
prompt_str = f"\n[{current}/{total}] {icon} {description} (current: {default_str}{placeholder_hint}): "
576593
else:
577594
prompt_str = f"\n[{current}/{total}] {icon} {description}: "
578595

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ dependencies = [
4242
"tos>=2.8.7",
4343
"volcengine",
4444
"pyfiglet>=1.0.2",
45+
"pyreadline3; sys_platform == 'win32'",
4546
]
4647

4748
[project.scripts]
@@ -101,4 +102,4 @@ exclude = [
101102

102103
[tool.ruff.lint.per-file-ignores]
103104
"__init__.py" = ["F401"]
104-
"tests/*" = ["F401", "F811"]
105+
"tests/*" = ["F401", "F811"]

requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,8 @@ volcengine-python-sdk
2121
pyfiglet>=1.0.2
2222
inquirerpy>=0.3.4
2323

24+
# Windows-only enhancement for interactive CLI input
25+
pyreadline3; sys_platform == 'win32'
26+
2427
# Development dependencies
2528
pre-commit>=4.3.0

0 commit comments

Comments
 (0)