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

fix token-dense dictation fallback

altic-dev7209d07c899d
diff --git a/Sources/Fluid/Persistence/SettingsStore.swift b/Sources/Fluid/Persistence/SettingsStore.swift
index d1f1ff53..901acb8a 100644
--- a/Sources/Fluid/Persistence/SettingsStore.swift
+++ b/Sources/Fluid/Persistence/SettingsStore.swift
@@ -24,6 +24,8 @@ final class SettingsStore: ObservableObject {
static let privateAIDictationSystemOverheadTokens = 1280
static let privateAIDictationMinimumOutputTokens = 256
static let privateAIDictationRoundTripTokenCost = 2.75
+ private static let privateAIDenseSegmentByteThreshold = 12
+ private static let privateAIDenseBytesPerToken = 2
static let privateAIBackendPreferenceDefaultsKey = "FluidIntelligenceBackendPreference"
private static let forcedOnboardingResetIntroducedAt = Date(timeIntervalSince1970: 1_782_091_732)
private let defaults = UserDefaults.standard
@@ -66,8 +68,7 @@ final class SettingsStore: ObservableObject {
}
static func privateAIDictationTokenBudget(forInputText inputText: String, contextTokenLimit: Int) -> PrivateAIDictationTokenBudget {
- let wordCount = inputText.split { $0.isWhitespace || $0.isNewline }.count
- let estimatedInputTokens = max(1, Int((Double(wordCount) / 0.75).rounded(.up)))
+ let estimatedInputTokens = self.estimatedPrivateAIInputTokens(for: inputText)
let requestedOutputTokens = max(
Self.privateAIDictationMinimumOutputTokens,
Int((Double(estimatedInputTokens) * 1.15).rounded(.up)) + 64
@@ -81,6 +82,23 @@ final class SettingsStore: ObservableObject {
)
}
+ private static func estimatedPrivateAIInputTokens(for inputText: String) -> Int {
+ let segments = inputText.split { $0.isWhitespace || $0.isNewline }
+ let wordBasedEstimate = Int((Double(segments.count) / 0.75).rounded(.up))
+ // Keep the existing prose estimate, but charge long unbroken input by UTF-8 size so
+ // URLs, identifiers, and languages without whitespace cannot look like a single token.
+ let denseSegmentEstimate = segments.reduce(into: 0) { estimate, segment in
+ let byteCount = segment.utf8.count
+ if byteCount > Self.privateAIDenseSegmentByteThreshold {
+ estimate += (byteCount + Self.privateAIDenseBytesPerToken - 1)
+ / Self.privateAIDenseBytesPerToken
+ } else {
+ estimate += 1
+ }
+ }
+ return max(1, max(wordBasedEstimate, denseSegmentEstimate))
+ }
+
static func privateAIMaxOutputTokens(forInputText inputText: String, contextTokenLimit: Int) -> Int {
self.privateAIDictationTokenBudget(
forInputText: inputText,
diff --git a/Tests/FluidDictationIntegrationTests/DictationE2ETests.swift b/Tests/FluidDictationIntegrationTests/DictationE2ETests.swift
index 2a92595e..42127792 100644
--- a/Tests/FluidDictationIntegrationTests/DictationE2ETests.swift
+++ b/Tests/FluidDictationIntegrationTests/DictationE2ETests.swift
@@ -1833,6 +1833,8 @@ final class DictationE2ETests: XCTestCase {
func testPrivateAIDictationTokenBudgetFallsBackBeforeLongInputCanTruncate() {
let shortTranscript = Array(repeating: "word", count: 600).joined(separator: " ")
let longTranscript = Array(repeating: "word", count: 3_385).joined(separator: " ")
+ let tokenDenseIdentifier = String(repeating: "a", count: 6_000)
+ let unspacedCJKTranscript = String(repeating: "漢", count: 2_500)
XCTAssertTrue(SettingsStore.privateAIDictationTokenBudget(
forInputText: shortTranscript,
@@ -1842,6 +1844,14 @@ final class DictationE2ETests: XCTestCase {
forInputText: longTranscript,
contextTokenLimit: 4096
).hasSufficientHeadroom)
+ XCTAssertFalse(SettingsStore.privateAIDictationTokenBudget(
+ forInputText: tokenDenseIdentifier,
+ contextTokenLimit: 4096
+ ).hasSufficientHeadroom)
+ XCTAssertFalse(SettingsStore.privateAIDictationTokenBudget(
+ forInputText: unspacedCJKTranscript,
+ contextTokenLimit: 4096
+ ).hasSufficientHeadroom)
}
func testPrivateAIDictationRejectsLongInputBeforeCallingProvider() async {
@@ -1878,6 +1888,40 @@ final class DictationE2ETests: XCTestCase {
}
}
+ func testPrivateAIDictationRejectsTokenDenseInputBeforeCallingProvider() async {
+ let tokenDenseTranscript = String(repeating: "a", count: 6_000)
+ let runtime = PrivateAIIntegrationService.RuntimeConfiguration(
+ selectedProviderID: "private",
+ providerKey: "private",
+ baseURL: "",
+ model: "fluid-1",
+ apiKey: "",
+ localModelPath: nil,
+ usesStablePromptPrefixKVCache: true,
+ usesFluid1Boost: true,
+ contextTokenLimit: 4096
+ )
+ let context = PrivateAIIntegrationService.AppContext(
+ appName: "Notes",
+ bundleID: "com.apple.Notes",
+ windowTitle: "",
+ appVersion: nil
+ )
+
+ do {
+ _ = try await PrivateAIIntegrationService.shared.enhanceDictation(
+ tokenDenseTranscript,
+ runtime: runtime,
+ context: context
+ )
+ XCTFail("Expected token-dense dictation to fall back before provider generation")
+ } catch AIProcessingError.dictationExceedsAIContextWindow {
+ // Expected: ContentView catches this and types the complete raw transcript.
+ } catch {
+ XCTFail("Unexpected error: \(error)")
+ }
+ }
+
func testPrivateAIProviderLocalRuntimeOnlyHandlesPrivateModels() {
self.withRestoredDefaults(keys: [self.privateAILocalModelPathKey]) {
let tempURL = FileManager.default.temporaryDirectory