Alloy.Provider.SSE (alloy v0.10.1)

Copy Markdown View Source

Shared SSE (Server-Sent Events) framing utilities.

Handles the transport-level concerns that are identical across all providers using SSE streaming: byte buffering, event boundary splitting, and field extraction.

Provider-specific event handling is NOT in this module — each provider (or shared parser like Alloy.Provider.OpenAIStream) pattern-matches on the parsed events returned here.

Summary

Functions

Process a raw chunk of bytes against a buffer.

Build a Req into: stream handler that accumulates SSE events.

Types

sse_event()

@type sse_event() :: %{event: String.t() | nil, data: String.t()}

Functions

process_chunk(buffer, chunk)

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

Process a raw chunk of bytes against a buffer.

Returns {complete_events, remaining_buffer} where each event is a map with :event (may be nil) and :data keys.

req_stream_handler(initial_acc, handle_event)

@spec req_stream_handler(map(), (map(), sse_event() -> map())) :: ({:data, String.t()},
                                                             {term(), term()} ->
                                                               {:cont,
                                                                {term(), term()}})

Build a Req into: stream handler that accumulates SSE events.

The handle_event function receives (accumulator, sse_event) and returns the updated accumulator. The accumulator must contain a :buffer key (string) for SSE framing state.

The accumulator is stored in resp.private.sse_acc.