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 the rendered terminal text exactly as a human would see it — including the prompt, which doubles as live cwd/branch telemetry for the agent.

Two input shapes:

  • command (string) — one command, one result.

  • commands (array) — runs each command in order in the same shell; all run regardless of failures (the agent reads merged output and decides what to do). Use shell chaining (+&&+) inside a single command if you need fail-fast.

See Also:

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

prompt_guidelines, schema, truncation_threshold

Constructor Details

#initialize(shell_session:, session:) ⇒ Bash

Returns a new instance of Bash.

Parameters:

  • shell_session (ShellSession)

    persistent shell backing this tool

  • session (Session)

    conversation session for interrupt checking



43
44
45
46
# File 'lib/tools/bash.rb', line 43

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

Class Method Details

.descriptionObject



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

def self.description = "Execute shell commands. Working directory and environment persist between calls."

.input_schemaObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/tools/bash.rb', line 25

def self.input_schema
  {
    type: "object",
    properties: {
      command: {
        type: "string"
      },
      commands: {
        type: "array",
        items: {type: "string"},
        description: "Each command gets its own timeout and result. All commands run regardless of failures — use a single command with shell chaining if you need fail-fast."
      }
    }
  }
end

.prompt_snippetObject



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

def self.prompt_snippet = "Run shell commands."

.tool_nameObject



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

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)

    rendered terminal output

  • (Hash)

    with :error key on failure



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/tools/bash.rb', line 53

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"], 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