Dictation in the chat (upstream #334, #353, 3b0e987)

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 <noreply@anthropic.com>
This commit is contained in:
maksarsanjeev
2026-10-14 00:20:21 +03:00
parent 0199e9a7f0
commit 7c4e9abc46
5 changed files with 371 additions and 0 deletions
@@ -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 = ""