|
| 1 | +"""Factory for creating appropriate terminal sessions based on system capabilities.""" |
| 2 | + |
| 3 | +import platform |
| 4 | +import subprocess |
| 5 | +from typing import Literal |
| 6 | + |
| 7 | +from openhands.sdk.logger import get_logger |
| 8 | +from openhands.tools.execute_bash.terminal.terminal_session import TerminalSession |
| 9 | + |
| 10 | + |
| 11 | +logger = get_logger(__name__) |
| 12 | + |
| 13 | + |
| 14 | +def _is_tmux_available() -> bool: |
| 15 | + """Check if tmux is available on the system.""" |
| 16 | + try: |
| 17 | + result = subprocess.run( |
| 18 | + ["tmux", "-V"], |
| 19 | + capture_output=True, |
| 20 | + text=True, |
| 21 | + timeout=5.0, |
| 22 | + ) |
| 23 | + return result.returncode == 0 |
| 24 | + except (subprocess.TimeoutExpired, FileNotFoundError): |
| 25 | + return False |
| 26 | + |
| 27 | + |
| 28 | +def _is_powershell_available() -> bool: |
| 29 | + """Check if PowerShell is available on the system.""" |
| 30 | + if platform.system() == "Windows": |
| 31 | + # Check for Windows PowerShell |
| 32 | + powershell_cmd = "powershell" |
| 33 | + else: |
| 34 | + # Check for PowerShell Core (pwsh) on non-Windows systems |
| 35 | + powershell_cmd = "pwsh" |
| 36 | + |
| 37 | + try: |
| 38 | + result = subprocess.run( |
| 39 | + [powershell_cmd, "-Command", "Write-Host 'PowerShell Available'"], |
| 40 | + capture_output=True, |
| 41 | + text=True, |
| 42 | + timeout=5.0, |
| 43 | + ) |
| 44 | + return result.returncode == 0 |
| 45 | + except (subprocess.TimeoutExpired, FileNotFoundError): |
| 46 | + return False |
| 47 | + |
| 48 | + |
| 49 | +def create_terminal_session( |
| 50 | + work_dir: str, |
| 51 | + username: str | None = None, |
| 52 | + no_change_timeout_seconds: int | None = None, |
| 53 | + terminal_type: Literal["tmux", "subprocess"] | None = None, |
| 54 | +) -> TerminalSession: |
| 55 | + """Create an appropriate terminal session based on system capabilities. |
| 56 | +
|
| 57 | + Args: |
| 58 | + work_dir: Working directory for the session |
| 59 | + username: Optional username for the session |
| 60 | + no_change_timeout_seconds: Timeout for no output change |
| 61 | + terminal_type: Force a specific session type ('tmux', 'subprocess') |
| 62 | + If None, auto-detect based on system capabilities |
| 63 | +
|
| 64 | + Returns: |
| 65 | + TerminalSession instance |
| 66 | +
|
| 67 | + Raises: |
| 68 | + RuntimeError: If the requested session type is not available |
| 69 | + """ |
| 70 | + from openhands.tools.execute_bash.terminal.terminal_session import TerminalSession |
| 71 | + |
| 72 | + if terminal_type: |
| 73 | + # Force specific session type |
| 74 | + if terminal_type == "tmux": |
| 75 | + if not _is_tmux_available(): |
| 76 | + raise RuntimeError("Tmux is not available on this system") |
| 77 | + from openhands.tools.execute_bash.terminal.tmux_terminal import TmuxTerminal |
| 78 | + |
| 79 | + logger.info("Using forced TmuxTerminal") |
| 80 | + terminal = TmuxTerminal(work_dir, username) |
| 81 | + return TerminalSession(terminal, no_change_timeout_seconds) |
| 82 | + elif terminal_type == "subprocess": |
| 83 | + from openhands.tools.execute_bash.terminal.subprocess_terminal import ( |
| 84 | + SubprocessTerminal, |
| 85 | + ) |
| 86 | + |
| 87 | + logger.info("Using forced SubprocessTerminal") |
| 88 | + terminal = SubprocessTerminal(work_dir, username) |
| 89 | + return TerminalSession(terminal, no_change_timeout_seconds) |
| 90 | + else: |
| 91 | + raise ValueError(f"Unknown session type: {terminal_type}") |
| 92 | + |
| 93 | + # Auto-detect based on system capabilities |
| 94 | + system = platform.system() |
| 95 | + |
| 96 | + if system == "Windows": |
| 97 | + raise NotImplementedError("Windows is not supported yet for OpenHands V1.") |
| 98 | + else: |
| 99 | + # On Unix-like systems, prefer tmux if available, otherwise use subprocess |
| 100 | + if _is_tmux_available(): |
| 101 | + from openhands.tools.execute_bash.terminal.tmux_terminal import TmuxTerminal |
| 102 | + |
| 103 | + logger.info("Auto-detected: Using TmuxTerminal (tmux available)") |
| 104 | + terminal = TmuxTerminal(work_dir, username) |
| 105 | + return TerminalSession(terminal, no_change_timeout_seconds) |
| 106 | + else: |
| 107 | + from openhands.tools.execute_bash.terminal.subprocess_terminal import ( |
| 108 | + SubprocessTerminal, |
| 109 | + ) |
| 110 | + |
| 111 | + logger.info("Auto-detected: Using SubprocessTerminal (tmux not available)") |
| 112 | + terminal = SubprocessTerminal(work_dir, username) |
| 113 | + return TerminalSession(terminal, no_change_timeout_seconds) |
0 commit comments