Class: Commiti::MessageGenerator

Inherits:
Object
  • Object
show all
Includes:
MessageGeneratorSupport
Defined in:
lib/services/message_generator.rb

Constant Summary collapse

COMMIT_PREFIX_ERROR =
'First line must start with a conventional commit type (feat:, fix:, etc.).'
DEFAULT_COMMIT_SUBJECT =
'update project files'
COMMIT_PREFIX_PATTERN =
/\A(feat|fix|chore|refactor|docs|style|test|perf|ci|build|revert)(\([^)]+\))?!?\s*:?\s*/i

Instance Method Summary collapse

Constructor Details

#initialize(flow_type:, run_stage:, text_generation_config: nil) ⇒ MessageGenerator

Returns a new instance of MessageGenerator.



13
14
15
16
17
# File 'lib/services/message_generator.rb', line 13

def initialize(flow_type:, run_stage:, text_generation_config: nil)
  @flow_type = flow_type
  @run_stage = run_stage
  @text_generation_config = text_generation_config || Commiti::TextGenerationStyle::DEFAULT_CONFIG
end

Instance Method Details

#generate_candidates(client:, prompt:, diff_metadata:, count:, model:) ⇒ Object



19
20
21
22
23
24
# File 'lib/services/message_generator.rb', line 19

def generate_candidates(client:, prompt:, diff_metadata:, count:, model:)
  (1..count).map do |index|
    puts "\nGenerating candidate #{index}/#{count}..."
    generate_with_quality_check(client: client, prompt: prompt, diff_metadata: , model: model)
  end
end

#generate_with_quality_check(client:, prompt:, diff_metadata:, model:) ⇒ Object



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
51
52
53
54
55
56
57
# File 'lib/services/message_generator.rb', line 26

def generate_with_quality_check(client:, prompt:, diff_metadata:, model:)
  message = clean_output(generate_from_client(
                           client: client,
                           system: prompt[:system],
                           user: prompt[:user],
                           model: model,
                           label: "Generating #{flow_type} with Google AI"
                         ))
  reason = invalid_generation_reason(message: message, diff_metadata: )
  return normalize_commit_message(message, diff_metadata: ) if reason.nil? && flow_type == :commit
  return message if reason.nil?

  puts "\nGenerated output looked weak: #{reason}"
  puts "Retrying once with stronger constraints...\n"

  retried_message = clean_output(generate_from_client(
                                   client: client,
                                   system: prompt[:system],
                                   user: retry_prompt(prompt:, reason: reason),
                                   model: model,
                                   label: "Regenerating #{flow_type} with stricter prompt"
                                 ))
  retry_reason = invalid_generation_reason(message: retried_message, diff_metadata: )
  if flow_type == :commit && retry_reason&.include?(COMMIT_PREFIX_ERROR)
    normalized_commit = normalize_commit_message(retried_message, diff_metadata: )
    return normalized_commit || retried_message
  end
  return normalize_commit_message(retried_message, diff_metadata: ) if retry_reason.nil? && flow_type == :commit
  return retried_message if retry_reason.nil?

  raise "Generated #{flow_type} is still invalid after retry: #{retry_reason}"
end