@@ -18,6 +18,24 @@ import Combine
1818import Foundation
1919import OrderedCollections
2020
21+ /// A ``Session`` represents a connection to a LiveKit Room that can contain an ``Agent``.
22+ ///
23+ /// ``Session`` is the main entry point for interacting with a LiveKit agent. It encapsulates
24+ /// the connection to a LiveKit ``Room``, manages the agent's lifecycle, and handles
25+ /// communication between the user and the agent.
26+ ///
27+ /// ``Session`` is created with a token source and optional configuration. The ``start()``
28+ /// method establishes the connection, and the ``end()`` method terminates it. The session's
29+ /// state, including connection status and any errors, is published for observation,
30+ /// making it suitable for use in SwiftUI applications.
31+ ///
32+ /// Communication with the agent is handled through messages. The ``send(text:)`` method
33+ /// sends a user message, and the ``messages`` property provides an ordered history of the
34+ /// conversation. The session can be configured with custom message senders and receivers
35+ /// to support different communication channels, such as text messages or transcription streams.
36+ ///
37+ /// - SeeAlso: [LiveKit SwiftUI Agent Starter](https://github.com/livekit-examples/agent-starter-swift).
38+ /// - SeeAlso: [LiveKit Agents documentation](https://docs.livekit.io/agents/).
2139@MainActor
2240open class Session : ObservableObject {
2341 private static let agentNameAttribute = " lk.agent_name "
@@ -40,9 +58,12 @@ open class Session: ObservableObject {
4058
4159 // MARK: - Published
4260
61+ /// The last error that occurred.
4362 @Published public private( set) var error : Error ?
4463
64+ /// The current connection state of the session.
4565 @Published public private( set) var connectionState : ConnectionState = . disconnected
66+ /// A boolean value indicating whether the session is connected.
4667 public var isConnected : Bool {
4768 switch connectionState {
4869 case . connecting, . connected:
@@ -52,13 +73,16 @@ open class Session: ObservableObject {
5273 }
5374 }
5475
76+ /// The ``Agent`` associated with this session.
5577 @Published public private( set) var agent = Agent ( )
5678
5779 @Published private var messagesDict : OrderedDictionary < ReceivedMessage . ID , ReceivedMessage > = [ : ]
80+ /// The ordered list of received messages.
5881 public var messages : [ ReceivedMessage ] { messagesDict. values. elements }
5982
6083 // MARK: - Dependencies
6184
85+ /// The underlying ``Room`` object for the session.
6286 public let room : Room
6387
6488 private enum TokenSourceConfiguration {
@@ -98,6 +122,12 @@ open class Session: ObservableObject {
98122 observe ( receivers: resolvedReceivers)
99123 }
100124
125+ /// Initializes a new ``Session`` with a fixed token source.
126+ /// - Parameters:
127+ /// - tokenSource: A token source that provides a fixed token.
128+ /// - options: The session options.
129+ /// - senders: An array of message senders.
130+ /// - receivers: An array of message receivers.
101131 public convenience init ( tokenSource: any TokenSourceFixed ,
102132 options: SessionOptions = . init( ) ,
103133 senders: [ any MessageSender ] ? = nil ,
@@ -109,6 +139,13 @@ open class Session: ObservableObject {
109139 receivers: receivers)
110140 }
111141
142+ /// Initializes a new ``Session`` with a configurable token source.
143+ /// - Parameters:
144+ /// - tokenSource: A token source that can generate tokens with specific options.
145+ /// - tokenOptions: The options for generating the token.
146+ /// - options: The session options.
147+ /// - senders: An array of message senders.
148+ /// - receivers: An array of message receivers.
112149 public convenience init ( tokenSource: any TokenSourceConfigurable ,
113150 tokenOptions: TokenRequestOptions = . init( ) ,
114151 options: SessionOptions = . init( ) ,
@@ -121,6 +158,15 @@ open class Session: ObservableObject {
121158 receivers: receivers)
122159 }
123160
161+ /// Creates a new ``Session`` configured for a specific agent.
162+ /// - Parameters:
163+ /// - agentName: The name of the agent to dispatch.
164+ /// - agentMetadata: Metadata passed to the agent.
165+ /// - tokenSource: A configurable token source.
166+ /// - options: The session options.
167+ /// - senders: An array of message senders.
168+ /// - receivers: An array of message receivers.
169+ /// - Returns: A new ``Session`` instance.
124170 public static func withAgent( _ agentName: String ,
125171 agentMetadata: String ? = nil ,
126172 tokenSource: any TokenSourceConfigurable ,
@@ -173,6 +219,7 @@ open class Session: ObservableObject {
173219
174220 // MARK: - Lifecycle
175221
222+ /// Starts the session.
176223 public func start( ) async {
177224 guard connectionState == . disconnected else { return }
178225
@@ -210,16 +257,21 @@ open class Session: ObservableObject {
210257 }
211258 }
212259
260+ /// Terminates the session.
213261 public func end( ) async {
214262 await room. disconnect ( )
215263 }
216264
265+ /// Resets the last error.
217266 public func resetError( ) {
218267 error = nil
219268 }
220269
221270 // MARK: - Messages
222271
272+ /// Sends a text message.
273+ /// - Parameter text: The text to send.
274+ /// - Returns: The ``SentMessage`` that was sent.
223275 @discardableResult
224276 public func send( text: String ) async -> SentMessage {
225277 let message = SentMessage ( id: UUID ( ) . uuidString, timestamp: Date ( ) , content: . userInput( text) )
@@ -233,10 +285,14 @@ open class Session: ObservableObject {
233285 return message
234286 }
235287
288+ /// Gets the message history.
289+ /// - Returns: An array of ``ReceivedMessage``.
236290 public func getMessageHistory( ) -> [ ReceivedMessage ] {
237291 messages
238292 }
239293
294+ /// Restores the message history.
295+ /// - Parameter messages: An array of ``ReceivedMessage`` to restore.
240296 public func restoreMessageHistory( _ messages: [ ReceivedMessage ] ) {
241297 messagesDict = . init( uniqueKeysWithValues: messages. sorted ( by: { $0. timestamp < $1. timestamp } ) . map { ( $0. id, $0) } )
242298 }
0 commit comments