From 1602bb5f027e837e87e46dea00fef6c9bc3fa93d Mon Sep 17 00:00:00 2001 From: Ali Alavi Date: Sun, 4 Oct 2026 19:31:59 +0000 Subject: [PATCH] fix: strictly restrict live streaming to Soniox AI-edited finalized tokens (is_final == true) --- src/SonioxClient.swift | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/src/SonioxClient.swift b/src/SonioxClient.swift index 1178b61..e773422 100644 --- a/src/SonioxClient.swift +++ b/src/SonioxClient.swift @@ -317,25 +317,19 @@ public final class SonioxLiveSession { } } - // Ultra-responsive real-time word-by-word streaming: - // A word in 'combined' is completed as soon as a whitespace character follows it. + // Emit live streaming deltas STRICTLY from Soniox-finalized tokens (is_final == true): var deltaToEmit: String? = nil - if !triggerDetected { - if let lastSpaceRange = combined.rangeOfCharacter(from: .whitespacesAndNewlines, options: .backwards) { - let stablePortion = String(combined[.. streamedPrefix.count { - if stablePortion.hasPrefix(streamedPrefix) { - let delta = String(stablePortion.dropFirst(streamedPrefix.count)) - streamedPrefix = stablePortion - deltaToEmit = delta - } else { - let common = stablePortion.commonPrefix(with: streamedPrefix) - if stablePortion.count > common.count { - let delta = String(stablePortion.dropFirst(common.count)) - streamedPrefix = stablePortion - deltaToEmit = delta - } - } + if !triggerDetected && 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 } } }