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 =
%i[text json].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#contentObject (readonly)

: Array



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

def content
  @content
end

#error_messageObject (readonly)

: String?



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

def error_message
  @error_message
end

#error_typeObject (readonly)

: Symbol?



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

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



64
65
66
# File 'lib/riffer/tools/response.rb', line 64

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



56
57
58
# File 'lib/riffer/tools/response.rb', line 56

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



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

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



48
49
50
# File 'lib/riffer/tools/response.rb', line 48

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

Instance Method Details

#error?Boolean

– : () -> bool

Returns:

  • (Boolean)


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

def error? = !@success

#success?Boolean

– : () -> bool

Returns:

  • (Boolean)


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

def success? = @success

#to_hObject

Returns a hash representation of the response.

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



80
81
82
# File 'lib/riffer/tools/response.rb', line 80

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