Class: LlmCostTracker::Integrations::StreamTracker

Inherits:
Object
  • Object
show all
Defined in:
lib/llm_cost_tracker/integrations/stream_tracker.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stream, collector, active, finish) ⇒ StreamTracker

Returns a new instance of StreamTracker.



15
16
17
18
19
20
21
22
23
# File 'lib/llm_cost_tracker/integrations/stream_tracker.rb', line 15

def initialize(stream, collector, active, finish)
  @stream = stream
  @collector = collector
  @active = active
  @finish = finish || proc { |errored:| @collector.finish!(errored: errored) }
  @finished = false
  @capture_failed = false
  @monitor = Monitor.new
end

Class Method Details

.wrap(stream, collector:, active:, finish: nil) ⇒ Object



13
# File 'lib/llm_cost_tracker/integrations/stream_tracker.rb', line 13

def self.wrap(stream, collector:, active:, finish: nil) = new(stream, collector, active, finish).wrap

Instance Method Details

#wrapObject



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/llm_cost_tracker/integrations/stream_tracker.rb', line 25

def wrap
  return @stream unless @stream

  iterator_wrapped = @stream.instance_variable_defined?(:@iterator) && wrap_iterator?
  wrap_each if !iterator_wrapped && @stream.respond_to?(:each)

  @stream
rescue StandardError => e
  Logging.warn("stream integration failed to install wrapper: #{e.class}: #{e.message}")
  @stream
end