@@ -0,0 +1,2604 @@
1+@file:Suppress("TooManyFunctions")
3+package soy.iko.opencode.ui.chat
5+import android.app.Activity
6+import android.content.Intent
8+import android.speech.RecognizerIntent
9+import androidx.activity.compose.BackHandler
10+import androidx.activity.compose.rememberLauncherForActivityResult
11+import androidx.activity.result.PickVisualMediaRequest
12+import androidx.activity.result.contract.ActivityResultContracts
13+import androidx.compose.animation.AnimatedVisibility
14+import androidx.compose.animation.core.RepeatMode
15+import androidx.compose.animation.core.animateFloat
16+import androidx.compose.animation.core.infiniteRepeatable
17+import androidx.compose.animation.core.rememberInfiniteTransition
18+import androidx.compose.animation.core.tween
19+import androidx.compose.foundation.background
20+import androidx.compose.foundation.clickable
21+import androidx.compose.foundation.content.contentReceiver
22+import androidx.compose.foundation.layout.Arrangement
23+import androidx.compose.foundation.layout.Box
24+import androidx.compose.foundation.layout.Column
25+import androidx.compose.foundation.layout.FlowRow
26+import androidx.compose.foundation.layout.PaddingValues
27+import androidx.compose.foundation.layout.Row
28+import androidx.compose.foundation.layout.Spacer
29+import androidx.compose.foundation.layout.WindowInsets
30+import androidx.compose.foundation.layout.fillMaxSize
31+import androidx.compose.foundation.layout.fillMaxWidth
32+import androidx.compose.foundation.layout.height
33+import androidx.compose.foundation.layout.imePadding
34+import androidx.compose.foundation.layout.navigationBars
35+import androidx.compose.foundation.layout.padding
36+import androidx.compose.foundation.layout.size
37+import androidx.compose.foundation.layout.union
38+import androidx.compose.foundation.layout.widthIn
39+import androidx.compose.foundation.layout.windowInsetsPadding
40+import androidx.compose.foundation.layout.wrapContentWidth
41+import androidx.compose.foundation.lazy.LazyColumn
42+import androidx.compose.foundation.lazy.items
43+import androidx.compose.foundation.lazy.rememberLazyListState
44+import androidx.compose.foundation.shape.RoundedCornerShape
45+import androidx.compose.material.icons.Icons
46+import androidx.compose.material.icons.automirrored.filled.ArrowBack
47+import androidx.compose.material.icons.automirrored.filled.Send
48+import androidx.compose.material.icons.filled.Add
49+import androidx.compose.material.icons.filled.AutoAwesome
50+import androidx.compose.material.icons.filled.Delete
51+import androidx.compose.material.icons.filled.Description
52+import androidx.compose.material.icons.filled.Close
53+import androidx.compose.material.icons.filled.Edit
54+import androidx.compose.material.icons.filled.Expand
55+import androidx.compose.material.icons.filled.ExpandLess
56+import androidx.compose.material.icons.filled.ExpandMore
57+import androidx.compose.material.icons.filled.Search
58+import androidx.compose.material.icons.filled.KeyboardArrowDown
59+import androidx.compose.material.icons.filled.KeyboardArrowUp
60+import androidx.compose.material.icons.filled.Mic
61+import androidx.compose.material.icons.filled.MoreVert
62+import androidx.compose.material.icons.filled.PhotoCamera
63+import androidx.compose.material.icons.filled.PhotoLibrary
64+import androidx.compose.material.icons.filled.Refresh
65+import androidx.compose.material.icons.filled.Restore
66+import androidx.compose.material.icons.automirrored.filled.PlaylistAdd
67+import androidx.compose.material.icons.automirrored.filled.Reply
68+import androidx.compose.material.icons.filled.Schedule
69+import androidx.compose.material.icons.filled.Stop
70+import androidx.compose.material3.AlertDialog
71+import androidx.compose.material3.Badge
72+import androidx.compose.material3.BadgedBox
73+import androidx.compose.material3.Button
74+import androidx.compose.material3.CircularProgressIndicator
75+import androidx.compose.material3.SmallFloatingActionButton
76+import androidx.compose.material3.DropdownMenu
77+import androidx.compose.material3.DropdownMenuItem
78+import androidx.compose.material3.ExperimentalMaterial3Api
79+import androidx.compose.material3.ExtendedFloatingActionButton
80+import androidx.compose.material3.FloatingActionButton
81+import androidx.compose.material3.Icon
82+import androidx.compose.material3.IconButton
83+import androidx.compose.material3.MaterialTheme
84+import androidx.compose.material3.OutlinedTextField
85+import androidx.compose.material3.Scaffold
86+import androidx.compose.material3.SnackbarDuration
87+import androidx.compose.material3.SwipeToDismissBox
88+import androidx.compose.material3.SwipeToDismissBoxValue
89+import androidx.compose.material3.rememberSwipeToDismissBoxState
90+import androidx.compose.material3.SnackbarHost
91+import androidx.compose.material3.SnackbarHostState
92+import androidx.compose.material3.SnackbarResult
93+import androidx.compose.material3.Surface
94+import androidx.compose.material3.Text
95+import androidx.compose.material3.TextButton
96+import androidx.compose.material3.TopAppBar
97+import androidx.compose.material3.pulltorefresh.PullToRefreshBox
98+import androidx.compose.runtime.Composable
99+import androidx.compose.runtime.CompositionLocalProvider
100+import androidx.compose.runtime.DisposableEffect
101+import androidx.compose.runtime.LaunchedEffect
102+import androidx.compose.runtime.derivedStateOf
103+import androidx.compose.runtime.getValue
104+import androidx.compose.runtime.mutableIntStateOf
105+import androidx.compose.runtime.mutableLongStateOf
106+import androidx.compose.runtime.mutableStateOf
107+import androidx.compose.runtime.produceState
108+import androidx.compose.runtime.remember
109+import androidx.compose.runtime.rememberCoroutineScope
110+import androidx.compose.runtime.rememberUpdatedState
111+import androidx.compose.runtime.saveable.rememberSaveable
112+import androidx.compose.runtime.setValue
113+import androidx.compose.runtime.snapshotFlow
114+import androidx.compose.foundation.text.KeyboardActions
115+import androidx.compose.foundation.text.KeyboardOptions
116+import androidx.compose.ui.text.input.KeyboardCapitalization
117+import androidx.compose.ui.Alignment
118+import androidx.compose.ui.Modifier
119+import androidx.compose.ui.draw.alpha
120+import androidx.compose.ui.draw.clip
121+import androidx.compose.ui.focus.focusRequester
122+import androidx.compose.ui.input.key.Key
123+import androidx.compose.ui.input.key.KeyEventType
124+import androidx.compose.ui.input.key.isCtrlPressed
125+import androidx.compose.ui.input.key.isShiftPressed
126+import androidx.compose.ui.input.key.key
127+import androidx.compose.ui.input.key.onPreviewKeyEvent
128+import androidx.compose.ui.input.key.type
129+import androidx.compose.ui.layout.onSizeChanged
130+import androidx.compose.ui.platform.LocalContext
131+import androidx.compose.ui.platform.LocalDensity
132+import androidx.compose.ui.platform.LocalHapticFeedback
133+import androidx.compose.ui.platform.LocalView
134+import androidx.compose.ui.platform.testTag
135+import androidx.compose.ui.res.stringResource
136+import androidx.compose.ui.semantics.Role
137+import androidx.compose.ui.semantics.contentDescription
138+import androidx.compose.ui.semantics.heading
139+import androidx.compose.ui.semantics.semantics
140+import androidx.compose.ui.text.input.ImeAction
141+import androidx.compose.ui.text.style.TextOverflow
142+import androidx.compose.ui.unit.dp
143+import androidx.lifecycle.compose.collectAsStateWithLifecycle
144+import androidx.lifecycle.viewmodel.compose.viewModel
145+import kotlinx.coroutines.Dispatchers
146+import kotlinx.coroutines.coroutineScope
147+import kotlinx.coroutines.delay
148+import kotlinx.coroutines.flow.distinctUntilChanged
149+import kotlinx.coroutines.launch
150+import kotlinx.coroutines.withContext
151+import soy.iko.opencode.data.model.Part
152+import soy.iko.opencode.data.model.MessageWithParts
153+import soy.iko.opencode.data.model.ReasoningPart
154+import soy.iko.opencode.data.model.TextPart
155+import soy.iko.opencode.data.model.ToolCompleted
156+import soy.iko.opencode.data.model.ToolError
157+import soy.iko.opencode.data.model.ToolPart
158+import soy.iko.opencode.data.model.ToolRunning
159+import soy.iko.opencode.data.network.EventStreamClient
160+import soy.iko.opencode.data.network.NetworkConfig
161+import soy.iko.opencode.di.AppContainer
162+import soy.iko.opencode.R
163+import soy.iko.opencode.ui.components.CommandPalette
164+import soy.iko.opencode.ui.components.ConnectionBanner
165+import soy.iko.opencode.ui.components.DiffView
166+import soy.iko.opencode.ui.components.PaletteAction
167+import soy.iko.opencode.ui.components.LocalReducedMotion
168+import soy.iko.opencode.ui.components.copyToClipboard
169+import soy.iko.opencode.ui.components.LocalRelativeTimeTick
170+import soy.iko.opencode.ui.components.rememberRelativeTimeTick
171+import soy.iko.opencode.ui.components.rememberVisibilityTransitions
172+import soy.iko.opencode.ui.components.showToast
173+import soy.iko.opencode.ui.components.toImageContext
174+import soy.iko.opencode.ui.vmFactory
175+import soy.iko.opencode.util.runCatchingCancellable
177+@OptIn(ExperimentalMaterial3Api::class)
180+ container: AppContainer,
183+ onOpenFile: ((String) -> Unit)? = null,
184+ onOpenSession: ((String) -> Unit)? = null,
186+ val vm: ChatViewModel = viewModel(key = sessionId, factory = vmFactory { ChatViewModel(container, sessionId) })
187+ val hasMessages by vm.hasMessages.collectAsStateWithLifecycle()
188+ val running by vm.running.collectAsStateWithLifecycle()
189+ val aborting by vm.aborting.collectAsStateWithLifecycle()
190+ val loading by vm.loading.collectAsStateWithLifecycle()
191+ val loadError by vm.loadError.collectAsStateWithLifecycle()
192+ val refreshing by vm.refreshing.collectAsStateWithLifecycle()
193+ val models by vm.models.collectAsStateWithLifecycle()
194+ val modelsLoading by vm.modelsLoading.collectAsStateWithLifecycle()
195+ val modelsError by vm.modelsError.collectAsStateWithLifecycle()
196+ val selectedModel by vm.selectedModel.collectAsStateWithLifecycle()
197+ val connectionState by vm.connectionState.collectAsStateWithLifecycle()
198+ val pendingPermission by vm.pendingPermission.collectAsStateWithLifecycle()
199+ val permissionProgress by vm.permissionProgress.collectAsStateWithLifecycle()
200+ val agents by vm.agents.collectAsStateWithLifecycle()
201+ val agentsLoading by vm.agentsLoading.collectAsStateWithLifecycle()
202+ val agentsError by vm.agentsError.collectAsStateWithLifecycle()
203+ val selectedAgent by vm.selectedAgent.collectAsStateWithLifecycle()
204+ val commands by vm.commands.collectAsStateWithLifecycle()
205+ val commandsLoading by vm.commandsLoading.collectAsStateWithLifecycle()
206+ val commandsError by vm.commandsError.collectAsStateWithLifecycle()
207+ val sessionTitle by vm.sessionTitle.collectAsStateWithLifecycle()
208+ val sessionDeleted by vm.sessionDeleted.collectAsStateWithLifecycle()
209+ val draft by vm.draft.collectAsStateWithLifecycle()
210+ val attachments by vm.attachments.collectAsStateWithLifecycle()
211+ val reverted by vm.reverted.collectAsStateWithLifecycle()
212+ val revertDiff by vm.revertDiff.collectAsStateWithLifecycle()
213+ val shareUrl by vm.shareUrl.collectAsStateWithLifecycle()
214+ val reconnecting by vm.reconnecting.collectAsStateWithLifecycle()
215+ val sendOnEnter by container.settingsStore.sendOnEnter.collectAsStateWithLifecycle(initialValue = true)
216+ val isOnline by container.isOnline.collectAsStateWithLifecycle()
217+ val haptics = LocalHapticFeedback.current
218+ val scope = rememberCoroutineScope()
219+ val shareContext = LocalContext.current
220+ val defaultShareSubject = stringResource(R.string.share_subject)
221+ val sessionLabel = stringResource(R.string.session)
222+ val defaultModelLabel = stringResource(R.string.default_model)
223+ val subtitle = remember(selectedModel?.modelLabel, selectedAgent, defaultModelLabel) {
225+ append(selectedModel?.modelLabel ?: defaultModelLabel)
226+ selectedAgent?.let { append(" · $it") }
230+ // Connection profile (with auth) for rendering inline image attachments.
231+ // Collect as state so a server switch recomposes the image context.
232+ val activeConnection by container.activeConnection.collectAsStateWithLifecycle()
233+ val imageContext = remember(activeConnection?.profile) {
234+ activeConnection?.profile?.toImageContext()
237+ // Mark this session as read while the user is viewing it; clear on leave so new
238+ // background activity can badge it again.
239+ DisposableEffect(sessionId) {
240+ container.setCurrentSession(sessionId)
241+ onDispose { if (container.currentSession.value == sessionId) container.setCurrentSession(null) }
244+ // Navigate away when the session is deleted via SSE so the user isn't left
245+ // on a zombie screen showing a conversation that no longer exists. Surface a
246+ // toast (not a snackbar) because the screen is about to be popped — a snackbar
247+ // would be torn down before the user could read it, while a toast survives.
248+ // Covers both SSE-driven deletion (deleted on another device) and a user-
249+ // initiated delete from the chat overflow menu (deleteSession in ChatViewModel
250+ // sets the same flag on success).
251+ val deletedContext = LocalContext.current
252+ LaunchedEffect(sessionDeleted) {
253+ if (sessionDeleted) {
254+ showToast(deletedContext, deletedContext.getString(R.string.session_deleted_chat))
259+ val snackbar = remember { SnackbarHostState() }
260+ val retryLabel = stringResource(R.string.retry)
261+ val inputFocusRequester = remember { androidx.compose.ui.focus.FocusRequester() }
262+ var showModelPicker by rememberSaveable { mutableStateOf(false) }
263+ var showAgentPicker by rememberSaveable { mutableStateOf(false) }
264+ var showTitleMenu by rememberSaveable { mutableStateOf(false) }
265+ var showCommandPicker by rememberSaveable { mutableStateOf(false) }
266+ var showExitConfirm by rememberSaveable { mutableStateOf(false) }
267+ var showStopConfirm by rememberSaveable { mutableStateOf(false) }
268+ var showOverflowMenu by rememberSaveable { mutableStateOf(false) }
269+ var showRenameDialog by rememberSaveable { mutableStateOf(false) }
270+ var showDeleteDialog by rememberSaveable { mutableStateOf(false) }
271+ var showShellDialog by rememberSaveable { mutableStateOf(false) }
272+ var showPalette by rememberSaveable { mutableStateOf(false) }
273+ var showShortcutsDialog by rememberSaveable { mutableStateOf(false) }
274+ var searchActive by rememberSaveable { mutableStateOf(false) }
275+ var chatSearch by rememberSaveable { mutableStateOf("") }
276+ // Index of the currently-focused in-conversation search match (into the filtered list).
277+ // Drives the up/down navigation: the search bar's arrows step through matches and scroll
278+ // the list to each one, mirroring desktop chat apps (the bare filtered list previously
279+ // required reading each message to find the term).
280+ var searchPos by rememberSaveable { mutableIntStateOf(0) }
281+ // Summarize and init (generate AGENTS.md) are irreversible, so a single tap is gated behind
282+ // a confirmation dialog (from both the overflow menu and the command palette).
283+ var showSummarizeConfirm by rememberSaveable { mutableStateOf(false) }
284+ var showInitConfirm by rememberSaveable { mutableStateOf(false) }
286+ // Command-palette entries (keyboard-first: Ctrl+K). Labels resolved here so the remembered
287+ // action list doesn't need a Composable context. Actions map to the same handlers the
288+ // overflow menu drives, so the palette is a keyboard shortcut to them, not a parallel path.
289+ val paletteModelLabel = stringResource(R.string.palette_switch_model)
290+ val paletteAgentLabel = stringResource(R.string.palette_choose_agent)
291+ val paletteCommandLabel = stringResource(R.string.palette_run_command)
292+ val paletteSummarizeLabel = stringResource(R.string.palette_summarize)
293+ val paletteRenameLabel = stringResource(R.string.rename_session_chat)
294+ val paletteShellLabel = stringResource(R.string.run_shell_command)
295+ val paletteActions = remember(
296+ paletteModelLabel, paletteAgentLabel, paletteCommandLabel,
297+ paletteSummarizeLabel, paletteRenameLabel, paletteShellLabel, hasMessages, running,
300+ add(PaletteAction("model", paletteModelLabel) { showModelPicker = true })
301+ add(PaletteAction("agent", paletteAgentLabel) { showAgentPicker = true })
302+ add(PaletteAction("command", paletteCommandLabel) { showCommandPicker = true })
303+ if (hasMessages && !running) add(PaletteAction("summarize", paletteSummarizeLabel) { showSummarizeConfirm = true })
304+ if (!running) add(PaletteAction("shell", paletteShellLabel) { showShellDialog = true })
305+ add(PaletteAction("rename", paletteRenameLabel) { showRenameDialog = true })
309+ // Keep the screen awake while the agent is working in *this* session. The foreground
310+ // service that holds process priority during a run is managed app-wide off
311+ // container.anyRunActive (see OpencodeApp) so it survives navigating away from the chat
312+ // mid-run — otherwise leaving the screen would drop priority and Doze could choke the
313+ // SSE stream before the completion notification fires.
314+ val currentView = LocalView.current
315+ val appContext = LocalContext.current.applicationContext
316+ DisposableEffect(running) {
317+ currentView.keepScreenOn = running
318+ onDispose { currentView.keepScreenOn = false }
321+ LaunchedEffect(Unit) {
322+ vm.errorEvents.collect { event ->
323+ // Only a failed *send* is retryable; attaching Retry to any other error (e.g. a
324+ // message-load failure) would silently re-submit the last prompt on tap.
325+ val result = if (event.retryable) {
326+ snackbar.showSnackbar(message = event.message, actionLabel = retryLabel)
328+ snackbar.showSnackbar(event.message)
330+ if (result == androidx.compose.material3.SnackbarResult.ActionPerformed) vm.retryFailed()
334+ // In-place Undo for a user-initiated session delete. The delete is deferred for an undo
335+ // window on the container; show the snackbar over the conversation the user is viewing so
336+ // they can cancel without being navigated to the session list first. If the window expires
337+ // the scheduled delete sets `sessionDeleted` and the effect below navigates away.
338+ val undoLabel = stringResource(R.string.undo)
339+ val sessionDeletedLabel = stringResource(R.string.session_deleted_chat)
340+ LaunchedEffect(Unit) {
341+ vm.deleteUndoEvents.collect {
343+ val dismisser = launch {
344+ delay(NetworkConfig.undoDeleteDelayMs)
345+ snackbar.currentSnackbarData?.dismiss()
347+ val result = snackbar.showSnackbar(
348+ message = sessionDeletedLabel,
349+ actionLabel = undoLabel,
350+ duration = SnackbarDuration.Indefinite,
353+ if (result == SnackbarResult.ActionPerformed) vm.cancelSessionDelete()
358+ // --- Attachments, voice, and share-link plumbing ---
359+ val attachTooLargeMsg = stringResource(R.string.attachment_too_large)
360+ val attachFailedMsg = stringResource(R.string.attachment_failed)
361+ val attachLimitMsg = stringResource(R.string.attachment_limit)
362+ val noCameraMsg = stringResource(R.string.no_camera_app)
363+ val noVoiceMsg = stringResource(R.string.no_voice_app)
364+ val voicePrompt = stringResource(R.string.voice_prompt)
365+ val linkCopiedMsg = stringResource(R.string.link_copied)
366+ val ttsUnavailableMsg = stringResource(R.string.tts_unavailable)
368+ // Count of picks currently being read + base64-encoded off the main thread, so the
369+ // composer can show a staging placeholder immediately (chips only materialize once done).
370+ var stagingCount by remember { mutableStateOf(0) }
372+ // Convert each picked Uri to a base64 attachment off the main thread, honoring the
373+ // per-prompt count cap and surfacing per-file errors without aborting the batch.
374+ fun stageUris(uris: List<Uri>) {
375+ if (uris.isEmpty()) return
380+ if (vm.attachments.value.size >= NetworkConfig.maxAttachments) {
381+ snackbar.showSnackbar(attachLimitMsg)
384+ when (val result = uri.toAttachmentResult(appContext)) {
385+ is AttachmentResult.Ok -> vm.addAttachment(result.attachment)
386+ AttachmentResult.TooLarge -> snackbar.showSnackbar(attachTooLargeMsg)
387+ AttachmentResult.Failed -> snackbar.showSnackbar(attachFailedMsg)
396+ val photoPicker = rememberLauncherForActivityResult(
397+ ActivityResultContracts.PickMultipleVisualMedia(NetworkConfig.maxAttachments),
398+ ) { uris -> stageUris(uris) }
400+ val filePicker = rememberLauncherForActivityResult(
401+ ActivityResultContracts.GetMultipleContents(),
402+ ) { uris -> stageUris(uris) }
404+ // Uri survives a config change mid-capture so the TakePicture result still resolves.
405+ var pendingCaptureUri by rememberSaveable { mutableStateOf<Uri?>(null) }
406+ val cameraLauncher = rememberLauncherForActivityResult(
407+ ActivityResultContracts.TakePicture(),
409+ val uri = pendingCaptureUri
410+ pendingCaptureUri = null
411+ if (success && uri != null) stageUris(listOf(uri))
414+ val voiceLauncher = rememberLauncherForActivityResult(
415+ ActivityResultContracts.StartActivityForResult(),
417+ if (result.resultCode == Activity.RESULT_OK) {
418+ val spoken = result.data
419+ ?.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS)
421+ ?.takeIf { it.isNotBlank() }
422+ if (spoken != null) {
423+ // Append to the existing draft rather than replacing, so dictation adds to
424+ // whatever the user already typed.
425+ val current = vm.draft.value
426+ val combined = if (current.isBlank()) spoken else "$current $spoken"
427+ vm.updateDraft(combined.take(NetworkConfig.maxDraftLengthChars))
432+ fun launchCamera() {
433+ val uri = newCameraCaptureUri(appContext)
435+ scope.launch { snackbar.showSnackbar(attachFailedMsg) }
438+ pendingCaptureUri = uri
439+ runCatching { cameraLauncher.launch(uri) }
440+ .onFailure { pendingCaptureUri = null; scope.launch { snackbar.showSnackbar(noCameraMsg) } }
444+ val intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH).apply {
445+ putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM)
446+ putExtra(RecognizerIntent.EXTRA_PROMPT, voicePrompt)
448+ runCatching { voiceLauncher.launch(intent) }
449+ .onFailure { scope.launch { snackbar.showSnackbar(noVoiceMsg) } }
452+ // A freshly-created share link is copied to the clipboard so the user can paste it right away.
453+ LaunchedEffect(Unit) {
454+ vm.shareLinkEvents.collect { url ->
455+ copyToClipboard(shareContext, shareContext.getString(R.string.clip_label_share), url)
456+ snackbar.showSnackbar(linkCopiedMsg)
460+ // Navigate to a session freshly branched from a message (branchFrom), once it's created.
461+ // Keyed on Unit (like the sibling one-shot collectors above): keying on the onOpenSession
462+ // lambda would cancel/restart this collector whenever the lambda's identity changes, risking
463+ // a dropped branch-navigation event in the gap.
464+ val branchedMsg = stringResource(R.string.branch_created)
465+ LaunchedEffect(Unit) {
466+ vm.branchEvents.collect { newId ->
467+ showToast(shareContext, branchedMsg)
468+ onOpenSession?.invoke(newId)
472+ // Stage images shared into the app (via the system share sheet) as attachments.
473+ // Keyed on pendingSharedMedia (not Unit) so the effect re-fires when new media arrives
474+ // while this ChatScreen is already composed — which happens in two-pane mode, where the
475+ // detail ChatScreen is persistent (keyed on sessionId) and a LaunchedEffect(Unit) would
476+ // only fire on first composition. consumePendingSharedMedia() atomically drains the
477+ // pending list (compareAndSet to empty), so re-firing on the now-empty value is a no-op.
478+ val pendingSharedMedia by container.pendingSharedMedia.collectAsStateWithLifecycle()
479+ LaunchedEffect(pendingSharedMedia) {
480+ val shared = container.consumePendingSharedMedia()
481+ if (shared.isNotEmpty()) stageUris(shared.map { Uri.parse(it) })
485+ if (vm.send(draft)) {
486+ // TextHandleMove (a light tick) is semantically right for a send commit;
487+ // LongPress is reserved for long-press/swipe gestures.
488+ haptics.performHapticFeedback(androidx.compose.ui.hapticfeedback.HapticFeedbackType.TextHandleMove)
492+ BackHandler(enabled = running && !showModelPicker && !showAgentPicker && !showCommandPicker) { showExitConfirm = true }
495+ // Hardware-keyboard shortcuts (tablets / DeX / Chromebooks): Ctrl+K opens the command
496+ // palette; Escape closes it, or stops a run. onPreviewKeyEvent on the Scaffold root sees
497+ // events before the focused text field, but only Ctrl+K / Escape are consumed so normal
498+ // typing (and the composer's own Enter-to-send handling) is untouched.
499+ modifier = Modifier.onPreviewKeyEvent { ev ->
500+ if (ev.type != KeyEventType.KeyDown) return@onPreviewKeyEvent false
502+ ev.isCtrlPressed && ev.key == Key.K -> { showPalette = true; true }
503+ // Ctrl+F opens in-conversation search (the standard find shortcut), focusing
504+ // the field so the user can type immediately.
505+ ev.isCtrlPressed && ev.key == Key.F -> { searchActive = true; true }
506+ ev.key == Key.Escape && showPalette -> { showPalette = false; true }
507+ ev.key == Key.Escape && searchActive -> { searchActive = false; chatSearch = ""; true }
508+ ev.key == Key.Escape && running && !showStopConfirm && !showExitConfirm -> {
509+ showStopConfirm = true; true
517+ val changeLabel = stringResource(R.string.change_model_agent)
518+ // The subtitle reads "model · agent", so tapping the title offers *both*
519+ // — a small menu picks which picker to open. Previously the tap went
520+ // straight to the model picker, leaving the agent changeable only via
521+ // the overflow menu despite the title implying otherwise.
524+ // mergeDescendants so TalkBack reads the title/subtitle Texts as one
525+ // node; the click label rides on `clickable` (onClickLabel) instead of
526+ // an explicit contentDescription, which would have SUPPRESSED the child
527+ // Texts and announced only "Change model and agent".
531+ onClickLabel = changeLabel,
532+ ) { showTitleMenu = true }
533+ .semantics(mergeDescendants = true) {},
534+ verticalAlignment = Alignment.CenterVertically,
536+ Column(modifier = Modifier.weight(1f, fill = false)) {
537+ // maxLines=1 + Ellipsis so a long session title can't wrap and
538+ // push the top bar to multiple lines, shifting the whole layout.
540+ sessionTitle ?: sessionLabel,
542+ overflow = TextOverflow.Ellipsis,
546+ style = MaterialTheme.typography.labelSmall,
547+ color = MaterialTheme.colorScheme.onSurfaceVariant,
549+ overflow = TextOverflow.Ellipsis,
552+ // Always show the affordance so the title reads as tappable even when
553+ // the model catalog failed to load (models empty). Without it the title
554+ // is indistinguishable from plain text and the pickers are effectively
555+ // undiscoverable — and the dropdown items already disable themselves
556+ // while a catalog is loading or unavailable.
558+ Icons.Filled.KeyboardArrowDown,
559+ contentDescription = null,
560+ tint = MaterialTheme.colorScheme.onSurfaceVariant,
561+ modifier = Modifier.size(20.dp),
564+ DropdownMenu(expanded = showTitleMenu, onDismissRequest = { showTitleMenu = false }) {
566+ text = { Text(stringResource(R.string.palette_switch_model)) },
567+ enabled = !modelsLoading || models.isNotEmpty(),
568+ onClick = { showTitleMenu = false; showModelPicker = true },
571+ text = { Text(stringResource(R.string.palette_choose_agent)) },
572+ enabled = !agentsLoading || agents.isNotEmpty(),
573+ onClick = { showTitleMenu = false; showAgentPicker = true },
579+ IconButton(onClick = {
580+ if (running) showExitConfirm = true else onBack()
582+ Icon(Icons.AutoMirrored.Filled.ArrowBack, contentDescription = stringResource(R.string.back))
586+ // At-a-glance run status in the top bar. When the user is scrolled
587+ // up (jump-to-latest FAB visible), the input-bar Stop button and the
588+ // trailing "working…" row are off-screen, so a top-bar spinner is the
589+ // only signal the agent is still working.
591+ val workingLabel = stringResource(R.string.working)
592+ CircularProgressIndicator(
593+ Modifier.size(18.dp).semantics { contentDescription = workingLabel },
597+ // Manual refresh: forces an SSE reconnect, which re-seeds messages
598+ // from REST. A recovery path when the stream silently drops and the
599+ // auto-reconnect re-seed is slow or fails. Shows a brief spinner as
600+ // immediate tap feedback — the SSE reconnect may not visibly change
601+ // the connection state when already Connected, so without it the tap
602+ // appears to do nothing.
603+ val refreshLabel = stringResource(R.string.refresh)
605+ onClick = { vm.refreshMessages() },
606+ enabled = activeConnection != null && !refreshing,
609+ CircularProgressIndicator(
610+ Modifier.size(18.dp).semantics { contentDescription = refreshLabel },
614+ Icon(Icons.Filled.Refresh, contentDescription = refreshLabel)
617+ // Overflow menu consolidates the less-frequent actions (share,
618+ // commands, agent, model, rename, delete) so the top bar stays
619+ // scannable on narrow phones — previously 5 icons crowded the bar
620+ // alongside the back button and tappable title. Catalog-loading
621+ // spinners stay inline (next to the overflow) so a load state is
622+ // visible without opening the menu.
623+ val moreLabel = stringResource(R.string.more)
624+ val loadingLabel = stringResource(R.string.loading)
625+ val shareLabel = stringResource(R.string.share_conversation)
626+ val commandsLabel = stringResource(R.string.commands)
627+ val renameLabel = stringResource(R.string.rename_session_chat)
628+ val deleteLabel = stringResource(R.string.delete_session_chat)
629+ val createShareLabel = stringResource(R.string.create_share_link)
630+ val copyShareLabel = stringResource(R.string.copy_share_link)
631+ val stopShareLabel = stringResource(R.string.stop_sharing)
632+ val summarizeLabel = stringResource(R.string.summarize_conversation)
633+ val initLabel = stringResource(R.string.generate_agents_md)
634+ val shellLabel = stringResource(R.string.run_shell_command)
636+ IconButton(onClick = { showOverflowMenu = true }) {
637+ Icon(Icons.Filled.MoreVert, contentDescription = moreLabel)
640+ expanded = showOverflowMenu,
641+ onDismissRequest = { showOverflowMenu = false },
644+ text = { Text(shareLabel) },
645+ enabled = hasMessages,
647+ showOverflowMenu = false
649+ val md = withContext(Dispatchers.Default) {
650+ buildConversationMarkdown(vm.messages.value, sessionTitle)
652+ val send = android.content.Intent(android.content.Intent.ACTION_SEND).apply {
653+ type = "text/markdown"
654+ putExtra(android.content.Intent.EXTRA_SUBJECT, sessionTitle ?: defaultShareSubject)
655+ putExtra(android.content.Intent.EXTRA_TEXT, md)
657+ runCatchingCancellable { shareContext.startActivity(android.content.Intent.createChooser(send, shareLabel)) }
658+ .onFailure { showToast(shareContext, shareContext.getString(R.string.no_share_app)) }
663+ text = { Text(commandsLabel) },
664+ enabled = !commandsLoading || commands.isNotEmpty(),
665+ onClick = { showOverflowMenu = false; showCommandPicker = true },
668+ text = { Text(stringResource(R.string.keyboard_shortcuts)) },
669+ onClick = { showOverflowMenu = false; showShortcutsDialog = true },
672+ text = { Text(stringResource(R.string.find_in_conversation)) },
674+ showOverflowMenu = false
676+ runCatching { inputFocusRequester.requestFocus() }
679+ androidx.compose.material3.HorizontalDivider()
680+ // Session sharing: create a public link, copy an existing one, or revoke it.
681+ if (shareUrl == null) {
683+ text = { Text(createShareLabel) },
684+ enabled = hasMessages,
685+ onClick = { showOverflowMenu = false; vm.shareSession() },
689+ text = { Text(copyShareLabel) },
691+ showOverflowMenu = false
693+ copyToClipboard(shareContext, shareContext.getString(R.string.clip_label_share), it)
694+ scope.launch { snackbar.showSnackbar(linkCopiedMsg) }
699+ text = { Text(stopShareLabel) },
700+ onClick = { showOverflowMenu = false; vm.unshareSession() },
704+ text = { Text(summarizeLabel) },
705+ enabled = hasMessages && !running,
706+ onClick = { showOverflowMenu = false; showSummarizeConfirm = true },
709+ text = { Text(initLabel) },
711+ onClick = { showOverflowMenu = false; showInitConfirm = true },
714+ text = { Text(shellLabel) },
716+ onClick = { showOverflowMenu = false; showShellDialog = true },
718+ androidx.compose.material3.HorizontalDivider()
720+ text = { Text(renameLabel) },
721+ onClick = { showOverflowMenu = false; showRenameDialog = true },
724+ text = { Text(deleteLabel, color = MaterialTheme.colorScheme.error) },
725+ onClick = { showOverflowMenu = false; showDeleteDialog = true },
728+ // While a catalog is initially loading (empty list + loading flag),
729+ // show a tiny inline spinner beside the overflow so the user can see
730+ // a load is in progress without opening the menu. Once loaded the
731+ // spinner disappears; the menu items are always enabled (tapping a
732+ // picker with no items shows the "no models/agents/commands" state).
733+ val catalogLoading = (commandsLoading && commands.isEmpty()) ||
734+ (agentsLoading && agents.isEmpty()) ||
735+ (modelsLoading && models.isEmpty())
736+ if (catalogLoading) {
737+ CircularProgressIndicator(
739+ .align(Alignment.TopEnd)
741+ .semantics { contentDescription = loadingLabel },
749+ snackbarHost = { SnackbarHost(snackbar) },
751+ val queuedFollowUp by vm.queuedFollowUp.collectAsStateWithLifecycle()
752+ val todoPlan by vm.todoPlan.collectAsStateWithLifecycle()
753+ val queuedOffline by vm.outbox.collectAsStateWithLifecycle()
754+ val outboxSending by container.outboxSending.collectAsStateWithLifecycle()
756+ // Pinned above the composer so the agent's plan and progress stay visible
757+ // while scrolled up mid-run; renders nothing when there's no plan.
758+ TodoPlanBar(todoPlan)
759+ val bannerMotion = rememberVisibilityTransitions()
762+ enter = bannerMotion.enter,
763+ exit = bannerMotion.exit,
767+ onUndo = { vm.unrevert() },
771+ visible = queuedOffline.isNotEmpty(),
772+ enter = bannerMotion.enter,
773+ exit = bannerMotion.exit,
776+ count = queuedOffline.size,
777+ sending = outboxSending,
778+ onFlush = { vm.flushQueued() },
779+ onDiscard = { vm.discardAllQueued() },
784+ onValueChange = vm::updateDraft,
786+ aborting = aborting,
787+ enabled = activeConnection != null,
788+ sendOnEnter = sendOnEnter,
790+ onAbort = { showStopConfirm = true },
791+ queuedFollowUp = queuedFollowUp,
792+ onQueueFollowUp = vm::queueFollowUp,
793+ onCancelQueue = { vm.queueFollowUp("") },
794+ focusRequester = inputFocusRequester,
795+ attachments = attachments,
796+ staging = stagingCount > 0,
797+ onRemoveAttachment = vm::removeAttachment,
800+ PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly),
803+ onPickFile = { filePicker.launch("*/*") },
804+ onCamera = { launchCamera() },
805+ onVoice = { launchVoice() },
806+ onPasteImage = { stageUris(it) },
811+ // Collect messages inside the content lambda so streaming token updates
812+ // recompose only this subtree, not the top bar / input bar / 20+ other
813+ // state reads in the ChatScreen body.
814+ val messages by vm.messages.collectAsStateWithLifecycle()
815+ val optimisticStatuses by vm.optimisticStatuses.collectAsStateWithLifecycle()
816+ val listState = rememberLazyListState()
817+ val contentScope = rememberCoroutineScope()
818+ val timeTick = rememberRelativeTimeTick()
819+ // Dismiss the soft keyboard when the user scrolls the message list, a standard Android
820+ // chat affordance for reading while the IME is open. Watching isScrollInProgress via
821+ // snapshotFlow avoids wiring a nested scroll connection and fires on any scroll gesture.
822+ val keyboardController = androidx.compose.ui.platform.LocalSoftwareKeyboardController.current
823+ androidx.compose.runtime.LaunchedEffect(listState) {
824+ androidx.compose.runtime.snapshotFlow { listState.isScrollInProgress }
825+ .distinctUntilChanged()
826+ .collect { scrolling -> if (scrolling) keyboardController?.hide() }
827+ } // Group messages by day and interleave date separators so a long conversation
828+ // has visual "Today"/"Yesterday"/date breaks. Computed once per messages
829+ // emission (memoized) so a scroll-induced recomposition doesn't re-scan. Lives
830+ // outside the PullToRefreshBox so the auto-scroll effects below can reference
831+ // the same list the LazyColumn renders (separators shift the indices vs. the
832+ // raw messages list, so scroll targets must be in listItems space).
833+ val todayLabel = stringResource(R.string.today)
834+ val yesterdayLabel = stringResource(R.string.yesterday)
835+ // In-conversation search: when active, narrow to messages whose text contains the query.
836+ // Computed on the filtered set so day separators still align with the visible messages.
837+ val searchQuery = chatSearch.trim()
838+ val searchMessages = remember(messages, searchQuery) {
839+ if (searchQuery.isEmpty()) messages
840+ else messages.filter { m ->
841+ m.parts.any { p -> (p as? TextPart)?.text?.contains(searchQuery, ignoreCase = true) == true }
844+ // Keep the focused-match index in range as the query (and thus the match set) changes.
845+ // A new query resets to the first match; a shrunk set clamps to the last valid index.
846+ LaunchedEffect(searchQuery, searchMessages.size) {
847+ if (searchPos >= searchMessages.size) searchPos = 0
849+ val listItems = remember(searchMessages, todayLabel, yesterdayLabel) {
850+ buildMessageListItems(searchMessages, todayLabel, yesterdayLabel)
852+ // listItems is a plain (non-snapshot) local, rebuilt each recomposition. The
853+ // derivedStateOf and snapshotFlow lambdas below are created once (keyless
854+ // remember / LaunchedEffect(Unit)); reading `listItems` directly inside them
855+ // would capture the first composition's list — which is empty, since `messages`
856+ // starts empty — freezing it forever. That pinned isPinnedToBottom at true and
857+ // AutoScrollSignal.size at 0, breaking streaming auto-scroll and the
858+ // jump-to-latest FAB. rememberUpdatedState hands those lambdas a stable State
859+ // whose value tracks the latest list.
860+ val currentListItems by rememberUpdatedState(listItems)
862+ // New-content signal for the jump-to-latest FAB: counts how many new items arrived
863+ // while the user was scrolled away from the bottom, cleared once they return. A count
864+ // (vs. a bare dot) tells the user how much they missed at a glance.
865+ var newContentCount by remember { mutableIntStateOf(0) }
867+ val isPinnedToBottom by remember {
869+ val lastVisible = listState.layoutInfo.visibleItemsInfo.lastOrNull()?.index ?: -1
870+ // When a run is active, the LazyColumn has one trailing "__typing"
871+ // item after the last list item. Account for it so "pinned to bottom"
872+ // includes the working indicator — otherwise the pin check targets
873+ // listItems.lastIndex, the typing row sits just below the viewport,
874+ // and the user never sees the progress indicator even when pinned.
875+ val items = currentListItems
876+ val effectiveLast = if (running) items.size else items.lastIndex
877+ lastVisible >= effectiveLast || items.isEmpty()
881+ // Jump to the newest message once, when the conversation first loads. Guarded by a
882+ // saveable one-shot flag so it doesn't re-fire on recomposition after a config change
883+ // (rotation) — rememberLazyListState restores the user's scroll offset, and an
884+ // unconditional scrollToItem here would clobber it, snapping them back to the bottom.
885+ var didInitialScroll by rememberSaveable { mutableStateOf(false) }
886+ LaunchedEffect(listItems.isNotEmpty()) {
887+ if (listItems.isNotEmpty() && !didInitialScroll) {
888+ // When a run is active the LazyColumn has a trailing "__typing" row at
889+ // listItems.size; target it (not listItems.lastIndex) so deep-linking into a
890+ // running session lands truly pinned to bottom and streaming auto-scroll engages.
891+ listState.scrollToItem(if (running) listItems.size else listItems.lastIndex)
892+ didInitialScroll = true
896+ // When a run starts, the LazyColumn gains a trailing "__typing" row, so
897+ // effectiveLast jumps from listItems.lastIndex to listItems.size and the
898+ // isPinnedToBottom check flips to false the instant you send — freezing
899+ // auto-scroll. If the user was pinned to the last item just before the run
900+ // began, bring the typing row into view so the pin (and streaming follow) is
901+ // preserved. Guard on the raw last-visible index (isPinnedToBottom has already
902+ // recomputed to false by now) so we don't scroll when the user had scrolled up.
903+ LaunchedEffect(running) {
904+ if (running && listItems.isNotEmpty()) {
905+ val lastVisible = listState.layoutInfo.visibleItemsInfo.lastOrNull()?.index ?: -1
906+ if (lastVisible >= listItems.lastIndex) {
907+ runCatchingCancellable { listState.animateScrollToItem(listItems.size) }
912+ // Scroll the focused search match into view whenever the user steps through results with
913+ // the up/down arrows. Resolves the filtered message to its slot in listItems (which
914+ // includes date separators) and animates there.
915+ LaunchedEffect(searchPos, searchQuery, listItems) {
916+ if (!searchActive || searchQuery.isEmpty()) return@LaunchedEffect
917+ val target = searchMessages.getOrNull(searchPos) ?: return@LaunchedEffect
918+ val index = listItems.indexOfFirst {
919+ it is MessageListItem.Message && it.message.info.id == target.info.id
921+ if (index >= 0) runCatchingCancellable { listState.animateScrollToItem(index) }
924+ LaunchedEffect(Unit) {
928+ // Track list size + the streaming length of the last part so we auto-scroll
929+ // as content arrives, plus the pinned flag. A small data class with primitive
930+ // fields avoids the per-frame boxing that Triple<Int,Int,Boolean> pays
931+ // (snapshotFlow re-evaluates this lambda every snapshot), and reading only the
932+ // last part's length is O(1) vs. a sumOf over every part. Covering reasoning
933+ // and tool output — not just TextPart — keeps a pinned view following a long
934+ // reasoning/tool block while it streams (previously it stalled at 0).
935+ val lastLen = streamingContentLength(messages.lastOrNull()?.parts?.lastOrNull())
936+ AutoScrollSignal(currentListItems.size, lastLen, isPinnedToBottom)
937+ }.collect { signal ->
938+ if (signal.pinned) {
939+ // Back at the bottom: clear the new-content badge.
941+ if (signal.size > 0) {
942+ // Scroll to the effective last index, including the trailing
943+ // "__typing" row when a run is active so the working indicator
944+ // is brought into view (not just the last message).
945+ val items = currentListItems
946+ val target = if (running) items.size else items.lastIndex
947+ listState.scrollToItem(target)
949+ } else if (signal.size > prevSize || signal.lastTextLength > prevLen) {
950+ // Content arrived while scrolled up — increment the jump-to-latest badge.
951+ // Only count whole new items (not per-token growth) so the number reflects
952+ // new messages, not a token count.
953+ if (signal.size > prevSize) newContentCount += signal.size - prevSize
954+ else if (newContentCount == 0) newContentCount = 1
956+ prevSize = signal.size
957+ prevLen = signal.lastTextLength
961+ CompositionLocalProvider(LocalRelativeTimeTick provides timeTick) {
962+ // The bottomBar (ChatInputBar) already has imePadding, and the Scaffold's
963+ // content padding accounts for the bottomBar's raised height, so the message
964+ // list is already above the keyboard. Adding imePadding here would double-apply
965+ // the IME inset and push messages too far up.
966+ Box(modifier = Modifier.fillMaxSize().padding(padding)) {
967+ if (activeConnection == null) {
969+ modifier = Modifier.align(Alignment.Center).padding(24.dp),
970+ horizontalAlignment = Alignment.CenterHorizontally,
973+ stringResource(R.string.not_connected),
974+ color = MaterialTheme.colorScheme.error,
976+ Spacer(Modifier.size(12.dp))
977+ Button(onClick = { vm.reconnect() }, enabled = !reconnecting) {
979+ CircularProgressIndicator(
980+ Modifier.size(20.dp),
981+ color = MaterialTheme.colorScheme.onPrimary,
985+ Text(stringResource(R.string.reconnect))
990+ val bannerVisible = !isOnline || connectionState != EventStreamClient.ConnectionState.Connected
991+ // Measure the banner's actual height instead of hardcoding 44dp. At large
992+ // accessibility font scales the banner's labelMedium text + spinner + Retry
993+ // button exceed 44dp, so a fixed inset lets the first message hide behind it
994+ // (the same hazard FileViewScreen's overlay calls out). onSizeChanged only
995+ // fires while the banner is composed (visible), so gate the inset on
996+ // bannerVisible and fall back to 44dp until the first measurement arrives.
997+ var bannerHeightPx by remember { mutableIntStateOf(0) }
998+ val density = LocalDensity.current
999+ val topPad = if (bannerVisible) {
1000+ if (bannerHeightPx > 0) {
1001+ with(density) { bannerHeightPx.toDp() } + 4.dp
1009+ state = connectionState,
1010+ modifier = Modifier.align(Alignment.TopCenter).onSizeChanged { bannerHeightPx = it.height },
1011+ isOnline = isOnline,
1012+ // On a hard failure, retry by forcing an SSE reconnect (which
1013+ // re-seeds from REST). refreshMessages is the right recovery path
1014+ // when the connection is present but the stream died; reconnect()
1015+ // is the path when the whole connection is gone (handled by the
1016+ // separate "Not connected" state below).
1017+ onRetry = { vm.refreshMessages() },
1019+ // Pull-to-refresh wraps ALL content states (loading, error, empty, list) so
1020+ // recovery is a swipe away everywhere — not just when the message list is
1021+ // populated. Previously PTR was only around the populated list, leaving the
1022+ // error/loading states reachable only via a button tap.
1024+ isRefreshing = refreshing,
1025+ onRefresh = { vm.refreshMessages() },
1026+ // Cap width and center on large screens for readability — full-width
1027+ // bubbles stretch edge-to-edge on tablets, hurting line-length comfort.
1028+ modifier = Modifier.fillMaxSize().wrapContentWidth(Alignment.CenterHorizontally)
1029+ .widthIn(max = NetworkConfig.chatContentMaxWidthDp.dp),
1031+ // Reserve top space when the banner is visible so it doesn't overlap the
1032+ // first message / state content. (topPad computed above.)
1033+ if (loading && messages.isEmpty()) {
1034+ val loadingLabel = stringResource(R.string.loading)
1035+ // Skeleton bubbles pre-structure the layout so the perceived load is faster
1036+ // than a bare centered spinner and the first real messages pop into an
1037+ // already-shaped list instead of a blank-then-pop. Alpha pulses (gated by
1038+ // reduced-motion) to convey ongoing work; the column carries the a11y label.
1039+ val reducedMotion = LocalReducedMotion.current
1040+ val transition = rememberInfiniteTransition(label = "skeleton")
1041+ val pulse by transition.animateFloat(
1042+ initialValue = 0.35f,
1043+ targetValue = 0.7f,
1044+ animationSpec = infiniteRepeatable(tween(900), RepeatMode.Reverse),
1045+ label = "skeletonAlpha",
1047+ val skeletonAlpha = if (reducedMotion) 0.5f else pulse
1048+ val skeletonColor = MaterialTheme.colorScheme.surfaceVariant
1050+ fun bar(widthFraction: Float) {
1053+ .fillMaxWidth(widthFraction)
1055+ .clip(RoundedCornerShape(4.dp))
1056+ .background(skeletonColor)
1057+ .alpha(skeletonAlpha),
1061+ modifier = Modifier
1062+ .align(Alignment.TopCenter)
1063+ .padding(top = topPad)
1066+ .semantics { contentDescription = loadingLabel },
1067+ verticalArrangement = Arrangement.spacedBy(14.dp),
1070+ val left = i % 2 == 0
1072+ modifier = Modifier.fillMaxWidth(),
1073+ horizontalArrangement = if (left) Arrangement.Start else Arrangement.End,
1076+ modifier = Modifier.fillMaxWidth(if (left) 0.7f else 0.55f),
1077+ verticalArrangement = Arrangement.spacedBy(6.dp),
1085+ } else if (loadError && messages.isEmpty()) {
1086+ // A failed load with nothing to show. Distinct from the empty
1087+ // conversation state below — the conversation isn't empty, it
1088+ // failed to load, and offering "Start a conversation" here is
1089+ // misleading. Offer a Retry instead, mirroring SessionListScreen.
1091+ modifier = Modifier.align(Alignment.Center).padding(top = topPad).padding(24.dp),
1092+ horizontalAlignment = Alignment.CenterHorizontally,
1095+ stringResource(R.string.load_messages_failed),
1096+ color = MaterialTheme.colorScheme.error,
1098+ Spacer(Modifier.size(12.dp))
1099+ Button(onClick = { vm.refreshMessages() }) {
1100+ Text(stringResource(R.string.retry))
1103+ } else if (messages.isEmpty() && running) {
1104+ // A run just started but no parts have arrived yet — the empty
1105+ // list with only the trailing "working…" row looks broken, so
1106+ // surface a clear starting state until the first part streams in.
1107+ val workingText = stringResource(R.string.working)
1109+ modifier = Modifier.align(Alignment.Center).padding(top = topPad).padding(24.dp),
1110+ horizontalAlignment = Alignment.CenterHorizontally,
1112+ CircularProgressIndicator(Modifier.size(28.dp), strokeWidth = 3.dp)
1113+ Spacer(Modifier.size(12.dp))
1114+ Text(workingText, style = MaterialTheme.typography.bodyMedium, color = MaterialTheme.colorScheme.onSurfaceVariant)
1116+ } else if (messages.isEmpty()) {
1120+ // Focus the input so the user can edit the suggestion before
1121+ // sending, instead of having to manually tap the field.
1122+ runCatching { inputFocusRequester.requestFocus() }
1124+ modifier = Modifier.align(Alignment.Center).padding(top = topPad),
1127+ val lastMessageId = messages.lastOrNull()?.info?.id
1128+ // Text-to-speech for reading assistant replies aloud; shuts down when the
1129+ // message list leaves composition.
1130+ val tts = rememberTtsController()
1131+ val speakingMessageId by tts.speakingId
1132+ val ttsState by tts.state
1135+ modifier = Modifier.fillMaxSize(),
1136+ // Extra bottom padding reserves room for the jump-to-latest FAB so it
1137+ // never floats over the last message. Top padding grows when the
1138+ // connection banner is visible so it doesn't overlap the first message.
1139+ contentPadding = PaddingValues(
1143+ bottom = 16.dp + NetworkConfig.chatListFabInsetDp.dp,
1145+ verticalArrangement = Arrangement.spacedBy(16.dp),
1150+ contentType = { it.contentType },
1152+ // When in-conversation search is stepping through matches, highlight the
1153+ // focused one's row so the user can spot it at a glance among the filtered
1154+ // results (full in-text term highlighting would require threading the query
1155+ // through the markdown renderer; the row highlight + scroll-to-match covers
1156+ // the navigation use case desktop chat apps solve with next/prev).
1157+ val focusedMatchId = if (searchActive && searchQuery.isNotEmpty()) {
1158+ searchMessages.getOrNull(searchPos)?.info?.id
1160+ val isFocusedMatch = focusedMatchId != null &&
1161+ item is MessageListItem.Message &&
1162+ item.message.info.id == focusedMatchId
1163+ // Default placement animation so inserted/moved rows glide in. Skipped
1164+ // entirely under reduced motion so a streaming message growing in place
1165+ // doesn't animate on every token (and respects the a11y preference).
1168+ .then(if (LocalReducedMotion.current) Modifier else Modifier.animateItem())
1170+ if (isFocusedMatch) {
1171+ Modifier.background(
1172+ MaterialTheme.colorScheme.secondaryContainer,
1173+ MaterialTheme.shapes.medium,
1181+ is MessageListItem.Separator -> DateSeparator(item.label)
1182+ is MessageListItem.Message -> {
1183+ val message = item.message
1184+ // Only the last (streaming) message needs isRunning — it drives the
1185+ // reasoning-block spinner. Passing the live flag to every bubble
1186+ // makes all visible messages recompose whenever a run starts or stops.
1187+ val modelLabel = remember(message.info, models) {
1188+ (message.info as? soy.iko.opencode.data.model.AssistantMessage)
1189+ ?.let { resolveModelLabel(it, models) }
1191+ // Text used to drive swipe-to-reply (and pre-fill the quote). Empty for
1192+ // image/code-only messages, which then opt out of the swipe gesture.
1193+ val quoteText = remember(message.parts) {
1195+ .filterIsInstance<TextPart>()
1196+ .joinToString("\n\n") { it.text }
1198+ .takeIf { it.isNotEmpty() }
1200+ // Swipe-start-to-end reveals a reply affordance and triggers quote-reply,
1201+ // snapping back (the message isn't dismissed — the quote fills the composer).
1202+ // Only enabled when there's text to quote, so non-text messages don't
1203+ // capture horizontal drags (e.g. inside a horizontally-scrollable code block).
1204+ val swipeState = rememberSwipeToDismissBoxState(
1205+ confirmValueChange = { value ->
1206+ if (value == SwipeToDismissBoxValue.StartToEnd && quoteText != null) {
1207+ haptics.performHapticFeedback(androidx.compose.ui.hapticfeedback.HapticFeedbackType.TextHandleMove)
1208+ vm.quoteReply(quoteText)
1209+ runCatching { inputFocusRequester.requestFocus() }
1215+ state = swipeState,
1216+ enableDismissFromEndToStart = false,
1217+ enableDismissFromStartToEnd = quoteText != null,
1218+ backgroundContent = {
1220+ Modifier.fillMaxSize().padding(horizontal = 16.dp),
1221+ contentAlignment = Alignment.CenterStart,
1224+ Icons.AutoMirrored.Filled.Reply,
1225+ contentDescription = null,
1226+ tint = MaterialTheme.colorScheme.onSurfaceVariant,
1233+ isRunning = running && message.info.id == lastMessageId,
1234+ imageContext = imageContext,
1235+ modelLabel = modelLabel,
1236+ onOpenFile = onOpenFile,
1237+ onRevert = { vm.revertTo(message.info.id) },
1239+ // editMessage reverts to before this message (hiding it and
1240+ // everything after); the revert banner surfaces the rewind
1241+ // with an Undo. Focus + scroll the composer into view like the
1242+ // Quote action so the prefilled draft isn't off-screen.
1243+ haptics.performHapticFeedback(androidx.compose.ui.hapticfeedback.HapticFeedbackType.TextHandleMove)
1244+ vm.editMessage(message.info.id, text)
1245+ runCatching { inputFocusRequester.requestFocus() }
1246+ contentScope.launch {
1247+ runCatchingCancellable {
1248+ listState.animateScrollToItem(listItems.lastIndex.coerceAtLeast(0))
1252+ onSpeak = { text ->
1253+ if (!tts.toggle(message.info.id, text)) {
1254+ // TTS engine unavailable (init failed / not yet ready
1255+ // / blank text): surface feedback so the button doesn't
1256+ // appear dead. Throttled so a rapid double-tap (stop
1257+ // then immediate re-tap before ready) doesn't spam.
1258+ scope.launch { snackbar.showSnackbar(ttsUnavailableMsg) }
1261+ isSpeaking = message.info.id == speakingMessageId,
1262+ ttsState = if (message.info.id == speakingMessageId) ttsState else TtsState.IDLE,
1263+ onPause = { tts.pause() },
1264+ onResume = { tts.resume() },
1265+ onQuote = { text ->
1266+ vm.quoteReply(text)
1267+ runCatching { inputFocusRequester.requestFocus() }
1269+ onBranch = { text -> vm.branchFrom(text) },
1270+ // Regenerate is only meaningful for a finished assistant reply
1271+ // (not the actively-streaming one), so gate it on !running.
1272+ onRegenerate = if (!running) {
1273+ { vm.regenerate(message.info.id) }
1275+ sendStatus = optimisticStatuses[message.info.id]?.let { failed ->
1276+ if (failed) soy.iko.opencode.ui.chat.MessageSendStatus.FAILED
1277+ else soy.iko.opencode.ui.chat.MessageSendStatus.SENDING
1279+ isEdited = message.info.time?.let { it.updated != null && it.updated != it.created } == true,
1280+ onRetry = optimisticStatuses[message.info.id]?.let { failed ->
1282+ { vm.retryOptimisticMessage(message.info.id) }
1287+ onDismiss = optimisticStatuses[message.info.id]?.let { failed ->
1289+ { vm.dismissOptimistic(message.info.id) }
1295+ // Per-message share: build a single-message Markdown
1296+ // transcript off-thread and fire ACTION_SEND, mirroring the
1297+ // whole-conversation share but scoped to just this message.
1299+ val md = withContext(Dispatchers.Default) {
1300+ buildMessageMarkdown(message)
1302+ val send = android.content.Intent(android.content.Intent.ACTION_SEND).apply {
1303+ type = "text/markdown"
1304+ putExtra(android.content.Intent.EXTRA_SUBJECT, sessionTitle ?: defaultShareSubject)
1305+ putExtra(android.content.Intent.EXTRA_TEXT, md)
1307+ runCatchingCancellable { shareContext.startActivity(android.content.Intent.createChooser(send, shareContext.getString(R.string.share_message))) }
1308+ .onFailure { showToast(shareContext, shareContext.getString(R.string.no_share_app)) }
1318+ item(key = "__typing") {
1319+ val workingText = stringResource(R.string.working)
1320+ // Elapsed-since-run chip: the typing row only composes while a run is
1321+ // active, so anchoring the timer to first composition approximates the
1322+ // run start and gives the user a sense of how long the agent has been
1323+ // working — distinguishing a healthy long run from a hung one. Updates
1324+ // once per second; the value is decorative (the a11y label is the row's).
1325+ var startMs by remember { mutableLongStateOf(0L) }
1326+ LaunchedEffect(Unit) { startMs = System.currentTimeMillis() }
1327+ val elapsedMs by produceState(0L, startMs) {
1328+ if (startMs == 0L) return@produceState
1330+ value = (System.currentTimeMillis() - startMs).coerceAtLeast(0L)
1334+ val elapsedText = remember(elapsedMs) {
1335+ val total = elapsedMs / 1000
1336+ // Locale.US for stable digit formatting: on locales with
1337+ // non-ASCII digits (ar-/fa-) the default locale would render
1338+ // localized digits inside the Monospace block, breaking the
1339+ // fixed-width alignment the monospace family was chosen for.
1340+ String.format(java.util.Locale.US, "%d:%02d", total / 60, total % 60)
1343+ verticalAlignment = Alignment.CenterVertically,
1344+ // Animate this slot in/out so the working row doesn't pop in and
1345+ // jolt the message list when a run starts/stops — matching the
1346+ // AnimatedVisibility used by every other transient banner/FAB here.
1347+ modifier = Modifier
1349+ .padding(vertical = 4.dp),
1351+ // Merge the indicator + label + clock into one TalkBack node so
1352+ // it's announced once (the working label, then the elapsed time)
1353+ // instead of "working… / working… / 0:14" with the label read
1354+ // twice. The Stop IconButton below stays separately focusable.
1356+ verticalAlignment = Alignment.CenterVertically,
1357+ modifier = Modifier.semantics(mergeDescendants = true) {
1358+ contentDescription = "$workingText $elapsedText"
1361+ CircularProgressIndicator(Modifier.size(16.dp), strokeWidth = 2.dp)
1362+ Text(workingText, style = MaterialTheme.typography.labelMedium, modifier = Modifier.padding(start = 6.dp))
1363+ Spacer(Modifier.size(8.dp))
1365+ Icons.Filled.Schedule,
1366+ contentDescription = null,
1367+ modifier = Modifier.size(14.dp),
1368+ tint = MaterialTheme.colorScheme.onSurfaceVariant,
1372+ style = MaterialTheme.typography.labelMedium,
1373+ fontFamily = androidx.compose.ui.text.font.FontFamily.Monospace,
1374+ color = MaterialTheme.colorScheme.onSurfaceVariant,
1375+ modifier = Modifier.padding(start = 4.dp),
1378+ // A trailing Stop on the working row so the run can be aborted
1379+ // without scrolling back to the composer (which may be off-screen
1380+ // while the user reads the streaming output up-thread).
1381+ Spacer(Modifier.weight(1f))
1382+ val stopLabel = stringResource(R.string.stop)
1384+ onClick = { showStopConfirm = true },
1385+ modifier = Modifier.semantics { contentDescription = stopLabel },
1387+ Icon(Icons.Filled.Stop, contentDescription = null)
1395+ // Jump-to-latest affordance when the user has scrolled away during a stream.
1396+ val fabMotion = rememberVisibilityTransitions()
1397+ AnimatedVisibility(
1398+ visible = !isPinnedToBottom && listItems.isNotEmpty(),
1399+ enter = fabMotion.enter,
1400+ exit = fabMotion.exit,
1401+ modifier = Modifier.align(Alignment.BottomEnd),
1403+ // Badge the FAB when new content arrived while the user was scrolled up, so
1404+ // they know there's something new to jump to (cleared once back at bottom).
1407+ if (newContentCount > 0) {
1408+ val newContentLabel = stringResource(R.string.new_messages)
1409+ Badge(modifier = Modifier.semantics { contentDescription = newContentLabel }) {
1410+ Text(if (newContentCount > 99) "99+" else newContentCount.toString())
1414+ modifier = Modifier.padding(end = 16.dp, bottom = 16.dp),
1416+ ExtendedFloatingActionButton(
1418+ newContentCount = 0
1419+ if (listItems.isNotEmpty()) {
1420+ // Scroll to the effective last index, including the
1421+ // trailing "__typing" row when a run is active.
1422+ val target = if (running) listItems.size else listItems.lastIndex
1423+ contentScope.launch { runCatchingCancellable { listState.animateScrollToItem(target) } }
1426+ // contentDescription null: the visible "Latest" text already labels
1427+ // the FAB, so a description here makes TalkBack announce "Latest" twice.
1428+ icon = { Icon(Icons.Filled.KeyboardArrowDown, contentDescription = null) },
1429+ text = { Text(stringResource(R.string.latest)) },
1433+ // Scroll-to-top affordance: a small FAB at the start edge, visible when the
1434+ // user has scrolled down past the first few messages. Symmetric to the
1435+ // jump-to-latest FAB, for long conversations where manual fling back is tedious.
1436+ val scrolledDown by remember {
1437+ derivedStateOf { listState.firstVisibleItemIndex > 0 || listState.firstVisibleItemScrollOffset > 200 }
1439+ AnimatedVisibility(
1440+ visible = scrolledDown && listItems.size > 4,
1441+ enter = fabMotion.enter,
1442+ exit = fabMotion.exit,
1443+ modifier = Modifier.align(Alignment.BottomStart),
1445+ SmallFloatingActionButton(
1447+ contentScope.launch { runCatchingCancellable { listState.animateScrollToItem(0) } }
1449+ modifier = Modifier.padding(start = 16.dp, bottom = 16.dp),
1452+ Icons.Filled.KeyboardArrowUp,
1453+ contentDescription = stringResource(R.string.scroll_to_top),
1457+ // In-conversation search bar. Overlays the list so typing filters messages in
1458+ // place; the match count + a no-matches note give immediate feedback.
1459+ val searchMotion = rememberVisibilityTransitions()
1460+ AnimatedVisibility(
1461+ visible = searchActive,
1462+ enter = searchMotion.enter,
1463+ exit = searchMotion.exit,
1464+ modifier = Modifier.align(Alignment.TopCenter),
1467+ tonalElevation = 3.dp,
1468+ modifier = Modifier
1469+ .padding(horizontal = 12.dp, vertical = 4.dp)
1473+ modifier = Modifier.padding(start = 8.dp, end = 4.dp),
1474+ verticalAlignment = Alignment.CenterVertically,
1477+ value = chatSearch,
1478+ onValueChange = { chatSearch = it },
1479+ modifier = Modifier.weight(1f),
1480+ placeholder = { Text(stringResource(R.string.search_in_conversation)) },
1482+ leadingIcon = { Icon(Icons.Filled.Search, contentDescription = null) },
1483+ keyboardOptions = KeyboardOptions(imeAction = ImeAction.Search),
1485+ // Match counter: "N of M" where N is the focused match (1-based) and
1486+ // M is the total matching messages. Only shown when there's a query.
1487+ val matchCount = searchMessages.size
1489+ if (searchQuery.isNotEmpty() && matchCount > 0) {
1490+ stringResource(R.string.search_match_count, searchPos + 1, matchCount)
1491+ } else if (searchQuery.isNotEmpty()) {
1492+ stringResource(R.string.search_match_count, 0, messages.size)
1494+ style = MaterialTheme.typography.labelSmall,
1495+ color = MaterialTheme.colorScheme.onSurfaceVariant,
1496+ modifier = Modifier.padding(horizontal = 4.dp),
1498+ // Step through matches (Previous / Next), disabled when there are
1499+ // fewer than two to navigate. Wrap around at the ends so repeated
1500+ // taps cycle through every hit.
1501+ val prevLabel = stringResource(R.string.find_previous)
1502+ val nextLabel = stringResource(R.string.find_next)
1505+ if (matchCount > 0) {
1506+ searchPos = (searchPos - 1 + matchCount) % matchCount
1509+ enabled = matchCount > 1,
1511+ Icon(Icons.Filled.KeyboardArrowUp, contentDescription = prevLabel)
1515+ if (matchCount > 0) {
1516+ searchPos = (searchPos + 1) % matchCount
1519+ enabled = matchCount > 1,
1521+ Icon(Icons.Filled.KeyboardArrowDown, contentDescription = nextLabel)
1523+ IconButton(onClick = {
1524+ searchActive = false
1527+ Icon(Icons.Filled.Close, contentDescription = stringResource(R.string.clear_search))
1532+ // When a search yields no matches (but the conversation isn't empty), say so
1533+ // instead of showing a blank list.
1534+ if (searchActive && searchQuery.isNotEmpty() && searchMessages.isEmpty() && messages.isNotEmpty()) {
1536+ stringResource(R.string.search_no_matches),
1537+ style = MaterialTheme.typography.bodyMedium,
1538+ color = MaterialTheme.colorScheme.onSurfaceVariant,
1539+ modifier = Modifier.align(Alignment.Center),
1547+ if (showExitConfirm) {
1549+ onDismissRequest = { showExitConfirm = false },
1550+ title = { Text(stringResource(R.string.stop_and_exit_title)) },
1551+ text = { Text(stringResource(R.string.stop_and_exit_text)) },
1553+ TextButton(onClick = {
1554+ showExitConfirm = false
1555+ haptics.performHapticFeedback(androidx.compose.ui.hapticfeedback.HapticFeedbackType.TextHandleMove)
1558+ }) { Text(stringResource(R.string.stop_and_exit)) }
1561+ TextButton(onClick = { showExitConfirm = false }) { Text(stringResource(R.string.stay)) }
1566+ if (showStopConfirm) {
1568+ onDismissRequest = { showStopConfirm = false },
1569+ title = { Text(stringResource(R.string.stop_run_title)) },
1570+ text = { Text(stringResource(R.string.stop_run_text)) },
1572+ TextButton(onClick = {
1573+ showStopConfirm = false
1574+ haptics.performHapticFeedback(androidx.compose.ui.hapticfeedback.HapticFeedbackType.TextHandleMove)
1576+ }) { Text(stringResource(R.string.stop), color = MaterialTheme.colorScheme.error) }
1579+ TextButton(onClick = { showStopConfirm = false }) { Text(stringResource(R.string.cancel)) }
1584+ if (showSummarizeConfirm) {
1586+ onDismissRequest = { showSummarizeConfirm = false },
1587+ title = { Text(stringResource(R.string.summarize_confirm_title)) },
1588+ text = { Text(stringResource(R.string.summarize_confirm_text)) },
1590+ TextButton(onClick = {
1591+ showSummarizeConfirm = false
1593+ }) { Text(stringResource(R.string.action_continue)) }
1596+ TextButton(onClick = { showSummarizeConfirm = false }) { Text(stringResource(R.string.cancel)) }
1601+ if (showInitConfirm) {
1603+ onDismissRequest = { showInitConfirm = false },
1604+ title = { Text(stringResource(R.string.init_confirm_title)) },
1605+ text = { Text(stringResource(R.string.init_confirm_text)) },
1607+ TextButton(onClick = {
1608+ showInitConfirm = false
1610+ }) { Text(stringResource(R.string.action_continue)) }
1613+ TextButton(onClick = { showInitConfirm = false }) { Text(stringResource(R.string.cancel)) }
1618+ if (showRenameDialog) {
1619+ RenameSessionChatDialog(
1620+ initialTitle = sessionTitle ?: "",
1621+ onDismiss = { showRenameDialog = false },
1622+ onConfirm = { newName ->
1623+ showRenameDialog = false
1624+ vm.renameSession(newName)
1629+ if (showDeleteDialog) {
1631+ onDismissRequest = { showDeleteDialog = false },
1632+ title = { Text(stringResource(R.string.delete_session_chat_title)) },
1633+ text = { Text(stringResource(R.string.delete_session_chat_text, sessionTitle ?: sessionLabel)) },
1635+ TextButton(onClick = {
1636+ showDeleteDialog = false
1637+ haptics.performHapticFeedback(androidx.compose.ui.hapticfeedback.HapticFeedbackType.LongPress)
1639+ }) { Text(stringResource(R.string.delete), color = MaterialTheme.colorScheme.error) }
1642+ TextButton(onClick = { showDeleteDialog = false }) { Text(stringResource(R.string.cancel)) }
1647+ if (showShellDialog) {
1648+ ShellCommandDialog(
1649+ onDismiss = { showShellDialog = false },
1651+ showShellDialog = false
1657+ if (showModelPicker) {
1660+ selected = selectedModel,
1661+ loading = modelsLoading,
1662+ error = modelsError,
1663+ onSelect = { vm.selectModel(it) },
1664+ onRetry = { vm.reloadModels() },
1665+ onDismiss = { showModelPicker = false },
1669+ if (showAgentPicker) {
1672+ selected = selectedAgent,
1673+ loading = agentsLoading,
1674+ error = agentsError,
1675+ onSelect = { vm.selectAgent(it?.name) },
1676+ onRetry = { vm.reloadAgents() },
1677+ onDismiss = { showAgentPicker = false },
1681+ if (showCommandPicker) {
1682+ CommandPickerSheet(
1683+ commands = commands,
1684+ loading = commandsLoading,
1685+ error = commandsError,
1686+ onSelect = { vm.runCommand(it) },
1687+ onRetry = { vm.reloadCommands() },
1688+ onDismiss = { showCommandPicker = false },
1692+ pendingPermission?.let { permission ->
1694+ permission = permission,
1695+ position = permissionProgress.position,
1696+ total = permissionProgress.total,
1697+ onRespond = { response -> vm.respondPermission(permission, response) },
1702+ CommandPalette(actions = paletteActions, onDismiss = { showPalette = false })
1705+ if (showShortcutsDialog) {
1706+ // Surfaces the hardware-keyboard shortcuts (Ctrl+K palette, Escape stop, Enter send)
1707+ // that are otherwise wired but undiscoverable. A simple key/value list; Enter behavior
1708+ // depends on the "Send on Enter" setting, noted inline.
1710+ onDismissRequest = { showShortcutsDialog = false },
1711+ title = { Text(stringResource(R.string.keyboard_shortcuts_title)) },
1713+ Column(verticalArrangement = Arrangement.spacedBy(10.dp)) {
1714+ ShortcutRow("Ctrl + K", stringResource(R.string.shortcut_open_palette))
1715+ ShortcutRow("Esc", stringResource(R.string.shortcut_stop_run))
1716+ ShortcutRow("Esc", stringResource(R.string.shortcut_close))
1717+ ShortcutRow("Enter", stringResource(R.string.shortcut_send))
1718+ ShortcutRow("Shift + Enter / Ctrl + Enter", stringResource(R.string.shortcut_newline))
1722+ TextButton(onClick = { showShortcutsDialog = false }) { Text(stringResource(R.string.action_continue)) }
1729+private fun ShortcutRow(keys: String, desc: String) {
1730+ Row(verticalAlignment = Alignment.CenterVertically) {
1733+ style = MaterialTheme.typography.labelLarge,
1734+ fontFamily = androidx.compose.ui.text.font.FontFamily.Monospace,
1735+ modifier = Modifier.widthIn(min = 160.dp),
1739+ style = MaterialTheme.typography.bodyMedium,
1740+ color = MaterialTheme.colorScheme.onSurfaceVariant,
1745+@OptIn(androidx.compose.foundation.ExperimentalFoundationApi::class)
1747+private fun ChatInputBar(
1749+ onValueChange: (String) -> Unit,
1753+ sendOnEnter: Boolean,
1754+ onSend: () -> Unit,
1755+ onAbort: () -> Unit,
1756+ queuedFollowUp: String?,
1757+ onQueueFollowUp: (String) -> Unit,
1758+ onCancelQueue: () -> Unit,
1759+ focusRequester: androidx.compose.ui.focus.FocusRequester,
1760+ attachments: List<PendingAttachment>,
1762+ onRemoveAttachment: (String) -> Unit,
1763+ onPickPhoto: () -> Unit,
1764+ onPickFile: () -> Unit,
1765+ onCamera: () -> Unit,
1766+ onVoice: () -> Unit,
1767+ onPasteImage: (List<Uri>) -> Unit,
1769+ // Sendable when there's text OR at least one attachment (an image-only prompt is valid).
1770+ val hasContent = value.isNotBlank() || attachments.isNotEmpty()
1771+ // Full-screen editor state: opened when the user wants more space for a long prompt.
1772+ var showFullScreenEditor by rememberSaveable { mutableStateOf(false) }
1773+ Surface(tonalElevation = 3.dp, modifier = Modifier.imePadding()) {
1774+ Column(modifier = Modifier.fillMaxWidth().windowInsetsPadding(WindowInsets.navigationBars)) {
1775+ // A queued follow-up replaces the Stop button with a "queued" chip so the
1776+ // user sees their message will be sent when the run finishes, and can cancel.
1777+ if (queuedFollowUp != null) {
1778+ val editLabel = stringResource(R.string.edit)
1780+ modifier = Modifier
1782+ .padding(start = 12.dp, top = 6.dp, end = 4.dp),
1783+ verticalAlignment = Alignment.CenterVertically,
1785+ // Tapping the chip reloads the queued text into the draft (and clears the
1786+ // queue) so a typo spotted while queuing can be fixed without retyping.
1788+ modifier = Modifier
1790+ .clip(MaterialTheme.shapes.small)
1791+ .clickable(role = Role.Button, onClickLabel = editLabel) {
1792+ onValueChange(queuedFollowUp)
1795+ .padding(vertical = 4.dp),
1798+ stringResource(R.string.queued),
1799+ style = MaterialTheme.typography.labelSmall,
1800+ color = MaterialTheme.colorScheme.onSurfaceVariant,
1804+ style = MaterialTheme.typography.bodySmall,
1805+ color = MaterialTheme.colorScheme.onSurfaceVariant,
1807+ overflow = TextOverflow.Ellipsis,
1810+ TextButton(onClick = onCancelQueue, contentPadding = androidx.compose.foundation.layout.PaddingValues(horizontal = 8.dp)) {
1811+ Text(stringResource(R.string.cancel))
1815+ // Staged attachments: horizontally-scrollable thumbnails/chips, each removable.
1816+ AttachmentStrip(attachments, onRemove = onRemoveAttachment, staging = staging)
1818+ modifier = Modifier.fillMaxWidth().padding(8.dp),
1819+ verticalAlignment = Alignment.Bottom,
1821+ // Attach menu: photo picker, camera, or any file.
1822+ var showAttachMenu by remember { mutableStateOf(false) }
1824+ IconButton(onClick = { showAttachMenu = true }, enabled = enabled) {
1825+ Icon(Icons.Filled.Add, contentDescription = stringResource(R.string.add_attachment))
1827+ DropdownMenu(expanded = showAttachMenu, onDismissRequest = { showAttachMenu = false }) {
1829+ leadingIcon = { Icon(Icons.Filled.PhotoLibrary, contentDescription = null) },
1830+ text = { Text(stringResource(R.string.attach_photo)) },
1831+ onClick = { showAttachMenu = false; onPickPhoto() },
1834+ leadingIcon = { Icon(Icons.Filled.PhotoCamera, contentDescription = null) },
1835+ text = { Text(stringResource(R.string.attach_camera)) },
1836+ onClick = { showAttachMenu = false; onCamera() },
1839+ leadingIcon = { Icon(Icons.Filled.Description, contentDescription = null) },
1840+ text = { Text(stringResource(R.string.attach_file)) },
1841+ onClick = { showAttachMenu = false; onPickFile() },
1845+ // Voice dictation: appends recognized speech to the draft.
1846+ IconButton(onClick = onVoice, enabled = enabled) {
1847+ Icon(Icons.Filled.Mic, contentDescription = stringResource(R.string.voice_input))
1849+ // Expand to a full-screen editor for long prompts (the inline field caps at 6 lines).
1850+ IconButton(onClick = { showFullScreenEditor = true }, enabled = enabled) {
1851+ Icon(Icons.Filled.Expand, contentDescription = stringResource(R.string.expand_editor))
1855+ onValueChange = { v ->
1856+ // Cap input length so a huge paste can't stall the UI. Shares are
1857+ // capped separately in MainActivity; this guards typed/pasted drafts.
1858+ onValueChange(v.take(NetworkConfig.maxDraftLengthChars))
1860+ modifier = Modifier
1862+ .focusRequester(focusRequester)
1863+ .testTag("chat_input")
1864+ // Intercept image content pasted/dropped into the field (keyboard paste
1865+ // of a copied screenshot, drag from Files, etc.) and route it through the
1866+ // same staging path as the attach menu. Non-image content (text) falls
1867+ // through unchanged so normal typing/paste works.
1870+ androidx.compose.foundation.content.ReceiveContentListener { transferable ->
1871+ val clipData = transferable.clipEntry?.clipData
1872+ if (clipData != null) {
1873+ val imageUris = (0 until clipData.itemCount).mapNotNull { i ->
1874+ val item = clipData.getItemAt(i)
1875+ item.uri?.takeIf { uri ->
1876+ item.text == null && uri.toString().let { s ->
1877+ s.startsWith("content:") || s.startsWith("file:")
1881+ if (imageUris.isNotEmpty()) {
1882+ onPasteImage(imageUris)
1889+ .onPreviewKeyEvent { event ->
1890+ if (event.type != KeyEventType.KeyDown || event.key != Key.Enter) return@onPreviewKeyEvent false
1891+ // With "Send on Enter" on, Enter sends (Shift+Enter newlines).
1892+ // With it off, Enter inserts a newline and Ctrl+Enter sends.
1893+ val send = enterShouldSend(
1895+ hasContent = hasContent,
1896+ sendOnEnter = sendOnEnter,
1897+ shift = event.isShiftPressed,
1898+ ctrl = event.isCtrlPressed,
1901+ if (running) onQueueFollowUp(value) else onSend()
1907+ // While a run is active a typed message only queues (sends after the run), so
1908+ // the placeholder says so instead of the plain "Message…".
1910+ Text(stringResource(if (running) R.string.composer_hint_running else R.string.message_placeholder))
1914+ // Show a "N / max" countdown once the draft crosses a high fraction of
1915+ // the cap, so the user knows a paste is about to be truncated instead
1916+ // of being silently cut off. Hidden for normal short prompts to avoid
1917+ // clutter under a typical one-line message.
1919+ val threshold = (NetworkConfig.maxDraftLengthChars * NetworkConfig.draftCountdownThresholdFraction).toInt()
1920+ if (value.length >= threshold) {
1923+ R.string.draft_chars_remaining,
1924+ NetworkConfig.maxDraftLengthChars - value.length,
1925+ NetworkConfig.maxDraftLengthChars,
1927+ style = MaterialTheme.typography.labelSmall,
1928+ color = if (value.length >= NetworkConfig.maxDraftLengthChars) MaterialTheme.colorScheme.error
1929+ else MaterialTheme.colorScheme.onSurfaceVariant,
1933+ keyboardOptions = KeyboardOptions(
1934+ // When "Send on Enter" is on, expose ImeAction.Send so the soft
1935+ // keyboard shows a send key — matching the setting's intent and
1936+ // giving touch-only users a keyboard send affordance. When off,
1937+ // ImeAction.Default keeps the enter/newline key for multi-line input.
1938+ imeAction = if (sendOnEnter) ImeAction.Send else ImeAction.Default,
1939+ capitalization = KeyboardCapitalization.Sentences,
1941+ keyboardActions = KeyboardActions(
1943+ if (enabled && hasContent) onSend()
1947+ ComposerTrailingButton(
1949+ aborting = aborting,
1950+ canSend = enabled && hasContent,
1951+ canQueue = enabled && value.isNotBlank(),
1954+ onQueue = { onQueueFollowUp(value) },
1958+ if (showFullScreenEditor) {
1961+ onValueChange = onValueChange,
1962+ onDismiss = { showFullScreenEditor = false },
1964+ showFullScreenEditor = false
1965+ // Mirror the inline composer's behavior: while a run is active a send
1966+ // can't go immediately, so queue the follow-up instead of silently
1967+ // dropping it (vm.send() returns false during a run, which previously
1968+ // lost the typed message with no feedback).
1969+ if (running) onQueueFollowUp(value) else onSend()
1971+ canSend = enabled && hasContent,
1973+ sendOnEnter = sendOnEnter,
1974+ attachments = attachments,
1975+ onRemoveAttachment = onRemoveAttachment,
1982+/** Full-screen editor dialog for composing long prompts. Gives the full viewport height to
1983+ * the text field (vs the inline 6-line cap) with a Send button and a collapse action. */
1984+@OptIn(ExperimentalMaterial3Api::class)
1986+private fun FullScreenEditor(
1988+ onValueChange: (String) -> Unit,
1989+ onDismiss: () -> Unit,
1990+ onSend: () -> Unit,
1992+ running: Boolean = false,
1993+ sendOnEnter: Boolean = false,
1994+ attachments: List<PendingAttachment> = emptyList(),
1995+ onRemoveAttachment: (String) -> Unit = {},
1996+ staging: Boolean = false,
1998+ androidx.compose.ui.window.Dialog(
1999+ onDismissRequest = onDismiss,
2000+ properties = androidx.compose.ui.window.DialogProperties(usePlatformDefaultWidth = false, decorFitsSystemWindows = false),
2004+ androidx.compose.material3.TopAppBar(
2005+ title = { Text(stringResource(R.string.message_placeholder)) },
2007+ androidx.compose.material3.TextButton(onClick = onDismiss) {
2008+ Text(stringResource(R.string.collapse_editor))
2014+ Surface(tonalElevation = 3.dp) {
2016+ modifier = Modifier.fillMaxWidth().imePadding().padding(12.dp),
2017+ horizontalArrangement = Arrangement.End,
2019+ androidx.compose.material3.Button(onClick = onSend, enabled = canSend) {
2020+ Text(stringResource(if (running) R.string.queue else R.string.send))
2026+ Column(modifier = Modifier.fillMaxSize().padding(padding)) {
2027+ // Show staged attachments in the full-screen editor too, so a user who staged
2028+ // images then expanded for a long prompt can still see and remove them without
2029+ // collapsing first (mirrors the inline composer's AttachmentStrip).
2030+ AttachmentStrip(attachments, onRemove = onRemoveAttachment, staging = staging)
2033+ onValueChange = { v -> onValueChange(v.take(NetworkConfig.maxDraftLengthChars)) },
2034+ modifier = Modifier
2037+ // Honor the "Send on Enter" setting here too, so the full-screen
2038+ // editor matches the inline composer: Enter sends (Shift+Enter
2039+ // newlines) when on; Enter inserts a newline and Ctrl+Enter sends
2040+ // when off. Previously this hard-coded ImeAction.Default and had no
2041+ // key handler, so a hardware-keyboard user got a different behaviour
2042+ // from the inline composer depending on which they happened to open.
2043+ .onPreviewKeyEvent { event ->
2044+ if (event.type != KeyEventType.KeyDown || event.key != Key.Enter) return@onPreviewKeyEvent false
2045+ val send = enterShouldSend(
2047+ hasContent = canSend,
2048+ sendOnEnter = sendOnEnter,
2049+ shift = event.isShiftPressed,
2050+ ctrl = event.isCtrlPressed,
2052+ if (send) { onSend(); true } else false
2054+ placeholder = { Text(stringResource(R.string.message_placeholder)) },
2055+ maxLines = Int.MAX_VALUE,
2056+ keyboardOptions = KeyboardOptions(
2057+ imeAction = if (sendOnEnter) ImeAction.Send else ImeAction.Default,
2058+ capitalization = KeyboardCapitalization.Sentences,
2060+ keyboardActions = KeyboardActions(
2061+ onSend = { if (canSend) onSend() },
2069+/** Whether a hardware Enter keypress should send: never when disabled/empty; otherwise
2070+ * Enter sends (unless Shift) when send-on-Enter is on, else only Ctrl+Enter sends. */
2071+private fun enterShouldSend(
2073+ hasContent: Boolean,
2074+ sendOnEnter: Boolean,
2078+ !enabled || !hasContent -> false
2079+ sendOnEnter -> !shift
2083+/** The composer's trailing button: while a run is active a Stop (with in-flight spinner),
2084+ * preceded by a Queue button when the user has typed a follow-up; otherwise Send. Extracted so
2085+ * [ChatInputBar] stays under the complexity threshold. */
2087+private fun ComposerTrailingButton(
2092+ onSend: () -> Unit,
2093+ onAbort: () -> Unit,
2094+ onQueue: () -> Unit,
2097+ Row(verticalAlignment = Alignment.CenterVertically) {
2098+ // While a run is active the trailing action is Stop, so the only send affordance is
2099+ // the keyboard's IME action — which is a plain newline when "Send on Enter" is off.
2100+ // That leaves a touch-only user with the setting off no way to queue a follow-up
2101+ // (Ctrl+Enter needs a hardware keyboard). Surface an explicit Queue button whenever
2102+ // there's typed TEXT so queuing is always reachable, without changing the user's
2103+ // Enter-inserts-a-newline preference. Gate on typed text — not the send-oriented
2104+ // canSend, which is also true for an attachment-only composer — because queuing "" just
2105+ // calls setQueuedFollowUp(null), silently cancelling any already-queued follow-up and
2106+ // dropping the staged attachment (queued follow-ups don't carry attachments anyway).
2110+ modifier = Modifier.testTag("queue_button"),
2112+ // PlaylistAdd reads as "add to a queue/list" more clearly than a clock
2113+ // icon (which can be read as "snooze" or "later"), and isn't confused with
2114+ // the Send action sitting right beside the Stop button.
2116+ Icons.AutoMirrored.Filled.PlaylistAdd,
2117+ contentDescription = stringResource(R.string.queue_followup),
2121+ // Show a spinner while the abort REST call is in flight so the user sees the stop
2122+ // was sent, and disable to prevent a double-tap.
2125+ enabled = !aborting,
2126+ modifier = Modifier.padding(start = 4.dp).testTag("stop_button"),
2129+ val stopLabel = stringResource(R.string.stop)
2130+ CircularProgressIndicator(
2131+ Modifier.size(18.dp).semantics { contentDescription = stopLabel },
2132+ strokeWidth = 2.dp,
2135+ Icon(Icons.Filled.Stop, contentDescription = stringResource(R.string.stop))
2143+ modifier = Modifier.padding(start = 4.dp).testTag("send_button"),
2145+ Icon(Icons.AutoMirrored.Filled.Send, contentDescription = stringResource(R.string.send))
2150+@OptIn(androidx.compose.foundation.layout.ExperimentalLayoutApi::class)
2152+private fun EmptyConversation(
2153+ onSuggestion: (String) -> Unit,
2154+ modifier: Modifier = Modifier,
2156+ // A few example prompts so a first-time user has a one-tap path to their first
2157+ // message instead of staring at a blank input. Tapping a chip fills the draft
2158+ // (without sending) so the user can edit it before sending.
2159+ val suggestions = remember {
2161+ R.string.empty_chat_suggest_1,
2162+ R.string.empty_chat_suggest_2,
2163+ R.string.empty_chat_suggest_3,
2167+ modifier = modifier.padding(32.dp),
2168+ horizontalAlignment = Alignment.CenterHorizontally,
2171+ Icons.Filled.AutoAwesome,
2172+ contentDescription = null,
2173+ modifier = Modifier.size(48.dp),
2174+ tint = MaterialTheme.colorScheme.onSurfaceVariant,
2176+ Spacer(Modifier.size(16.dp))
2178+ stringResource(R.string.empty_chat_title),
2179+ style = MaterialTheme.typography.titleMedium,
2181+ Spacer(Modifier.size(8.dp))
2183+ stringResource(R.string.empty_chat_hint),
2184+ style = MaterialTheme.typography.bodyMedium,
2185+ color = MaterialTheme.colorScheme.onSurfaceVariant,
2187+ Spacer(Modifier.size(20.dp))
2188+ // Suggestion chips wrap across rows on narrow screens instead of stacking full-width,
2189+ // so a longer localized prompt doesn't push the chips off-screen on a phone.
2191+ modifier = Modifier.fillMaxWidth(),
2192+ horizontalArrangement = Arrangement.spacedBy(8.dp),
2193+ verticalArrangement = Arrangement.spacedBy(8.dp),
2195+ suggestions.forEach { resId ->
2196+ val text = stringResource(resId)
2197+ androidx.compose.material3.AssistChip(
2198+ onClick = { onSuggestion(text) },
2199+ label = { Text(text, style = MaterialTheme.typography.bodyMedium) },
2206+/** A banner shown while a revert checkpoint is active, offering an Undo (unrevert). When the
2207+ * server included a diff of the revert, a collapsible "Show what changed" affordance reveals
2208+ * it inline via [DiffView] so the user can see what was rolled back before deciding to undo. */
2210+private fun RevertBanner(diff: String?, onUndo: () -> Unit) {
2211+ val haptics = LocalHapticFeedback.current
2212+ var showDiff by rememberSaveable { mutableStateOf(false) }
2213+ val expandMotion = rememberVisibilityTransitions()
2215+ color = MaterialTheme.colorScheme.tertiaryContainer,
2216+ tonalElevation = 2.dp,
2217+ modifier = Modifier.fillMaxWidth(),
2221+ modifier = Modifier.fillMaxWidth().padding(start = 12.dp, end = 4.dp, top = 4.dp, bottom = 4.dp),
2222+ verticalAlignment = Alignment.CenterVertically,
2223+ horizontalArrangement = Arrangement.SpaceBetween,
2225+ Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.weight(1f)) {
2227+ Icons.Filled.Restore,
2228+ contentDescription = null,
2229+ tint = MaterialTheme.colorScheme.onTertiaryContainer,
2230+ modifier = Modifier.size(18.dp),
2233+ stringResource(R.string.reverted_banner),
2234+ style = MaterialTheme.typography.bodySmall,
2235+ color = MaterialTheme.colorScheme.onTertiaryContainer,
2236+ modifier = Modifier.padding(start = 8.dp),
2238+ // "Show what changed" toggle, shown only when the server provided a diff.
2239+ // Collapsible so the banner stays compact by default; a user curious about
2240+ // what the revert rolled back can expand it inline without leaving the chat.
2241+ if (!diff.isNullOrBlank()) {
2244+ haptics.performHapticFeedback(androidx.compose.ui.hapticfeedback.HapticFeedbackType.TextHandleMove)
2245+ showDiff = !showDiff
2247+ contentPadding = androidx.compose.foundation.layout.PaddingValues(start = 8.dp, end = 4.dp),
2250+ if (showDiff) Icons.Filled.ExpandLess else Icons.Filled.ExpandMore,
2251+ contentDescription = null,
2252+ modifier = Modifier.size(16.dp),
2255+ stringResource(if (showDiff) R.string.revert_hide_diff else R.string.revert_show_diff),
2256+ style = MaterialTheme.typography.labelSmall,
2257+ modifier = Modifier.padding(start = 4.dp),
2262+ TextButton(onClick = {
2263+ // Haptic on undo to match the other destructive/important confirmations: a
2264+ // revert restores potentially hundreds of hidden messages.
2265+ haptics.performHapticFeedback(androidx.compose.ui.hapticfeedback.HapticFeedbackType.TextHandleMove)
2267+ }) { Text(stringResource(R.string.undo)) }
2269+ // The diff render lives inside the banner's Surface so it inherits the tertiary
2270+ // tonal container and reads as part of the banner, not a floating panel.
2271+ if (!diff.isNullOrBlank()) {
2272+ AnimatedVisibility(
2273+ visible = showDiff,
2274+ enter = expandMotion.enter,
2275+ exit = expandMotion.exit,
2277+ Column(modifier = Modifier.padding(start = 12.dp, end = 12.dp, bottom = 8.dp)) {
2279+ stringResource(R.string.revert_diff_header),
2280+ style = MaterialTheme.typography.labelSmall,
2281+ color = MaterialTheme.colorScheme.onTertiaryContainer,
2282+ modifier = Modifier.padding(bottom = 4.dp),
2284+ DiffView(diff, saveKey = "revertDiff")
2292+/** A banner shown while messages composed offline are queued in the outbox, with a
2293+ * "Send now" nudge and a discard. Shows a spinner while the queue is flushing. */
2295+private fun OutboxBanner(
2298+ onFlush: () -> Unit,
2299+ onDiscard: () -> Unit,
2302+ color = MaterialTheme.colorScheme.secondaryContainer,
2303+ tonalElevation = 2.dp,
2304+ modifier = Modifier.fillMaxWidth(),
2307+ modifier = Modifier.fillMaxWidth().padding(start = 12.dp, end = 4.dp, top = 4.dp, bottom = 4.dp),
2308+ verticalAlignment = Alignment.CenterVertically,
2309+ horizontalArrangement = Arrangement.SpaceBetween,
2311+ Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.weight(1f)) {
2313+ CircularProgressIndicator(
2314+ Modifier.size(16.dp),
2315+ strokeWidth = 2.dp,
2316+ color = MaterialTheme.colorScheme.onSecondaryContainer,
2318+ Spacer(Modifier.size(8.dp))
2322+ stringResource(R.string.outbox_sending)
2324+ val res = if (count == 1) R.string.outbox_pending_one else R.string.outbox_pending_many
2325+ stringResource(res, count)
2327+ style = MaterialTheme.typography.bodySmall,
2328+ color = MaterialTheme.colorScheme.onSecondaryContainer,
2332+ val haptics = LocalHapticFeedback.current
2333+ TextButton(onClick = onFlush) { Text(stringResource(R.string.outbox_flush)) }
2334+ TextButton(onClick = {
2335+ // Haptic on the irreversible "Discard queued" (drops every queued message
2336+ // for this session), matching the error-colored destructive actions.
2337+ haptics.performHapticFeedback(androidx.compose.ui.hapticfeedback.HapticFeedbackType.LongPress)
2340+ Text(stringResource(R.string.outbox_discard_all), color = MaterialTheme.colorScheme.error)
2347+/** Prompt for a one-off shell command to run in the session's worktree. */
2349+private fun ShellCommandDialog(onDismiss: () -> Unit, onRun: (String) -> Unit) {
2350+ var command by rememberSaveable { mutableStateOf("") }
2352+ onDismissRequest = onDismiss,
2353+ title = { Text(stringResource(R.string.run_shell_command)) },
2357+ onValueChange = { command = it },
2358+ modifier = Modifier.fillMaxWidth(),
2359+ placeholder = { Text(stringResource(R.string.shell_command_hint)) },
2360+ label = { Text(stringResource(R.string.shell_command_label)) },
2362+ keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
2363+ keyboardActions = KeyboardActions(onDone = { if (command.isNotBlank()) onRun(command.trim()) }),
2368+ onClick = { if (command.isNotBlank()) onRun(command.trim()) },
2369+ enabled = command.isNotBlank(),
2370+ ) { Text(stringResource(R.string.run)) }
2372+ dismissButton = { TextButton(onClick = onDismiss) { Text(stringResource(R.string.cancel)) } },
2377+ * Change signal for the auto-scroll-on-stream watcher. Primitive fields avoid the
2378+ * boxing `Triple<Int, Int, Boolean>` would impose on every snapshot evaluation, and
2379+ * equality is structural so `snapshotFlow` emits only when something relevant changes.
2381+private data class AutoScrollSignal(val size: Int, val lastTextLength: Int, val pinned: Boolean)
2383+/** Length of the streaming content carried by [part], driving auto-scroll as it grows.
2384+ * Covers text, reasoning, and tool output so a pinned view keeps following non-text
2385+ * streaming, not just plain assistant text. O(1). */
2386+private fun streamingContentLength(part: Part?): Int = when (part) {
2387+ is TextPart -> part.text.length
2388+ is ReasoningPart -> part.text.length
2389+ is ToolPart -> when (val st = part.state) {
2390+ is ToolCompleted -> st.output?.length ?: 0
2391+ is ToolError -> st.error?.length ?: 0
2392+ is ToolRunning -> st.title?.length ?: 0
2398+/** Rename dialog for the currently-open session. Mirrors the SessionListScreen rename
2399+ * dialog but lives in the chat screen so the user can rename without backing out to
2400+ * the list. Pre-filled with the current title; the cap matches the list dialog. */
2402+private fun RenameSessionChatDialog(
2403+ initialTitle: String,
2404+ onDismiss: () -> Unit,
2405+ onConfirm: (String) -> Unit,
2407+ // Seed the field once (no key input) so a later external title change — e.g. opencode
2408+ // auto-generating a session title after the first prompt, arriving via SessionUpdated —
2409+ // doesn't re-initialize the box and discard the user's in-progress edit.
2410+ var title by rememberSaveable { mutableStateOf(initialTitle) }
2412+ onDismissRequest = onDismiss,
2413+ title = { Text(stringResource(R.string.rename_session_chat)) },
2417+ onValueChange = { v -> title = v.take(NetworkConfig.maxSessionTitleChars) },
2418+ modifier = Modifier.fillMaxWidth(),
2419+ placeholder = { Text(stringResource(R.string.session_title_hint)) },
2420+ label = { Text(stringResource(R.string.rename_session_chat)) },
2422+ keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
2423+ keyboardActions = KeyboardActions(onDone = { if (title.isNotBlank()) onConfirm(title.trim()) }),
2427+ TextButton(onClick = { if (title.isNotBlank()) onConfirm(title.trim()) }, enabled = title.isNotBlank()) {
2428+ Text(stringResource(R.string.save))
2432+ TextButton(onClick = onDismiss) { Text(stringResource(R.string.cancel)) }
2438+ * A single item in the chat message list — either a [Message] bubble or a day
2439+ * [Separator] inserted before the first message of a new day. The separator shifts
2440+ * the LazyColumn indices vs. the raw messages list, so scroll-target math must use
2441+ * the [List] of these items (not `messages.size`/`lastIndex`).
2443+private sealed interface MessageListItem {
2445+ val contentType: Any
2447+ data class Message(val message: MessageWithParts, override val key: Any) : MessageListItem {
2448+ override val contentType: Any get() = message.info::class
2451+ data class Separator(val label: String, val ordinal: Int) : MessageListItem {
2452+ // Key on the day-occurrence ordinal, not the label: two non-contiguous groups
2453+ // can share a label (e.g. an untimestamped message — empty label — between two
2454+ // same-day messages, or two untimestamped groups), and a label-only key would
2455+ // then collide, which makes LazyColumn throw on duplicate keys. The ordinal is
2456+ // stable for a given message ordering so slots aren't needlessly recreated.
2457+ override val key: Any get() = "sep_${ordinal}_$label"
2458+ // Separators share a contentType so the LazyColumn can recycle their slots.
2459+ override val contentType: Any get() = "separator"
2464+ * Build the interleaved list of [MessageListItem]s for the message list, inserting a
2465+ * [MessageListItem.Separator] before the first message of each new calendar day. Uses
2466+ * the message's `time.created` (falling back to `updated` then `completed`) to bucket.
2467+ * Messages with no timestamp are grouped under an empty-label separator only if they
2468+ * start the list, so a server that omits timestamps doesn't suppress the first divider.
2470+ * [todayLabel]/[yesterdayLabel] are resolved by the caller (a @Composable can't call
2471+ * stringResource from inside this plain function) and used for the "Today"/"Yesterday"
2472+ * labels; older days fall back to a locale-stable medium-date format.
2474+private fun buildMessageListItems(
2475+ messages: List<MessageWithParts>,
2476+ todayLabel: String,
2477+ yesterdayLabel: String,
2478+): List<MessageListItem> {
2479+ if (messages.isEmpty()) return emptyList()
2480+ val today = java.time.LocalDate.now(java.time.ZoneId.systemDefault())
2481+ val dateFmt = mediumDateFormat()
2482+ val result = ArrayList<MessageListItem>(messages.size + 4)
2483+ val seenKeys = HashSet<String>(messages.size * 2)
2484+ var lastDayKey: String? = null
2486+ var emptyIdOrdinal = 0
2488+ for (message in messages) {
2489+ val ts = message.info.time?.created ?: message.info.time?.updated ?: message.info.time?.completed
2490+ val dayKey = ts?.let { dayKey(it) } ?: ""
2491+ // Emit a separator before the very first message (so a leading un-timestamped message
2492+ // still gets the first divider, empty label and all), and thereafter only when crossing
2493+ // into a new NON-empty calendar day. An un-timestamped message mid-list (empty key)
2494+ // neither emits its own separator nor resets the day — otherwise a following same-day
2495+ // message would see a "" -> "<today>" transition and emit a duplicate "Today" header.
2496+ if (first || (dayKey.isNotEmpty() && dayKey != lastDayKey)) {
2497+ result.add(MessageListItem.Separator(dayLabel(dayKey, ts ?: 0L, today, todayLabel, yesterdayLabel, dateFmt), sepOrdinal++))
2499+ if (dayKey.isNotEmpty()) lastDayKey = dayKey
2501+ // Most messages have a unique non-empty id. An unrecognized-role message can carry an
2502+ // empty id — and the reducer intentionally keeps several such messages as distinct
2503+ // holders — so give each a stable synthetic key and defensively de-collide any remaining
2504+ // duplicate; otherwise LazyColumn throws on a duplicate key and the whole screen crashes.
2505+ var key = message.info.id.ifEmpty { "msg-empty-${emptyIdOrdinal++}" }
2506+ if (!seenKeys.add(key)) {
2508+ while (!seenKeys.add("$key#$suffix")) suffix++
2509+ key = "$key#$suffix"
2511+ result.add(MessageListItem.Message(message, key))
2517+ * A stable bucket key for a timestamp's calendar day (epoch-days as a string).
2519+ * [buildMessageListItems] re-runs on every streaming snapshot (the memo is keyed on the
2520+ * whole `messages` list, which must stay so the live message's fresh parts render), so a
2521+ * naive implementation would re-allocate an `Instant`/`ZonedDateTime`/`LocalDate` for
2522+ * every message on every token. Message timestamps are immutable once assigned, so we
2523+ * memoize the parse per epoch-millis: after the first snapshot, each unchanged message is
2524+ * a cheap hash lookup. Bounded LRU (access-order) caps memory; only touched on the main
2525+ * thread during composition, so the coarse lock is uncontended.
2527+private val dayKeyCache = object : LinkedHashMap<Long, String>(128, 0.75f, true) {
2528+ override fun removeEldestEntry(eldest: MutableMap.MutableEntry<Long, String>): Boolean = size > 1024
2531+private fun dayKey(epochMillis: Long): String = synchronized(dayKeyCache) {
2532+ dayKeyCache.getOrPut(epochMillis) {
2533+ java.time.Instant.ofEpochMilli(epochMillis)
2534+ .atZone(java.time.ZoneId.systemDefault()).toLocalDate().toEpochDay().toString()
2538+// The medium-date formatter is an ICU locale lookup; cache one instance instead of
2539+// allocating a fresh one per buildMessageListItems call, rebuilding only if the default
2540+// locale changes at runtime. Main-thread-only, so no synchronization is needed.
2541+private var cachedDateFmt: java.text.DateFormat? = null
2542+private var cachedDateFmtLocale: java.util.Locale? = null
2544+private fun mediumDateFormat(): java.text.DateFormat {
2545+ val locale = java.util.Locale.getDefault()
2546+ val existing = cachedDateFmt
2547+ if (existing != null && cachedDateFmtLocale == locale) return existing
2548+ return java.text.DateFormat.getDateInstance(java.text.DateFormat.MEDIUM).also {
2550+ cachedDateFmtLocale = locale
2555+ * Resolve a day-key back to a human-readable label. "Today" / "Yesterday" for the
2556+ * recent days, otherwise a locale-stable medium-date format. Returns an empty string
2557+ * when [dayKey] is empty (no timestamp) so no separator is rendered for un-timestamped
2560+private fun dayLabel(
2563+ today: java.time.LocalDate,
2564+ todayLabel: String,
2565+ yesterdayLabel: String,
2566+ dateFmt: java.text.DateFormat,
2568+ if (dayKey.isEmpty()) return ""
2569+ val epochDay = dayKey.toLongOrNull() ?: return ""
2570+ val date = java.time.LocalDate.ofEpochDay(epochDay)
2572+ date == today -> todayLabel
2573+ date == today.minusDays(1) -> yesterdayLabel
2574+ else -> dateFmt.format(java.util.Date(ts))
2579+ * A centered day divider in the message list. Renders nothing for an empty label
2580+ * (the no-timestamp case) so un-timestamped messages don't get a stray blank divider.
2583+private fun DateSeparator(label: String) {
2584+ if (label.isBlank()) return
2586+ modifier = Modifier.fillMaxWidth(),
2587+ horizontalArrangement = Arrangement.Center,
2590+ color = MaterialTheme.colorScheme.surfaceVariant,
2591+ shape = MaterialTheme.shapes.small,
2592+ // Marked as a heading so TalkBack users can navigate day-by-day with the
2593+ // heading-skip gesture — the canonical affordance for date dividers in a list.
2594+ modifier = Modifier.semantics(mergeDescendants = true) { heading() },
2598+ style = MaterialTheme.typography.labelSmall,
2599+ color = MaterialTheme.colorScheme.onSurfaceVariant,
2600+ modifier = Modifier.padding(horizontal = 12.dp, vertical = 4.dp),