Class: Riffer::Tools::Response
- Inherits:
-
Object
- Object
- Riffer::Tools::Response
- 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.)
end
end
Constant Summary collapse
- VALID_FORMATS =
: Array
%i[text json].freeze
Instance Attribute Summary collapse
-
#content ⇒ Object
readonly
: String.
-
#error_message ⇒ Object
readonly
: String?.
-
#error_type ⇒ Object
readonly
: Symbol?.
Class Method Summary collapse
-
.error(message, type: :execution_error) ⇒ Object
Creates an error response.
-
.json(result) ⇒ Object
Creates a success response with JSON format.
-
.success(result, format: :text) ⇒ Object
Creates a success response.
-
.text(result) ⇒ Object
Creates a success response with text format.
Instance Method Summary collapse
-
#error? ⇒ Boolean
– : () -> bool.
-
#success? ⇒ Boolean
– : () -> bool.
-
#to_h ⇒ Object
Returns a hash representation of the response.
Instance Attribute Details
#content ⇒ Object (readonly)
: String
23 24 25 |
# File 'lib/riffer/tools/response.rb', line 23 def content @content end |
#error_message ⇒ Object (readonly)
: String?
24 25 26 |
# File 'lib/riffer/tools/response.rb', line 24 def @error_message end |
#error_type ⇒ Object (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(, type: :execution_error) new(content: , success: false, error_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
72 |
# File 'lib/riffer/tools/response.rb', line 72 def error? = !@success |
#success? ⇒ Boolean
– : () -> bool
68 |
# File 'lib/riffer/tools/response.rb', line 68 def success? = @success |
#to_h ⇒ Object
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 |