({ type, content })
| 208 | } |
| 209 | |
| 210 | function handleJSONMessage({ type, content }) { |
| 211 | if (type === "partial_user_request") { |
| 212 | typingUser = content?.trim() ? escapeHtml(content) : ""; |
| 213 | renderMessages(); |
| 214 | return; |
| 215 | } |
| 216 | if (type === "final_user_request") { |
| 217 | if (content?.trim()) { |
| 218 | chatHistory.push({ role: "user", content, type: "final" }); |
| 219 | } |
| 220 | typingUser = ""; |
| 221 | renderMessages(); |
| 222 | return; |
| 223 | } |
| 224 | if (type === "partial_assistant_answer") { |
| 225 | typingAssistant = content?.trim() ? escapeHtml(content) : ""; |
| 226 | renderMessages(); |
| 227 | return; |
| 228 | } |
| 229 | if (type === "final_assistant_answer") { |
| 230 | if (content?.trim()) { |
| 231 | chatHistory.push({ role: "assistant", content, type: "final" }); |
| 232 | } |
| 233 | typingAssistant = ""; |
| 234 | renderMessages(); |
| 235 | return; |
| 236 | } |
| 237 | if (type === "tts_chunk") { |
| 238 | if (ignoreIncomingTTS) return; |
| 239 | const int16Data = base64ToInt16Array(content); |
| 240 | if (ttsWorkletNode) { |
| 241 | ttsWorkletNode.port.postMessage(int16Data); |
| 242 | } |
| 243 | return; |
| 244 | } |
| 245 | if (type === "tts_interruption") { |
| 246 | if (ttsWorkletNode) { |
| 247 | ttsWorkletNode.port.postMessage({ type: "clear" }); |
| 248 | } |
| 249 | isTTSPlaying = false; |
| 250 | ignoreIncomingTTS = false; |
| 251 | return; |
| 252 | } |
| 253 | if (type === "stop_tts") { |
| 254 | if (ttsWorkletNode) { |
| 255 | ttsWorkletNode.port.postMessage({ type: "clear" }); |
| 256 | } |
| 257 | isTTSPlaying = false; |
| 258 | ignoreIncomingTTS = true; |
| 259 | console.log("TTS playback stopped. Reason: tts_interruption."); |
| 260 | socket.send(JSON.stringify({ type: 'tts_stop' })); |
| 261 | return; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | function escapeHtml(str) { |
| 266 | return (str ?? '') |
no test coverage detected