Class: Legion::Extensions::Llm::Canonical::ToolDefinition
- Inherits:
-
Data
- Object
- Data
- Legion::Extensions::Llm::Canonical::ToolDefinition
- Defined in:
- lib/legion/extensions/llm/canonical/tool_definition.rb
Overview
Canonical tool definition. Ports field vocabulary from Legion::LLM::Types::ToolDefinition.
Instance Attribute Summary collapse
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parameters ⇒ Object
readonly
Returns the value of attribute parameters.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Class Method Summary collapse
-
.build(name:, description: '', parameters: nil, source: nil) ⇒ Object
Build from keyword args (primary constructor).
-
.from_hash(hash, source: nil) ⇒ Object
Build from a Hash (raw provider response or deserialized wire payload).
-
.from_registry_entry(entry) ⇒ Object
Build from a registry entry (extension/registry tool metadata).
-
.sanitize_tool_name(raw) ⇒ Object
Sanitize a tool name to be safe for all wire formats.
Instance Method Summary collapse
-
#to_h ⇒ Object
Serialize to a Hash for AMQP/fleet/wire transport.
Instance Attribute Details
#description ⇒ Object (readonly)
Returns the value of attribute description
11 12 13 |
# File 'lib/legion/extensions/llm/canonical/tool_definition.rb', line 11 def description @description end |
#name ⇒ Object (readonly)
Returns the value of attribute name
11 12 13 |
# File 'lib/legion/extensions/llm/canonical/tool_definition.rb', line 11 def name @name end |
#parameters ⇒ Object (readonly)
Returns the value of attribute parameters
11 12 13 |
# File 'lib/legion/extensions/llm/canonical/tool_definition.rb', line 11 def parameters @parameters end |
#source ⇒ Object (readonly)
Returns the value of attribute source
11 12 13 |
# File 'lib/legion/extensions/llm/canonical/tool_definition.rb', line 11 def source @source end |
Class Method Details
.build(name:, description: '', parameters: nil, source: nil) ⇒ Object
Build from keyword args (primary constructor).
13 14 15 16 17 18 19 20 |
# File 'lib/legion/extensions/llm/canonical/tool_definition.rb', line 13 def self.build(name:, description: '', parameters: nil, source: nil) new( sanitize_tool_name(name), description.to_s, parameters || {}, source || { type: :builtin } ) end |
.from_hash(hash, source: nil) ⇒ Object
Build from a Hash (raw provider response or deserialized wire payload).
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/legion/extensions/llm/canonical/tool_definition.rb', line 23 def self.from_hash(hash, source: nil) return nil if hash.nil? normalized = hash.respond_to?(:transform_keys) ? hash.transform_keys(&:to_sym) : {} build( name: normalized[:name], description: normalized[:description], parameters: normalized[:parameters] || normalized[:input_schema], source: source || normalized[:source] ) end |
.from_registry_entry(entry) ⇒ Object
Build from a registry entry (extension/registry tool metadata).
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/legion/extensions/llm/canonical/tool_definition.rb', line 36 def self.from_registry_entry(entry) source = { type: entry[:tool_class] ? :registry : :extension, tool_class: entry[:tool_class], extension: entry[:extension], runner: entry[:runner], function: entry[:function] }.compact build( name: entry[:name], description: entry[:description], parameters: entry[:input_schema] || entry[:parameters], source: source.compact ) end |
.sanitize_tool_name(raw) ⇒ Object
Sanitize a tool name to be safe for all wire formats.
54 55 56 57 58 59 |
# File 'lib/legion/extensions/llm/canonical/tool_definition.rb', line 54 def self.sanitize_tool_name(raw) name = raw.to_s.tr('.', '_') name = name.gsub(/[^a-zA-Z0-9_-]/, '') name = name[0, TOOL_NAME_MAX_LENGTH] if name.length > TOOL_NAME_MAX_LENGTH name.empty? ? 'tool' : name end |
Instance Method Details
#to_h ⇒ Object
Serialize to a Hash for AMQP/fleet/wire transport.
62 63 64 65 66 67 68 |
# File 'lib/legion/extensions/llm/canonical/tool_definition.rb', line 62 def to_h { name: name, description: description, parameters: parameters }.compact.reject { |k, v| k == :description && v == '' } end |