Module: Legion::MCP::Patterns::Schema

Defined in:
lib/legion/mcp/patterns/schema.rb

Constant Summary collapse

SCHEMA_VERSION =
'1.0'
REQUIRED_FIELDS =
%i[schema_version pattern_id intent capability_chain confidence metadata].freeze
TRUST_LEVELS =
{
  local:     0.5,
  org:       0.4,
  community: 0.3
}.freeze

Class Method Summary collapse

Class Method Details

.deep_symbolize(obj) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/legion/mcp/patterns/schema.rb', line 43

def deep_symbolize(obj)
  case obj
  when Hash  then obj.to_h { |k, v| [k.to_sym, deep_symbolize(v)] }
  when Array then obj.map { |v| deep_symbolize(v) }
  else            obj
  end
end

.export(pattern) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/legion/mcp/patterns/schema.rb', line 20

def export(pattern)
  {
    schema_version:    SCHEMA_VERSION,
    pattern_id:        pattern[:intent_hash],
    intent:            {
      description: pattern[:intent_text],
      keywords:    extract_keywords(pattern[:intent_text])
    },
    capability_chain:  Array(pattern[:tool_chain]).map { |t| { tool: t, params_template: {} } },
    response_template: pattern[:response_template] ? { engine: 'mustache', template: pattern[:response_template] } : nil,
    confidence:        {
      suggested_initial: [pattern[:confidence], 0.5].min,
      source_hits:       pattern[:hit_count] || 0,
      source_misses:     pattern[:miss_count] || 0
    },
    metadata:          {
      source:      'local',
      sensitivity: 'public',
      created_at:  pattern[:created_at]&.iso8601
    }
  }
end

.extract_keywords(text) ⇒ Object



84
85
86
87
88
# File 'lib/legion/mcp/patterns/schema.rb', line 84

def extract_keywords(text)
  return [] unless text

  text.downcase.split(/\s+/).uniq.reject { |w| w.length < 3 }
end

.import(external, trust_level: :community) ⇒ Object



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
# File 'lib/legion/mcp/patterns/schema.rb', line 51

def import(external, trust_level: :community)
  external = deep_symbolize(external)

  confidence = external.dig(:confidence, :suggested_initial) || TRUST_LEVELS.fetch(trust_level, 0.3)
  confidence = [confidence, TRUST_LEVELS.fetch(trust_level, 0.3)].min

  intent_text = external.dig(:intent, :description) || ''
  intent_hash = external[:pattern_id] || Digest::SHA256.hexdigest(intent_text.downcase.strip)
  tool_chain = Array(external[:capability_chain]).map { |c| c[:tool] || c.to_s }

  template = external.dig(:response_template, :template)

  {
    intent_hash:          intent_hash,
    intent_text:          intent_text,
    intent_vector:        nil,
    tool_chain:           tool_chain,
    response_template:    template,
    confidence:           confidence,
    hit_count:            0,
    miss_count:           0,
    last_hit_at:          nil,
    created_at:           Time.now,
    context_requirements: nil
  }
end

.validate_schema(data) ⇒ Object



78
79
80
81
82
# File 'lib/legion/mcp/patterns/schema.rb', line 78

def validate_schema(data)
  return false unless data.is_a?(Hash)

  REQUIRED_FIELDS.all? { |f| data.key?(f) || data.key?(f.to_s) }
end