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.



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

def initialize(binding_context)
  @binding_context = binding_context
end

Instance Attribute Details

#binding_contextObject (readonly)

Returns the value of attribute binding_context.



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

def binding_context
  @binding_context
end

#last_errorObject (readonly)

Returns the value of attribute last_error.



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

def last_error
  @last_error
end

#on_promptObject

Returns the value of attribute on_prompt.



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

def on_prompt
  @on_prompt
end

Instance Method Details

#confirm_and_execute(code) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/console_agent/executor.rb', line 118

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

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

  loop do
    case answer
    when 'y', 'yes', 'a'
      return 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)
        edit_answer = $stdin.gets.to_s.strip.downcase
        echo_stdin(edit_answer)
        if edit_answer == 'y'
          return execute(edited)
        else
          $stdout.puts colorize("Cancelled.", :yellow)
          return nil
        end
      else
        return execute(code)
      end
    when 'n', 'no', ''
      $stdout.puts colorize("Cancelled.", :yellow)
      @last_cancelled = true
      return nil
    else
      $stdout.print colorize("Execute? [y/N/edit] ", :yellow)
      @on_prompt&.call
      answer = $stdin.gets.to_s.strip.downcase
      @last_answer = answer
      echo_stdin(answer)
    end
  end
end

#display_response(response) ⇒ Object



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

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



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/console_agent/executor.rb', line 75

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

  @last_error = nil
  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
  display_result(result)

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

#extract_code(response) ⇒ Object



53
54
55
56
# File 'lib/console_agent/executor.rb', line 53

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

#last_answerObject



110
111
112
# File 'lib/console_agent/executor.rb', line 110

def last_answer
  @last_answer
end

#last_cancelled?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/console_agent/executor.rb', line 114

def last_cancelled?
  @last_cancelled
end

#last_outputObject



106
107
108
# File 'lib/console_agent/executor.rb', line 106

def last_output
  @last_output
end