Module: Legion::LLM::Pipeline::Steps::ToolCalls

Includes:
Legion::Logging::Helper
Included in:
Executor
Defined in:
lib/legion/llm/pipeline/steps/tool_calls.rb

Constant Summary collapse

MAX_TOOL_LOOPS =
10

Instance Method Summary collapse

Instance Method Details

#step_tool_callsObject

rubocop:disable Metrics/MethodLength, Metrics/BlockLength



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
79
80
81
82
83
84
85
86
87
88
# File 'lib/legion/llm/pipeline/steps/tool_calls.rb', line 15

def step_tool_calls
  return unless @raw_response.respond_to?(:tool_calls) && @raw_response.tool_calls&.any?

  tool_calls = @raw_response.tool_calls
  log.info(
    "[llm][tools] detected request_id=#{@request.id} " \
    "conversation_id=#{@request.conversation_id || 'none'} count=#{tool_calls.size}"
  )
  tool_calls.each do |tc|
    tool_name = tc[:name] || tc['name']
    tool_call_id = tc[:id] || tc['id']
    source = find_tool_source(tool_name)
    next unless source

    # Skip builtin tools - RubyLLM handles those
    if source[:type] == :builtin
      log.info(
        "[llm][tools] builtin_passthrough request_id=#{@request.id} " \
        "tool_call_id=#{tool_call_id || 'none'} name=#{tool_name}"
      )
      next
    end

    log.info(
      "[llm][tools] dispatch request_id=#{@request.id} " \
      "tool_call_id=#{tool_call_id || 'none'} name=#{tool_name} " \
      "source=#{describe_tool_source(source)} " \
      "arguments=#{summarize_tool_arguments(tc[:arguments] || tc['arguments'])}"
    )

    tool_exchange_id = Tracing.exchange_id
    result = ToolDispatcher.dispatch(
      tool_call:   tc,
      source:      source,
      exchange_id: tool_exchange_id
    )

    @timeline.record(
      category: :tool, key: "tool:execute:#{tc[:name] || tc['name']}",
      exchange_id: tool_exchange_id, direction: :outbound,
      detail: "#{result[:status]} via #{source[:type]}",
      from: 'pipeline', to: "tool:#{tc[:name] || tc['name']}",
      duration_ms: result[:duration_ms],
      data: {
        tool_call_id: tool_call_id,
        arguments:    tc[:arguments] || tc['arguments'] || {},
        source:       describe_tool_source(source),
        status:       result[:status]
      }
    )

    @timeline.record(
      category: :tool, key: "tool:result:#{tc[:name] || tc['name']}",
      exchange_id: tool_exchange_id, direction: :inbound,
      detail: result[:result].to_s[0..100].to_s,
      from: "tool:#{tc[:name] || tc['name']}", to: 'pipeline',
      data: {
        tool_call_id: tool_call_id,
        status:       result[:status],
        result:       result[:result]
      }
    )

    log.info(
      "[llm][tools] result request_id=#{@request.id} " \
      "tool_call_id=#{tool_call_id || 'none'} name=#{tool_name} " \
      "status=#{result[:status]} duration_ms=#{result[:duration_ms]} " \
      "preview=#{summarize_tool_result(result[:result])}"
    )
  end
rescue StandardError => e
  @warnings << "Tool call handling error: #{e.message}"
  handle_exception(e, level: :warn, operation: 'llm.pipeline.steps.tool_calls')
end