Class: Rager::Result

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/rager/result.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, context_id:, operation:, input:, output:, options:, start_time:, end_time:, name: nil, context_name: nil, tags: [], input_ids: [], errors: [], attempt: 0) ⇒ Result

Returns a new instance of Result.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/rager/result.rb', line 74

def initialize(
  id:,
  context_id:,
  operation:,
  input:,
  output:,
  options:,
  start_time:,
  end_time:,
  name: nil,
  context_name: nil,
  tags: [],
  input_ids: [],
  errors: [],
  attempt: 0
)
  @id = id
  @context_id = context_id
  @operation = operation
  @input = input
  @output = output
  @options = options
  @start_time = start_time
  @end_time = end_time
  @name = T.let(name, T.nilable(String))
  @context_name = T.let(context_name, T.nilable(String))
  @tags = T.let(tags, T::Array[String])
  @input_ids = T.let(input_ids, T::Array[String])
  @errors = T.let(errors, T::Array[String])
  @attempt = attempt

  @stream = T.let(nil, T.nilable(Rager::Types::Stream))
  @buffer = T.let([], Rager::Types::Buffer)
  @consumed = T.let(false, T::Boolean)
end

Instance Attribute Details

#attemptObject (readonly)

Returns the value of attribute attempt.



54
55
56
# File 'lib/rager/result.rb', line 54

def attempt
  @attempt
end

#context_idObject (readonly)

Returns the value of attribute context_id.



18
19
20
# File 'lib/rager/result.rb', line 18

def context_id
  @context_id
end

#context_nameObject (readonly)

Returns the value of attribute context_name.



42
43
44
# File 'lib/rager/result.rb', line 42

def context_name
  @context_name
end

#end_timeObject (readonly)

Returns the value of attribute end_time.



36
37
38
# File 'lib/rager/result.rb', line 36

def end_time
  @end_time
end

#errorsObject (readonly)

Returns the value of attribute errors.



51
52
53
# File 'lib/rager/result.rb', line 51

def errors
  @errors
end

#idObject (readonly)

Returns the value of attribute id.



15
16
17
# File 'lib/rager/result.rb', line 15

def id
  @id
end

#inputObject (readonly)

Returns the value of attribute input.



24
25
26
# File 'lib/rager/result.rb', line 24

def input
  @input
end

#input_idsObject (readonly)

Returns the value of attribute input_ids.



48
49
50
# File 'lib/rager/result.rb', line 48

def input_ids
  @input_ids
end

#nameObject (readonly)

Returns the value of attribute name.



39
40
41
# File 'lib/rager/result.rb', line 39

def name
  @name
end

#operationObject (readonly)

Returns the value of attribute operation.



21
22
23
# File 'lib/rager/result.rb', line 21

def operation
  @operation
end

#optionsObject (readonly)

Returns the value of attribute options.



30
31
32
# File 'lib/rager/result.rb', line 30

def options
  @options
end

#outputObject (readonly)

Returns the value of attribute output.



27
28
29
# File 'lib/rager/result.rb', line 27

def output
  @output
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



33
34
35
# File 'lib/rager/result.rb', line 33

def start_time
  @start_time
end

#tagsObject (readonly)

Returns the value of attribute tags.



45
46
47
# File 'lib/rager/result.rb', line 45

def tags
  @tags
end

Instance Method Details

#logObject



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/rager/result.rb', line 221

def log
  return unless Rager.config.logger_type

  json = to_h.to_json

  logger = Rager.config.logger

  case Rager.config.logger_type
  when Rager::Logger::Stdout
    success? ? logger.info(json) : logger.error(json)
  when Rager::Logger::Remote
    http_adapter = Rager.config.http_adapter
    url = Rager.config.url
    api_key = Rager.config.api_key

    unless url && api_key
      raise Rager::Errors::CredentialsError.new("Rager Cloud", details: "Missing url or api_key for remote logging")
    end

    headers = {
      "Content-Type" => "application/json",
      "Authorization" => "Bearer #{api_key}"
    }

    request = Rager::Http::Request.new(
      url: url,
      verb: Rager::Http::Verb::Post,
      headers: headers,
      body: json
    )

    response = http_adapter.make_request(request)

    unless response.success?
      logger.error("Failed to log to remote server: #{response.status} #{response.body}")
    end
  end
end

#matObject



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/rager/result.rb', line 142

def mat
  return T.cast(@output, Rager::Types::NonStreamOutput) unless stream?

  if !@consumed
    T.cast(out, Rager::Types::Stream).each { |_| }
  end

  parts = {}

  @buffer.each do |message_delta|
    parts[message_delta.index] =
      (parts[message_delta.index] || "") + message_delta.content
  end

  parts
    .sort_by { |index, _| index }
    .map { |_, content| content }
    .then { |parts| (parts.length == 1) ? parts.first : parts }
end

#outObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/rager/result.rb', line 121

def out
  return T.must(@output) unless stream?
  return @buffer.each if @consumed

  @stream = Enumerator.new do |yielder|
    T.cast(@output, Rager::Types::Stream)
      .each { |message_delta|
      @buffer << message_delta
      yielder << message_delta
    }

    @consumed = true
    @end_time = Time.now.to_i

    log
  end

  @stream
end

#serialize_inputObject



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/rager/result.rb', line 171

def serialize_input
  case @input
  when String
    @input
  when Hash
    @input.transform_keys(&:to_s)
  when Array
    @input.map do |item|
      if !item.is_a?(String)
        item.serialize
      else
        item
      end
    end
  else
    @input.to_s
  end
end

#serialize_outputObject



191
192
193
194
195
# File 'lib/rager/result.rb', line 191

def serialize_output
  return nil unless success?
  return @consumed ? mat : "[STREAM]" if stream?
  T.cast(@output, Rager::Types::NonStreamOutput)
end

#stream?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/rager/result.rb', line 116

def stream?
  @output.is_a?(Enumerator)
end

#success?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/rager/result.rb', line 111

def success?
  @errors.empty?
end

#to_hObject



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/rager/result.rb', line 198

def to_h
  {
    id: @id,
    version: "1",
    data: {
      context_id: @context_id,
      operation: @operation.serialize,
      input: {input: serialize_input},
      output: {output: serialize_output},
      options: @options.serialize_safe,
      start_time: @start_time,
      end_time: @end_time,
      name: @name,
      context_name: @context_name,
      tags: @tags,
      input_ids: @input_ids,
      errors: @errors,
      attempt: @attempt
    }
  }
end