class Riffer::Params
Riffer::Params provides a DSL for defining parameters.
Used within a Toolβs params block to define required and optional parameters, and by StructuredOutput to define response schemas.
params do required :city, String, description: "The city name" optional :units, String, default: "celsius", enum: ["celsius", "fahrenheit"] end
Attributes
Public Class Methods
Public Instance Methods
Source
# File lib/riffer/params.rb, line 44 def optional(name, type, description: nil, enum: nil, default: nil, of: nil, &block) nested = build_nested(type, of, &block) @parameters << Riffer::Param.new( name: name, type: type, required: false, description: description, enum: enum, default: default, item_type: of, nested_params: nested ) end
Defines an optional parameter.
Source
# File lib/riffer/params.rb, line 27 def required(name, type, description: nil, enum: nil, of: nil, &block) nested = build_nested(type, of, &block) @parameters << Riffer::Param.new( name: name, type: type, required: true, description: description, enum: enum, item_type: of, nested_params: nested ) end
Defines a required parameter.
Source
# File lib/riffer/params.rb, line 109 def to_json_schema(strict: false) properties = {} required_params = [] @parameters.each do |param| properties[param.name.to_s] = param.to_json_schema(strict: strict) required_params << param.name.to_s if strict || param.required end { type: "object", properties: properties, required: required_params, additionalProperties: false } end
Converts all parameters to JSON Schema format.
When strict is true, every property appears in required and optional properties are made nullable instead. This satisfies providers that enforce strict structured output schemas.
Source
# File lib/riffer/params.rb, line 64 def validate(arguments) validated = {} errors = [] @parameters.each do |param| value = arguments[param.name] if value.nil? && param.required errors << "#{param.name} is required" next end if value.nil? validated[param.name] = param.default next end unless param.valid_type?(value) errors << "#{param.name} must be a #{param.type_name}" next end if param.enum && !param.enum.include?(value) errors << "#{param.name} must be one of: #{param.enum.join(", ")}" next end value = validate_nested(param, value, errors) validated[param.name] = value end raise Riffer::ValidationError, errors.join("; ") if errors.any? validated end
Validates arguments against parameter definitions.
Raises Riffer::ValidationError if validation fails.