Module: ToolSchema::RubyLlmBuilder

Extended by:
T::Sig
Defined in:
app/lib/tool_schema/ruby_llm_builder.rb

Class Method Summary collapse

Class Method Details

.build_param(ctx, param) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/lib/tool_schema/ruby_llm_builder.rb', line 20

def self.build_param(ctx, param)
  name = param[:name]
  case param[:type]
  when :object
    ctx.object(name) do
      (param[:children] || []).each do |child|
        RubyLlmBuilder.build_param(self, child)
      end
    end
  when :array
    item = param[:item_type]
    if item && scalar_type?(item[:type])
      ctx.array(name, of: scalar_symbol(item[:type]))
    elsif item&.dig(:type) == :object
      ctx.array(name) do
        object :item do
          (item[:children] || []).each do |child|
            RubyLlmBuilder.build_param(self, child)
          end
        end
      end
    else
      ctx.array(name)
    end
  when :any
    ctx.object(name, description: param[:description]) do
      additional_properties true
    end
  else
    method = scalar_method(param[:type])
    ctx.public_send(method, name)
  end
end

.params_block(params_ast) ⇒ Object



11
12
13
14
15
16
17
# File 'app/lib/tool_schema/ruby_llm_builder.rb', line 11

def self.params_block(params_ast)
  proc do
    params_ast.each do |param|
      RubyLlmBuilder.build_param(self, param)
    end
  end
end

.scalar_method(type) ⇒ Object



71
72
73
74
75
76
77
78
# File 'app/lib/tool_schema/ruby_llm_builder.rb', line 71

def self.scalar_method(type)
  case type
  when :integer then :integer
  when :float then :number  # RubyLLM uses `number` not `float`
  when :boolean then :boolean
  else :string
  end
end

.scalar_symbol(type) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'app/lib/tool_schema/ruby_llm_builder.rb', line 60

def self.scalar_symbol(type)
  case type
  when :string then :string
  when :integer then :integer
  when :float then :number  # RubyLLM uses `number` not `float`
  when :boolean then :boolean
  else :any
  end
end

.scalar_type?(type) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'app/lib/tool_schema/ruby_llm_builder.rb', line 55

def self.scalar_type?(type)
  %i[string integer float boolean].include?(type)
end