Browse Source

fix: stream ONLY finalized (is_final == true) tokens live as user speaks, eliminating raw acoustic noise and double-pasting

main
Ali Alavi 5 days ago
parent
commit
c3bce16df2
  1. 2
      src/AppDelegate.swift
  2. 24
      src/SonioxClient.swift

2
src/AppDelegate.swift

@ -228,14 +228,12 @@ public final class AppDelegate: NSObject, NSApplicationDelegate {
self.hasSpokenInCurrentSession = true self.hasSpokenInCurrentSession = true
self.lastVoiceDetectedTime = Date() self.lastVoiceDetectedTime = Date()
if SonioxSettings.shared.liveStreamingInput {
// If it's an executive slash command, do NOT type it into the input box // If it's an executive slash command, do NOT type it into the input box
if SonioxSettings.shared.voiceCommands && VoiceCommandExecutor.shared.isSlashCommand(text: delta) { if SonioxSettings.shared.voiceCommands && VoiceCommandExecutor.shared.isSlashCommand(text: delta) {
return return
} }
InputBoxManager.shared.streamDelta(text: delta) InputBoxManager.shared.streamDelta(text: delta)
} }
}
session.onEndTriggerDetected = { [weak self] in session.onEndTriggerDetected = { [weak self] in
guard let self = self else { return } guard let self = self else { return }

24
src/SonioxClient.swift

@ -316,28 +316,22 @@ 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 committed final tokens (is_final == true):
var deltaToEmit: String? = nil var deltaToEmit: String? = nil
if !triggerDetected {
if let lastSpaceRange = combined.rangeOfCharacter(from: .whitespacesAndNewlines, options: .backwards) {
let stablePortion = String(combined[..<lastSpaceRange.upperBound])
if stablePortion.count > streamedPrefix.count {
if stablePortion.hasPrefix(streamedPrefix) {
let delta = String(stablePortion.dropFirst(streamedPrefix.count))
streamedPrefix = stablePortion
if !triggerDetected && fullCommitted.count > streamedPrefix.count {
if fullCommitted.hasPrefix(streamedPrefix) {
let delta = String(fullCommitted.dropFirst(streamedPrefix.count))
streamedPrefix = fullCommitted
deltaToEmit = delta deltaToEmit = delta
} else { } else {
let common = stablePortion.commonPrefix(with: streamedPrefix)
if stablePortion.count > common.count {
let delta = String(stablePortion.dropFirst(common.count))
streamedPrefix = stablePortion
let common = fullCommitted.commonPrefix(with: streamedPrefix)
if fullCommitted.count > common.count {
let delta = String(fullCommitted.dropFirst(common.count))
streamedPrefix = fullCommitted
deltaToEmit = delta deltaToEmit = delta
} }
} }
} }
}
}
lock.unlock() lock.unlock()
if triggerDetected { if triggerDetected {

Loading…
Cancel
Save