LlmCall.SSE (llm_call v0.4.0)

Copy Markdown View Source

Server-sent event framing.

The transport hands over body chunks; this module turns them into data payloads. Multiple data: lines in one event are joined with a newline, as the SSE specification requires. event: and comment lines are dropped, because a codec reads the event name out of the payload.

retry: is the server's reconnection hint. It is kept on the state in retry_ms rather than dropped, so a caller that owns reconnection policy can read it; the library never reconnects a stream itself.

A chunk may split a line or an event; the remainder is kept in buffer until the next chunk. flush/1 returns a trailing event that arrived without its terminating blank line, and an empty buffer when the body ended cleanly.

A peer that never sends the blank line ending an event would grow the buffer without bound, so a pending event is capped at @max_buffer bytes. Exceeding it raises LlmCall.SSE.Error rather than truncating, because a truncated event would decode as a shorter payload that still looks valid.

{payloads, state} = SSE.parse(state, chunk)
state.retry_ms

Summary

Functions

Turns a data: payload into the term a codec decodes.

The exception raised for a stream that cannot be framed.

Returns the event left in the buffer at end of body, if any.

Parses one body chunk into the events it completes.

Types

t()

@type t() :: %LlmCall.SSE{
  buffer: String.t(),
  cr_pending?: boolean(),
  retry_ms: non_neg_integer() | nil
}

Functions

codec_input(payload, json)

Turns a data: payload into the term a codec decodes.

It is JSON text, decoded here rather than in LlmCall.Stream so that the stream driver carries no knowledge of any one format. [DONE] is the OpenAI-compatible terminator and is passed through as the string the codecs already match on.

error()

The exception raised for a stream that cannot be framed.

flush(sse)

@spec flush(t()) :: {[String.t()], t()}

Returns the event left in the buffer at end of body, if any.

Providers terminate events with a blank line, so this is normally empty; it exists so a truncated stream does not lose its last event.

parse(state, chunk)

@spec parse(t(), binary()) :: {[String.t()], t()}

Parses one body chunk into the events it completes.