andrymamboro commited on
Commit
773ff4e
·
verified ·
1 Parent(s): 72057da

Update templates/index.html

Browse files
Files changed (1) hide show
  1. templates/index.html +40 -40
templates/index.html CHANGED
@@ -1,70 +1,70 @@
1
  <!DOCTYPE html>
2
- <html lang="en">
3
  <head>
4
  <meta charset="UTF-8">
5
- <title>Voice Agent</title>
6
  <script src="https://cdn.jsdelivr.net/npm/livekit-client/dist/livekit-client.umd.min.js"></script>
7
  <style>
8
- .card { max-width: 400px; margin: 50px auto; padding: 20px; border: 1px solid #ccc; border-radius: 10px; text-align: center; }
9
- #volume-bar { height: 10px; background: #007bff; width: 0%; transition: width 0.1s; }
10
- #transcript { height: 150px; overflow-y: auto; border: 1px solid #eee; margin-top: 10px; padding: 5px; text-align: left; }
 
 
 
 
 
 
11
  </style>
12
  </head>
13
  <body>
14
  <div class="card">
15
- <h1>Agent Status</h1>
16
- <button id="start-btn">Hubungkan</button>
17
- <div id="status">Status: Terputus</div>
18
- <div style="background: #ddd; margin: 10px 0;"><div id="volume-bar"></div></div>
19
  <div id="transcript">Belum ada percakapan...</div>
20
  </div>
21
 
22
  <script>
23
- const startBtn = document.getElementById('start-btn');
24
- const statusDiv = document.getElementById('status');
25
- const volumeBar = document.getElementById('volume-bar');
26
- const transcriptDiv = document.getElementById('transcript');
27
  let room;
28
 
29
- async function connect() {
30
  try {
 
31
  const res = await fetch('/token');
32
  const data = await res.json();
33
 
34
- room = new LivekitClient.Room({
35
- adaptiveStream: true,
36
- dynacast: true,
37
- });
38
-
39
- // 1. Event saat ada data (transkrip) masuk
40
- room.on(LivekitClient.RoomEvent.DataReceived, (payload, participant) => {
41
  const text = new TextDecoder().decode(payload);
42
- transcriptDiv.innerHTML += `<p><b>Agent:</b> ${text}</p>`;
43
- transcriptDiv.scrollTop = transcriptDiv.scrollHeight;
44
  });
45
 
46
  await room.connect(data.server_url, data.token);
47
- statusDiv.innerText = "Status: Terhubung";
48
  await room.localParticipant.setMicrophoneEnabled(true);
49
-
50
- // 2. Loop untuk Indikator Suara
51
- setInterval(() => {
52
- if (room.localParticipant) {
53
- const trackPub = room.localParticipant.audioTrackPublications.values().next().value;
54
- if (trackPub && trackPub.track) {
55
- const level = trackPub.track.getVolumeLevel() * 100;
56
- volumeBar.style.width = Math.min(level * 2, 100) + "%";
57
- }
58
- }
59
- }, 100);
60
-
61
  } catch (e) {
62
- console.error(e);
63
- statusDiv.innerText = "Gagal: " + e.message;
64
  }
65
- }
66
 
67
- startBtn.onclick = connect;
 
 
 
 
 
 
 
 
68
  </script>
69
  </body>
70
  </html>
 
1
  <!DOCTYPE html>
2
+ <html lang="id">
3
  <head>
4
  <meta charset="UTF-8">
 
5
  <script src="https://cdn.jsdelivr.net/npm/livekit-client/dist/livekit-client.umd.min.js"></script>
6
  <style>
7
+ body { font-family: 'Segoe UI', sans-serif; background-color: #f4f7f6; display: flex; justify-content: center; padding-top: 50px; }
8
+ .card { background: white; padding: 30px; border-radius: 20px; box-shadow: 0 10px 25px rgba(0,0,0,0.1); width: 350px; text-align: center; }
9
+ h1 { color: #333; font-size: 24px; margin-bottom: 20px; }
10
+ button { padding: 12px 24px; font-size: 16px; border: none; border-radius: 10px; cursor: pointer; transition: 0.3s; margin: 5px; }
11
+ #btn-hubungkan { background: #007bff; color: white; }
12
+ #btn-batal { background: #dc3545; color: white; }
13
+ button:disabled { background: #ccc; cursor: not-allowed; }
14
+ #status { margin: 15px 0; font-weight: bold; color: #555; }
15
+ #transcript { margin-top: 20px; padding: 15px; background: #f8f9fa; border-left: 4px solid #007bff; border-radius: 5px; min-height: 80px; text-align: left; font-size: 14px; color: #444; }
16
  </style>
17
  </head>
18
  <body>
19
  <div class="card">
20
+ <h1>Voice Agent</h1>
21
+ <button id="btn-hubungkan">Hubungkan</button>
22
+ <button id="btn-batal" disabled>Batal</button>
23
+ <p id="status">Status: Terputus</p>
24
  <div id="transcript">Belum ada percakapan...</div>
25
  </div>
26
 
27
  <script>
28
+ const btnHubungkan = document.getElementById('btn-hubungkan');
29
+ const btnBatal = document.getElementById('btn-batal');
30
+ const status = document.getElementById('status');
31
+ const transcript = document.getElementById('transcript');
32
  let room;
33
 
34
+ btnHubungkan.onclick = async () => {
35
  try {
36
+ status.innerText = "Menghubungkan...";
37
  const res = await fetch('/token');
38
  const data = await res.json();
39
 
40
+ room = new LivekitClient.Room();
41
+
42
+ // Menerima teks dari Agen
43
+ room.on('dataReceived', (payload) => {
 
 
 
44
  const text = new TextDecoder().decode(payload);
45
+ transcript.innerHTML = `<b>Agen:</b> ${text}`;
 
46
  });
47
 
48
  await room.connect(data.server_url, data.token);
 
49
  await room.localParticipant.setMicrophoneEnabled(true);
50
+
51
+ status.innerText = "Status: Terhubung";
52
+ btnHubungkan.disabled = true;
53
+ btnBatal.disabled = false;
 
 
 
 
 
 
 
 
54
  } catch (e) {
55
+ status.innerText = "Error: " + e.message;
 
56
  }
57
+ };
58
 
59
+ btnBatal.onclick = () => {
60
+ if (room) {
61
+ room.disconnect();
62
+ status.innerText = "Status: Terputus";
63
+ btnHubungkan.disabled = false;
64
+ btnBatal.disabled = true;
65
+ transcript.innerHTML = "Belum ada percakapan...";
66
+ }
67
+ };
68
  </script>
69
  </body>
70
  </html>