Class: Senior::Agent Private

Inherits:
Object
  • Object
show all
Defined in:
lib/senior/agent.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Suggests code fixes

Instance Method Summary collapse

Constructor Details

#initialize(brain = Brains::OpenAI.new) ⇒ Agent

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Instantiates a new Agent



13
14
15
# File 'lib/senior/agent.rb', line 13

def initialize(brain = Brains::OpenAI.new)
  @brain = brain
end

Instance Method Details

#auto_debug(broken_method, args, broken_method_source = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Calls the given method continuously, using AI to attempt to fix it until it is no longer raises exceptions

Parameters:

  • broken_method (Method)

    A broken method to be fixed

  • args (Object)

    Arguments given to a broken method

  • broken_method_source (String|nil) (defaults to: nil)

    Source code of the broken method

Returns:

  • (Object)

    The return value of the previously broken but now fixed method



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/senior/agent.rb', line 27

def auto_debug(broken_method, args, broken_method_source = nil)
  broken_method.call(*args)
rescue StandardError => e
  puts "The invocation #{broken_method.name}(#{args}) failed. Debugging..."

  suggested_fix_method_source = brain.suggest_fix(
    erroneous_source: broken_method_source || broken_method.source,
    exception_backtrace: e.backtrace&.first.to_s
  )

  puts "\nSuggested fix:\n#{suggested_fix_method_source}\n\n"

  suggested_fix_method_name = eval(suggested_fix_method_source)

  auto_debug(method(suggested_fix_method_name), args, suggested_fix_method_source)
end

#suggest_fix(broken_method, args) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Suggests a fix for a broken method

Parameters:

  • broken_method (Method)

    A broken method to be fixed

  • args (Object)

    Arguments given to a broken method

Returns:

  • (String)

    The suggested fix



53
54
55
56
57
58
59
60
# File 'lib/senior/agent.rb', line 53

def suggest_fix(broken_method, args)
  broken_method.call(*args)
rescue StandardError => e
  brain.suggest_fix(
    erroneous_source: broken_method.source,
    exception_backtrace: e.backtrace&.first.to_s
  )
end