Files
pipelines/app/tool/probe_speech_fixture.dart
T
alan 6367cce68a 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.
2026-11-13 01:29:04 +00:00

93 lines
2.3 KiB
Dart

import 'dart:async';
import 'package:pipelines_app/features/chat/speech/speech.dart';
/// Opt-in (`?speech=fake`) scripted reader for captures: headless browsers
/// have no speech voices, so this engine "speaks" one word every
/// `?speechStep=` ms (default 600) and reports each as progress, which drives
/// the real controller, buttons, highlight and reveal scrolling.
void installSpeechFixture(Map<String, String> params) {
if (params['speech'] != 'fake') return;
final step = int.tryParse(params['speechStep'] ?? '') ?? 600;
debugSpeechController = SpeechController(
engine: _ScriptedSpeechEngine(Duration(milliseconds: step)),
);
}
class _ScriptedSpeechEngine extends SpeechEngine {
_ScriptedSpeechEngine(this.step);
final Duration step;
Timer? _timer;
String _text = '';
List<Match> _words = const [];
var _next = 0;
var _paused = false;
@override
bool get supported => true;
@override
Future<void> speak(String text) async {
if (_paused && text == _text) {
_paused = false;
onResume?.call();
_run();
return;
}
_text = text;
_words = RegExp(r'\S+').allMatches(text).toList();
_next = 0;
_paused = false;
Timer.run(() => onStart?.call());
_run();
}
void _run() {
_timer?.cancel();
_emit();
_timer = Timer.periodic(step, (_) => _emit());
}
void _emit() {
if (_next >= _words.length) {
_timer?.cancel();
onCompletion?.call();
return;
}
final word = _words[_next++];
onProgress?.call(_text, word.start, word.end);
}
@override
Future<void> pause() async {
_timer?.cancel();
_paused = true;
if (_next > 0) _next -= 1;
onPause?.call();
}
@override
Future<void> stop() async {
_timer?.cancel();
_paused = false;
onCancel?.call();
}
@override
Future<void> setRate(double rate) async {}
@override
Future<void> setVoice(SpeechVoice? voice) async {}
@override
Future<List<SpeechVoice>> voices() async => const [
SpeechVoice(name: 'Samantha', locale: 'en-US'),
SpeechVoice(name: 'Daniel', locale: 'en-GB'),
SpeechVoice(name: 'Karen', locale: 'en-AU'),
SpeechVoice(name: 'Thomas', locale: 'fr-FR'),
SpeechVoice(name: 'Anna', locale: 'de-DE'),
SpeechVoice(name: 'Kyoko', locale: 'ja-JP'),
];
}