|
|
|
@ -1,9 +1,17 @@
|
|
|
|
|
var el = document.getElementById('message-form');
|
|
|
|
|
if (el) {
|
|
|
|
|
el.addEventListener("submit", function (event) {
|
|
|
|
|
console.log("submitting")
|
|
|
|
|
var form = document.getElementById('message-form');
|
|
|
|
|
var errorModal = document.getElementById('error-alert')
|
|
|
|
|
document.getElementById('close').addEventListener('click',()=>{
|
|
|
|
|
errorModal.classList.toggle('hidden')
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function submitForm(event){
|
|
|
|
|
event.preventDefault()
|
|
|
|
|
var message = document.getElementById("message-input").value;
|
|
|
|
|
console.log(message.length)
|
|
|
|
|
if(message.length === 0){
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
msg_html = '<div class="bg-blue-500 text-white p-2 rounded-lg mb-2 self-end"><p class="text-sm">'
|
|
|
|
|
msg_html += message
|
|
|
|
|
msg_html += '</p></div>'
|
|
|
|
@ -17,6 +25,7 @@ if (el) {
|
|
|
|
|
localStorage.setItem('activeDocs', 'default')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fetch('/api/answer', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
|
|
|
@ -28,28 +37,37 @@ if (el) {
|
|
|
|
|
embeddings_key: localStorage.getItem('apiKey'),
|
|
|
|
|
history: localStorage.getItem('chatHistory'),
|
|
|
|
|
active_docs: localStorage.getItem('activeDocs')}),
|
|
|
|
|
})
|
|
|
|
|
.then(response => response.json())
|
|
|
|
|
}).then((response)=> response.json())
|
|
|
|
|
.then(data => {
|
|
|
|
|
console.log('Success:', data);
|
|
|
|
|
if(data.error){
|
|
|
|
|
document.getElementById('text-error').textContent = `Error : ${JSON.stringify(data.message)}`
|
|
|
|
|
errorModal.classList.toggle('hidden')
|
|
|
|
|
}
|
|
|
|
|
if(data.answer){
|
|
|
|
|
msg_html = '<div class="bg-indigo-500 text-white p-2 rounded-lg mb-2 self-start"><code class="text-sm">'
|
|
|
|
|
msg_html += data.answer
|
|
|
|
|
msg_html += '</code></div>'
|
|
|
|
|
document.getElementById("messages").innerHTML += msg_html;
|
|
|
|
|
let chatWindow = document.getElementById("messages-container");
|
|
|
|
|
chatWindow.scrollTop = chatWindow.scrollHeight;
|
|
|
|
|
}
|
|
|
|
|
document.getElementById("button-submit").innerHTML = 'Send';
|
|
|
|
|
document.getElementById("button-submit").disabled = false;
|
|
|
|
|
let chatHistory = [message, data.answer];
|
|
|
|
|
let chatHistory = [message, data.answer || ''];
|
|
|
|
|
localStorage.setItem('chatHistory', JSON.stringify(chatHistory));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
console.error('Error:', error);
|
|
|
|
|
console.log(error);
|
|
|
|
|
document.getElementById("button-submit").innerHTML = 'Send';
|
|
|
|
|
document.getElementById("button-submit").disabled = false;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// console.log(error);
|
|
|
|
|
// document.getElementById("button-submit").innerHTML = 'Send';
|
|
|
|
|
// document.getElementById("button-submit").disabled = false;
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
window.addEventListener('submit',submitForm)
|
|
|
|
|