Class: CommitCraft::AIClient

Inherits:
Object
  • Object
show all
Defined in:
lib/commitcraft/ai_client.rb

Instance Method Summary collapse

Constructor Details

#initialize(config = CommitCraft.configuration) ⇒ AIClient

Returns a new instance of AIClient.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/commitcraft/ai_client.rb', line 7

def initialize(config = CommitCraft.configuration)
  @config = config
  @config.validate!
  @client = Gemini.new(
    credentials: {
      service: "generative-language-api",
      api_key: @config.api_key
    },
    options: {
      model: @config.model,
      server_sent_events: true
    }
  )
end

Instance Method Details

#generate_commit_messages(diff, context = {}) ⇒ Object



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

def generate_commit_messages(diff, context = {})
  prompt = build_prompt(diff, context)

  result = @client.stream_generate_content({
                                             contents: { role: "user", parts: { text: prompt } }
                                           })

  # Collect the full response
  full_response = ""
  result.each do |event|
    next unless event.dig("candidates", 0, "content", "parts")

    event.dig("candidates", 0, "content", "parts").each do |part|
      full_response += part["text"] if part["text"]
    end
  end

  parse_response(full_response)
rescue StandardError => e
  raise AIError, "Failed to generate commit message: #{e.message}"
end