MinhDo123 commited on
Commit
93a00dd
·
verified ·
1 Parent(s): bc6f026

Update frontend/call_desktop.html

Browse files
Files changed (1) hide show
  1. frontend/call_desktop.html +27 -0
frontend/call_desktop.html CHANGED
@@ -36,6 +36,7 @@
36
  .latency-num { font-size: 14px; font-weight: 700; color: #fff; margin-top: 2px; display: block; }
37
  .stt-color { color: #4fc3f7; }
38
  .ai-color { color: #f1c40f; }
 
39
  .main-content { flex: 1; display: flex; flex-direction: column; background: white; border-radius: 30px 0 0 30px; margin-left: -20px; z-index: 20; overflow: hidden; box-shadow: -5px 0 20px rgba(0,0,0,0.05); }
40
  .top-bar { padding: 20px 40px; display: flex; justify-content: space-between; align-items: center; border-bottom: 1px solid #eee; background: white; }
41
  .call-input { display: flex; gap: 15px; align-items: center; }
@@ -181,12 +182,14 @@
181
  let audioContext, analyser, dataArray;
182
  let currentBotBubble = null;
183
  let abortController = null;
 
184
  // --- TIMER VARS (Độ chính xác cao) ---
185
  let timerInterval = null;
186
  let t0_record = 0; // Lúc thả mic
187
  let t1_stt_end = 0; // Lúc nhận text user
188
  let currentPhase = 'idle'; // 'stt' | 'ai' | 'idle'
189
  let isLogicReady = false; // Cờ hiệu: Logic đã xong hay chưa?
 
190
  // --- 1. VISUALIZER & UI ---
191
  function initVisualizer(stream) {
192
  if(!audioContext) audioContext = new (window.AudioContext || window.webkitAudioContext)();
@@ -197,6 +200,7 @@
197
  dataArray = new Uint8Array(analyser.frequencyBinCount);
198
  drawVisualizer();
199
  }
 
200
  function drawVisualizer() {
201
  if (!analyser) return;
202
  requestAnimationFrame(drawVisualizer);
@@ -206,6 +210,7 @@
206
  bar.style.height = h + 'px';
207
  });
208
  }
 
209
  function log(msg, type='info') {
210
  const logArea = document.getElementById('logArea');
211
  if (type === 'bot' && currentBotBubble) {
@@ -220,6 +225,7 @@
220
  }
221
  logArea.scrollTo({ top: logArea.scrollHeight, behavior: 'smooth' });
222
  }
 
223
  // --- 2. RATING ---
224
  let currentRating = 0;
225
  function showRating() { document.getElementById('ratingOverlay').style.display = 'flex'; currentRating = 0; updateStars(0); }
@@ -239,6 +245,7 @@
239
  document.querySelector('.btn-submit').innerText = "Gửi đánh giá";
240
  closeRating();
241
  }
 
242
  // --- 3. CALL CONTROL ---
243
  function resetCallUI() {
244
  document.getElementById('btnCall').disabled = false;
@@ -251,12 +258,14 @@
251
  currentBotBubble = null;
252
  stopTimer();
253
  }
 
254
  function endCall() {
255
  stopBotResponse();
256
  isSessionEnded = true;
257
  document.getElementById('mascotBox').classList.remove('active');
258
  showRating();
259
  }
 
260
  async function startCall() {
261
  const cid = document.getElementById('customerId').value;
262
  const btnCall = document.getElementById('btnCall');
@@ -265,6 +274,7 @@
265
  document.getElementById('status').innerText = "Đang khởi tạo...";
266
  isSessionEnded = false; audioQueue = []; isPlaying = false; currentBotBubble = null;
267
  stopBotResponse();
 
268
  try {
269
  abortController = new AbortController();
270
  const formData = new FormData();
@@ -286,6 +296,7 @@
286
  console.error(e); log("Lỗi kết nối Server!", 'info'); resetCallUI();
287
  }
288
  }
 
289
  function stopBotResponse() {
290
  if (currentAudioObject) { currentAudioObject.pause(); currentAudioObject = null; }
291
  audioQueue = []; isPlaying = false;
@@ -293,6 +304,7 @@
293
  document.getElementById('mascotBox').classList.remove('active');
294
  stopTimer();
295
  }
 
296
  // --- 4. STREAM & AUDIO ---
297
  async function readStream(response) {
298
  const reader = response.body.getReader();
@@ -301,6 +313,7 @@
301
 
302
  // Reset flags
303
  isLogicReady = false;
 
304
  try {
305
  while (true) {
306
  const { done, value } = await reader.read();
@@ -309,6 +322,7 @@
309
  buffer += decoder.decode(value, { stream: true });
310
  const lines = buffer.split("\n");
311
  buffer = lines.pop();
 
312
  for (const line of lines) {
313
  if (!line.trim()) continue;
314
  try {
@@ -326,6 +340,7 @@
326
  isLogicReady = true;
327
  updateUXDashboard(data.data);
328
  }
 
329
  // 3. BOT TEXT -> KIỂM TRA ĐỂ DỪNG TIMER
330
  if (data.bot_text) {
331
  log(data.bot_text, 'bot');
@@ -347,6 +362,7 @@
347
  stopTimer();
348
  if (audioQueue.length === 0 && !isPlaying) showRating();
349
  }
 
350
  } catch (e) { console.error("JSON Error:", e); }
351
  }
352
  }
@@ -355,6 +371,7 @@
355
  else console.error("Stream Error:", err);
356
  }
357
  }
 
