Class: Legion::Tools::Do

Inherits:
Base
  • Object
show all
Extended by:
Logging::Helper
Defined in:
lib/legion/tools/do.rb

Class Method Summary collapse

Methods inherited from Base

deferred, deferred?, description, error_response, extension, handle_exception, input_schema, log, mcp_category, mcp_tier, runner, tags, text_response, tool_name, trigger_words

Class Method Details

.call(intent:, params: {}, context: {}) ⇒ Object



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
# File 'lib/legion/tools/do.rb', line 35

def call(intent:, params: {}, context: {})
  request_id = context[:request_id] || "do_#{SecureRandom.hex(6)}"
  tool_params = params.transform_keys(&:to_sym)

  # Try Tier 0 (cached patterns) if MCP TierRouter is available
  tier_result = try_tier0(intent, tool_params, context, request_id: request_id)
  case tier_result&.dig(:tier)
  when 0
    return text_response(tier_result[:response].merge(
                           _meta: { tier: 0, latency_ms: tier_result[:latency_ms],
                                    confidence: tier_result[:pattern_confidence] }
                         ))
  when 1
    llm_result = try_llm(intent, hint: tier_result[:pattern], request_id: request_id)
    return text_response({ result: llm_result, _meta: { tier: 1 } }) if llm_result
  when 2
    llm_result = try_llm(intent, request_id: request_id)
    return text_response({ result: llm_result, _meta: { tier: 2 } }) if llm_result
  end

  # Fall back to Registry tool matching
  matched = match_tool(intent)
  return error_response("No matching tool found for intent: #{intent}") if matched.nil?

  result = tool_params.empty? ? matched.call : matched.call(**tool_params)
  record_feedback(intent, matched.tool_name, success: true)
  result.is_a?(Hash) ? result : text_response(result)
rescue StandardError => e
  handle_exception(e, level: :warn, handled: true, operation: :tool_do_call)
  error_response("Failed: #{e.message}")
end