Module: Legion::LLM::API::Native::Tiers
- Extended by:
- Legion::Logging::Helper
- Defined in:
- lib/legion/llm/api/native/tiers.rb
Constant Summary collapse
- CAPABILITY_NAMES_BY_OPERATION =
Legacy display names for supported operations (same mapping the LegacyCoordinatorAdapter and provider actors use for the capabilities display surface).
{ chat: :completion, stream_chat: :streaming, embed: :embedding, image: :image, transcribe: :audio_transcription, translate: :audio_transcription, speak: :audio_speech, moderate: :moderation }.freeze
Class Method Summary collapse
- .build_model_entry(offering) ⇒ Object
-
.build_tiers_tree ⇒ Object
D14: the tier tree is projected from the NEW Registry snapshot (model_catalog.rb is the in-repo precedent).
-
.instance_health_display(availability) ⇒ Object
Legacy response shape: a single circuit-state string per instance.
- .offering_capabilities(offering) ⇒ Object
- .offering_limits(offering) ⇒ Object
-
.registered(app) ⇒ Object
rubocop:disable Metrics/AbcSize.
Class Method Details
.build_model_entry(offering) ⇒ Object
224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/legion/llm/api/native/tiers.rb', line 224 def self.build_model_entry(offering) { id: offering.model.to_s, offering_id: offering.offering_id.to_s, type: offering.operation_status(operation: :embed) == :supported ? 'embedding' : 'inference', capabilities: offering_capabilities(offering), limits: offering_limits(offering), enabled: true, cost: {}, model_family: offering.[:model_family]&.to_s }.compact end |
.build_tiers_tree ⇒ Object
D14: the tier tree is projected from the NEW Registry snapshot (model_catalog.rb is the in-repo precedent). Instance-level health display derives from the instance's AvailabilityFact — the same fact the provider actors copy into the settings health hash — because the tree is keyed by the derived instance id, which the settings hash (keyed by config name) cannot address. The AvailabilityFact remains the routing authority; this is display.
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/legion/llm/api/native/tiers.rb', line 179 def self.build_tiers_tree snapshot = Legion::LLM::Inventory.snapshot inst_by_key = {} snapshot.each_instance { |inst| inst_by_key[inst.instance_key] = inst } grouped = {} snapshot.each_offering do |offering| # Compliance-by-absence: denied models never appear in the tier # view (same §9.5 policy as the offerings surface). next unless Legion::LLM::API::Native::Offerings.policy_permits?(offering) ik = offering.instance_key tier_name = offering.tier.to_s provider_name = ik.provider_family.to_s instance_name = ik.instance_id.to_s grouped[tier_name] ||= { available: Legion::LLM::Router.tier_available?(tier_name.to_sym), providers: {} } grouped[tier_name][:providers][provider_name] ||= { instances: {} } grouped[tier_name][:providers][provider_name][:instances][instance_name] ||= { health: instance_health_display(inst_by_key[ik]&.availability), capabilities: [], models: [] } inst = grouped[tier_name][:providers][provider_name][:instances][instance_name] inst[:capabilities] = (inst[:capabilities] + offering_capabilities(offering)).uniq.sort inst[:models] << build_model_entry(offering) end # Sort tiers by priority order. Router.tier_priority yields symbols; # the tree is keyed by tier Strings, so normalize to avoid duplicate # (symbol + string) keys for the same tier. priority = Legion::LLM::Router.tier_priority.map(&:to_s) sorted = {} priority.each { |t| sorted[t] = grouped.delete(t) if grouped.key?(t) } grouped.each { |t, v| sorted[t] = v } # Ensure all priority tiers appear even if empty priority.each do |t| sorted[t] ||= { available: Legion::LLM::Router.tier_available?(t.to_sym), providers: {} } end sorted end |
.instance_health_display(availability) ⇒ Object
Legacy response shape: a single circuit-state string per instance.
238 239 240 241 242 243 244 |
# File 'lib/legion/llm/api/native/tiers.rb', line 238 def self.instance_health_display(availability) case availability&.state when :available then 'closed' when :unavailable then 'open' else 'unknown' end end |
.offering_capabilities(offering) ⇒ Object
246 247 248 249 250 251 252 |
# File 'lib/legion/llm/api/native/tiers.rb', line 246 def self.offering_capabilities(offering) caps = offering.supported_operations.map { |op| CAPABILITY_NAMES_BY_OPERATION.fetch(op, op) } offering.capability_evidence.each do |capability, evidence| caps << capability if evidence.status == :supported end caps.map(&:to_s) end |
.offering_limits(offering) ⇒ Object
254 255 256 257 258 259 |
# File 'lib/legion/llm/api/native/tiers.rb', line 254 def self.offering_limits(offering) limits = {} limits[:context_window] = offering.context_evidence.value if offering.context_evidence.known? limits[:max_output_tokens] = offering.max_output_evidence.value if offering.max_output_evidence.known? limits end |
.registered(app) ⇒ Object
rubocop:disable Metrics/AbcSize
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 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 113 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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/legion/llm/api/native/tiers.rb', line 13 def self.registered(app) # rubocop:disable Metrics/AbcSize log.debug('[llm][api][tiers] registering tier routes') app.get '/api/llm/tiers' do require_llm! tiers_data = Legion::LLM::API::Native::Tiers.build_tiers_tree json_response({ tiers: tiers_data, priority: Legion::LLM::Router.tier_priority, privacy_mode: Legion::LLM::Router.privacy_mode? }) rescue StandardError => e handle_exception(e, level: :error, handled: true, operation: 'llm.api.tiers.list') json_error('tiers_error', e., status_code: 500) end app.get '/api/llm/tiers/:tier' do require_llm! tier_name = params[:tier].to_s tiers_data = Legion::LLM::API::Native::Tiers.build_tiers_tree tier = tiers_data[tier_name] halt json_error('tier_not_found', "Tier '#{tier_name}' not found", status_code: 404) unless tier json_response({ tier: tier_name, **tier }) rescue StandardError => e handle_exception(e, level: :error, handled: true, operation: 'llm.api.tiers.get') json_error('tiers_error', e., status_code: 500) end app.get '/api/llm/tiers/:tier/providers' do require_llm! tier_name = params[:tier].to_s tiers_data = Legion::LLM::API::Native::Tiers.build_tiers_tree tier = tiers_data[tier_name] halt json_error('tier_not_found', "Tier '#{tier_name}' not found", status_code: 404) unless tier json_response({ tier: tier_name, providers: tier[:providers] }) rescue StandardError => e handle_exception(e, level: :error, handled: true, operation: 'llm.api.tiers.providers') json_error('tiers_error', e., status_code: 500) end app.get '/api/llm/tiers/:tier/providers/:provider' do require_llm! tier_name = params[:tier].to_s provider_name = params[:provider].to_s tiers_data = Legion::LLM::API::Native::Tiers.build_tiers_tree tier = tiers_data[tier_name] halt json_error('tier_not_found', "Tier '#{tier_name}' not found", status_code: 404) unless tier provider = tier.dig(:providers, provider_name) halt json_error('provider_not_found', "Provider '#{provider_name}' not found in tier '#{tier_name}'", status_code: 404) unless provider json_response({ tier: tier_name, provider: provider_name, **provider }) rescue StandardError => e handle_exception(e, level: :error, handled: true, operation: 'llm.api.tiers.provider') json_error('tiers_error', e., status_code: 500) end app.get '/api/llm/tiers/:tier/providers/:provider/instances' do require_llm! tier_name = params[:tier].to_s provider_name = params[:provider].to_s tiers_data = Legion::LLM::API::Native::Tiers.build_tiers_tree tier = tiers_data[tier_name] halt json_error('tier_not_found', "Tier '#{tier_name}' not found", status_code: 404) unless tier provider = tier.dig(:providers, provider_name) halt json_error('provider_not_found', "Provider '#{provider_name}' not found in tier '#{tier_name}'", status_code: 404) unless provider json_response({ tier: tier_name, provider: provider_name, instances: provider[:instances] }) rescue StandardError => e handle_exception(e, level: :error, handled: true, operation: 'llm.api.tiers.instances') json_error('tiers_error', e., status_code: 500) end app.get '/api/llm/tiers/:tier/providers/:provider/instances/:instance' do require_llm! tier_name = params[:tier].to_s provider_name = params[:provider].to_s instance_name = params[:instance].to_s tiers_data = Legion::LLM::API::Native::Tiers.build_tiers_tree tier = tiers_data[tier_name] halt json_error('tier_not_found', "Tier '#{tier_name}' not found", status_code: 404) unless tier provider = tier.dig(:providers, provider_name) halt json_error('provider_not_found', "Provider '#{provider_name}' not found in tier '#{tier_name}'", status_code: 404) unless provider instance = provider.dig(:instances, instance_name) halt json_error('instance_not_found', "Instance '#{instance_name}' not found", status_code: 404) unless instance json_response({ tier: tier_name, provider: provider_name, instance: instance_name, **instance }) rescue StandardError => e handle_exception(e, level: :error, handled: true, operation: 'llm.api.tiers.instance') json_error('tiers_error', e., status_code: 500) end app.get '/api/llm/tiers/:tier/providers/:provider/instances/:instance/models' do require_llm! tier_name = params[:tier].to_s provider_name = params[:provider].to_s instance_name = params[:instance].to_s tiers_data = Legion::LLM::API::Native::Tiers.build_tiers_tree tier = tiers_data[tier_name] halt json_error('tier_not_found', "Tier '#{tier_name}' not found", status_code: 404) unless tier provider = tier.dig(:providers, provider_name) halt json_error('provider_not_found', "Provider '#{provider_name}' not found in tier '#{tier_name}'", status_code: 404) unless provider instance = provider.dig(:instances, instance_name) halt json_error('instance_not_found', "Instance '#{instance_name}' not found", status_code: 404) unless instance json_response({ tier: tier_name, provider: provider_name, instance: instance_name, models: instance[:models] }) rescue StandardError => e handle_exception(e, level: :error, handled: true, operation: 'llm.api.tiers.instance_models') json_error('tiers_error', e., status_code: 500) end app.get '/api/llm/tiers/:tier/providers/:provider/models' do require_llm! tier_name = params[:tier].to_s provider_name = params[:provider].to_s tiers_data = Legion::LLM::API::Native::Tiers.build_tiers_tree tier = tiers_data[tier_name] halt json_error('tier_not_found', "Tier '#{tier_name}' not found", status_code: 404) unless tier provider = tier.dig(:providers, provider_name) halt json_error('provider_not_found', "Provider '#{provider_name}' not found in tier '#{tier_name}'", status_code: 404) unless provider all_models = provider[:instances].values.flat_map { |inst| inst[:models] } seen = {} unique_models = all_models.select { |m| seen[m[:id]] ? false : (seen[m[:id]] = true) } json_response({ tier: tier_name, provider: provider_name, models: unique_models }) rescue StandardError => e handle_exception(e, level: :error, handled: true, operation: 'llm.api.tiers.provider_models') json_error('tiers_error', e., status_code: 500) end log.debug('[llm][api][tiers] tier routes registered') end |