LiveConnection

An open bidirectional (live) connection to a model, obtained from Model.connect.

A connection outlives a single turn: opened once, carrying many turns, closed when the conversation ends.

Send methods are called by one writer at a time, but closeSession must be safe to call while a send is in flight.

Inheritors

Properties

Link copied to clipboard
open val serverEnding: Job?

Completes when the session ends or the server announces that it will end the session (a go-away), without consuming any response from receive; returns the same job on every read, or null (the default) if the connection cannot tell. PrewarmingLiveModel waits on this job to drop ended spares, and keeps a spare whose connection provides none until the spare is taken or reaches its maximum age. Callers should only observe this job, since canceling it would look like a server end to every observer.

Functions

Link copied to clipboard
open override fun close()

Blocking AutoCloseable bridge to closeSession, for callers outside a coroutine.

Link copied to clipboard
abstract suspend fun closeSession()

Closes the connection; collectors of receive complete normally.

Link copied to clipboard
abstract fun receive(): Flow<LlmResponse>

The model's responses for the current turn, completing when the turn ends; collect it again for the next turn. A collection that completes with no response means the connection has ended. Unlike Model.generateContent, where each collection makes its own request, every collection here reads the same open connection, so only one is allowed at a time: implementations reject a concurrent one with IllegalStateException.

Link copied to clipboard
abstract suspend fun sendContent(content: Content, partial: Boolean = false)

Sends user content, which completes the turn and prompts a response unless partial is true.

Link copied to clipboard
open suspend fun sendContextUpdate(systemInstruction: Content? = null, tools: List<Tool>? = null)

Replaces the session's system instruction or tools for the rest of this connection: a null systemInstruction keeps the current one while one with no parts removes it, and null tools keep the current tools while an empty list removes them. Implementations reject an update that sets neither with IllegalArgumentException; the default throws UnsupportedOperationException. An update can invalidate the model's prefix cache, and a model that does not accept one can end the session, which receive reports.

Link copied to clipboard
abstract suspend fun sendHistory(history: List<Content>)

Seeds the session with prior conversation; Model.connect does not call it, so the caller invokes it at most once, right after connecting and before any other send. The model responds only if the last content is from the user, and otherwise waits for new input.

Link copied to clipboard
abstract suspend fun sendRealtime(input: RealtimeInput)

Sends realtime input: a chunk of media, or a signal marking the edge of user activity.