Files
pipelines/app/lib/features/chat/speech/speech_preferences.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

63 lines
1.9 KiB
Dart

import 'package:shared_preferences/shared_preferences.dart';
import 'speech_engine.dart';
const speechRateKey = 'pipelines.readAloud.rate.v1';
const speechVoiceNameKey = 'pipelines.readAloud.voiceName.v1';
const speechVoiceLocaleKey = 'pipelines.readAloud.voiceLocale.v1';
/// The normal speaking rate and the slider's bounds (a multiplier).
const defaultSpeechRate = 1.0;
const minSpeechRate = 0.5;
const maxSpeechRate = 2.0;
typedef SpeechPreferences = ({double rate, SpeechVoice? voice});
/// Device-local read-aloud settings.
abstract interface class SpeechPreferencesStore {
Future<SpeechPreferences?> load();
Future<bool> save(SpeechPreferences preferences);
}
class SharedSpeechPreferencesStore implements SpeechPreferencesStore {
const SharedSpeechPreferencesStore();
@override
Future<SpeechPreferences?> load() async {
try {
final prefs = await SharedPreferences.getInstance();
final name = prefs.getString(speechVoiceNameKey);
final locale = prefs.getString(speechVoiceLocaleKey);
return (
rate: prefs.getDouble(speechRateKey) ?? defaultSpeechRate,
voice: name == null || locale == null
? null
: SpeechVoice(name: name, locale: locale),
);
} catch (_) {
return null;
}
}
@override
Future<bool> save(SpeechPreferences preferences) async {
try {
final prefs = await SharedPreferences.getInstance();
final voice = preferences.voice;
final saved = [
await prefs.setDouble(speechRateKey, preferences.rate),
if (voice == null) ...[
await prefs.remove(speechVoiceNameKey),
await prefs.remove(speechVoiceLocaleKey),
] else ...[
await prefs.setString(speechVoiceNameKey, voice.name),
await prefs.setString(speechVoiceLocaleKey, voice.locale),
],
];
return !saved.contains(false);
} catch (_) {
return false;
}
}
}