Class: Tools::Bash

Inherits:
Base
  • Object
show all
Defined in:
lib/tools/bash.rb

Overview

Executes bash commands in a persistent ShellSession. Commands share working directory, environment variables, and shell history within a conversation. Output is truncated and timeouts are enforced by the underlying session.

Supports two modes:

  • Single command via command (string) — backward compatible

  • Batch via commands (array) with mode controlling error handling

See Also:

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

schema

Constructor Details

#initialize(shell_session:) ⇒ Bash

Returns a new instance of Bash.

Parameters:

  • shell_session (ShellSession)

    persistent shell backing this tool



49
50
51
# File 'lib/tools/bash.rb', line 49

def initialize(shell_session:, **)
  @shell_session = shell_session
end

Class Method Details

.descriptionObject



17
18
19
20
21
22
23
24
# File 'lib/tools/bash.rb', line 17

def self.description
  <<~DESC.squish
    Execute a bash command. Working directory and environment persist across calls within a conversation.
    Accepts either `command` (string) for a single command, or `commands` (array of strings) to run
    multiple commands as a batch — each command gets its own timeout and result. Batch `mode` controls
    error handling: "sequential" (default) stops on the first failure, "parallel" runs all regardless.
  DESC
end

.input_schemaObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/tools/bash.rb', line 26

def self.input_schema
  {
    type: "object",
    properties: {
      command: {
        type: "string",
        description: "The bash command to execute"
      },
      commands: {
        type: "array",
        items: {type: "string"},
        description: "Array of bash commands to execute as a batch. Each runs independently with its own timeout and result."
      },
      mode: {
        type: "string",
        enum: ["sequential", "parallel"],
        description: 'Batch error handling: "sequential" (default) stops on first non-zero exit; "parallel" runs all commands regardless of failures.'
      }
    }
  }
end

.tool_nameObject



15
# File 'lib/tools/bash.rb', line 15

def self.tool_name = "bash"

Instance Method Details

#execute(input) ⇒ String, Hash

Parameters:

  • input (Hash<String, Object>)

    string-keyed hash from the Anthropic API. Supports optional “timeout” key (seconds) to override the global command_timeout setting for long-running operations.

Returns:

  • (String)

    formatted output with stdout, stderr, and exit code

  • (Hash)

    with :error key on failure



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/tools/bash.rb', line 58

def execute(input)
  timeout = input["timeout"]
  has_command = input.key?("command")
  has_commands = input.key?("commands")

  if has_command && has_commands
    {error: "Provide either 'command' or 'commands', not both"}
  elsif has_commands
    execute_batch(input["commands"], mode: input.fetch("mode", "sequential"), timeout: timeout)
  elsif has_command
    execute_single(input["command"], timeout: timeout)
  else
    {error: "Either 'command' (string) or 'commands' (array of strings) is required"}
  end
end