File size: 4,374 Bytes
e39cf8d | 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 | /* static/script.js */
function chatApp() {
return {
// ------------------------------
// State
// ------------------------------
messages: [], // {id, role, content}
userInput: '',
loading: false,
dark: false, // dark‑mode flag
// ------------------------------
// Init – runs once when component mounts
// ------------------------------
init() {
// Detect system dark‑mode preference
this.dark = window.matchMedia('(prefers-color-scheme: dark)').matches;
this.applyDarkMode();
// Listen for changes (e.g., user switches OS theme)
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', e => {
this.dark = e.matches;
this.applyDarkMode();
});
},
// ------------------------------
// Dark‑mode helper
// ------------------------------
toggleDark() {
this.dark = !this.dark;
this.applyDarkMode();
},
applyDarkMode() {
if (this.dark) {
document.documentElement.classList.add('dark');
} else {
document.documentElement.classList.remove('dark');
}
},
// ------------------------------
// Auto‑grow textarea
// ------------------------------
autoResize(event) {
const el = event.target;
el.style.height = 'auto';
el.style.height = `${el.scrollHeight}px`;
},
// ------------------------------
// Send a message to the back‑end
// ------------------------------
async sendMessage() {
const content = this.userInput.trim();
if (!content) return;
// 1️⃣ Add user bubble
const userMsg = {
id: Date.now(),
role: 'user',
content: this.escapeHtml(content).replace(/\n/g, '<br>')
};
this.messages.push(userMsg);
this.userInput = '';
this.scrollToBottom();
// 2️⃣ Show loading indicator
this.loading = true;
// 3️⃣ Call the OpenAI‑compatible endpoint
try {
const response = await fetch('/v1/chat/completions', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
model: 'qwen2.5-1.5b-instruct',
messages: [
// Keep the whole conversation – the model can handle a few turns
...this.messages.map(m => ({
role: m.role,
content: this.stripHtml(m.content) // plain text for the model
}))
],
temperature: 0.7,
max_tokens: 512,
top_p: 0.9,
stream: false // set true later if you implement SSE streaming
})
});
if (!response.ok) {
const err = await response.json();
throw new Error(err.detail || 'API error');
}
const data = await response.json();
const assistant = data.choices[0].message.content;
// 4️⃣ Add assistant bubble
const assistantMsg = {
id: Date.now() + 1,
role: 'assistant',
content: this.escapeHtml(assistant).replace(/\n/g, '<br>')
};
this.messages.push(assistantMsg);
this.scrollToBottom();
} catch (e) {
console.error(e);
const errMsg = {
id: Date.now() + 1,
role: 'assistant',
content: `<span class="text-red-500">⚠️ ${this.escapeHtml(e.message)}</span>`
};
this.messages.push(errMsg);
this.scrollToBottom();
} finally {
this.loading = false;
}
},
// ------------------------------
// Helpers
// ------------------------------
scrollToBottom() {
this.$nextTick(() => {
const chat = document.getElementById('chat-window');
chat.scrollTop = chat.scrollHeight;
});
},
// Escape HTML to avoid XSS when we later render raw strings
escapeHtml(str) {
const div = document.createElement('div');
div.appendChild(document.createTextNode(str));
return div.innerHTML;
},
// Strip HTML tags – used when sending the prompt to the model
stripHtml(html) {
const div = document.createElement('div');
div.innerHTML = html;
return div.textContent || div.innerText || '';
}
};
}
|