Overview
The Terminal Session API provides tmux-based terminal management with ZMQ messaging, process monitoring, and cross-platform support for macOS and Linux.TermSesh Class
Manages terminal sessions using tmux and publishes output via ZMQ.Constructor
port(int) - ZMQ publisher port. Default: 5555terminal_app(str, optional) - Custom terminal applicationsession_name(str) - Tmux session identifier. Default: ‘zapper_session’
context- ZMQ context for messagingpublisher- ZMQ PUB socket for broadcasting terminal outputsystem- Platform identifier (Darwin/Linux)session_name- Tmux session nameterminal_process- Subprocess handlemonitor- ProcessMonitor instancetmux_stack- History of terminal output states
Session Management
open_new_terminal()
Create and attach to a new tmux session with platform-specific terminal window.bool- True if session created successfully, False otherwise
- macOS: Uses AppleScript to open Terminal.app
- Linux: Uses gnome-terminal
- Windows: Not yet supported (raises NotImplementedError)
kill_tmux_session()
Terminate the tmux session if it exists.is_terminal_active()
Check if terminal session is active and valid.bool- True if terminal is running, False otherwise
Command Execution
send_to_terminal()
Send a command to the tmux session for execution.command(str) - Shell command to execute
str | None- “Command sent” on success, None on error
Output Management
read_tmux_output()
Read and process output from the tmux session.str- New output since last read, or empty string
- Captures current tmux pane content
- Cleans and formats output
- Tracks changes in tmux_stack
- Returns only incremental output
clean_tmux_output()
Clean and format tmux output by removing ANSI codes and extra whitespace.raw_output(str) - Raw tmux output with ANSI codes
str- Cleaned and formatted output
- Remove ANSI escape sequences
- Remove Unicode prompt characters
- Strip whitespace from lines
- Remove empty lines
- Deduplicate consecutive identical lines
Code Transmission
send_code_segment()
Send compressed code segments with metadata via ZMQ.code_data(dict) - Dictionary containing:file_path(str) - Path to source filecode(str) - Source code contentmetadata(dict, optional) - Additional metadataaction(str) - Action type: ‘analyze’, ‘edit’, or ‘debug’
- Compresses code using zlib
- Base64 encodes compressed data
- Publishes via ZMQ publisher socket
ProcessMonitor Class
Monitors tmux output in a background thread and publishes updates via ZMQ.Constructor
term_sesh(TermSesh) - TermSesh instance to monitor
term_sesh- Associated terminal sessionmonitor_thread- Background monitoring threadis_running- Thread execution flag
Methods
start_monitoring()
Start background thread to monitor tmux output.- Creates daemon thread
- Starts monitoring loop
- Non-blocking operation
monitor_tmux()
Continuously read tmux output and publish via ZMQ.Complete Example
Source References
/home/daytona/workspace/source/cli/term_sesh.py- TermSesh implementation/home/daytona/workspace/source/cli/process_monitor.py- ProcessMonitor implementation