Module: Legion::LLM::Inference::Steps::TriggerMatch

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

Constant Summary collapse

HARNESS_PREFIXES =
[
  '[SUGGESTION MODE',
  '[REQUEST INTERRUPTED',
  'Write the title in the predominant language'
].freeze

Instance Method Summary collapse

Instance Method Details

#extract_recent_textObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/legion/llm/inference/steps/trigger_match.rb', line 79

def extract_recent_text
  depth = trigger_scan_depth
  messages = @request.messages.last(depth)

  text = messages.filter_map do |msg|
    next unless msg.is_a?(Hash)
    next unless (msg[:role] || msg['role']).to_s == 'user'

    content = msg[:content] || msg['content']
    raw = if content.is_a?(Array)
            content.filter_map do |c|
              if c.is_a?(String)
                c
              else
                (c.is_a?(Hash) ? (c[:text] || c['text']) : nil)
              end
            end.join(' ')
          else
            content.to_s
          end
    next if harness_message?(raw)

    raw
  end.join(' ')

  sanitize_trigger_text(text)
end

#extract_session_text(text) ⇒ Object



125
126
127
128
129
130
# File 'lib/legion/llm/inference/steps/trigger_match.rb', line 125

def extract_session_text(text)
  matches = text.to_s.scan(%r{<session\b[^>]*>(.*?)</session>}mi).flatten
  return nil if matches.empty?

  matches.join(' ')
end

#format_trigger_log_value(value) ⇒ Object



233
234
235
236
237
238
239
240
# File 'lib/legion/llm/inference/steps/trigger_match.rb', line 233

def format_trigger_log_value(value)
  case value
  when Array
    value.map(&:to_s).first(20).join(',')
  else
    value
  end
end

#harness_message?(text) ⇒ Boolean

Returns:

  • (Boolean)


118
119
120
121
122
123
# File 'lib/legion/llm/inference/steps/trigger_match.rb', line 118

def harness_message?(text)
  return false if text.nil? || text.empty?

  trimmed = text.lstrip
  HARNESS_PREFIXES.any? { |prefix| trimmed.start_with?(prefix) }
end

#log_trigger_match(action, **fields) ⇒ Object



223
224
225
226
227
228
229
230
231
# File 'lib/legion/llm/inference/steps/trigger_match.rb', line 223

def log_trigger_match(action, **fields)
  payload = fields.map { |key, value| "#{key}=#{format_trigger_log_value(value)}" }.join(' ')
  log.info(
    "[llm][tools][trigger] action=#{action} request_id=#{@request.id} " \
    "conversation_id=#{@request.conversation_id || 'none'} #{payload}".strip
  )
rescue StandardError => e
  handle_exception(e, level: :warn, handled: true, operation: 'llm.pipeline.steps.trigger_match.log')
end

#normalize_message_words(text) ⇒ Object



132
133
134
135
136
# File 'lib/legion/llm/inference/steps/trigger_match.rb', line 132

def normalize_message_words(text)
  return Set.new if text.nil? || text.empty?

  text.downcase.gsub(/[^a-z ]/, ' ').split.to_set
end

#rank_and_cap(matched, per_word, limit) ⇒ Object



176
177
178
179
180
181
182
183
184
# File 'lib/legion/llm/inference/steps/trigger_match.rb', line 176

def rank_and_cap(matched, per_word, limit)
  scores = Hash.new(0)
  per_word.each_value do |tools|
    tools.each { |tool| scores[tool] += 1 }
  end
  matched.to_a
         .sort_by { |tool| [-scores[tool], trigger_tool_name(tool)] }
         .first(limit)
end

#record_trigger_match_timeline(count, start_time = nil) ⇒ Object



242
243
244
245
246
247
248
249
250
251
252
# File 'lib/legion/llm/inference/steps/trigger_match.rb', line 242

def record_trigger_match_timeline(count, start_time = nil)
  return unless @timeline.respond_to?(:record)

  duration = start_time ? ((::Time.now - start_time) * 1000).to_i : 0
  @timeline.record(
    category: :enrichment, key: 'tool:trigger_match',
    direction: :inbound, detail: "#{count} tools matched via trigger words",
    from: 'settings_extensions', to: 'pipeline',
    duration_ms: duration
  )
end

#sanitize_trigger_text(text) ⇒ Object



107
108
109
110
111
112
# File 'lib/legion/llm/inference/steps/trigger_match.rb', line 107

def sanitize_trigger_text(text)
  session_text = extract_session_text(text)
  return session_text if session_text

  strip_tagged_blocks(text)
end

#settings_extension_tool_matches(word_set, extensions = settings_extensions) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/legion/llm/inference/steps/trigger_match.rb', line 144

def settings_extension_tool_matches(word_set, extensions = settings_extensions)
  matched = Set.new
  per_word = Hash.new { |hash, word| hash[word] = Set.new }
  return [matched, per_word] unless extensions.respond_to?(:tools)

  Array(extensions.tools).each do |entry|
    next unless entry.is_a?(Hash)

    tool_words = trigger_words_for_entry(entry)
    matching_words = word_set & tool_words
    next if matching_words.empty?

    matched << entry
    matching_words.each { |word| per_word[word] << entry }
  end

  [matched, per_word]
end

#settings_extensionsObject



