Browse Source

feat(v3.0): zero-spam Unicode typing, ClipboardGuard, dual typing modes & first-word drop fixes

main
Ali Alavi 23 hours ago
parent
commit
ddcb7c17ad
  1. 2
      Info.plist
  2. 91
      src/AppDelegate.swift
  3. 36
      src/AudioRecorder.swift
  4. 133
      src/ClipboardGuard.swift
  5. 644
      src/InputBoxManager.swift
  6. 273
      src/SonioxClient.swift
  7. 82
      src/SonioxSettings.swift
  8. 63
      src/StatusBarController.swift
  9. 284
      src/TextProcessor.swift

2
Info.plist

@ -19,7 +19,7 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>2.17</string>
<string>3.0</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSMinimumSystemVersion</key>

91
src/AppDelegate.swift

@ -182,11 +182,9 @@ public final class AppDelegate: NSObject, NSApplicationDelegate {
let processed = TextProcessor.shared.process(text: recoveredText)
guard !processed.isEmpty else { return }
HistoryManager.shared.addEntry(processed)
let pb = NSPasteboard.general
pb.clearContents()
pb.setString(processed, forType: .string)
ClipboardGuard.shared.commitFinalClipboardRecordIfDesired(text: processed, deliveryMode: .batchOnRelease)
self.playSound(named: "Glass")
print("AppDelegate: ✅ Offline queued speech recovered and placed on clipboard & history!")
print("AppDelegate: ✅ Offline queued speech recovered and saved to history!")
}
PingMonitor.shared.start()
@ -273,24 +271,25 @@ public final class AppDelegate: NSObject, NSApplicationDelegate {
lastVoiceDetectedTime = Date()
hasSpokenInCurrentSession = false
// Switch PingMonitor to 1.0s active cadence while HUD is shown
PingMonitor.shared.setActiveRecordingMode(true)
// Start Hands-free silence watchdog timer (checks every 250ms)
handsFreeSilenceTimer?.invalidate()
handsFreeSilenceTimer = Timer.scheduledTimer(withTimeInterval: 0.25, repeats: true) { [weak self] _ in
self?.checkHandsFreeSilence()
}
// 1. Reset session state without stealing focus
// 1. Reset input state & spin up non-blocking session + microphone IMMEDIATELY (t ≈ 0ms, before UI/Menu/Sound!)
InputBoxManager.shared.ensureFocusedInputBox()
// 2. Acquire pre-warmed / fast-connected WebSocket session
let session = SonioxSessionPool.shared.acquireSession()
session.setRecordingActive(true)
self.activeSession = session
// Handle network degradation warning
do {
try audioRecorder.startRecording()
} catch {
print("Failed to start recording: \(error)")
PingMonitor.shared.setActiveRecordingMode(false)
activeSession?.abort()
activeSession = nil
HUDOverlayController.shared.show(state: .error(message: "MIC ERR"))
return
}
// 2. Wire session callbacks
session.onNetworkDegraded = { [weak self] degraded in
guard let self = self, self.currentGeneration() == myGen else { return }
self.isNetworkDegraded = degraded
@ -302,34 +301,15 @@ public final class AppDelegate: NSObject, NSApplicationDelegate {
session.onEndTriggerDetected = { [weak self] in
guard let self = self, self.currentGeneration() == myGen else { return }
guard HotkeyManager.shared.currentMode != .pushToTalk else { return }
print("AppDelegate: In-flight end trigger ('and'/'تمام') detected! Stopping recording immediately...")
print("AppDelegate: In-flight end trigger ('تمام') detected! Stopping recording immediately...")
self.stopRecording()
}
// Stream live finalized tokens directly into input box word-by-word
session.onCommittedDelta = { [weak self] delta in
session.onLiveTranscriptUpdate = { [weak self] fullCandidate, confirmedPrefix in
guard let self = self, self.currentGeneration() == myGen else { return }
self.hasSpokenInCurrentSession = true
self.lastVoiceDetectedTime = Date()
if HotkeyManager.shared.currentMode != .pushToTalk {
let deltaWords = delta.components(separatedBy: .whitespacesAndNewlines).filter { !$0.isEmpty }
if let lastWord = deltaWords.last.map({ InputBoxManager.stripPunctuation($0).lowercased() }) {
if lastWord == "and" || lastWord == "تمام" || lastWord == "end" {
print("AppDelegate: In-speech end trigger detected ('\(lastWord)'). Finalizing speech...")
DispatchQueue.main.async {
self.stopRecording()
}
return
}
}
}
if SonioxSettings.shared.voiceCommands && VoiceCommandExecutor.shared.isSlashCommand(text: delta) {
return
}
InputBoxManager.shared.streamDelta(text: delta)
InputBoxManager.shared.updateLiveTranscript(fullCandidate: fullCandidate, confirmedPrefix: confirmedPrefix)
}
session.onPartialText = { [weak self] partial in
@ -341,25 +321,24 @@ public final class AppDelegate: NSObject, NSApplicationDelegate {
}
}
do {
try audioRecorder.startRecording()
statusBarController.updateIcon(state: .recording)
statusBarController.buildMenu(isRecording: true)
HUDOverlayController.shared.show(
state: .recording(
level: 0.05,
isLagging: false,
pingQuality: PingMonitor.shared.currentQuality
)
)
playStartSound()
} catch {
print("Failed to start recording: \(error)")
PingMonitor.shared.setActiveRecordingMode(false)
activeSession?.abort()
activeSession = nil
HUDOverlayController.shared.show(state: .error(message: "MIC ERR"))
// 3. Switch PingMonitor, Silence Watchdog, HUD, Status Bar & Chime (after mic is already capturing!)
PingMonitor.shared.setActiveRecordingMode(true)
handsFreeSilenceTimer?.invalidate()
handsFreeSilenceTimer = Timer.scheduledTimer(withTimeInterval: 0.25, repeats: true) { [weak self] _ in
self?.checkHandsFreeSilence()
}
statusBarController.updateIcon(state: .recording)
statusBarController.buildMenu(isRecording: true)
HUDOverlayController.shared.show(
state: .recording(
level: 0.05,
isLagging: false,
pingQuality: PingMonitor.shared.currentQuality
)
)
playStartSound()
}
private func cancelShortTapRecording() {

36
src/AudioRecorder.swift

@ -12,7 +12,7 @@ public final class AudioRecorder: NSObject, AVCaptureAudioDataOutputSampleBuffer
private var pcmBuffer = Data()
private let lock = NSLock()
private let captureQueue = DispatchQueue(label: "com.soniox.audiocapture", qos: .userInteractive)
private let sessionQueue = DispatchQueue(label: "com.soniox.audiosession", qos: .userInitiated)
private let sessionQueue = DispatchQueue(label: "com.soniox.audiosession", qos: .userInteractive)
// Pre-allocated reusable buffers to eliminate 50Hz per-frame heap allocations
private var reusableSrcBuffer: AVAudioPCMBuffer?
@ -182,16 +182,31 @@ public final class AudioRecorder: NSObject, AVCaptureAudioDataOutputSampleBuffer
guard let srcBuffer = reusableSrcBuffer else { return }
srcBuffer.frameLength = requiredSrcCap
var lengthAtOffset = 0
var totalLength = 0
var dataPointer: UnsafeMutablePointer<Int8>?
let bytesPerFrame = Int(srcFormat.streamDescription.pointee.mBytesPerFrame)
let totalBlockLen = CMBlockBufferGetDataLength(blockBuffer)
guard bytesPerFrame > 0, totalBlockLen > 0 else { return }
if CMBlockBufferGetDataPointer(blockBuffer, atOffset: 0, lengthAtOffsetOut: &lengthAtOffset, totalLengthOut: &totalLength, dataPointerOut: &dataPointer) == noErr,
let dataPtr = dataPointer {
if let floatData = srcBuffer.floatChannelData?[0] {
memcpy(floatData, dataPtr, min(totalLength, Int(srcBuffer.frameLength) * 4))
} else if let int16Data = srcBuffer.int16ChannelData?[0] {
memcpy(int16Data, dataPtr, min(totalLength, Int(srcBuffer.frameLength) * 2))
if srcFormat.isInterleaved {
let abl = srcBuffer.mutableAudioBufferList
if let dst = abl.pointee.mBuffers.mData {
let bytesToCopy = min(totalBlockLen, Int(requiredSrcCap) * bytesPerFrame)
guard CMBlockBufferCopyDataBytes(blockBuffer, atOffset: 0, dataLength: bytesToCopy, destination: dst) == kCMBlockBufferNoErr else { return }
abl.pointee.mBuffers.mDataByteSize = UInt32(bytesToCopy)
}
} else {
let chCount = Int(srcFormat.channelCount)
let planeBytes = Int(requiredSrcCap) * bytesPerFrame
for ch in 0..<chCount {
let offset = ch * planeBytes
if offset >= totalBlockLen { break }
let copyLen = min(planeBytes, totalBlockLen - offset)
if let floatChannel = srcBuffer.floatChannelData?[ch] {
_ = CMBlockBufferCopyDataBytes(blockBuffer, atOffset: offset, dataLength: copyLen, destination: floatChannel)
} else if let int16Channel = srcBuffer.int16ChannelData?[ch] {
_ = CMBlockBufferCopyDataBytes(blockBuffer, atOffset: offset, dataLength: copyLen, destination: int16Channel)
} else if let int32Channel = srcBuffer.int32ChannelData?[ch] {
_ = CMBlockBufferCopyDataBytes(blockBuffer, atOffset: offset, dataLength: copyLen, destination: int32Channel)
}
}
}
@ -200,6 +215,7 @@ public final class AudioRecorder: NSObject, AVCaptureAudioDataOutputSampleBuffer
reusableOutBuffer = AVAudioPCMBuffer(pcmFormat: targetFormat, frameCapacity: max(outCapacity, 2048))
}
guard let outBuffer = reusableOutBuffer else { return }
outBuffer.frameLength = 0
var error: NSError?
var haveData = true

133
src/ClipboardGuard.swift

@ -0,0 +1,133 @@
import Cocoa
/// Manages NSPasteboard writes with strict anti-spam guarantees:
/// - Never touches clipboard during live word-by-word typing.
/// - Writes at most ONCE per dictation when `.copyFinalOnce` is active.
/// - Supports `org.nspasteboard.TransientType` & `org.nspasteboard.AutoGeneratedType`
/// plus full multi-representation snapshot/restore when `.preserveOriginal` is active,
/// preventing pollution in Maccy, Raycast, Paste, Alfred, and LaunchBar.
public final class ClipboardGuard {
public static let shared = ClipboardGuard()
private static let transientType = NSPasteboard.PasteboardType("org.nspasteboard.TransientType")
private static let autoGeneratedType = NSPasteboard.PasteboardType("org.nspasteboard.AutoGeneratedType")
private struct SavedItem {
let representations: [(type: NSPasteboard.PasteboardType, data: Data)]
}
private let lock = NSLock()
private var pendingRestoreWorkItem: DispatchWorkItem?
private init() {}
/// Snapshots all current items & types on NSPasteboard.general before a transient paste.
private func snapshotCurrentPasteboard() -> [SavedItem] {
let pb = NSPasteboard.general
guard let items = pb.pasteboardItems, !items.isEmpty else { return [] }
var saved: [SavedItem] = []
for item in items {
var reps: [(NSPasteboard.PasteboardType, Data)] = []
for type in item.types {
if type == Self.transientType || type == Self.autoGeneratedType {
continue
}
if let data = item.data(forType: type) {
reps.append((type, data))
}
}
if !reps.isEmpty {
saved.append(SavedItem(representations: reps))
}
}
return saved
}
/// Restores previously saved items only if no external app modified the pasteboard in the meantime.
private func restorePasteboard(_ saved: [SavedItem], expectedChangeCount: Int) {
let pb = NSPasteboard.general
guard pb.changeCount == expectedChangeCount else { return }
pb.clearContents()
guard !saved.isEmpty else { return }
var restoredItems: [NSPasteboardItem] = []
for s in saved {
let item = NSPasteboardItem()
for (type, data) in s.representations {
item.setData(data, forType: type)
}
restoredItems.append(item)
}
pb.writeObjects(restoredItems)
}
/// Writes final dictation text to clipboard at most ONCE according to user's ClipboardBehavior,
/// when text has already been typed via direct Unicode keystrokes (Live Word-by-Word mode).
public func commitFinalClipboardRecordIfDesired(text: String, deliveryMode: TypingDeliveryMode) {
let clean = text.trimmingCharacters(in: .whitespacesAndNewlines)
guard !clean.isEmpty else { return }
let behavior = SonioxSettings.shared.clipboardBehavior
switch behavior {
case .copyFinalOnce:
lock.lock()
pendingRestoreWorkItem?.cancel()
pendingRestoreWorkItem = nil
lock.unlock()
let pb = NSPasteboard.general
pb.clearContents()
pb.setString(clean, forType: .string)
case .preserveOriginal:
// Zero clipboard touches in live typing + preserve mode!
break
case .copyInBatchOnly:
if deliveryMode == .batchOnRelease {
lock.lock()
pendingRestoreWorkItem?.cancel()
pendingRestoreWorkItem = nil
lock.unlock()
let pb = NSPasteboard.general
pb.clearContents()
pb.setString(clean, forType: .string)
}
}
}
/// Prepares NSPasteboard for a single atomic Cmd+V paste (used in Batch Mode),
/// respecting the configured ClipboardBehavior.
public func writeForAtomicPaste(textToPaste: String, cleanFinalText: String) {
lock.lock()
pendingRestoreWorkItem?.cancel()
pendingRestoreWorkItem = nil
lock.unlock()
let pb = NSPasteboard.general
let behavior = SonioxSettings.shared.clipboardBehavior
switch behavior {
case .copyFinalOnce, .copyInBatchOnly:
// Write ONCE so changeCount increments only 1 time per dictation!
pb.clearContents()
pb.setString(textToPaste, forType: .string)
case .preserveOriginal:
let snapshot = snapshotCurrentPasteboard()
pb.clearContents()
let item = NSPasteboardItem()
item.setString(textToPaste, forType: .string)
item.setData(Data(), forType: Self.transientType)
item.setData(Data(), forType: Self.autoGeneratedType)
pb.writeObjects([item])
let expectedCount = pb.changeCount
let work = DispatchWorkItem { [weak self] in
self?.restorePasteboard(snapshot, expectedChangeCount: expectedCount)
}
lock.lock()
pendingRestoreWorkItem = work
lock.unlock()
DispatchQueue.main.asyncAfter(deadline: .now() + 0.14, execute: work)
}
}
}

644
src/InputBoxManager.swift

@ -1,22 +1,39 @@
import Cocoa
import ApplicationServices
/// Zero-Spam Production Text Injection Engine (v3.0):
/// - Live Word-by-Word Mode: Injects characters directly via Quartz Unicode `CGEvent` (`keyboardSetUnicodeString`)
/// with clean modifier flags (`flags = []`), touching `NSPasteboard.general` **0 times** while speaking!
/// - Supports Provisional Live Words + Scalar-Safe Longest-Common-Prefix (LCP) Tail Rewrite so unfinalized words
/// appear immediately and smoothly transform into the exact final polished sentence when speech finishes.
/// - Batch Mode: Inserts the entire polished sentence at once upon key release, touching clipboard at most 1 time
/// (or 0 permanent times when `ClipboardBehavior.preserveOriginal` is selected).
public final class InputBoxManager {
public static let shared = InputBoxManager()
private let lock = NSLock()
private var accumulatedStreamedText: String = ""
private var pendingPrefixBuffer: String = ""
private var isPrefixResolved: Bool = false
private var needsNewlineBeforeNextDelta: Bool = false
private var sessionGeneration: UInt64 = 0
private var clipboardRestoreWorkItem: DispatchWorkItem?
private var hasInjectedAnyLiveText: Bool = false
// Accessed exclusively on serial `pasteQueue` (or reset via generation check)
private var displayedCharacters: [Character] = []
private var lastBackspaceRewriteTime: TimeInterval = 0
private var isExecutiveSlashSuppressed: Bool = false
private let pasteQueue = DispatchQueue(label: "com.soniox.pasteQueue", qos: .userInteractive)
private static let punctRegex: NSRegularExpression = {
return try! NSRegularExpression(pattern: #"[.,!?:;،؛؟"'()\[\]{}«»–—\-…]"#, options: [])
}()
// Combining marks / zero-width joiners stripped ONLY during provisional live preview so
// `1 Swift Character == 1 Backspace (kVK_Delete)` holds across 100% of macOS apps (AppKit, Chrome, Electron, Terminal).
private static let provisionalUnsafeScalars: CharacterSet = {
var set = CharacterSet.nonBaseCharacters
set.insert(charactersIn: "\u{200B}\u{200D}\u{FEFF}")
return set
}()
private init() {}
public static func stripPunctuation(_ str: String) -> String {
@ -24,412 +41,375 @@ public final class InputBoxManager {
return punctRegex.stringByReplacingMatches(in: str, options: [], range: range, withTemplate: "")
}
/// Reset session state at the beginning of speech
/// Converts provisional text into a strictly 1-character-per-backspace single-line representation.
public static func makeBackspaceSafeProvisional(_ text: String) -> String {
if text.isEmpty { return "" }
var out = String.UnicodeScalarView()
out.reserveCapacity(text.unicodeScalars.count)
var prevWasSpace = false
for scalar in text.unicodeScalars {
if scalar == "\u{200C}" || scalar == "\n" || scalar == "\r" || scalar == "\t" || scalar == "\u{00A0}" {
if !prevWasSpace {
out.append(" ")
prevWasSpace = true
}
continue
}
if provisionalUnsafeScalars.contains(scalar) {
continue
}
let isSpace = (scalar == " ")
if isSpace && prevWasSpace {
continue
}
prevWasSpace = isSpace
out.append(scalar)
}
return String(out)
}
/// Reset session state at the beginning of speech or on Escape cancel.
public func resetSession() {
lock.lock()
sessionGeneration &+= 1
accumulatedStreamedText = ""
pendingPrefixBuffer = ""
isPrefixResolved = false
needsNewlineBeforeNextDelta = false
clipboardRestoreWorkItem?.cancel()
clipboardRestoreWorkItem = nil
hasInjectedAnyLiveText = false
let gen = sessionGeneration
lock.unlock()
pasteQueue.async { [weak self] in
guard let self = self else { return }
self.lock.lock()
let current = self.sessionGeneration
self.lock.unlock()
if current == gen {
self.displayedCharacters.removeAll(keepingCapacity: true)
self.lastBackspaceRewriteTime = 0
self.isExecutiveSlashSuppressed = false
}
}
}
/// Returns true if any text has been streamed into the input box during this session
/// Returns true if any characters were typed live into the focused input box during this session.
public var hasStreamedText: Bool {
lock.lock()
defer { lock.unlock() }
let s = accumulatedStreamedText.trimmingCharacters(in: .whitespacesAndNewlines)
let p = pendingPrefixBuffer.trimmingCharacters(in: .whitespacesAndNewlines)
return !s.isEmpty || !p.isEmpty
return hasInjectedAnyLiveText
}
/// Prepares session state before recording starts.
/// STRICT RULE: Focus is 100% user-directed. Never steal focus to browser address bar or toolbar.
/// Prepares session state before recording starts without stealing focus or blocking on modal prompts.
public func ensureFocusedInputBox() {
resetSession()
let promptKey = kAXTrustedCheckOptionPrompt.takeUnretainedValue() as String
let isTrusted = AXIsProcessTrustedWithOptions([promptKey: true] as CFDictionary)
guard isTrusted else {
print("InputBoxManager: Accessibility not trusted.")
return
if !AXIsProcessTrusted() {
print("InputBoxManager: Accessibility permission not yet granted (Unicode typing / paste requires Accessibility).")
}
}
/// Streams word-by-word delta text directly into the focused input box in real time
public func streamDelta(text: String) {
guard !text.isEmpty else { return }
/// Updates live simultaneous typing in `.liveWordByWord` mode with ZERO clipboard usage.
/// - Parameters:
/// - fullCandidate: Confirmed tokens + current unfinalized hypothesis.
/// - confirmedPrefix: Strictly finalized (`is_final == true`) tokens only.
public func updateLiveTranscript(fullCandidate: String, confirmedPrefix: String) {
guard SonioxSettings.shared.typingDeliveryMode == .liveWordByWord else {
return
}
// Suppress trailing end triggers ("and", "تمام", "end") ONLY in hands-free/toggle mode, never in Push-To-Talk
if HotkeyManager.shared.currentMode != .pushToTalk {
let triggerWords = text.components(separatedBy: .whitespacesAndNewlines).filter { !$0.isEmpty }
if let lastRaw = triggerWords.last {
let last = InputBoxManager.stripPunctuation(lastRaw).lowercased()
if last == "and" || last == "تمام" || last == "end" {
if triggerWords.count <= 1 {
return
let engineMode = SonioxSettings.shared.liveStreamEngineMode
let rawSource: String
switch engineMode {
case .provisionalRewrite:
rawSource = fullCandidate
case .confirmedFinalsOnly:
// Hold back the last unfinished compound number/punctuation word until next confirmed word arrives
rawSource = Self.confirmedStableSlice(from: confirmedPrefix)
}
let trimmedCheck = fullCandidate.trimmingCharacters(in: .whitespacesAndNewlines)
guard !trimmedCheck.isEmpty else { return }
// Check Executive Slash Command holdback so "/chrome" or "اسلش چپ" is NEVER typed into the input box!
if SonioxSettings.shared.voiceCommands && !BotCommandProcessor.isBotCommand(text: trimmedCheck) {
let words = trimmedCheck.components(separatedBy: .whitespacesAndNewlines).filter { !$0.isEmpty }
if let first = words.first {
let cleanFirst = first.lowercased()
let slashTriggers = ["اسلش", "اصلش", "slash", "slsh", "/", "\\"]
let startsSlash = slashTriggers.contains(where: { cleanFirst.hasPrefix($0) })
if startsSlash && words.count < 2 {
// Wait for 2nd word before deciding whether it's an executive command or normal text
return
}
if VoiceCommandExecutor.shared.isSlashCommand(text: trimmedCheck) {
lock.lock()
let expectedGen = sessionGeneration
lock.unlock()
pasteQueue.async { [weak self] in
guard let self = self else { return }
self.lock.lock()
let valid = (self.sessionGeneration == expectedGen)
self.lock.unlock()
guard valid else { return }
if !self.displayedCharacters.isEmpty {
self.postBackspaces(self.displayedCharacters.count)
self.displayedCharacters.removeAll()
}
self.isExecutiveSlashSuppressed = true
}
let textWithoutEnd = triggerWords.dropLast().joined(separator: " ") + " "
streamDelta(text: textWithoutEnd)
return
}
}
}
// Apply lightweight inline formatting (domain vocabulary, voice punctuation, digits) so streamed words match final text
let normalizedDelta = TextProcessor.shared.processLiveChunk(text)
guard !rawSource.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else { return }
let formattedPreview = TextProcessor.shared.processLivePreview(rawSource)
let safeProvisional = Self.makeBackspaceSafeProvisional(formattedPreview)
guard !safeProvisional.isEmpty else { return }
lock.lock()
let expectedGen = sessionGeneration
if isPrefixResolved {
var textToPaste = normalizedDelta
if needsNewlineBeforeNextDelta {
needsNewlineBeforeNextDelta = false
let trimmed = normalizedDelta.trimmingCharacters(in: .whitespacesAndNewlines)
if !trimmed.isEmpty {
textToPaste = "\n" + trimmed
}
}
accumulatedStreamedText += textToPaste
lock.unlock()
lock.unlock()
pasteQueue.async { [weak self] in
guard let self = self else { return }
self.lock.lock()
let valid = (self.sessionGeneration == expectedGen)
self.lock.unlock()
guard valid, !self.isExecutiveSlashSuppressed else { return }
pasteQueue.async { [weak self] in
guard let self = self else { return }
self.lock.lock()
let valid = (self.sessionGeneration == expectedGen)
self.lock.unlock()
guard valid else { return }
self.pasteSynchronously(textToPaste)
let newChars = Array(safeProvisional)
if newChars == self.displayedCharacters {
return
}
return
}
// Not resolved yet: buffer to check if speech starts with a slash command
pendingPrefixBuffer += normalizedDelta
let trimmed = pendingPrefixBuffer.trimmingCharacters(in: .whitespacesAndNewlines)
let words = trimmed.components(separatedBy: .whitespacesAndNewlines).filter { !$0.isEmpty }
guard let firstWord = words.first else {
lock.unlock()
return
}
let slashTriggers = ["اسلش", "اصلش", "slash", "/", "\\"]
let isSlashPrefix = slashTriggers.contains { firstWord.lowercased().hasPrefix($0) }
if !isSlashPrefix {
// Normal speech: flush buffer immediately and continue normal streaming!
isPrefixResolved = true
let toFlush = pendingPrefixBuffer
pendingPrefixBuffer = ""
accumulatedStreamedText += toFlush
lock.unlock()
pasteQueue.async { [weak self] in
guard let self = self else { return }
self.lock.lock()
let valid = (self.sessionGeneration == expectedGen)
self.lock.unlock()
guard valid else { return }
self.pasteSynchronously(toFlush)
var lcp = 0
let limit = min(self.displayedCharacters.count, newChars.count)
while lcp < limit && self.displayedCharacters[lcp] == newChars[lcp] {
lcp += 1
}
return
}
// Starts with slash trigger: check if it matches a Bot Command (/steer, /queue, /cancel)
if let botRes = BotCommandProcessor.parse(text: trimmed) {
isPrefixResolved = true
var textToPaste = botRes.command
if !botRes.body.isEmpty {
textToPaste += "\n" + botRes.body
needsNewlineBeforeNextDelta = false
} else {
needsNewlineBeforeNextDelta = true
let eraseCount = self.displayedCharacters.count - lcp
if eraseCount > 0 {
// Throttle mid-speech backspace rewrites (~160ms) to avoid sub-syllable flicker while typing forward at 0ms!
let now = Date().timeIntervalSince1970
if (now - self.lastBackspaceRewriteTime) < 0.16 {
return
}
self.lastBackspaceRewriteTime = now
self.postBackspaces(eraseCount)
}
pendingPrefixBuffer = ""
accumulatedStreamedText = textToPaste
lock.unlock()
if lcp < newChars.count {
let deltaStr = String(newChars[lcp...])
self.typeUnicodeSynchronously(deltaStr)
}
pasteQueue.async { [weak self] in
guard let self = self else { return }
self.displayedCharacters = newChars
if !newChars.isEmpty {
self.lock.lock()
let valid = (self.sessionGeneration == expectedGen)
if self.sessionGeneration == expectedGen {
self.hasInjectedAnyLiveText = true
}
self.lock.unlock()
guard valid else { return }
self.pasteSynchronously(textToPaste)
}
return
}
// If 2 words have accumulated and it's NOT a bot command:
if words.count >= 2 {
isPrefixResolved = true
// If it's an executive voice command, do NOT stream it to the input box!
if SonioxSettings.shared.voiceCommands && VoiceCommandExecutor.shared.isSlashCommand(text: trimmed) {
pendingPrefixBuffer = ""
lock.unlock()
return
}
// Otherwise, it was just regular speech starting with "اسلش..."
let toFlush = pendingPrefixBuffer
pendingPrefixBuffer = ""
accumulatedStreamedText += toFlush
lock.unlock()
pasteQueue.async { [weak self] in
guard let self = self else { return }
self.lock.lock()
let valid = (self.sessionGeneration == expectedGen)
self.lock.unlock()
guard valid else { return }
self.pasteSynchronously(toFlush)
}
/// Legacy delta entry point kept for compatibility.
public func streamDelta(text: String) {
// Handled cleanly via `updateLiveTranscript(fullCandidate:confirmedPrefix:)`.
}
/// Holds back the trailing word in `.confirmedFinalsOnly` mode if it could be the first half of a
/// compound Persian number ("بیست" + "و پنج") or multi-word punctuation ("علامت" + "سوال").
private static func confirmedStableSlice(from confirmed: String) -> String {
let trimmed = confirmed.trimmingCharacters(in: .whitespacesAndNewlines)
guard !trimmed.isEmpty else { return "" }
let words = trimmed.components(separatedBy: .whitespacesAndNewlines).filter { !$0.isEmpty }
let holdbackTriggers: Set<String> = [
"بیست", "سی", "چهل", "پنجاه", "شصت", "هفتاد", "هشتاد", "نود",
"صد", "دویست", "سیصد", "چهارصد", "پانصد", "ششصد", "هفتصد", "هشتصد", "نهصد",
"علامت", "نقطه", "خط", "سطر", "سر", "دو", "سه", "گیومه", "پرانتز",
"question", "exclamation", "semi", "full", "new", "enter", "open", "close"
]
if let last = words.last {
let cleanLast = stripPunctuation(last).lowercased()
if holdbackTriggers.contains(cleanLast) {
if words.count <= 1 { return "" }
return words.dropLast().joined(separator: " ") + " "
}
return
}
lock.unlock()
return confirmed
}
/// Commits the final speech result: ensures 100% complete text in input box & clipboard without ANY duplication
/// Commits the final speech result:
/// - In Live Word-by-Word mode: smoothly rewrites only the differing tail from `displayedCharacters` to `finalOutput`
/// using pure Unicode keystrokes (0 `Cmd+V`), then copies full text ONCE to clipboard if configured.
/// - In Batch mode: inserts full polished text atomically in one shot, writing clipboard at most ONCE (or restoring original).
public func commitFinalText(fullText: String, addTrailingSpace: Bool) {
let trimmed = fullText.trimmingCharacters(in: .whitespacesAndNewlines)
guard !trimmed.isEmpty else { return }
let textWithTrailing = addTrailingSpace ? (trimmed + " ") : trimmed
let deliveryMode = SonioxSettings.shared.typingDeliveryMode
lock.lock()
let expectedGen = sessionGeneration
// Flush any pending buffer if recording ended before prefix resolution
if !isPrefixResolved && !pendingPrefixBuffer.isEmpty {
let pending = pendingPrefixBuffer
pendingPrefixBuffer = ""
if let botRes = BotCommandProcessor.parse(text: pending) {
accumulatedStreamedText = botRes.formattedText
} else if !(SonioxSettings.shared.voiceCommands && VoiceCommandExecutor.shared.isSlashCommand(text: pending)) {
accumulatedStreamedText += pending
}
}
let rawStreamed = accumulatedStreamedText
accumulatedStreamedText = ""
lock.unlock()
// CRITICAL: Process the streamed text through the EXACT same TextProcessor pipeline as fullText!
// This guarantees that numbers (۱ -> 1), ellipses (... -> . . .), and punctuation tokens have identical word counts & tokens!
let processedStreamed = rawStreamed.isEmpty ? "" : TextProcessor.shared.process(text: rawStreamed)
let cleanStreamed = processedStreamed.trimmingCharacters(in: .whitespacesAndNewlines)
let streamedEndedWithSpace = rawStreamed.unicodeScalars.last.map { CharacterSet.whitespacesAndNewlines.contains($0) } ?? false
pasteQueue.async { [weak self] in
guard let self = self else { return }
self.lock.lock()
let valid = (self.sessionGeneration == expectedGen)
self.hasInjectedAnyLiveText = false
self.lock.unlock()
guard valid else { return }
let restoreClipboardFullText = { [weak self] in
guard let self = self else { return }
let work = DispatchWorkItem { [weak self] in
guard let self = self else { return }
self.lock.lock()
let stillValid = (self.sessionGeneration == expectedGen)
self.lock.unlock()
guard stillValid else { return }
let pb = NSPasteboard.general
pb.clearContents()
pb.setString(textWithTrailing, forType: .string)
guard !trimmed.isEmpty else {
// Speech resolved to empty (e.g. stripped trigger): erase any provisional characters cleanly
if !self.displayedCharacters.isEmpty {
self.postBackspaces(self.displayedCharacters.count)
self.displayedCharacters.removeAll()
}
self.lock.lock()
self.clipboardRestoreWorkItem?.cancel()
self.clipboardRestoreWorkItem = work
self.lock.unlock()
DispatchQueue.main.asyncAfter(deadline: .now() + 0.30, execute: work)
}
// Case 1: Nothing was streamed live -> Paste entire text
if cleanStreamed.isEmpty {
self.pasteSynchronously(textWithTrailing)
restoreClipboardFullText()
return
}
// Case 2: Exactly matches streamed text
if cleanStreamed == trimmed {
if addTrailingSpace && !streamedEndedWithSpace {
self.pasteSynchronously(" ")
}
restoreClipboardFullText()
return
}
let finalOutput = addTrailingSpace ? (trimmed + " ") : trimmed
// Case 3: Streamed prefix is exact prefix of trimmed final text
if trimmed.count > cleanStreamed.count && trimmed.hasPrefix(cleanStreamed) {
let rawSuffix = String(trimmed.dropFirst(cleanStreamed.count))
let cleanSuffix = rawSuffix.trimmingCharacters(in: .whitespacesAndNewlines)
if !cleanSuffix.isEmpty {
let needsLeadingSpace = !streamedEndedWithSpace && (rawSuffix.first?.isWhitespace == true)
let suffixToPaste = (needsLeadingSpace ? " " : "") + cleanSuffix + (addTrailingSpace ? " " : "")
self.pasteSynchronously(suffixToPaste)
} else if addTrailingSpace && !streamedEndedWithSpace {
self.pasteSynchronously(" ")
// PATH A: Live text was already typed on screen -> Minimal LCP Tail Rewrite via Unicode Keystrokes (0 Cmd+V!)
if !self.displayedCharacters.isEmpty {
let finalChars = Array(finalOutput)
var lcp = 0
let limit = min(self.displayedCharacters.count, finalChars.count)
while lcp < limit && self.displayedCharacters[lcp] == finalChars[lcp] {
lcp += 1
}
let eraseCount = self.displayedCharacters.count - lcp
if eraseCount > 0 {
self.postBackspaces(eraseCount)
}
restoreClipboardFullText()
if lcp < finalChars.count {
let suffixToType = String(finalChars[lcp...])
self.typeUnicodeSynchronously(suffixToType)
}
self.displayedCharacters.removeAll(keepingCapacity: true)
self.lastBackspaceRewriteTime = 0
self.isExecutiveSlashSuppressed = false
// Record complete final text on clipboard at most ONCE (or 0 times if preserveOriginal)
ClipboardGuard.shared.commitFinalClipboardRecordIfDesired(text: trimmed, deliveryMode: .liveWordByWord)
return
}
// Case 4: Word-level Tail-Anchor Matching with 1-to-1 Aligned Index Mapping
let rawStreamedWords = cleanStreamed.components(separatedBy: .whitespacesAndNewlines).filter { !$0.isEmpty }
let rawFinalWords = trimmed.components(separatedBy: .whitespacesAndNewlines).filter { !$0.isEmpty }
// PATH B: Nothing was typed yet (Batch Mode, or short utterance before live threshold)
self.isExecutiveSlashSuppressed = false
let cleanWord: (String) -> String = { w in
let noZwnj = w.replacingOccurrences(of: "\u{200C}", with: "").replacingOccurrences(of: "\u{200D}", with: "")
return InputBoxManager.stripPunctuation(noZwnj).lowercased()
// If user is still physically holding a modifier key (e.g. Option), avoid Cmd+V modifier collision
// by injecting short/medium text via pure modifier-clean Unicode events!
if HotkeyManager.shared.isPhysicalKeyDown && finalOutput.count <= 240 {
self.typeUnicodeSynchronously(finalOutput)
ClipboardGuard.shared.commitFinalClipboardRecordIfDesired(text: trimmed, deliveryMode: deliveryMode)
return
}
// Keep ONLY words that contain actual alphanumeric/Persian letters so standalone punctuation (".", "...") NEVER shifts indices!
let streamedPairs: [(raw: String, norm: String)] = rawStreamedWords.compactMap { w in
let nw = cleanWord(w)
return nw.isEmpty ? nil : (w, nw)
}
let finalPairs: [(raw: String, norm: String)] = rawFinalWords.compactMap { w in
let nw = cleanWord(w)
return nw.isEmpty ? nil : (w, nw)
}
// Atomic Batch Paste (writes NSPasteboard at most ONCE, or snapshots+restores with TransientType)
let horizontalSpaceSet = CharacterSet(charactersIn: " \t")
let hasLeadingSpace = finalOutput.unicodeScalars.first.map { horizontalSpaceSet.contains($0) } ?? false
let hasTrailingSpace = finalOutput.unicodeScalars.last.map { horizontalSpaceSet.contains($0) } ?? false
let core = finalOutput.trimmingCharacters(in: horizontalSpaceSet)
guard !finalPairs.isEmpty else {
restoreClipboardFullText()
if core.isEmpty {
if hasLeadingSpace || hasTrailingSpace {
self.typeUnicodeSynchronously(" ")
}
return
}
let wordsMatch: (String, String) -> Bool = { w1, w2 in
if w1 == w2 { return true }
if w1.count >= 2 && w2.count >= 2 {
if w1.hasPrefix(w2) || w2.hasPrefix(w1) { return true }
}
return false
if hasLeadingSpace {
self.typeUnicodeSynchronously(" ")
}
var unstreamedPairIndex: Int? = nil
ClipboardGuard.shared.writeForAtomicPaste(textToPaste: core, cleanFinalText: trimmed)
self.postCleanCmdV()
usleep(25000)
// Strategy 4A: 3-word tail anchor
if streamedPairs.count >= 3 {
let a1 = streamedPairs[streamedPairs.count - 3].norm
let a2 = streamedPairs[streamedPairs.count - 2].norm
let a3 = streamedPairs[streamedPairs.count - 1].norm
for j in stride(from: finalPairs.count - 1, through: 2, by: -1) {
if wordsMatch(finalPairs[j].norm, a3) && wordsMatch(finalPairs[j - 1].norm, a2) && wordsMatch(finalPairs[j - 2].norm, a1) {
unstreamedPairIndex = j + 1
break
}
}
if hasTrailingSpace {
self.typeUnicodeSynchronously(" ")
}
// Strategy 4B: 2-word tail anchor
if unstreamedPairIndex == nil && streamedPairs.count >= 2 {
let a1 = streamedPairs[streamedPairs.count - 2].norm
let a2 = streamedPairs[streamedPairs.count - 1].norm
for j in stride(from: finalPairs.count - 1, through: 1, by: -1) {
if wordsMatch(finalPairs[j].norm, a2) && wordsMatch(finalPairs[j - 1].norm, a1) {
unstreamedPairIndex = j + 1
break
}
}
/// Types arbitrary Unicode text (Persian, ZWNJ, English, Arabic, emoji, multiline) directly via Quartz CGEvent
/// with `flags = []` so held hardware modifiers (Option/Fn/Control) never corrupt output and NSPasteboard is untouched.
private func typeUnicodeSynchronously(_ text: String) {
guard !text.isEmpty else { return }
let src = CGEventSource(stateID: .privateState)
let segments = text.components(separatedBy: "\n")
for (segIdx, segment) in segments.enumerated() {
if !segment.isEmpty {
let utf16 = Array(segment.utf16)
var idx = 0
let maxChunk = 16
while idx < utf16.count {
var end = min(idx + maxChunk, utf16.count)
// Never split a UTF-16 high/low surrogate pair across two CGEvents
if end < utf16.count && (0xD800...0xDBFF).contains(utf16[end - 1]) {
end -= 1
}
}
}
// Strategy 4C: 1-word tail anchor
if unstreamedPairIndex == nil && !streamedPairs.isEmpty {
let a1 = streamedPairs[streamedPairs.count - 1].norm
let searchStart = max(0, finalPairs.count - max(15, streamedPairs.count))
for j in stride(from: finalPairs.count - 1, through: searchStart, by: -1) {
if wordsMatch(finalPairs[j].norm, a1) {
unstreamedPairIndex = j + 1
break
let slice = Array(utf16[idx..<end])
if let down = CGEvent(keyboardEventSource: src, virtualKey: 0, keyDown: true),
let up = CGEvent(keyboardEventSource: src, virtualKey: 0, keyDown: false) {
down.flags = []
up.flags = []
slice.withUnsafeBufferPointer { buf in
guard let base = buf.baseAddress else { return }
down.keyboardSetUnicodeString(stringLength: buf.count, unicodeString: base)
up.keyboardSetUnicodeString(stringLength: buf.count, unicodeString: base)
}
down.post(tap: .cgAnnotatedSessionEventTap)
up.post(tap: .cgAnnotatedSessionEventTap)
}
}
}
// Strategy 4D: Forward prefix match fallback
if unstreamedPairIndex == nil {
var prefixMatchCount = 0
for i in 0..<min(streamedPairs.count, finalPairs.count) {
if wordsMatch(streamedPairs[i].norm, finalPairs[i].norm) {
prefixMatchCount = i + 1
} else {
break
idx = end
if idx < utf16.count {
usleep(2200)
}
}
if prefixMatchCount > 0 {
unstreamedPairIndex = prefixMatchCount
}
}
// Guaranteed Safety Fallback: aligned 1-to-1 on content-bearing pairs
if unstreamedPairIndex == nil {
unstreamedPairIndex = min(streamedPairs.count, finalPairs.count)
if segIdx < segments.count - 1 {
// Emit Shift+Return so multiline blocks (/steer\n..., "خط بعد") insert a clean line break
// without prematurely submitting chat messages in Telegram, Slack, Hermes, or browsers!
postShiftReturn(src: src)
usleep(4000)
}
if let startIndex = unstreamedPairIndex {
if startIndex < finalPairs.count {
let remainingWords = finalPairs.dropFirst(startIndex).map { $0.raw }
let suffix = remainingWords.joined(separator: " ")
let leadingSpace = streamedEndedWithSpace ? "" : " "
let trailingSpace = addTrailingSpace ? " " : ""
let suffixToPaste = leadingSpace + suffix + trailingSpace
self.pasteSynchronously(suffixToPaste)
restoreClipboardFullText()
return
} else {
if addTrailingSpace && !streamedEndedWithSpace {
self.pasteSynchronously(" ")
}
restoreClipboardFullText()
return
}
}
restoreClipboardFullText()
}
}
/// Places core word(s) on NSPasteboard via Cmd+V, while injecting leading/trailing spaces
/// via native Space keystrokes so apps that `.trim()` pasted clipboard text (like Hermes Desktop)
/// never glue words together or double-space!
private func pasteSynchronously(_ text: String) {
guard !text.isEmpty else { return }
let horizontalSpaceSet = CharacterSet(charactersIn: " \t")
let hasLeadingSpace = text.unicodeScalars.first.map { horizontalSpaceSet.contains($0) } ?? false
let hasTrailingSpace = text.unicodeScalars.last.map { horizontalSpaceSet.contains($0) } ?? false
let core = text.trimmingCharacters(in: horizontalSpaceSet)
if core.isEmpty {
if hasLeadingSpace || hasTrailingSpace {
postCleanSpace()
usleep(12000)
private func postBackspaces(_ count: Int) {
guard count > 0 else { return }
let src = CGEventSource(stateID: .privateState)
let deleteKeyCode: CGKeyCode = 0x33 // kVK_Delete
for i in 0..<count {
if let down = CGEvent(keyboardEventSource: src, virtualKey: deleteKeyCode, keyDown: true),
let up = CGEvent(keyboardEventSource: src, virtualKey: deleteKeyCode, keyDown: false) {
down.flags = [] // Explicit empty flags prevents Option+Delete deleting whole words!
up.flags = []
down.post(tap: .cgAnnotatedSessionEventTap)
up.post(tap: .cgAnnotatedSessionEventTap)
}
if (i + 1) % 8 == 0 {
usleep(2000)
} else {
usleep(700)
}
return
}
if hasLeadingSpace {
postCleanSpace()
usleep(12000)
}
let pb = NSPasteboard.general
pb.clearContents()
pb.setString(core, forType: .string)
postCleanCmdV()
usleep(22000) // 22ms guard ensures browser/app reads NSPasteboard before next chunk overwrites it (~45 chunks/sec capacity)
if hasTrailingSpace {
postCleanSpace()
usleep(12000)
}
private func postShiftReturn(src: CGEventSource?) {
let returnKeyCode: CGKeyCode = 0x24 // kVK_Return
if let down = CGEvent(keyboardEventSource: src, virtualKey: returnKeyCode, keyDown: true),
let up = CGEvent(keyboardEventSource: src, virtualKey: returnKeyCode, keyDown: false) {
down.flags = .maskShift
up.flags = .maskShift
down.post(tap: .cgAnnotatedSessionEventTap)
up.post(tap: .cgAnnotatedSessionEventTap)
}
}
@ -445,20 +425,4 @@ public final class InputBoxManager {
up.post(tap: .cgAnnotatedSessionEventTap)
}
}
private func postCleanSpace() {
let src = CGEventSource(stateID: .privateState)
let spaceKeyCode: CGKeyCode = 0x31 // ' ' (kVK_Space)
var spaceChar: UniChar = 0x0020
if let down = CGEvent(keyboardEventSource: src, virtualKey: spaceKeyCode, keyDown: true),
let up = CGEvent(keyboardEventSource: src, virtualKey: spaceKeyCode, keyDown: false) {
down.flags = []
up.flags = []
down.keyboardSetUnicodeString(stringLength: 1, unicodeString: &spaceChar)
up.keyboardSetUnicodeString(stringLength: 1, unicodeString: &spaceChar)
down.post(tap: .cgAnnotatedSessionEventTap)
up.post(tap: .cgAnnotatedSessionEventTap)
}
}
}

273
src/SonioxClient.swift

@ -41,6 +41,7 @@ public final class SonioxLiveSession {
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
@ -55,6 +56,7 @@ public final class SonioxLiveSession {
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
@ -63,6 +65,7 @@ public final class SonioxLiveSession {
// 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)?
@ -211,9 +214,17 @@ public final class SonioxLiveSession {
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)).")
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)
@ -269,13 +280,17 @@ public final class SonioxLiveSession {
self.isConnected = true
self.hasNetworkFailure = false
self.lastDataTime = Date()
let bufferedChunks = self.initialAudioBuffer
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 \(bufferedChunks.count) buffered chunks...")
for chunk in bufferedChunks {
newTask.send(.data(chunk)) { _ in }
print("SonioxLiveSession: ✅ Fallback endpoint connected! Flushing \(bufferedBytes) bytes of buffered speech...")
if hasBuffered {
self.flushReplayBufferToSocket(task: newTask, offset: 0)
}
} else {
self.hasNetworkFailure = true
@ -436,15 +451,21 @@ public final class SonioxLiveSession {
private func flushReplayBufferToSocket(task: URLSessionWebSocketTask, offset: Int) {
lock.lock()
guard isConnected, !isClosed, !isAborted, !isFinalizing, webSocketTask === task else {
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
@ -554,7 +575,11 @@ public final class SonioxLiveSession {
}
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 {
@ -563,42 +588,47 @@ public final class SonioxLiveSession {
return
}
print("SonioxLiveSession: Sending finalize packet...")
// 1. Send trailing 300ms (9600 bytes at 16kHz 16-bit PCM) silence frame
// so acoustic CTC model cleanly flushes and resolves trailing fast speech syllables
let silenceChunk = Data(count: 9600)
let silenceMsg = URLSessionWebSocketTask.Message.data(silenceChunk)
task.send(silenceMsg) { _ in }
// 2. Send finalize JSON
let finalizeMsg = URLSessionWebSocketTask.Message.string("{\"type\": \"finalize\"}")
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()
}
if flushing {
print("SonioxLiveSession: Waiting for initial/replay buffer flush to finish before sending finalize packet...")
} else {
sendFinalizeFramesOnTask(task)
}
// 3. Send official "END" token for complete Soniox termination
let endMsg = URLSessionWebSocketTask.Message.string("END")
task.send(endMsg) { _ in }
// Safety timeout fallback: allow up to 4.0s for STT inference & network transit
DispatchQueue.global().asyncAfter(deadline: .now() + 4.0) { [weak self] in
// Safety timeout fallback: allow up to 4.5s for STT inference & network transit
DispatchQueue.global().asyncAfter(deadline: .now() + 4.5) { [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 (4.0s) reached — completing.")
print("SonioxLiveSession: Finalize timeout (4.5s) 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
@ -725,15 +755,25 @@ public final class SonioxLiveSession {
combined = archivedCommittedText + (cleanSeg.isEmpty ? "" : (" " + cleanSeg))
}
// Check for in-flight end trigger ("and", "تمام", "end") ONLY in hands-free/toggle mode and ONLY on finalized tokens!
// Never trigger auto-stop during Push-To-Talk while user is physically holding the hotkey!
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 == "and" || cleanLast == "تمام" || cleanLast == "end" {
if cleanLast == "تمام" {
triggerDetected = true
}
}
@ -749,42 +789,16 @@ public final class SonioxLiveSession {
}()
let isTerminatingMessage = gotFin || sessionEnded || sessionDone || isFinished || hasRawFinished
let stillActivelyRecording = self.isRecordingActive && !self.isFinalizing
// Emit live streaming deltas in real time as the user speaks!
// We stream completed words in `combined` with a 1-word acoustic lookahead holdback so every emitted chunk ends with a trailing space.
var deltaToEmit: String? = nil
if !triggerDetected && !isTerminatingMessage && !self.isFinalizing {
let rTrimmedCombined = combined.replacingOccurrences(of: #"\s+$"#, with: "", options: .regularExpression)
var stableCandidate = ""
if let lastSpaceRange = rTrimmedCombined.rangeOfCharacter(from: .whitespacesAndNewlines, options: .backwards) {
stableCandidate = String(rTrimmedCombined[..<lastSpaceRange.upperBound])
}
if stableCandidate.count > streamedPrefix.count {
if stableCandidate.hasPrefix(streamedPrefix) {
let delta = String(stableCandidate.dropFirst(streamedPrefix.count))
streamedPrefix = stableCandidate
deltaToEmit = delta
} else {
let streamedWordsCount = streamedPrefix.components(separatedBy: .whitespacesAndNewlines).filter { !$0.isEmpty }.count
let candidateWords = stableCandidate.components(separatedBy: .whitespacesAndNewlines).filter { !$0.isEmpty }
if candidateWords.count > streamedWordsCount {
let delta = candidateWords.dropFirst(streamedWordsCount).joined(separator: " ") + " "
streamedPrefix = stableCandidate
deltaToEmit = delta
}
}
}
}
let shouldEmitLiveUpdate = !triggerDetected && !isTerminatingMessage && !self.isFinalizing && !combined.isEmpty
lock.unlock()
if triggerDetected {
DispatchQueue.main.async { [weak self] in
self?.onEndTriggerDetected?()
}
} else if let delta = deltaToEmit, !delta.isEmpty {
} else if shouldEmitLiveUpdate {
DispatchQueue.main.async { [weak self] in
self?.onCommittedDelta?(delta)
self?.onLiveTranscriptUpdate?(combined, confirmedCombined)
}
}
@ -799,6 +813,9 @@ public final class SonioxLiveSession {
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()
}
}
@ -813,6 +830,9 @@ public final class SonioxLiveSession {
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")
@ -847,8 +867,8 @@ public final class SonioxLiveSession {
return
}
let tailCount = max(0, currentSegmentAudioBuffer.count - lastConfirmedWordAudioByteOffset)
let hadDrop = hasNetworkFailure || hasSilentStall || isReconnectingMidRecording || !isConnected
let needsTailRecovery = hadDrop && tailCount > 11200
let hadDrop = hasNetworkFailure || hasSilentStall || isReconnectingMidRecording || !isConnected || (isFinalizing && !didReceiveTermination)
let needsTailRecovery = hadDrop && tailCount > 4800
isClosed = true
isReconnectingMidRecording = false
@ -896,10 +916,12 @@ public final class SonioxLiveSession {
isFinalizing = false
isReconnectingMidRecording = false
isFlushingReplayBuffer = false
pendingFinalizeAfterFlush = false
initialAudioBuffer.removeAll()
currentSegmentAudioBuffer.removeAll()
onPartialText = nil
onCommittedDelta = nil
onLiveTranscriptUpdate = nil
onEndTriggerDetected = nil
onFinalResult = nil
onConnectionStateChanged = nil
@ -1363,44 +1385,19 @@ public final class SonioxLiveSession {
let words = trimmed.components(separatedBy: .whitespacesAndNewlines).filter { !$0.isEmpty }
if words.isEmpty { return "" }
func matches(_ regex: NSRegularExpression, in str: String) -> Bool {
let range = NSRange(location: 0, length: str.utf16.count)
return regex.firstMatch(in: str, options: [], range: range) != nil
// 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 ""
}
var faCount = 0
var enCount = 0
for w in words {
if matches(faRegex, in: w) { faCount += 1 }
if matches(enRegex, in: w) { enCount += 1 }
}
let total = faCount + enCount
if total == 0 { return trimmed }
let faRatio = Double(faCount) / Double(total)
var cleaned: [String] = []
let stopWords: Set<String> = ["sex", "from", "no", "oh", "you", "all", "know", "that", "god", "loves", "men", "for", "day", "night", "mankind", "wanted", "moments"]
if faRatio >= 0.25 {
for w in words {
if matches(enRegex, in: w) && !matches(faRegex, in: w) {
let cleanW = w.lowercased().trimmingCharacters(in: punctCharSet)
// Strip ONLY known acoustic noise hallucinations; preserve legitimate English technical terms (Soniox, Voice, Sync, Mac, Push, Talk, API, etc.)
if stopWords.contains(cleanW) { continue }
}
cleaned.append(w)
}
} else {
cleaned = words
}
return cleaned.joined(separator: " ").trimmingCharacters(in: .whitespacesAndNewlines)
return words.joined(separator: " ").trimmingCharacters(in: .whitespacesAndNewlines)
}
}
// MARK: - Pre-warmed Session Pool
// MARK: - On-Demand Session Factory (Zero Idle Key Burn + Non-Blocking Acquisition)
public final class SonioxSessionPool {
public static let shared = SonioxSessionPool()
@ -1408,25 +1405,18 @@ public final class SonioxSessionPool {
private var prewarmedSession: SonioxLiveSession?
private var prewarmedSessionCreatedAt: Date = Date()
private let lock = NSLock()
private var isPrewarming = false
private init() {
prewarmNextSession()
setupSleepWakeObservers()
}
private func setupSleepWakeObservers() {
NSWorkspace.shared.notificationCenter.addObserver(forName: NSWorkspace.willSleepNotification, object: nil, queue: .main) { [weak self] _ in
print("SonioxSessionPool: Mac entering sleep. Discarding pre-warmed session.")
self?.discardPrewarmedSession()
}
NSWorkspace.shared.notificationCenter.addObserver(forName: NSWorkspace.didWakeNotification, object: nil, queue: .main) { [weak self] _ in
print("SonioxSessionPool: Mac woke from sleep. Re-priming fresh session in 2.0s...")
self?.discardPrewarmedSession()
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
self?.prewarmNextSession()
}
}
}
@ -1435,66 +1425,20 @@ public final class SonioxSessionPool {
prewarmedSession?.stopKeepAliveHeartbeat()
prewarmedSession?.abort()
prewarmedSession = nil
isPrewarming = false
lock.unlock()
}
public func prewarmNextSession() {
guard PingMonitor.shared.isPathSatisfied else { return }
lock.lock()
defer { lock.unlock() }
if let existing = prewarmedSession, existing.isReady {
let age = Date().timeIntervalSince(prewarmedSessionCreatedAt)
if age < 45.0 {
return
}
}
if isPrewarming { return }
isPrewarming = true
prewarmedSession?.stopKeepAliveHeartbeat()
prewarmedSession?.abort()
prewarmedSession = nil
DispatchQueue.global(qos: .userInitiated).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()
defer { self.lock.unlock() }
if connected {
session.startKeepAliveHeartbeat()
self.prewarmedSession = session
self.prewarmedSessionCreatedAt = Date()
self.isPrewarming = false
print("SonioxSessionPool: Fresh prewarmed session connected & ready.")
} else {
self.isPrewarming = false
}
}
DispatchQueue.global().asyncAfter(deadline: .now() + 12.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()
}
}
// 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!
}
public func reconnectAll() {
discardPrewarmedSession()
prewarmNextSession()
}
/// Non-blocking session creation (0ms main-thread wait!).
public func acquireSession() -> SonioxLiveSession {
lock.lock()
let session = prewarmedSession
@ -1502,34 +1446,15 @@ public final class SonioxSessionPool {
prewarmedSession = nil
lock.unlock()
DispatchQueue.global(qos: .userInitiated).async { [weak self] in
self?.prewarmNextSession()
}
let age = Date().timeIntervalSince(createdAt)
if let session = session, session.isReady && age < 60.0 {
if let session = session, session.isReady && age < 12.0 {
session.stopKeepAliveHeartbeat()
print("SonioxSessionPool: Acquired HOT prewarmed session (age: \(String(format: "%.1f", age))s, 0ms connect delay!)")
return session
}
// If prewarmed session is connecting or slightly aged, wait up to 0.4s for it before aborting
if let session = session, !session.isReady && age < 15.0 {
let tStart = Date()
while Date().timeIntervalSince(tStart) < 0.40 {
if session.isReady {
session.stopKeepAliveHeartbeat()
print("SonioxSessionPool: Acquired session after \(Int(Date().timeIntervalSince(tStart)*1000))ms connection wait.")
return session
}
usleep(25000) // 25ms
}
}
session?.stopKeepAliveHeartbeat()
session?.abort()
print("SonioxSessionPool: Prewarmed session not ready/stale (age: \(String(format: "%.1f", age))s), creating immediate on-demand session.")
let hints = SonioxSettings.shared.language.hints
let target = SonioxSettings.shared.targetLanguage.isEnabled ? SonioxSettings.shared.targetLanguage.rawValue : nil
return SonioxLiveSession(languageHints: hints, targetLanguage: target)

82
src/SonioxSettings.swift

@ -120,6 +120,51 @@ public enum NumberFormatOption: String, CaseIterable {
}
}
public enum TypingDeliveryMode: String, CaseIterable {
case liveWordByWord = "liveWordByWord"
case batchOnRelease = "batchOnRelease"
public var displayName: String {
switch self {
case .liveWordByWord:
return "⚡️ Live Word-by-Word (Simultaneous Typing)"
case .batchOnRelease:
return "📦 Full Sentence on Release (After Speaking)"
}
}
}
public enum LiveStreamEngineMode: String, CaseIterable {
case provisionalRewrite = "provisionalRewrite"
case confirmedFinalsOnly = "confirmedFinalsOnly"
public var displayName: String {
switch self {
case .provisionalRewrite:
return "✨ Instant Live Words + Auto-Polish on Finish (Recommended)"
case .confirmedFinalsOnly:
return "🔒 Confirmed Final Words Only (No Rewrite)"
}
}
}
public enum ClipboardBehavior: String, CaseIterable {
case copyFinalOnce = "copyFinalOnce"
case preserveOriginal = "preserveOriginal"
case copyInBatchOnly = "copyInBatchOnly"
public var displayName: String {
switch self {
case .copyFinalOnce:
return "📋 Copy Full Text Once at End (Default)"
case .preserveOriginal:
return "🛡️ Preserve Previous Clipboard (Zero History)"
case .copyInBatchOnly:
return "📦 Copy Only in Full-Sentence Mode"
}
}
}
public final class SonioxSettings {
public static let shared = SonioxSettings()
@ -216,10 +261,40 @@ public final class SonioxSettings {
public var liveStreamingInput: Bool {
get {
return defaults.bool(forKey: "SonioxLiveStreamingInput")
return typingDeliveryMode == .liveWordByWord
}
set {
typingDeliveryMode = newValue ? .liveWordByWord : .batchOnRelease
}
}
public var typingDeliveryMode: TypingDeliveryMode {
get {
let val = defaults.string(forKey: "SonioxTypingDeliveryMode") ?? TypingDeliveryMode.liveWordByWord.rawValue
return TypingDeliveryMode(rawValue: val) ?? .liveWordByWord
}
set {
defaults.set(newValue.rawValue, forKey: "SonioxTypingDeliveryMode")
}
}
public var liveStreamEngineMode: LiveStreamEngineMode {
get {
let val = defaults.string(forKey: "SonioxLiveStreamEngineMode") ?? LiveStreamEngineMode.provisionalRewrite.rawValue
return LiveStreamEngineMode(rawValue: val) ?? .provisionalRewrite
}
set {
defaults.set(newValue.rawValue, forKey: "SonioxLiveStreamEngineMode")
}
}
public var clipboardBehavior: ClipboardBehavior {
get {
let val = defaults.string(forKey: "SonioxClipboardBehavior") ?? ClipboardBehavior.copyFinalOnce.rawValue
return ClipboardBehavior(rawValue: val) ?? .copyFinalOnce
}
set {
defaults.set(newValue, forKey: "SonioxLiveStreamingInput")
defaults.set(newValue.rawValue, forKey: "SonioxClipboardBehavior")
}
}
@ -266,6 +341,9 @@ public final class SonioxSettings {
defaults.removeObject(forKey: "SonioxDomainVocabulary")
defaults.removeObject(forKey: "SonioxAddTrailingSpace")
defaults.removeObject(forKey: "SonioxNormalizeText")
defaults.removeObject(forKey: "SonioxTypingDeliveryMode")
defaults.removeObject(forKey: "SonioxLiveStreamEngineMode")
defaults.removeObject(forKey: "SonioxClipboardBehavior")
defaults.removeObject(forKey: "SonioxPlaySounds")
defaults.removeObject(forKey: "SonioxLaunchAtLogin")
defaults.removeObject(forKey: "SonioxPreferredAudioDevice")

63
src/StatusBarController.swift

@ -88,6 +88,34 @@ public final class StatusBarController: NSObject, NSMenuDelegate {
modeMenuItem.submenu = modeMenu
menu.addItem(modeMenuItem)
// 2.5. Typing Delivery Mode Submenu (Live Word-by-Word vs Full Sentence on Release)
let deliveryMenu = NSMenu()
for delivery in TypingDeliveryMode.allCases {
let item = NSMenuItem(title: delivery.displayName, action: #selector(selectTypingDeliveryAction(_:)), keyEquivalent: "")
item.target = self
item.representedObject = delivery
item.state = (SonioxSettings.shared.typingDeliveryMode == delivery) ? .on : .off
deliveryMenu.addItem(item)
}
let currentDelivery = SonioxSettings.shared.typingDeliveryMode
let deliveryShort = (currentDelivery == .liveWordByWord) ? "Word-by-Word" : "On Release"
let deliveryMenuItem = NSMenuItem(title: "✍️ Typing Mode (\(deliveryShort))", action: nil, keyEquivalent: "")
deliveryMenuItem.submenu = deliveryMenu
menu.addItem(deliveryMenuItem)
// 2.6. Clipboard Behavior Submenu
let clipboardMenu = NSMenu()
for behavior in ClipboardBehavior.allCases {
let item = NSMenuItem(title: behavior.displayName, action: #selector(selectClipboardBehaviorAction(_:)), keyEquivalent: "")
item.target = self
item.representedObject = behavior
item.state = (SonioxSettings.shared.clipboardBehavior == behavior) ? .on : .off
clipboardMenu.addItem(item)
}
let clipboardMenuItem = NSMenuItem(title: "📋 Clipboard Behavior", action: nil, keyEquivalent: "")
clipboardMenuItem.submenu = clipboardMenu
menu.addItem(clipboardMenuItem)
// 3. Hotkey Preset Submenu
let hotkeyMenu = NSMenu()
for preset in HotkeyPreset.allCases {
@ -182,6 +210,20 @@ public final class StatusBarController: NSObject, NSMenuDelegate {
// 8. Advanced Options Submenu
let advancedMenu = NSMenu()
let streamEngineMenu = NSMenu()
for eng in LiveStreamEngineMode.allCases {
let item = NSMenuItem(title: eng.displayName, action: #selector(selectLiveStreamEngineAction(_:)), keyEquivalent: "")
item.target = self
item.representedObject = eng
item.state = (SonioxSettings.shared.liveStreamEngineMode == eng) ? .on : .off
streamEngineMenu.addItem(item)
}
let streamEngineMenuItem = NSMenuItem(title: "✨ Word-by-Word Stream Engine", action: nil, keyEquivalent: "")
streamEngineMenuItem.submenu = streamEngineMenu
advancedMenu.addItem(streamEngineMenuItem)
advancedMenu.addItem(NSMenuItem.separator())
let slashCmdItem = NSMenuItem(title: "⚡️ Executive Voice Slash Commands", action: #selector(toggleVoiceCommandsAction(_:)), keyEquivalent: "")
slashCmdItem.target = self
slashCmdItem.state = SonioxSettings.shared.voiceCommands ? .on : .off
@ -346,6 +388,27 @@ public final class StatusBarController: NSObject, NSMenuDelegate {
}
}
@objc private func selectTypingDeliveryAction(_ sender: NSMenuItem) {
if let delivery = sender.representedObject as? TypingDeliveryMode {
SonioxSettings.shared.typingDeliveryMode = delivery
buildMenu()
}
}
@objc private func selectClipboardBehaviorAction(_ sender: NSMenuItem) {
if let behavior = sender.representedObject as? ClipboardBehavior {
SonioxSettings.shared.clipboardBehavior = behavior
buildMenu()
}
}
@objc private func selectLiveStreamEngineAction(_ sender: NSMenuItem) {
if let eng = sender.representedObject as? LiveStreamEngineMode {
SonioxSettings.shared.liveStreamEngineMode = eng
buildMenu()
}
}
@objc private func selectHotkeyAction(_ sender: NSMenuItem) {
if let preset = sender.representedObject as? HotkeyPreset {
HotkeyManager.shared.currentPreset = preset

284
src/TextProcessor.swift

@ -3,17 +3,62 @@ import Foundation
public final class TextProcessor {
public static let shared = TextProcessor()
// Only strip explicit stop triggers ("تمام", "پایان دیکته", "stop dictation") — NEVER strip "and" or "end" in normal speech!
private static let trailingEndRegex: NSRegularExpression = {
return try! NSRegularExpression(pattern: #"[،,:\s]+(and|تمام|end)[.!?]?$"#, options: .caseInsensitive)
return try! NSRegularExpression(pattern: #"[،,:\s]+(تمام|پایان دیکته|stop dictation)[.!?؟]?$"#, options: .caseInsensitive)
}()
private static let singleEndRegex: NSRegularExpression = {
return try! NSRegularExpression(pattern: #"^(and|تمام|end)[.!?]?$"#, options: .caseInsensitive)
return try! NSRegularExpression(pattern: #"^(تمام|پایان دیکته|stop dictation)[.!?؟]?$"#, options: .caseInsensitive)
}()
// Context-aware regexes (100% fixed-length ICU lookbehinds!) so content words
// ("نقطه ضعف", "این نقطه", "در خط بعد", "دکمه اینتر", "period of") are NEVER corrupted,
// while words ending in similar letters ("پایین نقطه" -> "پایین.") STILL convert properly!
private static let persianDotSafeRegex: NSRegularExpression = {
let guards = [
"(?<! این)", "(?<! آن)", "(?<! اون)", "(?<! همین)", "(?<! همان)",
"(?<! یک)", "(?<! یه)", "(?<! هر)", "(?<! چند)", "(?<! سر)"
].joined()
let pattern = guards + #" نقطه(?! (?:ضعف|قوت|نظر|عطف|شروع|پایان|اوج|کور|جوش|ذوب|اتصال|مقابل|عزیمت|ای\b|ها\b|های\b|گذاری|چین))(?= |$|[.،؛؟!:)])"#
return try! NSRegularExpression(pattern: pattern, options: [])
}()
private static let persianNewlineSafeRegex: NSRegularExpression = {
let guards = [
"(?<! در)", "(?<! توی)", "(?<! تو)", "(?<! از)",
"(?<! به)", "(?<! تا)", "(?<! روی)"
].joined()
let pattern = guards + #" (?:خط بعد|سطر بعد|سر خط|برو خط بعد)(?!ی)(?= |$|[.،؛؟!:])"#
return try! NSRegularExpression(pattern: pattern, options: [])
}()
private static let persianEnterSafeRegex: NSRegularExpression = {
let pattern = #"(?<! دکمه)(?<! کلید) اینتر(?= |$|[.،؛؟!:])"#
return try! NSRegularExpression(pattern: pattern, options: [])
}()
private static let englishPeriodSafeRegex: NSRegularExpression = {
let guards = [
"(?<! a)", "(?<! the)", "(?<! this)", "(?<! that)",
"(?<! short)", "(?<! long)", "(?<! time)"
].joined()
let pattern = guards + #" period(?! of\b)(?= |$|[.,!?:;)])"#
return try! NSRegularExpression(pattern: pattern, options: .caseInsensitive)
}()
private static let dotNotDecimalRegex: NSRegularExpression = {
return try! NSRegularExpression(pattern: #"(\.)(?![0-9۰-۹\s\.])"#, options: [])
}()
private init() {}
public func process(text: String) -> String {
let isPTT = (HotkeyManager.shared.currentMode == .pushToTalk)
return process(text: text, isPushToTalk: isPTT)
}
public func process(text: String, isPushToTalk: Bool) -> String {
var result = text.trimmingCharacters(in: .whitespacesAndNewlines)
if result.isEmpty { return "" }
@ -23,7 +68,7 @@ public final class TextProcessor {
if !botCmd.body.isEmpty {
var bodyText = botCmd.body
if SonioxSettings.shared.voicePunctuation {
bodyText = applyVoicePunctuation(bodyText)
bodyText = applyVoicePunctuation(bodyText, allowNewlines: true)
}
if SonioxSettings.shared.domainVocabulary {
bodyText = applyDomainVocabulary(bodyText)
@ -32,6 +77,9 @@ public final class TextProcessor {
if SonioxSettings.shared.normalizeText {
bodyText = normalizeText(bodyText)
}
if !isPushToTalk {
bodyText = stripTrailingEndTrigger(bodyText)
}
processedBody = bodyText.trimmingCharacters(in: .whitespacesAndNewlines)
} else {
processedBody = ""
@ -44,82 +92,107 @@ public final class TextProcessor {
}
}
// 1. Voice Punctuation Commands
// 1. Voice Punctuation Commands (Context-Aware)
if SonioxSettings.shared.voicePunctuation {
result = applyVoicePunctuation(result)
result = applyVoicePunctuation(result, allowNewlines: true)
}
// 2. Domain Vocabulary Enhancement
if SonioxSettings.shared.domainVocabulary {
result = applyDomainVocabulary(result)
}
// 3. Number Formatting
result = applyNumberFormatting(result, format: SonioxSettings.shared.numberFormat)
// 3.5. Sanitize foreign hallucinations & word loops
// 3.5. Sanitize silence noise artifacts
result = SonioxLiveSession.sanitizeText(result)
// 4. Text Normalization
if SonioxSettings.shared.normalizeText {
result = normalizeText(result)
}
// 5. Wake Word & End Trigger sanitization
result = stripTrailingEndTrigger(result)
return result.trimmingCharacters(in: .whitespacesAndNewlines)
// 5. End Trigger sanitization ONLY in Hands-Free / Toggle mode (never delete words in Push-To-Talk!)
if !isPushToTalk {
result = stripTrailingEndTrigger(result)
}
/// Lightweight inline formatter applied to live streaming chunks so words, domain terms,
/// and numbers match the final post-processed text in real time.
public func processLiveChunk(_ text: String) -> String {
if text.isEmpty { return "" }
let hadLeadingSpace = text.first?.isWhitespace == true
let hadTrailingSpace = text.last?.isWhitespace == true
var result = text
return result.trimmingCharacters(in: .whitespacesAndNewlines)
}
/// Formats the running live candidate consistently during simultaneous word-by-word typing.
/// Keeps output single-line (no `\n`) so live Backspace tail-rewrites are 100% deterministic across all apps;
/// newlines are applied cleanly at `commitFinalText`.
public func processLivePreview(_ text: String) -> String {
let trimmedInput = text.trimmingCharacters(in: .whitespacesAndNewlines)
if trimmedInput.isEmpty { return "" }
let hadTrailingSpace = text.unicodeScalars.last.map { CharacterSet.whitespaces.contains($0) } ?? false
if let botCmd = BotCommandProcessor.parse(text: trimmedInput) {
if botCmd.body.isEmpty {
return botCmd.command + (hadTrailingSpace ? " " : "")
}
var body = botCmd.body
if SonioxSettings.shared.voicePunctuation {
result = applyVoicePunctuation(result)
body = applyVoicePunctuation(body, allowNewlines: false)
}
if SonioxSettings.shared.domainVocabulary {
result = applyDomainVocabulary(result)
body = applyDomainVocabulary(body)
}
result = applyNumberFormatting(result, format: SonioxSettings.shared.numberFormat)
let resultHasLeadingSpace = result.first?.isWhitespace == true
let resultHasTrailingSpace = result.last?.isWhitespace == true
let trimmed = result.trimmingCharacters(in: .whitespaces)
if trimmed.isEmpty { return text }
let keepLeading = hadLeadingSpace || resultHasLeadingSpace
let keepTrailing = hadTrailingSpace || resultHasTrailingSpace
return (keepLeading ? " " : "") + trimmed + (keepTrailing ? " " : "")
body = applyNumberFormatting(body, format: SonioxSettings.shared.numberFormat)
if SonioxSettings.shared.normalizeText {
body = normalizeSingleLine(body)
}
return "\(botCmd.command) \(body)" + (hadTrailingSpace ? " " : "")
}
public func applyDomainVocabularyInline(_ text: String) -> String {
var result = trimmedInput
if SonioxSettings.shared.voicePunctuation {
result = applyVoicePunctuation(result, allowNewlines: false)
}
if SonioxSettings.shared.domainVocabulary {
result = applyDomainVocabulary(result)
}
result = applyNumberFormatting(result, format: SonioxSettings.shared.numberFormat)
result = SonioxLiveSession.sanitizeText(result)
if SonioxSettings.shared.normalizeText {
result = normalizeSingleLine(result)
}
if HotkeyManager.shared.currentMode != .pushToTalk {
result = stripTrailingEndTrigger(result)
}
let clean = result.trimmingCharacters(in: .whitespacesAndNewlines)
if clean.isEmpty { return "" }
return clean + (hadTrailingSpace ? " " : "")
}
/// Legacy chunk helper preserved for compatibility.
public func processLiveChunk(_ text: String) -> String {
return processLivePreview(text)
}
public func applyDomainVocabularyInline(_ text: String) -> String {
return applyDomainVocabulary(text)
}
private func applyVoicePunctuation(_ text: String) -> String {
private func applyVoicePunctuation(_ text: String, allowNewlines: Bool) -> String {
var t = " " + text + " "
while t.contains(" ") {
t = t.replacingOccurrences(of: " ", with: " ")
}
// Persian Voice Commands
// Unambiguous multi-word & symbol commands first
let persianMappings: [(String, String)] = [
(" علامت سوال", "؟"),
(" علامت سؤال", "؟"),
(" علامت پرسش", "؟"),
(" علامت تعجب", "!"),
(" نقطه ویرگول", "؛"),
(" خط بعد", "\n"),
(" سطر بعد", "\n"),
(" سر خط", "\n"),
(" اینتر", "\n"),
(" برو خط بعد", "\n"),
(" نقطه", "."),
(" ویرگول", "،"),
(" کاما", "،"),
(" دو نقطه", ":"),
(" سه نقطه", "..."),
(" ویرگول", "،"),
(" کاما", "،"),
(" گیومه باز", " «"),
(" گیومه بسته", "» "),
(" پرانتز باز", " ("),
@ -130,6 +203,29 @@ public final class TextProcessor {
t = t.replacingOccurrences(of: cmd, with: rep, options: .caseInsensitive)
}
// Context-guarded "نقطه" (protects "نقطه ضعف", "این نقطه", etc.)
t = Self.persianDotSafeRegex.stringByReplacingMatches(
in: t,
options: [],
range: NSRange(location: 0, length: t.utf16.count),
withTemplate: "."
)
if allowNewlines {
t = Self.persianNewlineSafeRegex.stringByReplacingMatches(
in: t,
options: [],
range: NSRange(location: 0, length: t.utf16.count),
withTemplate: "\n"
)
t = Self.persianEnterSafeRegex.stringByReplacingMatches(
in: t,
options: [],
range: NSRange(location: 0, length: t.utf16.count),
withTemplate: "\n"
)
}
// English Voice Commands
let englishMappings: [(String, String)] = [
(" question mark", "?"),
@ -137,12 +233,8 @@ public final class TextProcessor {
(" exclamation point", "!"),
(" semi colon", ";"),
(" semicolon", ";"),
(" new line", "\n"),
(" enter key", "\n"),
(" full stop", "."),
(" period", "."),
(" comma", ","),
(" colon", ":"),
(" ellipsis", "..."),
(" open quote", " \""),
(" close quote", "\" "),
@ -154,6 +246,18 @@ public final class TextProcessor {
t = t.replacingOccurrences(of: cmd, with: rep, options: .caseInsensitive)
}
if allowNewlines {
t = t.replacingOccurrences(of: " new line", with: "\n", options: .caseInsensitive)
t = t.replacingOccurrences(of: " enter key", with: "\n", options: .caseInsensitive)
}
t = Self.englishPeriodSafeRegex.stringByReplacingMatches(
in: t,
options: [],
range: NSRange(location: 0, length: t.utf16.count),
withTemplate: "."
)
return t
}
@ -174,6 +278,8 @@ public final class TextProcessor {
("سانی اوکس", "Soniox"),
("سانیوکس", "Soniox"),
("سانی اکس", "Soniox"),
("ثانیه ایکس", "Soniox"),
("ثانیه یکس", "Soniox"),
("تیم بای", "TeamBy"),
("تیمبی", "TeamBy"),
("دوودی", "Dovodi"),
@ -191,6 +297,7 @@ public final class TextProcessor {
("فلاتر", "Flutter"),
("وایارگارد", "WireGuard"),
("وایرگارد", "WireGuard"),
("پروتون وی پی ان", "ProtonVPN"),
("میکروتیک", "MikroTik"),
("هرمس", "Hermes"),
("نکست جی اس", "Next.js"),
@ -276,43 +383,54 @@ public final class TextProcessor {
}
}
private func normalizeSingleLine(_ text: String) -> String {
let collapsed = text.replacingOccurrences(of: "\n", with: " ").replacingOccurrences(of: "\r", with: " ")
return normalizeText(collapsed)
}
private func normalizeText(_ text: String) -> String {
var t = text
while t.contains(" ") {
t = t.replacingOccurrences(of: " ", with: " ")
}
let punctuationList = [".", "،", "؛", "؟", "!", ":", ")", "»"]
for p in punctuationList {
t = t.replacingOccurrences(of: " " + p, with: p)
}
t = t.replacingOccurrences(of: "...", with: "…")
for p in ["،", "؛", "؟", "!", "…"] {
t = t.replacingOccurrences(of: p, with: p + " ")
}
// Only add space after "." when it is NOT between two digits (preserve 4.3, 3.8, IP addresses)
let dotNotDecimal = try! NSRegularExpression(pattern: #"(\.)(?![0-9۰-۹\s\.])"#, options: [])
t = dotNotDecimal.stringByReplacingMatches(in: t, options: [], range: NSRange(location: 0, length: t.utf16.count), withTemplate: ". ")
t = t.replacingOccurrences(of: "…", with: "...")
let speechFixes: [(String, String)] = [
(" ماا ", " ما "),
(" اینپیگیری ", " این پیگیری "),
(" می‌کنکنیم ", " می‌کنیم "),
(" سر سمانش ", " سر و سامانش "),
(" دست و پا گیرلی ", " دست و پا گیر ولی ")
]
for (bad, good) in speechFixes {
t = t.replacingOccurrences(of: bad, with: good)
}
t = t.replacingOccurrences(of: "( ", with: "(")
t = t.replacingOccurrences(of: "« ", with: "«")
while t.contains(" ") {
t = t.replacingOccurrences(of: " ", with: " ")
// Normalize line-by-line so explicit `\n` line breaks are preserved!
let lines = text.components(separatedBy: "\n")
let normalizedLines = lines.map { rawLine -> String in
var t = rawLine
while t.contains(" ") {
t = t.replacingOccurrences(of: " ", with: " ")
}
let punctuationList = [".", "،", "؛", "؟", "!", ":", ")", "»"]
for p in punctuationList {
t = t.replacingOccurrences(of: " " + p, with: p)
}
t = t.replacingOccurrences(of: "...", with: "…")
for p in ["،", "؛", "؟", "!", "…"] {
t = t.replacingOccurrences(of: p, with: p + " ")
}
t = Self.dotNotDecimalRegex.stringByReplacingMatches(
in: t,
options: [],
range: NSRange(location: 0, length: t.utf16.count),
withTemplate: ". "
)
t = t.replacingOccurrences(of: "…", with: "...")
let speechFixes: [(String, String)] = [
(" ماا ", " ما "),
(" اینپیگیری ", " این پیگیری "),
(" می‌کنکنیم ", " می‌کنیم "),
(" سر سمانش ", " سر و سامانش "),
(" دست و پا گیرلی ", " دست و پا گیر ولی ")
]
for (bad, good) in speechFixes {
t = t.replacingOccurrences(of: bad, with: good)
}
t = t.replacingOccurrences(of: "( ", with: "(")
t = t.replacingOccurrences(of: "« ", with: "«")
while t.contains(" ") {
t = t.replacingOccurrences(of: " ", with: " ")
}
return t.trimmingCharacters(in: .whitespaces)
}
return t.trimmingCharacters(in: .whitespacesAndNewlines)
return normalizedLines.joined(separator: "\n").trimmingCharacters(in: .whitespacesAndNewlines)
}
public func stripTrailingEndTrigger(_ text: String) -> String {
var t = text.trimmingCharacters(in: .whitespacesAndNewlines)
let range1 = NSRange(location: 0, length: t.utf16.count)

Loading…
Cancel
Save