ciyidogan commited on
Commit
66e7ae8
·
verified ·
1 Parent(s): 9c4de2d

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

Browse files
flare-ui/src/app/services/conversation-manager.service.ts CHANGED
@@ -163,111 +163,99 @@ export class ConversationManagerService implements OnDestroy {
163
  }
164
 
165
  private setupSubscriptions(): void {
166
- // Audio chunks from microphone
167
- this.subscriptions.add(
168
- this.audioService.audioChunk$.subscribe({
169
- next: (chunk) => {
170
- if (!this.isInterrupting && this.wsService.isConnected()) {
171
- try {
172
- this.wsService.sendAudioChunk(chunk.data);
173
- } catch (error) {
174
- console.error('Failed to send audio chunk:', error);
 
175
  }
 
 
 
 
176
  }
177
- },
178
- error: (error) => {
179
- console.error('Audio stream error:', error);
 
 
 
180
  this.handleAudioError(error);
181
- }
182
- })
183
- );
184
-
185
- // Audio stream errors
186
- this.subscriptions.add(
187
- this.audioService.error$.subscribe(error => {
188
- this.handleAudioError(error);
189
- })
190
- );
191
-
192
- // WebSocket messages
193
- this.subscriptions.add(
194
- this.wsService.message$.subscribe({
195
- next: (message) => {
196
- this.handleMessage(message);
197
- },
198
- error: (error) => {
199
- console.error('WebSocket message error:', error);
200
- this.handleWebSocketError(error);
201
- }
202
- })
203
- );
204
-
205
- // Subscribe to transcription updates
206
- this.subscriptions.add(
207
- this.wsService.transcription$.subscribe(result => {
208
- console.log('📝 Transcription received:', result);
209
-
210
- // HER ZAMAN transcription'ı güncelle (final olsun olmasın)
211
- this.transcriptionSubject.next(result.text);
212
-
213
- // Final ise mesaj olarak da ekle
214
- if (result.is_final) {
215
- const messages = this.messagesSubject.value;
216
- const lastMessage = messages[messages.length - 1];
217
- if (!lastMessage || lastMessage.role !== 'user' || lastMessage.text !== result.text) {
218
- this.addMessage('user', result.text);
219
  }
220
- // Final olduktan sonra transcription'ı temizle
221
- setTimeout(() => {
222
- this.transcriptionSubject.next('');
223
- }, 100);
224
- }
225
- })
226
- );
227
-
228
- // State changes
229
- this.subscriptions.add(
230
- this.wsService.stateChange$.subscribe(change => {
231
- this.currentStateSubject.next(change.to as ConversationState);
232
- this.handleStateChange(change.from, change.to);
233
- })
234
- );
235
-
236
- // WebSocket errors
237
- this.subscriptions.add(
238
- this.wsService.error$.subscribe(error => {
239
- console.error('WebSocket error:', error);
240
- this.handleWebSocketError({ message: error });
241
- })
242
- );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
243
 
244
- // WebSocket connection state
245
- this.subscriptions.add(
246
- this.wsService.connection$.subscribe(connected => {
247
- if (!connected && this.currentStateSubject.value !== 'idle') {
248
- this.addSystemMessage('Connection lost. Attempting to reconnect...');
249
- }
250
- })
251
- );
252
- }
253
-
254
  private handleMessage(message: any): void {
255
  try {
256
  switch (message.type) {
257
  case 'transcription':
258
- // SADECE final transcription'ları mesaj olarak ekle
259
  if (message['is_final']) {
260
- // Eğer son mesaj aynı user mesajıysa ekleme (duplicate check)
261
  const messages = this.messagesSubject.value;
262
  const lastMessage = messages[messages.length - 1];
263
  if (!lastMessage || lastMessage.role !== 'user' || lastMessage.text !== message['text']) {
264
  this.addMessage('user', message['text']);
265
  }
266
- this.transcriptionSubject.next('');
267
- } else {
268
- // Interim transcription'ları accumulate et
269
- this.transcriptionSubject.next(message['text']);
270
  }
 
271
  break;
272
 
273
  case 'assistant_response':
@@ -324,7 +312,7 @@ export class ConversationManagerService implements OnDestroy {
324
  timestamp: new Date()
325
  });
326
  }
327
- }
328
 
