Module: Legion::LLM::API::Namespaces::Native::Providers

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

Class Method Summary collapse

Class Method Details

.registered(ns_context) ⇒ Object



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
# File 'lib/legion/llm/api/namespaces/native/providers.rb', line 17

def self.registered(ns_context)
  log.debug('[llm][api][namespaces][providers] registering routes')

  ns_context.get '' do
    log.debug('[llm][api][namespaces][providers] action=list_providers')
    require_llm!

    instances = begin
      Legion::LLM::Call::Registry.all_instances
    rescue StandardError => e
      handle_exception(e, level: :warn, handled: true, operation: 'llm.api.providers.registry_read')
      []
    end

    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][namespaces][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

  ns_context.get '/:name' do
    provider_name = params[:name].to_s
    log.debug("[llm][api][namespaces][providers] action=get_provider name=#{provider_name}")
    require_llm!

    provider_sym = provider_name.to_sym

    instances = begin
      Legion::LLM::Call::Registry.all_instances
    rescue StandardError => e
      handle_exception(e, level: :warn, handled: true, operation: 'llm.api.providers.registry_read')
      []
    end

    family = instances.select { |entry| entry[:provider].to_sym == provider_sym }

    unless family.any?
      log.debug("[llm][api][namespaces][providers] action=not_found name=#{provider_name}")
      halt json_error('provider_not_found', "Provider '#{provider_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][namespaces][providers] action=found name=#{provider_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][namespaces][providers] routes registered')
end