Class: Riffer::Tool Abstract

Inherits:
Object
  • Object
show all
Extended by:
Helpers::ClassNameConverter
Defined in:
lib/riffer/tool.rb

Overview

This class is abstract.

Subclasses must implement the ‘call` method.

Riffer::Tool is the base class for all tools in the Riffer framework.

Provides a DSL for defining tool description and parameters.

Examples:

class WeatherLookupTool < Riffer::Tool
  description "Provides current weather information for a specified city."

  params do
    required :city, String, description: "The city to look up"
    optional :units, String, default: "celsius"
  end

  def call(context:, city:, units: nil)
    # Implementation
  end
end

See Also:

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.description(value = nil) ⇒ String?

Gets or sets the tool description

Parameters:

  • value (String, nil) (defaults to: nil)

    the description to set, or nil to get

Returns:

  • (String, nil)

    the tool description



31
32
33
34
# File 'lib/riffer/tool.rb', line 31

def description(value = nil)
  return @description if value.nil?
  @description = value.to_s
end

.identifier(value = nil) ⇒ String Also known as: name

Gets or sets the tool identifier/name

Parameters:

  • value (String, nil) (defaults to: nil)

    the identifier to set, or nil to get

Returns:

  • (String)

    the tool identifier (defaults to snake_case class name)



39
40
41
42
# File 'lib/riffer/tool.rb', line 39

def identifier(value = nil)
  return @identifier || class_name_to_path(Module.instance_method(:name).bind_call(self)) if value.nil?
  @identifier = value.to_s
end

.parameters_schemaHash

Returns the JSON Schema for the tool’s parameters

Returns:

  • (Hash)

    the JSON Schema



58
59
60
# File 'lib/riffer/tool.rb', line 58

def parameters_schema
  @params_builder&.to_json_schema || empty_schema
end

.params { ... } ⇒ Riffer::Tools::Params?

Defines parameters using the Params DSL

Yields:

  • the parameter definition block

Returns:



50
51
52
53
54
# File 'lib/riffer/tool.rb', line 50

def params(&block)
  return @params_builder if block.nil?
  @params_builder = Riffer::Tools::Params.new
  @params_builder.instance_eval(&block)
end

Instance Method Details

#call(context:, **kwargs) ⇒ Object

Executes the tool with the given arguments

Parameters:

  • context (Object, nil)

    optional context passed from the agent

  • kwargs (Hash)

    the tool arguments

Returns:

  • (Object)

    the tool result

Raises:

  • (NotImplementedError)

    if not implemented by subclass



74
75
76
# File 'lib/riffer/tool.rb', line 74

def call(context:, **kwargs)
  raise NotImplementedError, "#{self.class} must implement #call"
end

#call_with_validation(context:, **kwargs) ⇒ Object

Executes the tool with validation (used by Agent)

Parameters:

  • context (Object, nil)

    context passed from the agent

  • kwargs (Hash)

    the tool arguments

Returns:

  • (Object)

    the tool result

Raises:



83
84
85
86
87
# File 'lib/riffer/tool.rb', line 83

def call_with_validation(context:, **kwargs)
  params_builder = self.class.params
  validated_args = params_builder ? params_builder.validate(kwargs) : kwargs
  call(context: context, **validated_args)
end