Class: Zephira::Tools::Shell

Inherits:
BaseTool show all
Defined in:
lib/zephira/tools/shell.rb

Constant Summary collapse

OUTPUT_TRUNCATION_WIDTH =
200
TRUNCATION_OVERHEAD =
23

Instance Attribute Summary

Attributes inherited from BaseTool

#agent, #args

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseTool

announces_intent?, #arg, #error_result, #initialize, read_only?, run, #success_result, #validate

Constructor Details

This class inherits a constructor from Zephira::Tools::BaseTool

Class Method Details

.descriptionObject



16
17
18
# File 'lib/zephira/tools/shell.rb', line 16

def description
  "Run a shell command"
end

.nameObject



12
13
14
# File 'lib/zephira/tools/shell.rb', line 12

def name
  "shell"
end

.parametersObject



20
21
22
23
24
25
26
27
28
29
# File 'lib/zephira/tools/shell.rb', line 20

def parameters
  {
    type: "object",
    properties: {
      command: {type: "string", description: "Command to run"},
      intent: {type: "string", description: "Brief summary of intent of the operation, meant to be used for context compaction and presentation to the user. Use active voice (e.g., 'Reading X to do Y')."}
    },
    required: ["command", "intent"]
  }
end

Instance Method Details

#runObject



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/zephira/tools/shell.rb', line 32

def run
  cmd = validate(arg(:command), arg_path: "command", type: String, allow_empty: false)

  agent.status.verbose(" • Running shell command: '#{cmd}'")
  stdout_str, stderr_str, status_obj = Open3.capture3(cmd, chdir: Dir.pwd)

  unless stdout_str.to_s.empty?
    message = truncate_string_to_fit(
      prefix: " • Shell command stdout: ",
      text_array: stdout_str.lines,
      max_characters: OUTPUT_TRUNCATION_WIDTH
    )
    agent.status.verbose(message)
  end

  unless stderr_str.to_s.empty?
    message = truncate_string_to_fit(
      prefix: " • \e[91mShell command stderr:\e[0m ",
      text_array: stderr_str.lines,
      max_characters: OUTPUT_TRUNCATION_WIDTH
    )
    agent.status.verbose(message)
  end

  agent.status.verbose(" • Shell command completed with exit status: #{status_obj.exitstatus}")
  success_result(status: status_obj.exitstatus, stdout: stdout_str, stderr: stderr_str)
rescue Errno::ENOENT
  error_result(message: "Command not found: #{arg(:command)}")
end