Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions Sources/ElevenLabs/Conversation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public final class Conversation: ObservableObject, RoomDelegate {
@Published public private(set) var startupState: ConversationStartupState = .idle
@Published public private(set) var startupMetrics: ConversationStartupMetrics?
@Published public private(set) var messages: [Message] = []
@Published public private(set) var agentState: AgentState = .listening
@Published public private(set) var agentState: ElevenLabs.AgentState = .listening
@Published public private(set) var isMuted: Bool = true // Start as true, will be updated based on actual state

/// Stream of client tool calls that need to be executed by the app
Expand Down Expand Up @@ -51,6 +51,7 @@ public final class Conversation: ObservableObject, RoomDelegate {
private var lastFeedbackSubmittedEventId: Int?
private var previousSpeechActivityHandler: AudioManager.OnSpeechActivity?
private var audioSpeechHandlerInstalled = false
private var shouldAbortRetries = false

// Audio tracks for advanced use cases
public var inputTrack: LocalAudioTrack? {
Expand Down Expand Up @@ -299,6 +300,7 @@ public final class Conversation: ObservableObject, RoomDelegate {

/// End and clean up.
public func endConversation() async {
shouldAbortRetries = true
guard state.isActive else { return }
guard let connectionManager = resolvedConnectionManager() else { return }
await connectionManager.disconnect()
Expand Down Expand Up @@ -887,14 +889,21 @@ public final class Conversation: ObservableObject, RoomDelegate {
? [0]
: options.startupConfiguration.initRetryDelays

self.shouldAbortRetries = false
for (index, delay) in delays.enumerated() {
let attemptNumber = index + 1
metrics.conversationInitAttempts = attemptNumber
updateStartupState(.sendingConversationInit(attempt: attemptNumber))

if delay > 0 {
print("[Retry] Attempt \(attemptNumber) delay: \(delay)s")
try? await Task.sleep(nanoseconds: UInt64(delay * 1_000_000_000))
try await Task.sleep(nanoseconds: UInt64(delay * 1_000_000_000))
try Task.checkCancellation()
}

if shouldAbortRetries {
print("[Retry] Aborting retry loop after sleep")
throw CancellationError()
}

let attemptStart = Date()
Expand Down
9 changes: 7 additions & 2 deletions Sources/ElevenLabs/Models/LiveKitNetworkConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ public struct LiveKitNetworkConfiguration: Sendable {
/// Optional custom ICE servers to use instead of those supplied by the ElevenLabs backend.
public var customIceServers: [IceServer]

public init(strategy: Strategy = .automatic, customIceServers: [IceServer] = []) {
/// Optional override for LiveKit's reconnectAttempts connection option
public var reconnectAttempts: Int? = nil

public init(strategy: Strategy = .automatic, customIceServers: [IceServer] = [], reconnectAttempts: Int? = nil) {
self.strategy = strategy
self.customIceServers = customIceServers
self.reconnectAttempts = reconnectAttempts
}

/// Default configuration (automatic ICE candidate gathering, no custom ICE servers).
Expand All @@ -46,7 +50,7 @@ extension LiveKitNetworkConfiguration {
}

var requiresCustomConnectOptions: Bool {
strategy != .automatic || !customIceServers.isEmpty
strategy != .automatic || !customIceServers.isEmpty || reconnectAttempts != nil
}

func makeConnectOptions() -> ConnectOptions? {
Expand All @@ -62,6 +66,7 @@ extension LiveKitNetworkConfiguration {
#endif

return ConnectOptions(
reconnectAttempts: reconnectAttempts ?? ConnectOptions().reconnectAttempts,
iceServers: customIceServers,
iceTransportPolicy: policy
)
Expand Down
Loading