hex/ios/HEXShared/EmptyCapture.swift
Ryan Vogel fbaf09648e iOS: end empty dictations quietly and keep error text human in the keyboard
Tapping Done (or ✕) in the keyboard's dictation screen without speaking
showed "Transcription failed: HEXKeyboard.(unknown context at $1…)": the
in-keyboard path printed the error's domain (a private Swift type name),
code and description for Apple Speech's "no words" result.

- New EmptyCapture rule (HEXShared): a capture under 0.3 s (or none at all)
  is a tap, and an engine's no-speech error or a cancellation means nothing
  was said; both end quietly. Engines say so through NoSpeechError
  (Parakeet's and Apple Speech's emptyTranscript, the keyboard's
  KeyboardSpeech.Failure.empty).
- KeyboardLocalDictation: Done before the microphone came up, or a capture
  too short to hold a word, returns to idle without transcribing (and a late
  "started" from WebKit stops the capture instead of opening a file);
  no-speech ends quietly; a transcription error from a cancelled or
  superseded job is dropped; every other failure shows a sentence from
  UserFacingError or the keyboard's own copy, while the probe log keeps the
  domain and code. KeyboardSpeech.Failure's descriptions are now sentences.
- DictationController (warm session and app): an engine's no-speech error
  returns to Ready with no message instead of failing.

Verified: xcodebuild test on the iPhone 18 Pro simulator after rebasing onto
origin/dev (bba07c64), 210 tests pass (1 skipped as before), including new
tests for the empty-capture rule and for a warm keyboard dictation whose
transcriber hears nothing (Ready, no message) next to one that fails (a
sentence). The in-keyboard WebKit capture path needs a physical iPhone.
2026-11-20 08:00:56 -05:00

24 lines
955 B
Swift

import Foundation
/// A dictation with nothing in it ends quietly, with no error: Done before speaking, a
/// capture too short to hold a word, a transcript with no words, or a cancel.
nonisolated enum EmptyCapture {
/// Shorter than this is a tap, not speech (the app discards warm captures the same way).
static let minimumDuration: TimeInterval = 0.30
static func isTooShort(frames: Int64, sampleRate: Double) -> Bool {
guard sampleRate > 0, frames > 0 else { return true }
return Double(frames) / sampleRate < minimumDuration
}
/// Errors that mean nothing was said, or that the person stopped it.
static func endsQuietly(_ error: Error) -> Bool {
if error is CancellationError { return true }
return (error as? NoSpeechError)?.meansNoSpeech ?? false
}
}
/// A speech engine's error, which can say it heard no words.
protocol NoSpeechError: Error {
var meansNoSpeech: Bool { get }
}