Skip to content

Your phone, on your command. Gotcha is world's first fully on-device AI copilot for Android that turns natural language into real device actions — safely.

refactor: enhance model categorization by introducing LLM hints and supporting both provider_type and task fields. closes #62

Rishabh Bajpai0aafeee43e85
diff --git a/app/src/main/java/com/gotcha/MainActivity.kt b/app/src/main/java/com/gotcha/MainActivity.kt
index 99ada3a..a0ac737 100644
--- a/app/src/main/java/com/gotcha/MainActivity.kt
+++ b/app/src/main/java/com/gotcha/MainActivity.kt
@@ -33,6 +33,7 @@ import androidx.compose.ui.graphics.toArgb
import androidx.lifecycle.lifecycleScope
import com.gotcha.agent.ChatViewModel
import com.gotcha.audio.AudioApi
+import com.gotcha.audio.AudioModel
import com.gotcha.audio.ModelCategory
import com.gotcha.auth.SamosaAuthManager
import com.gotcha.auth.SamosaSignInResult
@@ -474,31 +475,37 @@ class MainActivity : ComponentActivity() {
onRefreshAudioModels = { s ->
withContext(Dispatchers.IO) {
val ttsBase = s.effectiveTtsBaseUrl
- if (ttsBase.isBlank()) return@withContext Pair(emptyList(), emptyList())
- // AudioApi's onUnauthorized fires on an OkHttp thread; Toast
- // and refreshSettings() must run on the main thread.
- val onUnauthorized: () -> Unit = {
- }
- }
- val ttsApi = AudioApi(
- baseUrl = ttsBase,
- apiKey = s.effectiveTtsApiKey,
- onUnauthorized = onUnauthorized
- )
- val ttsAll = ttsApi.listAudioModels()
- val ttsModels = ttsAll.filter { it.category == ModelCategory.TTS }
val sttBase = s.effectiveSttBaseUrl
- val sttModels = if (sttBase.isNotBlank() && sttBase != ttsBase) {
- val sttApi = AudioApi(
- baseUrl = sttBase,
- apiKey = s.effectiveSttApiKey,
- onUnauthorized = onUnauthorized
- )
- sttApi.listAudioModels().filter { it.category == ModelCategory.STT }
+ if (ttsBase.isBlank() && sttBase.isBlank()) {
+ return@withContext Pair(emptyList(), emptyList())
+ }
+ val fetch: (String, String) -> List<AudioModel> =
+ { base, key ->
+ AudioApi(
+ baseUrl = base,
+ apiKey = key,
+ onUnauthorized = { [email protected] { onSamosaUnauthorized() } }
+ ).listAudioModels()
+ }
+ // Fetch the shared server once when both audio sides
+ // point at the same URL; fetch each side independently
+ // otherwise (STT-only configurations are valid).
+ val sharedAll = if (ttsBase.isNotBlank() && ttsBase == sttBase) {
+ fetch(ttsBase, s.effectiveTtsApiKey)
} else {
- ttsAll.filter { it.category == ModelCategory.STT }
+ emptyList()
+ }
+ val ttsModels = when {
+ ttsBase.isBlank() -> emptyList()
+ ttsBase == sttBase -> sharedAll.filter { it.category == ModelCategory.TTS }
+ else -> fetch(ttsBase, s.effectiveTtsApiKey)
+ .filter { it.category == ModelCategory.TTS }
+ }
+ val sttModels = when {
+ sttBase.isBlank() -> emptyList()
+ sttBase == ttsBase -> sharedAll.filter { it.category == ModelCategory.STT }
+ else -> fetch(sttBase, s.effectiveSttApiKey)
+ .filter { it.category == ModelCategory.STT }
}
Pair(ttsModels, sttModels)
}
diff --git a/app/src/main/java/com/gotcha/audio/AudioApi.kt b/app/src/main/java/com/gotcha/audio/AudioApi.kt
index e4c1094..ab696f7 100644
--- a/app/src/main/java/com/gotcha/audio/AudioApi.kt
+++ b/app/src/main/java/com/gotcha/audio/AudioApi.kt
@@ -82,7 +82,8 @@ class AudioApi(
val modelObj = element.jsonObject
val id = modelObj["id"]?.jsonPrimitive?.contentOrNull ?: return@mapNotNull null
val task = modelObj["task"]?.jsonPrimitive?.contentOrNull
- val category = AudioModel.categorize(id, task)
+ val providerType = modelObj["provider_type"]?.jsonPrimitive?.contentOrNull
+ val category = AudioModel.categorize(id, task, providerType)
val languages = when (val langElem = modelObj["language"]) {
is kotlinx.serialization.json.JsonArray -> langElem.mapNotNull { l ->
l.jsonPrimitive.contentOrNull?.takeIf { it.isNotBlank() }
diff --git a/app/src/main/java/com/gotcha/audio/AudioModels.kt b/app/src/main/java/com/gotcha/audio/AudioModels.kt
index 9e2a6d9..5c272b1 100644
--- a/app/src/main/java/com/gotcha/audio/AudioModels.kt
+++ b/app/src/main/java/com/gotcha/audio/AudioModels.kt
@@ -43,7 +43,7 @@ data class ModelInfo(
)
/** Categorization of a model from the API. */
-enum class ModelCategory { TTS, STT, UNKNOWN }
+enum class ModelCategory { TTS, STT, LLM, UNKNOWN }
/** Detailed voice metadata for TTS models. */
data class VoiceInfo(
@@ -83,19 +83,51 @@ data class AudioModel(
?: defaultVoice
companion object {
+ private val TTS_HINTS = setOf(
+ "text-to-speech",
+ "tts"
+ )
+ private val STT_HINTS = setOf(
+ "automatic-speech-recognition",
+ "stt",
+ "speech-to-text",
+ "transcription"
+ )
+ private val LLM_HINTS = setOf(
+ "llm",
+ "chat",
+ "text-generation",
+ "chat-completion",
+ "chat_completion",
+ "language-model",
+ "completions"
+ )
+
/**
- * Categorize a model by its [task] field (from the API) and fall back
- * to name heuristics when the task field is absent.
+ * Categorize a model by the API-declared hint fields and fall back to
+ * name heuristics. Different servers publish the hint under different
+ * keys — `task` is the OpenAI-compatible norm, `provider_type` is what
+ * the Samosa proxy uses, so both are checked. Name heuristics remain
+ * the last resort when neither is present.
*/
- fun categorize(id: String, task: String? = null): ModelCategory {
- if (task != null) {
- val t = task.lowercase().trim()
- when {
- t == "text-to-speech" || t == "tts" -> return ModelCategory.TTS
- t == "automatic-speech-recognition" || t == "stt" ||
- t == "speech-to-text" || t == "transcription" -> return ModelCategory.STT
- }
+ fun categorize(id: String, task: String? = null, providerType: String? = null): ModelCategory {
+ classifyByHint(task)?.let { return it }
+ classifyByHint(providerType)?.let { return it }
+ return classifyByName(id)
+ }
+
+ private fun classifyByHint(hint: String?): ModelCategory? {
+ if (hint.isNullOrBlank()) return null
+ val normalized = hint.lowercase().trim()
+ return when (normalized) {
+ in TTS_HINTS -> ModelCategory.TTS
+ in STT_HINTS -> ModelCategory.STT
+ in LLM_HINTS -> ModelCategory.LLM
+ else -> null
}
+ }
+
+ private fun classifyByName(id: String): ModelCategory {
val lower = id.lowercase()
return when {
lower.contains("whisper") -> ModelCategory.STT
diff --git a/app/src/main/java/com/gotcha/llm/LLMClient.kt b/app/src/main/java/com/gotcha/llm/LLMClient.kt
index bdfe85c..cce1763 100644
--- a/app/src/main/java/com/gotcha/llm/LLMClient.kt
+++ b/app/src/main/java/com/gotcha/llm/LLMClient.kt
@@ -1,6 +1,8 @@
package com.gotcha.llm
import android.content.Context
+import com.gotcha.audio.AudioModel
+import com.gotcha.audio.ModelCategory
import com.gotcha.i18n.Language
import com.jakewharton.retrofit2.converter.kotlinx.serialization.asConverterFactory
import kotlinx.serialization.builtins.ListSerializer
@@ -135,7 +137,20 @@ class LLMClient(
suspend fun listModels(): Result<List<String>> = runCatching {
val response = apiService.listModels()
- response.data.map { it.id }.sorted()
+ response.data
+ .mapNotNull { info ->
+ val category = AudioModel.categorize(info.id, info.task, info.providerType)
+ // Keep anything the server did not flag as audio. UNKNOWN covers
+ // servers that omit the hint field entirely (e.g. OpenAI's
+ // /v1/models); LLM is the explicit declaration; TTS/STT are
+ // dropped so they never reach the chat-model dropdown.
+ if (category == ModelCategory.TTS || category == ModelCategory.STT) {
+ null
+ } else {
+ info.id
+ }
+ }
+ .sorted()
}
private fun buildCacheKey(
diff --git a/app/src/main/java/com/gotcha/llm/Models.kt b/app/src/main/java/com/gotcha/llm/Models.kt
index 09c5e06..c01dbf4 100644
--- a/app/src/main/java/com/gotcha/llm/Models.kt
+++ b/app/src/main/java/com/gotcha/llm/Models.kt
@@ -138,5 +138,10 @@ data class ModelInfo(
@SerialName("object")
val objectType: String = "model",
@SerialName("owned_by")
- val ownedBy: String = ""
+ val ownedBy: String = "",
+ /** OpenAI-style task hint (e.g. "text-generation", "text-to-speech"). */
+ val task: String? = null,
+ /** Samosa-style provider hint (e.g. "llm", "tts", "stt"). */
+ @SerialName("provider_type")
+ val providerType: String? = null
)
diff --git a/app/src/test/java/com/gotcha/audio/AudioApiTest.kt b/app/src/test/java/com/gotcha/audio/AudioApiTest.kt
index a37a122..04d1ab9 100644
--- a/app/src/test/java/com/gotcha/audio/AudioApiTest.kt
+++ b/app/src/test/java/com/gotcha/audio/AudioApiTest.kt
@@ -128,6 +128,27 @@ class AudioApiTest {
assertEquals(0, calls)
}
+ @Test
+ fun `listAudioModels parses provider_type as a category hint`() {
+ val jsonResponse = """
+ {
+ "data": [
+ {"id": "kokoro-82m", "provider_type": "tts", "voices": ["af_heart"]},
+ {"id": "whisper-1", "provider_type": "stt"},
+ {"id": "my-llm", "provider_type": "llm"}
+ ]
+ }
+ """.trimIndent()
+
+ server.enqueue(MockResponse().setResponseCode(200).setBody(jsonResponse))
+
+ val models = audioApi.listAudioModels()
+ assertEquals(3, models.size)
+ assertEquals(ModelCategory.TTS, models[0].category)
+ assertEquals(ModelCategory.STT, models[1].category)
+ assertEquals(ModelCategory.LLM, models[2].category)
+ }
+
@Test
fun `401 on transcribe also fires onUnauthorized`() {
var calls = 0
diff --git a/app/src/test/java/com/gotcha/audio/AudioModelTest.kt b/app/src/test/java/com/gotcha/audio/AudioModelTest.kt
index 0768850..5b389cd 100644
--- a/app/src/test/java/com/gotcha/audio/AudioModelTest.kt
+++ b/app/src/test/java/com/gotcha/audio/AudioModelTest.kt
@@ -48,6 +48,29 @@ class AudioModelTest {
assertEquals(ModelCategory.UNKNOWN, AudioModel.categorize("llama-3-8b"))
}
+ @Test
+ fun `provider_type field is honored for audio hints`() {
+ assertEquals(ModelCategory.TTS, AudioModel.categorize("m", task = null, providerType = "tts"))
+ assertEquals(ModelCategory.STT, AudioModel.categorize("m", task = null, providerType = "STT"))
+ assertEquals(ModelCategory.LLM, AudioModel.categorize("m", task = null, providerType = "llm"))
+ }
+
+ @Test
+ fun `task field is honored for LLM hints`() {
+ assertEquals(ModelCategory.LLM, AudioModel.categorize("m", task = "text-generation"))
+ assertEquals(ModelCategory.LLM, AudioModel.categorize("m", task = "chat-completion"))
+ assertEquals(ModelCategory.LLM, AudioModel.categorize("m", task = "chat"))
+ assertEquals(ModelCategory.LLM, AudioModel.categorize("m", task = "language-model"))
+ }
+
+ @Test
+ fun `provider_type overrides name heuristics`() {
+ // Name says TTS (kokoro) but provider_type says LLM — provider_type wins.
+ assertEquals(ModelCategory.LLM, AudioModel.categorize("kokoro-82m", task = null, providerType = "llm"))
+ // Name says STT (whisper) but provider_type says TTS — provider_type wins.
+ assertEquals(ModelCategory.TTS, AudioModel.categorize("whisper-1", task = null, providerType = "tts"))
+ }
+
@Test
fun `default voice falls back to af_heart when no voices are listed`() {
val model = AudioModel(id = "kokoro-82m", category = ModelCategory.TTS)
diff --git a/app/src/test/java/com/gotcha/llm/LLMClientTest.kt b/app/src/test/java/com/gotcha/llm/LLMClientTest.kt
index 50394d5..cf2008b 100644
--- a/app/src/test/java/com/gotcha/llm/LLMClientTest.kt
+++ b/app/src/test/java/com/gotcha/llm/LLMClientTest.kt
@@ -163,4 +163,115 @@ class LLMClientTest {
assertTrue(body.contains("Never translate"))
assertTrue(body.contains("<dictation>"))
}
+
+ @Test
+ fun `listModels filters out audio models by provider_type`() = runTest {
+ server.enqueue(
+ MockResponse().setBody(
+ """
+ {
+ "data": [
+ {"id": "gpt-4o", "provider_type": "llm"},
+ {"id": "kokoro-82m", "provider_type": "tts"},
+ {"id": "whisper-1", "provider_type": "stt"},
+ {"id": "claude-opus-4", "provider_type": "llm"}
+ ]
+ }
+ """.trimIndent()
+ ).setHeader("Content-Type", "application/json")
+ )
+
+ val result = client.listModels().getOrThrow()
+
+ assertEquals(listOf("claude-opus-4", "gpt-4o"), result)
+ }
+
+ @Test
+ fun `listModels filters out audio models by task field`() = runTest {
+ server.enqueue(
+ MockResponse().setBody(
+ """
+ {
+ "data": [
+ {"id": "gpt-4o-mini"},
+ {"id": "tts-1", "task": "text-to-speech"},
+ {"id": "whisper-large-v3", "task": "automatic-speech-recognition"}
+ ]
+ }
+ """.trimIndent()
+ ).setHeader("Content-Type", "application/json")
+ )
+
+ val result = client.listModels().getOrThrow()
+
+ assertEquals(listOf("gpt-4o-mini"), result)
+ }
+
+ @Test
+ fun `listModels falls back to name heuristics when no hint field is present`() = runTest {
+ // OpenAI's /v1/models omits both `provider_type` and `task`; the name
+ // heuristic is the last line of defence.
+ server.enqueue(
+ MockResponse().setBody(
+ """
+ {
+ "data": [
+ {"id": "gpt-4o"},
+ {"id": "tts-1"},
+ {"id": "whisper-1"},
+ {"id": "llama-3-8b-instruct"}
+ ]
+ }
+ """.trimIndent()
+ ).setHeader("Content-Type", "application/json")
+ )
+
+ val result = client.listModels().getOrThrow()
+
+ assertEquals(listOf("gpt-4o", "llama-3-8b-instruct"), result)
+ }
+
+ @Test
+ fun `listModels keeps explicitly tagged LLM models alongside UNKNOWN ones`() = runTest {
+ server.enqueue(
+ MockResponse().setBody(
+ """
+ {
+ "data": [
+ {"id": "samosa-llm-pro", "provider_type": "llm"},
+ {"id": "gpt-4o"},
+ {"id": "kokoro-82m", "provider_type": "tts"}
+ ]
+ }
+ """.trimIndent()
+ ).setHeader("Content-Type", "application/json")
+ )
+
+ val result = client.listModels().getOrThrow()
+
+ assertEquals(listOf("gpt-4o", "samosa-llm-pro"), result)
+ }
+
+ @Test
+ fun `listModels keeps UNKNOWN-categorized models with no hint and no name match`() = runTest {
+ // Custom / private model with neither provider_type nor task field, and
+ // an id that triggers no audio name heuristic — must survive filtering.
+ server.enqueue(
+ MockResponse().setBody(
+ """
+ {
+ "data": [
+ {"id": "my-private-llm"},
+ {"id": "tts-1"},
+ {"id": "whisper-large-v3"}
+ ]
+ }
+ """.trimIndent()
+ ).setHeader("Content-Type", "application/json")
+ )
+
+ val result = client.listModels().getOrThrow()
+
+ assertEquals(listOf("my-private-llm"), result)
+ }
}
diff --git a/app/src/test/java/com/gotcha/llm/ModelsTest.kt b/app/src/test/java/com/gotcha/llm/ModelsTest.kt
index 2b6a2d6..12f694c 100644
--- a/app/src/test/java/com/gotcha/llm/ModelsTest.kt
+++ b/app/src/test/java/com/gotcha/llm/ModelsTest.kt
@@ -170,4 +170,26 @@ class ModelsTest {
assertEquals(original, decoded)
assertEquals(2, decoded.content!!.jsonArray.size)
}
+
+ @Test
+ fun `ModelInfo parses provider_type and task hints`() {
+ val decoded = json.decodeFromString(
+ ModelListResponse.serializer(),
+ """
+ {
+ "data": [
+ {"id": "gpt-4o", "provider_type": "llm"},
+ {"id": "kokoro-82m", "task": "text-to-speech"},
+ {"id": "plain-model"}
+ ]
+ }
+ """.trimIndent()
+ )
+ assertEquals(3, decoded.data.size)
+ assertEquals("gpt-4o", decoded.data[0].id)
+ assertEquals("llm", decoded.data[0].providerType)
+ assertEquals("text-to-speech", decoded.data[1].task)
+ assertNull(decoded.data[2].providerType)
+ assertNull(decoded.data[2].task)
+ }
}