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 long Fluid-1 dictation fallback

altic-dev15b10ba8c548
diff --git a/Sources/Fluid/ContentView.swift b/Sources/Fluid/ContentView.swift
index bba0d679..5e448b93 100644
--- a/Sources/Fluid/ContentView.swift
+++ b/Sources/Fluid/ContentView.swift
@@ -20,6 +20,7 @@ enum AIProcessingError: LocalizedError {
case missingAPIKey(provider: String)
case missingModel(provider: String)
case emptyResponse
+ case dictationExceedsAIContextWindow
var errorDescription: String? {
switch self {
@@ -31,6 +32,8 @@ enum AIProcessingError: LocalizedError {
return "No model selected for \(provider)"
case .emptyResponse:
return "AI returned an empty response"
+ case .dictationExceedsAIContextWindow:
+ return "Dictation exceeded the AI context window"
}
}
@@ -39,7 +42,7 @@ enum AIProcessingError: LocalizedError {
switch self {
case .noVerifiedProvider, .missingAPIKey, .missingModel:
return true
- case .emptyResponse:
+ case .emptyResponse, .dictationExceedsAIContextWindow:
return false
}
}
diff --git a/Sources/Fluid/Persistence/SettingsStore.swift b/Sources/Fluid/Persistence/SettingsStore.swift
index 09fc30be..d1f1ff53 100644
--- a/Sources/Fluid/Persistence/SettingsStore.swift
+++ b/Sources/Fluid/Persistence/SettingsStore.swift
@@ -60,18 +60,32 @@ final class SettingsStore: ObservableObject {
return max(100, Int((inputTokens * 0.75 / 50).rounded(.up)) * 50)
}
- static func privateAIMaxOutputTokens(forInputText inputText: String, contextTokenLimit: Int) -> Int {
+ struct PrivateAIDictationTokenBudget: Equatable {
+ let maxOutputTokens: Int
+ let hasSufficientHeadroom: Bool
+ }
+
+ 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 requestedOutputTokens = max(
Self.privateAIDictationMinimumOutputTokens,
Int((Double(estimatedInputTokens) * 1.15).rounded(.up)) + 64
)
- let availableOutputTokens = max(
- Self.privateAIDictationMinimumOutputTokens,
- Self.clampPrivateAIContextTokenLimit(contextTokenLimit) - Self.privateAIDictationSystemOverheadTokens - estimatedInputTokens
+ let availableOutputTokens = Self.clampPrivateAIContextTokenLimit(contextTokenLimit)
+ - Self.privateAIDictationSystemOverheadTokens
+ - estimatedInputTokens
+ return PrivateAIDictationTokenBudget(
+ maxOutputTokens: min(requestedOutputTokens, max(Self.privateAIDictationMinimumOutputTokens, availableOutputTokens)),
+ hasSufficientHeadroom: availableOutputTokens >= requestedOutputTokens
)
- return min(requestedOutputTokens, availableOutputTokens)
+ }
+
+ static func privateAIMaxOutputTokens(forInputText inputText: String, contextTokenLimit: Int) -> Int {
+ self.privateAIDictationTokenBudget(
+ forInputText: inputText,
+ contextTokenLimit: contextTokenLimit
+ ).maxOutputTokens
}
enum PrivateAIBackendPreference: String, Codable, CaseIterable, Identifiable {
diff --git a/Sources/Fluid/Services/PrivateAIIntegrationService.swift b/Sources/Fluid/Services/PrivateAIIntegrationService.swift
index d94114d8..25526846 100644
--- a/Sources/Fluid/Services/PrivateAIIntegrationService.swift
+++ b/Sources/Fluid/Services/PrivateAIIntegrationService.swift
@@ -391,7 +391,8 @@ actor PrivateAIIntegrationService {
runtime: RuntimeConfiguration,
context: AppContext
) async throws -> EnhancementResult {
- try await Self.provider.enhanceDictation(inputText, runtime: runtime, context: context)
+ try Self.validateDictationHeadroom(inputText, contextTokenLimit: runtime.contextTokenLimit)
+ return try await Self.provider.enhanceDictation(inputText, runtime: runtime, context: context)
}
func enhanceDictation(
@@ -400,7 +401,8 @@ actor PrivateAIIntegrationService {
context: AppContext,
streamHandler: PrivateAIStreamHandler?
) async throws -> EnhancementResult {
- try await Self.provider.enhanceDictation(
+ try Self.validateDictationHeadroom(inputText, contextTokenLimit: runtime.contextTokenLimit)
+ return try await Self.provider.enhanceDictation(
inputText,
runtime: runtime,
context: context,
@@ -408,6 +410,16 @@ actor PrivateAIIntegrationService {
)
}
+ private nonisolated static func validateDictationHeadroom(_ inputText: String, contextTokenLimit: Int) throws {
+ let budget = SettingsStore.privateAIDictationTokenBudget(
+ forInputText: inputText,
+ contextTokenLimit: contextTokenLimit
+ )
+ guard budget.hasSufficientHeadroom else {
+ throw AIProcessingError.dictationExceedsAIContextWindow
+ }
+ }
+
func rewrite(
_ inputText: String,
systemPrompt: String,
diff --git a/Tests/FluidDictationIntegrationTests/DictationE2ETests.swift b/Tests/FluidDictationIntegrationTests/DictationE2ETests.swift
index 641ecca6..2a92595e 100644
--- a/Tests/FluidDictationIntegrationTests/DictationE2ETests.swift
+++ b/Tests/FluidDictationIntegrationTests/DictationE2ETests.swift
@@ -1830,6 +1830,54 @@ 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: " ")
+
+ XCTAssertTrue(SettingsStore.privateAIDictationTokenBudget(
+ forInputText: shortTranscript,
+ contextTokenLimit: 4096
+ ).hasSufficientHeadroom)
+ XCTAssertFalse(SettingsStore.privateAIDictationTokenBudget(
+ forInputText: longTranscript,
+ contextTokenLimit: 4096
+ ).hasSufficientHeadroom)
+ }
+
+ func testPrivateAIDictationRejectsLongInputBeforeCallingProvider() async {
+ let longTranscript = Array(repeating: "word", count: 3_385).joined(separator: " ")
+ 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(
+ longTranscript,
+ runtime: runtime,
+ context: context
+ )
+ XCTFail("Expected long 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