Files
OSGKeyboard/OSGKeyboardShared/Services/TranscriptionPolishFallback.swift
Rocky 2cc81c4628 feat(keyboard): expand contextual skills and managed flows
Add local clipboard intent recommendations, webpage and phone actions, and safer host handoffs. Refine managed gateway, catalog refresh, onboarding, and adaptive polish behavior.
2026-08-23 14:02:33 +08:00

63 lines
2.4 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, .providerUnavailable, .providerRateLimited,
.providerTimeout, .providerFailure, .internalFailure, .server:
return degradedWarning()
}
}
return nil
}
public static func degradedWarning() -> String? {
SharedL10n.string("flow.warning.polishDegraded")
}
}