TypeScript
feat(mobile): add on-device Android dictation (#26734)
Alexd1ec71e6ff8b
diff --git a/apps/mobile/app.config.ts b/apps/mobile/app.config.tsindex e6d388b9a98..b94367a64eb 100644--- a/apps/mobile/app.config.ts+++ b/apps/mobile/app.config.ts@@ -367,7 +367,7 @@ const config: ExpoConfig = {"expo-audio",{microphonePermission: "Allow T3 Code to use your microphone for voice input.",- recordAudioAndroid: false,+ recordAudioAndroid: true,enableBackgroundPlayback: false,enableBackgroundRecording: false,},@@ -381,7 +381,14 @@ const config: ExpoConfig = {recordAudioAndroid: false,},],- ["expo-image-picker", { photosPermission: false, microphonePermission: false }],+ [+ "expo-image-picker",+ {+ photosPermission: false,+ // `false` makes this plugin add RECORD_AUDIO to blockedPermissions, breaking Android dictation.+ microphonePermission: "Allow T3 Code to use your microphone for voice input.",+ },+ ],["expo-splash-screen",{diff --git a/apps/mobile/modules/t3-voice-transcription/android/build.gradle b/apps/mobile/modules/t3-voice-transcription/android/build.gradlenew file mode 100644index 00000000000..05995a7cc4c--- /dev/null+++ b/apps/mobile/modules/t3-voice-transcription/android/build.gradle@@ -0,0 +1,31 @@+apply plugin: 'com.android.library'+apply plugin: 'org.jetbrains.kotlin.android'++group = 'com.t3tools.voicetranscription'+version = '0.0.0'++android {+ namespace 'expo.modules.t3voicetranscription'+ compileSdk rootProject.ext.compileSdkVersion++ defaultConfig {+ minSdkVersion rootProject.ext.minSdkVersion+ targetSdkVersion rootProject.ext.targetSdkVersion+ }+}++dependencies {+ implementation project(':expo-modules-core')+ implementation 'androidx.media3:media3-common:1.9.1'+ implementation 'com.google.mlkit:genai-speech-recognition:1.0.0-alpha2'+ testImplementation 'junit:junit:4.13.2'+ testImplementation 'org.robolectric:robolectric:4.16.1'+ testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.10.2'+}++tasks.withType(Test).configureEach {+ systemProperty 'robolectric.dependency.repo.url', 'https://repo.maven.apache.org/maven2'+ javaLauncher = javaToolchains.launcherFor {+ languageVersion = JavaLanguageVersion.of(21)+ }+}diff --git a/apps/mobile/modules/t3-voice-transcription/android/src/main/AndroidManifest.xml b/apps/mobile/modules/t3-voice-transcription/android/src/main/AndroidManifest.xmlnew file mode 100644index 00000000000..05bc93c9a7d--- /dev/null+++ b/apps/mobile/modules/t3-voice-transcription/android/src/main/AndroidManifest.xml@@ -0,0 +1,5 @@+<manifest xmlns:android="http://schemas.android.com/apk/res/android"+ xmlns:tools="http://schemas.android.com/tools">+ <!-- Recognition is gated to API 31; older devices retain the rest of the app. -->+ <uses-sdk tools:overrideLibrary="com.google.mlkit.genai.speechrecognition,com.google.mlkit.genai.common" />+</manifest>diff --git a/apps/mobile/modules/t3-voice-transcription/android/src/main/java/expo/modules/t3voicetranscription/GoogleVoiceTranscriber.kt b/apps/mobile/modules/t3-voice-transcription/android/src/main/java/expo/modules/t3voicetranscription/GoogleVoiceTranscriber.ktnew file mode 100644index 00000000000..95423844c1d--- /dev/null+++ b/apps/mobile/modules/t3-voice-transcription/android/src/main/java/expo/modules/t3voicetranscription/GoogleVoiceTranscriber.kt@@ -0,0 +1,162 @@+package expo.modules.t3voicetranscription++import android.content.Context+import com.google.mlkit.genai.common.DownloadStatus+import com.google.mlkit.genai.common.FeatureStatus+import com.google.mlkit.genai.common.GenAiException+import com.google.mlkit.genai.speechrecognition.SpeechRecognition+import com.google.mlkit.genai.speechrecognition.SpeechRecognizerOptions+import com.google.mlkit.genai.speechrecognition.SpeechRecognizerResponse+import com.google.mlkit.genai.speechrecognition.speechRecognizerOptions+import com.google.mlkit.genai.speechrecognition.speechRecognizerRequest+import java.io.Closeable+import java.util.Locale+import kotlinx.coroutines.NonCancellable+import kotlinx.coroutines.flow.Flow+import kotlinx.coroutines.flow.first+import kotlinx.coroutines.withContext++internal class VoiceTranscriptionException(+ val code: String,+ message: String,+ cause: Throwable? = null+) : Exception(message, cause)++private val recognitionModes = listOf(+ SpeechRecognizerOptions.Mode.MODE_ADVANCED,+ SpeechRecognizerOptions.Mode.MODE_BASIC+)++internal suspend fun isGoogleVoiceTranscriptionSupported(+ createClient: (Int) -> GoogleSpeechClient+): Boolean {+ for (mode in recognitionModes) {+ createClient(mode).use { client ->+ when (checkModelSupport(client)) {+ FeatureStatus.AVAILABLE,+ FeatureStatus.DOWNLOADABLE,+ FeatureStatus.DOWNLOADING -> return true+ }+ }+ }+ return false+}++internal suspend fun prepareGoogleVoiceTranscription(+ createClient: (Int) -> GoogleSpeechClient+): Int {+ for (mode in recognitionModes) {+ createClient(mode).use { client ->+ if (prepareModel(client)) return mode+ }+ }+ throw VoiceTranscriptionException(+ "unavailable",+ "On-device voice transcription is not available for this device language."+ )+}++private suspend fun prepareModel(client: GoogleSpeechClient): Boolean = try {+ val status = client.checkStatus()+ if (status == FeatureStatus.UNAVAILABLE) {+ false+ } else {+ if (status == FeatureStatus.DOWNLOADABLE ||+ status == FeatureStatus.DOWNLOADING+ ) {+ client.download()+ }+ when (client.checkStatus()) {+ FeatureStatus.UNAVAILABLE -> false+ FeatureStatus.AVAILABLE -> true+ else -> throw VoiceTranscriptionException(+ "preparation-failed",+ "Voice transcription could not prepare this language."+ )+ }+ }+} catch (error: GenAiException) {+ if (!isModelUnavailable(error)) throw error+ false+}++// Any probe failure for one mode means "try the next mode"; prepare surfaces the real error.+private suspend fun checkModelSupport(client: GoogleSpeechClient): Int = try {+ client.checkStatus()+} catch (_: GenAiException) {+ FeatureStatus.UNAVAILABLE+}++private fun isModelUnavailable(error: GenAiException): Boolean = when (error.errorCode) {+ GenAiException.ErrorCode.NOT_AVAILABLE,+ GenAiException.ErrorCode.NOT_SUPPORTED,+ GenAiException.ErrorCode.AICORE_INCOMPATIBLE,+ GenAiException.ErrorCode.NEEDS_SYSTEM_UPDATE -> true+ else -> false+}++internal suspend fun collectGoogleVoiceTranscript(+ responses: Flow<SpeechRecognizerResponse>+): String {+ val text = StringBuilder()+ var partial = ""+ var completed = false+ responses.collect { response ->+ when (response) {+ // Separators between final utterances are supplied by the SDK.+ is SpeechRecognizerResponse.FinalTextResponse -> {+ text.append(response.text)+ partial = ""+ }+ is SpeechRecognizerResponse.PartialTextResponse -> partial = response.text+ is SpeechRecognizerResponse.ErrorResponse -> throw response.e+ is SpeechRecognizerResponse.CompletedResponse -> completed = true+ else -> Unit+ }+ }+ requireCompleteAudio(completed) { "Speech recognition did not complete the recording." }+ // Some sessions complete with a trailing partial instead of another final event.+ return text.append(partial).toString().trim()+}++internal interface GoogleSpeechClient : Closeable {+ suspend fun checkStatus(): Int+ suspend fun download()+}++internal class MlKitSpeechClient(context: Context, locale: Locale, mode: Int) : GoogleSpeechClient {+ private val context = context.applicationContext+ private val recognizer = SpeechRecognition.getClient(+ speechRecognizerOptions {+ this.locale = locale+ preferredMode = mode+ }+ )++ override suspend fun checkStatus() = recognizer.checkStatus()++ override suspend fun download() {+ val result = recognizer.download().first {+ it is DownloadStatus.DownloadCompleted || it is DownloadStatus.DownloadFailed+ }+ if (result is DownloadStatus.DownloadFailed) throw result.e+ }++ suspend fun transcribe(uri: String): String = withRecordedPcm(context, uri) { pcm ->+ PcmPipe.open().use { pipe ->+ val request = speechRecognizerRequest { audioSource = pipe.source }+ try {+ collectGoogleVoiceTranscript(+ pacedRecordedResponses(pcm, pipe) {+ recognizer.startRecognition(request)+ }+ )+ } finally {+ // Stop inference before closing the SDK-owned descriptor, including cancellation.+ withContext(NonCancellable) { runCatching { recognizer.stopRecognition() } }+ }+ }+ }++ override fun close() = recognizer.close()+}diff --git a/apps/mobile/modules/t3-voice-transcription/android/src/main/java/expo/modules/t3voicetranscription/PcmOutput.kt b/apps/mobile/modules/t3-voice-transcription/android/src/main/java/expo/modules/t3voicetranscription/PcmOutput.ktnew file mode 100644index 00000000000..8673ce0d790--- /dev/null+++ b/apps/mobile/modules/t3-voice-transcription/android/src/main/java/expo/modules/t3voicetranscription/PcmOutput.kt@@ -0,0 +1,85 @@+package expo.modules.t3voicetranscription++import com.google.mlkit.genai.speechrecognition.SpeechRecognizerResponse+import kotlinx.coroutines.NonCancellable+import kotlinx.coroutines.TimeoutCancellationException+import kotlinx.coroutines.async+import kotlinx.coroutines.coroutineScope+import kotlinx.coroutines.delay+import kotlinx.coroutines.flow.Flow+import kotlinx.coroutines.flow.flow+import kotlinx.coroutines.withContext+import kotlinx.coroutines.withTimeout+import java.io.File+import java.util.concurrent.atomic.AtomicBoolean++internal interface PcmOutput : AutoCloseable {+ suspend fun write(buffer: ByteArray, count: Int)+ fun finish()+ fun verifyConsumed()+}++private suspend fun replayPcm(file: File, output: PcmOutput) {+ val expected = file.length()+ var sent = 0L+ file.inputStream().use { input ->+ val buffer = ByteArray(3200)+ while (true) {+ val count = input.read(buffer)+ if (count == -1) break+ requireCompleteAudio(count > 0 && count % 2 == 0) {+ "Recorded PCM contains an incomplete audio frame."+ }+ // STREAMING requires real-time delivery, including the final short buffer.+ delay((count * 1000L + 31999) / 32000)+ output.write(buffer, count)+ sent += count+ }+ }+ requireCompleteAudio(sent == expected && file.length() == expected) {+ "The recording changed while speech recognition was reading it. No transcript was added."+ }+}++internal fun pacedRecordedResponses(+ file: File,+ output: PcmOutput,+ responses: () -> Flow<SpeechRecognizerResponse>+): Flow<SpeechRecognizerResponse> = flow {+ coroutineScope {+ val exhausted = AtomicBoolean()+ val sender = async {+ replayPcm(file, output)+ exhausted.set(true)+ output.finish()+ }+ try {+ withTimeout(file.length() / 32 + 30000) {+ responses().collect { emit(it) }+ requireCompleteAudio(exhausted.get()) {+ "Speech recognition ended before receiving the complete recording. " ++ "No transcript was added. Retry dictation."+ }+ sender.await()+ output.verifyConsumed()+ }+ } catch (e: TimeoutCancellationException) {+ throw VoiceTranscriptionException(+ "incomplete-audio",+ "Speech recognition did not finish the complete recording. No transcript was added. Retry dictation.",+ e+ )+ } finally {+ sender.cancel()+ try {+ output.finish()+ } finally {+ withContext(NonCancellable) { sender.join() }+ }+ }+ }+}++internal inline fun requireCompleteAudio(condition: Boolean, message: () -> String) {+ if (!condition) throw VoiceTranscriptionException("incomplete-audio", message())+}diff --git a/apps/mobile/modules/t3-voice-transcription/android/src/main/java/expo/modules/t3voicetranscription/PcmPipe.kt b/apps/mobile/modules/t3-voice-transcription/android/src/main/java/expo/modules/t3voicetranscription/PcmPipe.ktnew file mode 100644index 00000000000..b654ce01a81--- /dev/null+++ b/apps/mobile/modules/t3-voice-transcription/android/src/main/java/expo/modules/t3voicetranscription/PcmPipe.kt@@ -0,0 +1,105 @@+package expo.modules.t3voicetranscription++import android.os.ParcelFileDescriptor+import android.system.ErrnoException+import android.system.Os+import android.system.OsConstants+import com.google.mlkit.genai.common.audio.AudioSource+import kotlinx.coroutines.currentCoroutineContext+import kotlinx.coroutines.delay+import kotlinx.coroutines.ensureActive+import kotlinx.coroutines.withTimeout+import kotlinx.coroutines.TimeoutCancellationException+import java.util.concurrent.atomic.AtomicBoolean++internal class PcmPipe private constructor(+ val source: AudioSource,+ private val writer: ParcelFileDescriptor,+ verificationReader: ParcelFileDescriptor+) : PcmOutput {+ private val closed = AtomicBoolean()+ private val reader = ParcelFileDescriptor.AutoCloseInputStream(verificationReader)+ override suspend fun write(buffer: ByteArray, count: Int) {+ try {+ withTimeout(5000) {+ var offset = 0+ while (offset < count) {+ currentCoroutineContext().ensureActive()+ try {+ val written = Os.write(writer.fileDescriptor, buffer, offset, count - offset)+ requireCompleteAudio(written > 0) { "On-device speech stopped reading the recording." }+ offset += written+ } catch (e: ErrnoException) {+ if (e.errno != OsConstants.EAGAIN) {+ throw VoiceTranscriptionException(+ "incomplete-audio",+ "On-device speech stopped reading the complete recording. No transcript was added. Retry dictation.",+ e+ )+ }+ delay(10)+ }+ }+ }+ } catch (e: TimeoutCancellationException) {+ throw VoiceTranscriptionException(+ "incomplete-audio",+ "On-device speech could not read the complete recording. No transcript was added. Retry dictation.",+ e+ )+ }+ }+ override fun finish() = writer.close()+ override fun verifyConsumed() {+ requireCompleteAudio(reader.available() == 0) {+ "Speech recognition ended with unread recorded audio. No transcript was added. Retry dictation."+ }+ }+ override fun close() {+ if (!closed.compareAndSet(false, true)) return+ try {+ writer.close()+ } finally {+ try {+ reader.close()+ } finally {+ source.close()+ }+ }+ }+ companion object {+ fun open(): PcmPipe {+ val ends = ParcelFileDescriptor.createPipe()+ var sdkReader: ParcelFileDescriptor? = null+ var retained = false+ try {+ Os.fcntlInt(ends[1].fileDescriptor, OsConstants.F_SETFL, OsConstants.O_NONBLOCK)+ // The SDK closes its descriptor; verification must retain a separate read end.+ sdkReader = ends[0].dup()+ val pipe = PcmPipe(+ AudioSource.fromPfd(sdkReader, AudioSource.Mode.STREAMING),+ ends[1],+ ends[0]+ )+ retained = true+ return pipe+ } finally {+ if (!retained) closeFailedPipe(sdkReader, ends)+ }+ }+ private fun closeFailedPipe(+ sdkReader: ParcelFileDescriptor?,+ ends: Array<ParcelFileDescriptor>+ ) {+ try {+ sdkReader?.close()+ } finally {+ try {+ ends[0].close()+ } finally {+ ends[1].close()+ }+ }+ }+ }+}diff --git a/apps/mobile/modules/t3-voice-transcription/android/src/main/java/expo/modules/t3voicetranscription/RecordedPcm.kt b/apps/mobile/modules/t3-voice-transcription/android/src/main/java/expo/modules/t3voicetranscription/RecordedPcm.ktnew file mode 100644index 00000000000..b719981df19--- /dev/null+++ b/apps/mobile/modules/t3-voice-transcription/android/src/main/java/expo/modules/t3voicetranscription/RecordedPcm.kt@@ -0,0 +1,172 @@+package expo.modules.t3voicetranscription++import android.content.Context+import android.media.AudioFormat+import android.media.MediaCodec+import android.media.MediaExtractor+import android.media.MediaFormat+import android.net.Uri+import java.io.Closeable+import java.io.File+import java.io.OutputStream+import java.nio.ByteOrder+import kotlinx.coroutines.currentCoroutineContext+import kotlinx.coroutines.ensureActive+import kotlinx.coroutines.TimeoutCancellationException+import kotlinx.coroutines.withTimeout++internal suspend fun <T> withRecordedPcm(+ context: Context,+ uri: String,+ transcribe: suspend (File) -> T+): T {+ val pcm = File.createTempFile("dictation-", ".pcm", context.cacheDir)+ try {+ try {+ withTimeout(30000) { decodeRecording(context, Uri.parse(uri), pcm) }+ } catch (_: TimeoutCancellationException) {+ throw VoiceTranscriptionException(+ "incomplete-audio",+ "Audio decoding did not finish the recording. Retry dictation."+ )+ }+ requireCompleteAudio(pcm.length() > 0 && pcm.length() % 2 == 0L) {+ "The recording did not contain complete audio samples."+ }+ return transcribe(pcm)+ } finally {+ pcm.delete()+ }+}++private suspend fun decodeRecording(context: Context, uri: Uri, output: File) {+ val extractor = MediaExtractor()+ Closeable { extractor.release() }.use {+ extractor.setDataSource(context, uri, null)+ val track = (0 until extractor.trackCount).firstOrNull {+ extractor.getTrackFormat(it).getString(MediaFormat.KEY_MIME)?.startsWith("audio/") == true+ } ?: throw VoiceTranscriptionException("incomplete-audio", "The recording has no audio track.")+ extractor.selectTrack(track)+ decodeTrack(extractor, extractor.getTrackFormat(track), output)+ }+}++private suspend fun decodeTrack(extractor: MediaExtractor, format: MediaFormat, output: File) {+ format.setInteger(MediaFormat.KEY_PCM_ENCODING, AudioFormat.ENCODING_PCM_16BIT)+ val decoder = MediaCodec.createDecoderByType(+ requireNotNull(format.getString(MediaFormat.KEY_MIME))+ )+ Closeable { decoder.release() }.use {+ decoder.configure(format, null, null, 0)+ decoder.start()+ Closeable { decoder.stop() }.use {+ output.outputStream().buffered().use { stream -> decodeBuffers(decoder, extractor, stream) }+ }+ }+}++private suspend fun decodeBuffers(+ decoder: MediaCodec,+ extractor: MediaExtractor,+ stream: OutputStream+) {+ var converter: RecordingPcmConverter? = null+ var decodedFormat: Triple<Int, Int, Int>? = null+ try {+ var inputEnded = false+ var outputEnded = false+ val info = MediaCodec.BufferInfo()+ while (!outputEnded) {+ currentCoroutineContext().ensureActive()+ if (!inputEnded) inputEnded = queueDecoderInput(decoder, extractor)+ val index = decoder.dequeueOutputBuffer(info, 10000)+ when {+ index == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED -> {+ val next = pcmFormat(decoder.outputFormat, decodedFormat)+ decodedFormat = next+ converter = converter ?: createConverter(next, stream)+ }+ index >= 0 -> {+ consumeDecodedBuffer(decoder, index, info, requireNotNull(converter))+ outputEnded = info.flags and MediaCodec.BUFFER_FLAG_END_OF_STREAM != 0+ }+ }+ }+ requireCompleteAudio(inputEnded) {+ "Audio decoding ended before reading the complete recording."+ }+ converter?.finish()+ } finally {+ converter?.close()+ }+}++private fun pcmFormat(+ format: MediaFormat,+ previous: Triple<Int, Int, Int>?+): Triple<Int, Int, Int> {+ val encoding = if (format.containsKey(MediaFormat.KEY_PCM_ENCODING)) {+ format.getInteger(MediaFormat.KEY_PCM_ENCODING)+ } else {+ AudioFormat.ENCODING_PCM_16BIT+ }+ requireCompleteAudio(encoding == AudioFormat.ENCODING_PCM_16BIT) {+ "The decoder did not provide 16-bit audio."+ }+ val next = Triple(+ format.getInteger(MediaFormat.KEY_SAMPLE_RATE),+ format.getInteger(MediaFormat.KEY_CHANNEL_COUNT),+ encoding+ )+ requireCompleteAudio(previous == null || previous == next) {+ "The recording changed audio format while decoding."+ }+ return next+}++private fun createConverter(+ format: Triple<Int, Int, Int>,+ stream: OutputStream+): RecordingPcmConverter {+ val bytes = ByteArray(8192)+ return RecordingPcmConverter(format.first, format.second) { buffer ->+ while (buffer.hasRemaining()) {+ val count = minOf(buffer.remaining(), bytes.size)+ buffer.get(bytes, 0, count)+ stream.write(bytes, 0, count)+ }+ }+}++private fun queueDecoderInput(decoder: MediaCodec, extractor: MediaExtractor): Boolean {+ val index = decoder.dequeueInputBuffer(10000)+ if (index < 0) return false+ val buffer = requireNotNull(decoder.getInputBuffer(index))+ val count = extractor.readSampleData(buffer, 0)+ return if (count < 0) {+ decoder.queueInputBuffer(index, 0, 0, 0, MediaCodec.BUFFER_FLAG_END_OF_STREAM)+ true+ } else {+ decoder.queueInputBuffer(index, 0, count, extractor.sampleTime, 0)+ extractor.advance()+ false+ }+}++private fun consumeDecodedBuffer(+ decoder: MediaCodec,+ index: Int,+ info: MediaCodec.BufferInfo,+ converter: RecordingPcmConverter+) {+ try {+ if (info.size <= 0 || info.flags and MediaCodec.BUFFER_FLAG_CODEC_CONFIG != 0) return+ val buffer = requireNotNull(decoder.getOutputBuffer(index))+ buffer.position(info.offset)+ buffer.limit(info.offset + info.size)+ buffer.order(ByteOrder.nativeOrder())+ converter.write(buffer)+ } finally {+ decoder.releaseOutputBuffer(index, false)+ }+}diff --git a/apps/mobile/modules/t3-voice-transcription/android/src/main/java/expo/modules/t3voicetranscription/RecordingPcmConverter.kt b/apps/mobile/modules/t3-voice-transcription/android/src/main/java/expo/modules/t3voicetranscription/RecordingPcmConverter.ktnew file mode 100644index 00000000000..3272b2bd7d5--- /dev/null+++ b/apps/mobile/modules/t3-voice-transcription/android/src/main/java/expo/modules/t3voicetranscription/RecordingPcmConverter.kt@@ -0,0 +1,68 @@+package expo.modules.t3voicetranscription++import androidx.annotation.OptIn+import androidx.media3.common.C+import androidx.media3.common.audio.AudioProcessingPipeline+import androidx.media3.common.audio.AudioProcessor+import androidx.media3.common.audio.ChannelMixingAudioProcessor+import androidx.media3.common.audio.ChannelMixingMatrix+import androidx.media3.common.audio.SonicAudioProcessor+import androidx.media3.common.util.UnstableApi+import com.google.common.collect.ImmutableList+import java.nio.ByteBuffer++@OptIn(UnstableApi::class)+internal class RecordingPcmConverter(+ sampleRate: Int,+ channels: Int,+ private val consume: (ByteBuffer) -> Unit+) : AutoCloseable {+ private val frameSize = channels * 2+ private val pipeline = AudioProcessingPipeline(+ ImmutableList.of(+ ChannelMixingAudioProcessor().apply {+ putChannelMixingMatrix(ChannelMixingMatrix.createForConstantGain(channels, 1))+ },+ SonicAudioProcessor().apply { setOutputSampleRateHz(16000) }+ )+ )++ init {+ pipeline.configure(AudioProcessor.AudioFormat(sampleRate, channels, C.ENCODING_PCM_16BIT))+ pipeline.flush()+ }++ fun write(input: ByteBuffer) {+ requireCompleteAudio(input.remaining() % frameSize == 0) {+ "Decoded audio contains an incomplete sample."+ }+ if (!pipeline.isOperational) {+ consume(input)+ return+ }+ while (input.hasRemaining()) {+ val before = input.remaining()+ pipeline.queueInput(input)+ val output = pipeline.output+ val produced = output.remaining()+ consume(output)+ requireCompleteAudio(input.remaining() < before || produced > 0) {+ "Audio conversion stopped before the recording ended."+ }+ }+ }++ fun finish() {+ if (!pipeline.isOperational) return+ pipeline.queueEndOfStream()+ while (!pipeline.isEnded) {+ val output = pipeline.output+ requireCompleteAudio(output.hasRemaining() || pipeline.isEnded) {+ "Audio conversion did not finish the recording."+ }+ consume(output)+ }+ }++ override fun close() = pipeline.reset()+}diff --git a/apps/mobile/modules/t3-voice-transcription/android/src/main/java/expo/modules/t3voicetranscription/T3VoiceTranscriptionModule.kt b/apps/mobile/modules/t3-voice-transcription/android/src/main/java/expo/modules/t3voicetranscription/T3VoiceTranscriptionModule.ktnew file mode 100644index 00000000000..b49791bea2b--- /dev/null+++ b/apps/mobile/modules/t3-voice-transcription/android/src/main/java/expo/modules/t3voicetranscription/T3VoiceTranscriptionModule.kt@@ -0,0 +1,112 @@+package expo.modules.t3voicetranscription++import android.content.res.Resources+import android.os.Build+import expo.modules.kotlin.Promise+import expo.modules.kotlin.functions.Coroutine+import expo.modules.kotlin.modules.Module+import expo.modules.kotlin.modules.ModuleDefinition+import java.util.Locale+import kotlinx.coroutines.CancellationException+import kotlinx.coroutines.CoroutineScope+import kotlinx.coroutines.CoroutineStart+import kotlinx.coroutines.Dispatchers+import kotlinx.coroutines.Job+import kotlinx.coroutines.SupervisorJob+import kotlinx.coroutines.cancel+import kotlinx.coroutines.cancelAndJoin+import kotlinx.coroutines.launch+import kotlinx.coroutines.withContext++class T3VoiceTranscriptionModule : Module() {+ private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)+ private val lock = Any()+ private var operation: Pair<Int, Job>? = null++ override fun definition() = ModuleDefinition {+ Name("T3VoiceTranscription")++ AsyncFunction("checkAvailability").Coroutine<Boolean> {+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {+ false+ } else {+ withContext(Dispatchers.IO) {+ val context = requireNotNull(appContext.reactContext)+ val locale = Resources.getSystem().configuration.locales[0]+ isGoogleVoiceTranscriptionSupported { MlKitSpeechClient(context, locale, it) }+ }+ }+ }++ AsyncFunction("prepare") { id: Int, promise: Promise ->+ runOperation(id, promise, "preparation-failed") {+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {+ throw VoiceTranscriptionException(+ "unavailable",+ "Voice transcription requires Android 12 or later."+ )+ }+ val context = requireNotNull(appContext.reactContext)+ val locale = Resources.getSystem().configuration.locales[0]+ val mode = prepareGoogleVoiceTranscription { MlKitSpeechClient(context, locale, it) }+ mapOf("locale" to locale.toLanguageTag(), "mode" to mode)+ }+ }++ AsyncFunction("transcribe") {+ id: Int,+ uri: String,+ language: String,+ mode: Int,+ promise: Promise+ ->+ runOperation(id, promise, "transcription-failed") {+ val context = requireNotNull(appContext.reactContext)+ MlKitSpeechClient(context, Locale.forLanguageTag(language), mode).use { it.transcribe(uri) }+ }+ }++ AsyncFunction("cancel") { id: Int, promise: Promise ->+ scope.launch {+ val job = synchronized(lock) { operation?.takeIf { it.first == id }?.second }+ job?.cancelAndJoin()+ promise.resolve(null)+ }+ }++ OnDestroy { scope.cancel() }+ }++ private fun runOperation(+ id: Int,+ promise: Promise,+ errorCode: String,+ block: suspend () -> Any?+ ) {+ var result: Result<Any?>? = null+ val job = scope.launch(start = CoroutineStart.LAZY) {+ result = runCatching { block() }+ }+ synchronized(lock) {+ if (operation != null) {+ job.cancel()+ promise.reject("busy", "Another voice transcription is already active.", null)+ return+ }+ operation = id to job+ }+ job.invokeOnCompletion { cause ->+ synchronized(lock) { if (operation?.first == id) operation = null }+ val outcome = cause?.let { Result.failure<Any?>(it) } ?: result+ ?: Result.failure(CancellationException("Voice transcription was cancelled."))+ outcome.fold(+ onSuccess = { promise.resolve(it) },+ onFailure = { error ->+ val code = voiceTranscriptionErrorCode(error, errorCode)+ promise.reject(code, error.message, error)+ }+ )+ }+ job.start()+ }+}diff --git a/apps/mobile/modules/t3-voice-transcription/android/src/main/java/expo/modules/t3voicetranscription/VoiceTranscriptionFailure.kt b/apps/mobile/modules/t3-voice-transcription/android/src/main/java/expo/modules/t3voicetranscription/VoiceTranscriptionFailure.ktnew file mode 100644index 00000000000..bc3dca6f4ef--- /dev/null+++ b/apps/mobile/modules/t3-voice-transcription/android/src/main/java/expo/modules/t3voicetranscription/VoiceTranscriptionFailure.kt@@ -0,0 +1,24 @@+package expo.modules.t3voicetranscription++import com.google.mlkit.genai.common.GenAiException+import kotlinx.coroutines.CancellationException++internal fun voiceTranscriptionErrorCode(+ error: Throwable,+ fallback: String+): String = when (error) {+ is CancellationException -> "cancelled"+ is VoiceTranscriptionException -> error.code+ is GenAiException -> when {+ error.message.orEmpty().contains("NO_SPEECH_DETECTED") -> "no-speech"+ error.errorCode == GenAiException.ErrorCode.NOT_ENOUGH_DISK_SPACE -> "storage"+ error.errorCode == GenAiException.ErrorCode.PER_APP_BATTERY_USE_QUOTA_EXCEEDED -> "quota"+ error.errorCode == GenAiException.ErrorCode.BUSY -> "busy"+ error.errorCode == GenAiException.ErrorCode.AUDIO_BUFFER_OVERFLOW -> "audio-overflow"+ error.errorCode == GenAiException.ErrorCode.NEEDS_SYSTEM_UPDATE ||+ error.errorCode == GenAiException.ErrorCode.AICORE_INCOMPATIBLE -> "needs-update"+ error.message.orEmpty().contains("DOWNLOAD_ERROR") -> "download-failed"+ else -> fallback+ }+ else -> fallback+}diff --git a/apps/mobile/modules/t3-voice-transcription/android/src/test/java/expo/modules/t3voicetranscription/GoogleVoiceTranscriberTest.kt b/apps/mobile/modules/t3-voice-transcription/android/src/test/java/expo/modules/t3voicetranscription/GoogleVoiceTranscriberTest.ktnew file mode 100644index 00000000000..aaa76dfbf07--- /dev/null+++ b/apps/mobile/modules/t3-voice-transcription/android/src/test/java/expo/modules/t3voicetranscription/GoogleVoiceTranscriberTest.kt@@ -0,0 +1,211 @@+package expo.modules.t3voicetranscription++import com.google.mlkit.genai.common.FeatureStatus+import com.google.mlkit.genai.common.GenAiException+import com.google.mlkit.genai.speechrecognition.SpeechRecognizerOptions+import com.google.mlkit.genai.speechrecognition.SpeechRecognizerResponse+import kotlinx.coroutines.CompletableDeferred+import kotlinx.coroutines.cancelAndJoin+import kotlinx.coroutines.flow.flow+import kotlinx.coroutines.launch+import kotlinx.coroutines.runBlocking+import org.junit.Assert.assertEquals+import org.junit.Assert.assertTrue+import org.junit.Test++class GoogleVoiceTranscriberTest {+ @Test+ fun preservesUtterancesAndTheirSpacingWithoutDuplicatingHypotheses() = runBlocking {+ for ((finals, trailing, expected) in listOf(+ Triple(+ listOf("The beginning.", " The middle.", " Repeat this.", " Repeat this."),+ emptyList(),+ "The beginning. The middle. Repeat this. Repeat this."+ ),+ Triple(listOf("最初の文。", "次の文。"), emptyList(), "最初の文。次の文。"),+ Triple(+ listOf("The beginning."),+ listOf(" An unfinished ending", " The final sentence."),+ "The beginning. The final sentence."+ )+ )) {+ val responses = flow {+ for (text in finals) {+ emit(SpeechRecognizerResponse.PartialTextResponse("Unfinished hypothesis"))+ emit(SpeechRecognizerResponse.FinalTextResponse(text, emptyList()))+ }+ for (text in trailing) emit(SpeechRecognizerResponse.PartialTextResponse(text))+ emit(SpeechRecognizerResponse.CompletedResponse)+ }+ assertEquals(expected, collectGoogleVoiceTranscript(responses))+ }+ val incomplete = runCatching {+ collectGoogleVoiceTranscript(+ flow {+ emit(SpeechRecognizerResponse.FinalTextResponse("Incomplete recording.", emptyList()))+ }+ )+ }+ assertEquals(+ "incomplete-audio",+ (incomplete.exceptionOrNull() as VoiceTranscriptionException).code+ )+ }++ private class Client(+ var status: Int,+ val onStatus: () -> Unit = {},+ val downloadedStatus: Int = FeatureStatus.AVAILABLE,+ val onDownload: suspend () -> Unit = {}+ ) : GoogleSpeechClient {+ var closed = false+ var downloads = 0++ override suspend fun checkStatus(): Int {+ onStatus()+ return status+ }++ override suspend fun download() {+ downloads += 1+ onDownload()+ status = downloadedStatus+ }++ override fun close() {+ closed = true+ }+ }++ @Test+ fun checksSupportWithoutDownloadingAndClosesProbedClients() = runBlocking {+ for ((advancedStatus, basicStatus, supported) in listOf(+ Triple(FeatureStatus.AVAILABLE, FeatureStatus.UNAVAILABLE, true),+ Triple(FeatureStatus.DOWNLOADABLE, FeatureStatus.UNAVAILABLE, true),+ Triple(FeatureStatus.DOWNLOADING, FeatureStatus.UNAVAILABLE, true),+ Triple(FeatureStatus.UNAVAILABLE, FeatureStatus.AVAILABLE, true),+ Triple(FeatureStatus.UNAVAILABLE, FeatureStatus.DOWNLOADABLE, true),+ Triple(FeatureStatus.UNAVAILABLE, FeatureStatus.DOWNLOADING, true),+ Triple(FeatureStatus.UNAVAILABLE, FeatureStatus.UNAVAILABLE, false)+ )) {+ val clients = mutableListOf<Client>()+ assertEquals(+ supported,+ isGoogleVoiceTranscriptionSupported { mode ->+ Client(+ if (mode == SpeechRecognizerOptions.Mode.MODE_ADVANCED) advancedStatus else basicStatus+ ).also { clients.add(it) }+ }+ )+ assertTrue(clients.all { it.closed && it.downloads == 0 })+ assertEquals(if (advancedStatus == FeatureStatus.UNAVAILABLE) 2 else 1, clients.size)+ }+ }++ @Test+ fun preparesAdvancedBeforeConsideringBasic() = runBlocking {+ for (status in listOf(+ FeatureStatus.AVAILABLE,+ FeatureStatus.DOWNLOADABLE,+ FeatureStatus.DOWNLOADING+ )) {+ val client = Client(status)+ val mode = prepareGoogleVoiceTranscription {+ assertEquals(SpeechRecognizerOptions.Mode.MODE_ADVANCED, it)+ client+ }+ assertEquals(SpeechRecognizerOptions.Mode.MODE_ADVANCED, mode)+ assertEquals(if (status == FeatureStatus.AVAILABLE) 0 else 1, client.downloads)+ assertTrue(client.closed)+ }+ }++ @Test+ fun fallsBackToBasicOnlyForUnavailableAdvancedModels() = runBlocking {+ for ((duringDownload, errorCode) in listOf(+ false to null,+ false to GenAiException.ErrorCode.NOT_AVAILABLE,+ false to GenAiException.ErrorCode.NOT_SUPPORTED,+ true to GenAiException.ErrorCode.NOT_AVAILABLE,+ true to GenAiException.ErrorCode.NOT_SUPPORTED,+ true to null+ )) {+ val advanced = Client(+ if (duringDownload) FeatureStatus.DOWNLOADABLE else FeatureStatus.UNAVAILABLE,+ downloadedStatus = if (duringDownload &&+ errorCode == null+ ) {+ FeatureStatus.UNAVAILABLE+ } else {+ FeatureStatus.AVAILABLE+ },+ onStatus = {+ if (!duringDownload && errorCode != null) {+ throw GenAiException(Exception("Unavailable"), errorCode)+ }+ },+ onDownload = {+ if (duringDownload && errorCode != null) {+ throw GenAiException(Exception("Unavailable"), errorCode)+ }+ }+ )+ val basic = Client(FeatureStatus.DOWNLOADABLE)+ val mode = prepareGoogleVoiceTranscription {+ if (it == SpeechRecognizerOptions.Mode.MODE_ADVANCED) advanced else basic+ }+ assertEquals(SpeechRecognizerOptions.Mode.MODE_BASIC, mode)+ assertTrue(advanced.closed && basic.closed)+ assertEquals(1, basic.downloads)+ }+ val clients = mutableListOf<Client>()+ val result = runCatching {+ prepareGoogleVoiceTranscription { Client(FeatureStatus.UNAVAILABLE).also { clients.add(it) } }+ }+ assertEquals("unavailable", (result.exceptionOrNull() as VoiceTranscriptionException).code)+ assertTrue(clients.all { it.closed })+ }++ @Test+ fun preservesDownloadAndQuotaFailuresInsteadOfSilentlyFallingBack() = runBlocking {+ for (errorCode in listOf(+ GenAiException.ErrorCode.NOT_ENOUGH_DISK_SPACE,+ GenAiException.ErrorCode.PER_APP_BATTERY_USE_QUOTA_EXCEEDED+ )) {+ val error = GenAiException(Exception("Preparation failed"), errorCode)+ val client = Client(FeatureStatus.DOWNLOADABLE, onDownload = { throw error })+ val result = runCatching {+ prepareGoogleVoiceTranscription {+ assertEquals(SpeechRecognizerOptions.Mode.MODE_ADVANCED, it)+ client+ }+ }+ assertEquals(error, result.exceptionOrNull())+ assertEquals(+ if (errorCode == GenAiException.ErrorCode.NOT_ENOUGH_DISK_SPACE) "storage" else "quota",+ voiceTranscriptionErrorCode(error, "preparation-failed")+ )+ assertTrue(client.closed)+ }+ }++ @Test+ fun cancellationClosesTheDownloadingClientWithoutStartingBasic() = runBlocking {+ val enteredDownload = CompletableDeferred<Unit>()+ val download = CompletableDeferred<Unit>()+ val client = Client(FeatureStatus.DOWNLOADABLE, onDownload = {+ enteredDownload.complete(Unit)+ download.await()+ })+ val job = launch {+ prepareGoogleVoiceTranscription {+ assertEquals(SpeechRecognizerOptions.Mode.MODE_ADVANCED, it)+ client+ }+ }+ enteredDownload.await()+ job.cancelAndJoin()+ assertTrue(client.closed)+ assertEquals(FeatureStatus.DOWNLOADABLE, client.status)+ }+}diff --git a/apps/mobile/modules/t3-voice-transcription/android/src/test/java/expo/modules/t3voicetranscription/PcmReplayTest.kt b/apps/mobile/modules/t3-voice-transcription/android/src/test/java/expo/modules/t3voicetranscription/PcmReplayTest.ktnew file mode 100644index 00000000000..f226cbebca7--- /dev/null+++ b/apps/mobile/modules/t3-voice-transcription/android/src/test/java/expo/modules/t3voicetranscription/PcmReplayTest.kt@@ -0,0 +1,126 @@+package expo.modules.t3voicetranscription++import com.google.mlkit.genai.speechrecognition.SpeechRecognizerResponse+import java.io.ByteArrayOutputStream+import java.io.File+import kotlinx.coroutines.CompletableDeferred+import kotlinx.coroutines.cancelAndJoin+import kotlinx.coroutines.flow.flow+import kotlinx.coroutines.launch+import kotlinx.coroutines.test.currentTime+import kotlinx.coroutines.test.runTest+import org.junit.Assert.assertArrayEquals+import org.junit.Assert.assertEquals+import org.junit.Assert.assertTrue+import org.junit.Test++@OptIn(kotlinx.coroutines.ExperimentalCoroutinesApi::class)+class PcmReplayTest {+ private class Output : PcmOutput {+ val bytes = ByteArrayOutputStream()+ val ended = CompletableDeferred<Unit>()+ var unread = false+ var closed = false++ override suspend fun write(buffer: ByteArray, count: Int) {+ bytes.write(buffer, 0, count)+ }+ override fun finish() {+ ended.complete(Unit)+ }+ override fun verifyConsumed() {+ requireCompleteAudio(!unread) { "Unread audio." }+ }+ override fun close() {+ closed = true+ }+ }++ @Test fun replaysTheEntireLongFileIncludingTheLastShortBuffer() = runTest {+ val pcm = ByteArray(1994140) { (it % 251).toByte() }+ val file = File.createTempFile("dictation-replay-", ".pcm").apply { writeBytes(pcm) }+ try {+ Output().use { output ->+ val responses = pacedRecordedResponses(file, output) {+ flow {+ output.ended.await()+ emit(SpeechRecognizerResponse.FinalTextResponse("Opening. ", emptyList()))+ emit(SpeechRecognizerResponse.FinalTextResponse("Repeat. Repeat. 中間。", emptyList()))+ emit(SpeechRecognizerResponse.PartialTextResponse(" A longer unfinished ending"))+ emit(SpeechRecognizerResponse.PartialTextResponse(" End."))+ emit(SpeechRecognizerResponse.CompletedResponse)+ }+ }+ assertEquals("Opening. Repeat. Repeat. 中間。 End.", collectGoogleVoiceTranscript(responses))+ assertArrayEquals(pcm, output.bytes.toByteArray())+ assertEquals(62317L, currentTime)+ }+ } finally {+ file.delete()+ }+ }++ @Test fun refusesSuccessfulSdkCompletionBeforeAllAudioIsConsumed() = runTest {+ val file = File.createTempFile("dictation-replay-", ".pcm").apply {+ writeBytes(ByteArray(6400))+ }+ try {+ for (early in listOf(true, false)) {+ Output().use { output ->+ output.unread = !early+ val result = runCatching {+ collectGoogleVoiceTranscript(+ pacedRecordedResponses(file, output) {+ flow {+ if (!early) output.ended.await()+ emit(+ SpeechRecognizerResponse.FinalTextResponse(+ "Plausible but incomplete.",+ emptyList()+ )+ )+ emit(SpeechRecognizerResponse.CompletedResponse)+ }+ }+ )+ }+ assertEquals(+ "incomplete-audio",+ (result.exceptionOrNull() as VoiceTranscriptionException).code+ )+ assertTrue(output.ended.isCompleted)+ }+ }+ } finally {+ file.delete()+ }+ }++ @Test fun cancellationSettlesTheSenderBeforeReleasingItsOutput() = runTest {+ val file = File.createTempFile("dictation-replay-", ".pcm").apply {+ writeBytes(ByteArray(6400))+ }+ val entered = CompletableDeferred<Unit>()+ val output = Output()+ try {+ val job = launch {+ output.use {+ collectGoogleVoiceTranscript(+ pacedRecordedResponses(file, it) {+ flow {+ entered.complete(Unit)+ CompletableDeferred<Unit>().await()+ }+ }+ )+ }+ }+ entered.await()+ job.cancelAndJoin()+ assertTrue(output.ended.isCompleted)+ assertTrue(output.closed)+ } finally {+ file.delete()+ }+ }+}diff --git a/apps/mobile/modules/t3-voice-transcription/android/src/test/java/expo/modules/t3voicetranscription/RecordingPcmConverterTest.kt b/apps/mobile/modules/t3-voice-transcription/android/src/test/java/expo/modules/t3voicetranscription/RecordingPcmConverterTest.ktnew file mode 100644index 00000000000..694523610e8--- /dev/null+++ b/apps/mobile/modules/t3-voice-transcription/android/src/test/java/expo/modules/t3voicetranscription/RecordingPcmConverterTest.kt@@ -0,0 +1,51 @@+package expo.modules.t3voicetranscription++import java.io.ByteArrayOutputStream+import java.nio.ByteBuffer+import java.nio.ByteOrder+import kotlin.math.abs+import org.junit.Assert.assertArrayEquals+import org.junit.Assert.assertEquals+import org.junit.Assert.assertTrue+import org.junit.Test+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import org.robolectric.annotation.Config++@RunWith(RobolectricTestRunner::class)+@Config(manifest = Config.NONE, sdk = [31])+class RecordingPcmConverterTest {+ @Test fun preservesAlreadyCompatiblePcmAndResamplesStereoThroughItsTail() {+ for ((rate, channels) in listOf(16000 to 1, 44100 to 2, 48000 to 2)) {+ val frames = rate * 2 + 37+ val input = ByteBuffer.allocateDirect(frames * channels * 2).order(ByteOrder.nativeOrder())+ repeat(frames) { frame ->+ val level = if (frame < rate) 6000 else 18000+ repeat(channels) { input.putShort(level.toShort()) }+ }+ input.flip()+ val original = ByteArray(input.remaining()).also { input.duplicate().get(it) }+ val result = ByteArrayOutputStream()+ RecordingPcmConverter(rate, channels) { buffer ->+ val bytes = ByteArray(buffer.remaining())+ buffer.get(bytes)+ result.write(bytes)+ }.use { converter ->+ while (input.hasRemaining()) {+ val end = minOf(input.limit(), input.position() + channels * 2 * 317)+ val block = input.slice().order(ByteOrder.nativeOrder())+ block.limit(end - input.position())+ converter.write(block)+ input.position(end)+ }+ converter.finish()+ }+ val bytes = result.toByteArray()+ if (rate == 16000) assertArrayEquals(original, bytes)+ val samples = ByteBuffer.wrap(bytes).order(ByteOrder.nativeOrder()).asShortBuffer()+ assertTrue(abs(samples.remaining() * rate.toLong() - frames * 16000L) <= rate)+ assertEquals(6000, samples[8000].toInt())+ assertEquals(18000, samples[samples.limit() - 10].toInt())+ }+ }+}diff --git a/apps/mobile/modules/t3-voice-transcription/expo-module.config.json b/apps/mobile/modules/t3-voice-transcription/expo-module.config.jsonnew file mode 100644index 00000000000..5867f41ffe7--- /dev/null+++ b/apps/mobile/modules/t3-voice-transcription/expo-module.config.json@@ -0,0 +1,6 @@+{+ "platforms": ["android"],+ "android": {+ "modules": ["expo.modules.t3voicetranscription.T3VoiceTranscriptionModule"]+ }+}diff --git a/apps/mobile/src/features/settings/SettingsMicrophoneRouteScreen.tsx b/apps/mobile/src/features/settings/SettingsMicrophoneRouteScreen.tsxindex 480ae40232e..be236d80cce 100644--- a/apps/mobile/src/features/settings/SettingsMicrophoneRouteScreen.tsx+++ b/apps/mobile/src/features/settings/SettingsMicrophoneRouteScreen.tsx@@ -3,7 +3,7 @@ import { useAtomSet, useAtomValue } from "@effect/atom-react";import { AsyncResult } from "effect/reactivity";import { getRecordingPermissionsAsync, requestRecordingPermissionsAsync } from "expo-audio";import { useState } from "react";-import { Linking, useWindowDimensions, View } from "react-native";+import { Linking, Platform, useWindowDimensions, View } from "react-native";import Reanimated, { ReduceMotion, useAnimatedStyle, withTiming } from "react-native-reanimated";import { useSafeAreaInsets } from "react-native-safe-area-context";@@ -16,15 +16,17 @@ import {type MicrophoneEntry,type MicrophoneKind,microphoneEntryKey,- microphoneOrder,} from "../../lib/microphonePriority";-import { getLocalVoiceTranscriber } from "../../native/voiceTranscription";import { mobilePreferencesAtom, updateMobilePreferencesAtom } from "../../state/preferences";import { ListRowLayout } from "../../components/ui/ListRowLayout";import { ThreadSwipeable } from "../home/thread-swipe-actions";import { ListActionRow as SettingsActionRow } from "../../components/ui/ListActionRow";import { SettingsDragHandle } from "./components/SettingsDragHandle";import { SettingsSection } from "./components/SettingsSection";+import {+ deviceMicrophoneOrder,+ localVoiceTranscriberAtom,+} from "../voice-input/VoiceInputProvider";type MicrophonePermission = "checking" | "granted" | "ask" | "denied";@@ -72,7 +74,10 @@ const KINDS: Record<bluetooth: {label: "Bluetooth",row: "Other Bluetooth",- rowDescription: "AirPods, headsets, and cars not listed",+ rowDescription:+ Platform.OS === "android"+ ? "Earbuds, headsets, and cars, including Android Auto, not listed"+ : "AirPods, headsets, and cars not listed",icon: "headphones",},builtIn: {@@ -89,8 +94,16 @@ const KINDS: Record<},};+const UNAVAILABLE_EXPLANATION =+ Platform.OS === "android"+ ? "T3 Code's voice input uses Google's on-device speech recognition, which needs Android 12 or later with compatible Google speech services for the device language. Without it, the composer has no microphone button, and allowing microphone access won't add one. Keyboard voice typing is separate and isn't affected."+ : "T3 Code's voice input uses Apple's on-device speech transcription, which needs iOS 26 or later on a device that supports it. Without it, the composer has no microphone button, and allowing microphone access won't add one. Apple keyboard dictation is separate and isn't affected.";+export function SettingsMicrophoneRouteScreen() {- return getLocalVoiceTranscriber() === null ? <VoiceInputUnavailable /> : <MicrophoneSettings />;+ const transcriber = useAtomValue(localVoiceTranscriberAtom);+ // Android's probe is asynchronous; an empty sheet avoids flashing "unavailable" first.+ if (transcriber === "checking") return <View collapsable={false} className="flex-1 bg-sheet" />;+ return transcriber === null ? <VoiceInputUnavailable /> : <MicrophoneSettings />;}function VoiceInputUnavailable() {@@ -107,12 +120,7 @@ function VoiceInputUnavailable() {<SettingsSection title="Voice input"><ListRowLayout icon="mic" label="Not available on this device" /></SettingsSection>- <Text className="px-2 text-sm text-foreground-muted">- T3 Code's voice input uses Apple's on-device speech transcription, which needs iOS 26 or- later on a device that supports it. Without it, the composer has no microphone button, and- allowing microphone access won't add one. Apple keyboard dictation is separate and isn't- affected.- </Text>+ <Text className="px-2 text-sm text-foreground-muted">{UNAVAILABLE_EXPLANATION}</Text></ScrollView></View>);@@ -123,7 +131,7 @@ function MicrophoneSettings() {const preferencesResult = useAtomValue(mobilePreferencesAtom);const savePreferences = useAtomSet(updateMobilePreferencesAtom);const ready = AsyncResult.isSuccess(preferencesResult) && !preferencesResult.waiting;- const entries = microphoneOrder(+ const entries = deviceMicrophoneOrder(AsyncResult.isSuccess(preferencesResult) ? preferencesResult.value.microphones : undefined,);const { permission, request: requestPermission } = useMicrophonePermission();@@ -138,7 +146,7 @@ function MicrophoneSettings() {const move = (key: string, steps: number) =>savePreferences({transform: (current) => {- const next = [...microphoneOrder(current.microphones)];+ const next = [...deviceMicrophoneOrder(current.microphones)];const from = next.findIndex((entry) => microphoneEntryKey(entry) === key);const to = Math.min(next.length - 1, Math.max(0, from + steps));if (from === -1 || from === to) return {};@@ -150,7 +158,7 @@ function MicrophoneSettings() {const forget = (key: string) =>savePreferences({transform: (current) => ({- microphones: microphoneOrder(current.microphones).filter(+ microphones: deviceMicrophoneOrder(current.microphones).filter((entry) => microphoneEntryKey(entry) !== key,),}),@@ -227,9 +235,12 @@ function MicrophoneSettings() {})}</SettingsSection><Text className="px-2 text-sm text-foreground-muted">- Voice input records from the first connected microphone in this list. Wired and Bluetooth+ Voice input prefers the first connected microphone in this list. Wired and Bluetoothdevices are added by name after you use voice input with them connected; until then, theOther row ranks them. Swipe a device left to forget it.+ {Platform.OS === "android"+ ? " Android may use another input when the preferred microphone cannot be routed."+ : null}</Text></ScrollView></View>diff --git a/apps/mobile/src/features/settings/SettingsRouteScreen.tsx b/apps/mobile/src/features/settings/SettingsRouteScreen.tsxindex 34de55fd440..59e75efa60c 100644--- a/apps/mobile/src/features/settings/SettingsRouteScreen.tsx+++ b/apps/mobile/src/features/settings/SettingsRouteScreen.tsx@@ -244,12 +244,8 @@ function useSettingsIndexSections(): SettingsListSection[] {...(navigationCustomizationAvailable()? [page("SettingsNavigation", "line.3.horizontal", "Navigation tabs")]: []),- ...(Platform.OS === "ios"- ? [- page("SettingsKeyboard", "keyboard", "Keyboard"),- page("SettingsMicrophone", "mic", "Microphone"),- ]- : []),+ ...(Platform.OS === "ios" ? [page("SettingsKeyboard", "keyboard", "Keyboard")] : []),+ page("SettingsMicrophone", "mic", "Microphone"),],},{diff --git a/apps/mobile/src/features/settings/components/ScheduledTaskPromptField.tsx b/apps/mobile/src/features/settings/components/ScheduledTaskPromptField.tsxindex b0b65ff5377..62cfea826b5 100644--- a/apps/mobile/src/features/settings/components/ScheduledTaskPromptField.tsx+++ b/apps/mobile/src/features/settings/components/ScheduledTaskPromptField.tsx@@ -4,6 +4,7 @@ import { AppText as Text } from "../../../components/ui/AppText";import type { ComposerEditorSelection } from "../../../components/ComposerEditor";import {ComposerDictationCancelAction,+ ComposerDictationError,ComposerDictationPrimaryAction,ComposerDictationStartAction,ComposerDictationStatus,@@ -22,7 +23,7 @@ export function ScheduledTaskPromptField(props: {}) {const voice = props.voiceInput;const presentation = resolveVoiceComposerPresentation(voice.state, voice.elapsedSeconds);- const showsDictation = presentation.statusLabel !== null;+ const showsDictation = presentation.statusKind === "active";const showsIdleTrigger = voice.isAvailable && !showsDictation;return (@@ -55,6 +56,10 @@ export function ScheduledTaskPromptField(props: {</View>) : null}</View>+ <ComposerDictationError+ error={voice.state.phase === "error" ? voice.state.error : null}+ onDismiss={voice.cancel}+ />{showsDictation ? (<ComposerDictationToolbar showsDictation><View className="h-11 flex-row items-center">@@ -64,7 +69,6 @@ export function ScheduledTaskPromptField(props: {elapsedSeconds={voice.elapsedSeconds}phase={voice.state.phase}presentation={presentation}- onDismissError={voice.cancel}/><ComposerDictationPrimaryActionstate={voice.state}diff --git a/apps/mobile/src/features/threads/ThreadComposer.tsx b/apps/mobile/src/features/threads/ThreadComposer.tsxindex ea2928f5313..f68f9787f49 100644--- a/apps/mobile/src/features/threads/ThreadComposer.tsx+++ b/apps/mobile/src/features/threads/ThreadComposer.tsx@@ -107,6 +107,7 @@ import { offersLinkedEnvironments } from "@t3tools/client-runtime/link-request";import {ComposerDictationCancelAction,ComposerDictationDraftContent,+ ComposerDictationError,ComposerDictationPrimaryAction,ComposerDictationStartAction,ComposerDictationStatus,@@ -576,7 +577,7 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerPropsvoiceInput.state,voiceInput.elapsedSeconds,);- const isVoiceInputPresented = voicePresentation.statusLabel !== null;+ const isVoiceInputPresented = voicePresentation.statusKind === "active";const isExpanded = isFocused || settingsSheetPresentation.keepsComposerExpanded;const { layout, setComposerEditing } = useAdaptiveWorkspaceLayout();const floatingSidebarComposer = useAndroidFloatingTabs() && layout.usesSplitView;@@ -881,6 +882,10 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerPropspaddingVertical: surfacePaddingVertical,}}>+ <ComposerDictationError+ error={voiceInput.state.phase === "error" ? voiceInput.state.error : null}+ onDismiss={voiceInput.cancel}+ />{isExpanded ? editingAttachments : null}<ViewonLayout={(event) => {@@ -1145,7 +1150,6 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerPropselapsedSeconds={voiceInput.elapsedSeconds}phase={voiceInput.state.phase}presentation={voicePresentation}- onDismissError={voiceInput.cancel}/><View className="shrink-0 flex-row items-center"><ComposerDictationPrimaryActiondiff --git a/apps/mobile/src/features/voice-input/ComposerDictationControl.tsx b/apps/mobile/src/features/voice-input/ComposerDictationControl.tsxindex f38c4f05825..615914589e8 100644--- a/apps/mobile/src/features/voice-input/ComposerDictationControl.tsx+++ b/apps/mobile/src/features/voice-input/ComposerDictationControl.tsx@@ -319,12 +319,39 @@ function VoiceActionButton(props: {);}+export function ComposerDictationError(props: {+ readonly error: string | null;+ readonly onDismiss: () => void;+}) {+ if (!props.error) return null;+ return (+ <View className="min-w-0 flex-row items-center gap-2 px-4 py-3">+ <Text accessibilityRole="alert" className="min-w-0 flex-1 text-sm text-danger-foreground">+ {props.error}+ </Text>+ <Pressable+ accessibilityLabel="Dismiss voice input error"+ accessibilityRole="button"+ className="size-7 items-center justify-center active:opacity-70"+ hitSlop={8}+ onPress={props.onDismiss}+ >+ <SymbolView+ name="xmark"+ size={12}+ tintColorClassName="accent-icon-muted"+ type="monochrome"+ />+ </Pressable>+ </View>+ );+}+export function ComposerDictationStatus(props: {readonly audioLevels: SharedValue<ReadonlyArray<number>>;readonly elapsedSeconds: number;readonly phase: VoiceInputPhase;readonly presentation: VoiceComposerPresentation;- readonly onDismissError: () => void;}) {const recording = props.phase === "recording";const waveformStyle = useAnimatedStyle(() => ({@@ -334,54 +361,28 @@ export function ComposerDictationStatus(props: {opacity: withTiming(recording ? 0 : 1, DICTATION_TIMING),}));- if (!props.presentation.statusLabel) return null;- const isError = props.presentation.statusKind === "error";+ if (!props.presentation.statusLabel || props.presentation.statusKind === "error") return null;return (<View className="relative h-11 min-w-0 flex-1 justify-center">- {isError ? (- <View className="min-w-0 flex-row items-center gap-1.5 px-2">- <Text className="min-w-0 flex-1 text-sm text-danger-foreground" numberOfLines={2}>+ <View+ accessible+ accessibilityLabel={props.presentation.statusLabel}+ accessibilityLiveRegion={props.phase === "recording" ? "none" : "polite"}+ className="h-11"+ >+ <Animated.View+ className="absolute inset-0 min-w-0 flex-row items-center gap-2 px-1"+ style={waveformStyle}+ >+ <VoiceWaveform audioLevels={props.audioLevels} />+ <DictationElapsedTime className="text-foreground-muted" seconds={props.elapsedSeconds} />+ </Animated.View>+ <Animated.View className="absolute inset-0 justify-center px-2" style={labelStyle}>+ <Text className="text-center text-sm text-foreground-muted" numberOfLines={1}>{props.presentation.statusLabel}</Text>- <Pressable- accessibilityLabel="Dismiss voice input error"- accessibilityRole="button"- className="size-7 items-center justify-center active:opacity-70"- hitSlop={8}- onPress={props.onDismissError}- >- <SymbolView- name="xmark"- size={12}- tintColorClassName="accent-icon-muted"- type="monochrome"- />- </Pressable>- </View>- ) : (- <View- accessible- accessibilityLabel={props.presentation.statusLabel}- accessibilityLiveRegion={props.phase === "recording" ? "none" : "polite"}- className="h-11"- >- <Animated.View- className="absolute inset-0 min-w-0 flex-row items-center gap-2 px-1"- style={waveformStyle}- >- <VoiceWaveform audioLevels={props.audioLevels} />- <DictationElapsedTime- className="text-foreground-muted"- seconds={props.elapsedSeconds}- />- </Animated.View>- <Animated.View className="absolute inset-0 justify-center px-2" style={labelStyle}>- <Text className="text-center text-sm text-foreground-muted" numberOfLines={1}>- {props.presentation.statusLabel}- </Text>- </Animated.View>- </View>- )}+ </Animated.View>+ </View></View>);}diff --git a/apps/mobile/src/features/voice-input/VoiceInputProvider.tsx b/apps/mobile/src/features/voice-input/VoiceInputProvider.tsxindex 809ee3a9d21..eab40efd490 100644--- a/apps/mobile/src/features/voice-input/VoiceInputProvider.tsx+++ b/apps/mobile/src/features/voice-input/VoiceInputProvider.tsx@@ -1,18 +1,14 @@import { useAtomValue } from "@effect/atom-react";-import {- AudioModule,- RecordingPresets,- requestRecordingPermissionsAsync,- type RecordingStatus,-} from "expo-audio";+import { AudioModule, RecordingPresets, type RecordingStatus } from "expo-audio";import { File } from "expo-file-system";import { activateKeepAwakeAsync, deactivateKeepAwake } from "expo-keep-awake";import { useSyncExternalStore } from "react";-import { AppState, Platform } from "react-native";+import { Platform } from "react-native";import { Atom, AsyncResult } from "effect/reactivity";import { makeMutable } from "react-native-reanimated";import {+ type MicrophoneEntry,microphoneOrder,preferredMicrophoneInput,rememberMicrophones,@@ -20,10 +16,15 @@ import {import { getLocalVoiceTranscriber } from "../../native/voiceTranscription";import { mobilePreferencesAtom, updateMobilePreferencesAtom } from "../../state/preferences";import { getNativeShowcaseScene } from "../showcase/nativeShowcaseScene";-import { voiceInputBlocksSubmission } from "@t3tools/client-runtime/voice-input";+import {+ voiceInputBlocksSubmission,+ type VoiceTranscriber,+} from "@t3tools/client-runtime/voice-input";import { createLazyVoiceRecorder } from "./lazyVoiceRecorder";import { VOICE_WAVEFORM_SAMPLE_COUNT } from "./voiceInputMetering";import { VoiceInputSession } from "./voiceInputSession";+import { createVoiceRecordingPermission } from "./voiceRecordingPermission";+import { makePermissionStatusAtom } from "../../lib/permissionStatusAtom";import {acquireAudioSession,activateAudioSession,@@ -49,6 +50,24 @@ function keepScreenAwake(tag: string): () => void {return () => void activation.then(() => deactivateKeepAwake(tag)).catch(() => {});}+export const deviceMicrophoneOrder = (saved?: ReadonlyArray<MicrophoneEntry>) =>+ microphoneOrder(saved, Platform.OS === "android" ? "android" : "ios");++/** "checking" until the first availability probe settles; re-probed on foreground. */+export const localVoiceTranscriberAtom = makePermissionStatusAtom<+ VoiceTranscriber | null | "checking"+>({+ label: "mobile:voice-input:transcriber",+ initial: "checking",+ read: () => getLocalVoiceTranscriber().catch(() => null),+}).pipe(Atom.keepAlive);++// Each foreground re-probe yields a new transcriber; composers re-render only when availability flips.+const localVoiceInputAvailableAtom = Atom.make((get) => {+ const transcriber = get(localVoiceTranscriberAtom);+ return transcriber !== null && transcriber !== "checking";+});+/*** The app has one microphone and one dictation at a time, so the session* lives for the registry's lifetime rather than any screen's.@@ -58,11 +77,15 @@ const voiceInputAtom = Atom.make((get) => {// Mounted so the recorder can read and remember microphones synchronously.get.mount(mobilePreferencesAtom);get.mount(updateMobilePreferencesAtom);+ get.mount(localVoiceTranscriberAtom);const audioLevels = makeMutable<ReadonlyArray<number>>(Array<number>(VOICE_WAVEFORM_SAMPLE_COUNT).fill(0),);let keepAwakeSequence = 0;let audioToken: ReturnType<typeof acquireAudioSession> | null = null;+ const permission = createVoiceRecordingPermission(() => {+ void session.controller.appMovedToBackground();+ });const recorder = createLazyVoiceRecorder({create: () => new AudioModule.AudioRecorder(VOICE_RECORDING_OPTIONS),onStatus: (status: RecordingStatus) => {@@ -77,12 +100,12 @@ const voiceInputAtom = Atom.make((get) => {selectInput: (inputs) => {const preferences = registry.get(mobilePreferencesAtom);if (!AsyncResult.isSuccess(preferences)) return null;- const known = microphoneOrder(preferences.value.microphones);+ const known = deviceMicrophoneOrder(preferences.value.microphones);const order = rememberMicrophones(known, inputs);if (order !== known) {registry.set(updateMobilePreferencesAtom, {transform: (current) => ({- microphones: rememberMicrophones(microphoneOrder(current.microphones), inputs),+ microphones: rememberMicrophones(deviceMicrophoneOrder(current.microphones), inputs),}),});}@@ -91,11 +114,11 @@ const voiceInputAtom = Atom.make((get) => {});const session: VoiceInputSession = new VoiceInputSession({recorder,- getTranscriber: getLocalVoiceTranscriber,- requestPermission: async () => {- const permission = await requestRecordingPermissionsAsync();- return { granted: permission.granted, canAskAgain: permission.canAskAgain };+ getTranscriber: () => {+ const transcriber = registry.get(localVoiceTranscriberAtom);+ return transcriber === "checking" ? null : transcriber;},+ requestPermission: permission.requestPermission,configureRecording: async () => {const token = acquireAudioSession("recording");audioToken = token;@@ -120,14 +143,8 @@ const voiceInputAtom = Atom.make((get) => {publishAudioLevels: (levels) => audioLevels.set(levels),keepAwake: () => keepScreenAwake(`voice-input:${++keepAwakeSequence}`),});- // iOS reports `inactive` while its permission dialog is open. Only the real- // background state cancels preparation; recorder status handles calls and- // route interruptions during capture.- const appStateSubscription = AppState.addEventListener("change", (nextState) => {- if (nextState === "background") void session.controller.appMovedToBackground();- });get.addFinalizer(() => {- appStateSubscription.remove();+ permission.dispose();session.dispose();recorder.release();});@@ -136,10 +153,11 @@ const voiceInputAtom = Atom.make((get) => {export function useGlobalVoiceInput() {const { session, audioLevels } = useAtomValue(voiceInputAtom);+ const isTranscriberAvailable = useAtomValue(localVoiceInputAvailableAtom);const snapshot = useSyncExternalStore(session.subscribe, session.getSnapshot);return {...snapshot,- isAvailable: getLocalVoiceTranscriber() !== null || getNativeShowcaseScene() !== null,+ isAvailable: isTranscriberAvailable || getNativeShowcaseScene() !== null,audioLevels,isBusy: voiceInputBlocksSubmission(snapshot.state),session,diff --git a/apps/mobile/src/features/voice-input/lazyVoiceRecorder.test.ts b/apps/mobile/src/features/voice-input/lazyVoiceRecorder.test.tsindex d3236bc7c99..fbb6059099a 100644--- a/apps/mobile/src/features/voice-input/lazyVoiceRecorder.test.ts+++ b/apps/mobile/src/features/voice-input/lazyVoiceRecorder.test.ts@@ -9,14 +9,20 @@ type State = { readonly isRecording: boolean };function createNativeRecorder(uri: string) {const listeners = new Set<(status: Status) => void>();+ // Like expo-audio on Android, preparing again requires a stop in between.+ let isPrepared = false;return {uri: null as string | null,prepared: uri,prepareToRecordAsync: vi.fn(async function (this: { uri: string | null; prepared: string }) {+ if (isPrepared) throw new Error("Recorder is already prepared");+ isPrepared = true;this.uri = this.prepared;}),record: vi.fn(),- stop: vi.fn(async (): Promise<void> => undefined),+ stop: vi.fn(async (): Promise<void> => {+ isPrepared = false;+ }),getStatus: vi.fn((): State => ({ isRecording: true })),addListener: vi.fn((_event: "recordingStatusUpdate", listener: (status: Status) => void) => {listeners.add(listener);@@ -26,7 +32,8 @@ function createNativeRecorder(uri: string) {{ uid: "car", name: "CarPlay", type: "CarAudio" },{ uid: "built-in", name: "iPhone Microphone", type: "MicrophoneBuiltIn" },]),- setInput: vi.fn((_uid: string) => undefined),+ setInput: vi.fn((_uid: string): void | Promise<void> => undefined),+ cancelInputSelection: vi.fn((): void => undefined),release: vi.fn(),emit: (status: Status) => listeners.forEach((listener) => listener(status)),};@@ -34,12 +41,14 @@ function createNativeRecorder(uri: string) {function createHarness(selectInput?: (inputs: ReadonlyArray<VoiceRecordingInput>) => VoiceRecordingInput | null,+ configure?: (native: ReturnType<typeof createNativeRecorder>) => void,) {const created: Array<ReturnType<typeof createNativeRecorder>> = [];const statuses: Status[] = [];const recorder = createLazyVoiceRecorder({create: () => {const native = createNativeRecorder(`file:///voice-${created.length}.m4a`);+ configure?.(native);created.push(native);return native;},@@ -57,10 +66,21 @@ describe("createLazyVoiceRecorder", () => {const disconnected = createHarness((inputs) => inputs[1] ?? null);await disconnected.recorder.prepareToRecordAsync();+ await disconnected.recorder.stop();disconnected.created[0]!.setInput.mockImplementationOnce(() => {throw new Error("Preferred input not found");});await expect(disconnected.recorder.prepareToRecordAsync()).resolves.toBeUndefined();++ const timedOut = createHarness(+ (inputs) => inputs[1] ?? null,+ (native) => {+ native.setInput.mockImplementationOnce(() =>+ Promise.reject(new Error("Routing timed out")),+ );+ },+ );+ await expect(timedOut.recorder.prepareToRecordAsync()).resolves.toBeUndefined();});it("forwards status events until the recorder is released", async () => {@@ -76,8 +96,12 @@ describe("createLazyVoiceRecorder", () => {});describe("with the voice input controller", () => {- function createController(granted: boolean) {- const harness = createHarness();+ function createController(+ granted: boolean,+ selectInput?: (inputs: ReadonlyArray<VoiceRecordingInput>) => VoiceRecordingInput | null,+ configure?: (native: ReturnType<typeof createNativeRecorder>) => void,+ ) {+ const harness = createHarness(selectInput, configure);const deleted: string[] = [];const controller = new VoiceInputController({recorder: harness.recorder,@@ -108,5 +132,29 @@ describe("createLazyVoiceRecorder", () => {expect(controller.currentState.phase).toBe("error");expect(created).toHaveLength(0);});++ it("abandons a pending input route when dictation is canceled", async () => {+ const routing = Promise.withResolvers<void>();+ const { controller, created } = createController(+ true,+ (inputs) => inputs[1] ?? null,+ (native) => {+ // Expo drops the promise of a canceled coroutine function, so it never settles.+ native.setInput.mockImplementationOnce(() => {+ routing.resolve();+ return new Promise(() => undefined);+ });+ },+ );+ const starting = controller.start();+ await routing.promise;+ controller.cancel();+ await starting;+ expect(created[0]!.cancelInputSelection).toHaveBeenCalled();+ expect(controller.currentState.phase).toBe("idle");++ await controller.start();+ expect(controller.currentState.phase).toBe("recording");+ });});});diff --git a/apps/mobile/src/features/voice-input/lazyVoiceRecorder.ts b/apps/mobile/src/features/voice-input/lazyVoiceRecorder.tsindex eceb06bab8c..6100a869fa4 100644--- a/apps/mobile/src/features/voice-input/lazyVoiceRecorder.ts+++ b/apps/mobile/src/features/voice-input/lazyVoiceRecorder.ts@@ -1,4 +1,7 @@-import type { VoiceRecorder } from "@t3tools/client-runtime/voice-input";+import {+ throwIfVoiceTranscriptionAborted,+ type VoiceRecorder,+} from "@t3tools/client-runtime/voice-input";type Subscription = { remove(): void };@@ -19,7 +22,8 @@ export interface NativeVoiceRecorder<TStatus, TRecorderState> {listener: (status: TStatus) => void,): Subscription;getAvailableInputs(): ReadonlyArray<VoiceRecordingInput>;- setInput(uid: string): void;+ setInput(uid: string): void | Promise<void>;+ cancelInputSelection?(): void;release(): void;}@@ -39,6 +43,7 @@ export function createLazyVoiceRecorder<TStatus, TRecorderState>(input: {let retired: NativeVoiceRecorder<TStatus, TRecorderState> | null = null;let retiredUri: string | null = null;let pending: Promise<unknown> = Promise.resolve();+ let abandonInputSelection: (() => void) | null = null;const track = <T>(promise: Promise<T>): Promise<T> => {pending = Promise.allSettled([pending, promise]).then(() => undefined);@@ -50,19 +55,42 @@ export function createLazyVoiceRecorder<TStatus, TRecorderState>(input: {if (recorder) return recorder.uri;return retired ? retired.uri : retiredUri;},- prepareToRecordAsync() {+ prepareToRecordAsync(signal) {if (!recorder) {recorder = input.create();subscription = recorder.addListener("recordingStatusUpdate", input.onStatus);}const prepared = recorder;return track(- prepared.prepareToRecordAsync().then(() => {+ prepared.prepareToRecordAsync().then(async () => {+ if (signal) throwIfVoiceTranscriptionAborted(signal);if (!input.selectInput) return;+ let selected: VoiceRecordingInput | null;try {- const selected = input.selectInput(prepared.getAvailableInputs());- if (selected) prepared.setInput(selected.uid);- } catch {}+ selected = input.selectInput(prepared.getAvailableInputs());+ } catch {+ return;+ }+ if (!selected) return;+ const uid = selected.uid;+ // Expo never settles a canceled coroutine function's promise, so stop waiting on abort.+ const abandoned = Promise.withResolvers<void>();+ const abandon = () => {+ prepared.cancelInputSelection?.();+ abandoned.resolve();+ };+ signal?.addEventListener("abort", abandon, { once: true });+ abandonInputSelection = abandon;+ try {+ await Promise.race([+ new Promise<void>((resolve) => resolve(prepared.setInput(uid))).catch(() => {}),+ abandoned.promise,+ ]);+ if (signal) throwIfVoiceTranscriptionAborted(signal);+ } finally {+ signal?.removeEventListener("abort", abandon);+ if (abandonInputSelection === abandon) abandonInputSelection = null;+ }}),);},@@ -79,6 +107,7 @@ export function createLazyVoiceRecorder<TStatus, TRecorderState>(input: {release() {const released = recorder;if (!released) return;+ abandonInputSelection?.();recorder = null;subscription?.remove();subscription = null;diff --git a/apps/mobile/src/features/voice-input/voiceRecordingPermission.test.ts b/apps/mobile/src/features/voice-input/voiceRecordingPermission.test.tsnew file mode 100644index 00000000000..ccb108811f7--- /dev/null+++ b/apps/mobile/src/features/voice-input/voiceRecordingPermission.test.ts@@ -0,0 +1,52 @@+import { expect, it, vi } from "vite-plus/test";++const mocks = vi.hoisted(() => ({+ state: "active",+ listeners: new Set<(state: string) => void>(),+ request: vi.fn<() => Promise<{ granted: boolean; canAskAgain: boolean }>>(),+}));++vi.mock("expo-audio", () => ({ requestRecordingPermissionsAsync: mocks.request }));+vi.mock("react-native", () => ({+ Platform: { OS: "android" },+ AppState: {+ get currentState() {+ return mocks.state;+ },+ addEventListener: (_event: string, listener: (state: string) => void) => {+ mocks.listeners.add(listener);+ return { remove: () => mocks.listeners.delete(listener) };+ },+ },+}));++import { createVoiceRecordingPermission } from "./voiceRecordingPermission";++function changeAppState(state: string) {+ mocks.state = state;+ mocks.listeners.forEach((listener) => listener(state));+}++it("keeps dictation alive while Android's permission activity backgrounds the app", async () => {+ const permission = Promise.withResolvers<{ granted: boolean; canAskAgain: boolean }>();+ mocks.request.mockReturnValue(permission.promise);+ const interrupted = vi.fn();+ const recording = createVoiceRecordingPermission(interrupted);+ let settled = false;+ const result = recording.requestPermission().then((value) => {+ settled = true;+ return value;+ });++ changeAppState("background");+ permission.resolve({ granted: true, canAskAgain: true });+ await Promise.resolve();+ expect(settled).toBe(false);+ changeAppState("active");+ expect(await result).toMatchObject({ granted: true });+ expect(interrupted).not.toHaveBeenCalled();++ changeAppState("background");+ expect(interrupted).toHaveBeenCalledOnce();+ recording.dispose();+});diff --git a/apps/mobile/src/features/voice-input/voiceRecordingPermission.ts b/apps/mobile/src/features/voice-input/voiceRecordingPermission.tsnew file mode 100644index 00000000000..21ff1df02dd--- /dev/null+++ b/apps/mobile/src/features/voice-input/voiceRecordingPermission.ts@@ -0,0 +1,36 @@+import { requestRecordingPermissionsAsync } from "expo-audio";+import { AppState, Platform } from "react-native";++export function createVoiceRecordingPermission(onBackground: () => void) {+ let requesting = false;+ let disposed = false;+ let resume: (() => void) | null = null;+ const subscription = AppState.addEventListener("change", (state) => {+ if (state === "active") resume?.();+ if (state === "background" && !requesting) onBackground();+ });+ return {+ requestPermission: async () => {+ if (Platform.OS !== "android") return requestRecordingPermissionsAsync();+ // Android's permission activity backgrounds the app; resume before preparing Google's model.+ requesting = true;+ try {+ const permission = await requestRecordingPermissionsAsync();+ if (!disposed && AppState.currentState !== "active") {+ await new Promise<void>((resolve) => {+ resume = resolve;+ });+ }+ return permission;+ } finally {+ requesting = false;+ resume = null;+ }+ },+ dispose: () => {+ disposed = true;+ subscription.remove();+ resume?.();+ },+ };+}diff --git a/apps/mobile/src/lib/microphonePriority.test.ts b/apps/mobile/src/lib/microphonePriority.test.tsindex fff3659057f..d25ec96e7ba 100644--- a/apps/mobile/src/lib/microphonePriority.test.ts+++ b/apps/mobile/src/lib/microphonePriority.test.ts@@ -31,6 +31,12 @@ describe("microphoneOrder", () => {"kind:bluetooth","kind:carPlay",]);+ expect(keys(microphoneOrder([{ kind: "carPlay" }, ...saved], "android"))).toEqual([+ "kind:wired",+ "kind:builtIn",+ "airpods",+ "kind:bluetooth",+ ]);});});@@ -75,5 +81,9 @@ describe("preferredMicrophoneInput", () => {expect(preferredMicrophoneInput([airPods, usb], order)).toBe(usb);expect(preferredMicrophoneInput([airPods, carPlay], order)).toBe(airPods);expect(preferredMicrophoneInput([airPods, iPhone], order)).toBe(iPhone);+ const headset = { uid: "android:7:headset", name: "Headset", type: "BluetoothSCO" };+ expect(preferredMicrophoneInput([headset, iPhone], microphoneOrder([], "android"))).toBe(+ headset,+ );});});diff --git a/apps/mobile/src/lib/microphonePriority.ts b/apps/mobile/src/lib/microphonePriority.tsindex e505837fef3..fbe928f081a 100644--- a/apps/mobile/src/lib/microphonePriority.ts+++ b/apps/mobile/src/lib/microphonePriority.ts@@ -1,11 +1,9 @@-/** iOS lists connected inputs only after recording-session setup. */const MICROPHONE_KINDS = ["wired", "bluetooth", "builtIn", "carPlay"] as const;export type MicrophoneKind = (typeof MICROPHONE_KINDS)[number];export interface MicrophoneDevice {readonly kind: MicrophoneKind;- /** iOS's port identifier, stable across reconnects of the same device. */readonly uid: string;readonly name: string;}@@ -18,12 +16,13 @@ interface MicrophoneInput {readonly type: string;}-// A car without CarPlay connects as a Bluetooth headset, so it is a Bluetooth device.+// A car without CarPlay, and Android Auto, routes its microphone over Bluetooth, so it is a Bluetooth device.const KIND_BY_PORT_TYPE: Readonly<Record<string, MicrophoneKind>> = {MicrophoneWired: "wired",USBAudio: "wired",LineIn: "wired",BluetoothHFP: "bluetooth",+ BluetoothSCO: "bluetooth",BluetoothLE: "bluetooth",MicrophoneBuiltIn: "builtIn",CarAudio: "carPlay",@@ -47,10 +46,13 @@ export function microphoneEntryKey(entry: MicrophoneEntry): string {export function microphoneOrder(saved: ReadonlyArray<MicrophoneEntry> = [],+ platform: "ios" | "android" = "ios",): ReadonlyArray<MicrophoneEntry> {+ const kinds = MICROPHONE_KINDS.filter((kind) => platform === "ios" || kind !== "carPlay");const order: MicrophoneEntry[] = [];const keys = new Set<string>();for (const entry of saved) {+ if (!kinds.includes(entry.kind)) continue;const row =isMicrophoneDevice(entry) && DEVICE_KINDS.has(entry.kind) ? entry : { kind: entry.kind };const key = microphoneEntryKey(row);@@ -58,7 +60,7 @@ export function microphoneOrder(keys.add(key);order.push(row);}- for (const kind of MICROPHONE_KINDS) {+ for (const kind of kinds) {if (keys.has(`kind:${kind}`)) continue;const lastDevice = order.map((entry) => entry.kind).lastIndexOf(kind);const below = order.findIndex(@@ -95,7 +97,7 @@ export function rememberMicrophones(return next ?? order;}-/** The connected input of the first entry that has one, or null to leave the route to iOS. */+/** The connected input of the first entry that has one, or null for the system default. */export function preferredMicrophoneInput<Input extends MicrophoneInput>(inputs: ReadonlyArray<Input>,order: ReadonlyArray<MicrophoneEntry>,diff --git a/apps/mobile/src/lib/permissionStatusAtom.test.ts b/apps/mobile/src/lib/permissionStatusAtom.test.tsindex 052b2a37cb0..ec3330c8f54 100644--- a/apps/mobile/src/lib/permissionStatusAtom.test.ts+++ b/apps/mobile/src/lib/permissionStatusAtom.test.ts@@ -26,6 +26,10 @@ function deferredReads() {pending.shift()?.(status);await Promise.resolve();},+ resolveLast: async (status: string) => {+ pending.pop()?.(status);+ await Promise.resolve();+ },get count() {return pending.length;},@@ -68,6 +72,21 @@ describe("makePermissionStatusAtom", () => {expect(native.listeners.size).toBe(0);});+ it("keeps the newest read when an older one settles last", async () => {+ const reads = deferredReads();+ const atom = makePermissionStatusAtom({ label: "test", initial: "checking", read: reads.read });+ const { registry, flush } = makeRegistry();+ const unmount = registry.mount(atom);++ for (const listener of native.listeners) listener("active");+ await reads.resolveLast("available");+ await reads.resolveNext("unavailable");+ expect(registry.get(atom)).toBe("available");++ unmount();+ flush();+ });+it("stores a request's outcome", () => {const reads = deferredReads();const atom = makePermissionStatusAtom({ label: "test", initial: "checking", read: reads.read });diff --git a/apps/mobile/src/lib/permissionStatusAtom.ts b/apps/mobile/src/lib/permissionStatusAtom.tsindex ee8ff5dcdfe..0aaded10957 100644--- a/apps/mobile/src/lib/permissionStatusAtom.ts+++ b/apps/mobile/src/lib/permissionStatusAtom.ts@@ -3,9 +3,9 @@ import { Atom } from "effect/reactivity";import { subscribeAppActive } from "./appState";/**- * A system permission as the app last saw it. Reads it when first mounted and- * again each time the app returns to the foreground, where the user may have- * changed it in system Settings. Writing stores the outcome of a request.+ * A system permission or device capability as the app last saw it. Reads it+ * when first mounted and again each time the app returns to the foreground,+ * where the user may have changed it in system Settings. Writing stores the outcome of a request.*/export function makePermissionStatusAtom<S>(options: {readonly label: string;@@ -15,8 +15,13 @@ export function makePermissionStatusAtom<S>(options: {return Atom.writable((get) => {// A read that settles after unmount is dropped: setSelf on a disposed lifetime is a no-op.+ // Only the latest read lands, so a slow earlier read cannot overwrite a newer status.+ let latest = 0;const refresh = () => {- void options.read().then((status) => get.setSelf(status));+ const read = ++latest;+ void options.read().then((status) => {+ if (read === latest) get.setSelf(status);+ });};refresh();get.addFinalizer(subscribeAppActive(refresh));diff --git a/apps/mobile/src/native/voiceTranscription.android.test.ts b/apps/mobile/src/native/voiceTranscription.android.test.tsnew file mode 100644index 00000000000..7a2e39e4304--- /dev/null+++ b/apps/mobile/src/native/voiceTranscription.android.test.ts@@ -0,0 +1,84 @@+import { expect, it, vi } from "vite-plus/test";++const mocks = vi.hoisted(() => ({+ checkAvailability: vi.fn<() => Promise<boolean>>(),+ prepare: vi.fn<(id: number) => Promise<{ locale: string; mode: number }>>(),+ transcribe: vi.fn<(id: number, uri: string, locale: string, mode: number) => Promise<string>>(),+ cancel: vi.fn<(id: number) => Promise<void>>(),+}));++vi.mock("expo", () => ({+ requireOptionalNativeModule: () => ({ isAvailable: () => true, ...mocks }),+}));++import {+ VoiceInputController,+ resetVoiceInputGlobalsForTests,+} from "../../../../packages/client-runtime/src/voice-input/controller";++import { getLocalVoiceTranscriber } from "./voiceTranscription.android";++it.each([false, true])(+ "waits for device support before offering a transcriber (%s)",+ async (supported) => {+ vi.resetAllMocks();+ const probe = Promise.withResolvers<boolean>();+ mocks.checkAvailability.mockReturnValue(probe.promise);+ let settled = false;+ const result = Promise.resolve(getLocalVoiceTranscriber()).then((transcriber) => {+ settled = true;+ return transcriber;+ });+ await Promise.resolve();+ expect(settled).toBe(false);+ probe.resolve(supported);+ expect((await result) !== null).toBe(supported);+ },+);++it.each([+ ["preparation", "storage", /disk|storage|space/i],+ ["preparation", "quota", /quota|limit/i],+ ["transcription", "storage", /disk|storage|space/i],+ ["transcription", "quota", /quota|limit/i],+] as const)("preserves an actionable %s error: %s", async (phase, code, expected) => {+ vi.resetAllMocks();+ resetVoiceInputGlobalsForTests();+ mocks.checkAvailability.mockResolvedValue(true);+ mocks.prepare.mockResolvedValue({ locale: "en-US", mode: 0 });+ mocks.transcribe.mockResolvedValue("Complete transcript.");+ mocks.cancel.mockResolvedValue(undefined);+ const rejection = Object.assign(new Error("Native SDK failure"), { code });+ (phase === "preparation" ? mocks.prepare : mocks.transcribe).mockRejectedValue(rejection);+ const transcriber = await getLocalVoiceTranscriber();+ const commit = vi.fn();+ const draft = {+ ownerKey: "acceptance:dictation",+ text: "Preserved draft.",+ selection: { start: 16, end: 16 },+ revision: 1,+ };+ const controller = new VoiceInputController({+ recorder: {+ uri: "file:///acceptance.m4a",+ prepareToRecordAsync: async () => {},+ record: () => {},+ stop: async () => {},+ },+ getTranscriber: () => transcriber,+ requestPermission: async () => ({ granted: true, canAskAgain: true }),+ configureRecording: async () => {},+ releaseRecording: async () => {},+ deleteRecording: () => {},+ readDraft: () => draft,+ commitDraft: commit,+ onStateChange: () => {},+ });+ await controller.start();+ if (phase === "transcription") await controller.stop();++ expect(controller.currentState).toMatchObject({ phase: "error", errorAction: "retry" });+ expect(commit).not.toHaveBeenCalled();+ expect(draft.text).toBe("Preserved draft.");+ expect(controller.currentState.error).toMatch(expected);+});diff --git a/apps/mobile/src/native/voiceTranscription.android.ts b/apps/mobile/src/native/voiceTranscription.android.tsnew file mode 100644index 00000000000..13e233d0ccc--- /dev/null+++ b/apps/mobile/src/native/voiceTranscription.android.ts@@ -0,0 +1,88 @@+import { requireOptionalNativeModule } from "expo";++import {+ VoiceTranscriptionError,+ throwIfVoiceTranscriptionAborted,+ type VoiceTranscriber,+} from "@t3tools/client-runtime/voice-input";++type PreparedGoogleTranscription = { readonly locale: string; readonly mode: number };++type GoogleTranscriptionModule = {+ checkAvailability(): Promise<boolean>;+ prepare(id: number): Promise<PreparedGoogleTranscription>;+ transcribe(id: number, uri: string, locale: string, mode: number): Promise<string>;+ cancel(id: number): Promise<void>;+};++const native = requireOptionalNativeModule<GoogleTranscriptionModule>("T3VoiceTranscription");++const nativeErrorMessages: Readonly<Record<string, string>> = {+ unavailable: "On-device voice transcription is not available for this device language.",+ storage: "Not enough storage for on-device dictation. Free device storage and retry.",+ quota: "On-device speech quota reached. Let the device rest or charge it, then retry.",+ "no-speech": "No speech was detected. Speak again and retry dictation.",+ busy: "On-device speech is busy. Wait for it to finish, then retry dictation.",+ "needs-update": "Update Android and its on-device speech services, then retry dictation.",+ "download-failed": "Speech model download failed. Check your network connection and retry.",+ "audio-overflow":+ "The speech engine could not process the complete recording. No text was added. Retry dictation.",+ "incomplete-audio":+ "Speech recognition could not process the complete recording. No text was added. Retry dictation.",+};++let nextOperationId = 0;++export async function getLocalVoiceTranscriber(): Promise<VoiceTranscriber | null> {+ if (!native || !(await native.checkAvailability())) return null;+ const module = native;+ return {+ prepare: async ({ signal }) => {+ const prepared = await runOperation(module, signal, "preparation-failed", (id) =>+ module.prepare(id),+ );+ return {+ locale: prepared.locale,+ transcribe: (uri, { signal }) =>+ runOperation(module, signal, "transcription-failed", (id) =>+ module.transcribe(id, uri, prepared.locale, prepared.mode),+ ),+ };+ },+ };+}++async function runOperation<T>(+ module: GoogleTranscriptionModule,+ signal: AbortSignal,+ fallbackCode: "preparation-failed" | "transcription-failed",+ operation: (id: number) => Promise<T>,+): Promise<T> {+ throwIfVoiceTranscriptionAborted(signal);+ const id = ++nextOperationId;+ let cancellation: Promise<void> | undefined;+ const abort = () => {+ cancellation = module.cancel(id).catch(() => {});+ };+ signal.addEventListener("abort", abort, { once: true });+ try {+ const result = await operation(id);+ throwIfVoiceTranscriptionAborted(signal);+ return result;+ } catch (error) {+ throwIfVoiceTranscriptionAborted(signal);+ const code =+ typeof error === "object" && error !== null && "code" in error ? error.code : undefined;+ throw new VoiceTranscriptionError(+ code === "unavailable" || code === "unsupported-locale" ? code : fallbackCode,+ (typeof code === "string" ? nativeErrorMessages[code] : undefined) ??+ (fallbackCode === "preparation-failed"+ ? "Voice transcription could not prepare this language. Check Google speech services and try again."+ : "Voice transcription failed."),+ { cause: error },+ );+ } finally {+ signal.removeEventListener("abort", abort);+ await cancellation;+ }+}diff --git a/apps/mobile/src/native/voiceTranscription.ios.test.ts b/apps/mobile/src/native/voiceTranscription.ios.test.tsindex d3ee19c262d..d83c371bc2a 100644--- a/apps/mobile/src/native/voiceTranscription.ios.test.ts+++ b/apps/mobile/src/native/voiceTranscription.ios.test.ts@@ -65,7 +65,7 @@ describe("getLocalVoiceTranscriber", () => {});const controller = new AbortController();const options = { signal: controller.signal };- const prepared = await getLocalVoiceTranscriber()!.prepare(options);+ const prepared = await (await getLocalVoiceTranscriber())!.prepare(options);const result = prepared.transcribe("file:///voice.m4a", options).catch((error: unknown) => error);diff --git a/apps/mobile/src/native/voiceTranscription.ios.ts b/apps/mobile/src/native/voiceTranscription.ios.tsindex 216b9e958dd..9f955664510 100644--- a/apps/mobile/src/native/voiceTranscription.ios.ts+++ b/apps/mobile/src/native/voiceTranscription.ios.ts@@ -33,7 +33,7 @@ function getNativeErrorCode(error: unknown): string | undefined {return typeof error.code === "string" ? error.code : undefined;}-export function getLocalVoiceTranscriber(): VoiceTranscriber | null {+export async function getLocalVoiceTranscriber(): Promise<VoiceTranscriber | null> {const locale = getDeviceLocale();if (!AppleTranscription.isAvailable(locale)) return null;return { prepare: (options) => prepareVoiceTranscription(locale, options) };diff --git a/apps/mobile/src/native/voiceTranscription.ts b/apps/mobile/src/native/voiceTranscription.tsindex e003064ae3f..496a862836a 100644--- a/apps/mobile/src/native/voiceTranscription.ts+++ b/apps/mobile/src/native/voiceTranscription.ts@@ -1,5 +1,5 @@import type { VoiceTranscriber } from "@t3tools/client-runtime/voice-input";-export function getLocalVoiceTranscriber(): VoiceTranscriber | null {+export async function getLocalVoiceTranscriber(): Promise<VoiceTranscriber | null> {return null;}diff --git a/docs/user/composer.md b/docs/user/composer.mdindex a3e06c9aed6..0008e15fce2 100644--- a/docs/user/composer.md+++ b/docs/user/composer.md@@ -177,28 +177,36 @@ Stashes containing uploaded files must be restored in their original environmentThose files are retained for 24 hours. After an upload expires, restore the promptand use **Attach again** or remove the missing file before sending.-## Voice input on iPhone--On supported iPhones with iOS 26 or later, use the composer's microphone to record,-then confirm to transcribe. Text is inserted where your selection was when-recording started, ready for you to review and edit before sending.--Voice input depends on Apple's on-device speech transcription. On a device without-it, the composer has no microphone button and **Settings → Microphone** says voice-input isn't available.--The first use may download Apple's speech model and needs a network connection.-Later transcription works offline for that language. Recordings can be up to five-minutes long. Canceling, leaving the screen, or an audio interruption discards the-recording and preserves your existing draft. While recording, the screen stays+## Voice input on mobile++On supported iPhones with iOS 26 or later and Android devices with Android 12 or+later, use the composer's microphone to record, then confirm to transcribe. Text is+inserted where your selection was when recording started, ready for you to review+and edit before sending.++Voice input uses on-device speech transcription: Apple's on iOS, and compatible+Google speech services on Android. When it is unavailable for the device or its+language, the composer has no microphone button and **Settings → Microphone** says+voice input isn't available. On Android, keep T3 Code open while recording and+transcribing.++The first use may download a speech model and needs a network connection.+Later transcription works offline for that language. On Android, transcription+processes the full recording at roughly its original duration. Recordings can be up to five+minutes long. Canceling or an audio interruption discards the recording and+preserves your existing draft. Navigating to another screen keeps dictation active;+use the floating controls to finish or cancel. While recording, the screen staysawake; it can sleep normally once recording stops.-Voice input records from the first connected microphone in **Settings → Microphone**.-The list mixes kinds (built-in, other wired, other Bluetooth, CarPlay) with wired and-Bluetooth devices you have dictated with, listed by name. Drag to rank them together:+Choose a preferred order in **Settings → Microphone**. Voice input requests the first+connected microphone in the list. The list mixes kinds (built-in, other wired, other+Bluetooth, and CarPlay on iOS) with wired and Bluetooth devices you have dictated+with, listed by name. Drag to rank them together:for example, put **Other Bluetooth** above your AirPods so a new headset is preferredwhile the AirPods microphone stays low. By default, CarPlay comes after the built-in-microphone. Swipe a device left to forget it.+microphone on iOS. Swipe a device left to forget it. Android may use another input+when the preferred microphone cannot be routed. Devices with the same name may share+a saved preference, but simultaneous same-name inputs remain separately selectable.Transcription runs on your device. T3 Code deletes the temporary audio aftertranscription or cancellation; only the message text is sent when you submit.diff --git a/packages/client-runtime/src/voice-input/controller.test.ts b/packages/client-runtime/src/voice-input/controller.test.tsindex d433748e5bd..4b7f9a9c3d4 100644--- a/packages/client-runtime/src/voice-input/controller.test.ts+++ b/packages/client-runtime/src/voice-input/controller.test.ts@@ -9,7 +9,11 @@ import {type VoiceInputControllerDependencies,type VoiceRecorder,} from "./controller.ts";-import type { PreparedVoiceTranscription, VoiceTranscriber } from "./transcription.ts";+import {+ VoiceTranscriptionError,+ type PreparedVoiceTranscription,+ type VoiceTranscriber,+} from "./transcription.ts";function deferred<T>() {let resolve!: (value: T) => void;@@ -116,6 +120,21 @@ describe("VoiceInputController", () => {expect(unsupported.controller.currentState.error).toContain("not available");expect(unsupported.recorder.record).not.toHaveBeenCalled();+ const reason = new VoiceTranscriptionError(+ "unavailable",+ "This device requires iOS 26 or later.",+ );+ const unavailable = createHarness({+ getTranscriber: () => ({+ prepare: async () => {+ throw reason;+ },+ }),+ });+ await unavailable.controller.start();+ expect(unavailable.controller.currentState.error).toBe(reason.message);+ expect(unavailable.recorder.record).not.toHaveBeenCalled();+const denied = createHarness({requestPermission: async () => ({ granted: false, canAskAgain: false }),});diff --git a/packages/client-runtime/src/voice-input/controller.ts b/packages/client-runtime/src/voice-input/controller.tsindex b9d0f54f676..41576db90cf 100644--- a/packages/client-runtime/src/voice-input/controller.ts+++ b/packages/client-runtime/src/voice-input/controller.ts@@ -1,6 +1,10 @@import { replaceTextRange } from "@t3tools/shared/composerTrigger";-import type { PreparedVoiceTranscription, VoiceTranscriber } from "./transcription.ts";+import {+ VoiceTranscriptionError,+ type PreparedVoiceTranscription,+ type VoiceTranscriber,+} from "./transcription.ts";export const VOICE_RECORDING_LIMIT_SECONDS = 5 * 60;@@ -34,7 +38,7 @@ export type VoiceRecorderStatus = {export interface VoiceRecorder {readonly uri: string | null;- prepareToRecordAsync(): Promise<void>;+ prepareToRecordAsync(signal?: AbortSignal): Promise<void>;record(options: { readonly forDuration: number }): void;stop(): Promise<void>;}@@ -167,14 +171,18 @@ function preparationErrorMessage(error: unknown): string {if (errorCode(error) === "unsupported-locale") {return "Voice transcription is not available for this language.";}- return "Could not prepare voice transcription.";+ return error instanceof VoiceTranscriptionError+ ? error.message+ : "Could not prepare voice transcription.";}function transcriptionErrorMessage(error: unknown): string {if (error instanceof Error && error.message === "voice-operation-busy") {return "Voice transcription is still finishing. Try again shortly.";}- return "Could not transcribe this recording.";+ return error instanceof VoiceTranscriptionError+ ? error.message+ : "Could not transcribe this recording.";}const IDLE_STATE: VoiceInputState = { phase: "idle", error: null, errorAction: null };@@ -258,7 +266,7 @@ export class VoiceInputController {await this.dependencies.configureRecording();this.recordingConfigured = true;if (!this.isCurrent(operationToken)) return;- await this.dependencies.recorder.prepareToRecordAsync();+ await this.dependencies.recorder.prepareToRecordAsync(abortController.signal);if (!this.isCurrent(operationToken)) return;this.recordingUri = this.dependencies.recorder.uri;this.rememberRecordingUri(this.recordingUri);@@ -441,12 +449,7 @@ export class VoiceInputController {? { phase: "error", error, errorAction: "retry" }: { phase: "idle", error: null, errorAction: null },);- try {- await this.dependencies.recorder.stop();- } catch {- } finally {- await this.releaseResources();- }+ await this.releaseResources();}private async releaseResources(): Promise<void> {@@ -454,6 +457,11 @@ export class VoiceInputController {try {this.rememberRecordingUri(this.dependencies.recorder.uri);} catch {}+ if (this.recordingConfigured) {+ try {+ await this.dependencies.recorder.stop();+ } catch {}+ }this.recordingUri = null;for (const uri of this.ownedRecordingUris) {try {diff --git a/patches/[email protected] b/patches/[email protected]index 8a644111bce..8249a399b3b 100644--- a/patches/[email protected]+++ b/patches/[email protected]@@ -72,9 +72,10 @@ diff --git a/ios/AudioModule.swift b/ios/AudioModule.swift!self.isSessionInUse else {returndiff --git a/src/AudioModule.types.ts b/src/AudioModule.types.ts+index 3194a8ebd1b91793d03a1692c563b5d2e83c2738..e7e713199f776b60093b75da782197427cf2000d 100644--- a/src/AudioModule.types.ts+++ b/src/AudioModule.types.ts-@@ -27,6 +27,8 @@+@@ -24,6 +24,8 @@ import type { AudioStream } from './AudioStream.types';*/export declare class NativeAudioModule extends NativeModule {setIsAudioActiveAsync(active: boolean): Promise<void>;@@ -83,7 +84,7 @@ diff --git a/src/AudioModule.types.ts b/src/AudioModule.types.tssetAudioModeAsync(category: Partial<AudioMode>): Promise<void>;requestRecordingPermissionsAsync(): Promise<PermissionResponse>;requestNotificationPermissionsAsync(): Promise<PermissionResponse>;-@@ -175,8 +175,9 @@+@@ -175,8 +177,9 @@ export declare class AudioPlayer extends SharedObject<AudioEvents> {/*** Pauses the player.@@ -95,9 +96,10 @@ diff --git a/src/AudioModule.types.ts b/src/AudioModule.types.ts/*** Replaces the current audio source with a new one.diff --git a/build/AudioModule.types.d.ts b/build/AudioModule.types.d.ts+index 0d63c9b949cfae7a101902fc3bb70fcf46094f68..b590f9654bbf74b5052b71e31b7ed6d3d0050aa4 100644--- a/build/AudioModule.types.d.ts+++ b/build/AudioModule.types.d.ts-@@ -6,6 +6,8 @@+@@ -8,6 +8,8 @@ import type { AudioStream } from './AudioStream.types';*/export declare class NativeAudioModule extends NativeModule {setIsAudioActiveAsync(active: boolean): Promise<void>;@@ -106,7 +108,7 @@ diff --git a/build/AudioModule.types.d.ts b/build/AudioModule.types.d.tssetAudioModeAsync(category: Partial<AudioMode>): Promise<void>;requestRecordingPermissionsAsync(): Promise<PermissionResponse>;requestNotificationPermissionsAsync(): Promise<PermissionResponse>;-@@ -135,8 +135,9 @@+@@ -135,8 +137,9 @@ export declare class AudioPlayer extends SharedObject<AudioEvents> {play(): void;/*** Pauses the player.@@ -118,12 +120,367 @@ diff --git a/build/AudioModule.types.d.ts b/build/AudioModule.types.d.ts* Replaces the current audio source with a new one.*/diff --git a/android/src/main/java/expo/modules/audio/AudioModule.kt b/android/src/main/java/expo/modules/audio/AudioModule.kt+index 6c81fce3dd518d3b82e3280940a001c7be84dd0a..16125c679c870e2eae48f8db953dedd3fc842c3d 100644--- a/android/src/main/java/expo/modules/audio/AudioModule.kt+++ b/android/src/main/java/expo/modules/audio/AudioModule.kt-@@ -678,5 +678,5 @@+@@ -677,7 +677,7 @@ class AudioModule : Module() {+ }}- Function("pause") { player: AudioPlayer ->+ Function("pause") { player: AudioPlayer, /* deactivateSession - iOS only */ _: Boolean? ->runOnMain {player.isPaused = false+ player.ref.pause()+@@ -824,10 +824,14 @@ class AudioModule : Module() {+ return@Function recorder.getAvailableInputs(audioManager)+ }++- Function("setInput") { recorder: AudioRecorder, input: String ->++ AsyncFunction("setInput") Coroutine { recorder: AudioRecorder, input: String ->+ recorder.setInput(input, audioManager)+ }+++ Function("cancelInputSelection") { recorder: AudioRecorder ->++ recorder.cancelInputSelection()++ }+++ Function("startRecordingAtTime") { recorder: AudioRecorder, seconds: Double ->+ checkRecordingPermission()+ if (recorder.isPrepared) {+diff --git a/android/src/main/java/expo/modules/audio/AudioRecorder.kt b/android/src/main/java/expo/modules/audio/AudioRecorder.kt+index 2ff4a474fdea3efa652436a8a3960c3669ad298b..1ced279991294d3d81cb20fa339ca73e5d6d6c01 100644+--- a/android/src/main/java/expo/modules/audio/AudioRecorder.kt++++ b/android/src/main/java/expo/modules/audio/AudioRecorder.kt+@@ -19,6 +19,9 @@ import expo.modules.kotlin.exception.CodedException+ import expo.modules.kotlin.exception.Exceptions+ import expo.modules.kotlin.sharedobjects.SharedObject+ import kotlinx.coroutines.Job++import kotlinx.coroutines.CancellationException++import kotlinx.coroutines.currentCoroutineContext++import kotlinx.coroutines.job+ import kotlinx.coroutines.delay+ import kotlinx.coroutines.launch+ import java.io.File+@@ -42,12 +45,19 @@ class AudioRecorder(+ var isPrepared = false++ private var recorder: MediaRecorder? = null++ private val inputRoute = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {++ RecordingInputRoute(context.getSystemService(Context.AUDIO_SERVICE) as AudioManager)++ } else {++ null++ }+ val id = UUID.randomUUID().toString()+ var startTime = 0L+ var isRecording = false+ var isPaused = false+ private var isPausedBySystem = false+ private var recordingTimerJob: Job? = null++ private var inputSelectionJob: Job? = null++ private var inputSelectionCanceled = false+ private var durationLimitMillis: Long? = null+ var useForegroundService = false++@@ -90,6 +100,7 @@ class AudioRecorder(+ throw AudioRecorderAlreadyPreparedException()+ }+++ synchronized(this) { inputSelectionCanceled = false }+ val recordingOptions = options ?: this.options+ val mediaRecorder = createRecorder(recordingOptions)+ recorder = mediaRecorder+@@ -245,10 +256,15 @@ class AudioRecorder(+ }++ private fun reset() {++ cancelInputSelection()+ recordingTimerJob?.cancel()+ recordingTimerJob = null++- recorder?.release()++ try {++ recorder?.release()++ } finally {++ inputRoute?.close()++ }+ recorder = null+ isRecording = false+ isPaused = false+@@ -468,55 +484,68 @@ class AudioRecorder(+ throw DeviceInfoNotFoundException()+ }++- return getMapFromDeviceInfo(deviceInfo)++ return getMapFromDeviceInfo(deviceInfo, recordingInputs(audioManager))+ }++ private fun hasRecordingPermissions() =+ ContextCompat.checkSelfPermission(context, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED++- fun getAvailableInputs(audioManager: AudioManager) =+- audioManager.getDevices(AudioManager.GET_DEVICES_INPUTS).mapNotNull { deviceInfo ->+- val type = deviceInfo.type+- if (type == AudioDeviceInfo.TYPE_BUILTIN_MIC || type == AudioDeviceInfo.TYPE_BLUETOOTH_SCO || type == AudioDeviceInfo.TYPE_WIRED_HEADSET) {+- getMapFromDeviceInfo(deviceInfo)+- } else {+- null+- }++ fun getAvailableInputs(audioManager: AudioManager): List<Bundle> {++ val inputs = recordingInputs(audioManager)++ return inputs.map { getMapFromDeviceInfo(it, inputs) }++ }++++ suspend fun setInput(uid: String, audioManager: AudioManager) {++ val job = currentCoroutineContext().job++ synchronized(this) {++ if (inputSelectionCanceled) throw CancellationException("Input selection canceled")++ inputSelectionJob = job++ }++ try {++ setInputDevice(uid, audioManager)++ } finally {++ synchronized(this) { if (inputSelectionJob === job) inputSelectionJob = null }+ }++ }++++ fun cancelInputSelection() {++ val job = synchronized(this) {++ inputSelectionCanceled = true++ inputSelectionJob++ }++ job?.cancel()++ }++ @Suppress("DEPRECATION")+- fun setInput(uid: String, audioManager: AudioManager) {+- val deviceInfo: AudioDeviceInfo? = getDeviceInfoFromUid(uid, audioManager)++ private suspend fun setInputDevice(uid: String, audioManager: AudioManager) {++ val deviceInfo = getDeviceInfoFromUid(uid, audioManager) ?: throw PreferredInputNotFoundException()++ if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {+ throw SetAudioInputNotSupportedException()+ }++- if (deviceInfo != null && deviceInfo.type == AudioDeviceInfo.TYPE_BLUETOOTH_SCO) {+- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {+- audioManager.setCommunicationDevice(deviceInfo)+- } else {++ try {++ if (inputRoute != null) {++ inputRoute.select(deviceInfo)++ } else if (deviceInfo.type == AudioDeviceInfo.TYPE_BLUETOOTH_SCO) {+ audioManager.startBluetoothSco()+- }+- } else {+- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {+- audioManager.clearCommunicationDevice()+ } else {+ audioManager.stopBluetoothSco()+ }+- }+-+- val success = recorder?.setPreferredDevice(deviceInfo)+- if (success == false) {+- throw PreferredInputNotFoundException()++ val source = getDeviceInfoFromUid(uid, audioManager)?.takeIf { it.isSource }++ if (recorder?.setPreferredDevice(source) != true) {++ throw PreferredInputNotFoundException()++ }++ } catch (error: Exception) {++ inputRoute?.close()++ throw error+ }+ }++ private fun getDeviceInfoFromUid(uid: String, audioManager: AudioManager): AudioDeviceInfo? {+- val id = uid.toInt()+- val audioDevices: Array<AudioDeviceInfo> = audioManager.getDevices(AudioManager.GET_DEVICES_INPUTS)++ val audioDevices = recordingInputs(audioManager)+ for (device in audioDevices) {+- if (device.id == id) {++ if (recordingInputUid(device, audioDevices) == uid) {+ return device+ }+ }+diff --git a/android/src/main/java/expo/modules/audio/AudioUtils.kt b/android/src/main/java/expo/modules/audio/AudioUtils.kt+index 81819c1d771bdeec093bb35a65a5e1e1af0cf9d7..cd0c7624b4cd8a4ebf7ed3b3390cfc76802e8530 100644+--- a/android/src/main/java/expo/modules/audio/AudioUtils.kt++++ b/android/src/main/java/expo/modules/audio/AudioUtils.kt+@@ -2,6 +2,8 @@ package expo.modules.audio++ import android.content.Context+ import android.media.AudioDeviceInfo++import android.media.AudioManager++import android.os.Build+ import android.os.Bundle+ import androidx.annotation.OptIn+ import androidx.media3.common.Player+@@ -17,22 +19,57 @@ fun ensureDirExists(dir: File): File {+ return dir+ }++-fun getMapFromDeviceInfo(deviceInfo: AudioDeviceInfo): Bundle {++fun getMapFromDeviceInfo(deviceInfo: AudioDeviceInfo, inputs: List<AudioDeviceInfo>): Bundle {+ val map = Bundle()+- val type = when (deviceInfo.type) {+- AudioDeviceInfo.TYPE_BUILTIN_MIC -> "MicrophoneBuiltIn"+- AudioDeviceInfo.TYPE_BLUETOOTH_SCO -> "BluetoothSCO"+- AudioDeviceInfo.TYPE_BLUETOOTH_A2DP -> "BluetoothA2DP"+- AudioDeviceInfo.TYPE_TELEPHONY -> "Telephony"+- AudioDeviceInfo.TYPE_WIRED_HEADSET -> "MicrophoneWired"+- else -> "Unknown device type"+- }++ val type = recordingInputType(deviceInfo.type) ?: "Unknown device type"+ map.putString("name", deviceInfo.productName.toString())+ map.putString("type", type)+- map.putString("uid", deviceInfo.id.toString())++ map.putString("uid", recordingInputUid(deviceInfo, inputs))+ return map+ }+++internal fun recordingInputs(manager: AudioManager): List<AudioDeviceInfo> {++ val inputs = manager.getDevices(AudioManager.GET_DEVICES_INPUTS).filter {++ recordingInputType(it.type) != null++ }++ val bluetooth = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {++ manager.availableCommunicationDevices.filter { isBluetoothInput(it.type) }++ } else {++ emptyList()++ }++ // Some Bluetooth microphone ports appear only after the communication route is activated.++ return (inputs + bluetooth).distinctBy {++ if (isBluetoothInput(it.type)) recordingInputUid(it) else "port:${it.id}"++ }++}++++internal fun isBluetoothInput(type: Int) = type == AudioDeviceInfo.TYPE_BLUETOOTH_SCO ||++ type == AudioDeviceInfo.TYPE_BLE_HEADSET++++internal fun recordingInputType(type: Int): String? = when (type) {++ AudioDeviceInfo.TYPE_BUILTIN_MIC -> "MicrophoneBuiltIn"++ AudioDeviceInfo.TYPE_BLUETOOTH_SCO -> "BluetoothSCO"++ AudioDeviceInfo.TYPE_BLE_HEADSET -> "BluetoothLE"++ AudioDeviceInfo.TYPE_WIRED_HEADSET -> "MicrophoneWired"++ AudioDeviceInfo.TYPE_USB_DEVICE, AudioDeviceInfo.TYPE_USB_HEADSET -> "USBAudio"++ AudioDeviceInfo.TYPE_LINE_ANALOG, AudioDeviceInfo.TYPE_LINE_DIGITAL -> "LineIn"++ else -> null++}++++internal fun recordingInputUid(device: AudioDeviceInfo, inputs: List<AudioDeviceInfo> = emptyList()): String {++ val base = recordingInputBaseUid(device)++ val ambiguous = !isBluetoothInput(device.type) && inputs.any {++ it.id != device.id && recordingInputBaseUid(it) == base++ }++ return if (ambiguous) "$base:port:${device.id}" else base++}++++private fun recordingInputBaseUid(device: AudioDeviceInfo): String {++ // Port IDs and USB addresses (ALSA card numbers) change on reconnect; Bluetooth addresses don't.++ val stable = Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && isBluetoothInput(device.type)++ return "android:${device.type}:${if (stable) device.address.ifBlank { device.productName } else device.productName}"++}+++ @OptIn(UnstableApi::class)+ fun buildBasicMediaSession(context: Context, player: Player, onPlay: () -> Unit): MediaSession {+ return MediaSession.Builder(context, PlayRequestingPlayer(player, onPlay))+diff --git a/android/src/main/java/expo/modules/audio/RecordingInputRoute.kt b/android/src/main/java/expo/modules/audio/RecordingInputRoute.kt+new file mode 100644+index 0000000000000000000000000000000000000000..cdba9724318cf06afd21b4df6b895b381afa8e6f+--- /dev/null++++ b/android/src/main/java/expo/modules/audio/RecordingInputRoute.kt+@@ -0,0 +1,53 @@++package expo.modules.audio++++import android.media.AudioDeviceInfo++import android.media.AudioManager++import androidx.annotation.RequiresApi++import java.io.Closeable++import java.util.concurrent.Executor++import kotlinx.coroutines.CompletableDeferred++import kotlinx.coroutines.withTimeout++++@RequiresApi(31)++internal class RecordingInputRoute(private val manager: AudioManager) : Closeable {++ private var previousMode: Int? = null++++ suspend fun select(input: AudioDeviceInfo) {++ if (!isBluetoothInput(input.type)) {++ close()++ return++ }++ // Communication routing takes the headset's output; Android pairs its microphone.++ val output = manager.availableCommunicationDevices.firstOrNull {++ it.isSink && it.type == input.type &&++ if (input.address.isNotBlank()) it.address == input.address else it.productName == input.productName++ } ?: throw PreferredInputNotFoundException()++ val ready = CompletableDeferred<Unit>()++ val listener = AudioManager.OnCommunicationDeviceChangedListener {++ if (it?.id == output.id) ready.complete(Unit)++ }++ manager.addOnCommunicationDeviceChangedListener(Executor { it.run() }, listener)++ try {++ if (previousMode == null) previousMode = manager.mode++ manager.mode = AudioManager.MODE_IN_COMMUNICATION++ if (!manager.setCommunicationDevice(output)) throw PreferredInputNotFoundException()++ if (manager.communicationDevice?.id == output.id) ready.complete(Unit)++ withTimeout(5_000) { ready.await() }++ } catch (error: Exception) {++ close()++ throw error++ } finally {++ manager.removeOnCommunicationDeviceChangedListener(listener)++ }++ }++++ override fun close() {++ val mode = previousMode ?: return++ previousMode = null++ try {++ manager.clearCommunicationDevice()++ } finally {++ if (manager.mode == AudioManager.MODE_IN_COMMUNICATION) manager.mode = mode++ }++ }++}+diff --git a/android/src/test/java/expo/modules/audio/RecordingInputIdentityTest.kt b/android/src/test/java/expo/modules/audio/RecordingInputIdentityTest.kt+new file mode 100644+index 0000000000000000000000000000000000000000..4dd0567e2170770e9b0f6a17dc4581bc769d5c6a+--- /dev/null++++ b/android/src/test/java/expo/modules/audio/RecordingInputIdentityTest.kt+@@ -0,0 +1,35 @@++package expo.modules.audio++++import android.media.AudioDeviceInfo++import android.media.AudioManager++import io.mockk.every++import io.mockk.mockk++import org.junit.Assert.*++import org.junit.Test++++class RecordingInputIdentityTest {++ private fun device(id: Int) = mockk<AudioDeviceInfo> {++ every { [email protected] } returns id++ every { type } returns AudioDeviceInfo.TYPE_USB_DEVICE++ every { productName } returns "USB Microphone"++ every { address } returns "card=$id;device=0;"++ }++++ @Test++ fun identicalUsbMicrophonesRemainSelectable() {++ val first = device(1)++ val second = device(2)++ val manager = mockk<AudioManager> {++ every { getDevices(AudioManager.GET_DEVICES_INPUTS) } returns arrayOf(first, second)++ }++ val inputs = recordingInputs(manager)++ assertEquals(2, inputs.size)++ val firstUid = recordingInputUid(first, inputs)++ val secondUid = recordingInputUid(second, inputs)++ assertNotEquals(firstUid, secondUid)++ assertSame(first, inputs.single { recordingInputUid(it, inputs) == firstUid })++ assertSame(second, inputs.single { recordingInputUid(it, inputs) == secondUid })++ val reconnected = device(99)++ assertEquals(recordingInputUid(first, listOf(first)), recordingInputUid(reconnected, listOf(reconnected)))++ }++}diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 755f2f95c2e..84d25ab1524 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -378,7 +378,7 @@ patchedDependencies:'@react-navigation/[email protected]': 990d575444c4af2ce8218b3b7a8280fe244c03abbdb32b822a2e862f791dbf42[email protected]: 0fbe9ae51783f5768ebe197d12d1fdb7d477a08c92e5db2f6356bcce62613c53[email protected]: cfff57561b0ee59b5addb3b2e6c6f20906e967507a530ab67e8db8108e520ba4- [email protected]: 530af071088a809fe1a97ce2b2269c78335fbb5dcd3893d6937ff5c304c85a0a+ [email protected]: b391b150cf67acdf23a9d924b791a63f8c4c84ec7a7b18cc82f815f7933fe4ba[email protected]: f0b0fd100550ed76e5c25b4d13eb7c9ff62825f8f197c53edf7bf447c50c9fb7[email protected]: cd09b6d1744dde4efa081b5619ebcff38877d98f7502a51cab121629dece8237[email protected]: 321a86e1471e34c53f1f2937fbb4304ea3f7b7a78ad9e4d35febc2b9e73e9688@@ -627,7 +627,7 @@ importers:version: 58.0.16(@tsc-rs/[email protected])([email protected])([email protected](@babel/[email protected]([email protected]))(@react-native/[email protected](@babel/[email protected]([email protected]))([email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected])([email protected]))([email protected])([email protected])expo-audio:specifier: 58.0.9- version: 58.0.9(patch_hash=530af071088a809fe1a97ce2b2269c78335fbb5dcd3893d6937ff5c304c85a0a)([email protected](@tsc-rs/[email protected])([email protected])([email protected](@babel/[email protected]([email protected]))(@react-native/[email protected](@babel/[email protected]([email protected]))([email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected])([email protected]))([email protected])([email protected]))([email protected])([email protected](@babel/[email protected]([email protected]))(@react-native/[email protected](@babel/[email protected]([email protected]))([email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected])([email protected]))([email protected])+ version: 58.0.9(patch_hash=b391b150cf67acdf23a9d924b791a63f8c4c84ec7a7b18cc82f815f7933fe4ba)([email protected](@tsc-rs/[email protected])([email protected])([email protected](@babel/[email protected]([email protected]))(@react-native/[email protected](@babel/[email protected]([email protected]))([email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected])([email protected]))([email protected])([email protected]))([email protected])([email protected](@babel/[email protected]([email protected]))(@react-native/[email protected](@babel/[email protected]([email protected]))([email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected])([email protected]))([email protected])expo-auth-session:specifier: ~58.0.13version: 58.0.13(@tsc-rs/[email protected])([email protected])([email protected](@babel/[email protected]([email protected]))(@react-native/[email protected](@babel/[email protected]([email protected]))([email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected])([email protected]))([email protected])([email protected])@@ -18508,7 +18508,7 @@ snapshots:- supports-color- typescript- [email protected](patch_hash=530af071088a809fe1a97ce2b2269c78335fbb5dcd3893d6937ff5c304c85a0a)([email protected](@tsc-rs/[email protected])([email protected])([email protected](@babel/[email protected]([email protected]))(@react-native/[email protected](@babel/[email protected]([email protected]))([email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected])([email protected]))([email protected])([email protected]))([email protected])([email protected](@babel/[email protected]([email protected]))(@react-native/[email protected](@babel/[email protected]([email protected]))([email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected])([email protected]))([email protected]):+ [email protected](patch_hash=b391b150cf67acdf23a9d924b791a63f8c4c84ec7a7b18cc82f815f7933fe4ba)([email protected](@tsc-rs/[email protected])([email protected])([email protected](@babel/[email protected]([email protected]))(@react-native/[email protected](@babel/[email protected]([email protected]))([email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected])([email protected]))([email protected])([email protected]))([email protected])([email protected](@babel/[email protected]([email protected]))(@react-native/[email protected](@babel/[email protected]([email protected]))([email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected])([email protected]))([email protected]):dependencies:expo: 58.0.14(277a83826bb26ba15256633fefeb0bc2)expo-asset: 58.0.16(@tsc-rs/[email protected])([email protected])([email protected](@babel/[email protected]([email protected]))(@react-native/[email protected](@babel/[email protected]([email protected]))([email protected])([email protected])([email protected]))(@types/[email protected])([email protected])([email protected])([email protected])([email protected]))([email protected])([email protected])