Class: Riffer::Tool

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

Overview

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

Provides a DSL for defining tool description and parameters. Subclasses must implement the call method.

See Riffer::Agent.

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

Constant Summary

Constants included from Toolable

Riffer::Toolable::DEFAULT_TIMEOUT

Instance Method Summary collapse

Methods included from Toolable

all, description, extended, identifier, kind, name, parameters_schema, params, timeout, to_tool_schema, validate_as_tool!

Instance Method Details

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

Executes the tool with the given arguments.

Raises NotImplementedError if not implemented by subclass.

– : (context: Hash[Symbol, untyped]?, **untyped) -> Riffer::Tools::Response

Raises:

  • (NotImplementedError)


37
38
39
# File 'lib/riffer/tool.rb', line 37

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

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

Executes the tool with validation and timeout (used by Agent).

Raises Riffer::ValidationError if validation fails. Raises Riffer::TimeoutError if execution exceeds the configured timeout. Raises Riffer::Error if the tool does not return a Response object.

– : (context: Hash[Symbol, untyped]?, **untyped) -> Riffer::Tools::Response



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/riffer/tool.rb', line 73

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

  result = Timeout.timeout(self.class.timeout) do
    call(context: context, **validated_args)
  end

  unless result.is_a?(Riffer::Tools::Response)
    raise Riffer::Error, "#{self.class} must return a Riffer::Tools::Response from #call"
  end

  result
rescue Timeout::Error
  raise Riffer::TimeoutError, "Tool execution timed out after #{self.class.timeout} seconds"
end

#error(message, type: :execution_error) ⇒ Object

Creates an error response. Shorthand for Riffer::Tools::Response.error.

– : (String, ?type: Symbol) -> Riffer::Tools::Response



61
62
63
# File 'lib/riffer/tool.rb', line 61

def error(message, type: :execution_error)
  Riffer::Tools::Response.error(message, type: type)
end

#json(result) ⇒ Object

Creates a JSON response. Shorthand for Riffer::Tools::Response.json.

– : (untyped) -> Riffer::Tools::Response



53
54
55
# File 'lib/riffer/tool.rb', line 53

def json(result)
  Riffer::Tools::Response.json(result)
end

#text(result) ⇒ Object

Creates a text response. Shorthand for Riffer::Tools::Response.text.

– : (untyped) -> Riffer::Tools::Response



45
46
47
# File 'lib/riffer/tool.rb', line 45

def text(result)
  Riffer::Tools::Response.text(result)
end