Class: ConsoleAgent::Executor
- Inherits:
-
Object
- Object
- ConsoleAgent::Executor
- Defined in:
- lib/console_agent/executor.rb
Constant Summary collapse
- CODE_REGEX =
/```ruby\s*\n(.*?)```/m
Instance Attribute Summary collapse
-
#binding_context ⇒ Object
readonly
Returns the value of attribute binding_context.
-
#last_error ⇒ Object
readonly
Returns the value of attribute last_error.
-
#on_prompt ⇒ Object
Returns the value of attribute on_prompt.
Instance Method Summary collapse
- #confirm_and_execute(code) ⇒ Object
- #display_response(response) ⇒ Object
- #execute(code) ⇒ Object
- #expand_output(id) ⇒ Object
- #extract_code(response) ⇒ Object
-
#initialize(binding_context) ⇒ Executor
constructor
A new instance of Executor.
- #last_answer ⇒ Object
- #last_cancelled? ⇒ Boolean
- #last_output ⇒ Object
- #recall_output(id) ⇒ Object
- #store_output(content) ⇒ Object
Constructor Details
#initialize(binding_context) ⇒ Executor
Returns a new instance of Executor.
49 50 51 52 53 54 55 |
# File 'lib/console_agent/executor.rb', line 49 def initialize(binding_context) @binding_context = binding_context @omitted_outputs = {} @omitted_counter = 0 @output_store = {} @output_counter = 0 end |
Instance Attribute Details
#binding_context ⇒ Object (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_error ⇒ Object (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_prompt ⇒ Object
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
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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/console_agent/executor.rb', line 136 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
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/console_agent/executor.rb', line 62 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
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 105 106 107 108 |
# File 'lib/console_agent/executor.rb', line 79 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.}" $stderr.puts colorize(@last_error, :red) @last_output = nil nil rescue => e $stdout = old_stdout if old_stdout @last_error = "#{e.class}: #{e.}" $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 |
#expand_output(id) ⇒ Object
114 115 116 |
# File 'lib/console_agent/executor.rb', line 114 def (id) @omitted_outputs[id] end |
#extract_code(response) ⇒ Object
57 58 59 60 |
# File 'lib/console_agent/executor.rb', line 57 def extract_code(response) match = response.match(CODE_REGEX) match ? match[1].strip : '' end |
#last_answer ⇒ Object
128 129 130 |
# File 'lib/console_agent/executor.rb', line 128 def last_answer @last_answer end |
#last_cancelled? ⇒ Boolean
132 133 134 |
# File 'lib/console_agent/executor.rb', line 132 def last_cancelled? @last_cancelled end |
#last_output ⇒ Object
110 111 112 |
# File 'lib/console_agent/executor.rb', line 110 def last_output @last_output end |
#recall_output(id) ⇒ Object
124 125 126 |
# File 'lib/console_agent/executor.rb', line 124 def recall_output(id) @output_store[id] end |
#store_output(content) ⇒ Object
118 119 120 121 122 |
# File 'lib/console_agent/executor.rb', line 118 def store_output(content) @output_counter += 1 @output_store[@output_counter] = content @output_counter end |