وویس تایپینگ سانی اوکس مک
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

1650 lines
71 KiB

import Foundation
import AppKit
public final class SonioxLiveSession {
public static let primaryWsBase = "wss://stt.newhorizonco.uk/ws/live-stt"
public static let fallbackWsBase = "wss://stt.newhorizonco.uk/ws/live-translate"
private static let apiKey = "nwh_live_Y54Zfil6MdYZEOu4IHlBzTWhGputnVbJ"
private static let userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
public static func origin(for endpoint: String) -> String {
return "https://stt.newhorizonco.uk"
}
private static func baseQuery(withHints hints: [String], targetLanguage: String?) -> String {
var parts: [String] = []
let hintStr = hints.map { "language_hints=\($0)" }.joined(separator: "&")
if !hintStr.isEmpty { parts.append(hintStr) }
parts.append("key=\(apiKey)")
if let target = targetLanguage, !target.isEmpty, target != "off" {
parts.append("target_language=\(target)")
}
return parts.joined(separator: "&")
}
// Pre-compiled static regexes for zero-allocation hot path
private static let faRegex: NSRegularExpression = {
return try! NSRegularExpression(pattern: "[\\u0600-\\u06FF\\u0750-\\u077F\\uFB50-\\uFDFF\\uFE70-\\uFEFF]", options: [])
}()
private static let enRegex: NSRegularExpression = {
return try! NSRegularExpression(pattern: "[a-zA-Z]", options: [])
}()
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
}
private var webSocketTask: URLSessionWebSocketTask?
private var urlSession: URLSession?
private var isFinalizing = false
private var didReceiveTermination = false
private var isClosed = false
private var isConnected = false
private var isAborted = false
private let lock = NSLock()
// Transcripts & Stall tracking (Multi-segment continuous speech + checkpointed audio buffer)
private var archivedCommittedText: String = ""
private var committedFinalTokens: [String] = []
private var currentNonFinalTokens: [String] = []
private var streamedPrefix: String = ""
private var currentSegmentAudioBuffer = Data()
private var lastConfirmedWordAudioByteOffset: Int = 0
private var isReconnectingMidRecording = false
private var isFlushingReplayBuffer = false
private var pendingFinalizeAfterFlush = false
private var segmentConnectTime: Date = Date()
private var sentAudioBytes: Int = 0
public private(set) var totalReceivedTokensCount: Int = 0
public private(set) var hasSilentStall: Bool = false
// Callbacks
public var onPartialText: ((String) -> Void)?
public var onCommittedDelta: ((String) -> Void)?
public var onLiveTranscriptUpdate: ((String, String) -> Void)?
public var onEndTriggerDetected: (() -> Void)?
public var onFinalResult: ((Result<String, Error>) -> Void)?
public var onConnectionStateChanged: ((Bool) -> Void)?
public var onNetworkDegraded: ((Bool) -> Void)?
public private(set) var hasNetworkFailure: Bool = false
public private(set) var isRecordingActive: Bool = false
private var lastDataTime: Date = Date()
private var heartbeatTimer: Timer?
public func setRecordingActive(_ active: Bool) {
lock.lock()
isRecordingActive = active
if active {
hasSilentStall = false
lastDataTime = Date()
segmentConnectTime = Date()
}
lock.unlock()
}
/// True when a mid-recording disconnect/stall occurred and there is >0.35s of unconfirmed tail audio waiting to be transcribed.
public var hasUntranscribedTailAudio: Bool {
lock.lock()
defer { lock.unlock() }
let tailCount = max(0, currentSegmentAudioBuffer.count - lastConfirmedWordAudioByteOffset)
let hadDrop = hasNetworkFailure || hasSilentStall || isReconnectingMidRecording || !isConnected
return hadDrop && tailCount > 11200
}
/// Returns the already-confirmed transcript prefix across all completed segments and finalized tokens.
public func confirmedTranscriptPrefix() -> String {
lock.lock()
let segCommitted = committedFinalTokens.joined().trimmingCharacters(in: .whitespacesAndNewlines)
let rawPrefix: String
if archivedCommittedText.isEmpty {
rawPrefix = segCommitted
} else if segCommitted.isEmpty {
rawPrefix = archivedCommittedText
} else {
rawPrefix = archivedCommittedText + " " + segCommitted
}
lock.unlock()
return SonioxLiveSession.sanitizeText(rawPrefix)
}
/// Returns the slice of raw PCM audio from `lastConfirmedWordAudioByteOffset` to the end of `currentSegmentAudioBuffer`.
public func untranscribedTailAudioData() -> Data {
lock.lock()
defer { lock.unlock() }
guard currentSegmentAudioBuffer.count > lastConfirmedWordAudioByteOffset else {
return Data()
}
return currentSegmentAudioBuffer.subdata(in: lastConfirmedWordAudioByteOffset..<currentSegmentAudioBuffer.count)
}
public var isReady: Bool {
lock.lock()
defer { lock.unlock() }
return isConnected && !isClosed && !isFinalizing && !isAborted
}
public var isAlive: Bool {
lock.lock()
defer { lock.unlock() }
return !isClosed && !isAborted && !hasNetworkFailure
}
public func startKeepAliveHeartbeat() {
DispatchQueue.main.async { [weak self] in
guard let self = self else { return }
self.heartbeatTimer?.invalidate()
self.heartbeatTimer = Timer.scheduledTimer(withTimeInterval: 18.0, repeats: true) { [weak self] _ in
guard let self = self else { return }
self.lock.lock()
guard !self.isClosed, !self.isFinalizing, !self.isAborted, let task = self.webSocketTask else {
self.lock.unlock()
return
}
self.lock.unlock()
let t0 = Date()
task.sendPing { [weak self] error in
guard let self = self else { return }
if let error = error {
print("SonioxLiveSession: Heartbeat ping failed (\(error)). Marking stale.")
self.lock.lock()
self.isConnected = false
self.hasNetworkFailure = true
self.isClosed = true
self.lock.unlock()
SonioxSessionPool.shared.prewarmNextSession()
} else {
let rtt = Date().timeIntervalSince(t0)
if rtt > 2.5 {
self.lock.lock()
self.hasSilentStall = true
self.lock.unlock()
self.onNetworkDegraded?(true)
}
}
}
}
}
}
public func stopKeepAliveHeartbeat() {
DispatchQueue.main.async { [weak self] in
self?.heartbeatTimer?.invalidate()
self?.heartbeatTimer = nil
}
}
public init(
languageHints: [String] = ["fa", "en", "ar"],
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
let queryStr = SonioxLiveSession.baseQuery(withHints: languageHints, targetLanguage: effectiveTarget)
let urlStr = "\(endpoint)?\(queryStr)"
guard let url = URL(string: urlStr) else { return }
var request = URLRequest(url: url)
request.setValue(SonioxLiveSession.userAgent, forHTTPHeaderField: "User-Agent")
request.setValue(SonioxLiveSession.origin(for: endpoint), forHTTPHeaderField: "Origin")
request.timeoutInterval = 12.0
let config = URLSessionConfiguration.default
config.waitsForConnectivity = false // Fail fast on dead interface so recovery/failover triggers immediately
config.requestCachePolicy = .reloadIgnoringLocalCacheData
let session = URLSession(configuration: config)
self.urlSession = session
let task = session.webSocketTask(with: request)
self.webSocketTask = task
task.resume()
// Ping to verify connection & latency
task.sendPing { [weak self] error in
guard let self = self else { return }
self.lock.lock()
if error == nil && !self.isClosed && !self.isAborted {
self.isConnected = true
self.lastDataTime = Date()
self.initialAudioBuffer.removeAll()
let hasBuffered = !self.currentSegmentAudioBuffer.isEmpty
if hasBuffered {
self.isFlushingReplayBuffer = true
}
let bufferedBytes = self.currentSegmentAudioBuffer.count
self.lock.unlock()
self.onConnectionStateChanged?(true)
print("SonioxLiveSession: Connected successfully (\(endpoint)). Flushing \(bufferedBytes) bytes of initial audio...")
if hasBuffered {
self.flushReplayBufferToSocket(task: task, offset: 0)
}
} 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)
}
}
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 queryStr = SonioxLiveSession.baseQuery(withHints: languageHints, targetLanguage: targetLanguage)
let urlStr = "\(endpoint)?\(queryStr)"
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()
self.initialAudioBuffer.removeAll()
let hasBuffered = !self.currentSegmentAudioBuffer.isEmpty
if hasBuffered {
self.isFlushingReplayBuffer = true
}
let bufferedBytes = self.currentSegmentAudioBuffer.count
self.lock.unlock()
self.onConnectionStateChanged?(true)
print("SonioxLiveSession: ✅ Fallback endpoint connected! Flushing \(bufferedBytes) bytes of buffered speech...")
if hasBuffered {
self.flushReplayBufferToSocket(task: newTask, offset: 0)
}
} 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 and auto-reconnect seamlessly
public func checkSilentStallWhileSpeaking() {
lock.lock()
let connected = isConnected && !isClosed && !isAborted
let activeRec = isRecordingActive && !isFinalizing && !isReconnectingMidRecording
let sent = sentAudioBytes
let elapsedSinceData = Date().timeIntervalSince(lastDataTime)
lock.unlock()
// If >1.0s of audio (32KB) was sent while speaking, but 0 frames arrived for >3.5s:
if connected && sent > 32000 && elapsedSinceData > 3.5 {
lock.lock()
hasSilentStall = true
lock.unlock()
DispatchQueue.main.async { [weak self] in
self?.onNetworkDegraded?(true)
}
if activeRec {
triggerMidRecordingReconnect(cleanArchive: false, reason: "Silent TCP/STT stall (\(String(format: "%.1f", elapsedSinceData))s without frames while speaking)")
}
}
}
/// Seamlessly archives confirmed transcript tokens and reconnects a fresh WebSocket in the background
/// while the user continues holding Push-To-Talk / speaking for 5–10+ minutes!
public func triggerMidRecordingReconnect(cleanArchive: Bool, reason: String) {
lock.lock()
guard isRecordingActive, !isFinalizing, !isClosed, !isAborted, !isReconnectingMidRecording else {
lock.unlock()
return
}
isReconnectingMidRecording = true
isConnected = false
let segCommitted = committedFinalTokens.joined()
let segNonFinal = currentNonFinalTokens.joined()
let toArchive = cleanArchive ? (segCommitted + segNonFinal) : segCommitted
let cleanToArchive = toArchive.trimmingCharacters(in: .whitespacesAndNewlines)
if !cleanToArchive.isEmpty {
if archivedCommittedText.isEmpty {
archivedCommittedText = cleanToArchive
} else {
archivedCommittedText += " " + cleanToArchive
}
}
committedFinalTokens.removeAll()
currentNonFinalTokens.removeAll()
if cleanArchive {
currentSegmentAudioBuffer.removeAll(keepingCapacity: true)
lastConfirmedWordAudioByteOffset = 0
} else if lastConfirmedWordAudioByteOffset > 0 && lastConfirmedWordAudioByteOffset <= currentSegmentAudioBuffer.count {
currentSegmentAudioBuffer.removeSubrange(0..<lastConfirmedWordAudioByteOffset)
lastConfirmedWordAudioByteOffset = 0
}
if !archivedCommittedText.isEmpty {
streamedPrefix = archivedCommittedText + " "
}
let bufferedBytes = currentSegmentAudioBuffer.count
let archivedCount = archivedCommittedText.count
let oldTask = webSocketTask
let oldSession = urlSession
webSocketTask = nil
urlSession = nil
lock.unlock()
print("SonioxLiveSession: 🔄 Mid-recording auto-reconnect triggered (\(reason)). Archived \(archivedCount) chars, buffering \(bufferedBytes) bytes of unconfirmed audio...")
oldTask?.cancel(with: .goingAway, reason: nil)
oldSession?.invalidateAndCancel()
attemptMidRecordingConnect(attempt: 1)
}
private func attemptMidRecordingConnect(attempt: Int) {
lock.lock()
guard isRecordingActive, !isFinalizing, !isClosed, !isAborted else {
isReconnectingMidRecording = false
lock.unlock()
return
}
let endpoint = (attempt % 2 == 1) ? SonioxLiveSession.primaryWsBase : SonioxLiveSession.fallbackWsBase
let queryStr = SonioxLiveSession.baseQuery(withHints: languageHints, targetLanguage: targetLanguage)
let urlStr = "\(endpoint)?\(queryStr)"
guard let url = URL(string: urlStr) else {
isReconnectingMidRecording = false
lock.unlock()
return
}
var request = URLRequest(url: url)
request.setValue(SonioxLiveSession.userAgent, forHTTPHeaderField: "User-Agent")
request.setValue(SonioxLiveSession.origin(for: endpoint), forHTTPHeaderField: "Origin")
request.timeoutInterval = 6.0
let config = URLSessionConfiguration.default
config.waitsForConnectivity = false
config.requestCachePolicy = .reloadIgnoringLocalCacheData
let newSession = URLSession(configuration: config)
let newTask = newSession.webSocketTask(with: request)
self.urlSession = newSession
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.isFinalizing {
self.isConnected = true
self.hasNetworkFailure = false
self.hasSilentStall = false
self.isReconnectingMidRecording = false
self.isFlushingReplayBuffer = true
self.lastDataTime = Date()
self.segmentConnectTime = Date()
self.sentAudioBytes = 0
let replaySnapshot = self.currentSegmentAudioBuffer
self.lock.unlock()
self.onNetworkDegraded?(false)
self.onConnectionStateChanged?(true)
print("SonioxLiveSession: ✅ Mid-recording reconnect succeeded on attempt \(attempt) (\(endpoint))! Replaying \(replaySnapshot.count) bytes of buffered speech...")
self.startReceiving()
self.flushReplayBufferToSocket(task: newTask, offset: 0)
} else {
let shouldRetry = self.isRecordingActive && !self.isFinalizing && !self.isClosed && !self.isAborted
self.webSocketTask = nil
self.urlSession = nil
self.lock.unlock()
newTask.cancel()
newSession.invalidateAndCancel()
if shouldRetry {
let delay = min(1.5, 0.5 + Double(min(attempt, 4)) * 0.25)
DispatchQueue.global(qos: .userInitiated).asyncAfter(deadline: .now() + delay) { [weak self] in
self?.attemptMidRecordingConnect(attempt: attempt + 1)
}
}
}
}
}
private func flushReplayBufferToSocket(task: URLSessionWebSocketTask, offset: Int) {
lock.lock()
guard isConnected, !isClosed, !isAborted, (!isFinalizing || pendingFinalizeAfterFlush), webSocketTask === task else {
isFlushingReplayBuffer = false
pendingFinalizeAfterFlush = false
lock.unlock()
return
}
let total = currentSegmentAudioBuffer.count
if offset >= total {
isFlushingReplayBuffer = false
let shouldSendFinalizeNow = pendingFinalizeAfterFlush
pendingFinalizeAfterFlush = false
lock.unlock()
if shouldSendFinalizeNow {
sendFinalizeFramesOnTask(task)
}
return
}
let chunkSize = 32000 // 1.0s chunk
let end = min(offset + chunkSize, total)
let slice = currentSegmentAudioBuffer.subdata(in: offset..<end)
lock.unlock()
task.send(.data(slice)) { [weak self] err in
guard let self = self else { return }
if let err = err {
print("SonioxLiveSession: Error flushing replay buffer:", err)
self.lock.lock()
self.isFlushingReplayBuffer = false
self.hasNetworkFailure = true
self.isConnected = false
let activeRec = self.isRecordingActive && !self.isFinalizing && !self.isClosed && !self.isAborted
self.lock.unlock()
if activeRec {
self.triggerMidRecordingReconnect(cleanArchive: false, reason: "Replay flush send error")
}
return
}
self.lock.lock()
self.sentAudioBytes += slice.count
self.lock.unlock()
self.flushReplayBufferToSocket(task: task, offset: end)
}
}
public func sendAudioChunk(_ data: Data) {
lock.lock()
guard !isFinalizing, !isClosed, !isAborted else {
lock.unlock()
return
}
currentSegmentAudioBuffer.append(data)
if !isConnected {
if initialAudioBuffer.count < 120 {
initialAudioBuffer.append(data)
}
let shouldReconnect = isRecordingActive && hasNetworkFailure && !isReconnectingMidRecording
lock.unlock()
if shouldReconnect {
triggerMidRecordingReconnect(cleanArchive: false, reason: "Audio chunk arrived while disconnected")
}
return
}
if isReconnectingMidRecording || isFlushingReplayBuffer {
lock.unlock()
return
}
guard let task = webSocketTask else {
lock.unlock()
return
}
// Proactive segment rotation at >150s when at a clean committed pause boundary so server never kills long 5–10m streams
let segAge = Date().timeIntervalSince(segmentConnectTime)
let canRotateProactively = segAge > 150.0 && currentNonFinalTokens.isEmpty && !committedFinalTokens.isEmpty
lock.unlock()
if canRotateProactively {
triggerMidRecordingReconnect(cleanArchive: true, reason: "Proactive 150s continuous speech segment rollover")
return
}
let t0 = Date()
let message = URLSessionWebSocketTask.Message.data(data)
task.send(message) { [weak self] error in
guard let self = self else { return }
if let error = error {
print("SonioxLiveSession: Error streaming audio chunk:", error)
self.lock.lock()
if !self.isFallbackSwitched && !self.isFinalizing && !self.isClosed && !self.isAborted && self.archivedCommittedText.isEmpty && self.committedFinalTokens.isEmpty {
self.lock.unlock()
self.switchToFallback()
return
}
self.hasNetworkFailure = true
self.isConnected = false
let activeRec = self.isRecordingActive && !self.isFinalizing && !self.isClosed && !self.isAborted
self.lock.unlock()
self.onNetworkDegraded?(true)
if activeRec {
self.triggerMidRecordingReconnect(cleanArchive: false, reason: "Send audio chunk error: \(error.localizedDescription)")
}
} else {
self.lock.lock()
self.sentAudioBytes += data.count
let sendDur = Date().timeIntervalSince(t0)
if sendDur > 2.5 {
self.hasSilentStall = true
}
self.lock.unlock()
}
}
}
public func finalizeStream() {
lock.lock()
guard !isFinalizing, !isClosed, !isAborted else {
lock.unlock()
return
}
isFinalizing = true
let connected = isConnected && !isReconnectingMidRecording
let flushing = isFlushingReplayBuffer
let task = webSocketTask
if connected && flushing && task != nil {
pendingFinalizeAfterFlush = true
}
lock.unlock()
guard connected, let task = task else {
print("SonioxLiveSession: finalizeStream called while socket disconnected/reconnecting — completing immediately for tail recovery.")
completeWithCurrentText()
return
}
if flushing {
print("SonioxLiveSession: Waiting for initial/replay buffer flush to finish before sending finalize packet...")
} else {
sendFinalizeFramesOnTask(task)
}
// Safety timeout fallback: allow up to 6.0s (aligned above gateway v2's 5.0s finalize drain)
DispatchQueue.global().asyncAfter(deadline: .now() + 6.0) { [weak self] in
guard let self = self else { return }
self.lock.lock()
let stillOpen = !self.isClosed && !self.isAborted
self.lock.unlock()
if stillOpen {
print("SonioxLiveSession: Finalize timeout (6.0s) reached — completing.")
self.completeWithCurrentText()
}
}
}
private func sendFinalizeFramesOnTask(_ task: URLSessionWebSocketTask) {
print("SonioxLiveSession: Sending finalize packet...")
let silenceChunk = Data(count: 9600)
let silenceMsg = URLSessionWebSocketTask.Message.data(silenceChunk)
let finalizeMsg = URLSessionWebSocketTask.Message.string("{\"type\": \"finalize\"}")
let endMsg = URLSessionWebSocketTask.Message.string("END")
task.send(silenceMsg) { [weak self] _ in
task.send(finalizeMsg) { [weak self] error in
if let error = error {
print("SonioxLiveSession: Error sending finalize:", error)
self?.lock.lock()
self?.hasNetworkFailure = true
self?.lock.unlock()
self?.completeWithCurrentText()
return
}
task.send(endMsg) { _ in }
}
}
}
private func startReceiving() {
guard let task = webSocketTask else { return }
task.receive { [weak self] result in
guard let self = self else { return }
self.lock.lock()
if self.isClosed || self.isAborted {
self.lock.unlock()
return
}
self.lock.unlock()
switch result {
case .success(let message):
self.lock.lock()
self.lastDataTime = Date()
self.hasSilentStall = false
self.lock.unlock()
self.onNetworkDegraded?(false)
var textReceived: String?
switch message {
case .string(let str):
textReceived = str
case .data(let data):
textReceived = String(data: data, encoding: .utf8)
@unknown default:
break
}
if let text = textReceived, let jsonData = text.data(using: .utf8) {
self.parseMessage(jsonData)
}
self.startReceiving()
case .failure(let error):
print("SonioxLiveSession: WebSocket receive error:", error)
self.lock.lock()
self.hasNetworkFailure = true
self.isConnected = false
let activeRecording = self.isRecordingActive && !self.isFinalizing && !self.isClosed && !self.isAborted
self.lock.unlock()
self.onNetworkDegraded?(true)
if !activeRecording {
self.completeWithCurrentText()
} else {
print("SonioxLiveSession: Network dropped mid-recording. Triggering automatic live reconnect...")
self.triggerMidRecordingReconnect(cleanArchive: false, reason: "WebSocket receive error: \(error.localizedDescription)")
}
}
}
}
private func parseMessage(_ data: Data) {
guard let json = try? JSONSerialization.jsonObject(with: data) as? [String: Any] else { return }
var gotFin = false
var newFinals: [String] = []
var newNonFinals: [String] = []
var tokenCountDelta = 0
if let type = json["type"] as? String, type == "data",
let parts = json["parts"] as? [[String: Any]] {
for part in parts {
let isTranslation = (part["translation_status"] as? String) == "translation"
if self.isTranslationMode {
if !isTranslation { continue }
} else {
if isTranslation { continue }
}
let pText = part["text"] as? String ?? ""
let isFinal = part["is_final"] as? Bool ?? false
if pText.contains("<fin>") {
gotFin = true
let clean = pText.replacingOccurrences(of: "<fin>", with: "")
if !clean.isEmpty {
newFinals.append(clean)
tokenCountDelta += 1
}
} else if isFinal {
if !pText.isEmpty {
newFinals.append(pText)
tokenCountDelta += 1
}
} else {
if !pText.isEmpty {
newNonFinals.append(pText)
tokenCountDelta += 1
}
}
}
lock.lock()
if isAborted {
lock.unlock()
return
}
totalReceivedTokensCount += tokenCountDelta
if !newFinals.isEmpty {
committedFinalTokens.append(contentsOf: newFinals)
// Advance confirmed audio checkpoint in `currentSegmentAudioBuffer`:
// Keep a safety acoustic lookback proportional to unfinalized words (plus 0.8s = 25600 bytes)
let unfinalizedWords = newNonFinals.joined().components(separatedBy: .whitespacesAndNewlines).filter { !$0.isEmpty }.count
let safetyLookbackBytes = max(25600, (unfinalizedWords + 1) * 16000)
if currentSegmentAudioBuffer.count > safetyLookbackBytes {
lastConfirmedWordAudioByteOffset = max(lastConfirmedWordAudioByteOffset, currentSegmentAudioBuffer.count - safetyLookbackBytes)
}
}
currentNonFinalTokens = newNonFinals
let fullCommitted = committedFinalTokens.joined()
let fullNonFinal = currentNonFinalTokens.joined()
let segmentCombined = fullCommitted + fullNonFinal
let combined: String
if archivedCommittedText.isEmpty {
combined = segmentCombined
} else if segmentCombined.isEmpty {
combined = archivedCommittedText
} else {
let cleanSeg = segmentCombined.trimmingCharacters(in: .whitespaces)
combined = archivedCommittedText + (cleanSeg.isEmpty ? "" : (" " + cleanSeg))
}
let confirmedCombined: String
if archivedCommittedText.isEmpty {
confirmedCombined = fullCommitted
} else if fullCommitted.isEmpty {
confirmedCombined = archivedCommittedText
} else {
let cleanComm = fullCommitted.trimmingCharacters(in: .whitespaces)
confirmedCombined = archivedCommittedText + (cleanComm.isEmpty ? "" : (" " + cleanComm))
}
// Check for in-flight end trigger ("تمام") ONLY in hands-free/toggle mode and ONLY on finalized tokens!
// Never trigger auto-stop during Push-To-Talk, and NEVER trigger on common English words like "and" / "end"!
var triggerDetected = false
if HotkeyManager.shared.currentMode != .pushToTalk && currentNonFinalTokens.isEmpty && !newFinals.isEmpty {
let trimmedCombined = combined.trimmingCharacters(in: .whitespacesAndNewlines)
let currentWords = trimmedCombined.components(separatedBy: .whitespacesAndNewlines).filter { !$0.isEmpty }
if let lastRaw = currentWords.last {
let cleanLast = InputBoxManager.stripPunctuation(lastRaw).lowercased()
if cleanLast == "تمام" {
triggerDetected = 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
let stillActivelyRecording = self.isRecordingActive && !self.isFinalizing
let shouldEmitLiveUpdate = !triggerDetected && !isTerminatingMessage && !self.isFinalizing && !combined.isEmpty
lock.unlock()
if triggerDetected {
DispatchQueue.main.async { [weak self] in
self?.onEndTriggerDetected?()
}
} else if shouldEmitLiveUpdate {
DispatchQueue.main.async { [weak self] in
self?.onLiveTranscriptUpdate?(combined, confirmedCombined)
}
}
if !combined.isEmpty {
DispatchQueue.main.async { [weak self] in
self?.onPartialText?(combined)
}
}
if isTerminatingMessage {
if stillActivelyRecording {
print("SonioxLiveSession: Server ended WebSocket segment while user is still speaking — seamlessly rotating to fresh segment...")
triggerMidRecordingReconnect(cleanArchive: true, reason: "Server ended WebSocket segment while recording is still active")
} else {
lock.lock()
didReceiveTermination = true
lock.unlock()
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 {
lock.lock()
let stillActivelyRecording = self.isRecordingActive && !self.isFinalizing
if !stillActivelyRecording {
self.didReceiveTermination = true
}
lock.unlock()
if stillActivelyRecording {
triggerMidRecordingReconnect(cleanArchive: true, reason: "Server session_done while recording is still active")
} else {
completeWithCurrentText()
}
}
}
}
public func currentBestTranscript() -> String {
lock.lock()
let fullCommitted = committedFinalTokens.joined()
let fullNonFinal = currentNonFinalTokens.joined()
let segCombined = fullCommitted.isEmpty ? fullNonFinal : (fullCommitted + fullNonFinal)
let rawCombined: String
if archivedCommittedText.isEmpty {
rawCombined = segCombined
} else if segCombined.isEmpty {
rawCombined = archivedCommittedText
} else {
rawCombined = archivedCommittedText + " " + segCombined.trimmingCharacters(in: .whitespaces)
}
lock.unlock()
return SonioxLiveSession.sanitizeText(rawCombined)
}
public func completeWithCurrentText() {
lock.lock()
if isClosed || isAborted {
lock.unlock()
return
}
let tailCount = max(0, currentSegmentAudioBuffer.count - lastConfirmedWordAudioByteOffset)
// If server cleanly terminated the stream (`didReceiveTermination`) without a network error,
// NEVER discard the valid transcript due to a transient mid-speech jitter flag!
let hadDrop: Bool
if didReceiveTermination && !hasNetworkFailure {
hadDrop = false
} else {
hadDrop = hasNetworkFailure || hasSilentStall || isReconnectingMidRecording || !isConnected || (isFinalizing && !didReceiveTermination)
}
let needsTailRecovery = hadDrop && tailCount > 4800
isClosed = true
isReconnectingMidRecording = false
let fullCommitted = committedFinalTokens.joined()
let fullNonFinal = currentNonFinalTokens.joined()
let segCombined = fullCommitted.isEmpty ? fullNonFinal : (fullCommitted + fullNonFinal)
let rawCombined: String
if archivedCommittedText.isEmpty {
rawCombined = segCombined
} else if segCombined.isEmpty {
rawCombined = archivedCommittedText
} else {
rawCombined = archivedCommittedText + " " + segCombined.trimmingCharacters(in: .whitespaces)
}
let cleaned = SonioxLiveSession.sanitizeText(rawCombined)
let failureOccurred = hasNetworkFailure
let cb = onFinalResult
webSocketTask?.cancel(with: .normalClosure, reason: nil)
webSocketTask = nil
urlSession?.finishTasksAndInvalidate()
urlSession = nil
lock.unlock()
stopKeepAliveHeartbeat()
DispatchQueue.main.async {
if needsTailRecovery {
cb?(.failure(NSError(domain: "SonioxLiveSession", code: -103, userInfo: [NSLocalizedDescriptionKey: "Partial transcript with \(tailCount) bytes of untranscribed tail audio"])))
} else if cleaned.isEmpty && failureOccurred {
cb?(.failure(NSError(domain: "SonioxLiveSession", code: -101, userInfo: [NSLocalizedDescriptionKey: "WebSocket network failure with empty transcript"])))
} else {
cb?(.success(cleaned))
}
}
}
/// Immediate hard cancellation (Escape key pressed) — suppresses all further callbacks
public func abort() {
lock.lock()
isAborted = true
isClosed = true
isConnected = false
isRecordingActive = false
isFinalizing = false
isReconnectingMidRecording = false
isFlushingReplayBuffer = false
pendingFinalizeAfterFlush = false
initialAudioBuffer.removeAll()
currentSegmentAudioBuffer.removeAll()
onPartialText = nil
onCommittedDelta = nil
onLiveTranscriptUpdate = nil
onEndTriggerDetected = nil
onFinalResult = nil
onConnectionStateChanged = nil
onNetworkDegraded = nil
let task = webSocketTask
let session = urlSession
webSocketTask = nil
urlSession = nil
lock.unlock()
stopKeepAliveHeartbeat()
task?.cancel()
session?.invalidateAndCancel()
}
public func cancel() {
abort()
}
// MARK: - Smart Audio Recovery / Fast Burst Retry
/// Re-transcribes the full audio buffer when the live stream had packet drops, disconnects, or missing trailing words.
public static func retryFullBufferedAudio(
pcmData: Data,
languageHints: [String] = SonioxSettings.shared.language.hints,
targetLanguage: String? = nil,
isCancelled: @escaping () -> Bool = { false },
onProgress: ((String) -> Void)? = nil,
onStatusUpdate: ((String) -> Void)? = nil,
completion: @escaping (Result<String, Error>) -> Void
) {
guard !pcmData.isEmpty, !isCancelled() else {
completion(.failure(NSError(domain: "SonioxLiveSession", code: -102, userInfo: [NSLocalizedDescriptionKey: "Cancelled or empty audio buffer"])))
return
}
let effectiveTarget = targetLanguage ?? (SonioxSettings.shared.targetLanguage.isEnabled ? SonioxSettings.shared.targetLanguage.rawValue : nil)
let durationSec = Double(pcmData.count) / 32000.0
print("SonioxLiveSession: Initiating Smart Burst Recovery: \(pcmData.count) bytes (\(String(format: "%.1f", durationSec))s audio, target: \(effectiveTarget ?? "orig"))...")
// For multi-minute recordings (>60s = 1,920,000 bytes), split into ~60s silence-aligned chunks
// so individual WebSocket burst connections never hit server buffer limits or timeouts!
let maxSingleBurstBytes = 1_920_000
if pcmData.count > maxSingleBurstBytes {
let chunks = splitIntoSilenceAlignedChunks(pcmData: pcmData, targetChunkBytes: maxSingleBurstBytes)
print("SonioxLiveSession: Splitting long recovery audio (\(String(format: "%.1f", durationSec))s) into \(chunks.count) sequential chunks...")
var recoveredParts: [String] = []
func processChunk(at index: Int) {
if isCancelled() {
completion(.failure(NSError(domain: "SonioxLiveSession", code: -999, userInfo: [NSLocalizedDescriptionKey: "Cancelled by Escape"])))
return
}
if index >= chunks.count {
let combinedText = recoveredParts.joined(separator: " ").trimmingCharacters(in: .whitespacesAndNewlines)
if !combinedText.isEmpty {
completion(.success(combinedText))
} else {
completion(.failure(NSError(domain: "SonioxLiveSession", code: -104, userInfo: [NSLocalizedDescriptionKey: "Empty chunked recovery result"])))
}
return
}
DispatchQueue.main.async {
onStatusUpdate?("PART \(index + 1)/\(chunks.count)")
}
retrySingleChunk(
pcmData: chunks[index],
languageHints: languageHints,
effectiveTarget: effectiveTarget,
isCancelled: isCancelled,
onProgress: onProgress,
onStatusUpdate: onStatusUpdate
) { result in
switch result {
case .success(let partText):
let cleanPart = partText.trimmingCharacters(in: .whitespacesAndNewlines)
if !cleanPart.isEmpty {
recoveredParts.append(cleanPart)
}
processChunk(at: index + 1)
case .failure(let err):
if !recoveredParts.isEmpty {
// Salvage already-recovered chunks if a later chunk fails
let partial = recoveredParts.joined(separator: " ").trimmingCharacters(in: .whitespacesAndNewlines)
completion(.success(partial))
} else {
completion(.failure(err))
}
}
}
}
processChunk(at: 0)
return
}
retrySingleChunk(
pcmData: pcmData,
languageHints: languageHints,
effectiveTarget: effectiveTarget,
isCancelled: isCancelled,
onProgress: onProgress,
onStatusUpdate: onStatusUpdate,
completion: completion
)
}
/// Splits a multi-minute 16kHz 16-bit mono PCM buffer at low-energy silence boundaries near `targetChunkBytes`.
private static func splitIntoSilenceAlignedChunks(pcmData: Data, targetChunkBytes: Int) -> [Data] {
var chunks: [Data] = []
var offset = 0
let total = pcmData.count
let searchWindowBytes = 160_000 // Search within last 5.0s of each 60s window for lowest energy pause
let windowStepBytes = 3200 // 100ms window
while offset < total {
let remaining = total - offset
if remaining <= targetChunkBytes + 160_000 {
chunks.append(pcmData.subdata(in: offset..<total))
break
}
var bestCut = offset + targetChunkBytes
let searchStart = max(offset + targetChunkBytes - searchWindowBytes, offset + 64000)
let searchEnd = min(offset + targetChunkBytes + 32000, total - windowStepBytes)
if searchEnd > searchStart {
var minEnergy: Float = .greatestFiniteMagnitude
pcmData.withUnsafeBytes { rawBuf in
guard let basePtr = rawBuf.baseAddress else { return }
var pos = searchStart & ~1 // align to 2-byte Int16 sample
while pos < searchEnd {
let sampleCount = windowStepBytes / 2
let ptr = basePtr.advanced(by: pos).assumingMemoryBound(to: Int16.self)
var sumSq: Float = 0
for i in 0..<sampleCount {
let s = Float(ptr[i])
sumSq += s * s
}
if sumSq < minEnergy {
minEnergy = sumSq
bestCut = pos + (windowStepBytes / 2)
}
pos += windowStepBytes
}
}
}
bestCut = bestCut & ~1
chunks.append(pcmData.subdata(in: offset..<bestCut))
offset = bestCut
}
return chunks
}
private static func retrySingleChunk(
pcmData: Data,
languageHints: [String],
effectiveTarget: String?,
isCancelled: @escaping () -> Bool,
onProgress: ((String) -> Void)?,
onStatusUpdate: ((String) -> Void)?,
completion: @escaping (Result<String, Error>) -> Void
) {
// Multi-stage resilient retry pipeline across dual endpoints with automatic backoff:
// Attempt 1: Primary WS
// Attempt 2: Fallback WS
// Attempt 3: (pause 0.8s for network interface/VPN to stabilize) Primary WS
// Attempt 4: Fallback WS
func executeAttempt(_ step: Int) {
if isCancelled() {
completion(.failure(NSError(domain: "SonioxLiveSession", code: -999, userInfo: [NSLocalizedDescriptionKey: "Cancelled by Escape"])))
return
}
let isPrimary = (step == 1 || step == 3)
let endpoint = isPrimary ? primaryWsBase : fallbackWsBase
let name = isPrimary ? "Primary" : "Fallback"
print("SonioxLiveSession: Smart Recovery Attempt \(step)/4 on \(name) endpoint...")
if step > 1 {
DispatchQueue.main.async {
onStatusUpdate?("RETRY \(step)/4")
}
}
// If network interface is completely offline on attempt >= 2, wait briefly for Wi-Fi/VPN handoff
if !PingMonitor.shared.isPathSatisfied && step >= 2 {
DispatchQueue.global(qos: .userInitiated).asyncAfter(deadline: .now() + 1.0) {
if isCancelled() {
completion(.failure(NSError(domain: "SonioxLiveSession", code: -999, userInfo: [NSLocalizedDescriptionKey: "Cancelled"])))
return
}
if step < 4 {
executeAttempt(step + 1)
} else {
completion(.failure(NSError(domain: "SonioxLiveSession", code: -1009, userInfo: [NSLocalizedDescriptionKey: "Network offline"])))
}
}
return
}
executeBurstSession(
endpoint: endpoint,
pcmData: pcmData,
languageHints: languageHints,
targetLanguage: effectiveTarget,
isCancelled: isCancelled,
onProgress: onProgress
) { result in
if isCancelled() {
completion(.failure(NSError(domain: "SonioxLiveSession", code: -999, userInfo: [NSLocalizedDescriptionKey: "Cancelled"])))
return
}
switch result {
case .success(let text):
print("SonioxLiveSession: ✅ Recovery Attempt \(step) SUCCEEDED (\(text.count) chars).")
completion(.success(text))
case .failure(let error):
print("SonioxLiveSession: Recovery Attempt \(step) failed: \(error)")
if step < 4 {
let delay = (step == 2) ? 0.8 : 0.25
DispatchQueue.global(qos: .userInitiated).asyncAfter(deadline: .now() + delay) {
executeAttempt(step + 1)
}
} else {
print("SonioxLiveSession: ❌ All 4 recovery attempts exhausted: \(error)")
completion(.failure(error))
}
}
}
}
executeAttempt(1)
}
private static func executeBurstSession(
endpoint: String,
pcmData: Data,
languageHints: [String],
targetLanguage: String?,
isCancelled: @escaping () -> Bool,
onProgress: ((String) -> Void)?,
completion: @escaping (Result<String, Error>) -> Void
) {
let isTranslationMode = targetLanguage != nil && !targetLanguage!.isEmpty && targetLanguage != "off"
let queryStr = SonioxLiveSession.baseQuery(withHints: languageHints, targetLanguage: targetLanguage)
let urlStr = "\(endpoint)?\(queryStr)"
guard let url = URL(string: urlStr) else {
completion(.failure(NSError(domain: "Soniox", code: -1, userInfo: [NSLocalizedDescriptionKey: "Invalid URL"])))
return
}
var request = URLRequest(url: url)
request.setValue(userAgent, forHTTPHeaderField: "User-Agent")
request.setValue(origin(for: endpoint), forHTTPHeaderField: "Origin")
let durationSec = Double(pcmData.count) / 32000.0
let watchdogTimeout = max(10.0, min(65.0, durationSec * 0.90 + 10.0))
request.timeoutInterval = 5.0 // Fast handshake timeout so dead endpoint fails over in <5s
let config = URLSessionConfiguration.ephemeral
config.timeoutIntervalForRequest = watchdogTimeout
config.timeoutIntervalForResource = watchdogTimeout + 3.0
config.waitsForConnectivity = false // Fail fast on dead interface so next endpoint attempt runs immediately!
let session = URLSession(configuration: config)
let task = session.webSocketTask(with: request)
task.resume()
var fullCommitted: [String] = []
var latestNonFinal: [String] = []
var isDone = false
let lock = NSLock()
func finishOnce(with result: Result<String, Error>) {
lock.lock()
if isDone { lock.unlock(); return }
isDone = true
task.cancel(with: .normalClosure, reason: nil)
session.finishTasksAndInvalidate()
lock.unlock()
DispatchQueue.main.async {
completion(result)
}
}
func receiveNext() {
if isCancelled() {
finishOnce(with: .failure(NSError(domain: "Soniox", code: -999, userInfo: [NSLocalizedDescriptionKey: "Cancelled"])))
return
}
task.receive { result in
lock.lock()
if isDone || isCancelled() {
lock.unlock()
finishOnce(with: .failure(NSError(domain: "Soniox", code: -999, userInfo: [NSLocalizedDescriptionKey: "Cancelled"])))
return
}
lock.unlock()
switch result {
case .success(let msg):
var textStr: String?
switch msg {
case .string(let s): textStr = s
case .data(let d): textStr = String(data: d, encoding: .utf8)
@unknown default: break
}
if let raw = textStr, let jData = raw.data(using: .utf8),
let json = try? JSONSerialization.jsonObject(with: jData) as? [String: Any] {
var gotFin = false
var packetNonFinal: [String] = []
var packetCommitted: [String] = []
if let parts = json["parts"] as? [[String: Any]] {
for p in parts {
let isTranslation = (p["translation_status"] as? String) == "translation"
if isTranslationMode {
if !isTranslation { continue }
} else {
if isTranslation { continue }
}
let t = p["text"] as? String ?? ""
let isFinal = p["is_final"] as? Bool ?? false
if t.contains("<fin>") {
gotFin = true
let clean = t.replacingOccurrences(of: "<fin>", with: "")
if !clean.isEmpty { packetCommitted.append(clean) }
} else if isFinal {
if !t.isEmpty { packetCommitted.append(t) }
} else {
if !t.isEmpty { packetNonFinal.append(t) }
}
}
}
lock.lock()
if !packetCommitted.isEmpty {
fullCommitted.append(contentsOf: packetCommitted)
}
latestNonFinal = packetNonFinal
let currentProgress = fullCommitted.joined() + (gotFin ? "" : latestNonFinal.joined())
lock.unlock()
if !currentProgress.isEmpty {
DispatchQueue.main.async {
onProgress?(currentProgress)
}
}
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 ended = sessionEnded || sessionDone || isFinished || hasRawFinished
if gotFin || ended {
lock.lock()
let textToReturn = fullCommitted.joined() + (gotFin ? "" : latestNonFinal.joined())
lock.unlock()
let sanitized = sanitizeText(textToReturn)
if !sanitized.isEmpty {
finishOnce(with: .success(sanitized))
} else {
finishOnce(with: .failure(NSError(domain: "Soniox", code: -4, userInfo: [NSLocalizedDescriptionKey: "Server returned empty transcript"])))
}
return
}
}
receiveNext()
case .failure(let err):
print("SonioxLiveSession: Burst receive error on \(endpoint):", err)
lock.lock()
let textToReturn = fullCommitted.joined() + latestNonFinal.joined()
lock.unlock()
let sanitized = sanitizeText(textToReturn)
if !sanitized.isEmpty {
finishOnce(with: .success(sanitized))
} else {
finishOnce(with: .failure(err))
}
}
}
}
receiveNext()
// Handshake verification + Chained Backpressure Upload (32KB / 1.0s chunks)
task.sendPing { pingError in
lock.lock()
let alreadyDone = isDone
lock.unlock()
if alreadyDone || isCancelled() {
finishOnce(with: .failure(NSError(domain: "Soniox", code: -999, userInfo: nil)))
return
}
if let pingErr = pingError {
print("SonioxLiveSession: Burst connection ping failed on \(endpoint): \(pingErr)")
finishOnce(with: .failure(pingErr))
return
}
let chunkSize = 32000 // 1.0s of 16kHz 16-bit PCM
func sendNextChunk(offset: Int) {
lock.lock()
let done = isDone
lock.unlock()
if done || isCancelled() { return }
if offset >= pcmData.count {
let silence = Data(count: 19200)
task.send(.data(silence)) { _ in
task.send(.string("{\"type\": \"finalize\"}")) { _ in
task.send(.string("END")) { _ in }
}
}
return
}
let end = min(offset + chunkSize, pcmData.count)
let slice = pcmData.subdata(in: offset..<end)
task.send(.data(slice)) { sendErr in
if let sendErr = sendErr {
finishOnce(with: .failure(sendErr))
return
}
sendNextChunk(offset: end)
}
}
sendNextChunk(offset: 0)
}
// Dynamic Watchdog timeout
DispatchQueue.global().asyncAfter(deadline: .now() + watchdogTimeout) {
lock.lock()
let textToReturn = fullCommitted.joined() + latestNonFinal.joined()
lock.unlock()
let sanitized = sanitizeText(textToReturn)
if !sanitized.isEmpty {
print("SonioxLiveSession: Burst watchdog fired, salvaging partial text: \(sanitized.prefix(30))...")
finishOnce(with: .success(sanitized))
} else {
print("SonioxLiveSession: Burst watchdog timed out after \(watchdogTimeout)s with no text.")
finishOnce(with: .failure(NSError(domain: "Soniox", code: -5, userInfo: [NSLocalizedDescriptionKey: "Burst watchdog timed out after \(watchdogTimeout)s"])))
}
}
}
// MARK: - Sanitization (Pre-compiled Regexes)
public static func sanitizeText(_ text: String) -> String {
let trimmed = text.trimmingCharacters(in: .whitespacesAndNewlines)
if trimmed.isEmpty { return "" }
let words = trimmed.components(separatedBy: .whitespacesAndNewlines).filter { !$0.isEmpty }
if words.isEmpty { return "" }
// Strip ONLY pure single-phrase silence noise hallucinations — NEVER delete valid English words
// ("for", "from", "all", "no", "day", "you", etc.) inside mixed Persian/English developer speech!
let lowerNormalized = words.map { $0.lowercased().trimmingCharacters(in: punctCharSet) }.joined(separator: " ")
let pureSilenceArtifacts: Set<String> = ["sex", "god loves mankind"]
if pureSilenceArtifacts.contains(lowerNormalized) {
return ""
}
return words.joined(separator: " ").trimmingCharacters(in: .whitespacesAndNewlines)
}
}
// MARK: - Zero-Key Pre-Warmed Session Pool (Gateway v2.0 Zero-Key Idle Prewarm Compatible)
public final class SonioxSessionPool {
public static let shared = SonioxSessionPool()
private var prewarmedSession: SonioxLiveSession?
private var prewarmedSessionCreatedAt: Date = Date()
private var isPrewarming = false
private let lock = NSLock()
private init() {
setupSleepWakeObservers()
prewarmNextSession()
}
private func setupSleepWakeObservers() {
NSWorkspace.shared.notificationCenter.addObserver(forName: NSWorkspace.willSleepNotification, object: nil, queue: .main) { [weak self] _ in
self?.discardPrewarmedSession()
}
NSWorkspace.shared.notificationCenter.addObserver(forName: NSWorkspace.didWakeNotification, object: nil, queue: .main) { [weak self] _ in
self?.discardPrewarmedSession()
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
self?.prewarmNextSession()
}
}
}
public func discardPrewarmedSession() {
lock.lock()
prewarmedSession?.stopKeepAliveHeartbeat()
prewarmedSession?.abort()
prewarmedSession = nil
isPrewarming = false
lock.unlock()
}
/// Pre-warms a WebSocket connection to Gateway v2.0 (`live_ws.py`), which holds ZERO Soniox KeyPool keys
/// until the first binary audio frame arrives — eliminating ~700ms TLS+WS handshake delay on every dictation!
public func prewarmNextSession() {
guard PingMonitor.shared.isPathSatisfied else { return }
lock.lock()
if let existing = prewarmedSession, existing.isReady {
let age = Date().timeIntervalSince(prewarmedSessionCreatedAt)
if age < 45.0 {
lock.unlock()
return
}
}
if isPrewarming {
lock.unlock()
return
}
isPrewarming = true
prewarmedSession?.stopKeepAliveHeartbeat()
prewarmedSession?.abort()
prewarmedSession = nil
lock.unlock()
DispatchQueue.global(qos: .utility).async { [weak self] in
guard let self = self else { return }
let hints = SonioxSettings.shared.language.hints
let target = SonioxSettings.shared.targetLanguage.isEnabled ? SonioxSettings.shared.targetLanguage.rawValue : nil
let session = SonioxLiveSession(languageHints: hints, targetLanguage: target)
session.onConnectionStateChanged = { [weak self, weak session] connected in
guard let self = self, let session = session else { return }
self.lock.lock()
if connected {
session.startKeepAliveHeartbeat()
self.prewarmedSession = session
self.prewarmedSessionCreatedAt = Date()
self.isPrewarming = false
print("SonioxSessionPool: Zero-key prewarmed gateway WS ready (0ms connect delay).")
} else {
self.isPrewarming = false
}
self.lock.unlock()
}
DispatchQueue.global(qos: .utility).asyncAfter(deadline: .now() + 10.0) { [weak self] in
guard let self = self else { return }
self.lock.lock()
if self.isPrewarming && self.prewarmedSession == nil {
self.isPrewarming = false
}
self.lock.unlock()
}
}
}
public func reconnectAll() {
discardPrewarmedSession()
prewarmNextSession()
}
/// Non-blocking session acquisition (0ms main-thread wait!).
public func acquireSession() -> SonioxLiveSession {
lock.lock()
let session = prewarmedSession
let createdAt = prewarmedSessionCreatedAt
prewarmedSession = nil
lock.unlock()
DispatchQueue.global(qos: .utility).async { [weak self] in
self?.prewarmNextSession()
}
let age = Date().timeIntervalSince(createdAt)
if let session = session, session.isReady && age < 55.0 {
session.stopKeepAliveHeartbeat()
print("SonioxSessionPool: Acquired HOT zero-key prewarmed session (age: \(String(format: "%.1f", age))s, 0ms connect delay!)")
return session
}
session?.stopKeepAliveHeartbeat()
session?.abort()
let hints = SonioxSettings.shared.language.hints
let target = SonioxSettings.shared.targetLanguage.isEnabled ? SonioxSettings.shared.targetLanguage.rawValue : nil
return SonioxLiveSession(languageHints: hints, targetLanguage: target)
}
}
// MARK: - Persistent Offline Audio Queue (Zero Data Loss on Multi-Minute Internet Outages)
public final class OfflineAudioQueueManager {
public static let shared = OfflineAudioQueueManager()
private let queueDir: URL
private let workerQueue = DispatchQueue(label: "com.soniox.offlinequeue", qos: .utility)
private var isProcessing = false
private let lock = NSLock()
public var onOfflineAudioRecovered: ((String) -> Void)?
private init() {
let appSupport = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first!
self.queueDir = appSupport.appendingPathComponent("SonioxVoice/PendingAudioQueue")
try? FileManager.default.createDirectory(at: queueDir, withIntermediateDirectories: true)
}
public func enqueueFailedAudio(pcmData: Data) {
guard pcmData.count >= 16000 else { return } // Only save meaningful speech (>= 0.5s)
workerQueue.async { [weak self] in
guard let self = self else { return }
try? FileManager.default.createDirectory(at: self.queueDir, withIntermediateDirectories: true)
let timestamp = Int(Date().timeIntervalSince1970 * 1000)
let fileURL = self.queueDir.appendingPathComponent("speech_\(timestamp).pcm")
do {
try pcmData.write(to: fileURL, options: .atomic)
print("OfflineAudioQueueManager: 💾 Saved \(pcmData.count) bytes of speech to disk queue (\(fileURL.lastPathComponent)) for automatic recovery when internet returns.")
} catch {
print("OfflineAudioQueueManager: Failed to write offline audio:", error)
}
}
}
public func clearAllPending() {
workerQueue.async { [weak self] in
guard let self = self else { return }
if let files = try? FileManager.default.contentsOfDirectory(at: self.queueDir, includingPropertiesForKeys: nil) {
for f in files where f.pathExtension == "pcm" {
try? FileManager.default.removeItem(at: f)
}
}
}
}
public func processPendingQueueIfNeeded(isCancelled: @escaping () -> Bool = { false }) {
guard PingMonitor.shared.isPathSatisfied else { return }
lock.lock()
if isProcessing {
lock.unlock()
return
}
isProcessing = true
lock.unlock()
workerQueue.async { [weak self] in
guard let self = self else { return }
guard let files = try? FileManager.default.contentsOfDirectory(at: self.queueDir, includingPropertiesForKeys: [.creationDateKey]),
!files.isEmpty else {
self.lock.lock()
self.isProcessing = false
self.lock.unlock()
return
}
let pcmFiles = files.filter { $0.pathExtension == "pcm" }.sorted { $0.lastPathComponent < $1.lastPathComponent }
guard let firstFile = pcmFiles.first else {
self.lock.lock()
self.isProcessing = false
self.lock.unlock()
return
}
// Drop stale files older than 2 hours
if let attrs = try? FileManager.default.attributesOfItem(atPath: firstFile.path),
let created = attrs[.creationDate] as? Date,
Date().timeIntervalSince(created) > 7200 {
try? FileManager.default.removeItem(at: firstFile)
self.lock.lock()
self.isProcessing = false
self.lock.unlock()
self.processPendingQueueIfNeeded(isCancelled: isCancelled)
return
}
guard let pcmData = try? Data(contentsOf: firstFile), !pcmData.isEmpty else {
try? FileManager.default.removeItem(at: firstFile)
self.lock.lock()
self.isProcessing = false
self.lock.unlock()
return
}
print("OfflineAudioQueueManager: 🌐 Network is back! Recovering queued speech file \(firstFile.lastPathComponent) (\(pcmData.count) bytes)...")
SonioxLiveSession.retryFullBufferedAudio(pcmData: pcmData, isCancelled: isCancelled) { [weak self] result in
guard let self = self else { return }
switch result {
case .success(let recoveredText):
let trimmed = recoveredText.trimmingCharacters(in: .whitespacesAndNewlines)
if !trimmed.isEmpty {
try? FileManager.default.removeItem(at: firstFile)
DispatchQueue.main.async {
self.onOfflineAudioRecovered?(trimmed)
}
self.lock.lock()
self.isProcessing = false
self.lock.unlock()
self.processPendingQueueIfNeeded(isCancelled: isCancelled)
return
}
case .failure:
break
}
self.lock.lock()
self.isProcessing = false
self.lock.unlock()
}
}
}
}