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.

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



26
27
28
# File 'lib/tools/bash.rb', line 26

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

Class Method Details

.descriptionObject



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

def self.description = "Execute a bash command. Working directory and environment persist across calls within a conversation."

.input_schemaObject



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

def self.input_schema
  {
    type: "object",
    properties: {
      command: {type: "string", description: "The bash command to execute"}
    },
    required: ["command"]
  }
end

.tool_nameObject



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

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



35
36
37
38
39
40
41
42
43
# File 'lib/tools/bash.rb', line 35

def execute(input)
  command = input["command"].to_s
  return {error: "Command cannot be blank"} if command.strip.empty?

  result = @shell_session.run(command, timeout: input["timeout"])
  return result if result.key?(:error)

  format_result(result[:stdout], result[:stderr], result[:exit_code])
end