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
75
76
77
78
|
# 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!
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][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
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][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
|