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

51 lines
1.7 KiB
Dart

import 'package:flutter/foundation.dart';
/// One platform voice. [locale] is a BCP 47 tag such as `en-GB`.
@immutable
class SpeechVoice {
const SpeechVoice({required this.name, required this.locale});
final String name;
final String locale;
/// The primary language subtag (`en` for `en-GB`), lower case.
String get language => locale.split(RegExp('[-_]')).first.toLowerCase();
@override
bool operator ==(Object other) =>
other is SpeechVoice && other.name == name && other.locale == locale;
@override
int get hashCode => Object.hash(name, locale);
}
/// The platform text-to-speech service the [SpeechController] drives.
///
/// [setRate] takes a multiplier where 1.0 is the platform's normal speed; an
/// implementation maps it onto its own scale. [onProgress] reports the word
/// being spoken as offsets into [utterance], the text the platform is actually
/// speaking (after a resume that can be a suffix of the text passed to
/// [speak]). Platforms without word boundaries never call it.
abstract class SpeechEngine {
/// Whether this platform can speak at all; the UI hides read aloud if not.
bool get supported;
VoidCallback? onStart;
VoidCallback? onPause;
VoidCallback? onResume;
VoidCallback? onCompletion;
VoidCallback? onCancel;
ValueChanged<String>? onError;
void Function(String utterance, int start, int end)? onProgress;
/// Speaks [text]. Called with the same text while paused, it resumes.
Future<void> speak(String text);
Future<void> pause();
Future<void> stop();
/// Selects [voice], or the platform default when null.
Future<void> setVoice(SpeechVoice? voice);
Future<void> setRate(double rate);
Future<List<SpeechVoice>> voices();
}