Gemini.SSE.Parser (GeminiEx v0.0.1)

View Source

Server-Sent Events (SSE) parser for streaming responses.

Handles partial chunks and maintains state across multiple calls. Properly parses SSE format with incremental data.

Summary

Functions

Extract text content from a streaming event.

Finalize parsing and return any remaining events in buffer.

Create a new SSE parser state.

Parse incoming SSE chunk and return events + updated state.

Check if an event indicates the stream is done.

Types

parse_result()

@type parse_result() :: {:ok, [map()], t()} | {:error, term()}

t()

@type t() :: %Gemini.SSE.Parser{buffer: String.t(), events: [map()]}

Functions

extract_text(arg1)

@spec extract_text(map()) :: String.t() | nil

Extract text content from a streaming event.

finalize(parser)

@spec finalize(t()) :: {:ok, [map()]}

Finalize parsing and return any remaining events in buffer.

Call this when the stream is complete to get any final partial events.

new()

@spec new() :: t()

Create a new SSE parser state.

parse_chunk(chunk, state)

@spec parse_chunk(String.t(), t()) :: parse_result()

Parse incoming SSE chunk and return events + updated state.

Examples

iex> parser = SSE.Parser.new()
iex> chunk = "data: {\"text\": \"hello\"}

"

iex> {:ok, events, new_parser} = SSE.Parser.parse_chunk(chunk, parser)
iex> length(events)
1

stream_done?(arg1)

@spec stream_done?(map()) :: boolean()

Check if an event indicates the stream is done.