Read aloud on a chat's replies, through the instance's text-to-speech
CI / check (push) Canceled after 0s

ReadAloud (one at a time, stopped when the chat is left) is provided to the
transcript only when /api/v2/me says the instance has text-to-speech; the
reply's foot row gains Read aloud / Stop reading beside Copy. Errors go to the
chat's snackbar through explain().

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
HomerandClaude Opus 5.5 committed 2026-11-10 12:15:56 +00:00
1 parent cb9ad78ee9
commit bd6c8df192
4 files changed
+108 -12

No files matched your search

+3
View File
@@ -16,6 +16,9 @@ is in alpha and releases come off `main`.
install a release itself.
- **Archived chats**, in the drawer under Scheduled: every archived chat, newest first. A tap
opens one; **Restore chat** puts it back among your chats.
- **Read aloud on a reply** in a chat, beside Copy, when your instance has text-to-speech: the
same voice the assistant answers in. Tap again (or another reply's) to stop; leaving the chat
stops it too.
### Changed
@@ -68,6 +68,7 @@ import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.material3.rememberModalBottomSheetState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
@@ -135,18 +136,27 @@ fun ChatScreen(
}
}
ChatContent(
state = state,
actions = model,
agent = isAgent,
snackbar = snackbar,
onMenu = onMenu,
onNewChat = onNewChat,
onLeave = onLeave,
onActivity = { activityOpen = true },
onShow = { showing = it },
onKind = onKind,
)
// Read aloud on a reply, when the instance has a voice; it stops when the chat is left.
val context = LocalContext.current
val scope = rememberCoroutineScope()
val readAloud = remember { ReadAloud(context, scope) { said -> scope.launch { snackbar.showSnackbar(said) } } }
LaunchedEffect(Unit) { readAloud.load() }
DisposableEffect(Unit) { onDispose { readAloud.stop() } }
CompositionLocalProvider(LocalReadAloud provides readAloud.takeIf { it.available }) {
ChatContent(
state = state,
actions = model,
agent = isAgent,
snackbar = snackbar,
onMenu = onMenu,
onNewChat = onNewChat,
onLeave = onLeave,
onActivity = { activityOpen = true },
onShow = { showing = it },
onKind = onKind,
)
}
if (activityOpen) {
// A device's files are on the device: the sheet lists them, the tree cannot open them.
@@ -0,0 +1,69 @@
package eu.llembas.android.ui.chat
import android.content.Context
import androidx.compose.runtime.Stable
import androidx.compose.runtime.compositionLocalOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import eu.llembas.android.assist.AssistController
import eu.llembas.android.assist.Speaker
import eu.llembas.android.lembas
import eu.llembas.android.ui.explain
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
/**
* A reply read aloud by the instance's text-to-speech (`/v1/audio/speech`), the same voice the
* assistant answers in. One at a time: another reply's Read aloud stops the first. [available]
* only when the instance says it has text-to-speech (`/api/v2/me` services), so a phone never
* shows a button that can only fail.
*/
@Stable
class ReadAloud(private val context: Context, private val scope: CoroutineScope, private val onError: (String) -> Unit) {
private val speaker = Speaker(context)
private var job: Job? = null
var available by mutableStateOf(false)
private set
/** The message being read (or fetched to be read), else null. */
var reading by mutableStateOf<String?>(null)
private set
suspend fun load() {
available = runCatching { context.lembas.instances.client()?.me()?.hasTextToSpeech == true }.getOrDefault(false)
}
fun toggle(messageId: String, markdown: String) {
if (reading == messageId) return stop()
stop()
reading = messageId
job = scope.launch {
try {
val api = context.lembas.instances.client() ?: return@launch
speaker.play(api.speech(AssistController.plain(markdown)))
speaker.playing.first { !it }
} catch (e: CancellationException) {
throw e
} catch (e: Exception) {
onError(context.explain(e))
} finally {
if (reading == messageId) reading = null
}
}
}
fun stop() {
job?.cancel()
job = null
speaker.stop()
reading = null
}
}
/** Provided by the chat; null where nothing can read aloud (the screenshots, a run's page). */
val LocalReadAloud = compositionLocalOf<ReadAloud?> { null }
@@ -34,6 +34,7 @@ import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.selection.SelectionContainer
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.VolumeUp
import androidx.compose.material.icons.filled.ContentCopy
import androidx.compose.material.icons.filled.Description
import androidx.compose.material.icons.filled.Edit
@@ -43,6 +44,7 @@ import androidx.compose.material.icons.filled.Image
import androidx.compose.material.icons.filled.Psychology
import androidx.compose.material.icons.filled.Refresh
import androidx.compose.material.icons.filled.SelectAll
import androidx.compose.material.icons.filled.Stop
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.DropdownMenu
@@ -180,6 +182,7 @@ fun AssistantMessage(
) {
val palette = LocalPalette.current
val copy = LocalCopy.current
val readAloud = LocalReadAloud.current
var menu by remember { mutableStateOf(false) }
var selecting by remember { mutableStateOf(false) }
val words = replyText(message.copy(steps = steps))
@@ -233,6 +236,17 @@ fun AssistantMessage(
)
}
}
if (readAloud != null && words.isNotBlank()) {
val reading = readAloud.reading == message.id
IconButton(onClick = { readAloud.toggle(message.id, words) }) {
Icon(
if (reading) Icons.Filled.Stop else Icons.AutoMirrored.Filled.VolumeUp,
contentDescription = stringResource(if (reading) R.string.assist_stop_reading else R.string.assist_read_aloud),
tint = if (reading) palette.accent else palette.inkFaint,
modifier = Modifier.size(20.dp),
)
}
}
if (canRegenerate) {
IconButton(onClick = onRegenerate) {
Icon(