Module: Legion::LLM::API::Native::Offerings

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

Class Method Summary collapse

Class Method Details

.compact_offering(offering) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/legion/llm/api/native/offerings.rb', line 192

def self.compact_offering(offering)
  {
    id:           offering[:offering_id] || offering[:id],
    model:        offering[:model].to_s,
    type:         offering[:type].to_s,
    model_family: offering[:model_family]&.to_s,
    capabilities: Array(offering[:capabilities]).map(&:to_s),
    limits:       offering[:limits] || {},
    enabled:      offering[:enabled] != false,
    cost:         offering[:cost] || {},
    health:       offering[:health] || {}
  }.compact
end

.group_offerings(offerings) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/legion/llm/api/native/offerings.rb', line 175

def self.group_offerings(offerings)
  grouped = {}

  offerings.each do |offering|
    tier = (offering[:tier] || :unknown).to_s
    provider = (offering[:provider_family] || :unknown).to_s
    instance = (offering[:instance_id] || offering[:provider_instance] || :default).to_s

    grouped[tier] ||= {}
    grouped[tier][provider] ||= {}
    grouped[tier][provider][instance] ||= []
    grouped[tier][provider][instance] << compact_offering(offering)
  end

  grouped
end

.lane_matches_filters?(lane, instance, filters) ⇒ Boolean

Returns:

  • (Boolean)


112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/legion/llm/api/native/offerings.rb', line 112

def self.lane_matches_filters?(lane, instance, filters)
  ik = lane.instance_key
  return false if filters[:provider] && ik.provider_family.to_s != filters[:provider].to_s
  return false if filters[:instance_id] && ik.instance_id.to_s != filters[:instance_id].to_s
  return false if filters[:model] && lane.model.to_s != filters[:model].to_s
  return false if filters[:tier] && lane.tier.to_s != filters[:tier].to_s
  return false if filters[:offering_id] && lane.lane_id != filters[:offering_id].to_s
  return false if filters[:model_family] && lane.[:model_family].to_s != filters[:model_family].to_s

  return false if filters[:type] && normalize_offering_type(filters[:type]) != lane_type(lane)

  if filters[:capability]
    caps = Legion::LLM::API::Native::Tiers.lane_capabilities(lane)
    return false unless caps.include?(filters[:capability].to_s)
  end
  if filters[:healthy]
    want_healthy = filters[:healthy].to_s != 'false'
    is_healthy = instance&.availability&.state == :available
    return false if want_healthy != is_healthy
  end

  true
end

.lane_type(lane) ⇒ Object



159
160
161
# File 'lib/legion/llm/api/native/offerings.rb', line 159

def self.lane_type(lane)
  Legion::Extensions::Llm::Taxonomies.lane_type_for(operation: lane.operation)
end

.normalize_offering_type(value) ⇒ Object

Request filter spellings for the coarse lane type: the embedding spellings collapse to :embedding, 'chat' to :inference (the v0.15.2 request vocabulary), everything else passes through as a Taxonomies::TYPES symbol.



167
168
169
170
171
172
173
# File 'lib/legion/llm/api/native/offerings.rb', line 167

def self.normalize_offering_type(value)
  case value.to_s
  when 'embedding', 'embeddings', 'embed' then :embedding
  when 'chat' then :inference
  else value.to_sym
  end
end

.offering_entry(lane, availability) ⇒ Object

The offering shape: same field names as v0.15.2, with offering_id/id set to the 5-tuple lane id. Registry lanes are always published (enabled).



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/legion/llm/api/native/offerings.rb', line 139

def self.offering_entry(lane, availability)
  {
    offering_id:       lane.lane_id,
    id:                lane.lane_id,
    model:             lane.model.to_s,
    provider_family:   lane.instance_key.provider_family.to_s,
    provider_instance: lane.instance_key.instance_id.to_s,
    instance_id:       lane.instance_key.instance_id.to_s,
    tier:              lane.tier.to_s,
    type:              lane_type(lane).to_s,
    model_family:      lane.[:model_family],
    capabilities:      Legion::LLM::API::Native::Tiers.lane_capabilities(lane),
    limits:            Legion::LLM::API::Native::Tiers.lane_limits(lane),
    enabled:           true,
    cost:              {},
    health:            Legion::LLM::API::Native::Models.health_display(availability),
    metadata:          lane.
  }
end

.policy_permits?(lane) ⇒ Boolean

