-
Notifications
You must be signed in to change notification settings - Fork 219
feat(terminal): standardize send_keys special key support across backends #2807
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sjathin
wants to merge
8
commits into
OpenHands:main
Choose a base branch
from
sjathin:sdk-112
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ad295f0
feat(terminal): standardize send_keys special key support across back…
c6820d9
feat(terminal): standardize send_keys special key support across back…
a78e020
Merge branch 'main' into sdk-112
xingyaoww 89a723f
fix(terminal): address review feedback on send_keys special key handling
76d076b
Merge branch 'main' into sdk-112
sjathin a6263df
Add runtime assertions that backend special key maps match SUPPORTED_…
openhands-agent c3c5efc
Merge branch 'main' into sdk-112
xingyaoww 1355f46
Merge branch 'main' into sdk-112
enyst File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,10 +15,37 @@ | |
| ) | ||
| from openhands.tools.terminal.metadata import CmdOutputMetadata | ||
| from openhands.tools.terminal.terminal import TerminalInterface | ||
| from openhands.tools.terminal.terminal.interface import ( | ||
| SUPPORTED_SPECIAL_KEYS, | ||
| parse_ctrl_key, | ||
| ) | ||
|
|
||
|
|
||
| logger = get_logger(__name__) | ||
|
|
||
| # Map normalized special key names to tmux key names. | ||
| _TMUX_SPECIALS: dict[str, str] = { | ||
| "ENTER": "Enter", | ||
| "TAB": "Tab", | ||
| "BS": "BSpace", | ||
| "ESC": "Escape", | ||
| "UP": "Up", | ||
| "DOWN": "Down", | ||
| "LEFT": "Left", | ||
| "RIGHT": "Right", | ||
| "HOME": "Home", | ||
| "END": "End", | ||
| "PGUP": "PPage", | ||
| "PGDN": "NPage", | ||
| "C-L": "C-l", | ||
| "C-D": "C-d", | ||
| "C-C": "C-c", | ||
| } | ||
| assert set(_TMUX_SPECIALS.keys()) == SUPPORTED_SPECIAL_KEYS, ( | ||
| f"_TMUX_SPECIALS keys {set(_TMUX_SPECIALS.keys())} " | ||
| f"do not match SUPPORTED_SPECIAL_KEYS {SUPPORTED_SPECIAL_KEYS}" | ||
| ) | ||
|
|
||
|
xingyaoww marked this conversation as resolved.
|
||
|
|
||
| class TmuxTerminal(TerminalInterface): | ||
| """Tmux-based terminal backend. | ||
|
|
@@ -114,14 +141,39 @@ def close(self) -> None: | |
| def send_keys(self, text: str, enter: bool = True) -> None: | ||
| """Send text/keys to the tmux pane. | ||
|
|
||
| Supports: | ||
| - Plain text (uses literal paste; preserves spaces/newlines) | ||
| - Named specials: ENTER, TAB, BS, ESC, UP, DOWN, LEFT, RIGHT, | ||
| HOME, END, PGUP, PGDN, C-L, C-D, C-C | ||
| - Generic Ctrl sequences: C-a..C-z, CTRL-x, CTRL+x | ||
|
|
||
| Args: | ||
| text: Text or key sequence to send | ||
| enter: Whether to send Enter key after the text | ||
| enter: Whether to send Enter key after the text. | ||
| Ignored for special/ctrl keys. | ||
| """ | ||
| if not self._initialized or not isinstance(self.pane, libtmux.Pane): | ||
| raise RuntimeError("Tmux terminal is not initialized") | ||
|
|
||
| self.pane.send_keys(text, enter=enter) | ||
| # Map normalized names to tmux key names | ||
| upper = text.strip().upper() | ||
|
|
||
| # 1) Named specials | ||
| if upper in _TMUX_SPECIALS: | ||
| self.pane.send_keys(_TMUX_SPECIALS[upper], enter=False) | ||
| return | ||
|
|
||
| # 2) Generic Ctrl-<letter> | ||
| ctrl = parse_ctrl_key(text) | ||
| if ctrl is not None: | ||
| self.pane.send_keys(ctrl, enter=False) | ||
| return | ||
|
|
||
| # 3) Plain text — use literal=True so tmux doesn't split on | ||
| # whitespace or interpret special tokens. | ||
| self.pane.send_keys(text, enter=False, literal=True) | ||
| if enter and not text.endswith("\n"): | ||
| self.pane.send_keys("Enter", enter=False) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟢 Acceptable: The |
||
|
|
||
| def read_screen(self) -> str: | ||
| """Read the current tmux pane content. | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.