Class: SolidLoop::SseStreamAggregator

Inherits:
Object
  • Object
show all
Defined in:
app/services/solid_loop/sse_stream_aggregator.rb

Constant Summary collapse

CONCAT_KEYS =
%w[content arguments reasoning_content reasoning thought].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSseStreamAggregator

Returns a new instance of SseStreamAggregator.



7
8
9
10
11
# File 'app/services/solid_loop/sse_stream_aggregator.rb', line 7

def initialize
  @result = {}
  @done = false
  @parser = EventStreamParser::Parser.new
end

Instance Attribute Details

#resultObject (readonly)

Returns the value of attribute result.



5
6
7
# File 'app/services/solid_loop/sse_stream_aggregator.rb', line 5

def result
  @result
end

Instance Method Details

#done?Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
30
# File 'app/services/solid_loop/sse_stream_aggregator.rb', line 24

def done?
  return true if @done
  choices = @result["choices"] || []
  return false if choices.empty?

  choices.any? { |c| c["finish_reason"].present? }
end

#ingest(chunk) ⇒ Object



13
14
15
16
17
18
19
20
# File 'app/services/solid_loop/sse_stream_aggregator.rb', line 13

def ingest(chunk)
  return if chunk.blank?

  # The block must be passed to feed in event_stream_parser v1.x
  @parser.feed(chunk) do |type, data, id, reconnection_time|
    process_event(type, data)
  end
end