Fastest and only macOS Dictation app with on-device STT and custom trained AI enhancement model. Windows, iOS testflight! Android, Linux waitlist open! A local Wispr Flow alternative. Follow https://x.com/fluidvoiceapp for more
feat(dictionary): add guided voice training and local match diagnostics
altic-dev8fc2aa9bb193
diff --git a/Sources/Fluid/Services/ASRService.swift b/Sources/Fluid/Services/ASRService.swiftindex 4320febb..6f5c7f2f 100644--- a/Sources/Fluid/Services/ASRService.swift+++ b/Sources/Fluid/Services/ASRService.swift@@ -1907,6 +1907,11 @@ final class ASRService: ObservableObject {// Thread-safe buffer to prevent "Array mutation while enumerating" and memory corruption crashes// during long sessions where reallocation occurs frequently.private let audioBuffer = ThreadSafeAudioBuffer()+ // Transient, read-only audio for a focused dictionary test. No history setting or disk write.+ private let dictionaryTestAudioSubject = PassthroughSubject<[Float], Never>()+ var dictionaryTestAudioPublisher: AnyPublisher<[Float], Never> {+ self.dictionaryTestAudioSubject.eraseToAnyPublisher()+ }private var lastCompletedAudioSnapshot: DictationAudioSnapshot?private var lastDictionaryLearningRecording: DictionaryLearningRecording?private var dictionaryLearningExpiryTask: Task<Void, Never>?@@ -3187,6 +3192,9 @@ final class ASRService: ObservableObject {)}+ /// Identifies the protected dictionary capture so a dismissed test cannot stop a newer recording.+ var dictionaryCaptureToken: Int? { self.isDictionaryTrainingCaptureActive ? self.benchmarkSessionID : nil }+/// Stops the recording session and returns the transcribed text.////// This method performs the complete transcription process:@@ -3225,8 +3233,11 @@ final class ASRService: ObservableObject {func stop(onCaptureStopped: (@MainActor () -> Void)? = nil,onFinalTranscriptionStarted: (@MainActor () -> Void)? = nil,- forDictionaryTraining: Bool = false+ forDictionaryTraining: Bool = false,+ captureDictionaryPronunciation: Bool? = nil,+ forDictionaryTesting: Bool = false) async -> String {+ guard !forDictionaryTesting || self.isDictionaryTrainingCaptureActive else { return "" }DebugLogger.shared.info("🛑 STOP() called - beginning shutdown sequence", source: "ASRService")self.lastStopOutcome = .emptyself.lastFinalTranscriptionDurationMs = nil@@ -3278,7 +3289,9 @@ final class ASRService: ObservableObject {}}self.stopStreamingScheduler(sessionID: stoppingSessionID)- let useDictionaryTrainingPath = forDictionaryTraining || self.isDictionaryTrainingCaptureActive+ let isolatedDictionaryCapture = forDictionaryTraining || self.isDictionaryTrainingCaptureActive+ // Tests use normal recognition and saved corrections, but never enroll or retain learning audio.+ let useDictionaryTrainingPath = isolatedDictionaryCapture && !forDictionaryTestingdefer {self.applyPendingParakeetVocabularyReloadIfNeeded()self.isDictionaryTrainingCaptureActive = false@@ -3477,9 +3490,20 @@ final class ASRService: ObservableObject {if useDictionaryTrainingPath {result = try await self.transcriptionExecutor.run { [provider] inself.publishStoppedState(for: stoppingSessionID)+ if let captureDictionaryPronunciation {+ return try await provider.transcribeDictionaryTraining(pcm, capturePronunciation: captureDictionaryPronunciation)+ }return try await provider.transcribeDictionaryTraining(pcm)}- self.lastDictionaryTrainingResult = result+ var enrollment = result.pronunciationEnrollment+ // Preserve the unpadded boundary alongside the exact model input.+ enrollment?.pendingInspection?.recordedSampleCount = capturedPCM.count+ self.lastDictionaryTrainingResult = ASRTranscriptionResult(+ text: result.text,+ confidence: result.confidence,+ pronunciationEnrollment: enrollment,+ dictionaryLearningAlignment: result.dictionaryLearningAlignment+ )finalSource = "dictionaryTraining"} else {let vocabularyProvider = provider as? FluidAudioProvider@@ -3554,7 +3578,7 @@ final class ASRService: ObservableObject {}// Do not update self.finalText here to avoid instant binding insert in playground- if !useDictionaryTrainingPath, SettingsStore.shared.automaticDictionaryLearningEnabled,+ if !isolatedDictionaryCapture, SettingsStore.shared.automaticDictionaryLearningEnabled,let alignment = result.dictionaryLearningAlignment{self.retainDictionaryLearningRecording(DictionaryLearningRecording(alignment: alignment, samples: capturedPCM))@@ -3567,12 +3591,12 @@ final class ASRService: ObservableObject {let outputText = useDictionaryTrainingPath? dictionaryText: ASRService.applySpokenPunctuationFormatting(dictionaryText)- if !useDictionaryTrainingPath {+ if !isolatedDictionaryCapture {self.recordWordBoostHitIfAny(transcribedText: outputText)}DebugLogger.shared.debug("After post-processing: '\(outputText)'", source: "ASRService")self.benchmarkLog("stop_end result=success totalMs=\(self.elapsedMilliseconds(since: stopStartedAt)) recordingAgeMs=\(self.elapsedMilliseconds(since: self.benchmarkRecordingStartedAt)) cleanedChars=\(outputText.count)")- if !useDictionaryTrainingPath,+ if !isolatedDictionaryCapture,SettingsStore.shared.saveTranscriptionHistory,SettingsStore.shared.saveAudioWithTranscriptionHistory,!capturedPCM.isEmpty@@ -3584,6 +3608,10 @@ final class ASRService: ObservableObject {)}+ if !isolatedDictionaryCapture, capturedPCM.count <= 16_000 * 120 {+ self.dictionaryTestAudioSubject.send(capturedPCM)+ }+self.lastStopOutcome = outputText.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty? .empty: .success@@ -6840,14 +6868,22 @@ final class ASRService: ObservableObject {/// Cache for compiled custom dictionary regexes./// Key: trigger word, Value: (compiled regex, escaped replacement template)/// Cleared when dictionary entries change.- private static var cachedDictionaryPatterns: [(regex: NSRegularExpression, template: String, canonical: NSRegularExpression?)] = []+ private struct CachedDictionaryPattern {+ let entryID: UUID+ let trigger: String+ let regex: NSRegularExpression+ let template: String+ let canonical: NSRegularExpression?+ }++ private static var cachedDictionaryPatterns: [CachedDictionaryPattern] = []private static var dictionaryCacheNeedsRebuild: Bool = true/// Rebuilds the regex cache if dictionary has changed./// Called lazily on first apply after settings change.private static func rebuildDictionaryCache() {let entries = SettingsStore.shared.customDictionaryEntries- var patterns: [(regex: NSRegularExpression, template: String, canonical: NSRegularExpression?)] = []+ var patterns: [CachedDictionaryPattern] = []for entry in entries {var canonical: NSRegularExpression?@@ -6872,7 +6908,13 @@ final class ASRService: ObservableObject {if needsProtection, canonical == nil {canonical = try? NSRegularExpression(pattern: self.dictionaryPattern(for: entry.replacement), options: .caseInsensitive)}- patterns.append((regex: regex, template: NSRegularExpression.escapedTemplate(for: entry.replacement), canonical: needsProtection ? canonical : nil))+ patterns.append(CachedDictionaryPattern(+ entryID: entry.id,+ trigger: trigger,+ regex: regex,+ template: NSRegularExpression.escapedTemplate(for: entry.replacement),+ canonical: needsProtection ? canonical : nil+ ))}}@@ -6933,12 +6975,32 @@ final class ASRService: ObservableObject {var result = text// Apply cached regexes - O(n) where n = number of patterns+ var replacementCount = 0for pattern in self.cachedDictionaryPatterns {result = DictionaryReplacementProtection.replacingMatches(in: result,regex: pattern.regex,template: pattern.template,- canonical: pattern.canonical+ canonical: pattern.canonical,+ onReplacement: { range, heard, replacement in+ replacementCount += 1+ guard replacementCount <= DictionaryReplacementDiagnostics.maximumEvents else { return }+ DebugLogger.shared.debug(+ "DICTIONARY_REPLACEMENT stage=exact_rule entryID=\(pattern.entryID) " ++ "trigger=\(DictionaryReplacementDiagnostics.quoted(pattern.trigger)) " ++ "heard=\(DictionaryReplacementDiagnostics.quoted(heard)) " ++ "replacement=\(DictionaryReplacementDiagnostics.quoted(replacement)) " ++ "utf16Location=\(range.location) utf16Length=\(range.length)",+ source: "CustomDictionary"+ )+ }+ )+ }+ if replacementCount > DictionaryReplacementDiagnostics.maximumEvents {+ DebugLogger.shared.info(+ "DICTIONARY_REPLACEMENT_SUMMARY total=\(replacementCount) " ++ "omitted=\(replacementCount - DictionaryReplacementDiagnostics.maximumEvents)",+ source: "CustomDictionary")}diff --git a/Sources/Fluid/Services/DictionaryAudioInspection.swift b/Sources/Fluid/Services/DictionaryAudioInspection.swiftnew file mode 100644index 00000000..9090e561--- /dev/null+++ b/Sources/Fluid/Services/DictionaryAudioInspection.swift@@ -0,0 +1,230 @@+// nil means an invalid cut, not a zero-length embedding.+import Foundation++/// Bounded local diagnostic evidence. Never used to change production matching.+nonisolated struct DictionaryAudioInspection: Codable, Equatable, Sendable {+ struct Frames: Codable, Equatable, Sendable {+ let offset: Double+ let frameDuration: Double+ let hiddenSize: Int+ let values: [Float]+ var count: Int { self.hiddenSize > 0 ? self.values.count / self.hiddenSize : 0 }+ }++ struct Word: Codable, Equatable, Sendable {+ let text: String+ let start: Double+ let end: Double+ }++ let samples: [Float] // Exact 16 kHz model input, including any minimum-length padding.+ var recordedSampleCount: Int+ let frames: [Frames]+ let words: [Word]+ let selectedStart: Double+ let selectedEnd: Double+ var duration: Double { Double(self.recordedSampleCount) / 16_000 }+ var paddedDuration: Double { Double(self.samples.count) / 16_000 }++ var isValid: Bool {+ guard !self.samples.isEmpty, self.samples.count <= 16_000 * 120,+ self.recordedSampleCount > 0, self.recordedSampleCount <= self.samples.count,+ self.samples.allSatisfy(\.isFinite), self.frames.count <= 9, !self.frames.isEmpty,+ self.selectedStart.isFinite, self.selectedEnd.isFinite,+ self.selectedStart >= 0, self.selectedStart < self.selectedEnd,+ self.selectedEnd <= self.paddedDuration + 0.081+ else { return false }+ var previousEnd = 0.0+ for frame in self.frames {+ guard frame.offset.isFinite, frame.offset >= previousEnd - 0.081,+ frame.frameDuration == 0.08, frame.hiddenSize > 0, frame.hiddenSize <= 2048,+ frame.values.count % frame.hiddenSize == 0, !frame.values.isEmpty, frame.count <= 190,+ frame.values.allSatisfy(\.isFinite),+ frame.offset + Double(frame.count) * frame.frameDuration <= self.paddedDuration + 0.081+ else { return false }+ previousEnd = frame.offset + Double(frame.count) * frame.frameDuration+ }+ return true+ }++ // Uses one encoder pass and the same mean/L2 operation as the matcher.+ // Crossing separately encoded chunks is deliberately unavailable.+ // swiftlint:disable:next discouraged_optional_collection+ func embedding(start: Double, end: Double) -> [Float]? {+ guard start.isFinite, end.isFinite, start >= 0, start < end else { return nil }+ guard let chunk = self.frames.first(where: {+ start >= $0.offset - 0.001 && end <= $0.offset + Double($0.count) * $0.frameDuration + 0.001+ }), chunk.hiddenSize > 0 else { return nil }+ let lower = max(0, Int(((start - chunk.offset) / chunk.frameDuration).rounded()))+ let upper = min(chunk.count, Int(((end - chunk.offset) / chunk.frameDuration).rounded()))+ guard lower < upper else { return nil }+ var mean = [Float](repeating: 0, count: chunk.hiddenSize)+ for frame in lower..<upper {+ for index in mean.indices {+ mean[index] += chunk.values[frame * chunk.hiddenSize + index]+ }+ }+ let norm = sqrt(mean.reduce(Float(0)) { $0 + $1 * $1 })+ guard norm.isFinite, norm > 0 else { return nil }+ return mean.map { $0 / norm }+ }++ // swiftlint:disable:next discouraged_optional_collection+ static func similarity(_ first: [Float]?, _ second: [Float]?) -> Float? {+ guard let first, let second, !first.isEmpty, first.count == second.count else { return nil }+ let firstNorm = sqrt(first.reduce(Float(0)) { $0 + $1 * $1 })+ let secondNorm = sqrt(second.reduce(Float(0)) { $0 + $1 * $1 })+ guard firstNorm > 0, secondNorm > 0 else { return nil }+ let value = zip(first, second).reduce(Float(0)) { $0 + $1.0 * $1.1 } / (firstNorm * secondNorm)+ return value.isFinite ? value : nil+ }+}++nonisolated struct DictionaryAudioCut: Equatable, Sendable {+ var start: Double+ var end: Double+}++nonisolated struct DictionaryAudioCutMetrics: Sendable {+ let leadingQuiet: Double+ let trailingQuiet: Double+ let suggested: DictionaryAudioCut?+}++/// CPU-only operations are isolated from SwiftUI and never load an ASR model.+actor DictionaryAudioInspector {+ static let shared = DictionaryAudioInspector()++ func waveform(_ audio: DictionaryAudioInspection) -> [Float] {+ let count = min(audio.recordedSampleCount, audio.samples.count)+ guard count > 0 else { return [] }+ let stride = max(1, (count + 255) / 256)+ return Swift.stride(from: 0, to: count, by: stride).map { start in+ audio.samples[start..<min(count, start + stride)].reduce(Float(0)) { max($0, abs($1)) }+ }+ }++ func metrics(_ audio: DictionaryAudioInspection, cut: DictionaryAudioCut) -> DictionaryAudioCutMetrics {+ let lower = max(0, min(audio.recordedSampleCount, Int(cut.start * 16_000)))+ let upper = max(lower, min(audio.recordedSampleCount, Int(cut.end * 16_000)))+ let blocks = Array(stride(from: lower, to: upper, by: 320)) // 20 ms RMS, -45 dBFS.+ let active = blocks.filter { start in+ let block = audio.samples[start..<min(upper, start + 320)]+ let rms = sqrt(block.reduce(Float(0)) { $0 + $1 * $1 } / Float(block.count))+ return rms >= 0.005_623_413+ }+ guard let first = active.first, let last = active.last else {+ return DictionaryAudioCutMetrics(leadingQuiet: Double(upper - lower) / 16_000, trailingQuiet: 0, suggested: nil)+ }+ let leading = Double(first - lower) / 16_000+ let trailing = Double(max(0, upper - last - 320)) / 16_000+ // Keep 80 ms of context; this is an optional comparison, never an automatic edit.+ let start = max(cut.start, floor((Double(first) / 16_000 - 0.08) / 0.08) * 0.08)+ let end = min(cut.end, ceil((Double(last + 320) / 16_000 + 0.08) / 0.08) * 0.08)+ return DictionaryAudioCutMetrics(leadingQuiet: leading, trailingQuiet: trailing, suggested: start < end ? DictionaryAudioCut(start: start, end: end) : nil)+ }++ func score(test: DictionaryAudioInspection, testCut: DictionaryAudioCut, reference: DictionaryAudioInspection?, referenceCut: DictionaryAudioCut?, storedVector: [Float]) -> Float? {+ // swiftlint:disable:next discouraged_optional_collection+ let target: [Float]?+ if let reference, let referenceCut {+ target = reference.embedding(start: referenceCut.start, end: referenceCut.end)+ } else { target = storedVector }+ return DictionaryAudioInspection.similarity(test.embedding(start: testCut.start, end: testCut.end), target)+ }++ /// One fixed test vector compared with every enrollment, preserving enrollment order.+ func scores(test: DictionaryAudioInspection, testCut: DictionaryAudioCut, references: [DictionaryAudioInspection?], referenceCuts: [DictionaryAudioCut?], storedVectors: [[Float]]) -> [Float?] {+ let vector = test.embedding(start: testCut.start, end: testCut.end)+ return storedVectors.indices.map { index in+ if references.indices.contains(index), referenceCuts.indices.contains(index),+ let reference = references[index], let cut = referenceCuts[index]+ {+ return DictionaryAudioInspection.similarity(vector, reference.embedding(start: cut.start, end: cut.end))+ }+ return DictionaryAudioInspection.similarity(vector, storedVectors[index])+ }+ }++ func wav(_ audio: DictionaryAudioInspection, cut: DictionaryAudioCut?) throws -> Data {+ let start = max(0, min(audio.recordedSampleCount, Int((cut?.start ?? 0) * 16_000)))+ let end = min(audio.recordedSampleCount, Int((cut?.end ?? audio.duration) * 16_000))+ guard start < end else { throw DictionaryMatchPlaygroundError.unavailable("This selection has no recorded audio.") }+ var pcm = Data(capacity: (end - start) * 2)+ for value in audio.samples[start..<end] {+ var sample = Int16(max(-1, min(1, value)) * Float(Int16.max)).littleEndian+ withUnsafeBytes(of: &sample) { pcm.append(contentsOf: $0) }+ }+ var data = Data("RIFF".utf8)+ func append<T: FixedWidthInteger>(_ value: T) {+ var little = value.littleEndian+ withUnsafeBytes(of: &little) { data.append(contentsOf: $0) }+ }+ append(UInt32(36 + pcm.count)); data.append(Data("WAVEfmt ".utf8))+ append(UInt32(16)); append(UInt16(1)); append(UInt16(1)); append(UInt32(16_000))+ append(UInt32(32_000)); append(UInt16(2)); append(UInt16(16))+ data.append(Data("data".utf8)); append(UInt32(pcm.count)); data.append(pcm)+ return data+ }+}++// Compact Float32 blobs avoid serializing millions of boxed floating-point values.+// Supported Macs are little-endian; the stored format is explicitly versioned.+extension DictionaryAudioInspection {+ private enum CodingKeys: String, CodingKey {+ case version, samples, recordedSampleCount, frames, words, selectedStart, selectedEnd+ }++ init(from decoder: Decoder) throws {+ let values = try decoder.container(keyedBy: CodingKeys.self)+ guard try values.decode(Int.self, forKey: .version) == 1 else {+ throw DecodingError.dataCorruptedError(forKey: .version, in: values, debugDescription: "Unsupported inspection format")+ }+ self.samples = try Self.floats(from: values.decode(Data.self, forKey: .samples))+ self.recordedSampleCount = try values.decode(Int.self, forKey: .recordedSampleCount)+ self.frames = try values.decode([Frames].self, forKey: .frames)+ self.words = try values.decode([Word].self, forKey: .words)+ self.selectedStart = try values.decode(Double.self, forKey: .selectedStart)+ self.selectedEnd = try values.decode(Double.self, forKey: .selectedEnd)+ }++ func encode(to encoder: Encoder) throws {+ var values = encoder.container(keyedBy: CodingKeys.self)+ try values.encode(1, forKey: .version)+ try values.encode(self.samples.withUnsafeBytes { Data($0) }, forKey: .samples)+ try values.encode(self.recordedSampleCount, forKey: .recordedSampleCount)+ try values.encode(self.frames, forKey: .frames)+ try values.encode(self.words, forKey: .words)+ try values.encode(self.selectedStart, forKey: .selectedStart)+ try values.encode(self.selectedEnd, forKey: .selectedEnd)+ }++ private static func floats(from data: Data) throws -> [Float] {+ guard data.count % MemoryLayout<Float>.size == 0, data.count <= 8_000_000 else {+ throw DictionaryMatchPlaygroundError.unavailable("Invalid inspection audio or encoder data.")+ }+ var values = [Float](repeating: 0, count: data.count / MemoryLayout<Float>.size)+ _ = values.withUnsafeMutableBytes { destination in data.copyBytes(to: destination) }+ return values+ }+}++extension DictionaryAudioInspection.Frames {+ private enum CodingKeys: String, CodingKey { case offset, frameDuration, hiddenSize, values }++ init(from decoder: Decoder) throws {+ let container = try decoder.container(keyedBy: CodingKeys.self)+ self.offset = try container.decode(Double.self, forKey: .offset)+ self.frameDuration = try container.decode(Double.self, forKey: .frameDuration)+ self.hiddenSize = try container.decode(Int.self, forKey: .hiddenSize)+ self.values = try DictionaryAudioInspection.floats(from: container.decode(Data.self, forKey: .values))+ }++ func encode(to encoder: Encoder) throws {+ var container = encoder.container(keyedBy: CodingKeys.self)+ try container.encode(self.offset, forKey: .offset)+ try container.encode(self.frameDuration, forKey: .frameDuration)+ try container.encode(self.hiddenSize, forKey: .hiddenSize)+ try container.encode(self.values.withUnsafeBytes { Data($0) }, forKey: .values)+ }+}diff --git a/Sources/Fluid/Services/DictionaryMatchPlayground.swift b/Sources/Fluid/Services/DictionaryMatchPlayground.swiftnew file mode 100644index 00000000..7c92e410--- /dev/null+++ b/Sources/Fluid/Services/DictionaryMatchPlayground.swift@@ -0,0 +1,240 @@+import AVFoundation+import Foundation+#if arch(arm64)+import FluidAudio+#endif++nonisolated struct DictionaryMatchReport: Codable, Sendable {+ struct Candidate: Codable, Identifiable, Sendable {+ let id: String+ let word: String+ var sampleNumber: Int?+ let enrollmentCount: Int+ let eligible: Bool+ var score: Float?+ var start: Double?+ var end: Double?+ var heard = ""+ var requiredScore: Float = 0.70+ var competingWord: String?+ var competingScore: Float?++ var explanation: String {+ guard let score else { return "No usable audio window" }+ if !self.eligible { return "Not active: needs more compatible recordings" }+ if score < self.requiredScore { return "Below cutoff by \(String(format: "%.3f", self.requiredScore - score))" }+ if self.heard.isEmpty { return "Score clears cutoff, but no word overlaps this audio window" }+ if let competingScore, competingScore > score - 0.05 { return "Close competing best window: less than 0.05 separation" }+ return "Above score cutoff in this replay; verify the matched phrase"+ }+ }++ /// One strongest individual recording per word, retaining its window and evidence.+ static func bestCandidates(from samples: [Candidate]) -> [Candidate] {+ Dictionary(grouping: samples, by: { $0.word.lowercased() }).values.compactMap { group in+ guard var best = group.max(by: { ($0.score ?? -.infinity) < ($1.score ?? -.infinity) }) else { return nil }+ best.sampleNumber = nil+ return best+ }+ }++ let createdAt: Date+ let recordingID: UUID+ let audioPath: String+ let targetWord: String+ let modelKey: String+ let duration: Double+ let rawTranscript: String+ let savedTranscript: String+ let availableProfileCount: Int+ let candidates: [Candidate]+ let targetSamples: [Candidate]+ // Local export retains the exact reference vectors used by this replay.+ let profiles: [PronunciationDictionaryProfile]+ var inspection: DictionaryAudioInspection? = nil+}++enum DictionaryMatchPlaygroundError: LocalizedError {+ case unavailable(String)++ var errorDescription: String? {+ switch self {+ case let .unavailable(message): message+ }+ }+}++/// Optional, on-demand replay. Does not use the live ASR service, write the dictionary,+/// open a microphone, or change production matching thresholds.+actor DictionaryMatchPlayground {+ static let shared = DictionaryMatchPlayground()+ private var running = false++ func analyze(recordingID: UUID, audioURL: URL, savedTranscript: String, target: String) async throws -> DictionaryMatchReport {+ let file = try AVAudioFile(forReading: audioURL)+ let duration = Double(file.length) / file.processingFormat.sampleRate+ guard duration > 0, duration <= 120 else {+ throw DictionaryMatchPlaygroundError.unavailable("Use a recording up to two minutes long.")+ }+ #if arch(arm64)+ let samples = try AudioConverter().resampleAudioFile(path: audioURL.path)+ return try await self.analyze(recordingID: recordingID, samples: samples, savedTranscript: savedTranscript, target: target, audioPath: audioURL.path)+ #else+ throw DictionaryMatchPlaygroundError.unavailable("Pronunciation matching requires Apple Silicon.")+ #endif+ }++ func analyze(recordingID: UUID, samples: [Float], savedTranscript: String, target: String, audioPath: String = "") async throws -> DictionaryMatchReport {+ // A cancelled prior attempt releases its model before a new one starts.+ for _ in 0..<100 where self.running {+ try await Task.sleep(for: .milliseconds(100))+ }+ try Task.checkCancellation()+ guard !self.running else { throw DictionaryMatchPlaygroundError.unavailable("The previous recording is still finishing. Try again.") }+ guard !samples.isEmpty, samples.count <= 16_000 * 120 else {+ throw DictionaryMatchPlaygroundError.unavailable("Use a recording up to two minutes long.")+ }+ self.running = true+ defer { self.running = false }+ let duration = Double(samples.count) / 16_000+ #if arch(arm64)+ let allProfiles = await PronunciationDictionaryStore.shared.allProfiles()+ guard let targetProfile = allProfiles.first(where: {+ $0.label.caseInsensitiveCompare(target) == .orderedSame && ["parakeet-v2", "parakeet-v3"].contains($0.modelKey)+ }) else {+ throw DictionaryMatchPlaygroundError.unavailable("No saved Parakeet pronunciation recordings for “\(target)”. Train this word by voice first.")+ }+ let matching = allProfiles.filter { $0.modelKey == targetProfile.modelKey }.sorted {+ let lhsTarget = $0.dictionaryEntryID == targetProfile.dictionaryEntryID+ let rhsTarget = $1.dictionaryEntryID == targetProfile.dictionaryEntryID+ if lhsTarget != rhsTarget { return lhsTarget }+ return $0.label < $1.label+ }+ let profiles = Array(matching.prefix(64))+ try Task.checkCancellation()+ let version: AsrModelVersion = targetProfile.modelKey == "parakeet-v2" ? .v2 : .v3+ let models = try await AsrModels.loadLocalOnly(from: AsrModels.defaultCacheDirectory(for: version), version: version)+ let manager = AsrManager(config: ASRConfig(tdtConfig: TdtConfig(blankId: version.blankId), encoderHiddenSize: version.encoderHiddenSize))+ do {+ try await manager.initialize(models: models)+ let report = try await self.scan(+ samples: samples,+ manager: manager,+ profiles: profiles,+ targetProfile: targetProfile,+ context: ReplayContext(recordingID: recordingID, audioPath: audioPath, savedTranscript: savedTranscript, duration: duration, availableProfileCount: matching.count)+ )+ await manager.cleanup()+ return report+ } catch {+ await manager.cleanup()+ throw error+ }+ #else+ throw DictionaryMatchPlaygroundError.unavailable("Pronunciation replay diagnostics require Apple Silicon.")+ #endif+ }++ #if arch(arm64)+ private struct ReplayContext {+ let recordingID: UUID+ let audioPath: String+ let savedTranscript: String+ let duration: Double+ let availableProfileCount: Int+ }++ private func scan(+ samples: [Float], manager: AsrManager, profiles: [PronunciationDictionaryProfile],+ targetProfile: PronunciationDictionaryProfile, context: ReplayContext+ ) async throws -> DictionaryMatchReport {+ let references = DictionaryPronunciationReferences.make(profiles: profiles)+ let vectors = references.map(\.embedding)+ var rows = references.map { reference in+ DictionaryMatchReport.Candidate(+ id: "\(reference.profile.id):sample-\(reference.sampleNumber)",+ word: reference.profile.label,+ sampleNumber: reference.sampleNumber,+ enrollmentCount: reference.profile.enrollments.count,+ eligible: reference.profile.isEligibleForMatching+ )+ }+ var transcripts: [String] = []+ var inspectedFrames: [DictionaryAudioInspection.Frames] = []+ var inspectedWords: [DictionaryAudioInspection.Word] = []+ // Match the same 80ms encoder frames in bounded chunks. No confidence cutoff:+ // bestMatches retains near misses that production allMatches intentionally drops.+ for offset in stride(from: 0, to: samples.count, by: 238_080) {+ try Task.checkCancellation()+ var chunk = Array(samples[offset..<min(offset + 238_080, samples.count)])+ let unpaddedCount = chunk.count+ if chunk.count < 16_000 { chunk += repeatElement(0, count: 16_000 - chunk.count) }+ await manager.setPronunciationCustomizationEnabled(true)+ let result = try await manager.transcribe(chunk, source: .microphone)+ let features = await manager.consumePronunciationEncoderFeatures()+ transcripts.append(result.text)+ guard let features else { continue }+ let words = WordAudioChunkExtractor.words(from: result.tokenTimings ?? [])+ let frameCount = min(features.frameCount, Int(ceil(Double(unpaddedCount) / 16_000 / features.frameDuration)))+ inspectedFrames.append(.init(+ offset: Double(offset) / 16_000,+ frameDuration: features.frameDuration,+ hiddenSize: features.hiddenSize,+ values: Array(features.values.prefix(frameCount * features.hiddenSize))+ ))+ inspectedWords += words.map { .init(text: $0.text, start: Double(offset) / 16_000 + $0.startTime, end: Double(offset) / 16_000 + $0.endTime) }+ let matches = PronunciationEmbeddingMatcher.bestMatches(prototypes: vectors, in: features)+ for (index, match) in matches.enumerated() {+ guard let match, match.score.isFinite, rows.indices.contains(index),+ rows[index].score == nil || match.score > (rows[index].score ?? -.infinity)+ else { continue }+ let start = Double(match.frameRange.lowerBound) * 0.08+ let end = Double(match.frameRange.upperBound) * 0.08+ let heard = words.filter {+ max(0, min($0.endTime, end) - max($0.startTime, start)) / max(0.0001, $0.endTime - $0.startTime) >= 0.5+ }.map(\.text).joined(separator: " ")+ rows[index].score = match.score+ rows[index].start = Double(offset) / 16_000 + start+ rows[index].end = Double(offset) / 16_000 + end+ rows[index].heard = heard+ rows[index].requiredScore = DictionaryPronunciationDecision.requiredScore(heardText: heard, profile: references[index].profile)+ }+ }+ try Task.checkCancellation()+ var candidates = DictionaryMatchReport.bestCandidates(from: rows)+ for index in candidates.indices {+ let current = candidates[index]+ let competing = candidates.filter {+ $0.word.caseInsensitiveCompare(current.word) != .orderedSame && $0.eligible &&+ ($0.score ?? -.infinity) >= $0.requiredScore &&+ max($0.start ?? 0, current.start ?? 0) < min($0.end ?? 0, current.end ?? 0)+ }.max { ($0.score ?? -.infinity) < ($1.score ?? -.infinity) }+ candidates[index].competingWord = competing?.word+ candidates[index].competingScore = competing?.score+ }+ candidates.sort { ($0.score ?? -.infinity) > ($1.score ?? -.infinity) }+ return DictionaryMatchReport(+ createdAt: Date(),+ recordingID: context.recordingID,+ audioPath: context.audioPath,+ targetWord: targetProfile.label,+ modelKey: targetProfile.modelKey,+ duration: context.duration,+ rawTranscript: transcripts.joined(separator: " "),+ savedTranscript: context.savedTranscript,+ availableProfileCount: context.availableProfileCount,+ candidates: candidates,+ targetSamples: rows.filter { $0.id.hasPrefix(targetProfile.id + ":sample-") },+ profiles: profiles,+ inspection: inspectedFrames.isEmpty ? nil : DictionaryAudioInspection(+ samples: samples,+ recordedSampleCount: samples.count,+ frames: inspectedFrames,+ words: inspectedWords,+ selectedStart: 0,+ selectedEnd: context.duration+ )+ )+ }+ #endif+}diff --git a/Sources/Fluid/Services/DictionaryPronunciationDecision.swift b/Sources/Fluid/Services/DictionaryPronunciationDecision.swiftindex ca19ab43..cb588069 100644--- a/Sources/Fluid/Services/DictionaryPronunciationDecision.swift+++ b/Sources/Fluid/Services/DictionaryPronunciationDecision.swift@@ -2,14 +2,29 @@ import Foundation/// Original corrections provide both text and acoustic evidence for the same intended spelling./// These conservative thresholds are evaluated separately from the legacy three-recording preview.-enum DictionaryPronunciationDecision {+nonisolated enum DictionaryPronunciationDecision {static func accepts(score: Float, heardText: String, profile: PronunciationDictionaryProfile) -> Bool {guard score.isFinite else { return false }+ return score >= self.requiredScore(heardText: heardText, profile: profile)+ }++ static func requiredScore(heardText: String, profile: PronunciationDictionaryProfile) -> Float {+ if let threshold = self.overrideThreshold(profile) { return threshold }+ guard profile.hasOriginalAudio else { return 0.70 }let heard = self.normalized(heardText)let knownVariant = profile.enrollments.contains {$0.observedText.map(self.normalized) == heard}- return score >= (knownVariant ? 0.70 : 0.85)+ return knownVariant ? 0.70 : 0.85+ }++ static func overrideThreshold(_ profile: PronunciationDictionaryProfile) -> Float? {+ guard let value = profile.matchThreshold, value.isFinite, (0.4...0.95).contains(value) else { return nil }+ return value+ }++ static func minimumSearchScore(profiles: [PronunciationDictionaryProfile]) -> Float {+ profiles.reduce(Float(0.70)) { min($0, self.overrideThreshold($1) ?? 0.70) }}/// A new spoken possessive is still the enrolled name. Preserve its grammatical ending,diff --git a/Sources/Fluid/Services/DictionaryPronunciationExperiment.swift b/Sources/Fluid/Services/DictionaryPronunciationExperiment.swiftnew file mode 100644index 00000000..4e398ff5--- /dev/null+++ b/Sources/Fluid/Services/DictionaryPronunciationExperiment.swift@@ -0,0 +1,141 @@+import Foundation++/// Local opt-in. Both switches default off in a fresh installation.+nonisolated enum DictionaryPronunciationExperiment {+ static var enabled: Bool { UserDefaults.standard.bool(forKey: "DictionaryEdgeMatchingEnabled") }+ static var captureEnabled: Bool { UserDefaults.standard.bool(forKey: "DictionaryPronunciationDebugCapture") }++ /// Only the outer quiet edges are removed. Internal pauses and low-energy consonants remain.+ static func trimmedRange(_ samples: [Float], within range: Range<Int>? = nil) -> Range<Int>? {+ let range = range ?? 0..<samples.count+ guard range.lowerBound >= 0, range.upperBound <= samples.count, !range.isEmpty else { return nil }+ var first: Int?+ var last = 0+ for start in stride(from: range.lowerBound, to: range.upperBound, by: 320) {+ let end = min(start + 320, range.upperBound)+ let energy = samples[start..<end].reduce(Float(0)) { $0 + $1 * $1 } / Float(end - start)+ if energy.isFinite, energy >= 0.00_001 { // -50 dBFS RMS+ if first == nil { first = start }+ last = end+ }+ }+ guard let first else { return nil }+ return max(range.lowerBound, first - 1920)..<min(range.upperBound, last + 1920)+ }++ // swiftlint:disable:next discouraged_optional_collection+ static func normalized(_ values: [Float]) -> [Float]? {+ guard !values.isEmpty, values.allSatisfy(\.isFinite) else { return nil }+ let norm = sqrt(values.reduce(Float(0)) { $0 + $1 * $1 })+ guard norm > 0, norm.isFinite else { return nil }+ return values.map { $0 / norm }+ }++ static func calibration(_ vectors: [[Float]]) -> (center: [Float], baseline: Float)? {+ let expectedCount = vectors.count+ let vectors = vectors.compactMap(self.normalized)+ guard vectors.count == expectedCount, vectors.count >= 3, let size = vectors.first?.count,+ vectors.allSatisfy({ $0.count == size }) else { return nil }+ var total: Float = 0+ var count = 0+ var center = [Float](repeating: 0, count: size)+ for i in vectors.indices {+ for j in 0..<i {+ total += zip(vectors[i], vectors[j]).reduce(Float(0)) { $0 + $1.0 * $1.1 }+ count += 1+ }+ for d in center.indices {+ center[d] += vectors[i][d]+ }+ }+ let baseline = total / Float(count)+ // A nonpositive baseline cannot define a useful relative scale.+ guard baseline.isFinite, baseline > 0, let center = self.normalized(center) else { return nil }+ return (center, min(1, baseline))+ }+}++extension PronunciationDictionaryProfile {+ var edgeCalibration: (center: [Float], baseline: Float)? {+ guard DictionaryPronunciationExperiment.enabled, !self.hasOriginalAudio,+ self.enrollments.allSatisfy({ $0.edgeEmbedding?.count == self.hiddenSize && ($0.edgeFrameCount ?? 0) > 0 })+ else { return nil }+ return DictionaryPronunciationExperiment.calibration(self.enrollments.compactMap(\.edgeEmbedding))+ }+}++/// Bounded, local-only evidence. Off means no new PCM, embeddings or reports are written.+actor DictionaryPronunciationDebugArchive {+ static let shared = DictionaryPronunciationDebugArchive()+ private let directory: URL?++ init(directory: URL? = nil) { self.directory = directory }+ struct Score: Codable, Sendable {+ let word: String+ let start: Double+ let end: Double+ let raw: Float+ let baseline: Float+ let relative: Float+ let inputEmbedding: [Float]+ }++ private struct Record: Codable {+ let date: Date+ let kind: String+ let model: String+ let samples: Data+ let transcript: String+ let profiles: [PronunciationDictionaryProfile]+ let scores: [Score]+ let inspection: DictionaryAudioInspection?+ let trainingCapture: PronunciationEnrollmentCapture?+ }++ func save(+ kind: String,+ model: String,+ samples: [Float],+ transcript: String,+ profiles: [PronunciationDictionaryProfile],+ scores: [Score] = [],+ inspection: DictionaryAudioInspection? = nil,+ trainingCapture: PronunciationEnrollmentCapture? = nil+ ) {+ guard !Task.isCancelled, DictionaryPronunciationExperiment.captureEnabled, !samples.isEmpty,+ samples.count <= 16_000 * 120 else { return }+ do {+ let root = try self.directory ?? FileManager.default.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: true)+ .appendingPathComponent("FluidVoice/DictionaryDebug", isDirectory: true)+ try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true)+ let encoder = PropertyListEncoder()+ encoder.outputFormat = .binary+ let data = try encoder.encode(Record(+ date: Date(),+ kind: kind,+ model: model,+ samples: samples.withUnsafeBytes { Data($0) },+ transcript: transcript,+ profiles: profiles,+ scores: scores,+ inspection: inspection,+ trainingCapture: trainingCapture+ ))+ guard !Task.isCancelled, DictionaryPronunciationExperiment.captureEnabled else { return }+ try data.write(to: root.appendingPathComponent("\(UUID().uuidString).plist"), options: .atomic)+ let files = try FileManager.default.contentsOfDirectory(at: root, includingPropertiesForKeys: [.contentModificationDateKey, .fileSizeKey])+ .filter { $0.pathExtension == "plist" }+ .compactMap { url -> (URL, Date, Int)? in+ guard let values = try? url.resourceValues(forKeys: [.contentModificationDateKey, .fileSizeKey]) else { return nil }+ return (url, values.contentModificationDate ?? .distantPast, values.fileSize ?? 0)+ }.sorted { $0.1 > $1.1 }+ var bytes = 0+ for (index, file) in files.enumerated() {+ bytes += file.2+ if index >= 50 || bytes > 200_000_000 { try FileManager.default.removeItem(at: file.0) }+ }+ } catch {+ DebugLogger.shared.warning("Dictionary debug archive write failed: \(error.localizedDescription)", source: "PronunciationMatching")+ }+ }+}diff --git a/Sources/Fluid/Services/DictionaryPronunciationReferences.swift b/Sources/Fluid/Services/DictionaryPronunciationReferences.swiftnew file mode 100644index 00000000..3dabeff6--- /dev/null+++ b/Sources/Fluid/Services/DictionaryPronunciationReferences.swift@@ -0,0 +1,34 @@+#if arch(arm64)+import FluidAudio++/// Each enrollment keeps its own center and duration. No cross-recording averaging.+enum DictionaryPronunciationReferences {+ struct Reference {+ let profile: PronunciationDictionaryProfile+ let sampleNumber: Int+ let embedding: PronunciationEmbedding+ }++ static func make(profiles: [PronunciationDictionaryProfile], hiddenSize: Int? = nil) -> [Reference] {+ profiles.flatMap { profile -> [Reference] in+ guard hiddenSize == nil || profile.hiddenSize == hiddenSize else { return [] }+ if let calibration = profile.edgeCalibration {+ let frames = profile.enrollments.compactMap(\.edgeFrameCount)+ return [Reference(profile: profile, sampleNumber: 0, embedding: PronunciationEmbedding(+ values: calibration.center, sourceFrameCount: max(1, frames.reduce(0, +) / frames.count)+ ))]+ }+ return profile.enrollments.enumerated().compactMap { index, capture in+ guard capture.values.count == profile.hiddenSize, capture.sourceFrameCount > 0,+ capture.values.allSatisfy(\.isFinite)+ else { return nil }+ // A singleton prototype only normalizes this recording's vector.+ guard let embedding = PronunciationEmbeddingMatcher.prototype(from: [+ PronunciationEmbedding(values: capture.values, sourceFrameCount: capture.sourceFrameCount),+ ]) else { return nil }+ return Reference(profile: profile, sampleNumber: index + 1, embedding: embedding)+ }+ }+ }+}+#endifdiff --git a/Sources/Fluid/Services/DictionaryReplacementProtection.swift b/Sources/Fluid/Services/DictionaryReplacementProtection.swiftindex 97f8e28a..646ecffd 100644--- a/Sources/Fluid/Services/DictionaryReplacementProtection.swift+++ b/Sources/Fluid/Services/DictionaryReplacementProtection.swift@@ -7,14 +7,15 @@ nonisolated enum DictionaryReplacementProtection {in text: String,regex: NSRegularExpression,template: String,- canonical: NSRegularExpression?+ canonical: NSRegularExpression?,+ onReplacement: ((NSRange, String, String) -> Void)? = nil) -> String {let fullRange = NSRange(text.startIndex..., in: text)- guard let canonical else {+ guard canonical != nil || onReplacement != nil else {return regex.stringByReplacingMatches(in: text, range: fullRange, withTemplate: template)}- let completed = canonical.matches(in: text, range: fullRange).map(\.range)- guard !completed.isEmpty else {+ let completed = canonical?.matches(in: text, range: fullRange).map(\.range) ?? []+ guard !completed.isEmpty || onReplacement != nil else {return regex.stringByReplacingMatches(in: text, range: fullRange, withTemplate: template)}@@ -32,11 +33,28 @@ nonisolated enum DictionaryReplacementProtection {guard !matches.isEmpty else { return text }let output = NSMutableString(string: text)for match in matches.reversed() {+ let replacement = regex.replacementString(for: match, in: text, offset: 0, template: template)+ if let onReplacement {+ let heard = (text as NSString).substring(with: match.range)+ if heard != replacement {+ onReplacement(match.range, heard, replacement)+ }+ }output.replaceCharacters(in: match.range,- with: regex.replacementString(for: match, in: text, offset: 0, template: template)+ with: replacement)}return output as String}}++/// Keep support logs bounded and on one line, including unusual user-defined rules.+nonisolated enum DictionaryReplacementDiagnostics {+ static let maximumEvents = 16++ static func quoted(_ text: String) -> String {+ let bounded = text.prefix(161)+ return (String(bounded.prefix(160)) + (bounded.count > 160 ? "…" : "")).debugDescription+ }+}diff --git a/Sources/Fluid/Services/FluidAudioProvider.swift b/Sources/Fluid/Services/FluidAudioProvider.swiftindex 268543ba..ae80dd2b 100644--- a/Sources/Fluid/Services/FluidAudioProvider.swift+++ b/Sources/Fluid/Services/FluidAudioProvider.swift@@ -114,6 +114,7 @@ final class FluidAudioProvider: TranscriptionProvider {private(set) var boostedVocabularyTermsCount: Int = 0private var boostedTermLookup: [String] = []private var pronunciationModelKey = ""+ private var edgeReferenceCache: [UUID: PronunciationEmbedding] = [:]/// Optional model override - if set, uses this model instead of the global setting./// Used for downloading specific models without changing the active selection.@@ -167,7 +168,8 @@ final class FluidAudioProvider: TranscriptionProvider {private var automaticPronunciationMatchingEnabled: Bool {self.meetingOptions == nil && self.enhancementOptions == nil- && SettingsStore.shared.automaticDictionaryLearningEnabled+ && (SettingsStore.shared.automaticDictionaryLearningEnabled+ || self.automaticPronunciationProfiles.contains { $0.automaticMatchingEnabled == true })}private var effectivePronunciationMatchingEnabled: Bool {@@ -179,7 +181,7 @@ final class FluidAudioProvider: TranscriptionProvider {private func refreshAutomaticPronunciationProfiles() async throws {guard !self.didLoadAutomaticPronunciationProfiles else { return }let generation = self.recordingGeneration- guard self.automaticPronunciationMatchingEnabled, !self.effectiveCustomDictionaryEntries.isEmpty else {+ guard self.meetingOptions == nil, self.enhancementOptions == nil, !self.effectiveCustomDictionaryEntries.isEmpty else {self.automaticPronunciationProfiles = []self.didLoadAutomaticPronunciationProfiles = truereturn@@ -187,19 +189,22 @@ final class FluidAudioProvider: TranscriptionProvider {let stored = await self.pronunciationStore.profiles(modelKey: self.pronunciationModelKey)try self.requireCurrentRecording(generation)self.automaticPronunciationProfiles = Self.matchingProfiles(- stored, entries: self.effectiveCustomDictionaryEntries, includeManual: false+ stored,+ entries: self.effectiveCustomDictionaryEntries,+ includeManual: false,+ includeOriginal: SettingsStore.shared.automaticDictionaryLearningEnabled)self.didLoadAutomaticPronunciationProfiles = true}static func matchingProfiles(_ stored: [PronunciationDictionaryProfile],- entries: [SettingsStore.CustomDictionaryEntry], includeManual: Bool+ entries: [SettingsStore.CustomDictionaryEntry], includeManual: Bool, includeOriginal: Bool = true) -> [PronunciationDictionaryProfile] {let labels = Self.dictionaryLabels(from: entries)return stored.filter { profile inguard profile.isEligibleForMatching,- includeManual || profile.hasOriginalAudio,+ includeManual || profile.automaticMatchingEnabled == true || (includeOriginal && profile.hasOriginalAudio),let label = labels[profile.dictionaryEntryID]else { return false }return !profile.hasOriginalAudio || profile.label.caseInsensitiveCompare(label) == .orderedSame@@ -526,6 +531,10 @@ final class FluidAudioProvider: TranscriptionProvider {}func transcribeDictionaryTraining(_ samples: [Float]) async throws -> ASRTranscriptionResult {+ try await self.transcribeDictionaryTraining(samples, capturePronunciation: self.explicitPronunciationMatchingEnabled)+ }++ func transcribeDictionaryTraining(_ samples: [Float], capturePronunciation: Bool) async throws -> ASRTranscriptionResult {guard let manager = self.streamingAsrManager else {throw NSError(domain: "FluidAudioProvider",@@ -533,19 +542,30 @@ final class FluidAudioProvider: TranscriptionProvider {userInfo: [NSLocalizedDescriptionKey: "ASR manager not initialized"])}- let shouldCapture = self.explicitPronunciationMatchingEnabled+ let shouldCapture = capturePronunciationawait manager.setPronunciationCustomizationEnabled(shouldCapture)do {let result = try await manager.transcribe(samples, source: AudioSource.microphone)let features = await manager.consumePronunciationEncoderFeatures()await manager.setPronunciationCustomizationEnabled(false)- return ASRTranscriptionResult(- text: result.text,- confidence: result.confidence,- pronunciationEnrollment: shouldCapture- ? self.makeEnrollment(result: result, features: features)- : nil+ var capture = shouldCapture ? self.makeEnrollment(result: result, features: features, samples: samples) : nil+ if capture != nil, DictionaryPronunciationExperiment.enabled,+ let range = DictionaryPronunciationExperiment.trimmedRange(samples)+ {+ let embedding = try await self.encodeEdge(Array(samples[range]), manager: manager)+ capture?.edgeEmbedding = embedding.values+ capture?.edgeFrameCount = embedding.sourceFrameCount+ }+ await DictionaryPronunciationDebugArchive.shared.save(+ kind: "training",+ model: self.pronunciationModelKey,+ samples: samples,+ transcript: result.text,+ profiles: [],+ inspection: capture?.pendingInspection,+ trainingCapture: capture)+ return ASRTranscriptionResult(text: result.text, confidence: result.confidence, pronunciationEnrollment: capture)} catch {_ = await manager.consumePronunciationEncoderFeatures()await manager.setPronunciationCustomizationEnabled(false)@@ -669,15 +689,25 @@ final class FluidAudioProvider: TranscriptionProvider {let appendFinishedAt = ProcessInfo.processInfo.systemUptimelet result = try await session.finish(finalAudioSamples: samples)try self.requireCurrentRecording(generation)- let matches = await session.pronunciationMatches+ var matches = await session.pronunciationMatcheslet originalText = await session.unboostedTexttry self.requireCurrentRecording(generation)+ let eligibleIDs = Set(Self.matchingProfiles(+ profiles,+ entries: self.effectiveCustomDictionaryEntries,+ includeManual: self.explicitPronunciationMatchingEnabled,+ includeOriginal: SettingsStore.shared.automaticDictionaryLearningEnabled+ ).map(\.dictionaryEntryID))+ if let manager = self.finalAsrManager ?? self.streamingAsrManager, originalText == result.text {+ matches = try await self.refineEdgeMatches(matches, profiles: profiles, samples: samples, manager: manager, transcript: result.text, timings: result.tokenTimings ?? [])+ }+ try self.requireCurrentRecording(generation)let text = self.effectivePronunciationMatchingEnabled && originalText == result.text? Self.applyPronunciationMatches(result: result,matches: matches,profiles: profiles,- labels: Self.dictionaryLabels(from: self.effectiveCustomDictionaryEntries)+ labels: Self.dictionaryLabels(from: self.effectiveCustomDictionaryEntries).filter { eligibleIDs.contains($0.key) }) : result.textlet finishFinishedAt = ProcessInfo.processInfo.systemUptimeDebugLogger.shared.debug(@@ -733,28 +763,23 @@ final class FluidAudioProvider: TranscriptionProvider {return Self.matchingProfiles(stored,entries: self.effectiveCustomDictionaryEntries,- includeManual: self.explicitPronunciationMatchingEnabled+ includeManual: self.explicitPronunciationMatchingEnabled,+ includeOriginal: SettingsStore.shared.automaticDictionaryLearningEnabled)}- private static func pronunciationPrototype(_ profile: PronunciationDictionaryProfile) -> PronunciationEmbedding? {- PronunciationEmbeddingMatcher.prototype(from: profile.enrollments.map {- PronunciationEmbedding(values: $0.values, sourceFrameCount: $0.sourceFrameCount)- })- }-private func startIncrementalSession(manager: AsrManager) async throws {let generation = self.recordingGeneration- let profiles = await self.pronunciationProfiles()+ let profiles = try await self.prepareEdgeProfiles(await self.pronunciationProfiles(), manager: manager)try self.requireCurrentRecording(generation)- let pairs = profiles.compactMap { profile in- Self.pronunciationPrototype(profile).map { (profile, $0) }- }+ let references = DictionaryPronunciationReferences.make(profiles: profiles)let session = try await manager.makeIncrementalSession(- source: .microphone, pronunciationPrototypes: pairs.map { $0.1 }+ source: .microphone,+ pronunciationPrototypes: references.map(\.embedding),+ pronunciationThreshold: profiles.contains { $0.edgeCalibration != nil } ? 0.25 : DictionaryPronunciationDecision.minimumSearchScore(profiles: profiles))try self.requireCurrentRecording(generation)- self.incrementalPronunciationProfiles = pairs.map { $0.0 }+ self.incrementalPronunciationProfiles = references.map(\.profile)self.incrementalSession = sessionself.incrementalAcceptedSampleCount = 0}@@ -811,7 +836,7 @@ final class FluidAudioProvider: TranscriptionProvider {return try (await self.transcribeIncrementalFinal(samples), nil, true)}let matchingEnabled = self.effectivePronunciationMatchingEnabled && samples.count <= 16_000 * 15- let profiles = matchingEnabled ? await self.pronunciationProfiles() : []+ let profiles = matchingEnabled ? try await self.prepareEdgeProfiles(await self.pronunciationProfiles(), manager: manager) : []// Rewritten text leaves timings on the old tokens; realigning would revert corrections.let textMayBeCorrected = self.isWordBoostingActive || !profiles.isEmptyawait manager.setPronunciationCustomizationEnabled(!profiles.isEmpty)@@ -823,7 +848,19 @@ final class FluidAudioProvider: TranscriptionProvider {return (ASRTranscriptionResult(text: result.text, confidence: result.confidence, dictionaryLearningAlignment: self.learningAlignment(for: result)), result.tokenTimings, textMayBeCorrected)}let startedAt = Date().timeIntervalSince1970- let corrected = self.applyPronunciationMatches(result: result, features: features, profiles: profiles)+ let references = DictionaryPronunciationReferences.make(profiles: profiles, hiddenSize: features.hiddenSize)+ let matches = PronunciationEmbeddingMatcher.allMatches(+ prototypes: references.map(\.embedding),+ in: features,+ threshold: references.contains { $0.profile.edgeCalibration != nil } ? 0.25 : DictionaryPronunciationDecision.minimumSearchScore(profiles: profiles),+ windowFrameCounts: references.map { reference in+ reference.profile.edgeCalibration != nil ? Array(5...32) : PronunciationEmbeddingMatcher.nearbyWindowCounts(around: reference.embedding.sourceFrameCount)+ }+ ).enumerated().flatMap { index, hits in+ hits.map { PronunciationWindowMatch(prototypeIndex: index, score: $0.score, frameRange: $0.frameRange) }+ }+ let refined = try await self.refineEdgeMatches(matches, profiles: references.map(\.profile), samples: samples, manager: manager, transcript: result.text, timings: result.tokenTimings ?? [])+ let corrected = Self.applyPronunciationMatches(result: result, matches: refined, profiles: references.map(\.profile), labels: Self.dictionaryLabels(from: self.effectiveCustomDictionaryEntries))let elapsedMs = Int(((Date().timeIntervalSince1970 - startedAt) * 1000).rounded())DebugLogger.shared.info("PRONUNCIATION_MATCH profiles=\(profiles.count) elapsedMs=\(elapsedMs) changed=\(corrected != result.text)",@@ -837,9 +874,112 @@ final class FluidAudioProvider: TranscriptionProvider {}}+ private func encodeEdge(_ samples: [Float], manager: AsrManager) async throws -> PronunciationEmbedding {+ let end = min(samples.count, max(1, Int((Double(samples.count) / 1280).rounded()) * 1280))+ return try await manager.pronunciationEmbedding(audioSamples: samples, focalSampleRange: 0..<end)+ }++ private func prepareEdgeProfiles(_ profiles: [PronunciationDictionaryProfile], manager: AsrManager) async throws -> [PronunciationDictionaryProfile] {+ guard DictionaryPronunciationExperiment.enabled else { return profiles }+ var prepared = profiles+ for index in prepared.indices where !prepared[index].hasOriginalAudio {+ for sample in prepared[index].enrollments.indices {+ let capture = prepared[index].enrollments[sample]+ guard capture.edgeEmbedding == nil, let id = capture.inspectionID else { continue }+ var embedding = self.edgeReferenceCache[id]+ if embedding == nil, let audio = try? await self.pronunciationStore.inspection(for: id),+ let range = DictionaryPronunciationExperiment.trimmedRange(Array(audio.samples.prefix(audio.recordedSampleCount)))+ {+ embedding = try await self.encodeEdge(Array(audio.samples[range]), manager: manager)+ if self.edgeReferenceCache.count >= 100 { self.edgeReferenceCache.removeAll(keepingCapacity: true) }+ self.edgeReferenceCache[id] = embedding+ }+ prepared[index].enrollments[sample].edgeEmbedding = embedding?.values+ prepared[index].enrollments[sample].edgeFrameCount = embedding?.sourceFrameCount+ }+ }+ return prepared+ }++ private func refineEdgeMatches(+ _ matches: [PronunciationWindowMatch],+ profiles: [PronunciationDictionaryProfile],+ samples: [Float],+ manager: AsrManager,+ transcript: String,+ timings: [TokenTiming]+ ) async throws -> [PronunciationWindowMatch] {+ let calibrated = profiles.map(\.edgeCalibration)+ var output = matches.filter { calibrated.indices.contains($0.prototypeIndex) && calibrated[$0.prototypeIndex] == nil }+ var reports: [DictionaryPronunciationDebugArchive.Score] = []+ let words = WordAudioChunkExtractor.words(from: timings)+ let wordIndex = WordAudioOverlapIndex(words: words)+ var used: [Int: [Range<Int>]] = [:]+ var encoded: [Range<Int>: PronunciationEmbedding] = [:]+ // Bounded experimental reranking: up to three non-overlapping candidates per word, 24 encoder calls total.+ for match in matches.sorted(by: { $0.score > $1.score }) {+ try Task.checkCancellation()+ guard calibrated.indices.contains(match.prototypeIndex), let calibration = calibrated[match.prototypeIndex] else { continue }+ // Score the complete overlapping transcript span, never just the prefix of a split word.+ let indices = wordIndex.substantiallyOverlappingWordIndices(+ startTime: Double(match.frameRange.lowerBound) * 0.08,+ endTime: Double(match.frameRange.upperBound) * 0.08,+ minimumOverlapRatio: 0+ )+ guard let first = indices.first, let last = indices.last else { continue }+ let firstFrame = max(0, Int(floor(words[first].startTime / 0.08)))+ let lastFrame = max(firstFrame + 1, Int(ceil(words[last].endTime / 0.08)))+ let alignedFrames = firstFrame..<lastFrame+ let lower = min(samples.count, firstFrame * 1280)+ let upper = min(samples.count, max(lower, lastFrame * 1280))+ let range = lower..<upper+ guard !range.isEmpty, range.count <= 16_000 * 15,+ (used[match.prototypeIndex]?.count ?? 0) < 3,+ !(used[match.prototypeIndex] ?? []).contains(where: { $0.overlaps(range) }),+ let trimmed = DictionaryPronunciationExperiment.trimmedRange(samples, within: range) else { continue }+ guard encoded.count < 24 || encoded[trimmed] != nil else { break }+ let embedding: PronunciationEmbedding+ if let existing = encoded[trimmed] {+ embedding = existing+ } else {+ do {+ embedding = try await self.encodeEdge(Array(samples[trimmed]), manager: manager)+ } catch {+ if error is CancellationError || Task.isCancelled { throw CancellationError() }+ DebugLogger.shared.warning("Dictionary candidate encoding failed; leaving this candidate unchanged", source: "PronunciationMatching")+ continue+ }+ encoded[trimmed] = embedding+ }+ used[match.prototypeIndex, default: []].append(range)+ let raw = zip(embedding.values, calibration.center).reduce(Float(0)) { $0 + $1.0 * $1.1 }+ let relative = raw / calibration.baseline+ output.append(PronunciationWindowMatch(prototypeIndex: match.prototypeIndex, score: relative, frameRange: alignedFrames))+ reports.append(.init(+ word: profiles[match.prototypeIndex].label,+ start: Double(trimmed.lowerBound) / 16_000,+ end: Double(trimmed.upperBound) / 16_000,+ raw: raw,+ baseline: calibration.baseline,+ relative: relative,+ inputEmbedding: embedding.values+ ))+ }+ await DictionaryPronunciationDebugArchive.shared.save(+ kind: "dictation",+ model: self.pronunciationModelKey,+ samples: samples,+ transcript: transcript,+ profiles: profiles,+ scores: reports+ )+ return output+ }+private func makeEnrollment(result: ASRResult,- features: EncoderFeatureSequence?+ features: EncoderFeatureSequence?,+ samples: [Float]) -> PronunciationEnrollmentCapture? {guard let features,let timings = result.tokenTimings,@@ -851,42 +991,24 @@ final class FluidAudioProvider: TranscriptionProvider {guard start < end,let embedding = PronunciationEmbeddingMatcher.embedding(from: features, frameRange: start..<end)else { return nil }- return PronunciationEnrollmentCapture(+ var capture = PronunciationEnrollmentCapture(values: embedding.values,sourceFrameCount: embedding.sourceFrameCount,modelKey: self.pronunciationModelKey)- }-- private func applyPronunciationMatches(- result: ASRResult,- features: EncoderFeatureSequence,- profiles: [PronunciationDictionaryProfile]- ) -> String {- guard let timings = result.tokenTimings else { return result.text }- let words = WordAudioChunkExtractor.words(from: timings)- guard !words.isEmpty else { return result.text }-- let usableProfiles = profiles.compactMap { profile -> (PronunciationDictionaryProfile, PronunciationEmbedding)? in- let embeddings = profile.enrollments.map {- PronunciationEmbedding(values: $0.values, sourceFrameCount: $0.sourceFrameCount)- }- guard profile.hiddenSize == features.hiddenSize,- let prototype = PronunciationEmbeddingMatcher.prototype(from: embeddings)- else { return nil }- return (profile, prototype)- }- let matches = PronunciationEmbeddingMatcher.allMatches(- prototypes: usableProfiles.map { $0.1 }, in: features- ).enumerated().flatMap { index, hits in- hits.map { PronunciationWindowMatch(prototypeIndex: index, score: $0.score, frameRange: $0.frameRange) }+ if samples.count <= 16_000 * 15,+ DictionaryPronunciationExperiment.captureEnabled+ {+ capture.pendingInspection = DictionaryAudioInspection(+ samples: samples,+ recordedSampleCount: samples.count,+ frames: [.init(offset: 0, frameDuration: features.frameDuration, hiddenSize: features.hiddenSize, values: features.values)],+ words: WordAudioChunkExtractor.words(from: timings).map { .init(text: $0.text, start: $0.startTime, end: $0.endTime) },+ selectedStart: Double(start) * features.frameDuration,+ selectedEnd: Double(end) * features.frameDuration+ )}- return Self.applyPronunciationMatches(- result: result,- matches: matches,- profiles: usableProfiles.map { $0.0 },- labels: Self.dictionaryLabels(from: self.effectiveCustomDictionaryEntries)- )+ return capture}static func applyPronunciationMatches(@@ -902,13 +1024,15 @@ final class FluidAudioProvider: TranscriptionProvider {let wordIndex = WordAudioOverlapIndex(words: words)struct Candidate {+ let entryID: UUIDlet label: Stringlet score: Floatlet wordIndices: [Int]+ let threshold: Float}var candidates: [Candidate] = []for match in matches {- guard match.score.isFinite, match.score >= PronunciationCustomizationDefaults.acceptanceThreshold,+ guard match.score.isFinite,profiles.indices.contains(match.prototypeIndex),let label = labels[profiles[match.prototypeIndex].dictionaryEntryID],!match.frameRange.isEmpty, match.frameRange.lowerBound >= 0@@ -921,18 +1045,16 @@ final class FluidAudioProvider: TranscriptionProvider {)guard !indices.isEmpty else { continue }let profile = profiles[match.prototypeIndex]+ let heard = indices.map { words[$0].text }.joined(separator: " ")+ guard DictionaryPronunciationDecision.accepts(score: match.score, heardText: heard, profile: profile) else { continue }var correctedLabel = labelif profile.hasOriginalAudio {guard profile.label.caseInsensitiveCompare(label) == .orderedSame else { continue }- let heard = indices.map { words[$0].text }.joined(separator: " ")- guard DictionaryPronunciationDecision.accepts(- score: match.score, heardText: heard, profile: profile- ) else { continue }correctedLabel = DictionaryPronunciationDecision.labelPreservingPossessive(label, heardText: heard, profile: profile)}- candidates.append(Candidate(label: correctedLabel, score: match.score, wordIndices: indices))+ candidates.append(Candidate(entryID: profile.dictionaryEntryID, label: correctedLabel, score: match.score, wordIndices: indices, threshold: DictionaryPronunciationDecision.requiredScore(heardText: heard, profile: profile)))}var leaders: [Int: [(label: String, score: Float)]] = [:]@@ -963,10 +1085,18 @@ final class FluidAudioProvider: TranscriptionProvider {guard !ambiguous else { continue }accepted.append(candidate)claimed.formUnion(candidate.wordIndices)- DebugLogger.shared.info(- "PRONUNCIATION_HIT score=\(String(format: "%.3f", candidate.score)) words=\(candidate.wordIndices)",- source: "PronunciationMatching"- )+ if accepted.count <= DictionaryReplacementDiagnostics.maximumEvents {+ let heard = candidate.wordIndices.map { words[$0].text }.joined(separator: " ")+ DebugLogger.shared.debug(+ "PRONUNCIATION_HIT stage=acoustic_candidate entryID=\(candidate.entryID) " ++ "score=\(String(format: "%.3f", candidate.score)) " ++ "threshold=\(candidate.threshold) " ++ "heard=\(DictionaryReplacementDiagnostics.quoted(heard)) " ++ "replacement=\(DictionaryReplacementDiagnostics.quoted(candidate.label)) " ++ "words=\(candidate.wordIndices)",+ source: "PronunciationMatching"+ )+ }}guard !accepted.isEmpty else { return result.text }diff --git a/Sources/Fluid/Services/PronunciationDictionaryStore.swift b/Sources/Fluid/Services/PronunciationDictionaryStore.swiftindex 8d165921..b4e6a5c0 100644--- a/Sources/Fluid/Services/PronunciationDictionaryStore.swift+++ b/Sources/Fluid/Services/PronunciationDictionaryStore.swift@@ -9,6 +9,18 @@ struct PronunciationEnrollmentCapture: Codable, Equatable, Sendable {var sourceRecordingID: UUID?var sourceFocalRange: Range<Int>?var extractorVersion: String?+ var inspectionID: UUID?+ // nil identifies legacy enrollment; an empty vector is invalid.+ // swiftlint:disable:next discouraged_optional_collection+ var edgeEmbedding: [Float]?+ var edgeFrameCount: Int?+ // Only held until the profile save. Large evidence is stored separately on disk.+ var pendingInspection: DictionaryAudioInspection? = nil++ private enum CodingKeys: String, CodingKey {+ case values, sourceFrameCount, modelKey, originalAudioID, observedText+ case sourceRecordingID, sourceFocalRange, extractorVersion, inspectionID, edgeEmbedding, edgeFrameCount+ }}struct PronunciationDictionaryProfile: Codable, Equatable, Identifiable, Sendable {@@ -17,6 +29,8 @@ struct PronunciationDictionaryProfile: Codable, Equatable, Identifiable, Sendabllet modelKey: Stringlet hiddenSize: Intvar enrollments: [PronunciationEnrollmentCapture]+ var automaticMatchingEnabled: Bool? = nil+ var matchThreshold: Float? = nilvar hasOriginalAudio: Bool { self.enrollments.contains { $0.originalAudioID != nil } }var isEligibleForMatching: Bool {@@ -96,7 +110,8 @@ actor PronunciationDictionaryStore {dictionaryEntryID: UUID,label: String,modelKey: String,- enrollments: [PronunciationEnrollmentCapture]+ enrollments: [PronunciationEnrollmentCapture],+ automaticMatchingEnabled: Bool = false) throws {guard let first = enrollments.first, !first.values.isEmpty else { return }guard enrollments.allSatisfy({ $0.modelKey == modelKey && $0.values.count == first.values.count }) else {@@ -108,7 +123,39 @@ actor PronunciationDictionaryStore {$0.dictionaryEntryID == dictionaryEntryID && $0.modelKey == modelKey})let existingEnrollments = existingIndex.map { profiles[$0].enrollments } ?? []- let combinedEnrollments = existingEnrollments + enrollments+ var prepared = enrollments+ var writtenInspectionIDs: [UUID] = []+ do {+ for index in prepared.indices {+ guard let evidence = prepared[index].pendingInspection else { continue }+ guard evidence.isValid, evidence.duration <= 15,+ evidence.frames.allSatisfy({ $0.hiddenSize == first.values.count })+ else { throw PronunciationDictionaryStoreError.inconsistentEnrollment }+ let id = UUID()+ let url = self.inspectionURL(id)+ try FileManager.default.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true)+ let encoder = PropertyListEncoder()+ encoder.outputFormat = .binary+ try encoder.encode(evidence).write(to: url, options: .atomic)+ writtenInspectionIDs.append(id)+ prepared[index].inspectionID = id+ prepared[index].pendingInspection = nil+ }+ } catch {+ for id in writtenInspectionIDs {+ try? FileManager.default.removeItem(at: self.inspectionURL(id))+ }+ throw error+ }+ var didPersist = false+ defer {+ if !didPersist {+ for id in writtenInspectionIDs {+ try? FileManager.default.removeItem(at: self.inspectionURL(id))+ }+ }+ }+ let combinedEnrollments = existingEnrollments + preparedguard combinedEnrollments.allSatisfy({$0.modelKey == modelKey && $0.values.count == first.values.count}) else {@@ -119,7 +166,9 @@ actor PronunciationDictionaryStore {label: label,modelKey: modelKey,hiddenSize: first.values.count,- enrollments: Array(combinedEnrollments.suffix(10))+ enrollments: Array(combinedEnrollments.suffix(10)),+ automaticMatchingEnabled: automaticMatchingEnabled || existingIndex.map { profiles[$0].automaticMatchingEnabled == true } == true,+ matchThreshold: existingIndex.flatMap { profiles[$0].matchThreshold })if let index = existingIndex {profiles[index] = profile@@ -130,6 +179,44 @@ actor PronunciationDictionaryStore {version: 1,profiles: profiles))+ didPersist = true+ let retained = Set(profile.enrollments.compactMap(\.inspectionID))+ for id in writtenInspectionIDs where !retained.contains(id) {+ try? FileManager.default.removeItem(at: self.inspectionURL(id))+ }+ }++ /// Reads only the selected example. Legacy profiles explicitly have no evidence.+ func inspection(for id: UUID) throws -> DictionaryAudioInspection {+ let url = self.inspectionURL(id)+ let attributes = try url.resourceValues(forKeys: [.fileSizeKey])+ guard let size = attributes.fileSize, size <= 24_000_000 else {+ throw PronunciationDictionaryStoreError.inconsistentEnrollment+ }+ let value = try PropertyListDecoder().decode(DictionaryAudioInspection.self, from: Data(contentsOf: url))+ guard value.isValid else { throw PronunciationDictionaryStoreError.inconsistentEnrollment }+ return value+ }++ private func inspectionURL(_ id: UUID) -> URL {+ self.fileURL.deletingLastPathComponent().appendingPathComponent("pronunciation-inspection", isDirectory: true)+ .appendingPathComponent(id.uuidString).appendingPathExtension("plist")+ }++ /// Updates only this word's matching level; preserves recordings and text rules.+ func setMatchThreshold(_ threshold: Float, dictionaryEntryID: UUID) throws {+ guard threshold.isFinite, (0.4...0.95).contains(threshold) else {+ throw PronunciationDictionaryStoreError.inconsistentEnrollment+ }+ self.loadIfNeeded()+ var profiles = self.document?.profiles ?? []+ guard profiles.contains(where: { $0.dictionaryEntryID == dictionaryEntryID }) else {+ throw PronunciationDictionaryStoreError.staleEvidence+ }+ for index in profiles.indices where profiles[index].dictionaryEntryID == dictionaryEntryID {+ profiles[index].matchThreshold = threshold+ }+ try self.persist(Document(version: 1, profiles: profiles))}func replaceAllProfiles(_ profiles: [PronunciationDictionaryProfile]) throws {@@ -340,6 +427,7 @@ actor PronunciationDictionaryStore {}self.document = decodedself.removeOrphanedAudio()+ self.removeOrphanedInspections()}/// A crash before the profile commit leaves an inactive clip; discard it after loading valid metadata.@@ -360,6 +448,20 @@ actor PronunciationDictionaryStore {}}+ private func removeOrphanedInspections() {+ let referenced = Set((self.document?.profiles ?? []).flatMap(\.enrollments).compactMap(\.inspectionID))+ guard let files = try? FileManager.default.contentsOfDirectory(+ at: self.inspectionURL(UUID()).deletingLastPathComponent(), includingPropertiesForKeys: [.contentModificationDateKey]+ ) else { return }+ for file in files where file.pathExtension == "plist" {+ guard let id = UUID(uuidString: file.deletingPathExtension().lastPathComponent), !referenced.contains(id),+ let values = try? file.resourceValues(forKeys: [.contentModificationDateKey]),+ let modified = values.contentModificationDate, modified.timeIntervalSinceNow < -60+ else { continue }+ try? FileManager.default.removeItem(at: file)+ }+ }+private func persist(_ updated: Document) throws {try FileManager.default.createDirectory(at: self.fileURL.deletingLastPathComponent(),@@ -372,7 +474,12 @@ actor PronunciationDictionaryStore {options: .atomic)let oldIDs = Set((self.document?.profiles ?? []).flatMap(\.enrollments).compactMap(\.originalAudioID))+ let oldInspectionIDs = Set((self.document?.profiles ?? []).flatMap(\.enrollments).compactMap(\.inspectionID))+ let newInspectionIDs = Set(updated.profiles.flatMap(\.enrollments).compactMap(\.inspectionID))self.document = updated+ for id in oldInspectionIDs.subtracting(newInspectionIDs) {+ try? FileManager.default.removeItem(at: self.inspectionURL(id))+ }let newIDs = Set(updated.profiles.flatMap(\.enrollments).compactMap(\.originalAudioID))for id in oldIDs.subtracting(newIDs) {try? FileManager.default.removeItem(at: self.audioURL(id))diff --git a/Sources/Fluid/Services/TranscriptionProvider.swift b/Sources/Fluid/Services/TranscriptionProvider.swiftindex ac322b12..99351bf6 100644--- a/Sources/Fluid/Services/TranscriptionProvider.swift+++ b/Sources/Fluid/Services/TranscriptionProvider.swift@@ -164,6 +164,7 @@ protocol TranscriptionProvider {/// Transcribe audio captured while training dictionary replacements./// Providers can bypass final-output transforms that would distort the saved phrase.func transcribeDictionaryTraining(_ samples: [Float]) async throws -> ASRTranscriptionResult+ func transcribeDictionaryTraining(_ samples: [Float], capturePronunciation: Bool) async throws -> ASRTranscriptionResult/// Meeting word alignment only; dictation never calls the method this gates.var supportsWordTimings: Bool { get }@@ -208,6 +209,10 @@ extension TranscriptionProvider {try await self.transcribe(samples)}+ func transcribeDictionaryTraining(_ samples: [Float], capturePronunciation: Bool) async throws -> ASRTranscriptionResult {+ try await self.transcribeDictionaryTraining(samples)+ }+func transcribeDictionaryTraining(_ samples: [Float]) async throws -> ASRTranscriptionResult {try await self.transcribeFinal(samples)}diff --git a/Sources/Fluid/Services/VoiceTrainingAliasFilter.swift b/Sources/Fluid/Services/VoiceTrainingAliasFilter.swiftnew file mode 100644index 00000000..7998aa93--- /dev/null+++ b/Sources/Fluid/Services/VoiceTrainingAliasFilter.swift@@ -0,0 +1,95 @@+import AppKit++/// Voice captures may teach pronunciation without creating a global text replacement.+/// A spell-check miss is only an eligibility guard, not proof that an alias is correct.+@MainActor+enum VoiceTrainingAliasFilter {+ struct Result {+ let accepted: [String]+ let rejected: [String]+ let lookupAvailable: Bool+ }++ enum LookupResult: Sendable {+ case checked([NSRange])+ case unavailable+ }++ typealias Lookup = @MainActor (String) async -> LookupResult++ static func filter(_ aliases: [String], lookup: Lookup = checkSpelling) async -> Result {+ // Training already caps captures at 20. Bound this API independently too.+ let bounded = Array(aliases.prefix(20))+ var text = ""+ var candidates: [(alias: String, range: NSRange)] = []+ for alias in bounded {+ guard !alias.isEmpty, alias.count <= 160, alias.contains(where: \.isLetter) else { continue }+ let normalized = alias.lowercased().split(whereSeparator: \.isWhitespace).joined(separator: " ")+ let range = NSRange(location: text.utf16.count, length: normalized.utf16.count)+ text += normalized + "\n"+ candidates.append((alias, range))+ }+ guard !candidates.isEmpty else {+ return Result(accepted: [], rejected: aliases, lookupAvailable: true)+ }+ guard case let .checked(misspellings) = await lookup(text), !Task.isCancelled else {+ // Never interpret an unavailable dictionary as permission to add every alias.+ return Result(accepted: [], rejected: aliases, lookupAvailable: false)+ }+ let accepted = candidates.filter { candidate in+ misspellings.contains { NSIntersectionRange($0, candidate.range).length > 0 }+ }.map(\.alias)+ let acceptedSet = Set(accepted)+ return Result(accepted: accepted, rejected: aliases.filter { !acceptedSet.contains($0) }, lookupAvailable: true)+ }++ /// English vocabulary is checked locally; no remote API or model inference.+ /// This is called only when saving voice training, never during live dictation/rendering.+ static func checkSpelling(_ text: String) async -> LookupResult {+ let request = SpellingRequest()+ return await withCheckedContinuation { continuation in+ request.start(text, continuation: continuation)+ }+ }++ @MainActor+ private final class SpellingRequest {+ private var continuation: CheckedContinuation<LookupResult, Never>?+ private var timeout: Task<Void, Never>?+ private var documentTag: Int?++ func start(_ text: String, continuation: CheckedContinuation<LookupResult, Never>) {+ self.continuation = continuation+ let tag = NSSpellChecker.uniqueSpellDocumentTag()+ self.documentTag = tag+ self.timeout = Task { @MainActor in+ do { try await Task.sleep(for: .seconds(2)) } catch { return }+ self.finish(.unavailable)+ }+ NSSpellChecker.shared.requestChecking(+ of: text,+ range: NSRange(text.startIndex..., in: text),+ types: NSTextCheckingResult.CheckingType.spelling.rawValue,+ options: [.orthography: NSOrthography.defaultOrthography(forLanguage: "en_US")],+ inSpellDocumentWithTag: tag+ ) { @Sendable _, results, _, wordCount in+ let ranges = results.filter { $0.resultType == .spelling }.map(\.range)+ Task { @MainActor in+ self.finish(wordCount > 0 ? .checked(ranges) : .unavailable)+ }+ }+ }++ private func finish(_ result: LookupResult) {+ guard let continuation = self.continuation else { return }+ self.continuation = nil+ self.timeout?.cancel()+ self.timeout = nil+ if let documentTag = self.documentTag {+ NSSpellChecker.shared.closeSpellDocument(withTag: documentTag)+ self.documentTag = nil+ }+ continuation.resume(returning: result)+ }+ }+}diff --git a/Sources/Fluid/UI/CustomDictionaryView.swift b/Sources/Fluid/UI/CustomDictionaryView.swiftindex 2811ec4c..d7164b0a 100644--- a/Sources/Fluid/UI/CustomDictionaryView.swift+++ b/Sources/Fluid/UI/CustomDictionaryView.swift@@ -13,6 +13,11 @@ import UniformTypeIdentifiers// This legacy screen still owns several dictionary editors; split them into standalone views incrementally.// swiftlint:disable:next type_body_lengthstruct CustomDictionaryView: View {+ var formattingOnly = false+ @State private var isWordDrawerPresented = false+ @State private var wordSearch = ""+ @State private var isDrawerActionsPresented = false+ @State private var drawerDeletion: SettingsStore.CustomDictionaryEntry?@Environment(\.theme) private var theme@Environment(\.accessibilityReduceMotion) private var reduceMotion@EnvironmentObject private var appServices: AppServices@@ -34,7 +39,10 @@ struct CustomDictionaryView: View {@State private var boostTermText = ""@State private var boostTermStrength: BoostStrengthPreset = .balanced+ @State private var wizardStep: DictionaryWordWizardStep = .spelling+ @State private var wizardSavedWord = ""@State private var trainingReplacement = ""+ @State private var trainingSaveID: UUID?@State private var trainingVariants: [String] = []@State private var pronunciationMatchingEnabled = SettingsStore.shared.pronunciationMatchingEnabled@State private var trainingPronunciationEnrollments: [PronunciationEnrollmentCapture] = []@@ -75,7 +83,7 @@ struct CustomDictionaryView: View {}private var activePronunciationMatching: Bool {- self.pronunciationMatchingEnabled && SettingsStore.shared.selectedSpeechModel.supportsPronunciationMatching+ SettingsStore.shared.selectedSpeechModel.supportsPronunciationMatching}private var pronunciationMatchingBinding: Binding<Bool> {@@ -116,9 +124,7 @@ struct CustomDictionaryView: View {if self.activePronunciationMatching {return self.trainingPronunciationEnrollments.count >= CustomDictionaryTrainingMerge.readyCoveredCount}- return !self.trainingAlreadyCorrectWithoutReplacement &&- self.trainingOutputIsCovered &&- self.consecutiveCoveredCaptures >= CustomDictionaryTrainingMerge.readyCoveredCount+ return self.trainingSampleCount >= CustomDictionaryTrainingMerge.readyCoveredCount}private var trainingAlreadyCorrectWithoutReplacement: Bool {@@ -250,19 +256,57 @@ struct CustomDictionaryView: View {}var body: some View {- ScrollView(.vertical, showsIndicators: false) {- VStack(alignment: .leading, spacing: self.theme.metrics.spacing.xl) {- self.pageHeader-- VStack(alignment: .leading, spacing: self.theme.metrics.spacing.xxl) {- self.trainReplacementSection- self.yourDictionarySection- self.punctuationDictionarySection- self.aiPostProcessingSection+ Group {+ if self.formattingOnly {+ self.punctuationDictionarySection+ } else {+ HStack(spacing: 0) {+ ScrollView(.vertical, showsIndicators: false) {+ VStack(alignment: .leading, spacing: self.theme.metrics.spacing.xl) {+ self.pageHeader+ self.trainReplacementSection+ }+ .frame(maxWidth: 860, alignment: .leading)+ .padding(self.theme.metrics.spacing.xl)+ }+ .frame(maxWidth: .infinity, maxHeight: .infinity)++ self.wordDrawer+ .frame(width: 240)+ .frame(maxHeight: .infinity)+ .transaction { $0.animation = nil }+ .frame(width: self.isWordDrawerPresented ? 240 : 0, alignment: .trailing)+ .clipped()+ .background {+ Rectangle()+ .fill(self.theme.materials.sidebar)+ .ignoresSafeArea(.container, edges: [.top, .bottom])+ .allowsHitTesting(false)+ .accessibilityHidden(true)+ }+ .overlay(alignment: .leading) {+ Divider()+ .ignoresSafeArea(.container, edges: [.top, .bottom])+ .allowsHitTesting(false)+ .accessibilityHidden(true)+ }+ .allowsHitTesting(self.isWordDrawerPresented)+ .disabled(!self.isWordDrawerPresented)+ .accessibilityElement(children: .contain)+ .accessibilityHidden(!self.isWordDrawerPresented)}+ .frame(maxWidth: .infinity, maxHeight: .infinity)+ .animation(self.drawerAnimation, value: self.isWordDrawerPresented)+ }+ }+ .overlay(alignment: .topTrailing) {+ if !self.formattingOnly {+ self.dictionaryDrawerToggle+ .offset(x: self.isWordDrawerPresented ? -40 : 0)+ .animation(self.drawerAnimation, value: self.isWordDrawerPresented)+ .padding(.trailing, self.theme.metrics.spacing.xl)+ .padding(.top, self.theme.metrics.spacing.xl)}- .frame(maxWidth: 860, alignment: .leading)- .padding(self.theme.metrics.spacing.xl)}.dismissTextFocusOnBackgroundTap().task(id: self.revealTarget) {@@ -301,20 +345,24 @@ struct CustomDictionaryView: View {}}.onAppear {- self.entries = SettingsStore.shared.customDictionaryEntries- self.loadBoostTerms()- self.pronunciationMatchingEnabled = SettingsStore.shared.pronunciationMatchingEnabled- if !SettingsStore.shared.selectedSpeechModel.supportsPronunciationMatching {- self.pronunciationMatchingEnabled = false- SettingsStore.shared.pronunciationMatchingEnabled = false+ if !self.formattingOnly {+ self.entries = SettingsStore.shared.customDictionaryEntries+ self.loadBoostTerms()+ self.pronunciationMatchingEnabled = SettingsStore.shared.pronunciationMatchingEnabled}self.punctuationAutoConvertEnabled = SettingsStore.shared.autoConvertPunctuationEnabledself.formattingActionRules = SettingsStore.shared.spokenFormattingActionRules}.onReceive(NotificationCenter.default.publisher(for: .parakeetVocabularyDidChange)) { _ in+ guard !self.formattingOnly else { return }self.entries = SettingsStore.shared.customDictionaryEntries}.onDisappear {+ guard !self.formattingOnly else { return }+ if self.trainingSaveID != nil {+ self.trainingSaveID = nil+ self.isTrainingProcessing = false+ }self.isAutomaticTrainingEnabled = falseDictionaryTrainingEndpointMonitor.shared.stop()guard self.isTrainingRecording else { return }@@ -333,24 +381,167 @@ struct CustomDictionaryView: View {VStack(alignment: .leading, spacing: 2) {Text("Custom Dictionary").font(self.theme.typography.title)- Text("Correct recurring mistakes and teach the voice engine the words you use.")+ Text("Your words, recognised your way.").font(self.theme.typography.bodySmall).foregroundStyle(self.theme.palette.secondaryText)}Spacer(minLength: self.theme.metrics.spacing.md)- HStack(spacing: self.theme.metrics.spacing.sm) {- Button(action: self.importDictionary) {- Label("Import", systemImage: "square.and.arrow.down")- }- .fluidButton(.compact, size: .compact)+ Color.clear.frame(width: self.isWordDrawerPresented ? 0 : 150, height: 32).accessibilityHidden(true)+ }+ }- Button(action: self.exportDictionary) {- Label("Export", systemImage: "square.and.arrow.up")+ private var drawerAnimation: Animation? {+ self.reduceMotion ? nil : .timingCurve(0.22, 0.8, 0.25, 1, duration: 0.34)+ }++ private var dictionaryDrawerToggle: some View {+ Button {+ self.isDrawerActionsPresented = false+ self.isWordDrawerPresented.toggle()+ } label: {+ HStack(spacing: 6) {+ Image(systemName: "sidebar.right")+ .font(.system(size: 13, weight: .semibold))+ .foregroundStyle(self.isWordDrawerPresented ? self.theme.palette.accent : self.theme.palette.primaryText)+ .frame(width: 20, height: 20)+ Text("Your Dictionary")+ }+ }+ .fluidGlassAction()+ .accessibilityLabel(self.isWordDrawerPresented ? "Collapse your dictionary" : "Expand your dictionary")+ .help(self.isWordDrawerPresented ? "Collapse your dictionary" : "Expand your dictionary")+ }++ private var drawerEntries: [SettingsStore.CustomDictionaryEntry] {+ self.entries.filter {+ self.wordSearch.isEmpty || $0.replacement.localizedCaseInsensitiveContains(self.wordSearch)+ || $0.triggers.contains { $0.localizedCaseInsensitiveContains(self.wordSearch) }+ }+ }++ private var wordDrawer: some View {+ VStack(alignment: .leading, spacing: self.theme.metrics.spacing.md) {+ HStack(alignment: .center, spacing: 8) {+ Color.clear.frame(width: 150, height: 32).accessibilityHidden(true)+ Spacer()+ Button { self.isDrawerActionsPresented.toggle() } label: {+ Image(systemName: "ellipsis")+ .font(.system(size: 13, weight: .semibold))+ .frame(width: 20, height: 20)+ }+ .fluidGlassAction(circular: true)+ .accessibilityLabel("Dictionary actions")+ .popover(isPresented: self.$isDrawerActionsPresented) {+ VStack(alignment: .leading, spacing: 12) {+ Button("Import…") {+ self.isDrawerActionsPresented = false+ self.importDictionary()+ }.fluidGlassAction()+ Button("Export…") {+ self.isDrawerActionsPresented = false+ self.exportDictionary()+ }.fluidGlassAction()+ Divider()+ Button("Custom Words (Advanced)…") {+ self.isDrawerActionsPresented = false+ self.presentCustomWords()+ }.fluidGlassAction()+ }+ .padding(16)+ }+ }+ HStack(spacing: 8) {+ Image(systemName: "magnifyingglass")+ .foregroundStyle(self.theme.palette.secondaryText)+ TextField("Search words", text: self.$wordSearch)+ .textFieldStyle(.plain)+ }+ .font(self.theme.typography.bodySmall)+ .padding(10)+ .background(self.theme.palette.contentBackground, in: RoundedRectangle(cornerRadius: 8))+ .overlay(RoundedRectangle(cornerRadius: 8).stroke(self.theme.palette.cardBorder.opacity(0.4)))+ ScrollView {+ LazyVStack(alignment: .leading, spacing: self.theme.metrics.spacing.md) {+ ForEach(self.drawerEntries) { entry in+ HStack(alignment: .center, spacing: 12) {+ VStack(alignment: .leading, spacing: 4) {+ Text(entry.replacement).font(self.theme.typography.bodyStrong)+ Text(entry.triggers.joined(separator: ", "))+ .font(self.theme.typography.caption)+ .foregroundStyle(self.theme.palette.secondaryText)+ .lineLimit(3)+ Button("Test word") {+ self.wizardSavedWord = entry.replacement+ self.wizardStep = .saved+ self.isWordDrawerPresented = false+ }.fluidGlassAction()+ .disabled(self.asr.isRunning || self.isTrainingStarting || self.isTrainingProcessing)+ .accessibilityLabel("Test \(entry.replacement)")+ }+ Spacer()+ Button { self.editingEntry = entry } label: {+ FluidPencilShape()+ .stroke(style: StrokeStyle(lineWidth: 1.7, lineCap: .round, lineJoin: .round))+ .frame(width: 20, height: 20)+ .frame(width: 22, height: 22)+ .contentShape(Rectangle())+ }+ .fluidGlassAction(circular: true)+ .help("Edit word")+ .accessibilityLabel("Edit \(entry.replacement)")+ Button(role: .destructive) { self.drawerDeletion = entry } label: {+ Image(systemName: "trash")+ .font(.fluidSystem(size: 15, weight: .regular))+ .frame(width: 22, height: 22)+ .contentShape(Rectangle())+ }+ .fluidGlassAction(circular: true)+ .help("Delete word")+ .accessibilityLabel("Delete \(entry.replacement)")+ }+ .contextMenu {+ Button("Edit…") { self.editingEntry = entry }+ Button("Remove word", role: .destructive) { self.drawerDeletion = entry }+ }+ Divider().opacity(0.3)+ }+ if self.drawerEntries.isEmpty {+ Text(self.wordSearch.isEmpty ? "Add a word to see it here." : "No matching words.")+ .font(self.theme.typography.bodySmall)+ .foregroundStyle(self.theme.palette.secondaryText)+ .padding(.vertical, self.theme.metrics.spacing.lg)+ }}- .fluidButton(.compact, size: .compact)}+ Text("\(self.entries.count) saved words")+ .font(self.theme.typography.caption)+ .foregroundStyle(self.theme.palette.secondaryText)+ }+ .padding(self.theme.metrics.spacing.lg)+ .padding(.top, 8)+ .alert("Remove word?", isPresented: Binding(+ get: { self.drawerDeletion != nil },+ set: { if !$0 { self.drawerDeletion = nil } }+ )) {+ Button("Cancel", role: .cancel) { self.drawerDeletion = nil }+ Button("Remove", role: .destructive) {+ if let entry = self.drawerDeletion { self.deleteEntry(entry) }+ self.drawerDeletion = nil+ }+ } message: {+ Text("This removes the saved corrections and pronunciation for this word.")+ }+ .popover(isPresented: self.$isCustomWordsPresented) {+ VStack(alignment: .leading, spacing: self.theme.metrics.spacing.md) {+ Toggle("Custom Words Boosting", isOn: self.$vocabBoostingEnabled)+ .onChange(of: self.vocabBoostingEnabled) { _, enabled in+ SettingsStore.shared.vocabularyBoostingEnabled = enabled+ }+ self.customWordsPopover+ }+ .padding(self.theme.metrics.spacing.md)}}@@ -378,34 +569,73 @@ struct CustomDictionaryView: View {.frame(width: 34, height: 34)}- // MARK: - Teach Words-private var trainReplacementSection: some View {ThemedCard(style: .standard, hoverEffect: false) {- VStack(alignment: .leading, spacing: self.theme.metrics.spacing.lg) {- HStack(alignment: .center, spacing: self.theme.metrics.spacing.md) {- self.settingsIconTile(systemName: "mic.fill")-- VStack(alignment: .leading, spacing: 3) {- Text("Teach Words")- .font(self.theme.typography.sectionTitle)- Text("Show FluidVoice the right spelling, by voice or by typing.")- .font(self.theme.typography.caption)- .foregroundStyle(self.theme.palette.secondaryText)- }+ if self.wizardStep == .manual {+ VStack(alignment: .leading, spacing: self.theme.metrics.spacing.lg) {+ Button { self.wizardStep = .recording } label: {+ Label("Back", systemImage: "chevron.left")+ }.fluidGlassAction()+ Text("Add a correction").font(self.theme.typography.sectionTitle)+ Text("When FluidVoice types the wrong version, we’ll change it to your word.")+ .font(self.theme.typography.bodySmall)+ .foregroundStyle(self.theme.palette.secondaryText)+ self.manualReplacementComposer}-- self.dictionaryComposerModePicker-- Group {- switch self.composerMode {- case .train:- self.trainReplacementComposer- case .manual:- self.manualReplacementComposer- }+ } else {+ DictionaryWordWizard(+ word: self.$trainingReplacement,+ step: self.wizardStep,+ count: self.activePronunciationMatching ? self.trainingPronunciationEnrollments.count : self.trainingSampleCount,+ heard: self.lastTrainingOutput,+ variants: self.trainingVariants,+ busy: self.isTrainingRecording || self.isTrainingProcessing || self.isTrainingStarting,+ recording: self.isTrainingRecording,+ processing: self.isTrainingProcessing || self.isTrainingStarting,+ starting: self.isTrainingStarting,+ error: self.trainingHasError ? self.trainingStatusMessage : nil,+ voiceSupported: self.activePronunciationMatching,+ alreadyCorrect: self.trainingAlreadyCorrectWithoutReplacement,+ savedWord: self.wizardSavedWord,+ onContinue: {+ let captures = self.activePronunciationMatching ? self.trainingPronunciationEnrollments.count : self.trainingSampleCount+ self.wizardStep = captures >= 3 ? .review : .recording+ },+ onRecord: {+ self.wizardStep = .recording+ Task { await self.toggleAutomaticTraining() }+ },+ onSave: {+ if self.trainingAlreadyCorrectWithoutReplacement {+ self.wizardSavedWord = self.normalizedTrainingReplacement+ self.resetTraining()+ self.wizardStep = .saved+ } else {+ Task { await self.addTrainedReplacement() }+ }+ },+ onBack: { self.wizardStep = .spelling },+ onNewWord: { self.resetTraining(); self.wizardStep = .spelling },+ onManual: {+ self.manualReplacement = self.normalizedTrainingReplacement+ self.wizardStep = .manual+ },+ onPracticeMore: {+ self.trainingReplacement = self.wizardSavedWord+ self.wizardStep = .recording+ },+ onRedo: {+ guard !self.isTrainingRecording, !self.isTrainingProcessing, !self.isTrainingStarting else { return }+ self.resetTraining(keepingWord: true)+ self.wizardStep = .recording+ },+ automaticCaptureActive: self.isAutomaticTrainingEnabled,+ audioLevels: self.asr.audioLevelPublisher+ )+ .onChange(of: self.trainingReplacement) { oldValue, newValue in+ self.handleTrainingReplacementChange(oldValue: oldValue, newValue: newValue)}- .frame(minHeight: 315, alignment: .topLeading)+ .task { await DictionaryTrainingEndpointMonitor.shared.prepare() }}}.frame(maxWidth: .infinity, alignment: .leading)@@ -556,11 +786,9 @@ struct CustomDictionaryView: View {Button {self.addManualReplacementIfValid()} label: {- Label("Add Replacement", systemImage: "plus")- .frame(maxWidth: .infinity)- .frame(height: 38)+ Label("Save correction", systemImage: "checkmark")}- .fluidButton(.accent, size: .small)+ .fluidGlassAction(prominent: true, tone: self.theme.palette.accent).disabled(!self.canAddManualReplacement).opacity(self.canAddManualReplacement ? 1 : 0.45)}@@ -568,7 +796,7 @@ struct CustomDictionaryView: View {private var manualTriggerField: some View {VStack(alignment: .leading, spacing: self.theme.metrics.spacing.sm) {- Text("When FluidVoice hears")+ Text("What FluidVoice types wrong").font(self.theme.typography.captionStrong)TextField("fluid voice, fluid boys", text: self.$manualTriggerDraft)@@ -583,7 +811,7 @@ struct CustomDictionaryView: View {private var manualReplacementField: some View {VStack(alignment: .leading, spacing: self.theme.metrics.spacing.sm) {- Text("Change it to")+ Text("What it should type").font(self.theme.typography.captionStrong)TextField("FluidVoice", text: self.$manualReplacement).dictionaryInputChrome()@@ -839,10 +1067,8 @@ struct CustomDictionaryView: View {self.presentYourDictionary()} label: {Label("Modify", systemImage: "slider.horizontal.3")- .frame(width: Self.DictionaryHeaderControlLayout.actionButtonLabelWidth)}- .frame(width: Self.DictionaryHeaderControlLayout.actionButtonWidth)- .fluidButton(.compact, size: .medium)+ .fluidGlassAction().help("Modify dictionary replacements").popover(isPresented: self.$isYourDictionaryPresented, arrowEdge: .top) {self.yourDictionaryPopover@@ -967,7 +1193,7 @@ struct CustomDictionaryView: View {if self.entries.isEmpty {self.dictionaryEmptyState(title: "No replacements yet",- detail: "Use Teach Words above to create your first one."+ detail: "Add your first word using the guided steps.")} else {ScrollView(.vertical, showsIndicators: true) {@@ -983,7 +1209,7 @@ struct CustomDictionaryView: View {private var yourDictionaryHelpNote: some View {Label {- Text("Use the Teach Words area above to add dictionary entries by voice or manually.")+ Text("Add a word using the guided steps. You can record your voice or enter a known mistake.").font(self.theme.typography.caption).foregroundStyle(self.theme.palette.secondaryText)} icon: {@@ -1038,10 +1264,8 @@ struct CustomDictionaryView: View {self.presentCustomWords()} label: {Label("Modify", systemImage: "slider.horizontal.3")- .frame(width: Self.DictionaryHeaderControlLayout.actionButtonLabelWidth)}- .frame(width: Self.DictionaryHeaderControlLayout.actionButtonWidth)- .fluidButton(.compact, size: .medium)+ .fluidGlassAction().disabled(!self.vocabBoostingEnabled).opacity(self.vocabBoostingEnabled ? 1 : 0.45).help(self.vocabBoostingEnabled ? "Modify custom words" : "Turn on Boosting to modify custom words.")@@ -1072,7 +1296,7 @@ struct CustomDictionaryView: View {Text("Custom Words").font(self.theme.typography.sectionTitle)- Text("Add names, products, and uncommon terms for Parakeet to recognize.")+ Text("Best for rare names and terms that sound different from everyday words.").font(self.theme.typography.caption).foregroundStyle(self.theme.palette.secondaryText)}@@ -1086,10 +1310,20 @@ struct CustomDictionaryView: View {.font(.fluidSystem(size: 11, weight: .bold)).frame(width: 28, height: 28)}- .buttonStyle(SquareIconButtonStyle())+ .fluidGlassAction(circular: true).help("Close")}+ VStack(alignment: .leading, spacing: 4) {+ Text("Use cautiously")+ .font(self.theme.typography.captionStrong)+ .foregroundStyle(self.theme.palette.primaryText)+ Text("FluidVoice may sometimes use these words when you meant something similar.")+ .font(self.theme.typography.caption)+ .foregroundStyle(self.theme.palette.secondaryText)+ .fixedSize(horizontal: false, vertical: true)+ }+if self.isBoostWordEditorPresented {self.boostWordEditor} else {@@ -1099,7 +1333,7 @@ struct CustomDictionaryView: View {} label: {Label("Add Word", systemImage: "plus")}- .fluidButton(.accent, size: .small)+ .fluidGlassAction(prominent: true, tone: self.theme.palette.accent)Spacer()}@@ -1192,19 +1426,19 @@ struct CustomDictionaryView: View {Button("Clear") {self.clearBoostTermFields()}- .fluidButton(.compact, size: .compact)+ .fluidGlassAction()Spacer()Button("Cancel") {self.dismissBoostTermEditor()}- .fluidButton(.compact, size: .compact)+ .fluidGlassAction()Button("Save Word") {self.saveBoostTermIfValid()}- .fluidButton(.accent, size: .small)+ .fluidGlassAction(prominent: true, tone: self.theme.palette.accent).disabled(!self.canSaveBoostTerm).opacity(self.canSaveBoostTerm ? 1 : 0.45)}@@ -1745,6 +1979,8 @@ struct CustomDictionaryView: View {replacement: self.sanitizedManualReplacement)self.addReplacementEntry(entry)+ self.wizardSavedWord = entry.replacement+ self.wizardStep = .savedself.manualTriggerDraft = ""self.manualReplacement = ""}@@ -2060,8 +2296,14 @@ struct CustomDictionaryView: View {self.trainingHasError = falseself.trainingStatusMessage = ""- let transcript = await self.asr.stop(forDictionaryTraining: true)+ let transcript = await self.asr.stop(forDictionaryTraining: true, captureDictionaryPronunciation: self.activePronunciationMatching)self.isTrainingProcessing = false+ guard !CustomDictionaryTrainingMerge.isOversizedResponse(transcript, intendedReplacement: self.normalizedTrainingReplacement) else {+ self.isAutomaticTrainingEnabled = false+ self.trainingHasError = true+ self.trainingStatusMessage = "That was longer than expected. Say only “\(self.normalizedTrainingReplacement)”, then pause. This try wasn’t saved."+ return+ }if self.activePronunciationMatching,CustomDictionaryTrainingMerge.normalizedTrigger(transcript) != nil,let enrollment = self.asr.lastDictionaryTrainingResult?.pronunciationEnrollment@@ -2069,6 +2311,14 @@ struct CustomDictionaryView: View {self.trainingPronunciationEnrollments.append(enrollment)}self.addTrainingVariant(from: transcript)+ if self.trainingHasError {+ self.isAutomaticTrainingEnabled = false+ return+ }+ if self.trainingFinalOutputIsReady || self.trainingAlreadyCorrectWithoutReplacement {+ self.isAutomaticTrainingEnabled = false+ self.wizardStep = .review+ }await self.continueAutomaticTrainingIfNeeded()}@@ -2168,27 +2418,48 @@ struct CustomDictionaryView: View {private func addTrainedReplacement() async {guard self.canAddTrainedReplacement else { return }self.isTrainingProcessing = true+ let saveID = UUID()+ self.trainingSaveID = saveID+ defer {+ if self.trainingSaveID == saveID {+ self.trainingSaveID = nil+ self.isTrainingProcessing = false+ }+ }let replacementText = self.normalizedTrainingReplacement- let updatesExisting = self.entries.contains {- $0.replacement.caseInsensitiveCompare(replacementText) == .orderedSame+ let enrollments = self.trainingPronunciationEnrollments+ let savePronunciation = self.activePronunciationMatching+ let filtered = await VoiceTrainingAliasFilter.filter(self.trainingVariants)+ guard self.trainingSaveID == saveID, !Task.isCancelled else { return }+ DebugLogger.shared.info(+ "VOICE_TRAINING_ALIAS_FILTER accepted=\(filtered.accepted.count) rejected=\(filtered.rejected.count) available=\(filtered.lookupAvailable)",+ source: "CustomDictionary"+ )+ guard savePronunciation || !filtered.accepted.isEmpty else {+ self.trainingHasError = true+ self.trainingStatusMessage = filtered.lookupAvailable+ ? "These recordings contain everyday words. Try again, or add a replacement manually."+ : "Couldn't check these words. Try saving again, or add a replacement manually."+ return}+let updatedEntries = CustomDictionaryTrainingMerge.mergedEntries(- current: self.entries,+ current: SettingsStore.shared.customDictionaryEntries,replacement: replacementText,- triggers: self.trainingVariants,- savePronunciation: self.activePronunciationMatching+ triggers: filtered.accepted,+ savePronunciation: savePronunciation)let entry = updatedEntries.first {$0.replacement.caseInsensitiveCompare(replacementText) == .orderedSame}- let enrollments = self.trainingPronunciationEnrollments- if self.activePronunciationMatching, let entry, let modelKey = enrollments.first?.modelKey {+ if savePronunciation, let entry, let modelKey = enrollments.first?.modelKey {do {try await PronunciationDictionaryStore.shared.upsert(dictionaryEntryID: entry.id,label: replacementText,modelKey: modelKey,- enrollments: enrollments+ enrollments: enrollments,+ automaticMatchingEnabled: true)} catch {self.isTrainingProcessing = false@@ -2201,16 +2472,12 @@ struct CustomDictionaryView: View {return}}- let savedPronunciation = self.activePronunciationMatching+ guard self.trainingSaveID == saveID, !Task.isCancelled else { return }self.entries = updatedEntriesself.saveEntries()+ self.wizardSavedWord = replacementTextself.resetTraining()- self.showReplacementConfirmation(- title: savedPronunciation ? "Word saved" : (updatesExisting ? "Replacement updated" : "Recorded"),- detail: savedPronunciation- ? "Spelling and pronunciation samples saved. Try the word in a sentence."- : (updatesExisting ? "Your variants are ready." : "Replacement added at the top.")- )+ self.wizardStep = .saved}private func removeTrainingVariant(_ variant: String) {@@ -2238,10 +2505,11 @@ struct CustomDictionaryView: View {}}- private func resetTraining(statusMessage: String = "Type the correct text.") {+ private func resetTraining(statusMessage: String = "Type the correct text.", keepingWord: Bool = false) {+ self.trainingSaveID = nilself.isAutomaticTrainingEnabled = falseDictionaryTrainingEndpointMonitor.shared.stop()- self.trainingReplacement = ""+ if !keepingWord { self.trainingReplacement = "" }self.trainingVariants = []self.trainingPronunciationEnrollments = []self.trainingSampleCount = 0@@ -2262,6 +2530,11 @@ struct CustomDictionaryView: View {let newKey = CustomDictionaryTrainingMerge.normalizedReplacement(newValue).lowercased()guard oldKey != newKey else { return }+ if self.trainingSaveID != nil {+ self.trainingSaveID = nil+ self.isTrainingProcessing = false+ }+self.trainingVariants = self.existingTrainingVariants(for: newValue)self.trainingPronunciationEnrollments = []self.trainingSampleCount = 0@@ -2925,6 +3198,13 @@ enum CustomDictionaryTrainingMerge {private static let edgePunctuation = CharacterSet(charactersIn: ".,!?;:\"'“”‘’")+ /// Conservative length guard, not a sentence classifier. ASR may split a single name into several words.+ static func isOversizedResponse(_ transcript: String, intendedReplacement: String) -> Bool {+ let targetCount = intendedReplacement.split(whereSeparator: { $0.isWhitespace }).count+ let heardCount = transcript.split(whereSeparator: { $0.isWhitespace }).count+ return heardCount > max(5, targetCount + 3)+ }+static func normalizedReplacement(_ value: String) -> String {value.trimmingCharacters(in: .whitespacesAndNewlines)}@@ -3473,106 +3753,76 @@ struct AddDictionaryEntrySheet: View {.font(.fluidSystem(.headline))Spacer()Button("Cancel") { self.dismiss() }- .fluidOutlinedButton()+ .fluidGlassAction()+ .keyboardShortcut(.cancelAction)}Divider()- // Triggers input- VStack(alignment: .leading, spacing: 6) {- Text("Misheard Words (triggers)")- .font(.fluidSystem(.subheadline).weight(.medium))- Text("Add one version per line. Commas can be saved too.")- .font(.fluidSystem(.caption))- .foregroundStyle(.secondary)- TextEditor(text: self.$triggersText)- .font(.fluidSystem(.body))- .frame(minHeight: 54, maxHeight: 76)- .scrollContentBackground(.hidden)- .dictionaryInputChrome(minHeight: 54)-- // Duplicate warning- if !self.duplicateTriggers.isEmpty {- HStack(spacing: 4) {- Image(systemName: "exclamationmark.triangle.fill")- .foregroundStyle(.orange)- Text("Duplicate triggers: \(self.duplicateTriggers.joined(separator: ", "))")- .foregroundStyle(.orange)- }- .font(.fluidSystem(.caption))- }- }-- // Replacement input- VStack(alignment: .leading, spacing: 6) {- Text("Correct Spelling (replacement)")- .font(.fluidSystem(.subheadline).weight(.medium))- Text("This is what will appear in the final transcription.")- .font(.fluidSystem(.caption))- .foregroundStyle(.secondary)- TextField("FluidVoice", text: self.$replacement)- .dictionaryInputChrome()- .onSubmit { self.saveIfValid() }- }-- Spacer()-- // Preview- if !self.triggersText.isEmpty && !self.replacement.isEmpty {- VStack(alignment: .leading, spacing: 4) {- Text("Preview")- .font(.fluidSystem(.caption).weight(.medium))- .foregroundStyle(.secondary)-- FlowLayout(spacing: 6) {- ForEach(self.parseTriggers(), id: \.self) { trigger in- Text(trigger)- .font(.fluidSystem(.caption))- .padding(.horizontal, 6)- .padding(.vertical, 3)- .background(- RoundedRectangle(cornerRadius: 4).fill(- self.duplicateTriggers.contains(trigger)- ? AnyShapeStyle(Color.orange.opacity(0.3))- : AnyShapeStyle(.quaternary)- )- )+ ScrollView {+ VStack(alignment: .leading, spacing: 20) {+ // Triggers input+ VStack(alignment: .leading, spacing: 6) {+ Text("Misheard Words (triggers)")+ .font(.fluidSystem(.subheadline).weight(.medium))+ Text("Add one version per line. Commas can be saved too.")+ .font(.fluidSystem(.caption))+ .foregroundStyle(.secondary)+ TextEditor(text: self.$triggersText)+ .font(.fluidSystem(.body))+ .frame(minHeight: 54, maxHeight: 76)+ .scrollContentBackground(.hidden)+ .dictionaryInputChrome(minHeight: 54)++ // Duplicate warning+ if !self.duplicateTriggers.isEmpty {+ HStack(spacing: 4) {+ Image(systemName: "exclamationmark.triangle.fill")+ .foregroundStyle(.orange)+ Text("Duplicate triggers: \(self.duplicateTriggers.joined(separator: ", "))")+ .foregroundStyle(.orange)+ }+ .font(.fluidSystem(.caption))}+ }- Image(systemName: "arrow.right")+ // Replacement input+ VStack(alignment: .leading, spacing: 6) {+ Text("Correct Spelling (replacement)")+ .font(.fluidSystem(.subheadline).weight(.medium))+ Text("This is what will appear in the final transcription.").font(.fluidSystem(.caption))- .foregroundStyle(.tertiary)+ .foregroundStyle(.secondary)+ TextField("FluidVoice", text: self.$replacement)+ .dictionaryInputChrome()+ .onSubmit { self.saveIfValid() }+ }- Text(self.replacement)- .font(.fluidSystem(.caption).weight(.medium))- .foregroundStyle(self.theme.palette.accent)+ if !self.parseTriggers().isEmpty && !self.replacement.isEmpty {+ DictionaryReplacementPreview(+ triggers: self.parseTriggers(),+ replacement: self.replacement,+ duplicateTriggers: self.duplicateTriggers+ )}}- .padding(10)- .frame(maxWidth: .infinity, alignment: .leading)- .background(- RoundedRectangle(cornerRadius: 8, style: .continuous)- .fill(self.theme.palette.cardBackground)- .overlay(- RoundedRectangle(cornerRadius: 8, style: .continuous)- .stroke(self.theme.palette.cardBorder.opacity(0.5), lineWidth: 1)- )- )+ .padding(2)}+ .frame(minHeight: 0, maxHeight: .infinity)+ Divider()// Save buttonHStack {Spacer()Button("Add Replacement") { self.saveIfValid() }- .buttonStyle(.borderedProminent)- .tint(self.theme.palette.accent)+ .fluidGlassAction(prominent: true, tone: self.theme.palette.accent).disabled(!self.canSave).keyboardShortcut(.return, modifiers: [])}}.padding(20)- .frame(minWidth: 400, idealWidth: 450, maxWidth: 500)- .frame(minHeight: 350, idealHeight: 400, maxHeight: 450)+ .frame(minWidth: 400, idealWidth: 480, maxWidth: 560)+ .frame(minHeight: 360, idealHeight: 500, maxHeight: 600).dismissTextFocusOnBackgroundTap()}@@ -3594,6 +3844,64 @@ struct AddDictionaryEntrySheet: View {}}+private struct DictionaryReplacementPreview: View {+ @Environment(\.theme) private var theme++ let triggers: [String]+ let replacement: String+ let duplicateTriggers: [String]++ var body: some View {+ Grid(alignment: .leading, horizontalSpacing: 16, verticalSpacing: 8) {+ GridRow(alignment: .firstTextBaseline) {+ Text("Misheard")+ .frame(maxWidth: .infinity, alignment: .leading)+ Color.clear+ .gridCellUnsizedAxes([.horizontal, .vertical])+ .accessibilityHidden(true)+ Text("Corrected")+ .frame(maxWidth: .infinity, alignment: .leading)+ }+ .font(.fluidSystem(.caption))+ .foregroundStyle(self.theme.palette.secondaryText)++ GridRow(alignment: .firstTextBaseline) {+ VStack(alignment: .leading, spacing: 6) {+ ForEach(self.triggers, id: \.self) { trigger in+ Text(trigger)+ .font(.fluidSystem(.body))+ .foregroundStyle(+ self.duplicateTriggers.contains(trigger)+ ? Color.orange : self.theme.palette.primaryText+ )+ .fixedSize(horizontal: false, vertical: true)+ }+ }+ .frame(maxWidth: .infinity, alignment: .leading)++ Image(systemName: "arrow.right")+ .font(.fluidSystem(.body).weight(.medium))+ .foregroundStyle(self.theme.palette.secondaryText)+ .accessibilityHidden(true)++ Text(CustomDictionaryManualEntry.replacementDisplayText(+ CustomDictionaryManualEntry.sanitizedReplacement(self.replacement)+ ))+ .font(.fluidSystem(.body).weight(.semibold))+ .foregroundStyle(self.theme.palette.accent)+ .fixedSize(horizontal: false, vertical: true)+ .frame(maxWidth: .infinity, alignment: .leading)+ }+ }+ .padding(16)+ .frame(maxWidth: .infinity, alignment: .leading)+ .background(+ RoundedRectangle(cornerRadius: 10, style: .continuous)+ .fill(self.theme.palette.primaryText.opacity(0.035))+ )+ }+}+// MARK: - Edit Entry Sheetstruct EditDictionaryEntrySheet: View {@@ -3625,108 +3933,76 @@ struct EditDictionaryEntrySheet: View {.font(.fluidSystem(.headline))Spacer()Button("Cancel") { self.dismiss() }- .fluidOutlinedButton()+ .fluidGlassAction()+ .keyboardShortcut(.cancelAction)}Divider()- // Triggers input- VStack(alignment: .leading, spacing: 6) {- Text("Misheard Words (triggers)")- .font(.fluidSystem(.subheadline).weight(.medium))- Text("Add one version per line. Commas can be saved too.")- .font(.fluidSystem(.caption))- .foregroundStyle(.secondary)- TextEditor(text: self.$triggersText)- .font(.fluidSystem(.body))- .frame(minHeight: 54, maxHeight: 76)- .scrollContentBackground(.hidden)- .dictionaryInputChrome(minHeight: 54)-- // Duplicate warning- if !self.duplicateTriggers.isEmpty {- HStack(spacing: 4) {- Image(systemName: "exclamationmark.triangle.fill")- .foregroundStyle(.orange)- Text("Duplicate triggers: \(self.duplicateTriggers.joined(separator: ", "))")- .foregroundStyle(.orange)- }- .font(.fluidSystem(.caption))- }- }-- // Replacement input- VStack(alignment: .leading, spacing: 6) {- Text("Correct Spelling (replacement)")- .font(.fluidSystem(.subheadline).weight(.medium))- Text("This is what will appear in the final transcription.")- .font(.fluidSystem(.caption))- .foregroundStyle(.secondary)- TextField("FluidVoice", text: self.$replacement)- .dictionaryInputChrome()- .onSubmit { self.saveIfValid() }- }-- Spacer()-- // Preview- if !self.triggersText.isEmpty && !self.replacement.isEmpty {- VStack(alignment: .leading, spacing: 4) {- Text("Preview")- .font(.fluidSystem(.caption).weight(.medium))- .foregroundStyle(.secondary)-- FlowLayout(spacing: 6) {- ForEach(self.parseTriggers(), id: \.self) { trigger in- Text(trigger)- .font(.fluidSystem(.caption))- .padding(.horizontal, 6)- .padding(.vertical, 3)- .background(- RoundedRectangle(cornerRadius: 4).fill(- self.duplicateTriggers.contains(trigger)- ? AnyShapeStyle(Color.orange.opacity(0.3))- : AnyShapeStyle(.quaternary)- )- )+ ScrollView {+ VStack(alignment: .leading, spacing: 20) {+ // Triggers input+ VStack(alignment: .leading, spacing: 6) {+ Text("Misheard Words (triggers)")+ .font(.fluidSystem(.subheadline).weight(.medium))+ Text("Add one version per line. Commas can be saved too.")+ .font(.fluidSystem(.caption))+ .foregroundStyle(.secondary)+ TextEditor(text: self.$triggersText)+ .font(.fluidSystem(.body))+ .frame(minHeight: 54, maxHeight: 76)+ .scrollContentBackground(.hidden)+ .dictionaryInputChrome(minHeight: 54)++ // Duplicate warning+ if !self.duplicateTriggers.isEmpty {+ HStack(spacing: 4) {+ Image(systemName: "exclamationmark.triangle.fill")+ .foregroundStyle(.orange)+ Text("Duplicate triggers: \(self.duplicateTriggers.joined(separator: ", "))")+ .foregroundStyle(.orange)+ }+ .font(.fluidSystem(.caption))}+ }- Image(systemName: "arrow.right")+ // Replacement input+ VStack(alignment: .leading, spacing: 6) {+ Text("Correct Spelling (replacement)")+ .font(.fluidSystem(.subheadline).weight(.medium))+ Text("This is what will appear in the final transcription.").font(.fluidSystem(.caption))- .foregroundStyle(.tertiary)+ .foregroundStyle(.secondary)+ TextField("FluidVoice", text: self.$replacement)+ .dictionaryInputChrome()+ .onSubmit { self.saveIfValid() }+ }- Text(CustomDictionaryManualEntry.replacementDisplayText(- CustomDictionaryManualEntry.sanitizedReplacement(self.replacement)- ))- .font(.fluidSystem(.caption).weight(.medium))- .foregroundStyle(self.theme.palette.accent)+ if !self.parseTriggers().isEmpty && !self.replacement.isEmpty {+ DictionaryReplacementPreview(+ triggers: self.parseTriggers(),+ replacement: self.replacement,+ duplicateTriggers: self.duplicateTriggers+ )}}- .padding(10)- .frame(maxWidth: .infinity, alignment: .leading)- .background(- RoundedRectangle(cornerRadius: 8, style: .continuous)- .fill(self.theme.palette.cardBackground)- .overlay(- RoundedRectangle(cornerRadius: 8, style: .continuous)- .stroke(self.theme.palette.cardBorder.opacity(0.5), lineWidth: 1)- )- )+ .padding(2)}+ .frame(minHeight: 0, maxHeight: .infinity)+ Divider()// Save buttonHStack {Spacer()Button("Save Changes") { self.saveIfValid() }- .buttonStyle(.borderedProminent)- .tint(self.theme.palette.accent)+ .fluidGlassAction(prominent: true, tone: self.theme.palette.accent).disabled(!self.canSave).keyboardShortcut(.return, modifiers: [])}}.padding(20)- .frame(minWidth: 400, idealWidth: 450, maxWidth: 500)- .frame(minHeight: 320, idealHeight: 380, maxHeight: 420)+ .frame(minWidth: 400, idealWidth: 480, maxWidth: 560)+ .frame(minHeight: 360, idealHeight: 500, maxHeight: 600).dismissTextFocusOnBackgroundTap().onAppear {self.triggersText = self.entry.triggers.joined(separator: "\n")diff --git a/Sources/Fluid/UI/DictionaryAudioInspectionView.swift b/Sources/Fluid/UI/DictionaryAudioInspectionView.swiftnew file mode 100644index 00000000..aead6df6--- /dev/null+++ b/Sources/Fluid/UI/DictionaryAudioInspectionView.swift@@ -0,0 +1,330 @@+import AVFoundation+import Combine+import SwiftUI++@MainActor+final class DictionaryInspectionPlayback: NSObject, ObservableObject, AVAudioPlayerDelegate {+ @Published private(set) var playing: String?+ @Published private(set) var error: String?+ private var player: AVAudioPlayer?+ private var task: Task<Void, Never>?+ private var generation = UUID()++ func stop() {+ self.generation = UUID()+ self.task?.cancel()+ self.task = nil+ self.player?.stop()+ self.player = nil+ self.playing = nil+ }++ func play(_ audio: DictionaryAudioInspection, cut: DictionaryAudioCut?, id: String) {+ let wasPlaying = self.playing == id+ self.stop()+ guard !wasPlaying else { return }+ let generation = self.generation+ self.error = nil+ self.playing = id+ self.task = Task { [weak self] in+ do {+ let data = try await DictionaryAudioInspector.shared.wav(audio, cut: cut)+ guard let self, !Task.isCancelled, self.generation == generation else { return }+ let player = try AVAudioPlayer(data: data)+ player.delegate = self+ self.player = player+ guard player.play() else { throw DictionaryMatchPlaygroundError.unavailable("Couldn't play this audio.") }+ } catch {+ guard let self, !Task.isCancelled, self.generation == generation else { return }+ self.stop()+ self.error = error.localizedDescription+ }+ }+ }++ nonisolated func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {+ Task { @MainActor [weak self] in+ guard self?.player === player else { return }+ self?.stop()+ }+ }+}++struct DictionaryAudioInspectionView: View {+ let report: DictionaryMatchReport+ let recordingBusy: Bool+ var loadReference: @Sendable (UUID) async throws -> DictionaryAudioInspection = { try await PronunciationDictionaryStore.shared.inspection(for: $0) }+ var onRecordMore: (() -> Void)? = nil+ @Environment(\.theme) private var theme+ @AppStorage("DictionaryPronunciationDebugCapture") private var retainTrainingAudio = false+ @StateObject private var playback = DictionaryInspectionPlayback()+ @State private var references: [Int: DictionaryAudioInspection] = [:]+ @State private var referenceCuts: [Int: DictionaryAudioCut] = [:]+ @State private var editedReferences: Set<Int> = []+ @State private var testCut = DictionaryAudioCut(start: 0, end: 0.08)+ @State private var adjustedScores: [Float?] = []+ @State private var loadingReferences = false+ @State private var comparing = false+ @State private var loadErrors: Set<Int> = []++ private var enrollments: [PronunciationEnrollmentCapture] {+ self.report.profiles.first { $0.label.caseInsensitiveCompare(self.report.targetWord) == .orderedSame }?.enrollments ?? []+ }++ private var comparisonID: String {+ "\(self.testCut):" + self.editedReferences.sorted().map { "\($0):\(String(describing: self.referenceCuts[$0]))" }.joined(separator: ";")+ }++ var body: some View {+ VStack(alignment: .leading, spacing: self.theme.metrics.spacing.md) {+ Text("This cut compared with every training recording")+ .font(self.theme.typography.bodyStrong)+ LazyVGrid(columns: Array(repeating: GridItem(.flexible(), alignment: .leading), count: 3), alignment: .leading, spacing: self.theme.metrics.spacing.sm) {+ ForEach(self.enrollments.indices, id: \.self) { index in+ VStack(alignment: .leading, spacing: 4) {+ Text("Recording \(index + 1)").font(self.theme.typography.caption)+ Text(self.comparing ? "…" : self.scoreText(index))+ .font(self.theme.typography.bodyStrong).monospacedDigit()+ .foregroundStyle(self.theme.palette.accent)+ }+ }+ }+ if let audio = self.report.inspection {+ DictionaryAudioCutView(+ title: "Your test recording",+ audio: audio,+ cut: self.$testCut,+ initialCut: self.initialTestCut,+ playback: self.playback,+ playbackID: "test"+ )+ }+ Divider()+ Text("Your training recordings").font(self.theme.typography.bodyStrong)+ if let onRecordMore = self.onRecordMore {+ Button("Record new examples") {+ self.playback.stop()+ onRecordMore()+ }.fluidGlassAction()+ }+ Text("Play each original or the exact section used for its saved embedding.")+ .font(self.theme.typography.caption).foregroundStyle(self.theme.palette.secondaryText)+ .fixedSize(horizontal: false, vertical: true)+ ForEach(self.enrollments.indices, id: \.self) { index in+ self.referenceRow(index)+ if index < self.enrollments.count - 1 { Divider() }+ }+ Text("A higher similarity score does not mean a cleaner recording. Extra context can raise it. Listen to the cuts before changing how matching works.")+ .font(self.theme.typography.caption).foregroundStyle(self.theme.palette.secondaryText)+ .fixedSize(horizontal: false, vertical: true)+ Text("These cuts update all scores above, without changing saved examples or your match level. Scores reuse existing encoder frames in 80 ms steps.")+ .font(self.theme.typography.caption).foregroundStyle(self.theme.palette.secondaryText)+ .fixedSize(horizontal: false, vertical: true)+ if !self.comparing, !self.adjustedScores.isEmpty, self.adjustedScores.allSatisfy({ $0 == nil }) {+ Text("Choose a non-empty cut within one encoder chunk (up to 14.88 seconds).")+ .font(self.theme.typography.caption).foregroundStyle(self.theme.palette.secondaryText)+ }+ HStack {+ Text("Keep audio with new training examples").font(self.theme.typography.bodySmall)+ Spacer(minLength: self.theme.metrics.spacing.md)+ Toggle("Keep audio with new training examples", isOn: self.$retainTrainingAudio)+ .labelsHidden().toggleStyle(.switch).controlSize(.small)+ }+ Text("Stored locally with up to 10 examples per word, up to 15 seconds each. Turning this off affects future examples.")+ .font(self.theme.typography.caption).foregroundStyle(self.theme.palette.secondaryText)+ .fixedSize(horizontal: false, vertical: true)+ if let error = self.playback.error {+ Text(error).font(self.theme.typography.caption).foregroundStyle(.red)+ }+ }+ .disabled(self.recordingBusy)+ .task(id: self.report.recordingID) {+ self.testCut = self.initialTestCut+ self.loadingReferences = true+ for index in self.enrollments.indices {+ guard !Task.isCancelled else { return }+ guard let id = self.enrollments[index].inspectionID else { continue }+ do {+ let audio = try await self.loadReference(id)+ guard !Task.isCancelled else { return }+ self.references[index] = audio+ self.referenceCuts[index] = .init(start: audio.selectedStart, end: min(audio.duration, audio.selectedEnd))+ } catch {+ guard !Task.isCancelled else { return }+ self.loadErrors.insert(index)+ }+ }+ self.loadingReferences = false+ }+ .task(id: self.comparisonID) {+ self.playback.stop()+ self.adjustedScores = []+ self.comparing = true+ do { try await Task.sleep(for: .milliseconds(120)) } catch { return }+ guard let test = self.report.inspection else { self.comparing = false; return }+ let scores = await DictionaryAudioInspector.shared.scores(+ test: test,+ testCut: self.testCut,+ references: self.enrollments.indices.map { self.references[$0] },+ referenceCuts: self.enrollments.indices.map { self.editedReferences.contains($0) ? self.referenceCuts[$0] : nil },+ storedVectors: self.enrollments.map(\.values)+ )+ guard !Task.isCancelled else { return }+ self.adjustedScores = scores+ self.comparing = false+ }+ .onChange(of: self.recordingBusy) { _, busy in if busy { self.playback.stop() } }+ .onDisappear { self.playback.stop() }+ }++ private func scoreText(_ index: Int) -> String {+ guard self.adjustedScores.indices.contains(index), let score = self.adjustedScores[index] else { return "—" }+ return String(format: "%.3f", score)+ }++ private func referenceRow(_ index: Int) -> some View {+ VStack(alignment: .leading, spacing: self.theme.metrics.spacing.sm) {+ HStack {+ Text("Recording \(index + 1)").font(self.theme.typography.bodyStrong)+ Spacer()+ Text(self.comparing ? "…" : self.scoreText(index)).monospacedDigit()+ .font(self.theme.typography.bodyStrong).foregroundStyle(self.theme.palette.accent)+ }+ if let audio = self.references[index] {+ Text(String(format: "Full %.2f s · trained section %.2f–%.2f s", audio.duration, audio.selectedStart, audio.selectedEnd))+ .font(self.theme.typography.caption).foregroundStyle(self.theme.palette.secondaryText)+ FluidGlassControlGroup {+ HStack(spacing: self.theme.metrics.spacing.sm) {+ Button(self.playback.playing == "original-\(index)" ? "Stop" : "Play original") {+ self.playback.play(audio, cut: nil, id: "original-\(index)")+ }.fluidGlassAction()+ Button(self.playback.playing == "trained-\(index)" ? "Stop" : self.editedReferences.contains(index) ? "Play edited cut" : "Play trained cut") {+ let cut = self.editedReferences.contains(index) ? self.referenceCuts[index] : DictionaryAudioCut(start: audio.selectedStart, end: audio.selectedEnd)+ self.playback.play(audio, cut: cut, id: "trained-\(index)")+ }.fluidGlassAction()+ }+ }+ DisclosureGroup("Inspect this training cut") {+ DictionaryAudioCutView(+ title: "Training recording \(index + 1)",+ audio: audio,+ cut: Binding(+ get: { self.referenceCuts[index] ?? .init(start: audio.selectedStart, end: min(audio.duration, audio.selectedEnd)) },+ set: {+ self.referenceCuts[index] = $0+ if $0 == DictionaryAudioCut(start: audio.selectedStart, end: min(audio.duration, audio.selectedEnd)) {+ self.editedReferences.remove(index)+ } else { self.editedReferences.insert(index) }+ }+ ),+ initialCut: .init(start: audio.selectedStart, end: min(audio.duration, audio.selectedEnd)),+ playback: self.playback,+ playbackID: "reference-\(index)"+ )+ .padding(.top, self.theme.metrics.spacing.sm)+ }+ } else {+ Text(self.loadingReferences && self.enrollments[index].inspectionID != nil ? "Loading audio…" : self.loadErrors.contains(index)+ ? "Saved audio couldn't be loaded. Its stored embedding is still compared."+ : "Audio wasn't saved for this older example. Record a new example to hear it; its stored embedding is still compared.")+ .font(self.theme.typography.caption).foregroundStyle(self.theme.palette.secondaryText)+ .fixedSize(horizontal: false, vertical: true)+ }+ }+ }++ private var initialTestCut: DictionaryAudioCut {+ let duration = self.report.inspection?.duration ?? self.report.duration+ let sample = self.report.targetSamples.max { ($0.score ?? -.infinity) < ($1.score ?? -.infinity) }+ return .init(start: min(max(0, duration - 0.08), sample?.start ?? 0), end: min(duration, sample?.end ?? duration))+ }+}++private struct DictionaryAudioCutView: View {+ let title: String+ let audio: DictionaryAudioInspection+ @Binding var cut: DictionaryAudioCut+ let initialCut: DictionaryAudioCut+ @ObservedObject var playback: DictionaryInspectionPlayback+ let playbackID: String+ @Environment(\.theme) private var theme+ @State private var peaks: [Float] = []+ @State private var metrics: DictionaryAudioCutMetrics?++ var body: some View {+ VStack(alignment: .leading, spacing: self.theme.metrics.spacing.sm) {+ HStack {+ Text(self.title).font(self.theme.typography.bodyStrong)+ Spacer()+ Text(String(format: "%.2f s", self.audio.duration)).font(self.theme.typography.caption).monospacedDigit()+ }+ Canvas { context, size in+ let duration = max(0.08, self.audio.duration)+ let lower = min(1, max(0, self.cut.start / duration)) * size.width+ let upper = min(1, max(0, self.cut.end / duration)) * size.width+ context.fill(Path(CGRect(x: lower, y: 0, width: max(0, upper - lower), height: size.height)), with: .color(self.theme.palette.accent.opacity(0.15)))+ let peak = max(0.01, self.peaks.max() ?? 0.01)+ for (index, value) in self.peaks.enumerated() {+ let x = CGFloat(index) / CGFloat(max(1, self.peaks.count)) * size.width+ let height = max(1, CGFloat(value / peak) * (size.height - 8))+ context.fill(Path(CGRect(x: x, y: (size.height - height) / 2, width: max(1, size.width / 256 - 1), height: height)), with: .color(self.theme.palette.accent))+ }+ }+ .frame(height: 64)+ .accessibilityLabel("Waveform, selected \(String(format: "%.2f", self.cut.start)) to \(String(format: "%.2f", self.cut.end)) seconds")+ self.boundarySlider("Start", value: Binding(get: { self.cut.start }, set: { self.cut.start = min($0, max(0, self.cut.end - 0.08)) }))+ self.boundarySlider("End", value: Binding(get: { self.cut.end }, set: { self.cut.end = max($0, self.cut.start + 0.08) }))+ FluidGlassControlGroup {+ HStack(spacing: self.theme.metrics.spacing.sm) {+ Button(self.playback.playing == self.playbackID + "full" ? "Stop" : "Play full") {+ self.playback.play(self.audio, cut: nil, id: self.playbackID + "full")+ }.fluidGlassAction()+ Button(self.playback.playing == self.playbackID + "cut" ? "Stop" : "Play cut") {+ self.playback.play(self.audio, cut: self.cut, id: self.playbackID + "cut")+ }.fluidGlassAction()+ Button("Reset") { self.cut = self.initialCut }.fluidGlassAction()+ }+ }+ if let metrics = self.metrics {+ Text(String(format: "Quiet in cut: %.2f s at start · %.2f s at end", metrics.leadingQuiet, metrics.trailingQuiet))+ .font(self.theme.typography.caption).foregroundStyle(self.theme.palette.secondaryText).fixedSize(horizontal: false, vertical: true)+ Button("Try quiet-edge trim") {+ if let suggested = metrics.suggested { self.cut = suggested }+ }+ .fluidGlassAction()+ .disabled(metrics.suggested == nil || metrics.suggested == self.cut)+ }+ Text("Quiet means below −45 dBFS; faint speech can also be quiet. No noise removal is applied.")+ .font(self.theme.typography.caption).foregroundStyle(self.theme.palette.secondaryText).fixedSize(horizontal: false, vertical: true)+ if self.audio.paddedDuration > self.audio.duration + 0.001 {+ Text(String(format: "Model input added %.2f s of trailing padding. Playback uses captured audio only.", self.audio.paddedDuration - self.audio.duration))+ .font(self.theme.typography.caption).foregroundStyle(self.theme.palette.secondaryText).fixedSize(horizontal: false, vertical: true)+ }+ let heard = self.audio.words.filter { $0.start < self.cut.end && $0.end > self.cut.start }.map(\.text).joined(separator: " ")+ Text("Recognizer heard: \(heard.isEmpty ? "No timestamped words" : heard)")+ .font(self.theme.typography.caption).foregroundStyle(self.theme.palette.secondaryText).fixedSize(horizontal: false, vertical: true)+ }+ .task {+ let values = await DictionaryAudioInspector.shared.waveform(self.audio)+ guard !Task.isCancelled else { return }+ self.peaks = values+ }+ .task(id: "\(self.cut)") {+ self.metrics = nil+ do { try await Task.sleep(for: .milliseconds(120)) } catch { return }+ let metrics = await DictionaryAudioInspector.shared.metrics(self.audio, cut: self.cut)+ guard !Task.isCancelled else { return }+ self.metrics = metrics+ }+ }++ private func boundarySlider(_ title: String, value: Binding<Double>) -> some View {+ HStack(spacing: self.theme.metrics.spacing.sm) {+ Text(title).frame(width: 34, alignment: .leading)+ Slider(value: value, in: 0...max(0.08, self.audio.duration), step: 0.08)+ .accessibilityLabel("\(self.title) \(title.lowercased())")+ Text(String(format: "%.2f s", value.wrappedValue)).monospacedDigit().frame(width: 56, alignment: .trailing)+ }+ .font(self.theme.typography.caption)+ }+}diff --git a/Sources/Fluid/UI/DictionaryMatchDiagnosticsView.swift b/Sources/Fluid/UI/DictionaryMatchDiagnosticsView.swiftnew file mode 100644index 00000000..01a1ac78--- /dev/null+++ b/Sources/Fluid/UI/DictionaryMatchDiagnosticsView.swift@@ -0,0 +1,198 @@+import SwiftUI++/// The current textbox attempt is primary; saved audio is an optional input below it.+struct DictionaryMatchDiagnosticsView: View {+ let word: String+ let recordingBusy: Bool+ let recording: DictionaryTestRecording?+ let onHistory: (UUID, String, URL) -> Void+ var onRecordMore: (() -> Void)? = nil+ @Environment(\.theme) private var theme+ @State private var profile: PronunciationDictionaryProfile?+ @State private var report: DictionaryMatchReport?+ @State private var threshold = 0.70+ @State private var savedThreshold = 0.70+ @State private var saving = false+ @State private var error: String?+ @State private var running = false+ @State private var historyExpanded = false+ @State private var inspectionExpanded = true+ @State private var history: [TranscriptionHistoryEntry] = []+ @State private var historyID: UUID?++ private var candidate: DictionaryMatchReport.Candidate? {+ self.report?.candidates.first { $0.word.caseInsensitiveCompare(self.word) == .orderedSame }+ }++ var body: some View {+ VStack(alignment: .leading, spacing: self.theme.metrics.spacing.md) {++ HStack {+ Text(self.running ? "Checking this recording…" : "Pronunciation match")+ .font(self.theme.typography.bodyStrong)+ Spacer()+ if self.running { ProgressView().controlSize(.small) }+ }+ if let report = self.report, report.inspection == nil || !self.inspectionExpanded {+ Text("Automatic search · before trimming")+ .font(self.theme.typography.caption).foregroundStyle(self.theme.palette.secondaryText)+ LazyVGrid(columns: Array(repeating: GridItem(.flexible(), alignment: .leading), count: 3), alignment: .leading, spacing: self.theme.metrics.spacing.sm) {+ ForEach(report.targetSamples) { sample in+ VStack(alignment: .leading, spacing: 4) {+ Text("Recording \(sample.sampleNumber ?? 1)")+ .font(self.theme.typography.caption)+ .foregroundStyle(self.theme.palette.secondaryText)+ Text(sample.score.map { String(format: "%.3f", $0) } ?? "—")+ .monospacedDigit()+ .font(self.theme.typography.bodyStrong)+ .foregroundStyle((sample.score ?? -.infinity) >= Float(self.threshold) ? self.theme.palette.accent : self.theme.palette.primaryText)+ }+ }+ }+ }+ Text(self.report?.inspection != nil && self.inspectionExpanded ? "Automatic search: \(self.status)" : self.status)+ .font(self.theme.typography.caption)+ .foregroundStyle(self.theme.palette.secondaryText)+ .frame(maxWidth: .infinity, alignment: .leading)+ VStack(spacing: 6) {+ HStack {+ Text("Match level")+ Spacer()+ Text(String(format: "%.2f", self.threshold)).monospacedDigit()+ }+ .font(self.theme.typography.bodyStrong)+ Slider(value: self.$threshold, in: 0.4...0.95, step: 0.01) { editing in+ if !editing { self.saveThreshold() }+ }+ .accessibilityLabel("Match level for \(self.word)")+ .disabled(self.profile == nil || self.saving || self.recordingBusy)+ HStack {+ Text("More forgiving")+ Spacer()+ Text("More exact")+ }+ .font(self.theme.typography.caption)+ .foregroundStyle(self.theme.palette.secondaryText)+ }+ Text(self.saving ? "Saving…" : "Applies to “\(self.word)” on your next dictation.")+ .font(self.theme.typography.caption)+ .foregroundStyle(self.theme.palette.secondaryText)+ if let error = self.error {+ Text(error).font(self.theme.typography.caption).foregroundStyle(.red)+ }+ if let report = self.report, report.inspection != nil {+ Divider()+ DisclosureGroup("Inspect audio cuts", isExpanded: self.$inspectionExpanded) {+ if self.inspectionExpanded {+ DictionaryAudioInspectionView(report: report, recordingBusy: self.recordingBusy, onRecordMore: self.onRecordMore)+ .id(report.recordingID)+ .padding(.top, self.theme.metrics.spacing.sm)+ }+ }+ }+ Divider()+ DisclosureGroup("History", isExpanded: self.$historyExpanded) {+ VStack(alignment: .leading, spacing: self.theme.metrics.spacing.sm) {+ Text("Load an older recording into the textbox.")+ .font(self.theme.typography.caption)+ .foregroundStyle(self.theme.palette.secondaryText)+ Picker("Recording", selection: self.$historyID) {+ Text("Choose a recording").tag(UUID?.none)+ ForEach(self.history) { entry in+ Text("\(entry.timestamp.formatted(date: .omitted, time: .shortened)) · \(String((entry.clipboardText ?? entry.rawText).prefix(60)))")+ .tag(Optional(entry.id))+ }+ }+ .fluidDropdownStyle()+ .disabled(self.recordingBusy)+ if self.history.isEmpty {+ Text("No saved audio yet.").font(self.theme.typography.caption)+ }+ }+ .padding(.top, self.theme.metrics.spacing.sm)+ }+ }+ .task(id: self.word) {+ self.report = nil+ self.profile = nil+ let profiles = await PronunciationDictionaryStore.shared.allProfiles()+ guard !Task.isCancelled else { return }+ self.profile = profiles.first { $0.label.caseInsensitiveCompare(self.word) == .orderedSame }+ self.threshold = Double(self.profile.flatMap(DictionaryPronunciationDecision.overrideThreshold) ?? 0.70)+ self.savedThreshold = self.threshold+ }+ .task(id: "\(self.word):\(self.recording?.id.uuidString ?? "")") {+ self.report = nil+ self.error = nil+ self.running = false+ guard let recording = self.recording else { return }+ self.running = true+ do {+ let report: DictionaryMatchReport+ if !recording.samples.isEmpty {+ report = try await DictionaryMatchPlayground.shared.analyze(+ recordingID: recording.id, samples: recording.samples, savedTranscript: recording.text, target: self.word+ )+ } else if let url = recording.audioURL {+ report = try await DictionaryMatchPlayground.shared.analyze(+ recordingID: recording.id, audioURL: url, savedTranscript: recording.text, target: self.word+ )+ } else { throw DictionaryMatchPlaygroundError.unavailable("This recording has no audio.") }+ guard !Task.isCancelled else { return }+ self.report = report+ if self.profile?.matchThreshold == nil, let candidate = self.candidate {+ self.threshold = Double(candidate.requiredScore)+ self.savedThreshold = self.threshold+ }+ } catch {+ guard !Task.isCancelled else { return }+ self.error = error.localizedDescription+ }+ self.running = false+ }+ .onChange(of: self.historyExpanded) { _, expanded in+ if expanded { self.refreshHistory() }+ }+ .onChange(of: self.recording?.id) { _, _ in+ if self.historyExpanded { self.refreshHistory() }+ }+ .onChange(of: self.historyID) { _, id in+ guard !self.recordingBusy, let entry = self.history.first(where: { $0.id == id }),+ let url = DictationAudioHistoryStore.shared.audioFileURL(for: entry)+ else { return }+ self.onHistory(entry.id, entry.clipboardText ?? entry.rawText, url)+ }+ }++ private var status: String {+ guard let candidate = self.candidate, let score = candidate.score else {+ return self.recordingBusy ? "Listening to your current test." : "Dictate in the textbox to see how closely it matches “\(self.word)”."+ }+ guard candidate.eligible else { return "Add more recordings for this word to enable matching." }+ if score < Float(self.threshold) { return "No recording clears your match level. Closest is \(String(format: "%.3f", Float(self.threshold) - score)) below." }+ if candidate.heard.isEmpty { return "Similar sound, but no clear word boundary was found." }+ if let competitor = candidate.competingScore, competitor > score - 0.05 { return "Similar, but too close to another saved word." }+ return "At least one recording clears your match level · “\(candidate.heard)”"+ }++ private func refreshHistory() {+ self.history = Array(TranscriptionHistoryStore.shared.entries.lazy.filter { $0.audio != nil }.prefix(50))+ }++ private func saveThreshold() {+ guard let profile = self.profile, !self.saving, self.threshold != self.savedThreshold else { return }+ let value = self.threshold+ self.saving = true+ Task {+ do {+ try await PronunciationDictionaryStore.shared.setMatchThreshold(Float(value), dictionaryEntryID: profile.dictionaryEntryID)+ self.savedThreshold = value+ self.error = nil+ } catch {+ self.threshold = self.savedThreshold+ self.error = "Couldn't save this match level. Try again."+ }+ self.saving = false+ }+ }+}diff --git a/Sources/Fluid/UI/DictionaryShortcutTestState.swift b/Sources/Fluid/UI/DictionaryShortcutTestState.swiftnew file mode 100644index 00000000..bded93e3--- /dev/null+++ b/Sources/Fluid/UI/DictionaryShortcutTestState.swift@@ -0,0 +1,98 @@+import Foundation++/// Local UI state only. Never starts/stops capture or changes shared dictation output.+struct DictionaryShortcutTestState {+ enum Phase: Equatable { case idle, recording, processing }+ enum Result: Equatable { case none, recognized, missed, empty }++ private(set) var phase: Phase = .idle+ private(set) var result: Result = .none+ private(set) var text = ""+ private(set) var successes = 0+ private(set) var attemptID: UUID?+ private(set) var recording: DictionaryTestRecording?+ private var pendingSamples: [Float] = []++ mutating func begin(editorFocused: Bool, appActive: Bool, dictionaryCapture: Bool) {+ self.cancel()+ guard editorFocused, appActive, !dictionaryCapture else { return }+ self.recording = nil+ self.attemptID = UUID()+ self.phase = .recording+ self.result = .none+ self.text = ""+ }++ mutating func receiveAudio(_ samples: [Float]) {+ guard self.phase == .processing, self.attemptID != nil, !samples.isEmpty, samples.count <= 16_000 * 120 else { return }+ self.pendingSamples = samples+ }++ mutating func useHistory(id: UUID, text: String, url: URL) {+ self.cancel()+ self.text = text+ self.result = .none+ self.recording = DictionaryTestRecording(id: id, text: text, samples: [], audioURL: url)+ }++ mutating func stopped() {+ guard self.phase == .recording else { return }+ self.phase = .processing+ }++ mutating func receive(_ text: String, word: String) {+ guard self.phase == .processing, self.attemptID != nil else { return }+ self.text = text.trimmingCharacters(in: .whitespacesAndNewlines)+ self.result = self.text.isEmpty ? .empty : (DictionaryWordTestResult.containsWord(word, in: self.text) ? .recognized : .missed)+ if let id = self.attemptID, !self.pendingSamples.isEmpty {+ self.recording = DictionaryTestRecording(id: id, text: self.text, samples: self.pendingSamples, audioURL: nil)+ }+ if self.result == .recognized { self.successes += 1 }+ self.cancel()+ }++ mutating func expire(_ attemptID: UUID) {+ guard self.attemptID == attemptID else { return }+ self.result = .empty+ self.cancel()+ }++ mutating func edit(_ text: String) {+ guard text != self.text else { return }+ self.cancel()+ self.recording = nil+ self.text = text+ self.result = .none+ }++ mutating func cancel() {+ self.phase = .idle+ self.attemptID = nil+ self.pendingSamples = []+ }+}++enum DictionaryCaptureAction {+ static func title(active: Bool, failed: Bool, count: Int) -> String {+ if active { return "Stop recording" }+ if failed { return "Record again" }+ return count < 1 ? "Record" : "Record more"+ }+}++enum DictionaryWordTestResult {+ static func containsWord(_ word: String, in transcript: String) -> Bool {+ let word = word.trimmingCharacters(in: .whitespacesAndNewlines)+ guard !word.isEmpty else { return false }+ let escaped = NSRegularExpression.escapedPattern(for: word)+ return transcript.range(of: "(?<![\\p{L}\\p{N}_])" + escaped + "(?![\\p{L}\\p{N}_])", options: [.regularExpression, .caseInsensitive]) != nil+ }+}++/// One exact textbox attempt. Changing the text invalidates its audio score.+struct DictionaryTestRecording: Identifiable, Sendable {+ let id: UUID+ let text: String+ let samples: [Float]+ let audioURL: URL?+}diff --git a/Sources/Fluid/UI/DictionaryWordWizard.swift b/Sources/Fluid/UI/DictionaryWordWizard.swiftnew file mode 100644index 00000000..9df32cc4--- /dev/null+++ b/Sources/Fluid/UI/DictionaryWordWizard.swift@@ -0,0 +1,902 @@+import Combine+import SwiftUI++enum DictionaryWordWizardStep: Equatable {+ case spelling, recording, review, saved, manual++ var position: Int {+ switch self {+ case .spelling: 1+ case .recording, .manual: 2+ case .review, .saved: 3+ }+ }+}++/// One task at a time. Recording and persistence remain with the existing dictionary services.+struct DictionaryWordWizard: View {+ @Binding var word: String+ let step: DictionaryWordWizardStep+ let count: Int+ let heard: String+ let variants: [String]+ let busy: Bool+ let recording: Bool+ let processing: Bool+ let starting: Bool+ let error: String?+ let voiceSupported: Bool+ let alreadyCorrect: Bool+ let savedWord: String+ let onContinue: () -> Void+ let onRecord: () -> Void+ let onSave: () -> Void+ let onBack: () -> Void+ let onNewWord: () -> Void+ let onManual: () -> Void+ let onPracticeMore: () -> Void+ var onRedo: () -> Void = {}+ var automaticCaptureActive = false+ var audioLevels: AnyPublisher<CGFloat, Never> = Empty().eraseToAnyPublisher()++ @Environment(\.theme) private var theme+ @Environment(\.accessibilityReduceMotion) private var reduceMotion+ @FocusState private var wordFocused: Bool+ @State private var showingRecordingTips = false+ @State private var hasViewedTips = false+ @State private var showingCapturedSpellings = false+ @State private var playgroundBusy = false+ @State private var confirmingRedo = false+ @State private var confirmingWordChange = false+ @State private var pendingWord = ""++ var body: some View {+ VStack(spacing: self.theme.metrics.spacing.lg) {+ HStack {+ if self.step != .spelling, self.step != .saved {+ Button(action: self.onBack) { Label("Change word", systemImage: "chevron.left") }+ .fluidGlassAction()+ .disabled(self.busy)+ }+ Spacer()+ if self.step == .recording || self.step == .review {+ self.guidanceButton+ }+ }+ .frame(minHeight: 32)+ .overlay {+ Text(self.step == .saved ? "Word saved" : "Step \(self.step.position) of 3")+ .font(self.theme.typography.caption)+ .foregroundStyle(self.theme.palette.secondaryText)+ .allowsHitTesting(false)+ }+ FluidGlassControlGroup {+ VStack(spacing: self.theme.metrics.spacing.lg) {+ switch self.step {+ case .spelling: self.spelling+ case .recording, .review: self.capture+ case .saved: self.saved+ case .manual: EmptyView()+ }+ }+ .frame(maxWidth: self.step == .recording || self.step == .review ? .infinity : 420)+ .frame(maxWidth: .infinity)+ .padding(.vertical, self.theme.metrics.spacing.sm)+ }+ }+ .alert("Discard these recordings and try again?", isPresented: self.$confirmingRedo) {+ Button("Cancel", role: .cancel) {}+ Button("Redo recordings", role: .destructive, action: self.onRedo)+ } message: {+ Text("Your word stays the same. Previously saved dictionary entries are unchanged.")+ }+ .alert("Change the word and discard these recordings?", isPresented: self.$confirmingWordChange) {+ Button("Cancel", role: .cancel) {}+ Button("Change word", role: .destructive) { self.word = self.pendingWord }+ }+ .task(id: self.step) { self.wordFocused = self.step == .spelling }+ }++ private var spelling: some View {+ VStack(spacing: self.theme.metrics.spacing.lg) {+ DictionaryLearningRing(progress: 0, active: false, symbol: "waveform")+ Text("Which word does FluidVoice keep getting wrong?")+ .font(self.theme.typography.sectionTitle)+ .multilineTextAlignment(.center)+ .fixedSize(horizontal: false, vertical: true)+ TextField("For example, FluidVoice", text: Binding(get: { self.word }, set: { value in+ if self.count >= 1, value != self.word {+ self.pendingWord = value+ self.confirmingWordChange = true+ } else { self.word = value }+ }))+ .textFieldStyle(.plain)+ .padding(14)+ .background(self.theme.palette.contentBackground, in: RoundedRectangle(cornerRadius: self.theme.metrics.corners.md))+ .overlay {+ RoundedRectangle(cornerRadius: self.theme.metrics.corners.md)+ .stroke(self.wordFocused ? self.theme.palette.accent.opacity(0.7) : self.theme.palette.cardBorder, lineWidth: 1)+ .allowsHitTesting(false)+ }+ .font(self.theme.typography.body)+ .focused(self.$wordFocused)+ .accessibilityLabel("Word to learn")+ .onSubmit { if !self.word.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty { self.onContinue() } }+ self.primary("Continue", action: self.onContinue)+ .disabled(self.word.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty)+ }+ }++ private var capture: some View {+ VStack(alignment: .leading, spacing: self.theme.metrics.spacing.lg) {+ HStack(alignment: .top, spacing: self.theme.metrics.spacing.lg) {+ self.captureStage.frame(minWidth: 264, maxWidth: .infinity)+ if self.showingRecordingTips {+ self.guidanceCard+ .frame(width: 240, alignment: .leading)+ .transition(.move(edge: .trailing).combined(with: .opacity))+ }+ }+ self.captureError+ Group {+ if self.step == .review {+ self.manualAlternative.hidden().accessibilityHidden(true)+ } else {+ self.manualAlternative+ }+ }+ }+ }++ private var manualAlternative: some View {+ VStack(spacing: self.theme.metrics.spacing.lg) {+ Divider().padding(.vertical, self.theme.metrics.spacing.md)+ ViewThatFits(in: .horizontal) {+ HStack(spacing: self.theme.metrics.spacing.lg) {+ self.manualExplanation.frame(minWidth: 240, maxWidth: .infinity, alignment: .leading)+ self.manualButton.disabled(self.busy)+ }+ VStack(alignment: .leading, spacing: self.theme.metrics.spacing.md) {+ self.manualExplanation+ self.manualButton.disabled(self.busy)+ }+ }+ }+ }++ private var captureStage: some View {+ VStack(spacing: self.theme.metrics.spacing.lg) {+ Text(self.step == .review ? "Your word is ready" : "Let’s hear your word")+ .font(self.theme.typography.body)+ .foregroundStyle(self.theme.palette.accent)+ Text("“\(self.word)”")+ .font(self.theme.typography.title)+ .fixedSize(horizontal: false, vertical: true)+ DictionaryLearningRing(+ progress: Double(min(self.count, 3)) / 3,+ active: self.recording && !self.starting,+ symbol: self.processing ? "ellipsis" : (self.count >= 3 ? "checkmark" : "waveform"),+ diameter: 264,+ count: self.count,+ processing: self.processing,+ audioLevels: self.audioLevels+ )+ .accessibilityElement(children: .ignore)+ .accessibilityLabel(DictionaryLearningEncouragement.messages(for: self.count)[0])+ .accessibilityValue(self.processing ? "Processing recording" : (self.recording ? "Listening" : ""))+ .accessibilityHidden(false)+ self.captureControls+ }+ .multilineTextAlignment(.center)+ .padding(.vertical, self.theme.metrics.spacing.lg)+ .frame(maxWidth: .infinity)+ .accessibilityElement(children: .contain)+ }++ private var learningTitle: String {+ if self.step == .review { return self.alreadyCorrect ? "It already sounds right" : "Ready to save" }+ if self.starting { return "Getting ready…" }+ if self.processing { return "Learning your voice…" }+ if self.recording { return self.count < 1 ? "Listening…" : "Say it again" }+ if self.count >= 3 { return "Ready to add" }+ return self.count < 1 ? "Let’s hear your word" : "Keep going—you’re nearly there"+ }++ private var guidanceButton: some View {+ Button {+ withAnimation(self.reduceMotion ? nil : .easeInOut(duration: 0.25)) {+ self.showingRecordingTips.toggle()+ self.hasViewedTips = true+ }+ } label: {+ HStack(spacing: self.theme.metrics.spacing.sm) {+ Image(systemName: "lightbulb")+ Text("Tips")+ Image(systemName: "chevron.down")+ .rotationEffect(.degrees(self.showingRecordingTips ? 180 : 0))+ .accessibilityHidden(true)+ }+ }+ .fluidGlassAction(prominent: !self.hasViewedTips || self.showingRecordingTips, tone: self.theme.palette.accent)+ .accessibilityValue(self.showingRecordingTips ? "Expanded" : "Collapsed")+ .accessibilityHint("Shows or hides guidance beside the learning area")+ }++ private var guidanceCard: some View {+ self.captureGuidance+ .padding(self.theme.metrics.spacing.xl)+ .frame(maxWidth: .infinity, alignment: .leading)+ .background(self.theme.palette.contentBackground, in: RoundedRectangle(cornerRadius: self.theme.metrics.corners.lg))+ .overlay {+ RoundedRectangle(cornerRadius: self.theme.metrics.corners.lg)+ .stroke(self.theme.palette.cardBorder.opacity(0.6), lineWidth: 1)+ .allowsHitTesting(false)+ }+ }++ private var captureGuidance: some View {+ VStack(alignment: .leading, spacing: self.theme.metrics.spacing.xxl) {+ Text(self.step == .review ? "Your word, your way" : "For best results")+ .font(self.theme.typography.title)+ if self.step == .review {+ Text(self.alreadyCorrect ? "FluidVoice recognised your word in each recording." : "Your recordings are ready. Save this word to use it in future dictations.")+ .font(self.theme.typography.statement)+ .foregroundStyle(self.theme.palette.secondaryText)+ Divider()+ Text("Want to add another example?")+ .font(self.theme.typography.sectionTitle)+ Text("You can record more before saving. Keep saying the word in your normal voice.")+ .font(self.theme.typography.body)+ .foregroundStyle(self.theme.palette.secondaryText)+ } else {+ self.instruction(1, title: "Say only the word", detail: "Use your normal voice. No sentence needed.")+ self.instruction(2, title: "Wait when you finish", detail: "Recording stops automatically.")+ self.instruction(3, title: "Wait for the next prompt", detail: "Speak again when you see “Listening…”.")+ Divider()+ Label("Choose a quiet place and keep a comfortable distance from your microphone.", systemImage: "ear")+ .font(self.theme.typography.body)+ .foregroundStyle(self.theme.palette.secondaryText)+ }+ if self.count >= 1, !self.capturedSpellings.isEmpty {+ DisclosureGroup("Captured spellings · \(self.capturedSpellings.count)", isExpanded: self.$showingCapturedSpellings) {+ self.capturedVariations.padding(.top, self.theme.metrics.spacing.sm)+ }+ .font(self.theme.typography.body)+ .tint(self.theme.palette.accent)+ }+ }+ .fixedSize(horizontal: false, vertical: true)+ .padding(.vertical, self.theme.metrics.spacing.lg)+ .accessibilityElement(children: .contain)+ }++ private func instruction(_ number: Int, title: String, detail: String) -> some View {+ HStack(alignment: .top, spacing: self.theme.metrics.spacing.md) {+ Text("\(number)")+ .font(self.theme.typography.bodyStrong)+ .foregroundStyle(self.theme.palette.accent)+ .frame(width: 28, height: 28)+ .background(self.theme.palette.accent.opacity(0.1), in: Circle())+ .accessibilityHidden(true)+ VStack(alignment: .leading, spacing: self.theme.metrics.spacing.sm) {+ Text(title).font(self.theme.typography.sectionTitle)+ Text(detail).font(self.theme.typography.body)+ .foregroundStyle(self.theme.palette.secondaryText)+ }+ }+ }++ @ViewBuilder private var captureError: some View {+ if let error {+ Text(error)+ .font(self.theme.typography.body)+ .foregroundStyle(self.theme.palette.warning)+ .frame(maxWidth: .infinity)+ .multilineTextAlignment(.center)+ .accessibilityLabel("Recording issue: \(error)")+ }+ }++ private var captureControls: some View {+ VStack(spacing: self.theme.metrics.spacing.lg) {+ self.stableInstruction("Say only “\(self.word)” once, then wait.", review: "Ready to add “\(self.word)”")+ .font(self.theme.typography.title)+ .foregroundStyle(self.theme.palette.primaryText)+ .fixedSize(horizontal: false, vertical: true)+ self.stableInstruction("We’ll stop automatically and let you know when to speak again.", review: "Add it now, or record another example.")+ .font(self.theme.typography.body)+ .foregroundStyle(self.theme.palette.secondaryText)+ ZStack(alignment: .top) {+ if self.step == .review {+ self.captureRecordActions.hidden().accessibilityHidden(true)+ self.reviewActions+ } else {+ self.reviewActions.hidden().accessibilityHidden(true)+ self.captureRecordActions+ }+ }+ }+ }++ private func stableInstruction(_ recording: String, review: String) -> some View {+ ZStack {+ Text(recording).hidden().accessibilityHidden(true)+ Text(review).hidden().accessibilityHidden(true)+ Text(self.step == .review ? review : recording)+ }+ }++ private var captureRecordActions: some View {+ VStack(spacing: self.theme.metrics.spacing.md) {+ Button(action: self.onRecord) {+ ZStack {+ Text("Stop recording").hidden()+ Text(DictionaryCaptureAction.title(active: self.captureActive, failed: self.error != nil, count: self.count))+ }+ }+ .buttonStyle(FluidRecordInvitationStyle(inviting: !self.captureActive && self.count < 1, recording: self.captureActive))+ .disabled(self.processing || self.starting)+ self.redoButton+ .hidden()+ .accessibilityHidden(true)+ .overlay {+ if self.count >= 1, !self.captureActive {+ self.redoButton+ }+ }+ }+ }++ private var captureActive: Bool {+ self.automaticCaptureActive || self.recording || self.processing || self.starting+ }++ private var redoButton: some View {+ Button("Redo recordings") { self.confirmingRedo = true }+ .fluidGlassAction()+ .disabled(self.busy)+ }++ private var manualExplanation: some View {+ VStack(alignment: .leading, spacing: self.theme.metrics.spacing.sm) {+ Text("Or, fix a specific spelling")+ .font(self.theme.typography.sectionTitle)+ Text("Tell us what FluidVoice types and what you want instead.")+ .font(self.theme.typography.body)+ .foregroundStyle(self.theme.palette.secondaryText)+ .fixedSize(horizontal: false, vertical: true)+ HStack(spacing: self.theme.metrics.spacing.md) {+ Text("chat GPT").foregroundStyle(self.theme.palette.secondaryText)+ Image(systemName: "arrow.right").foregroundStyle(self.theme.palette.secondaryText)+ Text("ChatGPT").foregroundStyle(self.theme.palette.accent)+ }+ .font(self.theme.typography.body)+ .accessibilityElement(children: .ignore)+ .accessibilityLabel("Example: chat space GPT becomes ChatGPT")+ }+ }++ private var manualButton: some View {+ Button(action: self.onManual) {+ Label("Add manually", systemImage: "keyboard")+ }+ .fluidGlassAction()+ }++ private var reviewActions: some View {+ DictionaryReviewActionLayout(spacing: self.theme.metrics.spacing.lg) {+ self.reviewButtons+ }+ }++ @ViewBuilder private var reviewButtons: some View {+ Button("Redo recordings") { self.confirmingRedo = true }+ .buttonStyle(FluidRecordInvitationStyle(inviting: false, recording: false, prominent: false, fillsWidth: true))+ .disabled(self.busy)+ Button(action: self.onSave) {+ ZStack {+ Text("Add “\(self.word)” to dictionary").hidden().accessibilityHidden(true)+ Text(self.busy ? "Saving…" : "Add “\(self.word)” to dictionary")+ }+ }+ .buttonStyle(FluidRecordInvitationStyle(inviting: self.step == .review && !self.busy, recording: false, fillsWidth: true))+ .disabled(self.busy)+ if self.count < CustomDictionaryTrainingMerge.maxSamples {+ VStack(spacing: self.theme.metrics.spacing.sm) {+ Button("Record more", action: self.onRecord)+ .buttonStyle(FluidRecordInvitationStyle(inviting: false, recording: false, prominent: false, fillsWidth: true))+ .disabled(self.busy)+ Text("Optional · Recommended for tricky words")+ .font(self.theme.typography.caption)+ .foregroundStyle(self.theme.palette.secondaryText)+ .frame(maxWidth: 190)+ .fixedSize(horizontal: false, vertical: true)+ }+ }+ }++ private var saved: some View {+ VStack(spacing: self.theme.metrics.spacing.lg) {+ DictionaryWordPlayground(word: self.savedWord, onPracticeMore: self.onPracticeMore, busy: self.$playgroundBusy)+ Button("Add another word", action: self.onNewWord)+ .fluidGlassAction()+ .disabled(self.playgroundBusy)+ }+ }++ private var capturedSpellings: [String] {+ var seen = Set<String>()+ return (self.variants + [self.heard]).filter {+ !$0.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty && seen.insert($0.lowercased()).inserted+ }+ }++ @ViewBuilder private var capturedVariations: some View {+ if !self.capturedSpellings.isEmpty {+ VStack(spacing: self.theme.metrics.spacing.sm) {+ Text("Spellings linked to “\(self.word)”")+ .font(self.theme.typography.caption)+ .foregroundStyle(self.theme.palette.secondaryText)+ FlowLayout(spacing: 6) {+ ForEach(self.capturedSpellings, id: \.self) { variant in+ Text(variant)+ .font(self.theme.typography.caption)+ .padding(.horizontal, 10).padding(.vertical, 6)+ .background(self.theme.palette.accent.opacity(0.08), in: Capsule())+ }+ }+ }+ }+ }++ private func heading(_ title: String, detail: String) -> some View {+ VStack(spacing: self.theme.metrics.spacing.sm) {+ Text(title).font(self.theme.typography.sectionTitle)+ Text(detail).font(self.theme.typography.bodySmall).foregroundStyle(self.theme.palette.secondaryText)+ }+ .multilineTextAlignment(.center)+ .fixedSize(horizontal: false, vertical: true)+ }++ private func primary(_ title: String, action: @escaping () -> Void) -> some View {+ Button(action: action) {+ Text(title)+ }+ .fluidGlassAction(prominent: true, tone: self.theme.palette.accent)+ }+}++/// Equal columns keep Add to dictionary on the ring's centre line despite unequal button labels.+/// Stack the same controls when their natural widths cannot fit without clipping.+private struct DictionaryReviewActionLayout: Layout {+ let spacing: CGFloat++ private func measurements(_ subviews: Subviews) -> (width: CGFloat, height: CGFloat) {+ let sizes = subviews.map { $0.sizeThatFits(.unspecified) }+ return (sizes.map(\.width).max() ?? 0, sizes.map(\.height).max() ?? 0)+ }++ func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize {+ let cell = self.measurements(subviews)+ let rowWidth = cell.width * CGFloat(subviews.count) + self.spacing * CGFloat(max(0, subviews.count - 1))+ if rowWidth <= proposal.width ?? .infinity {+ return CGSize(width: rowWidth, height: cell.height)+ }+ let height = subviews.reduce(CGFloat.zero) { $0 + $1.sizeThatFits(.unspecified).height }+ + self.spacing * CGFloat(max(0, subviews.count - 1))+ return CGSize(width: cell.width, height: height)+ }++ func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) {+ let cell = self.measurements(subviews)+ let rowWidth = cell.width * CGFloat(subviews.count) + self.spacing * CGFloat(max(0, subviews.count - 1))+ let horizontal = rowWidth <= proposal.width ?? .infinity+ var y = bounds.minY+ for (index, subview) in subviews.enumerated() {+ let x = horizontal+ ? bounds.midX - rowWidth / 2 + cell.width / 2 + CGFloat(index) * (cell.width + self.spacing)+ : bounds.midX+ subview.place(at: CGPoint(x: x, y: y), anchor: .top, proposal: ProposedViewSize(width: cell.width, height: nil))+ if !horizontal { y += subview.sizeThatFits(.unspecified).height + self.spacing }+ }+ }+}++enum DictionaryLearningEncouragement {+ static func messages(for count: Int) -> [String] {+ switch count {+ case ...0: ["Ready when you are"]+ case 1: ["Nice! 2 more to go"]+ case 2: ["One more—you’ve got this"]+ default: ["Got it! Ready to add"]+ }+ }+}++/// Bounded ambient motion redraws only the artwork; text stays stable and hidden rings stop updating.+private struct DictionaryLearningRing: View {+ let progress: Double+ let active: Bool+ let symbol: String+ var diameter: CGFloat = 176+ var count: Int? = nil+ var processing = false+ var audioLevels: AnyPublisher<CGFloat, Never> = Empty().eraseToAnyPublisher()+ @Environment(\.theme) private var theme+ @Environment(\.accessibilityReduceMotion) private var reduceMotion+ @Environment(\.accessibilityReduceTransparency) private var reduceTransparency+ @Environment(\.scenePhase) private var scenePhase++ // Match the preview blue explicitly, independent of the selected app accent or appearance.+ private let learningBlue = Color(red: 10 / 255, green: 132 / 255, blue: 1)++ @State private var isVisible = false+ @State private var voiceEnergy = 0.0+ @State private var listening = 0.0+ @State private var pulse = 0.0+ @State private var highlightsProgress = false+ @State private var completionBloom = 1.0++ private var animates: Bool {+ self.isVisible && !self.reduceMotion && self.scenePhase == .active &&+ (self.active || (self.count != nil && self.progress < 1))+ }++ var body: some View {+ ZStack {+ if !self.reduceTransparency {+ Circle()+ .fill(RadialGradient(+ colors: [self.learningBlue.opacity(0.04 + self.progress * 0.14), .clear],+ center: .center,+ startRadius: 10,+ endRadius: self.diameter / 2+ ))+ }+ TimelineView(.animation(minimumInterval: 1.0 / 30, paused: !self.animates)) { context in+ let phase = self.reduceMotion ? 0 : context.date.timeIntervalSinceReferenceDate.truncatingRemainder(dividingBy: 14) / 14 * .pi * 2+ DictionaryRingArtwork(+ phase: phase,+ progress: self.progress,+ energy: self.reduceMotion ? 0 : self.voiceEnergy,+ glow: max(self.listening, self.pulse, self.progress >= 1 ? 0.5 : 0),+ accent: self.learningBlue,+ rest: self.theme.palette.secondaryText,+ additive: !self.reduceTransparency+ )+ }+ .animation(self.reduceMotion ? nil : .easeInOut(duration: 0.65), value: self.progress)+ .animation(self.reduceMotion ? nil : .easeOut(duration: 0.12), value: self.voiceEnergy)+ if let count, count >= 3, !self.reduceMotion {+ DictionaryCompletionBloom(progress: self.completionBloom, color: self.learningBlue)+ .accessibilityHidden(true)+ .allowsHitTesting(false)+ }+ if let count {+ ZStack {+ Text(DictionaryLearningEncouragement.messages(for: count)[0])+ .font(self.theme.typography.bodyStrong)+ .foregroundStyle(self.highlightsProgress ? self.learningBlue : self.theme.palette.primaryText)+ .animation(self.reduceMotion ? nil : .easeInOut(duration: 0.9), value: self.highlightsProgress)+ .multilineTextAlignment(.center)+ .frame(height: 60)+ HStack(spacing: 8) {+ ForEach(0..<3) { index in+ Circle()+ .fill(index < count ? self.learningBlue : self.theme.palette.secondaryText.opacity(0.2))+ .frame(width: 8, height: 8)+ }+ }+ .offset(y: 44)+ ZStack {+ if self.processing { ProgressView().controlSize(.small) }+ }+ .frame(height: 20)+ .offset(y: 66)+ }+ .frame(width: self.diameter * 0.57, height: self.diameter)+ .transaction { $0.animation = nil }+ } else {+ Image(systemName: self.symbol)+ .font(.system(size: 27, weight: .light))+ .foregroundStyle(self.theme.palette.secondaryText)+ }+ }+ .frame(width: self.diameter, height: self.diameter)+ .animation(self.reduceMotion ? nil : .easeInOut(duration: 0.4), value: self.progress)+ .transaction { if self.reduceMotion { $0.animation = nil } }+ .accessibilityHidden(true)+ .allowsHitTesting(false)+ .onAppear { self.isVisible = true; self.listening = self.active ? 1 : 0 }+ .onDisappear { self.isVisible = false; self.voiceEnergy = 0 }+ .task(id: (self.count ?? 0) >= 3) {+ guard (self.count ?? 0) >= 3, !self.reduceMotion else {+ self.completionBloom = 1+ return+ }+ var reset = Transaction()+ reset.disablesAnimations = true+ withTransaction(reset) { self.completionBloom = 0 }+ do { try await Task.sleep(for: .milliseconds(40)) } catch { return }+ withAnimation(.easeOut(duration: 1.1)) { self.completionBloom = 1 }+ }+ .task(id: self.count) {+ var reset = Transaction()+ reset.disablesAnimations = true+ withTransaction(reset) { self.highlightsProgress = false }+ guard let count = self.count, count > 0 else { return }+ if !self.reduceMotion {+ do { try await Task.sleep(for: .milliseconds(80)) } catch { return }+ }+ guard !Task.isCancelled else { return }+ self.highlightsProgress = true+ }+ .onChange(of: self.active) { _, active in+ if !active { self.voiceEnergy = 0 }+ withAnimation(self.reduceMotion ? nil : .easeInOut(duration: 0.5)) { self.listening = active ? 1 : 0 }+ }+ .onChange(of: self.count) { old, new in+ // A captured word flashes the ring, then settles into the new progress.+ guard let old, let new, new > old, !self.reduceMotion else { return }+ self.pulse = 1+ withAnimation(.easeOut(duration: 1.4)) { self.pulse = 0 }+ }+ .onChange(of: self.scenePhase) { _, phase in+ if phase != .active { self.voiceEnergy = 0 }+ }+ .onReceive(self.audioLevels.throttle(for: .milliseconds(50), scheduler: RunLoop.main, latest: true)) { level in+ guard self.active, self.isVisible, self.scenePhase == .active, !self.reduceMotion else { return }+ let target = DictionaryRingResponse.energy(for: level)+ self.voiceEnergy += (target - self.voiceEnergy) * (target > self.voiceEnergy ? 0.75 : 0.35)+ }+ }+}++/// A single contained release of light at the third capture, with no change to layout or hit targets.+private struct DictionaryCompletionBloom: View, Animatable {+ var progress: Double+ let color: Color++ var animatableData: Double {+ get { self.progress }+ set { self.progress = newValue }+ }++ var body: some View {+ Canvas { context, size in+ let phase = min(1, max(0, self.progress))+ guard phase < 1 else { return }+ let centre = CGPoint(x: size.width / 2, y: size.height / 2)+ let radius = min(size.width, size.height) * (0.27 + phase * 0.19)+ let opacity = sin(phase * .pi)+ context.stroke(Path(ellipseIn: CGRect(x: centre.x - radius, y: centre.y - radius, width: radius * 2, height: radius * 2)), with: .color(self.color.opacity(opacity * 0.5)), lineWidth: 2 * (1 - phase) + 0.5)+ for index in 0..<16 {+ let angle = Double(index) / 16 * .pi * 2+ let length = 3 + (1 - phase) * 7+ var ray = Path()+ ray.move(to: CGPoint(x: centre.x + cos(angle) * (radius - length), y: centre.y + sin(angle) * (radius - length)))+ ray.addLine(to: CGPoint(x: centre.x + cos(angle) * radius, y: centre.y + sin(angle) * radius))+ context.stroke(ray, with: .color(self.color.opacity(opacity * 0.8)), style: StrokeStyle(lineWidth: 1.5, lineCap: .round))+ }+ }+ }+}++enum DictionaryRingResponse {+ static func energy(for level: CGFloat) -> Double {+ guard level.isFinite else { return 0 }+ return sqrt(Double(min(1, max(0, level))))+ }+}++/// Three translucent sheets fan wide and pinch loosely; lines inside a sheet never cross, so the ring reads as silk, not rope.+private struct DictionaryRingArtwork: View, Animatable {+ let phase: Double+ var progress: Double+ var energy: Double+ var glow: Double+ let accent: Color+ let rest: Color+ let additive: Bool++ var animatableData: AnimatablePair<Double, AnimatablePair<Double, Double>> {+ get { AnimatablePair(self.progress, AnimatablePair(self.energy, self.glow)) }+ set {+ self.progress = newValue.first+ self.energy = newValue.second.first+ self.glow = newValue.second.second+ }+ }++ var body: some View {+ Canvas(rendersAsynchronously: true) { context, size in+ let rect = CGRect(origin: .zero, size: size)+ let lit = min(1, max(0, self.glow) * 0.7 + self.energy * 0.5)+ let shading = GraphicsContext.Shading.conicGradient(+ Gradient(stops: self.stops),+ center: CGPoint(x: rect.midX, y: rect.midY),+ angle: .degrees(-90)+ )+ let paths = (0..<DictionaryRibbon.sheets).flatMap { sheet in+ (0..<7).map { line in+ DictionaryRibbon(phase: self.phase, strand: Double(line - 3) * 4 / 3, energy: self.energy, sheet: sheet).path(in: rect)+ }+ }+ if self.additive { context.blendMode = .plusLighter }+ context.drawLayer { layer in+ layer.addFilter(.blur(radius: 5 + lit * 5))+ layer.opacity = 0.3 + lit * 0.35+ for path in paths {+ layer.stroke(path, with: shading, lineWidth: 1.6 + lit * 0.8)+ }+ }+ context.opacity = 0.5 + lit * 0.3+ for path in paths {+ context.stroke(path, with: shading, lineWidth: 0.7 + lit * 0.25)+ }+ }+ }++ /// The learned arc fades into the resting color instead of ending on a hard edge.+ private var stops: [Gradient.Stop] {+ let learned = self.accent+ let resting = self.rest.opacity(0.55)+ guard self.progress > 0.001 else { return [.init(color: resting, location: 0), .init(color: resting, location: 1)] }+ guard self.progress < 0.999 else { return [.init(color: learned, location: 0), .init(color: learned, location: 1)] }+ let fade = 0.1+ let start = max(0, self.progress - fade / 2)+ let end = min(1, max(start, self.progress + fade / 2))+ let seam = max(end, 1 - fade)+ return [+ .init(color: learned, location: 0),+ .init(color: learned, location: start),+ .init(color: resting, location: end),+ .init(color: resting, location: seam),+ .init(color: learned, location: 1),+ ]+ }+}++struct DictionaryRibbon: Shape {+ static let sheets = 3++ let phase: Double+ let strand: Double+ let energy: Double+ var sheet = 0++ func path(in rect: CGRect) -> Path {+ let radius = min(rect.width, rect.height) * 0.34+ let offset = Double(self.sheet) * 2.1+ let lobes = Double([3, 2, 4][self.sheet % 3])+ let drift = self.sheet.isMultiple(of: 2) ? self.phase : -self.phase+ let spread = self.strand / 4+ return Path { path in+ for sample in 0...180 {+ let angle = Double(sample) / 180 * .pi * 2+ let center = 1 + sin(angle * 3 + offset + self.phase) * 0.05 + sin(angle * 2 + offset * 0.6 - self.phase) * 0.03 + self.energy * 0.02+ // The sheet twists through a loose pinch, then opens into a wide veil; speech opens it further.+ let width = cos(angle * lobes + offset + drift) * (0.11 + self.energy * 0.05) + 0.018+ let distance = radius * (center + spread * width)+ let point = CGPoint(+ x: rect.midX + cos(angle) * distance,+ y: rect.midY + sin(angle) * distance+ )+ if sample == 0 { path.move(to: point) } else { path.addLine(to: point) }+ }+ path.closeSubpath()+ }+ }+}++/// Uses normal shortcut dictation, accepting output only for a capture begun in this editor.+struct DictionaryWordPlayground: View {+ let word: String+ let onPracticeMore: () -> Void+ @Binding var busy: Bool+ @EnvironmentObject private var appServices: AppServices+ @Environment(\.theme) private var theme+ @FocusState private var editorFocused: Bool+ @State private var test = DictionaryShortcutTestState()+ @AppStorage("DictionaryPronunciationDebugCapture") private var debugCapture = false++ private var shortcut: String {+ let value = SettingsStore.shared.primaryDictationShortcutDisplayString+ return value.isEmpty ? "your dictation shortcut" : value+ }++ var body: some View {+ VStack(alignment: .leading, spacing: self.theme.metrics.spacing.md) {+ Text("Test “\(self.word)”")+ .font(self.theme.typography.title)+ ZStack(alignment: .topLeading) {+ TextEditor(text: Binding(get: { self.test.text }, set: { self.test.edit($0) }))+ .font(self.theme.typography.body)+ .scrollContentBackground(.hidden)+ .focused(self.$editorFocused)+ .padding(10)+ .accessibilityLabel("Dictionary test text")+ if self.test.text.isEmpty {+ Text(self.test.phase == .recording ? "Listening…" : (self.test.phase == .processing ? "Checking…" : "Use \(self.shortcut) and say something with “\(self.word)”."))+ .font(self.theme.typography.body)+ .foregroundStyle(self.theme.palette.secondaryText)+ .padding(15)+ .allowsHitTesting(false)+ }+ }+ .frame(height: 150)+ .background(self.theme.palette.contentBackground, in: RoundedRectangle(cornerRadius: self.theme.metrics.corners.md))+ .overlay {+ RoundedRectangle(cornerRadius: self.theme.metrics.corners.md)+ .stroke(self.editorFocused ? self.theme.palette.accent.opacity(0.5) : self.theme.palette.cardBorder, lineWidth: 1)+ .allowsHitTesting(false)+ }+ switch self.test.result {+ case .recognized:+ Text("“\(self.word)” recognized" + (self.test.successes > 1 ? " · \(self.test.successes) times" : ""))+ .font(self.theme.typography.bodyStrong)+ .foregroundStyle(self.theme.palette.accent)+ case .missed:+ Text("We didn’t catch “\(self.word)”. Try again, or add more examples.")+ .font(self.theme.typography.body)+ .foregroundStyle(self.theme.palette.secondaryText)+ Button("Record more examples", action: self.onPracticeMore)+ .fluidGlassAction()+ .disabled(self.busy)+ case .empty:+ Text("No words came through. Try your shortcut again.")+ .font(self.theme.typography.body)+ .foregroundStyle(self.theme.palette.secondaryText)+ case .none:+ EmptyView()+ }+ if DictionaryPronunciationExperiment.enabled {+ Toggle("Save dictionary debug audio locally", isOn: self.$debugCapture)+ .font(self.theme.typography.caption)+ .help("Saves audio, embeddings, cuts and scores locally. Switch off to stop collection. Keeps at most 50 debug reports / 200 MB.")+ }+ }+ .task(id: self.word) {+ self.test = DictionaryShortcutTestState()+ self.editorFocused = true+ self.busy = false+ }+ .onReceive(self.appServices.asr.$isRunning.dropFirst()) { running in+ if running {+ self.test.begin(editorFocused: self.editorFocused, appActive: NSApp.isActive, dictionaryCapture: self.appServices.asr.isDictionaryTrainingCaptureActive)+ } else {+ self.test.stopped()+ }+ self.busy = self.test.phase != .idle+ }+ .onReceive(self.appServices.asr.dictionaryTestAudioPublisher) { samples in+ self.test.receiveAudio(samples)+ }+ .onReceive(self.appServices.asr.$finalText.dropFirst()) { text in+ // Start clears shared output; only the final, nonempty value belongs to a test result.+ guard !text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else { return }+ self.test.receive(text, word: self.word)+ self.busy = self.test.phase != .idle+ }+ .onReceive(self.appServices.asr.$showError.dropFirst()) { failed in+ if failed, let attempt = self.test.attemptID {+ self.test.expire(attempt)+ self.busy = false+ }+ }+ .task(id: self.test.phase == .processing ? self.test.attemptID : nil) {+ guard self.test.phase == .processing, let attempt = self.test.attemptID else { return }+ do { try await Task.sleep(for: .seconds(60)) } catch { return }+ self.test.expire(attempt)+ self.busy = false+ }+ .onReceive(NotificationCenter.default.publisher(for: NSApplication.didResignActiveNotification)) { _ in+ self.test.cancel()+ self.busy = false+ }+ .onDisappear {+ self.test.cancel()+ self.busy = false+ }+ }+}diff --git a/Tests/DictionaryMatchReportTests.swift b/Tests/DictionaryMatchReportTests.swiftnew file mode 100644index 00000000..ef56232f--- /dev/null+++ b/Tests/DictionaryMatchReportTests.swift@@ -0,0 +1,81 @@+import Foundation++@main+struct DictionaryMatchReportTests {+ static func main() throws {+ var candidate = DictionaryMatchReport.Candidate(+ id: "target",+ word: "ModelOpt",+ sampleNumber: nil,+ enrollmentCount: 3,+ eligible: true,+ score: 0.699,+ start: 0,+ end: 0.5,+ heard: "model opt"+ )+ precondition(candidate.explanation.contains("Below cutoff"))+ candidate.score = 0.74+ precondition(candidate.explanation.contains("Above score cutoff"))+ candidate.competingWord = "Other"+ candidate.competingScore = 0.72+ precondition(candidate.explanation.contains("competing"))+ candidate.competingScore = nil+ candidate.heard = ""+ precondition(candidate.explanation.contains("no word overlaps"))+ let ineligible = DictionaryMatchReport.Candidate(+ id: "untrained", word: "Other", sampleNumber: nil, enrollmentCount: 1, eligible: false, score: 0.9+ )+ precondition(ineligible.explanation.contains("needs more"))+ candidate.score = nil+ precondition(candidate.explanation == "No usable audio window")++ let capture = PronunciationEnrollmentCapture(values: [1, 0], sourceFrameCount: 4, modelKey: "parakeet-v2", originalAudioID: UUID(), observedText: "model opt")+ let profile = PronunciationDictionaryProfile(dictionaryEntryID: UUID(), label: "ModelOpt", modelKey: "parakeet-v2", hiddenSize: 2, enrollments: [capture])+ precondition(DictionaryPronunciationDecision.requiredScore(heardText: "Model opt!", profile: profile) == 0.70)+ precondition(DictionaryPronunciationDecision.requiredScore(heardText: "other words", profile: profile) == 0.85)+ precondition(!DictionaryPronunciationDecision.accepts(score: .nan, heardText: "model opt", profile: profile))++ var tuned = profile+ tuned.matchThreshold = 0.45+ precondition(DictionaryPronunciationDecision.accepts(score: 0.49, heardText: "other words", profile: tuned))+ precondition(!DictionaryPronunciationDecision.accepts(score: 0.49, heardText: "other words", profile: profile))+ precondition(DictionaryPronunciationDecision.minimumSearchScore(profiles: [profile, tuned]) == 0.45)+ tuned.matchThreshold = .nan+ precondition(DictionaryPronunciationDecision.requiredScore(heardText: "other words", profile: tuned) == 0.85)+ tuned.matchThreshold = 0.96+ precondition(DictionaryPronunciationDecision.requiredScore(heardText: "model opt", profile: tuned) == 0.70)+ let legacy = try JSONDecoder().decode(PronunciationDictionaryProfile.self, from: JSONEncoder().encode(profile))+ precondition(legacy.matchThreshold == nil)++ let individual = [Float(0.49), 0.83, 0.62].enumerated().map { index, score in+ DictionaryMatchReport.Candidate(id: "sample-\(index)", word: "ModelOpt", sampleNumber: index + 1, enrollmentCount: 3, eligible: true, score: score, heard: "sample \(index + 1)")+ }+ let selected = DictionaryMatchReport.bestCandidates(from: individual)+ precondition(selected.count == 1 && selected[0].score == 0.83)+ precondition(selected[0].heard == "sample 2", "The decision retains the winning recording's evidence")+ precondition(individual.map(\.score) == [0.49, 0.83, 0.62], "Display all original scores without averaging")++ let report = DictionaryMatchReport(+ createdAt: Date(),+ recordingID: UUID(),+ audioPath: "/tmp/test.wav",+ targetWord: "ModelOpt",+ modelKey: "parakeet-v2",+ duration: 1,+ rawTranscript: "model opt",+ savedTranscript: "ModelOpt",+ availableProfileCount: 1,+ candidates: [candidate],+ targetSamples: [],+ profiles: [profile]+ )+ let data = try JSONEncoder().encode(report)+ let decoded = try JSONDecoder().decode(DictionaryMatchReport.self, from: data)+ precondition(decoded.profiles == [profile], "Export must preserve the reference embeddings")+ precondition(decoded.recordingID == report.recordingID)+ precondition(decoded.rawTranscript == "model opt" && decoded.savedTranscript == "ModelOpt")+ precondition(decoded.candidates.first?.score == nil, "Unavailable must not become a zero score")+ print("PASS: below-cutoff scores, competitors, missing alignment, eligibility, unavailable scores, shared thresholds, report round-trip")+ }+}diff --git a/Tests/DictionaryReplacementDiagnosticsTests.swift b/Tests/DictionaryReplacementDiagnosticsTests.swiftnew file mode 100644index 00000000..cc874906--- /dev/null+++ b/Tests/DictionaryReplacementDiagnosticsTests.swift@@ -0,0 +1,57 @@+import Foundation++@main+struct DictionaryReplacementDiagnosticsTests {+ static func main() throws {+ let regex = try NSRegularExpression(pattern: "\\bbut\\b", options: .caseInsensitive)+ let input = "🙂 But wait, but don't change butter."+ var events: [(NSRange, String, String)] = []+ let output = DictionaryReplacementProtection.replacingMatches(+ in: input,+ regex: regex,+ template: "buddamthss",+ canonical: nil,+ onReplacement: { events.append(($0, $1, $2)) }+ )+ precondition(output == "🙂 buddamthss wait, buddamthss don't change butter.")+ precondition(events.count == 2)+ precondition(Set(events.map { $0.1 }) == Set(["But", "but"]))+ for event in events {+ precondition((input as NSString).substring(with: event.0) == event.1)+ precondition(event.2 == "buddamthss")+ }+ precondition(output == DictionaryReplacementProtection.replacingMatches(+ in: input, regex: regex, template: "buddamthss", canonical: nil+ ), "Diagnostics must not change output")++ let canonical = try NSRegularExpression(pattern: "\\bbut better\\b", options: .caseInsensitive)+ events.removeAll()+ let protected = DictionaryReplacementProtection.replacingMatches(+ in: "but better but",+ regex: regex,+ template: "but better",+ canonical: canonical,+ onReplacement: { events.append(($0, $1, $2)) }+ )+ precondition(protected == "but better but better")+ precondition(events.count == 1 && events[0].0.location == 11)++ events.removeAll()+ for text in ["but", "butter", "", "no match"] {+ precondition(DictionaryReplacementProtection.replacingMatches(+ in: text,+ regex: regex,+ template: "but",+ canonical: nil,+ onReplacement: { events.append(($0, $1, $2)) }+ ) == text)+ }+ precondition(events.isEmpty, "Unchanged text must not report a replacement")++ let quoted = DictionaryReplacementDiagnostics.quoted("a\n\t\"b")+ precondition(!quoted.contains("\n") && !quoted.contains("\t"))+ precondition(quoted.contains("\\n") && quoted.contains("\\\""))+ precondition(DictionaryReplacementDiagnostics.quoted(String(repeating: "x", count: 10_000)).count == 163)+ print("PASS: replacement diagnostics, original UTF-16 ranges, canonical protection, no-ops, bounded escaping")+ }+}diff --git a/Tests/DictionaryShortcutTestStateTests.swift b/Tests/DictionaryShortcutTestStateTests.swiftnew file mode 100644index 00000000..ba50d057--- /dev/null+++ b/Tests/DictionaryShortcutTestStateTests.swift@@ -0,0 +1,96 @@+import Foundation++@main+enum DictionaryShortcutTestStateTests {+ static func main() {+ func expect(_ condition: @autoclosure () -> Bool, _ message: String) {+ precondition(condition(), message)+ }+ for count in [0, 1, 2, 3] {+ expect(DictionaryCaptureAction.title(active: false, failed: true, count: count) == "Record again", "Every failed capture offers retry")+ expect(DictionaryCaptureAction.title(active: true, failed: true, count: count) == "Stop recording", "Active capture retains Stop")+ }+ expect(DictionaryCaptureAction.title(active: false, failed: false, count: 0) == "Record", "First capture")+ expect(DictionaryCaptureAction.title(active: false, failed: false, count: 1) == "Record more", "Additional successful capture")++ var test = DictionaryShortcutTestState()+ test.receive("ModelOpt", word: "ModelOpt")+ expect(test.text.isEmpty && test.result == .none, "Ignore stale output on opening the page")+ for eligibility in [(false, true, false), (true, false, false), (true, true, true)] {+ test.begin(editorFocused: eligibility.0, appActive: eligibility.1, dictionaryCapture: eligibility.2)+ test.stopped()+ test.receive("ModelOpt", word: "ModelOpt")+ expect(test.phase == .idle && test.result == .none && test.text.isEmpty, "Ignore unfocused, external, and training captures")+ }+ test.edit("I typed ModelOpt")+ expect(test.result == .none && test.successes == 0, "Typing is not a successful speech test")+ test.begin(editorFocused: true, appActive: true, dictionaryCapture: false)+ guard let first = test.attemptID else { preconditionFailure("Missing first attempt") }+ test.receive("ModelOpt", word: "ModelOpt")+ expect(test.phase == .recording && test.text.isEmpty, "Do not grade a partial capture")+ test.stopped()+ test.receive("I use modelopt.", word: "ModelOpt")+ expect(test.result == .recognized && test.successes == 1 && test.phase == .idle, "Accept the completed focused capture")+ test.receive("Unrelated dictation", word: "ModelOpt")+ expect(test.text == "I use modelopt." && test.successes == 1, "Late output cannot replace a completed test")+ test.begin(editorFocused: true, appActive: true, dictionaryCapture: false)+ test.expire(first)+ expect(test.phase == .recording, "An old timeout cannot cancel the next attempt")+ test.stopped()+ test.receive("I use modelopt.", word: "ModelOpt")+ expect(test.successes == 2, "Identical consecutive dictations each count once")++ test.begin(editorFocused: true, appActive: true, dictionaryCapture: false)+ test.stopped()+ test.receive("Model optional", word: "ModelOpt")+ expect(test.result == .missed && test.successes == 2, "Only a real nonempty miss offers more examples")+ test.edit("ModelOpt")+ expect(test.result == .none && test.successes == 2, "Editing clears stale feedback without counting a success")+ test.begin(editorFocused: true, appActive: true, dictionaryCapture: false)+ test.stopped()+ test.receive(" ", word: "ModelOpt")+ expect(test.result == .empty && test.phase == .idle, "Silence is not a pronunciation mismatch")+ test.begin(editorFocused: true, appActive: true, dictionaryCapture: false)+ test.stopped()+ guard let expiringID = test.attemptID else { preconditionFailure("Missing expiring attempt") }+ test.expire(expiringID)+ expect(test.phase == .idle && test.result == .empty, "Missing output has a bounded exit")+ test.begin(editorFocused: true, appActive: true, dictionaryCapture: false)+ test.stopped()+ test.cancel()+ test.receive("ModelOpt", word: "ModelOpt")+ expect(test.phase == .idle && test.text.isEmpty && test.successes == 2, "Leaving the app ignores late output")++ let samples: [Float] = [0.1, -0.1]+ test.receiveAudio(samples)+ expect(test.recording == nil, "Unowned audio cannot become the current test")+ test.begin(editorFocused: true, appActive: true, dictionaryCapture: false)+ guard let currentID = test.attemptID else { preconditionFailure("Missing current attempt") }+ test.stopped()+ test.receiveAudio(samples)+ test.receive("Model optional", word: "ModelOpt")+ expect(test.recording?.id == currentID && test.recording?.text == test.text, "Audio must belong to the exact textbox attempt")+ expect(test.recording?.samples == samples, "Automatic scoring must work without saving history")+ test.edit(test.text)+ expect(test.recording?.id == currentID, "Identical binding updates preserve the current recording")+ test.edit("edited text")+ expect(test.recording == nil, "Manual edits must invalidate old audio scores")+ test.begin(editorFocused: true, appActive: true, dictionaryCapture: false)+ test.stopped()+ test.receiveAudio(samples)+ test.cancel()+ test.receive("late text", word: "ModelOpt")+ expect(test.recording == nil, "Cancelled audio cannot become a later score")+ let historyID = UUID()+ test.useHistory(id: historyID, text: "older sentence", url: URL(fileURLWithPath: "/tmp/old.wav"))+ expect(test.recording?.id == historyID && test.text == "older sentence", "History and textbox must move together")+ test.begin(editorFocused: true, appActive: true, dictionaryCapture: false)+ expect(test.recording == nil, "The next live attempt replaces history immediately")++ expect(DictionaryWordTestResult.containsWord("C++", in: "I write C++."), "Literal punctuation")+ expect(DictionaryWordTestResult.containsWord("Nemo-Gym", in: "Use nemo-gym today."), "Hyphenated words")+ expect(!DictionaryWordTestResult.containsWord("cat", in: "category"), "Whole words only")+ expect(!DictionaryWordTestResult.containsWord("", in: "hello"), "An empty target cannot pass")+ print("Dictionary shortcut tests passed: retry labels, focused output, repeated tests, misses, empty output, stale results, cancellation, timeout, and manual edits.")+ }+}diff --git a/Tests/FluidDictationIntegrationTests/CustomDictionaryManualEntryTests.swift b/Tests/FluidDictationIntegrationTests/CustomDictionaryManualEntryTests.swiftindex 5ce5af7c..3a4a0513 100644--- a/Tests/FluidDictationIntegrationTests/CustomDictionaryManualEntryTests.swift+++ b/Tests/FluidDictationIntegrationTests/CustomDictionaryManualEntryTests.swift@@ -1,9 +1,43 @@+import AppKit@testable import FluidVoice_Debugimport Foundation+import SwiftUIimport XCTest+#if arch(arm64)+import FluidAudio+#endif@MainActorfinal class CustomDictionaryManualEntryTests: XCTestCase {+ func testVoiceTrainingKeepsPronunciationWithoutEverydayTextAliases() async {+ let filtered = await VoiceTrainingAliasFilter.filter(["but", "now", "right now"]) { _ in .checked([]) }+ let entries = CustomDictionaryTrainingMerge.mergedEntries(+ current: [], replacement: "Lyft", triggers: filtered.accepted, savePronunciation: true+ )+ XCTAssertEqual(entries.count, 1)+ XCTAssertEqual(entries.first?.replacement, "Lyft")+ XCTAssertEqual(entries.first?.triggers, ["lyft"], "Only the canonical spelling anchors the voice profile")+ }++ func testVoiceTrainingFilterPreservesExistingManualRules() async {+ let manual = SettingsStore.CustomDictionaryEntry(triggers: ["now"], replacement: "Lyft")+ let unrelated = SettingsStore.CustomDictionaryEntry(triggers: ["but"], replacement: "ManualChoice")+ let filtered = await VoiceTrainingAliasFilter.filter(["now", "hello"]) { _ in .checked([]) }+ let entries = CustomDictionaryTrainingMerge.mergedEntries(+ current: [manual, unrelated], replacement: "Lyft", triggers: filtered.accepted, savePronunciation: true+ )+ XCTAssertEqual(entries, [manual, unrelated])+ XCTAssertEqual(CustomDictionaryManualEntry.normalizedTriggers(["but", "now", "right now"]), ["but", "now", "right now"])+ }++ func testUnavailableVocabularyCannotCreateTextOnlyTrainingRules() async {+ let filtered = await VoiceTrainingAliasFilter.filter(["now"]) { _ in .unavailable }+ let entries = CustomDictionaryTrainingMerge.mergedEntries(+ current: [], replacement: "Lyft", triggers: filtered.accepted+ )+ XCTAssertTrue(entries.isEmpty)+ }+func testKeepsWhitespaceOnlyReplacements() {XCTAssertEqual(CustomDictionaryManualEntry.sanitizedReplacement("\n"), "\n")XCTAssertEqual(CustomDictionaryManualEntry.sanitizedReplacement(" "), " ")@@ -147,3 +181,376 @@ final class CustomDictionaryManualEntryTests: XCTestCase {try await run()}}++extension CustomDictionaryManualEntryTests {+ func testMatchLevelPersistsOnlyForSelectedWordAndSurvivesMoreTraining() async throws {+ let directory = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)+ defer { try? FileManager.default.removeItem(at: directory) }+ let url = directory.appendingPathComponent("profiles.json")+ let store = PronunciationDictionaryStore(fileURL: url)+ let targetID = UUID(), otherID = UUID()+ let capture = PronunciationEnrollmentCapture(values: [1, 0], sourceFrameCount: 4, modelKey: "parakeet-v2")+ try await store.upsert(dictionaryEntryID: targetID, label: "Target", modelKey: capture.modelKey, enrollments: [capture, capture, capture])+ try await store.upsert(dictionaryEntryID: otherID, label: "Other", modelKey: capture.modelKey, enrollments: [capture, capture, capture])+ let original = await store.allProfiles()+ try await store.setMatchThreshold(0.45, dictionaryEntryID: targetID)+ let reloaded = await PronunciationDictionaryStore(fileURL: url).allProfiles()+ XCTAssertEqual(reloaded.first { $0.dictionaryEntryID == targetID }?.matchThreshold, 0.45)+ XCTAssertEqual(reloaded.first { $0.dictionaryEntryID == otherID }, original.first { $0.dictionaryEntryID == otherID })+ XCTAssertEqual(reloaded.first?.enrollments, original.first?.enrollments)+ try await store.upsert(dictionaryEntryID: targetID, label: "Target", modelKey: capture.modelKey, enrollments: [capture])+ let updated = await store.allProfiles()+ XCTAssertEqual(updated.first { $0.dictionaryEntryID == targetID }?.matchThreshold, 0.45)+ do {+ try await store.setMatchThreshold(.nan, dictionaryEntryID: targetID)+ XCTFail("Invalid levels must not be persisted")+ } catch {}+ do {+ try await store.setMatchThreshold(0.45, dictionaryEntryID: UUID())+ XCTFail("A deleted word must not be recreated by a pending save")+ } catch {}+ let afterInvalid = await store.allProfiles()+ XCTAssertEqual(afterInvalid, updated)+ }+}++extension CustomDictionaryManualEntryTests {+ private func inspectionFixture() -> DictionaryAudioInspection {+ DictionaryAudioInspection(+ samples: Array(repeating: 0, count: 320) + Array(repeating: 0.2, count: 1280) + Array(repeating: 0, count: 960),+ recordedSampleCount: 2400,+ frames: [.init(offset: 0, frameDuration: 0.08, hiddenSize: 2, values: [1, 0, 0, 1])],+ words: [.init(text: "word", start: 0, end: 0.16)],+ selectedStart: 0,+ selectedEnd: 0.16+ )+ }++ func testAudioInspectionCutsAreBoundedAndDoNotMutateEvidence() async throws {+ let audio = self.inspectionFixture()+ XCTAssertTrue(audio.isValid)+ XCTAssertEqual(audio.embedding(start: 0, end: 0.08), [1, 0])+ XCTAssertEqual(audio.embedding(start: 0.08, end: 0.16), [0, 1])+ XCTAssertNil(audio.embedding(start: .nan, end: 0.16))+ XCTAssertNil(audio.embedding(start: 0, end: 1))+ XCTAssertNil(audio.embedding(start: 0.08, end: 0.08))+ let original = audio+ let score = await DictionaryAudioInspector.shared.score(test: audio, testCut: .init(start: 0, end: 0.08), reference: nil, referenceCut: nil, storedVector: [0, 1])+ XCTAssertEqual(score, 0)+ let changed = await DictionaryAudioInspector.shared.score(test: audio, testCut: .init(start: 0.08, end: 0.16), reference: nil, referenceCut: nil, storedVector: [0, 1])+ XCTAssertEqual(changed, 1)+ XCTAssertEqual(audio, original)+ let full = try await DictionaryAudioInspector.shared.wav(audio, cut: nil)+ XCTAssertEqual(full.count, 44 + audio.recordedSampleCount * 2, "Padding must not be played as captured audio")+ let cut = try await DictionaryAudioInspector.shared.wav(audio, cut: .init(start: 0, end: 0.08))+ XCTAssertEqual(cut.count, 44 + 1280 * 2)+ let metrics = await DictionaryAudioInspector.shared.metrics(audio, cut: .init(start: 0, end: 0.16))+ XCTAssertEqual(metrics.leadingQuiet, 0.02, accuracy: 0.001)+ XCTAssertEqual(metrics.trailingQuiet, 0.05, accuracy: 0.001)+ }++ func testAudioInspectionRejectsCrossChunkAndSilentSelections() async {+ let audio = DictionaryAudioInspection(+ samples: Array(repeating: 0, count: 5120),+ recordedSampleCount: 5120,+ frames: [+ .init(offset: 0, frameDuration: 0.08, hiddenSize: 2, values: [1, 0, 1, 0]),+ .init(offset: 0.16, frameDuration: 0.08, hiddenSize: 2, values: [0, 1, 0, 1]),+ ],+ words: [],+ selectedStart: 0,+ selectedEnd: 0.32+ )+ XCTAssertTrue(audio.isValid)+ XCTAssertNil(audio.embedding(start: 0.08, end: 0.24), "Do not pool independent encoder contexts")+ let metrics = await DictionaryAudioInspector.shared.metrics(audio, cut: .init(start: 0, end: 0.32))+ XCTAssertNil(metrics.suggested, "Silent audio must not produce a fabricated word boundary")+ }++ func testTrainingInspectionPersistsSeparatelyAndFollowsEnrollmentLifetime() async throws {+ let directory = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)+ defer { try? FileManager.default.removeItem(at: directory) }+ let url = directory.appendingPathComponent("profiles.json")+ let store = PronunciationDictionaryStore(fileURL: url)+ let entryID = UUID()+ var capture = PronunciationEnrollmentCapture(values: [1, 0], sourceFrameCount: 1, modelKey: "parakeet-v2")+ capture.pendingInspection = self.inspectionFixture()+ try await store.upsert(dictionaryEntryID: entryID, label: "word", modelKey: capture.modelKey, enrollments: [capture])+ let initial = await store.allProfiles()+ let id = try XCTUnwrap(initial.first?.enrollments.first?.inspectionID)+ XCTAssertNil(initial.first?.enrollments.first?.pendingInspection)+ XCTAssertNil(initial.first?.enrollments.first?.originalAudioID, "Diagnostics must not change original-audio matching eligibility")+ XCTAssertFalse(try XCTUnwrap(initial.first).isEligibleForMatching)+ XCTAssertNil(initial.first?.matchThreshold)+ let reloaded = PronunciationDictionaryStore(fileURL: url)+ let evidence = try await reloaded.inspection(for: id)+ XCTAssertEqual(evidence, capture.pendingInspection)+ let metadata = try String(contentsOf: url, encoding: .utf8)+ XCTAssertFalse(metadata.contains("samples"), "Do not inflate production profile reads with PCM or encoder matrices")+ for _ in 0..<10 {+ try await store.upsert(dictionaryEntryID: entryID, label: "word", modelKey: capture.modelKey, enrollments: [capture])+ }+ let kept = await store.allProfiles()+ XCTAssertEqual(kept.first?.enrollments.count, 10)+ do { _ = try await store.inspection(for: id); XCTFail("Evicted enrollment evidence must be removed") } catch {}+ let latestID = try XCTUnwrap(kept.first?.enrollments.last?.inspectionID)+ try await store.delete(dictionaryEntryID: entryID)+ do { _ = try await store.inspection(for: latestID); XCTFail("Deleting a word must remove diagnostic audio") } catch {}+ }++ func testInvalidInspectionDoesNotWriteOrChangeProfiles() async throws {+ let directory = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)+ defer { try? FileManager.default.removeItem(at: directory) }+ let store = PronunciationDictionaryStore(fileURL: directory.appendingPathComponent("profiles.json"))+ let entryID = UUID()+ let legacy = PronunciationEnrollmentCapture(values: [1, 0], sourceFrameCount: 1, modelKey: "parakeet-v2")+ try await store.upsert(dictionaryEntryID: entryID, label: "word", modelKey: legacy.modelKey, enrollments: [legacy])+ let before = await store.allProfiles()+ var invalid = legacy+ var evidence = self.inspectionFixture()+ evidence.recordedSampleCount = Int.max+ invalid.pendingInspection = evidence+ do {+ try await store.upsert(dictionaryEntryID: entryID, label: "word", modelKey: legacy.modelKey, enrollments: [invalid])+ XCTFail("Invalid evidence should fail before activation")+ } catch {}+ let after = await store.allProfiles()+ XCTAssertEqual(after, before)+ XCTAssertNil(after.first?.enrollments.first?.inspectionID)+ }+}++extension CustomDictionaryManualEntryTests {+ func testAudioInspectorRendersWithoutChangingProductionState() async throws {+ var recordingRequests = 0+ let before = SettingsStore.shared.customDictionaryEntries+ let audio = self.inspectionFixture()+ let capture = PronunciationEnrollmentCapture(values: [1, 0], sourceFrameCount: 1, modelKey: "parakeet-v2", inspectionID: UUID())+ let profile = PronunciationDictionaryProfile(dictionaryEntryID: UUID(), label: "Manimekalai", modelKey: "parakeet-v2", hiddenSize: 2, enrollments: [capture, capture, capture])+ let candidate = DictionaryMatchReport.Candidate(id: "sample", word: profile.label, sampleNumber: 1, enrollmentCount: 1, eligible: false, score: 0.6, start: 0, end: 0.08)+ let report = DictionaryMatchReport(+ createdAt: Date(),+ recordingID: UUID(),+ audioPath: "",+ targetWord: profile.label,+ modelKey: profile.modelKey,+ duration: audio.duration,+ rawTranscript: "word",+ savedTranscript: "word",+ availableProfileCount: 1,+ candidates: [candidate],+ targetSamples: [candidate],+ profiles: [profile],+ inspection: audio+ )+ for width in [420.0, 640.0] {+ for scheme in [ColorScheme.light, .dark] {+ let view = DictionaryAudioInspectionView(report: report, recordingBusy: false, loadReference: { _ in audio }, onRecordMore: { recordingRequests += 1 })+ .padding(20).frame(width: width)+ .background(scheme == .dark ? Color(white: 0.12) : Color(white: 0.96))+ .appTheme(.adaptive(accent: FluidBrandColors.blue, colorScheme: scheme))+ .environment(\.colorScheme, scheme)+ let host = NSHostingView(rootView: view)+ let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: width, height: 1250), styleMask: [.titled], backing: .buffered, defer: false)+ window.contentView = host+ window.appearance = NSAppearance(named: scheme == .dark ? .darkAqua : .aqua)+ window.orderFrontRegardless()+ try await Task.sleep(for: .milliseconds(450))+ host.layoutSubtreeIfNeeded()+ host.displayIfNeeded()+ let bitmap = try XCTUnwrap(host.bitmapImageRepForCachingDisplay(in: host.bounds))+ host.cacheDisplay(in: host.bounds, to: bitmap)+ let png = try XCTUnwrap(bitmap.representation(using: .png, properties: [:]))+ try png.write(to: URL(fileURLWithPath: "/tmp/dictionary-inspector-\(Int(width))-\(scheme).png"))+ window.orderOut(nil)+ }+ }+ XCTAssertEqual(SettingsStore.shared.customDictionaryEntries, before)+ XCTAssertEqual(profile.matchThreshold, nil)+ XCTAssertEqual(recordingRequests, 0, "Loading reference audio must not start training")+ }+}++extension CustomDictionaryManualEntryTests {+ func testTrainingCaptureRetainsAudioWithoutChangingTheEmbedding() async throws {+ #if arch(arm64)+ guard let path = ProcessInfo.processInfo.environment["FV_INSPECTION_AUDIO"] else {+ throw XCTSkip("Opt-in local replay requires FV_INSPECTION_AUDIO and cached Parakeet v2")+ }+ let key = "DictionaryPronunciationDebugCapture"+ let previous = UserDefaults.standard.object(forKey: key)+ defer {+ if let previous { UserDefaults.standard.set(previous, forKey: key) } else { UserDefaults.standard.removeObject(forKey: key) }+ }+ let provider = FluidAudioProvider(modelOverride: .parakeetTDTv2, configureWordBoosting: false)+ try await provider.prepare()+ let samples = try AudioConverter().resampleAudioFile(path: path)+ UserDefaults.standard.set(true, forKey: key)+ let result = try await provider.transcribeDictionaryTraining(samples, capturePronunciation: true)+ let capture = try XCTUnwrap(result.pronunciationEnrollment)+ let evidence = try XCTUnwrap(capture.pendingInspection)+ XCTAssertTrue(evidence.isValid)+ XCTAssertEqual(evidence.samples, samples)+ XCTAssertEqual(evidence.frames.first?.hiddenSize, 1024)+ let vector = try XCTUnwrap(evidence.embedding(start: evidence.selectedStart, end: evidence.selectedEnd))+ XCTAssertEqual(try XCTUnwrap(DictionaryAudioInspection.similarity(vector, capture.values)), 1, accuracy: 0.00_001)+ UserDefaults.standard.set(false, forKey: key)+ let disabled = try await provider.transcribeDictionaryTraining(samples, capturePronunciation: true)+ let disabledCapture = try XCTUnwrap(disabled.pronunciationEnrollment)+ XCTAssertNil(disabledCapture.pendingInspection)+ XCTAssertEqual(try XCTUnwrap(DictionaryAudioInspection.similarity(disabledCapture.values, capture.values)), 1, accuracy: 0.00_001)+ XCTAssertEqual(disabled.text, result.text)+ #else+ throw XCTSkip("Parakeet encoder replay requires Apple Silicon")+ #endif+ }+}++extension CustomDictionaryManualEntryTests {+ func testOneCutComparesEveryEnrollmentWithoutChangingTheCut() async throws {+ let audio = self.inspectionFixture()+ let cut = DictionaryAudioCut(start: 0, end: 0.08)+ let originals: [[Float]] = [[1, 0], [0, 1], [1, 1]]+ let scores = await DictionaryAudioInspector.shared.scores(test: audio, testCut: cut, references: [], referenceCuts: [], storedVectors: originals)+ XCTAssertEqual(scores.count, 3)+ XCTAssertEqual(scores[0], 1)+ XCTAssertEqual(scores[1], 0)+ XCTAssertEqual(try XCTUnwrap(scores[2]), 1 / sqrt(2), accuracy: 0.00_001)+ let moved = await DictionaryAudioInspector.shared.scores(test: audio, testCut: .init(start: 0.08, end: 0.16), references: [], referenceCuts: [], storedVectors: originals)+ XCTAssertEqual(moved[0], 0)+ XCTAssertEqual(moved[1], 1)+ XCTAssertEqual(moved[2], scores[2])+ let adjustedReference = await DictionaryAudioInspector.shared.scores(+ test: audio,+ testCut: cut,+ references: [nil, audio, nil],+ referenceCuts: [nil, cut, nil],+ storedVectors: originals+ )+ XCTAssertEqual(adjustedReference[0], scores[0])+ XCTAssertEqual(adjustedReference[1], 1)+ XCTAssertEqual(adjustedReference[2], scores[2])+ XCTAssertEqual(cut, DictionaryAudioCut(start: 0, end: 0.08), "Loading or adjusting a reference cannot reset the user's test cut")+ XCTAssertEqual(originals, [[1, 0], [0, 1], [1, 1]])+ let invalid = await DictionaryAudioInspector.shared.scores(test: audio, testCut: .init(start: 0.08, end: 0.08), references: [], referenceCuts: [], storedVectors: originals)+ XCTAssertEqual(invalid.count, 3)+ XCTAssertTrue(invalid.allSatisfy { $0 == nil })+ }++ func testAddingContextCanRaiseSimilarityWithoutImprovingTheWordCut() async throws {+ let audio = self.inspectionFixture()+ let clean = await DictionaryAudioInspector.shared.scores(test: audio, testCut: .init(start: 0, end: 0.08), references: [], referenceCuts: [], storedVectors: [[1, 1]])+ let withContext = await DictionaryAudioInspector.shared.scores(test: audio, testCut: .init(start: 0, end: 0.16), references: [], referenceCuts: [], storedVectors: [[1, 1]])+ XCTAssertGreaterThan(try XCTUnwrap(withContext[0]), try XCTUnwrap(clean[0]), "Mean-pooled similarity is not a measure of word-boundary correctness")+ }+}++extension CustomDictionaryManualEntryTests {+ func testEdgeTrimmingPreservesQuietInsideAndOriginalSamples() throws {+ let samples = [Float](repeating: 0, count: 16_000) + [Float](repeating: 0.1, count: 3200)+ + [Float](repeating: 0, count: 3200) + [Float](repeating: 0.1, count: 3200)+ + [Float](repeating: 0, count: 16_000)+ let range = try XCTUnwrap(DictionaryPronunciationExperiment.trimmedRange(samples))+ XCTAssertEqual(range, 14_080..<27_520)+ XCTAssertEqual(Array(samples[19_200..<22_400]), [Float](repeating: 0, count: 3200))+ XCTAssertNil(DictionaryPronunciationExperiment.trimmedRange([Float](repeating: 0, count: 3200)))+ XCTAssertNil(DictionaryPronunciationExperiment.trimmedRange(samples, within: -1..<10))+ XCTAssertEqual(samples.count, 41_600)+ }++ func testRelativeCalibrationUsesPairwiseAverageAndOneNormalizedCenter() throws {+ let vectors: [[Float]] = [[1, 0], [0.8, 0.6], [0.8, -0.6]]+ let result = try XCTUnwrap(DictionaryPronunciationExperiment.calibration(vectors))+ XCTAssertEqual(result.baseline, (0.8 + 0.8 + 0.28) / 3, accuracy: 0.00_001)+ XCTAssertEqual(result.center[0], 1, accuracy: 0.00_001)+ XCTAssertEqual(result.center[1], 0, accuracy: 0.00_001)+ XCTAssertEqual(Float(0.63) / Float(0.9), 0.7, accuracy: 0.00_001)+ XCTAssertNil(DictionaryPronunciationExperiment.calibration([[0, 0], [1, 0], [1, 0]]))+ XCTAssertNil(DictionaryPronunciationExperiment.calibration([[1], [1, 0], [1, 0]]))+ XCTAssertNil(DictionaryPronunciationExperiment.calibration([[1, 0], [1, 0]]))+ }++ func testEdgeCaptureFieldsPersistWithoutDiagnosticAudio() throws {+ var capture = PronunciationEnrollmentCapture(values: [1, 0], sourceFrameCount: 20, modelKey: "test")+ capture.edgeEmbedding = [0.8, 0.6]+ capture.edgeFrameCount = 10+ let decoded = try JSONDecoder().decode(PronunciationEnrollmentCapture.self, from: JSONEncoder().encode(capture))+ XCTAssertEqual(decoded.edgeEmbedding, capture.edgeEmbedding)+ XCTAssertEqual(decoded.edgeFrameCount, 10)+ XCTAssertNil(decoded.pendingInspection)+ XCTAssertEqual(decoded.values, [1, 0])+ }+}++extension CustomDictionaryManualEntryTests {+ func testEdgeMatchingRealAudioReplay() async throws {+ #if arch(arm64)+ guard let directory = ProcessInfo.processInfo.environment["FV_EDGE_REPLAY_DIR"] else {+ throw XCTSkip("Local-only audio replay requires FV_EDGE_REPLAY_DIR")+ }+ struct Clip: Decodable { let id: String; let path: String; let kind: String }+ let clips = try JSONDecoder().decode([Clip].self, from: Data(contentsOf: URL(fileURLWithPath: directory).appendingPathComponent("clips.json")))+ let keys = ["DictionaryEdgeMatchingEnabled", "DictionaryPronunciationDebugCapture"]+ let previous = keys.map { UserDefaults.standard.object(forKey: $0) }+ defer { for (key, value) in zip(keys, previous) {+ if let value { UserDefaults.standard.set(value, forKey: key) } else { UserDefaults.standard.removeObject(forKey: key) }+ } }+ UserDefaults.standard.set(true, forKey: keys[0])+ UserDefaults.standard.set(false, forKey: keys[1])+ let folder = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)+ try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)+ defer { try? FileManager.default.removeItem(at: folder) }+ let store = PronunciationDictionaryStore(fileURL: folder.appendingPathComponent("profiles.json"))+ let entry = SettingsStore.CustomDictionaryEntry(triggers: ["manimekalai"], replacement: "Manimekalai")+ let provider = FluidAudioProvider(+ modelOverride: .parakeetTDTv2,+ configureWordBoosting: false,+ enhancementOptions: FluidAudioProviderEnhancementOptions(experimentalUnifiedFinalEnabled: false, pronunciationMatchingEnabled: true, customDictionaryEntries: [entry]),+ pronunciationStore: store+ )+ try await provider.prepare()+ var captures: [PronunciationEnrollmentCapture] = []+ for clip in clips where clip.kind == "reference" {+ let samples = try AudioConverter().resampleAudioFile(path: clip.path)+ let result = try await provider.transcribeDictionaryTraining(samples, capturePronunciation: true)+ let capture = try XCTUnwrap(result.pronunciationEnrollment)+ XCTAssertEqual(capture.edgeEmbedding?.count, 1024)+ XCTAssertNil(capture.pendingInspection)+ captures.append(capture)+ }+ try await store.upsert(dictionaryEntryID: entry.id, label: entry.replacement, modelKey: "parakeet-v2", enrollments: captures)+ let profiles = await store.profiles(modelKey: "parakeet-v2")+ XCTAssertEqual(DictionaryPronunciationReferences.make(profiles: profiles).count, 1)+ for clip in clips where clip.kind != "reference" {+ let result = try await provider.transcribeFinal(AudioConverter().resampleAudioFile(path: clip.path))+ print("EDGE_REPLAY \(clip.id) \(clip.kind): \(result.text)")+ if clip.kind == "negative" { XCTAssertFalse(result.text.lowercased().contains("manimekalai"), clip.id) } else {+ XCTAssertTrue(result.text.lowercased().contains("manimekalai"), clip.id)+ XCTAssertFalse(result.text.lowercased().contains("megala"), "Must replace the complete misheard name")+ }+ }+ UserDefaults.standard.set(false, forKey: keys[0])+ XCTAssertEqual(DictionaryPronunciationReferences.make(profiles: profiles).count, 3)+ #endif+ }+}++extension CustomDictionaryManualEntryTests {+ func testDictionaryDebugCaptureSwitchStopsNewWrites() async throws {+ let key = "DictionaryPronunciationDebugCapture"+ let previous = UserDefaults.standard.object(forKey: key)+ defer { if let previous { UserDefaults.standard.set(previous, forKey: key) } else { UserDefaults.standard.removeObject(forKey: key) } }+ let root = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)+ defer { try? FileManager.default.removeItem(at: root) }+ let archive = DictionaryPronunciationDebugArchive(directory: root)+ UserDefaults.standard.set(false, forKey: key)+ await archive.save(kind: "test", model: "test", samples: [0.1], transcript: "test", profiles: [])+ XCTAssertFalse(FileManager.default.fileExists(atPath: root.path))+ UserDefaults.standard.set(true, forKey: key)+ await archive.save(kind: "test", model: "test", samples: [0.1], transcript: "test", profiles: [])+ XCTAssertEqual(try FileManager.default.contentsOfDirectory(atPath: root.path).count, 1)+ UserDefaults.standard.set(false, forKey: key)+ await archive.save(kind: "test", model: "test", samples: [0.1], transcript: "test", profiles: [])+ XCTAssertEqual(try FileManager.default.contentsOfDirectory(atPath: root.path).count, 1)+ }+}diff --git a/Tests/FluidDictationIntegrationTests/DictationE2ETests.swift b/Tests/FluidDictationIntegrationTests/DictationE2ETests.swiftindex bc85541c..e814055b 100644--- a/Tests/FluidDictationIntegrationTests/DictationE2ETests.swift+++ b/Tests/FluidDictationIntegrationTests/DictationE2ETests.swift@@ -1,5 +1,7 @@+import AppKit@testable import FluidVoice_Debugimport Foundation+import SwiftUI#if arch(arm64)import FluidAudio#endif@@ -701,6 +703,91 @@ final class DictationE2ETests: XCTestCase {}extension DictationE2ETests {+ func testDictionaryPlaygroundRendersWithoutStartingCaptureOrChangingWords() async throws {+ let entries = SettingsStore.shared.customDictionaryEntries+ var actions = 0+ for width in [420.0, 640.0] {+ for scheme in [ColorScheme.dark, .light] {+ let view = DictionaryWordPlayground(word: "FluidVoice", onPracticeMore: { actions += 1 }, busy: .constant(false))+ .padding(24)+ .frame(width: width, height: 560, alignment: .top)+ .background(scheme == .dark ? Color(white: 0.12) : Color(white: 0.96))+ .appTheme(.adaptive(accent: FluidBrandColors.blue, colorScheme: scheme))+ .environment(\.colorScheme, scheme)+ .environmentObject(AppServices.shared)+ let host = NSHostingView(rootView: view)+ let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: width, height: 560), styleMask: [.borderless], backing: .buffered, defer: false)+ window.contentView = host+ window.appearance = NSAppearance(named: scheme == .dark ? .darkAqua : .aqua)+ window.orderFrontRegardless()+ try await Task.sleep(for: .milliseconds(200))+ host.layoutSubtreeIfNeeded()+ host.displayIfNeeded()+ let bitmap = try XCTUnwrap(host.bitmapImageRepForCachingDisplay(in: host.bounds))+ host.cacheDisplay(in: host.bounds, to: bitmap)+ let png = try XCTUnwrap(bitmap.representation(using: .png, properties: [:]))+ try png.write(to: URL(fileURLWithPath: "/tmp/dictionary-playground-\(Int(width))-\(scheme).png"))+ window.orderOut(nil)+ }+ }+ XCTAssertEqual(actions, 0)+ XCTAssertEqual(SettingsStore.shared.customDictionaryEntries, entries)+ }++ func testDictionaryRingAudioResponseIsBoundedAndGeometryStaysInsideItsFrame() {+ XCTAssertEqual(DictionaryRingResponse.energy(for: 0), 0)+ XCTAssertEqual(DictionaryRingResponse.energy(for: -1), 0)+ XCTAssertEqual(DictionaryRingResponse.energy(for: .nan), 0)+ XCTAssertEqual(DictionaryRingResponse.energy(for: .infinity), 0)+ XCTAssertEqual(DictionaryRingResponse.energy(for: 2), 1)+ XCTAssertGreaterThan(DictionaryRingResponse.energy(for: 0.7), DictionaryRingResponse.energy(for: 0.1))+ for size in [176.0, 240.0] {+ let frame = CGRect(x: 0, y: 0, width: size, height: size)+ for tick in 0..<32 {+ let phase = Double(tick) / 32 * .pi * 2+ for sheet in 0..<DictionaryRibbon.sheets {+ for strand in -4...4 {+ let quiet = DictionaryRibbon(phase: phase, strand: Double(strand), energy: 0, sheet: sheet).path(in: frame)+ let speech = DictionaryRibbon(phase: phase, strand: Double(strand), energy: 1, sheet: sheet).path(in: frame)+ XCTAssertNotEqual(quiet, speech)+ XCTAssertTrue(frame.contains(speech.boundingRect), "Speech motion must not clip")+ }+ }+ }+ }+ }++ func testDictionaryPlaygroundMatchesWholeWordsWithoutTreatingSimilarWordsAsSuccess() {+ XCTAssertTrue(DictionaryWordTestResult.containsWord("FluidVoice", in: "I use FluidVoice."))+ XCTAssertTrue(DictionaryWordTestResult.containsWord("ChatGPT", in: "Try chatgpt!"))+ XCTAssertTrue(DictionaryWordTestResult.containsWord("C++", in: "I write C++."))+ XCTAssertTrue(DictionaryWordTestResult.containsWord("New York", in: "Visit New York."))+ XCTAssertFalse(DictionaryWordTestResult.containsWord("cat", in: "The category changed."))+ XCTAssertFalse(DictionaryWordTestResult.containsWord("ChatGPT", in: "Chat GPT"))+ XCTAssertFalse(DictionaryWordTestResult.containsWord("", in: "Anything"))+ XCTAssertFalse(DictionaryWordTestResult.containsWord("FluidVoice", in: ""))+ }++ func testDictionaryPlaygroundDoesNotChangeStateWithoutDictionaryCapture() async {+ let asr = ASRService()+ asr.finalText = "Existing output"+ let entries = SettingsStore.shared.customDictionaryEntries+ let result = await asr.stop(forDictionaryTesting: true)+ XCTAssertEqual(result, "")+ XCTAssertEqual(asr.finalText, "Existing output")+ XCTAssertNil(asr.dictionaryCaptureToken)+ XCTAssertEqual(SettingsStore.shared.customDictionaryEntries, entries)+ XCTAssertFalse(asr.isRunning)+ }++ func testDictionaryTrainingRejectsOversizedResponsesButAllowsSplitWordsAndPhrases() {+ XCTAssertTrue(CustomDictionaryTrainingMerge.isOversizedResponse("The word I want you to learn is FluidVoice", intendedReplacement: "FluidVoice"))+ XCTAssertFalse(CustomDictionaryTrainingMerge.isOversizedResponse("you for bee", intendedReplacement: "U4B"))+ XCTAssertFalse(CustomDictionaryTrainingMerge.isOversizedResponse("fluid boys", intendedReplacement: "FluidVoice"))+ XCTAssertFalse(CustomDictionaryTrainingMerge.isOversizedResponse("University of California at Los Angeles", intendedReplacement: "University of California at Los Angeles"))+ XCTAssertFalse(CustomDictionaryTrainingMerge.isOversizedResponse("", intendedReplacement: "FluidVoice"))+ }+func testDictionaryTrainingNormalizesSamplesAndIgnoresIntendedText() {let triggers = CustomDictionaryTrainingMerge.normalizedTriggers(from: [" Fluid Voice. ", "FluidVoice", "fluid voice", " "],@@ -3096,14 +3183,17 @@ extension DictationE2ETests {}// Sample IDs verify copying/offsets only; this is not a speech recognition fixture.let samples = (0..<(120 * 16_000)).map { Float($0) }- let now = Date(timeIntervalSince1970: 1_000)+ let now = Date(timeIntervalSince1970: 1000)let recording = try XCTUnwrap(DictionaryLearningRecording(alignment: DictionaryLearningAlignment(modelKey: "parakeet-v3", words: words), samples: samples, now: now))let selection = (text as NSString).range(of: "flued voice", options: .backwards)let evidence = try DictionaryLearningAlignmentResolver.resolve(- recording: recording, deliveredTextBeforeEdit: text, selectedUTF16Range: selection,- observedText: "flued voice", now: now+ recording: recording,+ deliveredTextBeforeEdit: text,+ selectedUTF16Range: selection,+ observedText: "flued voice",+ now: now)XCTAssertEqual(evidence.recordingID, recording.id)XCTAssertEqual(evidence.sourceWordRange, 6..<8)@@ -3115,64 +3205,88 @@ extension DictationE2ETests {}func testDictionaryLearningPreservesFormattingAndRejectsAmbiguousRewrite() throws {- let now = Date(timeIntervalSince1970: 1_000)+ let now = Date(timeIntervalSince1970: 1000)func receipt(_ text: String) throws -> DictionaryLearningRecording {let words = text.split(separator: " ").enumerated().map { index, token inASRWordTiming(text: String(token), start: Double(index), end: Double(index) + 0.5)}return try XCTUnwrap(DictionaryLearningRecording(alignment: DictionaryLearningAlignment(modelKey: "parakeet-v3", words: words),- samples: [Float](repeating: 0, count: 16_000 * 15), now: now+ samples: [Float](repeating: 0, count: 16_000 * 15),+ now: now))}let formatted = "Open, FLUED VOICE now."let evidence = try DictionaryLearningAlignmentResolver.resolve(- recording: receipt("open flued voice now"), deliveredTextBeforeEdit: formatted,- selectedUTF16Range: (formatted as NSString).range(of: "FLUED VOICE"), observedText: "FLUED VOICE", now: now+ recording: receipt("open flued voice now"),+ deliveredTextBeforeEdit: formatted,+ selectedUTF16Range: (formatted as NSString).range(of: "FLUED VOICE"),+ observedText: "FLUED VOICE",+ now: now)XCTAssertEqual(evidence.sourceWordRange, 1..<3)let withoutFiller = "please open flued voice now thanks"let anchored = try DictionaryLearningAlignmentResolver.resolve(- recording: receipt("um please open flued voice now thanks"), deliveredTextBeforeEdit: withoutFiller,- selectedUTF16Range: (withoutFiller as NSString).range(of: "flued voice"), observedText: "flued voice", now: now+ recording: receipt("um please open flued voice now thanks"),+ deliveredTextBeforeEdit: withoutFiller,+ selectedUTF16Range: (withoutFiller as NSString).range(of: "flued voice"),+ observedText: "flued voice",+ now: now)XCTAssertEqual(anchored.sourceWordRange, 3..<5)let ambiguous = "please open flued voice now"XCTAssertThrowsError(try DictionaryLearningAlignmentResolver.resolve(- recording: receipt("well open flued voice now and open flued voice now"), deliveredTextBeforeEdit: ambiguous,- selectedUTF16Range: (ambiguous as NSString).range(of: "flued voice"), observedText: "flued voice", now: now+ recording: receipt("well open flued voice now and open flued voice now"),+ deliveredTextBeforeEdit: ambiguous,+ selectedUTF16Range: (ambiguous as NSString).range(of: "flued voice"),+ observedText: "flued voice",+ now: now)) { XCTAssertEqual($0 as? DictionaryLearningAlignmentError, .ambiguousSource) }let rewritten = "please launch flued voice today"XCTAssertThrowsError(try DictionaryLearningAlignmentResolver.resolve(- recording: receipt("open flued voice now"), deliveredTextBeforeEdit: rewritten,- selectedUTF16Range: (rewritten as NSString).range(of: "flued voice"), observedText: "flued voice", now: now+ recording: receipt("open flued voice now"),+ deliveredTextBeforeEdit: rewritten,+ selectedUTF16Range: (rewritten as NSString).range(of: "flued voice"),+ observedText: "flued voice",+ now: now)) { XCTAssertEqual($0 as? DictionaryLearningAlignmentError, .ambiguousSource) }}func testDictionaryLearningRejectsExpiredAndInvalidEvidence() throws {- let now = Date(timeIntervalSince1970: 1_000)+ let now = Date(timeIntervalSince1970: 1000)let words = [ASRWordTiming(text: "hello", start: 0.1, end: 0.5)]let recording = try XCTUnwrap(DictionaryLearningRecording(alignment: DictionaryLearningAlignment(modelKey: "parakeet-v3", words: words),- samples: [Float](repeating: 0, count: 16_000), now: now+ samples: [Float](repeating: 0, count: 16_000),+ now: now))XCTAssertThrowsError(try DictionaryLearningAlignmentResolver.resolve(- recording: recording, deliveredTextBeforeEdit: "hello", selectedUTF16Range: NSRange(location: 0, length: 5),- observedText: "hello", now: recording.expiresAt+ recording: recording,+ deliveredTextBeforeEdit: "hello",+ selectedUTF16Range: NSRange(location: 0, length: 5),+ observedText: "hello",+ now: recording.expiresAt)) { XCTAssertEqual($0 as? DictionaryLearningAlignmentError, .expired) }XCTAssertThrowsError(try DictionaryLearningAlignmentResolver.resolve(- recording: recording, deliveredTextBeforeEdit: "hello", selectedUTF16Range: NSRange(location: Int.max, length: 5),- observedText: "hello", now: now+ recording: recording,+ deliveredTextBeforeEdit: "hello",+ selectedUTF16Range: NSRange(location: Int.max, length: 5),+ observedText: "hello",+ now: now)) { XCTAssertEqual($0 as? DictionaryLearningAlignmentError, .invalidSelection) }let invalid = try XCTUnwrap(DictionaryLearningRecording(alignment: DictionaryLearningAlignment(modelKey: "parakeet-v3", words: [ASRWordTiming(text: "hello", start: .nan, end: 0.5)]),- samples: [Float](repeating: 0, count: 16_000), now: now+ samples: [Float](repeating: 0, count: 16_000),+ now: now))XCTAssertThrowsError(try DictionaryLearningAlignmentResolver.resolve(- recording: invalid, deliveredTextBeforeEdit: "hello", selectedUTF16Range: NSRange(location: 0, length: 5),- observedText: "hello", now: now+ recording: invalid,+ deliveredTextBeforeEdit: "hello",+ selectedUTF16Range: NSRange(location: 0, length: 5),+ observedText: "hello",+ now: now)) { XCTAssertEqual($0 as? DictionaryLearningAlignmentError, .invalidTiming) }}}@@ -3186,15 +3300,19 @@ extension DictationE2ETests {let entryID = UUID()let evidenceID = UUID()let evidence = DictionaryLearningAudioEvidence(- recordingID: UUID(), modelKey: "parakeet-v3", observedText: "flued voice",- sourceWordRange: 1..<3, sourceSampleRange: 0..<3200, focalSampleRange: 1280..<2560,+ recordingID: UUID(),+ modelKey: "parakeet-v3",+ observedText: "flued voice",+ sourceWordRange: 1..<3,+ sourceSampleRange: 0..<3200,+ focalSampleRange: 1280..<2560,samples: Array(repeating: 0, count: 3200))let capture = PronunciationEnrollmentCapture(values: [1, 0], sourceFrameCount: 1, modelKey: "parakeet-v3")let revision = await store.revision(for: entryID)var inserted: [Bool] = []for id in [evidenceID, evidenceID, UUID()] {- inserted.append(try await store.learnOriginalAudio(entryID: entryID, label: "FluidVoice", evidenceID: id, evidence: evidence, capture: capture, expectedRevision: revision))+ try inserted.append(await store.learnOriginalAudio(entryID: entryID, label: "FluidVoice", evidenceID: id, evidence: evidence, capture: capture, expectedRevision: revision))}XCTAssertEqual(inserted, [true, false, false], "Rollback must know whether this call actually inserted evidence")do {@@ -3286,12 +3404,12 @@ private actor DictionaryLearningTestExtractor {self.started = startedself.cancelled = cancelled}+func extract(_ evidence: DictionaryLearningAudioEvidence) async throws -> PronunciationEnrollmentCapture {self.calls += 1if self.calls == 1 {self.started.fulfill()- do { try await Task.sleep(for: .seconds(30)) }- catch { self.cancelled.fulfill(); throw error }+ do { try await Task.sleep(for: .seconds(30)) } catch { self.cancelled.fulfill(); throw error }}return PronunciationEnrollmentCapture(values: [1, 0], sourceFrameCount: 1, modelKey: evidence.modelKey)}@@ -3321,7 +3439,9 @@ extension DictationE2ETests {XCTAssertTrue(duringDictation.isEmpty)idle = trueservice.activityDidEnd()- for _ in 0..<200 where service.pendingCount > 0 { try await Task.sleep(for: .milliseconds(10)) }+ for _ in 0..<200 where service.pendingCount > 0 {+ try await Task.sleep(for: .milliseconds(10))+ }XCTAssertEqual(service.pendingCount, 0)let profiles = await store.allProfiles()XCTAssertEqual(profiles.first?.enrollments.count, 1)@@ -3335,11 +3455,16 @@ extension DictationE2ETests {let store = PronunciationDictionaryStore(fileURL: directory.appendingPathComponent("profiles.json"))let entry = SettingsStore.CustomDictionaryEntry(triggers: ["flued"], replacement: "Fluid")let evidence = DictionaryLearningAudioEvidence(recordingID: UUID(), modelKey: "parakeet-v3", observedText: "flued", sourceWordRange: 0..<1, sourceSampleRange: 0..<1, focalSampleRange: 0..<1, samples: [0])- let service = DictionaryAudioLearningService(store: store, lifetime: 0.05, extract: { _ in+ let service = DictionaryAudioLearningService(store: store,+ lifetime: 0.05,+ extract: { _ inXCTFail("Busy dictation must not run the background encoder")throw CancellationError()- }, canProcess: { false }, isCurrent: { _ in true })- for _ in 0..<6 { service.learn(entry: entry, evidenceID: UUID(), evidence: evidence) }+ }, canProcess: { false },+ isCurrent: { _ in true })+ for _ in 0..<6 {+ service.learn(entry: entry, evidenceID: UUID(), evidence: evidence)+ }XCTAssertEqual(service.pendingCount, 4)try await Task.sleep(for: .milliseconds(100))XCTAssertEqual(service.pendingCount, 0)@@ -3357,7 +3482,7 @@ extension DictationE2ETests {else { throw XCTSkip("Set the public real-speech fixture and local Parakeet model path") }let directory = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)defer { try? FileManager.default.removeItem(at: directory) }- let samples = Array(try AudioConverter().resampleAudioFile(path: path).prefix(160_000))+ let samples = try Array(AudioConverter().resampleAudioFile(path: path).prefix(160_000))let models = try await AsrModels.load(from: URL(fileURLWithPath: modelPath), version: .v3)let manager = AsrManager(config: ASRConfig(tdtConfig: TdtConfig(blankId: AsrModelVersion.v3.blankId), encoderHiddenSize: 1024))try await manager.initialize(models: models)@@ -3373,9 +3498,11 @@ extension DictationE2ETests {let service = DictionaryAudioLearningService(store: store, canProcess: { true }, isCurrent: { $0 == entry })let started = ProcessInfo.processInfo.systemUptimeservice.learn(entry: entry, evidenceID: UUID(), evidence: evidence)- for _ in 0..<1000 where service.pendingCount > 0 { try await Task.sleep(for: .milliseconds(10)) }+ for _ in 0..<1000 where service.pendingCount > 0 {+ try await Task.sleep(for: .milliseconds(10))+ }XCTAssertEqual(service.pendingCount, 0)- let backgroundWallMs = (ProcessInfo.processInfo.systemUptime-started)*1000+ let backgroundWallMs = (ProcessInfo.processInfo.systemUptime - started) * 1000let profiles = await store.allProfiles()let profile = try XCTUnwrap(profiles.first)let capture = try XCTUnwrap(profile.enrollments.first)@@ -3393,7 +3520,8 @@ extension DictationE2ETests {let output = FluidAudioProvider.applyPronunciationMatches(result: replay, matches: matches, profiles: profiles, labels: [entry.id: entry.replacement])XCTAssertTrue(output.contains("LearnedTarget"), "The saved original example must be usable by the production matcher")let provider = FluidAudioProvider(- modelOverride: .parakeetTDT, configureWordBoosting: false,+ modelOverride: .parakeetTDT,+ configureWordBoosting: false,enhancementOptions: FluidAudioProviderEnhancementOptions(experimentalUnifiedFinalEnabled: false, pronunciationMatchingEnabled: false, customDictionaryEntries: []),pronunciationStore: store)@@ -3401,7 +3529,7 @@ extension DictationE2ETests {let reuseStart = ProcessInfo.processInfo.systemUptimelet reused = try await provider.originalAudioEnrollment(evidence)XCTAssertEqual(reused?.values, capture.values, "Warm encoder reuse must preserve the exact embedding")- print("ORIGINAL_AUDIO_ENCODER_REUSE ms=\((ProcessInfo.processInfo.systemUptime-reuseStart)*1000)")+ print("ORIGINAL_AUDIO_ENCODER_REUSE ms=\((ProcessInfo.processInfo.systemUptime - reuseStart) * 1000)")let afterReuse = try await provider.transcribeFinal(evidence.samples)XCTAssertEqual(afterReuse.text, replay.text, "Background enrollment must not change later transcription")print("ORIGINAL_AUDIO_ROUNDTRIP backgroundWallMs=\(backgroundWallMs) samples=\(evidence.samples.count)")@@ -3438,23 +3566,23 @@ extension DictationE2ETests {func testDictionaryLearningReconciliationCostAtScale() {let wordCount = 1200let result = ASRResult(text: Array(repeating: "spoken", count: wordCount).joined(separator: " "), confidence: 1, duration: 600, processingTime: 0, tokenTimings: (0..<wordCount).map { index in- TokenTiming(token: "▁spoken", tokenId: 1, startTime: Double(index)*0.48, endTime: Double(index+1)*0.48, confidence: 1)+ TokenTiming(token: "▁spoken", tokenId: 1, startTime: Double(index) * 0.48, endTime: Double(index + 1) * 0.48, confidence: 1)})for count in [1, 10, 1000] {let profiles = (0..<count).map { index inPronunciationDictionaryProfile(dictionaryEntryID: UUID(), label: "Target\(index)", modelKey: "parakeet-v3", hiddenSize: 2, enrollments: [PronunciationEnrollmentCapture(values: [1, 0], sourceFrameCount: 6, modelKey: "parakeet-v3", originalAudioID: UUID(), observedText: "spoken")])}let labels = Dictionary(uniqueKeysWithValues: profiles.map { ($0.dictionaryEntryID, $0.label) })- let matches = (0..<count).map { PronunciationWindowMatch(prototypeIndex: $0, score: 0.95, frameRange: ($0*6)..<(($0+1)*6)) }+ let matches = (0..<count).map { PronunciationWindowMatch(prototypeIndex: $0, score: 0.95, frameRange: ($0 * 6)..<(($0 + 1) * 6)) }var durations: [Double] = []for run in 0..<6 {let start = ProcessInfo.processInfo.systemUptimelet output = FluidAudioProvider.applyPronunciationMatches(result: result, matches: matches, profiles: profiles, labels: labels)- let elapsed = (ProcessInfo.processInfo.systemUptime-start)*1000+ let elapsed = (ProcessInfo.processInfo.systemUptime - start) * 1000XCTAssertTrue(output.contains("Target0"))if run > 0 { durations.append(elapsed) }}- print("LEARNING_RECONCILIATION profiles=\(count) audioSeconds=600 meanMs=\(durations.reduce(0,+)/Double(durations.count)) maxMs=\(durations.max()!)")+ print("LEARNING_RECONCILIATION profiles=\(count) audioSeconds=600 meanMs=\(durations.reduce(0, +) / Double(durations.count)) maxMs=\(durations.max() ?? 0)")}}#endif@@ -3556,15 +3684,14 @@ extension DictationE2ETests {}let previousEntries = defaults.object(forKey: self.customDictionaryEntriesKey)defer {- if let previousEntries { defaults.set(previousEntries, forKey: self.customDictionaryEntriesKey) }- else { defaults.removeObject(forKey: self.customDictionaryEntriesKey) }+ if let previousEntries { defaults.set(previousEntries, forKey: self.customDictionaryEntriesKey) } else { defaults.removeObject(forKey: self.customDictionaryEntriesKey) }ASRService.invalidateDictionaryCache()}let folder = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)defer { try? FileManager.default.removeItem(at: folder) }let store = PronunciationDictionaryStore(fileURL: folder.appendingPathComponent("profiles.json"))let fullSamples = try AudioConverter().resampleAudioFile(path: path)- let samples = Array(fullSamples.prefix(160000))+ let samples = Array(fullSamples.prefix(160_000))let models = try await AsrModels.load(from: URL(fileURLWithPath: modelPath), version: .v3)let manager = AsrManager(config: ASRConfig(tdtConfig: TdtConfig(blankId: AsrModelVersion.v3.blankId), encoderHiddenSize: 1024))try await manager.initialize(models: models)@@ -3578,7 +3705,7 @@ extension DictationE2ETests {let edited = (delivered as NSString).replacingCharacters(in: oldRange, with: intended)var candidate = try XCTUnwrap(AutomaticDictionaryCorrectionDetector.candidate(before: delivered, after: edited, insertedRange: NSRange(location: 0, length: (delivered as NSString).length), allowsInsertionAtEnd: true))let recording = try XCTUnwrap(DictionaryLearningRecording(alignment: .init(modelKey: "parakeet-v3", words: words.map { .init(text: $0.text, start: $0.startTime, end: $0.endTime) }), samples: samples))- candidate.audioEvidence = try DictionaryLearningAlignmentResolver.resolve(recording: recording, deliveredTextBeforeEdit: delivered, selectedUTF16Range: try XCTUnwrap(candidate.sourceUTF16Range), observedText: candidate.heardText)+ candidate.audioEvidence = try DictionaryLearningAlignmentResolver.resolve(recording: recording, deliveredTextBeforeEdit: delivered, selectedUTF16Range: XCTUnwrap(candidate.sourceUTF16Range), observedText: candidate.heardText)let worker = DictionaryAudioLearningService(store: store, canProcess: { true })let session = AutomaticDictionaryTrainingSession(candidate: candidate, asr: AppServices.shared.asr, audioLearning: worker)let before = await store.allProfiles()@@ -3591,7 +3718,9 @@ extension DictationE2ETests {XCTAssertEqual(session.screen, .success)let entry = try XCTUnwrap(SettingsStore.shared.customDictionaryEntries.first { $0.replacement == intended })XCTAssertTrue(entry.triggers.contains(candidate.heardText.lowercased()))- for _ in 0..<1000 where worker.pendingCount > 0 { try await Task.sleep(for: .milliseconds(10)) }+ for _ in 0..<1000 where worker.pendingCount > 0 {+ try await Task.sleep(for: .milliseconds(10))+ }XCTAssertEqual(worker.pendingCount, 0)let learned = await store.allProfiles()XCTAssertEqual(learned.count, 1)@@ -3607,7 +3736,7 @@ extension DictationE2ETests {let aligned = try await provider.transcribeWithWordTimings(samples)XCTAssertTrue(aligned.result.text.contains(intended))provider.resetStreamingPreviewCache()- _ = try await provider.transcribeStreaming(Array(fullSamples.prefix(320000)))+ _ = try await provider.transcribeStreaming(Array(fullSamples.prefix(320_000)))_ = try await provider.transcribeStreaming(fullSamples)let longResult = try await provider.transcribeFinal(fullSamples)XCTAssertTrue(longResult.text.contains(intended), "Automatic evidence also participates in incremental long dictation")@@ -3615,7 +3744,21 @@ extension DictationE2ETests {provider.resetStreamingPreviewCache()let disabled = try await provider.transcribeFinal(samples)XCTAssertFalse(disabled.text.contains(intended), "Disabling both modes must preserve baseline ASR")+ }+ func testWizardPronunciationOptInRequiresThreeSamplesAndPreservesLegacyPolicy() throws {+ let entry = SettingsStore.CustomDictionaryEntry(triggers: ["fluid"], replacement: "Fluid Voice")+ let capture = PronunciationEnrollmentCapture(values: [1, 0], sourceFrameCount: 6, modelKey: "parakeet-v3")+ var profile = PronunciationDictionaryProfile(dictionaryEntryID: entry.id, label: entry.replacement, modelKey: capture.modelKey, hiddenSize: 2, enrollments: [capture, capture])+ profile.automaticMatchingEnabled = true+ XCTAssertTrue(FluidAudioProvider.matchingProfiles([profile], entries: [entry], includeManual: false, includeOriginal: false).isEmpty)+ profile.enrollments.append(capture)+ XCTAssertEqual(FluidAudioProvider.matchingProfiles([profile], entries: [entry], includeManual: false, includeOriginal: false).count, 1)+ let restored = try JSONDecoder().decode(PronunciationDictionaryProfile.self, from: JSONEncoder().encode(profile))+ XCTAssertEqual(restored.automaticMatchingEnabled, true)+ XCTAssertTrue(FluidAudioProvider.matchingProfiles([profile], entries: [], includeManual: false).isEmpty)+ profile.automaticMatchingEnabled = nil+ XCTAssertTrue(FluidAudioProvider.matchingProfiles([profile], entries: [entry], includeManual: false, includeOriginal: false).isEmpty)}func testDictionaryAutomaticProfileSelectionPreservesMeaningAndManualOptIn() {@@ -3634,3 +3777,118 @@ extension DictationE2ETests {}#endif}++extension DictationE2ETests {+ func testDictionaryWalkthroughStatesRenderWithoutCaptureOrPersistence() async throws {+ let entries = SettingsStore.shared.customDictionaryEntries+ var actions = 0+ for (index, state) in [(0, false, false), (1, true, false), (2, false, true), (3, false, false)].enumerated() {+ for width in [520.0, 900.0] {+ let view = DictionaryWordWizard(+ word: .constant("FluidVoice"),+ step: state.0 == 3 ? .review : .recording,+ count: state.0,+ heard: "",+ variants: [],+ busy: state.1 || state.2,+ recording: state.1,+ processing: state.2,+ starting: false,+ error: index == 0 ? "Couldn’t capture a voice profile. Try again." : nil,+ voiceSupported: true,+ alreadyCorrect: false,+ savedWord: "",+ onContinue: { actions += 1 },+ onRecord: { actions += 1 },+ onSave: { actions += 1 },+ onBack: { actions += 1 },+ onNewWord: { actions += 1 },+ onManual: { actions += 1 },+ onPracticeMore: { actions += 1 },+ onRedo: { actions += 1 }+ )+ .padding(24)+ .frame(width: width, height: 950, alignment: .top)+ .appTheme(.adaptive(accent: FluidBrandColors.blue, colorScheme: .light))+ .environmentObject(AppServices.shared)+ let host = NSHostingView(rootView: view)+ let window = NSWindow(contentRect: NSRect(x: 0, y: 0, width: width, height: 950), styleMask: [.borderless], backing: .buffered, defer: false)+ window.contentView = host+ window.appearance = NSAppearance(named: .aqua)+ window.orderFrontRegardless()+ try await Task.sleep(for: .milliseconds(200))+ host.layoutSubtreeIfNeeded()+ host.displayIfNeeded()+ let bitmap = try XCTUnwrap(host.bitmapImageRepForCachingDisplay(in: host.bounds))+ host.cacheDisplay(in: host.bounds, to: bitmap)+ let png = try XCTUnwrap(bitmap.representation(using: .png, properties: [:]))+ try png.write(to: URL(fileURLWithPath: "/tmp/dictionary-walkthrough-\(index)-\(Int(width)).png"))+ window.orderOut(nil)+ }+ }+ XCTAssertEqual(actions, 0)+ XCTAssertEqual(SettingsStore.shared.customDictionaryEntries, entries)+ }+}++#if arch(arm64)+extension DictationE2ETests {+ func testWordMatchLevelChangesReplacementWithoutWeakeningOtherWords() {+ let id = UUID(), otherID = UUID()+ let sample = PronunciationEnrollmentCapture(values: [1, 0], sourceFrameCount: 6, modelKey: "parakeet-v3")+ var profile = PronunciationDictionaryProfile(dictionaryEntryID: id, label: "Target", modelKey: "parakeet-v3", hiddenSize: 2, enrollments: [sample, sample, sample])+ let result = ASRResult(text: "misheard", confidence: 0.8, duration: 0.48, processingTime: 0.1, tokenTimings: [TokenTiming(token: "misheard", tokenId: 1, startTime: 0, endTime: 0.48, confidence: 0.8)])+ let match = PronunciationWindowMatch(prototypeIndex: 0, score: 0.49, frameRange: 0..<6)+ XCTAssertEqual(FluidAudioProvider.applyPronunciationMatches(result: result, matches: [match], profiles: [profile], labels: [id: "Target"]), "misheard")+ profile.matchThreshold = 0.45+ XCTAssertEqual(FluidAudioProvider.applyPronunciationMatches(result: result, matches: [match], profiles: [profile], labels: [id: "Target"]), "Target")+ profile.matchThreshold = 0.8+ XCTAssertEqual(FluidAudioProvider.applyPronunciationMatches(result: result, matches: [match], profiles: [profile], labels: [id: "Target"]), "misheard")+ let other = PronunciationDictionaryProfile(dictionaryEntryID: otherID, label: "Other", modelKey: "parakeet-v3", hiddenSize: 2, enrollments: [sample, sample, sample])+ profile.matchThreshold = 0.45+ let otherMatch = PronunciationWindowMatch(prototypeIndex: 1, score: 0.65, frameRange: 0..<6)+ XCTAssertEqual(FluidAudioProvider.applyPronunciationMatches(result: result, matches: [otherMatch], profiles: [profile, other], labels: [id: "Target", otherID: "Other"]), "misheard")+ }+}+#endif++#if arch(arm64)+extension DictationE2ETests {+ func testPronunciationMatchesIndividualRecordingsWithoutAveraging() throws {+ let id = UUID()+ let samples = [[Float(1), 0], [0, 1], [-1, 0]].map {+ PronunciationEnrollmentCapture(values: $0, sourceFrameCount: 6, modelKey: "parakeet-v3")+ }+ let profile = PronunciationDictionaryProfile(dictionaryEntryID: id, label: "Target", modelKey: "parakeet-v3", hiddenSize: 2, enrollments: samples)+ let references = DictionaryPronunciationReferences.make(profiles: [profile], hiddenSize: 2)+ XCTAssertEqual(references.count, 3)+ XCTAssertEqual(references.map(\.sampleNumber), [1, 2, 3])+ XCTAssertEqual(references.map { $0.embedding.values }, samples.map(\.values))+ XCTAssertEqual(references.map { $0.embedding.sourceFrameCount }, [6, 6, 6])+ XCTAssertTrue(DictionaryPronunciationReferences.make(profiles: [profile], hiddenSize: 4).isEmpty)+ let features = EncoderFeatureSequence(hiddenSize: 2, frameCount: 6, values: Array(repeating: [Float(1), 0], count: 6).flatMap { $0 })+ let matches = PronunciationEmbeddingMatcher.allMatches(prototypes: references.map(\.embedding), in: features, windowFrameCounts: [[6], [6], [6]])+ XCTAssertEqual(matches[0].first?.score, 1)+ XCTAssertTrue(matches[1].isEmpty && matches[2].isEmpty)+ let averaged = try XCTUnwrap(PronunciationEmbeddingMatcher.prototype(from: references.map(\.embedding)))+ XCTAssertTrue(PronunciationEmbeddingMatcher.allMatches(prototypes: [averaged], in: features, windowFrameCounts: [[6]])[0].isEmpty, "The old averaged center misses this valid individual example")+ let result = ASRResult(text: "misheard", confidence: 0.8, duration: 0.48, processingTime: 0.1, tokenTimings: [TokenTiming(token: "misheard", tokenId: 1, startTime: 0, endTime: 0.48, confidence: 0.8)])+ let hits = matches.enumerated().flatMap { index, hits in+ hits.map { PronunciationWindowMatch(prototypeIndex: index, score: $0.score, frameRange: $0.frameRange) }+ }+ XCTAssertEqual(FluidAudioProvider.applyPronunciationMatches(result: result, matches: hits, profiles: references.map(\.profile), labels: [id: "Target"]), "Target")+ let sameWord = hits + [PronunciationWindowMatch(prototypeIndex: 1, score: 0.99, frameRange: 0..<6)]+ XCTAssertEqual(FluidAudioProvider.applyPronunciationMatches(result: result, matches: sameWord, profiles: references.map(\.profile), labels: [id: "Target"]), "Target", "A second recording of the same word is not a competing word")+ let otherID = UUID()+ let competitor = PronunciationDictionaryProfile(dictionaryEntryID: otherID, label: "Other", modelKey: profile.modelKey, hiddenSize: 2, enrollments: samples)+ let competingHits = hits + [PronunciationWindowMatch(prototypeIndex: 3, score: 0.99, frameRange: 0..<6)]+ let competingOutput = FluidAudioProvider.applyPronunciationMatches(+ result: result,+ matches: competingHits,+ profiles: references.map(\.profile) + [competitor],+ labels: [id: "Target", otherID: "Other"]+ )+ XCTAssertEqual(competingOutput, "misheard", "Competing words still require separation")+ }+}+#endifdiff --git a/Tests/VoiceTrainingAliasFilterTests.swift b/Tests/VoiceTrainingAliasFilterTests.swiftnew file mode 100644index 00000000..89dfca09--- /dev/null+++ b/Tests/VoiceTrainingAliasFilterTests.swift@@ -0,0 +1,52 @@+import AppKit++@main+struct VoiceTrainingAliasFilterTests {+ @MainActor+ static func main() async {+ let ordinary = ["but", "NOW", "okay", "hello", "right now", "let’s do that"]+ let native = await VoiceTrainingAliasFilter.filter(ordinary + ["fluwidvois"])+ FileHandle.standardOutput.write(Data("native accepted=\(native.accepted) rejected=\(native.rejected) available=\(native.lookupAvailable)\n".utf8))+ precondition(native.lookupAvailable, "Native English word lookup must be available for this smoke test")+ precondition(native.accepted == ["fluwidvois"])+ precondition(native.rejected == ordinary)++ // Deterministic policy coverage, independent of the installed dictionary version.+ let aliases = ["now", "right now", "fluid zqxword", "🙂 zqxword", "123", "", String(repeating: "x", count: 161)]+ let filtered = await VoiceTrainingAliasFilter.filter(aliases) { text in+ guard let regex = try? NSRegularExpression(pattern: "zqxword") else { preconditionFailure("Invalid fixture pattern") }+ return .checked(regex.matches(in: text, range: NSRange(text.startIndex..., in: text)).map(\.range))+ }+ precondition(filtered.accepted == ["fluid zqxword", "🙂 zqxword"])+ precondition(filtered.rejected == ["now", "right now", "123", "", String(repeating: "x", count: 161)])++ let unavailable = await VoiceTrainingAliasFilter.filter(["now", "zqxword"]) { _ in .unavailable }+ precondition(!unavailable.lookupAvailable && unavailable.accepted.isEmpty)++ let cancelled = Task { @MainActor in+ await VoiceTrainingAliasFilter.filter(["zqxword"]) { text in+ try? await Task.sleep(for: .milliseconds(30))+ return .checked([NSRange(text.startIndex..., in: text)])+ }+ }+ cancelled.cancel()+ let cancelledResult = await cancelled.value+ precondition(cancelledResult.accepted.isEmpty)++ let results = await withTaskGroup(of: Bool.self) { group in+ for _ in 0..<3 {+ group.addTask {+ let result = await VoiceTrainingAliasFilter.filter(["now", "fluwidvois"])+ return result.accepted == ["fluwidvois"]+ }+ }+ var values: [Bool] = []+ for await result in group {+ values.append(result)+ }+ return values+ }+ precondition(results.count == 3 && results.allSatisfy { $0 })+ print("PASS: native vocabulary, common phrases, rare aliases, UTF-16 ranges, invalid input, unavailable lookup, cancellation, concurrent requests")+ }+}