688 lines
28 KiB
HTML
688 lines
28 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<meta name="description"
|
|
content="Chat with Moxie Cloud — the Moxiegen AI assistant running on our own inference stack. Sessions are private and time out after 10 minutes.">
|
|
<title>Moxie Cloud • Moxiegen</title>
|
|
|
|
<link rel="icon" type="image/svg+xml" href="/logo.svg">
|
|
|
|
<meta property="og:type" content="website">
|
|
<meta property="og:url" content="https://moxiegen.com/chat.html">
|
|
<meta property="og:title" content="Moxie Cloud — Chat with Moxiegen's AI">
|
|
<meta property="og:description" content="Chat with Moxie Cloud, the Moxiegen AI assistant.">
|
|
<meta property="og:image" content="https://moxiegen.com/logo.png">
|
|
<meta property="og:site_name" content="Moxiegen">
|
|
|
|
<script src="https://cdn.tailwindcss.com"></script>
|
|
<!-- Markdown + sanitizing for assistant replies -->
|
|
<script src="https://cdn.jsdelivr.net/npm/marked@12/marked.min.js"></script>
|
|
<script src="https://cdn.jsdelivr.net/npm/dompurify@3/dist/purify.min.js"></script>
|
|
|
|
<style>
|
|
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&family=Space+Grotesk:wght@500;600&display=swap');
|
|
|
|
* {
|
|
transition-property: color, background-color, border-color, text-decoration-color, fill, stroke;
|
|
transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
|
|
transition-duration: 150ms;
|
|
}
|
|
|
|
html,
|
|
body {
|
|
height: 100%;
|
|
}
|
|
|
|
/* .flex utilities would override the hidden attribute (same specificity, later in source order) */
|
|
[hidden] {
|
|
display: none !important;
|
|
}
|
|
|
|
body {
|
|
height: 100dvh;
|
|
display: flex;
|
|
flex-direction: column;
|
|
overflow: hidden;
|
|
}
|
|
|
|
/* Chat scroll area */
|
|
#chatScroll {
|
|
scrollbar-width: thin;
|
|
scrollbar-color: #1e293b transparent;
|
|
}
|
|
|
|
#chatScroll::-webkit-scrollbar {
|
|
width: 10px;
|
|
}
|
|
|
|
#chatScroll::-webkit-scrollbar-thumb {
|
|
background: #1e293b;
|
|
border-radius: 8px;
|
|
}
|
|
|
|
.chat-bubble {
|
|
transition-property: color, background-color, border-color;
|
|
}
|
|
|
|
/* Assistant markdown */
|
|
.md-body>*:first-child {
|
|
margin-top: 0;
|
|
}
|
|
|
|
.md-body>*:last-child {
|
|
margin-bottom: 0;
|
|
}
|
|
|
|
.md-body p {
|
|
margin: 0 0 0.75rem;
|
|
line-height: 1.6;
|
|
}
|
|
|
|
.md-body h1,
|
|
.md-body h2,
|
|
.md-body h3,
|
|
.md-body h4 {
|
|
font-family: 'Space Grotesk', sans-serif;
|
|
font-weight: 600;
|
|
color: #f8fafc;
|
|
margin: 1.1rem 0 0.5rem;
|
|
line-height: 1.25;
|
|
}
|
|
|
|
.md-body h1 {
|
|
font-size: 1.35rem;
|
|
}
|
|
|
|
.md-body h2 {
|
|
font-size: 1.2rem;
|
|
}
|
|
|
|
.md-body h3 {
|
|
font-size: 1.05rem;
|
|
}
|
|
|
|
.md-body ul,
|
|
.md-body ol {
|
|
margin: 0 0 0.75rem;
|
|
padding-left: 1.4rem;
|
|
}
|
|
|
|
.md-body ul {
|
|
list-style: disc;
|
|
}
|
|
|
|
.md-body ol {
|
|
list-style: decimal;
|
|
}
|
|
|
|
.md-body li {
|
|
margin: 0.25rem 0;
|
|
line-height: 1.55;
|
|
}
|
|
|
|
.md-body a {
|
|
color: #34d399;
|
|
text-decoration: underline;
|
|
text-underline-offset: 2px;
|
|
}
|
|
|
|
.md-body code {
|
|
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
|
|
font-size: 0.875em;
|
|
background: rgba(30, 41, 59, 0.9);
|
|
border: 1px solid #1e293b;
|
|
border-radius: 0.375rem;
|
|
padding: 0.1rem 0.35rem;
|
|
}
|
|
|
|
.md-body pre {
|
|
background: rgba(2, 6, 23, 0.85);
|
|
border: 1px solid #1e293b;
|
|
border-radius: 1rem;
|
|
padding: 1rem 1.1rem;
|
|
overflow-x: auto;
|
|
margin: 0 0 0.75rem;
|
|
line-height: 1.5;
|
|
}
|
|
|
|
.md-body pre code {
|
|
background: transparent;
|
|
border: 0;
|
|
padding: 0;
|
|
font-size: 0.85rem;
|
|
white-space: pre;
|
|
}
|
|
|
|
.md-body blockquote {
|
|
border-left: 4px solid rgba(52, 211, 153, 0.4);
|
|
padding-left: 0.9rem;
|
|
color: #94a3b8;
|
|
margin: 0 0 0.75rem;
|
|
}
|
|
|
|
.md-body table {
|
|
border-collapse: collapse;
|
|
margin: 0 0 0.75rem;
|
|
width: 100%;
|
|
font-size: 0.9rem;
|
|
}
|
|
|
|
.md-body th,
|
|
.md-body td {
|
|
border: 1px solid #1e293b;
|
|
padding: 0.45rem 0.7rem;
|
|
text-align: left;
|
|
}
|
|
|
|
.md-body th {
|
|
background: rgba(30, 41, 59, 0.6);
|
|
}
|
|
|
|
.md-body hr {
|
|
border-color: #1e293b;
|
|
margin: 1rem 0;
|
|
}
|
|
|
|
/* "Thinking" dots */
|
|
.typing-dot {
|
|
width: 7px;
|
|
height: 7px;
|
|
border-radius: 9999px;
|
|
background: #64748b;
|
|
animation: typing-bounce 1.2s infinite ease-in-out;
|
|
}
|
|
|
|
.typing-dot:nth-child(2) {
|
|
animation-delay: 0.15s;
|
|
}
|
|
|
|
.typing-dot:nth-child(3) {
|
|
animation-delay: 0.3s;
|
|
}
|
|
|
|
@keyframes typing-bounce {
|
|
|
|
0%,
|
|
60%,
|
|
100% {
|
|
transform: translateY(0);
|
|
opacity: 0.45;
|
|
}
|
|
|
|
30% {
|
|
transform: translateY(-4px);
|
|
opacity: 1;
|
|
}
|
|
}
|
|
|
|
/* Session countdown urgency */
|
|
.session-warn {
|
|
color: #f87171 !important;
|
|
border-color: rgba(248, 113, 113, 0.5) !important;
|
|
}
|
|
|
|
.fade-in {
|
|
animation: fade-in 0.25s ease;
|
|
}
|
|
|
|
@keyframes fade-in {
|
|
from {
|
|
opacity: 0;
|
|
transform: translateY(6px);
|
|
}
|
|
|
|
to {
|
|
opacity: 1;
|
|
transform: translateY(0);
|
|
}
|
|
}
|
|
|
|
textarea {
|
|
font-size: 16px;
|
|
/* prevents iOS zoom-on-focus */
|
|
}
|
|
</style>
|
|
</head>
|
|
|
|
<body class="bg-slate-950 text-slate-100 antialiased" style="font-family: 'Inter', sans-serif;">
|
|
|
|
<!-- NAVBAR (matches main site) -->
|
|
<nav class="border-b border-slate-800 bg-slate-950/80 backdrop-blur-lg z-50">
|
|
<div class="max-w-screen-2xl mx-auto px-4 sm:px-8 py-4 flex items-center justify-between">
|
|
<a href="index.html" class="flex items-center gap-x-3">
|
|
<img src="logo.svg" alt="Moxiegen" class="h-9 sm:h-11 w-auto" onerror="this.onerror=null; this.src='logo.png';">
|
|
<span class="font-semibold text-2xl sm:text-3xl tracking-[-1px] text-white"
|
|
style="font-family: 'Space Grotesk', sans-serif;">Moxiegen</span>
|
|
</a>
|
|
<div class="flex items-center gap-x-3 sm:gap-x-5">
|
|
<span id="sessionChip" title="Your session ends after 10 minutes of inactivity"
|
|
class="hidden sm:flex items-center gap-x-2 border border-slate-700 bg-slate-900 text-slate-300 text-xs font-medium px-4 py-2 rounded-3xl">
|
|
<svg class="w-3.5 h-3.5 text-emerald-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
|
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
|
|
d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"></path>
|
|
</svg>
|
|
Session <span id="sessionCountdown" class="text-emerald-400 font-semibold">10:00</span>
|
|
</span>
|
|
<button onclick="newChat()"
|
|
class="flex items-center gap-x-2 px-4 sm:px-6 py-2 sm:py-2.5 bg-white text-slate-950 text-sm font-semibold rounded-3xl hover:bg-emerald-400 hover:text-white">
|
|
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
|
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
|
|
d="M12 4v16m8-8H4"></path>
|
|
</svg>
|
|
New chat
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</nav>
|
|
|
|
<!-- CHAT LOG -->
|
|
<main id="chatScroll" class="flex-1 overflow-y-auto">
|
|
<div class="max-w-3xl mx-auto px-4 sm:px-8 py-8" id="chatLog">
|
|
|
|
<!-- WELCOME STATE -->
|
|
<div id="welcome" class="fade-in">
|
|
<div class="text-center pt-8 sm:pt-14 pb-10">
|
|
<div
|
|
class="inline-flex items-center gap-x-2 bg-slate-900 border border-emerald-400/30 text-emerald-400 text-sm font-medium px-5 py-2 rounded-3xl mb-6">
|
|
<div class="w-2 h-2 bg-emerald-400 rounded-full animate-pulse"></div>
|
|
MOXIE CLOUD · ONLINE
|
|
</div>
|
|
<h1 class="text-5xl sm:text-6xl font-semibold leading-none tracking-[-2px] mb-4"
|
|
style="font-family: 'Space Grotesk', sans-serif;">Chat with Moxie.</h1>
|
|
<p class="text-slate-400 text-lg sm:text-xl max-w-xl mx-auto">Powered by our own inference stack.
|
|
Sessions stay on the server and time out after 10 minutes.</p>
|
|
</div>
|
|
<div class="grid grid-cols-1 sm:grid-cols-3 gap-4 pb-4">
|
|
<button onclick="sendSuggestion('What is Moxiegen and what does the Moxiegen Method do?')"
|
|
class="text-left bg-slate-900 border border-slate-800 hover:border-emerald-400/40 rounded-3xl p-5">
|
|
<div class="text-emerald-400 text-2xl mb-3">◆</div>
|
|
<div class="font-medium mb-1">About Moxiegen</div>
|
|
<div class="text-slate-500 text-sm">What is Moxiegen and what does the Moxiegen Method do?</div>
|
|
</button>
|
|
<button onclick="sendSuggestion('Explain hybrid AI agents like I am new to AI, in plain English.')"
|
|
class="text-left bg-slate-900 border border-slate-800 hover:border-emerald-400/40 rounded-3xl p-5">
|
|
<div class="text-emerald-400 text-2xl mb-3">▶</div>
|
|
<div class="font-medium mb-1">Learn something</div>
|
|
<div class="text-slate-500 text-sm">Explain hybrid AI agents like I am new to AI, in plain
|
|
English.</div>
|
|
</button>
|
|
<button onclick="sendSuggestion('Write a Python function that reverses a string, with a short example.')"
|
|
class="text-left bg-slate-900 border border-slate-800 hover:border-emerald-400/40 rounded-3xl p-5">
|
|
<div class="text-emerald-400 text-2xl mb-3">{</div>
|
|
<div class="font-medium mb-1">Write some code</div>
|
|
<div class="text-slate-500 text-sm">Write a Python function that reverses a string, with a
|
|
short example.</div>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</main>
|
|
|
|
<!-- COMPOSER -->
|
|
<footer class="border-t border-slate-800 bg-slate-950/90 backdrop-blur-lg">
|
|
<div class="max-w-3xl mx-auto px-4 sm:px-8 py-4">
|
|
<form id="composer" class="flex items-end gap-3 bg-slate-900 border border-slate-700 focus-within:border-emerald-400 rounded-3xl px-5 py-3">
|
|
<textarea id="promptInput" rows="1" placeholder="Message Moxie Cloud…"
|
|
class="flex-1 bg-transparent outline-none resize-none text-base sm:text-lg text-slate-100 placeholder-slate-500 max-h-40"
|
|
autocomplete="off"></textarea>
|
|
<button id="sendBtn" type="submit" aria-label="Send message"
|
|
class="flex-shrink-0 h-11 w-11 rounded-2xl bg-emerald-400 hover:bg-emerald-300 text-slate-950 flex items-center justify-center disabled:opacity-40 disabled:cursor-not-allowed">
|
|
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
|
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 10l7-7m0 0l7 7m-7-7v18">
|
|
</path>
|
|
</svg>
|
|
</button>
|
|
<button id="stopBtn" type="button" onclick="stopGeneration()" hidden aria-label="Stop generating"
|
|
class="flex-shrink-0 h-11 w-11 rounded-2xl border border-slate-600 hover:border-red-400 hover:text-red-400 text-slate-300 flex items-center justify-center">
|
|
<svg class="w-4 h-4" fill="currentColor" viewBox="0 0 24 24">
|
|
<rect x="6" y="6" width="12" height="12" rx="2"></rect>
|
|
</svg>
|
|
</button>
|
|
</form>
|
|
<div class="flex items-center justify-between gap-4 mt-2 px-2">
|
|
<p class="text-slate-600 text-xs">Moxie Cloud can make mistakes — check important information.</p>
|
|
<p class="text-slate-600 text-xs font-mono hidden sm:block" id="modelTag"></p>
|
|
</div>
|
|
</div>
|
|
</footer>
|
|
|
|
<script>
|
|
// ============================================
|
|
// CONFIG
|
|
// ============================================
|
|
const OLLAMA_BASE_URL = 'https://fo.moxiegen.com';
|
|
const CLOUD_MODEL = 'huihui_ai/gemma-4-abliterated:26b';
|
|
const CLOUD_MODEL_LABEL = 'moxie cloud';
|
|
const SESSION_TIMEOUT_MS = 10 * 60 * 1000; // 10 minutes
|
|
const STORAGE_KEY = 'moxie-cloud-session-v1';
|
|
const SYSTEM_PROMPT = 'You are Moxie Cloud, the assistant on moxiegen.com — the website of Moxiegen, ' +
|
|
'makers of Moxie Desktop, a hybrid AI agent that runs locally on your own machine. ' +
|
|
'Be helpful, direct and friendly. Use markdown (lists, code blocks) when it improves clarity.';
|
|
|
|
// ============================================
|
|
// STATE
|
|
// ============================================
|
|
let messages = []; // {role, content} — what gets sent back to the model
|
|
let lastActivity = Date.now(); // session timer reference
|
|
let generating = false;
|
|
let controller = null; // AbortController for the in-flight request
|
|
let expired = false;
|
|
|
|
const chatScroll = document.getElementById('chatScroll');
|
|
const chatLog = document.getElementById('chatLog');
|
|
const welcome = document.getElementById('welcome');
|
|
const composer = document.getElementById('composer');
|
|
const input = document.getElementById('promptInput');
|
|
const sendBtn = document.getElementById('sendBtn');
|
|
const stopBtn = document.getElementById('stopBtn');
|
|
const countdownEl = document.getElementById('sessionCountdown');
|
|
const sessionChip = document.getElementById('sessionChip');
|
|
|
|
document.getElementById('modelTag').textContent = CLOUD_MODEL_LABEL + ' · ' + CLOUD_MODEL;
|
|
|
|
// ============================================
|
|
// MARKDOWN
|
|
// ============================================
|
|
function renderMarkdown(text) {
|
|
if (window.marked && window.DOMPurify) {
|
|
try {
|
|
return DOMPurify.sanitize(marked.parse(text, { breaks: true, gfm: true }));
|
|
} catch (err) { /* fall through to plain text */ }
|
|
}
|
|
return escapeHtml(text).replace(/\n/g, '<br>');
|
|
}
|
|
|
|
function escapeHtml(s) {
|
|
return String(s).replace(/&/g, '&').replace(/</g, '<')
|
|
.replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, ''');
|
|
}
|
|
|
|
// ============================================
|
|
// SESSION TIMEOUT (10 minutes of inactivity)
|
|
// ============================================
|
|
function touchSession() {
|
|
lastActivity = Date.now();
|
|
if (expired) return;
|
|
try {
|
|
sessionStorage.setItem(STORAGE_KEY, JSON.stringify({ messages, lastActivity }));
|
|
} catch (e) { /* storage unavailable — session stays in memory */ }
|
|
}
|
|
|
|
function formatCountdown(ms) {
|
|
const total = Math.max(0, Math.ceil(ms / 1000));
|
|
const m = Math.floor(total / 60);
|
|
const s = total % 60;
|
|
return m + ':' + String(s).padStart(2, '0');
|
|
}
|
|
|
|
function expireSession() {
|
|
expired = true;
|
|
stopGeneration(true);
|
|
messages = [];
|
|
try { sessionStorage.removeItem(STORAGE_KEY); } catch (e) { }
|
|
welcome.style.display = '';
|
|
countdownEl.textContent = formatCountdown(SESSION_TIMEOUT_MS);
|
|
countdownEl.classList.remove('session-warn');
|
|
addSystemPill('Your session timed out after 10 minutes of inactivity — this chat was cleared. Say hello to start a new one.');
|
|
chatScroll.scrollTop = chatScroll.scrollHeight;
|
|
}
|
|
|
|
setInterval(() => {
|
|
const remaining = SESSION_TIMEOUT_MS - (Date.now() - lastActivity);
|
|
if (remaining <= 0 && !expired) {
|
|
expireSession();
|
|
return;
|
|
}
|
|
if (!expired) {
|
|
countdownEl.textContent = formatCountdown(remaining);
|
|
countdownEl.classList.toggle('session-warn', remaining < 60000);
|
|
}
|
|
}, 1000);
|
|
|
|
// ============================================
|
|
// CHAT RENDERING
|
|
// ============================================
|
|
function nearBottom() {
|
|
return chatScroll.scrollHeight - chatScroll.scrollTop - chatScroll.clientHeight < 120;
|
|
}
|
|
|
|
function addSystemPill(text) {
|
|
const pill = document.createElement('div');
|
|
pill.className = 'fade-in mx-auto w-fit max-w-full text-center text-slate-400 text-sm bg-slate-900 border border-slate-800 rounded-3xl px-6 py-3 my-4';
|
|
pill.textContent = text;
|
|
chatLog.appendChild(pill);
|
|
}
|
|
|
|
function addErrorPill(text) {
|
|
const pill = document.createElement('div');
|
|
pill.className = 'fade-in mx-auto w-fit max-w-full text-center text-red-300 text-sm bg-red-500/10 border border-red-400/30 rounded-3xl px-6 py-3 my-4';
|
|
pill.textContent = text;
|
|
chatLog.appendChild(pill);
|
|
}
|
|
|
|
function avatarEl(kind) {
|
|
const wrap = document.createElement('div');
|
|
if (kind === 'assistant') {
|
|
wrap.className = 'flex-shrink-0 h-9 w-9 rounded-2xl bg-emerald-400/10 border border-emerald-400/30 text-emerald-400 flex items-center justify-center font-semibold';
|
|
wrap.style.fontFamily = "'Space Grotesk', sans-serif";
|
|
wrap.textContent = 'M';
|
|
} else {
|
|
wrap.className = 'flex-shrink-0 h-9 w-9 rounded-2xl bg-slate-800 border border-slate-700 text-slate-300 flex items-center justify-center text-sm font-medium';
|
|
wrap.textContent = 'You';
|
|
}
|
|
return wrap;
|
|
}
|
|
|
|
function addMessageRow(role) {
|
|
const row = document.createElement('div');
|
|
row.className = 'fade-in flex gap-3 mb-6 ' + (role === 'user' ? 'flex-row-reverse' : '');
|
|
row.appendChild(avatarEl(role));
|
|
const bubble = document.createElement('div');
|
|
bubble.className = role === 'user'
|
|
? 'chat-bubble max-w-[85%] sm:max-w-[75%] bg-emerald-400/10 border border-emerald-400/30 text-slate-100 rounded-3xl px-5 py-3.5 whitespace-pre-wrap break-words'
|
|
: 'chat-bubble max-w-[90%] sm:max-w-[80%] bg-slate-900 border border-slate-800 text-slate-200 rounded-3xl px-5 py-4 break-words';
|
|
row.appendChild(bubble);
|
|
chatLog.appendChild(row);
|
|
return bubble;
|
|
}
|
|
|
|
function hideWelcome() {
|
|
welcome.style.display = 'none';
|
|
}
|
|
|
|
// ============================================
|
|
// SENDING / STREAMING
|
|
// ============================================
|
|
function setGenerating(on) {
|
|
generating = on;
|
|
sendBtn.hidden = on;
|
|
stopBtn.hidden = !on;
|
|
input.disabled = on;
|
|
if (!on) input.focus();
|
|
}
|
|
|
|
function stopGeneration(silent) {
|
|
if (controller) { controller.abort(); controller = null; }
|
|
if (generating) {
|
|
setGenerating(false);
|
|
if (!silent) addSystemPill('Generation stopped.');
|
|
}
|
|
}
|
|
|
|
async function send(text) {
|
|
const prompt = (text || '').trim();
|
|
if (!prompt || generating) return;
|
|
if (expired) newChat(); // typing after a timeout seamlessly starts a fresh session
|
|
|
|
hideWelcome();
|
|
|
|
// User bubble
|
|
const userBubble = addMessageRow('user');
|
|
userBubble.textContent = prompt;
|
|
messages.push({ role: 'user', content: prompt });
|
|
touchSession(); // persist the user message immediately, not only after the reply
|
|
input.value = '';
|
|
autosize();
|
|
chatScroll.scrollTop = chatScroll.scrollHeight;
|
|
|
|
// Assistant placeholder with typing dots
|
|
const bubble = addMessageRow('assistant');
|
|
bubble.innerHTML = '<div class="flex items-center gap-1.5 py-1"><span class="typing-dot"></span><span class="typing-dot"></span><span class="typing-dot"></span></div>';
|
|
chatScroll.scrollTop = chatScroll.scrollHeight;
|
|
|
|
const payloadMessages = [{ role: 'system', content: SYSTEM_PROMPT }, ...messages];
|
|
controller = new AbortController();
|
|
setGenerating(true);
|
|
|
|
let raw = '';
|
|
let paintQueued = false;
|
|
const paint = () => {
|
|
if (paintQueued) return;
|
|
paintQueued = true;
|
|
requestAnimationFrame(() => {
|
|
paintQueued = false;
|
|
bubble.classList.add('md-body');
|
|
bubble.innerHTML = renderMarkdown(raw);
|
|
if (nearBottom()) chatScroll.scrollTop = chatScroll.scrollHeight;
|
|
});
|
|
};
|
|
|
|
try {
|
|
const res = await fetch(OLLAMA_BASE_URL + '/api/chat', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ model: CLOUD_MODEL, messages: payloadMessages, stream: true }),
|
|
signal: controller.signal
|
|
});
|
|
|
|
if (!res.ok) {
|
|
let detail = '';
|
|
try {
|
|
const j = await res.json();
|
|
if (j && j.error) detail = ' — ' + j.error;
|
|
} catch (e) { /* body was not JSON */ }
|
|
throw new Error('The endpoint responded with ' + res.status + detail);
|
|
}
|
|
|
|
const reader = res.body.getReader();
|
|
const decoder = new TextDecoder();
|
|
let buf = '';
|
|
let sawThinking = false;
|
|
while (true) {
|
|
const { done, value } = await reader.read();
|
|
if (done) break;
|
|
buf += decoder.decode(value, { stream: true });
|
|
const lines = buf.split('\n');
|
|
buf = lines.pop();
|
|
for (const line of lines) {
|
|
if (!line.trim()) continue;
|
|
let chunk;
|
|
try { chunk = JSON.parse(line); } catch (e) { continue; }
|
|
if (chunk.error) throw new Error(chunk.error);
|
|
if (chunk.message && chunk.message.content) {
|
|
raw += chunk.message.content;
|
|
paint();
|
|
} else if (chunk.message && chunk.message.thinking && !raw && !sawThinking) {
|
|
// Reasoning model: show a live indicator while only thinking tokens arrive
|
|
sawThinking = true;
|
|
bubble.innerHTML = '<div class="flex items-center gap-2 py-1 text-slate-500 text-sm"><span class="typing-dot"></span><span class="typing-dot"></span><span class="typing-dot"></span><span>Thinking…</span></div>';
|
|
}
|
|
}
|
|
}
|
|
if (!raw) throw new Error('The endpoint returned an empty response.');
|
|
} catch (err) {
|
|
if (err.name === 'AbortError') {
|
|
if (raw) { paint(); messages.push({ role: 'assistant', content: raw }); }
|
|
} else {
|
|
bubble.remove();
|
|
let hint = '';
|
|
if (err instanceof TypeError) {
|
|
hint = ' Could not reach ' + OLLAMA_BASE_URL + ' — the endpoint may be offline, or it needs CORS ' +
|
|
'enabled for this site (e.g. OLLAMA_ORIGINS).';
|
|
}
|
|
addErrorPill('Moxie Cloud hit a problem: ' + err.message + hint);
|
|
}
|
|
} finally {
|
|
controller = null;
|
|
if (generating) setGenerating(false);
|
|
if (raw && !messages.some(m => m.role === 'assistant' && m.content === raw)) {
|
|
messages.push({ role: 'assistant', content: raw });
|
|
}
|
|
touchSession(); // completing a response counts as activity
|
|
chatScroll.scrollTop = chatScroll.scrollHeight;
|
|
}
|
|
}
|
|
|
|
// ============================================
|
|
// COMPOSER
|
|
// ============================================
|
|
function autosize() {
|
|
input.style.height = 'auto';
|
|
input.style.height = Math.min(input.scrollHeight, 160) + 'px';
|
|
}
|
|
|
|
input.addEventListener('input', autosize);
|
|
input.addEventListener('keydown', (e) => {
|
|
if (e.key === 'Enter' && !e.shiftKey) {
|
|
e.preventDefault();
|
|
send(input.value);
|
|
}
|
|
});
|
|
composer.addEventListener('submit', (e) => {
|
|
e.preventDefault();
|
|
send(input.value);
|
|
});
|
|
|
|
function sendSuggestion(text) { send(text); }
|
|
|
|
function newChat() {
|
|
stopGeneration(true);
|
|
messages = [];
|
|
expired = false;
|
|
// Drop everything except the welcome screen
|
|
Array.from(chatLog.children).forEach(el => {
|
|
if (el.id !== 'welcome') el.remove();
|
|
});
|
|
welcome.style.display = '';
|
|
addSystemPill('New session started — the previous conversation was cleared.');
|
|
touchSession();
|
|
input.focus();
|
|
}
|
|
|
|
// ============================================
|
|
// INIT (restore recent session if still alive)
|
|
// ============================================
|
|
(function init() {
|
|
let restored = false;
|
|
try {
|
|
const saved = JSON.parse(sessionStorage.getItem(STORAGE_KEY) || 'null');
|
|
if (saved && Array.isArray(saved.messages) && saved.messages.length &&
|
|
Date.now() - saved.lastActivity < SESSION_TIMEOUT_MS) {
|
|
messages = saved.messages;
|
|
lastActivity = saved.lastActivity;
|
|
restored = true;
|
|
hideWelcome();
|
|
for (const m of messages) {
|
|
if (m.role === 'user') {
|
|
const b = addMessageRow('user');
|
|
b.textContent = m.content;
|
|
} else if (m.role === 'assistant') {
|
|
const b = addMessageRow('assistant');
|
|
b.classList.add('md-body');
|
|
b.innerHTML = renderMarkdown(m.content);
|
|
}
|
|
}
|
|
addSystemPill('Welcome back — your previous chat was restored before the 10-minute timeout.');
|
|
}
|
|
} catch (e) { /* corrupted storage — start fresh */ }
|
|
if (!restored) { lastActivity = Date.now(); touchSession(); }
|
|
input.focus();
|
|
})();
|
|
</script>
|
|
</body>
|
|
|
|
</html>
|