ciyidogan commited on
Commit
60ca111
·
verified ·
1 Parent(s): 4e9384b

Update flare-ui/src/app/services/conversation-manager.service.ts

Browse files
flare-ui/src/app/services/conversation-manager.service.ts CHANGED
@@ -382,69 +382,67 @@ export class ConversationManagerService implements OnDestroy {
382
  }
383
 
384
  private setupAudioPlayerHandlers(): void {
385
- if (!this.audioPlayer) return;
386
-
387
- this.audioPlayer.onended = async () => {
388
- console.log('🎵 [ConversationManager] Audio playback ended', {
389
- currentState: this.currentStateSubject.value,
390
- isRecording: this.audioService.isRecording(),
391
- timestamp: new Date().toISOString()
392
- });
393
 
394
- try {
395
- // Audio recording'in çalışmadığından emin ol
396
- if (this.audioService.isRecording()) {
397
- console.log('⚠️ [ConversationManager] Stopping existing recording first');
398
- this.audioService.stopRecording();
399
- }
400
-
401
- // Önce backend'e audio bittiğini bildir
402
- if (this.wsService.isConnected()) {
403
- console.log('📤 [ConversationManager] Sending audio_ended to backend');
404
- this.wsService.sendControl('audio_ended');
405
- }
406
-
407
- // Backend'in hazır olmasını bekle
408
- console.log('⏳ [ConversationManager] Waiting for backend to be ready...');
409
-
410
- // İlk olarak biraz bekle (network gecikmesi için)
411
- await new Promise(resolve => setTimeout(resolve, 1500));
412
-
413
- // State'in listening olmasını bekle
414
- let attempts = 0;
415
- while (this.currentStateSubject.value !== 'listening' && attempts < 20) {
416
- console.log(`⏳ [ConversationManager] Current state: ${this.currentStateSubject.value}, waiting... (attempt ${attempts + 1})`);
417
- await new Promise(resolve => setTimeout(resolve, 500));
418
- attempts++;
419
- }
420
 
421
- if (this.currentStateSubject.value === 'listening') {
422
- console.log(' [ConversationManager] State is listening, starting audio recording...');
423
-
424
- // Biraz daha bekle ki backend tamamen hazır olsun
425
- await new Promise(resolve => setTimeout(resolve, 500));
426
-
427
- if (!this.audioService.isRecording()) {
428
- await this.audioService.startRecording();
429
- console.log('✅ [ConversationManager] Audio recording started successfully');
430
- } else {
431
- console.warn('⚠️ [ConversationManager] Audio already recording, skipping');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
432
  }
433
- } else {
434
- console.error('❌ [ConversationManager] State is not listening after timeout');
435
- this.addSystemMessage('Speech recognition initialization failed. Please try again.');
 
436
  }
437
-
438
- } catch (error) {
439
- console.error('❌ [ConversationManager] Failed to handle audio end:', error);
440
- this.handleAudioError(error);
441
- }
442
- };
443
-
444
- this.audioPlayer.onerror = (error) => {
445
- console.error('Audio player error:', error);
446
- };
447
- }
448
 
449
  private stopAudioPlayback(): void {
450
  try {
 
382
  }
383
 
384
  private setupAudioPlayerHandlers(): void {
385
+ if (!this.audioPlayer) return;
 
 
 
 
 
 
 
386
 
387
+ this.audioPlayer.onended = async () => {
388
+ console.log('🎵 [ConversationManager] Audio playback ended', {
389
+ currentState: this.currentStateSubject.value,
390
+ isRecording: this.audioService.isRecording(),
391
+ timestamp: new Date().toISOString()
392
+ });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
393
 
394
+ try {
395
+ // Backend'e audio bittiğini bildir
396
+ if (this.wsService.isConnected()) {
397
+ console.log('📤 [ConversationManager] Sending audio_ended to backend');
398
+ this.wsService.sendControl('audio_ended');
399
+
400
+ // Backend'den STT ready sinyalini bekle
401
+ console.log('⏳ [ConversationManager] Waiting for STT ready signal...');
402
+
403
+ // STT ready handler'ı kur
404
+ const sttReadyPromise = new Promise<boolean>((resolve) => {
405
+ const subscription = this.wsService.message$.subscribe(message => {
406
+ if (message.type === 'stt_ready') {
407
+ console.log('✅ [ConversationManager] STT ready signal received');
408
+ subscription.unsubscribe();
409
+ resolve(true);
410
+ }
411
+ });
412
+
413
+ // 10 saniye timeout
414
+ setTimeout(() => {
415
+ subscription.unsubscribe();
416
+ resolve(false);
417
+ }, 10000);
418
+ });
419
+
420
+ const sttReady = await sttReadyPromise;
421
+
422
+ if (sttReady) {
423
+ console.log('🎤 [ConversationManager] Starting audio recording');
424
+
425
+ // Recording'i başlat
426
+ if (!this.audioService.isRecording()) {
427
+ await this.audioService.startRecording();
428
+ console.log('✅ [ConversationManager] Audio recording started');
429
+ }
430
+ } else {
431
+ console.error('❌ [ConversationManager] STT ready timeout');
432
+ this.addSystemMessage('Speech recognition initialization timeout. Please try again.');
433
+ }
434
  }
435
+
436
+ } catch (error) {
437
+ console.error(' [ConversationManager] Failed to handle audio end:', error);
438
+ this.handleAudioError(error);
439
  }
440
+ };
441
+
442
+ this.audioPlayer.onerror = (error) => {
443
+ console.error('Audio player error:', error);
444
+ };
445
+ }
 
 
 
 
 
446
 
447
  private stopAudioPlayback(): void {
448
  try {