OpenrouterSdk.SSE (OpenRouter SDK v0.1.0)

Copy Markdown View Source

incremental server-sent-events parser.

pure functional: feed(state, bytes) -> {events, new_state}. the caller pumps bytes in (typically arriving from finch), and gets back whatever fully-formed events the parser could complete from the combined buffer. partial lines stay in state.buffer.

understands:

  • data:, event:, id:, retry:
  • comment lines starting with :
  • crlf and lf line endings
  • multi-line data: (joined with \n)
  • the openai-style [DONE] terminator (emitted as :done)

Summary

Functions

feed bytes into the parser. returns {events, new_state} where events is the (possibly empty) list of newly completed events in arrival order.

fresh parser state

Types

emitted()

@type emitted() :: OpenrouterSdk.SSE.Event.t() | :done

state()

@type state() :: %{
  buffer: binary(),
  event: String.t() | nil,
  id: String.t() | nil,
  data: [String.t()],
  retry: non_neg_integer() | nil
}

Functions

feed(state, bytes)

@spec feed(state(), binary()) :: {[emitted()], state()}

feed bytes into the parser. returns {events, new_state} where events is the (possibly empty) list of newly completed events in arrival order.

init()

@spec init() :: state()

fresh parser state