class Riffer::Tool
Constants
- DEFAULT_TIMEOUT
-
@rbs self.@description: String? @rbs self.@identifier: String? @rbs self.@timeout: (Integer | Float)? @rbs self.@params_builder: Riffer::Params?
Public Class Methods
Source
# File lib/riffer/tool.rb, line 19 def description(value = nil) return @description if value.nil? @description = value.to_s end
Source
# File lib/riffer/tool.rb, line 27 def identifier(value = nil) return @identifier || Riffer::Helpers::Identifier.for(self) if value.nil? @identifier = value.to_s end
Source
# File lib/riffer/tool.rb, line 35 def name(value = nil) return identifier(value) if value identifier end
Source
# File lib/riffer/tool.rb, line 61 def parameters_schema(strict: false) @params_builder&.to_json_schema(strict: strict) || empty_schema end
Source
# File lib/riffer/tool.rb, line 51 def params(&block) return @params_builder if block.nil? builder = Riffer::Params.new builder.instance_eval(&block) @params_builder = builder end
Source
# File lib/riffer/tool.rb, line 43 def timeout(value = nil) return @timeout || DEFAULT_TIMEOUT if value.nil? @timeout = value.to_f end
Source
# File lib/riffer/tool.rb, line 67 def to_tool_schema(strict: false) { name: name, description: description, parameters_schema: parameters_schema(strict: strict), } end
Source
# File lib/riffer/tool.rb, line 77 def validate_as_tool! if description.nil? || description.to_s.strip.empty? raise Riffer::ArgumentError, "#{self} must define a description" end raise Riffer::ArgumentError, "#{self} must have an identifier" if identifier.nil? || identifier.to_s.strip.empty? true end
Public Instance Methods
Source
# File lib/riffer/tool.rb, line 98 def call(context:, **kwargs) raise NotImplementedError, "#{self.class} must implement #call" end
Source
# File lib/riffer/tool.rb, line 128 def call_with_validation(context:, **kwargs) params_builder = self.class.params begin validated_args = params_builder ? params_builder.validate(kwargs) : kwargs rescue Riffer::ValidationError => e return Riffer::Tools::Response.error(e.message, type: :validation_error) end result = Timeout.timeout(self.class.timeout, Riffer::TimeoutError) do call(context: context, **validated_args) #: untyped end unless result.is_a?(Riffer::Tools::Response) raise Riffer::Error, "#{self.class} must return a Riffer::Tools::Response from #call" end result rescue Riffer::TimeoutError Riffer::Tools::Response.error( "Tool execution timed out after #{self.class.timeout} seconds", type: :timeout_error, ) rescue Riffer::ToolExecutionError => e Riffer::Tools::Response.error(e.message, type: :execution_error) # Anything outside StandardError — an unimplemented #call above all — still # propagates: a broken tool is a broken deploy, not a bad request. rescue StandardError => e Riffer::Tools::Response.error( "Error executing tool: #{e.class}: #{e.message}", type: :unhandled_error, exception: e, ) end
Source
# File lib/riffer/tool.rb, line 122 def error(message, type: :execution_error) Riffer::Tools::Response.error(message, type: type) end
Source
# File lib/riffer/tool.rb, line 110 def json(result) Riffer::Tools::Response.json(result) end
Source
# File lib/riffer/tool.rb, line 116 def load_tools(tools) Riffer::Tools::Response.load_tools(tools) end
Source
# File lib/riffer/tool.rb, line 104 def text(result) Riffer::Tools::Response.text(result) end