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: false, amend: false) ⇒ CommitMessageGenerator

Returns a new instance of CommitMessageGenerator.



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

def initialize(model:, provider:, max_tokens:, force_external: false, amend: false)
  @model = model
  @provider = provider
  @max_tokens = max_tokens
  @force_external = force_external
  @amend = amend
  infer_provider_from_model
  check_provider_availability

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

Instance Method Details

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



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/aigcm/commit_message_generator.rb', line 27

def generate(style_guide, context = [])
  diff = GitDiff.new(dir: Dir.pwd, amend: @amend).generate_diff
  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 = @chat.ask(prompt)
    response.content.strip
  rescue StandardError => e
    "Error generating commit message: #{e.message}"
  end
end