Class: RubyPi::Tools::Definition

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_pi/tools/definition.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, description:, category: nil, parameters: {}) {|Hash| ... } ⇒ Definition

Creates a new tool definition.

Parameters:

  • name (String, Symbol)

    Unique identifier for the tool.

  • description (String)

    What the tool does (shown to the LLM).

  • category (Symbol, nil) (defaults to: nil)

    Optional grouping category.

  • parameters (Hash) (defaults to: {})

    JSON Schema hash for the tool’s input parameters.

Yields:

  • (Hash)

    Block that implements the tool logic. Receives a hash of arguments.

Raises:

  • (ArgumentError)

    If name or description is missing, or no block given.



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ruby_pi/tools/definition.rb', line 48

def initialize(name:, description:, category: nil, parameters: {}, &block)
  raise ArgumentError, "Tool name is required" if name.nil? || name.to_s.strip.empty?
  raise ArgumentError, "Tool description is required" if description.nil? || description.strip.empty?
  raise ArgumentError, "Tool implementation block is required" unless block_given?

  @name = name.to_sym
  @description = description
  @category = category&.to_sym
  @parameters = parameters
  @implementation = block
end

Instance Attribute Details

#categorySymbol? (readonly)

Returns An optional category for grouping related tools.

Returns:

  • (Symbol, nil)

    An optional category for grouping related tools.



35
36
37
# File 'lib/ruby_pi/tools/definition.rb', line 35

def category
  @category
end

#descriptionString (readonly)

Returns A human-readable description of what this tool does.

Returns:

  • (String)

    A human-readable description of what this tool does.



32
33
34
# File 'lib/ruby_pi/tools/definition.rb', line 32

def description
  @description
end

#nameSymbol (readonly)

Returns The unique name identifying this tool.

Returns:

  • (Symbol)

    The unique name identifying this tool.



29
30
31
# File 'lib/ruby_pi/tools/definition.rb', line 29

def name
  @name
end

#parametersHash (readonly)

Returns A JSON Schema hash describing the tool’s parameters.

Returns:

  • (Hash)

    A JSON Schema hash describing the tool’s parameters.



38
39
40
# File 'lib/ruby_pi/tools/definition.rb', line 38

def parameters
  @parameters
end

Instance Method Details

#call(args = {}) ⇒ Object

Invokes the tool with the given arguments.

Parameters:

  • args (Hash) (defaults to: {})

    The arguments to pass to the tool implementation.

Returns:

  • (Object)

    Whatever the implementation block returns.



64
65
66
# File 'lib/ruby_pi/tools/definition.rb', line 64

def call(args = {})
  @implementation.call(args)
end

#inspectString

Provides a human-readable string representation of the definition.

Returns:

  • (String)

    A summary string for debugging/logging.



119
120
121
# File 'lib/ruby_pi/tools/definition.rb', line 119

def inspect
  "#<RubyPi::Tools::Definition name=#{@name.inspect} category=#{@category.inspect}>"
end

#to_anthropic_formatHash

Converts this tool definition to Anthropic’s tool format.

Anthropic expects:

{ name: "...", description: "...", input_schema: { ... } }

Returns:

  • (Hash)

    The tool in Anthropic’s tool format.



89
90
91
92
93
94
95
96
# File 'lib/ruby_pi/tools/definition.rb', line 89

def to_anthropic_format
  tool = {
    name: @name.to_s,
    description: @description
  }
  tool[:input_schema] = @parameters unless @parameters.empty?
  tool
end

#to_gemini_formatHash

Converts this tool definition to Google Gemini function declaration format.

Gemini expects:

{ name: "...", description: "...", parameters: { ... } }

Returns:

  • (Hash)

    The tool in Gemini’s function declaration format.



74
75
76
77
78
79
80
81
# File 'lib/ruby_pi/tools/definition.rb', line 74

def to_gemini_format
  declaration = {
    name: @name.to_s,
    description: @description
  }
  declaration[:parameters] = @parameters unless @parameters.empty?
  declaration
end

#to_openai_formatHash

Converts this tool definition to OpenAI’s function calling format.

OpenAI expects:

{ type: "function", function: { name: "...", description: "...", parameters: { ... } } }

Returns:

  • (Hash)

    The tool in OpenAI’s function format.



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/ruby_pi/tools/definition.rb', line 104

def to_openai_format
  function = {
    name: @name.to_s,
    description: @description
  }
  function[:parameters] = @parameters unless @parameters.empty?
  {
    type: "function",
    function: function
  }
end