Class: ConsoleAgent::Repl
- Inherits:
-
Object
- Object
- ConsoleAgent::Repl
- Defined in:
- lib/console_agent/repl.rb
Instance Method Summary collapse
- #explain(query) ⇒ Object
- #init_guide ⇒ Object
-
#initialize(binding_context) ⇒ Repl
constructor
A new instance of Repl.
- #interactive ⇒ Object
- #one_shot(query) ⇒ Object
-
#one_shot_round(conversation) ⇒ Object
Executes one LLM round: send query, display, optionally execute code.
- #resume(session) ⇒ Object
Constructor Details
#initialize(binding_context) ⇒ Repl
Returns a new instance of Repl.
5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/console_agent/repl.rb', line 5 def initialize(binding_context) @binding_context = binding_context @executor = Executor.new(binding_context) @provider = nil @context_builder = nil @context = nil @history = [] @total_input_tokens = 0 @total_output_tokens = 0 @input_history = [] end |
Instance Method Details
#explain(query) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/console_agent/repl.rb', line 85 def explain(query) start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) console_capture = StringIO.new with_console_capture(console_capture) do result, _ = send_query(query) track_usage(result) @executor.display_response(result.text) display_usage(result) @_last_log_attrs = { query: query, conversation: [{ role: :user, content: query }, { role: :assistant, content: result.text }], mode: 'explain', executed: false, start_time: start_time } end log_session(@_last_log_attrs.merge(console_output: console_capture.string)) nil rescue Providers::ProviderError => e $stderr.puts "\e[31mConsoleAgent Error: #{e.message}\e[0m" nil rescue => e $stderr.puts "\e[31mConsoleAgent Error: #{e.class}: #{e.message}\e[0m" nil end |
#init_guide ⇒ Object
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 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 |
# File 'lib/console_agent/repl.rb', line 114 def init_guide storage = ConsoleAgent.storage existing_guide = begin content = storage.read(ConsoleAgent::GUIDE_KEY) (content && !content.strip.empty?) ? content.strip : nil rescue nil end if existing_guide $stdout.puts "\e[36m Existing guide found (#{existing_guide.length} chars). Will update.\e[0m" else $stdout.puts "\e[36m No existing guide. Exploring the app...\e[0m" end require 'console_agent/tools/registry' init_tools = Tools::Registry.new(mode: :init) sys_prompt = init_system_prompt(existing_guide) = [{ role: :user, content: "Explore this Rails application and generate the application guide." }] # Temporarily increase timeout — init conversations are large original_timeout = ConsoleAgent.configuration.timeout ConsoleAgent.configuration.timeout = [original_timeout, 120].max result, _ = send_query_with_tools(, system_prompt: sys_prompt, tools_override: init_tools) guide_text = result.text.to_s.strip # Strip markdown code fences if the LLM wrapped the response guide_text = guide_text.sub(/\A```(?:markdown)?\s*\n?/, '').sub(/\n?```\s*\z/, '') # Strip LLM preamble/thinking before the actual guide content guide_text = guide_text.sub(/\A.*?(?=^#\s)/m, '') if guide_text =~ /^#\s/m if guide_text.empty? $stdout.puts "\e[33m No guide content generated.\e[0m" return nil end storage.write(ConsoleAgent::GUIDE_KEY, guide_text) path = storage.respond_to?(:root_path) ? File.join(storage.root_path, ConsoleAgent::GUIDE_KEY) : ConsoleAgent::GUIDE_KEY $stdout.puts "\e[32m Guide saved to #{path} (#{guide_text.length} chars)\e[0m" display_usage(result) nil rescue Interrupt $stdout.puts "\n\e[33m Interrupted.\e[0m" nil rescue Providers::ProviderError => e $stderr.puts "\e[31mConsoleAgent Error: #{e.message}\e[0m" nil rescue => e $stderr.puts "\e[31mConsoleAgent Error: #{e.class}: #{e.message}\e[0m" nil ensure ConsoleAgent.configuration.timeout = original_timeout if original_timeout end |
#interactive ⇒ Object
170 171 172 173 |
# File 'lib/console_agent/repl.rb', line 170 def interactive init_interactive_state interactive_loop end |
#one_shot(query) ⇒ Object
17 18 19 20 21 22 23 24 25 26 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 |
# File 'lib/console_agent/repl.rb', line 17 def one_shot(query) start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) console_capture = StringIO.new exec_result = with_console_capture(console_capture) do conversation = [{ role: :user, content: query }] exec_result, code, executed = one_shot_round(conversation) # Auto-retry once if execution errored if executed && @executor.last_error error_msg = "Code execution failed with error: #{@executor.last_error}" error_msg = error_msg[0..1000] + '...' if error_msg.length > 1000 conversation << { role: :assistant, content: @_last_result_text } conversation << { role: :user, content: error_msg } $stdout.puts "\e[2m Attempting to fix...\e[0m" exec_result, code, executed = one_shot_round(conversation) end @_last_log_attrs = { query: query, conversation: conversation, mode: 'one_shot', code_executed: code, code_output: executed ? @executor.last_output : nil, code_result: executed && exec_result ? exec_result.inspect : nil, executed: executed, start_time: start_time } exec_result end log_session(@_last_log_attrs.merge(console_output: console_capture.string)) exec_result rescue Providers::ProviderError => e $stderr.puts "\e[31mConsoleAgent Error: #{e.message}\e[0m" nil rescue => e $stderr.puts "\e[31mConsoleAgent Error: #{e.class}: #{e.message}\e[0m" nil end |
#one_shot_round(conversation) ⇒ Object
Executes one LLM round: send query, display, optionally execute code. Returns [exec_result, code, executed].
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/console_agent/repl.rb', line 62 def one_shot_round(conversation) result, _ = send_query(nil, conversation: conversation) track_usage(result) code = @executor.display_response(result.text) display_usage(result) @_last_result_text = result.text exec_result = nil executed = false has_code = code && !code.strip.empty? if has_code exec_result = if ConsoleAgent.configuration.auto_execute @executor.execute(code) else @executor.confirm_and_execute(code) end executed = !@executor.last_cancelled? end [exec_result, has_code ? code : nil, executed] end |
#resume(session) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/console_agent/repl.rb', line 175 def resume(session) init_interactive_state # Restore state from the previous session @history = JSON.parse(session.conversation, symbolize_names: true) @interactive_session_id = session.id @interactive_query = session.query @interactive_session_name = session.name @total_input_tokens = session.input_tokens || 0 @total_output_tokens = session.output_tokens || 0 # Seed the capture buffer with previous output so it's preserved on save @interactive_console_capture.write(session.console_output.to_s) # Replay to the user via the real stdout (bypass TeeIO to avoid double-capture) if session.console_output && !session.console_output.strip.empty? @interactive_old_stdout.puts "\e[2m--- Replaying previous session output ---\e[0m" @interactive_old_stdout.puts session.console_output @interactive_old_stdout.puts "\e[2m--- End of previous output ---\e[0m" @interactive_old_stdout.puts end interactive_loop end |