|
|
@@ -42,21 +42,36 @@ from agent.llm import create_openrouter_llm_call
|
|
|
|
|
|
|
|
|
# ===== 非阻塞 stdin 检测 =====
|
|
|
+if sys.platform == 'win32':
|
|
|
+ import msvcrt
|
|
|
|
|
|
def check_stdin() -> str | None:
|
|
|
"""
|
|
|
- 非阻塞检查 stdin 是否有输入。
|
|
|
-
|
|
|
- 使用 select 轮询,不开后台线程,因此不会与交互菜单的 input() 抢 stdin。
|
|
|
+ 跨平台非阻塞检查 stdin 输入。
|
|
|
+ Windows: 使用 msvcrt.kbhit()
|
|
|
+ macOS/Linux: 使用 select.select()
|
|
|
"""
|
|
|
- ready, _, _ = select.select([sys.stdin], [], [], 0)
|
|
|
- if ready:
|
|
|
- line = sys.stdin.readline().strip().lower()
|
|
|
- if line in ('p', 'pause'):
|
|
|
- return 'pause'
|
|
|
- if line in ('q', 'quit'):
|
|
|
- return 'quit'
|
|
|
- return None
|
|
|
+ if sys.platform == 'win32':
|
|
|
+ # 检查是否有按键按下
|
|
|
+ if msvcrt.kbhit():
|
|
|
+ # 读取按下的字符(msvcrt.getwch 是非阻塞读取宽字符)
|
|
|
+ ch = msvcrt.getwch().lower()
|
|
|
+ if ch == 'p':
|
|
|
+ return 'pause'
|
|
|
+ if ch == 'q':
|
|
|
+ return 'quit'
|
|
|
+ # 如果是其他按键,可以选择消耗掉或者忽略
|
|
|
+ return None
|
|
|
+ else:
|
|
|
+ # Unix/Mac 逻辑
|
|
|
+ ready, _, _ = select.select([sys.stdin], [], [], 0)
|
|
|
+ if ready:
|
|
|
+ line = sys.stdin.readline().strip().lower()
|
|
|
+ if line in ('p', 'pause'):
|
|
|
+ return 'pause'
|
|
|
+ if line in ('q', 'quit'):
|
|
|
+ return 'quit'
|
|
|
+ return None
|
|
|
|
|
|
|
|
|
# ===== 交互菜单 =====
|