Module: Legion::LLM::API::Native::Instances
- Extended by:
- Legion::Logging::Helper
- Defined in:
- lib/legion/llm/api/native/instances.rb
Class Method Summary collapse
- .find_registry_instance(instance_id) ⇒ Object
-
.instance_health(provider_name, inst_id) ⇒ Object
D14: display health is the per-instance settings hash the provider's discovery actor writes after each registry commit.
- .registered(app) ⇒ Object
- .registry_instances ⇒ Object
Class Method Details
.find_registry_instance(instance_id) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/legion/llm/api/native/instances.rb', line 83 def self.find_registry_instance(instance_id) # Try exact composite id match first (e.g. "ollama/local") exact = registry_instances.find { |inst| inst[:id] == instance_id } return exact if exact # Fall back to bare instance name, but guard against ambiguity matches = registry_instances.select { |inst| inst[:instance] == instance_id } return matches.first if matches.size == 1 return :ambiguous if matches.size > 1 # No match found nil end |
.instance_health(provider_name, inst_id) ⇒ Object
D14: display health is the per-instance settings hash the provider's discovery actor writes after each registry commit. The registry instance name here is the operator config name — the same settings key. Empty hash when the actor has not committed yet (cold boot).
75 76 77 78 79 80 81 |
# File 'lib/legion/llm/api/native/instances.rb', line 75 def self.instance_health(provider_name, inst_id) cfg = Legion::Settings.dig(:extensions, :llm, provider_name.to_sym, :instances, inst_id.to_sym) (cfg.is_a?(Hash) ? cfg : {})[:health] || {} rescue StandardError => e handle_exception(e, level: :warn, handled: true, operation: 'api.instances.health_lookup') {} 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 |
# 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! instances = Legion::LLM::API::Native::Instances.registry_instances json_response({ instances: instances, summary: { total: instances.size, providers: instances.map { |inst| inst[:provider] }.uniq.size } }) rescue StandardError => e handle_exception(e, level: :error, handled: true, operation: 'llm.api.instances.list') json_error('instance_inventory_error', e., 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! result = Legion::LLM::API::Native::Instances.find_registry_instance(instance_id) if result == :ambiguous halt json_error('ambiguous_instance_id', "Instance id '#{instance_id}' matches multiple providers; " \ 'use composite id (provider/instance) to disambiguate', status_code: 400) end halt json_error('instance_not_found', "Instance '#{instance_id}' not found", status_code: 404) unless result json_response({ instance: result }) rescue StandardError => e handle_exception(e, level: :error, handled: true, operation: 'llm.api.instances.get') json_error('instance_inventory_error', e., status_code: 500) end log.debug('[llm][api][instances] provider instance inventory routes registered') end |
.registry_instances ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/legion/llm/api/native/instances.rb', line 56 def self.registry_instances instances = [] Legion::LLM::Call::Registry.available.each do |provider_name| Legion::LLM::Call::Registry.instances_for(provider_name).each_key do |inst_id| instances << { id: "#{provider_name}/#{inst_id}", provider: provider_name.to_s, instance: inst_id.to_s, health: instance_health(provider_name, inst_id) } end end instances.sort_by { |inst| inst[:id] } end |