Class: ConsoleAgent::Executor

Inherits:
Object
  • Object
show all
Defined in:
lib/console_agent/executor.rb

Constant Summary collapse

CODE_REGEX =
/```ruby\s*\n(.*?)```/m

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(binding_context) ⇒ Executor

Returns a new instance of Executor.



46
47
48
# File 'lib/console_agent/executor.rb', line 46

def initialize(binding_context)
  @binding_context = binding_context
end

Instance Attribute Details

#binding_contextObject (readonly)

Returns the value of attribute binding_context.



44
45
46
# File 'lib/console_agent/executor.rb', line 44

def binding_context
  @binding_context
end

Instance Method Details

#confirm_and_execute(code) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/console_agent/executor.rb', line 108

def confirm_and_execute(code)
  return nil if code.nil? || code.strip.empty?

  @last_cancelled = false
  $stdout.print colorize("Execute? [y/N/edit] ", :yellow)
  answer = $stdin.gets.to_s.strip.downcase

  case answer
  when 'y', 'yes'
    execute(code)
  when 'e', 'edit'
    edited = open_in_editor(code)
    if edited && edited != code
      $stdout.puts colorize("# Edited code:", :yellow)
      $stdout.puts highlight_code(edited)
      $stdout.print colorize("Execute edited code? [y/N] ", :yellow)
      if $stdin.gets.to_s.strip.downcase == 'y'
        execute(edited)
      else
        $stdout.puts colorize("Cancelled.", :yellow)
        nil
      end
    else
      execute(code)
    end
  else
    $stdout.puts colorize("Cancelled.", :yellow)
    @last_cancelled = true
    nil
  end
end

#display_response(response) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/console_agent/executor.rb', line 55

def display_response(response)
  code = extract_code(response)
  explanation = response.gsub(CODE_REGEX, '').strip

  $stdout.puts
  $stdout.puts colorize(explanation, :cyan) unless explanation.empty?

  unless code.empty?
    $stdout.puts
    $stdout.puts colorize("# Generated code:", :yellow)
    $stdout.puts highlight_code(code)
    $stdout.puts
  end

  code
end

#execute(code) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/console_agent/executor.rb', line 72

def execute(code)
  return nil if code.nil? || code.strip.empty?

  captured_output = StringIO.new
  old_stdout = $stdout
  # Tee output: capture it and also print to the real stdout
  $stdout = TeeIO.new(old_stdout, captured_output)

  result = binding_context.eval(code, "(console_agent)", 1)

  $stdout = old_stdout
  $stdout.puts colorize("=> #{result.inspect}", :green)

  @last_output = captured_output.string
  result
rescue SyntaxError => e
  $stdout = old_stdout if old_stdout
  $stderr.puts colorize("SyntaxError: #{e.message}", :red)
  @last_output = nil
  nil
rescue => e
  $stdout = old_stdout if old_stdout
  $stderr.puts colorize("Error: #{e.class}: #{e.message}", :red)
  e.backtrace.first(3).each { |line| $stderr.puts colorize("  #{line}", :red) }
  @last_output = captured_output&.string
  nil
end

#extract_code(response) ⇒ Object



50
51
52
53
# File 'lib/console_agent/executor.rb', line 50

def extract_code(response)
  match = response.match(CODE_REGEX)
  match ? match[1].strip : ''
end

#last_cancelled?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/console_agent/executor.rb', line 104

def last_cancelled?
  @last_cancelled
end

#last_outputObject



100
101
102
# File 'lib/console_agent/executor.rb', line 100

def last_output
  @last_output
end