|
|
@ -80,6 +80,7 @@ public final class SonioxLiveSession { |
|
|
lock.lock() |
|
|
lock.lock() |
|
|
isRecordingActive = active |
|
|
isRecordingActive = active |
|
|
if active { |
|
|
if active { |
|
|
|
|
|
hasSilentStall = false |
|
|
lastDataTime = Date() |
|
|
lastDataTime = Date() |
|
|
segmentConnectTime = Date() |
|
|
segmentConnectTime = Date() |
|
|
} |
|
|
} |
|
|
@ -137,7 +138,7 @@ public final class SonioxLiveSession { |
|
|
DispatchQueue.main.async { [weak self] in |
|
|
DispatchQueue.main.async { [weak self] in |
|
|
guard let self = self else { return } |
|
|
guard let self = self else { return } |
|
|
self.heartbeatTimer?.invalidate() |
|
|
self.heartbeatTimer?.invalidate() |
|
|
self.heartbeatTimer = Timer.scheduledTimer(withTimeInterval: 6.0, repeats: true) { [weak self] _ in |
|
|
|
|
|
|
|
|
self.heartbeatTimer = Timer.scheduledTimer(withTimeInterval: 18.0, repeats: true) { [weak self] _ in |
|
|
guard let self = self else { return } |
|
|
guard let self = self else { return } |
|
|
self.lock.lock() |
|
|
self.lock.lock() |
|
|
guard !self.isClosed, !self.isFinalizing, !self.isAborted, let task = self.webSocketTask else { |
|
|
guard !self.isClosed, !self.isFinalizing, !self.isAborted, let task = self.webSocketTask else { |
|
|
@ -159,7 +160,7 @@ public final class SonioxLiveSession { |
|
|
SonioxSessionPool.shared.prewarmNextSession() |
|
|
SonioxSessionPool.shared.prewarmNextSession() |
|
|
} else { |
|
|
} else { |
|
|
let rtt = Date().timeIntervalSince(t0) |
|
|
let rtt = Date().timeIntervalSince(t0) |
|
|
if rtt > 0.95 { |
|
|
|
|
|
|
|
|
if rtt > 2.5 { |
|
|
self.lock.lock() |
|
|
self.lock.lock() |
|
|
self.hasSilentStall = true |
|
|
self.hasSilentStall = true |
|
|
self.lock.unlock() |
|
|
self.lock.unlock() |
|
|
@ -559,7 +560,7 @@ public final class SonioxLiveSession { |
|
|
self.lock.lock() |
|
|
self.lock.lock() |
|
|
self.sentAudioBytes += data.count |
|
|
self.sentAudioBytes += data.count |
|
|
let sendDur = Date().timeIntervalSince(t0) |
|
|
let sendDur = Date().timeIntervalSince(t0) |
|
|
if sendDur > 0.70 { |
|
|
|
|
|
|
|
|
if sendDur > 2.5 { |
|
|
self.hasSilentStall = true |
|
|
self.hasSilentStall = true |
|
|
} |
|
|
} |
|
|
self.lock.unlock() |
|
|
self.lock.unlock() |
|
|
@ -594,14 +595,14 @@ public final class SonioxLiveSession { |
|
|
sendFinalizeFramesOnTask(task) |
|
|
sendFinalizeFramesOnTask(task) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Safety timeout fallback: allow up to 4.5s for STT inference & network transit |
|
|
|
|
|
DispatchQueue.global().asyncAfter(deadline: .now() + 4.5) { [weak self] in |
|
|
|
|
|
|
|
|
// 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 } |
|
|
guard let self = self else { return } |
|
|
self.lock.lock() |
|
|
self.lock.lock() |
|
|
let stillOpen = !self.isClosed && !self.isAborted |
|
|
let stillOpen = !self.isClosed && !self.isAborted |
|
|
self.lock.unlock() |
|
|
self.lock.unlock() |
|
|
if stillOpen { |
|
|
if stillOpen { |
|
|
print("SonioxLiveSession: Finalize timeout (4.5s) reached — completing.") |
|
|
|
|
|
|
|
|
print("SonioxLiveSession: Finalize timeout (6.0s) reached — completing.") |
|
|
self.completeWithCurrentText() |
|
|
self.completeWithCurrentText() |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
@ -867,7 +868,14 @@ public final class SonioxLiveSession { |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
let tailCount = max(0, currentSegmentAudioBuffer.count - lastConfirmedWordAudioByteOffset) |
|
|
let tailCount = max(0, currentSegmentAudioBuffer.count - lastConfirmedWordAudioByteOffset) |
|
|
let hadDrop = hasNetworkFailure || hasSilentStall || isReconnectingMidRecording || !isConnected || (isFinalizing && !didReceiveTermination) |
|
|
|
|
|
|
|
|
// 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 |
|
|
let needsTailRecovery = hadDrop && tailCount > 4800 |
|
|
|
|
|
|
|
|
isClosed = true |
|
|
isClosed = true |
|
|
@ -1397,17 +1405,19 @@ public final class SonioxLiveSession { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// MARK: - On-Demand Session Factory (Zero Idle Key Burn + Non-Blocking Acquisition) |
|
|
|
|
|
|
|
|
// MARK: - Zero-Key Pre-Warmed Session Pool (Gateway v2.0 Zero-Key Idle Prewarm Compatible) |
|
|
|
|
|
|
|
|
public final class SonioxSessionPool { |
|
|
public final class SonioxSessionPool { |
|
|
public static let shared = SonioxSessionPool() |
|
|
public static let shared = SonioxSessionPool() |
|
|
|
|
|
|
|
|
private var prewarmedSession: SonioxLiveSession? |
|
|
private var prewarmedSession: SonioxLiveSession? |
|
|
private var prewarmedSessionCreatedAt: Date = Date() |
|
|
private var prewarmedSessionCreatedAt: Date = Date() |
|
|
|
|
|
private var isPrewarming = false |
|
|
private let lock = NSLock() |
|
|
private let lock = NSLock() |
|
|
|
|
|
|
|
|
private init() { |
|
|
private init() { |
|
|
setupSleepWakeObservers() |
|
|
setupSleepWakeObservers() |
|
|
|
|
|
prewarmNextSession() |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private func setupSleepWakeObservers() { |
|
|
private func setupSleepWakeObservers() { |
|
|
@ -1417,6 +1427,9 @@ public final class SonioxSessionPool { |
|
|
|
|
|
|
|
|
NSWorkspace.shared.notificationCenter.addObserver(forName: NSWorkspace.didWakeNotification, object: nil, queue: .main) { [weak self] _ in |
|
|
NSWorkspace.shared.notificationCenter.addObserver(forName: NSWorkspace.didWakeNotification, object: nil, queue: .main) { [weak self] _ in |
|
|
self?.discardPrewarmedSession() |
|
|
self?.discardPrewarmedSession() |
|
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) { |
|
|
|
|
|
self?.prewarmNextSession() |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -1425,20 +1438,70 @@ public final class SonioxSessionPool { |
|
|
prewarmedSession?.stopKeepAliveHeartbeat() |
|
|
prewarmedSession?.stopKeepAliveHeartbeat() |
|
|
prewarmedSession?.abort() |
|
|
prewarmedSession?.abort() |
|
|
prewarmedSession = nil |
|
|
prewarmedSession = nil |
|
|
|
|
|
isPrewarming = false |
|
|
lock.unlock() |
|
|
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() { |
|
|
public func prewarmNextSession() { |
|
|
// Intentional no-op while idle: opening an idle WebSocket burns single-use KeyPool temp keys |
|
|
|
|
|
// and risks half-open upstream timeouts. AudioRecorder captures from t=0ms into currentSegmentAudioBuffer, |
|
|
|
|
|
// which flushes in strict FIFO order the instant the on-demand WebSocket connects! |
|
|
|
|
|
|
|
|
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() { |
|
|
public func reconnectAll() { |
|
|
discardPrewarmedSession() |
|
|
discardPrewarmedSession() |
|
|
|
|
|
prewarmNextSession() |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/// Non-blocking session creation (0ms main-thread wait!). |
|
|
|
|
|
|
|
|
/// Non-blocking session acquisition (0ms main-thread wait!). |
|
|
public func acquireSession() -> SonioxLiveSession { |
|
|
public func acquireSession() -> SonioxLiveSession { |
|
|
lock.lock() |
|
|
lock.lock() |
|
|
let session = prewarmedSession |
|
|
let session = prewarmedSession |
|
|
@ -1446,9 +1509,14 @@ public final class SonioxSessionPool { |
|
|
prewarmedSession = nil |
|
|
prewarmedSession = nil |
|
|
lock.unlock() |
|
|
lock.unlock() |
|
|
|
|
|
|
|
|
|
|
|
DispatchQueue.global(qos: .utility).async { [weak self] in |
|
|
|
|
|
self?.prewarmNextSession() |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
let age = Date().timeIntervalSince(createdAt) |
|
|
let age = Date().timeIntervalSince(createdAt) |
|
|
if let session = session, session.isReady && age < 12.0 { |
|
|
|
|
|
|
|
|
if let session = session, session.isReady && age < 55.0 { |
|
|
session.stopKeepAliveHeartbeat() |
|
|
session.stopKeepAliveHeartbeat() |
|
|
|
|
|
print("SonioxSessionPool: Acquired HOT zero-key prewarmed session (age: \(String(format: "%.1f", age))s, 0ms connect delay!)") |
|
|
return session |
|
|
return session |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|