From 7c4e9abc461ac8c2bbfd5303b0d3a88dba61787a Mon Sep 17 00:00:00 2001 From: maksarsanjeev Date: Wed, 14 Oct 2026 00:20:21 +0300 Subject: [PATCH] Dictation in the chat (upstream #334, #353, 3b0e987) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit MacDictation: on-device speech recognition when available; «Automatic» listens with up to three recognizers in the user's languages (macOS languages, keyboard layouts, English) and keeps the most confident transcript; a fixed language from the mic's right-click menu. 🎤 next to the chat input; sending while dictating takes the final words first. Hardened runtime gets com.apple.security.device.audio-input; Info.plist gets the microphone and speech-recognition usage strings (in Russian). Co-Authored-By: Claude Opus 5.5 --- NotchBuddy/Resources/BroV.entitlements | 3 + NotchBuddy/Resources/Info.plist | 4 + .../Sources/App/IslandViewContent.swift | 61 ++++ NotchBuddy/Sources/App/MacDictation.swift | 301 ++++++++++++++++++ NotchBuddy/project.yml | 2 + 5 files changed, 371 insertions(+) create mode 100644 NotchBuddy/Sources/App/MacDictation.swift diff --git a/NotchBuddy/Resources/BroV.entitlements b/NotchBuddy/Resources/BroV.entitlements index 36756c8..30f143b 100644 --- a/NotchBuddy/Resources/BroV.entitlements +++ b/NotchBuddy/Resources/BroV.entitlements @@ -5,5 +5,8 @@ com.apple.security.automation.apple-events + + com.apple.security.device.audio-input + diff --git a/NotchBuddy/Resources/Info.plist b/NotchBuddy/Resources/Info.plist index cab49d8..fc993bc 100644 --- a/NotchBuddy/Resources/Info.plist +++ b/NotchBuddy/Resources/Info.plist @@ -33,6 +33,10 @@ BroV jumps to your terminal, sends mail and controls Music. NSHighResolutionCapable + NSMicrophoneUsageDescription + BroV слушает микрофон, когда ты диктуешь сообщение в чат. + NSSpeechRecognitionUsageDescription + BroV превращает твой голос в текст сообщения в чате. NSSupportsAutomaticGraphicsSwitching diff --git a/NotchBuddy/Sources/App/IslandViewContent.swift b/NotchBuddy/Sources/App/IslandViewContent.swift index f36425e..9b2c330 100644 --- a/NotchBuddy/Sources/App/IslandViewContent.swift +++ b/NotchBuddy/Sources/App/IslandViewContent.swift @@ -1189,10 +1189,43 @@ struct MailView: View { // MARK: - Prompt (chat) +/// The mic's right-click menu: Automatic (the user's languages) or one fixed language. +private struct DictationLanguageMenu: View { + @Bindable var dictation: MacDictation + + var body: some View { + let automatic = MacDictation.automaticLocales().map(\.identifier) + Picker("Язык диктовки", selection: $dictation.language) { + Text("Автоматически (" + automatic.map(MacDictation.name(of:)).joined(separator: ", ") + ")") + .tag(MacDictation.automatic) + ForEach(quickChoices(automatic), id: \.self) { id in + Text(MacDictation.name(of: id)).tag(id) + } + } + .pickerStyle(.inline) + Menu("Другие языки") { + Picker("Язык диктовки", selection: $dictation.language) { + ForEach(MacDictation.allLanguages, id: \.self) { id in + Text(MacDictation.name(of: id)).tag(id) + } + } + .pickerStyle(.inline) + .labelsHidden() + } + } + + /// Automatic's languages, plus the fixed one when it is another. + private func quickChoices(_ automatic: [String]) -> [String] { + let chosen = dictation.language + return chosen == MacDictation.automatic || automatic.contains(chosen) ? automatic : automatic + [chosen] + } +} + struct PromptView: View { @ObservedObject var state: AppState @State private var text: String = "" @FocusState private var focused: Bool + @State private var dictation = MacDictation() @State private var showModelPicker = false var body: some View { @@ -1287,6 +1320,26 @@ struct PromptView: View { .focused($focused) .onSubmit { sendMessage() } + // Dictate instead of typing (on-device speech recognition when available). + Button { + Task { + if dictation.isRecording { text = await dictation.finish() } + else { await dictation.start(from: text) } + } + } label: { + Image(systemName: dictation.isRecording ? "mic.fill" : "mic") + .font(.system(size: 11, weight: .semibold)) + .foregroundColor(dictation.isRecording ? Color(hex: "#F4505E") : Color(hex: "#8E939C")) + .frame(width: 18, height: 18) + .contentShape(Rectangle()) + } + .buttonStyle(.plain) + .help(dictation.isRecording ? "Остановить диктовку" : "Диктовать (правый клик — выбрать язык)") + .contextMenu { DictationLanguageMenu(dictation: dictation) } + .onChange(of: dictation.transcript) { _, _ in + if dictation.isRecording { text = dictation.text } + } + Button(action: sendMessage) { Image(systemName: "arrow.up") .font(.system(size: 11, weight: .semibold)) @@ -1345,6 +1398,14 @@ struct PromptView: View { } private func sendMessage() { + // Still dictating: take the final words (and the right language) before sending. + if dictation.isRecording { + Task { + text = await dictation.finish() + sendMessage() + } + return + } let query = text.trimmingCharacters(in: .whitespacesAndNewlines) guard !query.isEmpty else { return } text = "" diff --git a/NotchBuddy/Sources/App/MacDictation.swift b/NotchBuddy/Sources/App/MacDictation.swift new file mode 100644 index 0000000..8e12564 --- /dev/null +++ b/NotchBuddy/Sources/App/MacDictation.swift @@ -0,0 +1,301 @@ +#if !APPSTORE +import AVFoundation +import Carbon +import Speech + +/// Speech to text for the notch chat, on-device when the Mac supports it. +/// From upstream Coucou (#334, #353, review fixes 3b0e987), MIT. +/// +/// Apple's recognizer only understands one language per request, so "Automatic" (the default) +/// listens with up to three recognizers at once — the user's languages (macOS languages, +/// keyboard layouts, then English) — shows the one that won last time while you speak, and when +/// you stop keeps the transcript the recognizers were the most confident about. A fixed language +/// can be chosen from the mic's right-click menu. +/// +/// Not main-actor bound, because the audio tap and the recognizers call back on their own +/// threads; the published values are set on the main actor. +@Observable +final class MacDictation: @unchecked Sendable { + static let automatic = "auto" + private static let languageKey = "dictationLanguage" + /// The language that won the last automatic dictation: shown first next time. + private static let lastHeardKey = "dictationLastLocale" + private static let maxCandidates = 3 + + var isRecording = false + var transcript = "" + /// What was typed before dictating, kept in front of the words. + var prefix = "" + /// `automatic` or a locale identifier (the mic's right-click menu). + var language: String = UserDefaults.standard.string(forKey: MacDictation.languageKey) ?? MacDictation.automatic { + didSet { UserDefaults.standard.set(language, forKey: Self.languageKey) } + } + + @ObservationIgnored private let engine = AVAudioEngine() + @ObservationIgnored private var candidates: [Candidate] = [] + /// The candidate whose words are shown while speaking. + @ObservationIgnored private var lead = 0 + @ObservationIgnored private var finishing: CheckedContinuation? + @ObservationIgnored private var isFinishing = false + @ObservationIgnored private var tapInstalled = false + + /// The text field's content: what was typed, then the words heard so far. + var text: String { prefix + transcript } + + /// One recognizer listening in one language. + private final class Candidate { + let locale: Locale + let recognizer: SFSpeechRecognizer + let request = SFSpeechAudioBufferRecognitionRequest() + var task: SFSpeechRecognitionTask? + var words = "" + /// Mean confidence of the words (Apple fills it in on the final result). + var confidence: Float = 0 + var finished = false + + init(locale: Locale, recognizer: SFSpeechRecognizer) { + self.locale = locale + self.recognizer = recognizer + request.shouldReportPartialResults = true + if recognizer.supportsOnDeviceRecognition { request.requiresOnDeviceRecognition = true } + } + } + + // MARK: Start / finish + + @MainActor func start(from text: String) async { + guard !isRecording, await Self.authorized() else { return } + let built = Self.makeCandidates(language == Self.automatic ? Self.automaticLocales() : [Locale(identifier: language)]) + guard !built.isEmpty, Self.startEngine(engine, feeding: built.map(\.request)) else { return } + tapInstalled = true + prefix = text.isEmpty || text.hasSuffix(" ") ? text : text + " " + transcript = "" + candidates = built + lead = 0 + isRecording = true + NotificationCenter.default.post(name: .dictationDidStart, object: nil) + for (index, candidate) in built.enumerated() { + candidate.task = Self.recognize(candidate) { [weak self] words, confidence, final, failed in + Task { @MainActor in + self?.update(index, words: words, confidence: confidence, final: final, failed: failed) + } + } + } + } + + /// Stops listening and returns the field's text with the most likely transcript. + @MainActor func finish() async -> String { + guard isRecording else { return text } + isFinishing = true + stopEngine() + candidates.forEach { $0.request.endAudio() } + // With several languages, wait a moment for the final results: they carry the confidence. + if candidates.count > 1, !candidates.allSatisfy(\.finished) { + await withCheckedContinuation { continuation in + finishing = continuation + Task { @MainActor [weak self] in + try? await Task.sleep(for: .milliseconds(1200)) + self?.resumeFinishing() + } + } + } + if let best = bestCandidate() { + transcript = best.words + if candidates.count > 1 { + UserDefaults.standard.set(best.locale.identifier, forKey: Self.lastHeardKey) + } + } + tearDown() + return text + } + + /// Cancels without waiting (the view went away). + @MainActor func stop() { + guard isRecording || !candidates.isEmpty else { return } + tearDown() + } + + @MainActor private func stopEngine() { + engine.stop() + if tapInstalled { engine.inputNode.removeTap(onBus: 0) } + tapInstalled = false + } + + @MainActor private func tearDown() { + stopEngine() + candidates.forEach { $0.request.endAudio(); $0.task?.cancel() } + candidates = [] + lead = 0 + NotificationCenter.default.post(name: .dictationDidEnd, object: nil) + isRecording = false + isFinishing = false + resumeFinishing() + } + + @MainActor private func resumeFinishing() { + finishing?.resume() + finishing = nil + } + + // MARK: Results + + @MainActor private func update(_ index: Int, words: String?, confidence: Float, final: Bool, failed: Bool) { + guard index < candidates.count else { return } + let candidate = candidates[index] + if let words { candidate.words = words } + if confidence > 0 { candidate.confidence = confidence } + if final || failed { candidate.finished = true } + + if isFinishing { + if candidates.allSatisfy(\.finished) { resumeFinishing() } + return + } + // A recognizer that gives up while you speak (no model, busy…) hands over to the next one. + if index == lead, candidate.finished, candidate.words.isEmpty, + let next = candidates.firstIndex(where: { !$0.finished }) { + lead = next + transcript = candidates[next].words + return + } + // When Apple reports confidences while speaking, follow the clearly better language. + let current = candidates[lead] + if index != lead, candidate.confidence > current.confidence + 0.15, + candidate.words.split(separator: " ").count >= 3 { + lead = index + } + if index == lead { transcript = candidate.words } + if candidates.allSatisfy(\.finished) { tearDown() } + } + + @MainActor private func bestCandidate() -> Candidate? { + guard lead < candidates.count else { return nil } + let leader = candidates[lead] + let best = candidates + .filter { !$0.words.isEmpty } + .max { $0.confidence < $1.confidence } + guard let best, best.confidence > leader.confidence else { return leader } + return best + } + + // MARK: Audio and recognizers (off the main actor: they call back on their own threads) + + nonisolated private static func startEngine(_ engine: AVAudioEngine, feeding requests: [SFSpeechAudioBufferRecognitionRequest]) -> Bool { + let input = engine.inputNode + input.installTap(onBus: 0, bufferSize: 1024, format: input.outputFormat(forBus: 0)) { buffer, _ in + for request in requests { request.append(buffer) } + } + engine.prepare() + do { try engine.start() } catch { + input.removeTap(onBus: 0) + return false + } + return true + } + + nonisolated private static func recognize( + _ candidate: Candidate, + report: @escaping @Sendable (String?, Float, Bool, Bool) -> Void + ) -> SFSpeechRecognitionTask { + candidate.recognizer.recognitionTask(with: candidate.request) { result, error in + let words = result?.bestTranscription.formattedString + let segments = result?.bestTranscription.segments ?? [] + let confidence = segments.isEmpty ? 0 : segments.map(\.confidence).reduce(0, +) / Float(segments.count) + report(words, confidence, result?.isFinal ?? false, error != nil) + } + } + + /// Recognizers that can run now. Extra languages in Automatic only listen on-device, + /// so the audio never goes to Apple in a language you didn't speak. + private static func makeCandidates(_ locales: [Locale]) -> [Candidate] { + var out: [Candidate] = [] + for locale in locales { + guard let recognizer = SFSpeechRecognizer(locale: locale), recognizer.isAvailable else { continue } + if !out.isEmpty && !recognizer.supportsOnDeviceRecognition { continue } + out.append(Candidate(locale: locale, recognizer: recognizer)) + } + if out.isEmpty, let fallback = SFSpeechRecognizer(), fallback.isAvailable { + out.append(Candidate(locale: fallback.locale, recognizer: fallback)) + } + return out + } + + // MARK: Languages + + /// The languages Automatic listens for: last winner, macOS languages, keyboard layouts, + /// Coucou's own language, then English — one per language, up to three. + static func automaticLocales() -> [Locale] { + var ids: [String] = [] + if let last = UserDefaults.standard.string(forKey: lastHeardKey) { ids.append(last) } + // The Mac's languages (not Coucou's override in Settings → General → Language). + ids += (CFPreferencesCopyValue("AppleLanguages" as CFString, kCFPreferencesAnyApplication, + kCFPreferencesCurrentUser, kCFPreferencesAnyHost) as? [String]) ?? [] + ids += keyboardLanguages() + ids += Locale.preferredLanguages + ids.append("en-US") + + let supported = Array(SFSpeechRecognizer.supportedLocales()) + var seen = Set() + var out: [Locale] = [] + for id in ids { + guard let locale = match(id, in: supported) else { continue } + let language = locale.language.languageCode?.identifier ?? locale.identifier + guard seen.insert(language).inserted else { continue } + out.append(locale) + if out.count == maxCandidates { break } + } + return out + } + + /// Every language the recognizer knows, by name. + static var allLanguages: [String] { + SFSpeechRecognizer.supportedLocales() + .map(\.identifier) + .sorted { name(of: $0).localizedCaseInsensitiveCompare(name(of: $1)) == .orderedAscending } + } + + static func name(of identifier: String) -> String { + let name = Locale.current.localizedString(forIdentifier: identifier) ?? identifier + return name.prefix(1).uppercased() + name.dropFirst() + } + + /// "fr" or "fr_FR" → the recognizer locale for it, preferring the Mac's region. + private static func match(_ id: String, in supported: [Locale]) -> Locale? { + let wanted = id.replacingOccurrences(of: "_", with: "-").lowercased() + if let exact = supported.first(where: { $0.identifier.replacingOccurrences(of: "_", with: "-").lowercased() == wanted }) { + return exact + } + let language = Locale(identifier: id).language.languageCode?.identifier + let sameLanguage = supported.filter { $0.language.languageCode?.identifier == language } + let region = Locale(identifier: id).region ?? Locale.current.region + return sameLanguage.first { $0.region == region } + ?? sameLanguage.min { $0.identifier < $1.identifier } + } + + /// The main language of each enabled keyboard layout ("fr" for AZERTY, "en" for ABC…). + private static func keyboardLanguages() -> [String] { + let filter = ["TISPropertyInputSourceCategory": "TISCategoryKeyboardInputSource"] as CFDictionary + guard let list = TISCreateInputSourceList(filter, false)?.takeRetainedValue() as? [TISInputSource] else { return [] } + return list.compactMap { source in + guard let raw = TISGetInputSourceProperty(source, "TISPropertyInputSourceLanguages" as CFString) else { return nil } + let languages = Unmanaged.fromOpaque(raw).takeUnretainedValue() as? [String] + return languages?.first + } + } + + private static func authorized() async -> Bool { + let speech = await withCheckedContinuation { continuation in + SFSpeechRecognizer.requestAuthorization { continuation.resume(returning: $0 == .authorized) } + } + guard speech else { return false } + return await AVAudioApplication.requestRecordPermission() + } +} + +// MARK: - Dictation notification names +// Defined here so both MacDictation and VoiceEngine can reference them without +// cross-file ordering issues in SourceKit. +extension Notification.Name { + static let dictationDidStart = Notification.Name("notchBuddy.dictationDidStart") + static let dictationDidEnd = Notification.Name("notchBuddy.dictationDidEnd") +} +#endif diff --git a/NotchBuddy/project.yml b/NotchBuddy/project.yml index 742b4b0..cabc672 100644 --- a/NotchBuddy/project.yml +++ b/NotchBuddy/project.yml @@ -72,6 +72,8 @@ targets: CFBundleShortVersionString: "0.1.0" CFBundlePackageType: APPL LSUIElement: YES + NSMicrophoneUsageDescription: "BroV слушает микрофон, когда ты диктуешь сообщение в чат." + NSSpeechRecognitionUsageDescription: "BroV превращает твой голос в текст сообщения в чате." NSHighResolutionCapable: YES NSSupportsAutomaticGraphicsSwitching: YES NSAppleEventsUsageDescription: "BroV jumps to your terminal, sends mail and controls Music."