import Cocoa import ApplicationServices 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 let pasteQueue = DispatchQueue(label: "com.soniox.pasteQueue", qos: .userInteractive) private static let punctRegex: NSRegularExpression = { return try! NSRegularExpression(pattern: #"[.,!?:;،؛؟"'()\[\]{}«»–—\-…]"#, options: []) }() private init() {} public static func stripPunctuation(_ str: String) -> String { let range = NSRange(location: 0, length: str.utf16.count) return punctRegex.stringByReplacingMatches(in: str, options: [], range: range, withTemplate: "") } /// Reset session state at the beginning of speech public func resetSession() { lock.lock() sessionGeneration &+= 1 accumulatedStreamedText = "" pendingPrefixBuffer = "" isPrefixResolved = false needsNewlineBeforeNextDelta = false clipboardRestoreWorkItem?.cancel() clipboardRestoreWorkItem = nil lock.unlock() } /// Returns true if any text has been streamed into the 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 } /// Prepares session state before recording starts. /// STRICT RULE: Focus is 100% user-directed. Never steal focus to browser address bar or toolbar. 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 } } /// 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 } // Suppress trailing end triggers ("and", "تمام", "end") from live typing 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 // Drop completely if it was just the end word } 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) 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() 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) } 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) } 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 } pendingPrefixBuffer = "" accumulatedStreamedText = textToPaste 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(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) } return } lock.unlock() } /// Commits the final speech result: ensures 100% complete text in input box & clipboard without ANY duplication public func commitFinalText(fullText: String, addTrailingSpace: Bool) { let trimmed = fullText.trimmingCharacters(in: .whitespacesAndNewlines) guard !trimmed.isEmpty else { return } let textWithTrailing = addTrailingSpace ? (trimmed + " ") : trimmed 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) 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 } 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) } 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 { let pb = NSPasteboard.general pb.clearContents() pb.setString(textWithTrailing, forType: .string) self.postCleanCmdV() restoreClipboardFullText() return } // Case 2: Exactly matches streamed text if cleanStreamed == trimmed { if addTrailingSpace { self.pasteSynchronously(" ") } restoreClipboardFullText() return } // Case 3: Streamed prefix is exact prefix of trimmed final text if trimmed.count > cleanStreamed.count && trimmed.hasPrefix(cleanStreamed) { let suffix = String(trimmed.dropFirst(cleanStreamed.count)) let suffixToPaste = addTrailingSpace ? (suffix + " ") : suffix self.pasteSynchronously(suffixToPaste) restoreClipboardFullText() 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 } let cleanWord: (String) -> String = { w in let noZwnj = w.replacingOccurrences(of: "\u{200C}", with: "").replacingOccurrences(of: "\u{200D}", with: "") return InputBoxManager.stripPunctuation(noZwnj).lowercased() } // 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) } guard !finalPairs.isEmpty else { restoreClipboardFullText() 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 } var unstreamedPairIndex: Int? = nil // 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 } } } // 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 } } } // 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 } } } // Strategy 4D: Forward prefix match fallback if unstreamedPairIndex == nil { var prefixMatchCount = 0 for i in 0.. 0 { unstreamedPairIndex = prefixMatchCount } } // Guaranteed Safety Fallback: aligned 1-to-1 on content-bearing pairs if unstreamedPairIndex == nil { unstreamedPairIndex = min(streamedPairs.count, finalPairs.count) } if let startIndex = unstreamedPairIndex { if startIndex < finalPairs.count { let remainingWords = finalPairs.dropFirst(startIndex).map { $0.raw } let suffix = remainingWords.joined(separator: " ") let suffixToPaste = addTrailingSpace ? (" " + suffix + " ") : (" " + suffix) self.pasteSynchronously(suffixToPaste) restoreClipboardFullText() return } else { if addTrailingSpace { self.pasteSynchronously(" ") } restoreClipboardFullText() return } } restoreClipboardFullText() } } /// Places text on NSPasteboard, posts Cmd+V, and waits 22ms so target app consumes this chunk BEFORE next clipboard write! private func pasteSynchronously(_ text: String) { let pb = NSPasteboard.general pb.clearContents() pb.setString(text, forType: .string) postCleanCmdV() usleep(22000) // 22ms guard ensures browser/app reads NSPasteboard before next chunk overwrites it (~45 chunks/sec capacity) } private func postCleanCmdV() { let src = CGEventSource(stateID: .privateState) let vKeyCode: CGKeyCode = 0x09 // 'v' if let down = CGEvent(keyboardEventSource: src, virtualKey: vKeyCode, keyDown: true), let up = CGEvent(keyboardEventSource: src, virtualKey: vKeyCode, keyDown: false) { down.flags = .maskCommand up.flags = .maskCommand down.post(tap: .cgAnnotatedSessionEventTap) up.post(tap: .cgAnnotatedSessionEventTap) } } }