diff --git a/CHANGELOG.md b/CHANGELOG.md index ec62d61..56c51a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,14 @@ what the app adds and answers as before. assistant's sheet does not say it, because it may be open over the lock screen. Nothing new is sent while the phone is locked. +### Fixed + +- **A dictation that fails says so.** When the instance refused a recording or could not be + reached, the chat's field simply stayed as it was. Now the chat says why. +- **A refusal about speech is said in the instance's own words.** A dictation or a reading the + instance refused showed "The server answered 502." in the chat and in the assistant; the + instance's sentence is shown instead. + ## [1.0.0-beta.11] — 2026-11-18 Needs LLeMbas 2.0.0-beta.14 for what is new here; with an older instance the app works as before. diff --git a/app/src/main/java/eu/llembas/android/api/ApiClient.kt b/app/src/main/java/eu/llembas/android/api/ApiClient.kt index d073959..0fabe19 100644 --- a/app/src/main/java/eu/llembas/android/api/ApiClient.kt +++ b/app/src/main/java/eu/llembas/android/api/ApiClient.kt @@ -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] diff --git a/app/src/main/java/eu/llembas/android/ui/chat/ChatViewModel.kt b/app/src/main/java/eu/llembas/android/ui/chat/ChatViewModel.kt index 6e36c07..8f8fff9 100644 --- a/app/src/main/java/eu/llembas/android/ui/chat/ChatViewModel.kt +++ b/app/src/main/java/eu/llembas/android/ui/chat/ChatViewModel.kt @@ -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().explain(e)) } + private val toldFallback = mutableSetOf() override fun mediaFellBack(kind: String, sentence: String) { diff --git a/app/src/main/java/eu/llembas/android/ui/chat/Composer.kt b/app/src/main/java/eu/llembas/android/ui/chat/Composer.kt index 012377e..f8376c2 100644 --- a/app/src/main/java/eu/llembas/android/ui/chat/Composer.kt +++ b/app/src/main/java/eu/llembas/android/ui/chat/Composer.kt @@ -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) { diff --git a/app/src/test/java/eu/llembas/android/MediaForTest.kt b/app/src/test/java/eu/llembas/android/MediaForTest.kt index 3938835..8ad5816 100644 --- a/app/src/test/java/eu/llembas/android/MediaForTest.kt +++ b/app/src/test/java/eu/llembas/android/MediaForTest.kt @@ -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("Bad gateway")) + assertNull(refusal("")) + } + @Test fun aFallbackIsNamedOnlyWhenTheServerSaysBoth() { assertEquals("Bree", ApiClient.fallbackSource("1", "Bree"))