Fail-closed model policy — same semantics as ModelCatalog.policy_permits?: a nonempty effective whitelist requires a case-insensitive substring match; a blacklist match always denies. The registry publishes the full provider catalog (policy is applied at selection time via SettingsState); the display surfaces apply it here so a denied model never appears in the API surface (compliance-by-absence invariant).

Returns:

  • (Boolean)


100
101
102
103
104
105
106
107
108
109
110
# File 'lib/legion/llm/api/native/offerings.rb', line 100

def self.policy_permits?(lane)
  policy = Legion::LLM::Routing::SettingsState.current.model_policy_for(offering: lane)
  whitelist = policy[:whitelist]
  blacklist = policy[:blacklist]
  model_lc = lane.model.to_s.downcase

  return false if whitelist.any? && whitelist.none? { |e| model_lc.include?(e.to_s.downcase) }
  return false if blacklist.any? { |e| model_lc.include?(e.to_s.downcase) }

  true
end

.registered(app) ⇒ Object



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/offerings.rb', line 15

def self.registered(app)
  log.debug('[llm][api][offerings] registering offering inventory routes')

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

    filters = Legion::LLM::API::Native::Offerings.request_filters(params)
    raw_offerings = Legion::LLM::API::Native::Offerings.snapshot_offerings(filters)
    grouped = Legion::LLM::API::Native::Offerings.group_offerings(raw_offerings)

    json_response({
                    offerings: grouped,
                    summary:   Legion::LLM::API::Native::Offerings.summary(raw_offerings)
                  })
  rescue StandardError => e
    handle_exception(e, level: :error, handled: true, operation: 'llm.api.offerings.list')
    json_error('offering_inventory_error', e.message, status_code: 500)
  end

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

    offering = Legion::LLM::API::Native::Offerings.snapshot_offering(offering_id)
    halt json_error('offering_not_found', "Offering '#{offering_id}' not found", status_code: 404) unless offering

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

  log.debug('[llm][api][offerings] offering inventory routes registered')
end

.request_filters(params) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/legion/llm/api/native/offerings.rb', line 52

def self.request_filters(params)
  {
    provider:     params[:provider] || params[:provider_family],
    instance_id:  params[:instance_id] || params[:instance],
    type:         params[:operation] || params[:type] || params[:purpose],
    model:        params[:model],
    offering_id:  params[:offering_id],
    model_family: params[:family] || params[:model_family],
    capability:   params[:capability],
    tier:         params[:tier],
    healthy:      params[:healthy]
  }
end

.snapshot_offering(offering_id) ⇒ Object

Resolves the 5-tuple id at /api/llm/offerings/:id.



84
85
86
87
88
89
90
91
# File 'lib/legion/llm/api/native/offerings.rb', line 84

def self.snapshot_offering(offering_id)
  snapshot = Legion::LLM::Inventory.snapshot
  lane = snapshot.lane(lane_id: offering_id.to_s)
  return nil unless lane && policy_permits?(lane)

  availability = snapshot.instance(instance_key: lane.instance_key)&.availability
  offering_entry(lane, availability)
end

.snapshot_offerings(filters) ⇒ Object

Offerings are projected from the registry snapshot — the single bucket of 5-tuple lanes. Display only; selection reads the AvailabilityFact itself, never these projections.



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/legion/llm/api/native/offerings.rb', line 69

def self.snapshot_offerings(filters)
  snapshot = Legion::LLM::Inventory.snapshot
  inst_by_key = {}
  snapshot.each_instance { |inst| inst_by_key[inst.instance_key] = inst }

  snapshot.each_lane.filter_map do |lane|
    instance = inst_by_key[lane.instance_key]
    next unless policy_permits?(lane)
    next unless lane_matches_filters?(lane, instance, filters)

    offering_entry(lane, instance&.availability)
  end
end

.summary(offerings) ⇒ Object



206
207
208
209
210
211
212
213
214
# File 'lib/legion/llm/api/native/offerings.rb', line 206

def self.summary(offerings)
  {
    total:     offerings.size,
    tiers:     offerings.map { |o| (o[:tier] || :unknown).to_s }.uniq.size,
    providers: offerings.map { |o| (o[:provider_family] || :unknown).to_s }.uniq.size,
    instances: offerings.map { |o| (o[:instance_id] || :default).to_s }.uniq.size,
    models:    offerings.map { |o| o[:model] }.uniq.size
  }
end