|
11 | 11 |
|
12 | 12 | import argparse |
13 | 13 | import asyncio |
| 14 | +import sys |
14 | 15 | from datetime import datetime |
15 | 16 | from pathlib import Path |
16 | 17 | from typing import List |
17 | 18 |
|
| 19 | +try: |
| 20 | + import termios |
| 21 | + import tty |
| 22 | +except ImportError: # pragma: no cover - Windows fallback |
| 23 | + termios = None # type: ignore[assignment] |
| 24 | + tty = None # type: ignore[assignment] |
| 25 | + |
18 | 26 | from prompt_toolkit import PromptSession |
19 | 27 | from prompt_toolkit.auto_suggest import AutoSuggestFromHistory |
20 | 28 | from prompt_toolkit.completion import WordCompleter |
@@ -70,6 +78,75 @@ class Colors: |
70 | 78 | BG_BLUE = "\033[44m" |
71 | 79 |
|
72 | 80 |
|
| 81 | +class EscapeKeyListener: |
| 82 | + """Listen for ESC key presses during agent execution to request a stop.""" |
| 83 | + |
| 84 | + def __init__(self, agent: Agent): |
| 85 | + self.agent = agent |
| 86 | + self._loop: asyncio.AbstractEventLoop | None = None |
| 87 | + self._fd: int | None = None |
| 88 | + self._old_settings = None |
| 89 | + self._cbreak_enabled = False |
| 90 | + self._reader_registered = False |
| 91 | + |
| 92 | + async def __aenter__(self): |
| 93 | + self._loop = asyncio.get_running_loop() |
| 94 | + await self._loop.run_in_executor(None, self._enable_cbreak_mode) |
| 95 | + if ( |
| 96 | + self._cbreak_enabled |
| 97 | + and self._loop |
| 98 | + and self._fd is not None |
| 99 | + and hasattr(self._loop, "add_reader") |
| 100 | + ): |
| 101 | + self._loop.add_reader(self._fd, self._handle_keypress) |
| 102 | + self._reader_registered = True |
| 103 | + return self |
| 104 | + |
| 105 | + async def __aexit__(self, exc_type, exc, tb): |
| 106 | + if self._reader_registered and self._loop and self._fd is not None: |
| 107 | + self._loop.remove_reader(self._fd) |
| 108 | + self._reader_registered = False |
| 109 | + if self._loop: |
| 110 | + await self._loop.run_in_executor(None, self._restore_terminal) |
| 111 | + |
| 112 | + def _enable_cbreak_mode(self): |
| 113 | + if termios is None or tty is None: |
| 114 | + return |
| 115 | + if not sys.stdin.isatty(): |
| 116 | + return |
| 117 | + try: |
| 118 | + self._fd = sys.stdin.fileno() |
| 119 | + self._old_settings = termios.tcgetattr(self._fd) |
| 120 | + tty.setcbreak(self._fd) |
| 121 | + self._cbreak_enabled = True |
| 122 | + except Exception: |
| 123 | + self._cbreak_enabled = False |
| 124 | + |
| 125 | + def _restore_terminal(self): |
| 126 | + if ( |
| 127 | + self._cbreak_enabled |
| 128 | + and self._fd is not None |
| 129 | + and self._old_settings is not None |
| 130 | + and termios is not None |
| 131 | + ): |
| 132 | + termios.tcsetattr(self._fd, termios.TCSADRAIN, self._old_settings) |
| 133 | + self._cbreak_enabled = False |
| 134 | + |
| 135 | + def _handle_keypress(self): |
| 136 | + if not self._cbreak_enabled: |
| 137 | + return |
| 138 | + try: |
| 139 | + ch = sys.stdin.read(1) |
| 140 | + except Exception: |
| 141 | + return |
| 142 | + if ch == "\x1b": # ESC key |
| 143 | + print(f"\n{Colors.BRIGHT_YELLOW}⏹️ Escape detected, requesting agent pause...{Colors.RESET}") |
| 144 | + self.agent.request_stop() |
| 145 | + if self._loop and self._reader_registered and self._fd is not None: |
| 146 | + self._loop.remove_reader(self._fd) |
| 147 | + self._reader_registered = False |
| 148 | + |
| 149 | + |
73 | 150 | def print_banner(): |
74 | 151 | """Print welcome banner with proper alignment""" |
75 | 152 | BOX_WIDTH = 58 |
@@ -105,13 +182,15 @@ def print_help(): |
105 | 182 | {Colors.BRIGHT_CYAN}Tab{Colors.RESET} - Auto-complete commands |
106 | 183 | {Colors.BRIGHT_CYAN}↑/↓{Colors.RESET} - Browse command history |
107 | 184 | {Colors.BRIGHT_CYAN}→{Colors.RESET} - Accept auto-suggestion |
| 185 | + {Colors.BRIGHT_CYAN}Esc{Colors.RESET} - Pause the current agent run |
108 | 186 |
|
109 | 187 | {Colors.BOLD}{Colors.BRIGHT_YELLOW}Usage:{Colors.RESET} |
110 | 188 | - Enter your task directly, Agent will help you complete it |
111 | 189 | - Agent remembers all conversation content in this session |
112 | 190 | - Use {Colors.BRIGHT_GREEN}/clear{Colors.RESET} to start a new session |
113 | 191 | - Press {Colors.BRIGHT_CYAN}Enter{Colors.RESET} to submit your message |
114 | 192 | - Use {Colors.BRIGHT_CYAN}Ctrl+J{Colors.RESET} to insert line breaks within your message |
| 193 | + - Press {Colors.BRIGHT_CYAN}Esc{Colors.RESET} anytime during execution to stop the agent |
115 | 194 | """ |
116 | 195 | print(help_text) |
117 | 196 |
|
@@ -551,7 +630,8 @@ def _(event): |
551 | 630 | # Run Agent |
552 | 631 | print(f"\n{Colors.BRIGHT_BLUE}Agent{Colors.RESET} {Colors.DIM}›{Colors.RESET} {Colors.DIM}Thinking...{Colors.RESET}\n") |
553 | 632 | agent.add_user_message(user_input) |
554 | | - _ = await agent.run() |
| 633 | + async with EscapeKeyListener(agent): |
| 634 | + _ = await agent.run() |
555 | 635 |
|
556 | 636 | # Visual separation - keep it simple like the reference code |
557 | 637 | print(f"\n{Colors.DIM}{'─' * 60}{Colors.RESET}\n") |
|
0 commit comments