The instance may give a connection speech models of its own, used for the chats whose model is on it. A transcription and a speech request now carry chat_id for a chat that exists, else chat_model with the model a new chat (or the assistant's next one) starts on; another app's dictation names neither and stays on the default. A server before this reads only the fields it knows. When the answer says the default answered for the chat's own (X-Lembas-Fallback), the chat screen says so once per kind, with the server's sentence. The assistant's sheet does not: it may be over the lock. No request was added on a locked path. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
21 files changed
+340
-28
No files matched your search
@@ -0,0 +1,139 @@
|
||||
package eu.llembas.android
|
||||
|
||||
import eu.llembas.android.api.ApiClient
|
||||
import eu.llembas.android.api.Chat
|
||||
import eu.llembas.android.api.MediaFor
|
||||
import eu.llembas.android.api.ModelInfo
|
||||
import eu.llembas.android.ui.chat.ChatState
|
||||
import eu.llembas.android.ui.chat.Draft
|
||||
import eu.llembas.android.ui.chat.MEDIA_DICTATION
|
||||
import eu.llembas.android.ui.chat.MEDIA_SPEECH
|
||||
import eu.llembas.android.ui.chat.fallbackToSay
|
||||
import eu.llembas.android.ui.chat.mediaFor
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import okhttp3.mockwebserver.MockResponse
|
||||
import okhttp3.mockwebserver.MockWebServer
|
||||
import org.junit.After
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
|
||||
/**
|
||||
* Speech follows the chat's model (server 2.0.0-beta.15): a transcription or a speech request
|
||||
* says which chat it is for -- `chat_id`, or `chat_model` before the chat exists -- and never
|
||||
* both; another app's dictation says neither.
|
||||
*/
|
||||
class MediaForTest {
|
||||
private lateinit var server: MockWebServer
|
||||
|
||||
@Before
|
||||
fun up() {
|
||||
server = MockWebServer().apply { start() }
|
||||
}
|
||||
|
||||
@After
|
||||
fun down() = server.shutdown()
|
||||
|
||||
private fun client() = ApiClient(server.url("/").toString().trimEnd('/'), "lmb_test")
|
||||
|
||||
@Test
|
||||
fun aChatThatExistsIsNamedByItsIdAlone() {
|
||||
assertEquals(MediaFor(chatId = "c1"), MediaFor.of("c1", "shire/pippin"))
|
||||
assertEquals(MediaFor(chatModel = "shire/pippin"), MediaFor.of(null, "shire/pippin"))
|
||||
assertEquals(MediaFor(chatModel = "shire/pippin"), MediaFor.of(" ", "shire/pippin"))
|
||||
assertEquals(MediaFor.DEFAULT, MediaFor.of(null, null))
|
||||
assertEquals(MediaFor.DEFAULT, MediaFor.of("", " "))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun theTranscriptionFieldsSayWhichChat() {
|
||||
assertEquals(listOf("chat_id" to "c1"), ApiClient.transcriptionFields(null, MediaFor.of("c1", "shire/pippin")))
|
||||
assertEquals(
|
||||
listOf("language" to "sk", "chat_model" to "shire/pippin"),
|
||||
ApiClient.transcriptionFields("sk", MediaFor.of(null, "shire/pippin")),
|
||||
)
|
||||
assertEquals(emptyList<Pair<String, String>>(), ApiClient.transcriptionFields(null, MediaFor.DEFAULT))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun theSpeechBodySaysWhichChat() {
|
||||
val inChat = ApiClient.speechBody("Hello", MediaFor.of("c1", "shire/pippin")).toString()
|
||||
assertTrue(inChat, inChat.contains("\"chat_id\":\"c1\""))
|
||||
assertFalse(inChat, inChat.contains("chat_model"))
|
||||
assertTrue(inChat, inChat.contains("\"input\":\"Hello\""))
|
||||
val before = ApiClient.speechBody("Hello", MediaFor.of(null, "shire/pippin")).toString()
|
||||
assertTrue(before, before.contains("\"chat_model\":\"shire/pippin\""))
|
||||
assertFalse(before, before.contains("chat_id"))
|
||||
val none = ApiClient.speechBody("Hello", MediaFor.DEFAULT).toString()
|
||||
assertFalse(none, none.contains("chat_"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun aTranscriptionCarriesTheChatAndReadsTheFallback() = runTest {
|
||||
server.enqueue(
|
||||
MockResponse().setBody("""{"text":"Second breakfast"}""")
|
||||
.setHeader("X-Lembas-Source", "Bree%20%2B%20Inn").setHeader("X-Lembas-Fallback", "1"),
|
||||
)
|
||||
val heard = client().transcribe(ByteArray(64), media = MediaFor.of("c1", null))
|
||||
assertEquals("Second breakfast", heard.text)
|
||||
assertEquals("Bree + Inn", heard.fallback)
|
||||
val request = server.takeRequest()
|
||||
assertEquals("/v1/audio/transcriptions", request.path)
|
||||
val body = request.body.readUtf8()
|
||||
assertTrue(body, body.contains("name=\"chat_id\""))
|
||||
assertFalse(body, body.contains("chat_model"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun anotherAppsDictationNamesNoChat() = runTest {
|
||||
server.enqueue(MockResponse().setBody("""{"text":"Elevenses"}""").setHeader("X-Lembas-Source", "stt%2Fsmall"))
|
||||
val heard = client().transcribe(ByteArray(64))
|
||||
assertEquals("Elevenses", heard.text)
|
||||
// The source is named, and it is the one expected: nothing to say.
|
||||
assertNull(heard.fallback)
|
||||
val body = server.takeRequest().body.readUtf8()
|
||||
assertFalse(body, body.contains("chat_id"))
|
||||
assertFalse(body, body.contains("chat_model"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun speechCarriesTheModelOfAChatNotMadeYet() = runTest {
|
||||
server.enqueue(MockResponse().setBody("mp3"))
|
||||
val spoken = client().speech("Hello", MediaFor.of(null, "shire/pippin"))
|
||||
// An older server names no source at all.
|
||||
assertNull(spoken.fallback)
|
||||
assertEquals(3, spoken.audio.size)
|
||||
val request = server.takeRequest()
|
||||
assertEquals("/v1/audio/speech", request.path)
|
||||
assertTrue(request.body.readUtf8().contains("\"chat_model\":\"shire/pippin\""))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun aFallbackIsNamedOnlyWhenTheServerSaysBoth() {
|
||||
assertEquals("Bree", ApiClient.fallbackSource("1", "Bree"))
|
||||
assertNull(ApiClient.fallbackSource(null, "Bree"))
|
||||
assertNull(ApiClient.fallbackSource("1", null))
|
||||
assertNull(ApiClient.fallbackSource("0", "Bree"))
|
||||
// A name that is not percent-encoding stays as it came.
|
||||
assertEquals("100%", ApiClient.fallbackSource("1", "100%"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun theChatScreenNamesItsChatOrTheModelItWillBeOn() {
|
||||
val models = listOf(ModelInfo(id = "pippin", ref = "shire/pippin", default = true), ModelInfo(id = "merry", ref = "buckland/merry"))
|
||||
assertEquals(MediaFor(chatModel = "shire/pippin"), ChatState(models = models).mediaFor)
|
||||
assertEquals(MediaFor(chatModel = "buckland/merry"), ChatState(models = models, draft = Draft(model = "buckland/merry")).mediaFor)
|
||||
assertEquals(MediaFor(chatId = "c1"), ChatState(models = models, chat = Chat(id = "c1", model = "merry")).mediaFor)
|
||||
assertEquals(MediaFor.DEFAULT, ChatState().mediaFor)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun aFallbackIsSaidOncePerKind() {
|
||||
assertTrue(fallbackToSay(emptySet(), MEDIA_DICTATION))
|
||||
assertFalse(fallbackToSay(setOf(MEDIA_DICTATION), MEDIA_DICTATION))
|
||||
assertTrue(fallbackToSay(setOf(MEDIA_DICTATION), MEDIA_SPEECH))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user