Class: Riffer::Tools::Response
- Inherits:
-
Object
- Object
- Riffer::Tools::Response
- Defined in:
- lib/riffer/tools/response.rb,
sig/generated/riffer/tools/response.rbs
Overview
Direct Known Subclasses
Constant Summary collapse
- VALID_FORMATS =
%i[text json].freeze
Instance Attribute Summary collapse
-
#content ⇒ String
readonly
The response content.
-
#error_message ⇒ String?
readonly
The error message, or
nilon success. -
#error_type ⇒ Symbol?
readonly
The error type, or
nilon success. -
#exception ⇒ Exception?
readonly
The exception an unhandled failure was folded from, or
nil.
Class Method Summary collapse
-
.error(message, type: :execution_error, exception: nil) ⇒ Riffer::Tools::Response
Creates an error response.
-
.json(result) ⇒ Riffer::Tools::Response
Creates a success response with JSON format.
-
.success(result, format: :text) ⇒ Riffer::Tools::Response
Creates a success response.
-
.text(result) ⇒ Riffer::Tools::Response
Creates a success response with text format.
Instance Method Summary collapse
-
#error? ⇒ Boolean
Returns true if the tool execution failed.
-
#initialize(content:, success:, error_message: nil, error_type: nil, exception: nil) ⇒ Response
constructor
-- : (content: String, success: bool, ?error_message: String?, ?error_type: Symbol?, ?exception: Exception?) -> void.
-
#success? ⇒ Boolean
Returns true if the tool execution succeeded.
-
#to_h ⇒ Hash[Symbol, untyped]
Returns a hash representation of the response.
Constructor Details
#initialize(content:, success:, error_message: nil, error_type: nil, exception: nil) ⇒ Response
-- : (content: String, success: bool, ?error_message: String?, ?error_type: Symbol?, ?exception: Exception?) -> void
96 97 98 99 100 101 102 |
# File 'lib/riffer/tools/response.rb', line 96 def initialize(content:, success:, error_message: nil, error_type: nil, exception: nil) @content = content @success = success @error_message = @error_type = error_type @exception = exception end |
Instance Attribute Details
#content ⇒ String (readonly)
The response content.
23 24 25 |
# File 'lib/riffer/tools/response.rb', line 23 def content @content end |
#error_message ⇒ String? (readonly)
The error message, or nil on success.
26 27 28 |
# File 'lib/riffer/tools/response.rb', line 26 def @error_message end |
#error_type ⇒ Symbol? (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 |
#exception ⇒ Exception? (readonly)
The exception an unhandled failure was folded from, or nil. Kept out of
every serialized form so it never reaches an LLM or a message payload.
33 34 35 |
# File 'lib/riffer/tools/response.rb', line 33 def exception @exception end |
Class Method Details
.error(message, type: :execution_error, exception: nil) ⇒ Riffer::Tools::Response
Creates an error response.
-- : (String, ?type: Symbol, ?exception: Exception?) -> Riffer::Tools::Response
70 71 72 |
# File 'lib/riffer/tools/response.rb', line 70 def self.error(, type: :execution_error, exception: nil) new(content: , success: false, error_message: , error_type: type, exception: exception) end |
.json(result) ⇒ Riffer::Tools::Response
Creates a success response with JSON format.
-- : (untyped) -> Riffer::Tools::Response
62 63 64 |
# File 'lib/riffer/tools/response.rb', line 62 def self.json(result) success(result, format: :json) end |
.success(result, format: :text) ⇒ Riffer::Tools::Response
Creates a success response.
Raises Riffer::ArgumentError if format is invalid.
-- : (untyped, ?format: Symbol) -> Riffer::Tools::Response
41 42 43 44 45 46 47 48 |
# File 'lib/riffer/tools/response.rb', line 41 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) ⇒ Riffer::Tools::Response
Creates a success response with text format.
-- : (untyped) -> Riffer::Tools::Response
54 55 56 |
# File 'lib/riffer/tools/response.rb', line 54 def self.text(result) success(result, format: :text) end |
Instance Method Details
#error? ⇒ Boolean
Returns true if the tool execution failed.
: () -> bool
82 |
# File 'lib/riffer/tools/response.rb', line 82 def error? = !@success |
#success? ⇒ Boolean
Returns true if the tool execution succeeded.
: () -> bool
77 |
# File 'lib/riffer/tools/response.rb', line 77 def success? = @success |
#to_h ⇒ Hash[Symbol, untyped]
Returns a hash representation of the response.
-- : () -> Hash[Symbol, untyped]
88 89 90 |
# File 'lib/riffer/tools/response.rb', line 88 def to_h { content: @content, error: @error_message, error_type: @error_type } end |