Module: Legion::LLM::ClaudeConfigLoader

Extended by:
Legion::Logging::Helper
Defined in:
lib/legion/llm/claude_config_loader.rb

Constant Summary collapse

CLAUDE_SETTINGS =
File.expand_path('~/.claude/settings.json')
CLAUDE_CONFIG =
File.expand_path('~/.claude.json')

Class Method Summary collapse

Class Method Details

.apply_api_keys(config) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/legion/llm/claude_config_loader.rb', line 36

def apply_api_keys(config)
  llm = Legion::LLM.settings
  providers = llm[:providers]

  if config[:anthropicApiKey] && providers.dig(:anthropic, :api_key).nil?
    providers[:anthropic][:api_key] = config[:anthropicApiKey]
    log.debug 'Imported Anthropic API key from Claude CLI config'
  end

  return unless config[:openaiApiKey] && providers.dig(:openai, :api_key).nil?

  providers[:openai][:api_key] = config[:openaiApiKey]
  log.debug 'Imported OpenAI API key from Claude CLI config'
end

.apply_claude_config(config) ⇒ Object



31
32
33
34
# File 'lib/legion/llm/claude_config_loader.rb', line 31

def apply_claude_config(config)
  apply_api_keys(config)
  apply_model_preference(config)
end

.apply_model_preference(config) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/legion/llm/claude_config_loader.rb', line 51

def apply_model_preference(config)
  return unless config[:preferredModel] || config[:model]

  model = config[:preferredModel] || config[:model]
  llm = Legion::LLM.settings
  return if llm[:default_model]

  llm[:default_model] = model
  log.debug "Imported model preference from Claude CLI config: #{model}"
end

.loadObject



14
15
16
17
18
19
# File 'lib/legion/llm/claude_config_loader.rb', line 14

def load
  config = read_json(CLAUDE_SETTINGS).merge(read_json(CLAUDE_CONFIG))
  return if config.empty?

  apply_claude_config(config)
end

.read_json(path) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/legion/llm/claude_config_loader.rb', line 21

def read_json(path)
  return {} unless File.exist?(path)

  require 'json'
  ::JSON.parse(File.read(path), symbolize_names: true)
rescue StandardError => e
  handle_exception(e, level: :debug)
  {}
end