Class: Brute::Tools::Shell

Inherits:
LLM::Tool
  • Object
show all
Defined in:
lib/brute/tools/shell.rb

Constant Summary collapse

TIMEOUT =

5 minutes

300
MAX_OUTPUT =
50_000

Instance Method Summary collapse

Instance Method Details

#call(command:, cwd: nil) ⇒ Object



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

def call(command:, cwd: nil)
  dir = cwd ? File.expand_path(cwd) : Dir.pwd
  raise "Directory not found: #{dir}" unless File.directory?(dir)

  stdout, stderr, status = nil
  Timeout.timeout(TIMEOUT) do
    stdout, stderr, status = Open3.capture3("bash", "-c", command, chdir: dir)
  end

  out = stdout.to_s
  err = stderr.to_s
  out = out[0...MAX_OUTPUT] + "\n...(truncated)" if out.size > MAX_OUTPUT
  err = err[0...MAX_OUTPUT] + "\n...(truncated)" if err.size > MAX_OUTPUT

  {stdout: out, stderr: err, exit_code: status.exitstatus}
rescue Timeout::Error
  {error: "Command timed out after #{TIMEOUT}s", command: command}
end