Files
iiEsaywebUIapp/lib/features/chat/services/voice_call_service.dart

355 lines
9.7 KiB
Dart
Raw Normal View History

import 'dart:async';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import '../../../core/providers/app_providers.dart';
import '../../../core/services/socket_service.dart';
import '../providers/chat_providers.dart';
import 'text_to_speech_service.dart';
import 'voice_input_service.dart';
part 'voice_call_service.g.dart';
enum VoiceCallState {
idle,
connecting,
listening,
processing,
speaking,
error,
disconnected,
}
class VoiceCallService {
final VoiceInputService _voiceInput;
final TextToSpeechService _tts;
final SocketService _socketService;
final Ref _ref;
VoiceCallState _state = VoiceCallState.idle;
String? _sessionId;
StreamSubscription<String>? _transcriptSubscription;
StreamSubscription<int>? _intensitySubscription;
String _accumulatedTranscript = '';
bool _isDisposed = false;
SocketEventSubscription? _socketSubscription;
final StreamController<VoiceCallState> _stateController =
StreamController<VoiceCallState>.broadcast();
final StreamController<String> _transcriptController =
StreamController<String>.broadcast();
final StreamController<String> _responseController =
StreamController<String>.broadcast();
final StreamController<int> _intensityController =
StreamController<int>.broadcast();
VoiceCallService({
required VoiceInputService voiceInput,
required TextToSpeechService tts,
required SocketService socketService,
required Ref ref,
}) : _voiceInput = voiceInput,
_tts = tts,
_socketService = socketService,
_ref = ref {
_tts.bindHandlers(
onStart: _handleTtsStart,
onComplete: _handleTtsComplete,
onError: _handleTtsError,
);
}
VoiceCallState get state => _state;
Stream<VoiceCallState> get stateStream => _stateController.stream;
Stream<String> get transcriptStream => _transcriptController.stream;
Stream<String> get responseStream => _responseController.stream;
Stream<int> get intensityStream => _intensityController.stream;
Future<void> initialize() async {
if (_isDisposed) return;
// Initialize voice input
final voiceInitialized = await _voiceInput.initialize();
if (!voiceInitialized) {
_updateState(VoiceCallState.error);
throw Exception('Voice input initialization failed');
}
// Check if local STT is available
final hasLocalStt = _voiceInput.hasLocalStt;
if (!hasLocalStt) {
_updateState(VoiceCallState.error);
throw Exception('Speech recognition not available on this device');
}
// Check microphone permissions
final hasMicPermission = await _voiceInput.checkPermissions();
if (!hasMicPermission) {
_updateState(VoiceCallState.error);
throw Exception('Microphone permission not granted');
}
// Initialize TTS
await _tts.initialize();
}
Future<void> startCall(String? conversationId) async {
if (_isDisposed) return;
try {
_updateState(VoiceCallState.connecting);
// Ensure socket connection
await _socketService.ensureConnected();
_sessionId = _socketService.sessionId;
if (_sessionId == null) {
throw Exception('Failed to establish socket connection');
}
// Set up socket event listener for assistant responses
_socketSubscription = _socketService.addChatEventHandler(
conversationId: conversationId,
sessionId: _sessionId,
requireFocus: false,
handler: _handleSocketEvent,
);
// Start listening for user voice input
await _startListening();
} catch (e) {
_updateState(VoiceCallState.error);
rethrow;
}
}
Future<void> _startListening() async {
if (_isDisposed) return;
try {
_accumulatedTranscript = '';
// Check if voice input is available
if (!_voiceInput.hasLocalStt) {
_updateState(VoiceCallState.error);
throw Exception('Voice input not available on this device');
}
_updateState(VoiceCallState.listening);
final stream = await _voiceInput.beginListening();
_transcriptSubscription = stream.listen(
(text) {
if (_isDisposed) return;
_accumulatedTranscript = text;
_transcriptController.add(text);
},
onError: (error) {
if (_isDisposed) return;
_updateState(VoiceCallState.error);
},
onDone: () async {
if (_isDisposed) return;
// User stopped speaking, send message to assistant
if (_accumulatedTranscript.trim().isNotEmpty) {
await _sendMessageToAssistant(_accumulatedTranscript);
} else {
// No input, restart listening
await _startListening();
}
},
);
// Forward intensity stream for waveform visualization
_intensitySubscription = _voiceInput.intensityStream.listen(
(intensity) {
if (_isDisposed) return;
_intensityController.add(intensity);
},
);
} catch (e) {
_updateState(VoiceCallState.error);
rethrow;
}
}
Future<void> _sendMessageToAssistant(String text) async {
if (_isDisposed) return;
try {
_updateState(VoiceCallState.processing);
_accumulatedResponse = ''; // Reset response accumulator
// Send message using the existing chat infrastructure
sendMessageFromService(_ref, text, null);
} catch (e) {
_updateState(VoiceCallState.error);
rethrow;
}
}
String _accumulatedResponse = '';
void _handleSocketEvent(
Map<String, dynamic> event,
void Function(dynamic response)? ack,
) {
if (_isDisposed) return;
final outerData = event['data'];
if (outerData is Map<String, dynamic>) {
final eventType = outerData['type']?.toString();
final innerData = outerData['data'];
if (eventType == 'chat:completion' && innerData is Map<String, dynamic>) {
// Handle streaming content chunks
if (innerData.containsKey('content')) {
final content = innerData['content']?.toString() ?? '';
if (content.isNotEmpty) {
_accumulatedResponse = content;
_responseController.add(content);
}
}
// Check for completion using choices[0].finish_reason
if (innerData.containsKey('choices')) {
final choices = innerData['choices'] as List?;
if (choices != null && choices.isNotEmpty) {
final firstChoice = choices[0] as Map<String, dynamic>?;
final finishReason = firstChoice?['finish_reason'];
if (finishReason == 'stop') {
if (_accumulatedResponse.isNotEmpty) {
_speakResponse(_accumulatedResponse);
_accumulatedResponse = '';
} else {
// No response, restart listening
_startListening();
}
}
}
}
}
}
}
Future<void> _speakResponse(String response) async {
if (_isDisposed) return;
try {
// Stop listening before speaking
await _voiceInput.stopListening();
await _transcriptSubscription?.cancel();
await _intensitySubscription?.cancel();
_updateState(VoiceCallState.speaking);
await _tts.speak(response);
// After speaking completes, _handleTtsComplete will restart listening
} catch (e) {
_updateState(VoiceCallState.error);
// Restart listening even if TTS fails
await _startListening();
}
}
void _handleTtsStart() {
if (_isDisposed) return;
_updateState(VoiceCallState.speaking);
}
void _handleTtsComplete() {
if (_isDisposed) return;
// After assistant finishes speaking, start listening for user again
_startListening();
}
void _handleTtsError(String error) {
if (_isDisposed) return;
_updateState(VoiceCallState.error);
// Try to recover by restarting listening
_startListening();
}
Future<void> stopCall() async {
if (_isDisposed) return;
await _transcriptSubscription?.cancel();
await _intensitySubscription?.cancel();
_socketSubscription?.dispose();
await _voiceInput.stopListening();
await _tts.stop();
_sessionId = null;
_accumulatedTranscript = '';
_updateState(VoiceCallState.disconnected);
}
Future<void> pauseListening() async {
if (_isDisposed) return;
await _voiceInput.stopListening();
await _transcriptSubscription?.cancel();
await _intensitySubscription?.cancel();
}
Future<void> resumeListening() async {
if (_isDisposed) return;
await _startListening();
}
Future<void> cancelSpeaking() async {
if (_isDisposed) return;
await _tts.stop();
// Immediately restart listening
await _startListening();
}
void _updateState(VoiceCallState newState) {
if (_isDisposed) return;
_state = newState;
_stateController.add(newState);
}
Future<void> dispose() async {
_isDisposed = true;
await _transcriptSubscription?.cancel();
await _intensitySubscription?.cancel();
_socketSubscription?.dispose();
_voiceInput.dispose();
await _tts.dispose();
await _stateController.close();
await _transcriptController.close();
await _responseController.close();
await _intensityController.close();
}
}
@Riverpod(keepAlive: true)
VoiceCallService voiceCallService(Ref ref) {
final voiceInput = ref.watch(voiceInputServiceProvider);
final tts = TextToSpeechService();
final socketService = ref.watch(socketServiceProvider);
if (socketService == null) {
throw Exception('Socket service not available');
}
final service = VoiceCallService(
voiceInput: voiceInput,
tts: tts,
socketService: socketService,
ref: ref,
);
ref.onDispose(() {
service.dispose();
});
return service;
}