2023-11-20 02:47:07 +01:00
|
|
|
<script lang="ts">
|
|
|
|
import { v4 as uuidv4 } from 'uuid';
|
|
|
|
|
2024-01-02 01:05:05 +01:00
|
|
|
import { chats, config, modelfiles, settings, user } from '$lib/stores';
|
2023-11-20 02:47:07 +01:00
|
|
|
import { tick } from 'svelte';
|
|
|
|
|
|
|
|
import toast from 'svelte-french-toast';
|
2023-12-26 21:50:52 +01:00
|
|
|
import { getChatList, updateChatById } from '$lib/apis/chats';
|
2023-11-20 02:47:07 +01:00
|
|
|
|
2023-12-30 08:03:48 +01:00
|
|
|
import UserMessage from './Messages/UserMessage.svelte';
|
|
|
|
import ResponseMessage from './Messages/ResponseMessage.svelte';
|
|
|
|
import Placeholder from './Messages/Placeholder.svelte';
|
2024-01-07 12:19:34 +01:00
|
|
|
import Spinner from '../common/Spinner.svelte';
|
2024-02-21 01:00:57 +01:00
|
|
|
import { cancelChatCompletion } from '$lib/apis/ollama';
|
2023-12-30 08:03:48 +01:00
|
|
|
|
2023-12-26 12:28:30 +01:00
|
|
|
export let chatId = '';
|
2023-11-20 02:47:07 +01:00
|
|
|
export let sendPrompt: Function;
|
2024-02-15 10:23:02 +01:00
|
|
|
export let continueGeneration: Function;
|
2023-11-20 02:47:07 +01:00
|
|
|
export let regenerateResponse: Function;
|
|
|
|
|
2024-01-07 12:19:34 +01:00
|
|
|
export let processing = '';
|
2023-12-14 01:21:50 +01:00
|
|
|
export let bottomPadding = false;
|
2023-11-20 02:47:07 +01:00
|
|
|
export let autoScroll;
|
2023-12-03 20:54:11 +01:00
|
|
|
export let selectedModels;
|
2023-11-20 02:47:07 +01:00
|
|
|
export let history = {};
|
|
|
|
export let messages = [];
|
|
|
|
|
2023-12-30 08:03:48 +01:00
|
|
|
export let selectedModelfiles = [];
|
2023-11-20 02:47:07 +01:00
|
|
|
|
2023-12-14 01:21:50 +01:00
|
|
|
$: if (autoScroll && bottomPadding) {
|
|
|
|
(async () => {
|
|
|
|
await tick();
|
2024-02-16 01:20:46 +01:00
|
|
|
scrollToBottom();
|
2023-12-14 01:21:50 +01:00
|
|
|
})();
|
|
|
|
}
|
|
|
|
|
2024-02-16 01:20:46 +01:00
|
|
|
const scrollToBottom = () => {
|
|
|
|
const element = document.getElementById('messages-container');
|
|
|
|
element.scrollTop = element.scrollHeight;
|
|
|
|
};
|
|
|
|
|
2023-11-20 02:47:07 +01:00
|
|
|
const copyToClipboard = (text) => {
|
|
|
|
if (!navigator.clipboard) {
|
|
|
|
var textArea = document.createElement('textarea');
|
|
|
|
textArea.value = text;
|
|
|
|
|
|
|
|
// Avoid scrolling to bottom
|
|
|
|
textArea.style.top = '0';
|
|
|
|
textArea.style.left = '0';
|
|
|
|
textArea.style.position = 'fixed';
|
|
|
|
|
|
|
|
document.body.appendChild(textArea);
|
|
|
|
textArea.focus();
|
|
|
|
textArea.select();
|
|
|
|
|
|
|
|
try {
|
|
|
|
var successful = document.execCommand('copy');
|
|
|
|
var msg = successful ? 'successful' : 'unsuccessful';
|
|
|
|
console.log('Fallback: Copying text command was ' + msg);
|
|
|
|
} catch (err) {
|
|
|
|
console.error('Fallback: Oops, unable to copy', err);
|
|
|
|
}
|
|
|
|
|
|
|
|
document.body.removeChild(textArea);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
navigator.clipboard.writeText(text).then(
|
|
|
|
function () {
|
|
|
|
console.log('Async: Copying to clipboard was successful!');
|
|
|
|
toast.success('Copying to clipboard was successful!');
|
|
|
|
},
|
|
|
|
function (err) {
|
|
|
|
console.error('Async: Could not copy text: ', err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2023-12-30 08:03:48 +01:00
|
|
|
const confirmEditMessage = async (messageId, content) => {
|
|
|
|
let userPrompt = content;
|
2023-11-20 02:47:07 +01:00
|
|
|
let userMessageId = uuidv4();
|
|
|
|
|
|
|
|
let userMessage = {
|
|
|
|
id: userMessageId,
|
|
|
|
parentId: history.messages[messageId].parentId,
|
|
|
|
childrenIds: [],
|
|
|
|
role: 'user',
|
2023-12-14 01:21:50 +01:00
|
|
|
content: userPrompt,
|
|
|
|
...(history.messages[messageId].files && { files: history.messages[messageId].files })
|
2023-11-20 02:47:07 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
let messageParentId = history.messages[messageId].parentId;
|
|
|
|
|
|
|
|
if (messageParentId !== null) {
|
|
|
|
history.messages[messageParentId].childrenIds = [
|
|
|
|
...history.messages[messageParentId].childrenIds,
|
|
|
|
userMessageId
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
history.messages[userMessageId] = userMessage;
|
|
|
|
history.currentId = userMessageId;
|
|
|
|
|
|
|
|
await tick();
|
2023-12-26 12:28:30 +01:00
|
|
|
await sendPrompt(userPrompt, userMessageId, chatId);
|
2023-11-20 02:47:07 +01:00
|
|
|
};
|
|
|
|
|
2023-12-30 08:03:48 +01:00
|
|
|
const confirmEditResponseMessage = async (messageId, content) => {
|
2023-12-30 08:25:40 +01:00
|
|
|
history.messages[messageId].originalContent = history.messages[messageId].content;
|
2023-12-30 08:03:48 +01:00
|
|
|
history.messages[messageId].content = content;
|
2023-12-30 08:25:40 +01:00
|
|
|
|
|
|
|
await tick();
|
|
|
|
|
|
|
|
await updateChatById(localStorage.token, chatId, {
|
|
|
|
messages: messages,
|
|
|
|
history: history
|
|
|
|
});
|
|
|
|
|
|
|
|
await chats.set(await getChatList(localStorage.token));
|
2023-12-11 07:47:16 +01:00
|
|
|
};
|
|
|
|
|
2023-12-30 08:03:48 +01:00
|
|
|
const rateMessage = async (messageId, rating) => {
|
|
|
|
history.messages[messageId].rating = rating;
|
|
|
|
await tick();
|
2023-12-26 21:50:52 +01:00
|
|
|
await updateChatById(localStorage.token, chatId, {
|
2023-11-20 02:47:07 +01:00
|
|
|
messages: messages,
|
|
|
|
history: history
|
|
|
|
});
|
2023-12-26 21:50:52 +01:00
|
|
|
|
|
|
|
await chats.set(await getChatList(localStorage.token));
|
2023-11-20 02:47:07 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const showPreviousMessage = async (message) => {
|
|
|
|
if (message.parentId !== null) {
|
|
|
|
let messageId =
|
|
|
|
history.messages[message.parentId].childrenIds[
|
|
|
|
Math.max(history.messages[message.parentId].childrenIds.indexOf(message.id) - 1, 0)
|
|
|
|
];
|
|
|
|
|
|
|
|
if (message.id !== messageId) {
|
|
|
|
let messageChildrenIds = history.messages[messageId].childrenIds;
|
|
|
|
|
|
|
|
while (messageChildrenIds.length !== 0) {
|
|
|
|
messageId = messageChildrenIds.at(-1);
|
|
|
|
messageChildrenIds = history.messages[messageId].childrenIds;
|
|
|
|
}
|
|
|
|
|
|
|
|
history.currentId = messageId;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let childrenIds = Object.values(history.messages)
|
|
|
|
.filter((message) => message.parentId === null)
|
|
|
|
.map((message) => message.id);
|
|
|
|
let messageId = childrenIds[Math.max(childrenIds.indexOf(message.id) - 1, 0)];
|
|
|
|
|
|
|
|
if (message.id !== messageId) {
|
|
|
|
let messageChildrenIds = history.messages[messageId].childrenIds;
|
|
|
|
|
|
|
|
while (messageChildrenIds.length !== 0) {
|
|
|
|
messageId = messageChildrenIds.at(-1);
|
|
|
|
messageChildrenIds = history.messages[messageId].childrenIds;
|
|
|
|
}
|
|
|
|
|
|
|
|
history.currentId = messageId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
await tick();
|
|
|
|
|
2024-02-16 01:20:46 +01:00
|
|
|
const element = document.getElementById('messages-container');
|
2024-02-16 05:10:48 +01:00
|
|
|
autoScroll = element.scrollHeight - element.scrollTop <= element.clientHeight + 50;
|
2023-11-20 02:47:07 +01:00
|
|
|
|
|
|
|
setTimeout(() => {
|
2024-02-16 01:20:46 +01:00
|
|
|
scrollToBottom();
|
2023-11-20 02:47:07 +01:00
|
|
|
}, 100);
|
|
|
|
};
|
|
|
|
|
|
|
|
const showNextMessage = async (message) => {
|
|
|
|
if (message.parentId !== null) {
|
|
|
|
let messageId =
|
|
|
|
history.messages[message.parentId].childrenIds[
|
|
|
|
Math.min(
|
|
|
|
history.messages[message.parentId].childrenIds.indexOf(message.id) + 1,
|
|
|
|
history.messages[message.parentId].childrenIds.length - 1
|
|
|
|
)
|
|
|
|
];
|
|
|
|
|
|
|
|
if (message.id !== messageId) {
|
|
|
|
let messageChildrenIds = history.messages[messageId].childrenIds;
|
|
|
|
|
|
|
|
while (messageChildrenIds.length !== 0) {
|
|
|
|
messageId = messageChildrenIds.at(-1);
|
|
|
|
messageChildrenIds = history.messages[messageId].childrenIds;
|
|
|
|
}
|
|
|
|
|
|
|
|
history.currentId = messageId;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let childrenIds = Object.values(history.messages)
|
|
|
|
.filter((message) => message.parentId === null)
|
|
|
|
.map((message) => message.id);
|
|
|
|
let messageId =
|
|
|
|
childrenIds[Math.min(childrenIds.indexOf(message.id) + 1, childrenIds.length - 1)];
|
|
|
|
|
|
|
|
if (message.id !== messageId) {
|
|
|
|
let messageChildrenIds = history.messages[messageId].childrenIds;
|
|
|
|
|
|
|
|
while (messageChildrenIds.length !== 0) {
|
|
|
|
messageId = messageChildrenIds.at(-1);
|
|
|
|
messageChildrenIds = history.messages[messageId].childrenIds;
|
|
|
|
}
|
|
|
|
|
|
|
|
history.currentId = messageId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
await tick();
|
|
|
|
|
2024-02-16 01:20:46 +01:00
|
|
|
const element = document.getElementById('messages-container');
|
2024-02-16 05:10:48 +01:00
|
|
|
autoScroll = element.scrollHeight - element.scrollTop <= element.clientHeight + 50;
|
2024-02-16 01:20:46 +01:00
|
|
|
|
2023-11-20 02:47:07 +01:00
|
|
|
setTimeout(() => {
|
2024-02-16 01:20:46 +01:00
|
|
|
scrollToBottom();
|
2023-11-20 02:47:07 +01:00
|
|
|
}, 100);
|
|
|
|
};
|
2024-02-21 00:12:06 +01:00
|
|
|
|
2024-02-21 04:47:30 +01:00
|
|
|
const deleteMessageAndDescendants = async (messageId: string) => {
|
|
|
|
if (history.messages[messageId]) {
|
|
|
|
history.messages[messageId].deleted = true;
|
2024-02-21 00:12:06 +01:00
|
|
|
|
2024-02-21 04:47:30 +01:00
|
|
|
for (const childId of history.messages[messageId].childrenIds) {
|
|
|
|
await deleteMessageAndDescendants(childId);
|
|
|
|
}
|
2024-02-21 01:35:44 +01:00
|
|
|
}
|
2024-02-21 04:47:30 +01:00
|
|
|
};
|
2024-02-21 01:35:44 +01:00
|
|
|
|
2024-02-21 04:47:30 +01:00
|
|
|
const triggerDeleteMessageRecursive = async (messageId: string) => {
|
|
|
|
await deleteMessageAndDescendants(messageId);
|
2024-02-21 00:12:06 +01:00
|
|
|
await updateChatById(localStorage.token, chatId, { history });
|
|
|
|
await chats.set(await getChatList(localStorage.token));
|
2024-02-20 22:24:22 +01:00
|
|
|
};
|
2023-11-20 02:47:07 +01:00
|
|
|
</script>
|
|
|
|
|
|
|
|
{#if messages.length == 0}
|
2023-12-30 08:03:48 +01:00
|
|
|
<Placeholder models={selectedModels} modelfiles={selectedModelfiles} />
|
2023-11-20 02:47:07 +01:00
|
|
|
{:else}
|
2024-02-16 01:20:46 +01:00
|
|
|
<div class=" pb-10">
|
|
|
|
{#key chatId}
|
|
|
|
{#each messages as message, messageIdx}
|
2024-02-20 22:24:22 +01:00
|
|
|
{#if !message.deleted}
|
|
|
|
<div class=" w-full">
|
|
|
|
<div
|
|
|
|
class="flex flex-col justify-between px-5 mb-3 {$settings?.fullScreenMode ?? null
|
|
|
|
? 'max-w-full'
|
|
|
|
: 'max-w-3xl'} mx-auto rounded-lg group"
|
|
|
|
>
|
|
|
|
{#if message.role === 'user'}
|
|
|
|
<UserMessage
|
2024-02-21 04:47:30 +01:00
|
|
|
on:delete={() => triggerDeleteMessageRecursive(message.id)}
|
2024-02-20 22:24:22 +01:00
|
|
|
user={$user}
|
|
|
|
{message}
|
|
|
|
isFirstMessage={messageIdx === 0}
|
|
|
|
siblings={message.parentId !== null
|
|
|
|
? history.messages[message.parentId]?.childrenIds ?? []
|
|
|
|
: Object.values(history.messages)
|
|
|
|
.filter((message) => message.parentId === null)
|
|
|
|
.map((message) => message.id) ?? []}
|
|
|
|
{confirmEditMessage}
|
|
|
|
{showPreviousMessage}
|
|
|
|
{showNextMessage}
|
|
|
|
{copyToClipboard}
|
|
|
|
/>
|
|
|
|
|
|
|
|
{#if messages.length - 1 === messageIdx && processing !== ''}
|
|
|
|
<div class="flex my-2.5 ml-12 items-center w-fit space-x-2.5">
|
|
|
|
<div class=" dark:text-blue-100">
|
|
|
|
<svg
|
|
|
|
class=" w-4 h-4 translate-y-[0.5px]"
|
|
|
|
fill="currentColor"
|
|
|
|
viewBox="0 0 24 24"
|
|
|
|
xmlns="http://www.w3.org/2000/svg"
|
|
|
|
><style>
|
|
|
|
.spinner_qM83 {
|
|
|
|
animation: spinner_8HQG 1.05s infinite;
|
2024-02-16 01:20:46 +01:00
|
|
|
}
|
2024-02-20 22:24:22 +01:00
|
|
|
.spinner_oXPr {
|
|
|
|
animation-delay: 0.1s;
|
2024-02-16 01:20:46 +01:00
|
|
|
}
|
2024-02-20 22:24:22 +01:00
|
|
|
.spinner_ZTLf {
|
|
|
|
animation-delay: 0.2s;
|
2024-02-16 01:20:46 +01:00
|
|
|
}
|
2024-02-20 22:24:22 +01:00
|
|
|
@keyframes spinner_8HQG {
|
|
|
|
0%,
|
|
|
|
57.14% {
|
|
|
|
animation-timing-function: cubic-bezier(0.33, 0.66, 0.66, 1);
|
|
|
|
transform: translate(0);
|
|
|
|
}
|
|
|
|
28.57% {
|
|
|
|
animation-timing-function: cubic-bezier(0.33, 0, 0.66, 0.33);
|
|
|
|
transform: translateY(-6px);
|
|
|
|
}
|
|
|
|
100% {
|
|
|
|
transform: translate(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</style><circle class="spinner_qM83" cx="4" cy="12" r="2.5" /><circle
|
|
|
|
class="spinner_qM83 spinner_oXPr"
|
|
|
|
cx="12"
|
|
|
|
cy="12"
|
|
|
|
r="2.5"
|
|
|
|
/><circle class="spinner_qM83 spinner_ZTLf" cx="20" cy="12" r="2.5" /></svg
|
|
|
|
>
|
|
|
|
</div>
|
|
|
|
<div class=" text-sm font-medium">
|
|
|
|
{processing}
|
|
|
|
</div>
|
2024-02-16 01:20:46 +01:00
|
|
|
</div>
|
2024-02-20 22:24:22 +01:00
|
|
|
{/if}
|
|
|
|
{:else}
|
|
|
|
<ResponseMessage
|
|
|
|
{message}
|
|
|
|
modelfiles={selectedModelfiles}
|
|
|
|
siblings={history.messages[message.parentId]?.childrenIds ?? []}
|
|
|
|
isLastMessage={messageIdx + 1 === messages.length}
|
|
|
|
{confirmEditResponseMessage}
|
|
|
|
{showPreviousMessage}
|
|
|
|
{showNextMessage}
|
|
|
|
{rateMessage}
|
|
|
|
{copyToClipboard}
|
|
|
|
{continueGeneration}
|
|
|
|
{regenerateResponse}
|
|
|
|
/>
|
2024-02-16 01:20:46 +01:00
|
|
|
{/if}
|
2024-02-20 22:24:22 +01:00
|
|
|
</div>
|
2024-02-16 01:20:46 +01:00
|
|
|
</div>
|
2024-02-20 22:24:22 +01:00
|
|
|
{/if}
|
2024-02-16 01:20:46 +01:00
|
|
|
{/each}
|
2023-12-14 01:21:50 +01:00
|
|
|
|
2024-02-16 01:20:46 +01:00
|
|
|
{#if bottomPadding}
|
|
|
|
<div class=" mb-10" />
|
|
|
|
{/if}
|
|
|
|
{/key}
|
|
|
|
</div>
|
2023-11-20 02:47:07 +01:00
|
|
|
{/if}
|