Module: Legion::LLM::Pipeline::Steps::TriggerMatch

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

Instance Method Summary collapse

Instance Method Details

#step_trigger_matchObject



12
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
46
47
48
# File 'lib/legion/llm/pipeline/steps/trigger_match.rb', line 12

def step_trigger_match
  start_time = nil
  return unless defined?(::Legion::Tools::TriggerIndex)
  return if ::Legion::Tools::TriggerIndex.empty?

  start_time = ::Time.now

  text = extract_recent_text
  word_set = normalize_message_words(text)
  return if word_set.empty?

  matched, per_word = ::Legion::Tools::TriggerIndex.match(word_set)
  subtract_always_loaded(matched)
  return if matched.empty?

  limit = trigger_tool_limit
  @triggered_tools = if matched.size <= limit
                       matched.to_a
                     else
                       rank_and_cap(matched, per_word, limit)
                     end

  if @triggered_tools.any?
    names = @triggered_tools.map(&:tool_name)
    @enrichments['tool:trigger_match'] = {
      content:   "#{@triggered_tools.size} tools matched via trigger words",
      data:      { tool_count: @triggered_tools.size, tool_names: names },
      timestamp: ::Time.now
    }
  end

  record_trigger_match_timeline(@triggered_tools.size, start_time)
rescue StandardError => e
  @warnings << "Trigger match error: #{e.message}"
  handle_exception(e, level: :warn, operation: 'llm.pipeline.steps.trigger_match')
  record_trigger_match_timeline(0, start_time)
end