File size: 10,576 Bytes
73af9f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
import { Component, OnInit, OnDestroy, ViewChild, ElementRef, AfterViewChecked } from '@angular/core';
import { CommonModule } from '@angular/common';
import { MatCardModule } from '@angular/material/card';
import { MatButtonModule } from '@angular/material/button';
import { MatIconModule } from '@angular/material/icon';
import { MatProgressSpinnerModule } from '@angular/material/progress-spinner';
import { MatDividerModule } from '@angular/material/divider';
import { MatChipsModule } from '@angular/material/chips';
import { Subscription } from 'rxjs';

import { ConversationManagerService, ConversationState, ConversationMessage } from '../../services/conversation-manager.service';
import { AudioStreamService } from '../../services/audio-stream.service';

@Component({
  selector: 'app-realtime-chat',
  standalone: true,
  imports: [
    CommonModule,
    MatCardModule,
    MatButtonModule,
    MatIconModule,
    MatProgressSpinnerModule,
    MatDividerModule,
    MatChipsModule
  ],
  template: `
    <mat-card class="realtime-chat-container">
      <mat-card-header>
        <mat-icon mat-card-avatar>voice_chat</mat-icon>
        <mat-card-title>Real-time Conversation</mat-card-title>
        <mat-card-subtitle>
          <mat-chip-listbox>
            <mat-chip [class.active]="currentState === state" 
                     *ngFor="let state of conversationStates">
              {{ getStateLabel(state) }}
            </mat-chip>
          </mat-chip-listbox>
        </mat-card-subtitle>
      </mat-card-header>

      <mat-divider></mat-divider>

      <mat-card-content>
        <!-- Transcription Display -->
        <div class="transcription-area" *ngIf="currentTranscription">
          <div class="transcription-label">Dinleniyor...</div>
          <div class="transcription-text">{{ currentTranscription }}</div>
        </div>

        <!-- Chat Messages -->
        <div class="chat-messages" #scrollContainer>
          <div *ngFor="let msg of messages" 
               [class]="'message ' + msg.role">
            <mat-icon class="message-icon">
              {{ msg.role === 'user' ? 'person' : 'smart_toy' }}
            </mat-icon>
            <div class="message-content">
              <div class="message-text">{{ msg.text }}</div>
              <div class="message-time">{{ msg.timestamp | date:'HH:mm:ss' }}</div>
              <button *ngIf="msg.audioUrl" 
                      mat-icon-button 
                      (click)="playAudio(msg.audioUrl)"
                      class="audio-button">
                <mat-icon>volume_up</mat-icon>
              </button>
            </div>
          </div>
        </div>

        <!-- Audio Visualizer -->
        <canvas #audioVisualizer 
                class="audio-visualizer" 
                width="600" 
                height="100"
                [class.active]="isRecording">
        </canvas>
      </mat-card-content>

      <mat-card-actions>
        <button mat-raised-button 
                color="primary" 
                (click)="toggleConversation()"
                [disabled]="!sessionId">
          <mat-icon>{{ isConversationActive ? 'stop' : 'mic' }}</mat-icon>
          {{ isConversationActive ? 'Konuşmayı Bitir' : 'Konuşmaya Başla' }}
        </button>

        <button mat-button 
                (click)="clearChat()"
                [disabled]="messages.length === 0">
          <mat-icon>clear</mat-icon>
          Temizle
        </button>
      </mat-card-actions>
    </mat-card>
  `,
  styles: [`
    .realtime-chat-container {
      max-width: 800px;
      margin: 20px auto;
      height: 80vh;
      display: flex;
      flex-direction: column;
    }

    .transcription-area {
      background: #f5f5f5;
      padding: 16px;
      border-radius: 8px;
      margin-bottom: 16px;
      min-height: 60px;
    }

    .transcription-label {
      font-size: 12px;
      color: #666;
      margin-bottom: 4px;
    }

    .transcription-text {
      font-size: 16px;
      color: #333;
      min-height: 24px;
    }

    .chat-messages {
      flex: 1;
      overflow-y: auto;
      padding: 16px;
      background: #fafafa;
      border-radius: 8px;
    }

    .message {
      display: flex;
      align-items: flex-start;
      margin-bottom: 16px;
      animation: slideIn 0.3s ease-out;
    }

    @keyframes slideIn {
      from {
        opacity: 0;
        transform: translateY(10px);
      }
      to {
        opacity: 1;
        transform: translateY(0);
      }
    }

    .message.user {
      flex-direction: row-reverse;
    }

    .message-icon {
      margin: 0 8px;
      color: #666;
    }

    .message-content {
      max-width: 70%;
      background: white;
      padding: 12px 16px;
      border-radius: 12px;
      box-shadow: 0 1px 2px rgba(0,0,0,0.1);
    }

    .message.user .message-content {
      background: #3f51b5;
      color: white;
    }

    .message-text {
      margin-bottom: 4px;
    }

    .message-time {
      font-size: 11px;
      opacity: 0.7;
    }

    .audio-button {
      margin-top: 8px;
    }

    .audio-visualizer {
      width: 100%;
      height: 100px;
      background: #333;
      border-radius: 8px;
      margin-top: 16px;
      opacity: 0.3;
      transition: opacity 0.3s;
    }

    .audio-visualizer.active {
      opacity: 1;
    }

    mat-chip {
      font-size: 12px;
    }

    mat-chip.active {
      background-color: #3f51b5 !important;
      color: white !important;
    }

    mat-card-actions {
      padding: 16px;
      justify-content: space-between;
    }
  `]
})
export class RealtimeChatComponent implements OnInit, OnDestroy, AfterViewChecked {
  @ViewChild('scrollContainer') private scrollContainer!: ElementRef;
  @ViewChild('audioVisualizer') private audioVisualizer!: ElementRef<HTMLCanvasElement>;
  
