Spaces:
Sleeping
Sleeping
File size: 7,471 Bytes
81726c9 529b177 81726c9 f6bb754 81726c9 f6bb754 81726c9 f6bb754 81726c9 f6bb754 81726c9 529b177 81726c9 529b177 81726c9 529b177 81726c9 f6bb754 81726c9 529b177 81726c9 529b177 81726c9 529b177 81726c9 529b177 81726c9 529b177 81726c9 529b177 81726c9 529b177 81726c9 529b177 81726c9 f6bb754 81726c9 f6bb754 81726c9 f6bb754 | 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 | // static/js/design.js
// ββ Marked.js config ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
marked.setOptions({
breaks: true,
gfm: true,
highlight: (code, lang) => {
if (lang && hljs.getLanguage(lang)) {
return hljs.highlight(code, { language: lang }).value;
}
return hljs.highlightAuto(code).value;
}
});
// ββ DOM refs ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
const chatWindow = document.getElementById("chatWindow");
const queryInput = document.getElementById("queryInput");
const sendBtn = document.getElementById("sendBtn");
const topicInput = document.getElementById("topicInput");
const clearBtn = document.getElementById("clearBtn");
const menuBtn = document.getElementById("menuBtn");
const sidebar = document.getElementById("sidebar");
const overlay = document.getElementById("overlay");
const sidebarClose = document.getElementById("sidebarClose");
const quickBtns = document.querySelectorAll(".quick-btn");
// ββ Conversation history ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
let history = [];
// ββ Sidebar toggle ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
function openSidebar() {
sidebar.classList.add("open");
overlay.classList.add("show");
}
function closeSidebar() {
sidebar.classList.remove("open");
overlay.classList.remove("show");
}
menuBtn.addEventListener("click", openSidebar);
sidebarClose.addEventListener("click", closeSidebar);
overlay.addEventListener("click", closeSidebar);
// ββ Auto-resize textarea ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
function autoResize() {
queryInput.style.height = "auto";
queryInput.style.height = Math.min(queryInput.scrollHeight, 200) + "px";
}
queryInput.addEventListener("input", autoResize);
// ββ Quick prompt buttons ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
quickBtns.forEach(btn => {
btn.addEventListener("click", () => {
queryInput.value = btn.dataset.prompt;
autoResize();
queryInput.focus();
closeSidebar();
});
});
// ββ Clear chat ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
clearBtn.addEventListener("click", () => {
history = [];
const welcome = document.getElementById("welcomeMsg");
chatWindow.innerHTML = "";
if (welcome) chatWindow.appendChild(welcome);
});
// ββ Scroll to bottom ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
function scrollToBottom() {
chatWindow.scrollTo({ top: chatWindow.scrollHeight, behavior: "smooth" });
}
// ββ Add message bubble ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
function addMessage(role, content) {
const msg = document.createElement("div");
msg.className = `message ${role}`;
const avatar = document.createElement("div");
avatar.className = "avatar";
avatar.textContent = role === "user" ? "YOU" : "SS";
const bubble = document.createElement("div");
bubble.className = "bubble";
if (role === "assistant") {
bubble.innerHTML = marked.parse(content);
bubble.querySelectorAll("pre code").forEach(el => hljs.highlightElement(el));
} else {
// user bubble: preserve newlines and show code syntax as plain text
bubble.style.whiteSpace = "pre-wrap";
bubble.textContent = content;
}
msg.appendChild(avatar);
msg.appendChild(bubble);
chatWindow.appendChild(msg);
scrollToBottom();
}
// ββ Typing indicator ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
function showTyping() {
const msg = document.createElement("div");
msg.className = "message assistant";
msg.id = "typingIndicator";
const avatar = document.createElement("div");
avatar.className = "avatar";
avatar.textContent = "SS";
const bubble = document.createElement("div");
bubble.className = "bubble";
bubble.innerHTML = `
<div class="typing-indicator">
<span></span><span></span><span></span>
</div>`;
msg.appendChild(avatar);
msg.appendChild(bubble);
chatWindow.appendChild(msg);
scrollToBottom();
}
function hideTyping() {
const indicator = document.getElementById("typingIndicator");
if (indicator) indicator.remove();
}
// ββ Send message ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
async function sendMessage() {
const query = queryInput.value.trim();
if (!query) return;
const topic = topicInput.value.trim() || null;
addMessage("user", query);
history.push({ role: "user", content: query });
// reset input
queryInput.value = "";
queryInput.style.height = "auto";
sendBtn.disabled = true;
showTyping();
try {
const res = await fetch("/chat", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ query, topic, history })
});
const data = await res.json();
hideTyping();
if (data.error) {
addMessage("assistant", `β οΈ **Error:** ${data.error}`);
} else {
addMessage("assistant", data.response);
history.push({ role: "assistant", content: data.response });
}
} catch (err) {
hideTyping();
addMessage("assistant", "β οΈ **Network error.** Flask server se connect nahi ho saka.");
} finally {
sendBtn.disabled = false;
queryInput.focus();
}
}
// ββ Keyboard handling βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
queryInput.addEventListener("keydown", (e) => {
if (e.key === "Enter") {
if (e.shiftKey) {
// Shift+Enter β insert a real newline and resize
// let the browser insert \n naturally, then resize
setTimeout(autoResize, 0);
return; // do NOT send
}
// plain Enter β send
e.preventDefault();
sendMessage();
}
});
// ββ Send button βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
sendBtn.addEventListener("click", sendMessage); |