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



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/legion/llm/api/native/providers.rb', line 114

def self.instance_to_hash(entry)
  provider_key = entry[:provider].to_sym
  instance_key = entry[:instance].to_sym

  # Display health/capabilities live in the per-instance settings
  # hash the provider's discovery actor writes after each registry
  # commit (the visibility surface). The Registry AvailabilityFact
  # remains the routing authority. entry[:instance] is the config name
  # (Call::Registry keys instances by the operator config name), which
  # is exactly the settings key the actor writes under.
  instance_settings = begin
    cfg = Legion::Settings.dig(:extensions, :llm, provider_key, :instances, instance_key)
    cfg.is_a?(Hash) ? cfg : {}
  rescue StandardError => e
    handle_exception(e, level: :warn, handled: true, operation: 'api.providers.instance_settings')
    {}
  end

  tier = instance_settings[:tier] || entry.dig(:metadata, :tier)
  capabilities = Array(instance_settings[:capabilities]).map(&:to_s)
  capabilities = Array(entry.dig(:metadata, :capabilities)).map(&:to_s) if capabilities.empty?

  result = {
    provider:     entry[:provider].to_s,
    instance:     entry[:instance].to_s,
    tier:         tier&.to_s,
    capabilities: capabilities,
    health:       instance_settings[:health] || {},
    native:       true
  }
  result[:source] = entry.dig(:metadata, :source) if entry.dig(:metadata, :source)
  result[:credential_fingerprint] = entry.dig(:metadata, :credential_fingerprint) if entry.dig(:metadata, :credential_fingerprint)
  result
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
# 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!

    # A registry read FAULT must not masquerade as an empty
    # registry (a 200 with []). The fault propagates to the route's
    # 500 handler — logged there, error-shaped to the caller.
    instances = Legion::LLM::Call::Registry.all_instances

    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

    # A registry read FAULT must not masquerade as an empty
    # registry (a 200 with []). The fault propagates to the route's
    # 500 handler — logged there, error-shaped to the caller.
    instances = Legion::LLM::Call::Registry.all_instances

    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

.union_ssot_providers(provider_list) ⇒ Object

Providers surface in the listing from the Registry snapshot even when they are absent from the call registry (deduped by provider+instance).



79
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
105
106
107
108
109
110
111
112
# File 'lib/legion/llm/api/native/providers.rb', line 79

def self.union_ssot_providers(provider_list)
  return provider_list unless defined?(Legion::Extensions::Llm::Inventory::Registry)

  snapshot = begin
    Legion::Extensions::Llm::Inventory::Registry.snapshot
  rescue StandardError => e
    handle_exception(e, level: :warn, handled: true, operation: 'llm.api.providers.ssot_snapshot_read')
    nil
  end
  return provider_list if snapshot.nil?

  seen = provider_list.to_set { |p| [p[:provider].to_sym, p[:instance].to_sym] }
  snapshot.each_instance do |record|
    family = record.instance_key.provider_family.to_sym
    instance_id = record.instance_key.instance_id.to_sym
    next if seen.include?([family, instance_id])

    seen << [family, instance_id]
    lanes = record.lanes_by_id.values
    capabilities = lanes.first&.capability_evidence&.select do |_cap, evidence|
      evidence.status == :supported
    end&.keys&.map(&:to_s) || []
    provider_list << {
      provider:     family.to_s,
      instance:     instance_id.to_s,
      tier:         lanes.first&.tier&.to_s,
      capabilities: capabilities,
      health:       { state: record.availability.state.to_s },
      native:       true,
      source:       'ssot'
    }
  end
  provider_list
end