Class: RailsAgents::ToolSet
- Inherits:
-
Object
- Object
- RailsAgents::ToolSet
- Defined in:
- lib/rails_agents/tool_set.rb
Class Method Summary collapse
Instance Method Summary collapse
- #+(other) ⇒ Object
- #definitions ⇒ Object
- #execute(name, arguments) ⇒ Object
- #find(name) ⇒ Object
-
#initialize(*tools) ⇒ ToolSet
constructor
A new instance of ToolSet.
- #resolve(klass) ⇒ Object
Constructor Details
#initialize(*tools) ⇒ ToolSet
Returns a new instance of ToolSet.
5 6 7 |
# File 'lib/rails_agents/tool_set.rb', line 5 def initialize(*tools) @tools = tools.flatten.compact.map { |t| resolve(t) } end |
Class Method Details
.from_directory(path = nil) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/rails_agents/tool_set.rb', line 9 def self.from_directory(path = nil) path ||= (defined?(::Rails) && ::Rails.respond_to?(:root) ? ::Rails.root.join("app/agents/tools") : nil) return new unless path && Dir.exist?(path) classes = Dir.glob("#{path}/**/*.rb").filter_map do |file| require_dependency file if defined?(Rails) const_name = file.delete_prefix("#{path}/").delete_suffix(".rb").camelize const_name.safe_constantize end new(*classes) end |
.use(*tools) ⇒ Object
21 |
# File 'lib/rails_agents/tool_set.rb', line 21 def self.use(*tools) = new(*tools) |
Instance Method Details
#+(other) ⇒ Object
44 |
# File 'lib/rails_agents/tool_set.rb', line 44 def +(other) = self.class.new(*@tools, *other.instance_variable_get(:@tools)) |
#definitions ⇒ Object
36 |
# File 'lib/rails_agents/tool_set.rb', line 36 def definitions = @tools.map(&:definition) |
#execute(name, arguments) ⇒ Object
38 39 40 41 42 |
# File 'lib/rails_agents/tool_set.rb', line 38 def execute(name, arguments) tool_class = find(name) raise Error, "Unknown tool: #{name}" unless tool_class tool_class.new.call(**arguments.transform_keys(&:to_sym)) end |
#find(name) ⇒ Object
37 |
# File 'lib/rails_agents/tool_set.rb', line 37 def find(name) = @tools.find { |t| t.tool_name == name.to_s } |
#resolve(klass) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/rails_agents/tool_set.rb', line 23 def resolve(klass) case klass when Class klass < Tool ? klass : klass when String, Symbol resolved = klass.to_s.camelize.constantize raise Error, "#{klass} is not a RailsAgents::Tool" unless resolved < Tool resolved else klass end end |