Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable Previous Messages Deletion #106

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion client/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -812,4 +812,10 @@ a:-webkit-any-link {
--clr-card-bg: hsl(209 50% 5%);
--colour-3: hsl(209 50% 90%);
--conversations: hsl(209 50% 80%);
}
}

.trash-icon {
position: absolute;
top: 20px;
right: 20px;
}
40 changes: 33 additions & 7 deletions client/js/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ const ask_gpt = async (message) => {
<div class="content" id="user_${token}">
${format(message)}
</div>
<i class="fa fa-trash trash-icon" onclick="deleteMessage('${token}')"></i>
</div>
`;

Expand Down Expand Up @@ -187,8 +188,8 @@ const ask_gpt = async (message) => {
"An error occured, please reload / refresh cache and try again.";
}

add_message(window.conversation_id, "user", message);
add_message(window.conversation_id, "assistant", text);
add_message(window.conversation_id, "user", message, token);
add_message(window.conversation_id, "assistant", text, token);

message_box.scrollTop = message_box.scrollHeight;
await remove_cancel_button();
Expand All @@ -197,7 +198,7 @@ const ask_gpt = async (message) => {
await load_conversations(20, 0);
window.scrollTo(0, 0);
} catch (e) {
add_message(window.conversation_id, "user", message);
add_message(window.conversation_id, "user", message, token);

message_box.scrollTop = message_box.scrollHeight;
await remove_cancel_button();
Expand All @@ -214,10 +215,10 @@ const ask_gpt = async (message) => {
let error_message = `oops ! something went wrong, please try again / reload. [stacktrace in console]`;

document.getElementById(`gpt_${window.token}`).innerHTML = error_message;
add_message(window.conversation_id, "assistant", error_message);
add_message(window.conversation_id, "assistant", error_message, token);
} else {
document.getElementById(`gpt_${window.token}`).innerHTML += ` [aborted]`;
add_message(window.conversation_id, "assistant", text + ` [aborted]`);
add_message(window.conversation_id, "assistant", text + ` [aborted]`, token);
}

window.scrollTo(0, 0);
Expand Down Expand Up @@ -316,13 +317,22 @@ const load_conversation = async (conversation_id) => {
: `<i class="fa-regular fa-phone-arrow-up-right"></i>`
}
</div>
<div class="content">
${
item.role == "user"
? `<div class="content" id="user_${item.token}">`
: `<div class="content" id="gpt_${item.token}">`
}
${
item.role == "assistant"
? markdown.render(item.content)
: item.content
}
</div>
${
item.role == "user"
? `<i class="fa fa-trash trash-icon" onclick="deleteMessage('${item.token}')"></i>`
: ''
}
</div>
`;
}
Expand Down Expand Up @@ -358,14 +368,15 @@ const add_conversation = async (conversation_id, title) => {
}
};

const add_message = async (conversation_id, role, content) => {
const add_message = async (conversation_id, role, content, token) => {
before_adding = JSON.parse(
localStorage.getItem(`conversation:${conversation_id}`)
);

before_adding.items.push({
role: role,
content: content,
token: token,
});

localStorage.setItem(
Expand Down Expand Up @@ -564,4 +575,19 @@ colorThemes.forEach((themeOption) => {
});
});

function deleteMessage(token) {
const messageDivUser = document.getElementById(`user_${token}`)
const messageDivGpt = document.getElementById(`gpt_${token}`)
if (messageDivUser) messageDivUser.parentNode.remove();
if (messageDivGpt) messageDivGpt.parentNode.remove();
const conversation = JSON.parse(localStorage.getItem(`conversation:${window.conversation_id}`));
conversation.items = conversation.items.filter(item => item.token !== token);
localStorage.setItem(`conversation:${window.conversation_id}`, JSON.stringify(conversation));

const messages = document.getElementsByClassName("message");
if (messages.length === 0) {
delete_conversation(window.conversation_id);
};
}

document.onload = setTheme();