Settings: the assistant's model, and it holds for every new chat
CI / check (push) Canceled after 0s

Settings › Assistant › Assistant model: "Instance default" or one of the
instance's models, saved as the existing assistantModel preference.

The controller keeps the model for new chats apart from the model of the
chat the sheet is in: New chat after "Continue the last chat" goes back to
it, a change in Settings reaches a sheet that is still alive, and a question
asked before the model list was read waits for it (and falls back to the
saved choice, not to the instance's default, when the list cannot be read).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
HomerandClaude Fable 5.2 committed 2026-11-13 14:17:27 +00:00
1 parent 2f43b94e56
commit 7ca44ef5e2
17 files changed
+212 -6

No files matched your search

@@ -28,7 +28,10 @@ import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.drop
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.transformWhile
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.isActive
@@ -142,11 +145,29 @@ class AssistController(private val context: Context, private val scope: Coroutin
private var usingPhone = false
private var screenText: String? = null
private var stt: Boolean? = null
/** The account's own default model, as the instance said it: what "Instance default" means. */
private var accountDefault: String? = null
/**
* The model a new chat starts on (Settings › Assistant › Assistant model). The sheet's
* [AssistUiState.modelId] is the model of the chat it is in, which for a continued chat is
* that chat's own; a new chat goes back to this one.
*/
private var newChatModel: String? = null
private val ready: Job
init {
ready = scope.launch { load() }
scope.launch { speaker.playing.collect { on -> _state.update { it.copy(speaking = on) } } }
// Settings changed the assistant's model while this sheet lives: the next new chat has it.
scope.launch {
context.lembas.instances.prefs.map { it.assistantModel }.distinctUntilChanged().drop(1).collect { chosen ->
ready.join()
newChatModel = assistantModelFor(chosen, accountDefault, _state.value.models) ?: chosen.ifBlank { null }
_state.update { if (it.chatId == null) it.copy(modelId = newChatModel) else it }
}
}
}
/** The instance, its models and what it offers this account; the choices kept in Settings. */
@@ -164,15 +185,17 @@ class AssistController(private val context: Context, private val scope: Coroutin
?.let { id -> LastAssist(id, prefs.lastAssistTitle, prefs.lastAssistAt) },
)
}
// Until the instance has said what it has, the choice in Settings stands as it is: a
// question asked while the list could not be read still goes to the chosen model.
newChatModel = prefs.assistantModel.ifBlank { null }
val api = instance?.let { store.clientFor(it) } ?: return
try {
val me = api.me()
val models = api.models()
stt = me.speechToText
val chosen = prefs.assistantModel.takeIf { id -> models.any { it.id == id } }
?: me.defaultModel?.takeIf { id -> models.any { it.id == id } }
?: models.firstOrNull { it.default }?.id
?: models.firstOrNull()?.id
accountDefault = me.defaultModel
val chosen = assistantModelFor(prefs.assistantModel, me.defaultModel, models)
newChatModel = chosen
_state.update {
it.copy(
connection = Connection.Online,
@@ -428,6 +451,8 @@ class AssistController(private val context: Context, private val scope: Coroutin
val store = context.lembas.instances
val api = store.client() ?: throw ApiException(401, "Not signed in.")
val prefs = store.prefs.first()
// A question typed the moment the sheet opened: the model is known first.
ready.join()
val now = _state.value
val withScreen = now.includeScreen
val attachments = buildList {
@@ -456,7 +481,7 @@ class AssistController(private val context: Context, private val scope: Coroutin
inChat = chatId
replyId = sent.reply?.id ?: return@launch
} else {
val made = api.createChat(text, attachments, model = now.modelId, temporary = prefs.assistantTemporary)
val made = api.createChat(text, attachments, model = now.modelId ?: newChatModel, temporary = prefs.assistantTemporary)
inChat = made.chat.id
replyId = made.reply?.id ?: return@launch
_state.update { it.copy(chatId = inChat) }
@@ -560,11 +585,13 @@ class AssistController(private val context: Context, private val scope: Coroutin
override fun newChat() {
following?.cancel()
speaker.stop()
_state.update { it.copy(chatId = null, turns = emptyList(), error = null, busy = false, conversation = false) }
// And on the assistant's own model again, not the one of the chat that was continued.
_state.update { it.copy(chatId = null, turns = emptyList(), error = null, busy = false, conversation = false, modelId = newChatModel ?: it.modelId) }
}
/** The model: for the chat this sheet is in, changed at once; for a new one, kept for next time. */
override fun pickModel(id: String) {
newChatModel = id
_state.update { it.copy(modelId = id) }
scope.launch {
context.lembas.instances.savePrefs { it.copy(assistantModel = id) }
@@ -637,6 +664,16 @@ class AssistController(private val context: Context, private val scope: Coroutin
private const val TICK_MS = EndOfSpeech.TICK_MS
private const val PARTIAL_EVERY_MS = 1_200L
/**
* The model a new assistant chat starts on: the one [chosen] in Settings while the
* instance still has it; else ("Instance default", or a model that is gone) the
* account's default, the instance's, or its first.
*/
fun assistantModelFor(chosen: String, accountDefault: String?, models: List<ModelInfo>): String? {
val has = { id: String? -> !id.isNullOrBlank() && models.any { it.id == id } }
return chosen.takeIf(has) ?: accountDefault?.takeIf(has) ?: models.firstOrNull { it.default }?.id ?: models.firstOrNull()?.id
}
/** A reply's words, for a voice: its text steps, without the markup. */
fun spokenWords(steps: List<Step>): String = plain(steps.filter { it.kind == "text" }.joinToString("\n\n") { it.markdown.orEmpty() })
@@ -48,6 +48,7 @@ import androidx.compose.material.icons.filled.Notifications
import androidx.compose.material.icons.filled.NotificationsActive
import androidx.compose.material.icons.filled.Palette
import androidx.compose.material.icons.filled.PhoneAndroid
import androidx.compose.material.icons.filled.Psychology
import androidx.compose.material.icons.filled.RecordVoiceOver
import androidx.compose.material.icons.filled.Science
import androidx.compose.material.icons.filled.ScreenLockPortrait
@@ -93,6 +94,7 @@ import eu.llembas.android.AppLanguage
import eu.llembas.android.BuildConfig
import eu.llembas.android.R
import eu.llembas.android.actions.NavApp
import eu.llembas.android.api.ModelInfo
import eu.llembas.android.assist.LembasAssistService
import eu.llembas.android.assist.PhoneRecognizer
import eu.llembas.android.auth.Instance
@@ -122,6 +124,7 @@ import eu.llembas.android.wake.WakeWordService
import eu.llembas.android.wake.WakeWords
import eu.llembas.android.wake.wakeAttemptWords
import eu.llembas.android.wake.wakeTitle
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
@@ -136,6 +139,7 @@ interface SettingsActions {
fun notificationSettings() {}
fun assistantSettings() {}
fun assistantTemporary(on: Boolean) {}
fun assistantModel(id: String) {}
fun voiceInput(value: String) {}
fun assistantListens(on: Boolean) {}
fun assistantConversation(on: Boolean) {}
@@ -195,6 +199,8 @@ data class SettingsState(
val notificationsBuild: Boolean = false,
/** The navigation apps installed: NavApp ids. */
val navApps: List<String> = emptyList(),
/** The instance's models, for Settings › Assistant › Assistant model; empty until (or unless) it said them. */
val assistantModels: List<ModelInfo> = emptyList(),
)
@Composable
@@ -218,6 +224,18 @@ fun SettingsScreen(onBack: () -> Unit, onAddInstance: () -> Unit) {
}
clock = SystemClock.elapsedRealtime()
}
// The instance's models, for the assistant's: asked for each time Settings opens on an instance.
var models by remember { mutableStateOf(emptyList<ModelInfo>()) }
LaunchedEffect(active?.id) {
models = try {
store.client()?.models().orEmpty()
} catch (e: CancellationException) {
throw e
} catch (e: Exception) {
// Not reachable now: the row keeps what was chosen and offers the instance's default.
emptyList()
}
}
var pushNote by remember { mutableStateOf<String?>(null) }
var updateNote by remember { mutableStateOf<String?>(null) }
var updateFound by remember { mutableStateOf<UpdateOffer?>(null) }
@@ -279,6 +297,10 @@ fun SettingsScreen(onBack: () -> Unit, onAddInstance: () -> Unit) {
scope.launch { store.savePrefs { it.copy(assistantTemporary = on) } }
}
override fun assistantModel(id: String) {
scope.launch { store.savePrefs { it.copy(assistantModel = id) } }
}
override fun voiceInput(value: String) {
scope.launch { store.savePrefs { it.copy(voiceInput = value) } }
}
@@ -420,6 +442,7 @@ fun SettingsScreen(onBack: () -> Unit, onAddInstance: () -> Unit) {
notificationAccess = NotificationAccess.granted(context),
notificationsBuild = NotificationAccess.AVAILABLE,
navApps = remember { NavApp.installedApps(context).map { it.id } },
assistantModels = models,
),
actions,
onBack,
@@ -444,6 +467,15 @@ fun SettingsContent(state: SettingsState, actions: SettingsActions, onBack: () -
})
}
}
var choosingModel by remember { mutableStateOf(false) }
if (choosingModel) {
ModalBottomSheet(onDismissRequest = { choosingModel = false }, sheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true)) {
AssistantModelChoices(state, onPick = {
choosingModel = false
actions.assistantModel(it)
})
}
}
if (choosingLanguage) {
ModalBottomSheet(onDismissRequest = { choosingLanguage = false }, sheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true)) {
LanguageChoices(state.prefs, onPick = {
@@ -567,6 +599,15 @@ fun SettingsContent(state: SettingsState, actions: SettingsActions, onBack: () -
onClick = actions::assistantSettings,
)
Rule()
// The model the assistant's new chats start on: the instance's default may be
// a large one, and a spoken question wants a quick answer.
SettingRow(
Icons.Filled.Psychology,
stringResource(R.string.assistant_model),
subtitle = assistantModelLabel(state),
onClick = { choosingModel = true },
)
Rule()
SettingRow(
Icons.Filled.Timer,
stringResource(R.string.assistant_temporary),
@@ -1109,6 +1150,40 @@ fun WakeChoices(which: WakeChoice, prefs: Prefs, onPick: ((SettingsActions) -> U
}
}
/**
* What the assistant's model row says: the chosen model by its name, or "Instance default" --
* also for a model the instance no longer has, since that is what the assistant then uses.
*/
@Composable
private fun assistantModelLabel(state: SettingsState): String {
val chosen = state.prefs.assistantModel
val model = state.assistantModels.firstOrNull { it.id == chosen }
// While the list is not read yet, or could not be: the choice as it was saved.
return when {
chosen.isBlank() -> stringResource(R.string.assistant_model_default)
model != null -> model.name.ifBlank { model.id }
state.assistantModels.isEmpty() -> chosen
else -> stringResource(R.string.assistant_model_default)
}
}
/**
* Settings › Assistant › Assistant model, as a sheet: "Instance default" (the account's default
* model, as in a new chat in the app), then the instance's models. The choice is the model a new
* assistant chat starts on, from every way in -- the sheet, the wake word, Share.
*/
@Composable
fun AssistantModelChoices(state: SettingsState, onPick: (String) -> Unit) {
val chosen = state.prefs.assistantModel.takeIf { id -> state.assistantModels.any { it.id == id } }.orEmpty()
val choices = listOf(
Choice(INSTANCE_DEFAULT, stringResource(R.string.assistant_model_default), stringResource(R.string.assistant_model_default_line), chosen.isEmpty()),
) + state.assistantModels.map { Choice(it.id, it.name.ifBlank { it.id }, null, it.id == chosen) }
ChoiceList(stringResource(R.string.assistant_model), choices) { id -> onPick(if (id == INSTANCE_DEFAULT) "" else id) }
}
/** The list's key for "Instance default": a key cannot be empty, and no model is named so. */
private const val INSTANCE_DEFAULT = " default"
private data class Choice(val id: String, val title: String, val subtitle: String?, val chosen: Boolean)
/** A sheet's list of choices: a title, then rows of words that give and a radio button that keeps its size. */