Session class

A Session represents a connection to a LiveKit Room that can contain an agent.

A session encapsulates the lifecycle of connecting to a room, dispatching an agent, and relaying messages between the user and the agent. It exposes observable state such as the agent's connection status, any session errors, and the ordered history of messages exchanged during the session.

To use a session, provide a token source (fixed or configurable) and call start. When finished, call end to disconnect from the room. Messages can be sent with sendText, and the message history can be inspected or restored via messages, getMessageHistory, and restoreMessageHistory.

Message transport is pluggable: you can provide custom MessageSender and MessageReceiver implementations to integrate with different channels. By default, Session uses:

  • TextMessageSender (topic 'lk.chat') to send user text and emit loopback messages for immediate UI updates.
  • TranscriptionStreamReceiver (topic 'lk.transcription') to receive agent and user transcripts as a message stream.

The session is designed to be observed from Flutter widgets (it extends ChangeNotifier through DisposableChangeNotifier) in the same way that the Swift implementation conforms to ObservableObject.

Inheritance

Constructors

Session.fromConfigurableTokenSource(TokenSourceConfigurable tokenSource, {TokenRequestOptions tokenOptions = const TokenRequestOptions(), SessionOptions? options, List<MessageSender>? senders, List<MessageReceiver>? receivers})
Initializes a new Session with a configurable token source.
factory
Session.fromFixedTokenSource(TokenSourceFixed tokenSource, {SessionOptions? options, List<MessageSender>? senders, List<MessageReceiver>? receivers})
Initializes a new Session with a fixed token source.
factory
Session.withAgent(String agentName, {String? agentMetadata, required TokenSourceConfigurable tokenSource, SessionOptions? options, List<MessageSender>? senders, List<MessageReceiver>? receivers})
Creates a new Session configured for a specific agent.
factory

Properties

agent Agent
no setter
connectionState ConnectionState
no setter
disposeFuncCount int
no setterinherited
error SessionError?
no setter
hashCode int
The hash code for this object.
no setterinherited
hasListeners bool
Whether any listeners are currently registered.
no setterinherited
isConnected bool
no setter
isDisposed bool
no setterinherited
messages List<ReceivedMessage>
no setter
room Room
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

addListener(VoidCallback listener) → void
Register a closure to be called when the object changes.
inherited
dismissError() → void
Clears the last error.
dispose() Future<bool>
Discards any resources used by the object. After this is called, the object is not in a usable state and should be discarded (calls to addListener will throw after the object is disposed).
inherited
end() Future<void>
Terminates the session and disconnects from the room.
getMessageHistory() List<ReceivedMessage>
Returns the message history.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
notifyListeners() → void
Call all the registered listeners.
inherited
onDispose(OnDisposeFunc func) → void
inherited
removeListener(VoidCallback listener) → void
Remove a previously registered closure from the list of closures that are notified when the object changes.
inherited
restoreMessageHistory(List<ReceivedMessage> messages) → void
Restores the message history with the provided messages.
sendText(String text) Future<SentMessage?>
Sends a text message to the agent.
start() Future<void>
Starts the session by fetching credentials and connecting to the room.
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited