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:, attempt: 0, errors: [], input_ids: [], name: nil, tags: [], context_name: nil, context_tags: [], transform: nil) ⇒ Result



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/rager/result.rb', line 82

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

  @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.



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

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.



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

def context_name
  @context_name
end

#context_tagsObject (readonly)

Returns the value of attribute context_tags.



57
58
59
# File 'lib/rager/result.rb', line 57

def context_tags
  @context_tags
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.



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

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.



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

def input_ids
  @input_ids
end

#nameObject (readonly)

Returns the value of attribute name.



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

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.



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

def tags
  @tags
end

#transformObject (readonly)

Returns the value of attribute transform.



60
61
62
# File 'lib/rager/result.rb', line 60

def transform
  @transform
end

Instance Method Details

#let(&block) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/rager/result.rb', line 159

def let(&block)
  transform = yield(value)

  Result.new(
    id: @id,
    context_id: @context_id,
    operation: @operation,
    input: @input,
    output: @output,
    options: @options,
    start_time: @start_time,
    end_time: @end_time,
    attempt: @attempt,
    errors: @errors,
    input_ids: @input_ids,
    name: @name,
    tags: @tags,
    context_name: @context_name,
    context_tags: @context_tags,
    transform: transform
  )
end

#logObject



212
213
214
215
216
217
218
# File 'lib/rager/result.rb', line 212

def log
  Rager::Utils::Http.log_remote(
    "results",
    to_json,
    !success?
  )
end

#streamObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/rager/result.rb', line 116

def stream
  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

#stream?Boolean



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

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

#success?Boolean



106
107
108
# File 'lib/rager/result.rb', line 106

def success?
  @errors.empty?
end

#to_hObject



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/rager/result.rb', line 183

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

#to_jsonObject



207
208
209
# File 'lib/rager/result.rb', line 207

def to_json
  to_h.to_json
end

#valueObject



137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/rager/result.rb', line 137

def value
  return @transform if @transform

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

  T.cast(stream, Rager::Types::Stream).each { |_| } unless @consumed

  @buffer
    .group_by(&:index)
    .transform_values { |deltas| deltas.map(&:content).join }
    .sort
    .map(&:last)
    .then { |parts| (parts.length == 1) ? parts.first : parts }
end