Module: Legion::LLM::Tools::Dispatcher

Extended by:
Legion::Logging::Helper
Defined in:
lib/legion/llm/tools/dispatcher.rb

Class Method Summary collapse

Class Method Details

.check_override(tool_name) ⇒ Object



69
70
71
72
73
74
# File 'lib/legion/llm/tools/dispatcher.rb', line 69

def check_override(tool_name)
  settings_override = check_settings_override(tool_name)
  return settings_override if settings_override

  check_registry_override(tool_name)
end

.check_registry_override(tool_name) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/legion/llm/tools/dispatcher.rb', line 76

def check_registry_override(tool_name)
  return nil unless Legion::Settings::Extensions.respond_to?(:find_tool)

  entry = Legion::Settings::Extensions.find_tool(tool_name)
  return nil unless entry

  if entry[:tool_class]
    { type: :registry, tool_class: entry[:tool_class] }
  elsif entry[:extension] && entry[:runner] && entry[:function]
    { type: :extension, lex: entry[:extension], runner: entry[:runner], function: entry[:function] }
  end
rescue StandardError => e
  handle_exception(e, level: :warn, operation: 'llm.tools.dispatcher.check_registry_override', tool_name: tool_name)
  nil
end

.check_settings_override(tool_name) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/legion/llm/tools/dispatcher.rb', line 92

def check_settings_override(tool_name)
  overrides = Legion::Settings.dig(:mcp, :overrides)
  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



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
# File 'lib/legion/llm/tools/dispatcher.rb', line 15

def dispatch(tool_call:, source:, exchange_id: nil)
  start_time = Time.now
  log.debug "[llm][tools_dispatcher] action=dispatch.enter tool=#{tool_call[:name]} source_type=#{source[:type]} exchange_id=#{exchange_id}"

  if source[:type] == :mcp
    override = check_override(tool_call[:name])
    if override
      log.warn "[llm][tools_dispatcher] action=source_overridden tool=#{tool_call[:name]} from=mcp:#{source[:server]} to=#{override[:type]}"
      overridden_source = source
      source = override.merge(overridden_from: overridden_source)
    end
  end

  log.info "[llm][tools_dispatcher] action=dispatch_path tool=#{tool_call[:name]} path=#{source[:type]} exchange_id=#{exchange_id}"
  result = case source[:type]
           when :mcp
             dispatch_mcp(tool_call, source)
           when :extension
             dispatch_extension(tool_call, source)
           when :registry
             dispatch_registry(tool_call, source)
           when :client
             dispatch_client(tool_call, source)
           when :special
             dispatch_special(tool_call)
           when :builtin
             dispatch_builtin(tool_call, source)
           else
             { status: :error, error: "Unknown tool source type: #{source[:type]}" }
           end

  duration_ms = ((Time.now - start_time) * 1000).to_i
  if result[:status] == :error
    err_detail = error_log_detail(result)
    log.warn "[llm][tools_dispatcher] action=dispatch_failed tool=#{tool_call[:name]} " \
             "path=#{source[:type]} duration_ms=#{duration_ms} error=#{err_detail}"
  else
    log.info "[llm][tools_dispatcher] action=dispatch.complete tool=#{tool_call[:name]} " \
             "path=#{source[:type]} status=#{result[:status]} duration_ms=#{duration_ms}"
  end
  result.merge(
    source:      source,
    exchange_id: exchange_id,
    duration_ms: duration_ms
  )
rescue StandardError => e
  duration_ms = ((Time.now - start_time) * 1000).to_i
  err_detail = trim_error_detail("#{e.class}:#{e.message}")
  log.warn "[llm][tools_dispatcher] action=dispatch_exception tool=#{tool_call[:name]} " \
           "path=#{source&.[](:type)} duration_ms=#{duration_ms} error=#{err_detail}"
  handle_exception(e, level: :warn, operation: 'llm.tools.dispatcher.dispatch_tool_call', tool_name: tool_call[:name])
  { status: :error, error: e.message, source: source, exchange_id: exchange_id, duration_ms: duration_ms }
end

.dispatch_builtin(_tool_call, _source) ⇒ Object



153
154
155
# File 'lib/legion/llm/tools/dispatcher.rb', line 153

def dispatch_builtin(_tool_call, _source)
  { status: :passthrough, result: nil }
end

.dispatch_client(tool_call, source) ⇒ Object



145
146
147
# File 'lib/legion/llm/tools/dispatcher.rb', line 145

def dispatch_client(tool_call, source)
  { status: :passthrough, result: nil, client_tool: true, tool_name: tool_call[:name], source: source }
end

.dispatch_extension(tool_call, source) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/legion/llm/tools/dispatcher.rb', line 117

