ciyidogan commited on
Commit
0828c34
·
verified ·
1 Parent(s): 479a219

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

Browse files
flare-ui/src/app/services/conversation-manager.service.ts CHANGED
@@ -456,7 +456,10 @@ export class ConversationManagerService implements OnDestroy {
456
  const audioData = this.audioQueue.join('');
457
 
458
  try {
459
- const audioBlob = this.base64ToBlob(audioData, message['mime_type'] || 'audio/mpeg');
 
 
 
460
  const audioUrl = URL.createObjectURL(audioBlob);
461
 
462
  // Update last message with audio URL
@@ -476,7 +479,20 @@ export class ConversationManagerService implements OnDestroy {
476
  } catch (error) {
477
  console.error('Error creating audio blob:', error);
478
  this.audioQueue = []; // Clear queue on error
479
- throw error;
 
 
 
 
 
 
 
 
 
 
 
 
 
480
  }
481
  } catch (error) {
482
  console.error('Error handling TTS audio:', error);
@@ -487,21 +503,25 @@ export class ConversationManagerService implements OnDestroy {
487
  private base64ToBlob(base64: string, mimeType: string): Blob {
488
  try {
489
  // Clean base64 string - remove any whitespace or newlines
490
- const cleanBase64 = base64.replace(/\s/g, '');
491
 
492
  // Validate base64 string
493
  if (!cleanBase64 || cleanBase64.length === 0) {
494
  throw new Error('Empty base64 string');
495
  }
496
 
497
- // Check if base64 string is valid
498
- const base64Regex = /^[A-Za-z0-9+/]*={0,2}$/;
499
- if (!base64Regex.test(cleanBase64)) {
500
- throw new Error('Invalid base64 string format');
501
  }
502
 
 
 
 
 
503
  // Decode base64
504
- const byteCharacters = atob(cleanBase64);
505
  const byteNumbers = new Array(byteCharacters.length);
506
 
507
  for (let i = 0; i < byteCharacters.length; i++) {
@@ -512,6 +532,7 @@ export class ConversationManagerService implements OnDestroy {
512
  return new Blob([byteArray], { type: mimeType });
513
  } catch (error) {
514
  console.error('Error converting base64 to blob:', error);
 
515
  console.error('Base64 preview:', base64.substring(0, 100) + '...');
516
  throw new Error('Failed to convert audio data');
517
  }
 
456
  const audioData = this.audioQueue.join('');
457
 
458
  try {
459
+ // Default to MP3 if mime_type not provided
460
+ const mimeType = message['mime_type'] || 'audio/mpeg';
461
+
462
+ const audioBlob = this.base64ToBlob(audioData, mimeType);
463
  const audioUrl = URL.createObjectURL(audioBlob);
464
 
465
  // Update last message with audio URL
 
479
  } catch (error) {
480
  console.error('Error creating audio blob:', error);
481
  this.audioQueue = []; // Clear queue on error
482
+
483
+ // Try direct blob creation as fallback
484
+ try {
485
+ const fallbackBlob = new Blob([atob(audioData)], { type: 'audio/mpeg' });
486
+ const audioUrl = URL.createObjectURL(fallbackBlob);
487
+
488
+ const messages = this.messagesSubject.value;
489
+ if (messages.length > 0 && messages[messages.length - 1].role === 'assistant') {
490
+ messages[messages.length - 1].audioUrl = audioUrl;
491
+ this.messagesSubject.next([...messages]);
492
+ }
493
+ } catch (fallbackError) {
494
+ console.error('Fallback audio creation also failed:', fallbackError);
495
+ }
496
  }
497
  } catch (error) {
498
  console.error('Error handling TTS audio:', error);
 
503
  private base64ToBlob(base64: string, mimeType: string): Blob {
504
  try {
505
  // Clean base64 string - remove any whitespace or newlines
506
+ const cleanBase64 = base64.replace(/[\s\n\r]/g, '');
507
 
508
  // Validate base64 string
509
  if (!cleanBase64 || cleanBase64.length === 0) {
510
  throw new Error('Empty base64 string');
511
  }
512
 
513
+ // Check if string contains MP3 header (ID3 tag)
514
+ if (cleanBase64.startsWith('SUQz')) {
515
+ // This is raw MP3 data, not base64 - likely a backend issue
516
+ console.log('Detected raw MP3 data instead of base64');
517
  }
518
 
519
+ // Pad base64 string if necessary
520
+ const padding = cleanBase64.length % 4;
521
+ const paddedBase64 = padding ? cleanBase64 + '='.repeat(4 - padding) : cleanBase64;
522
+
523
  // Decode base64
524
+ const byteCharacters = atob(paddedBase64);
525
  const byteNumbers = new Array(byteCharacters.length);
526
 
527
  for (let i = 0; i < byteCharacters.length; i++) {
 
532
  return new Blob([byteArray], { type: mimeType });
533
  } catch (error) {
534
  console.error('Error converting base64 to blob:', error);
535
+ console.error('Base64 length:', base64.length);
536
  console.error('Base64 preview:', base64.substring(0, 100) + '...');
537
  throw new Error('Failed to convert audio data');
538
  }