Class: SharedTools::Tools::Eval::ShellEvalTool

Inherits:
RubyLLM::Tool
  • Object
show all
Defined in:
lib/shared_tools/tools/eval/shell_eval_tool.rb

Overview

Examples:

tool = SharedTools::Tools::Eval::ShellEvalTool.new
tool.execute(command: "ls -la")

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger: nil) ⇒ ShellEvalTool

Returns a new instance of ShellEvalTool.

Parameters:

  • logger (Logger) (defaults to: nil)

    optional logger



20
21
22
# File 'lib/shared_tools/tools/eval/shell_eval_tool.rb', line 20

def initialize(logger: nil)
  @logger = logger || RubyLLM.logger
end

Class Method Details

.nameObject



11
# File 'lib/shared_tools/tools/eval/shell_eval_tool.rb', line 11

def self.name = 'eval_shell'

Instance Method Details

#execute(command:) ⇒ Hash

Returns execution result.

Parameters:

  • command (String)

    shell command to execute

Returns:

  • (Hash)

    execution result



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/shared_tools/tools/eval/shell_eval_tool.rb', line 27

def execute(command:)
  @logger.info("Requesting permission to execute command: '#{command}'")

  if command.strip.empty?
    error_msg = "Command cannot be empty"
    @logger.error(error_msg)
    return { error: error_msg }
  end

  # Show user the command and ask for confirmation
  allowed = SharedTools.execute?(tool: self.class.to_s, stuff: command)

  unless allowed
    @logger.warn("User declined to execute the command: '#{command}'")
    return { error: "User declined to execute the command" }
  end

  @logger.info("Executing command: '#{command}'")

  # Use Open3 for safer command execution with proper error handling
  require "open3"
  stdout, stderr, status = Open3.capture3(command)

  if status.success?
    @logger.debug("Command execution completed successfully with #{stdout.bytesize} bytes of output")
    { stdout: stdout, exit_status: status.exitstatus }
  else
    @logger.warn("Command execution failed with exit code #{status.exitstatus}: #{stderr}")
    { error: "Command failed with exit code #{status.exitstatus}", stderr: stderr, exit_status: status.exitstatus }
  end
rescue => e
  @logger.error("Command execution failed: #{e.message}")
  { error: e.message }
end