class Rager::Result
Attributes
Public Class Methods
Source
# File lib/rager/result.rb 74 def initialize( 75 id:, 76 context_id:, 77 operation:, 78 input:, 79 output:, 80 options:, 81 start_time:, 82 end_time:, 83 name: nil, 84 context_name: nil, 85 tags: [], 86 input_ids: [], 87 errors: [], 88 attempt: 0 89 ) 90 @id = id 91 @context_id = context_id 92 @operation = operation 93 @input = input 94 @output = output 95 @options = options 96 @start_time = start_time 97 @end_time = end_time 98 @name = T.let(name, T.nilable(String)) 99 @context_name = T.let(context_name, T.nilable(String)) 100 @tags = T.let(tags, T::Array[String]) 101 @input_ids = T.let(input_ids, T::Array[String]) 102 @errors = T.let(errors, T::Array[String]) 103 @attempt = attempt 104 105 @stream = T.let(nil, T.nilable(Rager::Types::Stream)) 106 @buffer = T.let([], Rager::Types::Buffer) 107 @consumed = T.let(false, T::Boolean) 108 end
Public Instance Methods
Source
# File lib/rager/result.rb 221 def log 222 return unless Rager.config.logger_type 223 224 json = to_h.to_json 225 226 logger = Rager.config.logger 227 228 case Rager.config.logger_type 229 when Rager::Logger::Stdout 230 success? ? logger.info(json) : logger.error(json) 231 when Rager::Logger::Remote 232 http_adapter = Rager.config.http_adapter 233 url = Rager.config.url 234 api_key = Rager.config.api_key 235 236 unless url && api_key 237 raise Rager::Errors::CredentialsError.new("Rager Cloud", details: "Missing url or api_key for remote logging") 238 end 239 240 headers = { 241 "Content-Type" => "application/json", 242 "Authorization" => "Bearer #{api_key}" 243 } 244 245 request = Rager::Http::Request.new( 246 url: url, 247 verb: Rager::Http::Verb::Post, 248 headers: headers, 249 body: json 250 ) 251 252 response = http_adapter.make_request(request) 253 254 unless response.success? 255 logger.error("Failed to log to remote server: #{response.status} #{response.body}") 256 end 257 end 258 end
Source
# File lib/rager/result.rb 142 def mat 143 return T.cast(@output, Rager::Types::NonStreamOutput) unless stream? 144 145 if !@consumed 146 T.cast(out, Rager::Types::Stream).each { |_| } 147 end 148 149 parts = {} 150 151 @buffer.each do |message_delta| 152 parts[message_delta.index] = 153 (parts[message_delta.index] || "") + message_delta.content 154 end 155 156 parts 157 .sort_by { |index, _| index } 158 .map { |_, content| content } 159 .then { |parts| (parts.length == 1) ? parts.first : parts } 160 end
Source
# File lib/rager/result.rb 121 def out 122 return T.must(@output) unless stream? 123 return @buffer.each if @consumed 124 125 @stream = Enumerator.new do |yielder| 126 T.cast(@output, Rager::Types::Stream) 127 .each { |message_delta| 128 @buffer << message_delta 129 yielder << message_delta 130 } 131 132 @consumed = true 133 @end_time = Time.now.to_i 134 135 log 136 end 137 138 @stream 139 end
Source
# File lib/rager/result.rb 171 def serialize_input 172 case @input 173 when String 174 @input 175 when Hash 176 @input.transform_keys(&:to_s) 177 when Array 178 @input.map do |item| 179 if !item.is_a?(String) 180 item.serialize 181 else 182 item 183 end 184 end 185 else 186 @input.to_s 187 end 188 end
Source
# File lib/rager/result.rb 191 def serialize_output 192 return nil unless success? 193 return @consumed ? mat : "[STREAM]" if stream? 194 T.cast(@output, Rager::Types::NonStreamOutput) 195 end
Source
# File lib/rager/result.rb 198 def to_h 199 { 200 id: @id, 201 version: "1", 202 data: { 203 context_id: @context_id, 204 operation: @operation.serialize, 205 input: {input: serialize_input}, 206 output: {output: serialize_output}, 207 options: @options.serialize_safe, 208 start_time: @start_time, 209 end_time: @end_time, 210 name: @name, 211 context_name: @context_name, 212 tags: @tags, 213 input_ids: @input_ids, 214 errors: @errors, 215 attempt: @attempt 216 } 217 } 218 end