File size: 10,030 Bytes
cb2c324 c0bf8f4 cb2c324 c0bf8f4 2cbc36c c0bf8f4 572e29a 1466a0d c0bf8f4 f579830 2cbc36c 572e29a 2cbc36c c0bf8f4 2cbc36c c0bf8f4 2cbc36c 572e29a 2cbc36c c0bf8f4 572e29a 2cbc36c 572e29a 2cbc36c 9afdcf4 572e29a dfed745 572e29a dfed745 6f207c7 572e29a 2cbc36c cb2c324 c0bf8f4 1466a0d c0bf8f4 dfed745 c0bf8f4 dfed745 c0bf8f4 dfed745 c0bf8f4 1466a0d 2cbc36c ea7787f c0bf8f4 572e29a cb2c324 572e29a ea7787f 572e29a 2cbc36c ea7787f c0bf8f4 cb2c324 2cbc36c cb2c324 2cbc36c 3ad8d2c cb2c324 54c1955 1466a0d c0bf8f4 6f207c7 1466a0d 2cbc36c 54c1955 2cbc36c 572e29a 2cbc36c 572e29a c0bf8f4 2cbc36c 572e29a 2cbc36c c0bf8f4 1466a0d c0bf8f4 dfed745 c0bf8f4 572e29a 2cbc36c 54c1955 fc6286e 572e29a c0bf8f4 572e29a c0bf8f4 cb2c324 ea7787f 572e29a cb2c324 2cbc36c 1466a0d 572e29a 2cbc36c c0bf8f4 1466a0d c0bf8f4 ea7787f 1466a0d f579830 572e29a 2cbc36c cb2c324 2cbc36c 572e29a c0bf8f4 572e29a 1466a0d c0bf8f4 572e29a c0bf8f4 572e29a c0bf8f4 572e29a 2cbc36c ea7787f c0bf8f4 572e29a cb2c324 | 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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | document.addEventListener("DOMContentLoaded", () => {
// --- UI Selectors ---
const sidebar = document.getElementById("sidebar");
const openSidebarBtn = document.getElementById("open-sidebar");
const closeSidebarBtn = document.getElementById("close-sidebar");
const chatMessages = document.getElementById("chat-messages");
const chatViewport = document.getElementById("chat-viewport");
const chatForm = document.getElementById("chat-form");
const userInput = document.getElementById("user-input");
const usageStats = document.getElementById("usage-stats");
const authTrigger = document.getElementById("auth-trigger");
const authModal = document.getElementById("auth-modal");
const loginForm = document.getElementById("login-form");
const signupForm = document.getElementById("signup-form");
const logoutBtn = document.getElementById("logout-btn");
const historyList = document.getElementById("history-list");
const tabBtns = document.querySelectorAll(".tab-btn");
const cpuVal = document.getElementById("cpu-val");
const ramVal = document.getElementById("ram-val");
const sysPills = document.getElementById("sys-pills");
const totalQEl = document.getElementById("total-q");
let isStreaming = false;
// --- Sidebar & Navigation ---
openSidebarBtn.onclick = () => {
sidebar.classList.remove("sidebar-closed");
fetchHistory();
};
closeSidebarBtn.onclick = () => sidebar.classList.add("sidebar-closed");
// --- Auth Modal Logic ---
authTrigger.onclick = () => authModal.classList.remove("hidden");
document.querySelector(".close-modal-btn").onclick = () => authModal.classList.add("hidden");
tabBtns.forEach(btn => {
btn.onclick = () => {
tabBtns.forEach(b => b.classList.remove("active"));
btn.classList.add("active");
if(btn.dataset.tab === "login") {
loginForm.classList.remove("hidden");
signupForm.classList.add("hidden");
} else {
loginForm.classList.add("hidden");
signupForm.classList.remove("hidden");
}
};
});
loginForm.onsubmit = (e) => handleAuth(e, "login");
signupForm.onsubmit = (e) => handleAuth(e, "signup");
async function handleAuth(e, action) {
e.preventDefault();
const email = e.target.querySelector("input[type='email']").value;
const password = e.target.querySelector("input[type='password']").value;
try {
const res = await fetch(`/auth/${action}`, {
method: "POST",
headers: {"Content-Type": "application/json"},
body: JSON.stringify({email, password})
});
const data = await res.json();
if(data.success) location.reload();
else alert(data.error);
} catch(err) { alert("خطأ في الاتصال"); }
}
logoutBtn.onclick = async () => {
await fetch("/logout");
location.reload();
};
// --- Chat & Streaming ---
function appendMessage(text, type, meta = null) {
const div = document.createElement("div");
div.className = `message ${type === 'user' ? 'user-msg' : 'bot-msg'}`;
let metaHtml = meta ? `
<div class="msg-footer">
<span><i class="fas fa-bolt"></i> ${meta.duration.toFixed(2)}s</span>
<span><i class="fas fa-coins"></i> ${meta.token_count}</span>
</div>
` : '';
div.innerHTML = `
<div class="msg-bubble">${text}</div>
${metaHtml}
`;
chatMessages.appendChild(div);
chatViewport.scrollTop = chatViewport.scrollHeight;
return div;
}
chatForm.onsubmit = async (e) => {
e.preventDefault();
const q = userInput.value.trim();
const tier = document.getElementById("model-tier").value;
if(!q || isStreaming) return;
appendMessage(q, 'user');
userInput.value = "";
isStreaming = true;
const botMsgDiv = document.createElement("div");
botMsgDiv.className = "message bot-msg";
const stepsContainer = document.createElement("div");
stepsContainer.className = "steps-container";
const currentStepEl = document.createElement("div");
currentStepEl.className = "generating-status active-step";
currentStepEl.innerHTML = `<i class="fas fa-circle-notch fa-spin"></i> <span>جاري البدء (${tier})...</span>`;
botMsgDiv.appendChild(stepsContainer);
botMsgDiv.appendChild(currentStepEl);
chatMessages.appendChild(botMsgDiv);
let completedSteps = new Set();
try {
const res = await fetch("/chat", {
method: "POST",
headers: {"Content-Type": "application/json"},
body: JSON.stringify({question: q, tier: tier})
});
if(!res.ok) {
const err = await res.json();
botMsgDiv.innerHTML = `<div class="msg-bubble">❌ ${err.error}</div>`;
isStreaming = false; return;
}
const reader = res.body.getReader();
const decoder = new TextDecoder();
while(true) {
const {value, done} = await reader.read();
if(done) break;
const lines = decoder.decode(value).split("\n");
for(const line of lines) {
if(!line.trim()) continue;
const data = JSON.parse(line);
if(data.status === "Generating") {
const stepText = data.step;
const currentText = currentStepEl.querySelector("span").textContent;
if (currentText !== stepText && currentText !== `جاري البدء (${tier})...`) {
if (!completedSteps.has(currentText)) {
const doneStep = document.createElement("div");
doneStep.className = "step-item completed";
doneStep.innerHTML = `<i class="fas fa-check-circle"></i> ${currentText}`;
stepsContainer.appendChild(doneStep);
completedSteps.add(currentText);
}
}
currentStepEl.querySelector("span").textContent = stepText;
} else if(data.status === "Complete" || data.response_text) {
currentStepEl.remove();
const bubble = document.createElement("div");
bubble.className = "msg-bubble fade-in";
bubble.innerHTML = data.response_text;
const footer = document.createElement("div");
footer.className = "msg-footer";
footer.innerHTML = `
<span><i class="fas fa-bolt"></i> ${data.duration.toFixed(2)}s</span>
<span><i class="fas fa-coins"></i> ${data.token_count}</span>
`;
botMsgDiv.appendChild(bubble);
botMsgDiv.appendChild(footer);
updateGlobalStatus();
}
}
}
} catch(err) {
botMsgDiv.innerHTML = `<div class="msg-bubble">❌ فشل في الاتصال</div>`;
} finally {
isStreaming = false;
chatViewport.scrollTop = chatViewport.scrollHeight;
}
};
async function updateGlobalStatus() {
try {
const res = await fetch("/status");
const data = await res.json();
if(data.logged_in) {
authTrigger.classList.add("hidden");
logoutBtn.classList.remove("hidden");
sysPills.classList.remove("hidden");
usageStats.innerHTML = `
<span class="pill primary"><i class="fas fa-user"></i> ${data.user}</span>
<span class="pill"><i class="fas fa-coins"></i> ${data.tokens}/${data.max}</span>
`;
} else {
authTrigger.classList.remove("hidden");
logoutBtn.classList.add("hidden");
sysPills.classList.add("hidden");
usageStats.innerHTML = `
<span class="pill"><i class="fas fa-user-secret"></i> ضيف</span>
<span class="pill"><i class="fas fa-coins"></i> ${data.tokens}/${data.max}</span>
`;
}
cpuVal.textContent = data.cpu.toFixed(0);
ramVal.textContent = data.ram.toFixed(0);
totalQEl.textContent = data.total_questions;
} catch(e) {}
}
async function fetchHistory() {
try {
const res = await fetch("/history");
const data = await res.json();
historyList.innerHTML = "";
data.reverse().forEach(item => {
const div = document.createElement("div");
div.className = "history-item";
div.innerHTML = `<i class="fas fa-comment"></i> ${item.q.substring(0, 30)}...`;
div.onclick = () => {
chatMessages.innerHTML = "";
appendMessage(item.q, 'user');
appendMessage(item.a, 'bot', {duration: 0, token_count: item.tokens || 0});
sidebar.classList.add("sidebar-closed");
};
historyList.appendChild(div);
});
} catch(e) {}
}
setInterval(updateGlobalStatus, 10000);
updateGlobalStatus();
});
|