ciyidogan commited on
Commit
f8352ce
·
verified ·
1 Parent(s): 705ec4b

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

Browse files
flare-ui/src/app/services/conversation-manager.service.ts CHANGED
@@ -83,59 +83,63 @@ export class ConversationManagerService implements OnDestroy {
83
  }
84
 
85
  async startConversation(sessionId: string, config?: ConversationConfig): Promise<void> {
86
- try {
87
- if (!sessionId) {
88
- throw new Error('Session ID is required');
89
- }
90
-
91
- // Update configuration
92
- if (config) {
93
- this.conversationConfig = { ...this.conversationConfig, ...config };
94
- }
95
-
96
- this.sessionId = sessionId;
97
-
98
- // Reset state
99
- this.currentStateSubject.next('idle');
100
-
101
- // Connect WebSocket first
102
- await this.wsService.connect(sessionId).catch(error => {
103
- throw new Error(`WebSocket connection failed: ${error.message}`);
104
- });
105
-
106
- // Set up subscriptions BEFORE sending any messages
107
- this.setupSubscriptions();
108
-
109
- // Send start signal with configuration
110
- this.wsService.sendControl('start_session', this.conversationConfig);
111
-
112
- // Start audio recording AFTER WebSocket is ready
113
- await this.audioService.startRecording().catch(error => {
114
- // Disconnect WebSocket if audio fails
115
- this.wsService.disconnect();
116
- throw new Error(`Audio recording failed: ${error.message}`);
117
- });
118
-
119
- console.log('✅ Conversation started with config:', this.conversationConfig);
120
-
121
- } catch (error: any) {
122
- console.error('Failed to start conversation:', error);
123
-
124
- const conversationError: ConversationError = {
125
- type: this.determineErrorType(error),
126
- message: error.message || 'Failed to start conversation',
127
- details: error,
128
- timestamp: new Date()
129
- };
130
-
131
- this.errorSubject.next(conversationError);
132
- this.currentStateSubject.next('error');
133
- this.cleanup();
134
-
135
- throw error;
136
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
137
  }
138
-
139
  stopConversation(): void {
140
  try {
141
  // First stop audio recording
@@ -325,17 +329,14 @@ export class ConversationManagerService implements OnDestroy {
325
  private handleStateChange(from: string, to: string): void {
326
  console.log(`📊 State: ${from} → ${to}`);
327
 
328
- // Handle state-specific logic
329
- if (to === 'listening') {
330
- // Clear transcription when starting to listen
331
- this.transcriptionSubject.next('');
332
- } else if (to === 'playing_audio') {
333
- // TTS playing - visualization should be OFF
334
- console.log('🔊 Playing audio - visualization should be inactive');
335
- } else if (to === 'idle') {
336
- // Clear any remaining transcription
337
  this.transcriptionSubject.next('');
338
  }
 
 
 
339
  }
340
 
341
  private playQueuedAudio(): void {
@@ -383,7 +384,8 @@ export class ConversationManagerService implements OnDestroy {
383
  if (!this.audioPlayer) return;
384
 
385
  this.audioPlayer.onended = () => {
386
- // Notify that audio playback ended
 
387
  if (this.wsService.isConnected()) {
388
  try {
389
  this.wsService.sendControl('audio_ended');
@@ -391,12 +393,10 @@ export class ConversationManagerService implements OnDestroy {
391
  console.error('Failed to send audio_ended signal:', error);
392
  }
393
  }
394
- this.currentStateSubject.next('idle');
395
  };
396
 
397
  this.audioPlayer.onerror = (error) => {
398
  console.error('Audio player error:', error);
399
- this.currentStateSubject.next('idle');
400
  };
401
  }
402
 
 
83
  }
84
 
85
  async startConversation(sessionId: string, config?: ConversationConfig): Promise<void> {
86
+ try {
87
+ if (!sessionId) {
88
+ throw new Error('Session ID is required');
89
+ }
90
+
91
+ // Update configuration
92
+ if (config) {
93
+ this.conversationConfig = { ...this.conversationConfig, ...config };
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
  }
95
+
96
+ this.sessionId = sessionId;
97
+
98
+ // Start in listening state
99
+ this.currentStateSubject.next('listening');
100
+ console.log('🎤 Starting conversation in continuous listening mode');
101
+
102
+ // Connect WebSocket first
103
+ await this.wsService.connect(sessionId).catch(error => {
104
+ throw new Error(`WebSocket connection failed: ${error.message}`);
105
+ });
106
+
107
+ // Set up subscriptions BEFORE sending any messages
108
+ this.setupSubscriptions();
109
+
110
+ // Send start signal with configuration
111
+ this.wsService.sendControl('start_session', {
112
+ ...this.conversationConfig,
113
+ continuous_listening: true
114
+ });
115
+
116
+ // Start audio recording for continuous listening
117
+ await this.audioService.startRecording().catch(error => {
118
+ // Disconnect WebSocket if audio fails
119
+ this.wsService.disconnect();
120
+ throw new Error(`Audio recording failed: ${error.message}`);
121
+ });
122
+
123
+ console.log('✅ Conversation started with continuous listening enabled');
124
+
125
+ } catch (error: any) {
126
+ console.error('Failed to start conversation:', error);
127
+
128
+ const conversationError: ConversationError = {
129
+ type: this.determineErrorType(error),
130
+ message: error.message || 'Failed to start conversation',
131
+ details: error,
132
+ timestamp: new Date()
133
+ };
134
+
135
+ this.errorSubject.next(conversationError);
136
+ this.currentStateSubject.next('error');
137
+ this.cleanup();
138
+
139
+ throw error;
140
+ }
141
  }
142
+
143
  stopConversation(): void {
144
  try {
145
  // First stop audio recording
 
329
  private handleStateChange(from: string, to: string): void {
330
  console.log(`📊 State: ${from} → ${to}`);
331
 
332
+ // State değişimlerinde transcription'ı temizleme
333
+ // Sadece error durumunda temizle
334
+ if (to === 'error') {
 
 
 
 
 
 
335
  this.transcriptionSubject.next('');
336
  }
337
+
338
+ // Log state changes for debugging
339
+ console.log(`🎤 Continuous listening mode - state: ${to}`);
340
  }
341
 
342
  private playQueuedAudio(): void {
 
384
  if (!this.audioPlayer) return;
385
 
386
  this.audioPlayer.onended = () => {
387
+ console.log('🎵 Audio playback ended');
388
+ // Audio bittiğinde state değiştirme, listening'de kal
389
  if (this.wsService.isConnected()) {
390
  try {
391
  this.wsService.sendControl('audio_ended');
 
393
  console.error('Failed to send audio_ended signal:', error);
394
  }
395
  }
 
396
  };
397
 
398
  this.audioPlayer.onerror = (error) => {
399
  console.error('Audio player error:', error);
 
400
  };
401
  }
402