Module: Legion::LLM::API::Native::Instances

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

Class Method Summary collapse

Class Method Details

.aggregate_capacity(offerings) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/legion/llm/api/native/instances.rb', line 79

def self.aggregate_capacity(offerings)
  {
    max_context_window: offerings.filter_map { |offering| offering.dig(:limits, :context_window) }.max,
    max_output_tokens:  offerings.filter_map { |offering| offering.dig(:limits, :max_output_tokens) }.max,
    offering_count:     offerings.size
  }.compact
end

.aggregate_health(offerings) ⇒ Object



74
75
76
77
# File 'lib/legion/llm/api/native/instances.rb', line 74

def self.aggregate_health(offerings)
  states = offerings.filter_map { |offering| offering.dig(:health, :circuit_state) }.uniq.sort
  { circuit_states: states }
end

.instance_from_offerings(instance_id, offerings) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/legion/llm/api/native/instances.rb', line 59

def self.instance_from_offerings(instance_id, offerings)
  rows = Array(offerings)
  return nil if rows.empty?

  {
    instance_id:     instance_id.to_s,
    provider_family: rows.map { |offering| offering[:provider_family] }.uniq.sort.join(','),
    tiers:           rows.map { |offering| offering[:tier].to_s }.uniq.sort,
    transports:      rows.map { |offering| offering[:transport].to_s }.uniq.sort,
    health:          aggregate_health(rows),
    capacity:        aggregate_capacity(rows),
    offerings:       rows.sort_by { |offering| offering[:offering_id].to_s }
  }
end

.instances_from_offerings(offerings) ⇒ Object



52
53
54
55
56
57
# File 'lib/legion/llm/api/native/instances.rb', line 52

def self.instances_from_offerings(offerings)
  instances = offerings.group_by { |offering| offering[:instance_id] }.filter_map do |instance_id, rows|
    instance_from_offerings(instance_id, rows)
  end
  instances.sort_by { |instance| instance[:instance_id] }
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
# File 'lib/legion/llm/api/native/instances.rb', line 12

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

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

    offerings = Legion::LLM::Inventory.offerings
    instances = Legion::LLM::API::Native::Instances.instances_from_offerings(offerings)

    json_response({
                    instances: instances,
                    summary:   {
                      total:     instances.size,
                      providers: instances.map { |instance| instance[:provider_family] }.uniq.size
                    }
                  })
  rescue StandardError => e
    handle_exception(e, level: :error, handled: true, operation: 'llm.api.instances.list')
    json_error('instance_inventory_error', e.message, status_code: 500)
  end

  app.get '/api/llm/instances/:id' do
    instance_id = params[:id]
    log.debug("[llm][api][instances] action=get_instance id=#{instance_id}")
    require_llm!

    offerings = Legion::LLM::Inventory.offerings(instance_id: instance_id)
    instance = Legion::LLM::API::Native::Instances.instance_from_offerings(instance_id, offerings)
    halt json_error('instance_not_found', "Instance '#{instance_id}' not found", status_code: 404) unless instance

    json_response({ instance: instance })
  rescue StandardError => e
    handle_exception(e, level: :error, handled: true, operation: 'llm.api.instances.get')
    json_error('instance_inventory_error', e.message, status_code: 500)
  end

  log.debug('[llm][api][instances] provider instance inventory routes registered')
end