358
  async function processAudioQueue() {
359
  if (isPlaying || audioQueue.length === 0) return;
360
  isPlaying = true;
@@ -375,6 +392,7 @@
375
  audio.onerror = () => { isPlaying = false; processAudioQueue(); };
376
  try { await audio.play(); } catch (e) { isPlaying = false; }
377
  }
 
378
  function updateUXDashboard(metrics) {
379
  if (metrics.latency_total) document.getElementById("ux-latency-total").innerText = metrics.latency_total;
380
  // Note: Chúng ta cập nhật UI bằng Timer local để mượt mà hơn,
@@ -390,6 +408,7 @@
390
  else { text.innerText = "Trung tính 😐"; text.style.color = "#f1c40f"; bar.style.background = "#f1c40f"; }
391
  document.getElementById("ux-intent").innerText = metrics.intent;
392
  }
 
393
  // --- 6. TIMER LOGIC (REAL-TIME UI) ---
394
  function startRecording() {
395
  if (!mediaRecorder) return;
@@ -406,6 +425,7 @@
406
  document.getElementById('recordBtn').classList.add('recording');
407
  document.getElementById('status').innerText = "Đang nghe bạn nói...";
408
  }
 
409
  function stopRecording() {
410
  if (mediaRecorder && mediaRecorder.state === "recording") {
411
  mediaRecorder.stop();
@@ -422,12 +442,14 @@
422
  timerInterval = setInterval(updateTimerUI, 30);
423
  }
424
  }
 
425
  function switchToAiPhase() {
426
  if (currentPhase === 'stt') {
427
  t1_stt_end = performance.now(); // Chốt STT time
428
  currentPhase = 'ai'; // Chuyển sang AI running
429
  }
430
  }
 
431
  function stopTimer() {
432
  if (timerInterval) {
433
  clearInterval(timerInterval);
@@ -436,10 +458,12 @@
436
  updateTimerUI(); // Render lần cuối
437
  }
438
  }
 
439
  function updateTimerUI() {
440
  const now = performance.now();
441
  let stt_val = 0;
442
  let ai_val = 0;
 
443
  if (currentPhase === 'stt') {
444
  stt_val = (now - t0_record) / 1000;
445
  ai_val = 0;
@@ -450,10 +474,12 @@
450
  // Đã dừng, giữ số liệu cũ (không làm gì)
451
  return;
452
  }
 
453
  document.getElementById("ux-latency-stt").innerText = stt_val.toFixed(2);
454
  document.getElementById("ux-latency-ai").innerText = ai_val.toFixed(2);
455
  document.getElementById("ux-latency-total").innerText = (stt_val + ai_val).toFixed(2);
456
  }
 
457
  // --- 7. MICROPHONE SETUP ---
