diff --git a/Info.plist b/Info.plist index 220765b..83001df 100644 --- a/Info.plist +++ b/Info.plist @@ -19,7 +19,7 @@ CFBundlePackageType APPL CFBundleShortVersionString - 2.11 + 2.12 CFBundleVersion 1 LSMinimumSystemVersion diff --git a/src/SonioxClient.swift b/src/SonioxClient.swift index a517cad..beab560 100644 --- a/src/SonioxClient.swift +++ b/src/SonioxClient.swift @@ -19,6 +19,9 @@ public final class SonioxLiveSession { private static let punctCharSet = CharacterSet(charactersIn: ".,!?:;،؛؟\"'()[]{}«»-–—") public let targetLanguage: String? + public let languageHints: [String] + private var isFallbackSwitched = false + private var initialAudioBuffer: [Data] = [] public var isTranslationMode: Bool { guard let target = targetLanguage, !target.isEmpty, target != "off" else { return false } return true @@ -124,6 +127,7 @@ public final class SonioxLiveSession { targetLanguage: String? = nil, endpoint: String = SonioxLiveSession.primaryWsBase ) { + self.languageHints = languageHints let effectiveTarget = targetLanguage ?? (SonioxSettings.shared.targetLanguage.isEnabled ? SonioxSettings.shared.targetLanguage.rawValue : nil) self.targetLanguage = effectiveTarget @@ -164,22 +168,94 @@ public final class SonioxLiveSession { if error == nil && !self.isClosed && !self.isAborted { self.isConnected = true self.lastDataTime = Date() + self.initialAudioBuffer.removeAll() self.lock.unlock() self.onConnectionStateChanged?(true) - print("SonioxLiveSession: Connected successfully.") + print("SonioxLiveSession: Connected successfully (\(endpoint)).") } else { + if let error = error { + print("SonioxLiveSession: Ping failed on \(endpoint):", error) + } + if endpoint == SonioxLiveSession.primaryWsBase && !self.isClosed && !self.isAborted && !self.isFallbackSwitched { + self.lock.unlock() + self.switchToFallback() + return + } self.hasNetworkFailure = true self.lock.unlock() self.onNetworkDegraded?(true) - if let error = error { - print("SonioxLiveSession: Ping failed:", error) - } } } startReceiving() } + private func switchToFallback() { + lock.lock() + guard !isFallbackSwitched, !isClosed, !isAborted else { + lock.unlock() + return + } + isFallbackSwitched = true + print("SonioxLiveSession: ⚡️ Primary connection failed. Seamlessly switching to fallback endpoint: \(SonioxLiveSession.fallbackWsBase)") + webSocketTask?.cancel(with: .goingAway, reason: nil) + + let endpoint = SonioxLiveSession.fallbackWsBase + let hints = languageHints.map { "language_hints=\($0)" }.joined(separator: "&") + var queryParams: [String] = [] + if !hints.isEmpty { queryParams.append(hints) } + queryParams.append("enable_speaker_diarization=false") + queryParams.append("enable_language_identification=true") + queryParams.append("enable_endpoint_detection=false") + queryParams.append("providers=soniox") + if let target = targetLanguage, !target.isEmpty, target != "off" { + queryParams.append("target_language=\(target)") + } + let urlStr = "\(endpoint)?" + queryParams.joined(separator: "&") + guard let url = URL(string: urlStr), let session = urlSession else { + hasNetworkFailure = true + lock.unlock() + onNetworkDegraded?(true) + return + } + + var request = URLRequest(url: url) + request.setValue(SonioxLiveSession.userAgent, forHTTPHeaderField: "User-Agent") + request.setValue(SonioxLiveSession.origin(for: endpoint), forHTTPHeaderField: "Origin") + request.timeoutInterval = 10.0 + + let newTask = session.webSocketTask(with: request) + self.webSocketTask = newTask + lock.unlock() + + newTask.resume() + newTask.sendPing { [weak self] err in + guard let self = self else { return } + self.lock.lock() + if err == nil && !self.isClosed && !self.isAborted { + self.isConnected = true + self.hasNetworkFailure = false + self.lastDataTime = Date() + let bufferedChunks = self.initialAudioBuffer + self.initialAudioBuffer.removeAll() + self.lock.unlock() + self.onConnectionStateChanged?(true) + print("SonioxLiveSession: ✅ Fallback endpoint connected! Flushing \(bufferedChunks.count) buffered chunks...") + for chunk in bufferedChunks { + newTask.send(.data(chunk)) { _ in } + } + } else { + self.hasNetworkFailure = true + self.lock.unlock() + self.onNetworkDegraded?(true) + if let err = err { + print("SonioxLiveSession: Fallback ping failed:", err) + } + } + } + startReceiving() + } + /// Called while user is actively speaking to detect half-open TCP silent stalls public func checkSilentStallWhileSpeaking() { lock.lock() @@ -201,7 +277,14 @@ public final class SonioxLiveSession { public func sendAudioChunk(_ data: Data) { lock.lock() - guard !isFinalizing, !isClosed, !isAborted, let task = webSocketTask else { + guard !isFinalizing, !isClosed, !isAborted else { + lock.unlock() + return + } + if !isConnected && initialAudioBuffer.count < 80 { + initialAudioBuffer.append(data) + } + guard let task = webSocketTask else { lock.unlock() return } @@ -214,6 +297,11 @@ public final class SonioxLiveSession { if let error = error { print("SonioxLiveSession: Error streaming audio chunk:", error) self.lock.lock() + if !self.isFallbackSwitched && !self.isFinalizing && !self.isClosed && !self.isAborted { + self.lock.unlock() + self.switchToFallback() + return + } self.hasNetworkFailure = true self.isConnected = false self.lock.unlock() @@ -395,20 +483,26 @@ public final class SonioxLiveSession { } } - // Emit live streaming deltas STRICTLY from Soniox-finalized tokens (is_final == true): + // Check if this message indicates end of session or final delivery + let sessionEnded = json["session_ended"] as? Bool ?? false + let sessionDone = (json["type"] as? String) == "session_done" + let isFinished = json["finished"] as? Bool ?? false + let hasRawFinished: Bool = { + guard let raw = json["raw"] as? String else { return false } + return raw.contains("\"finished\":true") || raw.contains("\"finished\": true") + }() + let isTerminatingMessage = gotFin || sessionEnded || sessionDone || isFinished || hasRawFinished + + // Emit live streaming deltas STRICTLY from Soniox-finalized tokens (is_final == true) + // and ONLY during active speech, NEVER on terminal finalize packets! + // When terminating, the final text is safely delivered to completeWithCurrentText() for full normalization + // and atomic insertion, completely eliminating dual-paste race conditions! var deltaToEmit: String? = nil - if !triggerDetected && fullCommitted.count > streamedPrefix.count { + if !triggerDetected && !isTerminatingMessage && !self.isFinalizing && fullCommitted.count > streamedPrefix.count { if fullCommitted.hasPrefix(streamedPrefix) { let delta = String(fullCommitted.dropFirst(streamedPrefix.count)) streamedPrefix = fullCommitted deltaToEmit = delta - } else { - let common = fullCommitted.commonPrefix(with: streamedPrefix) - if fullCommitted.count > common.count { - let delta = String(fullCommitted.dropFirst(common.count)) - streamedPrefix = fullCommitted - deltaToEmit = delta - } } } lock.unlock() @@ -428,18 +522,21 @@ public final class SonioxLiveSession { self?.onPartialText?(combined) } } - } - - let sessionEnded = json["session_ended"] as? Bool ?? false - let sessionDone = (json["type"] as? String) == "session_done" - let isFinished = json["finished"] as? Bool ?? false - let hasRawFinished: Bool = { - guard let raw = json["raw"] as? String else { return false } - return raw.contains("\"finished\":true") || raw.contains("\"finished\": true") - }() - - if gotFin || sessionEnded || sessionDone || isFinished || hasRawFinished { - completeWithCurrentText() + + if isTerminatingMessage { + completeWithCurrentText() + } + } else { + let sessionEnded = json["session_ended"] as? Bool ?? false + let sessionDone = (json["type"] as? String) == "session_done" + let isFinished = json["finished"] as? Bool ?? false + let hasRawFinished: Bool = { + guard let raw = json["raw"] as? String else { return false } + return raw.contains("\"finished\":true") || raw.contains("\"finished\": true") + }() + if sessionEnded || sessionDone || isFinished || hasRawFinished { + completeWithCurrentText() + } } } @@ -491,6 +588,7 @@ public final class SonioxLiveSession { isConnected = false isRecordingActive = false isFinalizing = false + initialAudioBuffer.removeAll() onPartialText = nil onCommittedDelta = nil onEndTriggerDetected = nil @@ -995,12 +1093,25 @@ public final class SonioxSessionPool { } let age = Date().timeIntervalSince(createdAt) - if let session = session, session.isReady && age < 55.0 { + if let session = session, session.isReady && age < 60.0 { session.stopKeepAliveHeartbeat() print("SonioxSessionPool: Acquired HOT prewarmed session (age: \(String(format: "%.1f", age))s, 0ms connect delay!)") return session } + // If prewarmed session is connecting or slightly aged, wait up to 0.4s for it before aborting + if let session = session, !session.isReady && age < 15.0 { + let tStart = Date() + while Date().timeIntervalSince(tStart) < 0.40 { + if session.isReady { + session.stopKeepAliveHeartbeat() + print("SonioxSessionPool: Acquired session after \(Int(Date().timeIntervalSince(tStart)*1000))ms connection wait.") + return session + } + usleep(25000) // 25ms + } + } + session?.stopKeepAliveHeartbeat() session?.abort()