Class: ToolSchema::RubyLlmFactory

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
app/lib/tool_schema/ruby_llm_factory.rb

Class Method Summary collapse

Class Method Details

.build(service_class, schema, before_call: nil, after_call: nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/lib/tool_schema/ruby_llm_factory.rb', line 19

def self.build(service_class, schema, before_call: nil, after_call: nil)
  tool_constant = tool_class_name(service_class)
  parent = Tools
  parent.send(:remove_const, tool_constant) if parent.const_defined?(tool_constant, false)

  tool_name = T.let(schema[:name], String)
  klass = Class.new(RubyLLM::Tool) do
    description(schema[:description])
    params(&RubyLlmBuilder.params_block(schema[:params]))

    # Override RubyLLM::Tool#name to use the tool_name from ToolMeta
    # instead of the auto-generated name from the Ruby class name.
    define_method(:name) { tool_name }

    define_method(:execute) do |**kwargs|
      before_call&.call(kwargs)
      result = service_class.new.public_send(schema[:entrypoint], **kwargs)
      after_call&.call(result)
      result
    end
  end

  parent.const_set(tool_constant, klass)
end

.tool_class_name(service_class) ⇒ Object



45
46
47
48
# File 'app/lib/tool_schema/ruby_llm_factory.rb', line 45

def self.tool_class_name(service_class)
  base = service_class.name.split('::').last
  base.gsub(/Service$/, '')
end