Module: RubyLLM::Providers::Bedrock::Streaming::PreludeHandling
- Defined in:
- lib/ruby_llm/providers/bedrock/streaming/prelude_handling.rb
Overview
Module for handling message preludes in AWS Bedrock streaming responses.
Instance Method Summary collapse
- #calculate_positions(offset, total_length, headers_length) ⇒ Object
- #can_read_prelude?(chunk, offset) ⇒ Boolean
- #find_next_message(chunk, offset) ⇒ Object
- #find_next_prelude(chunk, start_offset) ⇒ Object
- #read_prelude(chunk, offset) ⇒ Object
- #valid_lengths?(total_length, headers_length) ⇒ Boolean
- #valid_positions?(headers_end, payload_end, chunk_size) ⇒ Boolean
Instance Method Details
#calculate_positions(offset, total_length, headers_length) ⇒ Object
23 24 25 26 27 |
# File 'lib/ruby_llm/providers/bedrock/streaming/prelude_handling.rb', line 23 def calculate_positions(offset, total_length, headers_length) headers_end = offset + 12 + headers_length payload_end = offset + total_length - 4 # Subtract 4 bytes for message CRC [headers_end, payload_end] end |
#can_read_prelude?(chunk, offset) ⇒ Boolean
9 10 11 |
# File 'lib/ruby_llm/providers/bedrock/streaming/prelude_handling.rb', line 9 def can_read_prelude?(chunk, offset) chunk.bytesize - offset >= 12 end |
#find_next_message(chunk, offset) ⇒ Object
37 38 39 40 |
# File 'lib/ruby_llm/providers/bedrock/streaming/prelude_handling.rb', line 37 def (chunk, offset) next_prelude = find_next_prelude(chunk, offset + 4) next_prelude || chunk.bytesize end |
#find_next_prelude(chunk, start_offset) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/ruby_llm/providers/bedrock/streaming/prelude_handling.rb', line 42 def find_next_prelude(chunk, start_offset) scan_range(chunk, start_offset).each do |pos| return pos if valid_prelude_at_position?(chunk, pos) end nil end |
#read_prelude(chunk, offset) ⇒ Object
13 14 15 16 17 |
# File 'lib/ruby_llm/providers/bedrock/streaming/prelude_handling.rb', line 13 def read_prelude(chunk, offset) total_length = chunk[offset...(offset + 4)].unpack1('N') headers_length = chunk[(offset + 4)...(offset + 8)].unpack1('N') [total_length, headers_length] end |
#valid_lengths?(total_length, headers_length) ⇒ Boolean
19 20 21 |
# File 'lib/ruby_llm/providers/bedrock/streaming/prelude_handling.rb', line 19 def valid_lengths?(total_length, headers_length) valid_length_constraints?(total_length, headers_length) end |
#valid_positions?(headers_end, payload_end, chunk_size) ⇒ Boolean
29 30 31 32 33 34 35 |
# File 'lib/ruby_llm/providers/bedrock/streaming/prelude_handling.rb', line 29 def valid_positions?(headers_end, payload_end, chunk_size) return false if headers_end >= payload_end return false if headers_end >= chunk_size return false if payload_end > chunk_size true end |