Class: Aigcm::CommitMessageGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/aigcm/commit_message_generator.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

MAX_DIFF_SIZE =

Characters

4000

Instance Method Summary collapse

Constructor Details

#initialize(model:, provider:, max_tokens:, force_external:) ⇒ CommitMessageGenerator

Returns a new instance of CommitMessageGenerator.



13
14
15
16
17
18
19
20
21
# File 'lib/aigcm/commit_message_generator.rb', line 13

def initialize(model:, provider:, max_tokens:, force_external:)
  @force_external = force_external
  validate_model_provider_combination(model)
  check_provider_availability

  @client = AiClient.new(model, provider: provider)
rescue StandardError => e
  raise Error, "Failed to initialize AI client: #{e.message}"
end

Instance Method Details

#generate(diff, style_guide, context = []) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/aigcm/commit_message_generator.rb', line 23

def generate(diff, style_guide, context = [])
  return "No changes to commit" if diff.strip.empty?

  check_repository_privacy unless @force_external

  # Truncate diff if too large
  if diff.length > MAX_DIFF_SIZE
    diff = diff[0...MAX_DIFF_SIZE] + "\n...[diff truncated]"
  end

  processed_context = process_context(context)
  prompt = build_prompt(diff, style_guide, processed_context)

  begin
    response = @client.chat(prompt)
    response.to_s.strip
  rescue StandardError => e
    "Error generating commit message: #{e.message}"
  end
end