@@ -44,6 +44,12 @@ final class Conversation: ObservableObject {
4444 let agentObserverRegistry = AudioObserverRegistry ( )
4545 let micObserverRegistry = AudioObserverRegistry ( )
4646
47+ private let dependencyProvider : any ConversationDependencyProvider
48+ private var activeConnectionManager : ( any ConnectionManaging ) ?
49+ private var activeWebRTCConnectionManager : ( any WebRTCConnectionManaging ) ? {
50+ activeConnectionManager as? any WebRTCConnectionManaging
51+ }
52+
4753 /// Internal LiveKit tracks used to attach ``ConversationAudioObserver``s.
4854 private var inputTrack : ( any AudioTrackProtocol ) ? {
4955 activeWebRTCConnectionManager? . inputTrack
@@ -281,47 +287,6 @@ final class Conversation: ObservableObject {
281287
282288 // MARK: - Private
283289
284- private let dependencyProvider : any ConversationDependencyProvider
285- private var activeConnectionManager : ( any ConnectionManaging ) ?
286- private var activeWebRTCConnectionManager : ( any WebRTCConnectionManaging ) ? {
287- activeConnectionManager as? any WebRTCConnectionManaging
288- }
289-
290- private func updateStartupStage( _ stage: ConversationStartupState ) {
291- guard state. isConnecting, state != . connecting( stage) else { return }
292- state = . connecting( stage)
293- }
294-
295- private func setupAgentStateManager( ) {
296- guard let configuration = config. agentStateConfiguration else { return }
297- let manager = AgentStateManager ( configuration: configuration)
298- manager. onStateChange = { [ weak self] state in
299- self ? . agentState = state
300- self ? . callbacks. onAgentStateChange ? ( state)
301- }
302- agentStateManager = manager
303- }
304-
305- /// Forward a signal to the event-based state manager, or fall back to directly setting `agentState`.
306- private func applyStateSignal( _ signal: AgentStateSignal , fallback: AgentState ) {
307- if let manager = agentStateManager {
308- manager. processSignal ( signal)
309- } else {
310- agentState = fallback
311- }
312- }
313-
314- private func handleRemoteSpeakingUpdate( isSpeaking: Bool ) {
315- if let manager = agentStateManager {
316- manager. processSignal ( isSpeaking ? . agentStartedSpeaking : . agentStoppedSpeaking)
317- } else if isSpeaking {
318- speakingTimer? . cancel ( )
319- agentState = . speaking
320- } else {
321- scheduleBackToListening ( delay: 1.0 )
322- }
323- }
324-
325290 private func start(
326291 agentId: String ,
327292 isTextOnly: Bool ,
@@ -379,6 +344,41 @@ final class Conversation: ObservableObject {
379344 return result
380345 }
381346
347+ private func updateStartupStage( _ stage: ConversationStartupState ) {
348+ guard state. isConnecting, state != . connecting( stage) else { return }
349+ state = . connecting( stage)
350+ }
351+
352+ private func setupAgentStateManager( ) {
353+ guard let configuration = config. agentStateConfiguration else { return }
354+ let manager = AgentStateManager ( configuration: configuration)
355+ manager. onStateChange = { [ weak self] state in
356+ self ? . agentState = state
357+ self ? . callbacks. onAgentStateChange ? ( state)
358+ }
359+ agentStateManager = manager
360+ }
361+
362+ /// Forward a signal to the event-based state manager, or fall back to directly setting `agentState`.
363+ private func applyStateSignal( _ signal: AgentStateSignal , fallback: AgentState ) {
364+ if let manager = agentStateManager {
365+ manager. processSignal ( signal)
366+ } else {
367+ agentState = fallback
368+ }
369+ }
370+
371+ private func handleRemoteSpeakingUpdate( isSpeaking: Bool ) {
372+ if let manager = agentStateManager {
373+ manager. processSignal ( isSpeaking ? . agentStartedSpeaking : . agentStoppedSpeaking)
374+ } else if isSpeaking {
375+ speakingTimer? . cancel ( )
376+ agentState = . speaking
377+ } else {
378+ scheduleBackToListening ( delay: 1.0 )
379+ }
380+ }
381+
382382 private func handleIncomingEvent( _ event: IncomingEvent , from connectionManagerID: ObjectIdentifier ) async {
383383 guard let activeConnectionManager,
384384 ObjectIdentifier ( activeConnectionManager) == connectionManagerID,
0 commit comments