A dictation that fails says so, in the instance's own words
CI / check (push) Canceled after 0s

The composer's dictation kept a refused or unreachable transcription to
itself: the field stayed as it was. It is now said as any failure is.

And /v1 refuses in OpenAI's shape (error.message), which the client did
not read: a refusal about speech was "The server answered 502." in the
chat, in Read aloud and in the assistant. Both shapes are read now.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
HomerandClaude Fable 5.2 committed 2026-11-18 09:16:55 +00:00
1 parent 8e84f0d53d
commit df20788d7f
5 files changed
+65 -11

No files matched your search

@@ -9,7 +9,6 @@ import kotlinx.serialization.json.JsonNull
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.jsonPrimitive
import kotlinx.serialization.json.put
import kotlinx.serialization.serializer
import okhttp3.Call
@@ -108,12 +107,19 @@ private fun agentsJson(agents: AgentsSetting) = buildJsonObject {
internal fun detail(response: Response): String {
val text = runCatching { response.body?.string().orEmpty() }.getOrDefault("")
val said = runCatching {
LembasJson.parseToJsonElement(text).let { it as JsonObject }["detail"]?.jsonPrimitive?.content
}.getOrNull()
return said ?: "The server answered ${response.code}."
return refusal(text) ?: "The server answered ${response.code}."
}
/**
* The server's own sentence in a refusal's body: `detail` on `/api/v2`, and OpenAI's shape
* (`error.message`) on `/v1`, where speech lives. Null when the body says neither.
*/
internal fun refusal(body: String): String? = runCatching {
val said = LembasJson.parseToJsonElement(body) as JsonObject
fun words(of: Any?): String? = (of as? JsonPrimitive)?.takeUnless { it is JsonNull }?.content?.ifBlank { null }
words(said["detail"]) ?: words((said["error"] as? JsonObject)?.get("message"))
}.getOrNull()
/**
* Everything the app asks one instance, with its token. Every call is the server's
* `/api/v2` (or `/v1` for speech), and every refusal comes back as an [ApiException]
@@ -248,6 +248,9 @@ interface ChatActions {
/** The instance's default answered a dictation or a reading ([kind]) for the chat's own: [sentence] says so, once. */
fun mediaFellBack(kind: String, sentence: String) {}
/** Something the screen asked the instance itself (a dictation) failed: said as any failure is. */
fun failed(e: Throwable) {}
/** A new agent chat with no computer linked: read the linked devices again. */
fun recheckDevices() {}
@@ -566,6 +569,8 @@ class ChatViewModel(app: Application) :
refresh()
}
override fun failed(e: Throwable) = _state.update { it.copy(error = getApplication<Application>().explain(e)) }
private val toldFallback = mutableSetOf<String>()
override fun mediaFellBack(kind: String, sentence: String) {
@@ -94,6 +94,7 @@ import eu.llembas.android.auth.Prefs
import eu.llembas.android.lembas
import eu.llembas.android.ui.DirectoryPicker
import eu.llembas.android.ui.theme.LocalPalette
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
@@ -395,12 +396,18 @@ fun Composer(state: ChatState, actions: ChatActions, agent: Boolean, onTune: ()
scope.launch {
val wav = recorder.stop()
if (wav.isNotEmpty()) {
runCatching { context.lembas.instances.client()?.transcribe(wav, media = state.mediaFor) }
.onSuccess { heard ->
val said = heard?.text
if (!said.isNullOrBlank()) text = (text + " " + said).trim()
heard?.fallback?.let { actions.mediaFellBack(MEDIA_DICTATION, context.getString(R.string.transcribed_by_fallback, it)) }
}
// A transcription that was refused or never arrived is said: a field
// that stays as it was, without a word, reads as "nothing was heard".
try {
val heard = context.lembas.instances.client()?.transcribe(wav, media = state.mediaFor)
val said = heard?.text
if (!said.isNullOrBlank()) text = (text + " " + said).trim()
heard?.fallback?.let { actions.mediaFellBack(MEDIA_DICTATION, context.getString(R.string.transcribed_by_fallback, it)) }
} catch (e: CancellationException) {
throw e
} catch (e: Exception) {
actions.failed(e)
}
}
}
} else if (ContextCompat.checkSelfPermission(context, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED) {
@@ -1,9 +1,11 @@
package eu.llembas.android
import eu.llembas.android.api.ApiClient
import eu.llembas.android.api.ApiException
import eu.llembas.android.api.Chat
import eu.llembas.android.api.MediaFor
import eu.llembas.android.api.ModelInfo
import eu.llembas.android.api.refusal
import eu.llembas.android.ui.chat.ChatState
import eu.llembas.android.ui.chat.Draft
import eu.llembas.android.ui.chat.MEDIA_DICTATION
@@ -18,6 +20,7 @@ import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Assert.fail
import org.junit.Before
import org.junit.Test
@@ -111,6 +114,31 @@ class MediaForTest {
assertTrue(request.body.readUtf8().contains("\"chat_model\":\"shire/pippin\""))
}
@Test
fun aRefusedTranscriptionSaysTheServersSentence() = runTest {
// `/v1` refuses in OpenAI's shape, not `/api/v2`'s `detail`.
server.enqueue(
MockResponse().setResponseCode(502)
.setBody("""{"error":{"message":"The speech model did not answer.","type":"upstream_error","code":502}}"""),
)
try {
client().transcribe(ByteArray(64), media = MediaFor.of("c1", null))
fail("a 502 is a refusal")
} catch (e: ApiException) {
assertEquals(502, e.status)
assertEquals("The speech model did not answer.", e.message)
}
}
@Test
fun aRefusalIsReadInBothShapes() {
assertEquals("No.", refusal("""{"detail":"No."}"""))
assertEquals("Too long.", refusal("""{"error":{"message":"Too long.","code":413}}"""))
assertNull(refusal("""{"error":"plain"}"""))
assertNull(refusal("<html>Bad gateway</html>"))
assertNull(refusal(""))
}
@Test
fun aFallbackIsNamedOnlyWhenTheServerSaysBoth() {
assertEquals("Bree", ApiClient.fallbackSource("1", "Bree"))