Class: Baml::BamlStream

Inherits:
Object
  • Object
show all
Extended by:
T::Generic, T::Sig
Includes:
Enumerable
Defined in:
lib/stream.rb

Overview

def each(&block)

  @raw_stream.each do |raw_msg|
    yield Message.from(raw_msg)
  end
end

end

Constant Summary collapse

PartialType =
type_member
FinalType =
type_member

Instance Method Summary collapse

Constructor Details

#initialize(ffi_stream:, ctx_manager:) ⇒ BamlStream

Returns a new instance of BamlStream.



27
28
29
30
31
32
33
34
35
# File 'lib/stream.rb', line 27

def initialize(
  ffi_stream:,
  ctx_manager:
)
  @ffi_stream = ffi_stream
  @ctx_manager = ctx_manager

  @final_response = nil
end

Instance Method Details

#each(&block) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/stream.rb', line 45

def each(&block)
  # Implementing this and include-ing Enumerable allows users to treat this as a Ruby
  # collection: https://ruby-doc.org/3.1.6/Enumerable.html#module-Enumerable-label-Usage
  if @final_response == nil
    @final_response = @ffi_stream.done(@ctx_manager) do |event|
      block.call event.parsed_using_types(Baml::PartialTypes)
    end
  end

  self
end

#get_final_responseObject



62
63
64
65
66
67
68
# File 'lib/stream.rb', line 62

def get_final_response
  if @final_response == nil
    @final_response = @ffi_stream.done(@ctx_manager)
  end

  @final_response.parsed_using_types(Baml::Types)
end