Skip to content

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

perf(dictation): defer context reads until the mic stops

altic-devc32a110c12f2
diff --git a/Sources/Fluid/ContentView.swift b/Sources/Fluid/ContentView.swift
index 6920052c..e557f9cf 100644
--- a/Sources/Fluid/ContentView.swift
+++ b/Sources/Fluid/ContentView.swift
@@ -2242,16 +2242,39 @@ struct ContentView: View {
ownOverlayFocused: isOverlay
)
let app = target.flatMap { NSRunningApplication(processIdentifier: $0.pid) }
+ // Window title and preceding text need WindowServer and Accessibility
+ // round-trips; completeDictationStopSnapshot fills them after the
+ // microphone has stopped.
let info = (
name: app?.localizedName ?? "Unknown",
bundleId: target?.bundleIdentifier ?? "unknown",
- windowTitle: target.flatMap { self.getFrontmostWindowTitle(ownerPid: $0.pid) } ?? ""
+ windowTitle: ""
)
if !useOriginal { DictationAppSession.shared.activate(target?.bundleIdentifier) }
- let precedingText = useOriginal ? self.recordingPrecedingText
- : (self.settings.needsDictationFormattingContext ? TypingService.textBeforeCursorInFocusedField() : "")
- NotchContentState.shared.frozenDictationLabel = self.settings.dictationOverlayLabel(for: slot, appBundleID: info.bundleId)
- return .capture(target: target, appInfo: info, slot: slot, precedingText: precedingText)
+ let label = self.settings.dictationOverlayLabel(for: slot, appBundleID: info.bundleId)
+ if NotchContentState.shared.frozenDictationLabel != label {
+ NotchContentState.shared.frozenDictationLabel = label
+ }
+ return .capture(
+ target: target,
+ appInfo: info,
+ slot: slot,
+ precedingText: useOriginal ? self.recordingPrecedingText : "",
+ readsContextFromFocusedField: !useOriginal
+ )
+ }
+
+ private func completeDictationStopSnapshot(_ snapshot: inout DictationStopSnapshot) {
+ let windowTitle = snapshot.target.flatMap { self.getFrontmostWindowTitle(ownerPid: $0.pid) }
+ var precedingText: String?
+ if snapshot.readsContextFromFocusedField,
+ self.settings.needsDictationFormattingContext,
+ let target = snapshot.target,
+ TypingService.currentFocusedPID() == target.pid
+ {
+ precedingText = TypingService.textBeforeCursorInFocusedField()
+ }
+ snapshot.completeContext(windowTitle: windowTitle, precedingText: precedingText)
}
private func prepareStoppedDictationDelivery(_ text: String, keepBackup: Bool, snapshot: DictationStopSnapshot?, needsRestoration: Bool) async -> Bool {
@@ -2746,7 +2769,7 @@ struct ContentView: View {
let activeDictationSlot = self.currentDictationShortcutSlot(for: modeAtStop)
let promptOverride = self.promptModeOverrideText
let promptTest = DictationPromptTestCoordinator.shared
- let stopSnapshot = route == .normal && !wasRewriteMode && !wasCommandMode && !promptTest.isActive
+ var stopSnapshot = route == .normal && !wasRewriteMode && !wasCommandMode && !promptTest.isActive
? self.captureDictationStopSnapshot(slot: activeDictationSlot ?? .primary) : nil
let shouldUseAIOnStop = stopSnapshot?.usesAI ?? activeDictationSlot.map {
DictationAIPostProcessingGate.isConfigured(for: $0, appBundleID: self.recordingAppInfo?.bundleId)
@@ -2781,6 +2804,10 @@ struct ContentView: View {
onFinalTranscriptionStarted: stopOverlay.onFinalTranscriptionStarted
)
self.appBench("asr_stop_return elapsedMs=\(Int(((ProcessInfo.processInfo.systemUptime - asrStopStartedAt) * 1000).rounded()))")
+ if var snapshot = stopSnapshot {
+ self.completeDictationStopSnapshot(&snapshot)
+ stopSnapshot = snapshot
+ }
let audioSnapshot = self.asr.consumeLastCompletedAudioSnapshot()
let transcriptionDurationMilliseconds = self.asr.consumeLastFinalTranscriptionDurationMs()
DebugLogger.shared.info(
diff --git a/Sources/Fluid/Services/DictationStopSnapshot.swift b/Sources/Fluid/Services/DictationStopSnapshot.swift
index a35557fc..97e524e8 100644
--- a/Sources/Fluid/Services/DictationStopSnapshot.swift
+++ b/Sources/Fluid/Services/DictationStopSnapshot.swift
@@ -3,12 +3,22 @@ import AppKit
/// One immutable decision for a normal dictation, made before awaiting ASR finalization.
struct DictationStopSnapshot {
let target: TypingService.RecordingTargetContext?
- let appInfo: (name: String, bundleId: String, windowTitle: String)
+ private(set) var appInfo: (name: String, bundleId: String, windowTitle: String)
let route: DictationProviderRoute
let usesAI: Bool
let systemPrompt: String
let hasCustomPrompt: Bool
- let precedingText: String
+ private(set) var precedingText: String
+ /// True when the preceding text must be read from the field focused at
+ /// stop rather than reused from recording start.
+ let readsContextFromFocusedField: Bool
+
+ /// Fills the fields that need WindowServer or Accessibility round-trips.
+ /// Called after the microphone has stopped so they never delay it.
+ mutating func completeContext(windowTitle: String?, precedingText: String?) {
+ if let windowTitle { self.appInfo.windowTitle = windowTitle }
+ if let precedingText { self.precedingText = precedingText }
+ }
var focusTarget: TypingService.CapturedFocusTarget? {
guard let target, let element = target.element else { return nil }
@@ -29,7 +39,8 @@ struct DictationStopSnapshot {
target: TypingService.RecordingTargetContext?,
appInfo: (name: String, bundleId: String, windowTitle: String),
slot: SettingsStore.DictationShortcutSlot,
- precedingText: String
+ precedingText: String,
+ readsContextFromFocusedField: Bool = false
) -> Self {
let settings = SettingsStore.shared
let customPrompt = settings.resolvedDictationPromptProfile(for: slot, appBundleID: appInfo.bundleId)
@@ -41,7 +52,8 @@ struct DictationStopSnapshot {
usesAI: target != nil && DictationAIPostProcessingGate.isConfigured(for: slot, appBundleID: appInfo.bundleId),
systemPrompt: customPrompt ?? settings.effectiveDictationSystemPrompt(for: slot, appBundleID: appInfo.bundleId),
hasCustomPrompt: customPrompt != nil,
- precedingText: precedingText
+ precedingText: precedingText,
+ readsContextFromFocusedField: readsContextFromFocusedField
)
}
diff --git a/Tests/FluidDictationIntegrationTests/LLMClientRequestBodyTests.swift b/Tests/FluidDictationIntegrationTests/LLMClientRequestBodyTests.swift
index f0646fac..f4d15230 100644
--- a/Tests/FluidDictationIntegrationTests/LLMClientRequestBodyTests.swift
+++ b/Tests/FluidDictationIntegrationTests/LLMClientRequestBodyTests.swift
@@ -308,6 +308,21 @@ final class LLMClientRequestBodyTests: XCTestCase {
}
}
+ func testStopSnapshotDefersWindowTitleAndPrecedingText() {
+ let target = TypingService.RecordingTargetContext(id: UUID(), pid: 321, bundleIdentifier: "test.defer", window: nil, element: nil)
+ let info = (name: "Defer app", bundleId: "test.defer", windowTitle: "")
+ var snapshot = DictationStopSnapshot.capture(target: target, appInfo: info, slot: .primary, precedingText: "", readsContextFromFocusedField: true)
+ XCTAssertTrue(snapshot.readsContextFromFocusedField)
+ XCTAssertEqual(snapshot.appInfo.windowTitle, "")
+ snapshot.completeContext(windowTitle: "Draft", precedingText: "Hello ")
+ XCTAssertEqual(snapshot.appInfo.windowTitle, "Draft")
+ XCTAssertEqual(snapshot.precedingText, "Hello ")
+ // Missing reads keep the stop-time values instead of clearing them.
+ snapshot.completeContext(windowTitle: nil, precedingText: nil)
+ XCTAssertEqual(snapshot.appInfo.windowTitle, "Draft")
+ XCTAssertEqual(snapshot.precedingText, "Hello ")
+ }
+
func testMainWindowEndsAppVisitButOverlayDoesNot() {
let session = DictationAppSession.shared
let previousApp = session.appID