Method: Cli::Actions.run_coder

Defined in:
lib/cli/actions.rb

.run_coder(options) ⇒ Object



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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/cli/actions.rb', line 126

def self.run_coder(options)
    config = Cli::Configuration.new
    auto = true #!!options[:auto]

    if options[:id]
        id = options[:id]
    else
        coders = PairProgrammer::Api::Coder.list
        if coders.empty?
            Cli::Display.error_message("you have not created any requirements yet")
            return
        end

        id = Cli::Display.select("Select which requirements you would like to work on", coders.inject({}) { |hash, coder| hash[coder["requirements"]] = coder["id"]; hash })
    end

    while true do
        # RETRY when Net::ReadTimeout
        spinner = Cli::Display.spinner
        spinner.auto_spin
        begin
            response = PairProgrammer::Api::Coder.run(id)
        rescue Net::ReadTimeout
            Cli::Display.error_message("connection timed out but coder is still running. reconnecting...")
            next
        ensure
            spinner.stop()
        end
        
        if response["running"]
            Cli::Display.info_message("assistant is still running, will try again in 20 seconds")
            sleep(20)
            next
        elsif response["reached_max_length"]
            Cli::Display.error_message("conversation has reached its context length due to limitations with LLMs")
            Cli::Display.error_message("please create a new set of requirements")
            return
        elsif response["error"]
            Cli::Display.error_message("there was an error processing the assistant's response")
            Cli::Display.info_message("retrying in 5 seconds")
            sleep(5)
            next
        end

        if response["available_tokens"] && response["available_tokens"] < 500
            Cli::Display.info_message("conversation is getting long and approaching context length limit")
            Cli::Display.info_message("this conversation has #{response["available_tokens"]} tokens left")
        end

        system_message = response["system_message"]
        if system_message.nil?
            next
        end

        response_required = true
        # TODO if there is explanation but no command then response is required
        if system_message["explanation"] && !system_message["explanation"].empty?
            Cli::Display.message("assistant", system_message["explanation"])
        end

        if system_message["command"]
            skip_command = false
            command_display = PairProgrammer::Command.display_command(system_message["command"], system_message["arguments"])
            Cli::Display.info_message(command_display) if command_display
            response_required = false
            # command overwrites
            if system_message["command"] == "comment"
                Cli::Display.message("assistant", system_message["arguments"]["comment"])
                response_required = true
            elsif system_message["command"] == "ask_question"
                Cli::Display.message("assistant", system_message["arguments"]["question"])
                response_required = true
            else
                if system_message["command"] == "write_file"
                    # this fails if file doesn't exist
                    file_path = PairProgrammer::Configuration.absolute_path(system_message["arguments"]["file_path"])
                    begin
                        original_content = File.read(file_path)
                    rescue Errno::ENOENT => e
                        Cli::Display.error_message("there was an error running the command, notifying assistant of error.")
                        PairProgrammer::Api::Coder.append_exception(id, e)
                        Cli::Display.info_message("retrying...")
                        next
                    end
                    
                    Cli::Display.dispaly_diff(original_content, system_message["arguments"]["content"])
                    
                    confirmation = Cli::Display.confirmation("Accept changes?")
                    if !confirmation
                        Cli::Display.info_message("changes rejected")
                        # PairProgrammer::Api::Coder.append_output(id, "COMMAND REJECTED")
                        skip_command = true
                        response_required = true
                    else
                        Cli::Display.info_message("changes accepted")
                    end
                end

                if !skip_command
                    output = nil
                    begin
                        output = PairProgrammer::Command.run(system_message["command"], system_message["arguments"])
                    rescue => e
                        Cli::Display.error_message("there was an error running the command, notifying assistant of error.")
                        PairProgrammer::Api::Coder.append_exception(id, e)
                        Cli::Display.info_message("retrying...")
                        next
                    end
                    PairProgrammer::Api::Coder.append_output(id, output, system_message["command"])
                end
            end
        end

        while true do
            # user does not have to respond if auto is true
            if !response_required && auto
                break
            end

            display = response_required ? "required" : "optional"
            message = Cli::Display.get_input("response (#{display}): ")
            if message.empty?
                if response_required
                    Cli::Display.error_message("response required")
                    next
                else
                    break
                end
            else
                PairProgrammer::Api::Coder.add_user_message(id, message)
                response_required = false
            end
        end
    end
end