329
  private handleStateChange(from: string, to: string): void {
330
  console.log(`📊 State: ${from} → ${to}`);
 
163
  }
164
 
165
  private setupSubscriptions(): void {
166
+ // Audio chunks from microphone
167
+ this.subscriptions.add(
168
+ this.audioService.audioChunk$.subscribe({
169
+ next: (chunk) => {
170
+ if (!this.isInterrupting && this.wsService.isConnected()) {
171
+ try {
172
+ this.wsService.sendAudioChunk(chunk.data);
173
+ } catch (error) {
174
+ console.error('Failed to send audio chunk:', error);
175
+ }
176
  }
177
+ },
178
+ error: (error) => {
179
+ console.error('Audio stream error:', error);
180
+ this.handleAudioError(error);
181
  }
182
+ })
183
+ );
184
+
185
+ // Audio stream errors
186
+ this.subscriptions.add(
187
+ this.audioService.error$.subscribe(error => {
188
  this.handleAudioError(error);
189
+ })
190
+ );
191
+
192
+ // WebSocket messages
193
+ this.subscriptions.add(
194
+ this.wsService.message$.subscribe({
195
+ next: (message) => {
196
+ this.handleMessage(message);
197
+ },
198
+ error: (error) => {
199
+ console.error('WebSocket message error:', error);
200
+ this.handleWebSocketError(error);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
201
  }
202
+ })
203
+ );
204
+
205
+ // Subscribe to transcription updates - SADECE FINAL RESULTS
206
+ this.subscriptions.add(
207
+ this.wsService.transcription$.subscribe(result => {
208
+ // SADECE final transcription'ları işle
209
+ if (result.is_final) {
210
+ console.log('📝 Final transcription received:', result);
211
+ const messages = this.messagesSubject.value;
212
+ const lastMessage = messages[messages.length - 1];
213
+ if (!lastMessage || lastMessage.role !== 'user' || lastMessage.text !== result.text) {
214
+ this.addMessage('user', result.text);
215
+ }
216
+ }
217
+ })
218
+ );
219
+
220
+ // State changes
221
+ this.subscriptions.add(
222
+ this.wsService.stateChange$.subscribe(change => {
223
+ this.currentStateSubject.next(change.to as ConversationState);
224
+ this.handleStateChange(change.from, change.to);
225
+ })
226
+ );
227
+
228
+ // WebSocket errors
229
+ this.subscriptions.add(
230
+ this.wsService.error$.subscribe(error => {
231
+ console.error('WebSocket error:', error);
232
+ this.handleWebSocketError({ message: error });
233
+ })
234
+ );
235
+
236
+ // WebSocket connection state
237
+ this.subscriptions.add(
238
+ this.wsService.connection$.subscribe(connected => {
239
+ if (!connected && this.currentStateSubject.value !== 'idle') {
240
+ this.addSystemMessage('Connection lost. Attempting to reconnect...');
241
+ }
242
+ })
243
+ );
244
+ }
245
 
 
 
 
 
 
 
 
 
 
 
246
  private handleMessage(message: any): void {
247
  try {
248
  switch (message.type) {
249
  case 'transcription':
250
+ // SADECE final transcription'ları işle
251
  if (message['is_final']) {
 
252
  const messages = this.messagesSubject.value;
253
  const lastMessage = messages[messages.length - 1];
254
  if (!lastMessage || lastMessage.role !== 'user' || lastMessage.text !== message['text']) {
255
  this.addMessage('user', message['text']);
256
  }
 
 
 
 
257
  }
258
+ // Interim transcription'ları artık işlemiyoruz
259
  break;
260
 
261
  case 'assistant_response':
 
312
  timestamp: new Date()
313
  });
314
  }
315
+ }
316
 
317
  private handleStateChange(from: string, to: string): void {
318
  console.log(`📊 State: ${from} → ${to}`);