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(operation:, options:, input:, output:, start_time:, end_time:, context_id: nil, hash: nil) ⇒ Result



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rager/result.rb', line 48

def initialize(
  operation:,
  options:,
  input:,
  output:,
  start_time:,
  end_time:,
  context_id: nil,
  hash: nil
)
  @operation = operation
  @options = options
  @input = input
  @output = output
  @start_time = start_time
  @end_time = end_time
  @stream = T.let(nil, T.nilable(Rager::Types::Stream))
  @buffer = T.let([], Rager::Types::Buffer)
  @consumed = T.let(false, T::Boolean)
  @result_id = T.let(SecureRandom.uuid, T.nilable(String))
  @context_id = T.let(context_id, T.nilable(String))
  @hash = T.let(hash, T.nilable(String))
end

Instance Attribute Details

#context_idObject (readonly)

Returns the value of attribute context_id.



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

def context_id
  @context_id
end

#end_timeObject (readonly)

Returns the value of attribute end_time.



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

def end_time
  @end_time
end

#hashObject (readonly)

Returns the value of attribute hash.



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

def hash
  @hash
end

#inputObject (readonly)

Returns the value of attribute input.



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

def input
  @input
end

#operationObject (readonly)

Returns the value of attribute operation.



13
14
15
# File 'lib/rager/result.rb', line 13

def operation
  @operation
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#result_idObject (readonly)

Returns the value of attribute result_id.



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

def result_id
  @result_id
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



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

def start_time
  @start_time
end

Instance Method Details

#logObject



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/rager/result.rb', line 163

def log
  return unless Rager.config.logger

  json = to_h.to_json

  case Rager.config.logger
  when Rager::Logger::Stdout
    puts "LOG: #{json}"
  when Rager::Logger::Remote
    http_adapter = Rager.config.http_adapter
    url = Rager.config.url
    api_key = Rager.config.api_key

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

    if url && 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.status >= 200 && response.status < 300
        warn "Remote log failed: \\#{response.status} \\#{response.body}"
      end
    end
  end
end

#matObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/rager/result.rb', line 101

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 }
end

#outObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rager/result.rb', line 78

def out
  return @output unless stream?
  return @buffer.each if @consumed

  log

  @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



121
122
123
124
125
126
127
128
# File 'lib/rager/result.rb', line 121

def serialize_input
  case @input
  when String
    @input
  when Array
    @input.map(&:serialize)
  end
end

#serialize_outputObject



131
132
133
134
135
136
137
138
139
# File 'lib/rager/result.rb', line 131

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

#stream?Boolean



73
74
75
# File 'lib/rager/result.rb', line 73

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

#to_hObject



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 to_h
  {
    operation: @operation.serialize,
    options: @options.serialize_safe,
    input: serialize_input,
    output: if @consumed
              mat
            elsif stream?
              "[STREAM]"
            else
              @output
            end,
    start_time: @start_time,
    end_time: @end_time,
    result_id: @result_id,
    context_id: @context_id,
    hash: @hash
  }
end