From 1cce22ee2f224fa83e0a8b6f83fcb28bc3a1352a Mon Sep 17 00:00:00 2001 From: Renato Rosa Date: Wed, 19 Aug 2026 20:35:15 -0300 Subject: [PATCH] Adicionar web/chat.js --- web/chat.js | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 web/chat.js diff --git a/web/chat.js b/web/chat.js new file mode 100644 index 0000000..de4db89 --- /dev/null +++ b/web/chat.js @@ -0,0 +1,39 @@ +// All comments are in English. + +let conversationId = null; + +function login() { + window.location = "/login"; +} + +async function newConversation() { + const r = await fetch("/api/new_conversation"); + const data = await r.json(); + conversationId = data.conversation_id; + document.getElementById("chat").innerHTML = ""; +} + +async function sendMessage() { + const prompt = document.getElementById("prompt").value; + const model = "llama3.2"; + + const chatBox = document.getElementById("chat"); + chatBox.innerHTML += `
${prompt}
`; + + const url = `/api/stream?conversation_id=${conversationId}&model=${model}&prompt=${encodeURIComponent(prompt)}`; + const evtSource = new EventSource(url); + + let buffer = ""; + + evtSource.onmessage = function(event) { + const data = JSON.parse(event.data); + buffer += data.token; + + document.getElementById("chat").innerHTML += + `
${buffer}
`; + }; + + evtSource.onerror = function() { + evtSource.close(); + }; +}