Class: RubyBots::Tool

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_bots/tool.rb

Overview

Base class for all RubyBots tools and bots

Direct Known Subclasses

Bot, OpenAITool

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, description:) ⇒ Tool

Returns a new instance of Tool.



6
7
8
9
10
# File 'lib/ruby_bots/tool.rb', line 6

def initialize(name:, description:)
  @name = name
  @description = description
  @errors = []
end

Class Attribute Details

.input_validatorsObject

Returns the value of attribute input_validators.



13
14
15
# File 'lib/ruby_bots/tool.rb', line 13

def input_validators
  @input_validators
end

.output_validatorsObject

Returns the value of attribute output_validators.



13
14
15
# File 'lib/ruby_bots/tool.rb', line 13

def output_validators
  @output_validators
end

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



4
5
6
# File 'lib/ruby_bots/tool.rb', line 4

def description
  @description
end

#errorsObject

Returns the value of attribute errors.



4
5
6
# File 'lib/ruby_bots/tool.rb', line 4

def errors
  @errors
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/ruby_bots/tool.rb', line 4

def name
  @name
end

Class Method Details

.validate_input(method) ⇒ Object



16
17
18
19
# File 'lib/ruby_bots/tool.rb', line 16

def self.validate_input(method)
  @input_validators ||= []
  @input_validators << method
end

.validate_output(method) ⇒ Object



21
22
23
24
# File 'lib/ruby_bots/tool.rb', line 21

def self.validate_output(method)
  @output_validators ||= []
  @output_validators << method
end

Instance Method Details

#response(input) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ruby_bots/tool.rb', line 26

def response(input)
  run_input_validations(input)

  raise RubyBots::InvalidInputError, { errors: @errors } if @errors.any?

  output = run(input)

  run_output_validations(output)

  raise RubyBots::InvalidOutputError, { errors: @errors } if @errors.any?

  output
end