Class: IRB::ExtendCommand::Explain

Inherits:
Nop
  • Object
show all
Defined in:
lib/irb/ai.rb

Constant Summary collapse

IRB_PATH =
Gem.loaded_specs["irb"].full_gem_path

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.transform_args(args) ⇒ Object



258
259
260
261
262
263
264
265
# File 'lib/irb/ai.rb', line 258

def self.transform_args(args)
  # Return a string literal as is for backward compatibility
  if args.empty? || string_literal?(args)
    args
  else # Otherwise, consider the input as a String for convenience
    args.strip.dump
  end
end

Instance Method Details

#execute(*args) ⇒ Object



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/irb/ai.rb', line 267

def execute(*args)
  input = args.first&.chomp

  expression, context_obj_expression = input.split(/\sas\s/, 2)

  unless expression
    puts "Please provide the expression to explain. Usage: `explain <expression> [as <context object>]]`"
    return
  end

  context_binding = irb_context.workspace.binding

  context_obj =
    if context_obj_expression
      eval(context_obj_expression, context_binding)
    else
      irb_context.workspace.main
    end

  return_value, exception, traces =
    IRB::AI.execute_expression(
      expression: expression,
      context_binding: context_binding,
      context_obj: context_obj
    )

  response =
    IRB::AI.send_messages(
      expression: expression,
      context_binding: context_binding,
      context_obj: context_obj,
      traces: traces,
      exception: exception,
      return_value: return_value
    )

  while error = response.dig("error", "message")
    if error.match?(/reduce the length/)
      if traces.length >= 1
        puts "The generated request is too long. Trying again with reduced runtimne traces..."
        traces = traces.last(traces.length / 2)
        response =
          IRB::AI.send_messages(
            expression: expression,
            context_binding: context_binding,
            context_obj: context_obj,
            traces: traces,
            exception: exception,
            return_value: return_value
          )
      else
        puts "The generated request is too long even without runtime traces. Please try again with a shorter expression."
        return
      end
    else
      puts "OpenAI returned an error: #{error["message"]}"
      return
    end
  end

  finish_reason = response.dig("choices", 0, "finish_reason")

  case finish_reason
  when "stop"
  when "length"
  else
    puts "OpenAI did not finish processing the request (reason: #{finish_reason}). Please try again."
    return
  end

  content = response.dig("choices", 0, "message", "content")
  puts content

  parsed = TTY::Markdown.parse(content)
  puts parsed
end