Module: LangGraphRB::ToolDefinition

Included in:
ToolBase
Defined in:
lib/langgraph_rb/tool_definition.rb

Overview

Mixin to declare tool functions compatible with OpenAI tool/function calling

Defined Under Namespace

Classes: FunctionSchemaBuilder

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



6
7
8
# File 'lib/langgraph_rb/tool_definition.rb', line 6

def self.extended(base)
  base.instance_variable_set(:@__tool_functions, {})
end

Instance Method Details

#define_function(name, description: "", &block) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/langgraph_rb/tool_definition.rb', line 10

def define_function(name, description: "", &block)
  class_name = self.name.gsub('::', '--')
  fn_name = "#{class_name}__#{name}".to_sym
  @__tool_functions ||= {}
  @__tool_functions[fn_name] = {
    name: fn_name,
    description: description,
    parameters: { type: 'object', properties: {}, required: [] }
  }

  # Evaluate the DSL inside a builder to collect properties
  if block
    builder = FunctionSchemaBuilder.new(@__tool_functions[fn_name][:parameters])
    builder.instance_eval(&block)
  end
end

#to_openai_tool_schemaObject



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/langgraph_rb/tool_definition.rb', line 31

def to_openai_tool_schema
  # One class may expose multiple functions; return an array of tool entries
  tool_functions.values.map do |fn|
    {
      type: 'function',
      function: {
        name: fn[:name].to_s,
        description: fn[:description],
        parameters: fn[:parameters]
      }
    }
  end
end

#tool_functionsObject



27
28
29
# File 'lib/langgraph_rb/tool_definition.rb', line 27

def tool_functions
  @__tool_functions || {}
end