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

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

Class Method Summary collapse

Class Method Details

.instance_to_hash(entry) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/legion/llm/api/native/providers.rb', line 80

def self.instance_to_hash(entry)
  health = begin
    Legion::LLM::Router.health_tracker
  rescue StandardError
    nil
  end
  provider_key = entry[:provider].to_sym
  instance_key = entry[:instance].to_sym

  {
    provider:     entry[:provider].to_s,
    instance:     entry[:instance].to_s,
    tier:         entry.dig(:metadata, :tier)&.to_s,
    capabilities: Array(entry.dig(:metadata, :capabilities)).map(&:to_s),
    health:       if health
                    {
                      circuit_state: health.circuit_state(provider_key, instance: instance_key).to_s,
                      adjustment:    health.adjustment(provider_key, instance: instance_key)
                    }
                  else
                    {}
                  end,
    native:       true
  }
end

.registered(app) ⇒ Object



12
13
14
15
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 12

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!

    instances = begin
      Legion::LLM::Call::Registry.all_instances
    rescue StandardError => e
      handle_exception(e, level: :warn, handled: true, operation: 'llm.api.providers.registry_read')
      []
    end

    provider_list = instances.map do |entry|
      Legion::LLM::API::Native::Providers.instance_to_hash(entry)
    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_sym = provider_name.to_sym

    instances = begin
      Legion::LLM::Call::Registry.all_instances
    rescue StandardError => e
      handle_exception(e, level: :warn, handled: true, operation: 'llm.api.providers.registry_read')
      []
    end

    family = instances.select { |entry| entry[:provider].to_sym == provider_sym }

    unless family.any?
      log.debug("[llm][api][providers] action=not_found name=#{params[:name]}")
      halt json_error('provider_not_found',
                      "Provider '#{params[:name]}' not found",
                      status_code: 404)
    end

    provider_list = family.map do |entry|
      Legion::LLM::API::Native::Providers.instance_to_hash(entry)
    end

    log.debug("[llm][api][providers] action=found name=#{params[:name]} instances=#{provider_list.size}")
    json_response({ provider: provider_name, instances: provider_list })
  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