# `LlmCall.EventStream`
[🔗](https://github.com/gilbertwong96/llm_call/blob/main/lib/llm_call/event_stream.ex#L1)

`application/vnd.amazon.eventstream` framing.

Bedrock's `ConverseStream` does not answer with server-sent events: the body is a
sequence of binary frames, each carrying its own length, headers and CRC-32. This
module turns body chunks into `LlmCall.EventStream.Message` structs; it does not
know what the payload means and does not decode JSON.

## Frame layout

    +---------------------------------+---------------------------------+
    | total length     (4, big endian)| headers length   (4, big endian)|
    +---------------------------------+---------------------------------+
    | prelude CRC-32   (4)            |  CRC of the eight bytes above   |
    +-------------------------------------------------------------------+
    | headers          (headers length bytes)                           |
    +-------------------------------------------------------------------+
    | payload          (total - 16 - headers length bytes)              |
    +-------------------------------------------------------------------+
    | message CRC-32   (4)            |  CRC of every byte above        |
    +-------------------------------------------------------------------+

Both checksums are CRC-32 as `:erlang.crc32/1` computes it — the same polynomial
as zlib, not CRC-32C. They are the reason this format is safe to parse
incrementally: a corrupted length would otherwise be used as an offset into the
next frame, and a corrupted payload would decode as a shorter message that still
looks valid. The prelude is checked **before** its length fields are used.

## Headers

Each header is a one-byte name length, the name, a one-byte value type, then the
value: `0`/`1` boolean, `2` byte, `3` short, `4` integer, `5` long, `6` byte array,
`7` string, `8` timestamp, `9` UUID. See `LlmCall.EventStream.Message` for how each
becomes an Elixir value. An unknown type is an error rather than a skipped field,
because skipping it would misplace the next header.

## Buffering

A chunk may split a frame at any byte; the remainder stays in `buffer` until the
next chunk. A frame is capped at 16 MiB, and the cap is applied to the length the
frame *claims*, before the bytes arrive, so a peer cannot make this process buffer
gigabytes by announcing them.

`flush/1` refuses a buffer that still holds bytes: a body that ends mid-frame is a
truncated stream, not a complete one. That is the same rule §12 applies to SSE.

    {messages, state} = EventStream.parse(EventStream.new(), chunk)
    {[], state} = EventStream.flush(state)

# `t`

```elixir
@type t() :: %LlmCall.EventStream{buffer: binary()}
```

# `codec_input`

The framed message is the codec's input, payload bytes included.

Unlike SSE there is no bare JSON to decode: the payload sits inside an envelope
whose headers say what it is, so decoding it is the codec's decision.

# `error`

The exception raised for a body that cannot be framed.

# `flush`

```elixir
@spec flush(t()) :: {[], t()}
```

Asserts that the body ended on a frame boundary.

There is no trailing message to return, unlike SSE: event stream has no way to
carry a fragment of one. A body that stops mid-frame lost the rest of a message,
which is a truncated stream rather than a shorter one.

# `new`

```elixir
@spec new() :: t()
```

A decoder with an empty buffer.

# `parse`

```elixir
@spec parse(t(), binary()) :: {[LlmCall.EventStream.Message.t()], t()}
```

Appends a body chunk and returns the messages it completes.

Raises `LlmCall.EventStream.Error` when the bytes cannot be a frame: a bad
checksum, an impossible length, an unknown header type, or a header that runs past
its block. A chunk that ends inside a frame is not an error — the frame completes on
a later chunk.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
