rn;
          }

          buffer += event.data;
          buffer = buffer.replace(/\s+/g, " ").trim();
          const formattedBuffer = buffer.replace(/\n/g, "<br>");

          //console.log("Formatted response so far:", formattedBuffer);

          const chatDiv = document.getElementById("chat");
          let lastMessage = chatDiv.lastElementChild;

          if (lastMessage && lastMessage.className === "bot") {
            lastMessage.innerHTML = formattedBuffer;
          } else {
            const b