def dispatch_extension(tool_call, source)
  lex = source[:lex] || source[:extension]
  log.debug "[llm][tools] action=dispatch_extension tool=#{tool_call[:name]} lex=#{lex} runner=#{source[:runner]}"
  segments = (lex || '').delete_prefix('lex-').split('-')
  runner_path = (%w[Legion Extensions] + segments.map(&:capitalize) + ['Runners', source[:runner]]).join('::')

  # Resolve from Object (top-level) with inherit=false: the runner path is
  # absolute (Legion::Extensions::...), so the first segment must resolve
  # against Object's own constants — Kernel.const_get(path, false) fails on
  # it because top-level constants live on Object, not Kernel. Keeping
  # inherit=false still refuses to resolve a runner via an ancestor chain.
  runner = Object.const_get(runner_path, false)
  fn = source[:function].to_sym
  result = runner.send(fn, **symbolize_keys(tool_call[:arguments] || {}))
  { status: :success, result: result }
end

.dispatch_mcp(tool_call, source) ⇒ Object



107
108
109
110
111
112
113
114
115
# File 'lib/legion/llm/tools/dispatcher.rb', line 107

def dispatch_mcp(tool_call, source)
  log.debug "[llm][tools] action=dispatch_mcp tool=#{tool_call[:name]} server=#{source[:server]}"
  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

.dispatch_registry(tool_call, source) ⇒ Object



134
135
136
137
138
139
140
141
142
143
# File 'lib/legion/llm/tools/dispatcher.rb', line 134

def dispatch_registry(tool_call, source)
  log.debug "[llm][tools] action=dispatch_registry tool=#{tool_call[:name]}"
  tool_class = source[:tool_class]
  raise "No registry tool class for #{tool_call[:name]}" unless tool_class.respond_to?(:call)

  args = symbolize_keys(tool_call[:arguments] || {})
  args = Interceptor.intercept(tool_call[:name], **args)
  result = tool_class.call(**args)
  { status: result_error?(result) ? :error : :success, result: extract_content(result) }
end

.dispatch_special(tool_call) ⇒ Object



149
150
151
# File 'lib/legion/llm/tools/dispatcher.rb', line 149

def dispatch_special(tool_call)
  Special.dispatch(tool_call[:name], **symbolize_keys(tool_call[:arguments] || {}))
end

.error_log_detail(result) ⇒ Object



189
190
191
192
193
194
# File 'lib/legion/llm/tools/dispatcher.rb', line 189

def error_log_detail(result)
  detail = structured_error_detail(result)
  detail = result_value(result, :error).to_s if detail.empty?
  detail = result_value(result, :result).to_s if detail.empty?
  trim_error_detail(detail)
end

.extract_content(result) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/legion/llm/tools/dispatcher.rb', line 171

def extract_content(result)
  if result.respond_to?(:content) && result.content.is_a?(Array)
    result.content.filter_map { |c| c[:text] || c['text'] || c.to_s }.join("\n")
  elsif result.is_a?(Hash) && result[:content].is_a?(Array)
    result[:content].filter_map { |c| c[:text] || c['text'] }.join("\n")
  elsif result.is_a?(Hash)
    Legion::JSON.dump(result)
  elsif result.is_a?(String)
    result
  else
    result.to_s
  end
end

.result_error?(result) ⇒ Boolean

Returns:

  • (Boolean)


185
186
187
# File 'lib/legion/llm/tools/dispatcher.rb', line 185

def result_error?(result)
  result.is_a?(Hash) && (result[:error] || result['error'])
end

.result_value(result, key) ⇒ Object



210
211
212
213
214
# File 'lib/legion/llm/tools/dispatcher.rb', line 210

def result_value(result, key)
  return nil unless result.respond_to?(:[])

  result[key] || result[key.to_s]
end

.single_line(value) ⇒ Object



216
217
218
# File 'lib/legion/llm/tools/dispatcher.rb', line 216

def single_line(value)
  value.to_s.gsub(/\s+/, ' ').strip
end

.structured_error_detail(result) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/legion/llm/tools/dispatcher.rb', line 196

def structured_error_detail(result)
  fields = []
  exit_status = result_value(result, :exit_status)
  error = result_value(result, :error)
  output_tail = result_value(result, :output_tail)
  command_preview = result_value(result, :command_preview)

  fields << "exit_status=#{exit_status}" unless exit_status.nil?
  fields << "error=#{single_line(error)}" unless error.to_s.empty?
  fields << "output_tail=#{single_line(output_tail)}" unless output_tail.to_s.empty?
  fields << "command_preview=#{single_line(command_preview)}" if fields.empty? && !command_preview.to_s.empty?
  fields.join(' ')
end

.symbolize_keys(value) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/legion/llm/tools/dispatcher.rb', line 157

def symbolize_keys(value)
  case value
  when Hash
    value.to_h do |key, nested|
      normalized_key = key.respond_to?(:to_sym) ? key.to_sym : key
      [normalized_key, symbolize_keys(nested)]
    end
  when Array
    value.map { |entry| symbolize_keys(entry) }
  else
    value
  end
end

.tool_error_log_charsObject



224
225
226
# File 'lib/legion/llm/tools/dispatcher.rb', line 224

def tool_error_log_chars
  Legion::Settings[:llm][:tools][:error_log_chars]
end

.trim_error_detail(value) ⇒ Object



220
221
222
# File 'lib/legion/llm/tools/dispatcher.rb', line 220

def trim_error_detail(value)
  value.to_s[0, tool_error_log_chars]
end