139 linhas
4,9 KiB
JavaScript
139 linhas
4,9 KiB
JavaScript
/* MozOut 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.mozoutAiChat;
|
||
if (!cfg || !document.body) { return; }
|
||
|
||
var history = [];
|
||
var busy = false;
|
||
var STORE = 'mozoutAiChatHistory';
|
||
|
||
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', 'mozout-ai-chat mozout-ai-chat--' + cfg.position);
|
||
var toggle = el('button', 'mozout-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', 'mozout-ai-chat__panel');
|
||
panel.id = 'mozout-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', 'mozout-ai-chat__head');
|
||
head.appendChild(el('span', 'mozout-ai-chat__title', cfg.title));
|
||
var close = el('button', 'mozout-ai-chat__close');
|
||
close.type = 'button';
|
||
close.setAttribute('aria-label', cfg.close);
|
||
close.textContent = '×';
|
||
head.appendChild(close);
|
||
|
||
var list = el('div', 'mozout-ai-chat__list');
|
||
list.setAttribute('aria-live', 'polite');
|
||
|
||
var form = el('form', 'mozout-ai-chat__form');
|
||
var input = el('textarea', 'mozout-ai-chat__input');
|
||
input.rows = 1;
|
||
input.maxLength = cfg.maxChars;
|
||
input.placeholder = cfg.placeholder;
|
||
input.setAttribute('aria-label', cfg.placeholder);
|
||
var send = el('button', 'mozout-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', 'mozout-ai-chat__note', cfg.note));
|
||
root.appendChild(panel);
|
||
root.appendChild(toggle);
|
||
document.body.appendChild(root);
|
||
|
||
function bubble(role, text) {
|
||
var b = el('div', 'mozout-ai-chat__msg mozout-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 mozout-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();
|
||
});
|
||
});
|
||
})();
|