Terminal Node

Terminal Node#

Overview#

TerminalNode executes commands generated by LLM and returns execution results. It handles shell commands, keyboard inputs, and captures terminal output.

Its key features include:

  • Command Execution

    • Shell commands: self.terminal.send_text(cmd + "\n", paste=True)

    • Keyboard input: self.terminal.send_key(key) with keystroke pattern matching

    • Multiline support: Handles multiline commands with paste mode

    • Timeout handling: timeout = self.terminal.wait()

  • Output Capture

    • Screen content: self.terminal.get_screen() when timeout occurs

    • New text: self.terminal.get_new_text() for normal execution

    • Error handling: Captures and reports command execution errors

The terminal node is capable of executing the following types of commands in terminal:

  • Shell: Shell command execution

  • key**: Keystroke sequences with pattern validation

  • subtask: Subtask execution (handled by UserActionNode)

  • context: Context modification commands

Implementation#

The logic of the terminal node is simple and a sample implementation is given below. Note that the code below is only a demo, the actual implementation is subject to future changes.

class TerminalNode(BaseNode):
    def __init__(self, terminal: Term, workflow_config: WorkflowConfig):
        ...

    def set_output_nodes(self, node: BaseNode):
        self.output_node = node

    def run(self, data: ContextData, extra_info: Optional[ExtraInfo] = None):
        last_message = data.last_message
        commands = extract_commands(last_message.content)
        cmd_type, cmd = commands[0]

        try:
            output = self._exec_and_return_output(cmd_type, cmd)
        except Exception as e:
            self.console.print(f"[bold red]Error: {e}[/bold red]", message_type=MessageType.ERROR)
            return None, None

        self.append_and_print(self.role, self.name, output, data)
        return self.output_node, None