Class: Riffer::Tools::Response

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

Overview

Riffer::Tools::Response represents the result of a tool execution.

All tools must return a Response object from their call method. Use Response.success for successful results and Response.error for failures.

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

Constant Summary collapse

VALID_FORMATS =

: Array

%i[text json].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#contentObject (readonly)

: String



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

def content
  @content
end

#error_messageObject (readonly)

: String?



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

def error_message
  @error_message
end

#error_typeObject (readonly)

: Symbol?



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

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



62
63
64
# File 'lib/riffer/tools/response.rb', line 62

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



54
55
56
# File 'lib/riffer/tools/response.rb', line 54

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



33
34
35
36
37
38
39
40
# File 'lib/riffer/tools/response.rb', line 33

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



46
47
48
# File 'lib/riffer/tools/response.rb', line 46

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

Instance Method Details

#error?Boolean

– : () -> bool

Returns:



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

def error? = !@success

#success?Boolean

– : () -> bool

Returns:



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

def success? = @success

#to_hObject

Returns a hash representation of the response.

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



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

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