Spaces:
Sleeping
Sleeping
Download static/app.js from FunWith/irish-maths-ai: direct link, hf CLI and curl.
- Browser
- Download file 14.5 kB
-
https://huggingface.co/spaces/FunWith/irish-maths-ai/resolve/main/static/app.js
- Command line
-
hf download hf://spaces/FunWith/irish-maths-ai/static/app.js
-
curl -L -o app.js https://huggingface.co/spaces/FunWith/irish-maths-ai/resolve/main/static/app.js
14.5 kB
| /* ── Irish Maths AI — Mathaí Frontend ──────────────────────────────── */ | |
| let state = { | |
| role: "", // "student" | "teacher" | |
| level: "", // "primary" | "junior_cycle" | "senior_cycle" | |
| classGroup: "", // "5th Class", "Higher Level" etc. | |
| history: [], // [{role, content}] | |
| sending: false, | |
| }; | |
| // Elements | |
| const welcomeScreen = document.getElementById("welcomeScreen"); | |
| const chatScreen = document.getElementById("chatScreen"); | |
| const levelPicker = document.getElementById("levelPicker"); | |
| const classPicker = document.getElementById("classPicker"); | |
| const classGrid = document.getElementById("classGrid"); | |
| const chatMain = document.getElementById("chatMain"); | |
| const messages = document.getElementById("messages"); | |
| const quickTopics = document.getElementById("quickTopics"); | |
| const chatInput = document.getElementById("chatInput"); | |
| const sendBtn = document.getElementById("sendBtn"); | |
| const headerLevel = document.getElementById("headerLevel"); | |
| const topicsSidebar = document.getElementById("topicsSidebar"); | |
| const sidebarContent = document.getElementById("sidebarContent"); | |
| // ── Welcome Flow ───────────────────────────────────────────────────── | |
| document.getElementById("pickStudent").addEventListener("click", () => { | |
| state.role = "student"; | |
| document.querySelectorAll(".role-card").forEach(c => c.classList.remove("selected")); | |
| document.getElementById("pickStudent").classList.add("selected"); | |
| levelPicker.style.display = "block"; | |
| classPicker.style.display = "none"; | |
| levelPicker.scrollIntoView({ behavior: "smooth" }); | |
| }); | |
| document.getElementById("pickTeacher").addEventListener("click", () => { | |
| state.role = "teacher"; | |
| state.level = ""; | |
| state.classGroup = "Teacher"; | |
| startChat(); | |
| }); | |
| document.querySelectorAll(".level-card").forEach(card => { | |
| card.addEventListener("click", () => { | |
| state.level = card.dataset.level; | |
| if (state.level === "primary") { | |
| showClassPicker([ | |
| "Junior Infants", "Senior Infants", | |
| "1st Class", "2nd Class", | |
| "3rd Class", "4th Class", | |
| "5th Class", "6th Class" | |
| ]); | |
| } else if (state.level === "junior_cycle") { | |
| showClassPicker(["1st Year", "2nd Year", "3rd Year", "Ordinary Level", "Higher Level"]); | |
| } else if (state.level === "senior_cycle") { | |
| showClassPicker(["5th Year", "6th Year", "Foundation Level", "Ordinary Level", "Higher Level"]); | |
| } | |
| }); | |
| }); | |
| function showClassPicker(classes) { | |
| classPicker.style.display = "block"; | |
| classGrid.innerHTML = ""; | |
| classes.forEach(c => { | |
| const btn = document.createElement("button"); | |
| btn.className = "class-btn"; | |
| btn.textContent = c; | |
| btn.addEventListener("click", () => { | |
| state.classGroup = c; | |
| startChat(); | |
| }); | |
| classGrid.appendChild(btn); | |
| }); | |
| classPicker.scrollIntoView({ behavior: "smooth" }); | |
| } | |
| // ── Start Chat ─────────────────────────────────────────────────────── | |
| function startChat() { | |
| welcomeScreen.style.display = "none"; | |
| chatScreen.style.display = "flex"; | |
| // Set header level text | |
| if (state.role === "teacher") { | |
| headerLevel.textContent = "Teacher Mode"; | |
| } else { | |
| const levelLabels = { | |
| primary: "Primary", | |
| junior_cycle: "Junior Cycle", | |
| senior_cycle: "Senior Cycle" | |
| }; | |
| headerLevel.textContent = `${levelLabels[state.level] || ""} · ${state.classGroup}`; | |
| } | |
| state.history = []; | |
| messages.innerHTML = ""; | |
| // Show welcome message from Mathaí | |
| const greeting = state.role === "teacher" | |
| ? "Hello! 👋 I'm **Mathaí**, your Irish maths teaching assistant. I can help with lesson plans, worksheet ideas, curriculum references and more. What can I help with?" | |
| : `Hello! 👋 I'm **Mathaí**, your maths buddy! I'm here to help you with any maths questions. Just ask me anything — no question is too simple. What would you like to learn today?`; | |
| addMessage("ai", greeting); | |
| loadQuickTopics(); | |
| loadTopicsSidebar(); | |
| chatInput.focus(); | |
| } | |
| // ── Quick Topics ───────────────────────────────────────────────────── | |
| async function loadQuickTopics() { | |
| try { | |
| const resp = await fetch("/api/quick-topics"); | |
| const data = await resp.json(); | |
| let topics; | |
| if (state.role === "teacher") { | |
| topics = data.teacher || []; | |
| } else { | |
| topics = (data.student && data.student[state.level]) || []; | |
| } | |
| quickTopics.innerHTML = ""; | |
| topics.forEach(t => { | |
| const btn = document.createElement("button"); | |
| btn.className = "quick-topic-btn"; | |
| btn.innerHTML = `<span class="qt-icon">${t.icon || "📌"}</span> ${escapeHtml(t.label)}`; | |
| btn.addEventListener("click", () => { | |
| chatInput.value = t.query; | |
| sendMessage(); | |
| quickTopics.style.display = "none"; | |
| }); | |
| quickTopics.appendChild(btn); | |
| }); | |
| } catch (e) { | |
| quickTopics.innerHTML = ""; | |
| } | |
| } | |
| // ── Topics Sidebar ─────────────────────────────────────────────────── | |
| document.getElementById("topicsBtn").addEventListener("click", () => { | |
| topicsSidebar.classList.toggle("open"); | |
| }); | |
| document.getElementById("sidebarClose").addEventListener("click", () => { | |
| topicsSidebar.classList.remove("open"); | |
| }); | |
| async function loadTopicsSidebar() { | |
| const level = state.level || "primary"; | |
| try { | |
| const resp = await fetch(`/api/curriculum/${level}`); | |
| const data = await resp.json(); | |
| const strands = data.strands || {}; | |
| sidebarContent.innerHTML = ""; | |
| for (const [key, strand] of Object.entries(strands)) { | |
| const div = document.createElement("div"); | |
| div.className = "sidebar-strand"; | |
| const header = document.createElement("div"); | |
| header.className = "strand-header"; | |
| header.textContent = strand.label || key; | |
| div.appendChild(header); | |
| const topics = strand.topics || {}; | |
| for (const [tKey, topic] of Object.entries(topics)) { | |
| const btn = document.createElement("button"); | |
| btn.className = "topic-btn"; | |
| btn.textContent = topic.label || tKey; | |
| btn.addEventListener("click", () => { | |
| chatInput.value = `Explain ${topic.label} for ${state.classGroup || "me"}`; | |
| topicsSidebar.classList.remove("open"); | |
| sendMessage(); | |
| }); | |
| div.appendChild(btn); | |
| } | |
| sidebarContent.appendChild(div); | |
| } | |
| } catch (e) { | |
| sidebarContent.innerHTML = `<p style="padding:1rem;color:var(--text-muted)">Could not load topics.</p>`; | |
| } | |
| } | |
| // ── Chat ───────────────────────────────────────────────────────────── | |
| chatInput.addEventListener("keydown", (e) => { | |
| if (e.key === "Enter" && !e.shiftKey) { | |
| e.preventDefault(); | |
| sendMessage(); | |
| } | |
| }); | |
| chatInput.addEventListener("input", () => { | |
| chatInput.style.height = "auto"; | |
| chatInput.style.height = Math.min(chatInput.scrollHeight, 120) + "px"; | |
| }); | |
| sendBtn.addEventListener("click", sendMessage); | |
| async function sendMessage() { | |
| const text = chatInput.value.trim(); | |
| if (!text || state.sending) return; | |
| state.sending = true; | |
| sendBtn.disabled = true; | |
| chatInput.value = ""; | |
| chatInput.style.height = "auto"; | |
| // Hide quick topics after first message | |
| quickTopics.style.display = "none"; | |
| // Add user message | |
| addMessage("user", text); | |
| state.history.push({ role: "user", content: text }); | |
| // Show typing indicator | |
| const typingEl = addTyping(); | |
| try { | |
| const resp = await fetch("/api/chat", { | |
| method: "POST", | |
| headers: { "Content-Type": "application/json" }, | |
| body: JSON.stringify({ | |
| message: text, | |
| level: state.level, | |
| class_group: state.classGroup, | |
| role: state.role, | |
| history: state.history.slice(-10), | |
| }), | |
| }); | |
| const data = await resp.json(); | |
| typingEl.remove(); | |
| addMessage("ai", data.reply, data.sources || []); | |
| state.history.push({ role: "assistant", content: data.reply }); | |
| } catch (e) { | |
| typingEl.remove(); | |
| addMessage("ai", "Sorry, I couldn't connect. Please check your internet and try again."); | |
| } | |
| state.sending = false; | |
| sendBtn.disabled = false; | |
| chatInput.focus(); | |
| } | |
| // ── Message Rendering ──────────────────────────────────────────────── | |
| function addMessage(type, content, sources = []) { | |
| const div = document.createElement("div"); | |
| div.className = `msg msg-${type}`; | |
| const avatar = document.createElement("div"); | |
| avatar.className = "msg-avatar"; | |
| avatar.textContent = type === "user" ? "🙋" : "🧮"; | |
| const bubble = document.createElement("div"); | |
| bubble.className = "msg-bubble"; | |
| if (type === "ai") { | |
| bubble.innerHTML = renderMarkdown(content); | |
| // Render KaTeX | |
| if (window.renderMathInElement) { | |
| renderMathInElement(bubble, { | |
| delimiters: [ | |
| { left: "$$", right: "$$", display: true }, | |
| { left: "$", right: "$", display: false }, | |
| { left: "\\(", right: "\\)", display: false }, | |
| { left: "\\[", right: "\\]", display: true }, | |
| ], | |
| throwOnError: false, | |
| }); | |
| } | |
| // Add source tags | |
| if (sources.length > 0) { | |
| const sourcesDiv = document.createElement("div"); | |
| sourcesDiv.className = "msg-sources"; | |
| const seen = new Set(); | |
| sources.forEach(s => { | |
| const key = `${s.strand} · ${s.topic}`; | |
| if (seen.has(key)) return; | |
| seen.add(key); | |
| const tag = document.createElement("span"); | |
| tag.className = "source-tag"; | |
| tag.textContent = key; | |
| sourcesDiv.appendChild(tag); | |
| }); | |
| bubble.appendChild(sourcesDiv); | |
| } | |
| } else { | |
| bubble.textContent = content; | |
| } | |
| div.appendChild(avatar); | |
| div.appendChild(bubble); | |
| messages.appendChild(div); | |
| scrollToBottom(); | |
| } | |
| function addTyping() { | |
| const div = document.createElement("div"); | |
| div.className = "msg msg-ai"; | |
| div.id = "typing-indicator"; | |
| const avatar = document.createElement("div"); | |
| avatar.className = "msg-avatar"; | |
| avatar.textContent = "🧮"; | |
| const bubble = document.createElement("div"); | |
| bubble.className = "msg-bubble"; | |
| bubble.innerHTML = `<div class="typing-dots"><span></span><span></span><span></span></div>`; | |
| div.appendChild(avatar); | |
| div.appendChild(bubble); | |
| messages.appendChild(div); | |
| scrollToBottom(); | |
| return div; | |
| } | |
| function scrollToBottom() { | |
| chatMain.scrollTop = chatMain.scrollHeight; | |
| } | |
| // ── Simple Markdown → HTML ─────────────────────────────────────────── | |
| function renderMarkdown(text) { | |
| if (!text) return ""; | |
| let html = escapeHtml(text); | |
| // Bold: **text** | |
| html = html.replace(/\*\*(.+?)\*\*/g, "<strong>$1</strong>"); | |
| // Italic: *text* | |
| html = html.replace(/\*(.+?)\*/g, "<em>$1</em>"); | |
| // Inline code: `text` | |
| html = html.replace(/`(.+?)`/g, "<code>$1</code>"); | |
| // Unordered list items: - text or • text | |
| html = html.replace(/^[\-•]\s+(.+)$/gm, "<li>$1</li>"); | |
| // Ordered list items: 1. text | |
| html = html.replace(/^\d+\.\s+(.+)$/gm, "<li>$1</li>"); | |
| // Wrap consecutive <li> in <ul> | |
| html = html.replace(/((?:<li>.*<\/li>\n?)+)/g, "<ul>$1</ul>"); | |
| // Headings: ### text | |
| html = html.replace(/^###\s+(.+)$/gm, "<strong>$1</strong>"); | |
| // Paragraphs (double newline) | |
| html = html.replace(/\n\n/g, "</p><p>"); | |
| // Single newlines | |
| html = html.replace(/\n/g, "<br>"); | |
| return `<p>${html}</p>`; | |
| } | |
| function escapeHtml(str) { | |
| const div = document.createElement("div"); | |
| div.textContent = str; | |
| return div.innerHTML; | |
| } | |
| // ── Header Buttons ─────────────────────────────────────────────────── | |
| document.getElementById("backBtn").addEventListener("click", () => { | |
| chatScreen.style.display = "none"; | |
| welcomeScreen.style.display = "flex"; | |
| state.history = []; | |
| // Reset selections | |
| document.querySelectorAll(".role-card").forEach(c => c.classList.remove("selected")); | |
| levelPicker.style.display = "none"; | |
| classPicker.style.display = "none"; | |
| }); | |
| document.getElementById("newChatBtn").addEventListener("click", () => { | |
| state.history = []; | |
| messages.innerHTML = ""; | |
| quickTopics.style.display = "flex"; | |
| loadQuickTopics(); | |
| const greeting = state.role === "teacher" | |
| ? "Fresh start! What would you like help with?" | |
| : "Let's start fresh! 🎯 What maths topic can I help you with?"; | |
| addMessage("ai", greeting); | |
| chatInput.focus(); | |
| }); | |