138
139
140
141
142
# File 'lib/legion/llm/inference/steps/trigger_match.rb', line 138

def settings_extensions
  return unless defined?(Legion::Settings::Extensions)

  Legion::Settings::Extensions
end

#settings_value(*keys, default: nil) ⇒ Object



219
220
221
# File 'lib/legion/llm/inference/steps/trigger_match.rb', line 219

def settings_value(*keys, default: nil)
  Legion::Settings.dig(:llm, *keys) || default
end

#step_trigger_matchObject



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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/legion/llm/inference/steps/trigger_match.rb', line 14

def step_trigger_match
  start_time = nil
  extensions = settings_extensions
  unless extensions.respond_to?(:tools)
    log_step_debug(:trigger_match, :skipped, reason: :settings_extensions_unavailable)
    log_trigger_match(:skipped, reason: :settings_extensions_unavailable)
    return
  end

  start_time = ::Time.now

  text = extract_recent_text
  word_set = normalize_message_words(text)
  if word_set.empty?
    log_step_debug(:trigger_match, :skipped, reason: :no_words)
    return
  end
  log_step_debug(:trigger_match, :scanning, word_count: word_set.size)

  matched, per_word = settings_extension_tool_matches(word_set, extensions)
  pre_filter_count = matched.size
  subtract_always_loaded(matched, extensions)
  if matched.empty?
    log_step_debug(:trigger_match, :no_matches)
    log_trigger_match(:no_matches, word_count: word_set.size, pre_filter_count: pre_filter_count)
    return
  end

  limit = trigger_tool_limit
  @triggered_tools = if matched.size <= limit
                       matched.to_a
                     else
                       log.warn(
                         "[llm][steps][trigger_match] action=tools_capped request_id=#{@request.id} " \
                         "matched=#{matched.size} limit=#{limit} dropped=#{matched.size - limit}"
                       )
                       rank_and_cap(matched, per_word, limit)
                     end

  names = @triggered_tools.map { |tool| trigger_tool_name(tool) }
  if @triggered_tools.any?
    @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)
  log_step_debug(:trigger_match, :matched, matched_count: matched.size, injected_count: @triggered_tools.size, limit: limit)
  log_trigger_match(:matched, word_count: word_set.size, matched_count: matched.size,
                              injected_count: @triggered_tools.size, limit: limit,
                              names: names)
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

#strip_tagged_blocks(text) ⇒ Object



114
115
116
# File 'lib/legion/llm/inference/steps/trigger_match.rb', line 114

def strip_tagged_blocks(text)
  text.to_s.gsub(%r{<[a-z][\w-]*\b[^>]*>.*?</[a-z][\w-]*>}mi, ' ')
end

#subtract_always_loaded(matched, extensions = settings_extensions) ⇒ Object



186
187
188
189
190
191
192
193
194
195
# File 'lib/legion/llm/inference/steps/trigger_match.rb', line 186

def subtract_always_loaded(matched, extensions = settings_extensions)
  unless extensions.respond_to?(:filter_tools)
    log_step_debug(:trigger_match, :always_loaded_filter_skipped, reason: :settings_extensions_unavailable)
    return
  end

  always = extensions.filter_tools(deferred: false).map { |t| t[:name] }
  matched.reject! { |tool| always.include?(trigger_tool_name(tool)) }
  log_step_debug(:trigger_match, :always_loaded_filtered, always_loaded_count: always.size, remaining_count: matched.size)
end

#tool_trigger_setting(key, default = nil) ⇒ Object



215
216
217
# File 'lib/legion/llm/inference/steps/trigger_match.rb', line 215

def tool_trigger_setting(key, default = nil)
  Legion::Settings.dig(:llm, :tool_trigger, key) || default
end

#trigger_scan_depthObject



207
208
209
# File 'lib/legion/llm/inference/steps/trigger_match.rb', line 207

def trigger_scan_depth
  tool_trigger_setting(:scan_depth, 10)
end

#trigger_tool_limitObject



211
212
213
# File 'lib/legion/llm/inference/steps/trigger_match.rb', line 211

def trigger_tool_limit
  tool_trigger_setting(:tool_limit, 25)
end

#trigger_tool_name(tool) ⇒ Object



197
198
199
200
201
202
203
204
205
# File 'lib/legion/llm/inference/steps/trigger_match.rb', line 197

def trigger_tool_name(tool)
  if tool.is_a?(Hash)
    (tool[:name] || tool['name']).to_s
  elsif tool.respond_to?(:tool_name)
    tool.tool_name.to_s
  else
    tool.to_s
  end
end

#trigger_tool_name_for_word_match(name) ⇒ Object



172
173
174
# File 'lib/legion/llm/inference/steps/trigger_match.rb', line 172

def trigger_tool_name_for_word_match(name)
  name.to_s.sub(/\Alegion[-_]/, '')
end

#trigger_words_for_entry(entry) ⇒ Object



163
164
165
166
167
168
169
170
# File 'lib/legion/llm/inference/steps/trigger_match.rb', line 163

def trigger_words_for_entry(entry)
  values = Array(entry[:trigger_words])
  values << trigger_tool_name_for_word_match(entry[:name])
  values << entry[:extension]
  values << entry[:runner]
  values << entry[:function]
  normalize_message_words(values.compact.join(' '))
end