Class: Legion::Extensions::Capability

Inherits:
Data
  • Object
show all
Defined in:
lib/legion/extensions/capability.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description

Returns:

  • (Object)

    the current value of description



5
6
7
# File 'lib/legion/extensions/capability.rb', line 5

def description
  @description
end

#extensionObject (readonly)

Returns the value of attribute extension

Returns:

  • (Object)

    the current value of extension



5
6
7
# File 'lib/legion/extensions/capability.rb', line 5

def extension
  @extension
end

#functionObject (readonly)

Returns the value of attribute function

Returns:

  • (Object)

    the current value of function



5
6
7
# File 'lib/legion/extensions/capability.rb', line 5

def function
  @function
end

#loaded_atObject (readonly)

Returns the value of attribute loaded_at

Returns:

  • (Object)

    the current value of loaded_at



5
6
7
# File 'lib/legion/extensions/capability.rb', line 5

def loaded_at
  @loaded_at
end

#nameObject (readonly)

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



5
6
7
# File 'lib/legion/extensions/capability.rb', line 5

def name
  @name
end

#parametersObject (readonly)

Returns the value of attribute parameters

Returns:

  • (Object)

    the current value of parameters



5
6
7
# File 'lib/legion/extensions/capability.rb', line 5

def parameters
  @parameters
end

#runnerObject (readonly)

Returns the value of attribute runner

Returns:

  • (Object)

    the current value of runner



5
6
7
# File 'lib/legion/extensions/capability.rb', line 5

def runner
  @runner
end

#tagsObject (readonly)

Returns the value of attribute tags

Returns:

  • (Object)

    the current value of tags



5
6
7
# File 'lib/legion/extensions/capability.rb', line 5

def tags
  @tags
end

Class Method Details

.from_absorber(extension:, absorber:, patterns: [], description: nil) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/legion/extensions/capability.rb', line 9

def self.from_absorber(extension:, absorber:, patterns: [], description: nil)
  absorber_name = absorber.name&.split('::')&.last || absorber.object_id.to_s
  snake = absorber_name.gsub(/([A-Z])/, '_\1').sub(/^_/, '').downcase
  canonical = "#{extension}:absorber:#{snake}"
  new(
    name:        canonical,
    extension:   extension,
    runner:      'Absorber',
    function:    absorber_name,
    description: description,
    parameters:  { input: { type: :string, required: true } },
    tags:        ['absorber'] + patterns.map { |p| "pattern:#{p[:type]}:#{p[:value]}" },
    loaded_at:   Time.now
  )
end

.from_runner(extension:, runner:, function:, **opts) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/legion/extensions/capability.rb', line 25

def self.from_runner(extension:, runner:, function:, **opts)
  canonical = "#{extension}:#{runner.to_s.gsub(/([A-Z])/, '_\1').sub(/^_/, '').downcase}:#{function}"
  new(
    name:        canonical,
    extension:   extension,
    runner:      runner.to_s,
    function:    function.to_s,
    description: opts[:description],
    parameters:  opts[:parameters] || {},
    tags:        Array(opts[:tags]),
    loaded_at:   Time.now
  )
end

Instance Method Details

#matches_intent?(text) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
43
44
45
46
# File 'lib/legion/extensions/capability.rb', line 39

def matches_intent?(text)
  words = text.downcase.split(/\s+/)
  searchable = [description, *tags, extension, runner, function]
               .compact.join(' ').downcase

  matching = words.count { |w| searchable.include?(w) }
  matching.to_f / [words.length, 1].max >= 0.4
end

#to_mcp_toolObject



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

def to_mcp_tool
  snake_runner = runner.gsub(/([A-Z])/, '_\1').sub(/^_/, '').downcase
  tool_name = "legion.#{extension.delete_prefix('lex-').tr('-', '_')}.#{snake_runner}.#{function}"
  properties = (parameters || {}).transform_values do |v|
    v.is_a?(Hash) ? v : { type: v.to_s }
  end

  {
    name:         tool_name,
    description:  description || "#{extension} #{runner}##{function}",
    input_schema: {
      type:       'object',
      properties: properties,
      required:   parameters&.select { |_, v| v.is_a?(Hash) && v[:required] }&.keys&.map(&:to_s) || []
    }
  }
end