';
if (type === 'ai') {
html += 'bigshift.bot:';
}
else if (type === 'system') {
html += 'System:';
}
else {
html += 'Du:';
}
html += '' + text + '';
html += '
';
conversation.innerHTML += html;
conversation.scrollTop = conversation.scrollHeight;
// Syntax coloring
if (typeof hljs !== 'undefined') {
document.querySelectorAll('pre code').forEach((el) => {
hljs.highlightElement(el);
});
}
}
// Function to request the completion
function mwai_63d2e30c57daf_onSendClick() {
let input = document.querySelector('#mwai-chat-63d2e30c57daf .mwai-input textarea');
let inputText = input.value.trim();
if (inputText === '') {
return;
}
// Disable the button
var button = document.querySelector('#mwai-chat-63d2e30c57daf .mwai-input button');
button.disabled = true;
// Add the user reply
mwai_63d2e30c57daf_addReply(inputText, 'user');
mwai_63d2e30c57daf_onGoingPrompt += 'Du:' + inputText + '\n';
input.value = '';
input.setAttribute('rows', 1);
input.disabled = true;
// Let's build the prompt depending on the "system"
mwai_63d2e30c57daf_onGoingPrompt += 'bigshift.bot:';
let prompt = mwai_63d2e30c57daf_onGoingPrompt;
if (0) {
prompt = inputText + '';
}
// Prompt for the images
const data = mode === 'images' ? {
env: 'chatbot',
session: '63d2e30c57e45',
prompt: inputText,
maxResults: 3,
model: 'text-davinci-003',
apiKey: '',
// Prompt for the chat
} : {
env: 'chatbot',
session: '63d2e30c57e45',
prompt: prompt,
userName: 'Du:',
aiName: 'bigshift.bot:',
model: 'text-davinci-003',
temperature: '0.8',
maxTokens: '1024',
stop: '',
maxResults: '3',
apiKey: '',
};
console.log('[BOT] Sent: ', data);
fetch('https://bigshift.life/wp-json/ai-engine/v1/chat', { method: 'POST', headers: {
'Content-Type': 'application/json',
'X-WP-Nonce': '940e2cab83'
},
body: JSON.stringify(data)
})
.then(response => response.json())
.then(data => {
console.log('[BOT] Recv: ', data);
if (!data.success) {
mwai_63d2e30c57daf_addReply(data.message, 'system');
}
else {
mwai_63d2e30c57daf_addReply(data.images ? data.images : data.html, 'ai');
mwai_63d2e30c57daf_onGoingPrompt += data.answer + '\n';
}
button.disabled = false;
input.disabled = false;
// Only focus only on desktop (to avoid the mobile keyboard to kick-in)
if (!isMobile) {
input.focus();
}
})
.catch(error => {
console.error(error);
button.disabled = false;
input.disabled = false;
});
}
// Keep the textarea height in sync with the content
function mwaiSetTextAreaHeight(textarea, lines) {
var rows = textarea.getAttribute('rows');
if (lines !== rows) {
textarea.setAttribute('rows', lines > 5 ? 5 : lines);
}
}
// Init the chatbot
function mwai_63d2e30c57daf_initChatBot() {
var input = document.querySelector('#mwai-chat-63d2e30c57daf .mwai-input textarea');
input.addEventListener('keypress', (event) => {
if (event.keyCode === 13 && !event.shiftKey) {
mwai_63d2e30c57daf_onSendClick();
}
});
input.addEventListener('keydown', (event) => {
var rows = input.getAttribute('rows');
if (event.keyCode === 13 && event.shiftKey) {
var lines = input.value.split('\n').length + 1;
mwaiSetTextAreaHeight(input, lines);
}
});
input.addEventListener('keyup', (event) => {
var rows = input.getAttribute('rows');
var lines = input.value.split('\n').length ;
mwaiSetTextAreaHeight(input, lines);
});
var button = document.querySelector('#mwai-chat-63d2e30c57daf .mwai-input button');
button.addEventListener('click', (event) => {
mwai_63d2e30c57daf_onSendClick();
});
// If window, add event listener to mwai-open-button and mwai-close-button
if ( isWindow ) {
var openButton = document.querySelector('#mwai-chat-63d2e30c57daf .mwai-open-button');
openButton.addEventListener('click', (event) => {
var chat = document.querySelector('#mwai-chat-63d2e30c57daf');
chat.classList.add('mwai-open');
// Only focus only on desktop (to avoid the mobile keyboard to kick-in)
if (!isMobile) {
input.focus();
}
});
var closeButton = document.querySelector('#mwai-chat-63d2e30c57daf .mwai-close-button');
closeButton.addEventListener('click', (event) => {
var chat = document.querySelector('#mwai-chat-63d2e30c57daf');
chat.classList.remove('mwai-open');
});
var resizeButton = document.querySelector('#mwai-chat-63d2e30c57daf .mwai-resize-button');
resizeButton.addEventListener('click', (event) => {
var chat = document.querySelector('#mwai-chat-63d2e30c57daf');
chat.classList.toggle('mwai-fullscreen');
});
}
mwai_63d2e30c57daf_addReply('Was kann ich Gutes für Dich tun?', 'ai');
}
// Let's go totally meoooow on this!
mwai_63d2e30c57daf_initChatBot();
})();