lollms-webui/static/js/discussions.js

248 lines
9.1 KiB
JavaScript
Raw Normal View History

function load_discussion(discussion=0){
if(discussion)
2023-04-13 19:40:46 +00:00
{
console.log(discussion.id)
body = { id: discussion.id }
2023-04-13 19:40:46 +00:00
}
else{
body = { }
2023-04-13 19:40:46 +00:00
}
// send query with discussion id to reveal discussion messages
fetch('/load_discussion', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(body)
})
.then(response => {
if (response.ok) {
response.text().then(data => {
const messages = JSON.parse(data);
console.log(messages)
// process messages
var container = document.getElementById('chat-window');
container.innerHTML = '';
messages.forEach(message => {
2023-04-13 19:40:46 +00:00
console.log(`Adding message ${message.type}`)
if(message.type==0){
2023-04-13 19:40:46 +00:00
console.log("Showing message")
addMessage(message.sender, message.content, message.id, message.rank, true);
}
});
});
} else {
alert('Failed to query the discussion');
}
})
.catch(error => {
console.error('Failed to get messages:', error);
alert('Failed to get messages');
});
}
function populate_discussions_list()
{
// Populate discussions list
const discussionsList = document.querySelector('#discussions-list');
discussionsList.innerHTML = "";
2023-04-10 08:27:25 +00:00
fetch('/list_discussions')
.then(response => response.json())
.then(discussions => {
discussions.forEach(discussion => {
const buttonWrapper = document.createElement('div');
//buttonWrapper.classList.add('flex', 'space-x-2', 'mt-2');
2023-04-11 22:10:36 +00:00
buttonWrapper.classList.add('flex', 'items-center', 'mt-2', 'px-2', 'py-1', 'text-left');
const renameButton = document.createElement('button');
2023-04-13 10:31:48 +00:00
renameButton.classList.add('bg-green-500', 'hover:bg-green-700', 'text-white', 'font-bold', 'py-0', 'px-0', 'rounded',"w-10","h-10");
const renameImg = document.createElement('img');
renameImg.src = "/static/images/edit_discussion.png";
2023-04-15 13:57:23 +00:00
renameButton.title = "Rename discussion";
2023-04-11 22:10:36 +00:00
renameImg.classList.add('py-2', 'px-2', 'rounded', 'w-15', 'h-15');
renameButton.appendChild(renameImg);
//renameButton.style.backgroundImage = "/rename_discussion.svg"; //.textContent = 'Rename';
renameButton.addEventListener('click', () => {
const dialog = document.createElement('dialog');
2023-04-14 22:08:49 +00:00
dialog.classList.add('bg-gray-500', 'text-white', 'rounded', 'p-4');
const inputLabel = document.createElement('label');
inputLabel.textContent = 'New name: ';
const inputField = document.createElement('input');
2023-04-14 22:08:49 +00:00
inputField.classList.add('border', "px-2", "mx-2", 'bg-gray-800', 'border-gray-400', 'rounded', 'py-1', 'px-2');
inputField.setAttribute('type', 'text');
inputField.setAttribute('name', 'title');
inputField.setAttribute('value', discussion.title);
inputLabel.appendChild(inputField);
dialog.appendChild(inputLabel);
const cancelButton = document.createElement('button');
cancelButton.textContent = 'Cancel';
cancelButton.addEventListener('click', () => {
dialog.close();
});
const renameConfirmButton = document.createElement('button');
renameConfirmButton.classList.add('bg-green-500', 'hover:bg-green-700', 'text-white', 'font-bold', 'py-2', 'px-4', 'rounded', 'ml-2');
renameConfirmButton.textContent = 'Rename';
renameConfirmButton.addEventListener('click', () => {
const newTitle = inputField.value;
if (newTitle === '') {
alert('New name cannot be empty');
} else {
fetch('/rename', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ id: discussion.id, title: newTitle })
})
.then(response => {
if (response.ok) {
discussion.title = newTitle;
discussionButton.textContent = newTitle;
dialog.close();
} else {
alert('Failed to rename discussion');
}
})
.catch(error => {
console.error('Failed to rename discussion:', error);
alert('Failed to rename discussion');
});
}
});
dialog.appendChild(cancelButton);
dialog.appendChild(renameConfirmButton);
document.body.appendChild(dialog);
dialog.showModal();
});
const deleteButton = document.createElement('button');
2023-04-13 10:31:48 +00:00
deleteButton.classList.add('bg-green-500', 'hover:bg-green-700', 'text-white', 'font-bold', 'py-0', 'px-0', 'rounded',"w-10","h-10");
const deleteImg = document.createElement('img');
deleteImg.src = "/static/images/delete_discussion.png";
2023-04-15 13:57:23 +00:00
deleteButton.title = "Delete discussion";
2023-04-11 22:10:36 +00:00
deleteImg.classList.add('py-2', 'px-2', 'rounded', 'w-15', 'h-15');
deleteButton.addEventListener('click', () => {
fetch('/delete_discussion', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ id: discussion.id})
})
.then(response => {
if (response.ok) {
buttonWrapper.remove();
} else {
alert('Failed to delete discussion');
}
})
.catch(error => {
console.error('Failed to delete discussion:', error);
alert('Failed to delete discussion');
});
});
deleteButton.appendChild(deleteImg);
deleteButton.addEventListener('click', () => {
});
const discussionButton = document.createElement('button');
2023-04-13 10:31:48 +00:00
discussionButton.classList.add('bg-green-500', 'hover:bg-green-700', 'text-white', 'font-bold', 'py-2', 'px-4', 'rounded', 'ml-2', 'w-full');
discussionButton.textContent = discussion.title;
2023-04-15 13:57:23 +00:00
discussionButton.title = "Open discussion";
discussionButton.addEventListener('click', () => {
console.log(`Showing messages for discussion ${discussion.id}`);
2023-04-13 19:40:46 +00:00
load_discussion(discussion);
});
buttonWrapper.appendChild(renameButton);
buttonWrapper.appendChild(deleteButton);
buttonWrapper.appendChild(discussionButton);
discussionsList.appendChild(buttonWrapper);
});
})
.catch(error => {
console.error('Failed to get discussions:', error);
alert('Failed to get discussions');
});
}
2023-04-13 23:16:49 +00:00
function populate_menu(){
// adding export discussion button
const exportDiscussionButton = document.querySelector('#export-discussion-button');
2023-04-15 13:57:23 +00:00
exportDiscussionButton.title = "Export discussion to a file";
2023-04-13 23:16:49 +00:00
exportDiscussionButton.addEventListener('click', () => {
fetch(`/export_discussion`)
.then(response => response.text())
.then(data => {
2023-04-13 23:16:49 +00:00
const filename = window.prompt('Please enter a filename:', 'discussion.txt');
if (filename !== null) {
const text = data.replace(/\n/g, "\r\n");
const blob = new Blob([text], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = filename;
link.click();
URL.revokeObjectURL(url);
}
}).catch(function(error){
});
});
const actionBtns = document.querySelector('#action-buttons');
actionBtns.appendChild(exportDiscussionButton);
2023-04-13 23:16:49 +00:00
const newDiscussionBtn = document.querySelector('#new-discussion-btn');
2023-04-15 13:57:23 +00:00
newDiscussionBtn.title = "Create new discussion";
2023-04-13 23:16:49 +00:00
const resetDBButton = document.querySelector('#reset-discussions-btn');
2023-04-15 13:57:23 +00:00
resetDBButton.title = "Reset all discussions/database";
2023-04-13 23:16:49 +00:00
resetDBButton.addEventListener('click', () => {
});
newDiscussionBtn.addEventListener('click', () => {
const chatWindow = document.getElementById('chat-window');
const discussionName = prompt('Enter a name for the new discussion:');
if (discussionName) {
const sendbtn = document.querySelector("#submit-input")
const waitAnimation = document.querySelector("#wait-animation")
sendbtn.style.display="none";
waitAnimation.style.display="block";
// Add the discussion to the discussion list
const discussionItem = document.createElement('li');
discussionItem.textContent = discussionName;
fetch(`/new_discussion?title=${discussionName}`)
.then(response => response.json())
.then(data => {
console.log(`New chat ${data.welcome_message}`)
// Select the new discussion
//selectDiscussion(discussionId);
chatWindow.innerHTML=""
addMessage("GPT4ALL", data.welcome_message,0);
populate_discussions_list()
sendbtn.style.display="block";
waitAnimation.style.display="none";
})
.catch(error => {
// Handle any errors that occur
console.error(error);
});
}
});
}