Class: Riffer::Tools::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/riffer/tools/response.rb

Overview

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

Direct Known Subclasses

Mcp::SearchTool::Result

Constant Summary collapse

VALID_FORMATS =
%i[text json].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#contentObject (readonly)

The response content.



23
24
25
# File 'lib/riffer/tools/response.rb', line 23

def content
  @content
end

#error_messageObject (readonly)

The error message, or nil on success.



26
27
28
# File 'lib/riffer/tools/response.rb', line 26

def error_message
  @error_message
end

#error_typeObject (readonly)

The error type, or nil on success.



29
30
31
# File 'lib/riffer/tools/response.rb', line 29

def error_type
  @error_type
end

Class Method Details

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

Creates an error response.

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



66
67
68
# 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

.json(result) ⇒ Object

Creates a success response with JSON format.

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



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

def self.json(result)
  success(result, format: :json)
end

.success(result, format: :text) ⇒ Object

Creates a success response.

Raises Riffer::ArgumentError if format is invalid.

– : (untyped, ?format: Symbol) -> Riffer::Tools::Response



37
38
39
40
41
42
43
44
# 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

.text(result) ⇒ Object

Creates a success response with text format.

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



50
51
52
# File 'lib/riffer/tools/response.rb', line 50

def self.text(result)
  success(result, format: :text)
end

Instance Method Details

#error?Boolean

Returns true if the tool execution failed. – : () -> bool

Returns:

  • (Boolean)


78
# File 'lib/riffer/tools/response.rb', line 78

def error? = !@success

#success?Boolean

Returns true if the tool execution succeeded. – : () -> bool

Returns:

  • (Boolean)


73
# File 'lib/riffer/tools/response.rb', line 73

def success? = @success

#to_hObject

Returns a hash representation of the response.

– : () -> Hash[Symbol, untyped]



84
85
86
# File 'lib/riffer/tools/response.rb', line 84

def to_h
  {content: @content, error: @error_message, error_type: @error_type}
end