Gemini.Streaming.ManagerV2 (GeminiEx v0.0.1)

View Source

Improved GenServer for managing streaming connections and state.

Features:

  • Proper HTTP streaming with persistent connections
  • Resource management and cleanup
  • Subscriber pattern with backpressure
  • Error handling and automatic retries
  • Stream lifecycle management

Summary

Functions

Returns a specification to start this module under a supervisor.

Get manager statistics.

Get information about a stream.

List all active streams.

Start the streaming manager.

Stop a streaming session.

Subscribe to events from an existing stream.

Types

manager_state()

@type manager_state() :: %{
  streams: %{required(stream_id()) => stream_state()},
  stream_counter: non_neg_integer(),
  max_streams: pos_integer(),
  default_timeout: pos_integer()
}

stream_id()

@type stream_id() :: String.t()

stream_state()

@type stream_state() :: %{
  stream_id: stream_id(),
  stream_pid: pid() | nil,
  model: String.t(),
  request_body: map(),
  status: :starting | :active | :completed | :error | :stopped,
  error: term() | nil,
  started_at: DateTime.t(),
  subscribers: [subscriber_ref()],
  events_count: non_neg_integer(),
  last_event_at: DateTime.t() | nil,
  config: keyword()
}

subscriber_ref()

@type subscriber_ref() :: {pid(), reference()}

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

get_stats()

@spec get_stats() :: map()

Get manager statistics.

get_stream_info(stream_id)

@spec get_stream_info(stream_id()) :: {:ok, map()} | {:error, term()}

Get information about a stream.

list_streams()

@spec list_streams() :: [stream_id()]

List all active streams.

start_link(opts \\ [])

@spec start_link(keyword()) :: GenServer.on_start()

Start the streaming manager.

start_stream(contents, opts \\ [], subscriber_pid \\ self())

@spec start_stream(term(), keyword(), pid()) :: {:ok, stream_id()} | {:error, term()}

Start a new streaming session.

Parameters

  • contents - Content to stream (string or list of Content structs)
  • opts - Generation options (model, generation_config, etc.)
  • subscriber_pid - Process to receive stream events (default: calling process)

Returns

  • {:ok, stream_id} - Stream started successfully
  • {:error, reason} - Failed to start stream

Events sent to subscriber:

  • {:stream_event, stream_id, event_data} - New event received
  • {:stream_complete, stream_id} - Stream completed successfully
  • {:stream_error, stream_id, error} - Stream failed with error

stop_stream(stream_id)

@spec stop_stream(stream_id()) :: :ok | {:error, term()}

Stop a streaming session.

subscribe_stream(stream_id, subscriber_pid \\ self())

@spec subscribe_stream(stream_id(), pid()) :: :ok | {:error, term()}

Subscribe to events from an existing stream.

unsubscribe_stream(stream_id, subscriber_pid \\ self())

@spec unsubscribe_stream(stream_id(), pid()) :: :ok | {:error, term()}

Unsubscribe from a stream.