This commit is contained in:
Timothy J. Baek
2024-11-06 02:58:44 -08:00
parent ccbf5a08f3
commit 02269a21a9
6 changed files with 163 additions and 252 deletions
+33 -31
View File
@@ -29,8 +29,10 @@
export let continueResponse: Function;
export let regenerateResponse: Function;
export let mergeResponses: Function;
export let chatActionHandler: Function;
export let showMessage: Function = () => {};
export let submitMessage: Function = () => {};
export let readOnly = false;
@@ -79,9 +81,9 @@
element.scrollTop = element.scrollHeight;
};
const updateChatHistory = async () => {
await tick();
const updateChat = async () => {
history = history;
await tick();
await updateChatById(localStorage.token, chatId, {
history: history,
messages: messages
@@ -195,7 +197,7 @@
rating: rating
};
await updateChatHistory();
await updateChat();
};
const editMessage = async (messageId, content, submit = true) => {
@@ -232,7 +234,7 @@
} else {
// Edit user message
history.messages[messageId].content = content;
await updateChatHistory();
await updateChat();
}
} else {
if (submit) {
@@ -261,16 +263,25 @@
];
}
await updateChatHistory();
await updateChat();
} else {
// Edit response message
history.messages[messageId].originalContent = history.messages[messageId].content;
history.messages[messageId].content = content;
await updateChatHistory();
await updateChat();
}
}
};
const actionMessage = async (actionId, event = null) => {
await chatActionHandler(chatId, actionId, message.model, message.id, event);
};
const saveMessage = async (messageId, message) => {
history.messages[messageId] = message;
await updateChat();
};
const deleteMessage = async (messageId) => {
const messageToDelete = history.messages[messageId];
const parentMessageId = messageToDelete.parentId;
@@ -306,7 +317,17 @@
showMessage({ id: parentMessageId });
// Update the chat
await updateChatHistory();
await updateChat();
};
const triggerScroll = () => {
if (autoScroll) {
const element = document.getElementById('messages-container');
autoScroll = element.scrollHeight - element.scrollTop <= element.clientHeight + 50;
setTimeout(() => {
scrollToBottom();
}, 100);
}
};
</script>
@@ -372,37 +393,18 @@
{user}
{showPreviousMessage}
{showNextMessage}
{updateChat}
{editMessage}
{deleteMessage}
{rateMessage}
{actionMessage}
{saveMessage}
{submitMessage}
{regenerateResponse}
{continueResponse}
{mergeResponses}
{triggerScroll}
{readOnly}
on:submit={async (e) => {
dispatch('submit', e.detail);
}}
on:action={async (e) => {
if (typeof e.detail === 'string') {
await chatActionHandler(chatId, e.detail, message.model, message.id);
} else {
const { id, event } = e.detail;
await chatActionHandler(chatId, id, message.model, message.id, event);
}
}}
on:update={() => {
updateChatHistory();
}}
on:scroll={() => {
if (autoScroll) {
const element = document.getElementById('messages-container');
autoScroll =
element.scrollHeight - element.scrollTop <= element.clientHeight + 50;
setTimeout(() => {
scrollToBottom();
}, 100);
}
}}
/>
{/each}
</div>