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).
- .normalize_parameters(parameters) ⇒ Object
-
.sanitize_tool_name(raw) ⇒ Object
Sanitize a tool name to be safe for all wire formats.
Instance Method Summary collapse
- #input_schema ⇒ Object
- #params_schema ⇒ Object
-
#to_h ⇒ Object
Serialize to a Hash for AMQP/fleet/wire transport.
Instance Attribute Details
#description ⇒ Object (readonly)
Returns the value of attribute description
13 14 15 |
# File 'lib/legion/extensions/llm/canonical/tool_definition.rb', line 13 def description @description end |
#name ⇒ Object (readonly)
Returns the value of attribute name
13 14 15 |
# File 'lib/legion/extensions/llm/canonical/tool_definition.rb', line 13 def name @name end |
#parameters ⇒ Object (readonly)
Returns the value of attribute parameters
13 14 15 |
# File 'lib/legion/extensions/llm/canonical/tool_definition.rb', line 13 def parameters @parameters end |
#source ⇒ Object (readonly)
Returns the value of attribute source
13 14 15 |
# File 'lib/legion/extensions/llm/canonical/tool_definition.rb', line 13 def source @source end |
Class Method Details
.build(name:, description: '', parameters: nil, source: nil) ⇒ Object
Build from keyword args (primary constructor).
30 31 32 33 34 35 36 37 |
# File 'lib/legion/extensions/llm/canonical/tool_definition.rb', line 30 def self.build(name:, description: '', parameters: nil, source: nil) new( sanitize_tool_name(name), description.to_s, normalize_parameters(parameters), source || { type: :builtin } ) end |
.from_hash(hash, source: nil) ⇒ Object
Build from a Hash (raw provider response or deserialized wire payload).
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/legion/extensions/llm/canonical/tool_definition.rb', line 40 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).
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/legion/extensions/llm/canonical/tool_definition.rb', line 53 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 |
.normalize_parameters(parameters) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/legion/extensions/llm/canonical/tool_definition.rb', line 14 def self.normalize_parameters(parameters) empty = { type: 'object', properties: {} } return empty if parameters.nil? schema = if parameters.respond_to?(:transform_keys) parameters.transform_keys { |k| k.respond_to?(:to_sym) ? k.to_sym : k } end return empty if schema.nil? || schema.empty? return schema if schema.key?(:type) return schema.merge(type: 'object') if OBJECT_SCHEMA_KEYWORDS.any? { |k| schema.key?(k) } return schema if COMPOSITE_SCHEMA_KEYWORDS.any? { |k| schema.key?(k) } { type: 'object', properties: schema } end |
.sanitize_tool_name(raw) ⇒ Object
Sanitize a tool name to be safe for all wire formats.
71 72 73 74 75 76 |
# File 'lib/legion/extensions/llm/canonical/tool_definition.rb', line 71 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
#input_schema ⇒ Object
82 83 84 |
# File 'lib/legion/extensions/llm/canonical/tool_definition.rb', line 82 def input_schema parameters end |
#params_schema ⇒ Object
78 79 80 |
# File 'lib/legion/extensions/llm/canonical/tool_definition.rb', line 78 def params_schema parameters end |
#to_h ⇒ Object
Serialize to a Hash for AMQP/fleet/wire transport.
87 88 89 90 91 92 93 |
# File 'lib/legion/extensions/llm/canonical/tool_definition.rb', line 87 def to_h { name: name, description: description, parameters: parameters }.compact.reject { |k, v| k == :description && v == '' } end |