A2aSession

One conversation with the A2A agent. A session spans many turns (one user message and the server's streamed reply each) that all share the same contextId.

The context id is issued by the server. A new session sends its first message with an empty context id and adopts the id from the response; a resumed session starts with a known id. The server keeps a conversation for about 30 minutes. If it no longer knows the id it silently starts a new conversation and returns a different id, which this session adopts. Watch contextId to notice that (and to store the id for later resumption within the same process).

Only one turn can run at a time. Create sessions with A2aSessionManager.startSession.

Properties

Link copied to clipboard
Link copied to clipboard
val contextId: StateFlow<String?>

Server-issued conversation id. Null for a new session until its first response arrives; changes if the server starts a new conversation (for example after it expired).

Link copied to clipboard

Id of the task of the running turn; null between turns.

Link copied to clipboard
val errors: SharedFlow<SessionError>

Errors raised by turns and by stop. Not replayed to late collectors.

Link copied to clipboard
val state: StateFlow<SessionState>

Current lifecycle state.

Link copied to clipboard

Functions

Link copied to clipboard

Reports the outcome of a device action to the server, which paused the task after sending it (see A2aEvent.DeviceActionEvent), and streams the events of the resumed task.

Link copied to clipboard
fun sendMessage(text: String, locale: String? = null): Flow<A2aEvent>

Sends text as one turn and streams the server's responses as A2aEvents. Text typed or spoken in another language is translated to English first when SessionConfig.autoTranslate is on; pass the language as locale when it is known (for example the voice recognition locale), or leave it null to let the translator detect it. If translation fails the original text is sent. The returned flow is cold: the turn starts when it is collected.

Link copied to clipboard
suspend fun stop()

Stops the session: ends any running turn, asks the server to cancel the active task (best effort) and rejects further messages. Safe to call more than once.