diff --git a/CHANGELOG.md b/CHANGELOG.md index 707cbf3..3628510 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,15 @@ is in alpha and releases come off `main`. ## [Unreleased] +### Fixed + +- **A chat on a model two connections serve shows the right connection.** When an instance has + the same model on two connections, a chat, a reply and a scheduled task on the second one were + shown as being on the first: the title, the tick in the model list, the picture, the thinking + control. The instance now says which connection each is on, and the app shows that one. The + line under a reply and a scheduled task's model name the connection where two share a name. + An instance that does not say it yet is shown as before. + ## [1.0.0-beta.12] — 2026-11-18 For what is new here the instance must be newer than LLeMbas 2.0.0-beta.14; an older one ignores diff --git a/app/src/main/java/eu/llembas/android/api/Models.kt b/app/src/main/java/eu/llembas/android/api/Models.kt index 42beebf..83c46b4 100644 --- a/app/src/main/java/eu/llembas/android/api/Models.kt +++ b/app/src/main/java/eu/llembas/android/api/Models.kt @@ -175,6 +175,28 @@ fun List.named(name: String?): ModelInfo? { return firstOrNull { it.ref == name } ?: firstOrNull { it.id == name } } +/** + * The model a chat, a reply or a schedule is on, in a list. The server says the id (`model`) and, + * where it knows it, which connection serves it (`model_ref`). With a [ref] the model is the one + * with that ref and no other: one that is not in the list is no model here, never "the first + * with that id" -- that would name the wrong connection. Without one (an older server, or a + * connection that is gone) the bare [id] is all there is. + */ +fun List.serving(ref: String?, id: String?): ModelInfo? = if (ref.isNullOrBlank()) named(id) else firstOrNull { it.ref == ref } + +/** What such a thing's model is kept and sent by: its [ref] where the server says one, else its [id]; null for neither. */ +fun modelKey(ref: String?, id: String?): String? = ref?.ifBlank { null } ?: id?.ifBlank { null } + +/** + * The quiet line under a reply: the model's [id] as before, and its connection only where the + * reply says which one ([ref]) and another model here shares the id. Null when there is no id. + */ +fun List.replyModel(ref: String?, id: String?): String? { + val said = id?.ifBlank { null } ?: return null + val model = firstOrNull { !ref.isNullOrBlank() && it.ref == ref } ?: return said + return twinTitle(said, model.connection, count { it.id == model.id } > 1) +} + /** A model's name in a list of [all]: with its connection where another model there shares its id. */ fun ModelInfo.titleAmong(all: List): String = twinTitle(name.ifBlank { id }, connection, all.count { it.id == id } > 1) @@ -352,6 +374,8 @@ data class Chat( val kind: String = "chat", val title: String = "", val model: String = "", + /** Which connection serves [model] (`provider/model`); null from an older server, or when that connection is gone. */ + @SerialName("model_ref") val modelRef: String? = null, @SerialName("folder_id") val folderId: String? = null, val pinned: Boolean = false, val archived: Boolean = false, @@ -379,6 +403,9 @@ data class Chat( ) { val isAgent: Boolean get() = kind == "agent" + /** The chat's model as it is looked up and sent: its ref, else its id. */ + val modelKey: String? get() = modelKey(modelRef, model) + fun allows(control: String): Boolean = controls?.contains(control) ?: true } @@ -624,6 +651,8 @@ data class Message( val machine: Boolean = false, val origin: String = "", val model: String? = null, + /** Which connection wrote this reply (see [Chat.modelRef]). */ + @SerialName("model_ref") val modelRef: String? = null, val attachments: List = emptyList(), val text: String? = null, val steps: List = emptyList(), diff --git a/app/src/main/java/eu/llembas/android/api/Schedules.kt b/app/src/main/java/eu/llembas/android/api/Schedules.kt index 91c1f42..b183fbd 100644 --- a/app/src/main/java/eu/llembas/android/api/Schedules.kt +++ b/app/src/main/java/eu/llembas/android/api/Schedules.kt @@ -23,6 +23,8 @@ data class RunModel( val name: String? = null, val reason: String? = null, @SerialName("switched_from") val switchedFrom: String? = null, + /** Which connection served the run (`provider/model`); null from an older server. */ + val ref: String? = null, ) { /** What to call it: its name, else its id. */ val label: String? get() = name ?: id @@ -115,6 +117,8 @@ data class Schedule( @SerialName("catch_up") val catchUp: String = "", @SerialName("grace_minutes") val graceMinutes: Int? = null, val model: String? = null, + /** Which connection serves [model] (`provider/model`); null from an older server, or with no model of its own. */ + @SerialName("model_ref") val modelRef: String? = null, val effort: String? = null, val agents: Int? = null, val tools: List = emptyList(), @@ -132,6 +136,9 @@ data class Schedule( @SerialName("created_at") val createdAt: String? = null, ) { val paused: Boolean get() = !enabled + + /** The schedule's model as a form holds and sends it: its ref, else its id. */ + val modelKey: String? get() = modelKey(modelRef, model) } @Serializable @@ -210,6 +217,17 @@ fun List.named(name: String?): SchedulingModel? { return firstOrNull { it.ref == name } ?: firstOrNull { it.id == name } } +/** + * What a schedule's or a run's model is called among [this] list: [said] (the id or the name + * the server gave), and its connection where the server says which one ([ref]) and another + * model in the list shares the id. A ref the list does not have leaves [said] as it is. + */ +fun List.saidWithConnection(ref: String?, said: String?): String? { + val word = said?.ifBlank { null } ?: return null + val model = firstOrNull { !ref.isNullOrBlank() && it.ref == ref } ?: return word + return twinTitle(word, model.connection, count { it.id == model.id } > 1) +} + /** A scheduling model's name in a list of [all]: with its connection where another there shares its id. */ fun SchedulingModel.titleAmong(all: List): String = twinTitle(name.ifBlank { id }, connection, all.count { it.id == id } > 1) diff --git a/app/src/main/java/eu/llembas/android/assist/AssistController.kt b/app/src/main/java/eu/llembas/android/assist/AssistController.kt index e0fa5e0..0048676 100644 --- a/app/src/main/java/eu/llembas/android/assist/AssistController.kt +++ b/app/src/main/java/eu/llembas/android/assist/AssistController.kt @@ -715,7 +715,7 @@ class AssistController( val question = detail.messages.lastOrNull { it.isUser } val answer = detail.messages.lastOrNull { !it.isUser } val turns = if (question != null) listOf(AssistTurn(question.text.orEmpty(), earlier = answer?.steps.orEmpty())) else emptyList() - _state.update { it.copy(chatId = last.chatId, turns = turns, modelId = detail.chat.model.ifBlank { null } ?: it.modelId, last = null) } + _state.update { it.copy(chatId = last.chatId, turns = turns, modelId = detail.chat.modelKey ?: it.modelId, last = null) } } catch (e: Exception) { // A chat deleted since is simply not offered again. if (e is ApiException && e.status == 404) { diff --git a/app/src/main/java/eu/llembas/android/ui/chat/ChatScreen.kt b/app/src/main/java/eu/llembas/android/ui/chat/ChatScreen.kt index f8c0d56..dcb60a7 100644 --- a/app/src/main/java/eu/llembas/android/ui/chat/ChatScreen.kt +++ b/app/src/main/java/eu/llembas/android/ui/chat/ChatScreen.kt @@ -288,7 +288,7 @@ fun ChatContent( title = { ModelTitle( model = info, - fallback = state.modelId ?: stringResource(R.string.app_name), + fallback = state.modelWord ?: stringResource(R.string.app_name), // A report's conversation says which report: by what it was entered with, else by // what the chat itself says (after the app was closed in between). subtitle = if (report != null || chat?.report != null) { @@ -423,6 +423,7 @@ fun ChatContent( canRegenerate = live == null && message == rows.lastOrNull(), onRegenerate = { actions.regenerate(message) }, onShow = onShow, + models = state.models, ) } } @@ -565,7 +566,7 @@ private fun Greeting( } TextButton(onClick = onTune) { Text(where, textAlign = TextAlign.Center, color = palette.inkMuted) } } else { - ModelAvatar(state.modelInfo, state.modelId ?: "?", size = 64.dp) + ModelAvatar(state.modelInfo, state.modelWord ?: "?", size = 64.dp) Spacer(Modifier.height(20.dp)) Text(stringResource(R.string.empty_chat), style = MaterialTheme.typography.headlineSmall, textAlign = TextAlign.Center) } 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 8f8fff9..f22b173 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 @@ -35,6 +35,7 @@ import eu.llembas.android.api.StreamClient import eu.llembas.android.api.SubagentTree import eu.llembas.android.api.Turn import eu.llembas.android.api.named +import eu.llembas.android.api.serving import eu.llembas.android.lembas import eu.llembas.android.ui.explain import kotlinx.coroutines.CancellationException @@ -131,12 +132,14 @@ val ChatState.modelId: String? get() = chatModel ?: draft.model ?: folderModel ?: models.firstOrNull { it.default }?.key ?: models.firstOrNull()?.key /** - * An open chat's model. The chat says its model's id, which two connections may share; the one - * picked here ([ChatState.picked], a ref) says which of them, for as long as the chat is still - * on a model of that id. + * An open chat's model: the connection the chat itself names (`model_ref`), where the server + * says it. From an older server the chat says only its model's id, which two connections may + * share; the one picked here ([ChatState.picked], a ref) then says which of them, for as long + * as the chat is still on a model of that id. */ val ChatState.chatModel: String? get() { + chat?.modelRef?.ifBlank { null }?.let { return it } val served = chat?.model?.ifBlank { null } ?: return null return picked?.takeIf { models.named(it)?.id == served } ?: served } @@ -145,7 +148,16 @@ val ChatState.chatModel: String? val ChatState.folderModel: String? get() = start?.takeIf { chat == null }?.let { models.named(it.key) ?: models.named(it.model) }?.key -val ChatState.modelInfo: ModelInfo? get() = models.named(modelId) +/** The model in force, among the models offered: a chat that names its connection is on that one or on none of them. */ +val ChatState.modelInfo: ModelInfo? + get() { + val ref = chat?.modelRef?.ifBlank { null } ?: return models.named(modelId) + return models.serving(ref, chat?.model) + } + +/** What the model in force is called where the list does not have it: the chat's own id for it, not its ref. */ +val ChatState.modelWord: String? + get() = chat?.takeIf { !it.modelRef.isNullOrBlank() }?.model?.ifBlank { null } ?: modelId /** * Which chat a dictation or a reply read aloud is for: this chat once it exists, else the model diff --git a/app/src/main/java/eu/llembas/android/ui/chat/Transcript.kt b/app/src/main/java/eu/llembas/android/ui/chat/Transcript.kt index 2110173..a862d4e 100644 --- a/app/src/main/java/eu/llembas/android/ui/chat/Transcript.kt +++ b/app/src/main/java/eu/llembas/android/ui/chat/Transcript.kt @@ -84,6 +84,7 @@ import androidx.compose.ui.unit.sp import eu.llembas.android.R import eu.llembas.android.api.Attachment import eu.llembas.android.api.Message +import eu.llembas.android.api.ModelInfo import eu.llembas.android.api.ShowCard import eu.llembas.android.api.Step import eu.llembas.android.api.ToolCall @@ -206,6 +207,8 @@ fun AssistantMessage( onRegenerate: () -> Unit, onShow: ((ShowCard) -> Unit)?, modifier: Modifier = Modifier, + /** The models offered here: a reply by one of two that share an id says its connection. */ + models: List = emptyList(), ) { val palette = LocalPalette.current val copy = LocalCopy.current @@ -292,7 +295,7 @@ fun AssistantMessage( ) } } - footLine(message)?.let { line -> + footLine(message, models)?.let { line -> Text( line, style = MaterialTheme.typography.labelSmall, @@ -334,11 +337,12 @@ fun replyText(message: Message): String = pageProjection( private const val PAGE_UNTITLED = "Page" /** What a finished reply cost, in one quiet line: model, tokens, speed. Null when nothing was counted. */ -private fun footLine(message: Message): String? { - val m = message.metrics ?: return message.model - if (m.completionTokens == 0 && m.elapsedMs == 0L) return message.model +internal fun footLine(message: Message, models: List = emptyList()): String? { + val model = models.replyModel(message.modelRef, message.model) + val m = message.metrics ?: return model + if (m.completionTokens == 0 && m.elapsedMs == 0L) return model return buildList { - message.model?.let { add(it) } + model?.let { add(it) } if (m.completionTokens > 0) add("${if (m.estimated) "~" else ""}${m.completionTokens} tok") if (m.tokPerSecond > 0) add("${"%.1f".format(m.tokPerSecond)} tok/s") }.joinToString(" · ").ifBlank { null } diff --git a/app/src/main/java/eu/llembas/android/ui/scheduled/RunScreen.kt b/app/src/main/java/eu/llembas/android/ui/scheduled/RunScreen.kt index fb30f79..16e8fa6 100644 --- a/app/src/main/java/eu/llembas/android/ui/scheduled/RunScreen.kt +++ b/app/src/main/java/eu/llembas/android/ui/scheduled/RunScreen.kt @@ -39,6 +39,7 @@ import androidx.lifecycle.viewmodel.compose.viewModel import eu.llembas.android.R import eu.llembas.android.api.ApiException import eu.llembas.android.api.RunResult +import eu.llembas.android.api.SchedulingModel import eu.llembas.android.lembas import eu.llembas.android.ui.chat.LocalCopy import eu.llembas.android.ui.chat.RichMarkdown @@ -49,7 +50,14 @@ import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch -data class RunState(val title: String = "", val result: RunResult? = null, val loading: Boolean = false, val error: String? = null) +data class RunState( + val title: String = "", + val result: RunResult? = null, + val loading: Boolean = false, + val error: String? = null, + /** The models scheduled work may run on, for naming the run's by its connection. */ + val models: List = emptyList(), +) class RunViewModel(app: Application) : AndroidViewModel(app) { private val _state = MutableStateFlow(RunState()) @@ -64,6 +72,7 @@ class RunViewModel(app: Application) : AndroidViewModel(app) { val result = api.scheduleRun(schedule, run) val title = runCatching { api.schedule(schedule).title }.getOrDefault("") _state.update { it.copy(result = result, title = title, loading = false) } + runCatching { api.schedulingModels() }.onSuccess { m -> _state.update { it.copy(models = m.models) } } } catch (e: Exception) { _state.update { it.copy(loading = false, error = getApplication().explain(e)) } } @@ -128,7 +137,7 @@ fun RunContent(state: RunState, onBack: () -> Unit = {}, onOpenChat: (String) -> modifier = Modifier.weight(1f), ) } - RunFacts(run, withPreview = false) + RunFacts(run, withPreview = false, models = state.models) // What the run filed for the person, when it filed anything. run.reportId?.let { report -> ReportLink(onClick = { onOpenReport(report) }) } } diff --git a/app/src/main/java/eu/llembas/android/ui/scheduled/ScheduledScreens.kt b/app/src/main/java/eu/llembas/android/ui/scheduled/ScheduledScreens.kt index 2ec1a00..4e0efdf 100644 --- a/app/src/main/java/eu/llembas/android/ui/scheduled/ScheduledScreens.kt +++ b/app/src/main/java/eu/llembas/android/ui/scheduled/ScheduledScreens.kt @@ -102,6 +102,7 @@ import eu.llembas.android.api.agentic import eu.llembas.android.api.computerAway import eu.llembas.android.api.named import eu.llembas.android.api.notifyChoice +import eu.llembas.android.api.saidWithConnection import eu.llembas.android.api.titleAmong import eu.llembas.android.lembas import eu.llembas.android.ui.DirectoryPicker @@ -381,6 +382,8 @@ data class ScheduleState( val loading: Boolean = false, val error: String? = null, val notice: String? = null, + /** The models scheduled work may run on: what tells two with one id apart by their connection. */ + val models: List = emptyList(), ) class ScheduleViewModel(app: Application) : AndroidViewModel(app) { @@ -397,6 +400,7 @@ class ScheduleViewModel(app: Application) : AndroidViewModel(app) { val schedule = api.schedule(id) val runs = runCatching { api.scheduleRuns(id) }.getOrNull() _state.update { it.copy(schedule = schedule, runs = runs?.runs.orEmpty(), more = runs?.more == true, loading = false) } + runCatching { api.schedulingModels() }.onSuccess { m -> _state.update { it.copy(models = m.models) } } } catch (e: Exception) { _state.update { it.copy(loading = false, error = getApplication().explain(e)) } } @@ -563,7 +567,7 @@ private fun ScheduleBody( // An Agentic schedule with no model of its own runs on the folder's usual one. stringResource( R.string.schedule_model, - schedule.model?.takeIf { it.isNotBlank() } + state.models.saidWithConnection(schedule.modelRef, schedule.model) ?: stringResource(if (schedule.agentic) R.string.schedule_folder_model else R.string.schedule_first_free_model), ), modeWords(schedule.mode), @@ -628,7 +632,9 @@ private fun ScheduleBody( ) } } - items(state.runs, key = { it.id }) { run -> RunRow(run, schedule.runsOn, onOpen = { onOpenRun(run) }, onOpenReport = onOpenReport) } + items(state.runs, key = { it.id }) { run -> + RunRow(run, schedule.runsOn, onOpen = { onOpenRun(run) }, onOpenReport = onOpenReport, models = state.models) + } if (state.more) item { LaunchedEffect(state.runs.size) { onMore() } } } } @@ -757,7 +763,7 @@ internal fun Header(text: String) { * whole row opens the run. */ @Composable -private fun RunRow(run: ScheduleRun, runsOn: RunsOn?, onOpen: () -> Unit, onOpenReport: (String) -> Unit = {}) { +private fun RunRow(run: ScheduleRun, runsOn: RunsOn?, onOpen: () -> Unit, onOpenReport: (String) -> Unit = {}, models: List = emptyList()) { val palette = LocalPalette.current Surface( onClick = onOpen, @@ -777,7 +783,7 @@ private fun RunRow(run: ScheduleRun, runsOn: RunsOn?, onOpen: () -> Unit, onOpen ran != null -> RanOnLine(ran) runsOn != null -> ComputerName(runsOn, away = run.computerAway) } - RunFacts(run, maxLines = 2) + RunFacts(run, maxLines = 2, models = models) run.reportId?.let { report -> ReportLink(onClick = { onOpenReport(report) }) } } } @@ -800,9 +806,10 @@ internal fun ReportLink(onClick: () -> Unit) { /** Under a run's time: the model and why, what it is doing or the trouble, then its note (or the result's first lines). */ @Composable -internal fun RunFacts(run: ScheduleRun, maxLines: Int = Int.MAX_VALUE, withPreview: Boolean = true) { +internal fun RunFacts(run: ScheduleRun, maxLines: Int = Int.MAX_VALUE, withPreview: Boolean = true, models: List = emptyList()) { val palette = LocalPalette.current - val name = run.model.label + // Which connection ran it, where the run says so and two models here share the id. + val name = models.saidWithConnection(run.model.ref, run.model.label) val from = run.model.switchedFrom val why = runModelWhy(run.model.reason) val model = when { @@ -980,7 +987,7 @@ fun EditState.read(s: Schedule): EditState { readWhere = on?.let { WhereToRun.Computer(it.device, it.cwd.trim(), it.mode.ifBlank { "edit" }, it.approvalWaitMinutes?.takeIf { minutes -> minutes > 0 }) }, - readModel = s.model.orEmpty(), + readModel = s.modelKey.orEmpty(), readWatch = watch, // Its own computer is in the list also when it is not linked now, so it can stay chosen. devices = if (on == null || known) devices else devices + LinkedDevice(id = on.device, name = on.deviceName), @@ -990,7 +997,8 @@ fun EditState.read(s: Schedule): EditState { agentMode = if (whereTouched || on == null) agentMode else on.mode.ifBlank { agentMode }, wait = if (whereTouched || on == null) wait else on.approvalWaitMinutes?.toString().orEmpty(), // A Chat schedule's own model too: made Agentic, it is shown as the model it keeps. - model = if (whereTouched) model else s.model.orEmpty(), + // By its connection where the server says it: the form then ticks the right one of two with one id. + model = if (whereTouched) model else s.modelKey.orEmpty(), watchCommand = if (whereTouched) watchCommand else watch.command, watchTimeout = if (whereTouched) watchTimeout else watch.timeoutS?.toString().orEmpty(), ) diff --git a/app/src/test/java/eu/llembas/android/ModelRefTest.kt b/app/src/test/java/eu/llembas/android/ModelRefTest.kt new file mode 100644 index 0000000..f52c7cb --- /dev/null +++ b/app/src/test/java/eu/llembas/android/ModelRefTest.kt @@ -0,0 +1,131 @@ +package eu.llembas.android + +import eu.llembas.android.api.Chat +import eu.llembas.android.api.ChatPage +import eu.llembas.android.api.LembasJson +import eu.llembas.android.api.Message +import eu.llembas.android.api.Metrics +import eu.llembas.android.api.ModelInfo +import eu.llembas.android.api.RunResult +import eu.llembas.android.api.Schedule +import eu.llembas.android.api.ScheduleEnvelope +import eu.llembas.android.api.SchedulingModel +import eu.llembas.android.api.modelKey +import eu.llembas.android.api.named +import eu.llembas.android.api.replyModel +import eu.llembas.android.api.saidWithConnection +import eu.llembas.android.api.serving +import eu.llembas.android.ui.chat.ChatState +import eu.llembas.android.ui.chat.footLine +import eu.llembas.android.ui.chat.modelId +import eu.llembas.android.ui.chat.modelInfo +import eu.llembas.android.ui.chat.modelWord +import eu.llembas.android.ui.scheduled.EditState +import eu.llembas.android.ui.scheduled.modelToSend +import eu.llembas.android.ui.scheduled.read +import kotlinx.serialization.KSerializer +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNull +import org.junit.Test + +/** + * Two connections serve one model id, and a chat, a reply and a schedule are on the second: + * each says which connection (`model_ref`), and the app shows that one, not the first with the id. + */ +class ModelRefTest { + private val home = ModelInfo(id = "qwen", name = "Qwen", ref = "local/qwen", provider = "local", connection = "Local", default = true) + private val cloud = ModelInfo(id = "qwen", name = "Qwen", ref = "studio/qwen", provider = "studio", connection = "Model Studio") + private val other = ModelInfo(id = "gpt-oss", name = "gpt-oss", ref = "local/gpt-oss", provider = "local", connection = "Local") + private val models = listOf(home, cloud, other) + + private fun read(name: String, serializer: KSerializer): T = LembasJson.decodeFromString( + serializer, + requireNotNull(javaClass.classLoader!!.getResource("$name.json")) { + "contract/$name.json is missing" + }.readText(), + ) + + @Test + fun aChatOnTheSecondConnectionShowsTheSecond() { + val chat = Chat(id = "c", model = "qwen", modelRef = "studio/qwen") + val open = ChatState(models = models, chat = chat) + assertEquals("studio/qwen", chat.modelKey) + // The header, the composer's facts and the picker's tick all read these two. + assertEquals("studio/qwen", open.modelId) + assertEquals(cloud, open.modelInfo) + assertEquals(cloud.key, models.named(open.modelId)?.key) + // What was picked here earlier does not outrank what the chat itself says. + assertEquals(cloud, open.copy(picked = "local/qwen").modelInfo) + // The bare id alone still names the first, as before and as on the server. + assertEquals(home, ChatState(models = models, chat = Chat(id = "c", model = "qwen")).modelInfo) + assertEquals(home, ChatState(models = models, chat = Chat(id = "c", model = "qwen", modelRef = "")).modelInfo) + } + + @Test + fun aRefTheListDoesNotHaveIsNoModelRatherThanTheFirstWithThatId() { + val open = ChatState(models = models, chat = Chat(id = "c", model = "qwen", modelRef = "gone/qwen")) + assertNull(open.modelInfo) + // The title then says the chat's own id for it, not a ref nobody chose by. + assertEquals("qwen", open.modelWord) + assertNull(models.serving("gone/qwen", "qwen")) + assertEquals(home, models.serving(null, "qwen")) + assertEquals(cloud, models.serving("studio/qwen", "anything")) + } + + @Test + fun whatAThingIsKeptAndSentBy() { + assertEquals("studio/qwen", modelKey("studio/qwen", "qwen")) + assertEquals("qwen", modelKey(null, "qwen")) + assertEquals("qwen", modelKey("", "qwen")) + assertNull(modelKey(null, "")) + assertNull(modelKey(null, null)) + } + + @Test + fun theLineUnderAReplyNamesTheConnectionOfATwin() { + assertEquals("qwen · Model Studio", models.replyModel("studio/qwen", "qwen")) + assertEquals("qwen · Local", models.replyModel("local/qwen", "qwen")) + // A model alone with its id says nothing more, and neither does a reply with no ref. + assertEquals("gpt-oss", models.replyModel("local/gpt-oss", "gpt-oss")) + assertEquals("qwen", models.replyModel(null, "qwen")) + assertEquals("qwen", models.replyModel("gone/qwen", "qwen")) + assertNull(models.replyModel("studio/qwen", null)) + val reply = Message(id = "m", role = "assistant", model = "qwen", modelRef = "studio/qwen", metrics = Metrics(completionTokens = 12)) + assertEquals("qwen · Model Studio · 12 tok", footLine(reply, models)) + assertEquals("qwen · 12 tok", footLine(reply)) + assertEquals("qwen · Model Studio", footLine(reply.copy(metrics = null), models)) + } + + @Test + fun aScheduleAndItsRunNameTheConnection() { + val offered = listOf( + SchedulingModel(id = "qwen", name = "Qwen", ref = "local/qwen", connection = "Local"), + SchedulingModel(id = "qwen", name = "Qwen", ref = "studio/qwen", connection = "Model Studio"), + SchedulingModel(id = "gpt-oss", name = "gpt-oss", ref = "local/gpt-oss", connection = "Local"), + ) + assertEquals("qwen · Model Studio", offered.saidWithConnection("studio/qwen", "qwen")) + assertEquals("gpt-oss", offered.saidWithConnection("local/gpt-oss", "gpt-oss")) + assertEquals("qwen", offered.saidWithConnection(null, "qwen")) + assertEquals("qwen", emptyList().saidWithConnection("studio/qwen", "qwen")) + assertNull(offered.saidWithConnection("studio/qwen", "")) + + // The form starts from the schedule's own connection, ticks that one, and a save that + // changed nothing says no model. + val schedule = Schedule(id = "s", model = "qwen", modelRef = "studio/qwen") + assertEquals("studio/qwen", schedule.modelKey) + val form = EditState(models = offered, loaded = true).read(schedule).copy(agentic = true) + assertEquals("studio/qwen", form.model) + assertEquals(offered[1], offered.named(form.model)) + assertNull(modelToSend(form, editing = true)) + assertEquals("local/qwen", modelToSend(form.copy(model = "local/qwen"), editing = true)) + } + + @Test + fun thePinnedContractSaysTheRefs() { + assertEquals("local/qwen", read("chat", Chat.serializer()).modelRef) + assertEquals("local/qwen", read("chats_listed", ChatPage.serializer()).chats.first().modelRef) + assertEquals("local/qwen", read("message_assistant", Message.serializer()).modelRef) + assertEquals("local/qwen", read("schedule", ScheduleEnvelope.serializer()).schedule.modelRef) + assertEquals("local/gpt-oss", read("schedule_run", RunResult.serializer()).run.model.ref) + } +}