Module: Legion::MCP::PatternSchema

Defined in:
lib/legion/mcp/pattern_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

.export(pattern) ⇒ Object



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

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



73
74
75
76
77
# File 'lib/legion/mcp/pattern_schema.rb', line 73

def extract_keywords(text)
  return [] unless text

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

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



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

def import(external, trust_level: :community)
  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



67
68
69
70
71
# File 'lib/legion/mcp/pattern_schema.rb', line 67

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

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