File size: 3,149 Bytes
f2ea5fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const chatBox = document.getElementById("chat-box");
const sendBtn = document.getElementById("send-btn");
const textInput = document.getElementById("text-input");
const micBtn = document.getElementById("mic-btn");

const userId = "walid";


// =======================
// CHAT WEBSOCKET
// =======================

const chatSocket = new WebSocket("ws://127.0.0.1:8679/ws/chat");

chatSocket.onmessage = (event) => {

    const data = event.data;

    if (data === "[[END]]") {
        return;
    }

    appendMessage(data, "ai");
};


sendBtn.onclick = () => {
    sendTextMessage();
};

textInput.addEventListener("keydown", (e) => {
    if (e.key === "Enter") {
        sendTextMessage();
    }
});


function sendTextMessage() {

    const message = textInput.value.trim();

    if (!message) return;

    appendMessage(message, "user");

    chatSocket.send(JSON.stringify({
        user_id: userId,
        user_query: message
    }));

    textInput.value = "";
}


// =======================
// VOICE WEBSOCKET
// =======================

const voiceSocket = new WebSocket("ws://127.0.0.1:8679/ws/voice");

voiceSocket.binaryType = "arraybuffer";

let mediaRecorder;
let audioChunks = [];
let isRecording = false;

voiceSocket.onmessage = async (event) => {

    // TEXT MESSAGE
    if (typeof event.data === "string") {

        const text = event.data;

        if (text.startsWith("[STT]:")) {
            appendMessage("🎤 " + text.replace("[STT]:", ""), "user");
        }

        else if (text.startsWith("[LLM]:")) {
            appendMessage(
                text.replace("[LLM]:", ""),
                "ai"
            );
        }

        return;
    }

    // AUDIO MESSAGE
    const audioBlob = new Blob([event.data], { type: "audio/mp3" });

    const audioUrl = URL.createObjectURL(audioBlob);

    const audio = new Audio(audioUrl);

    audio.play();
};


micBtn.onclick = async () => {

    if (!isRecording) {
        startRecording();
    } else {
        stopRecording();
    }
};


async function startRecording() {

    const stream = await navigator.mediaDevices.getUserMedia({
        audio: true
    });

    mediaRecorder = new MediaRecorder(stream, {
        mimeType: "audio/webm"
    });

    mediaRecorder.start(250);

    mediaRecorder.ondataavailable = async (event) => {

        if (event.data.size > 0 &&
            voiceSocket.readyState === WebSocket.OPEN) {

            const arrayBuffer = await event.data.arrayBuffer();

            voiceSocket.send(arrayBuffer);
        }
    };

    isRecording = true;

    micBtn.innerText = "⏹ Stop Voice";
    micBtn.classList.add("recording");
}


function stopRecording() {

    mediaRecorder.stop();

    isRecording = false;

    micBtn.innerText = "🎤 Start Voice";
    micBtn.classList.remove("recording");
}


// =======================
// UI
// =======================

function appendMessage(text, sender) {

    const div = document.createElement("div");

    div.classList.add("message");
    div.classList.add(sender);

    div.innerHTML = marked.parse(text);

    chatBox.appendChild(div);

    chatBox.scrollTop = chatBox.scrollHeight;
}