Class: Legion::LLM::Types::ToolDefinition
- Inherits:
-
Data
- Object
- Data
- Legion::LLM::Types::ToolDefinition
- Defined in:
- lib/legion/llm/types/tool_definition.rb
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
- .from_hash(hash, source: nil) ⇒ Object
- .from_tool_class(tool_class, source: nil) ⇒ Object
- .sanitize_tool_name(raw) ⇒ Object
Instance Method Summary collapse
Instance Attribute Details
#description ⇒ Object (readonly)
Returns the value of attribute description
7 8 9 |
# File 'lib/legion/llm/types/tool_definition.rb', line 7 def description @description end |
#name ⇒ Object (readonly)
Returns the value of attribute name
7 8 9 |
# File 'lib/legion/llm/types/tool_definition.rb', line 7 def name @name end |
#parameters ⇒ Object (readonly)
Returns the value of attribute parameters
7 8 9 |
# File 'lib/legion/llm/types/tool_definition.rb', line 7 def parameters @parameters end |
#source ⇒ Object (readonly)
Returns the value of attribute source
7 8 9 |
# File 'lib/legion/llm/types/tool_definition.rb', line 7 def source @source end |
Class Method Details
.build(name:, description: '', parameters: nil, source: nil) ⇒ Object
8 9 10 11 12 13 14 15 |
# File 'lib/legion/llm/types/tool_definition.rb', line 8 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
17 18 19 20 21 22 23 24 25 |
# File 'lib/legion/llm/types/tool_definition.rb', line 17 def self.from_hash(hash, source: 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_tool_class(tool_class, source: nil) ⇒ Object
27 28 29 30 31 32 33 34 35 |
# File 'lib/legion/llm/types/tool_definition.rb', line 27 def self.from_tool_class(tool_class, source: nil) raw_name = tool_class.respond_to?(:tool_name) ? tool_class.tool_name : tool_class.name.to_s build( name: raw_name, description: tool_class.respond_to?(:description) ? tool_class.description : '', parameters: tool_class.respond_to?(:input_schema) ? tool_class.input_schema : {}, source: source || { type: :registry, tool_class: tool_class } ) end |
.sanitize_tool_name(raw) ⇒ Object
37 38 39 40 41 42 |
# File 'lib/legion/llm/types/tool_definition.rb', line 37 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
44 45 46 47 48 49 50 |
# File 'lib/legion/llm/types/tool_definition.rb', line 44 def to_h { name: name, description: description, parameters: parameters }.compact end |