Class: Baml::BamlStream

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

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.



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

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

  @final_response = nil
end

Instance Method Details

#each(&block) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/stream.rb', line 63

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::Types, Baml::PartialTypes, true)
    end
  end

  self
end

#get_final_responseObject



80
81
82
83
84
85
86
# File 'lib/stream.rb', line 80

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

  @final_response.parsed_using_types(Baml::Types, Baml::PartialTypes, false)
end