Module: Legion::LLM::Fleet::Dispatcher
- Extended by:
- Legion::Logging::Helper
- Defined in:
- lib/legion/llm/fleet/dispatcher.rb
Class Method Summary collapse
- .build_routing_key(provider:, request_type:, model:) ⇒ Object
-
.dispatch(model: nil, messages: nil, request: nil, message_context: {}, routing_key: nil, reply_to: nil, **opts) ⇒ Object
Backwards-compatible shim: supports old (model:, messages:) and new (request:, message_context:) callers.
- .error_result(reason, message_context: {}) ⇒ Object
- .fleet_available? ⇒ Boolean
- .fleet_enabled? ⇒ Boolean
- .publish_request(**opts) ⇒ Object
- .resolve_timeout(request_type: :default, override: nil) ⇒ Object
- .sanitize_model(model) ⇒ Object
- .timeout_result(correlation_id, timeout, message_context: {}) ⇒ Object
- .transport_ready? ⇒ Boolean
- .wait_for_response(correlation_id, timeout:, message_context: {}) ⇒ Object
Class Method Details
.build_routing_key(provider:, request_type:, model:) ⇒ Object
55 56 57 |
# File 'lib/legion/llm/fleet/dispatcher.rb', line 55 def build_routing_key(provider:, request_type:, model:) "llm.request.#{provider}.#{request_type}.#{sanitize_model(model)}" end |
.dispatch(model: nil, messages: nil, request: nil, message_context: {}, routing_key: nil, reply_to: nil, **opts) ⇒ Object
Backwards-compatible shim: supports old (model:, messages:) and new (request:, message_context:) callers
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 |
# File 'lib/legion/llm/fleet/dispatcher.rb', line 14 def dispatch(model: nil, messages: nil, request: nil, message_context: {}, routing_key: nil, reply_to: nil, **opts) return error_result('fleet_unavailable', message_context: ) unless fleet_available? # Old calling convention: build minimal params from model/messages if request.nil? && (model || ) provider = opts[:provider] || 'ollama' request_type = opts[:request_type] || 'chat' routing_key ||= build_routing_key(provider: provider, request_type: request_type, model: model) reply_to ||= ReplyDispatcher.agent_queue_name correlation_id = publish_request( routing_key: routing_key, reply_to: reply_to, provider: provider, model: model, request_type: request_type, messages: , message_context: , **opts ) timeout = resolve_timeout(request_type: request_type, override: opts[:timeout]) return wait_for_response(correlation_id, timeout: timeout, message_context: ) end # New calling convention request_opts = if request.respond_to?(:to_h) request.to_h.transform_keys(&:to_sym) else {} end request_opts = request_opts.merge(opts) provider = request_opts[:provider] || 'ollama' request_type = request_opts[:request_type] || 'chat' model = request_opts[:model] routing_key ||= build_routing_key(provider: provider, request_type: request_type, model: model) reply_to ||= ReplyDispatcher.agent_queue_name correlation_id = publish_request( routing_key: routing_key, reply_to: reply_to, provider: provider, model: model, request_type: request_type, message_context: , **request_opts.except(:provider, :model, :request_type, :timeout) ) timeout = resolve_timeout(request_type: request_type, override: request_opts[:timeout] || opts[:timeout]) wait_for_response(correlation_id, timeout: timeout, message_context: ) end |
.error_result(reason, message_context: {}) ⇒ Object
128 129 130 |
# File 'lib/legion/llm/fleet/dispatcher.rb', line 128 def error_result(reason, message_context: {}) { success: false, error: reason, message_context: } end |
.fleet_available? ⇒ Boolean
63 64 65 |
# File 'lib/legion/llm/fleet/dispatcher.rb', line 63 def fleet_available? transport_ready? && fleet_enabled? end |
.fleet_enabled? ⇒ Boolean
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/legion/llm/fleet/dispatcher.rb', line 72 def fleet_enabled? return true unless defined?(Legion::Settings) settings = begin Legion::Settings[:llm] rescue StandardError => e handle_exception(e, level: :debug, operation: 'llm.fleet.dispatcher.fleet_enabled') nil end return true unless settings.is_a?(Hash) routing = settings[:routing] return true unless routing.is_a?(Hash) routing.fetch(:use_fleet, true) end |
.publish_request(**opts) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/legion/llm/fleet/dispatcher.rb', line 99 def publish_request(**opts) correlation_id = "req_#{SecureRandom.uuid}" opts[:fleet_correlation_id] = correlation_id log.debug("[llm][fleet][dispatcher] action=publish_request correlation_id=#{correlation_id} routing_key=#{opts[:routing_key]}") if defined?(Legion::LLM::Transport::Messages::FleetRequest) Legion::LLM::Transport::Messages::FleetRequest.new(**opts).publish else log.debug('[llm][fleet][dispatcher] action=skip_publish reason=transport_not_loaded') end correlation_id end |
.resolve_timeout(request_type: :default, override: nil) ⇒ Object
89 90 91 92 93 94 95 96 97 |
# File 'lib/legion/llm/fleet/dispatcher.rb', line 89 def resolve_timeout(request_type: :default, override: nil) return override if override fleet = Legion::LLM.settings.dig(:routing, :tiers, :fleet) || {} fleet.dig(:timeouts, request_type.to_sym) || fleet[:timeout_seconds] || 30 rescue StandardError => e handle_exception(e, level: :debug, operation: 'llm.fleet.dispatcher.resolve_timeout') 30 end |
.sanitize_model(model) ⇒ Object
59 60 61 |
# File 'lib/legion/llm/fleet/dispatcher.rb', line 59 def sanitize_model(model) model.to_s.gsub(':', '.') end |
.timeout_result(correlation_id, timeout, message_context: {}) ⇒ Object
123 124 125 126 |
# File 'lib/legion/llm/fleet/dispatcher.rb', line 123 def timeout_result(correlation_id, timeout, message_context: {}) { success: false, error: 'fleet_timeout', correlation_id: correlation_id, timeout: timeout, message_context: } end |
.transport_ready? ⇒ Boolean
67 68 69 70 |
# File 'lib/legion/llm/fleet/dispatcher.rb', line 67 def transport_ready? !!(defined?(Legion::Settings) && Legion::Settings[:transport][:connected] == true) end |
.wait_for_response(correlation_id, timeout:, message_context: {}) ⇒ Object
113 114 115 116 117 118 119 120 121 |
# File 'lib/legion/llm/fleet/dispatcher.rb', line 113 def wait_for_response(correlation_id, timeout:, message_context: {}) future = ReplyDispatcher.register(correlation_id) result = future.value!(timeout) result || timeout_result(correlation_id, timeout, message_context: ) rescue Concurrent::CancelledOperationError timeout_result(correlation_id, timeout, message_context: ) ensure ReplyDispatcher.deregister(correlation_id) end |