Atualizar web/chat.js
This commit is contained in:
+94
-22
@@ -1,39 +1,111 @@
|
|||||||
// All comments are in English.
|
// All comments are in English.
|
||||||
|
|
||||||
let conversationId = null;
|
let conversationId = null;
|
||||||
|
let conversations = [];
|
||||||
|
|
||||||
function login() {
|
async function login() {
|
||||||
window.location = "/login";
|
window.location = "/login";
|
||||||
|
}
|
||||||
|
|
||||||
|
function toggleTheme() {
|
||||||
|
document.body.classList.toggle("dark");
|
||||||
|
}
|
||||||
|
|
||||||
|
async function loadConversations() {
|
||||||
|
const r = await fetch("/api/conversations");
|
||||||
|
conversations = await r.json();
|
||||||
|
renderTabs();
|
||||||
|
}
|
||||||
|
|
||||||
|
function renderTabs() {
|
||||||
|
const tabs = document.getElementById("tabs");
|
||||||
|
tabs.innerHTML = "";
|
||||||
|
conversations.forEach(c => {
|
||||||
|
const btn = document.createElement("button");
|
||||||
|
btn.textContent = c.title;
|
||||||
|
btn.onclick = () => selectConversation(c.id);
|
||||||
|
tabs.appendChild(btn);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function newConversation() {
|
async function newConversation() {
|
||||||
const r = await fetch("/api/new_conversation");
|
const title = prompt("Conversation title:");
|
||||||
const data = await r.json();
|
const r = await fetch("/api/new_conversation", {
|
||||||
conversationId = data.conversation_id;
|
method: "POST",
|
||||||
document.getElementById("chat").innerHTML = "";
|
headers: {"Content-Type": "application/json"},
|
||||||
|
body: JSON.stringify({title})
|
||||||
|
});
|
||||||
|
const data = await r.json();
|
||||||
|
conversationId = data.conversation_id;
|
||||||
|
await loadConversations();
|
||||||
|
await loadHistory();
|
||||||
|
}
|
||||||
|
|
||||||
|
async function selectConversation(id) {
|
||||||
|
conversationId = id;
|
||||||
|
await loadHistory();
|
||||||
|
}
|
||||||
|
|
||||||
|
async function loadHistory() {
|
||||||
|
const r = await fetch(`/api/history/${conversationId}`);
|
||||||
|
const msgs = await r.json();
|
||||||
|
const chat = document.getElementById("chat");
|
||||||
|
chat.innerHTML = "";
|
||||||
|
msgs.forEach(m => {
|
||||||
|
const div = document.createElement("div");
|
||||||
|
div.className = "msg " + m.role;
|
||||||
|
div.innerHTML = `<div class="avatar">${m.role === "user" ? "🧑" : "🤖"}</div>
|
||||||
|
<div class="bubble">${m.content}</div>`;
|
||||||
|
chat.appendChild(div);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function sendMessage() {
|
async function sendMessage() {
|
||||||
const prompt = document.getElementById("prompt").value;
|
const promptText = document.getElementById("prompt").value;
|
||||||
const model = "llama3.2";
|
const file = document.getElementById("attachment").files[0];
|
||||||
|
|
||||||
const chatBox = document.getElementById("chat");
|
if (!conversationId) {
|
||||||
chatBox.innerHTML += `<div class="user">${prompt}</div>`;
|
await newConversation();
|
||||||
|
}
|
||||||
|
|
||||||
const url = `/api/stream?conversation_id=${conversationId}&model=${model}&prompt=${encodeURIComponent(prompt)}`;
|
if (file) {
|
||||||
const evtSource = new EventSource(url);
|
const form = new FormData();
|
||||||
|
form.append("file", file);
|
||||||
|
await fetch(`/api/attachment/${conversationId}`, {
|
||||||
|
method: "POST",
|
||||||
|
body: form
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
let buffer = "";
|
const chat = document.getElementById("chat");
|
||||||
|
const userDiv = document.createElement("div");
|
||||||
|
userDiv.className = "msg user";
|
||||||
|
userDiv.innerHTML = `<div class="avatar">🧑</div>
|
||||||
|
<div class="bubble">${marked.parse(promptText)}</div>`;
|
||||||
|
chat.appendChild(userDiv);
|
||||||
|
|
||||||
evtSource.onmessage = function(event) {
|
const assistantDiv = document.createElement("div");
|
||||||
const data = JSON.parse(event.data);
|
assistantDiv.className = "msg assistant streaming";
|
||||||
buffer += data.token;
|
assistantDiv.innerHTML = `<div class="avatar">🤖</div>
|
||||||
|
<div class="bubble"><span id="stream-text"></span><span class="cursor">▌</span></div>`;
|
||||||
|
chat.appendChild(assistantDiv);
|
||||||
|
|
||||||
document.getElementById("chat").innerHTML +=
|
const url = `/api/stream?conversation_id=${conversationId}&model=llama3.2&prompt=${encodeURIComponent(promptText)}`;
|
||||||
`<div class="assistant">${buffer}</div>`;
|
const evtSource = new EventSource(url);
|
||||||
};
|
|
||||||
|
|
||||||
evtSource.onerror = function() {
|
let buffer = "";
|
||||||
evtSource.close();
|
const streamSpan = document.getElementById("stream-text");
|
||||||
};
|
|
||||||
|
evtSource.onmessage = (event) => {
|
||||||
|
const data = JSON.parse(event.data);
|
||||||
|
buffer += data.token;
|
||||||
|
streamSpan.innerHTML = marked.parse(buffer);
|
||||||
|
};
|
||||||
|
|
||||||
|
evtSource.onerror = () => {
|
||||||
|
evtSource.close();
|
||||||
|
assistantDiv.classList.remove("streaming");
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
window.onload = loadConversations;
|
||||||
|
|||||||
Reference in New Issue
Block a user