Module: Legion::LLM::API::Native::Providers

Extended by:
Legion::Logging::Helper
Defined in:
lib/legion/llm/api/native/providers.rb

Constant Summary collapse

SECRET_KEYS =
%w[
  api_key secret_key bearer_token session_token auth_token authorization password
].freeze

Class Method Summary collapse

Class Method Details

.config_value(config, key, default = nil) ⇒ Object



89
90
91
92
93
94
95
96
# File 'lib/legion/llm/api/native/providers.rb', line 89

def self.config_value(config, key, default = nil)
  return default unless config.respond_to?(:key?)

  string_key = key.to_s
  return config[string_key] if config.key?(string_key)

  config.key?(key) ? config[key] : default
end

.enabled_provider_config?(config) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/legion/llm/api/native/providers.rb', line 85

def self.enabled_provider_config?(config)
  config.is_a?(Hash) && config_value(config, :enabled, true) != false
end

.find_provider_config(name) ⇒ Object



80
81
82
83
# File 'lib/legion/llm/api/native/providers.rb', line 80

def self.find_provider_config(name)
  providers = settings_value(:providers, {})
  providers[name.to_sym] || providers[name.to_s]
end

.provider_health(name) ⇒ Object



119
120
121
122
123
124
125
126
127
128
# File 'lib/legion/llm/api/native/providers.rb', line 119

def self.provider_health(name)
  if Legion::LLM::Router.routing_enabled?
    tracker = Legion::LLM::Router.health_tracker
    provider_key = name.to_sym
    { circuit_state: tracker.circuit_state(provider_key).to_s,
      adjustment:    tracker.adjustment(provider_key) }
  else
    { circuit_state: 'unknown' }
  end
end

.redact_value(value) ⇒ Object



108
109
110
111
112
113
114
115
116
117
# File 'lib/legion/llm/api/native/providers.rb', line 108

def self.redact_value(value)
  case value
  when Hash
    safe_config(value)
  when Array
    value.map { |entry| redact_value(entry) }
  else
    value
  end
end

.registered(app) ⇒ Object



16
17
18
19
20
21
22
23
24
25
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/legion/llm/api/native/providers.rb', line 16

def self.registered(app)
  log.debug('[llm][api][providers] registering provider routes')

  app.get '/api/llm/providers' do
    log.debug('[llm][api][providers] action=list_providers')
    require_llm!

    providers_config = Legion::LLM::API::Native::Providers.settings_value(:providers, {})
    provider_list = providers_config.filter_map do |name, config|
      next unless Legion::LLM::API::Native::Providers.enabled_provider_config?(config)

      provider_name = name.to_s

      {
        name:          provider_name,
        enabled:       true,
        default_model: Legion::LLM::API::Native::Providers.config_value(config, :default_model),
        health:        Legion::LLM::API::Native::Providers.provider_health(provider_name),
        native:        Legion::LLM::Call::Registry.registered?(provider_name.to_sym)
      }
    end

    summary = {
      total:           provider_list.size,
      native:          provider_list.count { |p| p[:native] },
      routing_enabled: Legion::LLM::Router.routing_enabled?
    }

    log.debug("[llm][api][providers] action=listed count=#{provider_list.size}")
    json_response({ providers: provider_list, summary: summary })
  rescue StandardError => e
    handle_exception(e, level: :error, handled: true, operation: 'llm.api.providers.list')
    json_error('provider_error', e.message, status_code: 500)
  end

  app.get '/api/llm/providers/:name' do
    log.debug("[llm][api][providers] action=get_provider name=#{params[:name]}")
    require_llm!

    provider_name = params[:name].to_s
    provider_config = Legion::LLM::API::Native::Providers.find_provider_config(provider_name)

    unless Legion::LLM::API::Native::Providers.enabled_provider_config?(provider_config)
      log.debug("[llm][api][providers] action=not_found name=#{params[:name]}")
      halt json_error('provider_not_found', "Provider '#{params[:name]}' not found or disabled", status_code: 404)
    end

    log.debug("[llm][api][providers] action=found name=#{params[:name]}")
    json_response({
                    name:          provider_name,
                    enabled:       true,
                    default_model: Legion::LLM::API::Native::Providers.config_value(provider_config, :default_model),
                    health:        Legion::LLM::API::Native::Providers.provider_health(provider_name),
                    native:        Legion::LLM::Call::Registry.registered?(provider_name.to_sym),
                    config:        Legion::LLM::API::Native::Providers.safe_config(provider_config)
                  })
  rescue StandardError => e
    handle_exception(e, level: :error, handled: true, operation: 'llm.api.providers.get')
    json_error('provider_error', e.message, status_code: 500)
  end

  log.debug('[llm][api][providers] provider routes registered')
end

.safe_config(config) ⇒ Object



102
103
104
105
106
# File 'lib/legion/llm/api/native/providers.rb', line 102

def self.safe_config(config)
  config.each_with_object({}) do |(key, value), safe|
    safe[key] = redact_value(value) unless SECRET_KEYS.include?(key.to_s)
  end
end

.settings_value(key, default = nil) ⇒ Object



98
99
100
# File 'lib/legion/llm/api/native/providers.rb', line 98

def self.settings_value(key, default = nil)
  Legion::LLM::Settings.value(key, default: default)
end