458
  navigator.mediaDevices.getUserMedia({ audio: true }).then(stream => {
459
  initVisualizer(stream);
@@ -464,6 +490,7 @@
464
  log("Lỗi: Không thể truy cập Micro", 'info');
465
  document.getElementById('status').classList.add('error');
466
  });
 
467
  async function sendAudio() {
468
  const audioBlob = new Blob(audioChunks, { type: 'audio/webm' });
469
  if (audioBlob.size < 1000) { document.getElementById('status').innerText = "Nói quá ngắn, thử lại."; return; }
 
36
  .latency-num { font-size: 14px; font-weight: 700; color: #fff; margin-top: 2px; display: block; }
37
  .stt-color { color: #4fc3f7; }
38
  .ai-color { color: #f1c40f; }
39
+
40
  .main-content { flex: 1; display: flex; flex-direction: column; background: white; border-radius: 30px 0 0 30px; margin-left: -20px; z-index: 20; overflow: hidden; box-shadow: -5px 0 20px rgba(0,0,0,0.05); }
41
  .top-bar { padding: 20px 40px; display: flex; justify-content: space-between; align-items: center; border-bottom: 1px solid #eee; background: white; }
42
  .call-input { display: flex; gap: 15px; align-items: center; }
 
182
  let audioContext, analyser, dataArray;
183
  let currentBotBubble = null;
184
  let abortController = null;
185
+
186
  // --- TIMER VARS (Độ chính xác cao) ---
187
  let timerInterval = null;
188
  let t0_record = 0; // Lúc thả mic
189
  let t1_stt_end = 0; // Lúc nhận text user
190
  let currentPhase = 'idle'; // 'stt' | 'ai' | 'idle'
191
  let isLogicReady = false; // Cờ hiệu: Logic đã xong hay chưa?
192
+
193
  // --- 1. VISUALIZER & UI ---
194
  function initVisualizer(stream) {
195
  if(!audioContext) audioContext = new (window.AudioContext || window.webkitAudioContext)();
 
200
  dataArray = new Uint8Array(analyser.frequencyBinCount);
201
  drawVisualizer();
202
  }
203
+
204
  function drawVisualizer() {
205
  if (!analyser) return;
206
  requestAnimationFrame(drawVisualizer);
 
210
  bar.style.height = h + 'px';
211
  });
212
  }
213
+
214
  function log(msg, type='info') {
215
  const logArea = document.getElementById('logArea');
216
  if (type === 'bot' && currentBotBubble) {
 
225
  }
226
  logArea.scrollTo({ top: logArea.scrollHeight, behavior: 'smooth' });
227
  }
228
+
229
  // --- 2. RATING ---
230
  let currentRating = 0;
231
  function showRating() { document.getElementById('ratingOverlay').style.display = 'flex'; currentRating = 0; updateStars(0); }
 
245
  document.querySelector('.btn-submit').innerText = "Gửi đánh giá";
246
  closeRating();
247
  }
248
+
249
  // --- 3. CALL CONTROL ---
250
  function resetCallUI() {
251
  document.getElementById('btnCall').disabled = false;
 
258
  currentBotBubble = null;
259
  stopTimer();
260
  }
261
+
262
  function endCall() {
263
  stopBotResponse();
264
  isSessionEnded = true;
265
  document.getElementById('mascotBox').classList.remove('active');
266
  showRating();
267
  }
268
+
269
  async function startCall() {
270
  const cid = document.getElementById('customerId').value;
271
  const btnCall = document.getElementById('btnCall');
 
274
  document.getElementById('status').innerText = "Đang khởi tạo...";
275
  isSessionEnded = false; audioQueue = []; isPlaying = false; currentBotBubble = null;
276
  stopBotResponse();
277
+
278
  try {
279
  abortController = new AbortController();
280
  const formData = new FormData();
 
296
  console.error(e); log("Lỗi kết nối Server!", 'info'); resetCallUI();
297
  }
298
  }
299
+
300
  function stopBotResponse() {
301
  if (currentAudioObject) { currentAudioObject.pause(); currentAudioObject = null; }
302
  audioQueue = []; isPlaying = false;
 
304
  document.getElementById('mascotBox').classList.remove('active');
305
  stopTimer();
306
  }
307
+
308
  // --- 4. STREAM & AUDIO ---
309
  async function readStream(response) {
310
  const reader = response.body.getReader();
 
313
 
314
  // Reset flags
315
  isLogicReady = false;
316
+
317
  try {
318
  while (true) {
319
  const { done, value } = await reader.read();
 
322
  buffer += decoder.decode(value, { stream: true });
323
  const lines = buffer.split("\n");
324
  buffer = lines.pop();
325
+
326
  for (const line of lines) {
327
  if (!line.trim()) continue;
328
  try {
 
340
  isLogicReady = true;
341
  updateUXDashboard(data.data);
342
  }
343
+
344
  // 3. BOT TEXT -> KIỂM TRA ĐỂ DỪNG TIMER
345
  if (data.bot_text) {
346
  log(data.bot_text, 'bot');
 
362
  stopTimer();
363
  if (audioQueue.length === 0 && !isPlaying) showRating();
364
  }
365
+
366
  } catch (e) { console.error("JSON Error:", e); }
367
  }
368
  }
 
371
  else console.error("Stream Error:", err);
372
  }
373
  }
374
+
375
  async function processAudioQueue() {
376
  if (isPlaying || audioQueue.length === 0) return;
377
  isPlaying = true;
 
392
  audio.onerror = () => { isPlaying = false; processAudioQueue(); };
393
  try { await audio.play(); } catch (e) { isPlaying = false; }
394
  }
395
+
396
  function updateUXDashboard(metrics) {
397
  if (metrics.latency_total) document.getElementById("ux-latency-total").innerText = metrics.latency_total;
398
  // Note: Chúng ta cập nhật UI bằng Timer local để mượt mà hơn,
 
408
  else { text.innerText = "Trung tính 😐"; text.style.color = "#f1c40f"; bar.style.background = "#f1c40f"; }
409
  document.getElementById("ux-intent").innerText = metrics.intent;
410
  }
411
+
412
  // --- 6. TIMER LOGIC (REAL-TIME UI) ---
413
  function startRecording() {
414
  if (!mediaRecorder) return;
 
425
  document.getElementById('recordBtn').classList.add('recording');
426
  document.getElementById('status').innerText = "Đang nghe bạn nói...";
427
  }
428
+
429
  function stopRecording() {
430
  if (mediaRecorder && mediaRecorder.state === "recording") {
431
  mediaRecorder.stop();
 
442
  timerInterval = setInterval(updateTimerUI, 30);
443
  }
444
  }
445
+
446
  function switchToAiPhase() {
447
  if (currentPhase === 'stt') {
448
  t1_stt_end = performance.now(); // Chốt STT time
449
  currentPhase = 'ai'; // Chuyển sang AI running
450
  }
451
  }
452
+
453
  function stopTimer() {
454
  if (timerInterval) {
455
  clearInterval(timerInterval);
 
458
  updateTimerUI(); // Render lần cuối
459
  }
460
  }
461
+
462
  function updateTimerUI() {
463
  const now = performance.now();
464
  let stt_val = 0;
465
  let ai_val = 0;
466
+
467
  if (currentPhase === 'stt') {
468
  stt_val = (now - t0_record) / 1000;
469
  ai_val = 0;
 
474
  // Đã dừng, giữ số liệu cũ (không làm gì)
475
  return;
476
  }
477
+
478
  document.getElementById("ux-latency-stt").innerText = stt_val.toFixed(2);
479
  document.getElementById("ux-latency-ai").innerText = ai_val.toFixed(2);
480
  document.getElementById("ux-latency-total").innerText = (stt_val + ai_val).toFixed(2);
481
  }
482
+
483
  // --- 7. MICROPHONE SETUP ---
484
  navigator.mediaDevices.getUserMedia({ audio: true }).then(stream => {
485
  initVisualizer(stream);
 
490
  log("Lỗi: Không thể truy cập Micro", 'info');
491
  document.getElementById('status').classList.add('error');
492
  });
493
+
494
  async function sendAudio() {
495
  const audioBlob = new Blob(audioChunks, { type: 'audio/webm' });
496
  if (audioBlob.size < 1000) { document.getElementById('status').innerText = "Nói quá ngắn, thử lại."; return; }