Class: LLM::Bedrock::StreamDecoder Private

Inherits:
Object
  • Object
show all
Defined in:
lib/llm/providers/bedrock/stream_decoder.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Decodes AWS Event Stream binary frames.

Bedrock Converse Stream uses the AWS Event Stream protocol, a binary framing format (not SSE). Each message has:

- total length (4 bytes, big-endian)
- headers length (4 bytes, big-endian)
- prelude CRC (4 bytes)
- headers (variable)
- payload (variable, usually JSON)
- message CRC (4 bytes)

Implements #<< to match the interface expected by llm.rb’s streaming transport, so it can replace the SSE-based StreamDecoder when streaming from Bedrock.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parser) ⇒ StreamDecoder

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of StreamDecoder.

Parameters:



30
31
32
33
# File 'lib/llm/providers/bedrock/stream_decoder.rb', line 30

def initialize(parser)
  @buffer = +"".b
  @parser = parser
end

Instance Attribute Details

#parserLLM::Bedrock::StreamParser (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



26
27
28
# File 'lib/llm/providers/bedrock/stream_decoder.rb', line 26

def parser
  @parser
end

Instance Method Details

#<<(chunk) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Feeds a raw binary chunk into the decoder. Accumulates data until complete frames are available, then decodes them and passes the JSON payload to the parser.

Parameters:

  • chunk (String)

    Raw binary data



42
43
44
45
# File 'lib/llm/providers/bedrock/stream_decoder.rb', line 42

def <<(chunk)
  @buffer << chunk
  decode_frames
end

#bodyHash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns The fully constructed response body.

Returns:

  • (Hash)

    The fully constructed response body



49
50
51
# File 'lib/llm/providers/bedrock/stream_decoder.rb', line 49

def body
  parser.body
end

#freevoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.



55
56
57
58
# File 'lib/llm/providers/bedrock/stream_decoder.rb', line 55

def free
  @buffer.clear
  parser.free if parser.respond_to?(:free)
end