  sessionId: string | null = null;
  isConversationActive = false;
  isRecording = false;
  currentState: ConversationState = 'idle';
  currentTranscription = '';
  messages: ConversationMessage[] = [];
  
  conversationStates: ConversationState[] = [
    'idle', 'listening', 'processing_stt', 'processing_llm', 'processing_tts', 'playing_audio'
  ];
  
  private subscriptions = new Subscription();
  private shouldScrollToBottom = false;
  private animationId: number | null = null;

  constructor(
    private conversationManager: ConversationManagerService,
    private audioService: AudioStreamService
  ) {}

  ngOnInit(): void {
    // Get session ID from parent component or service
    this.sessionId = localStorage.getItem('current_session_id');
    
    // Subscribe to conversation state
    this.subscriptions.add(
      this.conversationManager.currentState$.subscribe(state => {
        this.currentState = state;
        this.updateRecordingState(state);
      })
    );
    
    // Subscribe to messages
    this.subscriptions.add(
      this.conversationManager.messages$.subscribe(messages => {
        this.messages = messages;
        this.shouldScrollToBottom = true;
      })
    );
    
    // Subscribe to transcription
    this.subscriptions.add(
      this.conversationManager.transcription$.subscribe(text => {
        this.currentTranscription = text;
      })
    );
    
    // Check browser support
    if (!AudioStreamService.checkBrowserSupport()) {
      alert('Tarayıcınız ses kaydını desteklemiyor. Lütfen modern bir tarayıcı kullanın.');
    }
  }

  ngAfterViewChecked(): void {
    if (this.shouldScrollToBottom) {
      this.scrollToBottom();
      this.shouldScrollToBottom = false;
    }
  }

  ngOnDestroy(): void {
    this.subscriptions.unsubscribe();
    this.stopVisualization();
    if (this.isConversationActive) {
      this.conversationManager.stopConversation();
    }
  }

  async toggleConversation(): Promise<void> {
    if (!this.sessionId) return;
    
    if (this.isConversationActive) {
      this.conversationManager.stopConversation();
      this.isConversationActive = false;
      this.stopVisualization();
    } else {
      try {
        await this.conversationManager.startConversation(this.sessionId);
        this.isConversationActive = true;
        this.startVisualization();
      } catch (error) {
        console.error('Failed to start conversation:', error);
        alert('Konuşma başlatılamadı. Lütfen tekrar deneyin.');
      }
    }
  }

  clearChat(): void {
    this.conversationManager.clearMessages();
    this.currentTranscription = '';
  }

  playAudio(audioUrl: string): void {
    const audio = new Audio(audioUrl);
    audio.play();
  }

  getStateLabel(state: ConversationState): string {
    const labels: Record<ConversationState, string> = {
      'idle': 'Bekliyor',
      'listening': 'Dinliyor',
      'processing_stt': 'Metin Dönüştürme',
      'processing_llm': 'Yanıt Hazırlanıyor',
      'processing_tts': 'Ses Oluşturuluyor',
      'playing_audio': 'Konuşuyor'
    };
    return labels[state] || state;
  }

  private updateRecordingState(state: ConversationState): void {
    this.isRecording = state === 'listening';
  }

  private scrollToBottom(): void {
    try {
      this.scrollContainer.nativeElement.scrollTop = 
        this.scrollContainer.nativeElement.scrollHeight;
    } catch(err) {}
  }

  private startVisualization(): void {
    if (!this.audioVisualizer) return;
    
    const canvas = this.audioVisualizer.nativeElement;
    const ctx = canvas.getContext('2d');
    if (!ctx) return;
    
    // Simple visualization animation
    const draw = () => {
      ctx.fillStyle = '#333';
      ctx.fillRect(0, 0, canvas.width, canvas.height);
      
      // Draw random bars for demo
      ctx.fillStyle = '#4caf50';
      const barCount = 50;
      const barWidth = canvas.width / barCount;
      
      for (let i = 0; i < barCount; i++) {
        const barHeight = Math.random() * canvas.height * 0.8;
        const x = i * barWidth;
        const y = canvas.height - barHeight;
        
        ctx.fillRect(x, y, barWidth - 2, barHeight);
      }
      
      if (this.isRecording) {
        this.animationId = requestAnimationFrame(draw);
      }
    };
    
    draw();
  }

  private stopVisualization(): void {
    if (this.animationId) {
      cancelAnimationFrame(this.animationId);
      this.animationId = null;
    }
    
    // Clear canvas
    if (this.audioVisualizer) {
      const canvas = this.audioVisualizer.nativeElement;
      const ctx = canvas.getContext('2d');
      if (ctx) {
        ctx.fillStyle = '#333';
        ctx.fillRect(0, 0, canvas.width, canvas.height);
      }
    }
  }
}