class Riffer::Tools::Response
Represents the result of a tool execution; every toolβs call must return one.
class MyTool < Riffer::Tool def call(context:, **kwargs) result = perform_operation Riffer::Tools::Response.success(result) rescue MyError => e Riffer::Tools::Response.error(e.message) end end
Constants
- VALID_FORMATS
-
@rbs @success: bool
Attributes
The response content.
The error message, or nil on success.
The error type, or nil on success.
Public Class Methods
Source
# File lib/riffer/tools/response.rb, line 66 def self.error(message, type: :execution_error) new(content: message, success: false, error_message: message, error_type: type) end
Creates an error response.
Source
# File lib/riffer/tools/response.rb, line 58 def self.json(result) success(result, format: :json) end
Creates a success response with JSON format.
Source
# File lib/riffer/tools/response.rb, line 92 def initialize(content:, success:, error_message: nil, error_type: nil) @content = content @success = success @error_message = error_message @error_type = error_type end
Source
# File lib/riffer/tools/response.rb, line 37 def self.success(result, format: :text) unless VALID_FORMATS.include?(format) raise Riffer::ArgumentError, "Invalid format: #{format}. Must be one of: #{VALID_FORMATS.join(", ")}" end content = (format == :json) ? result.to_json : result.to_s new(content: content, success: true) end
Creates a success response.
Raises Riffer::ArgumentError if format is invalid.
Source
# File lib/riffer/tools/response.rb, line 50 def self.text(result) success(result, format: :text) end
Creates a success response with text format.
Public Instance Methods
Source
# File lib/riffer/tools/response.rb, line 78 def error? = !@success # Returns a hash representation of the response. # #-- #: () -> Hash[Symbol, untyped] def to_h {content: @content, error: @error_message, error_type: @error_type} end private #-- #: (content: String, success: bool, ?error_message: String?, ?error_type: Symbol?) -> void def initialize(content:, success:, error_message: nil, error_type: nil) @content = content @success = success @error_message = error_message @error_type = error_type end end
Returns true if the tool execution failed.
Source
# File lib/riffer/tools/response.rb, line 73 def success? = @success # Returns true if the tool execution failed. #-- #: () -> bool def error? = !@success # Returns a hash representation of the response. # #-- #: () -> Hash[Symbol, untyped] def to_h {content: @content, error: @error_message, error_type: @error_type} end private #-- #: (content: String, success: bool, ?error_message: String?, ?error_type: Symbol?) -> void def initialize(content:, success:, error_message: nil, error_type: nil) @content = content @success = success @error_message = error_message @error_type = error_type end
Returns true if the tool execution succeeded.
Source
# File lib/riffer/tools/response.rb, line 84 def to_h {content: @content, error: @error_message, error_type: @error_type} end
Returns a hash representation of the response.