open-webui/src/lib/components/chat/Messages/Message.svelte

163 lines
4.2 KiB
Svelte
Raw Normal View History

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';
2024-09-22 22:55:13 +00:00
export let chatId;
2024-09-23 12:24:50 +00:00
export let idx = 0;
2024-09-22 22:55:13 +00:00
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;
2024-09-23 12:24:50 +00:00
let message = JSON.parse(JSON.stringify(history.messages[messageId]));
$: if (history.messages) {
if (JSON.stringify(message) !== JSON.stringify(history.messages[messageId])) {
message = JSON.parse(JSON.stringify(history.messages[messageId]));
}
}
2024-09-22 22:55:13 +00:00
const copyToClipboardWithToast = async (text) => {
const res = await copyToClipboard(text);
if (res) {
toast.success($i18n.t('Copying to clipboard was successful!'));
}
};
2024-09-23 12:24:50 +00:00
onMount(() => {
console.log('message', idx);
});
2024-09-22 22:55:13 +00:00
</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"
>
2024-09-23 12:24:50 +00:00
{#if message}
2024-09-22 23:36:46 +00:00
{#if message.role === 'user'}
2024-09-22 22:55:13 +00:00
<UserMessage
{user}
2024-09-22 23:36:46 +00:00
{message}
2024-09-23 12:24:50 +00:00
isFirstMessage={idx === 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>