diff --git a/src/SonioxClient.swift b/src/SonioxClient.swift index c99aa2e..f357d14 100644 --- a/src/SonioxClient.swift +++ b/src/SonioxClient.swift @@ -316,19 +316,25 @@ public final class SonioxLiveSession { } } - // Emit live streaming deltas STRICTLY from committed final tokens (is_final == true): + // Ultra-responsive real-time word-by-word streaming: + // A word in 'combined' is completed as soon as a whitespace character follows it. var deltaToEmit: String? = nil - 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 + 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 + } + } } } }