_to_plain

def _to_plain(text: str):
Convert ANSI text to plain text using rich if available.

TerminalToolkit

class TerminalToolkit(BaseToolkit):
A toolkit for LLM agents to execute and interact with terminal commands in either a local or a sandboxed Docker environment. Parameters:
  • use_docker_backend (bool): If True, all commands are executed in a Docker container. Defaults to False.
  • docker_container_name (Optional[str]): The name of the Docker container to use. Required if use_docker_backend is True.
  • working_dir (str): The base directory for all operations. For the local backend, this acts as a security sandbox.
  • session_logs_dir (Optional[str]): The directory to store session logs. Defaults to a ‘terminal_logs’ subfolder in the working_dir.
  • timeout (int): The default timeout in seconds for blocking commands. Defaults to 60.

init

def __init__(
    self,
    timeout: Optional[float] = 20.0,
    working_directory: Optional[str] = None,
    use_docker_backend: bool = False,
    docker_container_name: Optional[str] = None,
    session_logs_dir: Optional[str] = None,
    safe_mode: bool = True,
    allowed_commands: Optional[List[str]] = None,
    clone_current_env: bool = False
):

_setup_cloned_environment

def _setup_cloned_environment(self):
Set up a cloned Python environment.

_setup_initial_environment

def _setup_initial_environment(self):
Set up an initial environment with Python 3.10.

_adapt_command_for_environment

def _adapt_command_for_environment(self, command: str):
Adapt command to use virtual environment if available.

_write_to_log

def _write_to_log(self, log_file: str, content: str):
Write content to log file with optional ANSI stripping. Parameters:
  • log_file (str): Path to the log file
  • content (str): Content to write

_sanitize_command

def _sanitize_command(self, command: str):
A comprehensive command sanitizer for both local and Docker backends.

_start_output_reader_thread

def _start_output_reader_thread(self, session_id: str):
Starts a thread to read stdout from a non-blocking process.

_collect_output_until_idle

def _collect_output_until_idle(
    self,
    id: str,
    idle_duration: float = 0.5,
    check_interval: float = 0.1,
    max_wait: float = 5.0
):
Collects output from a session until it’s idle or a max wait time is reached. Parameters:
  • id (str): The session ID.
  • idle_duration (float): How long the stream must be empty to be considered idle.(default: 0.5)
  • check_interval (float): The time to sleep between checks. (default: 0.1)
  • max_wait (float): The maximum total time to wait for the process to go idle. (default: 5.0)
Returns: str: The collected output. If max_wait is reached while the process is still outputting, a warning is appended.

shell_exec

def shell_exec(
    self,
    id: str,
    command: str,
    block: bool = True
):
This function executes a shell command. The command can run in blocking mode (waits for completion) or non-blocking mode (runs in the background). A unique session ID is created for each session. Parameters:
  • command (str): The command to execute.
  • block (bool): If True, the command runs synchronously, waiting for it to complete or time out, and returns its full output. If False, the command runs asynchronously in the background.
  • id (Optional[str]): A specific ID for the session. If not provided, a unique ID is generated for non-blocking sessions.
Returns: str: If block is True, returns the complete stdout and stderr. If block is False, returns a message containing the new session ID and the initial output from the command after it goes idle.

shell_write_to_process

def shell_write_to_process(self, id: str, command: str):
This function sends command to a running non-blocking process and returns the resulting output after the process becomes idle again. A newline \n is automatically appended to the input command. Parameters:
  • id (str): The unique session ID of the non-blocking process.
  • command (str): The text to write to the process’s standard input.
Returns: str: The output from the process after the command is sent.

shell_view

def shell_view(self, id: str):
This function retrieves any new output from a non-blocking session since the last time this function was called. If the process has terminated, it drains the output queue and appends a termination message. If the process is still running, it simply returns any new output. Parameters:
  • id (str): The unique session ID of the non-blocking process.
Returns: str: The new output from the process’s stdout and stderr. Returns an empty string if there is no new output.

shell_wait

def shell_wait(self, id: str, wait_seconds: float = 5.0):
This function waits for a specified duration for a non-blocking process to produce more output or terminate. Parameters:
  • id (str): The unique session ID of the non-blocking process.
  • wait_seconds (float): The maximum number of seconds to wait.
Returns: str: All output collected during the wait period.

shell_kill_process

def shell_kill_process(self, id: str):
This function forcibly terminates a running non-blocking process. Parameters:
  • id (str): The unique session ID of the process to kill.
Returns: str: A confirmation message indicating the process was terminated.

shell_ask_user_for_help

def shell_ask_user_for_help(self, id: str, prompt: str):
This function pauses execution and asks a human for help with an interactive session. This method can handle different scenarios:
  1. If session exists: Shows session output and allows interaction
  2. If session doesn’t exist: Creates a temporary session for help
Parameters:
  • id (str): The session ID of the interactive process needing help. Can be empty string for general help without session context.
  • prompt (str): The question or instruction from the LLM to show the human user (e.g., “The program is asking for a filename. Please enter ‘config.json’.”).
Returns: str: The output from the shell session after the user’s command has been executed, or help information for general queries.

del

def __del__(self):

get_tools

def get_tools(self):
Returns: List[FunctionTool]: A list of FunctionTool objects representing the functions in the toolkit.