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:
@@ -0,0 +1,74 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart';
|
||||
import 'package:pipelines_app/features/chat/speech/speech.dart';
|
||||
import 'package:pipelines_app/features/chat/ui/transcript/chat_read_aloud_button.dart';
|
||||
|
||||
import '../speech/fake_speech_engine.dart';
|
||||
import 'chat_thread_fixtures.dart';
|
||||
|
||||
Widget _thread(FakeChatGateway gateway, String sessionId) => MaterialApp(
|
||||
theme: ThemeData(platform: TargetPlatform.android),
|
||||
home: Scaffold(
|
||||
body: ChatThreadView(gateway: gateway, sessionId: sessionId, onBack: () {}),
|
||||
),
|
||||
);
|
||||
|
||||
Future<(FakeSpeechEngine, SpeechController, FakeChatGateway)> _reading(
|
||||
WidgetTester tester,
|
||||
) async {
|
||||
tester.view.physicalSize = const Size(800, 1600);
|
||||
tester.view.devicePixelRatio = 1;
|
||||
addTearDown(tester.view.reset);
|
||||
final engine = FakeSpeechEngine();
|
||||
final speech = installFakeSpeech(engine);
|
||||
final gateway = FakeChatGateway();
|
||||
addTearDown(gateway.close);
|
||||
gateway.history = (_) async => chatPage([
|
||||
chatMessage(
|
||||
'a1',
|
||||
'assistant',
|
||||
parts: [
|
||||
{'type': 'text', 'text': 'Hello from the agent.'},
|
||||
],
|
||||
),
|
||||
]);
|
||||
await tester.pumpWidget(_thread(gateway, 's1'));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.byTooltip(chatReadAloudLabel));
|
||||
await tester.pump();
|
||||
engine.onStart!();
|
||||
expect(engine.spoken, ['Hello from the agent.']);
|
||||
expect(speech.messageId, startsWith(speechScopePrefix('s1')));
|
||||
return (engine, speech, gateway);
|
||||
}
|
||||
|
||||
void main() {
|
||||
testWidgets('switching the pane to another chat stops reading', (
|
||||
tester,
|
||||
) async {
|
||||
final (engine, speech, gateway) = await _reading(tester);
|
||||
await tester.pumpWidget(_thread(gateway, 's2'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(speech.state, SpeechState.idle);
|
||||
expect(engine.calls, contains('stop'));
|
||||
});
|
||||
|
||||
testWidgets('closing the chat stops reading', (tester) async {
|
||||
final (engine, speech, _) = await _reading(tester);
|
||||
await tester.pumpWidget(const MaterialApp(home: SizedBox()));
|
||||
await tester.pumpAndSettle();
|
||||
expect(speech.state, SpeechState.idle);
|
||||
expect(engine.calls.last, 'stop');
|
||||
});
|
||||
|
||||
testWidgets('closing a chat leaves another chat reading', (tester) async {
|
||||
final (engine, speech, _) = await _reading(tester);
|
||||
await speech.read(speechMessageId('elsewhere', 'b1'), ['Other reply']);
|
||||
engine.onStart!();
|
||||
await tester.pumpWidget(const MaterialApp(home: SizedBox()));
|
||||
await tester.pumpAndSettle();
|
||||
expect(speech.state, SpeechState.speaking);
|
||||
expect(speech.messageId, speechMessageId('elsewhere', 'b1'));
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user