Module: Legion::MCP::EmbeddingIndex

Extended by:
Logging::Helper
Defined in:
lib/legion/mcp/embedding_index.rb

Class Method Summary collapse

Class Method Details

.build_composite(name, description, params) ⇒ Object



129
130
131
132
133
# File 'lib/legion/mcp/embedding_index.rb', line 129

def build_composite(name, description, params)
  parts = [name, '--', description]
  parts << "Params: #{params.join(', ')}" unless params.empty?
  parts.join(' ')
end

.build_from_tool_data(tool_data, embedder: default_embedder) ⇒ Object

rubocop:disable Metrics/AbcSize



10
11
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
# File 'lib/legion/mcp/embedding_index.rb', line 10

def build_from_tool_data(tool_data, embedder: default_embedder) # rubocop:disable Metrics/AbcSize
  log.debug("[mcp][embedding] action=build_from_tool_data tools=#{tool_data.size} " \
            "embedder_present=#{!embedder.nil?}")
  @embedder = embedder
  mutex.synchronize do
    composites = tool_data.to_h do |tool|
      [tool[:name], build_composite(tool[:name], tool[:description], tool[:params])]
    end

    cached_vectors = bulk_cache_lookup(composites.values)

    uncached_names = composites.keys.reject { |name| cached_vectors.key?(composites[name]) }
    newly_embedded = {}
    uncached_names.each do |name|
      composite = composites[name]
      vector = safe_embed(composite, embedder)
      newly_embedded[composite] = vector if vector
    end

    bulk_cache_store(newly_embedded) unless newly_embedded.empty?

    tool_data.each do |tool|
      composite = composites[tool[:name]]
      vector = cached_vectors[composite] || newly_embedded[composite]
      next unless vector

      index[tool[:name]] = {
        name:           tool[:name],
        composite_text: composite,
        vector:         vector,
        built_at:       Time.now
      }
    end
  end
end

.bulk_cache_lookup(composite_texts) ⇒ Object



109
110
111
112
113
114
115
116
117
# File 'lib/legion/mcp/embedding_index.rb', line 109

def bulk_cache_lookup(composite_texts)
  return {} unless defined?(Legion::Tools::EmbeddingCache) &&
                   Legion::Tools::EmbeddingCache.respond_to?(:bulk_lookup)

  Legion::Tools::EmbeddingCache.bulk_lookup(composite_texts)
rescue StandardError => e
  handle_exception(e, level: :debug, operation: 'legion.mcp.embedding_index.bulk_cache_lookup')
  {}
end

.bulk_cache_store(composite_to_vector) ⇒ Object



119
120
121
122
123
124
125
126
127
# File 'lib/legion/mcp/embedding_index.rb', line 119

def bulk_cache_store(composite_to_vector)
  return unless defined?(Legion::Tools::EmbeddingCache) &&
                Legion::Tools::EmbeddingCache.respond_to?(:bulk_store)

  Legion::Tools::EmbeddingCache.bulk_store(composite_to_vector)
rescue StandardError => e
  handle_exception(e, level: :debug, operation: 'legion.mcp.embedding_index.bulk_cache_store')
  nil
end

.cosine_similarity(vec_a, vec_b) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/legion/mcp/embedding_index.rb', line 66

def cosine_similarity(vec_a, vec_b)
  dot = vec_a.zip(vec_b).sum { |a, b| a * b }
  mag_a = Math.sqrt(vec_a.sum { |x| x**2 })
  mag_b = Math.sqrt(vec_b.sum { |x| x**2 })
  return 0.0 if mag_a.zero? || mag_b.zero?

  dot / (mag_a * mag_b)
end

.coverageObject



87
88
89
90
91
92
93
94
# File 'lib/legion/mcp/embedding_index.rb', line 87

def coverage
  mutex.synchronize do
    return 0.0 if index.empty?

    with_vectors = index.values.count { |e| e[:vector] }
    with_vectors.to_f / index.size
  end
end

.default_embedderObject



147
148
149
150
151
152
153
154
# File 'lib/legion/mcp/embedding_index.rb', line 147

def default_embedder
  return nil unless defined?(Legion::LLM) && Legion::LLM.respond_to?(:started?) && Legion::LLM.started?

  ->(text) { Legion::LLM.embed(text)[:vector] }
rescue StandardError => e
  handle_exception(e, level: :debug, operation: 'legion.mcp.embedding_index.default_embedder')
  nil
end

.entry(tool_name) ⇒ Object



75
76
77
# File 'lib/legion/mcp/embedding_index.rb', line 75

def entry(tool_name)
  mutex.synchronize { index[tool_name] }
end

.indexObject



101
102
103
# File 'lib/legion/mcp/embedding_index.rb', line 101

def index
  @index ||= {}
end

.mutexObject



105
106
107
# File 'lib/legion/mcp/embedding_index.rb', line 105

def mutex
  @mutex ||= Mutex.new
end

.populated?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/legion/mcp/embedding_index.rb', line 83

def populated?
  mutex.synchronize { !index.empty? }
end

.reset!Object



96
97
98
99
# File 'lib/legion/mcp/embedding_index.rb', line 96

def reset!
  @embedder = nil
  mutex.synchronize { index.clear }
end

.safe_embed(text, embedder) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/legion/mcp/embedding_index.rb', line 135

def safe_embed(text, embedder)
  return nil unless embedder

  result = embedder.call(text)
  return nil unless result.is_a?(Array) && !result.empty?

  result
rescue StandardError => e
  handle_exception(e, level: :debug, operation: 'legion.mcp.embedding_index.safe_embed')
  nil
end

.semantic_match(intent, embedder: @embedder || default_embedder, limit: 5) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/legion/mcp/embedding_index.rb', line 46

def semantic_match(intent, embedder: @embedder || default_embedder, limit: 5)
  return [] if index.empty?

  intent_vec = safe_embed(intent, embedder)
  return [] unless intent_vec

  log.debug("[mcp][embedding] action=semantic_match index_size=#{index.size} limit=#{limit}")

  scores = mutex.synchronize do
    index.values.filter_map do |entry|
      next unless entry[:vector]

      score = cosine_similarity(intent_vec, entry[:vector])
      { name: entry[:name], score: score }
    end
  end

  scores.sort_by { |s| -s[:score] }.first(limit)
end

.sizeObject



79
80
81
# File 'lib/legion/mcp/embedding_index.rb', line 79

def size
  mutex.synchronize { index.size }
end