e5a83843db
Add resilient usage analytics, OOBE gateway flows, clipboard semantic ranking, purchase recovery, style learning, and managed current-information search.
62 lines
2.3 KiB
Swift
62 lines
2.3 KiB
Swift
// TranscriptionPolishFallback.swift
|
|
// OSGKeyboard · Shared
|
|
//
|
|
// Shared polish-failure handling: conservative raw ASR cleanup plus
|
|
// bilingual user-visible warnings (iOS Flow + macOS dictation).
|
|
|
|
import Foundation
|
|
|
|
public enum TranscriptionPolishFallback: Sendable {
|
|
|
|
public static func makeDelivery(
|
|
rawText: String,
|
|
error: Error,
|
|
engineMode: String,
|
|
chunkWarning: String?
|
|
) -> TranscriptionDelivery {
|
|
let fallbackText = TranscriptPostProcessor.cleanRawASRFallback(rawText)
|
|
if error as? ManagedGatewayError == .oobeFeatureAlreadyUsed {
|
|
// The server confirms this page already succeeded in the current
|
|
// OOBE session. Preserve the raw text without misreporting a weak network.
|
|
return TranscriptionDelivery(text: fallbackText, polishWarning: nil)
|
|
}
|
|
let warning = warning(for: error, engineMode: engineMode)
|
|
?? degradedWarning()
|
|
?? chunkWarning
|
|
return TranscriptionDelivery(text: fallbackText, polishWarning: warning)
|
|
}
|
|
|
|
public static func warning(for error: Error, engineMode: String) -> String? {
|
|
if let polishError = error as? PolishingService.PolishError {
|
|
switch polishError {
|
|
case .missingAPIKey:
|
|
return SharedL10n.string("flow.warning.polishMissingAPIKey")
|
|
case .timeout, .keychainLocked:
|
|
return degradedWarning()
|
|
case .noTranscript:
|
|
return nil
|
|
}
|
|
}
|
|
if error is LLMError {
|
|
return degradedWarning()
|
|
}
|
|
if let managedError = error as? ManagedGatewayError {
|
|
switch managedError {
|
|
case .insufficientCredits:
|
|
return SharedL10n.string("flow.warning.managedInsufficientCredits")
|
|
case .missingGrant, .scopeNotGranted, .invalidGrant:
|
|
return SharedL10n.string("flow.warning.managedGrantRejected")
|
|
case .oobeFeatureAlreadyUsed:
|
|
return SharedL10n.string("flow.warning.oobeFeatureAlreadyUsed")
|
|
case .timeout, .server:
|
|
return degradedWarning()
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
public static func degradedWarning() -> String? {
|
|
SharedL10n.string("flow.warning.polishDegraded")
|
|
}
|
|
}
|