Read chat replies aloud with word highlighting

An assistant reply gets Read aloud beside Copy; while it reads it becomes
Pause/Resume and Stop, one reply at a time app-wide. The spoken word is
highlighted in the rendered reply (across multi-part replies) and the
transcript scrolls only when that word leaves the viewport. Reading stops
when its chat closes or is swapped, another reply starts, or the app goes
to the background. Markdown is spoken as sentences; code blocks are
announced as "Code block.".

Speech goes through a SpeechEngine seam with a flutter_tts implementation
(Android, macOS, web; hidden on Linux) and a fake for tests. Settings has
a Read aloud section: a voice picker grouped by language (device language
first, a sample per voice), a rate slider and reset, saved per device.

flutter_tts has no Swift package, so macOS/iOS gain the CocoaPods
integration (Podfiles with the Runner deployment targets); Android
declares the TTS_SERVICE query. The capture fixture gains ?speech=fake.
This commit is contained in:
2026-11-13 01:29:04 +00:00
parent 26e68140fd
commit 6367cce68a
40 changed files with 2643 additions and 29 deletions
@@ -14,6 +14,8 @@ import 'chat_message_bubble.dart';
import 'chat_turn_activity_view.dart';
import 'native_disclosure.dart';
import 'chat_message_timestamp.dart';
import 'chat_read_aloud_button.dart';
import '../../speech/speech_controller.dart';
/// Builds an assistant text part; the default keeps the server's text exactly.
typedef ChatMessageTextBuilder = Widget Function(BuildContext, String);
@@ -27,9 +29,14 @@ class ChatMessageView extends StatelessWidget {
this.textBuilder,
this.footer = const [],
this.images,
this.speechScope = 'chat',
});
final JsonMap row;
/// Names the conversation in the reader's message ids, so leaving it can
/// stop only its own reply (see [speechMessageId]).
final String speechScope;
final ChatMessageTextBuilder? textBuilder;
/// Loads attached images for the operator's turn; without one, only images
@@ -179,13 +186,41 @@ class ChatMessageView extends StatelessWidget {
Widget answer,
double gutter,
) {
final copyText = row['streaming'] == true ? '' : _answerText(_partsOf(row));
final copy = copyText.isEmpty ? null : ChatCopyButton(text: copyText);
if (gutter <= 0) return _agentTurn(context, content, answer, copy);
return ChatCopyGutterRow(
width: gutter,
copy: copy,
child: _agentTurn(context, content, answer, null),
final answerParts = row['streaming'] == true
? const <String>[]
: _answerParts(_partsOf(row));
final copyText = answerParts.join('\n\n');
final id = speechMessageId(speechScope, '${row['key'] ?? row['id'] ?? ''}');
Widget? actions(Axis direction) => copyText.isEmpty
? null
: Flex(
direction: direction,
mainAxisSize: MainAxisSize.min,
verticalDirection: VerticalDirection.up,
children: [
ChatCopyButton(text: copyText),
ChatReadAloudButton(
messageId: id,
parts: answerParts,
direction: direction,
),
],
);
if (gutter <= 0) {
return _agentTurn(context, content, answer, actions(Axis.horizontal));
}
// The gutter is one button wide: Read aloud stacks above Copy, and stays
// shown on a desktop while this reply reads.
final turn = _agentTurn(context, content, answer, null);
return ListenableBuilder(
listenable: speechController,
child: turn,
builder: (context, child) => ChatCopyGutterRow(
width: gutter,
copy: actions(Axis.vertical),
pinned: speechController.messageId == id,
child: child!,
),
);
}
@@ -267,13 +302,13 @@ class ChatMessageView extends StatelessWidget {
/// The answer as the operator reads it: the turn's text parts in order,
/// without reasoning, tools or attachments.
String _answerText(List<JsonMap> parts) => [
List<String> _answerParts(List<JsonMap> parts) => [
for (final part in parts)
if ((part['type'] == null || part['type'] == 'text') &&
part['text'] is String &&
(part['text'] as String).trim().isNotEmpty)
(part['text'] as String).trim(),
].join('\n\n');
];
List<Widget> _agentParts(BuildContext context) {
final widgets = <Widget>[];