2024-09-22 22:55:13 +00:00
|
|
|
<script lang="ts">
|
|
|
|
import { toast } from 'svelte-sonner';
|
|
|
|
|
|
|
|
import { tick, getContext, onMount } from 'svelte';
|
|
|
|
const i18n = getContext('i18n');
|
|
|
|
|
|
|
|
import { settings } from '$lib/stores';
|
|
|
|
import { copyToClipboard } from '$lib/utils';
|
|
|
|
|
|
|
|
import MultiResponseMessages from './MultiResponseMessages.svelte';
|
|
|
|
import ResponseMessage from './ResponseMessage.svelte';
|
|
|
|
import UserMessage from './UserMessage.svelte';
|
2024-09-22 23:36:46 +00:00
|
|
|
import { updateChatById } from '$lib/apis/chats';
|
|
|
|
|
|
|
|
// h here refers to the height of the message graph
|
|
|
|
export let h;
|
2024-09-22 22:55:13 +00:00
|
|
|
|
|
|
|
export let chatId;
|
|
|
|
|
|
|
|
export let history;
|
|
|
|
export let messageId;
|
|
|
|
|
|
|
|
export let user;
|
|
|
|
|
|
|
|
export let scrollToBottom;
|
|
|
|
export let chatActionHandler;
|
|
|
|
|
|
|
|
export let showPreviousMessage;
|
|
|
|
export let showNextMessage;
|
|
|
|
|
2024-09-22 23:36:46 +00:00
|
|
|
export let editMessage;
|
2024-09-22 22:55:13 +00:00
|
|
|
export let deleteMessage;
|
2024-09-22 23:36:46 +00:00
|
|
|
export let rateMessage;
|
2024-09-22 22:55:13 +00:00
|
|
|
|
|
|
|
export let regenerateResponse;
|
2024-09-22 23:36:46 +00:00
|
|
|
export let continueResponse;
|
2024-09-22 22:55:13 +00:00
|
|
|
|
|
|
|
// MultiResponseMessages
|
|
|
|
export let mergeResponses;
|
|
|
|
|
|
|
|
export let readOnly = false;
|
|
|
|
|
|
|
|
const copyToClipboardWithToast = async (text) => {
|
|
|
|
const res = await copyToClipboard(text);
|
|
|
|
if (res) {
|
|
|
|
toast.success($i18n.t('Copying to clipboard was successful!'));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
</script>
|
|
|
|
|
2024-09-22 23:36:46 +00:00
|
|
|
<div
|
|
|
|
class="flex flex-col justify-between px-5 mb-3 w-full {($settings?.widescreenMode ?? null)
|
|
|
|
? 'max-w-full'
|
|
|
|
: 'max-w-5xl'} mx-auto rounded-lg group"
|
|
|
|
>
|
|
|
|
{#if history.messages[messageId]}
|
|
|
|
{@const message = history.messages[messageId]}
|
|
|
|
{#if message.role === 'user'}
|
2024-09-22 22:55:13 +00:00
|
|
|
<UserMessage
|
|
|
|
{user}
|
2024-09-22 23:36:46 +00:00
|
|
|
{message}
|
|
|
|
isFirstMessage={h === 0}
|
2024-09-22 22:55:13 +00:00
|
|
|
siblings={message.parentId !== null
|
|
|
|
? (history.messages[message.parentId]?.childrenIds ?? [])
|
|
|
|
: (Object.values(history.messages)
|
|
|
|
.filter((message) => message.parentId === null)
|
|
|
|
.map((message) => message.id) ?? [])}
|
|
|
|
{showPreviousMessage}
|
|
|
|
{showNextMessage}
|
2024-09-22 23:36:46 +00:00
|
|
|
{editMessage}
|
2024-09-22 22:55:13 +00:00
|
|
|
on:delete={() => deleteMessage(message.id)}
|
|
|
|
{readOnly}
|
|
|
|
/>
|
|
|
|
{:else if (history.messages[message.parentId]?.models?.length ?? 1) === 1}
|
|
|
|
<ResponseMessage
|
|
|
|
{message}
|
2024-09-22 23:36:46 +00:00
|
|
|
isLastMessage={messageId === history.currentId}
|
2024-09-22 22:55:13 +00:00
|
|
|
siblings={history.messages[message.parentId]?.childrenIds ?? []}
|
|
|
|
{showPreviousMessage}
|
|
|
|
{showNextMessage}
|
2024-09-22 23:36:46 +00:00
|
|
|
{editMessage}
|
2024-09-22 22:55:13 +00:00
|
|
|
{rateMessage}
|
2024-09-22 23:36:46 +00:00
|
|
|
{continueResponse}
|
2024-09-22 22:55:13 +00:00
|
|
|
{regenerateResponse}
|
|
|
|
on:action={async (e) => {
|
|
|
|
console.log('action', 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);
|
|
|
|
}
|
|
|
|
}}
|
2024-09-22 23:36:46 +00:00
|
|
|
on:update={async (e) => {
|
|
|
|
console.log('update', e);
|
|
|
|
// call updateChatHistory
|
|
|
|
}}
|
2024-09-22 22:55:13 +00:00
|
|
|
on:save={async (e) => {
|
|
|
|
console.log('save', e);
|
|
|
|
|
|
|
|
const message = e.detail;
|
|
|
|
if (message) {
|
|
|
|
history.messages[message.id] = message;
|
|
|
|
await updateChatById(localStorage.token, chatId, {
|
|
|
|
history: history
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
await updateChatById(localStorage.token, chatId, {
|
|
|
|
history: history
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}}
|
2024-09-22 23:36:46 +00:00
|
|
|
{readOnly}
|
2024-09-22 22:55:13 +00:00
|
|
|
/>
|
|
|
|
{:else}
|
|
|
|
<MultiResponseMessages
|
|
|
|
bind:history
|
|
|
|
{chatId}
|
|
|
|
messageIds={[]}
|
|
|
|
parentMessage={history.messages[message.parentId]}
|
2024-09-22 23:36:46 +00:00
|
|
|
isLastMessage={messageId === history.currentId}
|
|
|
|
{editMessage}
|
2024-09-22 22:55:13 +00:00
|
|
|
{rateMessage}
|
2024-09-22 23:36:46 +00:00
|
|
|
{continueResponse}
|
2024-09-22 22:55:13 +00:00
|
|
|
{mergeResponses}
|
|
|
|
{regenerateResponse}
|
2024-09-22 23:36:46 +00:00
|
|
|
copyToClipboard={copyToClipboardWithToast}
|
|
|
|
{readOnly}
|
2024-09-22 22:55:13 +00:00
|
|
|
on:action={async (e) => {
|
|
|
|
console.log('action', 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:change={async () => {
|
|
|
|
await updateChatById(localStorage.token, chatId, {
|
|
|
|
history: history
|
|
|
|
});
|
|
|
|
|
|
|
|
if (autoScroll) {
|
|
|
|
const element = document.getElementById('messages-container');
|
|
|
|
autoScroll = element.scrollHeight - element.scrollTop <= element.clientHeight + 50;
|
|
|
|
setTimeout(() => {
|
|
|
|
scrollToBottom();
|
|
|
|
}, 100);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
{/if}
|
2024-09-22 23:36:46 +00:00
|
|
|
{/if}
|
2024-09-22 22:55:13 +00:00
|
|
|
</div>
|
2024-09-22 23:36:46 +00:00
|
|
|
|
|
|
|
{#if history.messages[messageId]?.parentId !== null}
|
|
|
|
<svelte:self
|
|
|
|
h={h + 1}
|
|
|
|
{chatId}
|
|
|
|
{history}
|
|
|
|
messageId={history.messages[messageId]?.parentId}
|
|
|
|
{user}
|
|
|
|
{scrollToBottom}
|
|
|
|
{chatActionHandler}
|
|
|
|
{showPreviousMessage}
|
|
|
|
{showNextMessage}
|
|
|
|
{editMessage}
|
|
|
|
{deleteMessage}
|
|
|
|
{rateMessage}
|
|
|
|
{regenerateResponse}
|
|
|
|
{continueResponse}
|
|
|
|
{mergeResponses}
|
|
|
|
{readOnly}
|
|
|
|
></svelte:self>
|
|
|
|
{/if}
|