Class: LlmFixer::StaticAnalysisFixer

Inherits:
Object
  • Object
show all
Defined in:
lib/llm_fixer/static_analysis_fixer.rb

Constant Summary collapse

DEFAULT_MODEL =
"gpt-4o"
DEFAULT_API_BASE =
"https://api.openai.com/v1"

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ StaticAnalysisFixer

Returns a new instance of StaticAnalysisFixer.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/llm_fixer/static_analysis_fixer.rb', line 15

def initialize(api_key)
  @client = OpenAI::Client.new(
    access_token: ENV.fetch("LLM_API_KEY", api_key),
    uri_base: ENV.fetch("LLM_API_BASE", DEFAULT_API_BASE),
    request_timeout: 600,
  )
  @model = ENV.fetch("LLM_MODEL", DEFAULT_MODEL)
  @reasoning_effort = ENV.fetch("LLM_REASONING_EFFORT", nil)
  puts "LLM model: #{@model}"
  puts "Reasoning effort: #{@reasoning_effort}" if @reasoning_effort
end

Instance Method Details

#fix_file(args, additional_prompt = nil) ⇒ Object



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
# File 'lib/llm_fixer/static_analysis_fixer.rb', line 27

def fix_file(args, additional_prompt = nil)
  command = args.join(" ")
  output, succeeded = run_command(command)
  if succeeded
    puts "No errors found"
    return true
  end

  # Search for existing file paths from command line arguments in reverse order
  file_path = args.reverse.find { |arg| File.exist?(arg) }
  return false unless file_path

  # Request correction from LLM
  result = generate_fix(file_path, command, output, additional_prompt)
  return false unless result

  File.write(file_path, result)

  # Check again
  _, succeeded = run_command(command)
  if succeeded
    puts "#{file_path} was fixed!".green
    true
  else
    puts "#{file_path} was not fixed!".red
    false
  end
end