wordpress-ai-plugin/interweb-mz-ai/assets/chat.js
2026-12-01 13:26:00 +02:00

139 lines
4.9 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* Interweb Mozambique All in One AI: floating AI assistant. Talks only to this WordPress; the API key stays on the server. */
(function () {
'use strict';
var cfg = window.interwebMzAiChat;
if (!cfg || !document.body) { return; }
var history = [];
var busy = false;
var STORE = 'interwebMzAiChatHistory';
try {
var saved = JSON.parse(window.sessionStorage.getItem(STORE) || '[]');
if (Array.isArray(saved)) { history = saved.slice(-10); }
} catch (e) { history = []; }
function el(tag, cls, text) {
var n = document.createElement(tag);
if (cls) { n.className = cls; }
if (text) { n.textContent = text; }
return n;
}
var root = el('div', 'interweb-mz-ai-chat interweb-mz-ai-chat--' + cfg.position);
var toggle = el('button', 'interweb-mz-ai-chat__toggle');
toggle.type = 'button';
toggle.setAttribute('aria-expanded', 'false');
toggle.setAttribute('aria-label', cfg.open);
toggle.innerHTML = '<svg viewBox="0 0 24 24" width="26" height="26" aria-hidden="true" focusable="false"><path fill="currentColor" d="M4 4h16a2 2 0 0 1 2 2v10a2 2 0 0 1-2 2H9l-5 4v-4H4a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2zm3 6.5a1.5 1.5 0 1 0 0 .01zm5 0a1.5 1.5 0 1 0 0 .01zm5 0a1.5 1.5 0 1 0 0 .01z"/></svg>';
var panel = el('div', 'interweb-mz-ai-chat__panel');
panel.id = 'interweb-mz-ai-chat-panel';
panel.hidden = true;
panel.setAttribute('role', 'dialog');
panel.setAttribute('aria-label', cfg.title);
toggle.setAttribute('aria-controls', panel.id);
var head = el('div', 'interweb-mz-ai-chat__head');
head.appendChild(el('span', 'interweb-mz-ai-chat__title', cfg.title));
var close = el('button', 'interweb-mz-ai-chat__close');
close.type = 'button';
close.setAttribute('aria-label', cfg.close);
close.textContent = '×';
head.appendChild(close);
var list = el('div', 'interweb-mz-ai-chat__list');
list.setAttribute('aria-live', 'polite');
var form = el('form', 'interweb-mz-ai-chat__form');
var input = el('textarea', 'interweb-mz-ai-chat__input');
input.rows = 1;
input.maxLength = cfg.maxChars;
input.placeholder = cfg.placeholder;
input.setAttribute('aria-label', cfg.placeholder);
var send = el('button', 'interweb-mz-ai-chat__send', cfg.send);
send.type = 'submit';
form.appendChild(input);
form.appendChild(send);
panel.appendChild(head);
panel.appendChild(list);
panel.appendChild(form);
panel.appendChild(el('p', 'interweb-mz-ai-chat__note', cfg.note));
root.appendChild(panel);
root.appendChild(toggle);
document.body.appendChild(root);
function bubble(role, text) {
var b = el('div', 'interweb-mz-ai-chat__msg interweb-mz-ai-chat__msg--' + role, text);
list.appendChild(b);
list.scrollTop = list.scrollHeight;
return b;
}
function render() {
list.textContent = '';
bubble('assistant', cfg.welcome);
history.forEach(function (m) { bubble(m.role, m.content); });
}
function save() {
try { window.sessionStorage.setItem(STORE, JSON.stringify(history.slice(-10))); } catch (e) { /* private mode */ }
}
function openPanel(open) {
panel.hidden = !open;
toggle.setAttribute('aria-expanded', open ? 'true' : 'false');
toggle.setAttribute('aria-label', open ? cfg.close : cfg.open);
root.classList.toggle('is-open', open);
if (open) { render(); input.focus(); }
}
toggle.addEventListener('click', function () { openPanel(panel.hidden); });
close.addEventListener('click', function () { openPanel(false); toggle.focus(); });
root.addEventListener('keydown', function (e) {
if (e.key === 'Escape' && !panel.hidden) { openPanel(false); toggle.focus(); }
});
input.addEventListener('keydown', function (e) {
if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); form.requestSubmit ? form.requestSubmit() : form.dispatchEvent(new Event('submit')); }
});
form.addEventListener('submit', function (e) {
e.preventDefault();
var text = input.value.trim();
if (!text || busy) { return; }
busy = true;
send.disabled = true;
input.value = '';
history.push({ role: 'user', content: text });
bubble('user', text);
var wait = bubble('assistant interweb-mz-ai-chat__msg--wait', cfg.thinking);
fetch(cfg.url, {
method: 'POST',
credentials: 'same-origin',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ lang: cfg.lang, messages: history.slice(-10) })
}).then(function (r) {
return r.json().catch(function () { return { ok: false }; });
}).then(function (data) {
wait.remove();
if (data && data.ok && data.answer) {
history.push({ role: 'assistant', content: data.answer });
bubble('assistant', data.answer);
} else {
history.pop();
bubble('error', (data && data.message) || cfg.error);
}
}).catch(function () {
wait.remove();
history.pop();
bubble('error', cfg.error);
}).then(function () {
busy = false;
send.disabled = false;
save();
input.focus();
});
});
})();