Module: Legion::LLM::Tools::Dispatcher
- Extended by:
- Legion::Logging::Helper
- Defined in:
- lib/legion/llm/tools/dispatcher.rb
Class Method Summary collapse
- .check_catalog_override(tool_name) ⇒ Object
- .check_override(tool_name) ⇒ Object
- .check_settings_override(tool_name) ⇒ Object
- .dispatch(tool_call:, source:, exchange_id: nil) ⇒ Object
- .dispatch_builtin(_tool_call, _source) ⇒ Object
- .dispatch_extension(tool_call, source) ⇒ Object
- .dispatch_mcp(tool_call, source) ⇒ Object
- .run_shadow(tool_call, _source, mcp_result) ⇒ Object
Class Method Details
.check_catalog_override(tool_name) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/legion/llm/tools/dispatcher.rb', line 69 def check_catalog_override(tool_name) return nil unless defined?(Legion::Extensions::Catalog::Registry) return nil unless Legion::LLM::OverrideConfidence.should_override?(tool_name) cap = Legion::Extensions::Catalog::Registry.for_override(tool_name) return nil unless cap { type: :extension, lex: cap.extension, runner: cap.runner, function: cap.function } end |
.check_override(tool_name) ⇒ Object
47 48 49 50 51 52 |
# File 'lib/legion/llm/tools/dispatcher.rb', line 47 def check_override(tool_name) settings_override = check_settings_override(tool_name) return settings_override if settings_override check_catalog_override(tool_name) end |
.check_settings_override(tool_name) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/legion/llm/tools/dispatcher.rb', line 54 def check_settings_override(tool_name) overrides = Legion::Settings.dig(:mcp, :overrides) rescue nil # rubocop:disable Style/RescueModifier return nil unless overrides.is_a?(Hash) override = overrides[tool_name] return nil unless override { type: :extension, lex: override[:lex] || override['lex'], runner: override[:runner] || override['runner'], function: override[:function] || override['function'] } end |
.dispatch(tool_call:, source:, exchange_id: nil) ⇒ Object
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 |
# File 'lib/legion/llm/tools/dispatcher.rb', line 13 def dispatch(tool_call:, source:, exchange_id: nil) start_time = Time.now if source[:type] == :mcp override = check_override(tool_call[:name]) if override overridden_source = source source = override.merge(overridden_from: overridden_source) end end result = case source[:type] when :mcp mcp_result = dispatch_mcp(tool_call, source) run_shadow(tool_call, source, mcp_result) mcp_result when :extension dispatch_extension(tool_call, source) when :builtin dispatch_builtin(tool_call, source) else { status: :error, error: "Unknown tool source type: #{source[:type]}" } end result.merge( source: source, exchange_id: exchange_id, duration_ms: ((Time.now - start_time) * 1000).to_i ) rescue StandardError => e handle_exception(e, level: :warn, operation: 'llm.tools.dispatcher.dispatch_tool_call', tool_name: tool_call[:name]) { status: :error, error: e., source: source, exchange_id: exchange_id } end |
.dispatch_builtin(_tool_call, _source) ⇒ Object
103 104 105 |
# File 'lib/legion/llm/tools/dispatcher.rb', line 103 def dispatch_builtin(_tool_call, _source) { status: :passthrough, result: nil } end |
.dispatch_extension(tool_call, source) ⇒ Object
93 94 95 96 97 98 99 100 101 |
# File 'lib/legion/llm/tools/dispatcher.rb', line 93 def dispatch_extension(tool_call, source) segments = (source[:lex] || '').delete_prefix('lex-').split('-') runner_path = (%w[Legion Extensions] + segments.map(&:capitalize) + ['Runners', source[:runner]]).join('::') runner = Kernel.const_get(runner_path) fn = source[:function].to_sym result = runner.send(fn, **(tool_call[:arguments] || {})) { status: :success, result: result } end |
.dispatch_mcp(tool_call, source) ⇒ Object
84 85 86 87 88 89 90 91 |
# File 'lib/legion/llm/tools/dispatcher.rb', line 84 def dispatch_mcp(tool_call, source) conn = ::Legion::MCP::Client::Pool.connection_for(source[:server]) raise "No connection for MCP server: #{source[:server]}" unless conn raw = conn.call_tool(name: tool_call[:name], arguments: tool_call[:arguments] || {}) content = raw[:content]&.map { |c| c[:text] || c['text'] }&.join("\n") { status: raw[:error] ? :error : :success, result: content } end |
.run_shadow(tool_call, _source, mcp_result) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/legion/llm/tools/dispatcher.rb', line 107 def run_shadow(tool_call, _source, mcp_result) tool_name = tool_call[:name] return unless Legion::LLM::OverrideConfidence.should_shadow?(tool_name) return unless defined?(Legion::Extensions::Catalog::Registry) cap = Legion::Extensions::Catalog::Registry.for_override(tool_name) return unless cap shadow_source = { type: :extension, lex: cap.extension, runner: cap.runner, function: cap.function } shadow_result = dispatch_extension(tool_call, shadow_source) if shadow_result[:status] == :success && mcp_result[:status] == :success Legion::LLM::OverrideConfidence.record_success(tool_name) else Legion::LLM::OverrideConfidence.record_failure(tool_name) end rescue StandardError => e Legion::LLM::OverrideConfidence.record_failure(tool_name) if tool_name handle_exception(e, level: :debug, operation: 'llm.tools.dispatcher.shadow_execution', tool_name: tool_name) end |