Class: AIRefactor::FileProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/ai_refactor/file_processor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prompt:, ai_client:, logger:, output_path: nil, options: {}) ⇒ FileProcessor

Returns a new instance of FileProcessor.



11
12
13
14
15
16
17
# File 'lib/ai_refactor/file_processor.rb', line 11

def initialize(prompt:, ai_client:, logger:, output_path: nil, options: {})
  @prompt = prompt
  @ai_client = ai_client
  @logger = logger
  @output_path = output_path
  @options = options
end

Instance Attribute Details

#input_file_pathObject (readonly)

Returns the value of attribute input_file_path.



9
10
11
# File 'lib/ai_refactor/file_processor.rb', line 9

def input_file_path
  @input_file_path
end

#loggerObject (readonly)

Returns the value of attribute logger.



9
10
11
# File 'lib/ai_refactor/file_processor.rb', line 9

def logger
  @logger
end

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/ai_refactor/file_processor.rb', line 9

def options
  @options
end

#output_pathObject (readonly)

Returns the value of attribute output_path.



9
10
11
# File 'lib/ai_refactor/file_processor.rb', line 9

def output_path
  @output_path
end

Instance Method Details

#output_exists?Boolean

Returns:

  • (Boolean)


19
20
21
22
# File 'lib/ai_refactor/file_processor.rb', line 19

def output_exists?
  return false unless output_path
  File.exist?(output_path)
end

#process!Object



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
# File 'lib/ai_refactor/file_processor.rb', line 24

def process!
  logger.debug("Processing #{@prompt.input_file_path} with prompt in #{options.prompt_file_path}")
  logger.debug("Options: #{options.inspect}")
  messages = @prompt.chat_messages
  if options[:review_prompt]
    logger.info "Review prompt:\n"
    messages.each do |message|
      logger.info "\n-- Start of prompt for Role #{message[:role]} --\n"
      logger.info message[:content]
      logger.info "\n-- End of prompt for Role #{message[:role]} --\n"
    end
    return [nil, "Skipped as review prompt was requested", nil]
  end

  content, finished_reason, usage = generate_next_message(messages, options, ai_max_attempts)

  content = if content && content.length > 0
    processed = block_given? ? yield(content) : content
    if output_path
      write_output(output_path, processed)
      logger.verbose "Wrote output to #{output_path}..."
    end
    processed
  end

  [content, finished_reason, usage]
end